blob: 1ed918c635178253c93b8172fc4f8554396cf38a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masone20d96d2007-03-22 12:13:20 -040019#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -040020#include <linux/blkdev.h>
Chris Mason87cbda52007-03-28 19:44:27 -040021#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -040022#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -040023#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -040024#include <linux/writeback.h>
Chris Masond3977122009-01-05 21:25:51 -050025#include <linux/buffer_head.h>
Chris Masonce9adaa2008-04-09 16:28:12 -040026#include <linux/workqueue.h>
Chris Masona74a4b92008-06-25 16:01:31 -040027#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Chris Mason784b4e22010-11-21 22:20:49 -050029#include <linux/migrate.h>
David Sterba7a36dde2011-05-06 15:33:15 +020030#include <linux/ratelimit.h>
Stefan Behrens6463fe52013-04-19 15:08:05 +000031#include <linux/uuid.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020032#include <linux/semaphore.h>
David Sterba7e75bf32011-03-18 22:56:43 +000033#include <asm/unaligned.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050034#include "ctree.h"
35#include "disk-io.h"
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +000036#include "hash.h"
Chris Masone089f052007-03-16 16:20:31 -040037#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040038#include "btrfs_inode.h"
Chris Mason0b86a832008-03-24 15:01:56 -040039#include "volumes.h"
Chris Masondb945352007-10-15 16:15:53 -040040#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040041#include "locking.h"
Chris Masone02119d2008-09-05 16:13:11 -040042#include "tree-log.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040043#include "free-space-cache.h"
Omar Sandoval70f6d822015-09-29 20:50:38 -070044#include "free-space-tree.h"
Li Zefan581bb052011-04-20 10:06:11 +080045#include "inode-map.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010046#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040047#include "rcu-string.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010048#include "dev-replace.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050049#include "raid56.h"
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040050#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070051#include "qgroup.h"
Anand Jainebb87652016-03-10 17:26:59 +080052#include "compression.h"
Qu Wenruo90eb4c02017-10-09 01:51:02 +000053#include "tree-checker.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050054
Josef Bacikde0022b2012-09-25 14:25:58 -040055#ifdef CONFIG_X86
56#include <asm/cpufeature.h>
57#endif
58
Qu Wenruo319e4d02015-12-15 09:14:36 +080059#define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
60 BTRFS_HEADER_FLAG_RELOC |\
61 BTRFS_SUPER_FLAG_ERROR |\
62 BTRFS_SUPER_FLAG_SEEDING |\
Anand Jain8d99eb42018-01-09 09:05:41 +080063 BTRFS_SUPER_FLAG_METADUMP |\
64 BTRFS_SUPER_FLAG_METADUMP_V2)
Qu Wenruo319e4d02015-12-15 09:14:36 +080065
David Sterbae8c9f182015-01-02 18:23:10 +010066static const struct extent_io_ops btree_extent_io_ops;
Chris Mason8b712842008-06-11 16:50:36 -040067static void end_workqueue_fn(struct btrfs_work *work);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040068static void free_fs_root(struct btrfs_root *root);
David Sterbafcd1f0652012-03-06 00:06:18 +010069static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
liuboacce9522011-01-06 19:30:25 +080070 int read_only);
Jeff Mahoney143bede2012-03-01 14:56:26 +010071static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
liuboacce9522011-01-06 19:30:25 +080072static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
73 struct btrfs_root *root);
Jeff Mahoney143bede2012-03-01 14:56:26 +010074static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
liuboacce9522011-01-06 19:30:25 +080075static int btrfs_destroy_marked_extents(struct btrfs_root *root,
76 struct extent_io_tree *dirty_pages,
77 int mark);
78static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
79 struct extent_io_tree *pinned_extents);
Eric Sandeen48a3b632013-04-25 20:41:01 +000080static int btrfs_cleanup_transaction(struct btrfs_root *root);
81static void btrfs_error_commit_super(struct btrfs_root *root);
Chris Masonce9adaa2008-04-09 16:28:12 -040082
Chris Masond352ac62008-09-29 15:18:18 -040083/*
David Sterba97eb6b62014-07-30 00:55:42 +020084 * btrfs_end_io_wq structs are used to do processing in task context when an IO
85 * is complete. This is used during reads to verify checksums, and it is used
Chris Masond352ac62008-09-29 15:18:18 -040086 * by writes to insert metadata for new file extents after IO is complete.
87 */
David Sterba97eb6b62014-07-30 00:55:42 +020088struct btrfs_end_io_wq {
Chris Masonce9adaa2008-04-09 16:28:12 -040089 struct bio *bio;
90 bio_end_io_t *end_io;
91 void *private;
92 struct btrfs_fs_info *info;
93 int error;
David Sterbabfebd8b2014-07-30 00:25:45 +020094 enum btrfs_wq_endio_type metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -040095 struct list_head list;
Chris Mason8b712842008-06-11 16:50:36 -040096 struct btrfs_work work;
Chris Masonce9adaa2008-04-09 16:28:12 -040097};
Chris Mason0da54682007-11-07 21:08:01 -050098
David Sterba97eb6b62014-07-30 00:55:42 +020099static struct kmem_cache *btrfs_end_io_wq_cache;
100
101int __init btrfs_end_io_wq_init(void)
102{
103 btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq",
104 sizeof(struct btrfs_end_io_wq),
105 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300106 SLAB_MEM_SPREAD,
David Sterba97eb6b62014-07-30 00:55:42 +0200107 NULL);
108 if (!btrfs_end_io_wq_cache)
109 return -ENOMEM;
110 return 0;
111}
112
113void btrfs_end_io_wq_exit(void)
114{
Kinglong Mee5598e902016-01-29 21:36:35 +0800115 kmem_cache_destroy(btrfs_end_io_wq_cache);
David Sterba97eb6b62014-07-30 00:55:42 +0200116}
117
Chris Masond352ac62008-09-29 15:18:18 -0400118/*
119 * async submit bios are used to offload expensive checksumming
120 * onto the worker threads. They checksum file and metadata bios
121 * just before they are sent down the IO stack.
122 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400123struct async_submit_bio {
124 struct inode *inode;
125 struct bio *bio;
126 struct list_head list;
Chris Mason4a69a412008-11-06 22:03:00 -0500127 extent_submit_bio_hook_t *submit_bio_start;
128 extent_submit_bio_hook_t *submit_bio_done;
Chris Mason44b8bd72008-04-16 11:14:51 -0400129 int mirror_num;
Chris Masonc8b97812008-10-29 14:49:59 -0400130 unsigned long bio_flags;
Chris Masoneaf25d92010-05-25 09:48:28 -0400131 /*
132 * bio_offset is optional, can be used if the pages in the bio
133 * can't tell us where in the file the bio should go
134 */
135 u64 bio_offset;
Chris Mason8b712842008-06-11 16:50:36 -0400136 struct btrfs_work work;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100137 int error;
Chris Mason44b8bd72008-04-16 11:14:51 -0400138};
139
Chris Mason85d4e462011-07-26 16:11:19 -0400140/*
141 * Lockdep class keys for extent_buffer->lock's in this root. For a given
142 * eb, the lockdep key is determined by the btrfs_root it belongs to and
143 * the level the eb occupies in the tree.
Chris Mason4008c042009-02-12 14:09:45 -0500144 *
Chris Mason85d4e462011-07-26 16:11:19 -0400145 * Different roots are used for different purposes and may nest inside each
146 * other and they require separate keysets. As lockdep keys should be
147 * static, assign keysets according to the purpose of the root as indicated
148 * by btrfs_root->objectid. This ensures that all special purpose roots
149 * have separate keysets.
Chris Mason4008c042009-02-12 14:09:45 -0500150 *
Chris Mason85d4e462011-07-26 16:11:19 -0400151 * Lock-nesting across peer nodes is always done with the immediate parent
152 * node locked thus preventing deadlock. As lockdep doesn't know this, use
153 * subclass to avoid triggering lockdep warning in such cases.
Chris Mason4008c042009-02-12 14:09:45 -0500154 *
Chris Mason85d4e462011-07-26 16:11:19 -0400155 * The key is set by the readpage_end_io_hook after the buffer has passed
156 * csum validation but before the pages are unlocked. It is also set by
157 * btrfs_init_new_buffer on freshly allocated blocks.
158 *
159 * We also add a check to make sure the highest level of the tree is the
160 * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
161 * needs update as well.
Chris Mason4008c042009-02-12 14:09:45 -0500162 */
163#ifdef CONFIG_DEBUG_LOCK_ALLOC
164# if BTRFS_MAX_LEVEL != 8
165# error
166# endif
Chris Mason85d4e462011-07-26 16:11:19 -0400167
168static struct btrfs_lockdep_keyset {
169 u64 id; /* root objectid */
170 const char *name_stem; /* lock name stem */
171 char names[BTRFS_MAX_LEVEL + 1][20];
172 struct lock_class_key keys[BTRFS_MAX_LEVEL + 1];
173} btrfs_lockdep_keysets[] = {
174 { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" },
175 { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" },
176 { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" },
177 { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" },
178 { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" },
179 { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" },
David Sterba60b62972013-04-30 17:29:29 +0000180 { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" },
Chris Mason85d4e462011-07-26 16:11:19 -0400181 { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" },
182 { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" },
183 { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" },
David Sterba13fd8da2013-09-03 18:28:57 +0200184 { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" },
David Sterba6b20e0a2016-01-25 16:30:22 +0100185 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" },
Chris Mason85d4e462011-07-26 16:11:19 -0400186 { .id = 0, .name_stem = "tree" },
Chris Mason4008c042009-02-12 14:09:45 -0500187};
Chris Mason85d4e462011-07-26 16:11:19 -0400188
189void __init btrfs_init_lockdep(void)
190{
191 int i, j;
192
193 /* initialize lockdep class names */
194 for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) {
195 struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i];
196
197 for (j = 0; j < ARRAY_SIZE(ks->names); j++)
198 snprintf(ks->names[j], sizeof(ks->names[j]),
199 "btrfs-%s-%02d", ks->name_stem, j);
200 }
201}
202
203void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb,
204 int level)
205{
206 struct btrfs_lockdep_keyset *ks;
207
208 BUG_ON(level >= ARRAY_SIZE(ks->keys));
209
210 /* find the matching keyset, id 0 is the default entry */
211 for (ks = btrfs_lockdep_keysets; ks->id; ks++)
212 if (ks->id == objectid)
213 break;
214
215 lockdep_set_class_and_name(&eb->lock,
216 &ks->keys[level], ks->names[level]);
217}
218
Chris Mason4008c042009-02-12 14:09:45 -0500219#endif
220
Chris Masond352ac62008-09-29 15:18:18 -0400221/*
222 * extents on the btree inode are pretty simple, there's one extent
223 * that covers the entire device
224 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500225static struct extent_map *btree_get_extent(struct inode *inode,
David Sterba306e16c2011-04-19 14:29:38 +0200226 struct page *page, size_t pg_offset, u64 start, u64 len,
Christoph Hellwigb2950862008-12-02 09:54:17 -0500227 int create)
Chris Mason5f39d392007-10-15 16:14:19 -0400228{
229 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
230 struct extent_map *em;
231 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400232
Chris Mason890871b2009-09-02 16:24:52 -0400233 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500234 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -0400235 if (em) {
236 em->bdev =
237 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -0400238 read_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -0400239 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400240 }
Chris Mason890871b2009-09-02 16:24:52 -0400241 read_unlock(&em_tree->lock);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400242
David Sterba172ddd62011-04-21 00:48:27 +0200243 em = alloc_extent_map();
Chris Mason5f39d392007-10-15 16:14:19 -0400244 if (!em) {
245 em = ERR_PTR(-ENOMEM);
246 goto out;
247 }
248 em->start = 0;
Chris Mason0afbaf82008-04-18 14:17:20 -0400249 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -0400250 em->block_len = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400251 em->block_start = 0;
Chris Masona061fc82008-05-07 11:43:44 -0400252 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -0500253
Chris Mason890871b2009-09-02 16:24:52 -0400254 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400255 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400256 if (ret == -EEXIST) {
257 free_extent_map(em);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400258 em = lookup_extent_mapping(em_tree, start, len);
Tsutomu Itohb4f359a2012-09-13 03:32:54 -0600259 if (!em)
Tsutomu Itoh0433f202012-09-13 03:32:32 -0600260 em = ERR_PTR(-EIO);
Chris Mason5f39d392007-10-15 16:14:19 -0400261 } else if (ret) {
Chris Mason7b13b7b2008-04-18 10:29:50 -0400262 free_extent_map(em);
Tsutomu Itoh0433f202012-09-13 03:32:32 -0600263 em = ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400264 }
Chris Mason890871b2009-09-02 16:24:52 -0400265 write_unlock(&em_tree->lock);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400266
Chris Mason5f39d392007-10-15 16:14:19 -0400267out:
268 return em;
269}
270
Liu Bob0496682013-03-14 14:57:45 +0000271u32 btrfs_csum_data(char *data, u32 seed, size_t len)
Chris Mason19c00dd2007-10-15 16:19:22 -0400272{
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000273 return btrfs_crc32c(seed, data, len);
Chris Mason19c00dd2007-10-15 16:19:22 -0400274}
275
276void btrfs_csum_final(u32 crc, char *result)
277{
David Sterba7e75bf32011-03-18 22:56:43 +0000278 put_unaligned_le32(~crc, result);
Chris Mason19c00dd2007-10-15 16:19:22 -0400279}
280
Chris Masond352ac62008-09-29 15:18:18 -0400281/*
282 * compute the csum for a btree block, and either verify it or write it
283 * into the csum field of the block.
284 */
Daniel Dressler01d58472014-11-21 17:15:07 +0900285static int csum_tree_block(struct btrfs_fs_info *fs_info,
286 struct extent_buffer *buf,
Chris Mason19c00dd2007-10-15 16:19:22 -0400287 int verify)
288{
Daniel Dressler01d58472014-11-21 17:15:07 +0900289 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Josef Bacik607d4322008-12-02 07:17:45 -0500290 char *result = NULL;
Chris Mason19c00dd2007-10-15 16:19:22 -0400291 unsigned long len;
292 unsigned long cur_len;
293 unsigned long offset = BTRFS_CSUM_SIZE;
Chris Mason19c00dd2007-10-15 16:19:22 -0400294 char *kaddr;
295 unsigned long map_start;
296 unsigned long map_len;
297 int err;
298 u32 crc = ~(u32)0;
Josef Bacik607d4322008-12-02 07:17:45 -0500299 unsigned long inline_result;
Chris Mason19c00dd2007-10-15 16:19:22 -0400300
301 len = buf->len - offset;
Chris Masond3977122009-01-05 21:25:51 -0500302 while (len > 0) {
Chris Mason19c00dd2007-10-15 16:19:22 -0400303 err = map_private_extent_buffer(buf, offset, 32,
Chris Masona6591712011-07-19 12:04:14 -0400304 &kaddr, &map_start, &map_len);
Chris Masond3977122009-01-05 21:25:51 -0500305 if (err)
Alex Lyakas8bd98f02016-03-10 13:09:46 +0200306 return err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400307 cur_len = min(len, map_len - (offset - map_start));
Liu Bob0496682013-03-14 14:57:45 +0000308 crc = btrfs_csum_data(kaddr + offset - map_start,
Chris Mason19c00dd2007-10-15 16:19:22 -0400309 crc, cur_len);
310 len -= cur_len;
311 offset += cur_len;
Chris Mason19c00dd2007-10-15 16:19:22 -0400312 }
Josef Bacik607d4322008-12-02 07:17:45 -0500313 if (csum_size > sizeof(inline_result)) {
David Sterba31e818f2015-02-20 18:00:26 +0100314 result = kzalloc(csum_size, GFP_NOFS);
Josef Bacik607d4322008-12-02 07:17:45 -0500315 if (!result)
Alex Lyakas8bd98f02016-03-10 13:09:46 +0200316 return -ENOMEM;
Josef Bacik607d4322008-12-02 07:17:45 -0500317 } else {
318 result = (char *)&inline_result;
319 }
320
Chris Mason19c00dd2007-10-15 16:19:22 -0400321 btrfs_csum_final(crc, result);
322
323 if (verify) {
Josef Bacik607d4322008-12-02 07:17:45 -0500324 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
Chris Masone4204de2008-01-08 15:46:27 -0500325 u32 val;
326 u32 found = 0;
Josef Bacik607d4322008-12-02 07:17:45 -0500327 memcpy(&found, result, csum_size);
Chris Masone4204de2008-01-08 15:46:27 -0500328
Josef Bacik607d4322008-12-02 07:17:45 -0500329 read_extent_buffer(buf, &val, 0, csum_size);
David Sterba94647322015-10-08 11:01:36 +0200330 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400331 "%s checksum verify failed on %llu wanted %X found %X level %d",
Daniel Dressler01d58472014-11-21 17:15:07 +0900332 fs_info->sb->s_id, buf->start,
Frank Holtonefe120a2013-12-20 11:37:06 -0500333 val, found, btrfs_header_level(buf));
Josef Bacik607d4322008-12-02 07:17:45 -0500334 if (result != (char *)&inline_result)
335 kfree(result);
Alex Lyakas8bd98f02016-03-10 13:09:46 +0200336 return -EUCLEAN;
Chris Mason19c00dd2007-10-15 16:19:22 -0400337 }
338 } else {
Josef Bacik607d4322008-12-02 07:17:45 -0500339 write_extent_buffer(buf, result, 0, csum_size);
Chris Mason19c00dd2007-10-15 16:19:22 -0400340 }
Josef Bacik607d4322008-12-02 07:17:45 -0500341 if (result != (char *)&inline_result)
342 kfree(result);
Chris Mason19c00dd2007-10-15 16:19:22 -0400343 return 0;
344}
345
Chris Masond352ac62008-09-29 15:18:18 -0400346/*
347 * we can't consider a given block up to date unless the transid of the
348 * block matches the transid in the parent node's pointer. This is how we
349 * detect blocks that either didn't get written at all or got written
350 * in the wrong place.
351 */
Chris Mason1259ab72008-05-12 13:39:03 -0400352static int verify_parent_transid(struct extent_io_tree *io_tree,
Chris Masonb9fab912012-05-06 07:23:47 -0400353 struct extent_buffer *eb, u64 parent_transid,
354 int atomic)
Chris Mason1259ab72008-05-12 13:39:03 -0400355{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000356 struct extent_state *cached_state = NULL;
Chris Mason1259ab72008-05-12 13:39:03 -0400357 int ret;
David Sterba2755a0d2014-07-31 00:43:18 +0200358 bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB);
Chris Mason1259ab72008-05-12 13:39:03 -0400359
360 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
361 return 0;
362
Chris Masonb9fab912012-05-06 07:23:47 -0400363 if (atomic)
364 return -EAGAIN;
365
Josef Bacika26e8c92014-03-28 17:07:27 -0400366 if (need_lock) {
367 btrfs_tree_read_lock(eb);
368 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
369 }
370
Josef Bacik2ac55d42010-02-03 19:33:23 +0000371 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +0100372 &cached_state);
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400373 if (extent_buffer_uptodate(eb) &&
Chris Mason1259ab72008-05-12 13:39:03 -0400374 btrfs_header_generation(eb) == parent_transid) {
375 ret = 0;
376 goto out;
377 }
David Sterba94647322015-10-08 11:01:36 +0200378 btrfs_err_rl(eb->fs_info,
379 "parent transid verify failed on %llu wanted %llu found %llu",
380 eb->start,
Wang Shilong29549ae2014-07-04 17:59:06 +0800381 parent_transid, btrfs_header_generation(eb));
Chris Mason1259ab72008-05-12 13:39:03 -0400382 ret = 1;
Josef Bacika26e8c92014-03-28 17:07:27 -0400383
384 /*
385 * Things reading via commit roots that don't have normal protection,
386 * like send, can have a really old block in cache that may point at a
Nicholas D Steeves01327612016-05-19 21:18:45 -0400387 * block that has been freed and re-allocated. So don't clear uptodate
Josef Bacika26e8c92014-03-28 17:07:27 -0400388 * if we find an eb that is under IO (dirty/writeback) because we could
389 * end up reading in the stale data and then writing it back out and
390 * making everybody very sad.
391 */
392 if (!extent_buffer_under_io(eb))
393 clear_extent_buffer_uptodate(eb);
Chris Mason33958dc2008-07-30 10:29:12 -0400394out:
Josef Bacik2ac55d42010-02-03 19:33:23 +0000395 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
396 &cached_state, GFP_NOFS);
Josef Bacik472b9092014-06-25 13:45:41 -0700397 if (need_lock)
398 btrfs_tree_read_unlock_blocking(eb);
Chris Mason1259ab72008-05-12 13:39:03 -0400399 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400400}
401
Chris Masond352ac62008-09-29 15:18:18 -0400402/*
David Sterba1104a882013-03-06 15:57:46 +0100403 * Return 0 if the superblock checksum type matches the checksum value of that
404 * algorithm. Pass the raw disk superblock data.
405 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400406static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
407 char *raw_disk_sb)
David Sterba1104a882013-03-06 15:57:46 +0100408{
409 struct btrfs_super_block *disk_sb =
410 (struct btrfs_super_block *)raw_disk_sb;
411 u16 csum_type = btrfs_super_csum_type(disk_sb);
412 int ret = 0;
413
414 if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
415 u32 crc = ~(u32)0;
416 const int csum_size = sizeof(crc);
417 char result[csum_size];
418
419 /*
420 * The super_block structure does not span the whole
421 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
Nicholas D Steeves01327612016-05-19 21:18:45 -0400422 * is filled with zeros and is included in the checksum.
David Sterba1104a882013-03-06 15:57:46 +0100423 */
424 crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
425 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
426 btrfs_csum_final(crc, result);
427
428 if (memcmp(raw_disk_sb, result, csum_size))
429 ret = 1;
430 }
431
432 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400433 btrfs_err(fs_info, "unsupported checksum algorithm %u",
David Sterba1104a882013-03-06 15:57:46 +0100434 csum_type);
435 ret = 1;
436 }
437
438 return ret;
439}
440
441/*
Chris Masond352ac62008-09-29 15:18:18 -0400442 * helper to read a given tree block, doing retries as required when
443 * the checksums don't match and we have alternate mirrors to try.
444 */
Chris Masonf1885912008-04-09 16:28:12 -0400445static int btree_read_extent_buffer_pages(struct btrfs_root *root,
446 struct extent_buffer *eb,
Josef Bacik8436ea912016-09-02 15:40:03 -0400447 u64 parent_transid)
Chris Masonf1885912008-04-09 16:28:12 -0400448{
449 struct extent_io_tree *io_tree;
Josef Bacikea466792012-03-26 21:57:36 -0400450 int failed = 0;
Chris Masonf1885912008-04-09 16:28:12 -0400451 int ret;
452 int num_copies = 0;
453 int mirror_num = 0;
Josef Bacikea466792012-03-26 21:57:36 -0400454 int failed_mirror = 0;
Chris Masonf1885912008-04-09 16:28:12 -0400455
456 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
457 while (1) {
Nikolay Borisovc50ccf22018-11-06 16:40:20 +0200458 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
Josef Bacik8436ea912016-09-02 15:40:03 -0400459 ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE,
Chris Masonf1885912008-04-09 16:28:12 -0400460 btree_get_extent, mirror_num);
Stefan Behrens256dd1b2012-08-10 08:58:21 -0600461 if (!ret) {
462 if (!verify_parent_transid(io_tree, eb,
Chris Masonb9fab912012-05-06 07:23:47 -0400463 parent_transid, 0))
Stefan Behrens256dd1b2012-08-10 08:58:21 -0600464 break;
465 else
466 ret = -EIO;
467 }
Chris Masond3977122009-01-05 21:25:51 -0500468
Stefan Behrens5d964052012-11-05 14:59:07 +0100469 num_copies = btrfs_num_copies(root->fs_info,
Chris Masonf1885912008-04-09 16:28:12 -0400470 eb->start, eb->len);
Chris Mason42352982008-04-28 16:40:52 -0400471 if (num_copies == 1)
Josef Bacikea466792012-03-26 21:57:36 -0400472 break;
Chris Mason42352982008-04-28 16:40:52 -0400473
Josef Bacik5cf1ab52012-04-16 09:42:26 -0400474 if (!failed_mirror) {
475 failed = 1;
476 failed_mirror = eb->read_mirror;
477 }
478
Chris Masonf1885912008-04-09 16:28:12 -0400479 mirror_num++;
Josef Bacikea466792012-03-26 21:57:36 -0400480 if (mirror_num == failed_mirror)
481 mirror_num++;
482
Chris Mason42352982008-04-28 16:40:52 -0400483 if (mirror_num > num_copies)
Josef Bacikea466792012-03-26 21:57:36 -0400484 break;
Chris Masonf1885912008-04-09 16:28:12 -0400485 }
Josef Bacikea466792012-03-26 21:57:36 -0400486
Stefan Behrensc0901582012-07-10 07:30:17 -0600487 if (failed && !ret && failed_mirror)
Josef Bacikea466792012-03-26 21:57:36 -0400488 repair_eb_io_failure(root, eb, failed_mirror);
489
490 return ret;
Chris Masonf1885912008-04-09 16:28:12 -0400491}
Chris Mason19c00dd2007-10-15 16:19:22 -0400492
Chris Masond352ac62008-09-29 15:18:18 -0400493/*
Chris Masond3977122009-01-05 21:25:51 -0500494 * checksum a dirty tree block before IO. This has extra checks to make sure
495 * we only fill in the checksum field in the first page of a multi-page block
Chris Masond352ac62008-09-29 15:18:18 -0400496 */
Chris Masond3977122009-01-05 21:25:51 -0500497
Daniel Dressler01d58472014-11-21 17:15:07 +0900498static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page)
Chris Mason19c00dd2007-10-15 16:19:22 -0400499{
Miao Xie4eee4fa2012-12-21 09:17:45 +0000500 u64 start = page_offset(page);
Chris Mason19c00dd2007-10-15 16:19:22 -0400501 u64 found_start;
Chris Mason19c00dd2007-10-15 16:19:22 -0400502 struct extent_buffer *eb;
Chris Masonf1885912008-04-09 16:28:12 -0400503
Josef Bacik4f2de97a2012-03-07 16:20:05 -0500504 eb = (struct extent_buffer *)page->private;
505 if (page != eb->pages[0])
506 return 0;
Alex Lyakas0f805532016-03-10 13:10:15 +0200507
Chris Mason19c00dd2007-10-15 16:19:22 -0400508 found_start = btrfs_header_bytenr(eb);
Alex Lyakas0f805532016-03-10 13:10:15 +0200509 /*
510 * Please do not consolidate these warnings into a single if.
511 * It is useful to know what went wrong.
512 */
513 if (WARN_ON(found_start != start))
514 return -EUCLEAN;
515 if (WARN_ON(!PageUptodate(page)))
516 return -EUCLEAN;
517
518 ASSERT(memcmp_extent_buffer(eb, fs_info->fsid,
519 btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0);
520
Alex Lyakas8bd98f02016-03-10 13:09:46 +0200521 return csum_tree_block(fs_info, eb, 0);
Chris Mason19c00dd2007-10-15 16:19:22 -0400522}
523
Daniel Dressler01d58472014-11-21 17:15:07 +0900524static int check_tree_block_fsid(struct btrfs_fs_info *fs_info,
Yan Zheng2b820322008-11-17 21:11:30 -0500525 struct extent_buffer *eb)
526{
Daniel Dressler01d58472014-11-21 17:15:07 +0900527 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -0500528 u8 fsid[BTRFS_UUID_SIZE];
529 int ret = 1;
530
Ross Kirk0a4e5582013-09-24 10:12:38 +0100531 read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500532 while (fs_devices) {
533 if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
534 ret = 0;
535 break;
536 }
537 fs_devices = fs_devices->seed;
538 }
539 return ret;
540}
541
Miao Xiefacc8a222013-07-25 19:22:34 +0800542static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
543 u64 phy_offset, struct page *page,
544 u64 start, u64 end, int mirror)
Chris Masonce9adaa2008-04-09 16:28:12 -0400545{
Chris Masonce9adaa2008-04-09 16:28:12 -0400546 u64 found_start;
547 int found_level;
Chris Masonce9adaa2008-04-09 16:28:12 -0400548 struct extent_buffer *eb;
549 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Zhao Lei02873e42015-12-31 22:46:45 +0800550 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonf1885912008-04-09 16:28:12 -0400551 int ret = 0;
Chris Mason727011e2010-08-06 13:21:20 -0400552 int reads_done;
Chris Masonce9adaa2008-04-09 16:28:12 -0400553
Chris Masonce9adaa2008-04-09 16:28:12 -0400554 if (!page->private)
555 goto out;
Chris Masond3977122009-01-05 21:25:51 -0500556
Josef Bacik4f2de97a2012-03-07 16:20:05 -0500557 eb = (struct extent_buffer *)page->private;
Chris Masond3977122009-01-05 21:25:51 -0500558
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400559 /* the pending IO might have been the only thing that kept this buffer
560 * in memory. Make sure we have a ref for all this other checks
561 */
562 extent_buffer_get(eb);
563
564 reads_done = atomic_dec_and_test(&eb->io_pages);
Chris Mason727011e2010-08-06 13:21:20 -0400565 if (!reads_done)
566 goto err;
Chris Masonf1885912008-04-09 16:28:12 -0400567
Josef Bacik5cf1ab52012-04-16 09:42:26 -0400568 eb->read_mirror = mirror;
Filipe Manana656f30d2014-09-26 12:25:56 +0100569 if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
Josef Bacikea466792012-03-26 21:57:36 -0400570 ret = -EIO;
571 goto err;
572 }
573
Chris Masonce9adaa2008-04-09 16:28:12 -0400574 found_start = btrfs_header_bytenr(eb);
Chris Mason727011e2010-08-06 13:21:20 -0400575 if (found_start != eb->start) {
Zhao Lei02873e42015-12-31 22:46:45 +0800576 btrfs_err_rl(fs_info, "bad tree block start %llu %llu",
577 found_start, eb->start);
Chris Masonf1885912008-04-09 16:28:12 -0400578 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400579 goto err;
580 }
Zhao Lei02873e42015-12-31 22:46:45 +0800581 if (check_tree_block_fsid(fs_info, eb)) {
582 btrfs_err_rl(fs_info, "bad fsid on block %llu",
583 eb->start);
Chris Mason1259ab72008-05-12 13:39:03 -0400584 ret = -EIO;
585 goto err;
586 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400587 found_level = btrfs_header_level(eb);
Josef Bacik1c24c3c2013-04-23 11:30:14 -0400588 if (found_level >= BTRFS_MAX_LEVEL) {
Zhao Lei02873e42015-12-31 22:46:45 +0800589 btrfs_err(fs_info, "bad tree block level %d",
590 (int)btrfs_header_level(eb));
Josef Bacik1c24c3c2013-04-23 11:30:14 -0400591 ret = -EIO;
592 goto err;
593 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400594
Chris Mason85d4e462011-07-26 16:11:19 -0400595 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb),
596 eb, found_level);
Chris Mason4008c042009-02-12 14:09:45 -0500597
Zhao Lei02873e42015-12-31 22:46:45 +0800598 ret = csum_tree_block(fs_info, eb, 1);
Alex Lyakas8bd98f02016-03-10 13:09:46 +0200599 if (ret)
Josef Bacika826d6d2011-03-16 13:42:43 -0400600 goto err;
Josef Bacika826d6d2011-03-16 13:42:43 -0400601
602 /*
603 * If this is a leaf block and it is corrupt, set the corrupt bit so
604 * that we don't try and read the other copies of this block, just
605 * return -EIO.
606 */
Qu Wenruof7438e62017-11-08 08:54:24 +0800607 if (found_level == 0 && btrfs_check_leaf_full(root, eb)) {
Josef Bacika826d6d2011-03-16 13:42:43 -0400608 set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
609 ret = -EIO;
610 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400611
Qu Wenruo90eb4c02017-10-09 01:51:02 +0000612 if (found_level > 0 && btrfs_check_node(root, eb))
Liu Bo053ab70f2016-08-23 17:37:45 -0700613 ret = -EIO;
614
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400615 if (!ret)
616 set_extent_buffer_uptodate(eb);
Chris Masonce9adaa2008-04-09 16:28:12 -0400617err:
Josef Bacik79fb65a2013-04-20 10:18:27 -0400618 if (reads_done &&
619 test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
Zhao Lei02873e42015-12-31 22:46:45 +0800620 btree_readahead_hook(fs_info, eb, eb->start, ret);
Arne Jansen4bb31e92011-06-10 13:55:54 +0200621
David Woodhouse53b381b2013-01-29 18:40:14 -0500622 if (ret) {
623 /*
624 * our io error hook is going to dec the io pages
625 * again, we have to make sure it has something
626 * to decrement
627 */
628 atomic_inc(&eb->io_pages);
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400629 clear_extent_buffer_uptodate(eb);
David Woodhouse53b381b2013-01-29 18:40:14 -0500630 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400631 free_extent_buffer(eb);
Chris Masonce9adaa2008-04-09 16:28:12 -0400632out:
Chris Masonf1885912008-04-09 16:28:12 -0400633 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -0400634}
635
Josef Bacikea466792012-03-26 21:57:36 -0400636static int btree_io_failed_hook(struct page *page, int failed_mirror)
Arne Jansen4bb31e92011-06-10 13:55:54 +0200637{
Arne Jansen4bb31e92011-06-10 13:55:54 +0200638 struct extent_buffer *eb;
Arne Jansen4bb31e92011-06-10 13:55:54 +0200639
Josef Bacik4f2de97a2012-03-07 16:20:05 -0500640 eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +0100641 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -0400642 eb->read_mirror = failed_mirror;
David Woodhouse53b381b2013-01-29 18:40:14 -0500643 atomic_dec(&eb->io_pages);
Josef Bacikea466792012-03-26 21:57:36 -0400644 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
Zhao Lei02873e42015-12-31 22:46:45 +0800645 btree_readahead_hook(eb->fs_info, eb, eb->start, -EIO);
Arne Jansen4bb31e92011-06-10 13:55:54 +0200646 return -EIO; /* we fixed nothing */
647}
648
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200649static void end_workqueue_bio(struct bio *bio)
Chris Masonce9adaa2008-04-09 16:28:12 -0400650{
David Sterba97eb6b62014-07-30 00:55:42 +0200651 struct btrfs_end_io_wq *end_io_wq = bio->bi_private;
Chris Masonce9adaa2008-04-09 16:28:12 -0400652 struct btrfs_fs_info *fs_info;
Liu Bo9e0af232014-08-15 23:36:53 +0800653 struct btrfs_workqueue *wq;
654 btrfs_work_func_t func;
Chris Masonce9adaa2008-04-09 16:28:12 -0400655
Chris Masonce9adaa2008-04-09 16:28:12 -0400656 fs_info = end_io_wq->info;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200657 end_io_wq->error = bio->bi_error;
Chris Masond20f7042008-12-08 16:58:54 -0500658
Mike Christie37226b22016-06-05 14:31:52 -0500659 if (bio_op(bio) == REQ_OP_WRITE) {
Liu Bo9e0af232014-08-15 23:36:53 +0800660 if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) {
661 wq = fs_info->endio_meta_write_workers;
662 func = btrfs_endio_meta_write_helper;
663 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) {
664 wq = fs_info->endio_freespace_worker;
665 func = btrfs_freespace_write_helper;
666 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
667 wq = fs_info->endio_raid56_workers;
668 func = btrfs_endio_raid56_helper;
669 } else {
670 wq = fs_info->endio_write_workers;
671 func = btrfs_endio_write_helper;
672 }
Chris Masond20f7042008-12-08 16:58:54 -0500673 } else {
Miao Xie8b110e32014-09-12 18:44:03 +0800674 if (unlikely(end_io_wq->metadata ==
675 BTRFS_WQ_ENDIO_DIO_REPAIR)) {
676 wq = fs_info->endio_repair_workers;
677 func = btrfs_endio_repair_helper;
678 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
Liu Bo9e0af232014-08-15 23:36:53 +0800679 wq = fs_info->endio_raid56_workers;
680 func = btrfs_endio_raid56_helper;
681 } else if (end_io_wq->metadata) {
682 wq = fs_info->endio_meta_workers;
683 func = btrfs_endio_meta_helper;
684 } else {
685 wq = fs_info->endio_workers;
686 func = btrfs_endio_helper;
687 }
Chris Masond20f7042008-12-08 16:58:54 -0500688 }
Liu Bo9e0af232014-08-15 23:36:53 +0800689
690 btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL);
691 btrfs_queue_work(wq, &end_io_wq->work);
Chris Masonce9adaa2008-04-09 16:28:12 -0400692}
693
Chris Mason22c59942008-04-09 16:28:12 -0400694int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
David Sterbabfebd8b2014-07-30 00:25:45 +0200695 enum btrfs_wq_endio_type metadata)
Chris Mason0b86a832008-03-24 15:01:56 -0400696{
David Sterba97eb6b62014-07-30 00:55:42 +0200697 struct btrfs_end_io_wq *end_io_wq;
Miao Xie8b110e32014-09-12 18:44:03 +0800698
David Sterba97eb6b62014-07-30 00:55:42 +0200699 end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS);
Chris Masonce9adaa2008-04-09 16:28:12 -0400700 if (!end_io_wq)
701 return -ENOMEM;
702
703 end_io_wq->private = bio->bi_private;
704 end_io_wq->end_io = bio->bi_end_io;
Chris Mason22c59942008-04-09 16:28:12 -0400705 end_io_wq->info = info;
Chris Masonce9adaa2008-04-09 16:28:12 -0400706 end_io_wq->error = 0;
707 end_io_wq->bio = bio;
Chris Mason22c59942008-04-09 16:28:12 -0400708 end_io_wq->metadata = metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -0400709
710 bio->bi_private = end_io_wq;
711 bio->bi_end_io = end_workqueue_bio;
Chris Mason22c59942008-04-09 16:28:12 -0400712 return 0;
713}
714
Chris Masonb64a2852008-08-20 13:39:41 -0400715unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
Chris Mason4854ddd2008-08-15 15:34:17 -0400716{
717 unsigned long limit = min_t(unsigned long,
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +0800718 info->thread_pool_size,
Chris Mason4854ddd2008-08-15 15:34:17 -0400719 info->fs_devices->open_devices);
720 return 256 * limit;
721}
722
Chris Mason4a69a412008-11-06 22:03:00 -0500723static void run_one_async_start(struct btrfs_work *work)
724{
Chris Mason4a69a412008-11-06 22:03:00 -0500725 struct async_submit_bio *async;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100726 int ret;
Chris Mason4a69a412008-11-06 22:03:00 -0500727
728 async = container_of(work, struct async_submit_bio, work);
Mike Christie81a75f62016-06-05 14:31:54 -0500729 ret = async->submit_bio_start(async->inode, async->bio,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100730 async->mirror_num, async->bio_flags,
731 async->bio_offset);
732 if (ret)
733 async->error = ret;
Chris Mason4a69a412008-11-06 22:03:00 -0500734}
735
736static void run_one_async_done(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400737{
738 struct btrfs_fs_info *fs_info;
739 struct async_submit_bio *async;
Chris Mason4854ddd2008-08-15 15:34:17 -0400740 int limit;
Chris Mason8b712842008-06-11 16:50:36 -0400741
742 async = container_of(work, struct async_submit_bio, work);
743 fs_info = BTRFS_I(async->inode)->root->fs_info;
Chris Mason4854ddd2008-08-15 15:34:17 -0400744
Chris Masonb64a2852008-08-20 13:39:41 -0400745 limit = btrfs_async_submit_limit(fs_info);
Chris Mason4854ddd2008-08-15 15:34:17 -0400746 limit = limit * 2 / 3;
747
David Sterbaee863952015-02-16 19:41:40 +0100748 /*
749 * atomic_dec_return implies a barrier for waitqueue_active
750 */
Josef Bacik66657b32012-08-01 15:36:24 -0400751 if (atomic_dec_return(&fs_info->nr_async_submits) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400752 waitqueue_active(&fs_info->async_submit_wait))
Chris Mason4854ddd2008-08-15 15:34:17 -0400753 wake_up(&fs_info->async_submit_wait);
754
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -0800755 /* If an error occurred we just want to clean up the bio and move on */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100756 if (async->error) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200757 async->bio->bi_error = async->error;
758 bio_endio(async->bio);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100759 return;
760 }
761
Mike Christie81a75f62016-06-05 14:31:54 -0500762 async->submit_bio_done(async->inode, async->bio, async->mirror_num,
763 async->bio_flags, async->bio_offset);
Chris Mason4a69a412008-11-06 22:03:00 -0500764}
765
766static void run_one_async_free(struct btrfs_work *work)
767{
768 struct async_submit_bio *async;
769
770 async = container_of(work, struct async_submit_bio, work);
Chris Mason8b712842008-06-11 16:50:36 -0400771 kfree(async);
772}
773
Chris Mason44b8bd72008-04-16 11:14:51 -0400774int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
Mike Christie81a75f62016-06-05 14:31:54 -0500775 struct bio *bio, int mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -0400776 unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -0400777 u64 bio_offset,
Chris Mason4a69a412008-11-06 22:03:00 -0500778 extent_submit_bio_hook_t *submit_bio_start,
779 extent_submit_bio_hook_t *submit_bio_done)
Chris Mason44b8bd72008-04-16 11:14:51 -0400780{
781 struct async_submit_bio *async;
782
783 async = kmalloc(sizeof(*async), GFP_NOFS);
784 if (!async)
785 return -ENOMEM;
786
787 async->inode = inode;
Chris Mason44b8bd72008-04-16 11:14:51 -0400788 async->bio = bio;
789 async->mirror_num = mirror_num;
Chris Mason4a69a412008-11-06 22:03:00 -0500790 async->submit_bio_start = submit_bio_start;
791 async->submit_bio_done = submit_bio_done;
792
Liu Bo9e0af232014-08-15 23:36:53 +0800793 btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start,
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +0800794 run_one_async_done, run_one_async_free);
Chris Mason4a69a412008-11-06 22:03:00 -0500795
Chris Masonc8b97812008-10-29 14:49:59 -0400796 async->bio_flags = bio_flags;
Chris Masoneaf25d92010-05-25 09:48:28 -0400797 async->bio_offset = bio_offset;
Chris Mason8c8bee12008-09-29 11:19:10 -0400798
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100799 async->error = 0;
800
Chris Masoncb03c742008-05-15 16:15:45 -0400801 atomic_inc(&fs_info->nr_async_submits);
Chris Masond313d7a2009-04-20 15:50:09 -0400802
Jens Axboe1eff9d32016-08-05 15:35:16 -0600803 if (bio->bi_opf & REQ_SYNC)
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +0800804 btrfs_set_work_high_priority(&async->work);
Chris Masond313d7a2009-04-20 15:50:09 -0400805
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +0800806 btrfs_queue_work(fs_info->workers, &async->work);
Chris Mason9473f162008-08-28 06:15:24 -0400807
Chris Masond3977122009-01-05 21:25:51 -0500808 while (atomic_read(&fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500809 atomic_read(&fs_info->nr_async_submits)) {
810 wait_event(fs_info->async_submit_wait,
811 (atomic_read(&fs_info->nr_async_submits) == 0));
812 }
813
Chris Mason44b8bd72008-04-16 11:14:51 -0400814 return 0;
815}
816
Chris Masonce3ed712008-09-23 13:14:12 -0400817static int btree_csum_one_bio(struct bio *bio)
818{
Kent Overstreet2c30c712013-11-07 12:20:26 -0800819 struct bio_vec *bvec;
Chris Masonce3ed712008-09-23 13:14:12 -0400820 struct btrfs_root *root;
Kent Overstreet2c30c712013-11-07 12:20:26 -0800821 int i, ret = 0;
Chris Masonce3ed712008-09-23 13:14:12 -0400822
Kent Overstreet2c30c712013-11-07 12:20:26 -0800823 bio_for_each_segment_all(bvec, bio, i) {
Chris Masonce3ed712008-09-23 13:14:12 -0400824 root = BTRFS_I(bvec->bv_page->mapping->host)->root;
Daniel Dressler01d58472014-11-21 17:15:07 +0900825 ret = csum_dirty_buffer(root->fs_info, bvec->bv_page);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100826 if (ret)
827 break;
Chris Masonce3ed712008-09-23 13:14:12 -0400828 }
Kent Overstreet2c30c712013-11-07 12:20:26 -0800829
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100830 return ret;
Chris Masonce3ed712008-09-23 13:14:12 -0400831}
832
Mike Christie81a75f62016-06-05 14:31:54 -0500833static int __btree_submit_bio_start(struct inode *inode, struct bio *bio,
834 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -0400835 u64 bio_offset)
Chris Mason22c59942008-04-09 16:28:12 -0400836{
Chris Mason8b712842008-06-11 16:50:36 -0400837 /*
838 * when we're called for a write, we're already in the async
Chris Mason5443be42008-08-15 15:34:16 -0400839 * submission context. Just jump into btrfs_map_bio
Chris Mason8b712842008-06-11 16:50:36 -0400840 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100841 return btree_csum_one_bio(bio);
Chris Mason4a69a412008-11-06 22:03:00 -0500842}
Chris Mason22c59942008-04-09 16:28:12 -0400843
Mike Christie81a75f62016-06-05 14:31:54 -0500844static int __btree_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -0400845 int mirror_num, unsigned long bio_flags,
846 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -0500847{
Stefan Behrens61891922012-11-05 18:51:52 +0100848 int ret;
849
Chris Mason8b712842008-06-11 16:50:36 -0400850 /*
Chris Mason4a69a412008-11-06 22:03:00 -0500851 * when we're called for a write, we're already in the async
852 * submission context. Just jump into btrfs_map_bio
Chris Mason8b712842008-06-11 16:50:36 -0400853 */
Mike Christie81a75f62016-06-05 14:31:54 -0500854 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200855 if (ret) {
856 bio->bi_error = ret;
857 bio_endio(bio);
858 }
Stefan Behrens61891922012-11-05 18:51:52 +0100859 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -0400860}
861
Josef Bacikde0022b2012-09-25 14:25:58 -0400862static int check_async_write(struct inode *inode, unsigned long bio_flags)
863{
864 if (bio_flags & EXTENT_BIO_TREE_LOG)
865 return 0;
866#ifdef CONFIG_X86
Borislav Petkovbc696ca2016-01-26 22:12:05 +0100867 if (static_cpu_has(X86_FEATURE_XMM4_2))
Josef Bacikde0022b2012-09-25 14:25:58 -0400868 return 0;
869#endif
870 return 1;
871}
872
Mike Christie81a75f62016-06-05 14:31:54 -0500873static int btree_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -0400874 int mirror_num, unsigned long bio_flags,
875 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -0400876{
Josef Bacikde0022b2012-09-25 14:25:58 -0400877 int async = check_async_write(inode, bio_flags);
Chris Masoncad321a2008-12-17 14:51:42 -0500878 int ret;
879
Mike Christie37226b22016-06-05 14:31:52 -0500880 if (bio_op(bio) != REQ_OP_WRITE) {
Chris Mason4a69a412008-11-06 22:03:00 -0500881 /*
882 * called for a read, do the setup so that checksum validation
883 * can happen in the async kernel threads
884 */
Chris Masonf3f266a2012-03-23 10:22:46 -0400885 ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info,
David Sterbabfebd8b2014-07-30 00:25:45 +0200886 bio, BTRFS_WQ_ENDIO_METADATA);
Chris Mason1d4284b2012-03-28 20:31:37 -0400887 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +0100888 goto out_w_error;
Mike Christie81a75f62016-06-05 14:31:54 -0500889 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 0);
Josef Bacikde0022b2012-09-25 14:25:58 -0400890 } else if (!async) {
891 ret = btree_csum_one_bio(bio);
892 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +0100893 goto out_w_error;
Mike Christie81a75f62016-06-05 14:31:54 -0500894 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +0100895 } else {
896 /*
897 * kthread helpers are used to submit writes so that
898 * checksumming can happen in parallel across all CPUs
899 */
900 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Mike Christie81a75f62016-06-05 14:31:54 -0500901 inode, bio, mirror_num, 0,
Stefan Behrens61891922012-11-05 18:51:52 +0100902 bio_offset,
903 __btree_submit_bio_start,
904 __btree_submit_bio_done);
Chris Mason44b8bd72008-04-16 11:14:51 -0400905 }
Chris Masond313d7a2009-04-20 15:50:09 -0400906
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200907 if (ret)
908 goto out_w_error;
909 return 0;
910
Stefan Behrens61891922012-11-05 18:51:52 +0100911out_w_error:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200912 bio->bi_error = ret;
913 bio_endio(bio);
Stefan Behrens61891922012-11-05 18:51:52 +0100914 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -0400915}
916
Jan Beulich3dd14622010-12-07 14:54:09 +0000917#ifdef CONFIG_MIGRATION
Chris Mason784b4e22010-11-21 22:20:49 -0500918static int btree_migratepage(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800919 struct page *newpage, struct page *page,
920 enum migrate_mode mode)
Chris Mason784b4e22010-11-21 22:20:49 -0500921{
922 /*
923 * we can't safely write a btree page from here,
924 * we haven't done the locking hook
925 */
926 if (PageDirty(page))
927 return -EAGAIN;
928 /*
929 * Buffers may be managed in a filesystem specific way.
930 * We must have no buffers or drop them.
931 */
932 if (page_has_private(page) &&
933 !try_to_release_page(page, GFP_KERNEL))
934 return -EAGAIN;
Mel Gormana6bc32b2012-01-12 17:19:43 -0800935 return migrate_page(mapping, newpage, page, mode);
Chris Mason784b4e22010-11-21 22:20:49 -0500936}
Jan Beulich3dd14622010-12-07 14:54:09 +0000937#endif
Chris Mason784b4e22010-11-21 22:20:49 -0500938
Chris Mason0da54682007-11-07 21:08:01 -0500939
940static int btree_writepages(struct address_space *mapping,
941 struct writeback_control *wbc)
942{
Miao Xiee2d84522013-01-29 10:09:20 +0000943 struct btrfs_fs_info *fs_info;
944 int ret;
945
Chris Masond8d5f3e2007-12-11 12:42:00 -0500946 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason448d6402007-11-27 07:52:01 -0800947
948 if (wbc->for_kupdate)
949 return 0;
950
Miao Xiee2d84522013-01-29 10:09:20 +0000951 fs_info = BTRFS_I(mapping->host)->root->fs_info;
Chris Masonb9473432009-03-13 11:00:37 -0400952 /* this is a bit racy, but that's ok */
Ethan Liend92fa5e2018-07-02 15:44:58 +0800953 ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
954 BTRFS_DIRTY_METADATA_THRESH,
955 fs_info->dirty_metadata_batch);
Miao Xiee2d84522013-01-29 10:09:20 +0000956 if (ret < 0)
Chris Mason793955b2007-11-26 16:34:41 -0800957 return 0;
Chris Mason793955b2007-11-26 16:34:41 -0800958 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400959 return btree_write_cache_pages(mapping, wbc);
Chris Mason0da54682007-11-07 21:08:01 -0500960}
961
Christoph Hellwigb2950862008-12-02 09:54:17 -0500962static int btree_readpage(struct file *file, struct page *page)
Chris Mason5f39d392007-10-15 16:14:19 -0400963{
Chris Masond1310b22008-01-24 16:13:08 -0500964 struct extent_io_tree *tree;
965 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +0200966 return extent_read_full_page(tree, page, btree_get_extent, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400967}
968
Chris Mason70dec802008-01-29 09:59:12 -0500969static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400970{
Chris Mason98509cf2008-09-11 15:51:43 -0400971 if (PageWriteback(page) || PageDirty(page))
Chris Masond3977122009-01-05 21:25:51 -0500972 return 0;
David Sterba0c4e5382012-01-26 15:01:12 -0500973
David Sterbaf7a52a42013-04-26 14:56:29 +0000974 return try_release_extent_buffer(page);
Chris Masone20d96d2007-03-22 12:13:20 -0400975}
Chris Mason123abc82007-03-14 14:14:43 -0400976
Lukas Czernerd47992f2013-05-21 23:17:23 -0400977static void btree_invalidatepage(struct page *page, unsigned int offset,
978 unsigned int length)
Chris Masond98237b2007-03-28 13:57:48 -0400979{
Chris Masond1310b22008-01-24 16:13:08 -0500980 struct extent_io_tree *tree;
981 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400982 extent_invalidatepage(tree, page, offset);
983 btree_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -0400984 if (PagePrivate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500985 btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info,
986 "page private not zero on page %llu",
987 (unsigned long long)page_offset(page));
Chris Mason9ad6b7b2008-04-18 16:11:30 -0400988 ClearPagePrivate(page);
989 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300990 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -0400991 }
Chris Masond98237b2007-03-28 13:57:48 -0400992}
993
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400994static int btree_set_page_dirty(struct page *page)
995{
Josef Bacikbb146eb2012-10-15 13:30:43 -0400996#ifdef DEBUG
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400997 struct extent_buffer *eb;
998
999 BUG_ON(!PagePrivate(page));
1000 eb = (struct extent_buffer *)page->private;
1001 BUG_ON(!eb);
1002 BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
1003 BUG_ON(!atomic_read(&eb->refs));
1004 btrfs_assert_tree_locked(eb);
Josef Bacikbb146eb2012-10-15 13:30:43 -04001005#endif
Josef Bacik0b32f4b2012-03-13 09:38:00 -04001006 return __set_page_dirty_nobuffers(page);
1007}
1008
Alexey Dobriyan7f094102009-09-21 17:01:10 -07001009static const struct address_space_operations btree_aops = {
Chris Masond98237b2007-03-28 13:57:48 -04001010 .readpage = btree_readpage,
Chris Mason0da54682007-11-07 21:08:01 -05001011 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -04001012 .releasepage = btree_releasepage,
1013 .invalidatepage = btree_invalidatepage,
Chris Mason5a92bc82010-11-29 09:49:11 -05001014#ifdef CONFIG_MIGRATION
Chris Mason784b4e22010-11-21 22:20:49 -05001015 .migratepage = btree_migratepage,
Chris Mason5a92bc82010-11-29 09:49:11 -05001016#endif
Josef Bacik0b32f4b2012-03-13 09:38:00 -04001017 .set_page_dirty = btree_set_page_dirty,
Chris Masond98237b2007-03-28 13:57:48 -04001018};
1019
David Sterbad3e46fe2014-06-15 02:04:19 +02001020void readahead_tree_block(struct btrfs_root *root, u64 bytenr)
Chris Mason090d1872007-05-01 08:53:32 -04001021{
Chris Mason5f39d392007-10-15 16:14:19 -04001022 struct extent_buffer *buf = NULL;
1023 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason090d1872007-05-01 08:53:32 -04001024
David Sterbaa83fffb2014-06-15 02:39:54 +02001025 buf = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07001026 if (IS_ERR(buf))
David Sterba6197d862014-06-15 00:49:36 +02001027 return;
Chris Masond1310b22008-01-24 16:13:08 -05001028 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Josef Bacik8436ea912016-09-02 15:40:03 -04001029 buf, WAIT_NONE, btree_get_extent, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001030 free_extent_buffer(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001031}
1032
David Sterbac0dcaa42014-06-15 02:24:21 +02001033int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr,
Arne Jansenab0fff02011-05-23 14:25:41 +02001034 int mirror_num, struct extent_buffer **eb)
1035{
1036 struct extent_buffer *buf = NULL;
1037 struct inode *btree_inode = root->fs_info->btree_inode;
1038 struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
1039 int ret;
1040
David Sterbaa83fffb2014-06-15 02:39:54 +02001041 buf = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07001042 if (IS_ERR(buf))
Arne Jansenab0fff02011-05-23 14:25:41 +02001043 return 0;
1044
1045 set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags);
1046
Josef Bacik8436ea912016-09-02 15:40:03 -04001047 ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK,
Arne Jansenab0fff02011-05-23 14:25:41 +02001048 btree_get_extent, mirror_num);
1049 if (ret) {
1050 free_extent_buffer(buf);
1051 return ret;
1052 }
1053
1054 if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) {
1055 free_extent_buffer(buf);
1056 return -EIO;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04001057 } else if (extent_buffer_uptodate(buf)) {
Arne Jansenab0fff02011-05-23 14:25:41 +02001058 *eb = buf;
1059 } else {
1060 free_extent_buffer(buf);
1061 }
1062 return 0;
1063}
1064
Daniel Dressler01d58472014-11-21 17:15:07 +09001065struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
David Sterba0308af42014-06-15 01:43:40 +02001066 u64 bytenr)
Chris Mason0999df52008-04-01 13:48:14 -04001067{
Daniel Dressler01d58472014-11-21 17:15:07 +09001068 return find_extent_buffer(fs_info, bytenr);
Chris Mason0999df52008-04-01 13:48:14 -04001069}
1070
1071struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
David Sterbaa83fffb2014-06-15 02:39:54 +02001072 u64 bytenr)
Chris Mason0999df52008-04-01 13:48:14 -04001073{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001074 if (btrfs_is_testing(root->fs_info))
Feifei Xub9ef22d2016-06-01 19:18:25 +08001075 return alloc_test_extent_buffer(root->fs_info, bytenr,
1076 root->nodesize);
David Sterbace3e6982014-06-15 03:00:04 +02001077 return alloc_extent_buffer(root->fs_info, bytenr);
Chris Mason0999df52008-04-01 13:48:14 -04001078}
1079
1080
Chris Masone02119d2008-09-05 16:13:11 -04001081int btrfs_write_tree_block(struct extent_buffer *buf)
1082{
Chris Mason727011e2010-08-06 13:21:20 -04001083 return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
Christoph Hellwig8aa38c32009-10-01 12:58:30 -04001084 buf->start + buf->len - 1);
Chris Masone02119d2008-09-05 16:13:11 -04001085}
1086
1087int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
1088{
Chris Mason727011e2010-08-06 13:21:20 -04001089 return filemap_fdatawait_range(buf->pages[0]->mapping,
Christoph Hellwig8aa38c32009-10-01 12:58:30 -04001090 buf->start, buf->start + buf->len - 1);
Chris Masone02119d2008-09-05 16:13:11 -04001091}
1092
Chris Masondb945352007-10-15 16:15:53 -04001093struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
David Sterbace86cd52014-06-15 01:07:32 +02001094 u64 parent_transid)
Chris Masone20d96d2007-03-22 12:13:20 -04001095{
Chris Mason5f39d392007-10-15 16:14:19 -04001096 struct extent_buffer *buf = NULL;
Chris Mason19c00dd2007-10-15 16:19:22 -04001097 int ret;
1098
David Sterbaa83fffb2014-06-15 02:39:54 +02001099 buf = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07001100 if (IS_ERR(buf))
1101 return buf;
Chris Masone4204de2008-01-08 15:46:27 -05001102
Josef Bacik8436ea912016-09-02 15:40:03 -04001103 ret = btree_read_extent_buffer_pages(root, buf, parent_transid);
Filipe David Borba Manana0f0fe8f2013-07-31 00:39:56 +01001104 if (ret) {
1105 free_extent_buffer(buf);
Liu Bo64c043d2015-05-25 17:30:15 +08001106 return ERR_PTR(ret);
Filipe David Borba Manana0f0fe8f2013-07-31 00:39:56 +01001107 }
Chris Mason5f39d392007-10-15 16:14:19 -04001108 return buf;
Chris Masonce9adaa2008-04-09 16:28:12 -04001109
Chris Masoneb60cea2007-02-02 09:18:22 -05001110}
1111
Daniel Dressler01d58472014-11-21 17:15:07 +09001112void clean_tree_block(struct btrfs_trans_handle *trans,
1113 struct btrfs_fs_info *fs_info,
Jeff Mahoneyd5c13f92012-03-01 14:56:27 +01001114 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -05001115{
Chris Mason55c69072008-01-09 15:55:33 -05001116 if (btrfs_header_generation(buf) ==
Miao Xiee2d84522013-01-29 10:09:20 +00001117 fs_info->running_transaction->transid) {
Chris Masonb9447ef2009-03-09 11:45:38 -04001118 btrfs_assert_tree_locked(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001119
Chris Masonb9473432009-03-13 11:00:37 -04001120 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
Miao Xiee2d84522013-01-29 10:09:20 +00001121 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
1122 -buf->len,
1123 fs_info->dirty_metadata_batch);
Josef Baciked7b63e2012-10-15 13:33:54 -04001124 /* ugh, clear_extent_buffer_dirty needs to lock the page */
1125 btrfs_set_lock_blocking(buf);
1126 clear_extent_buffer_dirty(buf);
1127 }
Chris Mason925baed2008-06-25 16:01:30 -04001128 }
Chris Mason5f39d392007-10-15 16:14:19 -04001129}
1130
Miao Xie8257b2d2014-03-06 13:38:19 +08001131static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void)
1132{
1133 struct btrfs_subvolume_writers *writers;
1134 int ret;
1135
1136 writers = kmalloc(sizeof(*writers), GFP_NOFS);
1137 if (!writers)
1138 return ERR_PTR(-ENOMEM);
1139
Jeff Mahoney9c38c3b2018-03-16 14:36:27 -04001140 ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS);
Miao Xie8257b2d2014-03-06 13:38:19 +08001141 if (ret < 0) {
1142 kfree(writers);
1143 return ERR_PTR(ret);
1144 }
1145
1146 init_waitqueue_head(&writers->wait);
1147 return writers;
1148}
1149
1150static void
1151btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers)
1152{
1153 percpu_counter_destroy(&writers->counter);
1154 kfree(writers);
1155}
1156
David Sterba707e8a02014-06-04 19:22:26 +02001157static void __setup_root(u32 nodesize, u32 sectorsize, u32 stripesize,
1158 struct btrfs_root *root, struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001159 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -05001160{
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001161 bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
Chris Masoncfaa7292007-02-21 17:04:57 -05001162 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -05001163 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -04001164 root->sectorsize = sectorsize;
1165 root->nodesize = nodesize;
Chris Mason87ee04e2007-11-30 11:30:34 -05001166 root->stripesize = stripesize;
Miao Xie27cdeb72014-04-02 19:51:05 +08001167 root->state = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04001168 root->orphan_cleanup_state = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001169
Chris Mason0f7d52f2007-04-09 10:42:37 -04001170 root->objectid = objectid;
1171 root->last_trans = 0;
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001172 root->highest_objectid = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00001173 root->nr_delalloc_inodes = 0;
Miao Xie199c2a92013-05-15 07:48:23 +00001174 root->nr_ordered_extents = 0;
Josef Bacik58176a92007-08-29 15:47:34 -04001175 root->name = NULL;
Eric Paris6bef4d32010-02-23 19:43:04 +00001176 root->inode_tree = RB_ROOT;
Miao Xie16cdcec2011-04-22 18:12:22 +08001177 INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001178 root->block_rsv = NULL;
Yan, Zhengd68fc572010-05-16 10:49:58 -04001179 root->orphan_block_rsv = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001180
1181 INIT_LIST_HEAD(&root->dirty_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001182 INIT_LIST_HEAD(&root->root_list);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001183 INIT_LIST_HEAD(&root->delalloc_inodes);
1184 INIT_LIST_HEAD(&root->delalloc_root);
Miao Xie199c2a92013-05-15 07:48:23 +00001185 INIT_LIST_HEAD(&root->ordered_extents);
1186 INIT_LIST_HEAD(&root->ordered_root);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001187 INIT_LIST_HEAD(&root->logged_list[0]);
1188 INIT_LIST_HEAD(&root->logged_list[1]);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001189 spin_lock_init(&root->orphan_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190 spin_lock_init(&root->inode_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001191 spin_lock_init(&root->delalloc_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00001192 spin_lock_init(&root->ordered_extent_lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001193 spin_lock_init(&root->accounting_lock);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001194 spin_lock_init(&root->log_extents_lock[0]);
1195 spin_lock_init(&root->log_extents_lock[1]);
Chris Masona2135012008-06-25 16:01:30 -04001196 mutex_init(&root->objectid_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04001197 mutex_init(&root->log_mutex);
Miao Xie31f3d252014-03-06 13:55:02 +08001198 mutex_init(&root->ordered_extent_mutex);
Miao Xie573bfb72014-03-06 13:55:03 +08001199 mutex_init(&root->delalloc_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05001200 init_waitqueue_head(&root->log_writer_wait);
1201 init_waitqueue_head(&root->log_commit_wait[0]);
1202 init_waitqueue_head(&root->log_commit_wait[1]);
Miao Xie8b050d32014-02-20 18:08:58 +08001203 INIT_LIST_HEAD(&root->log_ctxs[0]);
1204 INIT_LIST_HEAD(&root->log_ctxs[1]);
Yan Zheng7237f182009-01-21 12:54:03 -05001205 atomic_set(&root->log_commit[0], 0);
1206 atomic_set(&root->log_commit[1], 0);
1207 atomic_set(&root->log_writers, 0);
Miao Xie2ecb7922012-09-06 04:04:27 -06001208 atomic_set(&root->log_batch, 0);
Josef Bacik8a35d952012-05-23 14:26:42 -04001209 atomic_set(&root->orphan_inodes, 0);
Miao Xieb0feb9d2013-05-15 07:48:20 +00001210 atomic_set(&root->refs, 1);
Miao Xie8257b2d2014-03-06 13:38:19 +08001211 atomic_set(&root->will_be_snapshoted, 0);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08001212 atomic_set(&root->qgroup_meta_rsv, 0);
Yan Zheng7237f182009-01-21 12:54:03 -05001213 root->log_transid = 0;
Miao Xied1433de2014-02-20 18:08:59 +08001214 root->log_transid_committed = -1;
Chris Mason257c62e2009-10-13 13:21:08 -04001215 root->last_log_commit = 0;
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001216 if (!dummy)
Josef Bacik06ea65a2013-09-19 16:07:01 -04001217 extent_io_tree_init(&root->dirty_log_pages,
1218 fs_info->btree_inode->i_mapping);
Chris Mason017e5362008-07-28 15:32:51 -04001219
Chris Mason3768f362007-03-13 16:47:54 -04001220 memset(&root->root_key, 0, sizeof(root->root_key));
1221 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -04001222 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001223 if (!dummy)
Josef Bacik06ea65a2013-09-19 16:07:01 -04001224 root->defrag_trans_start = fs_info->generation;
1225 else
1226 root->defrag_trans_start = 0;
Chris Mason4d775672007-04-20 20:23:12 -04001227 root->root_key.objectid = objectid;
Al Viro0ee5dc62011-07-07 15:44:25 -04001228 root->anon_dev = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +02001229
Anand Jain5f3ab902012-12-07 09:28:54 +00001230 spin_lock_init(&root->root_item_lock);
Chris Mason3768f362007-03-13 16:47:54 -04001231}
1232
David Sterba74e4d822016-02-11 11:01:55 +01001233static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
1234 gfp_t flags)
Al Viro6f07e422011-11-17 00:46:16 -05001235{
David Sterba74e4d822016-02-11 11:01:55 +01001236 struct btrfs_root *root = kzalloc(sizeof(*root), flags);
Al Viro6f07e422011-11-17 00:46:16 -05001237 if (root)
1238 root->fs_info = fs_info;
1239 return root;
1240}
1241
Josef Bacik06ea65a2013-09-19 16:07:01 -04001242#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1243/* Should only be used by the testing infrastructure */
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001244struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info,
1245 u32 sectorsize, u32 nodesize)
Josef Bacik06ea65a2013-09-19 16:07:01 -04001246{
1247 struct btrfs_root *root;
1248
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001249 if (!fs_info)
1250 return ERR_PTR(-EINVAL);
1251
1252 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Josef Bacik06ea65a2013-09-19 16:07:01 -04001253 if (!root)
1254 return ERR_PTR(-ENOMEM);
Feifei Xub9ef22d2016-06-01 19:18:25 +08001255 /* We don't use the stripesize in selftest, set it as sectorsize */
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001256 __setup_root(nodesize, sectorsize, sectorsize, root, fs_info,
Feifei Xuef9f2db2016-06-01 19:18:28 +08001257 BTRFS_ROOT_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001258 root->alloc_bytenr = 0;
Josef Bacik06ea65a2013-09-19 16:07:01 -04001259
1260 return root;
1261}
1262#endif
1263
Arne Jansen20897f52011-09-13 12:44:20 +02001264struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
1265 struct btrfs_fs_info *fs_info,
1266 u64 objectid)
1267{
1268 struct extent_buffer *leaf;
1269 struct btrfs_root *tree_root = fs_info->tree_root;
1270 struct btrfs_root *root;
1271 struct btrfs_key key;
1272 int ret = 0;
Stefan Behrens6463fe52013-04-19 15:08:05 +00001273 uuid_le uuid;
Arne Jansen20897f52011-09-13 12:44:20 +02001274
David Sterba74e4d822016-02-11 11:01:55 +01001275 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Arne Jansen20897f52011-09-13 12:44:20 +02001276 if (!root)
1277 return ERR_PTR(-ENOMEM);
1278
David Sterba707e8a02014-06-04 19:22:26 +02001279 __setup_root(tree_root->nodesize, tree_root->sectorsize,
1280 tree_root->stripesize, root, fs_info, objectid);
Arne Jansen20897f52011-09-13 12:44:20 +02001281 root->root_key.objectid = objectid;
1282 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1283 root->root_key.offset = 0;
1284
David Sterba4d75f8a2014-06-15 01:54:12 +02001285 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Arne Jansen20897f52011-09-13 12:44:20 +02001286 if (IS_ERR(leaf)) {
1287 ret = PTR_ERR(leaf);
Tsutomu Itoh1dd05682013-03-21 04:32:32 +00001288 leaf = NULL;
Arne Jansen20897f52011-09-13 12:44:20 +02001289 goto fail;
1290 }
1291
Arne Jansen20897f52011-09-13 12:44:20 +02001292 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
1293 btrfs_set_header_bytenr(leaf, leaf->start);
1294 btrfs_set_header_generation(leaf, trans->transid);
1295 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1296 btrfs_set_header_owner(leaf, objectid);
1297 root->node = leaf;
1298
Ross Kirk0a4e5582013-09-24 10:12:38 +01001299 write_extent_buffer(leaf, fs_info->fsid, btrfs_header_fsid(),
Arne Jansen20897f52011-09-13 12:44:20 +02001300 BTRFS_FSID_SIZE);
1301 write_extent_buffer(leaf, fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02001302 btrfs_header_chunk_tree_uuid(leaf),
Arne Jansen20897f52011-09-13 12:44:20 +02001303 BTRFS_UUID_SIZE);
1304 btrfs_mark_buffer_dirty(leaf);
1305
1306 root->commit_root = btrfs_root_node(root);
Miao Xie27cdeb72014-04-02 19:51:05 +08001307 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
Arne Jansen20897f52011-09-13 12:44:20 +02001308
1309 root->root_item.flags = 0;
1310 root->root_item.byte_limit = 0;
1311 btrfs_set_root_bytenr(&root->root_item, leaf->start);
1312 btrfs_set_root_generation(&root->root_item, trans->transid);
1313 btrfs_set_root_level(&root->root_item, 0);
1314 btrfs_set_root_refs(&root->root_item, 1);
1315 btrfs_set_root_used(&root->root_item, leaf->len);
1316 btrfs_set_root_last_snapshot(&root->root_item, 0);
1317 btrfs_set_root_dirid(&root->root_item, 0);
Stefan Behrens6463fe52013-04-19 15:08:05 +00001318 uuid_le_gen(&uuid);
1319 memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE);
Arne Jansen20897f52011-09-13 12:44:20 +02001320 root->root_item.drop_level = 0;
1321
1322 key.objectid = objectid;
1323 key.type = BTRFS_ROOT_ITEM_KEY;
1324 key.offset = 0;
1325 ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
1326 if (ret)
1327 goto fail;
1328
1329 btrfs_tree_unlock(leaf);
1330
Arne Jansen20897f52011-09-13 12:44:20 +02001331 return root;
Tsutomu Itoh1dd05682013-03-21 04:32:32 +00001332
1333fail:
1334 if (leaf) {
1335 btrfs_tree_unlock(leaf);
Tsutomu Itoh59885b32014-04-09 09:18:04 +09001336 free_extent_buffer(root->commit_root);
Tsutomu Itoh1dd05682013-03-21 04:32:32 +00001337 free_extent_buffer(leaf);
1338 }
1339 kfree(root);
1340
1341 return ERR_PTR(ret);
Arne Jansen20897f52011-09-13 12:44:20 +02001342}
1343
Yan Zheng7237f182009-01-21 12:54:03 -05001344static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1345 struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -04001346{
1347 struct btrfs_root *root;
1348 struct btrfs_root *tree_root = fs_info->tree_root;
Yan Zheng7237f182009-01-21 12:54:03 -05001349 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04001350
David Sterba74e4d822016-02-11 11:01:55 +01001351 root = btrfs_alloc_root(fs_info, GFP_NOFS);
Chris Masone02119d2008-09-05 16:13:11 -04001352 if (!root)
Yan Zheng7237f182009-01-21 12:54:03 -05001353 return ERR_PTR(-ENOMEM);
Chris Masone02119d2008-09-05 16:13:11 -04001354
David Sterba707e8a02014-06-04 19:22:26 +02001355 __setup_root(tree_root->nodesize, tree_root->sectorsize,
1356 tree_root->stripesize, root, fs_info,
1357 BTRFS_TREE_LOG_OBJECTID);
Chris Masone02119d2008-09-05 16:13:11 -04001358
1359 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
1360 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1361 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
Miao Xie27cdeb72014-04-02 19:51:05 +08001362
Yan Zheng7237f182009-01-21 12:54:03 -05001363 /*
Miao Xie27cdeb72014-04-02 19:51:05 +08001364 * DON'T set REF_COWS for log trees
1365 *
Yan Zheng7237f182009-01-21 12:54:03 -05001366 * log trees do not get reference counted because they go away
1367 * before a real commit is actually done. They do store pointers
1368 * to file data extents, and those reference counts still get
1369 * updated (along with back refs to the log tree).
1370 */
Chris Masone02119d2008-09-05 16:13:11 -04001371
David Sterba4d75f8a2014-06-15 01:54:12 +02001372 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
1373 NULL, 0, 0, 0);
Yan Zheng7237f182009-01-21 12:54:03 -05001374 if (IS_ERR(leaf)) {
1375 kfree(root);
1376 return ERR_CAST(leaf);
1377 }
Chris Masone02119d2008-09-05 16:13:11 -04001378
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001379 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
1380 btrfs_set_header_bytenr(leaf, leaf->start);
1381 btrfs_set_header_generation(leaf, trans->transid);
1382 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1383 btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
Yan Zheng7237f182009-01-21 12:54:03 -05001384 root->node = leaf;
Chris Masone02119d2008-09-05 16:13:11 -04001385
1386 write_extent_buffer(root->node, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01001387 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone02119d2008-09-05 16:13:11 -04001388 btrfs_mark_buffer_dirty(root->node);
1389 btrfs_tree_unlock(root->node);
Yan Zheng7237f182009-01-21 12:54:03 -05001390 return root;
1391}
1392
1393int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1394 struct btrfs_fs_info *fs_info)
1395{
1396 struct btrfs_root *log_root;
1397
1398 log_root = alloc_log_tree(trans, fs_info);
1399 if (IS_ERR(log_root))
1400 return PTR_ERR(log_root);
1401 WARN_ON(fs_info->log_root_tree);
1402 fs_info->log_root_tree = log_root;
1403 return 0;
1404}
1405
1406int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1407 struct btrfs_root *root)
1408{
1409 struct btrfs_root *log_root;
1410 struct btrfs_inode_item *inode_item;
1411
1412 log_root = alloc_log_tree(trans, root->fs_info);
1413 if (IS_ERR(log_root))
1414 return PTR_ERR(log_root);
1415
1416 log_root->last_trans = trans->transid;
1417 log_root->root_key.offset = root->root_key.objectid;
1418
1419 inode_item = &log_root->root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001420 btrfs_set_stack_inode_generation(inode_item, 1);
1421 btrfs_set_stack_inode_size(inode_item, 3);
1422 btrfs_set_stack_inode_nlink(inode_item, 1);
David Sterba707e8a02014-06-04 19:22:26 +02001423 btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001424 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Yan Zheng7237f182009-01-21 12:54:03 -05001425
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001426 btrfs_set_root_node(&log_root->root_item, log_root->node);
Yan Zheng7237f182009-01-21 12:54:03 -05001427
1428 WARN_ON(root->log_root);
1429 root->log_root = log_root;
1430 root->log_transid = 0;
Miao Xied1433de2014-02-20 18:08:59 +08001431 root->log_transid_committed = -1;
Chris Mason257c62e2009-10-13 13:21:08 -04001432 root->last_log_commit = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001433 return 0;
1434}
1435
Stefan Behrens35a36212013-08-14 18:12:25 +02001436static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
1437 struct btrfs_key *key)
Chris Masone02119d2008-09-05 16:13:11 -04001438{
1439 struct btrfs_root *root;
1440 struct btrfs_fs_info *fs_info = tree_root->fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001441 struct btrfs_path *path;
Yan Zheng84234f32008-10-29 14:49:05 -04001442 u64 generation;
Miao Xiecb517ea2013-05-15 07:48:19 +00001443 int ret;
1444
1445 path = btrfs_alloc_path();
1446 if (!path)
1447 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001448
David Sterba74e4d822016-02-11 11:01:55 +01001449 root = btrfs_alloc_root(fs_info, GFP_NOFS);
Miao Xiecb517ea2013-05-15 07:48:19 +00001450 if (!root) {
1451 ret = -ENOMEM;
1452 goto alloc_fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001453 }
1454
David Sterba707e8a02014-06-04 19:22:26 +02001455 __setup_root(tree_root->nodesize, tree_root->sectorsize,
1456 tree_root->stripesize, root, fs_info, key->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001457
Miao Xiecb517ea2013-05-15 07:48:19 +00001458 ret = btrfs_find_root(tree_root, key, path,
1459 &root->root_item, &root->root_key);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001460 if (ret) {
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001461 if (ret > 0)
1462 ret = -ENOENT;
Miao Xiecb517ea2013-05-15 07:48:19 +00001463 goto find_fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001464 }
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001465
Yan Zheng84234f32008-10-29 14:49:05 -04001466 generation = btrfs_root_generation(&root->root_item);
Chris Masondb945352007-10-15 16:15:53 -04001467 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
David Sterbace86cd52014-06-15 01:07:32 +02001468 generation);
Liu Bo64c043d2015-05-25 17:30:15 +08001469 if (IS_ERR(root->node)) {
1470 ret = PTR_ERR(root->node);
Miao Xiecb517ea2013-05-15 07:48:19 +00001471 goto find_fail;
1472 } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
1473 ret = -EIO;
Liu Bo64c043d2015-05-25 17:30:15 +08001474 free_extent_buffer(root->node);
1475 goto find_fail;
Josef Bacik416bc652013-04-23 14:17:42 -04001476 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001477 root->commit_root = btrfs_root_node(root);
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001478out:
Miao Xiecb517ea2013-05-15 07:48:19 +00001479 btrfs_free_path(path);
1480 return root;
1481
Miao Xiecb517ea2013-05-15 07:48:19 +00001482find_fail:
1483 kfree(root);
1484alloc_fail:
1485 root = ERR_PTR(ret);
1486 goto out;
1487}
1488
1489struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
1490 struct btrfs_key *location)
1491{
1492 struct btrfs_root *root;
1493
1494 root = btrfs_read_tree_root(tree_root, location);
1495 if (IS_ERR(root))
1496 return root;
1497
1498 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie27cdeb72014-04-02 19:51:05 +08001499 set_bit(BTRFS_ROOT_REF_COWS, &root->state);
Li Zefan08fe4db2011-03-28 02:01:25 +00001500 btrfs_check_and_init_root_item(&root->root_item);
1501 }
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001502
Chris Mason5eda7b52007-06-22 14:16:25 -04001503 return root;
1504}
1505
Miao Xiecb517ea2013-05-15 07:48:19 +00001506int btrfs_init_fs_root(struct btrfs_root *root)
1507{
1508 int ret;
Miao Xie8257b2d2014-03-06 13:38:19 +08001509 struct btrfs_subvolume_writers *writers;
Miao Xiecb517ea2013-05-15 07:48:19 +00001510
1511 root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
1512 root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
1513 GFP_NOFS);
1514 if (!root->free_ino_pinned || !root->free_ino_ctl) {
1515 ret = -ENOMEM;
1516 goto fail;
1517 }
1518
Miao Xie8257b2d2014-03-06 13:38:19 +08001519 writers = btrfs_alloc_subvolume_writers();
1520 if (IS_ERR(writers)) {
1521 ret = PTR_ERR(writers);
1522 goto fail;
1523 }
1524 root->subv_writers = writers;
1525
Miao Xiecb517ea2013-05-15 07:48:19 +00001526 btrfs_init_free_ino_ctl(root);
David Sterba57cdc8d2014-02-05 02:37:48 +01001527 spin_lock_init(&root->ino_cache_lock);
1528 init_waitqueue_head(&root->ino_cache_wait);
Miao Xiecb517ea2013-05-15 07:48:19 +00001529
1530 ret = get_anon_bdev(&root->anon_dev);
1531 if (ret)
Liu Bo876d2cf2016-06-28 13:44:38 -07001532 goto fail;
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301533
1534 mutex_lock(&root->objectid_mutex);
1535 ret = btrfs_find_highest_objectid(root,
1536 &root->highest_objectid);
1537 if (ret) {
1538 mutex_unlock(&root->objectid_mutex);
Liu Bo876d2cf2016-06-28 13:44:38 -07001539 goto fail;
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301540 }
1541
1542 ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
1543
1544 mutex_unlock(&root->objectid_mutex);
1545
Miao Xiecb517ea2013-05-15 07:48:19 +00001546 return 0;
1547fail:
Liu Bo876d2cf2016-06-28 13:44:38 -07001548 /* the caller is responsible to call free_fs_root */
Miao Xiecb517ea2013-05-15 07:48:19 +00001549 return ret;
1550}
1551
Jeff Mahoney35bbb972016-08-17 21:58:33 -04001552struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1553 u64 root_id)
Miao Xiecb517ea2013-05-15 07:48:19 +00001554{
1555 struct btrfs_root *root;
1556
1557 spin_lock(&fs_info->fs_roots_radix_lock);
1558 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1559 (unsigned long)root_id);
1560 spin_unlock(&fs_info->fs_roots_radix_lock);
1561 return root;
1562}
1563
1564int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1565 struct btrfs_root *root)
1566{
1567 int ret;
1568
David Sterbae1860a72016-05-09 14:11:38 +02001569 ret = radix_tree_preload(GFP_NOFS);
Miao Xiecb517ea2013-05-15 07:48:19 +00001570 if (ret)
1571 return ret;
1572
1573 spin_lock(&fs_info->fs_roots_radix_lock);
1574 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1575 (unsigned long)root->root_key.objectid,
1576 root);
1577 if (ret == 0)
Miao Xie27cdeb72014-04-02 19:51:05 +08001578 set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
Miao Xiecb517ea2013-05-15 07:48:19 +00001579 spin_unlock(&fs_info->fs_roots_radix_lock);
1580 radix_tree_preload_end();
1581
1582 return ret;
1583}
1584
Miao Xiec00869f2013-09-25 21:47:44 +08001585struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1586 struct btrfs_key *location,
1587 bool check_ref)
Chris Mason5eda7b52007-06-22 14:16:25 -04001588{
1589 struct btrfs_root *root;
David Sterba381cf652015-01-02 18:45:16 +01001590 struct btrfs_path *path;
David Sterba1d4c08e2015-01-02 19:36:14 +01001591 struct btrfs_key key;
Chris Mason5eda7b52007-06-22 14:16:25 -04001592 int ret;
1593
Chris Masonedbd8d42007-12-21 16:27:24 -05001594 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1595 return fs_info->tree_root;
1596 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1597 return fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001598 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1599 return fs_info->chunk_root;
1600 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1601 return fs_info->dev_root;
Yan Zheng0403e472008-12-10 20:32:51 -05001602 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1603 return fs_info->csum_root;
Arne Jansenbcef60f2011-09-13 15:23:30 +02001604 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
1605 return fs_info->quota_root ? fs_info->quota_root :
1606 ERR_PTR(-ENOENT);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02001607 if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
1608 return fs_info->uuid_root ? fs_info->uuid_root :
1609 ERR_PTR(-ENOENT);
Omar Sandoval70f6d822015-09-29 20:50:38 -07001610 if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
1611 return fs_info->free_space_root ? fs_info->free_space_root :
1612 ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001613again:
Miao Xiecb517ea2013-05-15 07:48:19 +00001614 root = btrfs_lookup_fs_root(fs_info, location->objectid);
Stefan Behrens48475472013-08-23 10:34:42 +02001615 if (root) {
Miao Xiec00869f2013-09-25 21:47:44 +08001616 if (check_ref && btrfs_root_refs(&root->root_item) == 0)
Stefan Behrens48475472013-08-23 10:34:42 +02001617 return ERR_PTR(-ENOENT);
Chris Mason5eda7b52007-06-22 14:16:25 -04001618 return root;
Stefan Behrens48475472013-08-23 10:34:42 +02001619 }
Chris Mason5eda7b52007-06-22 14:16:25 -04001620
Miao Xiecb517ea2013-05-15 07:48:19 +00001621 root = btrfs_read_fs_root(fs_info->tree_root, location);
Chris Mason5eda7b52007-06-22 14:16:25 -04001622 if (IS_ERR(root))
1623 return root;
Chris Mason3394e162008-11-17 20:42:26 -05001624
Miao Xiec00869f2013-09-25 21:47:44 +08001625 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04001626 ret = -ENOENT;
1627 goto fail;
1628 }
1629
Miao Xiecb517ea2013-05-15 07:48:19 +00001630 ret = btrfs_init_fs_root(root);
1631 if (ret)
1632 goto fail;
1633
David Sterba381cf652015-01-02 18:45:16 +01001634 path = btrfs_alloc_path();
1635 if (!path) {
1636 ret = -ENOMEM;
1637 goto fail;
1638 }
David Sterba1d4c08e2015-01-02 19:36:14 +01001639 key.objectid = BTRFS_ORPHAN_OBJECTID;
1640 key.type = BTRFS_ORPHAN_ITEM_KEY;
1641 key.offset = location->objectid;
1642
1643 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
David Sterba381cf652015-01-02 18:45:16 +01001644 btrfs_free_path(path);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001645 if (ret < 0)
1646 goto fail;
1647 if (ret == 0)
Miao Xie27cdeb72014-04-02 19:51:05 +08001648 set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001649
Miao Xiecb517ea2013-05-15 07:48:19 +00001650 ret = btrfs_insert_fs_root(fs_info, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001651 if (ret) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001652 if (ret == -EEXIST) {
1653 free_fs_root(root);
1654 goto again;
1655 }
1656 goto fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001657 }
Chris Masonedbd8d42007-12-21 16:27:24 -05001658 return root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001659fail:
1660 free_fs_root(root);
1661 return ERR_PTR(ret);
Chris Masonedbd8d42007-12-21 16:27:24 -05001662}
1663
Chris Mason04160082008-03-26 10:28:07 -04001664static int btrfs_congested_fn(void *congested_data, int bdi_bits)
1665{
1666 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
1667 int ret = 0;
Chris Mason04160082008-03-26 10:28:07 -04001668 struct btrfs_device *device;
1669 struct backing_dev_info *bdi;
Chris Masonb7967db2009-04-27 07:29:04 -04001670
Xiao Guangrong1f781602011-04-20 10:09:16 +00001671 rcu_read_lock();
1672 list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001673 if (!device->bdev)
1674 continue;
Jan Karab8439982017-02-02 15:56:53 +01001675 bdi = device->bdev->bd_bdi;
Tejun Heoff9ea322014-09-08 08:03:56 +09001676 if (bdi_congested(bdi, bdi_bits)) {
Chris Mason04160082008-03-26 10:28:07 -04001677 ret = 1;
1678 break;
1679 }
1680 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001681 rcu_read_unlock();
Chris Mason04160082008-03-26 10:28:07 -04001682 return ret;
1683}
1684
Chris Mason04160082008-03-26 10:28:07 -04001685static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1686{
Jens Axboead081f12009-06-12 14:43:40 +02001687 int err;
1688
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001689 err = bdi_setup_and_register(bdi, "btrfs");
Jens Axboead081f12009-06-12 14:43:40 +02001690 if (err)
1691 return err;
1692
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001693 bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
Chris Mason04160082008-03-26 10:28:07 -04001694 bdi->congested_fn = btrfs_congested_fn;
1695 bdi->congested_data = info;
Chris Masonda2f0f72015-07-02 13:57:22 -07001696 bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK;
Chris Mason04160082008-03-26 10:28:07 -04001697 return 0;
1698}
1699
Chris Mason8b712842008-06-11 16:50:36 -04001700/*
1701 * called by the kthread helper functions to finally call the bio end_io
1702 * functions. This is where read checksum verification actually happens
1703 */
1704static void end_workqueue_fn(struct btrfs_work *work)
Chris Masonce9adaa2008-04-09 16:28:12 -04001705{
Chris Masonce9adaa2008-04-09 16:28:12 -04001706 struct bio *bio;
David Sterba97eb6b62014-07-30 00:55:42 +02001707 struct btrfs_end_io_wq *end_io_wq;
Chris Masonce9adaa2008-04-09 16:28:12 -04001708
David Sterba97eb6b62014-07-30 00:55:42 +02001709 end_io_wq = container_of(work, struct btrfs_end_io_wq, work);
Chris Mason8b712842008-06-11 16:50:36 -04001710 bio = end_io_wq->bio;
Chris Masonce9adaa2008-04-09 16:28:12 -04001711
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001712 bio->bi_error = end_io_wq->error;
Chris Mason8b712842008-06-11 16:50:36 -04001713 bio->bi_private = end_io_wq->private;
1714 bio->bi_end_io = end_io_wq->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001715 bio_endio(bio);
Omar Sandoval2d3ff932019-09-16 11:30:54 -07001716 kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
Chris Mason44b8bd72008-04-16 11:14:51 -04001717}
1718
Chris Masona74a4b92008-06-25 16:01:31 -04001719static int cleaner_kthread(void *arg)
1720{
1721 struct btrfs_root *root = arg;
Miao Xied0278242013-05-14 10:20:40 +00001722 int again;
Filipe Mananada288d22015-06-13 06:55:31 +01001723 struct btrfs_trans_handle *trans;
Chris Masona74a4b92008-06-25 16:01:31 -04001724
1725 do {
Miao Xied0278242013-05-14 10:20:40 +00001726 again = 0;
David Sterba9d1a2a32013-03-12 15:13:28 +00001727
Miao Xied0278242013-05-14 10:20:40 +00001728 /* Make the cleaner go to sleep early. */
Miao Xiebabbf1702013-05-14 10:20:43 +00001729 if (btrfs_need_cleaner_sleep(root))
Miao Xied0278242013-05-14 10:20:40 +00001730 goto sleep;
Chris Masona74a4b92008-06-25 16:01:31 -04001731
Zygo Blaxell90c711a2016-06-12 23:39:58 -04001732 /*
1733 * Do not do anything if we might cause open_ctree() to block
1734 * before we have finished mounting the filesystem.
1735 */
Josef Bacikafcdd122016-09-02 15:40:02 -04001736 if (!test_bit(BTRFS_FS_OPEN, &root->fs_info->flags))
Zygo Blaxell90c711a2016-06-12 23:39:58 -04001737 goto sleep;
1738
Miao Xied0278242013-05-14 10:20:40 +00001739 if (!mutex_trylock(&root->fs_info->cleaner_mutex))
1740 goto sleep;
1741
Miao Xiedc7f3702013-05-14 10:20:42 +00001742 /*
1743 * Avoid the problem that we change the status of the fs
1744 * during the above check and trylock.
1745 */
Miao Xiebabbf1702013-05-14 10:20:43 +00001746 if (btrfs_need_cleaner_sleep(root)) {
Miao Xiedc7f3702013-05-14 10:20:42 +00001747 mutex_unlock(&root->fs_info->cleaner_mutex);
1748 goto sleep;
Chris Masona74a4b92008-06-25 16:01:31 -04001749 }
1750
Filipe Mananac2d6cb12016-01-15 11:05:12 +00001751 mutex_lock(&root->fs_info->cleaner_delayed_iput_mutex);
Miao Xied0278242013-05-14 10:20:40 +00001752 btrfs_run_delayed_iputs(root);
Filipe Mananac2d6cb12016-01-15 11:05:12 +00001753 mutex_unlock(&root->fs_info->cleaner_delayed_iput_mutex);
1754
Miao Xied0278242013-05-14 10:20:40 +00001755 again = btrfs_clean_one_deleted_snapshot(root);
1756 mutex_unlock(&root->fs_info->cleaner_mutex);
1757
1758 /*
Miao Xie05323cd2013-05-14 10:20:41 +00001759 * The defragger has dealt with the R/O remount and umount,
1760 * needn't do anything special here.
Miao Xied0278242013-05-14 10:20:40 +00001761 */
1762 btrfs_run_defrag_inodes(root->fs_info);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01001763
1764 /*
1765 * Acquires fs_info->delete_unused_bgs_mutex to avoid racing
1766 * with relocation (btrfs_relocate_chunk) and relocation
1767 * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
1768 * after acquiring fs_info->delete_unused_bgs_mutex. So we
1769 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
1770 * unused block groups.
1771 */
1772 btrfs_delete_unused_bgs(root->fs_info);
Miao Xied0278242013-05-14 10:20:40 +00001773sleep:
Jiri Kosina838fe182016-03-15 11:28:54 +01001774 if (!again) {
Chris Masona74a4b92008-06-25 16:01:31 -04001775 set_current_state(TASK_INTERRUPTIBLE);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001776 if (!kthread_should_stop())
1777 schedule();
Chris Masona74a4b92008-06-25 16:01:31 -04001778 __set_current_state(TASK_RUNNING);
1779 }
1780 } while (!kthread_should_stop());
Filipe Mananada288d22015-06-13 06:55:31 +01001781
1782 /*
1783 * Transaction kthread is stopped before us and wakes us up.
1784 * However we might have started a new transaction and COWed some
1785 * tree blocks when deleting unused block groups for example. So
1786 * make sure we commit the transaction we started to have a clean
1787 * shutdown when evicting the btree inode - if it has dirty pages
1788 * when we do the final iput() on it, eviction will trigger a
1789 * writeback for it which will fail with null pointer dereferences
1790 * since work queues and other resources were already released and
1791 * destroyed by the time the iput/eviction/writeback is made.
1792 */
1793 trans = btrfs_attach_transaction(root);
1794 if (IS_ERR(trans)) {
1795 if (PTR_ERR(trans) != -ENOENT)
1796 btrfs_err(root->fs_info,
1797 "cleaner transaction attach returned %ld",
1798 PTR_ERR(trans));
1799 } else {
1800 int ret;
1801
1802 ret = btrfs_commit_transaction(trans, root);
1803 if (ret)
1804 btrfs_err(root->fs_info,
1805 "cleaner open transaction commit returned %d",
1806 ret);
1807 }
1808
Chris Masona74a4b92008-06-25 16:01:31 -04001809 return 0;
1810}
1811
1812static int transaction_kthread(void *arg)
1813{
1814 struct btrfs_root *root = arg;
1815 struct btrfs_trans_handle *trans;
1816 struct btrfs_transaction *cur;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001817 u64 transid;
Chris Masona74a4b92008-06-25 16:01:31 -04001818 unsigned long now;
1819 unsigned long delay;
Jan Kara914b2002012-03-12 16:05:50 +01001820 bool cannot_commit;
Chris Masona74a4b92008-06-25 16:01:31 -04001821
1822 do {
Jan Kara914b2002012-03-12 16:05:50 +01001823 cannot_commit = false;
David Sterba8b87dc12013-08-01 18:14:52 +02001824 delay = HZ * root->fs_info->commit_interval;
Chris Masona74a4b92008-06-25 16:01:31 -04001825 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1826
Josef Bacika4abeea2011-04-11 17:25:13 -04001827 spin_lock(&root->fs_info->trans_lock);
Chris Masona74a4b92008-06-25 16:01:31 -04001828 cur = root->fs_info->running_transaction;
1829 if (!cur) {
Josef Bacika4abeea2011-04-11 17:25:13 -04001830 spin_unlock(&root->fs_info->trans_lock);
Chris Masona74a4b92008-06-25 16:01:31 -04001831 goto sleep;
1832 }
Yan Zheng31153d82008-07-28 15:32:19 -04001833
Chris Masona74a4b92008-06-25 16:01:31 -04001834 now = get_seconds();
Miao Xie4a9d8bd2013-05-17 03:53:43 +00001835 if (cur->state < TRANS_STATE_BLOCKED &&
David Sterba8b87dc12013-08-01 18:14:52 +02001836 (now < cur->start_time ||
1837 now - cur->start_time < root->fs_info->commit_interval)) {
Josef Bacika4abeea2011-04-11 17:25:13 -04001838 spin_unlock(&root->fs_info->trans_lock);
Chris Masona74a4b92008-06-25 16:01:31 -04001839 delay = HZ * 5;
1840 goto sleep;
1841 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001842 transid = cur->transid;
Josef Bacika4abeea2011-04-11 17:25:13 -04001843 spin_unlock(&root->fs_info->trans_lock);
Chris Mason56bec292009-03-13 10:10:06 -04001844
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001845 /* If the file system is aborted, this will always fail. */
Miao Xie354aa0f2012-09-20 01:54:00 -06001846 trans = btrfs_attach_transaction(root);
Jan Kara914b2002012-03-12 16:05:50 +01001847 if (IS_ERR(trans)) {
Miao Xie354aa0f2012-09-20 01:54:00 -06001848 if (PTR_ERR(trans) != -ENOENT)
1849 cannot_commit = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001850 goto sleep;
Jan Kara914b2002012-03-12 16:05:50 +01001851 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001852 if (transid == trans->transid) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001853 btrfs_commit_transaction(trans, root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001854 } else {
1855 btrfs_end_transaction(trans, root);
1856 }
Chris Masona74a4b92008-06-25 16:01:31 -04001857sleep:
1858 wake_up_process(root->fs_info->cleaner_kthread);
1859 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
1860
Josef Bacik4e121c02013-09-27 16:32:39 -04001861 if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
1862 &root->fs_info->fs_state)))
1863 btrfs_cleanup_transaction(root);
Jiri Kosinace63f892016-03-15 11:28:59 +01001864 set_current_state(TASK_INTERRUPTIBLE);
1865 if (!kthread_should_stop() &&
1866 (!btrfs_transaction_blocked(root->fs_info) ||
1867 cannot_commit))
1868 schedule_timeout(delay);
1869 __set_current_state(TASK_RUNNING);
Chris Masona74a4b92008-06-25 16:01:31 -04001870 } while (!kthread_should_stop());
1871 return 0;
1872}
1873
Chris Masonaf31f5e2011-11-03 15:17:42 -04001874/*
1875 * this will find the highest generation in the array of
1876 * root backups. The index of the highest array is returned,
1877 * or -1 if we can't find anything.
1878 *
1879 * We check to make sure the array is valid by comparing the
1880 * generation of the latest root in the array with the generation
1881 * in the super block. If they don't match we pitch it.
1882 */
1883static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
1884{
1885 u64 cur;
1886 int newest_index = -1;
1887 struct btrfs_root_backup *root_backup;
1888 int i;
1889
1890 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
1891 root_backup = info->super_copy->super_roots + i;
1892 cur = btrfs_backup_tree_root_gen(root_backup);
1893 if (cur == newest_gen)
1894 newest_index = i;
1895 }
1896
1897 /* check to see if we actually wrapped around */
1898 if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) {
1899 root_backup = info->super_copy->super_roots;
1900 cur = btrfs_backup_tree_root_gen(root_backup);
1901 if (cur == newest_gen)
1902 newest_index = 0;
1903 }
1904 return newest_index;
1905}
1906
1907
1908/*
1909 * find the oldest backup so we know where to store new entries
1910 * in the backup array. This will set the backup_root_index
1911 * field in the fs_info struct
1912 */
1913static void find_oldest_super_backup(struct btrfs_fs_info *info,
1914 u64 newest_gen)
1915{
1916 int newest_index = -1;
1917
1918 newest_index = find_newest_super_backup(info, newest_gen);
1919 /* if there was garbage in there, just move along */
1920 if (newest_index == -1) {
1921 info->backup_root_index = 0;
1922 } else {
1923 info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS;
1924 }
1925}
1926
1927/*
1928 * copy all the root pointers into the super backup array.
1929 * this will bump the backup pointer by one when it is
1930 * done
1931 */
1932static void backup_super_roots(struct btrfs_fs_info *info)
1933{
1934 int next_backup;
1935 struct btrfs_root_backup *root_backup;
1936 int last_backup;
1937
1938 next_backup = info->backup_root_index;
1939 last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) %
1940 BTRFS_NUM_BACKUP_ROOTS;
1941
1942 /*
1943 * just overwrite the last backup if we're at the same generation
1944 * this happens only at umount
1945 */
1946 root_backup = info->super_for_commit->super_roots + last_backup;
1947 if (btrfs_backup_tree_root_gen(root_backup) ==
1948 btrfs_header_generation(info->tree_root->node))
1949 next_backup = last_backup;
1950
1951 root_backup = info->super_for_commit->super_roots + next_backup;
1952
1953 /*
1954 * make sure all of our padding and empty slots get zero filled
1955 * regardless of which ones we use today
1956 */
1957 memset(root_backup, 0, sizeof(*root_backup));
1958
1959 info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
1960
1961 btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
1962 btrfs_set_backup_tree_root_gen(root_backup,
1963 btrfs_header_generation(info->tree_root->node));
1964
1965 btrfs_set_backup_tree_root_level(root_backup,
1966 btrfs_header_level(info->tree_root->node));
1967
1968 btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
1969 btrfs_set_backup_chunk_root_gen(root_backup,
1970 btrfs_header_generation(info->chunk_root->node));
1971 btrfs_set_backup_chunk_root_level(root_backup,
1972 btrfs_header_level(info->chunk_root->node));
1973
1974 btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start);
1975 btrfs_set_backup_extent_root_gen(root_backup,
1976 btrfs_header_generation(info->extent_root->node));
1977 btrfs_set_backup_extent_root_level(root_backup,
1978 btrfs_header_level(info->extent_root->node));
1979
Chris Mason7c7e82a2011-11-06 18:50:56 -05001980 /*
1981 * we might commit during log recovery, which happens before we set
1982 * the fs_root. Make sure it is valid before we fill it in.
1983 */
1984 if (info->fs_root && info->fs_root->node) {
1985 btrfs_set_backup_fs_root(root_backup,
1986 info->fs_root->node->start);
1987 btrfs_set_backup_fs_root_gen(root_backup,
Chris Masonaf31f5e2011-11-03 15:17:42 -04001988 btrfs_header_generation(info->fs_root->node));
Chris Mason7c7e82a2011-11-06 18:50:56 -05001989 btrfs_set_backup_fs_root_level(root_backup,
Chris Masonaf31f5e2011-11-03 15:17:42 -04001990 btrfs_header_level(info->fs_root->node));
Chris Mason7c7e82a2011-11-06 18:50:56 -05001991 }
Chris Masonaf31f5e2011-11-03 15:17:42 -04001992
1993 btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
1994 btrfs_set_backup_dev_root_gen(root_backup,
1995 btrfs_header_generation(info->dev_root->node));
1996 btrfs_set_backup_dev_root_level(root_backup,
1997 btrfs_header_level(info->dev_root->node));
1998
1999 btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start);
2000 btrfs_set_backup_csum_root_gen(root_backup,
2001 btrfs_header_generation(info->csum_root->node));
2002 btrfs_set_backup_csum_root_level(root_backup,
2003 btrfs_header_level(info->csum_root->node));
2004
2005 btrfs_set_backup_total_bytes(root_backup,
2006 btrfs_super_total_bytes(info->super_copy));
2007 btrfs_set_backup_bytes_used(root_backup,
2008 btrfs_super_bytes_used(info->super_copy));
2009 btrfs_set_backup_num_devices(root_backup,
2010 btrfs_super_num_devices(info->super_copy));
2011
2012 /*
2013 * if we don't copy this out to the super_copy, it won't get remembered
2014 * for the next commit
2015 */
2016 memcpy(&info->super_copy->super_roots,
2017 &info->super_for_commit->super_roots,
2018 sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
2019}
2020
2021/*
2022 * this copies info out of the root backup array and back into
2023 * the in-memory super block. It is meant to help iterate through
2024 * the array, so you send it the number of backups you've already
2025 * tried and the last backup index you used.
2026 *
2027 * this returns -1 when it has tried all the backups
2028 */
2029static noinline int next_root_backup(struct btrfs_fs_info *info,
2030 struct btrfs_super_block *super,
2031 int *num_backups_tried, int *backup_index)
2032{
2033 struct btrfs_root_backup *root_backup;
2034 int newest = *backup_index;
2035
2036 if (*num_backups_tried == 0) {
2037 u64 gen = btrfs_super_generation(super);
2038
2039 newest = find_newest_super_backup(info, gen);
2040 if (newest == -1)
2041 return -1;
2042
2043 *backup_index = newest;
2044 *num_backups_tried = 1;
2045 } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) {
2046 /* we've tried all the backups, all done */
2047 return -1;
2048 } else {
2049 /* jump to the next oldest backup */
2050 newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) %
2051 BTRFS_NUM_BACKUP_ROOTS;
2052 *backup_index = newest;
2053 *num_backups_tried += 1;
2054 }
2055 root_backup = super->super_roots + newest;
2056
2057 btrfs_set_super_generation(super,
2058 btrfs_backup_tree_root_gen(root_backup));
2059 btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
2060 btrfs_set_super_root_level(super,
2061 btrfs_backup_tree_root_level(root_backup));
2062 btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
2063
2064 /*
2065 * fixme: the total bytes and num_devices need to match or we should
2066 * need a fsck
2067 */
2068 btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
2069 btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
2070 return 0;
2071}
2072
Liu Bo7abadb62013-03-17 02:10:31 +00002073/* helper to cleanup workers */
2074static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
2075{
Qu Wenruodc6e3202014-02-28 10:46:14 +08002076 btrfs_destroy_workqueue(fs_info->fixup_workers);
Qu Wenruoafe3d242014-02-28 10:46:07 +08002077 btrfs_destroy_workqueue(fs_info->delalloc_workers);
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +08002078 btrfs_destroy_workqueue(fs_info->workers);
Qu Wenruofccb5d82014-02-28 10:46:10 +08002079 btrfs_destroy_workqueue(fs_info->endio_workers);
2080 btrfs_destroy_workqueue(fs_info->endio_meta_workers);
2081 btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
Miao Xie8b110e32014-09-12 18:44:03 +08002082 btrfs_destroy_workqueue(fs_info->endio_repair_workers);
Qu Wenruod05a33a2014-02-28 10:46:11 +08002083 btrfs_destroy_workqueue(fs_info->rmw_workers);
Qu Wenruofccb5d82014-02-28 10:46:10 +08002084 btrfs_destroy_workqueue(fs_info->endio_meta_write_workers);
2085 btrfs_destroy_workqueue(fs_info->endio_write_workers);
2086 btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
Qu Wenruoa8c93d42014-02-28 10:46:08 +08002087 btrfs_destroy_workqueue(fs_info->submit_workers);
Qu Wenruo5b3bc442014-02-28 10:46:15 +08002088 btrfs_destroy_workqueue(fs_info->delayed_workers);
Qu Wenruoe66f0bb2014-02-28 10:46:12 +08002089 btrfs_destroy_workqueue(fs_info->caching_workers);
Qu Wenruo736cfa12014-02-28 10:46:13 +08002090 btrfs_destroy_workqueue(fs_info->readahead_workers);
Qu Wenruoa44903a2014-02-28 10:46:09 +08002091 btrfs_destroy_workqueue(fs_info->flush_workers);
Qu Wenruofc97fab2014-02-28 10:46:16 +08002092 btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
Chris Masona79b7d42014-05-22 16:18:52 -07002093 btrfs_destroy_workqueue(fs_info->extent_workers);
Liu Bo7abadb62013-03-17 02:10:31 +00002094}
2095
Rashika2e9f5952013-10-31 02:45:20 +05302096static void free_root_extent_buffers(struct btrfs_root *root)
2097{
2098 if (root) {
2099 free_extent_buffer(root->node);
2100 free_extent_buffer(root->commit_root);
2101 root->node = NULL;
2102 root->commit_root = NULL;
2103 }
2104}
2105
Chris Masonaf31f5e2011-11-03 15:17:42 -04002106/* helper to cleanup tree roots */
Anand Jainfb8c8122019-10-10 10:39:25 +08002107static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
Chris Masonaf31f5e2011-11-03 15:17:42 -04002108{
Rashika2e9f5952013-10-31 02:45:20 +05302109 free_root_extent_buffers(info->tree_root);
Josef Bacik655b09f2013-05-17 14:06:51 -04002110
Rashika2e9f5952013-10-31 02:45:20 +05302111 free_root_extent_buffers(info->dev_root);
2112 free_root_extent_buffers(info->extent_root);
2113 free_root_extent_buffers(info->csum_root);
2114 free_root_extent_buffers(info->quota_root);
2115 free_root_extent_buffers(info->uuid_root);
Anand Jainfb8c8122019-10-10 10:39:25 +08002116 if (free_chunk_root)
Rashika2e9f5952013-10-31 02:45:20 +05302117 free_root_extent_buffers(info->chunk_root);
Omar Sandoval70f6d822015-09-29 20:50:38 -07002118 free_root_extent_buffers(info->free_space_root);
Chris Masonaf31f5e2011-11-03 15:17:42 -04002119}
2120
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002121void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
Josef Bacik171f6532013-04-24 16:35:41 -04002122{
2123 int ret;
2124 struct btrfs_root *gang[8];
2125 int i;
2126
2127 while (!list_empty(&fs_info->dead_roots)) {
2128 gang[0] = list_entry(fs_info->dead_roots.next,
2129 struct btrfs_root, root_list);
2130 list_del(&gang[0]->root_list);
2131
Miao Xie27cdeb72014-04-02 19:51:05 +08002132 if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) {
Miao Xiecb517ea2013-05-15 07:48:19 +00002133 btrfs_drop_and_free_fs_root(fs_info, gang[0]);
Josef Bacik171f6532013-04-24 16:35:41 -04002134 } else {
2135 free_extent_buffer(gang[0]->node);
2136 free_extent_buffer(gang[0]->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00002137 btrfs_put_fs_root(gang[0]);
Josef Bacik171f6532013-04-24 16:35:41 -04002138 }
2139 }
2140
2141 while (1) {
2142 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2143 (void **)gang, 0,
2144 ARRAY_SIZE(gang));
2145 if (!ret)
2146 break;
2147 for (i = 0; i < ret; i++)
Miao Xiecb517ea2013-05-15 07:48:19 +00002148 btrfs_drop_and_free_fs_root(fs_info, gang[i]);
Josef Bacik171f6532013-04-24 16:35:41 -04002149 }
Liu Bo1a4319c2014-01-13 19:53:53 +08002150
2151 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
2152 btrfs_free_log_root_tree(NULL, fs_info);
2153 btrfs_destroy_pinned_extent(fs_info->tree_root,
2154 fs_info->pinned_extents);
2155 }
Josef Bacik171f6532013-04-24 16:35:41 -04002156}
Chris Masonaf31f5e2011-11-03 15:17:42 -04002157
Eric Sandeen638aa7e2014-08-01 18:12:38 -05002158static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
2159{
2160 mutex_init(&fs_info->scrub_lock);
2161 atomic_set(&fs_info->scrubs_running, 0);
2162 atomic_set(&fs_info->scrub_pause_req, 0);
2163 atomic_set(&fs_info->scrubs_paused, 0);
2164 atomic_set(&fs_info->scrub_cancel_req, 0);
2165 init_waitqueue_head(&fs_info->scrub_pause_wait);
2166 fs_info->scrub_workers_refcnt = 0;
2167}
2168
Eric Sandeen779a65a2014-08-01 18:12:39 -05002169static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
2170{
2171 spin_lock_init(&fs_info->balance_lock);
2172 mutex_init(&fs_info->balance_mutex);
2173 atomic_set(&fs_info->balance_running, 0);
2174 atomic_set(&fs_info->balance_pause_req, 0);
2175 atomic_set(&fs_info->balance_cancel_req, 0);
2176 fs_info->balance_ctl = NULL;
2177 init_waitqueue_head(&fs_info->balance_wait_q);
2178}
2179
Eric Sandeenf37938e2014-08-01 18:12:40 -05002180static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info,
2181 struct btrfs_root *tree_root)
2182{
2183 fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
2184 set_nlink(fs_info->btree_inode, 1);
2185 /*
2186 * we set the i_size on the btree inode to the max possible int.
2187 * the real end of the address space is determined by all of
2188 * the devices in the system
2189 */
2190 fs_info->btree_inode->i_size = OFFSET_MAX;
2191 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Eric Sandeenf37938e2014-08-01 18:12:40 -05002192
2193 RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node);
2194 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
2195 fs_info->btree_inode->i_mapping);
2196 BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0;
2197 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree);
2198
2199 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
2200
2201 BTRFS_I(fs_info->btree_inode)->root = tree_root;
2202 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
2203 sizeof(struct btrfs_key));
2204 set_bit(BTRFS_INODE_DUMMY,
2205 &BTRFS_I(fs_info->btree_inode)->runtime_flags);
2206 btrfs_insert_inode_hash(fs_info->btree_inode);
2207}
2208
Eric Sandeenad618362014-08-01 18:12:41 -05002209static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
2210{
2211 fs_info->dev_replace.lock_owner = 0;
2212 atomic_set(&fs_info->dev_replace.nesting_level, 0);
2213 mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
Liu Bo73beece2015-07-17 16:49:19 +08002214 rwlock_init(&fs_info->dev_replace.lock);
2215 atomic_set(&fs_info->dev_replace.read_locks, 0);
2216 atomic_set(&fs_info->dev_replace.blocking_readers, 0);
Eric Sandeenad618362014-08-01 18:12:41 -05002217 init_waitqueue_head(&fs_info->replace_wait);
Liu Bo73beece2015-07-17 16:49:19 +08002218 init_waitqueue_head(&fs_info->dev_replace.read_lock_wq);
Eric Sandeenad618362014-08-01 18:12:41 -05002219}
2220
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002221static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
2222{
2223 spin_lock_init(&fs_info->qgroup_lock);
2224 mutex_init(&fs_info->qgroup_ioctl_lock);
2225 fs_info->qgroup_tree = RB_ROOT;
2226 fs_info->qgroup_op_tree = RB_ROOT;
2227 INIT_LIST_HEAD(&fs_info->dirty_qgroups);
2228 fs_info->qgroup_seq = 1;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002229 fs_info->qgroup_ulist = NULL;
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002230 fs_info->qgroup_rescan_running = false;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002231 mutex_init(&fs_info->qgroup_rescan_lock);
2232}
2233
Eric Sandeen2a458192015-02-16 16:29:26 +01002234static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
2235 struct btrfs_fs_devices *fs_devices)
2236{
2237 int max_active = fs_info->thread_pool_size;
David Sterba6f011052015-02-16 18:34:01 +01002238 unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
Eric Sandeen2a458192015-02-16 16:29:26 +01002239
2240 fs_info->workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002241 btrfs_alloc_workqueue(fs_info, "worker",
2242 flags | WQ_HIGHPRI, max_active, 16);
Eric Sandeen2a458192015-02-16 16:29:26 +01002243
2244 fs_info->delalloc_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002245 btrfs_alloc_workqueue(fs_info, "delalloc",
2246 flags, max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002247
2248 fs_info->flush_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002249 btrfs_alloc_workqueue(fs_info, "flush_delalloc",
2250 flags, max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002251
2252 fs_info->caching_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002253 btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002254
2255 /*
2256 * a higher idle thresh on the submit workers makes it much more
2257 * likely that bios will be send down in a sane order to the
2258 * devices
2259 */
2260 fs_info->submit_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002261 btrfs_alloc_workqueue(fs_info, "submit", flags,
Eric Sandeen2a458192015-02-16 16:29:26 +01002262 min_t(u64, fs_devices->num_devices,
2263 max_active), 64);
2264
2265 fs_info->fixup_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002266 btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002267
2268 /*
2269 * endios are largely parallel and should have a very
2270 * low idle thresh
2271 */
2272 fs_info->endio_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002273 btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4);
Eric Sandeen2a458192015-02-16 16:29:26 +01002274 fs_info->endio_meta_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002275 btrfs_alloc_workqueue(fs_info, "endio-meta", flags,
2276 max_active, 4);
Eric Sandeen2a458192015-02-16 16:29:26 +01002277 fs_info->endio_meta_write_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002278 btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags,
2279 max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002280 fs_info->endio_raid56_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002281 btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,
2282 max_active, 4);
Eric Sandeen2a458192015-02-16 16:29:26 +01002283 fs_info->endio_repair_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002284 btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002285 fs_info->rmw_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002286 btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002287 fs_info->endio_write_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002288 btrfs_alloc_workqueue(fs_info, "endio-write", flags,
2289 max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002290 fs_info->endio_freespace_worker =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002291 btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
2292 max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002293 fs_info->delayed_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002294 btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
2295 max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002296 fs_info->readahead_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002297 btrfs_alloc_workqueue(fs_info, "readahead", flags,
2298 max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002299 fs_info->qgroup_rescan_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002300 btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002301 fs_info->extent_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002302 btrfs_alloc_workqueue(fs_info, "extent-refs", flags,
Eric Sandeen2a458192015-02-16 16:29:26 +01002303 min_t(u64, fs_devices->num_devices,
2304 max_active), 8);
2305
2306 if (!(fs_info->workers && fs_info->delalloc_workers &&
2307 fs_info->submit_workers && fs_info->flush_workers &&
2308 fs_info->endio_workers && fs_info->endio_meta_workers &&
2309 fs_info->endio_meta_write_workers &&
2310 fs_info->endio_repair_workers &&
2311 fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
2312 fs_info->endio_freespace_worker && fs_info->rmw_workers &&
2313 fs_info->caching_workers && fs_info->readahead_workers &&
2314 fs_info->fixup_workers && fs_info->delayed_workers &&
2315 fs_info->extent_workers &&
2316 fs_info->qgroup_rescan_workers)) {
2317 return -ENOMEM;
2318 }
2319
2320 return 0;
2321}
2322
Eric Sandeen63443bf2014-08-01 18:12:46 -05002323static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2324 struct btrfs_fs_devices *fs_devices)
2325{
2326 int ret;
2327 struct btrfs_root *tree_root = fs_info->tree_root;
2328 struct btrfs_root *log_tree_root;
2329 struct btrfs_super_block *disk_super = fs_info->super_copy;
2330 u64 bytenr = btrfs_super_log_root(disk_super);
2331
2332 if (fs_devices->rw_devices == 0) {
David Sterbaf14d1042015-10-08 11:37:06 +02002333 btrfs_warn(fs_info, "log replay required on RO media");
Eric Sandeen63443bf2014-08-01 18:12:46 -05002334 return -EIO;
2335 }
2336
David Sterba74e4d822016-02-11 11:01:55 +01002337 log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002338 if (!log_tree_root)
2339 return -ENOMEM;
2340
2341 __setup_root(tree_root->nodesize, tree_root->sectorsize,
2342 tree_root->stripesize, log_tree_root, fs_info,
2343 BTRFS_TREE_LOG_OBJECTID);
2344
2345 log_tree_root->node = read_tree_block(tree_root, bytenr,
2346 fs_info->generation + 1);
Liu Bo64c043d2015-05-25 17:30:15 +08002347 if (IS_ERR(log_tree_root->node)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002348 btrfs_warn(fs_info, "failed to read log tree");
Liu Bo0eeff232015-06-11 14:16:44 +08002349 ret = PTR_ERR(log_tree_root->node);
Liu Bo64c043d2015-05-25 17:30:15 +08002350 kfree(log_tree_root);
Liu Bo0eeff232015-06-11 14:16:44 +08002351 return ret;
Liu Bo64c043d2015-05-25 17:30:15 +08002352 } else if (!extent_buffer_uptodate(log_tree_root->node)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002353 btrfs_err(fs_info, "failed to read log tree");
Eric Sandeen63443bf2014-08-01 18:12:46 -05002354 free_extent_buffer(log_tree_root->node);
2355 kfree(log_tree_root);
2356 return -EIO;
2357 }
2358 /* returns with log_tree_root freed on success */
2359 ret = btrfs_recover_log_trees(log_tree_root);
2360 if (ret) {
Anand Jain34d97002016-03-16 16:43:06 +08002361 btrfs_handle_fs_error(tree_root->fs_info, ret,
Eric Sandeen63443bf2014-08-01 18:12:46 -05002362 "Failed to recover log tree");
2363 free_extent_buffer(log_tree_root->node);
2364 kfree(log_tree_root);
2365 return ret;
2366 }
2367
2368 if (fs_info->sb->s_flags & MS_RDONLY) {
2369 ret = btrfs_commit_super(tree_root);
2370 if (ret)
2371 return ret;
2372 }
2373
2374 return 0;
2375}
2376
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002377static int btrfs_read_roots(struct btrfs_fs_info *fs_info,
2378 struct btrfs_root *tree_root)
2379{
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002380 struct btrfs_root *root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002381 struct btrfs_key location;
2382 int ret;
2383
2384 location.objectid = BTRFS_EXTENT_TREE_OBJECTID;
2385 location.type = BTRFS_ROOT_ITEM_KEY;
2386 location.offset = 0;
2387
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002388 root = btrfs_read_tree_root(tree_root, &location);
2389 if (IS_ERR(root))
2390 return PTR_ERR(root);
2391 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2392 fs_info->extent_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002393
2394 location.objectid = BTRFS_DEV_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002395 root = btrfs_read_tree_root(tree_root, &location);
2396 if (IS_ERR(root))
2397 return PTR_ERR(root);
2398 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2399 fs_info->dev_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002400 btrfs_init_devices_late(fs_info);
2401
2402 location.objectid = BTRFS_CSUM_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002403 root = btrfs_read_tree_root(tree_root, &location);
2404 if (IS_ERR(root))
2405 return PTR_ERR(root);
2406 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2407 fs_info->csum_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002408
2409 location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002410 root = btrfs_read_tree_root(tree_root, &location);
2411 if (!IS_ERR(root)) {
2412 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
Josef Bacikafcdd122016-09-02 15:40:02 -04002413 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002414 fs_info->quota_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002415 }
2416
2417 location.objectid = BTRFS_UUID_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002418 root = btrfs_read_tree_root(tree_root, &location);
2419 if (IS_ERR(root)) {
2420 ret = PTR_ERR(root);
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002421 if (ret != -ENOENT)
2422 return ret;
2423 } else {
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002424 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2425 fs_info->uuid_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002426 }
2427
Omar Sandoval70f6d822015-09-29 20:50:38 -07002428 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2429 location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID;
2430 root = btrfs_read_tree_root(tree_root, &location);
2431 if (IS_ERR(root))
2432 return PTR_ERR(root);
2433 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2434 fs_info->free_space_root = root;
2435 }
2436
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002437 return 0;
2438}
2439
Al Viroad2b2c82011-11-17 01:10:02 -05002440int open_ctree(struct super_block *sb,
2441 struct btrfs_fs_devices *fs_devices,
2442 char *options)
Chris Masoneb60cea2007-02-02 09:18:22 -05002443{
Chris Masondb945352007-10-15 16:15:53 -04002444 u32 sectorsize;
2445 u32 nodesize;
Chris Mason87ee04e2007-11-30 11:30:34 -05002446 u32 stripesize;
Yan Zheng84234f32008-10-29 14:49:05 -04002447 u64 generation;
Josef Bacikf2b636e2008-12-02 06:36:08 -05002448 u64 features;
Chris Mason3de45862008-11-17 21:02:50 -05002449 struct btrfs_key location;
Chris Masona061fc82008-05-07 11:43:44 -04002450 struct buffer_head *bh;
Ilya Dryomov4d34b272011-11-09 00:08:15 +02002451 struct btrfs_super_block *disk_super;
Al Viro815745c2011-11-17 15:40:49 -05002452 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Al Virof84a8bd2011-11-17 15:57:57 -05002453 struct btrfs_root *tree_root;
Ilya Dryomov4d34b272011-11-09 00:08:15 +02002454 struct btrfs_root *chunk_root;
Chris Masoneb60cea2007-02-02 09:18:22 -05002455 int ret;
Yane58ca022008-04-01 11:21:34 -04002456 int err = -EINVAL;
Chris Masonaf31f5e2011-11-03 15:17:42 -04002457 int num_backups_tried = 0;
2458 int backup_index = 0;
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +08002459 int max_active;
Omar Sandoval6675df32016-09-22 17:24:22 -07002460 int clear_free_space_tree = 0;
Chris Mason4543df72008-06-11 21:47:56 -04002461
David Sterba74e4d822016-02-11 11:01:55 +01002462 tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
2463 chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Miao Xiecb517ea2013-05-15 07:48:19 +00002464 if (!tree_root || !chunk_root) {
Chris Mason39279cc2007-06-12 06:35:45 -04002465 err = -ENOMEM;
2466 goto fail;
2467 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002468
2469 ret = init_srcu_struct(&fs_info->subvol_srcu);
2470 if (ret) {
2471 err = ret;
2472 goto fail;
2473 }
2474
2475 ret = setup_bdi(fs_info, &fs_info->bdi);
2476 if (ret) {
2477 err = ret;
2478 goto fail_srcu;
2479 }
2480
Tejun Heo908c7f12014-09-08 09:51:29 +09002481 ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
Miao Xiee2d84522013-01-29 10:09:20 +00002482 if (ret) {
2483 err = ret;
2484 goto fail_bdi;
2485 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002486 fs_info->dirty_metadata_batch = PAGE_SIZE *
Miao Xiee2d84522013-01-29 10:09:20 +00002487 (1 + ilog2(nr_cpu_ids));
2488
Tejun Heo908c7f12014-09-08 09:51:29 +09002489 ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
Miao Xie963d6782013-01-29 10:10:51 +00002490 if (ret) {
2491 err = ret;
2492 goto fail_dirty_metadata_bytes;
2493 }
2494
Tejun Heo908c7f12014-09-08 09:51:29 +09002495 ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL);
Miao Xiec404e0d2014-01-30 16:46:55 +08002496 if (ret) {
2497 err = ret;
2498 goto fail_delalloc_bytes;
2499 }
2500
Yan, Zheng76dda932009-09-21 16:00:26 -04002501 fs_info->btree_inode = new_inode(sb);
2502 if (!fs_info->btree_inode) {
2503 err = -ENOMEM;
Miao Xiec404e0d2014-01-30 16:46:55 +08002504 goto fail_bio_counter;
Yan, Zheng76dda932009-09-21 16:00:26 -04002505 }
2506
Chris Masona6591712011-07-19 12:04:14 -04002507 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Miao Xie1561ded2011-03-27 16:07:36 +08002508
Yan, Zheng76dda932009-09-21 16:00:26 -04002509 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
Josef Bacikf28491e2013-12-16 13:24:27 -05002510 INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
Chris Mason8fd17792007-04-19 21:01:03 -04002511 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -04002512 INIT_LIST_HEAD(&fs_info->dead_roots);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002513 INIT_LIST_HEAD(&fs_info->delayed_iputs);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002514 INIT_LIST_HEAD(&fs_info->delalloc_roots);
Yan Zheng11833d62009-09-11 16:11:19 -04002515 INIT_LIST_HEAD(&fs_info->caching_block_groups);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002516 spin_lock_init(&fs_info->delalloc_root_lock);
Josef Bacika4abeea2011-04-11 17:25:13 -04002517 spin_lock_init(&fs_info->trans_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04002518 spin_lock_init(&fs_info->fs_roots_radix_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002519 spin_lock_init(&fs_info->delayed_iput_lock);
Chris Mason4cb53002011-05-24 15:35:30 -04002520 spin_lock_init(&fs_info->defrag_inodes_lock);
Josef Bacik2bf64752011-09-26 17:12:22 -04002521 spin_lock_init(&fs_info->free_chunk_lock);
Miao Xieceda0862013-04-11 10:30:16 +00002522 spin_lock_init(&fs_info->super_lock);
Josef Bacikfcebe452014-05-13 17:30:47 -07002523 spin_lock_init(&fs_info->qgroup_op_lock);
Josef Bacikf28491e2013-12-16 13:24:27 -05002524 spin_lock_init(&fs_info->buffer_lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002525 spin_lock_init(&fs_info->unused_bgs_lock);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002526 rwlock_init(&fs_info->tree_mod_log_lock);
Zhao Leid7c15172015-02-26 10:49:20 +08002527 mutex_init(&fs_info->unused_bg_unpin_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002528 mutex_init(&fs_info->delete_unused_bgs_mutex);
Chris Mason75857172011-06-13 20:00:16 -04002529 mutex_init(&fs_info->reloc_mutex);
Miao Xie573bfb72014-03-06 13:55:03 +08002530 mutex_init(&fs_info->delalloc_root_mutex);
Filipe Mananac2d6cb12016-01-15 11:05:12 +00002531 mutex_init(&fs_info->cleaner_delayed_iput_mutex);
Miao Xiede98ced2013-01-29 10:13:12 +00002532 seqlock_init(&fs_info->profiles_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -04002533
Chris Mason0b86a832008-03-24 15:01:56 -04002534 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -04002535 INIT_LIST_HEAD(&fs_info->space_info);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002536 INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002537 INIT_LIST_HEAD(&fs_info->unused_bgs);
Chris Mason0b86a832008-03-24 15:01:56 -04002538 btrfs_mapping_init(&fs_info->mapping_tree);
Miao Xie66d8f3d2012-09-06 04:02:28 -06002539 btrfs_init_block_rsv(&fs_info->global_block_rsv,
2540 BTRFS_BLOCK_RSV_GLOBAL);
2541 btrfs_init_block_rsv(&fs_info->delalloc_block_rsv,
2542 BTRFS_BLOCK_RSV_DELALLOC);
2543 btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
2544 btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
2545 btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
2546 btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
2547 BTRFS_BLOCK_RSV_DELOPS);
Chris Masoncb03c742008-05-15 16:15:45 -04002548 atomic_set(&fs_info->nr_async_submits, 0);
Chris Mason771ed682008-11-06 22:02:51 -05002549 atomic_set(&fs_info->async_delalloc_pages, 0);
Chris Mason8c8bee12008-09-29 11:19:10 -04002550 atomic_set(&fs_info->async_submit_draining, 0);
Chris Mason0986fe9e2008-08-15 15:34:15 -04002551 atomic_set(&fs_info->nr_async_bios, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002552 atomic_set(&fs_info->defrag_running, 0);
Josef Bacikfcebe452014-05-13 17:30:47 -07002553 atomic_set(&fs_info->qgroup_op_seq, 0);
Zhao Lei2fefd552016-01-07 18:38:48 +08002554 atomic_set(&fs_info->reada_works_cnt, 0);
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002555 atomic64_set(&fs_info->tree_mod_seq, 0);
Wang Xiaoguang9e7cc912016-08-01 13:28:08 +08002556 fs_info->fs_frozen = 0;
Chris Masone20d96d2007-03-22 12:13:20 -04002557 fs_info->sb = sb;
Filipe David Borba Manana95ac5672013-08-08 22:45:48 +01002558 fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002559 fs_info->metadata_ratio = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04002560 fs_info->defrag_inodes = RB_ROOT;
Josef Bacik2bf64752011-09-26 17:12:22 -04002561 fs_info->free_chunk_space = 0;
Jan Schmidtf29021b2012-05-16 17:55:38 +02002562 fs_info->tree_mod_log = RB_ROOT;
David Sterba8b87dc12013-08-01 18:14:52 +02002563 fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
David Sterbaf8c269d2015-01-16 17:21:12 +01002564 fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */
Arne Jansen90519d62011-05-23 14:30:00 +02002565 /* readahead state */
Mel Gormand0164ad2015-11-06 16:28:21 -08002566 INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Arne Jansen90519d62011-05-23 14:30:00 +02002567 spin_lock_init(&fs_info->reada_lock);
Chris Masonc8b97812008-10-29 14:49:59 -04002568
Chris Masonb34b0862008-12-19 15:43:22 -05002569 fs_info->thread_pool_size = min_t(unsigned long,
2570 num_online_cpus() + 2, 8);
Chris Mason0afbaf82008-04-18 14:17:20 -04002571
Miao Xie199c2a92013-05-15 07:48:23 +00002572 INIT_LIST_HEAD(&fs_info->ordered_roots);
2573 spin_lock_init(&fs_info->ordered_root_lock);
Miao Xie16cdcec2011-04-22 18:12:22 +08002574 fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
David Sterba74e4d822016-02-11 11:01:55 +01002575 GFP_KERNEL);
Miao Xie16cdcec2011-04-22 18:12:22 +08002576 if (!fs_info->delayed_root) {
2577 err = -ENOMEM;
2578 goto fail_iput;
2579 }
2580 btrfs_init_delayed_root(fs_info->delayed_root);
Chris Mason3eaa2882008-07-24 11:57:52 -04002581
Eric Sandeen638aa7e2014-08-01 18:12:38 -05002582 btrfs_init_scrub(fs_info);
Stefan Behrens21adbd52011-11-09 13:44:05 +01002583#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2584 fs_info->check_integrity_print_mask = 0;
2585#endif
Eric Sandeen779a65a2014-08-01 18:12:39 -05002586 btrfs_init_balance(fs_info);
Miao Xie21c7e752014-05-13 17:29:04 -07002587 btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work);
Arne Jansena2de7332011-03-08 14:14:00 +01002588
Chris Masona061fc82008-05-07 11:43:44 -04002589 sb->s_blocksize = 4096;
2590 sb->s_blocksize_bits = blksize_bits(4096);
Jens Axboe32a88aa2009-09-16 15:02:33 +02002591 sb->s_bdi = &fs_info->bdi;
Chris Masona061fc82008-05-07 11:43:44 -04002592
Eric Sandeenf37938e2014-08-01 18:12:40 -05002593 btrfs_init_btree_inode(fs_info, tree_root);
Chris Mason39279cc2007-06-12 06:35:45 -04002594
Chris Masone02119d2008-09-05 16:13:11 -04002595 spin_lock_init(&fs_info->block_group_cache_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002596 fs_info->block_group_cache_tree = RB_ROOT;
Liu Boa1897fd2012-12-27 09:01:23 +00002597 fs_info->first_logical_byte = (u64)-1;
Chris Mason925baed2008-06-25 16:01:30 -04002598
Yan Zheng11833d62009-09-11 16:11:19 -04002599 extent_io_tree_init(&fs_info->freed_extents[0],
David Sterbaf993c882011-04-20 23:35:57 +02002600 fs_info->btree_inode->i_mapping);
Yan Zheng11833d62009-09-11 16:11:19 -04002601 extent_io_tree_init(&fs_info->freed_extents[1],
David Sterbaf993c882011-04-20 23:35:57 +02002602 fs_info->btree_inode->i_mapping);
Yan Zheng11833d62009-09-11 16:11:19 -04002603 fs_info->pinned_extents = &fs_info->freed_extents[0];
Josef Bacikafcdd122016-09-02 15:40:02 -04002604 set_bit(BTRFS_FS_BARRIER, &fs_info->flags);
Chris Mason509659c2007-05-10 12:36:17 -04002605
Chris Mason5a3f23d2009-03-31 13:27:11 -04002606 mutex_init(&fs_info->ordered_operations_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04002607 mutex_init(&fs_info->tree_log_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002608 mutex_init(&fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04002609 mutex_init(&fs_info->transaction_kthread_mutex);
2610 mutex_init(&fs_info->cleaner_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04002611 mutex_init(&fs_info->volume_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07002612 mutex_init(&fs_info->ro_block_group_mutex);
Josef Bacik9e351cc2014-03-13 15:42:13 -04002613 init_rwsem(&fs_info->commit_root_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00002614 init_rwsem(&fs_info->cleanup_work_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -04002615 init_rwsem(&fs_info->subvol_sem);
Stefan Behrens803b2f52013-08-15 17:11:21 +02002616 sema_init(&fs_info->uuid_tree_rescan_sem, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002617
Eric Sandeenad618362014-08-01 18:12:41 -05002618 btrfs_init_dev_replace_locks(fs_info);
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002619 btrfs_init_qgroup(fs_info);
Arne Jansen416ac512011-09-13 12:56:09 +02002620
Chris Masonfa9c0d792009-04-03 09:47:43 -04002621 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2622 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2623
Chris Masone6dcd2d2008-07-17 12:53:50 -04002624 init_waitqueue_head(&fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04002625 init_waitqueue_head(&fs_info->transaction_wait);
Sage Weilbb9c12c2010-10-29 15:37:34 -04002626 init_waitqueue_head(&fs_info->transaction_blocked_wait);
Chris Mason4854ddd2008-08-15 15:34:17 -04002627 init_waitqueue_head(&fs_info->async_submit_wait);
Chris Mason3768f362007-03-13 16:47:54 -04002628
Filipe Manana04216822014-11-27 21:14:15 +00002629 INIT_LIST_HEAD(&fs_info->pinned_chunks);
2630
David Woodhouse53b381b2013-01-29 18:40:14 -05002631 ret = btrfs_alloc_stripe_hash_table(fs_info);
2632 if (ret) {
David Sterba83c82662013-03-01 15:03:00 +00002633 err = ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05002634 goto fail_alloc;
2635 }
2636
David Sterba707e8a02014-06-04 19:22:26 +02002637 __setup_root(4096, 4096, 4096, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002638 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -04002639
Chris Mason3c4bb262012-03-27 18:56:56 -04002640 invalidate_bdev(fs_devices->latest_bdev);
David Sterba1104a882013-03-06 15:57:46 +01002641
2642 /*
2643 * Read super block and check the signature bytes only
2644 */
Yan Zhenga512bbf2008-12-08 16:46:26 -05002645 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +08002646 if (IS_ERR(bh)) {
2647 err = PTR_ERR(bh);
Miao Xie16cdcec2011-04-22 18:12:22 +08002648 goto fail_alloc;
Dave Young20b45072011-01-08 10:09:13 +00002649 }
Chris Mason39279cc2007-06-12 06:35:45 -04002650
David Sterba1104a882013-03-06 15:57:46 +01002651 /*
2652 * We want to check superblock checksum, the type is stored inside.
2653 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
2654 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002655 if (btrfs_check_super_csum(fs_info, bh->b_data)) {
David Sterba05135f52016-05-09 11:32:39 +02002656 btrfs_err(fs_info, "superblock checksum mismatch");
David Sterba1104a882013-03-06 15:57:46 +01002657 err = -EINVAL;
Anand Jainb2acddd2015-10-07 17:23:23 +08002658 brelse(bh);
David Sterba1104a882013-03-06 15:57:46 +01002659 goto fail_alloc;
2660 }
2661
2662 /*
2663 * super_copy is zeroed at allocation time and we never touch the
2664 * following bytes up to INFO_SIZE, the checksum is calculated from
2665 * the whole block of INFO_SIZE
2666 */
David Sterba6c417612011-04-13 15:41:04 +02002667 memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy));
2668 memcpy(fs_info->super_for_commit, fs_info->super_copy,
2669 sizeof(*fs_info->super_for_commit));
Chris Masona061fc82008-05-07 11:43:44 -04002670 brelse(bh);
Chris Mason5f39d392007-10-15 16:14:19 -04002671
David Sterba6c417612011-04-13 15:41:04 +02002672 memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002673
David Sterba1104a882013-03-06 15:57:46 +01002674 ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY);
2675 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002676 btrfs_err(fs_info, "superblock contains fatal errors");
David Sterba1104a882013-03-06 15:57:46 +01002677 err = -EINVAL;
2678 goto fail_alloc;
2679 }
2680
David Sterba6c417612011-04-13 15:41:04 +02002681 disk_super = fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -04002682 if (!btrfs_super_root(disk_super))
Miao Xie16cdcec2011-04-22 18:12:22 +08002683 goto fail_alloc;
Chris Mason0f7d52f2007-04-09 10:42:37 -04002684
liuboacce9522011-01-06 19:30:25 +08002685 /* check FS state, whether FS is broken. */
Miao Xie87533c42013-01-29 10:14:48 +00002686 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
2687 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
liuboacce9522011-01-06 19:30:25 +08002688
Liu Bo75e7cb72011-03-22 10:12:20 +00002689 /*
Chris Masonaf31f5e2011-11-03 15:17:42 -04002690 * run through our array of backup supers and setup
2691 * our ring pointer to the oldest one
2692 */
2693 generation = btrfs_super_generation(disk_super);
2694 find_oldest_super_backup(fs_info, generation);
2695
2696 /*
Liu Bo75e7cb72011-03-22 10:12:20 +00002697 * In the long term, we'll store the compression type in the super
2698 * block, and it'll be used for per file compression control.
2699 */
2700 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2701
Qu Wenruo96da0912016-01-19 10:23:03 +08002702 ret = btrfs_parse_options(tree_root, options, sb->s_flags);
Yan Zheng2b820322008-11-17 21:11:30 -05002703 if (ret) {
2704 err = ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08002705 goto fail_alloc;
Yan Zheng2b820322008-11-17 21:11:30 -05002706 }
Chris Masondfe25022008-05-13 13:46:40 -04002707
Josef Bacikf2b636e2008-12-02 06:36:08 -05002708 features = btrfs_super_incompat_flags(disk_super) &
2709 ~BTRFS_FEATURE_INCOMPAT_SUPP;
2710 if (features) {
David Sterba05135f52016-05-09 11:32:39 +02002711 btrfs_err(fs_info,
2712 "cannot mount because of unsupported optional features (%llx)",
2713 features);
Josef Bacikf2b636e2008-12-02 06:36:08 -05002714 err = -EINVAL;
Miao Xie16cdcec2011-04-22 18:12:22 +08002715 goto fail_alloc;
Josef Bacikf2b636e2008-12-02 06:36:08 -05002716 }
2717
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002718 features = btrfs_super_incompat_flags(disk_super);
Li Zefana6fa6fa2010-10-25 15:12:26 +08002719 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
Li Zefan69e380d2012-06-11 17:10:51 +08002720 if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
Li Zefana6fa6fa2010-10-25 15:12:26 +08002721 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
Chris Mason727011e2010-08-06 13:21:20 -04002722
Josef Bacik3173a182013-03-07 14:22:04 -05002723 if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
David Sterba05135f52016-05-09 11:32:39 +02002724 btrfs_info(fs_info, "has skinny extents");
Josef Bacik3173a182013-03-07 14:22:04 -05002725
Chris Mason727011e2010-08-06 13:21:20 -04002726 /*
2727 * flag our filesystem as having big metadata blocks if
2728 * they are bigger than the page size
2729 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002730 if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) {
Chris Mason727011e2010-08-06 13:21:20 -04002731 if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA))
David Sterba05135f52016-05-09 11:32:39 +02002732 btrfs_info(fs_info,
2733 "flagging fs with big metadata feature");
Chris Mason727011e2010-08-06 13:21:20 -04002734 features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
2735 }
2736
Chris Masonbc3f1162012-03-29 17:02:47 -04002737 nodesize = btrfs_super_nodesize(disk_super);
Chris Masonbc3f1162012-03-29 17:02:47 -04002738 sectorsize = btrfs_super_sectorsize(disk_super);
Chandan Rajendrab7f67052016-06-23 15:16:44 +05302739 stripesize = sectorsize;
David Sterba707e8a02014-06-04 19:22:26 +02002740 fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
Miao Xie963d6782013-01-29 10:10:51 +00002741 fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
Chris Masonbc3f1162012-03-29 17:02:47 -04002742
2743 /*
2744 * mixed block groups end up with duplicate but slightly offset
2745 * extent buffers for the same range. It leads to corruptions
2746 */
2747 if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
David Sterba707e8a02014-06-04 19:22:26 +02002748 (sectorsize != nodesize)) {
David Sterba05135f52016-05-09 11:32:39 +02002749 btrfs_err(fs_info,
2750"unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
2751 nodesize, sectorsize);
Chris Masonbc3f1162012-03-29 17:02:47 -04002752 goto fail_alloc;
2753 }
2754
Miao Xieceda0862013-04-11 10:30:16 +00002755 /*
2756 * Needn't use the lock because there is no other task which will
2757 * update the flag.
2758 */
Li Zefana6fa6fa2010-10-25 15:12:26 +08002759 btrfs_set_super_incompat_flags(disk_super, features);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002760
Josef Bacikf2b636e2008-12-02 06:36:08 -05002761 features = btrfs_super_compat_ro_flags(disk_super) &
2762 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
2763 if (!(sb->s_flags & MS_RDONLY) && features) {
David Sterba05135f52016-05-09 11:32:39 +02002764 btrfs_err(fs_info,
2765 "cannot mount read-write because of unsupported optional features (%llx)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002766 features);
Josef Bacikf2b636e2008-12-02 06:36:08 -05002767 err = -EINVAL;
Miao Xie16cdcec2011-04-22 18:12:22 +08002768 goto fail_alloc;
Josef Bacikf2b636e2008-12-02 06:36:08 -05002769 }
Chris Mason61d92c32009-10-02 19:11:56 -04002770
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +08002771 max_active = fs_info->thread_pool_size;
Chris Mason61d92c32009-10-02 19:11:56 -04002772
Eric Sandeen2a458192015-02-16 16:29:26 +01002773 ret = btrfs_init_workqueues(fs_info, fs_devices);
2774 if (ret) {
2775 err = ret;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002776 goto fail_sb_buffer;
2777 }
Chris Mason4543df72008-06-11 21:47:56 -04002778
Chris Mason4575c9c2008-04-18 16:13:31 -04002779 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
Chris Masonc8b97812008-10-29 14:49:59 -04002780 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002781 SZ_4M / PAGE_SIZE);
Chris Mason4575c9c2008-04-18 16:13:31 -04002782
Chris Masondb945352007-10-15 16:15:53 -04002783 tree_root->nodesize = nodesize;
Chris Masondb945352007-10-15 16:15:53 -04002784 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -05002785 tree_root->stripesize = stripesize;
Chris Masona061fc82008-05-07 11:43:44 -04002786
2787 sb->s_blocksize = sectorsize;
2788 sb->s_blocksize_bits = blksize_bits(sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04002789
Chris Mason925baed2008-06-25 16:01:30 -04002790 mutex_lock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002791 ret = btrfs_read_sys_array(tree_root);
Chris Mason925baed2008-06-25 16:01:30 -04002792 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason84eed902008-04-25 09:04:37 -04002793 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002794 btrfs_err(fs_info, "failed to read the system array: %d", ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795 goto fail_sb_buffer;
Chris Mason84eed902008-04-25 09:04:37 -04002796 }
Chris Mason0b86a832008-03-24 15:01:56 -04002797
Yan Zheng84234f32008-10-29 14:49:05 -04002798 generation = btrfs_super_chunk_root_generation(disk_super);
Chris Mason0b86a832008-03-24 15:01:56 -04002799
David Sterba707e8a02014-06-04 19:22:26 +02002800 __setup_root(nodesize, sectorsize, stripesize, chunk_root,
2801 fs_info, BTRFS_CHUNK_TREE_OBJECTID);
Chris Mason0b86a832008-03-24 15:01:56 -04002802
2803 chunk_root->node = read_tree_block(chunk_root,
2804 btrfs_super_chunk_root(disk_super),
David Sterbace86cd52014-06-15 01:07:32 +02002805 generation);
Liu Bo64c043d2015-05-25 17:30:15 +08002806 if (IS_ERR(chunk_root->node) ||
2807 !extent_buffer_uptodate(chunk_root->node)) {
David Sterba05135f52016-05-09 11:32:39 +02002808 btrfs_err(fs_info, "failed to read chunk root");
chandane5fffba2015-10-05 22:14:25 +05302809 if (!IS_ERR(chunk_root->node))
2810 free_extent_buffer(chunk_root->node);
Zhao Lei95ab1f62015-07-15 21:02:09 +08002811 chunk_root->node = NULL;
Chris Masonaf31f5e2011-11-03 15:17:42 -04002812 goto fail_tree_roots;
David Woodhouse83121942009-07-22 16:52:13 -04002813 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002814 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
2815 chunk_root->commit_root = btrfs_root_node(chunk_root);
Chris Mason0b86a832008-03-24 15:01:56 -04002816
Chris Masone17cade2008-04-15 15:41:47 -04002817 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02002818 btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002819
Chris Mason0b86a832008-03-24 15:01:56 -04002820 ret = btrfs_read_chunk_tree(chunk_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002821 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002822 btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
Chris Masonaf31f5e2011-11-03 15:17:42 -04002823 goto fail_tree_roots;
Yan Zheng2b820322008-11-17 21:11:30 -05002824 }
Chris Mason0b86a832008-03-24 15:01:56 -04002825
Stefan Behrens8dabb742012-11-06 13:15:27 +01002826 /*
2827 * keep the device that is marked to be the target device for the
2828 * dev_replace procedure
2829 */
Eric Sandeen9eaed212014-08-01 18:12:35 -05002830 btrfs_close_extra_devices(fs_devices, 0);
Chris Masondfe25022008-05-13 13:46:40 -04002831
Chris Masona6b0d5c2012-02-20 20:53:43 -05002832 if (!fs_devices->latest_bdev) {
David Sterba05135f52016-05-09 11:32:39 +02002833 btrfs_err(fs_info, "failed to read devices");
Chris Masona6b0d5c2012-02-20 20:53:43 -05002834 goto fail_tree_roots;
2835 }
2836
Chris Masonaf31f5e2011-11-03 15:17:42 -04002837retry_root_backup:
Yan Zheng84234f32008-10-29 14:49:05 -04002838 generation = btrfs_super_generation(disk_super);
Chris Mason0b86a832008-03-24 15:01:56 -04002839
Chris Masone20d96d2007-03-22 12:13:20 -04002840 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04002841 btrfs_super_root(disk_super),
David Sterbace86cd52014-06-15 01:07:32 +02002842 generation);
Liu Bo64c043d2015-05-25 17:30:15 +08002843 if (IS_ERR(tree_root->node) ||
2844 !extent_buffer_uptodate(tree_root->node)) {
David Sterba05135f52016-05-09 11:32:39 +02002845 btrfs_warn(fs_info, "failed to read tree root");
chandane5fffba2015-10-05 22:14:25 +05302846 if (!IS_ERR(tree_root->node))
2847 free_extent_buffer(tree_root->node);
Zhao Lei95ab1f62015-07-15 21:02:09 +08002848 tree_root->node = NULL;
Chris Masonaf31f5e2011-11-03 15:17:42 -04002849 goto recovery_tree_root;
David Woodhouse83121942009-07-22 16:52:13 -04002850 }
Chris Masonaf31f5e2011-11-03 15:17:42 -04002851
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002852 btrfs_set_root_node(&tree_root->root_item, tree_root->node);
2853 tree_root->commit_root = btrfs_root_node(tree_root);
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02002854 btrfs_set_root_refs(&tree_root->root_item, 1);
Chris Masondb945352007-10-15 16:15:53 -04002855
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05302856 mutex_lock(&tree_root->objectid_mutex);
2857 ret = btrfs_find_highest_objectid(tree_root,
2858 &tree_root->highest_objectid);
2859 if (ret) {
2860 mutex_unlock(&tree_root->objectid_mutex);
2861 goto recovery_tree_root;
2862 }
2863
2864 ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
2865
2866 mutex_unlock(&tree_root->objectid_mutex);
2867
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002868 ret = btrfs_read_roots(fs_info, tree_root);
2869 if (ret)
Chris Masonaf31f5e2011-11-03 15:17:42 -04002870 goto recovery_tree_root;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02002871
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002872 fs_info->generation = generation;
2873 fs_info->last_trans_committed = generation;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002874
Ilya Dryomov68310a52012-06-22 12:24:12 -06002875 ret = btrfs_recover_balance(fs_info);
2876 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002877 btrfs_err(fs_info, "failed to recover balance: %d", ret);
Ilya Dryomov68310a52012-06-22 12:24:12 -06002878 goto fail_block_groups;
2879 }
2880
Stefan Behrens733f4fb2012-05-25 16:06:10 +02002881 ret = btrfs_init_dev_stats(fs_info);
2882 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002883 btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02002884 goto fail_block_groups;
2885 }
2886
Stefan Behrens8dabb742012-11-06 13:15:27 +01002887 ret = btrfs_init_dev_replace(fs_info);
2888 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002889 btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
Stefan Behrens8dabb742012-11-06 13:15:27 +01002890 goto fail_block_groups;
2891 }
2892
Eric Sandeen9eaed212014-08-01 18:12:35 -05002893 btrfs_close_extra_devices(fs_devices, 1);
Stefan Behrens8dabb742012-11-06 13:15:27 +01002894
Anand Jainb7c35e82015-03-10 06:38:38 +08002895 ret = btrfs_sysfs_add_fsid(fs_devices, NULL);
2896 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002897 btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
2898 ret);
Anand Jainb7c35e82015-03-10 06:38:38 +08002899 goto fail_block_groups;
2900 }
2901
2902 ret = btrfs_sysfs_add_device(fs_devices);
2903 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002904 btrfs_err(fs_info, "failed to init sysfs device interface: %d",
2905 ret);
Anand Jainb7c35e82015-03-10 06:38:38 +08002906 goto fail_fsdev_sysfs;
2907 }
2908
Anand Jain96f31362015-08-14 18:32:46 +08002909 ret = btrfs_sysfs_add_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04002910 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002911 btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
Anand Jainb7c35e82015-03-10 06:38:38 +08002912 goto fail_fsdev_sysfs;
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04002913 }
2914
liuboc59021f2011-03-07 02:13:14 +00002915 ret = btrfs_init_space_info(fs_info);
2916 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002917 btrfs_err(fs_info, "failed to initialize space info: %d", ret);
Anand Jain2365dd32014-01-22 11:15:51 +08002918 goto fail_sysfs;
liuboc59021f2011-03-07 02:13:14 +00002919 }
2920
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002921 ret = btrfs_read_block_groups(fs_info->extent_root);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00002922 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002923 btrfs_err(fs_info, "failed to read block groups: %d", ret);
Anand Jain2365dd32014-01-22 11:15:51 +08002924 goto fail_sysfs;
Josef Bacik1b1d1f62010-03-19 20:49:55 +00002925 }
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002926 fs_info->num_tolerated_disk_barrier_failures =
2927 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
Stefan Behrens292fd7f2012-10-30 17:16:16 +00002928 if (fs_info->fs_devices->missing_devices >
2929 fs_info->num_tolerated_disk_barrier_failures &&
2930 !(sb->s_flags & MS_RDONLY)) {
David Sterba05135f52016-05-09 11:32:39 +02002931 btrfs_warn(fs_info,
2932"missing devices (%llu) exceeds the limit (%d), writeable mount is not allowed",
Zhao Lei78fa1772015-07-20 17:54:50 +08002933 fs_info->fs_devices->missing_devices,
2934 fs_info->num_tolerated_disk_barrier_failures);
Anand Jain2365dd32014-01-22 11:15:51 +08002935 goto fail_sysfs;
Stefan Behrens292fd7f2012-10-30 17:16:16 +00002936 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002937
Chris Masona74a4b92008-06-25 16:01:31 -04002938 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
2939 "btrfs-cleaner");
Qinghuang Feng57506d52009-01-21 10:49:16 -05002940 if (IS_ERR(fs_info->cleaner_kthread))
Anand Jain2365dd32014-01-22 11:15:51 +08002941 goto fail_sysfs;
Chris Masona74a4b92008-06-25 16:01:31 -04002942
2943 fs_info->transaction_kthread = kthread_run(transaction_kthread,
2944 tree_root,
2945 "btrfs-transaction");
Qinghuang Feng57506d52009-01-21 10:49:16 -05002946 if (IS_ERR(fs_info->transaction_kthread))
Chris Mason3f157a22008-06-25 16:01:31 -04002947 goto fail_cleaner;
Chris Masona74a4b92008-06-25 16:01:31 -04002948
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002949 if (!btrfs_test_opt(tree_root->fs_info, SSD) &&
2950 !btrfs_test_opt(tree_root->fs_info, NOSSD) &&
Chris Masonc2898112009-06-10 09:51:32 -04002951 !fs_info->fs_devices->rotating) {
David Sterba05135f52016-05-09 11:32:39 +02002952 btrfs_info(fs_info, "detected SSD devices, enabling SSD mode");
Chris Masonc2898112009-06-10 09:51:32 -04002953 btrfs_set_opt(fs_info->mount_opt, SSD);
2954 }
2955
David Sterba572d9ab2014-02-05 15:26:17 +01002956 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002957 * Mount does not set all options immediately, we can do it now and do
David Sterba572d9ab2014-02-05 15:26:17 +01002958 * not have to wait for transaction commit
2959 */
2960 btrfs_apply_pending_changes(fs_info);
Qu Wenruo3818aea2014-01-13 13:36:06 +08002961
Stefan Behrens21adbd52011-11-09 13:44:05 +01002962#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002963 if (btrfs_test_opt(tree_root->fs_info, CHECK_INTEGRITY)) {
Stefan Behrens21adbd52011-11-09 13:44:05 +01002964 ret = btrfsic_mount(tree_root, fs_devices,
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002965 btrfs_test_opt(tree_root->fs_info,
Stefan Behrens21adbd52011-11-09 13:44:05 +01002966 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
2967 1 : 0,
2968 fs_info->check_integrity_print_mask);
2969 if (ret)
David Sterba05135f52016-05-09 11:32:39 +02002970 btrfs_warn(fs_info,
2971 "failed to initialize integrity check module: %d",
2972 ret);
Stefan Behrens21adbd52011-11-09 13:44:05 +01002973 }
2974#endif
Arne Jansenbcef60f2011-09-13 15:23:30 +02002975 ret = btrfs_read_qgroup_config(fs_info);
2976 if (ret)
2977 goto fail_trans_kthread;
Stefan Behrens21adbd52011-11-09 13:44:05 +01002978
Qu Wenruo96da0912016-01-19 10:23:03 +08002979 /* do not make disk changes in broken FS or nologreplay is given */
2980 if (btrfs_super_log_root(disk_super) != 0 &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002981 !btrfs_test_opt(tree_root->fs_info, NOLOGREPLAY)) {
David Sterba66ada402020-02-05 17:12:16 +01002982 btrfs_info(fs_info, "start tree-log replay");
Eric Sandeen63443bf2014-08-01 18:12:46 -05002983 ret = btrfs_replay_log(fs_info, fs_devices);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002984 if (ret) {
Eric Sandeen63443bf2014-08-01 18:12:46 -05002985 err = ret;
Wang Shilong28c16cb2014-04-23 19:33:35 +08002986 goto fail_qgroup;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002987 }
Chris Masone02119d2008-09-05 16:13:11 -04002988 }
Zheng Yan1a40e232008-09-26 10:09:34 -04002989
Yan, Zheng76dda932009-09-21 16:00:26 -04002990 ret = btrfs_find_orphan_roots(tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 if (ret)
Wang Shilong28c16cb2014-04-23 19:33:35 +08002992 goto fail_qgroup;
Yan, Zheng76dda932009-09-21 16:00:26 -04002993
Chris Mason7c2ca462008-11-19 15:13:35 -05002994 if (!(sb->s_flags & MS_RDONLY)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 ret = btrfs_cleanup_fs_roots(fs_info);
Ilya Dryomov44c44af2012-06-22 12:14:13 -06002996 if (ret)
Wang Shilong28c16cb2014-04-23 19:33:35 +08002997 goto fail_qgroup;
Zygo Blaxell90c711a2016-06-12 23:39:58 -04002998
2999 mutex_lock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 ret = btrfs_recover_relocation(tree_root);
Zygo Blaxell90c711a2016-06-12 23:39:58 -04003001 mutex_unlock(&fs_info->cleaner_mutex);
Miao Xied7ce5842010-02-02 08:46:44 +00003002 if (ret < 0) {
David Sterba05135f52016-05-09 11:32:39 +02003003 btrfs_warn(fs_info, "failed to recover relocation: %d",
3004 ret);
Miao Xied7ce5842010-02-02 08:46:44 +00003005 err = -EINVAL;
Arne Jansenbcef60f2011-09-13 15:23:30 +02003006 goto fail_qgroup;
Miao Xied7ce5842010-02-02 08:46:44 +00003007 }
Chris Mason7c2ca462008-11-19 15:13:35 -05003008 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003009
Chris Mason3de45862008-11-17 21:02:50 -05003010 location.objectid = BTRFS_FS_TREE_OBJECTID;
3011 location.type = BTRFS_ROOT_ITEM_KEY;
Miao Xiecb517ea2013-05-15 07:48:19 +00003012 location.offset = 0;
Chris Mason3de45862008-11-17 21:02:50 -05003013
Chris Mason3de45862008-11-17 21:02:50 -05003014 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
Dan Carpenter3140c9a2010-05-29 09:44:10 +00003015 if (IS_ERR(fs_info->fs_root)) {
3016 err = PTR_ERR(fs_info->fs_root);
Arne Jansenbcef60f2011-09-13 15:23:30 +02003017 goto fail_qgroup;
Dan Carpenter3140c9a2010-05-29 09:44:10 +00003018 }
Chris Masonc2898112009-06-10 09:51:32 -04003019
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003020 if (sb->s_flags & MS_RDONLY)
3021 return 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003022
Omar Sandovalf8d468a2016-09-22 17:24:21 -07003023 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
3024 btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
Omar Sandoval6675df32016-09-22 17:24:22 -07003025 clear_free_space_tree = 1;
3026 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
3027 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
3028 btrfs_warn(fs_info, "free space tree is invalid");
3029 clear_free_space_tree = 1;
3030 }
3031
3032 if (clear_free_space_tree) {
Omar Sandovalf8d468a2016-09-22 17:24:21 -07003033 btrfs_info(fs_info, "clearing free space tree");
3034 ret = btrfs_clear_free_space_tree(fs_info);
3035 if (ret) {
3036 btrfs_warn(fs_info,
3037 "failed to clear free space tree: %d", ret);
3038 close_ctree(tree_root);
3039 return ret;
3040 }
3041 }
3042
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003043 if (btrfs_test_opt(tree_root->fs_info, FREE_SPACE_TREE) &&
Chris Mason511711a2015-12-30 07:52:35 -08003044 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
David Sterba05135f52016-05-09 11:32:39 +02003045 btrfs_info(fs_info, "creating free space tree");
Chris Mason511711a2015-12-30 07:52:35 -08003046 ret = btrfs_create_free_space_tree(fs_info);
3047 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003048 btrfs_warn(fs_info,
3049 "failed to create free space tree: %d", ret);
Chris Mason511711a2015-12-30 07:52:35 -08003050 close_ctree(tree_root);
3051 return ret;
3052 }
3053 }
3054
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003055 down_read(&fs_info->cleanup_work_sem);
3056 if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
3057 (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
Josef Bacike3acc2a2010-01-26 14:30:53 +00003058 up_read(&fs_info->cleanup_work_sem);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003059 close_ctree(tree_root);
3060 return ret;
3061 }
3062 up_read(&fs_info->cleanup_work_sem);
Ilya Dryomov59641012012-01-16 22:04:48 +02003063
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003064 ret = btrfs_resume_balance_async(fs_info);
3065 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003066 btrfs_warn(fs_info, "failed to resume balance: %d", ret);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003067 close_ctree(tree_root);
3068 return ret;
Josef Bacike3acc2a2010-01-26 14:30:53 +00003069 }
3070
Stefan Behrens8dabb742012-11-06 13:15:27 +01003071 ret = btrfs_resume_dev_replace_async(fs_info);
3072 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003073 btrfs_warn(fs_info, "failed to resume device replace: %d", ret);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003074 close_ctree(tree_root);
3075 return ret;
3076 }
3077
Jan Schmidtb382a322013-05-28 15:47:24 +00003078 btrfs_qgroup_rescan_resume(fs_info);
3079
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05003080 if (!fs_info->uuid_root) {
David Sterba05135f52016-05-09 11:32:39 +02003081 btrfs_info(fs_info, "creating UUID tree");
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003082 ret = btrfs_create_uuid_tree(fs_info);
3083 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003084 btrfs_warn(fs_info,
3085 "failed to create the UUID tree: %d", ret);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003086 close_ctree(tree_root);
3087 return ret;
3088 }
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003089 } else if (btrfs_test_opt(tree_root->fs_info, RESCAN_UUID_TREE) ||
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05003090 fs_info->generation !=
3091 btrfs_super_uuid_tree_generation(disk_super)) {
David Sterba05135f52016-05-09 11:32:39 +02003092 btrfs_info(fs_info, "checking UUID tree");
Stefan Behrens70f80172013-08-15 17:11:23 +02003093 ret = btrfs_check_uuid_tree(fs_info);
3094 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003095 btrfs_warn(fs_info,
3096 "failed to check the UUID tree: %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003097 close_ctree(tree_root);
3098 return ret;
3099 }
3100 } else {
Josef Bacikafcdd122016-09-02 15:40:02 -04003101 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003102 }
Josef Bacikafcdd122016-09-02 15:40:02 -04003103 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003104
Qu Wenruo8dcddfa2016-01-19 10:23:02 +08003105 /*
3106 * backuproot only affect mount behavior, and if open_ctree succeeded,
3107 * no need to keep the flag
3108 */
3109 btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
3110
Al Viroad2b2c82011-11-17 01:10:02 -05003111 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003112
Arne Jansenbcef60f2011-09-13 15:23:30 +02003113fail_qgroup:
3114 btrfs_free_qgroup_config(fs_info);
Chris Mason7c2ca462008-11-19 15:13:35 -05003115fail_trans_kthread:
3116 kthread_stop(fs_info->transaction_kthread);
Josef Bacik54067ae2013-04-25 13:44:38 -04003117 btrfs_cleanup_transaction(fs_info->tree_root);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003118 btrfs_free_fs_roots(fs_info);
Chris Mason3f157a22008-06-25 16:01:31 -04003119fail_cleaner:
Chris Masona74a4b92008-06-25 16:01:31 -04003120 kthread_stop(fs_info->cleaner_kthread);
Chris Mason7c2ca462008-11-19 15:13:35 -05003121
3122 /*
3123 * make sure we're done with the btree inode before we stop our
3124 * kthreads
3125 */
3126 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
Chris Mason7c2ca462008-11-19 15:13:35 -05003127
Anand Jain2365dd32014-01-22 11:15:51 +08003128fail_sysfs:
Anand Jain6618a592015-08-14 18:32:47 +08003129 btrfs_sysfs_remove_mounted(fs_info);
Anand Jain2365dd32014-01-22 11:15:51 +08003130
Anand Jainb7c35e82015-03-10 06:38:38 +08003131fail_fsdev_sysfs:
3132 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3133
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003134fail_block_groups:
Josef Bacik54067ae2013-04-25 13:44:38 -04003135 btrfs_put_block_group_cache(fs_info);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003136 btrfs_free_block_groups(fs_info);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003137
3138fail_tree_roots:
Anand Jainfb8c8122019-10-10 10:39:25 +08003139 free_root_pointers(fs_info, true);
Miao Xie2b8195b2013-02-07 06:01:35 +00003140 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003141
Chris Mason39279cc2007-06-12 06:35:45 -04003142fail_sb_buffer:
Liu Bo7abadb62013-03-17 02:10:31 +00003143 btrfs_stop_all_workers(fs_info);
Miao Xie16cdcec2011-04-22 18:12:22 +08003144fail_alloc:
Chris Mason4543df72008-06-11 21:47:56 -04003145fail_iput:
Ilya Dryomov586e46e2011-11-09 13:26:37 +02003146 btrfs_mapping_tree_free(&fs_info->mapping_tree);
3147
Chris Mason4543df72008-06-11 21:47:56 -04003148 iput(fs_info->btree_inode);
Miao Xiec404e0d2014-01-30 16:46:55 +08003149fail_bio_counter:
3150 percpu_counter_destroy(&fs_info->bio_counter);
Miao Xie963d6782013-01-29 10:10:51 +00003151fail_delalloc_bytes:
3152 percpu_counter_destroy(&fs_info->delalloc_bytes);
Miao Xiee2d84522013-01-29 10:09:20 +00003153fail_dirty_metadata_bytes:
3154 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
Jens Axboead081f12009-06-12 14:43:40 +02003155fail_bdi:
Qinghuang Feng7e662852009-01-21 10:49:16 -05003156 bdi_destroy(&fs_info->bdi);
Yan, Zheng76dda932009-09-21 16:00:26 -04003157fail_srcu:
3158 cleanup_srcu_struct(&fs_info->subvol_srcu);
Qinghuang Feng7e662852009-01-21 10:49:16 -05003159fail:
David Woodhouse53b381b2013-01-29 18:40:14 -05003160 btrfs_free_stripe_hash_table(fs_info);
Ilya Dryomov586e46e2011-11-09 13:26:37 +02003161 btrfs_close_devices(fs_info->fs_devices);
Al Viroad2b2c82011-11-17 01:10:02 -05003162 return err;
Chris Masonaf31f5e2011-11-03 15:17:42 -04003163
3164recovery_tree_root:
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003165 if (!btrfs_test_opt(tree_root->fs_info, USEBACKUPROOT))
Chris Masonaf31f5e2011-11-03 15:17:42 -04003166 goto fail_tree_roots;
3167
Anand Jainfb8c8122019-10-10 10:39:25 +08003168 free_root_pointers(fs_info, false);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003169
3170 /* don't use the log in recovery mode, it won't be valid */
3171 btrfs_set_super_log_root(disk_super, 0);
3172
3173 /* we can't trust the free space cache either */
3174 btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
3175
3176 ret = next_root_backup(fs_info, fs_info->super_copy,
3177 &num_backups_tried, &backup_index);
3178 if (ret == -1)
3179 goto fail_block_groups;
3180 goto retry_root_backup;
Chris Masoneb60cea2007-02-02 09:18:22 -05003181}
3182
Chris Masonf2984462008-04-10 16:19:33 -04003183static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
3184{
Chris Masonf2984462008-04-10 16:19:33 -04003185 if (uptodate) {
3186 set_buffer_uptodate(bh);
3187 } else {
Stefan Behrens442a4f62012-05-25 16:06:08 +02003188 struct btrfs_device *device = (struct btrfs_device *)
3189 bh->b_private;
3190
David Sterbab14af3b2015-10-08 10:43:10 +02003191 btrfs_warn_rl_in_rcu(device->dev_root->fs_info,
3192 "lost page write due to IO error on %s",
Josef Bacik606686e2012-06-04 14:03:51 -04003193 rcu_str_deref(device->name));
Nicholas D Steeves01327612016-05-19 21:18:45 -04003194 /* note, we don't set_buffer_write_io_error because we have
Chris Mason1259ab72008-05-12 13:39:03 -04003195 * our own ways of dealing with the IO errors
3196 */
Chris Masonf2984462008-04-10 16:19:33 -04003197 clear_buffer_uptodate(bh);
Stefan Behrens442a4f62012-05-25 16:06:08 +02003198 btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
Chris Masonf2984462008-04-10 16:19:33 -04003199 }
3200 unlock_buffer(bh);
3201 put_bh(bh);
3202}
3203
Anand Jain29c36d72015-08-14 18:32:58 +08003204int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
3205 struct buffer_head **bh_ret)
3206{
3207 struct buffer_head *bh;
3208 struct btrfs_super_block *super;
3209 u64 bytenr;
3210
3211 bytenr = btrfs_sb_offset(copy_num);
3212 if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode))
3213 return -EINVAL;
3214
3215 bh = __bread(bdev, bytenr / 4096, BTRFS_SUPER_INFO_SIZE);
3216 /*
3217 * If we fail to read from the underlying devices, as of now
3218 * the best option we have is to mark it EIO.
3219 */
3220 if (!bh)
3221 return -EIO;
3222
3223 super = (struct btrfs_super_block *)bh->b_data;
3224 if (btrfs_super_bytenr(super) != bytenr ||
3225 btrfs_super_magic(super) != BTRFS_MAGIC) {
3226 brelse(bh);
3227 return -EINVAL;
3228 }
3229
3230 *bh_ret = bh;
3231 return 0;
3232}
3233
3234
Yan Zhenga512bbf2008-12-08 16:46:26 -05003235struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
3236{
3237 struct buffer_head *bh;
3238 struct buffer_head *latest = NULL;
3239 struct btrfs_super_block *super;
3240 int i;
3241 u64 transid = 0;
Anand Jain92fc03f2015-08-14 18:32:51 +08003242 int ret = -EINVAL;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003243
3244 /* we would like to check all the supers, but that would make
3245 * a btrfs mount succeed after a mkfs from a different FS.
3246 * So, we need to add a special mount option to scan for
3247 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
3248 */
3249 for (i = 0; i < 1; i++) {
Anand Jain29c36d72015-08-14 18:32:58 +08003250 ret = btrfs_read_dev_one_super(bdev, i, &bh);
3251 if (ret)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003252 continue;
3253
3254 super = (struct btrfs_super_block *)bh->b_data;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003255
3256 if (!latest || btrfs_super_generation(super) > transid) {
3257 brelse(latest);
3258 latest = bh;
3259 transid = btrfs_super_generation(super);
3260 } else {
3261 brelse(bh);
3262 }
3263 }
Anand Jain92fc03f2015-08-14 18:32:51 +08003264
3265 if (!latest)
3266 return ERR_PTR(ret);
3267
Yan Zhenga512bbf2008-12-08 16:46:26 -05003268 return latest;
3269}
3270
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003271/*
3272 * this should be called twice, once with wait == 0 and
3273 * once with wait == 1. When wait == 0 is done, all the buffer heads
3274 * we write are pinned.
3275 *
3276 * They are released when wait == 1 is done.
3277 * max_mirrors must be the same for both runs, and it indicates how
3278 * many supers on this one device should be written.
3279 *
3280 * max_mirrors == 0 means to write them all.
3281 */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003282static int write_dev_supers(struct btrfs_device *device,
3283 struct btrfs_super_block *sb,
3284 int do_barriers, int wait, int max_mirrors)
3285{
3286 struct buffer_head *bh;
3287 int i;
3288 int ret;
3289 int errors = 0;
3290 u32 crc;
3291 u64 bytenr;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003292
3293 if (max_mirrors == 0)
3294 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3295
Yan Zhenga512bbf2008-12-08 16:46:26 -05003296 for (i = 0; i < max_mirrors; i++) {
3297 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003298 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3299 device->commit_total_bytes)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003300 break;
3301
3302 if (wait) {
3303 bh = __find_get_block(device->bdev, bytenr / 4096,
3304 BTRFS_SUPER_INFO_SIZE);
Josef Bacik634554d2013-04-29 10:05:57 -04003305 if (!bh) {
3306 errors++;
3307 continue;
3308 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003309 wait_on_buffer(bh);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003310 if (!buffer_uptodate(bh))
3311 errors++;
3312
3313 /* drop our reference */
3314 brelse(bh);
3315
3316 /* drop the reference from the wait == 0 run */
3317 brelse(bh);
3318 continue;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003319 } else {
3320 btrfs_set_super_bytenr(sb, bytenr);
3321
3322 crc = ~(u32)0;
Liu Bob0496682013-03-14 14:57:45 +00003323 crc = btrfs_csum_data((char *)sb +
Yan Zhenga512bbf2008-12-08 16:46:26 -05003324 BTRFS_CSUM_SIZE, crc,
3325 BTRFS_SUPER_INFO_SIZE -
3326 BTRFS_CSUM_SIZE);
3327 btrfs_csum_final(crc, sb->csum);
3328
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003329 /*
3330 * one reference for us, and we leave it for the
3331 * caller
3332 */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003333 bh = __getblk(device->bdev, bytenr / 4096,
3334 BTRFS_SUPER_INFO_SIZE);
Josef Bacik634554d2013-04-29 10:05:57 -04003335 if (!bh) {
David Sterbaf14d1042015-10-08 11:37:06 +02003336 btrfs_err(device->dev_root->fs_info,
3337 "couldn't get super buffer head for bytenr %llu",
3338 bytenr);
Josef Bacik634554d2013-04-29 10:05:57 -04003339 errors++;
3340 continue;
3341 }
3342
Yan Zhenga512bbf2008-12-08 16:46:26 -05003343 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
3344
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003345 /* one reference for submit_bh */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003346 get_bh(bh);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003347
3348 set_buffer_uptodate(bh);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003349 lock_buffer(bh);
3350 bh->b_end_io = btrfs_end_buffer_write_sync;
Stefan Behrens442a4f62012-05-25 16:06:08 +02003351 bh->b_private = device;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003352 }
3353
Chris Mason387125f2011-11-18 15:07:51 -05003354 /*
3355 * we fua the first super. The others we allow
3356 * to go down lazy.
3357 */
Wang Shilonge8117c22014-01-03 18:22:57 +08003358 if (i == 0)
Mike Christie2a222ca2016-06-05 14:31:43 -05003359 ret = btrfsic_submit_bh(REQ_OP_WRITE, WRITE_FUA, bh);
Wang Shilonge8117c22014-01-03 18:22:57 +08003360 else
Mike Christie2a222ca2016-06-05 14:31:43 -05003361 ret = btrfsic_submit_bh(REQ_OP_WRITE, WRITE_SYNC, bh);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003362 if (ret)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003363 errors++;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003364 }
3365 return errors < i ? 0 : -1;
3366}
3367
Chris Mason387125f2011-11-18 15:07:51 -05003368/*
3369 * endio for the write_dev_flush, this will wake anyone waiting
3370 * for the barrier when it is done
3371 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003372static void btrfs_end_empty_barrier(struct bio *bio)
Chris Mason387125f2011-11-18 15:07:51 -05003373{
Chris Mason387125f2011-11-18 15:07:51 -05003374 if (bio->bi_private)
3375 complete(bio->bi_private);
3376 bio_put(bio);
3377}
3378
3379/*
3380 * trigger flushes for one the devices. If you pass wait == 0, the flushes are
3381 * sent down. With wait == 1, it waits for the previous flush.
3382 *
3383 * any device where the flush fails with eopnotsupp are flagged as not-barrier
3384 * capable
3385 */
3386static int write_dev_flush(struct btrfs_device *device, int wait)
3387{
3388 struct bio *bio;
3389 int ret = 0;
3390
3391 if (device->nobarriers)
3392 return 0;
3393
3394 if (wait) {
3395 bio = device->flush_bio;
3396 if (!bio)
3397 return 0;
3398
3399 wait_for_completion(&device->flush_wait);
3400
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003401 if (bio->bi_error) {
3402 ret = bio->bi_error;
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003403 btrfs_dev_stat_inc_and_print(device,
3404 BTRFS_DEV_STAT_FLUSH_ERRS);
Chris Mason387125f2011-11-18 15:07:51 -05003405 }
3406
3407 /* drop the reference from the wait == 0 run */
3408 bio_put(bio);
3409 device->flush_bio = NULL;
3410
3411 return ret;
3412 }
3413
3414 /*
3415 * one reference for us, and we leave it for the
3416 * caller
3417 */
Jesper Juhl9c017ab2012-02-27 00:04:56 +01003418 device->flush_bio = NULL;
Chris Mason9be33952013-05-17 18:30:14 -04003419 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
Chris Mason387125f2011-11-18 15:07:51 -05003420 if (!bio)
3421 return -ENOMEM;
3422
3423 bio->bi_end_io = btrfs_end_empty_barrier;
3424 bio->bi_bdev = device->bdev;
Mike Christie37226b22016-06-05 14:31:52 -05003425 bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH);
Chris Mason387125f2011-11-18 15:07:51 -05003426 init_completion(&device->flush_wait);
3427 bio->bi_private = &device->flush_wait;
3428 device->flush_bio = bio;
3429
3430 bio_get(bio);
Mike Christie4e49ea42016-06-05 14:31:41 -05003431 btrfsic_submit_bio(bio);
Chris Mason387125f2011-11-18 15:07:51 -05003432
3433 return 0;
3434}
3435
3436/*
3437 * send an empty flush down to each device in parallel,
3438 * then wait for them
3439 */
3440static int barrier_all_devices(struct btrfs_fs_info *info)
3441{
3442 struct list_head *head;
3443 struct btrfs_device *dev;
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003444 int errors_send = 0;
3445 int errors_wait = 0;
Chris Mason387125f2011-11-18 15:07:51 -05003446 int ret;
3447
3448 /* send down all the barriers */
3449 head = &info->fs_devices->devices;
3450 list_for_each_entry_rcu(dev, head, dev_list) {
Hidetoshi Setof88ba6a2014-02-05 16:34:38 +09003451 if (dev->missing)
3452 continue;
Chris Mason387125f2011-11-18 15:07:51 -05003453 if (!dev->bdev) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003454 errors_send++;
Chris Mason387125f2011-11-18 15:07:51 -05003455 continue;
3456 }
3457 if (!dev->in_fs_metadata || !dev->writeable)
3458 continue;
3459
3460 ret = write_dev_flush(dev, 0);
3461 if (ret)
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003462 errors_send++;
Chris Mason387125f2011-11-18 15:07:51 -05003463 }
3464
3465 /* wait for all the barriers */
3466 list_for_each_entry_rcu(dev, head, dev_list) {
Hidetoshi Setof88ba6a2014-02-05 16:34:38 +09003467 if (dev->missing)
3468 continue;
Chris Mason387125f2011-11-18 15:07:51 -05003469 if (!dev->bdev) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003470 errors_wait++;
Chris Mason387125f2011-11-18 15:07:51 -05003471 continue;
3472 }
3473 if (!dev->in_fs_metadata || !dev->writeable)
3474 continue;
3475
3476 ret = write_dev_flush(dev, 1);
3477 if (ret)
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003478 errors_wait++;
Chris Mason387125f2011-11-18 15:07:51 -05003479 }
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003480 if (errors_send > info->num_tolerated_disk_barrier_failures ||
3481 errors_wait > info->num_tolerated_disk_barrier_failures)
Chris Mason387125f2011-11-18 15:07:51 -05003482 return -EIO;
3483 return 0;
3484}
3485
Zhao Lei943c6e92015-08-19 15:54:15 +08003486int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
3487{
Zhao Lei8789f4f2015-09-15 21:08:07 +08003488 int raid_type;
3489 int min_tolerated = INT_MAX;
Zhao Lei943c6e92015-08-19 15:54:15 +08003490
Zhao Lei8789f4f2015-09-15 21:08:07 +08003491 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
3492 (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
3493 min_tolerated = min(min_tolerated,
3494 btrfs_raid_array[BTRFS_RAID_SINGLE].
3495 tolerated_failures);
Zhao Lei943c6e92015-08-19 15:54:15 +08003496
Zhao Lei8789f4f2015-09-15 21:08:07 +08003497 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
3498 if (raid_type == BTRFS_RAID_SINGLE)
3499 continue;
3500 if (!(flags & btrfs_raid_group[raid_type]))
3501 continue;
3502 min_tolerated = min(min_tolerated,
3503 btrfs_raid_array[raid_type].
3504 tolerated_failures);
3505 }
Zhao Lei943c6e92015-08-19 15:54:15 +08003506
Zhao Lei8789f4f2015-09-15 21:08:07 +08003507 if (min_tolerated == INT_MAX) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04003508 pr_warn("BTRFS: unknown raid flag: %llu", flags);
Zhao Lei8789f4f2015-09-15 21:08:07 +08003509 min_tolerated = 0;
3510 }
3511
3512 return min_tolerated;
Zhao Lei943c6e92015-08-19 15:54:15 +08003513}
3514
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003515int btrfs_calc_num_tolerated_disk_barrier_failures(
3516 struct btrfs_fs_info *fs_info)
3517{
3518 struct btrfs_ioctl_space_info space;
3519 struct btrfs_space_info *sinfo;
3520 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3521 BTRFS_BLOCK_GROUP_SYSTEM,
3522 BTRFS_BLOCK_GROUP_METADATA,
3523 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003524 int i;
3525 int c;
3526 int num_tolerated_disk_barrier_failures =
3527 (int)fs_info->fs_devices->num_devices;
3528
Zhao Lei2c458042015-07-16 15:00:46 +08003529 for (i = 0; i < ARRAY_SIZE(types); i++) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003530 struct btrfs_space_info *tmp;
3531
3532 sinfo = NULL;
3533 rcu_read_lock();
3534 list_for_each_entry_rcu(tmp, &fs_info->space_info, list) {
3535 if (tmp->flags == types[i]) {
3536 sinfo = tmp;
3537 break;
3538 }
3539 }
3540 rcu_read_unlock();
3541
3542 if (!sinfo)
3543 continue;
3544
3545 down_read(&sinfo->groups_sem);
3546 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
Zhao Lei2c458042015-07-16 15:00:46 +08003547 u64 flags;
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003548
Zhao Lei2c458042015-07-16 15:00:46 +08003549 if (list_empty(&sinfo->block_groups[c]))
3550 continue;
3551
3552 btrfs_get_block_group_info(&sinfo->block_groups[c],
3553 &space);
3554 if (space.total_bytes == 0 || space.used_bytes == 0)
3555 continue;
3556 flags = space.flags;
Zhao Lei943c6e92015-08-19 15:54:15 +08003557
3558 num_tolerated_disk_barrier_failures = min(
3559 num_tolerated_disk_barrier_failures,
3560 btrfs_get_num_tolerated_disk_barrier_failures(
3561 flags));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003562 }
3563 up_read(&sinfo->groups_sem);
3564 }
3565
3566 return num_tolerated_disk_barrier_failures;
3567}
3568
Eric Sandeen48a3b632013-04-25 20:41:01 +00003569static int write_all_supers(struct btrfs_root *root, int max_mirrors)
Chris Masonf2984462008-04-10 16:19:33 -04003570{
Chris Masone5e9a522009-06-10 15:17:02 -04003571 struct list_head *head;
Chris Masonf2984462008-04-10 16:19:33 -04003572 struct btrfs_device *dev;
Chris Masona061fc82008-05-07 11:43:44 -04003573 struct btrfs_super_block *sb;
Chris Masonf2984462008-04-10 16:19:33 -04003574 struct btrfs_dev_item *dev_item;
Chris Masonf2984462008-04-10 16:19:33 -04003575 int ret;
3576 int do_barriers;
Chris Masona236aed2008-04-29 09:38:00 -04003577 int max_errors;
3578 int total_errors = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003579 u64 flags;
Chris Masonf2984462008-04-10 16:19:33 -04003580
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003581 do_barriers = !btrfs_test_opt(root->fs_info, NOBARRIER);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003582 backup_super_roots(root->fs_info);
Chris Masonf2984462008-04-10 16:19:33 -04003583
David Sterba6c417612011-04-13 15:41:04 +02003584 sb = root->fs_info->super_for_commit;
Chris Masona061fc82008-05-07 11:43:44 -04003585 dev_item = &sb->dev_item;
Chris Masone5e9a522009-06-10 15:17:02 -04003586
Chris Mason174ba502011-05-27 10:03:58 -04003587 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masone5e9a522009-06-10 15:17:02 -04003588 head = &root->fs_info->fs_devices->devices;
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01003589 max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
Chris Mason387125f2011-11-18 15:07:51 -05003590
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003591 if (do_barriers) {
3592 ret = barrier_all_devices(root->fs_info);
3593 if (ret) {
3594 mutex_unlock(
3595 &root->fs_info->fs_devices->device_list_mutex);
Anand Jain34d97002016-03-16 16:43:06 +08003596 btrfs_handle_fs_error(root->fs_info, ret,
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003597 "errors while submitting device barriers.");
3598 return ret;
3599 }
3600 }
Chris Mason387125f2011-11-18 15:07:51 -05003601
Xiao Guangrong1f781602011-04-20 10:09:16 +00003602 list_for_each_entry_rcu(dev, head, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04003603 if (!dev->bdev) {
3604 total_errors++;
3605 continue;
3606 }
Yan Zheng2b820322008-11-17 21:11:30 -05003607 if (!dev->in_fs_metadata || !dev->writeable)
Chris Masondfe25022008-05-13 13:46:40 -04003608 continue;
3609
Yan Zheng2b820322008-11-17 21:11:30 -05003610 btrfs_set_stack_device_generation(dev_item, 0);
Chris Masona061fc82008-05-07 11:43:44 -04003611 btrfs_set_stack_device_type(dev_item, dev->type);
3612 btrfs_set_stack_device_id(dev_item, dev->devid);
Miao Xie7df69d32014-07-24 11:37:13 +08003613 btrfs_set_stack_device_total_bytes(dev_item,
Miao Xie935e5cc2014-09-03 21:35:33 +08003614 dev->commit_total_bytes);
Miao Xiece7213c2014-09-03 21:35:34 +08003615 btrfs_set_stack_device_bytes_used(dev_item,
3616 dev->commit_bytes_used);
Chris Masona061fc82008-05-07 11:43:44 -04003617 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
3618 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
3619 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
3620 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003621 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003622
Chris Masona061fc82008-05-07 11:43:44 -04003623 flags = btrfs_super_flags(sb);
3624 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonf2984462008-04-10 16:19:33 -04003625
Yan Zhenga512bbf2008-12-08 16:46:26 -05003626 ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
Chris Masona236aed2008-04-29 09:38:00 -04003627 if (ret)
3628 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04003629 }
Chris Masona236aed2008-04-29 09:38:00 -04003630 if (total_errors > max_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003631 btrfs_err(root->fs_info, "%d errors while writing supers",
Chris Masond3977122009-01-05 21:25:51 -05003632 total_errors);
Stefan Behrensa724b432013-09-11 09:59:22 +02003633 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003634
Stefan Behrens9d565ba2013-08-09 17:08:40 +02003635 /* FUA is masked off if unsupported and can't be the reason */
Anand Jain34d97002016-03-16 16:43:06 +08003636 btrfs_handle_fs_error(root->fs_info, -EIO,
Stefan Behrens9d565ba2013-08-09 17:08:40 +02003637 "%d errors while writing supers", total_errors);
3638 return -EIO;
Chris Masona236aed2008-04-29 09:38:00 -04003639 }
Chris Masonf2984462008-04-10 16:19:33 -04003640
Yan Zhenga512bbf2008-12-08 16:46:26 -05003641 total_errors = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00003642 list_for_each_entry_rcu(dev, head, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04003643 if (!dev->bdev)
3644 continue;
Yan Zheng2b820322008-11-17 21:11:30 -05003645 if (!dev->in_fs_metadata || !dev->writeable)
Chris Masondfe25022008-05-13 13:46:40 -04003646 continue;
3647
Yan Zhenga512bbf2008-12-08 16:46:26 -05003648 ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
3649 if (ret)
3650 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04003651 }
Chris Mason174ba502011-05-27 10:03:58 -04003652 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masona236aed2008-04-29 09:38:00 -04003653 if (total_errors > max_errors) {
Anand Jain34d97002016-03-16 16:43:06 +08003654 btrfs_handle_fs_error(root->fs_info, -EIO,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003655 "%d errors while writing supers", total_errors);
3656 return -EIO;
Chris Masona236aed2008-04-29 09:38:00 -04003657 }
Chris Masonf2984462008-04-10 16:19:33 -04003658 return 0;
3659}
3660
Yan Zhenga512bbf2008-12-08 16:46:26 -05003661int write_ctree_super(struct btrfs_trans_handle *trans,
3662 struct btrfs_root *root, int max_mirrors)
Chris Masoncfaa7292007-02-21 17:04:57 -05003663{
Rashikaf570e7572013-10-31 16:50:42 +05303664 return write_all_supers(root, max_mirrors);
Chris Masoncfaa7292007-02-21 17:04:57 -05003665}
3666
Miao Xiecb517ea2013-05-15 07:48:19 +00003667/* Drop a fs root from the radix tree and free it. */
3668void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
3669 struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -04003670{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003671 spin_lock(&fs_info->fs_roots_radix_lock);
Chris Mason2619ba12007-04-10 16:58:11 -04003672 radix_tree_delete(&fs_info->fs_roots_radix,
3673 (unsigned long)root->root_key.objectid);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003674 spin_unlock(&fs_info->fs_roots_radix_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003675
3676 if (btrfs_root_refs(&root->root_item) == 0)
3677 synchronize_srcu(&fs_info->subvol_srcu);
3678
Liu Bo1c1ea4f2016-07-19 15:36:05 -07003679 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
Liu Bo33217192013-02-27 13:28:24 +00003680 btrfs_free_log(NULL, root);
Liu Bo1c1ea4f2016-07-19 15:36:05 -07003681 if (root->reloc_root) {
3682 free_extent_buffer(root->reloc_root->node);
3683 free_extent_buffer(root->reloc_root->commit_root);
3684 btrfs_put_fs_root(root->reloc_root);
3685 root->reloc_root = NULL;
3686 }
3687 }
Liu Bo33217192013-02-27 13:28:24 +00003688
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003689 if (root->free_ino_pinned)
3690 __btrfs_remove_free_space_cache(root->free_ino_pinned);
3691 if (root->free_ino_ctl)
3692 __btrfs_remove_free_space_cache(root->free_ino_ctl);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003693 free_fs_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003694}
3695
3696static void free_fs_root(struct btrfs_root *root)
3697{
David Sterba57cdc8d2014-02-05 02:37:48 +01003698 iput(root->ino_cache_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003699 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
Filipe David Borba Manana1cb048f2013-08-20 00:52:51 +01003700 btrfs_free_block_rsv(root, root->orphan_block_rsv);
3701 root->orphan_block_rsv = NULL;
Al Viro0ee5dc62011-07-07 15:44:25 -04003702 if (root->anon_dev)
3703 free_anon_bdev(root->anon_dev);
Miao Xie8257b2d2014-03-06 13:38:19 +08003704 if (root->subv_writers)
3705 btrfs_free_subvolume_writers(root->subv_writers);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003706 free_extent_buffer(root->node);
3707 free_extent_buffer(root->commit_root);
Li Zefan581bb052011-04-20 10:06:11 +08003708 kfree(root->free_ino_ctl);
3709 kfree(root->free_ino_pinned);
Chris Masond3977122009-01-05 21:25:51 -05003710 kfree(root->name);
Miao Xieb0feb9d2013-05-15 07:48:20 +00003711 btrfs_put_fs_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -04003712}
3713
Miao Xiecb517ea2013-05-15 07:48:19 +00003714void btrfs_free_fs_root(struct btrfs_root *root)
3715{
3716 free_fs_root(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05003717}
3718
Yan Zhengc146afa2008-11-12 14:34:12 -05003719int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
3720{
3721 u64 root_objectid = 0;
3722 struct btrfs_root *gang[8];
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003723 int i = 0;
3724 int err = 0;
3725 unsigned int ret = 0;
3726 int index;
Yan Zhengc146afa2008-11-12 14:34:12 -05003727
3728 while (1) {
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003729 index = srcu_read_lock(&fs_info->subvol_srcu);
Yan Zhengc146afa2008-11-12 14:34:12 -05003730 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
3731 (void **)gang, root_objectid,
3732 ARRAY_SIZE(gang));
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003733 if (!ret) {
3734 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan Zhengc146afa2008-11-12 14:34:12 -05003735 break;
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003736 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003737 root_objectid = gang[ret - 1]->root_key.objectid + 1;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003738
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003739 for (i = 0; i < ret; i++) {
3740 /* Avoid to grab roots in dead_roots */
3741 if (btrfs_root_refs(&gang[i]->root_item) == 0) {
3742 gang[i] = NULL;
3743 continue;
3744 }
3745 /* grab all the search result for later use */
3746 gang[i] = btrfs_grab_fs_root(gang[i]);
3747 }
3748 srcu_read_unlock(&fs_info->subvol_srcu, index);
3749
3750 for (i = 0; i < ret; i++) {
3751 if (!gang[i])
3752 continue;
Yan Zhengc146afa2008-11-12 14:34:12 -05003753 root_objectid = gang[i]->root_key.objectid;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003754 err = btrfs_orphan_cleanup(gang[i]);
3755 if (err)
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003756 break;
3757 btrfs_put_fs_root(gang[i]);
Yan Zhengc146afa2008-11-12 14:34:12 -05003758 }
3759 root_objectid++;
3760 }
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003761
3762 /* release the uncleaned roots due to error */
3763 for (; i < ret; i++) {
3764 if (gang[i])
3765 btrfs_put_fs_root(gang[i]);
3766 }
3767 return err;
Yan Zhengc146afa2008-11-12 14:34:12 -05003768}
3769
3770int btrfs_commit_super(struct btrfs_root *root)
3771{
3772 struct btrfs_trans_handle *trans;
Yan Zhengc146afa2008-11-12 14:34:12 -05003773
3774 mutex_lock(&root->fs_info->cleaner_mutex);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003775 btrfs_run_delayed_iputs(root);
Yan Zhengc146afa2008-11-12 14:34:12 -05003776 mutex_unlock(&root->fs_info->cleaner_mutex);
David Sterba9d1a2a32013-03-12 15:13:28 +00003777 wake_up_process(root->fs_info->cleaner_kthread);
Yan, Zhengc71bf092009-11-12 09:34:40 +00003778
3779 /* wait until ongoing cleanup work done */
3780 down_write(&root->fs_info->cleanup_work_sem);
3781 up_write(&root->fs_info->cleanup_work_sem);
3782
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003783 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003784 if (IS_ERR(trans))
3785 return PTR_ERR(trans);
Liu Bod52c1bc2013-11-05 11:45:53 +08003786 return btrfs_commit_transaction(trans, root);
Yan Zhengc146afa2008-11-12 14:34:12 -05003787}
3788
David Sterba3abdbd72014-06-04 18:10:45 +02003789void close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -05003790{
Chris Mason0f7d52f2007-04-09 10:42:37 -04003791 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003792 int ret;
Chris Masone089f052007-03-16 16:20:31 -04003793
Josef Bacikafcdd122016-09-02 15:40:02 -04003794 set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
Chris Masona2135012008-06-25 16:01:30 -04003795
Justin Maggard7343dd62015-11-04 15:56:16 -08003796 /* wait for the qgroup rescan worker to stop */
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04003797 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard7343dd62015-11-04 15:56:16 -08003798
Stefan Behrens803b2f52013-08-15 17:11:21 +02003799 /* wait for the uuid_scan task to finish */
3800 down(&fs_info->uuid_tree_rescan_sem);
3801 /* avoid complains from lockdep et al., set sem back to initial state */
3802 up(&fs_info->uuid_tree_rescan_sem);
3803
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003804 /* pause restriper - we want to resume on mount */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003805 btrfs_pause_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003806
Stefan Behrens8dabb742012-11-06 13:15:27 +01003807 btrfs_dev_replace_suspend_for_unmount(fs_info);
3808
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003809 btrfs_scrub_cancel(fs_info);
Chris Mason4cb53002011-05-24 15:35:30 -04003810
3811 /* wait for any defraggers to finish */
3812 wait_event(fs_info->transaction_wait,
3813 (atomic_read(&fs_info->defrag_running) == 0));
3814
3815 /* clear out the rbtree of defraggable inodes */
Miao Xie26176e72012-11-26 09:26:20 +00003816 btrfs_cleanup_defrag_inodes(fs_info);
Chris Mason4cb53002011-05-24 15:35:30 -04003817
Miao Xie21c7e752014-05-13 17:29:04 -07003818 cancel_work_sync(&fs_info->async_reclaim_work);
3819
Yan Zhengc146afa2008-11-12 14:34:12 -05003820 if (!(fs_info->sb->s_flags & MS_RDONLY)) {
Jeff Mahoneye44163e2015-06-15 09:41:18 -04003821 /*
3822 * If the cleaner thread is stopped and there are
3823 * block groups queued for removal, the deletion will be
3824 * skipped when we quit the cleaner thread.
3825 */
Jeff Mahoneye44163e2015-06-15 09:41:18 -04003826 btrfs_delete_unused_bgs(root->fs_info);
Jeff Mahoneye44163e2015-06-15 09:41:18 -04003827
liuboacce9522011-01-06 19:30:25 +08003828 ret = btrfs_commit_super(root);
3829 if (ret)
Eric Sandeen04892342014-08-01 18:12:36 -05003830 btrfs_err(fs_info, "commit super ret %d", ret);
liuboacce9522011-01-06 19:30:25 +08003831 }
3832
Miao Xie87533c42013-01-29 10:14:48 +00003833 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Stefan Behrens68ce9682012-08-01 05:45:52 -06003834 btrfs_error_commit_super(root);
Chris Masoned2ff2c2007-03-01 18:59:40 -05003835
Al Viroe3029d92011-11-17 00:56:18 -05003836 kthread_stop(fs_info->transaction_kthread);
3837 kthread_stop(fs_info->cleaner_kthread);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003838
Josef Bacikafcdd122016-09-02 15:40:02 -04003839 set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
Yan Zhengf25784b2009-07-28 08:41:57 -04003840
Eric Sandeen04892342014-08-01 18:12:36 -05003841 btrfs_free_qgroup_config(fs_info);
Arne Jansenbcef60f2011-09-13 15:23:30 +02003842
Miao Xie963d6782013-01-29 10:10:51 +00003843 if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
Eric Sandeen04892342014-08-01 18:12:36 -05003844 btrfs_info(fs_info, "at unmount delalloc count %lld",
Miao Xie963d6782013-01-29 10:10:51 +00003845 percpu_counter_sum(&fs_info->delalloc_bytes));
Chris Masonb0c68f82008-01-31 11:05:37 -05003846 }
Yanbcc63ab2008-07-30 16:29:20 -04003847
Anand Jain6618a592015-08-14 18:32:47 +08003848 btrfs_sysfs_remove_mounted(fs_info);
Anand Jainb7c35e82015-03-10 06:38:38 +08003849 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04003850
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003851 btrfs_free_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -05003852
Liu Bo1a4319c2014-01-13 19:53:53 +08003853 btrfs_put_block_group_cache(fs_info);
3854
Josef Bacik2b1360d2013-10-07 15:14:44 -04003855 btrfs_free_block_groups(fs_info);
3856
Wang Shilongde348ee2014-04-09 19:23:22 +08003857 /*
3858 * we must make sure there is not any read request to
3859 * submit after we stopping all workers.
3860 */
3861 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
Josef Bacik96192492013-10-16 13:53:28 -04003862 btrfs_stop_all_workers(fs_info);
3863
Josef Bacikafcdd122016-09-02 15:40:02 -04003864 clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
Anand Jainfb8c8122019-10-10 10:39:25 +08003865 free_root_pointers(fs_info, true);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003866
Josef Bacik13e6c372013-05-30 16:55:44 -04003867 iput(fs_info->btree_inode);
Chris Masond6bfde82008-04-30 13:59:35 -04003868
Stefan Behrens21adbd52011-11-09 13:44:05 +01003869#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003870 if (btrfs_test_opt(root->fs_info, CHECK_INTEGRITY))
Stefan Behrens21adbd52011-11-09 13:44:05 +01003871 btrfsic_unmount(root, fs_info->fs_devices);
3872#endif
3873
Chris Masondfe25022008-05-13 13:46:40 -04003874 btrfs_close_devices(fs_info->fs_devices);
Chris Mason0b86a832008-03-24 15:01:56 -04003875 btrfs_mapping_tree_free(&fs_info->mapping_tree);
Chris Masonb248a412008-04-14 09:48:18 -04003876
Miao Xiee2d84522013-01-29 10:09:20 +00003877 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
Miao Xie963d6782013-01-29 10:10:51 +00003878 percpu_counter_destroy(&fs_info->delalloc_bytes);
Miao Xiec404e0d2014-01-30 16:46:55 +08003879 percpu_counter_destroy(&fs_info->bio_counter);
Chris Mason04160082008-03-26 10:28:07 -04003880 bdi_destroy(&fs_info->bdi);
Yan, Zheng76dda932009-09-21 16:00:26 -04003881 cleanup_srcu_struct(&fs_info->subvol_srcu);
Chris Mason0b86a832008-03-24 15:01:56 -04003882
David Woodhouse53b381b2013-01-29 18:40:14 -05003883 btrfs_free_stripe_hash_table(fs_info);
3884
Chris Masoncdfb0802015-04-06 18:17:00 -07003885 __btrfs_free_block_rsv(root->orphan_block_rsv);
Filipe David Borba Manana1cb048f2013-08-20 00:52:51 +01003886 root->orphan_block_rsv = NULL;
Filipe Manana04216822014-11-27 21:14:15 +00003887
3888 lock_chunks(root);
3889 while (!list_empty(&fs_info->pinned_chunks)) {
3890 struct extent_map *em;
3891
3892 em = list_first_entry(&fs_info->pinned_chunks,
3893 struct extent_map, list);
3894 list_del_init(&em->list);
3895 free_extent_map(em);
3896 }
3897 unlock_chunks(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05003898}
3899
Chris Masonb9fab912012-05-06 07:23:47 -04003900int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
3901 int atomic)
Chris Masonccd467d2007-06-28 15:57:36 -04003902{
Chris Mason1259ab72008-05-12 13:39:03 -04003903 int ret;
Chris Mason727011e2010-08-06 13:21:20 -04003904 struct inode *btree_inode = buf->pages[0]->mapping->host;
Chris Mason1259ab72008-05-12 13:39:03 -04003905
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003906 ret = extent_buffer_uptodate(buf);
Chris Mason1259ab72008-05-12 13:39:03 -04003907 if (!ret)
3908 return ret;
3909
3910 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
Chris Masonb9fab912012-05-06 07:23:47 -04003911 parent_transid, atomic);
3912 if (ret == -EAGAIN)
3913 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -04003914 return !ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003915}
Chris Mason6702ed42007-08-07 16:15:09 -04003916
Chris Mason5f39d392007-10-15 16:14:19 -04003917void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
3918{
Josef Bacik06ea65a2013-09-19 16:07:01 -04003919 struct btrfs_root *root;
Chris Mason5f39d392007-10-15 16:14:19 -04003920 u64 transid = btrfs_header_generation(buf);
Chris Masonb9473432009-03-13 11:00:37 -04003921 int was_dirty;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003922
Josef Bacik06ea65a2013-09-19 16:07:01 -04003923#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3924 /*
3925 * This is a fast path so only do this check if we have sanity tests
3926 * enabled. Normal people shouldn't be marking dummy buffers as dirty
3927 * outside of the sanity tests.
3928 */
3929 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags)))
3930 return;
3931#endif
3932 root = BTRFS_I(buf->pages[0]->mapping->host)->root;
Chris Masonb9447ef2009-03-09 11:45:38 -04003933 btrfs_assert_tree_locked(buf);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003934 if (transid != root->fs_info->generation)
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003935 WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003936 buf->start, transid, root->fs_info->generation);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003937 was_dirty = set_extent_buffer_dirty(buf);
Miao Xiee2d84522013-01-29 10:09:20 +00003938 if (!was_dirty)
3939 __percpu_counter_add(&root->fs_info->dirty_metadata_bytes,
3940 buf->len,
3941 root->fs_info->dirty_metadata_batch);
Filipe Manana1f21ef02014-04-09 15:37:06 +01003942#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
Qu Wenruof7438e62017-11-08 08:54:24 +08003943 /*
3944 * Since btrfs_mark_buffer_dirty() can be called with item pointer set
3945 * but item data not updated.
3946 * So here we should only check item pointers, not item data.
3947 */
3948 if (btrfs_header_level(buf) == 0 &&
3949 btrfs_check_leaf_relaxed(root, buf)) {
Filipe Manana1f21ef02014-04-09 15:37:06 +01003950 btrfs_print_leaf(root, buf);
3951 ASSERT(0);
3952 }
3953#endif
Chris Masoneb60cea2007-02-02 09:18:22 -05003954}
3955
Liu Bob53d3f52012-11-14 14:34:34 +00003956static void __btrfs_btree_balance_dirty(struct btrfs_root *root,
3957 int flush_delayed)
Chris Mason35b7e472007-05-02 15:53:43 -04003958{
Chris Mason188de642008-05-09 11:52:25 -04003959 /*
3960 * looks as though older kernels can get into trouble with
3961 * this code, they end up stuck in balance_dirty_pages forever
3962 */
Miao Xiee2d84522013-01-29 10:09:20 +00003963 int ret;
Chris Masond6bfde82008-04-30 13:59:35 -04003964
Jens Axboe6933c022009-03-17 09:36:37 +01003965 if (current->flags & PF_MEMALLOC)
Chris Masond6bfde82008-04-30 13:59:35 -04003966 return;
3967
Liu Bob53d3f52012-11-14 14:34:34 +00003968 if (flush_delayed)
3969 btrfs_balance_delayed_items(root);
Miao Xie16cdcec2011-04-22 18:12:22 +08003970
Ethan Liend92fa5e2018-07-02 15:44:58 +08003971 ret = __percpu_counter_compare(&root->fs_info->dirty_metadata_bytes,
3972 BTRFS_DIRTY_METADATA_THRESH,
3973 root->fs_info->dirty_metadata_batch);
Miao Xiee2d84522013-01-29 10:09:20 +00003974 if (ret > 0) {
Namjae Jeond0e1d662012-12-11 16:00:21 -08003975 balance_dirty_pages_ratelimited(
3976 root->fs_info->btree_inode->i_mapping);
Miao Xie16cdcec2011-04-22 18:12:22 +08003977 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003978}
3979
Liu Bob53d3f52012-11-14 14:34:34 +00003980void btrfs_btree_balance_dirty(struct btrfs_root *root)
Miao Xie16cdcec2011-04-22 18:12:22 +08003981{
Liu Bob53d3f52012-11-14 14:34:34 +00003982 __btrfs_btree_balance_dirty(root, 1);
3983}
Miao Xie16cdcec2011-04-22 18:12:22 +08003984
Liu Bob53d3f52012-11-14 14:34:34 +00003985void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root)
3986{
3987 __btrfs_btree_balance_dirty(root, 0);
Chris Mason35b7e472007-05-02 15:53:43 -04003988}
Chris Mason6b800532007-10-15 16:17:34 -04003989
Chris Masonca7a79a2008-05-12 12:59:19 -04003990int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
Chris Mason6b800532007-10-15 16:17:34 -04003991{
Chris Mason727011e2010-08-06 13:21:20 -04003992 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
Josef Bacik8436ea912016-09-02 15:40:03 -04003993 return btree_read_extent_buffer_pages(root, buf, parent_transid);
Chris Mason6b800532007-10-15 16:17:34 -04003994}
Chris Mason0da54682007-11-07 21:08:01 -05003995
David Sterbafcd1f0652012-03-06 00:06:18 +01003996static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
liuboacce9522011-01-06 19:30:25 +08003997 int read_only)
3998{
David Sterbac9260932014-09-30 19:16:47 +02003999 struct btrfs_super_block *sb = fs_info->super_copy;
Qu Wenruo319e4d02015-12-15 09:14:36 +08004000 u64 nodesize = btrfs_super_nodesize(sb);
4001 u64 sectorsize = btrfs_super_sectorsize(sb);
David Sterbac9260932014-09-30 19:16:47 +02004002 int ret = 0;
4003
Qu Wenruo319e4d02015-12-15 09:14:36 +08004004 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004005 btrfs_err(fs_info, "no valid FS found");
Qu Wenruo319e4d02015-12-15 09:14:36 +08004006 ret = -EINVAL;
4007 }
Anand Jain3feba592018-01-09 09:05:43 +08004008 if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) {
4009 btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu",
Qu Wenruo319e4d02015-12-15 09:14:36 +08004010 btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
Anand Jain3feba592018-01-09 09:05:43 +08004011 ret = -EINVAL;
4012 }
David Sterba21e76262014-10-27 13:52:21 +01004013 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004014 btrfs_err(fs_info, "tree_root level too big: %d >= %d",
David Sterba21e76262014-10-27 13:52:21 +01004015 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
David Sterbac9260932014-09-30 19:16:47 +02004016 ret = -EINVAL;
4017 }
David Sterba21e76262014-10-27 13:52:21 +01004018 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004019 btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
David Sterba21e76262014-10-27 13:52:21 +01004020 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
David Sterbac9260932014-09-30 19:16:47 +02004021 ret = -EINVAL;
4022 }
David Sterba21e76262014-10-27 13:52:21 +01004023 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004024 btrfs_err(fs_info, "log_root level too big: %d >= %d",
David Sterba21e76262014-10-27 13:52:21 +01004025 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
David Sterbac9260932014-09-30 19:16:47 +02004026 ret = -EINVAL;
4027 }
4028
David Sterba1104a882013-03-06 15:57:46 +01004029 /*
Qu Wenruo319e4d02015-12-15 09:14:36 +08004030 * Check sectorsize and nodesize first, other check will need it.
4031 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
David Sterba1104a882013-03-06 15:57:46 +01004032 */
Qu Wenruo319e4d02015-12-15 09:14:36 +08004033 if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
4034 sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004035 btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
David Sterba75d6ad32014-10-31 17:18:08 +01004036 ret = -EINVAL;
4037 }
Qu Wenruo319e4d02015-12-15 09:14:36 +08004038 /* Only PAGE SIZE is supported yet */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004039 if (sectorsize != PAGE_SIZE) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004040 btrfs_err(fs_info,
4041 "sectorsize %llu not supported yet, only support %lu",
4042 sectorsize, PAGE_SIZE);
Qu Wenruo319e4d02015-12-15 09:14:36 +08004043 ret = -EINVAL;
4044 }
4045 if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
4046 nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004047 btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
Qu Wenruo319e4d02015-12-15 09:14:36 +08004048 ret = -EINVAL;
4049 }
4050 if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004051 btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
4052 le32_to_cpu(sb->__unused_leafsize), nodesize);
Qu Wenruo319e4d02015-12-15 09:14:36 +08004053 ret = -EINVAL;
4054 }
4055
4056 /* Root alignment check */
4057 if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004058 btrfs_warn(fs_info, "tree_root block unaligned: %llu",
4059 btrfs_super_root(sb));
Qu Wenruo319e4d02015-12-15 09:14:36 +08004060 ret = -EINVAL;
4061 }
4062 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004063 btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
4064 btrfs_super_chunk_root(sb));
Qu Wenruo319e4d02015-12-15 09:14:36 +08004065 ret = -EINVAL;
4066 }
4067 if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004068 btrfs_warn(fs_info, "log_root block unaligned: %llu",
4069 btrfs_super_log_root(sb));
David Sterba75d6ad32014-10-31 17:18:08 +01004070 ret = -EINVAL;
4071 }
4072
David Sterbac9260932014-09-30 19:16:47 +02004073 if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004074 btrfs_err(fs_info,
4075 "dev_item UUID does not match fsid: %pU != %pU",
4076 fs_info->fsid, sb->dev_item.fsid);
David Sterbac9260932014-09-30 19:16:47 +02004077 ret = -EINVAL;
4078 }
4079
4080 /*
4081 * Hint to catch really bogus numbers, bitflips or so, more exact checks are
4082 * done later
4083 */
Liu Bo99e3ecf2016-06-03 12:05:14 -07004084 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
4085 btrfs_err(fs_info, "bytes_used is too small %llu",
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004086 btrfs_super_bytes_used(sb));
Liu Bo99e3ecf2016-06-03 12:05:14 -07004087 ret = -EINVAL;
4088 }
Chandan Rajendrab7f67052016-06-23 15:16:44 +05304089 if (!is_power_of_2(btrfs_super_stripesize(sb))) {
Liu Bo99e3ecf2016-06-03 12:05:14 -07004090 btrfs_err(fs_info, "invalid stripesize %u",
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004091 btrfs_super_stripesize(sb));
Liu Bo99e3ecf2016-06-03 12:05:14 -07004092 ret = -EINVAL;
4093 }
David Sterba21e76262014-10-27 13:52:21 +01004094 if (btrfs_super_num_devices(sb) > (1UL << 31))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004095 btrfs_warn(fs_info, "suspicious number of devices: %llu",
4096 btrfs_super_num_devices(sb));
David Sterba75d6ad32014-10-31 17:18:08 +01004097 if (btrfs_super_num_devices(sb) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004098 btrfs_err(fs_info, "number of devices is 0");
David Sterba75d6ad32014-10-31 17:18:08 +01004099 ret = -EINVAL;
4100 }
David Sterbac9260932014-09-30 19:16:47 +02004101
David Sterba21e76262014-10-27 13:52:21 +01004102 if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004103 btrfs_err(fs_info, "super offset mismatch %llu != %u",
4104 btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
David Sterbac9260932014-09-30 19:16:47 +02004105 ret = -EINVAL;
4106 }
4107
4108 /*
David Sterbace7fca52014-10-31 18:42:05 +01004109 * Obvious sys_chunk_array corruptions, it must hold at least one key
4110 * and one chunk
4111 */
4112 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004113 btrfs_err(fs_info, "system chunk array too big %u > %u",
4114 btrfs_super_sys_array_size(sb),
4115 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
David Sterbace7fca52014-10-31 18:42:05 +01004116 ret = -EINVAL;
4117 }
4118 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
4119 + sizeof(struct btrfs_chunk)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004120 btrfs_err(fs_info, "system chunk array too small %u < %zu",
4121 btrfs_super_sys_array_size(sb),
4122 sizeof(struct btrfs_disk_key)
4123 + sizeof(struct btrfs_chunk));
David Sterbace7fca52014-10-31 18:42:05 +01004124 ret = -EINVAL;
4125 }
4126
4127 /*
David Sterbac9260932014-09-30 19:16:47 +02004128 * The generation is a global counter, we'll trust it more than the others
4129 * but it's still possible that it's the one that's wrong.
4130 */
David Sterba21e76262014-10-27 13:52:21 +01004131 if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004132 btrfs_warn(fs_info,
4133 "suspicious: generation < chunk_root_generation: %llu < %llu",
4134 btrfs_super_generation(sb),
4135 btrfs_super_chunk_root_generation(sb));
David Sterba21e76262014-10-27 13:52:21 +01004136 if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
4137 && btrfs_super_cache_generation(sb) != (u64)-1)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004138 btrfs_warn(fs_info,
4139 "suspicious: generation < cache_generation: %llu < %llu",
4140 btrfs_super_generation(sb),
4141 btrfs_super_cache_generation(sb));
David Sterbac9260932014-09-30 19:16:47 +02004142
4143 return ret;
liuboacce9522011-01-06 19:30:25 +08004144}
4145
Eric Sandeen48a3b632013-04-25 20:41:01 +00004146static void btrfs_error_commit_super(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004147{
liuboacce9522011-01-06 19:30:25 +08004148 mutex_lock(&root->fs_info->cleaner_mutex);
4149 btrfs_run_delayed_iputs(root);
4150 mutex_unlock(&root->fs_info->cleaner_mutex);
4151
4152 down_write(&root->fs_info->cleanup_work_sem);
4153 up_write(&root->fs_info->cleanup_work_sem);
4154
4155 /* cleanup FS via transaction */
4156 btrfs_cleanup_transaction(root);
liuboacce9522011-01-06 19:30:25 +08004157}
4158
Jeff Mahoney143bede2012-03-01 14:56:26 +01004159static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004160{
liuboacce9522011-01-06 19:30:25 +08004161 struct btrfs_ordered_extent *ordered;
liuboacce9522011-01-06 19:30:25 +08004162
Miao Xie199c2a92013-05-15 07:48:23 +00004163 spin_lock(&root->ordered_extent_lock);
Josef Bacik779880e2013-01-31 14:30:08 -05004164 /*
4165 * This will just short circuit the ordered completion stuff which will
4166 * make sure the ordered extent gets properly cleaned up.
4167 */
Miao Xie199c2a92013-05-15 07:48:23 +00004168 list_for_each_entry(ordered, &root->ordered_extents,
Josef Bacik779880e2013-01-31 14:30:08 -05004169 root_extent_list)
4170 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
Miao Xie199c2a92013-05-15 07:48:23 +00004171 spin_unlock(&root->ordered_extent_lock);
4172}
4173
4174static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4175{
4176 struct btrfs_root *root;
4177 struct list_head splice;
4178
4179 INIT_LIST_HEAD(&splice);
4180
4181 spin_lock(&fs_info->ordered_root_lock);
4182 list_splice_init(&fs_info->ordered_roots, &splice);
4183 while (!list_empty(&splice)) {
4184 root = list_first_entry(&splice, struct btrfs_root,
4185 ordered_root);
Josef Bacik1de2cfd2013-09-27 16:36:02 -04004186 list_move_tail(&root->ordered_root,
4187 &fs_info->ordered_roots);
Miao Xie199c2a92013-05-15 07:48:23 +00004188
Liu Bo2a85d9c2014-02-10 17:07:16 +08004189 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00004190 btrfs_destroy_ordered_extents(root);
4191
Liu Bo2a85d9c2014-02-10 17:07:16 +08004192 cond_resched();
4193 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00004194 }
4195 spin_unlock(&fs_info->ordered_root_lock);
Josef Bacikfa9b1702018-11-21 14:05:45 -05004196
4197 /*
4198 * We need this here because if we've been flipped read-only we won't
4199 * get sync() from the umount, so we need to make sure any ordered
4200 * extents that haven't had their dirty pages IO start writeout yet
4201 * actually get run and error out properly.
4202 */
4203 btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
liuboacce9522011-01-06 19:30:25 +08004204}
4205
Stefan Behrens35a36212013-08-14 18:12:25 +02004206static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4207 struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004208{
4209 struct rb_node *node;
4210 struct btrfs_delayed_ref_root *delayed_refs;
4211 struct btrfs_delayed_ref_node *ref;
4212 int ret = 0;
4213
4214 delayed_refs = &trans->delayed_refs;
4215
4216 spin_lock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004217 if (atomic_read(&delayed_refs->num_entries) == 0) {
David Sterbacfece4d2011-04-25 19:43:52 -04004218 spin_unlock(&delayed_refs->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05004219 btrfs_info(root->fs_info, "delayed_refs has NO entry");
liuboacce9522011-01-06 19:30:25 +08004220 return ret;
4221 }
4222
Josef Bacikd7df2c72014-01-23 09:21:38 -05004223 while ((node = rb_first(&delayed_refs->href_root)) != NULL) {
4224 struct btrfs_delayed_ref_head *head;
Qu Wenruoc6fc2452015-03-30 17:03:00 +08004225 struct btrfs_delayed_ref_node *tmp;
Josef Bacike78417d2013-06-03 16:42:36 -04004226 bool pin_bytes = false;
liuboacce9522011-01-06 19:30:25 +08004227
Josef Bacikd7df2c72014-01-23 09:21:38 -05004228 head = rb_entry(node, struct btrfs_delayed_ref_head,
4229 href_node);
4230 if (!mutex_trylock(&head->mutex)) {
4231 atomic_inc(&head->node.refs);
4232 spin_unlock(&delayed_refs->lock);
liuboacce9522011-01-06 19:30:25 +08004233
Josef Bacikd7df2c72014-01-23 09:21:38 -05004234 mutex_lock(&head->mutex);
Josef Bacike78417d2013-06-03 16:42:36 -04004235 mutex_unlock(&head->mutex);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004236 btrfs_put_delayed_ref(&head->node);
4237 spin_lock(&delayed_refs->lock);
4238 continue;
Josef Bacike78417d2013-06-03 16:42:36 -04004239 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05004240 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08004241 list_for_each_entry_safe_reverse(ref, tmp, &head->ref_list,
4242 list) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05004243 ref->in_tree = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +08004244 list_del(&ref->list);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004245 atomic_dec(&delayed_refs->num_entries);
4246 btrfs_put_delayed_ref(ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004247 }
4248 if (head->must_insert_reserved)
4249 pin_bytes = true;
4250 btrfs_free_delayed_extent_op(head->extent_op);
4251 delayed_refs->num_heads--;
4252 if (head->processing == 0)
4253 delayed_refs->num_heads_ready--;
4254 atomic_dec(&delayed_refs->num_entries);
4255 head->node.in_tree = 0;
4256 rb_erase(&head->href_node, &delayed_refs->href_root);
4257 spin_unlock(&head->lock);
4258 spin_unlock(&delayed_refs->lock);
4259 mutex_unlock(&head->mutex);
liuboacce9522011-01-06 19:30:25 +08004260
Josef Bacikd7df2c72014-01-23 09:21:38 -05004261 if (pin_bytes)
4262 btrfs_pin_extent(root, head->node.bytenr,
4263 head->node.num_bytes, 1);
4264 btrfs_put_delayed_ref(&head->node);
liuboacce9522011-01-06 19:30:25 +08004265 cond_resched();
4266 spin_lock(&delayed_refs->lock);
4267 }
4268
4269 spin_unlock(&delayed_refs->lock);
4270
4271 return ret;
4272}
4273
Jeff Mahoney143bede2012-03-01 14:56:26 +01004274static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004275{
4276 struct btrfs_inode *btrfs_inode;
4277 struct list_head splice;
4278
4279 INIT_LIST_HEAD(&splice);
4280
Miao Xieeb73c1b2013-05-15 07:48:22 +00004281 spin_lock(&root->delalloc_lock);
4282 list_splice_init(&root->delalloc_inodes, &splice);
liuboacce9522011-01-06 19:30:25 +08004283
4284 while (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00004285 btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
4286 delalloc_inodes);
liuboacce9522011-01-06 19:30:25 +08004287
4288 list_del_init(&btrfs_inode->delalloc_inodes);
Miao Xiedf0af1a2013-01-29 10:11:59 +00004289 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
4290 &btrfs_inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00004291 spin_unlock(&root->delalloc_lock);
liuboacce9522011-01-06 19:30:25 +08004292
4293 btrfs_invalidate_inodes(btrfs_inode->root);
Miao Xieb216cbf2013-05-15 07:48:21 +00004294
Miao Xieeb73c1b2013-05-15 07:48:22 +00004295 spin_lock(&root->delalloc_lock);
liuboacce9522011-01-06 19:30:25 +08004296 }
4297
Miao Xieeb73c1b2013-05-15 07:48:22 +00004298 spin_unlock(&root->delalloc_lock);
4299}
4300
4301static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4302{
4303 struct btrfs_root *root;
4304 struct list_head splice;
4305
4306 INIT_LIST_HEAD(&splice);
4307
4308 spin_lock(&fs_info->delalloc_root_lock);
4309 list_splice_init(&fs_info->delalloc_roots, &splice);
4310 while (!list_empty(&splice)) {
4311 root = list_first_entry(&splice, struct btrfs_root,
4312 delalloc_root);
4313 list_del_init(&root->delalloc_root);
4314 root = btrfs_grab_fs_root(root);
4315 BUG_ON(!root);
4316 spin_unlock(&fs_info->delalloc_root_lock);
4317
4318 btrfs_destroy_delalloc_inodes(root);
4319 btrfs_put_fs_root(root);
4320
4321 spin_lock(&fs_info->delalloc_root_lock);
4322 }
4323 spin_unlock(&fs_info->delalloc_root_lock);
liuboacce9522011-01-06 19:30:25 +08004324}
4325
4326static int btrfs_destroy_marked_extents(struct btrfs_root *root,
4327 struct extent_io_tree *dirty_pages,
4328 int mark)
4329{
4330 int ret;
liuboacce9522011-01-06 19:30:25 +08004331 struct extent_buffer *eb;
4332 u64 start = 0;
4333 u64 end;
liuboacce9522011-01-06 19:30:25 +08004334
4335 while (1) {
4336 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04004337 mark, NULL);
liuboacce9522011-01-06 19:30:25 +08004338 if (ret)
4339 break;
4340
David Sterba91166212016-04-26 23:54:39 +02004341 clear_extent_bits(dirty_pages, start, end, mark);
liuboacce9522011-01-06 19:30:25 +08004342 while (start <= end) {
Daniel Dressler01d58472014-11-21 17:15:07 +09004343 eb = btrfs_find_tree_block(root->fs_info, start);
David Sterba707e8a02014-06-04 19:22:26 +02004344 start += root->nodesize;
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004345 if (!eb)
liuboacce9522011-01-06 19:30:25 +08004346 continue;
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004347 wait_on_extent_buffer_writeback(eb);
liuboacce9522011-01-06 19:30:25 +08004348
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004349 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
4350 &eb->bflags))
4351 clear_extent_buffer_dirty(eb);
4352 free_extent_buffer_stale(eb);
liuboacce9522011-01-06 19:30:25 +08004353 }
4354 }
4355
4356 return ret;
4357}
4358
4359static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
4360 struct extent_io_tree *pinned_extents)
4361{
Lu Fengqi33f3f3b2018-11-20 11:17:32 +08004362 struct btrfs_fs_info *fs_info = root->fs_info;
liuboacce9522011-01-06 19:30:25 +08004363 struct extent_io_tree *unpin;
4364 u64 start;
4365 u64 end;
4366 int ret;
Liu Boed0eaa12012-06-14 02:23:21 -06004367 bool loop = true;
liuboacce9522011-01-06 19:30:25 +08004368
4369 unpin = pinned_extents;
Liu Boed0eaa12012-06-14 02:23:21 -06004370again:
liuboacce9522011-01-06 19:30:25 +08004371 while (1) {
Lu Fengqi33f3f3b2018-11-20 11:17:32 +08004372 /*
4373 * The btrfs_finish_extent_commit() may get the same range as
4374 * ours between find_first_extent_bit and clear_extent_dirty.
4375 * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
4376 * the same extent range.
4377 */
4378 mutex_lock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004379 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04004380 EXTENT_DIRTY, NULL);
Lu Fengqi33f3f3b2018-11-20 11:17:32 +08004381 if (ret) {
4382 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004383 break;
Lu Fengqi33f3f3b2018-11-20 11:17:32 +08004384 }
liuboacce9522011-01-06 19:30:25 +08004385
David Sterbaaf6f8f62016-04-26 23:54:39 +02004386 clear_extent_dirty(unpin, start, end);
liuboacce9522011-01-06 19:30:25 +08004387 btrfs_error_unpin_extent_range(root, start, end);
Lu Fengqi33f3f3b2018-11-20 11:17:32 +08004388 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004389 cond_resched();
4390 }
4391
Liu Boed0eaa12012-06-14 02:23:21 -06004392 if (loop) {
Lu Fengqi33f3f3b2018-11-20 11:17:32 +08004393 if (unpin == &fs_info->freed_extents[0])
4394 unpin = &fs_info->freed_extents[1];
Liu Boed0eaa12012-06-14 02:23:21 -06004395 else
Lu Fengqi33f3f3b2018-11-20 11:17:32 +08004396 unpin = &fs_info->freed_extents[0];
Liu Boed0eaa12012-06-14 02:23:21 -06004397 loop = false;
4398 goto again;
4399 }
4400
liuboacce9522011-01-06 19:30:25 +08004401 return 0;
4402}
4403
Liu Boc79a1752016-07-20 17:44:12 -07004404static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache)
4405{
4406 struct inode *inode;
4407
4408 inode = cache->io_ctl.inode;
4409 if (inode) {
4410 invalidate_inode_pages2(inode->i_mapping);
4411 BTRFS_I(inode)->generation = 0;
4412 cache->io_ctl.inode = NULL;
4413 iput(inode);
4414 }
4415 btrfs_put_block_group(cache);
4416}
4417
4418void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4419 struct btrfs_root *root)
4420{
4421 struct btrfs_block_group_cache *cache;
4422
4423 spin_lock(&cur_trans->dirty_bgs_lock);
4424 while (!list_empty(&cur_trans->dirty_bgs)) {
4425 cache = list_first_entry(&cur_trans->dirty_bgs,
4426 struct btrfs_block_group_cache,
4427 dirty_list);
4428 if (!cache) {
4429 btrfs_err(root->fs_info,
4430 "orphan block group dirty_bgs list");
4431 spin_unlock(&cur_trans->dirty_bgs_lock);
4432 return;
4433 }
4434
4435 if (!list_empty(&cache->io_list)) {
4436 spin_unlock(&cur_trans->dirty_bgs_lock);
4437 list_del_init(&cache->io_list);
4438 btrfs_cleanup_bg_io(cache);
4439 spin_lock(&cur_trans->dirty_bgs_lock);
4440 }
4441
4442 list_del_init(&cache->dirty_list);
4443 spin_lock(&cache->lock);
4444 cache->disk_cache_state = BTRFS_DC_ERROR;
4445 spin_unlock(&cache->lock);
4446
4447 spin_unlock(&cur_trans->dirty_bgs_lock);
4448 btrfs_put_block_group(cache);
4449 spin_lock(&cur_trans->dirty_bgs_lock);
4450 }
4451 spin_unlock(&cur_trans->dirty_bgs_lock);
4452
4453 while (!list_empty(&cur_trans->io_bgs)) {
4454 cache = list_first_entry(&cur_trans->io_bgs,
4455 struct btrfs_block_group_cache,
4456 io_list);
4457 if (!cache) {
4458 btrfs_err(root->fs_info,
4459 "orphan block group on io_bgs list");
4460 return;
4461 }
4462
4463 list_del_init(&cache->io_list);
4464 spin_lock(&cache->lock);
4465 cache->disk_cache_state = BTRFS_DC_ERROR;
4466 spin_unlock(&cache->lock);
4467 btrfs_cleanup_bg_io(cache);
4468 }
4469}
4470
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004471void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4472 struct btrfs_root *root)
4473{
Liu Boc79a1752016-07-20 17:44:12 -07004474 btrfs_cleanup_dirty_bgs(cur_trans, root);
4475 ASSERT(list_empty(&cur_trans->dirty_bgs));
4476 ASSERT(list_empty(&cur_trans->io_bgs));
4477
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004478 btrfs_destroy_delayed_refs(cur_trans, root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004479
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004480 cur_trans->state = TRANS_STATE_COMMIT_START;
Josef Bacikd7096fc2012-05-31 15:49:57 -04004481 wake_up(&root->fs_info->transaction_blocked_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004482
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004483 cur_trans->state = TRANS_STATE_UNBLOCKED;
Josef Bacikd7096fc2012-05-31 15:49:57 -04004484 wake_up(&root->fs_info->transaction_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004485
Miao Xie67cde342012-06-14 02:23:22 -06004486 btrfs_destroy_delayed_inodes(root);
4487 btrfs_assert_delayed_root_empty(root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004488
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004489 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages,
4490 EXTENT_DIRTY);
Liu Bo6e841e32012-06-14 02:23:20 -06004491 btrfs_destroy_pinned_extent(root,
4492 root->fs_info->pinned_extents);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004493
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004494 cur_trans->state =TRANS_STATE_COMPLETED;
4495 wake_up(&cur_trans->commit_wait);
4496
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004497 /*
4498 memset(cur_trans, 0, sizeof(*cur_trans));
4499 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
4500 */
4501}
4502
Eric Sandeen48a3b632013-04-25 20:41:01 +00004503static int btrfs_cleanup_transaction(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004504{
4505 struct btrfs_transaction *t;
liuboacce9522011-01-06 19:30:25 +08004506
liuboacce9522011-01-06 19:30:25 +08004507 mutex_lock(&root->fs_info->transaction_kthread_mutex);
4508
Josef Bacika4abeea2011-04-11 17:25:13 -04004509 spin_lock(&root->fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004510 while (!list_empty(&root->fs_info->trans_list)) {
4511 t = list_first_entry(&root->fs_info->trans_list,
4512 struct btrfs_transaction, list);
4513 if (t->state >= TRANS_STATE_COMMIT_START) {
4514 atomic_inc(&t->use_count);
4515 spin_unlock(&root->fs_info->trans_lock);
4516 btrfs_wait_for_commit(root, t->transid);
4517 btrfs_put_transaction(t);
4518 spin_lock(&root->fs_info->trans_lock);
4519 continue;
4520 }
4521 if (t == root->fs_info->running_transaction) {
4522 t->state = TRANS_STATE_COMMIT_DOING;
4523 spin_unlock(&root->fs_info->trans_lock);
4524 /*
4525 * We wait for 0 num_writers since we don't hold a trans
4526 * handle open currently for this transaction.
4527 */
4528 wait_event(t->writer_wait,
4529 atomic_read(&t->num_writers) == 0);
4530 } else {
4531 spin_unlock(&root->fs_info->trans_lock);
4532 }
4533 btrfs_cleanup_one_transaction(t, root);
Josef Bacika4abeea2011-04-11 17:25:13 -04004534
Josef Bacik724e2312013-09-30 11:36:38 -04004535 spin_lock(&root->fs_info->trans_lock);
4536 if (t == root->fs_info->running_transaction)
4537 root->fs_info->running_transaction = NULL;
liuboacce9522011-01-06 19:30:25 +08004538 list_del_init(&t->list);
Josef Bacik724e2312013-09-30 11:36:38 -04004539 spin_unlock(&root->fs_info->trans_lock);
liuboacce9522011-01-06 19:30:25 +08004540
Josef Bacik724e2312013-09-30 11:36:38 -04004541 btrfs_put_transaction(t);
4542 trace_btrfs_transaction_commit(root);
4543 spin_lock(&root->fs_info->trans_lock);
4544 }
4545 spin_unlock(&root->fs_info->trans_lock);
4546 btrfs_destroy_all_ordered_extents(root->fs_info);
4547 btrfs_destroy_delayed_inodes(root);
4548 btrfs_assert_delayed_root_empty(root);
4549 btrfs_destroy_pinned_extent(root, root->fs_info->pinned_extents);
4550 btrfs_destroy_all_delalloc_inodes(root->fs_info);
liuboacce9522011-01-06 19:30:25 +08004551 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
liuboacce9522011-01-06 19:30:25 +08004552
4553 return 0;
4554}
4555
David Sterbae8c9f182015-01-02 18:23:10 +01004556static const struct extent_io_ops btree_extent_io_ops = {
Chris Masonce9adaa2008-04-09 16:28:12 -04004557 .readpage_end_io_hook = btree_readpage_end_io_hook,
Arne Jansen4bb31e92011-06-10 13:55:54 +02004558 .readpage_io_failed_hook = btree_io_failed_hook,
Chris Mason0b86a832008-03-24 15:01:56 -04004559 .submit_bio_hook = btree_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004560 /* note we're sharing with inode.c for the merge bio hook */
4561 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason0da54682007-11-07 21:08:01 -05004562};