blob: a6f5441e62d103094384f48d4e225e4fd5f28785 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Masone20d96d2007-03-22 12:13:20 -04006#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -04007#include <linux/blkdev.h>
David Sterbafc7cbcd2022-07-15 13:59:21 +02008#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -04009#include <linux/writeback.h>
Chris Masonce9adaa2008-04-09 16:28:12 -040010#include <linux/workqueue.h>
Chris Masona74a4b92008-06-25 16:01:31 -040011#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Chris Mason784b4e22010-11-21 22:20:49 -050013#include <linux/migrate.h>
David Sterba7a36dde2011-05-06 15:33:15 +020014#include <linux/ratelimit.h>
Stefan Behrens6463fe52013-04-19 15:08:05 +000015#include <linux/uuid.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020016#include <linux/semaphore.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090017#include <linux/error-injection.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020018#include <linux/crc32c.h>
Filipe Mananab89f6d1f2018-12-13 21:16:45 +000019#include <linux/sched/mm.h>
David Sterba7e75bf32011-03-18 22:56:43 +000020#include <asm/unaligned.h>
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +020021#include <crypto/hash.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040024#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040025#include "btrfs_inode.h"
Christoph Hellwig103c1972022-11-15 10:44:05 +010026#include "bio.h"
Chris Masondb945352007-10-15 16:15:53 -040027#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040028#include "locking.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "tree-log.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040030#include "free-space-cache.h"
Omar Sandoval70f6d822015-09-29 20:50:38 -070031#include "free-space-tree.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010032#include "dev-replace.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050033#include "raid56.h"
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040034#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070035#include "qgroup.h"
Anand Jainebb87652016-03-10 17:26:59 +080036#include "compression.h"
Qu Wenruo557ea5d2017-10-09 01:51:02 +000037#include "tree-checker.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040038#include "ref-verify.h"
Josef Bacikaac00232019-06-20 15:37:44 -040039#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080040#include "discard.h"
Nikolay Borisovf603bb92020-01-20 16:09:08 +020041#include "space-info.h"
Naohiro Aotab70f5092020-11-10 20:26:08 +090042#include "zoned.h"
Qu Wenruo139e8cd2021-03-25 15:14:39 +080043#include "subpage.h"
Josef Bacikc7f13d42022-10-19 10:50:47 -040044#include "fs.h"
Josef Bacik07e81dc2022-10-19 10:51:00 -040045#include "accessors.h"
Josef Bacika0231802022-10-24 14:46:57 -040046#include "extent-tree.h"
Josef Bacik45c40c82022-10-24 14:47:00 -040047#include "root-tree.h"
Josef Bacik59b818e2022-10-26 15:08:25 -040048#include "defrag.h"
Josef Bacikc7a03b52022-10-26 15:08:28 -040049#include "uuid-tree.h"
Josef Bacik67707472022-10-26 15:08:34 -040050#include "relocation.h"
Josef Bacik2fc68222022-10-26 15:08:35 -040051#include "scrub.h"
Josef Bacikc03b2202022-10-26 15:08:39 -040052#include "super.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050053
Qu Wenruo319e4d02015-12-15 09:14:36 +080054#define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
55 BTRFS_HEADER_FLAG_RELOC |\
56 BTRFS_SUPER_FLAG_ERROR |\
57 BTRFS_SUPER_FLAG_SEEDING |\
Anand Jaine2731e52018-01-09 09:05:41 +080058 BTRFS_SUPER_FLAG_METADUMP |\
59 BTRFS_SUPER_FLAG_METADUMP_V2)
Qu Wenruo319e4d02015-12-15 09:14:36 +080060
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040061static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
62static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
Chris Masonce9adaa2008-04-09 16:28:12 -040063
Josef Bacik141386e2020-01-24 09:32:57 -050064static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
65{
66 if (fs_info->csum_shash)
67 crypto_free_shash(fs_info->csum_shash);
68}
69
Chris Masond352ac62008-09-29 15:18:18 -040070/*
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +010071 * Compute the csum of a btree block and store the result to provided buffer.
Chris Masond352ac62008-09-29 15:18:18 -040072 */
David Sterbac67b3892020-02-27 21:00:49 +010073static void csum_tree_block(struct extent_buffer *buf, u8 *result)
Chris Mason19c00dd2007-10-15 16:19:22 -040074{
Johannes Thumshirnd5178572019-06-03 16:58:57 +020075 struct btrfs_fs_info *fs_info = buf->fs_info;
Qu Wenruo397239e2023-11-16 15:49:06 +103076 int num_pages;
77 u32 first_page_part;
Johannes Thumshirnd5178572019-06-03 16:58:57 +020078 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Chris Mason19c00dd2007-10-15 16:19:22 -040079 char *kaddr;
David Sterbae9be5a32020-02-27 21:00:47 +010080 int i;
Johannes Thumshirnd5178572019-06-03 16:58:57 +020081
82 shash->tfm = fs_info->csum_shash;
83 crypto_shash_init(shash);
Qu Wenruo397239e2023-11-16 15:49:06 +103084
85 if (buf->addr) {
86 /* Pages are contiguous, handle them as a big one. */
87 kaddr = buf->addr;
88 first_page_part = fs_info->nodesize;
89 num_pages = 1;
90 } else {
Qu Wenruo082d5bb2023-12-07 09:39:27 +103091 kaddr = folio_address(buf->folios[0]);
Qu Wenruo397239e2023-11-16 15:49:06 +103092 first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
93 num_pages = num_extent_pages(buf);
94 }
95
David Sterbae9be5a32020-02-27 21:00:47 +010096 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
Qu Wenruoa26663e2020-11-03 21:30:47 +080097 first_page_part - BTRFS_CSUM_SIZE);
Chris Mason19c00dd2007-10-15 16:19:22 -040098
Qu Wenruo13df3772023-12-07 09:39:28 +103099 /*
100 * Multiple single-page folios case would reach here.
101 *
102 * nodesize <= PAGE_SIZE and large folio all handled by above
103 * crypto_shash_update() already.
104 */
pengfuyuan5ad9b472023-05-23 15:09:55 +0800105 for (i = 1; i < num_pages && INLINE_EXTENT_BUFFER_PAGES > 1; i++) {
Qu Wenruo082d5bb2023-12-07 09:39:27 +1030106 kaddr = folio_address(buf->folios[i]);
David Sterbae9be5a32020-02-27 21:00:47 +0100107 crypto_shash_update(shash, kaddr, PAGE_SIZE);
Chris Mason19c00dd2007-10-15 16:19:22 -0400108 }
David Sterba71a63552017-11-06 19:23:00 +0100109 memset(result, 0, BTRFS_CSUM_SIZE);
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200110 crypto_shash_final(shash, result);
Chris Mason19c00dd2007-10-15 16:19:22 -0400111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/*
114 * we can't consider a given block up to date unless the transid of the
115 * block matches the transid in the parent node's pointer. This is how we
116 * detect blocks that either didn't get written at all or got written
117 * in the wrong place.
118 */
Christoph Hellwigd87e6572023-05-03 17:24:24 +0200119int btrfs_buffer_uptodate(struct extent_buffer *eb, u64 parent_transid, int atomic)
Chris Mason1259ab72008-05-12 13:39:03 -0400120{
Christoph Hellwigd87e6572023-05-03 17:24:24 +0200121 if (!extent_buffer_uptodate(eb))
122 return 0;
Chris Mason1259ab72008-05-12 13:39:03 -0400123
124 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
Christoph Hellwigd87e6572023-05-03 17:24:24 +0200125 return 1;
Chris Mason1259ab72008-05-12 13:39:03 -0400126
Chris Masonb9fab912012-05-06 07:23:47 -0400127 if (atomic)
128 return -EAGAIN;
129
Christoph Hellwigd87e6572023-05-03 17:24:24 +0200130 if (!extent_buffer_uptodate(eb) ||
131 btrfs_header_generation(eb) != parent_transid) {
132 btrfs_err_rl(eb->fs_info,
Qu Wenruo8f0ed7d2022-06-19 21:47:56 +0800133"parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
134 eb->start, eb->read_mirror,
Wang Shilong29549ae2014-07-04 17:59:06 +0800135 parent_transid, btrfs_header_generation(eb));
Christoph Hellwigd87e6572023-05-03 17:24:24 +0200136 clear_extent_buffer_uptodate(eb);
Christoph Hellwig9e2aff92023-05-03 17:24:39 +0200137 return 0;
Christoph Hellwigd87e6572023-05-03 17:24:24 +0200138 }
Christoph Hellwig9e2aff92023-05-03 17:24:39 +0200139 return 1;
Chris Mason1259ab72008-05-12 13:39:03 -0400140}
141
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200142static bool btrfs_supported_super_csum(u16 csum_type)
143{
144 switch (csum_type) {
145 case BTRFS_CSUM_TYPE_CRC32:
Johannes Thumshirn3951e7f2019-10-07 11:11:01 +0200146 case BTRFS_CSUM_TYPE_XXHASH:
Johannes Thumshirn3831bf02019-10-07 11:11:02 +0200147 case BTRFS_CSUM_TYPE_SHA256:
David Sterba352ae072019-10-07 11:11:02 +0200148 case BTRFS_CSUM_TYPE_BLAKE2:
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200149 return true;
150 default:
151 return false;
152 }
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
David Sterba1104a882013-03-06 15:57:46 +0100156 * Return 0 if the superblock checksum type matches the checksum value of that
157 * algorithm. Pass the raw disk superblock data.
158 */
Qu Wenruo3d17ade2022-10-18 09:56:38 +0800159int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
160 const struct btrfs_super_block *disk_sb)
David Sterba1104a882013-03-06 15:57:46 +0100161{
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +0200162 char result[BTRFS_CSUM_SIZE];
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200163 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
164
165 shash->tfm = fs_info->csum_shash;
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200166
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +0200167 /*
168 * The super_block structure does not span the whole
169 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
170 * filled with zeros and is included in the checksum.
171 */
Qu Wenruo3d17ade2022-10-18 09:56:38 +0800172 crypto_shash_digest(shash, (const u8 *)disk_sb + BTRFS_CSUM_SIZE,
Eric Biggersfd080012020-04-30 23:51:59 -0700173 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +0200174
David Sterba55fc29b2020-06-30 02:01:31 +0200175 if (memcmp(disk_sb->csum, result, fs_info->csum_size))
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200176 return 1;
David Sterba1104a882013-03-06 15:57:46 +0100177
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200178 return 0;
David Sterba1104a882013-03-06 15:57:46 +0100179}
180
Christoph Hellwigbacf60e2022-11-15 10:44:06 +0100181static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb,
182 int mirror_num)
183{
184 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo96c36ea2023-12-12 15:54:10 +1030185 int num_folios = num_extent_folios(eb);
Christoph Hellwigbacf60e2022-11-15 10:44:06 +0100186 int ret = 0;
187
188 if (sb_rdonly(fs_info->sb))
189 return -EROFS;
190
Qu Wenruo96c36ea2023-12-12 15:54:10 +1030191 for (int i = 0; i < num_folios; i++) {
192 struct folio *folio = eb->folios[i];
193 u64 start = max_t(u64, eb->start, folio_pos(folio));
Qu Wenruo082d5bb2023-12-07 09:39:27 +1030194 u64 end = min_t(u64, eb->start + eb->len,
Qu Wenruo84cda1a2024-01-05 16:05:55 +1030195 folio_pos(folio) + eb->folio_size);
Qu Wenruo917ac772023-05-26 20:30:20 +0800196 u32 len = end - start;
Christoph Hellwigbacf60e2022-11-15 10:44:06 +0100197
Qu Wenruo917ac772023-05-26 20:30:20 +0800198 ret = btrfs_repair_io_failure(fs_info, 0, start, len,
Qu Wenruo96c36ea2023-12-12 15:54:10 +1030199 start, folio, offset_in_folio(folio, start),
200 mirror_num);
Christoph Hellwigbacf60e2022-11-15 10:44:06 +0100201 if (ret)
202 break;
Christoph Hellwigbacf60e2022-11-15 10:44:06 +0100203 }
204
205 return ret;
206}
207
David Sterba1104a882013-03-06 15:57:46 +0100208/*
Chris Masond352ac62008-09-29 15:18:18 -0400209 * helper to read a given tree block, doing retries as required when
210 * the checksums don't match and we have alternate mirrors to try.
Qu Wenruo581c1762018-03-29 09:08:11 +0800211 *
Qu Wenruo789d6a32022-09-14 13:32:50 +0800212 * @check: expected tree parentness check, see the comments of the
213 * structure for details.
Chris Masond352ac62008-09-29 15:18:18 -0400214 */
Filipe Manana6a2e9dc2022-03-11 11:35:34 +0000215int btrfs_read_extent_buffer(struct extent_buffer *eb,
David Sterba2917f742024-05-30 19:14:12 +0200216 const struct btrfs_tree_parent_check *check)
Chris Masonf1885912008-04-09 16:28:12 -0400217{
David Sterba5ab12d12019-03-20 14:56:39 +0100218 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacikea466792012-03-26 21:57:36 -0400219 int failed = 0;
Chris Masonf1885912008-04-09 16:28:12 -0400220 int ret;
221 int num_copies = 0;
222 int mirror_num = 0;
Josef Bacikea466792012-03-26 21:57:36 -0400223 int failed_mirror = 0;
Chris Masonf1885912008-04-09 16:28:12 -0400224
Qu Wenruo789d6a32022-09-14 13:32:50 +0800225 ASSERT(check);
226
Chris Masonf1885912008-04-09 16:28:12 -0400227 while (1) {
Nikolay Borisovf8397d62018-11-06 16:40:20 +0200228 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
Qu Wenruo947a6292022-09-14 13:32:51 +0800229 ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num, check);
230 if (!ret)
231 break;
Chris Masond3977122009-01-05 21:25:51 -0500232
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400233 num_copies = btrfs_num_copies(fs_info,
Chris Masonf1885912008-04-09 16:28:12 -0400234 eb->start, eb->len);
Chris Mason42352982008-04-28 16:40:52 -0400235 if (num_copies == 1)
Josef Bacikea466792012-03-26 21:57:36 -0400236 break;
Chris Mason42352982008-04-28 16:40:52 -0400237
Josef Bacik5cf1ab52012-04-16 09:42:26 -0400238 if (!failed_mirror) {
239 failed = 1;
240 failed_mirror = eb->read_mirror;
241 }
242
Chris Masonf1885912008-04-09 16:28:12 -0400243 mirror_num++;
Josef Bacikea466792012-03-26 21:57:36 -0400244 if (mirror_num == failed_mirror)
245 mirror_num++;
246
Chris Mason42352982008-04-28 16:40:52 -0400247 if (mirror_num > num_copies)
Josef Bacikea466792012-03-26 21:57:36 -0400248 break;
Chris Masonf1885912008-04-09 16:28:12 -0400249 }
Josef Bacikea466792012-03-26 21:57:36 -0400250
Stefan Behrensc0901582012-07-10 07:30:17 -0600251 if (failed && !ret && failed_mirror)
David Sterba20a1fbf92019-03-20 11:23:44 +0100252 btrfs_repair_eb_io_failure(eb, failed_mirror);
Josef Bacikea466792012-03-26 21:57:36 -0400253
254 return ret;
Chris Masonf1885912008-04-09 16:28:12 -0400255}
Chris Mason19c00dd2007-10-15 16:19:22 -0400256
Christoph Hellwig31d89392023-05-03 17:24:35 +0200257/*
258 * Checksum a dirty tree block before IO.
259 */
260blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
Qu Wenruoeca0f6f2021-03-25 15:14:40 +0800261{
Christoph Hellwig31d89392023-05-03 17:24:35 +0200262 struct extent_buffer *eb = bbio->private;
Qu Wenruoeca0f6f2021-03-25 15:14:40 +0800263 struct btrfs_fs_info *fs_info = eb->fs_info;
Christoph Hellwig31d89392023-05-03 17:24:35 +0200264 u64 found_start = btrfs_header_bytenr(eb);
Filipe Manana01248552023-10-04 11:38:51 +0100265 u64 last_trans;
Qu Wenruoeca0f6f2021-03-25 15:14:40 +0800266 u8 result[BTRFS_CSUM_SIZE];
267 int ret;
268
Christoph Hellwig31d89392023-05-03 17:24:35 +0200269 /* Btree blocks are always contiguous on disk. */
270 if (WARN_ON_ONCE(bbio->file_offset != eb->start))
271 return BLK_STS_IOERR;
272 if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))
273 return BLK_STS_IOERR;
274
Johannes Thumshirnaa6313e2023-11-23 07:47:16 -0800275 /*
276 * If an extent_buffer is marked as EXTENT_BUFFER_ZONED_ZEROOUT, don't
277 * checksum it but zero-out its content. This is done to preserve
278 * ordering of I/O without unnecessarily writing out data.
279 */
Johannes Thumshirncbf44cd2023-11-23 07:47:15 -0800280 if (test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags)) {
Johannes Thumshirnaa6313e2023-11-23 07:47:16 -0800281 memzero_extent_buffer(eb, 0, eb->len);
Christoph Hellwig31d89392023-05-03 17:24:35 +0200282 return BLK_STS_OK;
283 }
284
285 if (WARN_ON_ONCE(found_start != eb->start))
286 return BLK_STS_IOERR;
Qu Wenruo55151ea2023-12-12 12:58:37 +1030287 if (WARN_ON(!btrfs_folio_test_uptodate(fs_info, eb->folios[0],
288 eb->start, eb->len)))
Christoph Hellwig31d89392023-05-03 17:24:35 +0200289 return BLK_STS_IOERR;
290
Qu Wenruoeca0f6f2021-03-25 15:14:40 +0800291 ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
292 offsetof(struct btrfs_header, fsid),
293 BTRFS_FSID_SIZE) == 0);
294 csum_tree_block(eb, result);
295
296 if (btrfs_header_level(eb))
297 ret = btrfs_check_node(eb);
298 else
Josef Bacik85d8a822023-04-29 16:07:12 -0400299 ret = btrfs_check_leaf(eb);
Qu Wenruoeca0f6f2021-03-25 15:14:40 +0800300
Qu Wenruo37773692022-03-02 09:10:21 +0800301 if (ret < 0)
302 goto error;
303
304 /*
305 * Also check the generation, the eb reached here must be newer than
306 * last committed. Or something seriously wrong happened.
307 */
Filipe Manana01248552023-10-04 11:38:51 +0100308 last_trans = btrfs_get_last_trans_committed(fs_info);
309 if (unlikely(btrfs_header_generation(eb) <= last_trans)) {
Qu Wenruo37773692022-03-02 09:10:21 +0800310 ret = -EUCLEAN;
Qu Wenruoeca0f6f2021-03-25 15:14:40 +0800311 btrfs_err(fs_info,
Qu Wenruo37773692022-03-02 09:10:21 +0800312 "block=%llu bad generation, have %llu expect > %llu",
Filipe Manana01248552023-10-04 11:38:51 +0100313 eb->start, btrfs_header_generation(eb), last_trans);
Qu Wenruo37773692022-03-02 09:10:21 +0800314 goto error;
Qu Wenruoeca0f6f2021-03-25 15:14:40 +0800315 }
316 write_extent_buffer(eb, result, 0, fs_info->csum_size);
Christoph Hellwig31d89392023-05-03 17:24:35 +0200317 return BLK_STS_OK;
Qu Wenruo37773692022-03-02 09:10:21 +0800318
319error:
320 btrfs_print_tree(eb, 0);
321 btrfs_err(fs_info, "block=%llu write time tree block corruption detected",
322 eb->start);
Filipe Manana16199ad2023-01-10 14:56:37 +0000323 /*
324 * Be noisy if this is an extent buffer from a log tree. We don't abort
325 * a transaction in case there's a bad log tree extent buffer, we just
326 * fallback to a transaction commit. Still we want to know when there is
327 * a bad log tree extent buffer, as that may signal a bug somewhere.
328 */
329 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG) ||
330 btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID);
Christoph Hellwigdeb62162023-01-21 07:50:15 +0100331 return errno_to_blk_status(ret);
332}
333
Anand Jain413fb1b2023-05-24 20:02:39 +0800334static bool check_tree_block_fsid(struct extent_buffer *eb)
Yan Zheng2b820322008-11-17 21:11:30 -0500335{
David Sterbab0c9b3b2019-03-20 13:12:00 +0100336 struct btrfs_fs_info *fs_info = eb->fs_info;
Nikolay Borisov944d3f92020-07-16 10:25:33 +0300337 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
Anand Jain44880fd2017-07-29 17:50:09 +0800338 u8 fsid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -0500339
David Sterba9a8658e2019-03-20 13:15:57 +0100340 read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid),
341 BTRFS_FSID_SIZE);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200342
Anand Jain67bc5ad2023-07-31 19:16:36 +0800343 /*
Anand Jainf7361d82023-08-23 22:52:13 +0800344 * alloc_fsid_devices() copies the fsid into fs_devices::metadata_uuid.
345 * This is then overwritten by metadata_uuid if it is present in the
346 * device_list_add(). The same true for a seed device as well. So use of
347 * fs_devices::metadata_uuid is appropriate here.
Anand Jain67bc5ad2023-07-31 19:16:36 +0800348 */
349 if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)
Anand Jain413fb1b2023-05-24 20:02:39 +0800350 return false;
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200351
Nikolay Borisov944d3f92020-07-16 10:25:33 +0300352 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list)
353 if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE))
Anand Jain413fb1b2023-05-24 20:02:39 +0800354 return false;
Nikolay Borisov944d3f92020-07-16 10:25:33 +0300355
Anand Jain413fb1b2023-05-24 20:02:39 +0800356 return true;
Yan Zheng2b820322008-11-17 21:11:30 -0500357}
358
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800359/* Do basic extent buffer checks at read time */
Christoph Hellwig046b5622023-05-03 17:24:28 +0200360int btrfs_validate_extent_buffer(struct extent_buffer *eb,
David Sterba2917f742024-05-30 19:14:12 +0200361 const struct btrfs_tree_parent_check *check)
Chris Masonce9adaa2008-04-09 16:28:12 -0400362{
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800363 struct btrfs_fs_info *fs_info = eb->fs_info;
Chris Masonce9adaa2008-04-09 16:28:12 -0400364 u64 found_start;
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800365 const u32 csum_size = fs_info->csum_size;
366 u8 found_level;
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100367 u8 result[BTRFS_CSUM_SIZE];
David Sterbadfd29ee2020-09-21 22:07:14 +0200368 const u8 *header_csum;
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800369 int ret = 0;
Qu Wenruo169aaaf2024-06-14 13:52:30 +0930370 const bool ignore_csum = btrfs_test_opt(fs_info, IGNOREMETACSUMS);
Josef Bacikea466792012-03-26 21:57:36 -0400371
Qu Wenruo947a6292022-09-14 13:32:51 +0800372 ASSERT(check);
373
Chris Masonce9adaa2008-04-09 16:28:12 -0400374 found_start = btrfs_header_bytenr(eb);
Chris Mason727011e2010-08-06 13:21:20 -0400375 if (found_start != eb->start) {
Qu Wenruo8f0ed7d2022-06-19 21:47:56 +0800376 btrfs_err_rl(fs_info,
377 "bad tree block start, mirror %u want %llu have %llu",
378 eb->read_mirror, eb->start, found_start);
Chris Masonf1885912008-04-09 16:28:12 -0400379 ret = -EIO;
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800380 goto out;
Chris Masonce9adaa2008-04-09 16:28:12 -0400381 }
David Sterbab0c9b3b2019-03-20 13:12:00 +0100382 if (check_tree_block_fsid(eb)) {
Qu Wenruo8f0ed7d2022-06-19 21:47:56 +0800383 btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u",
384 eb->start, eb->read_mirror);
Chris Mason1259ab72008-05-12 13:39:03 -0400385 ret = -EIO;
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800386 goto out;
Chris Mason1259ab72008-05-12 13:39:03 -0400387 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400388 found_level = btrfs_header_level(eb);
Josef Bacik1c24c3c2013-04-23 11:30:14 -0400389 if (found_level >= BTRFS_MAX_LEVEL) {
Qu Wenruo8f0ed7d2022-06-19 21:47:56 +0800390 btrfs_err(fs_info,
391 "bad tree block level, mirror %u level %d on logical %llu",
392 eb->read_mirror, btrfs_header_level(eb), eb->start);
Josef Bacik1c24c3c2013-04-23 11:30:14 -0400393 ret = -EIO;
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800394 goto out;
Josef Bacik1c24c3c2013-04-23 11:30:14 -0400395 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400396
David Sterbac67b3892020-02-27 21:00:49 +0100397 csum_tree_block(eb, result);
Qu Wenruo082d5bb2023-12-07 09:39:27 +1030398 header_csum = folio_address(eb->folios[0]) +
Qu Wenruo8d993612023-12-12 12:58:36 +1030399 get_eb_offset_in_folio(eb, offsetof(struct btrfs_header, csum));
Josef Bacika826d6d2011-03-16 13:42:43 -0400400
David Sterbadfd29ee2020-09-21 22:07:14 +0200401 if (memcmp(result, header_csum, csum_size) != 0) {
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100402 btrfs_warn_rl(fs_info,
Qu Wenruo169aaaf2024-06-14 13:52:30 +0930403"checksum verify failed on logical %llu mirror %u wanted " CSUM_FMT " found " CSUM_FMT " level %d%s",
Qu Wenruo8f0ed7d2022-06-19 21:47:56 +0800404 eb->start, eb->read_mirror,
David Sterbadfd29ee2020-09-21 22:07:14 +0200405 CSUM_FMT_VALUE(csum_size, header_csum),
Johannes Thumshirn35be8852020-09-21 16:57:14 +0900406 CSUM_FMT_VALUE(csum_size, result),
Qu Wenruo169aaaf2024-06-14 13:52:30 +0930407 btrfs_header_level(eb),
408 ignore_csum ? ", ignored" : "");
409 if (!ignore_csum) {
410 ret = -EUCLEAN;
411 goto out;
412 }
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100413 }
414
Qu Wenruo947a6292022-09-14 13:32:51 +0800415 if (found_level != check->level) {
Qu Wenruo77177ed2022-12-29 07:32:23 +0800416 btrfs_err(fs_info,
417 "level verify failed on logical %llu mirror %u wanted %u found %u",
418 eb->start, eb->read_mirror, check->level, found_level);
Qu Wenruo947a6292022-09-14 13:32:51 +0800419 ret = -EIO;
420 goto out;
421 }
422 if (unlikely(check->transid &&
423 btrfs_header_generation(eb) != check->transid)) {
424 btrfs_err_rl(eb->fs_info,
425"parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
426 eb->start, eb->read_mirror, check->transid,
427 btrfs_header_generation(eb));
428 ret = -EIO;
429 goto out;
430 }
431 if (check->has_first_key) {
David Sterba2917f742024-05-30 19:14:12 +0200432 const struct btrfs_key *expect_key = &check->first_key;
Qu Wenruo947a6292022-09-14 13:32:51 +0800433 struct btrfs_key found_key;
434
435 if (found_level)
436 btrfs_node_key_to_cpu(eb, &found_key, 0);
437 else
438 btrfs_item_key_to_cpu(eb, &found_key, 0);
439 if (unlikely(btrfs_comp_cpu_keys(expect_key, &found_key))) {
440 btrfs_err(fs_info,
441"tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
442 eb->start, check->transid,
443 expect_key->objectid,
444 expect_key->type, expect_key->offset,
445 found_key.objectid, found_key.type,
446 found_key.offset);
447 ret = -EUCLEAN;
448 goto out;
449 }
450 }
451 if (check->owner_root) {
452 ret = btrfs_check_eb_owner(eb, check->owner_root);
453 if (ret < 0)
454 goto out;
455 }
456
Josef Bacika826d6d2011-03-16 13:42:43 -0400457 /*
458 * If this is a leaf block and it is corrupt, set the corrupt bit so
459 * that we don't try and read the other copies of this block, just
460 * return -EIO.
461 */
Josef Bacik85d8a822023-04-29 16:07:12 -0400462 if (found_level == 0 && btrfs_check_leaf(eb)) {
Josef Bacika826d6d2011-03-16 13:42:43 -0400463 set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
464 ret = -EIO;
465 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400466
David Sterba813fd1d2019-03-20 16:25:00 +0100467 if (found_level > 0 && btrfs_check_node(eb))
Liu Bo053ab70f2016-08-23 17:37:45 -0700468 ret = -EIO;
469
Christoph Hellwigaebcc152023-05-03 17:24:23 +0200470 if (ret)
Qu Wenruo75391f02019-03-20 14:27:40 +0800471 btrfs_err(fs_info,
Qu Wenruo8f0ed7d2022-06-19 21:47:56 +0800472 "read time tree block corruption detected on logical %llu mirror %u",
473 eb->start, eb->read_mirror);
Qu Wenruo77bf40a2020-11-03 21:30:48 +0800474out:
475 return ret;
476}
477
Jan Beulich3dd14622010-12-07 14:54:09 +0000478#ifdef CONFIG_MIGRATION
Matthew Wilcox (Oracle)8958b552022-06-06 09:22:19 -0400479static int btree_migrate_folio(struct address_space *mapping,
480 struct folio *dst, struct folio *src, enum migrate_mode mode)
Chris Mason784b4e22010-11-21 22:20:49 -0500481{
482 /*
483 * we can't safely write a btree page from here,
484 * we haven't done the locking hook
485 */
Matthew Wilcox (Oracle)8958b552022-06-06 09:22:19 -0400486 if (folio_test_dirty(src))
Chris Mason784b4e22010-11-21 22:20:49 -0500487 return -EAGAIN;
488 /*
489 * Buffers may be managed in a filesystem specific way.
490 * We must have no buffers or drop them.
491 */
Matthew Wilcox (Oracle)8958b552022-06-06 09:22:19 -0400492 if (folio_get_private(src) &&
493 !filemap_release_folio(src, GFP_KERNEL))
Chris Mason784b4e22010-11-21 22:20:49 -0500494 return -EAGAIN;
Matthew Wilcox (Oracle)54184652022-06-06 10:27:41 -0400495 return migrate_folio(mapping, dst, src, mode);
Chris Mason784b4e22010-11-21 22:20:49 -0500496}
Matthew Wilcox (Oracle)8958b552022-06-06 09:22:19 -0400497#else
498#define btree_migrate_folio NULL
Jan Beulich3dd14622010-12-07 14:54:09 +0000499#endif
Chris Mason784b4e22010-11-21 22:20:49 -0500500
Chris Mason0da54682007-11-07 21:08:01 -0500501static int btree_writepages(struct address_space *mapping,
502 struct writeback_control *wbc)
503{
Miao Xiee2d84522013-01-29 10:09:20 +0000504 int ret;
505
Chris Masond8d5f3e2007-12-11 12:42:00 -0500506 if (wbc->sync_mode == WB_SYNC_NONE) {
David Sterba41044b42023-09-14 16:45:41 +0200507 struct btrfs_fs_info *fs_info;
Chris Mason448d6402007-11-27 07:52:01 -0800508
509 if (wbc->for_kupdate)
510 return 0;
511
David Sterba41044b42023-09-14 16:45:41 +0200512 fs_info = inode_to_fs_info(mapping->host);
Chris Masonb9473432009-03-13 11:00:37 -0400513 /* this is a bit racy, but that's ok */
Ethan Liend814a492018-07-02 15:44:58 +0800514 ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
515 BTRFS_DIRTY_METADATA_THRESH,
516 fs_info->dirty_metadata_batch);
Miao Xiee2d84522013-01-29 10:09:20 +0000517 if (ret < 0)
Chris Mason793955b2007-11-26 16:34:41 -0800518 return 0;
Chris Mason793955b2007-11-26 16:34:41 -0800519 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400520 return btree_write_cache_pages(mapping, wbc);
Chris Mason0da54682007-11-07 21:08:01 -0500521}
522
Matthew Wilcox (Oracle)f913cff2022-04-30 23:15:16 -0400523static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400524{
Matthew Wilcox (Oracle)f913cff2022-04-30 23:15:16 -0400525 if (folio_test_writeback(folio) || folio_test_dirty(folio))
526 return false;
David Sterba0c4e5382012-01-26 15:01:12 -0500527
Matthew Wilcox (Oracle)f913cff2022-04-30 23:15:16 -0400528 return try_release_extent_buffer(&folio->page);
Chris Masone20d96d2007-03-22 12:13:20 -0400529}
Chris Mason123abc82007-03-14 14:14:43 -0400530
Matthew Wilcox (Oracle)895586e2022-02-09 20:21:39 +0000531static void btree_invalidate_folio(struct folio *folio, size_t offset,
532 size_t length)
Chris Masond98237b2007-03-28 13:57:48 -0400533{
Chris Masond1310b22008-01-24 16:13:08 -0500534 struct extent_io_tree *tree;
David Sterbac8293892023-09-13 16:11:29 +0200535
536 tree = &folio_to_inode(folio)->io_tree;
Matthew Wilcox (Oracle)895586e2022-02-09 20:21:39 +0000537 extent_invalidate_folio(tree, folio, offset);
Matthew Wilcox (Oracle)f913cff2022-04-30 23:15:16 -0400538 btree_release_folio(folio, GFP_NOFS);
Matthew Wilcox (Oracle)895586e2022-02-09 20:21:39 +0000539 if (folio_get_private(folio)) {
David Sterbab33d2e52023-09-14 16:24:43 +0200540 btrfs_warn(folio_to_fs_info(folio),
Matthew Wilcox (Oracle)895586e2022-02-09 20:21:39 +0000541 "folio private not zero on folio %llu",
542 (unsigned long long)folio_pos(folio));
543 folio_detach_private(folio);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -0400544 }
Chris Masond98237b2007-03-28 13:57:48 -0400545}
546
Josef Bacikbb146eb2012-10-15 13:30:43 -0400547#ifdef DEBUG
Matthew Wilcox (Oracle)0079c3b2022-02-09 20:22:02 +0000548static bool btree_dirty_folio(struct address_space *mapping,
549 struct folio *folio)
550{
David Sterba41044b42023-09-14 16:45:41 +0200551 struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
Qu Wenruo5e0e8792023-08-22 13:50:51 +0800552 struct btrfs_subpage_info *spi = fs_info->subpage_info;
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800553 struct btrfs_subpage *subpage;
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400554 struct extent_buffer *eb;
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800555 int cur_bit = 0;
Matthew Wilcox (Oracle)0079c3b2022-02-09 20:22:02 +0000556 u64 page_start = folio_pos(folio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400557
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800558 if (fs_info->sectorsize == PAGE_SIZE) {
Matthew Wilcox (Oracle)0079c3b2022-02-09 20:22:02 +0000559 eb = folio_get_private(folio);
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800560 BUG_ON(!eb);
561 BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
562 BUG_ON(!atomic_read(&eb->refs));
Filipe Manana49d0c642021-09-22 10:36:45 +0100563 btrfs_assert_tree_write_locked(eb);
Matthew Wilcox (Oracle)0079c3b2022-02-09 20:22:02 +0000564 return filemap_dirty_folio(mapping, folio);
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800565 }
Qu Wenruo5e0e8792023-08-22 13:50:51 +0800566
567 ASSERT(spi);
Matthew Wilcox (Oracle)0079c3b2022-02-09 20:22:02 +0000568 subpage = folio_get_private(folio);
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800569
Qu Wenruo5e0e8792023-08-22 13:50:51 +0800570 for (cur_bit = spi->dirty_offset;
571 cur_bit < spi->dirty_offset + spi->bitmap_nr_bits;
572 cur_bit++) {
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800573 unsigned long flags;
574 u64 cur;
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800575
576 spin_lock_irqsave(&subpage->lock, flags);
Qu Wenruo5e0e8792023-08-22 13:50:51 +0800577 if (!test_bit(cur_bit, subpage->bitmaps)) {
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800578 spin_unlock_irqrestore(&subpage->lock, flags);
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800579 continue;
580 }
581 spin_unlock_irqrestore(&subpage->lock, flags);
582 cur = page_start + cur_bit * fs_info->sectorsize;
583
584 eb = find_extent_buffer(fs_info, cur);
585 ASSERT(eb);
586 ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
587 ASSERT(atomic_read(&eb->refs));
Filipe Manana49d0c642021-09-22 10:36:45 +0100588 btrfs_assert_tree_write_locked(eb);
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800589 free_extent_buffer(eb);
590
Qu Wenruo5e0e8792023-08-22 13:50:51 +0800591 cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits) - 1;
Qu Wenruo139e8cd2021-03-25 15:14:39 +0800592 }
Matthew Wilcox (Oracle)0079c3b2022-02-09 20:22:02 +0000593 return filemap_dirty_folio(mapping, folio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400594}
Matthew Wilcox (Oracle)0079c3b2022-02-09 20:22:02 +0000595#else
596#define btree_dirty_folio filemap_dirty_folio
597#endif
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400598
Alexey Dobriyan7f094102009-09-21 17:01:10 -0700599static const struct address_space_operations btree_aops = {
Chris Mason0da54682007-11-07 21:08:01 -0500600 .writepages = btree_writepages,
Matthew Wilcox (Oracle)f913cff2022-04-30 23:15:16 -0400601 .release_folio = btree_release_folio,
Matthew Wilcox (Oracle)895586e2022-02-09 20:21:39 +0000602 .invalidate_folio = btree_invalidate_folio,
Matthew Wilcox (Oracle)8958b552022-06-06 09:22:19 -0400603 .migrate_folio = btree_migrate_folio,
604 .dirty_folio = btree_dirty_folio,
Chris Masond98237b2007-03-28 13:57:48 -0400605};
606
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400607struct extent_buffer *btrfs_find_create_tree_block(
608 struct btrfs_fs_info *fs_info,
Josef Bacik3fbaf252020-11-05 10:45:20 -0500609 u64 bytenr, u64 owner_root,
610 int level)
Chris Mason0999df52008-04-01 13:48:14 -0400611{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400612 if (btrfs_is_testing(fs_info))
613 return alloc_test_extent_buffer(fs_info, bytenr);
Josef Bacik3fbaf252020-11-05 10:45:20 -0500614 return alloc_extent_buffer(fs_info, bytenr, owner_root, level);
Chris Mason0999df52008-04-01 13:48:14 -0400615}
616
Qu Wenruo581c1762018-03-29 09:08:11 +0800617/*
618 * Read tree block at logical address @bytenr and do variant basic but critical
619 * verification.
620 *
Qu Wenruo789d6a32022-09-14 13:32:50 +0800621 * @check: expected tree parentness check, see comments of the
622 * structure for details.
Qu Wenruo581c1762018-03-29 09:08:11 +0800623 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400624struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
Qu Wenruo789d6a32022-09-14 13:32:50 +0800625 struct btrfs_tree_parent_check *check)
Chris Masone20d96d2007-03-22 12:13:20 -0400626{
Chris Mason5f39d392007-10-15 16:14:19 -0400627 struct extent_buffer *buf = NULL;
Chris Mason19c00dd2007-10-15 16:19:22 -0400628 int ret;
629
Qu Wenruo789d6a32022-09-14 13:32:50 +0800630 ASSERT(check);
631
632 buf = btrfs_find_create_tree_block(fs_info, bytenr, check->owner_root,
633 check->level);
Liu Boc871b0f2016-06-06 12:01:23 -0700634 if (IS_ERR(buf))
635 return buf;
Chris Masone4204de2008-01-08 15:46:27 -0500636
Qu Wenruo789d6a32022-09-14 13:32:50 +0800637 ret = btrfs_read_extent_buffer(buf, check);
Filipe David Borba Manana0f0fe8f2013-07-31 00:39:56 +0100638 if (ret) {
Nikolay Borisov537f38f2019-03-14 09:52:35 +0200639 free_extent_buffer_stale(buf);
Liu Bo64c043d2015-05-25 17:30:15 +0800640 return ERR_PTR(ret);
Filipe David Borba Manana0f0fe8f2013-07-31 00:39:56 +0100641 }
Chris Mason5f39d392007-10-15 16:14:19 -0400642 return buf;
Chris Masonce9adaa2008-04-09 16:28:12 -0400643
Chris Masoneb60cea2007-02-02 09:18:22 -0500644}
645
Jeff Mahoneyda170662016-06-15 09:22:56 -0400646static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100647 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500648{
David Sterbafef998d2024-04-18 00:47:13 +0200649 bool dummy = btrfs_is_testing(fs_info);
Josef Bacik2e608bd2021-11-05 16:45:44 -0400650
651 memset(&root->root_key, 0, sizeof(root->root_key));
652 memset(&root->root_item, 0, sizeof(root->root_item));
653 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik96dfcb42020-01-24 09:32:18 -0500654 root->fs_info = fs_info;
Josef Bacik2e608bd2021-11-05 16:45:44 -0400655 root->root_key.objectid = objectid;
Chris Masoncfaa7292007-02-21 17:04:57 -0500656 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500657 root->commit_root = NULL;
Miao Xie27cdeb72014-04-02 19:51:05 +0800658 root->state = 0;
Josef Bacikabed4aa2021-11-05 16:45:51 -0400659 RB_CLEAR_NODE(&root->rb_node);
Chris Mason0b86a832008-03-24 15:01:56 -0400660
Filipe Mananaca845292024-07-01 10:51:28 +0100661 btrfs_set_root_last_trans(root, 0);
Nikolay Borisov6b8fad52020-12-07 17:32:35 +0200662 root->free_objectid = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +0000663 root->nr_delalloc_inodes = 0;
Miao Xie199c2a92013-05-15 07:48:23 +0000664 root->nr_ordered_extents = 0;
Filipe Manana310b2f52024-04-24 16:58:01 +0100665 xa_init(&root->inodes);
Filipe Manana905a95f2024-04-17 16:06:13 +0100666 xa_init(&root->delayed_nodes);
Josef Bacik2e608bd2021-11-05 16:45:44 -0400667
668 btrfs_init_root_block_rsv(root);
Chris Mason0b86a832008-03-24 15:01:56 -0400669
670 INIT_LIST_HEAD(&root->dirty_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400671 INIT_LIST_HEAD(&root->root_list);
Miao Xieeb73c1b2013-05-15 07:48:22 +0000672 INIT_LIST_HEAD(&root->delalloc_inodes);
673 INIT_LIST_HEAD(&root->delalloc_root);
Miao Xie199c2a92013-05-15 07:48:23 +0000674 INIT_LIST_HEAD(&root->ordered_extents);
675 INIT_LIST_HEAD(&root->ordered_root);
Qu Wenruod2311e62019-01-23 15:15:14 +0800676 INIT_LIST_HEAD(&root->reloc_dirty_list);
Miao Xieeb73c1b2013-05-15 07:48:22 +0000677 spin_lock_init(&root->delalloc_lock);
Miao Xie199c2a92013-05-15 07:48:23 +0000678 spin_lock_init(&root->ordered_extent_lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400679 spin_lock_init(&root->accounting_lock);
Qu Wenruo82874752017-12-12 15:34:34 +0800680 spin_lock_init(&root->qgroup_meta_rsv_lock);
Chris Masona2135012008-06-25 16:01:30 -0400681 mutex_init(&root->objectid_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400682 mutex_init(&root->log_mutex);
Miao Xie31f3d252014-03-06 13:55:02 +0800683 mutex_init(&root->ordered_extent_mutex);
Miao Xie573bfb72014-03-06 13:55:03 +0800684 mutex_init(&root->delalloc_mutex);
Qu Wenruoc53e9652020-07-13 18:50:48 +0800685 init_waitqueue_head(&root->qgroup_flush_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500686 init_waitqueue_head(&root->log_writer_wait);
687 init_waitqueue_head(&root->log_commit_wait[0]);
688 init_waitqueue_head(&root->log_commit_wait[1]);
Miao Xie8b050d32014-02-20 18:08:58 +0800689 INIT_LIST_HEAD(&root->log_ctxs[0]);
690 INIT_LIST_HEAD(&root->log_ctxs[1]);
Yan Zheng7237f182009-01-21 12:54:03 -0500691 atomic_set(&root->log_commit[0], 0);
692 atomic_set(&root->log_commit[1], 0);
693 atomic_set(&root->log_writers, 0);
Miao Xie2ecb7922012-09-06 04:04:27 -0600694 atomic_set(&root->log_batch, 0);
Elena Reshetova0700cea2017-03-03 10:55:18 +0200695 refcount_set(&root->refs, 1);
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800696 atomic_set(&root->snapshot_force_cow, 0);
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700697 atomic_set(&root->nr_swapfiles, 0);
Filipe Manana60088592023-10-04 11:38:49 +0100698 btrfs_set_root_log_transid(root, 0);
Miao Xied1433de2014-02-20 18:08:59 +0800699 root->log_transid_committed = -1;
Filipe Mananaf9850782023-10-04 11:38:48 +0100700 btrfs_set_root_last_log_commit(root, 0);
Josef Bacik2e608bd2021-11-05 16:45:44 -0400701 root->anon_dev = 0;
Filipe Mananae289f032020-05-18 12:14:50 +0100702 if (!dummy) {
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800703 extent_io_tree_init(fs_info, &root->dirty_log_pages,
David Sterba35da5a72022-10-28 02:47:06 +0200704 IO_TREE_ROOT_DIRTY_LOG_PAGES);
Filipe Mananae289f032020-05-18 12:14:50 +0100705 extent_io_tree_init(fs_info, &root->log_csum_range,
David Sterba35da5a72022-10-28 02:47:06 +0200706 IO_TREE_LOG_CSUM_RANGE);
Filipe Mananae289f032020-05-18 12:14:50 +0100707 }
Chris Mason017e5362008-07-28 15:32:51 -0400708
Anand Jain5f3ab902012-12-07 09:28:54 +0000709 spin_lock_init(&root->root_item_lock);
Qu Wenruo370a11b2019-01-23 15:15:16 +0800710 btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
Josef Bacikbd647ce2020-01-24 09:33:00 -0500711#ifdef CONFIG_BTRFS_DEBUG
712 INIT_LIST_HEAD(&root->leak_list);
David Sterbafc7cbcd2022-07-15 13:59:21 +0200713 spin_lock(&fs_info->fs_roots_radix_lock);
Josef Bacikbd647ce2020-01-24 09:33:00 -0500714 list_add_tail(&root->leak_list, &fs_info->allocated_roots);
David Sterbafc7cbcd2022-07-15 13:59:21 +0200715 spin_unlock(&fs_info->fs_roots_radix_lock);
Josef Bacikbd647ce2020-01-24 09:33:00 -0500716#endif
Chris Mason3768f362007-03-13 16:47:54 -0400717}
718
David Sterba74e4d822016-02-11 11:01:55 +0100719static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
Josef Bacik96dfcb42020-01-24 09:32:18 -0500720 u64 objectid, gfp_t flags)
Al Viro6f07e422011-11-17 00:46:16 -0500721{
David Sterba74e4d822016-02-11 11:01:55 +0100722 struct btrfs_root *root = kzalloc(sizeof(*root), flags);
Al Viro6f07e422011-11-17 00:46:16 -0500723 if (root)
Josef Bacik96dfcb42020-01-24 09:32:18 -0500724 __setup_root(root, fs_info, objectid);
Al Viro6f07e422011-11-17 00:46:16 -0500725 return root;
726}
727
Josef Bacik06ea65a2013-09-19 16:07:01 -0400728#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
729/* Should only be used by the testing infrastructure */
Jeff Mahoneyda170662016-06-15 09:22:56 -0400730struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
Josef Bacik06ea65a2013-09-19 16:07:01 -0400731{
732 struct btrfs_root *root;
733
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400734 if (!fs_info)
735 return ERR_PTR(-EINVAL);
736
Josef Bacik96dfcb42020-01-24 09:32:18 -0500737 root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
Josef Bacik06ea65a2013-09-19 16:07:01 -0400738 if (!root)
739 return ERR_PTR(-ENOMEM);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400740
Feifei Xub9ef22d2016-06-01 19:18:25 +0800741 /* We don't use the stripesize in selftest, set it as sectorsize */
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400742 root->alloc_bytenr = 0;
Josef Bacik06ea65a2013-09-19 16:07:01 -0400743
744 return root;
745}
746#endif
747
Josef Bacikabed4aa2021-11-05 16:45:51 -0400748static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node)
749{
750 const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node);
751 const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node);
752
753 return btrfs_comp_cpu_keys(&a->root_key, &b->root_key);
754}
755
756static int global_root_key_cmp(const void *k, const struct rb_node *node)
757{
758 const struct btrfs_key *key = k;
759 const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node);
760
761 return btrfs_comp_cpu_keys(key, &root->root_key);
762}
763
764int btrfs_global_root_insert(struct btrfs_root *root)
765{
766 struct btrfs_fs_info *fs_info = root->fs_info;
767 struct rb_node *tmp;
Qu Wenruo745806f2023-06-11 08:09:13 +0800768 int ret = 0;
Josef Bacikabed4aa2021-11-05 16:45:51 -0400769
770 write_lock(&fs_info->global_root_lock);
771 tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp);
772 write_unlock(&fs_info->global_root_lock);
Josef Bacikabed4aa2021-11-05 16:45:51 -0400773
Qu Wenruo745806f2023-06-11 08:09:13 +0800774 if (tmp) {
775 ret = -EEXIST;
776 btrfs_warn(fs_info, "global root %llu %llu already exists",
Josef Bacike094f482024-04-15 16:16:23 -0400777 btrfs_root_id(root), root->root_key.offset);
Qu Wenruo745806f2023-06-11 08:09:13 +0800778 }
779 return ret;
Josef Bacikabed4aa2021-11-05 16:45:51 -0400780}
781
782void btrfs_global_root_delete(struct btrfs_root *root)
783{
784 struct btrfs_fs_info *fs_info = root->fs_info;
785
786 write_lock(&fs_info->global_root_lock);
787 rb_erase(&root->rb_node, &fs_info->global_root_tree);
788 write_unlock(&fs_info->global_root_lock);
789}
790
791struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
792 struct btrfs_key *key)
793{
794 struct rb_node *node;
795 struct btrfs_root *root = NULL;
796
797 read_lock(&fs_info->global_root_lock);
798 node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp);
799 if (node)
800 root = container_of(node, struct btrfs_root, rb_node);
801 read_unlock(&fs_info->global_root_lock);
802
803 return root;
804}
805
Josef Bacikf7238e52021-12-15 15:40:08 -0500806static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
807{
808 struct btrfs_block_group *block_group;
809 u64 ret;
810
811 if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
812 return 0;
813
814 if (bytenr)
815 block_group = btrfs_lookup_block_group(fs_info, bytenr);
816 else
817 block_group = btrfs_lookup_first_block_group(fs_info, bytenr);
818 ASSERT(block_group);
819 if (!block_group)
820 return 0;
821 ret = block_group->global_root_id;
822 btrfs_put_block_group(block_group);
823
824 return ret;
825}
826
Josef Bacikabed4aa2021-11-05 16:45:51 -0400827struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr)
828{
829 struct btrfs_key key = {
830 .objectid = BTRFS_CSUM_TREE_OBJECTID,
831 .type = BTRFS_ROOT_ITEM_KEY,
Josef Bacikf7238e52021-12-15 15:40:08 -0500832 .offset = btrfs_global_root_id(fs_info, bytenr),
Josef Bacikabed4aa2021-11-05 16:45:51 -0400833 };
834
835 return btrfs_global_root(fs_info, &key);
836}
837
838struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr)
839{
840 struct btrfs_key key = {
841 .objectid = BTRFS_EXTENT_TREE_OBJECTID,
842 .type = BTRFS_ROOT_ITEM_KEY,
Josef Bacikf7238e52021-12-15 15:40:08 -0500843 .offset = btrfs_global_root_id(fs_info, bytenr),
Josef Bacikabed4aa2021-11-05 16:45:51 -0400844 };
845
846 return btrfs_global_root(fs_info, &key);
847}
848
Arne Jansen20897f52011-09-13 12:44:20 +0200849struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
Arne Jansen20897f52011-09-13 12:44:20 +0200850 u64 objectid)
851{
David Sterba9b7a2442019-03-20 13:20:49 +0100852 struct btrfs_fs_info *fs_info = trans->fs_info;
Arne Jansen20897f52011-09-13 12:44:20 +0200853 struct extent_buffer *leaf;
854 struct btrfs_root *tree_root = fs_info->tree_root;
855 struct btrfs_root *root;
856 struct btrfs_key key;
Filipe Mananab89f6d1f2018-12-13 21:16:45 +0000857 unsigned int nofs_flag;
Arne Jansen20897f52011-09-13 12:44:20 +0200858 int ret = 0;
Arne Jansen20897f52011-09-13 12:44:20 +0200859
Filipe Mananab89f6d1f2018-12-13 21:16:45 +0000860 /*
861 * We're holding a transaction handle, so use a NOFS memory allocation
862 * context to avoid deadlock if reclaim happens.
863 */
864 nofs_flag = memalloc_nofs_save();
Josef Bacik96dfcb42020-01-24 09:32:18 -0500865 root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL);
Filipe Mananab89f6d1f2018-12-13 21:16:45 +0000866 memalloc_nofs_restore(nofs_flag);
Arne Jansen20897f52011-09-13 12:44:20 +0200867 if (!root)
868 return ERR_PTR(-ENOMEM);
869
Arne Jansen20897f52011-09-13 12:44:20 +0200870 root->root_key.objectid = objectid;
871 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
872 root->root_key.offset = 0;
873
Josef Bacik9631e4c2020-08-20 11:46:03 -0400874 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
Boris Burkov60ea1052023-06-21 17:37:23 -0700875 0, BTRFS_NESTING_NORMAL);
Arne Jansen20897f52011-09-13 12:44:20 +0200876 if (IS_ERR(leaf)) {
877 ret = PTR_ERR(leaf);
Tsutomu Itoh1dd05682013-03-21 04:32:32 +0000878 leaf = NULL;
Peng Haoc1b07852022-10-07 18:33:35 +0200879 goto fail;
Arne Jansen20897f52011-09-13 12:44:20 +0200880 }
881
Arne Jansen20897f52011-09-13 12:44:20 +0200882 root->node = leaf;
Filipe Manana50564b62023-09-12 13:04:29 +0100883 btrfs_mark_buffer_dirty(trans, leaf);
Arne Jansen20897f52011-09-13 12:44:20 +0200884
885 root->commit_root = btrfs_root_node(root);
Miao Xie27cdeb72014-04-02 19:51:05 +0800886 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
Arne Jansen20897f52011-09-13 12:44:20 +0200887
David Sterbaf944d2c2020-09-15 21:00:04 +0200888 btrfs_set_root_flags(&root->root_item, 0);
889 btrfs_set_root_limit(&root->root_item, 0);
Arne Jansen20897f52011-09-13 12:44:20 +0200890 btrfs_set_root_bytenr(&root->root_item, leaf->start);
891 btrfs_set_root_generation(&root->root_item, trans->transid);
892 btrfs_set_root_level(&root->root_item, 0);
893 btrfs_set_root_refs(&root->root_item, 1);
894 btrfs_set_root_used(&root->root_item, leaf->len);
895 btrfs_set_root_last_snapshot(&root->root_item, 0);
896 btrfs_set_root_dirid(&root->root_item, 0);
Qu Wenruo33d85fd2017-10-31 14:08:16 +0800897 if (is_fstree(objectid))
Andy Shevchenko807fc792020-02-24 17:37:51 +0200898 generate_random_guid(root->root_item.uuid);
899 else
900 export_guid(root->root_item.uuid, &guid_null);
David Sterbac8422682020-09-15 21:44:52 +0200901 btrfs_set_root_drop_level(&root->root_item, 0);
Arne Jansen20897f52011-09-13 12:44:20 +0200902
Boris Burkov8a6a87c2020-11-18 15:06:27 -0800903 btrfs_tree_unlock(leaf);
904
Arne Jansen20897f52011-09-13 12:44:20 +0200905 key.objectid = objectid;
906 key.type = BTRFS_ROOT_ITEM_KEY;
907 key.offset = 0;
908 ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
909 if (ret)
910 goto fail;
911
Arne Jansen20897f52011-09-13 12:44:20 +0200912 return root;
Tsutomu Itoh1dd05682013-03-21 04:32:32 +0000913
Boris Burkov8a6a87c2020-11-18 15:06:27 -0800914fail:
Josef Bacik00246522020-01-24 09:33:01 -0500915 btrfs_put_root(root);
Tsutomu Itoh1dd05682013-03-21 04:32:32 +0000916
917 return ERR_PTR(ret);
Arne Jansen20897f52011-09-13 12:44:20 +0200918}
919
Yan Zheng7237f182009-01-21 12:54:03 -0500920static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
921 struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400922{
923 struct btrfs_root *root;
Chris Masone02119d2008-09-05 16:13:11 -0400924
Josef Bacik96dfcb42020-01-24 09:32:18 -0500925 root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS);
Chris Masone02119d2008-09-05 16:13:11 -0400926 if (!root)
Yan Zheng7237f182009-01-21 12:54:03 -0500927 return ERR_PTR(-ENOMEM);
Chris Masone02119d2008-09-05 16:13:11 -0400928
Chris Masone02119d2008-09-05 16:13:11 -0400929 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
930 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
931 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
Miao Xie27cdeb72014-04-02 19:51:05 +0800932
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900933 return root;
934}
935
936int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
937 struct btrfs_root *root)
938{
939 struct extent_buffer *leaf;
940
Yan Zheng7237f182009-01-21 12:54:03 -0500941 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800942 * DON'T set SHAREABLE bit for log trees.
Miao Xie27cdeb72014-04-02 19:51:05 +0800943 *
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800944 * Log trees are not exposed to user space thus can't be snapshotted,
945 * and they go away before a real commit is actually done.
946 *
947 * They do store pointers to file data extents, and those reference
948 * counts still get updated (along with back refs to the log tree).
Yan Zheng7237f182009-01-21 12:54:03 -0500949 */
Chris Masone02119d2008-09-05 16:13:11 -0400950
David Sterba4d75f8a2014-06-15 01:54:12 +0200951 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
Boris Burkov60ea1052023-06-21 17:37:23 -0700952 NULL, 0, 0, 0, 0, BTRFS_NESTING_NORMAL);
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900953 if (IS_ERR(leaf))
954 return PTR_ERR(leaf);
Chris Masone02119d2008-09-05 16:13:11 -0400955
Yan Zheng7237f182009-01-21 12:54:03 -0500956 root->node = leaf;
Chris Masone02119d2008-09-05 16:13:11 -0400957
Filipe Manana50564b62023-09-12 13:04:29 +0100958 btrfs_mark_buffer_dirty(trans, root->node);
Chris Masone02119d2008-09-05 16:13:11 -0400959 btrfs_tree_unlock(root->node);
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900960
961 return 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500962}
963
964int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
965 struct btrfs_fs_info *fs_info)
966{
967 struct btrfs_root *log_root;
968
969 log_root = alloc_log_tree(trans, fs_info);
970 if (IS_ERR(log_root))
971 return PTR_ERR(log_root);
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900972
Naohiro Aota3ddebf22021-02-04 19:22:20 +0900973 if (!btrfs_is_zoned(fs_info)) {
974 int ret = btrfs_alloc_log_tree_node(trans, log_root);
975
976 if (ret) {
977 btrfs_put_root(log_root);
978 return ret;
979 }
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900980 }
981
Yan Zheng7237f182009-01-21 12:54:03 -0500982 WARN_ON(fs_info->log_root_tree);
983 fs_info->log_root_tree = log_root;
984 return 0;
985}
986
987int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root)
989{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400990 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -0500991 struct btrfs_root *log_root;
992 struct btrfs_inode_item *inode_item;
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900993 int ret;
Yan Zheng7237f182009-01-21 12:54:03 -0500994
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400995 log_root = alloc_log_tree(trans, fs_info);
Yan Zheng7237f182009-01-21 12:54:03 -0500996 if (IS_ERR(log_root))
997 return PTR_ERR(log_root);
998
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900999 ret = btrfs_alloc_log_tree_node(trans, log_root);
1000 if (ret) {
1001 btrfs_put_root(log_root);
1002 return ret;
1003 }
1004
Filipe Mananaca845292024-07-01 10:51:28 +01001005 btrfs_set_root_last_trans(log_root, trans->transid);
Josef Bacike094f482024-04-15 16:16:23 -04001006 log_root->root_key.offset = btrfs_root_id(root);
Yan Zheng7237f182009-01-21 12:54:03 -05001007
1008 inode_item = &log_root->root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001009 btrfs_set_stack_inode_generation(inode_item, 1);
1010 btrfs_set_stack_inode_size(inode_item, 3);
1011 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001012 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001014 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Yan Zheng7237f182009-01-21 12:54:03 -05001015
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016 btrfs_set_root_node(&log_root->root_item, log_root->node);
Yan Zheng7237f182009-01-21 12:54:03 -05001017
1018 WARN_ON(root->log_root);
1019 root->log_root = log_root;
Filipe Manana60088592023-10-04 11:38:49 +01001020 btrfs_set_root_log_transid(root, 0);
Miao Xied1433de2014-02-20 18:08:59 +08001021 root->log_transid_committed = -1;
Filipe Mananaf9850782023-10-04 11:38:48 +01001022 btrfs_set_root_last_log_commit(root, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001023 return 0;
1024}
1025
Josef Bacik49d11be2020-10-19 16:02:31 -04001026static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
1027 struct btrfs_path *path,
David Sterba2917f742024-05-30 19:14:12 +02001028 const struct btrfs_key *key)
Chris Masone02119d2008-09-05 16:13:11 -04001029{
1030 struct btrfs_root *root;
Qu Wenruo789d6a32022-09-14 13:32:50 +08001031 struct btrfs_tree_parent_check check = { 0 };
Chris Masone02119d2008-09-05 16:13:11 -04001032 struct btrfs_fs_info *fs_info = tree_root->fs_info;
Yan Zheng84234f32008-10-29 14:49:05 -04001033 u64 generation;
Miao Xiecb517ea2013-05-15 07:48:19 +00001034 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08001035 int level;
Miao Xiecb517ea2013-05-15 07:48:19 +00001036
Josef Bacik96dfcb42020-01-24 09:32:18 -05001037 root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
Josef Bacik49d11be2020-10-19 16:02:31 -04001038 if (!root)
1039 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001040
Miao Xiecb517ea2013-05-15 07:48:19 +00001041 ret = btrfs_find_root(tree_root, key, path,
1042 &root->root_item, &root->root_key);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001043 if (ret) {
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001044 if (ret > 0)
1045 ret = -ENOENT;
Josef Bacik49d11be2020-10-19 16:02:31 -04001046 goto fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001047 }
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001048
Yan Zheng84234f32008-10-29 14:49:05 -04001049 generation = btrfs_root_generation(&root->root_item);
Qu Wenruo581c1762018-03-29 09:08:11 +08001050 level = btrfs_root_level(&root->root_item);
Qu Wenruo789d6a32022-09-14 13:32:50 +08001051 check.level = level;
1052 check.transid = generation;
1053 check.owner_root = key->objectid;
1054 root->node = read_tree_block(fs_info, btrfs_root_bytenr(&root->root_item),
1055 &check);
Liu Bo64c043d2015-05-25 17:30:15 +08001056 if (IS_ERR(root->node)) {
1057 ret = PTR_ERR(root->node);
Josef Bacik8c389382020-02-14 16:11:42 -05001058 root->node = NULL;
Josef Bacik49d11be2020-10-19 16:02:31 -04001059 goto fail;
Qu Wenruo4eb150d2022-02-22 15:41:19 +08001060 }
1061 if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
Miao Xiecb517ea2013-05-15 07:48:19 +00001062 ret = -EIO;
Josef Bacik49d11be2020-10-19 16:02:31 -04001063 goto fail;
Josef Bacik416bc652013-04-23 14:17:42 -04001064 }
Qu Wenruo88c602a2022-03-16 08:05:58 +08001065
1066 /*
1067 * For real fs, and not log/reloc trees, root owner must
1068 * match its root node owner
1069 */
David Sterbafef998d2024-04-18 00:47:13 +02001070 if (!btrfs_is_testing(fs_info) &&
Josef Bacike094f482024-04-15 16:16:23 -04001071 btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
1072 btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
1073 btrfs_root_id(root) != btrfs_header_owner(root->node)) {
Qu Wenruo88c602a2022-03-16 08:05:58 +08001074 btrfs_crit(fs_info,
1075"root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
Josef Bacike094f482024-04-15 16:16:23 -04001076 btrfs_root_id(root), root->node->start,
Qu Wenruo88c602a2022-03-16 08:05:58 +08001077 btrfs_header_owner(root->node),
Josef Bacike094f482024-04-15 16:16:23 -04001078 btrfs_root_id(root));
Qu Wenruo88c602a2022-03-16 08:05:58 +08001079 ret = -EUCLEAN;
1080 goto fail;
1081 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 root->commit_root = btrfs_root_node(root);
Miao Xiecb517ea2013-05-15 07:48:19 +00001083 return root;
Josef Bacik49d11be2020-10-19 16:02:31 -04001084fail:
Josef Bacik00246522020-01-24 09:33:01 -05001085 btrfs_put_root(root);
Josef Bacik49d11be2020-10-19 16:02:31 -04001086 return ERR_PTR(ret);
1087}
1088
1089struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
David Sterba2917f742024-05-30 19:14:12 +02001090 const struct btrfs_key *key)
Josef Bacik49d11be2020-10-19 16:02:31 -04001091{
1092 struct btrfs_root *root;
1093 struct btrfs_path *path;
1094
1095 path = btrfs_alloc_path();
1096 if (!path)
1097 return ERR_PTR(-ENOMEM);
1098 root = read_tree_root_path(tree_root, path, key);
1099 btrfs_free_path(path);
1100
1101 return root;
Miao Xiecb517ea2013-05-15 07:48:19 +00001102}
1103
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001104/*
1105 * Initialize subvolume root in-memory structure
1106 *
1107 * @anon_dev: anonymous device to attach to the root, if zero, allocate new
1108 */
1109static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev)
Miao Xiecb517ea2013-05-15 07:48:19 +00001110{
1111 int ret;
1112
David Sterba0b548532023-03-01 21:47:08 +01001113 btrfs_drew_lock_init(&root->snapshot_lock);
Miao Xie8257b2d2014-03-06 13:38:19 +08001114
Josef Bacike094f482024-04-15 16:16:23 -04001115 if (btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
Qu Wenruo6ebcd022023-08-03 17:20:43 +08001116 !btrfs_is_data_reloc_root(root) &&
Josef Bacike094f482024-04-15 16:16:23 -04001117 is_fstree(btrfs_root_id(root))) {
Qu Wenruo92a7cc42020-05-15 14:01:40 +08001118 set_bit(BTRFS_ROOT_SHAREABLE, &root->state);
Josef Bacikf39e4572020-01-24 09:32:19 -05001119 btrfs_check_and_init_root_item(&root->root_item);
1120 }
1121
Qu Wenruo851fd732020-06-16 10:17:34 +08001122 /*
1123 * Don't assign anonymous block device to roots that are not exposed to
1124 * userspace, the id pool is limited to 1M
1125 */
Josef Bacike094f482024-04-15 16:16:23 -04001126 if (is_fstree(btrfs_root_id(root)) &&
Qu Wenruo851fd732020-06-16 10:17:34 +08001127 btrfs_root_refs(&root->root_item) > 0) {
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001128 if (!anon_dev) {
1129 ret = get_anon_bdev(&root->anon_dev);
1130 if (ret)
1131 goto fail;
1132 } else {
1133 root->anon_dev = anon_dev;
1134 }
Qu Wenruo851fd732020-06-16 10:17:34 +08001135 }
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301136
1137 mutex_lock(&root->objectid_mutex);
Nikolay Borisov453e4872020-12-07 17:32:32 +02001138 ret = btrfs_init_root_free_objectid(root);
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301139 if (ret) {
1140 mutex_unlock(&root->objectid_mutex);
Liu Bo876d2cf2016-06-28 13:44:38 -07001141 goto fail;
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301142 }
1143
Nikolay Borisov6b8fad52020-12-07 17:32:35 +02001144 ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301145
1146 mutex_unlock(&root->objectid_mutex);
1147
Miao Xiecb517ea2013-05-15 07:48:19 +00001148 return 0;
1149fail:
David Sterba84db5cc2018-07-20 16:30:25 +02001150 /* The caller is responsible to call btrfs_free_fs_root */
Miao Xiecb517ea2013-05-15 07:48:19 +00001151 return ret;
1152}
1153
Josef Bacika98db0f2020-01-24 09:32:25 -05001154static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1155 u64 root_id)
Miao Xiecb517ea2013-05-15 07:48:19 +00001156{
1157 struct btrfs_root *root;
1158
David Sterbafc7cbcd2022-07-15 13:59:21 +02001159 spin_lock(&fs_info->fs_roots_radix_lock);
1160 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1161 (unsigned long)root_id);
Christoph Hellwig25ac0472023-05-23 10:40:20 +02001162 root = btrfs_grab_root(root);
David Sterbafc7cbcd2022-07-15 13:59:21 +02001163 spin_unlock(&fs_info->fs_roots_radix_lock);
Miao Xiecb517ea2013-05-15 07:48:19 +00001164 return root;
1165}
1166
Josef Bacik49d11be2020-10-19 16:02:31 -04001167static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info,
1168 u64 objectid)
1169{
Josef Bacikabed4aa2021-11-05 16:45:51 -04001170 struct btrfs_key key = {
1171 .objectid = objectid,
1172 .type = BTRFS_ROOT_ITEM_KEY,
1173 .offset = 0,
1174 };
1175
Christoph Hellwige91909a2023-05-23 10:40:19 +02001176 switch (objectid) {
1177 case BTRFS_ROOT_TREE_OBJECTID:
Josef Bacik49d11be2020-10-19 16:02:31 -04001178 return btrfs_grab_root(fs_info->tree_root);
Christoph Hellwige91909a2023-05-23 10:40:19 +02001179 case BTRFS_EXTENT_TREE_OBJECTID:
Josef Bacikabed4aa2021-11-05 16:45:51 -04001180 return btrfs_grab_root(btrfs_global_root(fs_info, &key));
Christoph Hellwige91909a2023-05-23 10:40:19 +02001181 case BTRFS_CHUNK_TREE_OBJECTID:
Josef Bacik49d11be2020-10-19 16:02:31 -04001182 return btrfs_grab_root(fs_info->chunk_root);
Christoph Hellwige91909a2023-05-23 10:40:19 +02001183 case BTRFS_DEV_TREE_OBJECTID:
Josef Bacik49d11be2020-10-19 16:02:31 -04001184 return btrfs_grab_root(fs_info->dev_root);
Christoph Hellwige91909a2023-05-23 10:40:19 +02001185 case BTRFS_CSUM_TREE_OBJECTID:
Josef Bacikabed4aa2021-11-05 16:45:51 -04001186 return btrfs_grab_root(btrfs_global_root(fs_info, &key));
Christoph Hellwige91909a2023-05-23 10:40:19 +02001187 case BTRFS_QUOTA_TREE_OBJECTID:
Christoph Hellwig85724172023-05-23 10:40:18 +02001188 return btrfs_grab_root(fs_info->quota_root);
Christoph Hellwige91909a2023-05-23 10:40:19 +02001189 case BTRFS_UUID_TREE_OBJECTID:
Christoph Hellwig85724172023-05-23 10:40:18 +02001190 return btrfs_grab_root(fs_info->uuid_root);
Christoph Hellwige91909a2023-05-23 10:40:19 +02001191 case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
Christoph Hellwig85724172023-05-23 10:40:18 +02001192 return btrfs_grab_root(fs_info->block_group_root);
Christoph Hellwige91909a2023-05-23 10:40:19 +02001193 case BTRFS_FREE_SPACE_TREE_OBJECTID:
Christoph Hellwig85724172023-05-23 10:40:18 +02001194 return btrfs_grab_root(btrfs_global_root(fs_info, &key));
Johannes Thumshirn51502092023-09-14 09:06:57 -07001195 case BTRFS_RAID_STRIPE_TREE_OBJECTID:
1196 return btrfs_grab_root(fs_info->stripe_root);
Christoph Hellwige91909a2023-05-23 10:40:19 +02001197 default:
1198 return NULL;
1199 }
Josef Bacik49d11be2020-10-19 16:02:31 -04001200}
1201
Miao Xiecb517ea2013-05-15 07:48:19 +00001202int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1203 struct btrfs_root *root)
1204{
1205 int ret;
1206
David Sterbafc7cbcd2022-07-15 13:59:21 +02001207 ret = radix_tree_preload(GFP_NOFS);
1208 if (ret)
1209 return ret;
1210
1211 spin_lock(&fs_info->fs_roots_radix_lock);
1212 ret = radix_tree_insert(&fs_info->fs_roots_radix,
Josef Bacike094f482024-04-15 16:16:23 -04001213 (unsigned long)btrfs_root_id(root),
David Sterbafc7cbcd2022-07-15 13:59:21 +02001214 root);
Josef Bacikaf01d2e2020-01-24 09:32:27 -05001215 if (ret == 0) {
Josef Bacik00246522020-01-24 09:33:01 -05001216 btrfs_grab_root(root);
David Sterbafc7cbcd2022-07-15 13:59:21 +02001217 set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
Josef Bacikaf01d2e2020-01-24 09:32:27 -05001218 }
David Sterbafc7cbcd2022-07-15 13:59:21 +02001219 spin_unlock(&fs_info->fs_roots_radix_lock);
1220 radix_tree_preload_end();
Miao Xiecb517ea2013-05-15 07:48:19 +00001221
1222 return ret;
1223}
1224
David Sterba2917f742024-05-30 19:14:12 +02001225void btrfs_check_leaked_roots(const struct btrfs_fs_info *fs_info)
Josef Bacikbd647ce2020-01-24 09:33:00 -05001226{
1227#ifdef CONFIG_BTRFS_DEBUG
1228 struct btrfs_root *root;
1229
1230 while (!list_empty(&fs_info->allocated_roots)) {
Josef Bacik457f1862020-09-03 14:29:51 -04001231 char buf[BTRFS_ROOT_NAME_BUF_LEN];
1232
Josef Bacikbd647ce2020-01-24 09:33:00 -05001233 root = list_first_entry(&fs_info->allocated_roots,
1234 struct btrfs_root, leak_list);
Josef Bacik457f1862020-09-03 14:29:51 -04001235 btrfs_err(fs_info, "leaked root %s refcount %d",
Josef Bacik71008732020-12-16 11:18:44 -05001236 btrfs_root_name(&root->root_key, buf),
Josef Bacikbd647ce2020-01-24 09:33:00 -05001237 refcount_read(&root->refs));
Josef Bacik8fd2b122024-01-02 15:18:07 -05001238 WARN_ON_ONCE(1);
Josef Bacikbd647ce2020-01-24 09:33:00 -05001239 while (refcount_read(&root->refs) > 1)
Josef Bacik00246522020-01-24 09:33:01 -05001240 btrfs_put_root(root);
1241 btrfs_put_root(root);
Josef Bacikbd647ce2020-01-24 09:33:00 -05001242 }
1243#endif
1244}
1245
Josef Bacikabed4aa2021-11-05 16:45:51 -04001246static void free_global_roots(struct btrfs_fs_info *fs_info)
1247{
1248 struct btrfs_root *root;
1249 struct rb_node *node;
1250
1251 while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) {
1252 root = rb_entry(node, struct btrfs_root, rb_node);
1253 rb_erase(&root->rb_node, &fs_info->global_root_tree);
1254 btrfs_put_root(root);
1255 }
1256}
1257
Josef Bacik0d4b0462020-01-24 09:32:53 -05001258void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
1259{
Filipe Mananaf1d97e72024-03-22 18:02:59 +00001260 struct percpu_counter *em_counter = &fs_info->evictable_extent_maps;
1261
Josef Bacik141386e2020-01-24 09:32:57 -05001262 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
1263 percpu_counter_destroy(&fs_info->delalloc_bytes);
Josef Bacik5deb17e2020-10-09 09:28:20 -04001264 percpu_counter_destroy(&fs_info->ordered_bytes);
Filipe Mananaf1d97e72024-03-22 18:02:59 +00001265 if (percpu_counter_initialized(em_counter))
1266 ASSERT(percpu_counter_sum_positive(em_counter) == 0);
1267 percpu_counter_destroy(em_counter);
Josef Bacik141386e2020-01-24 09:32:57 -05001268 percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
1269 btrfs_free_csum_hash(fs_info);
1270 btrfs_free_stripe_hash_table(fs_info);
1271 btrfs_free_ref_cache(fs_info);
Josef Bacik0d4b0462020-01-24 09:32:53 -05001272 kfree(fs_info->balance_ctl);
1273 kfree(fs_info->delayed_root);
Josef Bacikabed4aa2021-11-05 16:45:51 -04001274 free_global_roots(fs_info);
Josef Bacik00246522020-01-24 09:33:01 -05001275 btrfs_put_root(fs_info->tree_root);
1276 btrfs_put_root(fs_info->chunk_root);
1277 btrfs_put_root(fs_info->dev_root);
Josef Bacik00246522020-01-24 09:33:01 -05001278 btrfs_put_root(fs_info->quota_root);
1279 btrfs_put_root(fs_info->uuid_root);
Josef Bacik00246522020-01-24 09:33:01 -05001280 btrfs_put_root(fs_info->fs_root);
Qu Wenruoaeb935a2020-05-15 14:01:42 +08001281 btrfs_put_root(fs_info->data_reloc_root);
Josef Bacik9c54e802021-12-15 15:40:07 -05001282 btrfs_put_root(fs_info->block_group_root);
Johannes Thumshirn51502092023-09-14 09:06:57 -07001283 btrfs_put_root(fs_info->stripe_root);
Josef Bacikbd647ce2020-01-24 09:33:00 -05001284 btrfs_check_leaked_roots(fs_info);
Josef Bacik3fd63722020-02-14 16:11:40 -05001285 btrfs_extent_buffer_leak_debug_check(fs_info);
Josef Bacik0d4b0462020-01-24 09:32:53 -05001286 kfree(fs_info->super_copy);
1287 kfree(fs_info->super_for_commit);
Qu Wenruo8481dd82021-08-17 17:38:51 +08001288 kfree(fs_info->subpage_info);
Josef Bacik0d4b0462020-01-24 09:32:53 -05001289 kvfree(fs_info);
1290}
1291
1292
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001293/*
1294 * Get an in-memory reference of a root structure.
1295 *
1296 * For essential trees like root/extent tree, we grab it from fs_info directly.
1297 * For subvolume trees, we check the cached filesystem roots first. If not
1298 * found, then read it from disk and add it to cached fs roots.
1299 *
1300 * Caller should release the root by calling btrfs_put_root() after the usage.
1301 *
1302 * NOTE: Reloc and log trees can't be read by this function as they share the
1303 * same root objectid.
1304 *
1305 * @objectid: root id
1306 * @anon_dev: preallocated anonymous block device number for new roots,
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001307 * pass NULL for a new allocation.
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001308 * @check_ref: whether to check root item references, If true, return -ENOENT
1309 * for orphan roots
1310 */
1311static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001312 u64 objectid, dev_t *anon_dev,
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001313 bool check_ref)
Chris Mason5eda7b52007-06-22 14:16:25 -04001314{
1315 struct btrfs_root *root;
David Sterba381cf652015-01-02 18:45:16 +01001316 struct btrfs_path *path;
David Sterba1d4c08e2015-01-02 19:36:14 +01001317 struct btrfs_key key;
Chris Mason5eda7b52007-06-22 14:16:25 -04001318 int ret;
1319
Josef Bacik49d11be2020-10-19 16:02:31 -04001320 root = btrfs_get_global_root(fs_info, objectid);
1321 if (root)
1322 return root;
Qu Wenruo773e7222023-08-03 17:20:41 +08001323
1324 /*
1325 * If we're called for non-subvolume trees, and above function didn't
1326 * find one, do not try to read it from disk.
1327 *
1328 * This is namely for free-space-tree and quota tree, which can change
1329 * at runtime and should only be grabbed from fs_info.
1330 */
1331 if (!is_fstree(objectid) && objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
1332 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001333again:
David Sterba56e93572020-05-15 19:35:55 +02001334 root = btrfs_lookup_fs_root(fs_info, objectid);
Stefan Behrens48475472013-08-23 10:34:42 +02001335 if (root) {
Qu Wenruoe03ee2f2024-01-20 19:41:28 +10301336 /*
1337 * Some other caller may have read out the newly inserted
1338 * subvolume already (for things like backref walk etc). Not
1339 * that common but still possible. In that case, we just need
1340 * to free the anon_dev.
1341 */
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001342 if (unlikely(anon_dev && *anon_dev)) {
1343 free_anon_bdev(*anon_dev);
1344 *anon_dev = 0;
Qu Wenruoe03ee2f2024-01-20 19:41:28 +10301345 }
1346
Josef Bacikbc44d7c2020-01-24 09:32:56 -05001347 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
Josef Bacik00246522020-01-24 09:33:01 -05001348 btrfs_put_root(root);
Stefan Behrens48475472013-08-23 10:34:42 +02001349 return ERR_PTR(-ENOENT);
Josef Bacikbc44d7c2020-01-24 09:32:56 -05001350 }
Chris Mason5eda7b52007-06-22 14:16:25 -04001351 return root;
Stefan Behrens48475472013-08-23 10:34:42 +02001352 }
Chris Mason5eda7b52007-06-22 14:16:25 -04001353
David Sterba56e93572020-05-15 19:35:55 +02001354 key.objectid = objectid;
1355 key.type = BTRFS_ROOT_ITEM_KEY;
1356 key.offset = (u64)-1;
1357 root = btrfs_read_tree_root(fs_info->tree_root, &key);
Chris Mason5eda7b52007-06-22 14:16:25 -04001358 if (IS_ERR(root))
1359 return root;
Chris Mason3394e162008-11-17 20:42:26 -05001360
Miao Xiec00869f2013-09-25 21:47:44 +08001361 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04001362 ret = -ENOENT;
1363 goto fail;
1364 }
1365
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001366 ret = btrfs_init_fs_root(root, anon_dev ? *anon_dev : 0);
Miao Xiecb517ea2013-05-15 07:48:19 +00001367 if (ret)
1368 goto fail;
1369
David Sterba381cf652015-01-02 18:45:16 +01001370 path = btrfs_alloc_path();
1371 if (!path) {
1372 ret = -ENOMEM;
1373 goto fail;
1374 }
David Sterba1d4c08e2015-01-02 19:36:14 +01001375 key.objectid = BTRFS_ORPHAN_OBJECTID;
1376 key.type = BTRFS_ORPHAN_ITEM_KEY;
David Sterba56e93572020-05-15 19:35:55 +02001377 key.offset = objectid;
David Sterba1d4c08e2015-01-02 19:36:14 +01001378
1379 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
David Sterba381cf652015-01-02 18:45:16 +01001380 btrfs_free_path(path);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001381 if (ret < 0)
1382 goto fail;
1383 if (ret == 0)
Miao Xie27cdeb72014-04-02 19:51:05 +08001384 set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001385
Miao Xiecb517ea2013-05-15 07:48:19 +00001386 ret = btrfs_insert_fs_root(fs_info, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001387 if (ret) {
Jia-Ju Bai168a2f72022-03-24 06:44:54 -07001388 if (ret == -EEXIST) {
1389 btrfs_put_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001390 goto again;
Jia-Ju Bai168a2f72022-03-24 06:44:54 -07001391 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001392 goto fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001393 }
Chris Masonedbd8d42007-12-21 16:27:24 -05001394 return root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001395fail:
Filipe Manana33fab972021-12-10 19:02:18 +00001396 /*
1397 * If our caller provided us an anonymous device, then it's his
David Sterba143823c2022-05-25 16:27:25 +02001398 * responsibility to free it in case we fail. So we have to set our
Filipe Manana33fab972021-12-10 19:02:18 +00001399 * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root()
1400 * and once again by our caller.
1401 */
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001402 if (anon_dev && *anon_dev)
Filipe Manana33fab972021-12-10 19:02:18 +00001403 root->anon_dev = 0;
Josef Bacik8c389382020-02-14 16:11:42 -05001404 btrfs_put_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001405 return ERR_PTR(ret);
Chris Masonedbd8d42007-12-21 16:27:24 -05001406}
1407
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001408/*
1409 * Get in-memory reference of a root structure
1410 *
1411 * @objectid: tree objectid
1412 * @check_ref: if set, verify that the tree exists and the item has at least
1413 * one reference
1414 */
1415struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1416 u64 objectid, bool check_ref)
1417{
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001418 return btrfs_get_root_ref(fs_info, objectid, NULL, check_ref);
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001419}
1420
1421/*
1422 * Get in-memory reference of a root structure, created as new, optionally pass
1423 * the anonymous block device id
1424 *
1425 * @objectid: tree objectid
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001426 * @anon_dev: if NULL, allocate a new anonymous block device or use the
1427 * parameter value if not NULL
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001428 */
1429struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info,
Filipe Mananae2b54ea2024-02-23 16:38:43 +00001430 u64 objectid, dev_t *anon_dev)
Qu Wenruo2dfb1e42020-06-16 10:17:36 +08001431{
1432 return btrfs_get_root_ref(fs_info, objectid, anon_dev, true);
1433}
1434
Chris Mason8b712842008-06-11 16:50:36 -04001435/*
David Sterba95805032023-09-08 01:09:25 +02001436 * Return a root for the given objectid.
1437 *
Josef Bacik49d11be2020-10-19 16:02:31 -04001438 * @fs_info: the fs_info
1439 * @objectid: the objectid we need to lookup
1440 *
1441 * This is exclusively used for backref walking, and exists specifically because
1442 * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref
1443 * creation time, which means we may have to read the tree_root in order to look
1444 * up a fs root that is not in memory. If the root is not in memory we will
1445 * read the tree root commit root and look up the fs root from there. This is a
1446 * temporary root, it will not be inserted into the radix tree as it doesn't
1447 * have the most uptodate information, it'll simply be discarded once the
1448 * backref code is finished using the root.
1449 */
1450struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info,
1451 struct btrfs_path *path,
1452 u64 objectid)
1453{
1454 struct btrfs_root *root;
1455 struct btrfs_key key;
1456
1457 ASSERT(path->search_commit_root && path->skip_locking);
1458
1459 /*
1460 * This can return -ENOENT if we ask for a root that doesn't exist, but
1461 * since this is called via the backref walking code we won't be looking
1462 * up a root that doesn't exist, unless there's corruption. So if root
1463 * != NULL just return it.
1464 */
1465 root = btrfs_get_global_root(fs_info, objectid);
1466 if (root)
1467 return root;
1468
1469 root = btrfs_lookup_fs_root(fs_info, objectid);
1470 if (root)
1471 return root;
1472
1473 key.objectid = objectid;
1474 key.type = BTRFS_ROOT_ITEM_KEY;
1475 key.offset = (u64)-1;
1476 root = read_tree_root_path(fs_info->tree_root, path, &key);
1477 btrfs_release_path(path);
1478
1479 return root;
1480}
1481
Chris Masona74a4b92008-06-25 16:01:31 -04001482static int cleaner_kthread(void *arg)
1483{
Yu Zhe0d031dc2022-03-31 03:34:08 -07001484 struct btrfs_fs_info *fs_info = arg;
Miao Xied0278242013-05-14 10:20:40 +00001485 int again;
Chris Masona74a4b92008-06-25 16:01:31 -04001486
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07001487 while (1) {
Miao Xied0278242013-05-14 10:20:40 +00001488 again = 0;
David Sterba9d1a2a32013-03-12 15:13:28 +00001489
Josef Bacikfd340d02019-01-11 10:21:02 -05001490 set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1491
Miao Xied0278242013-05-14 10:20:40 +00001492 /* Make the cleaner go to sleep early. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001493 if (btrfs_need_cleaner_sleep(fs_info))
Miao Xied0278242013-05-14 10:20:40 +00001494 goto sleep;
Chris Masona74a4b92008-06-25 16:01:31 -04001495
Zygo Blaxell90c711a2016-06-12 23:39:58 -04001496 /*
1497 * Do not do anything if we might cause open_ctree() to block
1498 * before we have finished mounting the filesystem.
1499 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001500 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Zygo Blaxell90c711a2016-06-12 23:39:58 -04001501 goto sleep;
1502
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001503 if (!mutex_trylock(&fs_info->cleaner_mutex))
Miao Xied0278242013-05-14 10:20:40 +00001504 goto sleep;
1505
Miao Xiedc7f3702013-05-14 10:20:42 +00001506 /*
1507 * Avoid the problem that we change the status of the fs
1508 * during the above check and trylock.
1509 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001510 if (btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001511 mutex_unlock(&fs_info->cleaner_mutex);
Miao Xiedc7f3702013-05-14 10:20:42 +00001512 goto sleep;
Chris Masona74a4b92008-06-25 16:01:31 -04001513 }
1514
Qu Wenruob7625f42023-01-13 19:11:39 +08001515 if (test_and_clear_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags))
1516 btrfs_sysfs_feature_update(fs_info);
1517
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001518 btrfs_run_delayed_iputs(fs_info);
Filipe Mananac2d6cb12016-01-15 11:05:12 +00001519
Josef Bacik33c44182022-02-18 14:56:11 -05001520 again = btrfs_clean_one_deleted_snapshot(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001521 mutex_unlock(&fs_info->cleaner_mutex);
Miao Xied0278242013-05-14 10:20:40 +00001522
1523 /*
Miao Xie05323cd2013-05-14 10:20:41 +00001524 * The defragger has dealt with the R/O remount and umount,
1525 * needn't do anything special here.
Miao Xied0278242013-05-14 10:20:40 +00001526 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001527 btrfs_run_defrag_inodes(fs_info);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01001528
1529 /*
Johannes Thumshirnf3372062021-04-19 16:41:01 +09001530 * Acquires fs_info->reclaim_bgs_lock to avoid racing
Filipe Manana67c5e7d2015-06-11 00:58:53 +01001531 * with relocation (btrfs_relocate_chunk) and relocation
1532 * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
Johannes Thumshirnf3372062021-04-19 16:41:01 +09001533 * after acquiring fs_info->reclaim_bgs_lock. So we
Filipe Manana67c5e7d2015-06-11 00:58:53 +01001534 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
1535 * unused block groups.
1536 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001537 btrfs_delete_unused_bgs(fs_info);
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001538
1539 /*
1540 * Reclaim block groups in the reclaim_bgs list after we deleted
1541 * all unused block_groups. This possibly gives us some more free
1542 * space.
1543 */
1544 btrfs_reclaim_bgs(fs_info);
Miao Xied0278242013-05-14 10:20:40 +00001545sleep:
Filipe Mananaa0a1db72020-12-14 10:10:47 +00001546 clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07001547 if (kthread_should_park())
1548 kthread_parkme();
1549 if (kthread_should_stop())
1550 return 0;
Jiri Kosina838fe182016-03-15 11:28:54 +01001551 if (!again) {
Chris Masona74a4b92008-06-25 16:01:31 -04001552 set_current_state(TASK_INTERRUPTIBLE);
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07001553 schedule();
Chris Masona74a4b92008-06-25 16:01:31 -04001554 __set_current_state(TASK_RUNNING);
1555 }
Filipe Mananada288d22015-06-13 06:55:31 +01001556 }
Chris Masona74a4b92008-06-25 16:01:31 -04001557}
1558
1559static int transaction_kthread(void *arg)
1560{
1561 struct btrfs_root *root = arg;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001562 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masona74a4b92008-06-25 16:01:31 -04001563 struct btrfs_trans_handle *trans;
1564 struct btrfs_transaction *cur;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001565 u64 transid;
Nikolay Borisov643900b2020-10-08 15:24:29 +03001566 time64_t delta;
Chris Masona74a4b92008-06-25 16:01:31 -04001567 unsigned long delay;
Jan Kara914b2002012-03-12 16:05:50 +01001568 bool cannot_commit;
Chris Masona74a4b92008-06-25 16:01:31 -04001569
1570 do {
Jan Kara914b2002012-03-12 16:05:50 +01001571 cannot_commit = false;
Nikolay Borisovba1bc002020-10-08 15:24:27 +03001572 delay = msecs_to_jiffies(fs_info->commit_interval * 1000);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001573 mutex_lock(&fs_info->transaction_kthread_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001574
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001575 spin_lock(&fs_info->trans_lock);
1576 cur = fs_info->running_transaction;
Chris Masona74a4b92008-06-25 16:01:31 -04001577 if (!cur) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001578 spin_unlock(&fs_info->trans_lock);
Chris Masona74a4b92008-06-25 16:01:31 -04001579 goto sleep;
1580 }
Yan Zheng31153d82008-07-28 15:32:19 -04001581
Nikolay Borisov643900b2020-10-08 15:24:29 +03001582 delta = ktime_get_seconds() - cur->start_time;
Josef Bacikfdfbf022021-11-05 16:45:28 -04001583 if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) &&
Josef Bacik77d20c62023-08-24 16:59:22 -04001584 cur->state < TRANS_STATE_COMMIT_PREP &&
Nikolay Borisov643900b2020-10-08 15:24:29 +03001585 delta < fs_info->commit_interval) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001586 spin_unlock(&fs_info->trans_lock);
Nikolay Borisovfb8a7e92020-10-20 12:44:17 +03001587 delay -= msecs_to_jiffies((delta - 1) * 1000);
1588 delay = min(delay,
1589 msecs_to_jiffies(fs_info->commit_interval * 1000));
Chris Masona74a4b92008-06-25 16:01:31 -04001590 goto sleep;
1591 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001592 transid = cur->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001593 spin_unlock(&fs_info->trans_lock);
Chris Mason56bec292009-03-13 10:10:06 -04001594
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001595 /* If the file system is aborted, this will always fail. */
Miao Xie354aa0f2012-09-20 01:54:00 -06001596 trans = btrfs_attach_transaction(root);
Jan Kara914b2002012-03-12 16:05:50 +01001597 if (IS_ERR(trans)) {
Miao Xie354aa0f2012-09-20 01:54:00 -06001598 if (PTR_ERR(trans) != -ENOENT)
1599 cannot_commit = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001600 goto sleep;
Jan Kara914b2002012-03-12 16:05:50 +01001601 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001602 if (transid == trans->transid) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001603 btrfs_commit_transaction(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001604 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001605 btrfs_end_transaction(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001606 }
Chris Masona74a4b92008-06-25 16:01:31 -04001607sleep:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001608 wake_up_process(fs_info->cleaner_kthread);
1609 mutex_unlock(&fs_info->transaction_kthread_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001610
Josef Bacik84961532021-10-05 16:35:25 -04001611 if (BTRFS_FS_ERROR(fs_info))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001612 btrfs_cleanup_transaction(fs_info);
Jiri Kosinace63f892016-03-15 11:28:59 +01001613 if (!kthread_should_stop() &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001614 (!btrfs_transaction_blocked(fs_info) ||
Jiri Kosinace63f892016-03-15 11:28:59 +01001615 cannot_commit))
Nikolay Borisovbc5511d0e2018-01-23 14:46:53 +02001616 schedule_timeout_interruptible(delay);
Chris Masona74a4b92008-06-25 16:01:31 -04001617 } while (!kthread_should_stop());
1618 return 0;
1619}
1620
Chris Masonaf31f5e2011-11-03 15:17:42 -04001621/*
Nikolay Borisov01f0f9da2019-10-15 18:42:17 +03001622 * This will find the highest generation in the array of root backups. The
1623 * index of the highest array is returned, or -EINVAL if we can't find
1624 * anything.
Chris Masonaf31f5e2011-11-03 15:17:42 -04001625 *
1626 * We check to make sure the array is valid by comparing the
1627 * generation of the latest root in the array with the generation
1628 * in the super block. If they don't match we pitch it.
1629 */
Nikolay Borisov01f0f9da2019-10-15 18:42:17 +03001630static int find_newest_super_backup(struct btrfs_fs_info *info)
Chris Masonaf31f5e2011-11-03 15:17:42 -04001631{
Nikolay Borisov01f0f9da2019-10-15 18:42:17 +03001632 const u64 newest_gen = btrfs_super_generation(info->super_copy);
Chris Masonaf31f5e2011-11-03 15:17:42 -04001633 u64 cur;
Chris Masonaf31f5e2011-11-03 15:17:42 -04001634 struct btrfs_root_backup *root_backup;
1635 int i;
1636
1637 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
1638 root_backup = info->super_copy->super_roots + i;
1639 cur = btrfs_backup_tree_root_gen(root_backup);
1640 if (cur == newest_gen)
Nikolay Borisov01f0f9da2019-10-15 18:42:17 +03001641 return i;
Chris Masonaf31f5e2011-11-03 15:17:42 -04001642 }
1643
Nikolay Borisov01f0f9da2019-10-15 18:42:17 +03001644 return -EINVAL;
Chris Masonaf31f5e2011-11-03 15:17:42 -04001645}
1646
Chris Masonaf31f5e2011-11-03 15:17:42 -04001647/*
Chris Masonaf31f5e2011-11-03 15:17:42 -04001648 * copy all the root pointers into the super backup array.
1649 * this will bump the backup pointer by one when it is
1650 * done
1651 */
1652static void backup_super_roots(struct btrfs_fs_info *info)
1653{
Nikolay Borisov6ef108d2019-10-15 18:42:24 +03001654 const int next_backup = info->backup_root_index;
Chris Masonaf31f5e2011-11-03 15:17:42 -04001655 struct btrfs_root_backup *root_backup;
Chris Masonaf31f5e2011-11-03 15:17:42 -04001656
1657 root_backup = info->super_for_commit->super_roots + next_backup;
1658
1659 /*
1660 * make sure all of our padding and empty slots get zero filled
1661 * regardless of which ones we use today
1662 */
1663 memset(root_backup, 0, sizeof(*root_backup));
1664
1665 info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
1666
1667 btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
1668 btrfs_set_backup_tree_root_gen(root_backup,
1669 btrfs_header_generation(info->tree_root->node));
1670
1671 btrfs_set_backup_tree_root_level(root_backup,
1672 btrfs_header_level(info->tree_root->node));
1673
1674 btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
1675 btrfs_set_backup_chunk_root_gen(root_backup,
1676 btrfs_header_generation(info->chunk_root->node));
1677 btrfs_set_backup_chunk_root_level(root_backup,
1678 btrfs_header_level(info->chunk_root->node));
1679
Qu Wenruo1c56ab92022-08-09 13:02:18 +08001680 if (!btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE)) {
Josef Bacik9c54e802021-12-15 15:40:07 -05001681 struct btrfs_root *extent_root = btrfs_extent_root(info, 0);
Josef Bacikf7238e52021-12-15 15:40:08 -05001682 struct btrfs_root *csum_root = btrfs_csum_root(info, 0);
Josef Bacik9c54e802021-12-15 15:40:07 -05001683
1684 btrfs_set_backup_extent_root(root_backup,
1685 extent_root->node->start);
1686 btrfs_set_backup_extent_root_gen(root_backup,
1687 btrfs_header_generation(extent_root->node));
1688 btrfs_set_backup_extent_root_level(root_backup,
1689 btrfs_header_level(extent_root->node));
Josef Bacikf7238e52021-12-15 15:40:08 -05001690
1691 btrfs_set_backup_csum_root(root_backup, csum_root->node->start);
1692 btrfs_set_backup_csum_root_gen(root_backup,
1693 btrfs_header_generation(csum_root->node));
1694 btrfs_set_backup_csum_root_level(root_backup,
1695 btrfs_header_level(csum_root->node));
Josef Bacik9c54e802021-12-15 15:40:07 -05001696 }
Chris Masonaf31f5e2011-11-03 15:17:42 -04001697
Chris Mason7c7e82a2011-11-06 18:50:56 -05001698 /*
1699 * we might commit during log recovery, which happens before we set
1700 * the fs_root. Make sure it is valid before we fill it in.
1701 */
1702 if (info->fs_root && info->fs_root->node) {
1703 btrfs_set_backup_fs_root(root_backup,
1704 info->fs_root->node->start);
1705 btrfs_set_backup_fs_root_gen(root_backup,
Chris Masonaf31f5e2011-11-03 15:17:42 -04001706 btrfs_header_generation(info->fs_root->node));
Chris Mason7c7e82a2011-11-06 18:50:56 -05001707 btrfs_set_backup_fs_root_level(root_backup,
Chris Masonaf31f5e2011-11-03 15:17:42 -04001708 btrfs_header_level(info->fs_root->node));
Chris Mason7c7e82a2011-11-06 18:50:56 -05001709 }
Chris Masonaf31f5e2011-11-03 15:17:42 -04001710
1711 btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
1712 btrfs_set_backup_dev_root_gen(root_backup,
1713 btrfs_header_generation(info->dev_root->node));
1714 btrfs_set_backup_dev_root_level(root_backup,
1715 btrfs_header_level(info->dev_root->node));
1716
Chris Masonaf31f5e2011-11-03 15:17:42 -04001717 btrfs_set_backup_total_bytes(root_backup,
1718 btrfs_super_total_bytes(info->super_copy));
1719 btrfs_set_backup_bytes_used(root_backup,
1720 btrfs_super_bytes_used(info->super_copy));
1721 btrfs_set_backup_num_devices(root_backup,
1722 btrfs_super_num_devices(info->super_copy));
1723
1724 /*
1725 * if we don't copy this out to the super_copy, it won't get remembered
1726 * for the next commit
1727 */
1728 memcpy(&info->super_copy->super_roots,
1729 &info->super_for_commit->super_roots,
1730 sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
1731}
1732
1733/*
David Sterba95805032023-09-08 01:09:25 +02001734 * Reads a backup root based on the passed priority. Prio 0 is the newest, prio
1735 * 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
Nikolay Borisovbd2336b2019-10-15 18:42:19 +03001736 *
David Sterba95805032023-09-08 01:09:25 +02001737 * @fs_info: filesystem whose backup roots need to be read
1738 * @priority: priority of backup root required
Nikolay Borisovbd2336b2019-10-15 18:42:19 +03001739 *
1740 * Returns backup root index on success and -EINVAL otherwise.
1741 */
1742static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority)
1743{
1744 int backup_index = find_newest_super_backup(fs_info);
1745 struct btrfs_super_block *super = fs_info->super_copy;
1746 struct btrfs_root_backup *root_backup;
1747
1748 if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) {
1749 if (priority == 0)
1750 return backup_index;
1751
1752 backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority;
1753 backup_index %= BTRFS_NUM_BACKUP_ROOTS;
1754 } else {
1755 return -EINVAL;
1756 }
1757
1758 root_backup = super->super_roots + backup_index;
1759
1760 btrfs_set_super_generation(super,
1761 btrfs_backup_tree_root_gen(root_backup));
1762 btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
1763 btrfs_set_super_root_level(super,
1764 btrfs_backup_tree_root_level(root_backup));
1765 btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
1766
1767 /*
1768 * Fixme: the total bytes and num_devices need to match or we should
1769 * need a fsck
1770 */
1771 btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
1772 btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
1773
1774 return backup_index;
1775}
1776
Liu Bo7abadb62013-03-17 02:10:31 +00001777/* helper to cleanup workers */
1778static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
1779{
Qu Wenruodc6e3202014-02-28 10:46:14 +08001780 btrfs_destroy_workqueue(fs_info->fixup_workers);
Qu Wenruoafe3d242014-02-28 10:46:07 +08001781 btrfs_destroy_workqueue(fs_info->delalloc_workers);
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +08001782 btrfs_destroy_workqueue(fs_info->workers);
Christoph Hellwigd7b94162022-05-26 09:36:40 +02001783 if (fs_info->endio_workers)
1784 destroy_workqueue(fs_info->endio_workers);
Christoph Hellwig385de0e2022-04-18 06:43:11 +02001785 if (fs_info->rmw_workers)
1786 destroy_workqueue(fs_info->rmw_workers);
Christoph Hellwigfed8a722022-05-26 09:36:38 +02001787 if (fs_info->compressed_write_workers)
1788 destroy_workqueue(fs_info->compressed_write_workers);
Qu Wenruofccb5d82014-02-28 10:46:10 +08001789 btrfs_destroy_workqueue(fs_info->endio_write_workers);
1790 btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
Qu Wenruo5b3bc442014-02-28 10:46:15 +08001791 btrfs_destroy_workqueue(fs_info->delayed_workers);
Qu Wenruoe66f0bb2014-02-28 10:46:12 +08001792 btrfs_destroy_workqueue(fs_info->caching_workers);
Qu Wenruoa44903a2014-02-28 10:46:09 +08001793 btrfs_destroy_workqueue(fs_info->flush_workers);
Qu Wenruofc97fab2014-02-28 10:46:16 +08001794 btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
Dennis Zhoub0643e52019-12-13 16:22:14 -08001795 if (fs_info->discard_ctl.discard_workers)
1796 destroy_workqueue(fs_info->discard_ctl.discard_workers);
Filipe Mananaa9b94772017-02-04 17:12:00 +00001797 /*
1798 * Now that all other work queues are destroyed, we can safely destroy
1799 * the queues used for metadata I/O, since tasks from those other work
1800 * queues can do metadata I/O operations.
1801 */
Christoph Hellwigd7b94162022-05-26 09:36:40 +02001802 if (fs_info->endio_meta_workers)
1803 destroy_workqueue(fs_info->endio_meta_workers);
Liu Bo7abadb62013-03-17 02:10:31 +00001804}
1805
Rashika2e9f5952013-10-31 02:45:20 +05301806static void free_root_extent_buffers(struct btrfs_root *root)
1807{
1808 if (root) {
1809 free_extent_buffer(root->node);
1810 free_extent_buffer(root->commit_root);
1811 root->node = NULL;
1812 root->commit_root = NULL;
1813 }
1814}
1815
Josef Bacikabed4aa2021-11-05 16:45:51 -04001816static void free_global_root_pointers(struct btrfs_fs_info *fs_info)
1817{
1818 struct btrfs_root *root, *tmp;
1819
1820 rbtree_postorder_for_each_entry_safe(root, tmp,
1821 &fs_info->global_root_tree,
1822 rb_node)
1823 free_root_extent_buffers(root);
1824}
1825
Chris Masonaf31f5e2011-11-03 15:17:42 -04001826/* helper to cleanup tree roots */
Anand Jain4273eaf2019-10-10 10:39:25 +08001827static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
Chris Masonaf31f5e2011-11-03 15:17:42 -04001828{
Rashika2e9f5952013-10-31 02:45:20 +05301829 free_root_extent_buffers(info->tree_root);
Josef Bacik655b09f2013-05-17 14:06:51 -04001830
Josef Bacikabed4aa2021-11-05 16:45:51 -04001831 free_global_root_pointers(info);
Rashika2e9f5952013-10-31 02:45:20 +05301832 free_root_extent_buffers(info->dev_root);
Rashika2e9f5952013-10-31 02:45:20 +05301833 free_root_extent_buffers(info->quota_root);
1834 free_root_extent_buffers(info->uuid_root);
Josef Bacik8c389382020-02-14 16:11:42 -05001835 free_root_extent_buffers(info->fs_root);
Qu Wenruoaeb935a2020-05-15 14:01:42 +08001836 free_root_extent_buffers(info->data_reloc_root);
Josef Bacik9c54e802021-12-15 15:40:07 -05001837 free_root_extent_buffers(info->block_group_root);
Johannes Thumshirn51502092023-09-14 09:06:57 -07001838 free_root_extent_buffers(info->stripe_root);
Anand Jain4273eaf2019-10-10 10:39:25 +08001839 if (free_chunk_root)
Rashika2e9f5952013-10-31 02:45:20 +05301840 free_root_extent_buffers(info->chunk_root);
Chris Masonaf31f5e2011-11-03 15:17:42 -04001841}
1842
Josef Bacik8c389382020-02-14 16:11:42 -05001843void btrfs_put_root(struct btrfs_root *root)
1844{
1845 if (!root)
1846 return;
1847
1848 if (refcount_dec_and_test(&root->refs)) {
Filipe Manana310b2f52024-04-24 16:58:01 +01001849 if (WARN_ON(!xa_empty(&root->inodes)))
1850 xa_destroy(&root->inodes);
Qu Wenruo1dae7e02020-05-20 14:58:51 +08001851 WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
Josef Bacik8c389382020-02-14 16:11:42 -05001852 if (root->anon_dev)
1853 free_anon_bdev(root->anon_dev);
Johannes Thumshirn923eb522020-06-23 17:40:07 +09001854 free_root_extent_buffers(root);
Josef Bacik8c389382020-02-14 16:11:42 -05001855#ifdef CONFIG_BTRFS_DEBUG
David Sterbafc7cbcd2022-07-15 13:59:21 +02001856 spin_lock(&root->fs_info->fs_roots_radix_lock);
Josef Bacik8c389382020-02-14 16:11:42 -05001857 list_del_init(&root->leak_list);
David Sterbafc7cbcd2022-07-15 13:59:21 +02001858 spin_unlock(&root->fs_info->fs_roots_radix_lock);
Josef Bacik8c389382020-02-14 16:11:42 -05001859#endif
1860 kfree(root);
1861 }
1862}
1863
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001864void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
Josef Bacik171f6532013-04-24 16:35:41 -04001865{
David Sterbafc7cbcd2022-07-15 13:59:21 +02001866 int ret;
1867 struct btrfs_root *gang[8];
1868 int i;
Josef Bacik171f6532013-04-24 16:35:41 -04001869
1870 while (!list_empty(&fs_info->dead_roots)) {
David Sterbafc7cbcd2022-07-15 13:59:21 +02001871 gang[0] = list_entry(fs_info->dead_roots.next,
1872 struct btrfs_root, root_list);
1873 list_del(&gang[0]->root_list);
Josef Bacik171f6532013-04-24 16:35:41 -04001874
David Sterbafc7cbcd2022-07-15 13:59:21 +02001875 if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state))
1876 btrfs_drop_and_free_fs_root(fs_info, gang[0]);
1877 btrfs_put_root(gang[0]);
Josef Bacik171f6532013-04-24 16:35:41 -04001878 }
1879
David Sterbafc7cbcd2022-07-15 13:59:21 +02001880 while (1) {
1881 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1882 (void **)gang, 0,
1883 ARRAY_SIZE(gang));
1884 if (!ret)
1885 break;
1886 for (i = 0; i < ret; i++)
1887 btrfs_drop_and_free_fs_root(fs_info, gang[i]);
Josef Bacik171f6532013-04-24 16:35:41 -04001888 }
1889}
Chris Masonaf31f5e2011-11-03 15:17:42 -04001890
Eric Sandeen638aa7e2014-08-01 18:12:38 -05001891static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
1892{
1893 mutex_init(&fs_info->scrub_lock);
1894 atomic_set(&fs_info->scrubs_running, 0);
1895 atomic_set(&fs_info->scrub_pause_req, 0);
1896 atomic_set(&fs_info->scrubs_paused, 0);
1897 atomic_set(&fs_info->scrub_cancel_req, 0);
1898 init_waitqueue_head(&fs_info->scrub_pause_wait);
Anand Jainff09c4c2019-01-30 14:45:02 +08001899 refcount_set(&fs_info->scrub_workers_refcnt, 0);
Eric Sandeen638aa7e2014-08-01 18:12:38 -05001900}
1901
Eric Sandeen779a65a2014-08-01 18:12:39 -05001902static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
1903{
1904 spin_lock_init(&fs_info->balance_lock);
1905 mutex_init(&fs_info->balance_mutex);
Eric Sandeen779a65a2014-08-01 18:12:39 -05001906 atomic_set(&fs_info->balance_pause_req, 0);
1907 atomic_set(&fs_info->balance_cancel_req, 0);
1908 fs_info->balance_ctl = NULL;
1909 init_waitqueue_head(&fs_info->balance_wait_q);
David Sterba907d2712021-05-18 00:37:36 +02001910 atomic_set(&fs_info->reloc_cancel_req, 0);
Eric Sandeen779a65a2014-08-01 18:12:39 -05001911}
1912
Christoph Hellwigdcb21372023-02-19 19:10:22 +01001913static int btrfs_init_btree_inode(struct super_block *sb)
Eric Sandeenf37938e2014-08-01 18:12:40 -05001914{
Christoph Hellwigdcb21372023-02-19 19:10:22 +01001915 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Josef Bacike2569272022-09-14 19:04:49 -04001916 unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
1917 fs_info->tree_root);
Christoph Hellwigdcb21372023-02-19 19:10:22 +01001918 struct inode *inode;
1919
1920 inode = new_inode(sb);
1921 if (!inode)
1922 return -ENOMEM;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001923
Filipe Manana7a7bc212024-05-05 13:47:02 +01001924 btrfs_set_inode_number(BTRFS_I(inode), BTRFS_BTREE_INODE_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001925 set_nlink(inode, 1);
Eric Sandeenf37938e2014-08-01 18:12:40 -05001926 /*
1927 * we set the i_size on the btree inode to the max possible int.
1928 * the real end of the address space is determined by all of
1929 * the devices in the system
1930 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001931 inode->i_size = OFFSET_MAX;
1932 inode->i_mapping->a_ops = &btree_aops;
Christoph Hellwigdcb21372023-02-19 19:10:22 +01001933 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Eric Sandeenf37938e2014-08-01 18:12:40 -05001934
Qu Wenruo43eb5f22019-03-01 10:47:59 +08001935 extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
David Sterba35da5a72022-10-28 02:47:06 +02001936 IO_TREE_BTREE_INODE_IO);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001937 extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
Eric Sandeenf37938e2014-08-01 18:12:40 -05001938
Josef Bacik5c8fd992020-02-14 16:11:43 -05001939 BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001940 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Josef Bacike2569272022-09-14 19:04:49 -04001941 __insert_inode_hash(inode, hash);
Christoph Hellwigdcb21372023-02-19 19:10:22 +01001942 fs_info->btree_inode = inode;
1943
1944 return 0;
Eric Sandeenf37938e2014-08-01 18:12:40 -05001945}
1946
Eric Sandeenad618362014-08-01 18:12:41 -05001947static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
1948{
Eric Sandeenad618362014-08-01 18:12:41 -05001949 mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
David Sterba129827e2018-04-05 01:29:24 +02001950 init_rwsem(&fs_info->dev_replace.rwsem);
David Sterba7f8d2362018-04-05 01:04:49 +02001951 init_waitqueue_head(&fs_info->dev_replace.replace_wait);
Eric Sandeenad618362014-08-01 18:12:41 -05001952}
1953
Eric Sandeenf9e92e42014-08-01 18:12:42 -05001954static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
1955{
1956 spin_lock_init(&fs_info->qgroup_lock);
1957 mutex_init(&fs_info->qgroup_ioctl_lock);
1958 fs_info->qgroup_tree = RB_ROOT;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05001959 INIT_LIST_HEAD(&fs_info->dirty_qgroups);
1960 fs_info->qgroup_seq = 1;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05001961 fs_info->qgroup_ulist = NULL;
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04001962 fs_info->qgroup_rescan_running = false;
Qu Wenruo011b46c2022-08-24 09:14:09 +08001963 fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05001964 mutex_init(&fs_info->qgroup_rescan_lock);
1965}
1966
Su Yued21deec2021-11-10 14:42:17 +08001967static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
Eric Sandeen2a458192015-02-16 16:29:26 +01001968{
Anand Jainf7b885b2018-02-13 17:50:42 +08001969 u32 max_active = fs_info->thread_pool_size;
David Sterba6f011052015-02-16 18:34:01 +01001970 unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
Tejun Heo58e814f2023-05-25 13:33:08 -10001971 unsigned int ordered_flags = WQ_MEM_RECLAIM | WQ_FREEZABLE;
Eric Sandeen2a458192015-02-16 16:29:26 +01001972
1973 fs_info->workers =
Christoph Hellwiga31b4a42022-04-18 06:43:09 +02001974 btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16);
Eric Sandeen2a458192015-02-16 16:29:26 +01001975
1976 fs_info->delalloc_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04001977 btrfs_alloc_workqueue(fs_info, "delalloc",
1978 flags, max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01001979
1980 fs_info->flush_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04001981 btrfs_alloc_workqueue(fs_info, "flush_delalloc",
1982 flags, max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01001983
1984 fs_info->caching_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04001985 btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01001986
Eric Sandeen2a458192015-02-16 16:29:26 +01001987 fs_info->fixup_workers =
Tejun Heo58e814f2023-05-25 13:33:08 -10001988 btrfs_alloc_ordered_workqueue(fs_info, "fixup", ordered_flags);
Eric Sandeen2a458192015-02-16 16:29:26 +01001989
Eric Sandeen2a458192015-02-16 16:29:26 +01001990 fs_info->endio_workers =
Christoph Hellwigd7b94162022-05-26 09:36:40 +02001991 alloc_workqueue("btrfs-endio", flags, max_active);
Eric Sandeen2a458192015-02-16 16:29:26 +01001992 fs_info->endio_meta_workers =
Christoph Hellwigd7b94162022-05-26 09:36:40 +02001993 alloc_workqueue("btrfs-endio-meta", flags, max_active);
Christoph Hellwig385de0e2022-04-18 06:43:11 +02001994 fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);
Eric Sandeen2a458192015-02-16 16:29:26 +01001995 fs_info->endio_write_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04001996 btrfs_alloc_workqueue(fs_info, "endio-write", flags,
1997 max_active, 2);
Christoph Hellwigfed8a722022-05-26 09:36:38 +02001998 fs_info->compressed_write_workers =
1999 alloc_workqueue("btrfs-compressed-write", flags, max_active);
Eric Sandeen2a458192015-02-16 16:29:26 +01002000 fs_info->endio_freespace_worker =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002001 btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
2002 max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002003 fs_info->delayed_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002004 btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
2005 max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002006 fs_info->qgroup_rescan_workers =
Tejun Heo58e814f2023-05-25 13:33:08 -10002007 btrfs_alloc_ordered_workqueue(fs_info, "qgroup-rescan",
2008 ordered_flags);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002009 fs_info->discard_ctl.discard_workers =
Tejun Heo58e814f2023-05-25 13:33:08 -10002010 alloc_ordered_workqueue("btrfs_discard", WQ_FREEZABLE);
Eric Sandeen2a458192015-02-16 16:29:26 +01002011
Christoph Hellwig8bfec2e2023-05-03 09:06:15 +02002012 if (!(fs_info->workers &&
Christoph Hellwiga31b4a42022-04-18 06:43:09 +02002013 fs_info->delalloc_workers && fs_info->flush_workers &&
Eric Sandeen2a458192015-02-16 16:29:26 +01002014 fs_info->endio_workers && fs_info->endio_meta_workers &&
Christoph Hellwigfed8a722022-05-26 09:36:38 +02002015 fs_info->compressed_write_workers &&
Qu Wenruo1a1a28512022-11-01 19:16:12 +08002016 fs_info->endio_write_workers &&
Eric Sandeen2a458192015-02-16 16:29:26 +01002017 fs_info->endio_freespace_worker && fs_info->rmw_workers &&
Qu Wenruof26c9232021-12-14 21:01:45 +08002018 fs_info->caching_workers && fs_info->fixup_workers &&
2019 fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
Dennis Zhoub0643e52019-12-13 16:22:14 -08002020 fs_info->discard_ctl.discard_workers)) {
Eric Sandeen2a458192015-02-16 16:29:26 +01002021 return -ENOMEM;
2022 }
2023
2024 return 0;
2025}
2026
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002027static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
2028{
2029 struct crypto_shash *csum_shash;
David Sterbab4e967b2019-10-08 18:41:33 +02002030 const char *csum_driver = btrfs_super_csum_driver(csum_type);
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002031
David Sterbab4e967b2019-10-08 18:41:33 +02002032 csum_shash = crypto_alloc_shash(csum_driver, 0, 0);
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002033
2034 if (IS_ERR(csum_shash)) {
2035 btrfs_err(fs_info, "error allocating %s hash for checksum",
David Sterbab4e967b2019-10-08 18:41:33 +02002036 csum_driver);
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002037 return PTR_ERR(csum_shash);
2038 }
2039
2040 fs_info->csum_shash = csum_shash;
2041
Christoph Hellwig68d99ab2023-03-29 09:13:05 +09002042 /*
2043 * Check if the checksum implementation is a fast accelerated one.
2044 * As-is this is a bit of a hack and should be replaced once the csum
2045 * implementations provide that information themselves.
2046 */
2047 switch (csum_type) {
2048 case BTRFS_CSUM_TYPE_CRC32:
2049 if (!strstr(crypto_shash_driver_name(csum_shash), "generic"))
2050 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
2051 break;
David Sterbaefcfcbc2023-04-04 00:06:02 +02002052 case BTRFS_CSUM_TYPE_XXHASH:
2053 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
2054 break;
Christoph Hellwig68d99ab2023-03-29 09:13:05 +09002055 default:
2056 break;
2057 }
2058
David Sterbac8a5f8c2022-06-22 20:45:18 +02002059 btrfs_info(fs_info, "using %s (%s) checksum algorithm",
2060 btrfs_super_csum_name(csum_type),
2061 crypto_shash_driver_name(csum_shash));
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002062 return 0;
2063}
2064
Eric Sandeen63443bf2014-08-01 18:12:46 -05002065static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2066 struct btrfs_fs_devices *fs_devices)
2067{
2068 int ret;
Qu Wenruo789d6a32022-09-14 13:32:50 +08002069 struct btrfs_tree_parent_check check = { 0 };
Eric Sandeen63443bf2014-08-01 18:12:46 -05002070 struct btrfs_root *log_tree_root;
2071 struct btrfs_super_block *disk_super = fs_info->super_copy;
2072 u64 bytenr = btrfs_super_log_root(disk_super);
Qu Wenruo581c1762018-03-29 09:08:11 +08002073 int level = btrfs_super_log_root_level(disk_super);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002074
2075 if (fs_devices->rw_devices == 0) {
David Sterbaf14d1042015-10-08 11:37:06 +02002076 btrfs_warn(fs_info, "log replay required on RO media");
Eric Sandeen63443bf2014-08-01 18:12:46 -05002077 return -EIO;
2078 }
2079
Josef Bacik96dfcb42020-01-24 09:32:18 -05002080 log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID,
2081 GFP_KERNEL);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002082 if (!log_tree_root)
2083 return -ENOMEM;
2084
Qu Wenruo789d6a32022-09-14 13:32:50 +08002085 check.level = level;
2086 check.transid = fs_info->generation + 1;
2087 check.owner_root = BTRFS_TREE_LOG_OBJECTID;
2088 log_tree_root->node = read_tree_block(fs_info, bytenr, &check);
Liu Bo64c043d2015-05-25 17:30:15 +08002089 if (IS_ERR(log_tree_root->node)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002090 btrfs_warn(fs_info, "failed to read log tree");
Liu Bo0eeff232015-06-11 14:16:44 +08002091 ret = PTR_ERR(log_tree_root->node);
Josef Bacik8c389382020-02-14 16:11:42 -05002092 log_tree_root->node = NULL;
Josef Bacik00246522020-01-24 09:33:01 -05002093 btrfs_put_root(log_tree_root);
Liu Bo0eeff232015-06-11 14:16:44 +08002094 return ret;
Qu Wenruo4eb150d2022-02-22 15:41:19 +08002095 }
2096 if (!extent_buffer_uptodate(log_tree_root->node)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002097 btrfs_err(fs_info, "failed to read log tree");
Josef Bacik00246522020-01-24 09:33:01 -05002098 btrfs_put_root(log_tree_root);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002099 return -EIO;
2100 }
Qu Wenruo4eb150d2022-02-22 15:41:19 +08002101
Eric Sandeen63443bf2014-08-01 18:12:46 -05002102 /* returns with log_tree_root freed on success */
2103 ret = btrfs_recover_log_trees(log_tree_root);
2104 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002105 btrfs_handle_fs_error(fs_info, ret,
2106 "Failed to recover log tree");
Josef Bacik00246522020-01-24 09:33:01 -05002107 btrfs_put_root(log_tree_root);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002108 return ret;
2109 }
2110
David Howellsbc98a422017-07-17 08:45:34 +01002111 if (sb_rdonly(fs_info->sb)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002112 ret = btrfs_commit_super(fs_info);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002113 if (ret)
2114 return ret;
2115 }
2116
2117 return 0;
2118}
2119
Josef Bacikabed4aa2021-11-05 16:45:51 -04002120static int load_global_roots_objectid(struct btrfs_root *tree_root,
2121 struct btrfs_path *path, u64 objectid,
2122 const char *name)
2123{
2124 struct btrfs_fs_info *fs_info = tree_root->fs_info;
2125 struct btrfs_root *root;
Josef Bacikf7238e52021-12-15 15:40:08 -05002126 u64 max_global_id = 0;
Josef Bacikabed4aa2021-11-05 16:45:51 -04002127 int ret;
2128 struct btrfs_key key = {
2129 .objectid = objectid,
2130 .type = BTRFS_ROOT_ITEM_KEY,
2131 .offset = 0,
2132 };
2133 bool found = false;
2134
2135 /* If we have IGNOREDATACSUMS skip loading these roots. */
2136 if (objectid == BTRFS_CSUM_TREE_OBJECTID &&
2137 btrfs_test_opt(fs_info, IGNOREDATACSUMS)) {
Qu Wenruo169aaaf2024-06-14 13:52:30 +09302138 set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
Josef Bacikabed4aa2021-11-05 16:45:51 -04002139 return 0;
2140 }
2141
2142 while (1) {
2143 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2144 if (ret < 0)
2145 break;
2146
2147 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2148 ret = btrfs_next_leaf(tree_root, path);
2149 if (ret) {
2150 if (ret > 0)
2151 ret = 0;
2152 break;
2153 }
2154 }
2155 ret = 0;
2156
2157 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2158 if (key.objectid != objectid)
2159 break;
2160 btrfs_release_path(path);
2161
Josef Bacikf7238e52021-12-15 15:40:08 -05002162 /*
2163 * Just worry about this for extent tree, it'll be the same for
2164 * everybody.
2165 */
2166 if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2167 max_global_id = max(max_global_id, key.offset);
2168
Josef Bacikabed4aa2021-11-05 16:45:51 -04002169 found = true;
2170 root = read_tree_root_path(tree_root, path, &key);
2171 if (IS_ERR(root)) {
2172 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2173 ret = PTR_ERR(root);
2174 break;
2175 }
2176 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2177 ret = btrfs_global_root_insert(root);
2178 if (ret) {
2179 btrfs_put_root(root);
2180 break;
2181 }
2182 key.offset++;
2183 }
2184 btrfs_release_path(path);
2185
Josef Bacikf7238e52021-12-15 15:40:08 -05002186 if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2187 fs_info->nr_global_roots = max_global_id + 1;
2188
Josef Bacikabed4aa2021-11-05 16:45:51 -04002189 if (!found || ret) {
2190 if (objectid == BTRFS_CSUM_TREE_OBJECTID)
Qu Wenruo169aaaf2024-06-14 13:52:30 +09302191 set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
Josef Bacikabed4aa2021-11-05 16:45:51 -04002192
2193 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2194 ret = ret ? ret : -ENOENT;
2195 else
2196 ret = 0;
2197 btrfs_err(fs_info, "failed to load root %s", name);
2198 }
2199 return ret;
2200}
2201
2202static int load_global_roots(struct btrfs_root *tree_root)
2203{
2204 struct btrfs_path *path;
2205 int ret = 0;
2206
2207 path = btrfs_alloc_path();
2208 if (!path)
2209 return -ENOMEM;
2210
2211 ret = load_global_roots_objectid(tree_root, path,
2212 BTRFS_EXTENT_TREE_OBJECTID, "extent");
2213 if (ret)
2214 goto out;
2215 ret = load_global_roots_objectid(tree_root, path,
2216 BTRFS_CSUM_TREE_OBJECTID, "csum");
2217 if (ret)
2218 goto out;
2219 if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE))
2220 goto out;
2221 ret = load_global_roots_objectid(tree_root, path,
2222 BTRFS_FREE_SPACE_TREE_OBJECTID,
2223 "free space");
2224out:
2225 btrfs_free_path(path);
2226 return ret;
2227}
2228
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002229static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002230{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002231 struct btrfs_root *tree_root = fs_info->tree_root;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002232 struct btrfs_root *root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002233 struct btrfs_key location;
2234 int ret;
2235
David Sterba2467d0f2024-01-24 01:09:46 +01002236 ASSERT(fs_info->tree_root);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002237
Josef Bacikabed4aa2021-11-05 16:45:51 -04002238 ret = load_global_roots(tree_root);
2239 if (ret)
2240 return ret;
2241
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002242 location.type = BTRFS_ROOT_ITEM_KEY;
2243 location.offset = 0;
2244
Qu Wenruo1c56ab92022-08-09 13:02:18 +08002245 if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) {
Qu Wenruo14033b02022-08-09 13:02:17 +08002246 location.objectid = BTRFS_BLOCK_GROUP_TREE_OBJECTID;
2247 root = btrfs_read_tree_root(tree_root, &location);
2248 if (IS_ERR(root)) {
2249 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2250 ret = PTR_ERR(root);
2251 goto out;
2252 }
2253 } else {
2254 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2255 fs_info->block_group_root = root;
2256 }
2257 }
2258
2259 location.objectid = BTRFS_DEV_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002260 root = btrfs_read_tree_root(tree_root, &location);
Liu Bof50f4352018-03-29 06:11:45 +08002261 if (IS_ERR(root)) {
Josef Bacik42437a62020-10-16 11:29:18 -04002262 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2263 ret = PTR_ERR(root);
2264 goto out;
2265 }
2266 } else {
2267 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
Josef Bacik42437a62020-10-16 11:29:18 -04002268 fs_info->dev_root = root;
Liu Bof50f4352018-03-29 06:11:45 +08002269 }
Josef Bacik820a49d2021-03-11 11:23:14 -05002270 /* Initialize fs_info for all devices in any case */
Johannes Thumshirna8d1b162022-11-04 07:12:34 -07002271 ret = btrfs_init_devices_late(fs_info);
2272 if (ret)
2273 goto out;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002274
Qu Wenruoaeb935a2020-05-15 14:01:42 +08002275 /*
2276 * This tree can share blocks with some other fs tree during relocation
2277 * and we need a proper setup by btrfs_get_fs_root
2278 */
David Sterba56e93572020-05-15 19:35:55 +02002279 root = btrfs_get_fs_root(tree_root->fs_info,
2280 BTRFS_DATA_RELOC_TREE_OBJECTID, true);
Qu Wenruoaeb935a2020-05-15 14:01:42 +08002281 if (IS_ERR(root)) {
Josef Bacik42437a62020-10-16 11:29:18 -04002282 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2283 ret = PTR_ERR(root);
2284 goto out;
2285 }
2286 } else {
2287 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2288 fs_info->data_reloc_root = root;
Qu Wenruoaeb935a2020-05-15 14:01:42 +08002289 }
Qu Wenruoaeb935a2020-05-15 14:01:42 +08002290
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002291 location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002292 root = btrfs_read_tree_root(tree_root, &location);
2293 if (!IS_ERR(root)) {
2294 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002295 fs_info->quota_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002296 }
2297
2298 location.objectid = BTRFS_UUID_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002299 root = btrfs_read_tree_root(tree_root, &location);
2300 if (IS_ERR(root)) {
Josef Bacik42437a62020-10-16 11:29:18 -04002301 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2302 ret = PTR_ERR(root);
2303 if (ret != -ENOENT)
2304 goto out;
2305 }
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002306 } else {
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002307 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2308 fs_info->uuid_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002309 }
2310
Johannes Thumshirn51502092023-09-14 09:06:57 -07002311 if (btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE)) {
2312 location.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID;
2313 root = btrfs_read_tree_root(tree_root, &location);
2314 if (IS_ERR(root)) {
2315 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2316 ret = PTR_ERR(root);
2317 goto out;
2318 }
2319 } else {
2320 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2321 fs_info->stripe_root = root;
2322 }
2323 }
2324
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002325 return 0;
Liu Bof50f4352018-03-29 06:11:45 +08002326out:
2327 btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
2328 location.objectid, ret);
2329 return ret;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002330}
2331
Qu Wenruo069ec952018-05-11 13:35:26 +08002332/*
2333 * Real super block validation
2334 * NOTE: super csum type and incompat features will not be checked here.
2335 *
2336 * @sb: super block to check
2337 * @mirror_num: the super block number to check its bytenr:
2338 * 0 the primary (1st) sb
2339 * 1, 2 2nd and 3rd backup copy
2340 * -1 skip bytenr check
2341 */
David Sterba2917f742024-05-30 19:14:12 +02002342int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
2343 const struct btrfs_super_block *sb, int mirror_num)
Qu Wenruo21a852b2018-05-11 13:35:25 +08002344{
Qu Wenruo21a852b2018-05-11 13:35:25 +08002345 u64 nodesize = btrfs_super_nodesize(sb);
2346 u64 sectorsize = btrfs_super_sectorsize(sb);
2347 int ret = 0;
Qu Wenruo32e62162024-06-14 13:52:31 +09302348 const bool ignore_flags = btrfs_test_opt(fs_info, IGNORESUPERFLAGS);
Qu Wenruo21a852b2018-05-11 13:35:25 +08002349
2350 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
2351 btrfs_err(fs_info, "no valid FS found");
2352 ret = -EINVAL;
2353 }
Qu Wenruo32e62162024-06-14 13:52:31 +09302354 if ((btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP)) {
2355 if (!ignore_flags) {
2356 btrfs_err(fs_info,
2357 "unrecognized or unsupported super flag 0x%llx",
2358 btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
2359 ret = -EINVAL;
2360 } else {
2361 btrfs_info(fs_info,
2362 "unrecognized or unsupported super flags: 0x%llx, ignored",
2363 btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
2364 }
Qu Wenruo21a852b2018-05-11 13:35:25 +08002365 }
2366 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
2367 btrfs_err(fs_info, "tree_root level too big: %d >= %d",
2368 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
2369 ret = -EINVAL;
2370 }
2371 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
2372 btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
2373 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
2374 ret = -EINVAL;
2375 }
2376 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
2377 btrfs_err(fs_info, "log_root level too big: %d >= %d",
2378 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
2379 ret = -EINVAL;
2380 }
2381
2382 /*
2383 * Check sectorsize and nodesize first, other check will need it.
2384 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
2385 */
2386 if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
2387 sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
2388 btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
2389 ret = -EINVAL;
2390 }
Qu Wenruo0bb3eb32021-01-26 16:34:02 +08002391
2392 /*
Qu Wenruo1a42daab2022-01-13 13:22:10 +08002393 * We only support at most two sectorsizes: 4K and PAGE_SIZE.
2394 *
2395 * We can support 16K sectorsize with 64K page size without problem,
2396 * but such sectorsize/pagesize combination doesn't make much sense.
2397 * 4K will be our future standard, PAGE_SIZE is supported from the very
2398 * beginning.
Qu Wenruo0bb3eb32021-01-26 16:34:02 +08002399 */
Qu Wenruo1a42daab2022-01-13 13:22:10 +08002400 if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) {
Qu Wenruo21a852b2018-05-11 13:35:25 +08002401 btrfs_err(fs_info,
Qu Wenruo0bb3eb32021-01-26 16:34:02 +08002402 "sectorsize %llu not yet supported for page size %lu",
Qu Wenruo21a852b2018-05-11 13:35:25 +08002403 sectorsize, PAGE_SIZE);
2404 ret = -EINVAL;
2405 }
Qu Wenruo0bb3eb32021-01-26 16:34:02 +08002406
Qu Wenruo21a852b2018-05-11 13:35:25 +08002407 if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
2408 nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
2409 btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
2410 ret = -EINVAL;
2411 }
2412 if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
2413 btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
2414 le32_to_cpu(sb->__unused_leafsize), nodesize);
2415 ret = -EINVAL;
2416 }
2417
2418 /* Root alignment check */
2419 if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
2420 btrfs_warn(fs_info, "tree_root block unaligned: %llu",
2421 btrfs_super_root(sb));
2422 ret = -EINVAL;
2423 }
2424 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
2425 btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
2426 btrfs_super_chunk_root(sb));
2427 ret = -EINVAL;
2428 }
2429 if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
2430 btrfs_warn(fs_info, "log_root block unaligned: %llu",
2431 btrfs_super_log_root(sb));
2432 ret = -EINVAL;
2433 }
2434
Anand Jaina5b8a5f2023-09-28 09:09:47 +08002435 if (!fs_info->fs_devices->temp_fsid &&
2436 memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
Nikolay Borisovaefd7f72021-05-31 12:26:01 +03002437 btrfs_err(fs_info,
2438 "superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
Anand Jaind167aa72023-07-31 19:16:34 +08002439 sb->fsid, fs_info->fs_devices->fsid);
Nikolay Borisovaefd7f72021-05-31 12:26:01 +03002440 ret = -EINVAL;
2441 }
2442
Anand Jain6bfe39592023-07-31 19:16:35 +08002443 if (memcmp(fs_info->fs_devices->metadata_uuid, btrfs_sb_fsid_ptr(sb),
2444 BTRFS_FSID_SIZE) != 0) {
Nikolay Borisovaefd7f72021-05-31 12:26:01 +03002445 btrfs_err(fs_info,
2446"superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU",
Anand Jain6bfe39592023-07-31 19:16:35 +08002447 btrfs_sb_fsid_ptr(sb), fs_info->fs_devices->metadata_uuid);
Nikolay Borisovaefd7f72021-05-31 12:26:01 +03002448 ret = -EINVAL;
2449 }
2450
Anand Jain25984a52023-05-24 20:02:42 +08002451 if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid,
2452 BTRFS_FSID_SIZE) != 0) {
2453 btrfs_err(fs_info,
2454 "dev_item UUID does not match metadata fsid: %pU != %pU",
2455 fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid);
2456 ret = -EINVAL;
2457 }
2458
Qu Wenruo1c56ab92022-08-09 13:02:18 +08002459 /*
2460 * Artificial requirement for block-group-tree to force newer features
2461 * (free-space-tree, no-holes) so the test matrix is smaller.
2462 */
2463 if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
2464 (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) ||
2465 !btrfs_fs_incompat(fs_info, NO_HOLES))) {
2466 btrfs_err(fs_info,
Mark Harmstone0102ab52024-06-20 16:04:51 +01002467 "block-group-tree feature requires free-space-tree and no-holes");
Qu Wenruo1c56ab92022-08-09 13:02:18 +08002468 ret = -EINVAL;
2469 }
2470
Qu Wenruo21a852b2018-05-11 13:35:25 +08002471 /*
2472 * Hint to catch really bogus numbers, bitflips or so, more exact checks are
2473 * done later
2474 */
2475 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
2476 btrfs_err(fs_info, "bytes_used is too small %llu",
2477 btrfs_super_bytes_used(sb));
2478 ret = -EINVAL;
2479 }
2480 if (!is_power_of_2(btrfs_super_stripesize(sb))) {
2481 btrfs_err(fs_info, "invalid stripesize %u",
2482 btrfs_super_stripesize(sb));
2483 ret = -EINVAL;
2484 }
2485 if (btrfs_super_num_devices(sb) > (1UL << 31))
2486 btrfs_warn(fs_info, "suspicious number of devices: %llu",
2487 btrfs_super_num_devices(sb));
2488 if (btrfs_super_num_devices(sb) == 0) {
2489 btrfs_err(fs_info, "number of devices is 0");
2490 ret = -EINVAL;
2491 }
2492
Qu Wenruo069ec952018-05-11 13:35:26 +08002493 if (mirror_num >= 0 &&
2494 btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
Qu Wenruo21a852b2018-05-11 13:35:25 +08002495 btrfs_err(fs_info, "super offset mismatch %llu != %u",
2496 btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
2497 ret = -EINVAL;
2498 }
2499
2500 /*
2501 * Obvious sys_chunk_array corruptions, it must hold at least one key
2502 * and one chunk
2503 */
2504 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
2505 btrfs_err(fs_info, "system chunk array too big %u > %u",
2506 btrfs_super_sys_array_size(sb),
2507 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
2508 ret = -EINVAL;
2509 }
2510 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
2511 + sizeof(struct btrfs_chunk)) {
2512 btrfs_err(fs_info, "system chunk array too small %u < %zu",
2513 btrfs_super_sys_array_size(sb),
2514 sizeof(struct btrfs_disk_key)
2515 + sizeof(struct btrfs_chunk));
2516 ret = -EINVAL;
2517 }
2518
2519 /*
2520 * The generation is a global counter, we'll trust it more than the others
2521 * but it's still possible that it's the one that's wrong.
2522 */
2523 if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
2524 btrfs_warn(fs_info,
2525 "suspicious: generation < chunk_root_generation: %llu < %llu",
2526 btrfs_super_generation(sb),
2527 btrfs_super_chunk_root_generation(sb));
2528 if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
2529 && btrfs_super_cache_generation(sb) != (u64)-1)
2530 btrfs_warn(fs_info,
2531 "suspicious: generation < cache_generation: %llu < %llu",
2532 btrfs_super_generation(sb),
2533 btrfs_super_cache_generation(sb));
2534
2535 return ret;
2536}
2537
Qu Wenruo069ec952018-05-11 13:35:26 +08002538/*
2539 * Validation of super block at mount time.
2540 * Some checks already done early at mount time, like csum type and incompat
2541 * flags will be skipped.
2542 */
2543static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
2544{
Qu Wenruoa05d3c92022-08-24 20:16:22 +08002545 return btrfs_validate_super(fs_info, fs_info->super_copy, 0);
Qu Wenruo069ec952018-05-11 13:35:26 +08002546}
2547
Qu Wenruo75cb8572018-05-11 13:35:27 +08002548/*
2549 * Validation of super block at write time.
2550 * Some checks like bytenr check will be skipped as their values will be
2551 * overwritten soon.
2552 * Extra checks like csum type and incompat flags will be done here.
2553 */
2554static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
2555 struct btrfs_super_block *sb)
2556{
2557 int ret;
2558
Qu Wenruoa05d3c92022-08-24 20:16:22 +08002559 ret = btrfs_validate_super(fs_info, sb, -1);
Qu Wenruo75cb8572018-05-11 13:35:27 +08002560 if (ret < 0)
2561 goto out;
Johannes Thumshirne7e16f42019-06-03 16:58:53 +02002562 if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
Qu Wenruo75cb8572018-05-11 13:35:27 +08002563 ret = -EUCLEAN;
2564 btrfs_err(fs_info, "invalid csum type, has %u want %u",
2565 btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
2566 goto out;
2567 }
2568 if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
2569 ret = -EUCLEAN;
2570 btrfs_err(fs_info,
2571 "invalid incompat flags, has 0x%llx valid mask 0x%llx",
2572 btrfs_super_incompat_flags(sb),
2573 (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
2574 goto out;
2575 }
2576out:
2577 if (ret < 0)
2578 btrfs_err(fs_info,
2579 "super block corruption detected before writing it to disk");
2580 return ret;
2581}
2582
Josef Bacikbd676442021-12-15 15:40:06 -05002583static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level)
2584{
Qu Wenruo789d6a32022-09-14 13:32:50 +08002585 struct btrfs_tree_parent_check check = {
2586 .level = level,
2587 .transid = gen,
Josef Bacike094f482024-04-15 16:16:23 -04002588 .owner_root = btrfs_root_id(root)
Qu Wenruo789d6a32022-09-14 13:32:50 +08002589 };
Josef Bacikbd676442021-12-15 15:40:06 -05002590 int ret = 0;
2591
Qu Wenruo789d6a32022-09-14 13:32:50 +08002592 root->node = read_tree_block(root->fs_info, bytenr, &check);
Josef Bacikbd676442021-12-15 15:40:06 -05002593 if (IS_ERR(root->node)) {
2594 ret = PTR_ERR(root->node);
2595 root->node = NULL;
Qu Wenruo4eb150d2022-02-22 15:41:19 +08002596 return ret;
2597 }
2598 if (!extent_buffer_uptodate(root->node)) {
Josef Bacikbd676442021-12-15 15:40:06 -05002599 free_extent_buffer(root->node);
2600 root->node = NULL;
Qu Wenruo4eb150d2022-02-22 15:41:19 +08002601 return -EIO;
Josef Bacikbd676442021-12-15 15:40:06 -05002602 }
2603
Josef Bacikbd676442021-12-15 15:40:06 -05002604 btrfs_set_root_node(&root->root_item, root->node);
2605 root->commit_root = btrfs_root_node(root);
2606 btrfs_set_root_refs(&root->root_item, 1);
2607 return ret;
2608}
2609
2610static int load_important_roots(struct btrfs_fs_info *fs_info)
2611{
2612 struct btrfs_super_block *sb = fs_info->super_copy;
2613 u64 gen, bytenr;
2614 int level, ret;
2615
2616 bytenr = btrfs_super_root(sb);
2617 gen = btrfs_super_generation(sb);
2618 level = btrfs_super_root_level(sb);
2619 ret = load_super_root(fs_info->tree_root, bytenr, gen, level);
Josef Bacik9c54e802021-12-15 15:40:07 -05002620 if (ret) {
Josef Bacikbd676442021-12-15 15:40:06 -05002621 btrfs_warn(fs_info, "couldn't read tree root");
Josef Bacik9c54e802021-12-15 15:40:07 -05002622 return ret;
2623 }
Qu Wenruo14033b02022-08-09 13:02:17 +08002624 return 0;
Josef Bacikbd676442021-12-15 15:40:06 -05002625}
2626
Nikolay Borisov6ef108d2019-10-15 18:42:24 +03002627static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002628{
Nikolay Borisov6ef108d2019-10-15 18:42:24 +03002629 int backup_index = find_newest_super_backup(fs_info);
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002630 struct btrfs_super_block *sb = fs_info->super_copy;
2631 struct btrfs_root *tree_root = fs_info->tree_root;
2632 bool handle_error = false;
2633 int ret = 0;
2634 int i;
2635
2636 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002637 if (handle_error) {
2638 if (!IS_ERR(tree_root->node))
2639 free_extent_buffer(tree_root->node);
2640 tree_root->node = NULL;
2641
2642 if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
2643 break;
2644
2645 free_root_pointers(fs_info, 0);
2646
2647 /*
2648 * Don't use the log in recovery mode, it won't be
2649 * valid
2650 */
2651 btrfs_set_super_log_root(sb, 0);
2652
Qu Wenruo745806f2023-06-11 08:09:13 +08002653 btrfs_warn(fs_info, "try to load backup roots slot %d", i);
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002654 ret = read_backup_root(fs_info, i);
Nikolay Borisov6ef108d2019-10-15 18:42:24 +03002655 backup_index = ret;
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002656 if (ret < 0)
2657 return ret;
2658 }
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002659
Josef Bacikbd676442021-12-15 15:40:06 -05002660 ret = load_important_roots(fs_info);
2661 if (ret) {
Nikolay Borisov217f5002020-08-12 16:16:35 +03002662 handle_error = true;
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002663 continue;
2664 }
2665
Nikolay Borisov336a0d82019-10-15 18:42:21 +03002666 /*
2667 * No need to hold btrfs_root::objectid_mutex since the fs
2668 * hasn't been fully initialised and we are the only user
2669 */
Nikolay Borisov453e4872020-12-07 17:32:32 +02002670 ret = btrfs_init_root_free_objectid(tree_root);
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002671 if (ret < 0) {
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002672 handle_error = true;
2673 continue;
2674 }
2675
Nikolay Borisov6b8fad52020-12-07 17:32:35 +02002676 ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002677
2678 ret = btrfs_read_roots(fs_info);
2679 if (ret < 0) {
2680 handle_error = true;
2681 continue;
2682 }
2683
2684 /* All successful */
Josef Bacikbd676442021-12-15 15:40:06 -05002685 fs_info->generation = btrfs_header_generation(tree_root->node);
Filipe Manana01248552023-10-04 11:38:51 +01002686 btrfs_set_last_trans_committed(fs_info, fs_info->generation);
Filipe Mananad96b3422021-11-22 12:03:38 +00002687 fs_info->last_reloc_trans = 0;
Nikolay Borisov6ef108d2019-10-15 18:42:24 +03002688
2689 /* Always begin writing backup roots after the one being used */
2690 if (backup_index < 0) {
2691 fs_info->backup_root_index = 0;
2692 } else {
2693 fs_info->backup_root_index = backup_index + 1;
2694 fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS;
2695 }
Nikolay Borisovb8522a12019-10-15 18:42:20 +03002696 break;
2697 }
2698
2699 return ret;
2700}
2701
Josef Bacik8260edba2020-01-24 09:32:59 -05002702void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
Chris Masoneb60cea2007-02-02 09:18:22 -05002703{
David Sterbafc7cbcd2022-07-15 13:59:21 +02002704 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
David Sterba01cd3902022-07-15 13:59:31 +02002705 INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
Chris Mason8fd17792007-04-19 21:01:03 -04002706 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -04002707 INIT_LIST_HEAD(&fs_info->dead_roots);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002708 INIT_LIST_HEAD(&fs_info->delayed_iputs);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002709 INIT_LIST_HEAD(&fs_info->delalloc_roots);
Yan Zheng11833d62009-09-11 16:11:19 -04002710 INIT_LIST_HEAD(&fs_info->caching_block_groups);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002711 spin_lock_init(&fs_info->delalloc_root_lock);
Josef Bacika4abeea2011-04-11 17:25:13 -04002712 spin_lock_init(&fs_info->trans_lock);
David Sterbafc7cbcd2022-07-15 13:59:21 +02002713 spin_lock_init(&fs_info->fs_roots_radix_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002714 spin_lock_init(&fs_info->delayed_iput_lock);
Chris Mason4cb53002011-05-24 15:35:30 -04002715 spin_lock_init(&fs_info->defrag_inodes_lock);
Miao Xieceda0862013-04-11 10:30:16 +00002716 spin_lock_init(&fs_info->super_lock);
Josef Bacikf28491e2013-12-16 13:24:27 -05002717 spin_lock_init(&fs_info->buffer_lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002718 spin_lock_init(&fs_info->unused_bgs_lock);
Naohiro Aota40ab3be2021-02-04 19:22:18 +09002719 spin_lock_init(&fs_info->treelog_bg_lock);
Naohiro Aotaafba2bc2021-08-19 21:19:17 +09002720 spin_lock_init(&fs_info->zone_active_bgs_lock);
Johannes Thumshirnc2707a22021-09-09 01:19:26 +09002721 spin_lock_init(&fs_info->relocation_bg_lock);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002722 rwlock_init(&fs_info->tree_mod_log_lock);
Josef Bacikabed4aa2021-11-05 16:45:51 -04002723 rwlock_init(&fs_info->global_root_lock);
Zhao Leid7c15172015-02-26 10:49:20 +08002724 mutex_init(&fs_info->unused_bg_unpin_mutex);
Johannes Thumshirnf3372062021-04-19 16:41:01 +09002725 mutex_init(&fs_info->reclaim_bgs_lock);
Chris Mason75857172011-06-13 20:00:16 -04002726 mutex_init(&fs_info->reloc_mutex);
Miao Xie573bfb72014-03-06 13:55:03 +08002727 mutex_init(&fs_info->delalloc_root_mutex);
Naohiro Aota0bc09ca2021-02-04 19:22:08 +09002728 mutex_init(&fs_info->zoned_meta_io_lock);
Naohiro Aota5f0addf2022-04-18 16:15:03 +09002729 mutex_init(&fs_info->zoned_data_reloc_io_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002730 seqlock_init(&fs_info->profiles_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -04002731
Ioannis Angelakopoulose1489b42022-07-25 15:11:48 -07002732 btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers);
Ioannis Angelakopoulos5a9ba672022-07-25 15:11:50 -07002733 btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters);
Ioannis Angelakopoulos8b537792022-07-25 15:11:54 -07002734 btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered);
Ioannis Angelakopoulos5f4403e2022-07-25 15:11:59 -07002735 btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent);
Josef Bacik77d20c62023-08-24 16:59:22 -04002736 btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_prep,
2737 BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
Ioannis Angelakopoulos3e738c52022-07-25 15:11:52 -07002738 btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked,
2739 BTRFS_LOCKDEP_TRANS_UNBLOCKED);
2740 btrfs_state_lockdep_init_map(fs_info, btrfs_trans_super_committed,
2741 BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
2742 btrfs_state_lockdep_init_map(fs_info, btrfs_trans_completed,
2743 BTRFS_LOCKDEP_TRANS_COMPLETED);
Ioannis Angelakopoulose1489b42022-07-25 15:11:48 -07002744
Chris Mason0b86a832008-03-24 15:01:56 -04002745 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -04002746 INIT_LIST_HEAD(&fs_info->space_info);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002747 INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002748 INIT_LIST_HEAD(&fs_info->unused_bgs);
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002749 INIT_LIST_HEAD(&fs_info->reclaim_bgs);
Naohiro Aotaafba2bc2021-08-19 21:19:17 +09002750 INIT_LIST_HEAD(&fs_info->zone_active_bgs);
Josef Bacikbd647ce2020-01-24 09:33:00 -05002751#ifdef CONFIG_BTRFS_DEBUG
2752 INIT_LIST_HEAD(&fs_info->allocated_roots);
Josef Bacik3fd63722020-02-14 16:11:40 -05002753 INIT_LIST_HEAD(&fs_info->allocated_ebs);
2754 spin_lock_init(&fs_info->eb_leak_lock);
Josef Bacikbd647ce2020-01-24 09:33:00 -05002755#endif
Filipe Manana7dc66ab2023-11-21 13:38:38 +00002756 fs_info->mapping_tree = RB_ROOT_CACHED;
2757 rwlock_init(&fs_info->mapping_tree_lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06002758 btrfs_init_block_rsv(&fs_info->global_block_rsv,
2759 BTRFS_BLOCK_RSV_GLOBAL);
Miao Xie66d8f3d2012-09-06 04:02:28 -06002760 btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
2761 btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
2762 btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
2763 btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
2764 BTRFS_BLOCK_RSV_DELOPS);
Josef Bacikba2c4d42018-12-03 10:20:33 -05002765 btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
2766 BTRFS_BLOCK_RSV_DELREFS);
2767
Chris Mason771ed682008-11-06 22:02:51 -05002768 atomic_set(&fs_info->async_delalloc_pages, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002769 atomic_set(&fs_info->defrag_running, 0);
Josef Bacik034f7842018-12-03 11:06:52 -05002770 atomic_set(&fs_info->nr_delayed_iputs, 0);
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002771 atomic64_set(&fs_info->tree_mod_seq, 0);
Josef Bacikabed4aa2021-11-05 16:45:51 -04002772 fs_info->global_root_tree = RB_ROOT;
Filipe David Borba Manana95ac5672013-08-08 22:45:48 +01002773 fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002774 fs_info->metadata_ratio = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04002775 fs_info->defrag_inodes = RB_ROOT;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002776 atomic64_set(&fs_info->free_chunk_space, 0);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002777 fs_info->tree_mod_log = RB_ROOT;
David Sterba8b87dc12013-08-01 18:14:52 +02002778 fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
Josef Bacikfd708b82017-09-29 15:43:50 -04002779 btrfs_init_ref_verify(fs_info);
Chris Masonc8b97812008-10-29 14:49:59 -04002780
Chris Masonb34b0862008-12-19 15:43:22 -05002781 fs_info->thread_pool_size = min_t(unsigned long,
2782 num_online_cpus() + 2, 8);
Chris Mason0afbaf82008-04-18 14:17:20 -04002783
Miao Xie199c2a92013-05-15 07:48:23 +00002784 INIT_LIST_HEAD(&fs_info->ordered_roots);
2785 spin_lock_init(&fs_info->ordered_root_lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04002786
Eric Sandeen638aa7e2014-08-01 18:12:38 -05002787 btrfs_init_scrub(fs_info);
Eric Sandeen779a65a2014-08-01 18:12:39 -05002788 btrfs_init_balance(fs_info);
Josef Bacik57056742020-07-21 10:22:33 -04002789 btrfs_init_async_reclaim_work(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002790
Filipe Manana16b0c252022-04-13 16:20:41 +01002791 rwlock_init(&fs_info->block_group_cache_lock);
Filipe Manana08dddb22022-04-13 16:20:40 +01002792 fs_info->block_group_cache_tree = RB_ROOT_CACHED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002793
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002794 extent_io_tree_init(fs_info, &fs_info->excluded_extents,
David Sterba35da5a72022-10-28 02:47:06 +02002795 IO_TREE_FS_EXCLUDED_EXTENTS);
Chris Mason39279cc2007-06-12 06:35:45 -04002796
Chris Mason5a3f23d2009-03-31 13:27:11 -04002797 mutex_init(&fs_info->ordered_operations_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002798 mutex_init(&fs_info->tree_log_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002799 mutex_init(&fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04002800 mutex_init(&fs_info->transaction_kthread_mutex);
2801 mutex_init(&fs_info->cleaner_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07002802 mutex_init(&fs_info->ro_block_group_mutex);
Josef Bacik9e351cc2014-03-13 15:42:13 -04002803 init_rwsem(&fs_info->commit_root_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00002804 init_rwsem(&fs_info->cleanup_work_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -04002805 init_rwsem(&fs_info->subvol_sem);
Stefan Behrens803b2f52013-08-15 17:11:21 +02002806 sema_init(&fs_info->uuid_tree_rescan_sem, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002807
Eric Sandeenad618362014-08-01 18:12:41 -05002808 btrfs_init_dev_replace_locks(fs_info);
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002809 btrfs_init_qgroup(fs_info);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002810 btrfs_discard_init(fs_info);
Arne Jansen416ac512011-09-13 12:56:09 +02002811
Chris Masonfa9c0d792009-04-03 09:47:43 -04002812 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2813 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2814
Chris Masone6dcd2d2008-07-17 12:53:50 -04002815 init_waitqueue_head(&fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04002816 init_waitqueue_head(&fs_info->transaction_wait);
Sage Weilbb9c12c2010-10-29 15:37:34 -04002817 init_waitqueue_head(&fs_info->transaction_blocked_wait);
Chris Mason4854ddd2008-08-15 15:34:17 -04002818 init_waitqueue_head(&fs_info->async_submit_wait);
Josef Bacik034f7842018-12-03 11:06:52 -05002819 init_waitqueue_head(&fs_info->delayed_iputs_wait);
Chris Mason3768f362007-03-13 16:47:54 -04002820
Jeff Mahoneyda170662016-06-15 09:22:56 -04002821 /* Usable values until the real ones are cached from the superblock */
2822 fs_info->nodesize = 4096;
2823 fs_info->sectorsize = 4096;
David Sterbaab108d92020-07-01 20:45:04 +02002824 fs_info->sectorsize_bits = ilog2(4096);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002825 fs_info->stripesize = 4096;
2826
Josef Bacik6207c9e32023-11-22 12:17:39 -05002827 /* Default compress algorithm when user does -o compress */
2828 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2829
Naohiro Aotaf7b12a62022-07-09 08:18:40 +09002830 fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;
2831
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002832 spin_lock_init(&fs_info->swapfile_pins_lock);
2833 fs_info->swapfile_pins = RB_ROOT;
2834
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002835 fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH;
2836 INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work);
Josef Bacik8260edba2020-01-24 09:32:59 -05002837}
2838
2839static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
2840{
2841 int ret;
2842
2843 fs_info->sb = sb;
David Sterba4e004222024-01-16 17:33:20 +01002844 /* Temporary fixed values for block size until we read the superblock. */
Josef Bacik8260edba2020-01-24 09:32:59 -05002845 sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
2846 sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
Filipe Manana9e967492019-04-22 16:44:09 +01002847
Josef Bacik5deb17e2020-10-09 09:28:20 -04002848 ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL);
Josef Bacikae18c372020-01-24 09:32:58 -05002849 if (ret)
2850 return ret;
2851
Filipe Mananaf1d97e72024-03-22 18:02:59 +00002852 ret = percpu_counter_init(&fs_info->evictable_extent_maps, 0, GFP_KERNEL);
2853 if (ret)
2854 return ret;
2855
Filipe Manana44849402024-07-08 15:42:45 +01002856 spin_lock_init(&fs_info->extent_map_shrinker_lock);
2857
Josef Bacikae18c372020-01-24 09:32:58 -05002858 ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
2859 if (ret)
Josef Bacikc75e8392020-02-14 16:11:47 -05002860 return ret;
Josef Bacikae18c372020-01-24 09:32:58 -05002861
2862 fs_info->dirty_metadata_batch = PAGE_SIZE *
2863 (1 + ilog2(nr_cpu_ids));
2864
2865 ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
2866 if (ret)
Josef Bacikc75e8392020-02-14 16:11:47 -05002867 return ret;
Josef Bacikae18c372020-01-24 09:32:58 -05002868
2869 ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0,
2870 GFP_KERNEL);
2871 if (ret)
Josef Bacikc75e8392020-02-14 16:11:47 -05002872 return ret;
Josef Bacikae18c372020-01-24 09:32:58 -05002873
2874 fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
2875 GFP_KERNEL);
Josef Bacikc75e8392020-02-14 16:11:47 -05002876 if (!fs_info->delayed_root)
2877 return -ENOMEM;
Josef Bacikae18c372020-01-24 09:32:58 -05002878 btrfs_init_delayed_root(fs_info->delayed_root);
2879
Filipe Mananaa0a1db72020-12-14 10:10:47 +00002880 if (sb_rdonly(sb))
2881 set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
Qu Wenruo169aaaf2024-06-14 13:52:30 +09302882 if (btrfs_test_opt(fs_info, IGNOREMETACSUMS))
2883 set_bit(BTRFS_FS_STATE_SKIP_META_CSUMS, &fs_info->fs_state);
Filipe Mananaa0a1db72020-12-14 10:10:47 +00002884
Josef Bacikc75e8392020-02-14 16:11:47 -05002885 return btrfs_alloc_stripe_hash_table(fs_info);
Josef Bacikae18c372020-01-24 09:32:58 -05002886}
2887
Nikolay Borisov97f4dd02020-02-18 16:56:08 +02002888static int btrfs_uuid_rescan_kthread(void *data)
2889{
Yu Zhe0d031dc2022-03-31 03:34:08 -07002890 struct btrfs_fs_info *fs_info = data;
Nikolay Borisov97f4dd02020-02-18 16:56:08 +02002891 int ret;
2892
2893 /*
2894 * 1st step is to iterate through the existing UUID tree and
2895 * to delete all entries that contain outdated data.
2896 * 2nd step is to add all missing entries to the UUID tree.
2897 */
2898 ret = btrfs_uuid_tree_iterate(fs_info);
2899 if (ret < 0) {
Josef Bacikc94bec22020-02-14 15:05:01 -05002900 if (ret != -EINTR)
2901 btrfs_warn(fs_info, "iterating uuid_tree failed %d",
2902 ret);
Nikolay Borisov97f4dd02020-02-18 16:56:08 +02002903 up(&fs_info->uuid_tree_rescan_sem);
2904 return ret;
2905 }
2906 return btrfs_uuid_scan_kthread(data);
2907}
2908
2909static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
2910{
2911 struct task_struct *task;
2912
2913 down(&fs_info->uuid_tree_rescan_sem);
2914 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
2915 if (IS_ERR(task)) {
2916 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
2917 btrfs_warn(fs_info, "failed to start uuid_rescan task");
2918 up(&fs_info->uuid_tree_rescan_sem);
2919 return PTR_ERR(task);
2920 }
2921
2922 return 0;
2923}
2924
Filipe Manana504b1592023-07-26 16:57:07 +01002925static int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
2926{
2927 u64 root_objectid = 0;
2928 struct btrfs_root *gang[8];
Anand Jain53d6c0d2024-03-19 20:25:09 +05302929 int ret = 0;
Filipe Manana504b1592023-07-26 16:57:07 +01002930
2931 while (1) {
Anand Jain53d6c0d2024-03-19 20:25:09 +05302932 unsigned int found;
2933
Filipe Manana504b1592023-07-26 16:57:07 +01002934 spin_lock(&fs_info->fs_roots_radix_lock);
Anand Jain53d6c0d2024-03-19 20:25:09 +05302935 found = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
Filipe Manana504b1592023-07-26 16:57:07 +01002936 (void **)gang, root_objectid,
2937 ARRAY_SIZE(gang));
Anand Jain53d6c0d2024-03-19 20:25:09 +05302938 if (!found) {
Filipe Manana504b1592023-07-26 16:57:07 +01002939 spin_unlock(&fs_info->fs_roots_radix_lock);
2940 break;
2941 }
Anand Jain53d6c0d2024-03-19 20:25:09 +05302942 root_objectid = btrfs_root_id(gang[found - 1]) + 1;
Filipe Manana504b1592023-07-26 16:57:07 +01002943
Anand Jain53d6c0d2024-03-19 20:25:09 +05302944 for (int i = 0; i < found; i++) {
Filipe Manana504b1592023-07-26 16:57:07 +01002945 /* Avoid to grab roots in dead_roots. */
2946 if (btrfs_root_refs(&gang[i]->root_item) == 0) {
2947 gang[i] = NULL;
2948 continue;
2949 }
2950 /* Grab all the search result for later use. */
2951 gang[i] = btrfs_grab_root(gang[i]);
2952 }
2953 spin_unlock(&fs_info->fs_roots_radix_lock);
2954
Anand Jain53d6c0d2024-03-19 20:25:09 +05302955 for (int i = 0; i < found; i++) {
Filipe Manana504b1592023-07-26 16:57:07 +01002956 if (!gang[i])
2957 continue;
Josef Bacike094f482024-04-15 16:16:23 -04002958 root_objectid = btrfs_root_id(gang[i]);
Anand Jain53d6c0d2024-03-19 20:25:09 +05302959 /*
2960 * Continue to release the remaining roots after the first
2961 * error without cleanup and preserve the first error
2962 * for the return.
2963 */
2964 if (!ret)
2965 ret = btrfs_orphan_cleanup(gang[i]);
Filipe Manana504b1592023-07-26 16:57:07 +01002966 btrfs_put_root(gang[i]);
2967 }
Anand Jain53d6c0d2024-03-19 20:25:09 +05302968 if (ret)
2969 break;
2970
Filipe Manana504b1592023-07-26 16:57:07 +01002971 root_objectid++;
2972 }
Anand Jain53d6c0d2024-03-19 20:25:09 +05302973 return ret;
Filipe Manana504b1592023-07-26 16:57:07 +01002974}
2975
Boris Burkov44c0ca22020-11-18 15:06:16 -08002976/*
2977 * Mounting logic specific to read-write file systems. Shared by open_ctree
2978 * and btrfs_remount when remounting from read-only to read-write.
2979 */
2980int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
2981{
2982 int ret;
Boris Burkov94846222020-11-18 15:06:22 -08002983 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
Qu Wenruo1d6a4fc2023-04-28 14:13:05 +08002984 bool rebuild_free_space_tree = false;
Boris Burkov8b228322020-11-18 15:06:21 -08002985
2986 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
2987 btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
Josef Bacik272efa32023-11-22 12:17:41 -05002988 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
2989 btrfs_warn(fs_info,
2990 "'clear_cache' option is ignored with extent tree v2");
2991 else
2992 rebuild_free_space_tree = true;
Boris Burkov8b228322020-11-18 15:06:21 -08002993 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
2994 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
2995 btrfs_warn(fs_info, "free space tree is invalid");
Qu Wenruo1d6a4fc2023-04-28 14:13:05 +08002996 rebuild_free_space_tree = true;
Boris Burkov8b228322020-11-18 15:06:21 -08002997 }
2998
Qu Wenruo1d6a4fc2023-04-28 14:13:05 +08002999 if (rebuild_free_space_tree) {
3000 btrfs_info(fs_info, "rebuilding free space tree");
3001 ret = btrfs_rebuild_free_space_tree(fs_info);
Boris Burkov8b228322020-11-18 15:06:21 -08003002 if (ret) {
3003 btrfs_warn(fs_info,
Qu Wenruo1d6a4fc2023-04-28 14:13:05 +08003004 "failed to rebuild free space tree: %d", ret);
3005 goto out;
3006 }
3007 }
3008
3009 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
3010 !btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
3011 btrfs_info(fs_info, "disabling free space tree");
3012 ret = btrfs_delete_free_space_tree(fs_info);
3013 if (ret) {
3014 btrfs_warn(fs_info,
3015 "failed to disable free space tree: %d", ret);
Boris Burkov8b228322020-11-18 15:06:21 -08003016 goto out;
3017 }
3018 }
Boris Burkov44c0ca22020-11-18 15:06:16 -08003019
Filipe Manana8d488a82021-03-16 16:53:46 +00003020 /*
3021 * btrfs_find_orphan_roots() is responsible for finding all the dead
3022 * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load
David Sterbafc7cbcd2022-07-15 13:59:21 +02003023 * them into the fs_info->fs_roots_radix tree. This must be done before
Filipe Manana8d488a82021-03-16 16:53:46 +00003024 * calling btrfs_orphan_cleanup() on the tree root. If we don't do it
3025 * first, then btrfs_orphan_cleanup() will delete a dead root's orphan
3026 * item before the root's tree is deleted - this means that if we unmount
3027 * or crash before the deletion completes, on the next mount we will not
3028 * delete what remains of the tree because the orphan item does not
3029 * exists anymore, which is what tells us we have a pending deletion.
3030 */
3031 ret = btrfs_find_orphan_roots(fs_info);
3032 if (ret)
3033 goto out;
3034
Boris Burkov44c0ca22020-11-18 15:06:16 -08003035 ret = btrfs_cleanup_fs_roots(fs_info);
3036 if (ret)
3037 goto out;
3038
Boris Burkov8f1c21d2020-11-18 15:06:17 -08003039 down_read(&fs_info->cleanup_work_sem);
3040 if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
3041 (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
3042 up_read(&fs_info->cleanup_work_sem);
3043 goto out;
3044 }
3045 up_read(&fs_info->cleanup_work_sem);
3046
Boris Burkov44c0ca22020-11-18 15:06:16 -08003047 mutex_lock(&fs_info->cleaner_mutex);
Josef Bacik7eefae62022-02-18 14:56:12 -05003048 ret = btrfs_recover_relocation(fs_info);
Boris Burkov44c0ca22020-11-18 15:06:16 -08003049 mutex_unlock(&fs_info->cleaner_mutex);
3050 if (ret < 0) {
3051 btrfs_warn(fs_info, "failed to recover relocation: %d", ret);
3052 goto out;
3053 }
3054
Boris Burkov50111392020-11-18 15:06:19 -08003055 if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
3056 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
3057 btrfs_info(fs_info, "creating free space tree");
3058 ret = btrfs_create_free_space_tree(fs_info);
3059 if (ret) {
3060 btrfs_warn(fs_info,
3061 "failed to create free space tree: %d", ret);
3062 goto out;
3063 }
3064 }
3065
Boris Burkov94846222020-11-18 15:06:22 -08003066 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) {
3067 ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
3068 if (ret)
3069 goto out;
3070 }
3071
Boris Burkov44c0ca22020-11-18 15:06:16 -08003072 ret = btrfs_resume_balance_async(fs_info);
3073 if (ret)
3074 goto out;
3075
3076 ret = btrfs_resume_dev_replace_async(fs_info);
3077 if (ret) {
3078 btrfs_warn(fs_info, "failed to resume dev_replace");
3079 goto out;
3080 }
3081
3082 btrfs_qgroup_rescan_resume(fs_info);
3083
3084 if (!fs_info->uuid_root) {
3085 btrfs_info(fs_info, "creating UUID tree");
3086 ret = btrfs_create_uuid_tree(fs_info);
3087 if (ret) {
3088 btrfs_warn(fs_info,
3089 "failed to create the UUID tree %d", ret);
3090 goto out;
3091 }
3092 }
3093
3094out:
3095 return ret;
3096}
3097
Qu Wenruod7f67ac2022-09-12 13:44:37 +08003098/*
3099 * Do various sanity and dependency checks of different features.
3100 *
Qu Wenruo2ba48b22022-12-22 07:59:17 +08003101 * @is_rw_mount: If the mount is read-write.
3102 *
Qu Wenruod7f67ac2022-09-12 13:44:37 +08003103 * This is the place for less strict checks (like for subpage or artificial
3104 * feature dependencies).
3105 *
3106 * For strict checks or possible corruption detection, see
3107 * btrfs_validate_super().
3108 *
3109 * This should be called after btrfs_parse_options(), as some mount options
3110 * (space cache related) can modify on-disk format like free space tree and
3111 * screw up certain feature dependencies.
3112 */
Qu Wenruo2ba48b22022-12-22 07:59:17 +08003113int btrfs_check_features(struct btrfs_fs_info *fs_info, bool is_rw_mount)
Qu Wenruod7f67ac2022-09-12 13:44:37 +08003114{
3115 struct btrfs_super_block *disk_super = fs_info->super_copy;
3116 u64 incompat = btrfs_super_incompat_flags(disk_super);
3117 const u64 compat_ro = btrfs_super_compat_ro_flags(disk_super);
3118 const u64 compat_ro_unsupp = (compat_ro & ~BTRFS_FEATURE_COMPAT_RO_SUPP);
3119
3120 if (incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
3121 btrfs_err(fs_info,
3122 "cannot mount because of unknown incompat features (0x%llx)",
3123 incompat);
3124 return -EINVAL;
3125 }
3126
3127 /* Runtime limitation for mixed block groups. */
3128 if ((incompat & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
3129 (fs_info->sectorsize != fs_info->nodesize)) {
3130 btrfs_err(fs_info,
3131"unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
3132 fs_info->nodesize, fs_info->sectorsize);
3133 return -EINVAL;
3134 }
3135
3136 /* Mixed backref is an always-enabled feature. */
3137 incompat |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
3138
3139 /* Set compression related flags just in case. */
3140 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
3141 incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
3142 else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
3143 incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
3144
3145 /*
3146 * An ancient flag, which should really be marked deprecated.
3147 * Such runtime limitation doesn't really need a incompat flag.
3148 */
3149 if (btrfs_super_nodesize(disk_super) > PAGE_SIZE)
3150 incompat |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
3151
Qu Wenruo2ba48b22022-12-22 07:59:17 +08003152 if (compat_ro_unsupp && is_rw_mount) {
Qu Wenruod7f67ac2022-09-12 13:44:37 +08003153 btrfs_err(fs_info,
3154 "cannot mount read-write because of unknown compat_ro features (0x%llx)",
3155 compat_ro);
3156 return -EINVAL;
3157 }
3158
3159 /*
3160 * We have unsupported RO compat features, although RO mounted, we
3161 * should not cause any metadata writes, including log replay.
3162 * Or we could screw up whatever the new feature requires.
3163 */
3164 if (compat_ro_unsupp && btrfs_super_log_root(disk_super) &&
3165 !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3166 btrfs_err(fs_info,
3167"cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay",
3168 compat_ro);
3169 return -EINVAL;
3170 }
3171
3172 /*
3173 * Artificial limitations for block group tree, to force
3174 * block-group-tree to rely on no-holes and free-space-tree.
3175 */
3176 if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
3177 (!btrfs_fs_incompat(fs_info, NO_HOLES) ||
3178 !btrfs_test_opt(fs_info, FREE_SPACE_TREE))) {
3179 btrfs_err(fs_info,
3180"block-group-tree feature requires no-holes and free-space-tree features");
3181 return -EINVAL;
3182 }
3183
3184 /*
3185 * Subpage runtime limitation on v1 cache.
3186 *
3187 * V1 space cache still has some hard codeed PAGE_SIZE usage, while
3188 * we're already defaulting to v2 cache, no need to bother v1 as it's
3189 * going to be deprecated anyway.
3190 */
3191 if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
3192 btrfs_warn(fs_info,
3193 "v1 space cache is not supported for page size %lu with sectorsize %u",
3194 PAGE_SIZE, fs_info->sectorsize);
3195 return -EINVAL;
3196 }
3197
3198 /* This can be called by remount, we need to protect the super block. */
3199 spin_lock(&fs_info->super_lock);
3200 btrfs_set_super_incompat_flags(disk_super, incompat);
3201 spin_unlock(&fs_info->super_lock);
3202
3203 return 0;
3204}
3205
Josef Bacikae18c372020-01-24 09:32:58 -05003206int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices,
David Sterba2917f742024-05-30 19:14:12 +02003207 const char *options)
Josef Bacikae18c372020-01-24 09:32:58 -05003208{
3209 u32 sectorsize;
3210 u32 nodesize;
3211 u32 stripesize;
3212 u64 generation;
Josef Bacikae18c372020-01-24 09:32:58 -05003213 u16 csum_type;
Josef Bacikae18c372020-01-24 09:32:58 -05003214 struct btrfs_super_block *disk_super;
3215 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
3216 struct btrfs_root *tree_root;
3217 struct btrfs_root *chunk_root;
3218 int ret;
Josef Bacikae18c372020-01-24 09:32:58 -05003219 int level;
3220
Josef Bacik8260edba2020-01-24 09:32:59 -05003221 ret = init_mount_fs_info(fs_info, sb);
Qu Wenruo4871c332023-02-28 08:44:30 +08003222 if (ret)
Josef Bacikae18c372020-01-24 09:32:58 -05003223 goto fail;
David Woodhouse53b381b2013-01-29 18:40:14 -05003224
Josef Bacikae18c372020-01-24 09:32:58 -05003225 /* These need to be init'ed before we start creating inodes and such. */
3226 tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID,
3227 GFP_KERNEL);
3228 fs_info->tree_root = tree_root;
3229 chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID,
3230 GFP_KERNEL);
3231 fs_info->chunk_root = chunk_root;
3232 if (!tree_root || !chunk_root) {
Qu Wenruo4871c332023-02-28 08:44:30 +08003233 ret = -ENOMEM;
Josef Bacikc75e8392020-02-14 16:11:47 -05003234 goto fail;
Josef Bacikae18c372020-01-24 09:32:58 -05003235 }
3236
Christoph Hellwigdcb21372023-02-19 19:10:22 +01003237 ret = btrfs_init_btree_inode(sb);
Qu Wenruo4871c332023-02-28 08:44:30 +08003238 if (ret)
Josef Bacikc75e8392020-02-14 16:11:47 -05003239 goto fail;
Josef Bacikae18c372020-01-24 09:32:58 -05003240
Anand Jaind24fa5c2021-08-24 13:05:19 +08003241 invalidate_bdev(fs_devices->latest_dev->bdev);
David Sterba1104a882013-03-06 15:57:46 +01003242
3243 /*
3244 * Read super block and check the signature bytes only
3245 */
Anand Jaind24fa5c2021-08-24 13:05:19 +08003246 disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev);
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003247 if (IS_ERR(disk_super)) {
Qu Wenruo4871c332023-02-28 08:44:30 +08003248 ret = PTR_ERR(disk_super);
Miao Xie16cdcec2011-04-22 18:12:22 +08003249 goto fail_alloc;
Dave Young20b45072011-01-08 10:09:13 +00003250 }
Chris Mason39279cc2007-06-12 06:35:45 -04003251
Qu Wenruo2db31322023-11-02 07:54:50 +10303252 btrfs_info(fs_info, "first mount of filesystem %pU", disk_super->fsid);
David Sterba1104a882013-03-06 15:57:46 +01003253 /*
Randy Dunlap260db432020-08-04 19:48:34 -07003254 * Verify the type first, if that or the checksum value are
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02003255 * corrupted, we'll find out
3256 */
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003257 csum_type = btrfs_super_csum_type(disk_super);
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +02003258 if (!btrfs_supported_super_csum(csum_type)) {
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02003259 btrfs_err(fs_info, "unsupported checksum algorithm: %u",
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +02003260 csum_type);
Qu Wenruo4871c332023-02-28 08:44:30 +08003261 ret = -EINVAL;
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003262 btrfs_release_disk_super(disk_super);
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02003263 goto fail_alloc;
3264 }
3265
Su Yue83c68bb2021-02-11 16:38:28 +08003266 fs_info->csum_size = btrfs_super_csum_size(disk_super);
3267
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02003268 ret = btrfs_init_csum_hash(fs_info, csum_type);
3269 if (ret) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003270 btrfs_release_disk_super(disk_super);
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02003271 goto fail_alloc;
3272 }
3273
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02003274 /*
David Sterba1104a882013-03-06 15:57:46 +01003275 * We want to check superblock checksum, the type is stored inside.
3276 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
3277 */
Qu Wenruo3d17ade2022-10-18 09:56:38 +08003278 if (btrfs_check_super_csum(fs_info, disk_super)) {
David Sterba05135f52016-05-09 11:32:39 +02003279 btrfs_err(fs_info, "superblock checksum mismatch");
Qu Wenruo4871c332023-02-28 08:44:30 +08003280 ret = -EINVAL;
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003281 btrfs_release_disk_super(disk_super);
Josef Bacik141386e2020-01-24 09:32:57 -05003282 goto fail_alloc;
David Sterba1104a882013-03-06 15:57:46 +01003283 }
3284
3285 /*
3286 * super_copy is zeroed at allocation time and we never touch the
3287 * following bytes up to INFO_SIZE, the checksum is calculated from
3288 * the whole block of INFO_SIZE
3289 */
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003290 memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy));
3291 btrfs_release_disk_super(disk_super);
Chris Mason5f39d392007-10-15 16:14:19 -04003292
Nikolay Borisovfbc6fea2018-10-30 16:43:25 +02003293 disk_super = fs_info->super_copy;
3294
Nikolay Borisovfbc6fea2018-10-30 16:43:25 +02003295 memcpy(fs_info->super_for_commit, fs_info->super_copy,
3296 sizeof(*fs_info->super_for_commit));
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003297
Qu Wenruo069ec952018-05-11 13:35:26 +08003298 ret = btrfs_validate_mount_super(fs_info);
David Sterba1104a882013-03-06 15:57:46 +01003299 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003300 btrfs_err(fs_info, "superblock contains fatal errors");
Qu Wenruo4871c332023-02-28 08:44:30 +08003301 ret = -EINVAL;
Josef Bacik141386e2020-01-24 09:32:57 -05003302 goto fail_alloc;
David Sterba1104a882013-03-06 15:57:46 +01003303 }
3304
Qu Wenruo4871c332023-02-28 08:44:30 +08003305 if (!btrfs_super_root(disk_super)) {
3306 btrfs_err(fs_info, "invalid superblock tree root bytenr");
3307 ret = -EINVAL;
Josef Bacik141386e2020-01-24 09:32:57 -05003308 goto fail_alloc;
Qu Wenruo4871c332023-02-28 08:44:30 +08003309 }
Chris Mason0f7d52f2007-04-09 10:42:37 -04003310
liuboacce9522011-01-06 19:30:25 +08003311 /* check FS state, whether FS is broken. */
Miao Xie87533c42013-01-29 10:14:48 +00003312 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
Filipe Mananaae3364e2023-07-26 16:57:04 +01003313 WRITE_ONCE(fs_info->fs_error, -EUCLEAN);
liuboacce9522011-01-06 19:30:25 +08003314
Anand Jain6f93e832021-08-10 23:23:44 +08003315 /* Set up fs_info before parsing mount options */
3316 nodesize = btrfs_super_nodesize(disk_super);
3317 sectorsize = btrfs_super_sectorsize(disk_super);
3318 stripesize = sectorsize;
3319 fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
3320 fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
3321
3322 fs_info->nodesize = nodesize;
3323 fs_info->sectorsize = sectorsize;
3324 fs_info->sectorsize_bits = ilog2(sectorsize);
3325 fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
3326 fs_info->stripesize = stripesize;
3327
Josef Bacika6a8f222023-11-22 12:17:40 -05003328 /*
3329 * Handle the space caching options appropriately now that we have the
3330 * super block loaded and validated.
3331 */
3332 btrfs_set_free_space_cache_settings(fs_info);
3333
Josef Bacikad21f152023-11-22 12:17:50 -05003334 if (!btrfs_check_options(fs_info, &fs_info->mount_opt, sb->s_flags)) {
3335 ret = -EINVAL;
Josef Bacik141386e2020-01-24 09:32:57 -05003336 goto fail_alloc;
Josef Bacikad21f152023-11-22 12:17:50 -05003337 }
Chris Masondfe25022008-05-13 13:46:40 -04003338
Qu Wenruo2ba48b22022-12-22 07:59:17 +08003339 ret = btrfs_check_features(fs_info, !sb_rdonly(sb));
Qu Wenruo4871c332023-02-28 08:44:30 +08003340 if (ret < 0)
Josef Bacik141386e2020-01-24 09:32:57 -05003341 goto fail_alloc;
Josef Bacikf2b636e2008-12-02 06:36:08 -05003342
Josef Bacikad21f152023-11-22 12:17:50 -05003343 /*
3344 * At this point our mount options are validated, if we set ->max_inline
3345 * to something non-standard make sure we truncate it to sectorsize.
3346 */
3347 fs_info->max_inline = min_t(u64, fs_info->max_inline, fs_info->sectorsize);
3348
Qu Wenruo8481dd82021-08-17 17:38:51 +08003349 if (sectorsize < PAGE_SIZE) {
3350 struct btrfs_subpage_info *subpage_info;
3351
Qu Wenruo95ea0482021-07-26 14:35:06 +08003352 btrfs_warn(fs_info,
3353 "read-write for sector size %u with page size %lu is experimental",
3354 sectorsize, PAGE_SIZE);
Qu Wenruo8481dd82021-08-17 17:38:51 +08003355 subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL);
Qu Wenruo4871c332023-02-28 08:44:30 +08003356 if (!subpage_info) {
3357 ret = -ENOMEM;
Qu Wenruo8481dd82021-08-17 17:38:51 +08003358 goto fail_alloc;
Qu Wenruo4871c332023-02-28 08:44:30 +08003359 }
Qu Wenruo8481dd82021-08-17 17:38:51 +08003360 btrfs_init_subpage_info(subpage_info, sectorsize);
3361 fs_info->subpage_info = subpage_info;
Qu Wenruoc8050b32021-07-26 14:35:01 +08003362 }
Qu Wenruo0bb3eb32021-01-26 16:34:02 +08003363
Su Yued21deec2021-11-10 14:42:17 +08003364 ret = btrfs_init_workqueues(fs_info);
Qu Wenruo4871c332023-02-28 08:44:30 +08003365 if (ret)
Josef Bacik0dc3b842011-11-18 14:37:27 -05003366 goto fail_sb_buffer;
Chris Mason4543df72008-06-11 21:47:56 -04003367
Jan Kara9e11cee2017-04-12 12:24:32 +02003368 sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
3369 sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
Chris Mason4575c9c2008-04-18 16:13:31 -04003370
David Sterba4e004222024-01-16 17:33:20 +01003371 /* Update the values for the current filesystem. */
Chris Masona061fc82008-05-07 11:43:44 -04003372 sb->s_blocksize = sectorsize;
3373 sb->s_blocksize_bits = blksize_bits(sectorsize);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003374 memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE);
Chris Masondb945352007-10-15 16:15:53 -04003375
Chris Mason925baed2008-06-25 16:01:30 -04003376 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003377 ret = btrfs_read_sys_array(fs_info);
Chris Mason925baed2008-06-25 16:01:30 -04003378 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason84eed902008-04-25 09:04:37 -04003379 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003380 btrfs_err(fs_info, "failed to read the system array: %d", ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003381 goto fail_sb_buffer;
Chris Mason84eed902008-04-25 09:04:37 -04003382 }
Chris Mason0b86a832008-03-24 15:01:56 -04003383
Yan Zheng84234f32008-10-29 14:49:05 -04003384 generation = btrfs_super_chunk_root_generation(disk_super);
Qu Wenruo581c1762018-03-29 09:08:11 +08003385 level = btrfs_super_chunk_root_level(disk_super);
Josef Bacikbd676442021-12-15 15:40:06 -05003386 ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super),
3387 generation, level);
3388 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003389 btrfs_err(fs_info, "failed to read chunk root");
Chris Masonaf31f5e2011-11-03 15:17:42 -04003390 goto fail_tree_roots;
David Woodhouse83121942009-07-22 16:52:13 -04003391 }
Chris Mason0b86a832008-03-24 15:01:56 -04003392
Chris Masone17cade2008-04-15 15:41:47 -04003393 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
David Sterbac4ac7542019-03-20 13:17:13 +01003394 offsetof(struct btrfs_header, chunk_tree_uuid),
3395 BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003396
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003397 ret = btrfs_read_chunk_tree(fs_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003398 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003399 btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003400 goto fail_tree_roots;
Yan Zheng2b820322008-11-17 21:11:30 -05003401 }
Chris Mason0b86a832008-03-24 15:01:56 -04003402
Stefan Behrens8dabb742012-11-06 13:15:27 +01003403 /*
Anand Jainbacce862020-11-06 16:06:33 +08003404 * At this point we know all the devices that make this filesystem,
3405 * including the seed devices but we don't know yet if the replace
3406 * target is required. So free devices that are not part of this
David Sterba1a9fd412021-05-21 17:42:23 +02003407 * filesystem but skip the replace target device which is checked
Anand Jainbacce862020-11-06 16:06:33 +08003408 * below in btrfs_init_dev_replace().
Stefan Behrens8dabb742012-11-06 13:15:27 +01003409 */
Anand Jainbacce862020-11-06 16:06:33 +08003410 btrfs_free_extra_devids(fs_devices);
Anand Jaind24fa5c2021-08-24 13:05:19 +08003411 if (!fs_devices->latest_dev->bdev) {
David Sterba05135f52016-05-09 11:32:39 +02003412 btrfs_err(fs_info, "failed to read devices");
Qu Wenruo4871c332023-02-28 08:44:30 +08003413 ret = -EIO;
Chris Masona6b0d5c2012-02-20 20:53:43 -05003414 goto fail_tree_roots;
3415 }
3416
Nikolay Borisovb8522a12019-10-15 18:42:20 +03003417 ret = init_tree_roots(fs_info);
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05003418 if (ret)
Nikolay Borisovb8522a12019-10-15 18:42:20 +03003419 goto fail_tree_roots;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003420
Josef Bacik75ec1db2020-02-14 15:22:06 -05003421 /*
Naohiro Aota73651042021-02-04 19:21:42 +09003422 * Get zone type information of zoned block devices. This will also
3423 * handle emulation of a zoned filesystem if a regular device has the
3424 * zoned incompat feature flag set.
3425 */
3426 ret = btrfs_get_dev_zone_info_all_devices(fs_info);
3427 if (ret) {
3428 btrfs_err(fs_info,
Qu Wenruo4871c332023-02-28 08:44:30 +08003429 "zoned: failed to read device zone info: %d", ret);
Naohiro Aota73651042021-02-04 19:21:42 +09003430 goto fail_block_groups;
3431 }
3432
3433 /*
Josef Bacik75ec1db2020-02-14 15:22:06 -05003434 * If we have a uuid root and we're not being told to rescan we need to
3435 * check the generation here so we can set the
3436 * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the
3437 * transaction during a balance or the log replay without updating the
3438 * uuid generation, and then if we crash we would rescan the uuid tree,
3439 * even though it was perfectly fine.
3440 */
3441 if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
3442 fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
3443 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
3444
Qu Wenruocf90d882018-08-01 10:37:19 +08003445 ret = btrfs_verify_dev_extents(fs_info);
3446 if (ret) {
3447 btrfs_err(fs_info,
3448 "failed to verify dev extents against chunks: %d",
3449 ret);
3450 goto fail_block_groups;
3451 }
Ilya Dryomov68310a52012-06-22 12:24:12 -06003452 ret = btrfs_recover_balance(fs_info);
3453 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003454 btrfs_err(fs_info, "failed to recover balance: %d", ret);
Ilya Dryomov68310a52012-06-22 12:24:12 -06003455 goto fail_block_groups;
3456 }
3457
Stefan Behrens733f4fb2012-05-25 16:06:10 +02003458 ret = btrfs_init_dev_stats(fs_info);
3459 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003460 btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02003461 goto fail_block_groups;
3462 }
3463
Stefan Behrens8dabb742012-11-06 13:15:27 +01003464 ret = btrfs_init_dev_replace(fs_info);
3465 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003466 btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003467 goto fail_block_groups;
3468 }
3469
Naohiro Aotab70f5092020-11-10 20:26:08 +09003470 ret = btrfs_check_zoned_mode(fs_info);
3471 if (ret) {
3472 btrfs_err(fs_info, "failed to initialize zoned mode: %d",
3473 ret);
3474 goto fail_block_groups;
3475 }
3476
Anand Jainc6761a92019-11-21 17:33:32 +08003477 ret = btrfs_sysfs_add_fsid(fs_devices);
Anand Jainb7c35e812015-03-10 06:38:38 +08003478 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003479 btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
3480 ret);
Anand Jainb7c35e812015-03-10 06:38:38 +08003481 goto fail_block_groups;
3482 }
3483
Anand Jain96f31362015-08-14 18:32:46 +08003484 ret = btrfs_sysfs_add_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04003485 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003486 btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
Anand Jainb7c35e812015-03-10 06:38:38 +08003487 goto fail_fsdev_sysfs;
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04003488 }
3489
liuboc59021f2011-03-07 02:13:14 +00003490 ret = btrfs_init_space_info(fs_info);
3491 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003492 btrfs_err(fs_info, "failed to initialize space info: %d", ret);
Anand Jain2365dd32014-01-22 11:15:51 +08003493 goto fail_sysfs;
liuboc59021f2011-03-07 02:13:14 +00003494 }
3495
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003496 ret = btrfs_read_block_groups(fs_info);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003497 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003498 btrfs_err(fs_info, "failed to read block groups: %d", ret);
Anand Jain2365dd32014-01-22 11:15:51 +08003499 goto fail_sysfs;
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003500 }
Qu Wenruo4330e182017-03-09 09:34:37 +08003501
Naohiro Aota16beac82021-11-11 14:14:38 +09003502 btrfs_free_zone_cache(fs_info);
3503
Naohiro Aotaa7e1ac7b2023-08-08 01:12:36 +09003504 btrfs_check_active_zone_reservation(fs_info);
3505
Anand Jain5c78a5e2021-10-19 18:43:38 +08003506 if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices &&
3507 !btrfs_check_rw_degradable(fs_info, NULL)) {
David Sterba05135f52016-05-09 11:32:39 +02003508 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01003509 "writable mount is not allowed due to too many missing devices");
Qu Wenruo4871c332023-02-28 08:44:30 +08003510 ret = -EINVAL;
Anand Jain2365dd32014-01-22 11:15:51 +08003511 goto fail_sysfs;
Stefan Behrens292fd7f2012-10-30 17:16:16 +00003512 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003513
Josef Bacik33c44182022-02-18 14:56:11 -05003514 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
Chris Masona74a4b92008-06-25 16:01:31 -04003515 "btrfs-cleaner");
Qu Wenruo4871c332023-02-28 08:44:30 +08003516 if (IS_ERR(fs_info->cleaner_kthread)) {
3517 ret = PTR_ERR(fs_info->cleaner_kthread);
Anand Jain2365dd32014-01-22 11:15:51 +08003518 goto fail_sysfs;
Qu Wenruo4871c332023-02-28 08:44:30 +08003519 }
Chris Masona74a4b92008-06-25 16:01:31 -04003520
3521 fs_info->transaction_kthread = kthread_run(transaction_kthread,
3522 tree_root,
3523 "btrfs-transaction");
Qu Wenruo4871c332023-02-28 08:44:30 +08003524 if (IS_ERR(fs_info->transaction_kthread)) {
3525 ret = PTR_ERR(fs_info->transaction_kthread);
Chris Mason3f157a22008-06-25 16:01:31 -04003526 goto fail_cleaner;
Qu Wenruo4871c332023-02-28 08:44:30 +08003527 }
Chris Masona74a4b92008-06-25 16:01:31 -04003528
Arne Jansenbcef60f2011-09-13 15:23:30 +02003529 ret = btrfs_read_qgroup_config(fs_info);
3530 if (ret)
3531 goto fail_trans_kthread;
Stefan Behrens21adbd52011-11-09 13:44:05 +01003532
Josef Bacikfd708b82017-09-29 15:43:50 -04003533 if (btrfs_build_ref_tree(fs_info))
3534 btrfs_err(fs_info, "couldn't build ref tree");
3535
Qu Wenruo96da0912016-01-19 10:23:03 +08003536 /* do not make disk changes in broken FS or nologreplay is given */
3537 if (btrfs_super_log_root(disk_super) != 0 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003538 !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
David Sterbae8294f22020-02-05 17:12:16 +01003539 btrfs_info(fs_info, "start tree-log replay");
Eric Sandeen63443bf2014-08-01 18:12:46 -05003540 ret = btrfs_replay_log(fs_info, fs_devices);
Qu Wenruo4871c332023-02-28 08:44:30 +08003541 if (ret)
Wang Shilong28c16cb2014-04-23 19:33:35 +08003542 goto fail_qgroup;
Chris Masone02119d2008-09-05 16:13:11 -04003543 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003544
David Sterba56e93572020-05-15 19:35:55 +02003545 fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
Dan Carpenter3140c9a2010-05-29 09:44:10 +00003546 if (IS_ERR(fs_info->fs_root)) {
Qu Wenruo4871c332023-02-28 08:44:30 +08003547 ret = PTR_ERR(fs_info->fs_root);
3548 btrfs_warn(fs_info, "failed to read fs tree: %d", ret);
Josef Bacik315bf8e2020-02-13 10:47:28 -05003549 fs_info->fs_root = NULL;
Arne Jansenbcef60f2011-09-13 15:23:30 +02003550 goto fail_qgroup;
Dan Carpenter3140c9a2010-05-29 09:44:10 +00003551 }
Chris Masonc2898112009-06-10 09:51:32 -04003552
David Howellsbc98a422017-07-17 08:45:34 +01003553 if (sb_rdonly(sb))
Josef Bacik83e3a402023-11-22 12:17:53 -05003554 return 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003555
Boris Burkov44c0ca22020-11-18 15:06:16 -08003556 ret = btrfs_start_pre_rw_mount(fs_info);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003557 if (ret) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003558 close_ctree(fs_info);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003559 return ret;
Josef Bacike3acc2a2010-01-26 14:30:53 +00003560 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08003561 btrfs_discard_resume(fs_info);
Jan Schmidtb382a322013-05-28 15:47:24 +00003562
Boris Burkov44c0ca22020-11-18 15:06:16 -08003563 if (fs_info->uuid_root &&
3564 (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
3565 fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) {
David Sterba05135f52016-05-09 11:32:39 +02003566 btrfs_info(fs_info, "checking UUID tree");
Stefan Behrens70f8017542013-08-15 17:11:23 +02003567 ret = btrfs_check_uuid_tree(fs_info);
3568 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003569 btrfs_warn(fs_info,
3570 "failed to check the UUID tree: %d", ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003571 close_ctree(fs_info);
Stefan Behrens70f8017542013-08-15 17:11:23 +02003572 return ret;
3573 }
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003574 }
Boris Burkov94846222020-11-18 15:06:22 -08003575
Josef Bacikafcdd122016-09-02 15:40:02 -04003576 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003577
Josef Bacikb4be6ae2022-02-18 14:56:10 -05003578 /* Kick the cleaner thread so it'll start deleting snapshots. */
3579 if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
3580 wake_up_process(fs_info->cleaner_kthread);
3581
Al Viroad2b2c82011-11-17 01:10:02 -05003582 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003583
Arne Jansenbcef60f2011-09-13 15:23:30 +02003584fail_qgroup:
3585 btrfs_free_qgroup_config(fs_info);
Chris Mason7c2ca462008-11-19 15:13:35 -05003586fail_trans_kthread:
3587 kthread_stop(fs_info->transaction_kthread);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003588 btrfs_cleanup_transaction(fs_info);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003589 btrfs_free_fs_roots(fs_info);
Chris Mason3f157a22008-06-25 16:01:31 -04003590fail_cleaner:
Chris Masona74a4b92008-06-25 16:01:31 -04003591 kthread_stop(fs_info->cleaner_kthread);
Chris Mason7c2ca462008-11-19 15:13:35 -05003592
3593 /*
3594 * make sure we're done with the btree inode before we stop our
3595 * kthreads
3596 */
3597 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
Chris Mason7c2ca462008-11-19 15:13:35 -05003598
Anand Jain2365dd32014-01-22 11:15:51 +08003599fail_sysfs:
Anand Jain6618a592015-08-14 18:32:47 +08003600 btrfs_sysfs_remove_mounted(fs_info);
Anand Jain2365dd32014-01-22 11:15:51 +08003601
Anand Jainb7c35e812015-03-10 06:38:38 +08003602fail_fsdev_sysfs:
3603 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3604
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003605fail_block_groups:
Josef Bacik54067ae2013-04-25 13:44:38 -04003606 btrfs_put_block_group_cache(fs_info);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003607
3608fail_tree_roots:
Josef Bacik9e3aa802020-09-03 14:29:50 -04003609 if (fs_info->data_reloc_root)
3610 btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root);
Anand Jain4273eaf2019-10-10 10:39:25 +08003611 free_root_pointers(fs_info, true);
Miao Xie2b8195b2013-02-07 06:01:35 +00003612 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003613
Chris Mason39279cc2007-06-12 06:35:45 -04003614fail_sb_buffer:
Liu Bo7abadb62013-03-17 02:10:31 +00003615 btrfs_stop_all_workers(fs_info);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00003616 btrfs_free_block_groups(fs_info);
Miao Xie16cdcec2011-04-22 18:12:22 +08003617fail_alloc:
Filipe Manana7dc66ab2023-11-21 13:38:38 +00003618 btrfs_mapping_tree_free(fs_info);
Ilya Dryomov586e46e2011-11-09 13:26:37 +02003619
Chris Mason4543df72008-06-11 21:47:56 -04003620 iput(fs_info->btree_inode);
Qinghuang Feng7e662852009-01-21 10:49:16 -05003621fail:
Ilya Dryomov586e46e2011-11-09 13:26:37 +02003622 btrfs_close_devices(fs_info->fs_devices);
Qu Wenruo4871c332023-02-28 08:44:30 +08003623 ASSERT(ret < 0);
3624 return ret;
Chris Masoneb60cea2007-02-02 09:18:22 -05003625}
Masami Hiramatsu663faf92018-01-13 02:55:33 +09003626ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
Chris Masoneb60cea2007-02-02 09:18:22 -05003627
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003628static void btrfs_end_super_write(struct bio *bio)
Chris Masonf2984462008-04-10 16:19:33 -04003629{
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003630 struct btrfs_device *device = bio->bi_private;
Matthew Wilcox (Oracle)617fb102024-04-20 03:49:58 +01003631 struct folio_iter fi;
Stefan Behrens442a4f62012-05-25 16:06:08 +02003632
Matthew Wilcox (Oracle)617fb102024-04-20 03:49:58 +01003633 bio_for_each_folio_all(fi, bio) {
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003634 if (bio->bi_status) {
3635 btrfs_warn_rl_in_rcu(device->fs_info,
Matthew Wilcox (Oracle)617fb102024-04-20 03:49:58 +01003636 "lost super block write due to IO error on %s (%d)",
Qu Wenruocb3e2172022-11-13 09:32:07 +08003637 btrfs_dev_name(device),
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003638 blk_status_to_errno(bio->bi_status));
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003639 btrfs_dev_stat_inc_and_print(device,
3640 BTRFS_DEV_STAT_WRITE_ERRS);
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003641 /* Ensure failure if the primary sb fails. */
3642 if (bio->bi_opf & REQ_FUA)
3643 atomic_add(BTRFS_SUPER_PRIMARY_WRITE_ERROR,
3644 &device->sb_write_errors);
3645 else
3646 atomic_inc(&device->sb_write_errors);
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003647 }
Matthew Wilcox (Oracle)617fb102024-04-20 03:49:58 +01003648 folio_unlock(fi.folio);
3649 folio_put(fi.folio);
Chris Masonf2984462008-04-10 16:19:33 -04003650 }
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003651
3652 bio_put(bio);
Chris Masonf2984462008-04-10 16:19:33 -04003653}
3654
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003655struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev,
Qu Wenruoa05d3c92022-08-24 20:16:22 +08003656 int copy_num, bool drop_cache)
Anand Jain29c36d72015-08-14 18:32:58 +08003657{
Anand Jain29c36d72015-08-14 18:32:58 +08003658 struct btrfs_super_block *super;
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003659 struct page *page;
Naohiro Aota12659252020-11-10 20:26:14 +09003660 u64 bytenr, bytenr_orig;
Al Viro224941e2024-04-11 15:53:37 +01003661 struct address_space *mapping = bdev->bd_mapping;
Naohiro Aota12659252020-11-10 20:26:14 +09003662 int ret;
Anand Jain29c36d72015-08-14 18:32:58 +08003663
Naohiro Aota12659252020-11-10 20:26:14 +09003664 bytenr_orig = btrfs_sb_offset(copy_num);
3665 ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr);
3666 if (ret == -ENOENT)
3667 return ERR_PTR(-EINVAL);
3668 else if (ret)
3669 return ERR_PTR(ret);
3670
Christoph Hellwigcda00eb2021-10-18 12:11:12 +02003671 if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev))
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003672 return ERR_PTR(-EINVAL);
Anand Jain29c36d72015-08-14 18:32:58 +08003673
Qu Wenruoa05d3c92022-08-24 20:16:22 +08003674 if (drop_cache) {
3675 /* This should only be called with the primary sb. */
3676 ASSERT(copy_num == 0);
3677
3678 /*
3679 * Drop the page of the primary superblock, so later read will
3680 * always read from the device.
3681 */
3682 invalidate_inode_pages2_range(mapping,
3683 bytenr >> PAGE_SHIFT,
3684 (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT);
3685 }
3686
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003687 page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS);
3688 if (IS_ERR(page))
3689 return ERR_CAST(page);
Anand Jain29c36d72015-08-14 18:32:58 +08003690
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003691 super = page_address(page);
Anand Jain96c2e062020-09-30 21:09:52 +08003692 if (btrfs_super_magic(super) != BTRFS_MAGIC) {
3693 btrfs_release_disk_super(super);
3694 return ERR_PTR(-ENODATA);
3695 }
3696
Naohiro Aota12659252020-11-10 20:26:14 +09003697 if (btrfs_super_bytenr(super) != bytenr_orig) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003698 btrfs_release_disk_super(super);
3699 return ERR_PTR(-EINVAL);
Anand Jain29c36d72015-08-14 18:32:58 +08003700 }
3701
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003702 return super;
Anand Jain29c36d72015-08-14 18:32:58 +08003703}
3704
3705
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003706struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003707{
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003708 struct btrfs_super_block *super, *latest = NULL;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003709 int i;
3710 u64 transid = 0;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003711
3712 /* we would like to check all the supers, but that would make
3713 * a btrfs mount succeed after a mkfs from a different FS.
3714 * So, we need to add a special mount option to scan for
3715 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
3716 */
3717 for (i = 0; i < 1; i++) {
Qu Wenruoa05d3c92022-08-24 20:16:22 +08003718 super = btrfs_read_dev_one_super(bdev, i, false);
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003719 if (IS_ERR(super))
Yan Zhenga512bbf2008-12-08 16:46:26 -05003720 continue;
3721
Yan Zhenga512bbf2008-12-08 16:46:26 -05003722 if (!latest || btrfs_super_generation(super) > transid) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003723 if (latest)
3724 btrfs_release_disk_super(super);
3725
3726 latest = super;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003727 transid = btrfs_super_generation(super);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003728 }
3729 }
Anand Jain92fc03fbd2015-08-14 18:32:51 +08003730
Johannes Thumshirn8f323802020-02-14 00:24:32 +09003731 return super;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003732}
3733
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003734/*
David Sterbaabbb3b82017-06-16 00:50:33 +02003735 * Write superblock @sb to the @device. Do not wait for completion, all the
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003736 * folios we use for writing are locked.
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003737 *
David Sterbaabbb3b82017-06-16 00:50:33 +02003738 * Write @max_mirrors copies of the superblock, where 0 means default that fit
3739 * the expected device size at commit time. Note that max_mirrors must be
3740 * same for write and wait phases.
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003741 *
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003742 * Return number of errors when folio is not found or submission fails.
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003743 */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003744static int write_dev_supers(struct btrfs_device *device,
David Sterbaabbb3b82017-06-16 00:50:33 +02003745 struct btrfs_super_block *sb, int max_mirrors)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003746{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003747 struct btrfs_fs_info *fs_info = device->fs_info;
Al Viro224941e2024-04-11 15:53:37 +01003748 struct address_space *mapping = device->bdev->bd_mapping;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003749 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003750 int i;
Naohiro Aota12659252020-11-10 20:26:14 +09003751 int ret;
3752 u64 bytenr, bytenr_orig;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003753
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003754 atomic_set(&device->sb_write_errors, 0);
3755
Yan Zhenga512bbf2008-12-08 16:46:26 -05003756 if (max_mirrors == 0)
3757 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3758
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003759 shash->tfm = fs_info->csum_shash;
3760
Yan Zhenga512bbf2008-12-08 16:46:26 -05003761 for (i = 0; i < max_mirrors; i++) {
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003762 struct folio *folio;
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003763 struct bio *bio;
3764 struct btrfs_super_block *disk_super;
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003765 size_t offset;
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003766
Naohiro Aota12659252020-11-10 20:26:14 +09003767 bytenr_orig = btrfs_sb_offset(i);
3768 ret = btrfs_sb_log_location(device, i, WRITE, &bytenr);
3769 if (ret == -ENOENT) {
3770 continue;
3771 } else if (ret < 0) {
3772 btrfs_err(device->fs_info,
3773 "couldn't get super block location for mirror %d",
3774 i);
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003775 atomic_inc(&device->sb_write_errors);
Naohiro Aota12659252020-11-10 20:26:14 +09003776 continue;
3777 }
Miao Xie935e5cc2014-09-03 21:35:33 +08003778 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3779 device->commit_total_bytes)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003780 break;
3781
Naohiro Aota12659252020-11-10 20:26:14 +09003782 btrfs_set_super_bytenr(sb, bytenr_orig);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003783
Eric Biggersfd080012020-04-30 23:51:59 -07003784 crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE,
3785 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE,
3786 sb->csum);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003787
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003788 folio = __filemap_get_folio(mapping, bytenr >> PAGE_SHIFT,
3789 FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
3790 GFP_NOFS);
3791 if (IS_ERR(folio)) {
David Sterbaabbb3b82017-06-16 00:50:33 +02003792 btrfs_err(device->fs_info,
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003793 "couldn't get super block page for bytenr %llu",
David Sterbaabbb3b82017-06-16 00:50:33 +02003794 bytenr);
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003795 atomic_inc(&device->sb_write_errors);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003796 continue;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003797 }
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003798 ASSERT(folio_order(folio) == 0);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003799
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003800 offset = offset_in_folio(folio, bytenr);
3801 disk_super = folio_address(folio) + offset;
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003802 memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE);
David Sterbaabbb3b82017-06-16 00:50:33 +02003803
Chris Mason387125f2011-11-18 15:07:51 -05003804 /*
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003805 * Directly use bios here instead of relying on the page cache
3806 * to do I/O, so we don't lose the ability to do integrity
3807 * checking.
Chris Mason387125f2011-11-18 15:07:51 -05003808 */
Christoph Hellwig07888c662022-01-24 10:11:05 +01003809 bio = bio_alloc(device->bdev, 1,
3810 REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO,
3811 GFP_NOFS);
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003812 bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT;
3813 bio->bi_private = device;
3814 bio->bi_end_io = btrfs_end_super_write;
Matthew Wilcox (Oracle)f93ee0d2024-04-20 03:49:57 +01003815 bio_add_folio_nofail(bio, folio, BTRFS_SUPER_INFO_SIZE, offset);
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003816
3817 /*
3818 * We FUA only the first super block. The others we allow to
3819 * go down lazy and there's a short window where the on-disk
3820 * copies might still contain the older version.
3821 */
Omar Sandoval1b9e6192017-12-05 22:54:02 -08003822 if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003823 bio->bi_opf |= REQ_FUA;
Christoph Hellwig58ff51f2022-04-04 06:45:18 +02003824 submit_bio(bio);
Naohiro Aota8376d9e12021-08-19 21:19:14 +09003825
3826 if (btrfs_advance_sb_log(device, i))
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003827 atomic_inc(&device->sb_write_errors);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003828 }
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003829 return atomic_read(&device->sb_write_errors) < i ? 0 : -1;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003830}
3831
Chris Mason387125f2011-11-18 15:07:51 -05003832/*
David Sterbaabbb3b82017-06-16 00:50:33 +02003833 * Wait for write completion of superblocks done by write_dev_supers,
3834 * @max_mirrors same for write and wait phases.
3835 *
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003836 * Return -1 if primary super block write failed or when there were no super block
3837 * copies written. Otherwise 0.
David Sterbaabbb3b82017-06-16 00:50:33 +02003838 */
3839static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
3840{
David Sterbaabbb3b82017-06-16 00:50:33 +02003841 int i;
3842 int errors = 0;
Howard McLauchlanb6a535f2018-02-02 11:09:01 -08003843 bool primary_failed = false;
Naohiro Aota12659252020-11-10 20:26:14 +09003844 int ret;
David Sterbaabbb3b82017-06-16 00:50:33 +02003845 u64 bytenr;
3846
3847 if (max_mirrors == 0)
3848 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3849
3850 for (i = 0; i < max_mirrors; i++) {
Matthew Wilcox (Oracle)c94b7342024-04-20 03:49:56 +01003851 struct folio *folio;
Johannes Thumshirn314b6dd2020-02-14 00:24:33 +09003852
Naohiro Aota12659252020-11-10 20:26:14 +09003853 ret = btrfs_sb_log_location(device, i, READ, &bytenr);
3854 if (ret == -ENOENT) {
3855 break;
3856 } else if (ret < 0) {
3857 errors++;
3858 if (i == 0)
3859 primary_failed = true;
3860 continue;
3861 }
David Sterbaabbb3b82017-06-16 00:50:33 +02003862 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3863 device->commit_total_bytes)
3864 break;
3865
Linus Torvalds38da32e2024-05-21 09:51:42 -07003866 folio = filemap_get_folio(device->bdev->bd_mapping,
Matthew Wilcox (Oracle)c94b7342024-04-20 03:49:56 +01003867 bytenr >> PAGE_SHIFT);
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003868 /* If the folio has been removed, then we know it completed. */
3869 if (IS_ERR(folio))
David Sterbaabbb3b82017-06-16 00:50:33 +02003870 continue;
Matthew Wilcox (Oracle)c94b7342024-04-20 03:49:56 +01003871 ASSERT(folio_order(folio) == 0);
3872
3873 /* Folio will be unlocked once the write completes. */
3874 folio_wait_locked(folio);
Matthew Wilcox (Oracle)c94b7342024-04-20 03:49:56 +01003875 folio_put(folio);
David Sterbaabbb3b82017-06-16 00:50:33 +02003876 }
3877
Matthew Wilcox (Oracle)bc009652024-04-20 03:49:59 +01003878 errors += atomic_read(&device->sb_write_errors);
3879 if (errors >= BTRFS_SUPER_PRIMARY_WRITE_ERROR)
3880 primary_failed = true;
Howard McLauchlanb6a535f2018-02-02 11:09:01 -08003881 if (primary_failed) {
3882 btrfs_err(device->fs_info, "error writing primary super block to device %llu",
3883 device->devid);
3884 return -1;
3885 }
3886
David Sterbaabbb3b82017-06-16 00:50:33 +02003887 return errors < i ? 0 : -1;
3888}
3889
3890/*
Chris Mason387125f2011-11-18 15:07:51 -05003891 * endio for the write_dev_flush, this will wake anyone waiting
3892 * for the barrier when it is done
3893 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003894static void btrfs_end_empty_barrier(struct bio *bio)
Chris Mason387125f2011-11-18 15:07:51 -05003895{
Christoph Hellwigf9e69aa2022-04-06 08:12:24 +02003896 bio_uninit(bio);
David Sterbae0ae9992017-06-06 17:06:06 +02003897 complete(bio->bi_private);
Chris Mason387125f2011-11-18 15:07:51 -05003898}
3899
3900/*
Anand Jain4fc64412017-06-13 17:05:41 +08003901 * Submit a flush request to the device if it supports it. Error handling is
3902 * done in the waiting counterpart.
Chris Mason387125f2011-11-18 15:07:51 -05003903 */
Anand Jain4fc64412017-06-13 17:05:41 +08003904static void write_dev_flush(struct btrfs_device *device)
Chris Mason387125f2011-11-18 15:07:51 -05003905{
Christoph Hellwigf9e69aa2022-04-06 08:12:24 +02003906 struct bio *bio = &device->flush_bio;
Chris Mason387125f2011-11-18 15:07:51 -05003907
Anand Jainbfd3ea92023-03-27 17:53:07 +08003908 device->last_flush_error = BLK_STS_OK;
3909
Christoph Hellwigf9e69aa2022-04-06 08:12:24 +02003910 bio_init(bio, device->bdev, NULL, 0,
3911 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
Chris Mason387125f2011-11-18 15:07:51 -05003912 bio->bi_end_io = btrfs_end_empty_barrier;
Chris Mason387125f2011-11-18 15:07:51 -05003913 init_completion(&device->flush_wait);
3914 bio->bi_private = &device->flush_wait;
Christoph Hellwig58ff51f2022-04-04 06:45:18 +02003915 submit_bio(bio);
Anand Jain1c3063b2017-12-04 12:54:56 +08003916 set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
Anand Jain4fc64412017-06-13 17:05:41 +08003917}
Chris Mason387125f2011-11-18 15:07:51 -05003918
Anand Jain4fc64412017-06-13 17:05:41 +08003919/*
3920 * If the flush bio has been submitted by write_dev_flush, wait for it.
Anand Jain1b465782023-03-27 17:53:09 +08003921 * Return true for any error, and false otherwise.
Anand Jain4fc64412017-06-13 17:05:41 +08003922 */
Anand Jain1b465782023-03-27 17:53:09 +08003923static bool wait_dev_flush(struct btrfs_device *device)
Anand Jain4fc64412017-06-13 17:05:41 +08003924{
Christoph Hellwigf9e69aa2022-04-06 08:12:24 +02003925 struct bio *bio = &device->flush_bio;
Anand Jain4fc64412017-06-13 17:05:41 +08003926
Anand Jain7e812f22023-03-27 17:53:10 +08003927 if (!test_and_clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
Anand Jain1b465782023-03-27 17:53:09 +08003928 return false;
Anand Jain4fc64412017-06-13 17:05:41 +08003929
David Sterba2980d572017-06-15 16:04:26 +02003930 wait_for_completion_io(&device->flush_wait);
Anand Jain4fc64412017-06-13 17:05:41 +08003931
Anand Jainbfd3ea92023-03-27 17:53:07 +08003932 if (bio->bi_status) {
3933 device->last_flush_error = bio->bi_status;
3934 btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_FLUSH_ERRS);
Anand Jain1b465782023-03-27 17:53:09 +08003935 return true;
Anand Jainbfd3ea92023-03-27 17:53:07 +08003936 }
3937
Anand Jain1b465782023-03-27 17:53:09 +08003938 return false;
Chris Mason387125f2011-11-18 15:07:51 -05003939}
3940
3941/*
3942 * send an empty flush down to each device in parallel,
3943 * then wait for them
3944 */
3945static int barrier_all_devices(struct btrfs_fs_info *info)
3946{
3947 struct list_head *head;
3948 struct btrfs_device *dev;
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003949 int errors_wait = 0;
Chris Mason387125f2011-11-18 15:07:51 -05003950
David Sterba1538e6c2017-06-16 00:28:47 +02003951 lockdep_assert_held(&info->fs_devices->device_list_mutex);
Chris Mason387125f2011-11-18 15:07:51 -05003952 /* send down all the barriers */
3953 head = &info->fs_devices->devices;
David Sterba1538e6c2017-06-16 00:28:47 +02003954 list_for_each_entry(dev, head, dev_list) {
Anand Jaine6e674b2017-12-04 12:54:54 +08003955 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
Hidetoshi Setof88ba6a2014-02-05 16:34:38 +09003956 continue;
Anand Jaincea7c8b2017-06-13 17:05:40 +08003957 if (!dev->bdev)
Chris Mason387125f2011-11-18 15:07:51 -05003958 continue;
Anand Jaine12c9622017-12-04 12:54:53 +08003959 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08003960 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Mason387125f2011-11-18 15:07:51 -05003961 continue;
3962
Anand Jain4fc64412017-06-13 17:05:41 +08003963 write_dev_flush(dev);
Chris Mason387125f2011-11-18 15:07:51 -05003964 }
3965
3966 /* wait for all the barriers */
David Sterba1538e6c2017-06-16 00:28:47 +02003967 list_for_each_entry(dev, head, dev_list) {
Anand Jaine6e674b2017-12-04 12:54:54 +08003968 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
Hidetoshi Setof88ba6a2014-02-05 16:34:38 +09003969 continue;
Chris Mason387125f2011-11-18 15:07:51 -05003970 if (!dev->bdev) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003971 errors_wait++;
Chris Mason387125f2011-11-18 15:07:51 -05003972 continue;
3973 }
Anand Jaine12c9622017-12-04 12:54:53 +08003974 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08003975 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Mason387125f2011-11-18 15:07:51 -05003976 continue;
3977
Anand Jain1b465782023-03-27 17:53:09 +08003978 if (wait_dev_flush(dev))
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003979 errors_wait++;
Chris Mason387125f2011-11-18 15:07:51 -05003980 }
Anand Jain401b41e2017-05-06 07:17:54 +08003981
Anand Jainde38a202023-03-27 17:53:08 +08003982 /*
3983 * Checks last_flush_error of disks in order to determine the device
3984 * state.
3985 */
3986 if (errors_wait && !btrfs_check_rw_degradable(info, NULL))
3987 return -EIO;
3988
Chris Mason387125f2011-11-18 15:07:51 -05003989 return 0;
3990}
3991
Zhao Lei943c6e92015-08-19 15:54:15 +08003992int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
3993{
Zhao Lei8789f4f2015-09-15 21:08:07 +08003994 int raid_type;
3995 int min_tolerated = INT_MAX;
Zhao Lei943c6e92015-08-19 15:54:15 +08003996
Zhao Lei8789f4f2015-09-15 21:08:07 +08003997 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
3998 (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
David Sterba8c3e3582019-05-17 11:43:36 +02003999 min_tolerated = min_t(int, min_tolerated,
Zhao Lei8789f4f2015-09-15 21:08:07 +08004000 btrfs_raid_array[BTRFS_RAID_SINGLE].
4001 tolerated_failures);
Zhao Lei943c6e92015-08-19 15:54:15 +08004002
Zhao Lei8789f4f2015-09-15 21:08:07 +08004003 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4004 if (raid_type == BTRFS_RAID_SINGLE)
4005 continue;
Anand Jain41a6e892018-04-25 19:01:43 +08004006 if (!(flags & btrfs_raid_array[raid_type].bg_flag))
Zhao Lei8789f4f2015-09-15 21:08:07 +08004007 continue;
David Sterba8c3e3582019-05-17 11:43:36 +02004008 min_tolerated = min_t(int, min_tolerated,
Zhao Lei8789f4f2015-09-15 21:08:07 +08004009 btrfs_raid_array[raid_type].
4010 tolerated_failures);
4011 }
Zhao Lei943c6e92015-08-19 15:54:15 +08004012
Zhao Lei8789f4f2015-09-15 21:08:07 +08004013 if (min_tolerated == INT_MAX) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004014 pr_warn("BTRFS: unknown raid flag: %llu", flags);
Zhao Lei8789f4f2015-09-15 21:08:07 +08004015 min_tolerated = 0;
4016 }
4017
4018 return min_tolerated;
Zhao Lei943c6e92015-08-19 15:54:15 +08004019}
4020
David Sterbaeece6a92017-02-10 19:04:32 +01004021int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
Chris Masonf2984462008-04-10 16:19:33 -04004022{
Chris Masone5e9a522009-06-10 15:17:02 -04004023 struct list_head *head;
Chris Masonf2984462008-04-10 16:19:33 -04004024 struct btrfs_device *dev;
Chris Masona061fc82008-05-07 11:43:44 -04004025 struct btrfs_super_block *sb;
Chris Masonf2984462008-04-10 16:19:33 -04004026 struct btrfs_dev_item *dev_item;
Chris Masonf2984462008-04-10 16:19:33 -04004027 int ret;
4028 int do_barriers;
Chris Masona236aed2008-04-29 09:38:00 -04004029 int max_errors;
4030 int total_errors = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004031 u64 flags;
Chris Masonf2984462008-04-10 16:19:33 -04004032
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004033 do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
Liu Bofed3b382017-09-13 12:25:21 -06004034
4035 /*
4036 * max_mirrors == 0 indicates we're from commit_transaction,
4037 * not from fsync where the tree roots in fs_info have not
4038 * been consistent on disk.
4039 */
4040 if (max_mirrors == 0)
4041 backup_super_roots(fs_info);
Chris Masonf2984462008-04-10 16:19:33 -04004042
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004043 sb = fs_info->super_for_commit;
Chris Masona061fc82008-05-07 11:43:44 -04004044 dev_item = &sb->dev_item;
Chris Masone5e9a522009-06-10 15:17:02 -04004045
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004046 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4047 head = &fs_info->fs_devices->devices;
4048 max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
Chris Mason387125f2011-11-18 15:07:51 -05004049
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004050 if (do_barriers) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004051 ret = barrier_all_devices(fs_info);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004052 if (ret) {
4053 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004054 &fs_info->fs_devices->device_list_mutex);
4055 btrfs_handle_fs_error(fs_info, ret,
4056 "errors while submitting device barriers.");
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004057 return ret;
4058 }
4059 }
Chris Mason387125f2011-11-18 15:07:51 -05004060
David Sterba1538e6c2017-06-16 00:28:47 +02004061 list_for_each_entry(dev, head, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04004062 if (!dev->bdev) {
4063 total_errors++;
4064 continue;
4065 }
Anand Jaine12c9622017-12-04 12:54:53 +08004066 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08004067 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Masondfe25022008-05-13 13:46:40 -04004068 continue;
4069
Yan Zheng2b820322008-11-17 21:11:30 -05004070 btrfs_set_stack_device_generation(dev_item, 0);
Chris Masona061fc82008-05-07 11:43:44 -04004071 btrfs_set_stack_device_type(dev_item, dev->type);
4072 btrfs_set_stack_device_id(dev_item, dev->devid);
Miao Xie7df69d32014-07-24 11:37:13 +08004073 btrfs_set_stack_device_total_bytes(dev_item,
Miao Xie935e5cc2014-09-03 21:35:33 +08004074 dev->commit_total_bytes);
Miao Xiece7213c2014-09-03 21:35:34 +08004075 btrfs_set_stack_device_bytes_used(dev_item,
4076 dev->commit_bytes_used);
Chris Masona061fc82008-05-07 11:43:44 -04004077 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
4078 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
4079 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
4080 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02004081 memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
4082 BTRFS_FSID_SIZE);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004083
Chris Masona061fc82008-05-07 11:43:44 -04004084 flags = btrfs_super_flags(sb);
4085 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonf2984462008-04-10 16:19:33 -04004086
Qu Wenruo75cb8572018-05-11 13:35:27 +08004087 ret = btrfs_validate_write_super(fs_info, sb);
4088 if (ret < 0) {
4089 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4090 btrfs_handle_fs_error(fs_info, -EUCLEAN,
4091 "unexpected superblock corruption detected");
4092 return -EUCLEAN;
4093 }
4094
David Sterbaabbb3b82017-06-16 00:50:33 +02004095 ret = write_dev_supers(dev, sb, max_mirrors);
Chris Masona236aed2008-04-29 09:38:00 -04004096 if (ret)
4097 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04004098 }
Chris Masona236aed2008-04-29 09:38:00 -04004099 if (total_errors > max_errors) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004100 btrfs_err(fs_info, "%d errors while writing supers",
4101 total_errors);
4102 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103
Stefan Behrens9d565ba2013-08-09 17:08:40 +02004104 /* FUA is masked off if unsupported and can't be the reason */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004105 btrfs_handle_fs_error(fs_info, -EIO,
4106 "%d errors while writing supers",
4107 total_errors);
Stefan Behrens9d565ba2013-08-09 17:08:40 +02004108 return -EIO;
Chris Masona236aed2008-04-29 09:38:00 -04004109 }
Chris Masonf2984462008-04-10 16:19:33 -04004110
Yan Zhenga512bbf2008-12-08 16:46:26 -05004111 total_errors = 0;
David Sterba1538e6c2017-06-16 00:28:47 +02004112 list_for_each_entry(dev, head, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04004113 if (!dev->bdev)
4114 continue;
Anand Jaine12c9622017-12-04 12:54:53 +08004115 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08004116 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Masondfe25022008-05-13 13:46:40 -04004117 continue;
4118
David Sterbaabbb3b82017-06-16 00:50:33 +02004119 ret = wait_dev_supers(dev, max_mirrors);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004120 if (ret)
4121 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04004122 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004123 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Masona236aed2008-04-29 09:38:00 -04004124 if (total_errors > max_errors) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004125 btrfs_handle_fs_error(fs_info, -EIO,
4126 "%d errors while writing supers",
4127 total_errors);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 return -EIO;
Chris Masona236aed2008-04-29 09:38:00 -04004129 }
Chris Masonf2984462008-04-10 16:19:33 -04004130 return 0;
4131}
4132
Miao Xiecb517ea2013-05-15 07:48:19 +00004133/* Drop a fs root from the radix tree and free it. */
4134void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
4135 struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -04004136{
Josef Bacik4785e242020-02-14 16:11:45 -05004137 bool drop_ref = false;
4138
David Sterbafc7cbcd2022-07-15 13:59:21 +02004139 spin_lock(&fs_info->fs_roots_radix_lock);
4140 radix_tree_delete(&fs_info->fs_roots_radix,
Josef Bacike094f482024-04-15 16:16:23 -04004141 (unsigned long)btrfs_root_id(root));
David Sterbafc7cbcd2022-07-15 13:59:21 +02004142 if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state))
Josef Bacik4785e242020-02-14 16:11:45 -05004143 drop_ref = true;
David Sterbafc7cbcd2022-07-15 13:59:21 +02004144 spin_unlock(&fs_info->fs_roots_radix_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004145
Josef Bacik84961532021-10-05 16:35:25 -04004146 if (BTRFS_FS_ERROR(fs_info)) {
Josef Bacikef679632020-03-24 10:47:52 -04004147 ASSERT(root->log_root == NULL);
Liu Bo1c1ea4f2016-07-19 15:36:05 -07004148 if (root->reloc_root) {
Josef Bacik00246522020-01-24 09:33:01 -05004149 btrfs_put_root(root->reloc_root);
Liu Bo1c1ea4f2016-07-19 15:36:05 -07004150 root->reloc_root = NULL;
4151 }
4152 }
Liu Bo33217192013-02-27 13:28:24 +00004153
Josef Bacik4785e242020-02-14 16:11:45 -05004154 if (drop_ref)
4155 btrfs_put_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -04004156}
4157
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004158int btrfs_commit_super(struct btrfs_fs_info *fs_info)
Yan Zhengc146afa2008-11-12 14:34:12 -05004159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004160 mutex_lock(&fs_info->cleaner_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004161 btrfs_run_delayed_iputs(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004162 mutex_unlock(&fs_info->cleaner_mutex);
4163 wake_up_process(fs_info->cleaner_kthread);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004164
4165 /* wait until ongoing cleanup work done */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004166 down_write(&fs_info->cleanup_work_sem);
4167 up_write(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004168
Filipe Mananaded980e2024-05-22 09:26:44 +01004169 return btrfs_commit_current_transaction(fs_info->tree_root);
Yan Zhengc146afa2008-11-12 14:34:12 -05004170}
4171
Qu Wenruo36c86a92021-12-16 19:47:36 +08004172static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
4173{
4174 struct btrfs_transaction *trans;
4175 struct btrfs_transaction *tmp;
4176 bool found = false;
4177
Qu Wenruo36c86a92021-12-16 19:47:36 +08004178 /*
4179 * This function is only called at the very end of close_ctree(),
4180 * thus no other running transaction, no need to take trans_lock.
4181 */
4182 ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags));
4183 list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) {
4184 struct extent_state *cached = NULL;
4185 u64 dirty_bytes = 0;
4186 u64 cur = 0;
4187 u64 found_start;
4188 u64 found_end;
4189
4190 found = true;
Filipe Mananae5860f82023-06-30 16:03:49 +01004191 while (find_first_extent_bit(&trans->dirty_pages, cur,
Qu Wenruo36c86a92021-12-16 19:47:36 +08004192 &found_start, &found_end, EXTENT_DIRTY, &cached)) {
4193 dirty_bytes += found_end + 1 - found_start;
4194 cur = found_end + 1;
4195 }
4196 btrfs_warn(fs_info,
4197 "transaction %llu (with %llu dirty metadata bytes) is not committed",
4198 trans->transid, dirty_bytes);
4199 btrfs_cleanup_one_transaction(trans, fs_info);
4200
4201 if (trans == fs_info->running_transaction)
4202 fs_info->running_transaction = NULL;
4203 list_del_init(&trans->list);
4204
4205 btrfs_put_transaction(trans);
4206 trace_btrfs_transaction_commit(fs_info);
4207 }
4208 ASSERT(!found);
4209}
4210
David Sterbab105e922019-10-01 19:57:35 +02004211void __cold close_ctree(struct btrfs_fs_info *fs_info)
Chris Masoneb60cea2007-02-02 09:18:22 -05004212{
Yan Zhengc146afa2008-11-12 14:34:12 -05004213 int ret;
Chris Masone089f052007-03-16 16:20:31 -04004214
Josef Bacikafcdd122016-09-02 15:40:02 -04004215 set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
Filipe Manana31e70e52022-05-18 10:41:48 +01004216
4217 /*
Filipe Manana8a1f1e32022-09-08 12:31:50 +01004218 * If we had UNFINISHED_DROPS we could still be processing them, so
4219 * clear that bit and wake up relocation so it can stop.
4220 * We must do this before stopping the block group reclaim task, because
4221 * at btrfs_relocate_block_group() we wait for this bit, and after the
4222 * wait we stop with -EINTR if btrfs_fs_closing() returns non-zero - we
4223 * have just set BTRFS_FS_CLOSING_START, so btrfs_fs_closing() will
4224 * return 1.
4225 */
4226 btrfs_wake_unfinished_drop(fs_info);
4227
4228 /*
Filipe Manana31e70e52022-05-18 10:41:48 +01004229 * We may have the reclaim task running and relocating a data block group,
4230 * in which case it may create delayed iputs. So stop it before we park
4231 * the cleaner kthread otherwise we can get new delayed iputs after
4232 * parking the cleaner, and that can make the async reclaim task to hang
4233 * if it's waiting for delayed iputs to complete, since the cleaner is
4234 * parked and can not run delayed iputs - this will make us hang when
4235 * trying to stop the async reclaim task.
4236 */
4237 cancel_work_sync(&fs_info->reclaim_bgs_work);
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07004238 /*
4239 * We don't want the cleaner to start new transactions, add more delayed
4240 * iputs, etc. while we're closing. We can't use kthread_stop() yet
4241 * because that frees the task_struct, and the transaction kthread might
4242 * still try to wake up the cleaner.
4243 */
4244 kthread_park(fs_info->cleaner_kthread);
Chris Masona2135012008-06-25 16:01:30 -04004245
Justin Maggard7343dd62015-11-04 15:56:16 -08004246 /* wait for the qgroup rescan worker to stop */
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04004247 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard7343dd62015-11-04 15:56:16 -08004248
Stefan Behrens803b2f52013-08-15 17:11:21 +02004249 /* wait for the uuid_scan task to finish */
4250 down(&fs_info->uuid_tree_rescan_sem);
4251 /* avoid complains from lockdep et al., set sem back to initial state */
4252 up(&fs_info->uuid_tree_rescan_sem);
4253
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004254 /* pause restriper - we want to resume on mount */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004255 btrfs_pause_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004256
Stefan Behrens8dabb742012-11-06 13:15:27 +01004257 btrfs_dev_replace_suspend_for_unmount(fs_info);
4258
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004259 btrfs_scrub_cancel(fs_info);
Chris Mason4cb53002011-05-24 15:35:30 -04004260
4261 /* wait for any defraggers to finish */
4262 wait_event(fs_info->transaction_wait,
4263 (atomic_read(&fs_info->defrag_running) == 0));
4264
4265 /* clear out the rbtree of defraggable inodes */
Miao Xie26176e72012-11-26 09:26:20 +00004266 btrfs_cleanup_defrag_inodes(fs_info);
Chris Mason4cb53002011-05-24 15:35:30 -04004267
Filipe Mananaa362bb82022-09-08 12:31:51 +01004268 /*
4269 * After we parked the cleaner kthread, ordered extents may have
4270 * completed and created new delayed iputs. If one of the async reclaim
4271 * tasks is running and in the RUN_DELAYED_IPUTS flush state, then we
4272 * can hang forever trying to stop it, because if a delayed iput is
4273 * added after it ran btrfs_run_delayed_iputs() and before it called
4274 * btrfs_wait_on_delayed_iputs(), it will hang forever since there is
4275 * no one else to run iputs.
4276 *
4277 * So wait for all ongoing ordered extents to complete and then run
4278 * delayed iputs. This works because once we reach this point no one
4279 * can either create new ordered extents nor create delayed iputs
4280 * through some other means.
4281 *
4282 * Also note that btrfs_wait_ordered_roots() is not safe here, because
4283 * it waits for BTRFS_ORDERED_COMPLETE to be set on an ordered extent,
4284 * but the delayed iput for the respective inode is made only when doing
4285 * the final btrfs_put_ordered_extent() (which must happen at
4286 * btrfs_finish_ordered_io() when we are unmounting).
4287 */
4288 btrfs_flush_workqueue(fs_info->endio_write_workers);
4289 /* Ordered extents for free space inodes. */
4290 btrfs_flush_workqueue(fs_info->endio_freespace_worker);
4291 btrfs_run_delayed_iputs(fs_info);
4292
Miao Xie21c7e752014-05-13 17:29:04 -07004293 cancel_work_sync(&fs_info->async_reclaim_work);
Josef Bacik57056742020-07-21 10:22:33 -04004294 cancel_work_sync(&fs_info->async_data_reclaim_work);
Josef Bacik576fa342020-10-09 09:28:22 -04004295 cancel_work_sync(&fs_info->preempt_reclaim_work);
Miao Xie21c7e752014-05-13 17:29:04 -07004296
Dennis Zhoub0643e52019-12-13 16:22:14 -08004297 /* Cancel or finish ongoing discard work */
4298 btrfs_discard_cleanup(fs_info);
4299
David Howellsbc98a422017-07-17 08:45:34 +01004300 if (!sb_rdonly(fs_info->sb)) {
Jeff Mahoneye44163e2015-06-15 09:41:18 -04004301 /*
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07004302 * The cleaner kthread is stopped, so do one final pass over
4303 * unused block groups.
Jeff Mahoneye44163e2015-06-15 09:41:18 -04004304 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004305 btrfs_delete_unused_bgs(fs_info);
Jeff Mahoneye44163e2015-06-15 09:41:18 -04004306
Filipe Mananaf0cc2cd2020-02-28 13:04:36 +00004307 /*
4308 * There might be existing delayed inode workers still running
4309 * and holding an empty delayed inode item. We must wait for
4310 * them to complete first because they can create a transaction.
4311 * This happens when someone calls btrfs_balance_delayed_items()
4312 * and then a transaction commit runs the same delayed nodes
4313 * before any delayed worker has done something with the nodes.
4314 * We must wait for any worker here and not at transaction
4315 * commit time since that could cause a deadlock.
4316 * This is a very rare case.
4317 */
4318 btrfs_flush_workqueue(fs_info->delayed_workers);
4319
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004320 ret = btrfs_commit_super(fs_info);
liuboacce9522011-01-06 19:30:25 +08004321 if (ret)
Eric Sandeen04892342014-08-01 18:12:36 -05004322 btrfs_err(fs_info, "commit super ret %d", ret);
liuboacce9522011-01-06 19:30:25 +08004323 }
4324
Josef Bacik84961532021-10-05 16:35:25 -04004325 if (BTRFS_FS_ERROR(fs_info))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004326 btrfs_error_commit_super(fs_info);
Chris Masoned2ff2c2007-03-01 18:59:40 -05004327
Al Viroe3029d92011-11-17 00:56:18 -05004328 kthread_stop(fs_info->transaction_kthread);
4329 kthread_stop(fs_info->cleaner_kthread);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004330
Josef Bacike1878312018-09-28 07:18:03 -04004331 ASSERT(list_empty(&fs_info->delayed_iputs));
Josef Bacikafcdd122016-09-02 15:40:02 -04004332 set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
Yan Zhengf25784b2009-07-28 08:41:57 -04004333
Qu Wenruo59582532020-06-10 09:04:44 +08004334 if (btrfs_check_quota_leak(fs_info)) {
4335 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4336 btrfs_err(fs_info, "qgroup reserved space leaked");
4337 }
4338
Eric Sandeen04892342014-08-01 18:12:36 -05004339 btrfs_free_qgroup_config(fs_info);
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004340 ASSERT(list_empty(&fs_info->delalloc_roots));
Arne Jansenbcef60f2011-09-13 15:23:30 +02004341
Miao Xie963d6782013-01-29 10:10:51 +00004342 if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
Eric Sandeen04892342014-08-01 18:12:36 -05004343 btrfs_info(fs_info, "at unmount delalloc count %lld",
Miao Xie963d6782013-01-29 10:10:51 +00004344 percpu_counter_sum(&fs_info->delalloc_bytes));
Chris Masonb0c68f82008-01-31 11:05:37 -05004345 }
Yanbcc63ab2008-07-30 16:29:20 -04004346
Josef Bacik5deb17e2020-10-09 09:28:20 -04004347 if (percpu_counter_sum(&fs_info->ordered_bytes))
Josef Bacik4297ff82019-04-10 15:56:09 -04004348 btrfs_info(fs_info, "at unmount dio bytes count %lld",
Josef Bacik5deb17e2020-10-09 09:28:20 -04004349 percpu_counter_sum(&fs_info->ordered_bytes));
Josef Bacik4297ff82019-04-10 15:56:09 -04004350
Anand Jain6618a592015-08-14 18:32:47 +08004351 btrfs_sysfs_remove_mounted(fs_info);
Anand Jainb7c35e812015-03-10 06:38:38 +08004352 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04004353
Liu Bo1a4319c2014-01-13 19:53:53 +08004354 btrfs_put_block_group_cache(fs_info);
4355
Wang Shilongde348ee2014-04-09 19:23:22 +08004356 /*
4357 * we must make sure there is not any read request to
4358 * submit after we stopping all workers.
4359 */
4360 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
Josef Bacik96192492013-10-16 13:53:28 -04004361 btrfs_stop_all_workers(fs_info);
4362
Filipe Manana0a31daa2020-12-14 10:10:48 +00004363 /* We shouldn't have any transaction open at this point */
Qu Wenruo36c86a92021-12-16 19:47:36 +08004364 warn_about_uncommitted_trans(fs_info);
Filipe Manana0a31daa2020-12-14 10:10:48 +00004365
Josef Bacikafcdd122016-09-02 15:40:02 -04004366 clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
Anand Jain4273eaf2019-10-10 10:39:25 +08004367 free_root_pointers(fs_info, true);
Josef Bacik8c389382020-02-14 16:11:42 -05004368 btrfs_free_fs_roots(fs_info);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04004369
Josef Bacik4e194432020-01-21 09:17:06 -05004370 /*
4371 * We must free the block groups after dropping the fs_roots as we could
4372 * have had an IO error and have left over tree log blocks that aren't
4373 * cleaned up until the fs roots are freed. This makes the block group
4374 * accounting appear to be wrong because there's pending reserved bytes,
4375 * so make sure we do the block group cleanup afterwards.
4376 */
4377 btrfs_free_block_groups(fs_info);
4378
Josef Bacik13e6c372013-05-30 16:55:44 -04004379 iput(fs_info->btree_inode);
Chris Masond6bfde82008-04-30 13:59:35 -04004380
Filipe Manana7dc66ab2023-11-21 13:38:38 +00004381 btrfs_mapping_tree_free(fs_info);
Nikolay Borisov68c94e52019-02-12 16:13:14 +02004382 btrfs_close_devices(fs_info->fs_devices);
Chris Masoneb60cea2007-02-02 09:18:22 -05004383}
4384
Filipe Manana50564b62023-09-12 13:04:29 +01004385void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
4386 struct extent_buffer *buf)
Chris Mason5f39d392007-10-15 16:14:19 -04004387{
Qu Wenruo2f4d60d2020-11-03 21:30:46 +08004388 struct btrfs_fs_info *fs_info = buf->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004389 u64 transid = btrfs_header_generation(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004390
Josef Bacik06ea65a2013-09-19 16:07:01 -04004391#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4392 /*
4393 * This is a fast path so only do this check if we have sanity tests
Andrea Gelmini52042d82018-11-28 12:05:13 +01004394 * enabled. Normal people shouldn't be using unmapped buffers as dirty
Josef Bacik06ea65a2013-09-19 16:07:01 -04004395 * outside of the sanity tests.
4396 */
Nikolay Borisovb0132a3b2018-06-27 16:38:24 +03004397 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
Josef Bacik06ea65a2013-09-19 16:07:01 -04004398 return;
4399#endif
Filipe Manana50564b62023-09-12 13:04:29 +01004400 /* This is an active transaction (its state < TRANS_STATE_UNBLOCKED). */
4401 ASSERT(trans->transid == fs_info->generation);
Filipe Manana49d0c642021-09-22 10:36:45 +01004402 btrfs_assert_tree_write_locked(buf);
Filipe Manana4ebe8d42023-09-12 13:04:31 +01004403 if (unlikely(transid != fs_info->generation)) {
Filipe Manana50564b62023-09-12 13:04:29 +01004404 btrfs_abort_transaction(trans, -EUCLEAN);
Filipe Manana20cbe462023-09-12 13:04:30 +01004405 btrfs_crit(fs_info,
4406"dirty buffer transid mismatch, logical %llu found transid %llu running transid %llu",
4407 buf->start, transid, fs_info->generation);
Filipe Manana50564b62023-09-12 13:04:29 +01004408 }
Christoph Hellwigf18cc972023-05-08 07:58:38 -07004409 set_extent_buffer_dirty(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004410}
4411
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004412static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
Liu Bob53d3f52012-11-14 14:34:34 +00004413 int flush_delayed)
Chris Mason35b7e472007-05-02 15:53:43 -04004414{
Chris Mason188de642008-05-09 11:52:25 -04004415 /*
4416 * looks as though older kernels can get into trouble with
4417 * this code, they end up stuck in balance_dirty_pages forever
4418 */
Miao Xiee2d84522013-01-29 10:09:20 +00004419 int ret;
Chris Masond6bfde82008-04-30 13:59:35 -04004420
Jens Axboe6933c022009-03-17 09:36:37 +01004421 if (current->flags & PF_MEMALLOC)
Chris Masond6bfde82008-04-30 13:59:35 -04004422 return;
4423
Liu Bob53d3f52012-11-14 14:34:34 +00004424 if (flush_delayed)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004425 btrfs_balance_delayed_items(fs_info);
Miao Xie16cdcec2011-04-22 18:12:22 +08004426
Ethan Liend814a492018-07-02 15:44:58 +08004427 ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4428 BTRFS_DIRTY_METADATA_THRESH,
4429 fs_info->dirty_metadata_batch);
Miao Xiee2d84522013-01-29 10:09:20 +00004430 if (ret > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004431 balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
Miao Xie16cdcec2011-04-22 18:12:22 +08004432 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004433}
4434
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004435void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
Miao Xie16cdcec2011-04-22 18:12:22 +08004436{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004437 __btrfs_btree_balance_dirty(fs_info, 1);
Liu Bob53d3f52012-11-14 14:34:34 +00004438}
Miao Xie16cdcec2011-04-22 18:12:22 +08004439
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004440void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
Liu Bob53d3f52012-11-14 14:34:34 +00004441{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004442 __btrfs_btree_balance_dirty(fs_info, 0);
Chris Mason35b7e472007-05-02 15:53:43 -04004443}
Chris Mason6b800532007-10-15 16:17:34 -04004444
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004445static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
liuboacce9522011-01-06 19:30:25 +08004446{
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004447 /* cleanup FS via transaction */
4448 btrfs_cleanup_transaction(fs_info);
4449
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004450 mutex_lock(&fs_info->cleaner_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004451 btrfs_run_delayed_iputs(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004452 mutex_unlock(&fs_info->cleaner_mutex);
liuboacce9522011-01-06 19:30:25 +08004453
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004454 down_write(&fs_info->cleanup_work_sem);
4455 up_write(&fs_info->cleanup_work_sem);
liuboacce9522011-01-06 19:30:25 +08004456}
4457
Josef Bacikef679632020-03-24 10:47:52 -04004458static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info)
4459{
David Sterbafc7cbcd2022-07-15 13:59:21 +02004460 struct btrfs_root *gang[8];
4461 u64 root_objectid = 0;
4462 int ret;
Josef Bacikef679632020-03-24 10:47:52 -04004463
David Sterbafc7cbcd2022-07-15 13:59:21 +02004464 spin_lock(&fs_info->fs_roots_radix_lock);
4465 while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
4466 (void **)gang, root_objectid,
4467 ARRAY_SIZE(gang))) != 0) {
4468 int i;
Josef Bacikef679632020-03-24 10:47:52 -04004469
David Sterbafc7cbcd2022-07-15 13:59:21 +02004470 for (i = 0; i < ret; i++)
4471 gang[i] = btrfs_grab_root(gang[i]);
4472 spin_unlock(&fs_info->fs_roots_radix_lock);
4473
4474 for (i = 0; i < ret; i++) {
4475 if (!gang[i])
Josef Bacikef679632020-03-24 10:47:52 -04004476 continue;
Josef Bacike094f482024-04-15 16:16:23 -04004477 root_objectid = btrfs_root_id(gang[i]);
David Sterbafc7cbcd2022-07-15 13:59:21 +02004478 btrfs_free_log(NULL, gang[i]);
4479 btrfs_put_root(gang[i]);
Josef Bacikef679632020-03-24 10:47:52 -04004480 }
David Sterbafc7cbcd2022-07-15 13:59:21 +02004481 root_objectid++;
4482 spin_lock(&fs_info->fs_roots_radix_lock);
Josef Bacikef679632020-03-24 10:47:52 -04004483 }
David Sterbafc7cbcd2022-07-15 13:59:21 +02004484 spin_unlock(&fs_info->fs_roots_radix_lock);
Josef Bacikef679632020-03-24 10:47:52 -04004485 btrfs_free_log_root_tree(NULL, fs_info);
4486}
4487
Jeff Mahoney143bede2012-03-01 14:56:26 +01004488static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004489{
liuboacce9522011-01-06 19:30:25 +08004490 struct btrfs_ordered_extent *ordered;
liuboacce9522011-01-06 19:30:25 +08004491
Miao Xie199c2a92013-05-15 07:48:23 +00004492 spin_lock(&root->ordered_extent_lock);
Josef Bacik779880e2013-01-31 14:30:08 -05004493 /*
4494 * This will just short circuit the ordered completion stuff which will
4495 * make sure the ordered extent gets properly cleaned up.
4496 */
Miao Xie199c2a92013-05-15 07:48:23 +00004497 list_for_each_entry(ordered, &root->ordered_extents,
Josef Bacik779880e2013-01-31 14:30:08 -05004498 root_extent_list)
4499 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
Miao Xie199c2a92013-05-15 07:48:23 +00004500 spin_unlock(&root->ordered_extent_lock);
4501}
4502
4503static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4504{
4505 struct btrfs_root *root;
Ruan Jinjie84af9942023-08-10 11:00:22 +08004506 LIST_HEAD(splice);
Miao Xie199c2a92013-05-15 07:48:23 +00004507
4508 spin_lock(&fs_info->ordered_root_lock);
4509 list_splice_init(&fs_info->ordered_roots, &splice);
4510 while (!list_empty(&splice)) {
4511 root = list_first_entry(&splice, struct btrfs_root,
4512 ordered_root);
Josef Bacik1de2cfd2013-09-27 16:36:02 -04004513 list_move_tail(&root->ordered_root,
4514 &fs_info->ordered_roots);
Miao Xie199c2a92013-05-15 07:48:23 +00004515
Liu Bo2a85d9c2014-02-10 17:07:16 +08004516 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00004517 btrfs_destroy_ordered_extents(root);
4518
Liu Bo2a85d9c2014-02-10 17:07:16 +08004519 cond_resched();
4520 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00004521 }
4522 spin_unlock(&fs_info->ordered_root_lock);
Josef Bacik74d5d222018-11-21 14:05:45 -05004523
4524 /*
4525 * We need this here because if we've been flipped read-only we won't
4526 * get sync() from the umount, so we need to make sure any ordered
4527 * extents that haven't had their dirty pages IO start writeout yet
4528 * actually get run and error out properly.
4529 */
David Sterba42317ab2024-05-14 16:48:12 +02004530 btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
liuboacce9522011-01-06 19:30:25 +08004531}
4532
Filipe Manana99f09ce2023-06-02 12:19:42 +01004533static void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4534 struct btrfs_fs_info *fs_info)
liuboacce9522011-01-06 19:30:25 +08004535{
4536 struct rb_node *node;
Filipe Mananafb33eb22024-06-03 12:49:08 +01004537 struct btrfs_delayed_ref_root *delayed_refs = &trans->delayed_refs;
liuboacce9522011-01-06 19:30:25 +08004538 struct btrfs_delayed_ref_node *ref;
liuboacce9522011-01-06 19:30:25 +08004539
liuboacce9522011-01-06 19:30:25 +08004540 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08004541 while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05004542 struct btrfs_delayed_ref_head *head;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04004543 struct rb_node *n;
Josef Bacike78417d2013-06-03 16:42:36 -04004544 bool pin_bytes = false;
liuboacce9522011-01-06 19:30:25 +08004545
Josef Bacikd7df2c72014-01-23 09:21:38 -05004546 head = rb_entry(node, struct btrfs_delayed_ref_head,
4547 href_node);
Josef Bacik3069bd22018-11-21 14:05:39 -05004548 if (btrfs_delayed_ref_lock(delayed_refs, head))
Josef Bacikd7df2c72014-01-23 09:21:38 -05004549 continue;
Josef Bacik3069bd22018-11-21 14:05:39 -05004550
Josef Bacikd7df2c72014-01-23 09:21:38 -05004551 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08004552 while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04004553 ref = rb_entry(n, struct btrfs_delayed_ref_node,
4554 ref_node);
Liu Boe3d03962018-08-23 03:51:50 +08004555 rb_erase_cached(&ref->ref_node, &head->ref_tree);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04004556 RB_CLEAR_NODE(&ref->ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08004557 if (!list_empty(&ref->add_list))
4558 list_del(&ref->add_list);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004559 atomic_dec(&delayed_refs->num_entries);
4560 btrfs_put_delayed_ref(ref);
Filipe Mananaadb86db2023-09-08 18:20:37 +01004561 btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004562 }
4563 if (head->must_insert_reserved)
4564 pin_bytes = true;
4565 btrfs_free_delayed_extent_op(head->extent_op);
Josef Bacikfa781ce2018-11-21 14:05:40 -05004566 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004567 spin_unlock(&head->lock);
4568 spin_unlock(&delayed_refs->lock);
4569 mutex_unlock(&head->mutex);
liuboacce9522011-01-06 19:30:25 +08004570
Nikolay Borisovf603bb92020-01-20 16:09:08 +02004571 if (pin_bytes) {
4572 struct btrfs_block_group *cache;
4573
4574 cache = btrfs_lookup_block_group(fs_info, head->bytenr);
4575 BUG_ON(!cache);
4576
4577 spin_lock(&cache->space_info->lock);
4578 spin_lock(&cache->lock);
4579 cache->pinned += head->num_bytes;
4580 btrfs_space_info_update_bytes_pinned(fs_info,
4581 cache->space_info, head->num_bytes);
4582 cache->reserved -= head->num_bytes;
4583 cache->space_info->bytes_reserved -= head->num_bytes;
4584 spin_unlock(&cache->lock);
4585 spin_unlock(&cache->space_info->lock);
Nikolay Borisovf603bb92020-01-20 16:09:08 +02004586
4587 btrfs_put_block_group(cache);
4588
4589 btrfs_error_unpin_extent_range(fs_info, head->bytenr,
4590 head->bytenr + head->num_bytes - 1);
4591 }
Josef Bacik31890da2018-11-21 14:05:41 -05004592 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacikd2788502017-09-29 15:43:57 -04004593 btrfs_put_delayed_ref_head(head);
liuboacce9522011-01-06 19:30:25 +08004594 cond_resched();
4595 spin_lock(&delayed_refs->lock);
4596 }
Jeff Mahoney81f7eb02020-02-11 15:25:37 +08004597 btrfs_qgroup_destroy_extent_records(trans);
liuboacce9522011-01-06 19:30:25 +08004598
4599 spin_unlock(&delayed_refs->lock);
liuboacce9522011-01-06 19:30:25 +08004600}
4601
Jeff Mahoney143bede2012-03-01 14:56:26 +01004602static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004603{
4604 struct btrfs_inode *btrfs_inode;
Ruan Jinjie84af9942023-08-10 11:00:22 +08004605 LIST_HEAD(splice);
liuboacce9522011-01-06 19:30:25 +08004606
Miao Xieeb73c1b2013-05-15 07:48:22 +00004607 spin_lock(&root->delalloc_lock);
4608 list_splice_init(&root->delalloc_inodes, &splice);
liuboacce9522011-01-06 19:30:25 +08004609
4610 while (!list_empty(&splice)) {
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004611 struct inode *inode = NULL;
Miao Xieeb73c1b2013-05-15 07:48:22 +00004612 btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
4613 delalloc_inodes);
David Sterba5a8a57f2024-02-22 09:56:17 +01004614 btrfs_del_delalloc_inode(btrfs_inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00004615 spin_unlock(&root->delalloc_lock);
liuboacce9522011-01-06 19:30:25 +08004616
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004617 /*
4618 * Make sure we get a live inode and that it'll not disappear
4619 * meanwhile.
4620 */
4621 inode = igrab(&btrfs_inode->vfs_inode);
4622 if (inode) {
Josef Bacik597441b2023-05-11 12:45:59 -04004623 unsigned int nofs_flag;
4624
4625 nofs_flag = memalloc_nofs_save();
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004626 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik597441b2023-05-11 12:45:59 -04004627 memalloc_nofs_restore(nofs_flag);
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004628 iput(inode);
4629 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00004630 spin_lock(&root->delalloc_lock);
liuboacce9522011-01-06 19:30:25 +08004631 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00004632 spin_unlock(&root->delalloc_lock);
4633}
4634
4635static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4636{
4637 struct btrfs_root *root;
Ruan Jinjie84af9942023-08-10 11:00:22 +08004638 LIST_HEAD(splice);
Miao Xieeb73c1b2013-05-15 07:48:22 +00004639
4640 spin_lock(&fs_info->delalloc_root_lock);
4641 list_splice_init(&fs_info->delalloc_roots, &splice);
4642 while (!list_empty(&splice)) {
4643 root = list_first_entry(&splice, struct btrfs_root,
4644 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05004645 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00004646 BUG_ON(!root);
4647 spin_unlock(&fs_info->delalloc_root_lock);
4648
4649 btrfs_destroy_delalloc_inodes(root);
Josef Bacik00246522020-01-24 09:33:01 -05004650 btrfs_put_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00004651
4652 spin_lock(&fs_info->delalloc_root_lock);
4653 }
4654 spin_unlock(&fs_info->delalloc_root_lock);
liuboacce9522011-01-06 19:30:25 +08004655}
4656
Filipe Mananaaec57162023-06-30 16:03:47 +01004657static void btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
4658 struct extent_io_tree *dirty_pages,
4659 int mark)
liuboacce9522011-01-06 19:30:25 +08004660{
liuboacce9522011-01-06 19:30:25 +08004661 struct extent_buffer *eb;
4662 u64 start = 0;
4663 u64 end;
liuboacce9522011-01-06 19:30:25 +08004664
Filipe Mananae5860f82023-06-30 16:03:49 +01004665 while (find_first_extent_bit(dirty_pages, start, &start, &end,
4666 mark, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02004667 clear_extent_bits(dirty_pages, start, end, mark);
liuboacce9522011-01-06 19:30:25 +08004668 while (start <= end) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004669 eb = find_extent_buffer(fs_info, start);
4670 start += fs_info->nodesize;
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004671 if (!eb)
liuboacce9522011-01-06 19:30:25 +08004672 continue;
liuboacce9522011-01-06 19:30:25 +08004673
Josef Bacikc4e54a62023-01-26 16:00:56 -05004674 btrfs_tree_lock(eb);
4675 wait_on_extent_buffer_writeback(eb);
Josef Bacik190a8332023-01-26 16:00:58 -05004676 btrfs_clear_buffer_dirty(NULL, eb);
Josef Bacikc4e54a62023-01-26 16:00:56 -05004677 btrfs_tree_unlock(eb);
4678
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004679 free_extent_buffer_stale(eb);
liuboacce9522011-01-06 19:30:25 +08004680 }
4681 }
liuboacce9522011-01-06 19:30:25 +08004682}
4683
Filipe Manana46d81eb2023-06-30 16:03:48 +01004684static void btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
4685 struct extent_io_tree *unpin)
liuboacce9522011-01-06 19:30:25 +08004686{
liuboacce9522011-01-06 19:30:25 +08004687 u64 start;
4688 u64 end;
liuboacce9522011-01-06 19:30:25 +08004689
liuboacce9522011-01-06 19:30:25 +08004690 while (1) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00004691 struct extent_state *cached_state = NULL;
4692
Lu Fengqifcd5e742018-10-24 20:24:03 +08004693 /*
4694 * The btrfs_finish_extent_commit() may get the same range as
4695 * ours between find_first_extent_bit and clear_extent_dirty.
4696 * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
4697 * the same extent range.
4698 */
4699 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Mananae5860f82023-06-30 16:03:49 +01004700 if (!find_first_extent_bit(unpin, 0, &start, &end,
4701 EXTENT_DIRTY, &cached_state)) {
Lu Fengqifcd5e742018-10-24 20:24:03 +08004702 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004703 break;
Lu Fengqifcd5e742018-10-24 20:24:03 +08004704 }
liuboacce9522011-01-06 19:30:25 +08004705
Filipe Manana0e6ec382018-11-16 13:04:44 +00004706 clear_extent_dirty(unpin, start, end, &cached_state);
4707 free_extent_state(cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004708 btrfs_error_unpin_extent_range(fs_info, start, end);
Lu Fengqifcd5e742018-10-24 20:24:03 +08004709 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004710 cond_resched();
4711 }
liuboacce9522011-01-06 19:30:25 +08004712}
4713
David Sterba32da53862019-10-29 19:20:18 +01004714static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
Liu Boc79a1752016-07-20 17:44:12 -07004715{
4716 struct inode *inode;
4717
4718 inode = cache->io_ctl.inode;
4719 if (inode) {
Josef Bacik597441b2023-05-11 12:45:59 -04004720 unsigned int nofs_flag;
4721
4722 nofs_flag = memalloc_nofs_save();
Liu Boc79a1752016-07-20 17:44:12 -07004723 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik597441b2023-05-11 12:45:59 -04004724 memalloc_nofs_restore(nofs_flag);
4725
Liu Boc79a1752016-07-20 17:44:12 -07004726 BTRFS_I(inode)->generation = 0;
4727 cache->io_ctl.inode = NULL;
4728 iput(inode);
4729 }
Filipe Mananabbc37d62020-08-14 11:04:09 +01004730 ASSERT(cache->io_ctl.pages == NULL);
Liu Boc79a1752016-07-20 17:44:12 -07004731 btrfs_put_block_group(cache);
4732}
4733
4734void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004735 struct btrfs_fs_info *fs_info)
Liu Boc79a1752016-07-20 17:44:12 -07004736{
David Sterba32da53862019-10-29 19:20:18 +01004737 struct btrfs_block_group *cache;
Liu Boc79a1752016-07-20 17:44:12 -07004738
4739 spin_lock(&cur_trans->dirty_bgs_lock);
4740 while (!list_empty(&cur_trans->dirty_bgs)) {
4741 cache = list_first_entry(&cur_trans->dirty_bgs,
David Sterba32da53862019-10-29 19:20:18 +01004742 struct btrfs_block_group,
Liu Boc79a1752016-07-20 17:44:12 -07004743 dirty_list);
Liu Boc79a1752016-07-20 17:44:12 -07004744
4745 if (!list_empty(&cache->io_list)) {
4746 spin_unlock(&cur_trans->dirty_bgs_lock);
4747 list_del_init(&cache->io_list);
4748 btrfs_cleanup_bg_io(cache);
4749 spin_lock(&cur_trans->dirty_bgs_lock);
4750 }
4751
4752 list_del_init(&cache->dirty_list);
4753 spin_lock(&cache->lock);
4754 cache->disk_cache_state = BTRFS_DC_ERROR;
4755 spin_unlock(&cache->lock);
4756
4757 spin_unlock(&cur_trans->dirty_bgs_lock);
4758 btrfs_put_block_group(cache);
Filipe Mananaf66e0202023-09-28 11:12:49 +01004759 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
Liu Boc79a1752016-07-20 17:44:12 -07004760 spin_lock(&cur_trans->dirty_bgs_lock);
4761 }
4762 spin_unlock(&cur_trans->dirty_bgs_lock);
4763
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02004764 /*
4765 * Refer to the definition of io_bgs member for details why it's safe
4766 * to use it without any locking
4767 */
Liu Boc79a1752016-07-20 17:44:12 -07004768 while (!list_empty(&cur_trans->io_bgs)) {
4769 cache = list_first_entry(&cur_trans->io_bgs,
David Sterba32da53862019-10-29 19:20:18 +01004770 struct btrfs_block_group,
Liu Boc79a1752016-07-20 17:44:12 -07004771 io_list);
Liu Boc79a1752016-07-20 17:44:12 -07004772
4773 list_del_init(&cache->io_list);
4774 spin_lock(&cache->lock);
4775 cache->disk_cache_state = BTRFS_DC_ERROR;
4776 spin_unlock(&cache->lock);
4777 btrfs_cleanup_bg_io(cache);
4778 }
4779}
4780
Boris Burkovb321a522023-12-01 13:00:11 -08004781static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
4782{
4783 struct btrfs_root *gang[8];
4784 int i;
4785 int ret;
4786
4787 spin_lock(&fs_info->fs_roots_radix_lock);
4788 while (1) {
4789 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
4790 (void **)gang, 0,
4791 ARRAY_SIZE(gang),
4792 BTRFS_ROOT_TRANS_TAG);
4793 if (ret == 0)
4794 break;
4795 for (i = 0; i < ret; i++) {
4796 struct btrfs_root *root = gang[i];
4797
4798 btrfs_qgroup_free_meta_all_pertrans(root);
4799 radix_tree_tag_clear(&fs_info->fs_roots_radix,
Josef Bacike094f482024-04-15 16:16:23 -04004800 (unsigned long)btrfs_root_id(root),
Boris Burkovb321a522023-12-01 13:00:11 -08004801 BTRFS_ROOT_TRANS_TAG);
4802 }
4803 }
4804 spin_unlock(&fs_info->fs_roots_radix_lock);
4805}
4806
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004807void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004808 struct btrfs_fs_info *fs_info)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004809{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004810 struct btrfs_device *dev, *tmp;
4811
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004812 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Liu Boc79a1752016-07-20 17:44:12 -07004813 ASSERT(list_empty(&cur_trans->dirty_bgs));
4814 ASSERT(list_empty(&cur_trans->io_bgs));
4815
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004816 list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
4817 post_commit_list) {
4818 list_del_init(&dev->post_commit_list);
4819 }
4820
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004821 btrfs_destroy_delayed_refs(cur_trans, fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004822
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004823 cur_trans->state = TRANS_STATE_COMMIT_START;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004824 wake_up(&fs_info->transaction_blocked_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004825
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004826 cur_trans->state = TRANS_STATE_UNBLOCKED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004827 wake_up(&fs_info->transaction_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004828
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004829 btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004830 EXTENT_DIRTY);
Nikolay Borisovfe119a62020-01-20 16:09:18 +02004831 btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004832
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004833 cur_trans->state =TRANS_STATE_COMPLETED;
4834 wake_up(&cur_trans->commit_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004835}
4836
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004837static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
liuboacce9522011-01-06 19:30:25 +08004838{
4839 struct btrfs_transaction *t;
liuboacce9522011-01-06 19:30:25 +08004840
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004841 mutex_lock(&fs_info->transaction_kthread_mutex);
liuboacce9522011-01-06 19:30:25 +08004842
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 spin_lock(&fs_info->trans_lock);
4844 while (!list_empty(&fs_info->trans_list)) {
4845 t = list_first_entry(&fs_info->trans_list,
Josef Bacik724e2312013-09-30 11:36:38 -04004846 struct btrfs_transaction, list);
Josef Bacik77d20c62023-08-24 16:59:22 -04004847 if (t->state >= TRANS_STATE_COMMIT_PREP) {
Elena Reshetova9b64f572017-03-03 10:55:11 +02004848 refcount_inc(&t->use_count);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004849 spin_unlock(&fs_info->trans_lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004850 btrfs_wait_for_commit(fs_info, t->transid);
Josef Bacik724e2312013-09-30 11:36:38 -04004851 btrfs_put_transaction(t);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004852 spin_lock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004853 continue;
4854 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004855 if (t == fs_info->running_transaction) {
Josef Bacik724e2312013-09-30 11:36:38 -04004856 t->state = TRANS_STATE_COMMIT_DOING;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 spin_unlock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004858 /*
4859 * We wait for 0 num_writers since we don't hold a trans
4860 * handle open currently for this transaction.
4861 */
4862 wait_event(t->writer_wait,
4863 atomic_read(&t->num_writers) == 0);
4864 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004865 spin_unlock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004866 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004867 btrfs_cleanup_one_transaction(t, fs_info);
Josef Bacika4abeea2011-04-11 17:25:13 -04004868
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004869 spin_lock(&fs_info->trans_lock);
4870 if (t == fs_info->running_transaction)
4871 fs_info->running_transaction = NULL;
liuboacce9522011-01-06 19:30:25 +08004872 list_del_init(&t->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004873 spin_unlock(&fs_info->trans_lock);
liuboacce9522011-01-06 19:30:25 +08004874
Josef Bacik724e2312013-09-30 11:36:38 -04004875 btrfs_put_transaction(t);
Josef Bacik2e4e97ab2021-11-05 16:45:29 -04004876 trace_btrfs_transaction_commit(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004877 spin_lock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004878 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 spin_unlock(&fs_info->trans_lock);
4880 btrfs_destroy_all_ordered_extents(fs_info);
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04004881 btrfs_destroy_delayed_inodes(fs_info);
4882 btrfs_assert_delayed_root_empty(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004883 btrfs_destroy_all_delalloc_inodes(fs_info);
Josef Bacikef679632020-03-24 10:47:52 -04004884 btrfs_drop_all_logs(fs_info);
Boris Burkov5f2fb812024-03-26 11:17:12 -07004885 btrfs_free_all_qgroup_pertrans(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004886 mutex_unlock(&fs_info->transaction_kthread_mutex);
liuboacce9522011-01-06 19:30:25 +08004887
4888 return 0;
4889}
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004890
Nikolay Borisov453e4872020-12-07 17:32:32 +02004891int btrfs_init_root_free_objectid(struct btrfs_root *root)
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004892{
4893 struct btrfs_path *path;
4894 int ret;
4895 struct extent_buffer *l;
4896 struct btrfs_key search_key;
4897 struct btrfs_key found_key;
4898 int slot;
4899
4900 path = btrfs_alloc_path();
4901 if (!path)
4902 return -ENOMEM;
4903
4904 search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
4905 search_key.type = -1;
4906 search_key.offset = (u64)-1;
4907 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
4908 if (ret < 0)
4909 goto error;
David Sterbaa6724292024-01-23 23:34:57 +01004910 if (ret == 0) {
4911 /*
4912 * Key with offset -1 found, there would have to exist a root
4913 * with such id, but this is out of valid range.
4914 */
4915 ret = -EUCLEAN;
4916 goto error;
4917 }
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004918 if (path->slots[0] > 0) {
4919 slot = path->slots[0] - 1;
4920 l = path->nodes[0];
4921 btrfs_item_key_to_cpu(l, &found_key, slot);
Nikolay Borisov23125102020-12-07 17:32:36 +02004922 root->free_objectid = max_t(u64, found_key.objectid + 1,
4923 BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004924 } else {
Nikolay Borisov23125102020-12-07 17:32:36 +02004925 root->free_objectid = BTRFS_FIRST_FREE_OBJECTID;
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004926 }
4927 ret = 0;
4928error:
4929 btrfs_free_path(path);
4930 return ret;
4931}
4932
Nikolay Borisov543068a2020-12-07 17:32:33 +02004933int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid)
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004934{
4935 int ret;
4936 mutex_lock(&root->objectid_mutex);
4937
Nikolay Borisov6b8fad52020-12-07 17:32:35 +02004938 if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004939 btrfs_warn(root->fs_info,
4940 "the objectid of root %llu reaches its highest value",
Josef Bacike094f482024-04-15 16:16:23 -04004941 btrfs_root_id(root));
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004942 ret = -ENOSPC;
4943 goto out;
4944 }
4945
Nikolay Borisov23125102020-12-07 17:32:36 +02004946 *objectid = root->free_objectid++;
Nikolay Borisovec7d6df2020-11-26 15:10:37 +02004947 ret = 0;
4948out:
4949 mutex_unlock(&root->objectid_mutex);
4950 return ret;
4951}