xref: /linux/fs/btrfs/disk-io.c (revision d3e7c23ec3d004de154dc85d3ddeae0feff4159d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/fs.h>
7 #include <linux/blkdev.h>
8 #include <linux/radix-tree.h>
9 #include <linux/writeback.h>
10 #include <linux/workqueue.h>
11 #include <linux/kthread.h>
12 #include <linux/slab.h>
13 #include <linux/migrate.h>
14 #include <linux/ratelimit.h>
15 #include <linux/uuid.h>
16 #include <linux/semaphore.h>
17 #include <linux/error-injection.h>
18 #include <linux/crc32c.h>
19 #include <linux/sched/mm.h>
20 #include <linux/unaligned.h>
21 #include "ctree.h"
22 #include "disk-io.h"
23 #include "transaction.h"
24 #include "btrfs_inode.h"
25 #include "delayed-inode.h"
26 #include "bio.h"
27 #include "print-tree.h"
28 #include "locking.h"
29 #include "tree-log.h"
30 #include "free-space-cache.h"
31 #include "free-space-tree.h"
32 #include "dev-replace.h"
33 #include "raid56.h"
34 #include "sysfs.h"
35 #include "qgroup.h"
36 #include "compression.h"
37 #include "tree-checker.h"
38 #include "ref-verify.h"
39 #include "block-group.h"
40 #include "discard.h"
41 #include "space-info.h"
42 #include "zoned.h"
43 #include "subpage.h"
44 #include "fs.h"
45 #include "accessors.h"
46 #include "extent-tree.h"
47 #include "root-tree.h"
48 #include "defrag.h"
49 #include "uuid-tree.h"
50 #include "relocation.h"
51 #include "scrub.h"
52 #include "super.h"
53 
54 #define BTRFS_SUPER_FLAG_SUPP	(BTRFS_HEADER_FLAG_WRITTEN |\
55 				 BTRFS_HEADER_FLAG_RELOC |\
56 				 BTRFS_SUPER_FLAG_ERROR |\
57 				 BTRFS_SUPER_FLAG_SEEDING |\
58 				 BTRFS_SUPER_FLAG_METADUMP |\
59 				 BTRFS_SUPER_FLAG_METADUMP_V2)
60 
61 static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
62 static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
63 
64 /*
65  * Compute the csum of a btree block and store the result to provided buffer.
66  */
67 static void csum_tree_block(struct extent_buffer *buf, u8 *result)
68 {
69 	struct btrfs_fs_info *fs_info = buf->fs_info;
70 	int num_pages;
71 	u32 first_page_part;
72 	struct btrfs_csum_ctx csum;
73 	char *kaddr;
74 	int i;
75 
76 	btrfs_csum_init(&csum, fs_info->csum_type);
77 
78 	if (buf->addr) {
79 		/* Pages are contiguous, handle them as a big one. */
80 		kaddr = buf->addr;
81 		first_page_part = fs_info->nodesize;
82 		num_pages = 1;
83 	} else {
84 		kaddr = folio_address(buf->folios[0]);
85 		first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
86 		num_pages = num_extent_pages(buf);
87 	}
88 
89 	btrfs_csum_update(&csum, kaddr + BTRFS_CSUM_SIZE,
90 			  first_page_part - BTRFS_CSUM_SIZE);
91 
92 	/*
93 	 * Multiple single-page folios case would reach here.
94 	 *
95 	 * nodesize <= PAGE_SIZE and large folio all handled by above
96 	 * btrfs_csum_update() already.
97 	 */
98 	for (i = 1; i < num_pages && INLINE_EXTENT_BUFFER_PAGES > 1; i++) {
99 		kaddr = folio_address(buf->folios[i]);
100 		btrfs_csum_update(&csum, kaddr, PAGE_SIZE);
101 	}
102 	memset(result, 0, BTRFS_CSUM_SIZE);
103 	btrfs_csum_final(&csum, result);
104 }
105 
106 /*
107  * we can't consider a given block up to date unless the transid of the
108  * block matches the transid in the parent node's pointer.  This is how we
109  * detect blocks that either didn't get written at all or got written
110  * in the wrong place.
111  */
112 int btrfs_buffer_uptodate(struct extent_buffer *eb, u64 parent_transid,
113 			  const struct btrfs_tree_parent_check *check)
114 {
115 	if (!extent_buffer_uptodate(eb))
116 		return 0;
117 
118 	if (!parent_transid || btrfs_header_generation(eb) == parent_transid) {
119 		/*
120 		 * On a cache hit, the caller may still need tree parent
121 		 * verification before reusing the buffer.
122 		 */
123 		if (unlikely(check && btrfs_verify_level_key(eb, check)))
124 			return -EUCLEAN;
125 		return 1;
126 	}
127 
128 	btrfs_err_rl(eb->fs_info,
129 "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
130 		     eb->start, eb->read_mirror,
131 		     parent_transid, btrfs_header_generation(eb));
132 	clear_extent_buffer_uptodate(eb);
133 
134 	return 0;
135 }
136 
137 static bool btrfs_supported_super_csum(u16 csum_type)
138 {
139 	switch (csum_type) {
140 	case BTRFS_CSUM_TYPE_CRC32:
141 	case BTRFS_CSUM_TYPE_XXHASH:
142 	case BTRFS_CSUM_TYPE_SHA256:
143 	case BTRFS_CSUM_TYPE_BLAKE2:
144 		return true;
145 	default:
146 		return false;
147 	}
148 }
149 
150 /*
151  * Return 0 if the superblock checksum type matches the checksum value of that
152  * algorithm. Pass the raw disk superblock data.
153  */
154 int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
155 			   const struct btrfs_super_block *disk_sb)
156 {
157 	u8 result[BTRFS_CSUM_SIZE];
158 
159 	/*
160 	 * The super_block structure does not span the whole
161 	 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
162 	 * filled with zeros and is included in the checksum.
163 	 */
164 	btrfs_csum(fs_info->csum_type, (const u8 *)disk_sb + BTRFS_CSUM_SIZE,
165 		   BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
166 
167 	if (memcmp(disk_sb->csum, result, fs_info->csum_size))
168 		return 1;
169 
170 	return 0;
171 }
172 
173 static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb,
174 				      int mirror_num)
175 {
176 	struct btrfs_fs_info *fs_info = eb->fs_info;
177 	struct btrfs_bio *bbio;
178 	int ret;
179 
180 	if (sb_rdonly(fs_info->sb))
181 		return -EROFS;
182 
183 	/*
184 	 * This bbio is only to queue all pages for btrfs_repair_bbio_failure().
185 	 * Thus it will never get its endio called.
186 	 */
187 	bbio = btrfs_bio_alloc(max(1, fs_info->nodesize >> PAGE_SHIFT), REQ_OP_READ,
188 			       BTRFS_I(fs_info->btree_inode), eb->start, NULL, NULL);
189 	bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
190 	for (int i = 0; i < num_extent_pages(eb); i++) {
191 		struct folio *folio = eb->folios[i];
192 
193 		/* No large folio support yet. */
194 		ASSERT(folio_order(folio) == 0);
195 
196 		/*
197 		 * For nodesize < page size, there is just one paddr, with some
198 		 * offset inside the page.
199 		 *
200 		 * For nodesize >= page size, it's one or more paddrs, and eb->start
201 		 * must be aligned to page boundary.
202 		 */
203 		ret = bio_add_page(&bbio->bio, &folio->page, min(PAGE_SIZE, fs_info->nodesize),
204 				   offset_in_page(eb->start));
205 		ASSERT(ret == min(PAGE_SIZE, fs_info->nodesize));
206 	}
207 	/* Since the bbio is never submitted, we have to save the iter manually. */
208 	bbio->saved_iter = bbio->bio.bi_iter;
209 
210 	ret = btrfs_repair_bbio_failure(bbio, &bbio->saved_iter, fs_info->nodesize,
211 					mirror_num);
212 	bio_put(&bbio->bio);
213 	return ret;
214 }
215 
216 /*
217  * helper to read a given tree block, doing retries as required when
218  * the checksums don't match and we have alternate mirrors to try.
219  *
220  * @check:		expected tree parentness check, see the comments of the
221  *			structure for details.
222  */
223 int btrfs_read_extent_buffer(struct extent_buffer *eb,
224 			     const struct btrfs_tree_parent_check *check)
225 {
226 	struct btrfs_fs_info *fs_info = eb->fs_info;
227 	bool failed = false;
228 	int ret;
229 	int num_copies = 0;
230 	int mirror_num = 0;
231 	int failed_mirror = 0;
232 
233 	ASSERT(check);
234 
235 	while (1) {
236 		ret = read_extent_buffer_pages(eb, mirror_num, check);
237 		if (!ret)
238 			break;
239 
240 		num_copies = btrfs_num_copies(fs_info,
241 					      eb->start, eb->len);
242 		if (num_copies == 1)
243 			break;
244 
245 		if (!failed_mirror) {
246 			failed = true;
247 			failed_mirror = eb->read_mirror;
248 		}
249 
250 		mirror_num++;
251 		if (mirror_num == failed_mirror)
252 			mirror_num++;
253 
254 		if (mirror_num > num_copies)
255 			break;
256 	}
257 
258 	if (failed && !ret && failed_mirror)
259 		btrfs_repair_eb_io_failure(eb, failed_mirror);
260 
261 	return ret;
262 }
263 
264 /*
265  * Checksum a dirty tree block before IO.
266  */
267 int btree_csum_one_bio(struct btrfs_bio *bbio)
268 {
269 	struct extent_buffer *eb = bbio->private;
270 	struct btrfs_fs_info *fs_info = eb->fs_info;
271 	u64 found_start = btrfs_header_bytenr(eb);
272 	u64 last_trans;
273 	u8 result[BTRFS_CSUM_SIZE];
274 	int ret;
275 
276 	/* Btree blocks are always contiguous on disk. */
277 	if (WARN_ON_ONCE(bbio->file_offset != eb->start))
278 		return -EIO;
279 	if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))
280 		return -EIO;
281 
282 	/*
283 	 * An extent_buffer marked EXTENT_BUFFER_ZONED_ZEROOUT is written out as
284 	 * zeros to preserve ordering of I/O without persisting the now
285 	 * unnecessary block. The bio is fed from the shared zero page (see
286 	 * write_one_eb()), so there is nothing to checksum here. Crucially, the
287 	 * buffer's own content is left intact: it may still be referenced, e.g.
288 	 * btrfs_free_tree_block() reads its header to add a delayed reference.
289 	 */
290 	if (test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags))
291 		return 0;
292 
293 	if (WARN_ON_ONCE(found_start != eb->start))
294 		return -EIO;
295 	if (WARN_ON(!btrfs_meta_folio_test_uptodate(eb->folios[0], eb)))
296 		return -EIO;
297 
298 	ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
299 				    offsetof(struct btrfs_header, fsid),
300 				    BTRFS_FSID_SIZE) == 0);
301 	csum_tree_block(eb, result);
302 
303 	if (btrfs_header_level(eb))
304 		ret = btrfs_check_node(eb);
305 	else
306 		ret = btrfs_check_leaf(eb);
307 
308 	if (ret < 0)
309 		goto error;
310 
311 	/*
312 	 * Also check the generation, the eb reached here must be newer than
313 	 * last committed. Or something seriously wrong happened.
314 	 */
315 	last_trans = btrfs_get_last_trans_committed(fs_info);
316 	if (unlikely(btrfs_header_generation(eb) <= last_trans)) {
317 		ret = -EUCLEAN;
318 		btrfs_err(fs_info,
319 			"block=%llu bad generation, have %llu expect > %llu",
320 			  eb->start, btrfs_header_generation(eb), last_trans);
321 		goto error;
322 	}
323 	write_extent_buffer(eb, result, 0, fs_info->csum_size);
324 	return 0;
325 
326 error:
327 	btrfs_print_tree(eb, 0);
328 	btrfs_err(fs_info, "block=%llu write time tree block corruption detected",
329 		  eb->start);
330 	/*
331 	 * Be noisy if this is an extent buffer from a log tree. We don't abort
332 	 * a transaction in case there's a bad log tree extent buffer, we just
333 	 * fallback to a transaction commit. Still we want to know when there is
334 	 * a bad log tree extent buffer, as that may signal a bug somewhere.
335 	 */
336 	WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG) ||
337 		btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID);
338 	return ret;
339 }
340 
341 static bool check_tree_block_fsid(struct extent_buffer *eb)
342 {
343 	struct btrfs_fs_info *fs_info = eb->fs_info;
344 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
345 	u8 fsid[BTRFS_FSID_SIZE];
346 
347 	read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid),
348 			   BTRFS_FSID_SIZE);
349 
350 	/*
351 	 * alloc_fsid_devices() copies the fsid into fs_devices::metadata_uuid.
352 	 * This is then overwritten by metadata_uuid if it is present in the
353 	 * device_list_add(). The same true for a seed device as well. So use of
354 	 * fs_devices::metadata_uuid is appropriate here.
355 	 */
356 	if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)
357 		return false;
358 
359 	list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list)
360 		if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE))
361 			return false;
362 
363 	return true;
364 }
365 
366 /* Do basic extent buffer checks at read time */
367 int btrfs_validate_extent_buffer(struct extent_buffer *eb,
368 				 const struct btrfs_tree_parent_check *check)
369 {
370 	struct btrfs_fs_info *fs_info = eb->fs_info;
371 	u64 found_start;
372 	const u32 csum_size = fs_info->csum_size;
373 	u8 found_level;
374 	u8 result[BTRFS_CSUM_SIZE];
375 	const u8 *header_csum;
376 	int ret = 0;
377 	const bool ignore_csum = btrfs_test_opt(fs_info, IGNOREMETACSUMS);
378 
379 	ASSERT(check);
380 
381 	found_start = btrfs_header_bytenr(eb);
382 	if (unlikely(found_start != eb->start)) {
383 		btrfs_err_rl(fs_info,
384 			"bad tree block start, mirror %u want %llu have %llu",
385 			     eb->read_mirror, eb->start, found_start);
386 		return -EIO;
387 	}
388 	if (unlikely(check_tree_block_fsid(eb))) {
389 		btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u",
390 			     eb->start, eb->read_mirror);
391 		return -EIO;
392 	}
393 	found_level = btrfs_header_level(eb);
394 	if (unlikely(found_level >= BTRFS_MAX_LEVEL)) {
395 		btrfs_err(fs_info,
396 			"bad tree block level, mirror %u level %d on logical %llu",
397 			eb->read_mirror, btrfs_header_level(eb), eb->start);
398 		return -EIO;
399 	}
400 
401 	csum_tree_block(eb, result);
402 	header_csum = folio_address(eb->folios[0]) +
403 		get_eb_offset_in_folio(eb, offsetof(struct btrfs_header, csum));
404 
405 	if (memcmp(result, header_csum, csum_size) != 0) {
406 		btrfs_warn_rl(fs_info,
407 "checksum verify failed on logical %llu mirror %u wanted " BTRFS_CSUM_FMT " found " BTRFS_CSUM_FMT " level %d%s",
408 			      eb->start, eb->read_mirror,
409 			      BTRFS_CSUM_FMT_VALUE(csum_size, header_csum),
410 			      BTRFS_CSUM_FMT_VALUE(csum_size, result),
411 			      btrfs_header_level(eb),
412 			      ignore_csum ? ", ignored" : "");
413 		if (unlikely(!ignore_csum))
414 			return -EUCLEAN;
415 	}
416 
417 	if (unlikely(found_level != check->level)) {
418 		btrfs_err(fs_info,
419 		"level verify failed on logical %llu mirror %u wanted %u found %u",
420 			  eb->start, eb->read_mirror, check->level, found_level);
421 		return -EIO;
422 	}
423 	if (unlikely(check->transid &&
424 		     btrfs_header_generation(eb) != check->transid)) {
425 		btrfs_err_rl(eb->fs_info,
426 "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
427 				eb->start, eb->read_mirror, check->transid,
428 				btrfs_header_generation(eb));
429 		return -EIO;
430 	}
431 	if (check->has_first_key) {
432 		const struct btrfs_key *expect_key = &check->first_key;
433 		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 			return -EUCLEAN;
448 		}
449 	}
450 	if (check->owner_root) {
451 		ret = btrfs_check_eb_owner(eb, check->owner_root);
452 		if (ret < 0)
453 			return ret;
454 	}
455 
456 	/* If this is a leaf block and it is corrupt, just return -EIO. */
457 	if (found_level == 0 && btrfs_check_leaf(eb))
458 		ret = -EIO;
459 
460 	if (found_level > 0 && btrfs_check_node(eb))
461 		ret = -EIO;
462 
463 	if (ret)
464 		btrfs_err(fs_info,
465 		"read time tree block corruption detected on logical %llu mirror %u",
466 			  eb->start, eb->read_mirror);
467 	return ret;
468 }
469 
470 #ifdef CONFIG_MIGRATION
471 static int btree_migrate_folio(struct address_space *mapping,
472 		struct folio *dst, struct folio *src, enum migrate_mode mode)
473 {
474 	/*
475 	 * we can't safely write a btree page from here,
476 	 * we haven't done the locking hook
477 	 */
478 	if (folio_test_dirty(src))
479 		return -EAGAIN;
480 	/*
481 	 * Buffers may be managed in a filesystem specific way.
482 	 * We must have no buffers or drop them.
483 	 */
484 	if (folio_get_private(src) &&
485 	    !filemap_release_folio(src, GFP_KERNEL))
486 		return -EAGAIN;
487 	return migrate_folio(mapping, dst, src, mode);
488 }
489 #else
490 #define btree_migrate_folio NULL
491 #endif
492 
493 static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags)
494 {
495 	if (folio_test_writeback(folio) || folio_test_dirty(folio))
496 		return false;
497 
498 	return try_release_extent_buffer(folio);
499 }
500 
501 static void btree_invalidate_folio(struct folio *folio, size_t offset,
502 				 size_t length)
503 {
504 	struct extent_io_tree *tree = &folio_to_inode(folio)->io_tree;
505 	struct extent_state *cached_state = NULL;
506 	const u64 start = folio_pos(folio);
507 	const u64 end = folio_next_pos(folio) - 1;
508 
509 	/*
510 	 * The range must cover the full @folio.
511 	 * Btree inode is never exposed to regular file operations, thus there
512 	 * is no partial truncation.
513 	 * The folio is only invalidated when the btree inode is evicted.
514 	 */
515 	ASSERT(offset == 0, "folio=%llu offset=%zu", folio_pos(folio), offset);
516 	ASSERT(length == folio_size(folio), "folio=%llu folio_size=%zu length=%zu",
517 	       folio_pos(folio), folio_size(folio), length);
518 
519 	/* This function is only called for the btree inode */
520 	ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
521 
522 	btrfs_lock_extent(tree, start, end, &cached_state);
523 	folio_wait_writeback(folio);
524 
525 	/*
526 	 * Currently for btree io tree, only EXTENT_LOCKED is utilized,
527 	 * so here we only need to unlock the extent range to free any
528 	 * existing extent state.
529 	 */
530 	btrfs_unlock_extent(tree, start, end, &cached_state);
531 
532 	btree_release_folio(folio, GFP_NOFS);
533 	if (folio_get_private(folio)) {
534 		btrfs_warn(folio_to_fs_info(folio),
535 			   "folio private not zero on folio %llu",
536 			   (unsigned long long)folio_pos(folio));
537 		folio_detach_private(folio);
538 	}
539 }
540 
541 #ifdef DEBUG
542 static bool btree_dirty_folio(struct address_space *mapping,
543 		struct folio *folio)
544 {
545 	struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
546 	struct btrfs_subpage_info *spi = fs_info->subpage_info;
547 	struct btrfs_subpage *subpage;
548 	struct extent_buffer *eb;
549 	int cur_bit = 0;
550 	u64 page_start = folio_pos(folio);
551 
552 	if (fs_info->sectorsize == PAGE_SIZE) {
553 		eb = folio_get_private(folio);
554 		BUG_ON(!eb);
555 		BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
556 		BUG_ON(!atomic_read(&eb->refs));
557 		btrfs_assert_tree_write_locked(eb);
558 		return filemap_dirty_folio(mapping, folio);
559 	}
560 
561 	ASSERT(spi);
562 	subpage = folio_get_private(folio);
563 
564 	for (cur_bit = spi->dirty_offset;
565 	     cur_bit < spi->dirty_offset + spi->bitmap_nr_bits;
566 	     cur_bit++) {
567 		unsigned long flags;
568 		u64 cur;
569 
570 		spin_lock_irqsave(&subpage->lock, flags);
571 		if (!test_bit(cur_bit, subpage->bitmaps)) {
572 			spin_unlock_irqrestore(&subpage->lock, flags);
573 			continue;
574 		}
575 		spin_unlock_irqrestore(&subpage->lock, flags);
576 		cur = page_start + (cur_bit << fs_info->sectorsize_bits);
577 
578 		eb = find_extent_buffer(fs_info, cur);
579 		ASSERT(eb);
580 		ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
581 		ASSERT(atomic_read(&eb->refs));
582 		btrfs_assert_tree_write_locked(eb);
583 		free_extent_buffer(eb);
584 
585 		cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits) - 1;
586 	}
587 	return filemap_dirty_folio(mapping, folio);
588 }
589 #else
590 #define btree_dirty_folio filemap_dirty_folio
591 #endif
592 
593 static const struct address_space_operations btree_aops = {
594 	.writepages	= btree_writepages,
595 	.release_folio	= btree_release_folio,
596 	.invalidate_folio = btree_invalidate_folio,
597 	.migrate_folio	= btree_migrate_folio,
598 	.dirty_folio	= btree_dirty_folio,
599 };
600 
601 struct extent_buffer *btrfs_find_create_tree_block(
602 						struct btrfs_fs_info *fs_info,
603 						struct btrfs_eb_prealloc *pa,
604 						u64 bytenr, u64 owner_root,
605 						int level)
606 {
607 	if (btrfs_is_testing(fs_info))
608 		return alloc_test_extent_buffer(fs_info, bytenr);
609 	return alloc_extent_buffer(fs_info, pa, bytenr, owner_root, level);
610 }
611 
612 /*
613  * Read tree block at logical address @bytenr and do variant basic but critical
614  * verification.
615  *
616  * @check:		expected tree parentness check, see comments of the
617  *			structure for details.
618  */
619 struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
620 				      struct btrfs_tree_parent_check *check)
621 {
622 	struct btrfs_eb_prealloc pa = { 0 };
623 	struct extent_buffer *buf = NULL;
624 	int ret;
625 
626 	ASSERT(check);
627 
628 	buf = btrfs_find_create_tree_block(fs_info, &pa, bytenr, check->owner_root,
629 					   check->level);
630 	if (IS_ERR(buf))
631 		return buf;
632 
633 	ret = btrfs_read_extent_buffer(buf, check);
634 	if (ret) {
635 		free_extent_buffer_stale(buf);
636 		return ERR_PTR(ret);
637 	}
638 	return buf;
639 
640 }
641 
642 static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
643 					   u64 objectid, gfp_t flags)
644 {
645 	struct btrfs_root *root;
646 
647 	root = kzalloc_obj(*root, flags);
648 	if (!root)
649 		return NULL;
650 
651 	root->fs_info = fs_info;
652 	root->root_key.objectid = objectid;
653 	RB_CLEAR_NODE(&root->rb_node);
654 
655 	xa_init(&root->inodes);
656 	xa_init(&root->delayed_nodes);
657 
658 	btrfs_init_root_block_rsv(root);
659 
660 	INIT_LIST_HEAD(&root->dirty_list);
661 	INIT_LIST_HEAD(&root->root_list);
662 	INIT_LIST_HEAD(&root->delalloc_inodes);
663 	INIT_LIST_HEAD(&root->delalloc_root);
664 	INIT_LIST_HEAD(&root->ordered_extents);
665 	INIT_LIST_HEAD(&root->ordered_root);
666 	INIT_LIST_HEAD(&root->reloc_dirty_list);
667 	spin_lock_init(&root->delalloc_lock);
668 	spin_lock_init(&root->ordered_extent_lock);
669 	spin_lock_init(&root->accounting_lock);
670 	spin_lock_init(&root->qgroup_meta_rsv_lock);
671 	mutex_init(&root->objectid_mutex);
672 	mutex_init(&root->log_mutex);
673 	mutex_init(&root->ordered_extent_mutex);
674 	mutex_init(&root->delalloc_mutex);
675 	init_waitqueue_head(&root->qgroup_flush_wait);
676 	init_waitqueue_head(&root->log_writer_wait);
677 	init_waitqueue_head(&root->log_commit_wait[0]);
678 	init_waitqueue_head(&root->log_commit_wait[1]);
679 	INIT_LIST_HEAD(&root->log_ctxs[0]);
680 	INIT_LIST_HEAD(&root->log_ctxs[1]);
681 	atomic_set(&root->log_writers, 0);
682 	refcount_set(&root->refs, 1);
683 	atomic_set(&root->snapshot_force_cow, 0);
684 	atomic_set(&root->nr_swapfiles, 0);
685 	root->log_transid_committed = -1;
686 	if (!btrfs_is_testing(fs_info)) {
687 		btrfs_extent_io_tree_init(fs_info, &root->dirty_log_pages,
688 					  IO_TREE_ROOT_DIRTY_LOG_PAGES);
689 		btrfs_extent_io_tree_init(fs_info, &root->log_csum_range,
690 					  IO_TREE_LOG_CSUM_RANGE);
691 	}
692 
693 	spin_lock_init(&root->root_item_lock);
694 	btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
695 #ifdef CONFIG_BTRFS_DEBUG
696 	INIT_LIST_HEAD(&root->leak_list);
697 	spin_lock(&fs_info->fs_roots_radix_lock);
698 	list_add_tail(&root->leak_list, &fs_info->allocated_roots);
699 	spin_unlock(&fs_info->fs_roots_radix_lock);
700 #endif
701 
702 	return root;
703 }
704 
705 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
706 /* Should only be used by the testing infrastructure */
707 struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
708 {
709 	struct btrfs_root *root;
710 
711 	if (!fs_info)
712 		return ERR_PTR(-EINVAL);
713 
714 	root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
715 	if (!root)
716 		return ERR_PTR(-ENOMEM);
717 
718 	/* We don't use the stripesize in selftest, set it as sectorsize */
719 	root->alloc_bytenr = 0;
720 
721 	return root;
722 }
723 #endif
724 
725 static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node)
726 {
727 	const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node);
728 	const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node);
729 
730 	return btrfs_comp_cpu_keys(&a->root_key, &b->root_key);
731 }
732 
733 static int global_root_key_cmp(const void *k, const struct rb_node *node)
734 {
735 	const struct btrfs_key *key = k;
736 	const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node);
737 
738 	return btrfs_comp_cpu_keys(key, &root->root_key);
739 }
740 
741 int btrfs_global_root_insert(struct btrfs_root *root)
742 {
743 	struct btrfs_fs_info *fs_info = root->fs_info;
744 	struct rb_node *tmp;
745 	int ret = 0;
746 
747 	write_lock(&fs_info->global_root_lock);
748 	tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp);
749 	write_unlock(&fs_info->global_root_lock);
750 
751 	if (tmp) {
752 		ret = -EEXIST;
753 		btrfs_warn(fs_info, "global root %llu %llu already exists",
754 			   btrfs_root_id(root), root->root_key.offset);
755 	}
756 	return ret;
757 }
758 
759 void btrfs_global_root_delete(struct btrfs_root *root)
760 {
761 	struct btrfs_fs_info *fs_info = root->fs_info;
762 
763 	write_lock(&fs_info->global_root_lock);
764 	rb_erase(&root->rb_node, &fs_info->global_root_tree);
765 	write_unlock(&fs_info->global_root_lock);
766 }
767 
768 struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
769 				     const struct btrfs_key *key)
770 {
771 	struct rb_node *node;
772 	struct btrfs_root *root = NULL;
773 
774 	read_lock(&fs_info->global_root_lock);
775 	node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp);
776 	if (node)
777 		root = container_of(node, struct btrfs_root, rb_node);
778 	read_unlock(&fs_info->global_root_lock);
779 
780 	return root;
781 }
782 
783 static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
784 {
785 	struct btrfs_block_group *block_group;
786 	u64 ret;
787 
788 	if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
789 		return 0;
790 
791 	if (bytenr)
792 		block_group = btrfs_lookup_block_group(fs_info, bytenr);
793 	else
794 		block_group = btrfs_lookup_first_block_group(fs_info, bytenr);
795 	ASSERT(block_group);
796 	if (!block_group)
797 		return 0;
798 	ret = block_group->global_root_id;
799 	btrfs_put_block_group(block_group);
800 
801 	return ret;
802 }
803 
804 struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr)
805 {
806 	const struct btrfs_key key = {
807 		.objectid = BTRFS_CSUM_TREE_OBJECTID,
808 		.type = BTRFS_ROOT_ITEM_KEY,
809 		.offset = btrfs_global_root_id(fs_info, bytenr),
810 	};
811 
812 	return btrfs_global_root(fs_info, &key);
813 }
814 
815 struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr)
816 {
817 	const struct btrfs_key key = {
818 		.objectid = BTRFS_EXTENT_TREE_OBJECTID,
819 		.type = BTRFS_ROOT_ITEM_KEY,
820 		.offset = btrfs_global_root_id(fs_info, bytenr),
821 	};
822 
823 	return btrfs_global_root(fs_info, &key);
824 }
825 
826 struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
827 				     u64 objectid)
828 {
829 	struct btrfs_fs_info *fs_info = trans->fs_info;
830 	struct extent_buffer *leaf;
831 	struct btrfs_root *tree_root = fs_info->tree_root;
832 	struct btrfs_root *root;
833 	unsigned int nofs_flag;
834 	int ret = 0;
835 
836 	/*
837 	 * We're holding a transaction handle, so use a NOFS memory allocation
838 	 * context to avoid deadlock if reclaim happens.
839 	 */
840 	nofs_flag = memalloc_nofs_save();
841 	root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL);
842 	memalloc_nofs_restore(nofs_flag);
843 	if (!root)
844 		return ERR_PTR(-ENOMEM);
845 
846 	root->root_key.objectid = objectid;
847 	root->root_key.type = BTRFS_ROOT_ITEM_KEY;
848 	root->root_key.offset = 0;
849 
850 	leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
851 				      0, BTRFS_NESTING_NORMAL);
852 	if (IS_ERR(leaf)) {
853 		ret = PTR_ERR(leaf);
854 		leaf = NULL;
855 		goto fail;
856 	}
857 
858 	root->node = leaf;
859 	btrfs_mark_buffer_dirty(trans, leaf);
860 
861 	root->commit_root = btrfs_root_node(root);
862 	set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
863 
864 	btrfs_set_root_flags(&root->root_item, 0);
865 	btrfs_set_root_limit(&root->root_item, 0);
866 	btrfs_set_root_bytenr(&root->root_item, leaf->start);
867 	btrfs_set_root_generation(&root->root_item, trans->transid);
868 	btrfs_set_root_level(&root->root_item, 0);
869 	btrfs_set_root_refs(&root->root_item, 1);
870 	btrfs_set_root_used(&root->root_item, leaf->len);
871 	btrfs_set_root_last_snapshot(&root->root_item, 0);
872 	btrfs_set_root_dirid(&root->root_item, 0);
873 	if (btrfs_is_fstree(objectid))
874 		generate_random_guid(root->root_item.uuid);
875 	else
876 		export_guid(root->root_item.uuid, &guid_null);
877 	btrfs_set_root_drop_level(&root->root_item, 0);
878 
879 	btrfs_tree_unlock(leaf);
880 
881 	ret = btrfs_insert_root(trans, tree_root, &root->root_key, &root->root_item);
882 	if (ret)
883 		goto fail;
884 
885 	return root;
886 
887 fail:
888 	btrfs_put_root(root);
889 
890 	return ERR_PTR(ret);
891 }
892 
893 static struct btrfs_root *alloc_log_tree(struct btrfs_fs_info *fs_info)
894 {
895 	struct btrfs_root *root;
896 
897 	root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS);
898 	if (!root)
899 		return ERR_PTR(-ENOMEM);
900 
901 	root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
902 	root->root_key.type = BTRFS_ROOT_ITEM_KEY;
903 	root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
904 
905 	return root;
906 }
907 
908 int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
909 			      struct btrfs_root *root)
910 {
911 	struct extent_buffer *leaf;
912 
913 	/*
914 	 * DON'T set SHAREABLE bit for log trees.
915 	 *
916 	 * Log trees are not exposed to user space thus can't be snapshotted,
917 	 * and they go away before a real commit is actually done.
918 	 *
919 	 * They do store pointers to file data extents, and those reference
920 	 * counts still get updated (along with back refs to the log tree).
921 	 */
922 
923 	leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
924 			NULL, 0, 0, 0, 0, BTRFS_NESTING_NORMAL);
925 	if (IS_ERR(leaf))
926 		return PTR_ERR(leaf);
927 
928 	root->node = leaf;
929 
930 	btrfs_mark_buffer_dirty(trans, root->node);
931 	btrfs_tree_unlock(root->node);
932 
933 	return 0;
934 }
935 
936 int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
937 			     struct btrfs_fs_info *fs_info)
938 {
939 	struct btrfs_root *log_root;
940 
941 	log_root = alloc_log_tree(fs_info);
942 	if (IS_ERR(log_root))
943 		return PTR_ERR(log_root);
944 
945 	if (!btrfs_is_zoned(fs_info)) {
946 		int ret = btrfs_alloc_log_tree_node(trans, log_root);
947 
948 		if (ret) {
949 			btrfs_put_root(log_root);
950 			return ret;
951 		}
952 	}
953 
954 	WARN_ON(fs_info->log_root_tree);
955 	fs_info->log_root_tree = log_root;
956 	return 0;
957 }
958 
959 int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
960 		       struct btrfs_root *root)
961 {
962 	struct btrfs_fs_info *fs_info = root->fs_info;
963 	struct btrfs_root *log_root;
964 	struct btrfs_inode_item *inode_item;
965 	int ret;
966 
967 	log_root = alloc_log_tree(fs_info);
968 	if (IS_ERR(log_root))
969 		return PTR_ERR(log_root);
970 
971 	ret = btrfs_alloc_log_tree_node(trans, log_root);
972 	if (ret) {
973 		btrfs_put_root(log_root);
974 		return ret;
975 	}
976 
977 	btrfs_set_root_last_trans(log_root, trans->transid);
978 	log_root->root_key.offset = btrfs_root_id(root);
979 
980 	inode_item = &log_root->root_item.inode;
981 	btrfs_set_stack_inode_generation(inode_item, 1);
982 	btrfs_set_stack_inode_size(inode_item, 3);
983 	btrfs_set_stack_inode_nlink(inode_item, 1);
984 	btrfs_set_stack_inode_nbytes(inode_item,
985 				     fs_info->nodesize);
986 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
987 
988 	btrfs_set_root_node(&log_root->root_item, log_root->node);
989 
990 	WARN_ON(root->log_root);
991 	root->log_root = log_root;
992 	btrfs_set_root_log_transid(root, 0);
993 	root->log_transid_committed = -1;
994 	btrfs_set_root_last_log_commit(root, 0);
995 	return 0;
996 }
997 
998 static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
999 					      struct btrfs_path *path,
1000 					      const struct btrfs_key *key)
1001 {
1002 	struct btrfs_root *root;
1003 	struct btrfs_tree_parent_check check = { 0 };
1004 	struct btrfs_fs_info *fs_info = tree_root->fs_info;
1005 	u64 generation;
1006 	int ret;
1007 	int level;
1008 
1009 	root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
1010 	if (!root)
1011 		return ERR_PTR(-ENOMEM);
1012 
1013 	ret = btrfs_find_root(tree_root, key, path,
1014 			      &root->root_item, &root->root_key);
1015 	if (ret) {
1016 		if (ret > 0)
1017 			ret = -ENOENT;
1018 		goto fail;
1019 	}
1020 
1021 	generation = btrfs_root_generation(&root->root_item);
1022 	level = btrfs_root_level(&root->root_item);
1023 	check.level = level;
1024 	check.transid = generation;
1025 	check.owner_root = key->objectid;
1026 	root->node = read_tree_block(fs_info, btrfs_root_bytenr(&root->root_item),
1027 				     &check);
1028 	if (IS_ERR(root->node)) {
1029 		ret = PTR_ERR(root->node);
1030 		root->node = NULL;
1031 		goto fail;
1032 	}
1033 
1034 	ret = btrfs_buffer_uptodate(root->node, generation, &check);
1035 	if (unlikely(ret <= 0)) {
1036 		if (ret == 0)
1037 			ret = -EIO;
1038 		goto fail;
1039 	}
1040 
1041 	/*
1042 	 * For real fs, and not log/reloc trees, root owner must
1043 	 * match its root node owner
1044 	 */
1045 	if (unlikely(!btrfs_is_testing(fs_info) &&
1046 		     btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
1047 		     btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
1048 		     btrfs_root_id(root) != btrfs_header_owner(root->node))) {
1049 		btrfs_crit(fs_info,
1050 "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
1051 			   btrfs_root_id(root), root->node->start,
1052 			   btrfs_header_owner(root->node),
1053 			   btrfs_root_id(root));
1054 		ret = -EUCLEAN;
1055 		goto fail;
1056 	}
1057 	root->commit_root = btrfs_root_node(root);
1058 	return root;
1059 fail:
1060 	btrfs_put_root(root);
1061 	return ERR_PTR(ret);
1062 }
1063 
1064 struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
1065 					const struct btrfs_key *key)
1066 {
1067 	struct btrfs_root *root;
1068 	BTRFS_PATH_AUTO_FREE(path);
1069 
1070 	path = btrfs_alloc_path();
1071 	if (!path)
1072 		return ERR_PTR(-ENOMEM);
1073 	root = read_tree_root_path(tree_root, path, key);
1074 
1075 	return root;
1076 }
1077 
1078 /*
1079  * Initialize subvolume root in-memory structure.
1080  *
1081  * @anon_dev:	anonymous device to attach to the root, if zero, allocate new
1082  *
1083  * In case of failure the caller is responsible to call btrfs_free_fs_root()
1084  */
1085 static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev)
1086 {
1087 	int ret;
1088 
1089 	btrfs_drew_lock_init(&root->snapshot_lock);
1090 
1091 	if (btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
1092 	    !btrfs_is_data_reloc_root(root) &&
1093 	    btrfs_is_fstree(btrfs_root_id(root))) {
1094 		set_bit(BTRFS_ROOT_SHAREABLE, &root->state);
1095 		btrfs_check_and_init_root_item(&root->root_item);
1096 	}
1097 
1098 	/*
1099 	 * Don't assign anonymous block device to roots that are not exposed to
1100 	 * userspace, the id pool is limited to 1M
1101 	 */
1102 	if (btrfs_is_fstree(btrfs_root_id(root)) &&
1103 	    btrfs_root_refs(&root->root_item) > 0) {
1104 		if (!anon_dev) {
1105 			ret = get_anon_bdev(&root->anon_dev);
1106 			if (ret)
1107 				return ret;
1108 		} else {
1109 			root->anon_dev = anon_dev;
1110 		}
1111 	}
1112 
1113 	mutex_lock(&root->objectid_mutex);
1114 	ret = btrfs_init_root_free_objectid(root);
1115 	if (ret) {
1116 		mutex_unlock(&root->objectid_mutex);
1117 		return ret;
1118 	}
1119 
1120 	ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
1121 
1122 	mutex_unlock(&root->objectid_mutex);
1123 
1124 	return 0;
1125 }
1126 
1127 static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1128 					       u64 root_id)
1129 {
1130 	struct btrfs_root *root;
1131 
1132 	spin_lock(&fs_info->fs_roots_radix_lock);
1133 	root = radix_tree_lookup(&fs_info->fs_roots_radix,
1134 				 (unsigned long)root_id);
1135 	root = btrfs_grab_root(root);
1136 	spin_unlock(&fs_info->fs_roots_radix_lock);
1137 	return root;
1138 }
1139 
1140 static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info,
1141 						u64 objectid)
1142 {
1143 	struct btrfs_key key = {
1144 		.objectid = objectid,
1145 		.type = BTRFS_ROOT_ITEM_KEY,
1146 		.offset = 0,
1147 	};
1148 
1149 	switch (objectid) {
1150 	case BTRFS_ROOT_TREE_OBJECTID:
1151 		return btrfs_grab_root(fs_info->tree_root);
1152 	case BTRFS_EXTENT_TREE_OBJECTID:
1153 		return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1154 	case BTRFS_CHUNK_TREE_OBJECTID:
1155 		return btrfs_grab_root(fs_info->chunk_root);
1156 	case BTRFS_DEV_TREE_OBJECTID:
1157 		return btrfs_grab_root(fs_info->dev_root);
1158 	case BTRFS_CSUM_TREE_OBJECTID:
1159 		return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1160 	case BTRFS_QUOTA_TREE_OBJECTID:
1161 		return btrfs_grab_root(fs_info->quota_root);
1162 	case BTRFS_UUID_TREE_OBJECTID:
1163 		return btrfs_grab_root(fs_info->uuid_root);
1164 	case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
1165 		return btrfs_grab_root(fs_info->block_group_root);
1166 	case BTRFS_FREE_SPACE_TREE_OBJECTID:
1167 		return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1168 	case BTRFS_RAID_STRIPE_TREE_OBJECTID:
1169 		return btrfs_grab_root(fs_info->stripe_root);
1170 	case BTRFS_REMAP_TREE_OBJECTID:
1171 		return btrfs_grab_root(fs_info->remap_root);
1172 	case BTRFS_DATA_RELOC_TREE_OBJECTID:
1173 		return btrfs_grab_root(fs_info->data_reloc_root);
1174 	default:
1175 		return NULL;
1176 	}
1177 }
1178 
1179 int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1180 			 struct btrfs_root *root)
1181 {
1182 	int ret;
1183 
1184 	ret = radix_tree_preload(GFP_NOFS);
1185 	if (ret)
1186 		return ret;
1187 
1188 	spin_lock(&fs_info->fs_roots_radix_lock);
1189 	ret = radix_tree_insert(&fs_info->fs_roots_radix,
1190 				(unsigned long)btrfs_root_id(root),
1191 				root);
1192 	if (ret == 0) {
1193 		btrfs_grab_root(root);
1194 		set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
1195 	}
1196 	spin_unlock(&fs_info->fs_roots_radix_lock);
1197 	radix_tree_preload_end();
1198 
1199 	return ret;
1200 }
1201 
1202 void btrfs_check_leaked_roots(const struct btrfs_fs_info *fs_info)
1203 {
1204 #ifdef CONFIG_BTRFS_DEBUG
1205 	struct btrfs_root *root;
1206 
1207 	while (!list_empty(&fs_info->allocated_roots)) {
1208 		char buf[BTRFS_ROOT_NAME_BUF_LEN];
1209 
1210 		root = list_first_entry(&fs_info->allocated_roots,
1211 					struct btrfs_root, leak_list);
1212 		btrfs_err(fs_info, "leaked root %s refcount %d",
1213 			  btrfs_root_name(&root->root_key, buf),
1214 			  refcount_read(&root->refs));
1215 		WARN_ON_ONCE(1);
1216 		while (refcount_read(&root->refs) > 1)
1217 			btrfs_put_root(root);
1218 		btrfs_put_root(root);
1219 	}
1220 #endif
1221 }
1222 
1223 static void free_global_roots(struct btrfs_fs_info *fs_info)
1224 {
1225 	struct btrfs_root *root;
1226 	struct rb_node *node;
1227 
1228 	while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) {
1229 		root = rb_entry(node, struct btrfs_root, rb_node);
1230 		rb_erase(&root->rb_node, &fs_info->global_root_tree);
1231 		btrfs_put_root(root);
1232 	}
1233 }
1234 
1235 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
1236 {
1237 	struct percpu_counter *em_counter = &fs_info->evictable_extent_maps;
1238 
1239 	if (fs_info->fs_devices)
1240 		btrfs_close_devices(fs_info->fs_devices);
1241 	btrfs_free_compress_wsm(fs_info);
1242 	percpu_counter_destroy(&fs_info->stats_read_blocks);
1243 	percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
1244 	percpu_counter_destroy(&fs_info->delalloc_bytes);
1245 	percpu_counter_destroy(&fs_info->ordered_bytes);
1246 	if (percpu_counter_initialized(em_counter))
1247 		ASSERT(percpu_counter_sum_positive(em_counter) == 0);
1248 	percpu_counter_destroy(em_counter);
1249 	percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
1250 	btrfs_free_stripe_hash_table(fs_info);
1251 	btrfs_free_ref_cache(fs_info);
1252 	kfree(fs_info->balance_ctl);
1253 	free_global_roots(fs_info);
1254 	btrfs_put_root(fs_info->tree_root);
1255 	btrfs_put_root(fs_info->chunk_root);
1256 	btrfs_put_root(fs_info->dev_root);
1257 	btrfs_put_root(fs_info->quota_root);
1258 	btrfs_put_root(fs_info->uuid_root);
1259 	btrfs_put_root(fs_info->fs_root);
1260 	btrfs_put_root(fs_info->data_reloc_root);
1261 	btrfs_put_root(fs_info->block_group_root);
1262 	btrfs_put_root(fs_info->stripe_root);
1263 	btrfs_put_root(fs_info->remap_root);
1264 	btrfs_check_leaked_roots(fs_info);
1265 	btrfs_extent_buffer_leak_debug_check(fs_info);
1266 	kfree(fs_info->super_copy);
1267 	kfree(fs_info->super_for_commit);
1268 	kvfree(fs_info);
1269 }
1270 
1271 
1272 /*
1273  * Get an in-memory reference of a root structure.
1274  *
1275  * For essential trees like root/extent tree, we grab it from fs_info directly.
1276  * For subvolume trees, we check the cached filesystem roots first. If not
1277  * found, then read it from disk and add it to cached fs roots.
1278  *
1279  * Caller should release the root by calling btrfs_put_root() after the usage.
1280  *
1281  * NOTE: Reloc and log trees can't be read by this function as they share the
1282  *	 same root objectid.
1283  *
1284  * @objectid:	root id
1285  * @anon_dev:	preallocated anonymous block device number for new roots,
1286  *		pass NULL for a new allocation.
1287  * @check_ref:	whether to check root item references, If true, return -ENOENT
1288  *		for orphan roots
1289  */
1290 static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
1291 					     u64 objectid, dev_t *anon_dev,
1292 					     bool check_ref)
1293 {
1294 	struct btrfs_root *root;
1295 	struct btrfs_path *path;
1296 	struct btrfs_key key;
1297 	int ret;
1298 
1299 	root = btrfs_get_global_root(fs_info, objectid);
1300 	if (root)
1301 		return root;
1302 
1303 	/*
1304 	 * If we're called for non-subvolume trees, and above function didn't
1305 	 * find one, do not try to read it from disk.
1306 	 *
1307 	 * This is namely for free-space-tree and quota tree, which can change
1308 	 * at runtime and should only be grabbed from fs_info.
1309 	 *
1310 	 * The data reloc tree is also such a tree.  It is loaded at mount time
1311 	 * by read_data_reloc_root() and never inserted into the radix tree, as
1312 	 * radix tree keys are of type `unsigned long`, which is 32 bits wide
1313 	 * on 32-bit platforms.
1314 	 * Casting the objectid to `unsigned long` can therefore result in a
1315 	 * conflict with a subvolume.
1316 	 */
1317 	if (!btrfs_is_fstree(objectid))
1318 		return ERR_PTR(-ENOENT);
1319 again:
1320 	root = btrfs_lookup_fs_root(fs_info, objectid);
1321 	if (root) {
1322 		/*
1323 		 * Some other caller may have read out the newly inserted
1324 		 * subvolume already (for things like backref walk etc).  Not
1325 		 * that common but still possible.  In that case, we just need
1326 		 * to free the anon_dev.
1327 		 */
1328 		if (unlikely(anon_dev && *anon_dev)) {
1329 			free_anon_bdev(*anon_dev);
1330 			*anon_dev = 0;
1331 		}
1332 
1333 		if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1334 			btrfs_put_root(root);
1335 			return ERR_PTR(-ENOENT);
1336 		}
1337 		return root;
1338 	}
1339 
1340 	key.objectid = objectid;
1341 	key.type = BTRFS_ROOT_ITEM_KEY;
1342 	key.offset = (u64)-1;
1343 	root = btrfs_read_tree_root(fs_info->tree_root, &key);
1344 	if (IS_ERR(root))
1345 		return root;
1346 
1347 	if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1348 		ret = -ENOENT;
1349 		goto fail;
1350 	}
1351 
1352 	ret = btrfs_init_fs_root(root, anon_dev ? *anon_dev : 0);
1353 	if (ret)
1354 		goto fail;
1355 
1356 	path = btrfs_alloc_path();
1357 	if (!path) {
1358 		ret = -ENOMEM;
1359 		goto fail;
1360 	}
1361 	key.objectid = BTRFS_ORPHAN_OBJECTID;
1362 	key.type = BTRFS_ORPHAN_ITEM_KEY;
1363 	key.offset = objectid;
1364 
1365 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1366 	btrfs_free_path(path);
1367 	if (ret < 0)
1368 		goto fail;
1369 	if (ret == 0)
1370 		set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
1371 
1372 	ret = btrfs_insert_fs_root(fs_info, root);
1373 	if (ret) {
1374 		if (ret == -EEXIST) {
1375 			btrfs_put_root(root);
1376 			goto again;
1377 		}
1378 		goto fail;
1379 	}
1380 	return root;
1381 fail:
1382 	/*
1383 	 * If our caller provided us an anonymous device, then it's his
1384 	 * responsibility to free it in case we fail. So we have to set our
1385 	 * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root()
1386 	 * and once again by our caller.
1387 	 */
1388 	if (anon_dev && *anon_dev)
1389 		root->anon_dev = 0;
1390 	btrfs_put_root(root);
1391 	return ERR_PTR(ret);
1392 }
1393 
1394 /*
1395  * Get in-memory reference of a root structure
1396  *
1397  * @objectid:	tree objectid
1398  * @check_ref:	if set, verify that the tree exists and the item has at least
1399  *		one reference
1400  */
1401 struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1402 				     u64 objectid, bool check_ref)
1403 {
1404 	return btrfs_get_root_ref(fs_info, objectid, NULL, check_ref);
1405 }
1406 
1407 /*
1408  * Get in-memory reference of a root structure, created as new, optionally pass
1409  * the anonymous block device id
1410  *
1411  * @objectid:	tree objectid
1412  * @anon_dev:	if NULL, allocate a new anonymous block device or use the
1413  *		parameter value if not NULL
1414  */
1415 struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info,
1416 					 u64 objectid, dev_t *anon_dev)
1417 {
1418 	return btrfs_get_root_ref(fs_info, objectid, anon_dev, true);
1419 }
1420 
1421 /* Read and initialize the data reloc tree. */
1422 static struct btrfs_root *read_data_reloc_root(struct btrfs_fs_info *fs_info)
1423 {
1424 	struct btrfs_key key = {
1425 		.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID,
1426 		.type = BTRFS_ROOT_ITEM_KEY,
1427 		.offset = (u64)-1,
1428 	};
1429 	struct btrfs_root *root;
1430 	int ret;
1431 
1432 	root = btrfs_read_tree_root(fs_info->tree_root, &key);
1433 	if (IS_ERR(root))
1434 		return root;
1435 
1436 	if (btrfs_root_refs(&root->root_item) == 0) {
1437 		ret = -ENOENT;
1438 		goto fail;
1439 	}
1440 
1441 	/*
1442 	 * This tree can share blocks with some other fs tree during
1443 	 * relocation and we need a proper setup by btrfs_init_fs_root().
1444 	 */
1445 	ret = btrfs_init_fs_root(root, 0);
1446 	if (ret)
1447 		goto fail;
1448 	return root;
1449 fail:
1450 	btrfs_put_root(root);
1451 	return ERR_PTR(ret);
1452 }
1453 
1454 /*
1455  * Return a root for the given objectid.
1456  *
1457  * @fs_info:	the fs_info
1458  * @objectid:	the objectid we need to lookup
1459  *
1460  * This is exclusively used for backref walking, and exists specifically because
1461  * of how qgroups does lookups.  Qgroups will do a backref lookup at delayed ref
1462  * creation time, which means we may have to read the tree_root in order to look
1463  * up a fs root that is not in memory.  If the root is not in memory we will
1464  * read the tree root commit root and look up the fs root from there.  This is a
1465  * temporary root, it will not be inserted into the radix tree as it doesn't
1466  * have the most uptodate information, it'll simply be discarded once the
1467  * backref code is finished using the root.
1468  */
1469 struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info,
1470 						 struct btrfs_path *path,
1471 						 u64 objectid)
1472 {
1473 	struct btrfs_root *root;
1474 	struct btrfs_key key;
1475 
1476 	ASSERT(path->search_commit_root && path->skip_locking);
1477 
1478 	/*
1479 	 * This can return -ENOENT if we ask for a root that doesn't exist, but
1480 	 * since this is called via the backref walking code we won't be looking
1481 	 * up a root that doesn't exist, unless there's corruption.  So if root
1482 	 * != NULL just return it.
1483 	 */
1484 	root = btrfs_get_global_root(fs_info, objectid);
1485 	if (root)
1486 		return root;
1487 
1488 	root = btrfs_lookup_fs_root(fs_info, objectid);
1489 	if (root)
1490 		return root;
1491 
1492 	key.objectid = objectid;
1493 	key.type = BTRFS_ROOT_ITEM_KEY;
1494 	key.offset = (u64)-1;
1495 	root = read_tree_root_path(fs_info->tree_root, path, &key);
1496 	btrfs_release_path(path);
1497 
1498 	return root;
1499 }
1500 
1501 static int cleaner_kthread(void *arg)
1502 {
1503 	struct btrfs_fs_info *fs_info = arg;
1504 	int again;
1505 
1506 	while (1) {
1507 		again = 0;
1508 
1509 		set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1510 
1511 		/* Make the cleaner go to sleep early. */
1512 		if (btrfs_need_cleaner_sleep(fs_info))
1513 			goto sleep;
1514 
1515 		/*
1516 		 * Do not do anything if we might cause open_ctree() to block
1517 		 * before we have finished mounting the filesystem.
1518 		 */
1519 		if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1520 			goto sleep;
1521 
1522 		if (!mutex_trylock(&fs_info->cleaner_mutex))
1523 			goto sleep;
1524 
1525 		/*
1526 		 * Avoid the problem that we change the status of the fs
1527 		 * during the above check and trylock.
1528 		 */
1529 		if (btrfs_need_cleaner_sleep(fs_info)) {
1530 			mutex_unlock(&fs_info->cleaner_mutex);
1531 			goto sleep;
1532 		}
1533 
1534 		if (test_and_clear_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags))
1535 			btrfs_sysfs_feature_update(fs_info);
1536 
1537 		btrfs_run_delayed_iputs(fs_info);
1538 
1539 		set_bit(BTRFS_QGROUP_RUNTIME_BIT_REJECT_RESCAN, &fs_info->qgroup_flags);
1540 		again = btrfs_clean_one_deleted_snapshot(fs_info);
1541 		clear_bit(BTRFS_QGROUP_RUNTIME_BIT_REJECT_RESCAN, &fs_info->qgroup_flags);
1542 		mutex_unlock(&fs_info->cleaner_mutex);
1543 
1544 		/*
1545 		 * The defragger has dealt with the R/O remount and umount,
1546 		 * needn't do anything special here.
1547 		 */
1548 		btrfs_run_defrag_inodes(fs_info);
1549 
1550 		if (btrfs_fs_incompat(fs_info, REMAP_TREE) &&
1551 		    !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1552 			btrfs_handle_fully_remapped_bgs(fs_info);
1553 
1554 		/*
1555 		 * Acquires fs_info->reclaim_bgs_lock to avoid racing
1556 		 * with relocation (btrfs_relocate_chunk) and relocation
1557 		 * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
1558 		 * after acquiring fs_info->reclaim_bgs_lock. So we
1559 		 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
1560 		 * unused block groups.
1561 		 */
1562 		btrfs_delete_unused_bgs(fs_info);
1563 
1564 		/*
1565 		 * Reclaim block groups in the reclaim_bgs list after we deleted
1566 		 * all unused block_groups. This possibly gives us some more free
1567 		 * space.
1568 		 */
1569 		btrfs_reclaim_bgs(fs_info);
1570 sleep:
1571 		clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1572 		if (kthread_should_park())
1573 			kthread_parkme();
1574 		if (kthread_should_stop())
1575 			return 0;
1576 		if (!again) {
1577 			set_current_state(TASK_INTERRUPTIBLE);
1578 			schedule();
1579 			__set_current_state(TASK_RUNNING);
1580 		}
1581 	}
1582 }
1583 
1584 static int transaction_kthread(void *arg)
1585 {
1586 	struct btrfs_root *root = arg;
1587 	struct btrfs_fs_info *fs_info = root->fs_info;
1588 	struct btrfs_trans_handle *trans;
1589 	struct btrfs_transaction *cur;
1590 	u64 transid;
1591 	time64_t delta;
1592 	unsigned long delay;
1593 	bool cannot_commit;
1594 
1595 	do {
1596 		cannot_commit = false;
1597 		delay = secs_to_jiffies(fs_info->commit_interval);
1598 		mutex_lock(&fs_info->transaction_kthread_mutex);
1599 
1600 		spin_lock(&fs_info->trans_lock);
1601 		cur = fs_info->running_transaction;
1602 		if (!cur) {
1603 			spin_unlock(&fs_info->trans_lock);
1604 			goto sleep;
1605 		}
1606 
1607 		delta = ktime_get_seconds() - cur->start_time;
1608 		if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) &&
1609 		    cur->state < TRANS_STATE_COMMIT_PREP &&
1610 		    delta < fs_info->commit_interval) {
1611 			spin_unlock(&fs_info->trans_lock);
1612 			delay -= secs_to_jiffies(delta - 1);
1613 			delay = min(delay,
1614 				    secs_to_jiffies(fs_info->commit_interval));
1615 			goto sleep;
1616 		}
1617 		transid = cur->transid;
1618 		spin_unlock(&fs_info->trans_lock);
1619 
1620 		/* If the file system is aborted, this will always fail. */
1621 		trans = btrfs_attach_transaction(root);
1622 		if (IS_ERR(trans)) {
1623 			if (PTR_ERR(trans) != -ENOENT)
1624 				cannot_commit = true;
1625 			goto sleep;
1626 		}
1627 		if (transid == trans->transid) {
1628 			btrfs_commit_transaction(trans);
1629 		} else {
1630 			btrfs_end_transaction(trans);
1631 		}
1632 sleep:
1633 		wake_up_process(fs_info->cleaner_kthread);
1634 		mutex_unlock(&fs_info->transaction_kthread_mutex);
1635 
1636 		if (unlikely(BTRFS_FS_ERROR(fs_info)))
1637 			btrfs_cleanup_transaction(fs_info);
1638 		if (!kthread_should_stop() &&
1639 				(!btrfs_transaction_blocked(fs_info) ||
1640 				 cannot_commit))
1641 			schedule_timeout_interruptible(delay);
1642 	} while (!kthread_should_stop());
1643 	return 0;
1644 }
1645 
1646 /*
1647  * This will find the highest generation in the array of root backups.  The
1648  * index of the highest array is returned, or -EINVAL if we can't find
1649  * anything.
1650  *
1651  * We check to make sure the array is valid by comparing the
1652  * generation of the latest  root in the array with the generation
1653  * in the super block.  If they don't match we pitch it.
1654  */
1655 static int find_newest_super_backup(struct btrfs_fs_info *info)
1656 {
1657 	const u64 newest_gen = btrfs_super_generation(info->super_copy);
1658 	u64 cur;
1659 	struct btrfs_root_backup *root_backup;
1660 	int i;
1661 
1662 	for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
1663 		root_backup = info->super_copy->super_roots + i;
1664 		cur = btrfs_backup_tree_root_gen(root_backup);
1665 		if (cur == newest_gen)
1666 			return i;
1667 	}
1668 
1669 	return -EINVAL;
1670 }
1671 
1672 /*
1673  * copy all the root pointers into the super backup array.
1674  * this will bump the backup pointer by one when it is
1675  * done
1676  */
1677 static int backup_super_roots(struct btrfs_fs_info *info)
1678 {
1679 	const int next_backup = info->backup_root_index;
1680 	struct btrfs_root_backup *root_backup;
1681 
1682 	root_backup = info->super_for_commit->super_roots + next_backup;
1683 
1684 	/*
1685 	 * make sure all of our padding and empty slots get zero filled
1686 	 * regardless of which ones we use today
1687 	 */
1688 	memset(root_backup, 0, sizeof(*root_backup));
1689 
1690 	info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
1691 
1692 	btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
1693 	btrfs_set_backup_tree_root_gen(root_backup,
1694 			       btrfs_header_generation(info->tree_root->node));
1695 
1696 	btrfs_set_backup_tree_root_level(root_backup,
1697 			       btrfs_header_level(info->tree_root->node));
1698 
1699 	btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
1700 	btrfs_set_backup_chunk_root_gen(root_backup,
1701 			       btrfs_header_generation(info->chunk_root->node));
1702 	btrfs_set_backup_chunk_root_level(root_backup,
1703 			       btrfs_header_level(info->chunk_root->node));
1704 
1705 	if (!btrfs_fs_incompat(info, EXTENT_TREE_V2)) {
1706 		struct btrfs_root *extent_root = btrfs_extent_root(info, 0);
1707 		struct btrfs_root *csum_root = btrfs_csum_root(info, 0);
1708 
1709 		if (unlikely(!extent_root)) {
1710 			btrfs_err(info, "missing extent root for extent at bytenr 0");
1711 			return -EUCLEAN;
1712 		}
1713 		if (unlikely(!csum_root)) {
1714 			btrfs_err(info, "missing csum root for extent at bytenr 0");
1715 			return -EUCLEAN;
1716 		}
1717 
1718 		btrfs_set_backup_extent_root(root_backup,
1719 					     extent_root->node->start);
1720 		btrfs_set_backup_extent_root_gen(root_backup,
1721 				btrfs_header_generation(extent_root->node));
1722 		btrfs_set_backup_extent_root_level(root_backup,
1723 					btrfs_header_level(extent_root->node));
1724 
1725 		btrfs_set_backup_csum_root(root_backup, csum_root->node->start);
1726 		btrfs_set_backup_csum_root_gen(root_backup,
1727 					       btrfs_header_generation(csum_root->node));
1728 		btrfs_set_backup_csum_root_level(root_backup,
1729 						 btrfs_header_level(csum_root->node));
1730 	}
1731 
1732 	/*
1733 	 * we might commit during log recovery, which happens before we set
1734 	 * the fs_root.  Make sure it is valid before we fill it in.
1735 	 */
1736 	if (info->fs_root && info->fs_root->node) {
1737 		btrfs_set_backup_fs_root(root_backup,
1738 					 info->fs_root->node->start);
1739 		btrfs_set_backup_fs_root_gen(root_backup,
1740 			       btrfs_header_generation(info->fs_root->node));
1741 		btrfs_set_backup_fs_root_level(root_backup,
1742 			       btrfs_header_level(info->fs_root->node));
1743 	}
1744 
1745 	btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
1746 	btrfs_set_backup_dev_root_gen(root_backup,
1747 			       btrfs_header_generation(info->dev_root->node));
1748 	btrfs_set_backup_dev_root_level(root_backup,
1749 				       btrfs_header_level(info->dev_root->node));
1750 
1751 	btrfs_set_backup_total_bytes(root_backup,
1752 			     btrfs_super_total_bytes(info->super_copy));
1753 	btrfs_set_backup_bytes_used(root_backup,
1754 			     btrfs_super_bytes_used(info->super_copy));
1755 	btrfs_set_backup_num_devices(root_backup,
1756 			     btrfs_super_num_devices(info->super_copy));
1757 
1758 	/*
1759 	 * if we don't copy this out to the super_copy, it won't get remembered
1760 	 * for the next commit
1761 	 */
1762 	memcpy(&info->super_copy->super_roots,
1763 	       &info->super_for_commit->super_roots,
1764 	       sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
1765 
1766 	return 0;
1767 }
1768 
1769 /*
1770  * Reads a backup root based on the passed priority. Prio 0 is the newest, prio
1771  * 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
1772  *
1773  * @fs_info:  filesystem whose backup roots need to be read
1774  * @priority: priority of backup root required
1775  *
1776  * Returns backup root index on success and -EINVAL otherwise.
1777  */
1778 static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority)
1779 {
1780 	int backup_index = find_newest_super_backup(fs_info);
1781 	struct btrfs_super_block *super = fs_info->super_copy;
1782 	struct btrfs_root_backup *root_backup;
1783 
1784 	if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) {
1785 		if (priority == 0)
1786 			return backup_index;
1787 
1788 		backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority;
1789 		backup_index %= BTRFS_NUM_BACKUP_ROOTS;
1790 	} else {
1791 		return -EINVAL;
1792 	}
1793 
1794 	root_backup = super->super_roots + backup_index;
1795 
1796 	btrfs_set_super_generation(super,
1797 				   btrfs_backup_tree_root_gen(root_backup));
1798 	btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
1799 	btrfs_set_super_root_level(super,
1800 				   btrfs_backup_tree_root_level(root_backup));
1801 	btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
1802 
1803 	/*
1804 	 * Fixme: the total bytes and num_devices need to match or we should
1805 	 * need a fsck
1806 	 */
1807 	btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
1808 	btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
1809 
1810 	return backup_index;
1811 }
1812 
1813 /* helper to cleanup workers */
1814 static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
1815 {
1816 	if (fs_info->fixup_workers)
1817 		destroy_workqueue(fs_info->fixup_workers);
1818 	btrfs_destroy_workqueue(fs_info->delalloc_workers);
1819 	btrfs_destroy_workqueue(fs_info->workers);
1820 	if (fs_info->endio_workers)
1821 		destroy_workqueue(fs_info->endio_workers);
1822 	if (fs_info->rmw_workers)
1823 		destroy_workqueue(fs_info->rmw_workers);
1824 	btrfs_destroy_workqueue(fs_info->endio_write_workers);
1825 	btrfs_destroy_workqueue(fs_info->delayed_workers);
1826 	btrfs_destroy_workqueue(fs_info->caching_workers);
1827 	btrfs_destroy_workqueue(fs_info->flush_workers);
1828 	btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
1829 	if (fs_info->discard_ctl.discard_workers)
1830 		destroy_workqueue(fs_info->discard_ctl.discard_workers);
1831 	/*
1832 	 * Now that all other work queues are destroyed, we can safely destroy
1833 	 * the queues used for metadata I/O, since tasks from those other work
1834 	 * queues can do metadata I/O operations.
1835 	 */
1836 	if (fs_info->endio_meta_workers)
1837 		destroy_workqueue(fs_info->endio_meta_workers);
1838 }
1839 
1840 static void free_root_extent_buffers(struct btrfs_root *root)
1841 {
1842 	if (root) {
1843 		free_extent_buffer(root->node);
1844 		free_extent_buffer(root->commit_root);
1845 		root->node = NULL;
1846 		root->commit_root = NULL;
1847 	}
1848 }
1849 
1850 static void free_global_root_pointers(struct btrfs_fs_info *fs_info)
1851 {
1852 	struct btrfs_root *root, *tmp;
1853 
1854 	rbtree_postorder_for_each_entry_safe(root, tmp,
1855 					     &fs_info->global_root_tree,
1856 					     rb_node)
1857 		free_root_extent_buffers(root);
1858 }
1859 
1860 /* helper to cleanup tree roots */
1861 static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
1862 {
1863 	free_root_extent_buffers(info->tree_root);
1864 
1865 	free_global_root_pointers(info);
1866 	free_root_extent_buffers(info->dev_root);
1867 	free_root_extent_buffers(info->quota_root);
1868 	free_root_extent_buffers(info->uuid_root);
1869 	free_root_extent_buffers(info->fs_root);
1870 	free_root_extent_buffers(info->data_reloc_root);
1871 	free_root_extent_buffers(info->block_group_root);
1872 	free_root_extent_buffers(info->stripe_root);
1873 	free_root_extent_buffers(info->remap_root);
1874 	if (free_chunk_root)
1875 		free_root_extent_buffers(info->chunk_root);
1876 }
1877 
1878 void btrfs_put_root(struct btrfs_root *root)
1879 {
1880 	if (!root)
1881 		return;
1882 
1883 	if (refcount_dec_and_test(&root->refs)) {
1884 		if (WARN_ON(!xa_empty(&root->inodes)))
1885 			xa_destroy(&root->inodes);
1886 		if (WARN_ON(!xa_empty(&root->delayed_nodes)))
1887 			xa_destroy(&root->delayed_nodes);
1888 		WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
1889 		if (root->anon_dev)
1890 			free_anon_bdev(root->anon_dev);
1891 		free_root_extent_buffers(root);
1892 		btrfs_qgroup_clean_swapped_blocks(root);
1893 #ifdef CONFIG_BTRFS_DEBUG
1894 		spin_lock(&root->fs_info->fs_roots_radix_lock);
1895 		list_del_init(&root->leak_list);
1896 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
1897 #endif
1898 		kfree(root);
1899 	}
1900 }
1901 
1902 void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
1903 {
1904 	int ret;
1905 	struct btrfs_root *gang[8];
1906 	int i;
1907 
1908 	/*
1909 	 * Some dirty roots may still be linked to dirty_cowonly_roots after
1910 	 * the transaction is aborted. Cleanup them here.
1911 	 */
1912 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
1913 		struct btrfs_root *root;
1914 
1915 		root = list_first_entry(&fs_info->dirty_cowonly_roots,
1916 					struct btrfs_root, dirty_list);
1917 		list_del_init(&root->dirty_list);
1918 		/*
1919 		 * The dirty_log_pages should never be populated for
1920 		 * cowonly trees.
1921 		 */
1922 		spin_lock(&root->dirty_log_pages.lock);
1923 		ASSERT(RB_EMPTY_ROOT(&root->dirty_log_pages.state));
1924 		spin_unlock(&root->dirty_log_pages.lock);
1925 	}
1926 
1927 	while (!list_empty(&fs_info->dead_roots)) {
1928 		gang[0] = list_first_entry(&fs_info->dead_roots,
1929 					   struct btrfs_root, root_list);
1930 		list_del(&gang[0]->root_list);
1931 
1932 		if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state))
1933 			btrfs_drop_and_free_fs_root(fs_info, gang[0]);
1934 		btrfs_put_root(gang[0]);
1935 	}
1936 
1937 	while (1) {
1938 		ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1939 					     (void **)gang, 0,
1940 					     ARRAY_SIZE(gang));
1941 		if (!ret)
1942 			break;
1943 		for (i = 0; i < ret; i++)
1944 			btrfs_drop_and_free_fs_root(fs_info, gang[i]);
1945 	}
1946 }
1947 
1948 static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
1949 {
1950 	mutex_init(&fs_info->scrub_lock);
1951 	atomic_set(&fs_info->scrubs_running, 0);
1952 	atomic_set(&fs_info->scrub_pause_req, 0);
1953 	atomic_set(&fs_info->scrubs_paused, 0);
1954 	atomic_set(&fs_info->scrub_cancel_req, 0);
1955 	init_waitqueue_head(&fs_info->scrub_pause_wait);
1956 	refcount_set(&fs_info->scrub_workers_refcnt, 0);
1957 }
1958 
1959 static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
1960 {
1961 	spin_lock_init(&fs_info->balance_lock);
1962 	mutex_init(&fs_info->balance_mutex);
1963 	atomic_set(&fs_info->balance_pause_req, 0);
1964 	atomic_set(&fs_info->balance_cancel_req, 0);
1965 	fs_info->balance_ctl = NULL;
1966 	init_waitqueue_head(&fs_info->balance_wait_q);
1967 	atomic_set(&fs_info->reloc_cancel_req, 0);
1968 }
1969 
1970 static int btrfs_init_btree_inode(struct super_block *sb)
1971 {
1972 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1973 	unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
1974 					      fs_info->tree_root);
1975 	struct inode *inode;
1976 
1977 	inode = new_inode(sb);
1978 	if (!inode)
1979 		return -ENOMEM;
1980 
1981 	btrfs_set_inode_number(BTRFS_I(inode), BTRFS_BTREE_INODE_OBJECTID);
1982 	set_nlink(inode, 1);
1983 	/*
1984 	 * we set the i_size on the btree inode to the max possible int.
1985 	 * the real end of the address space is determined by all of
1986 	 * the devices in the system
1987 	 */
1988 	inode->i_size = OFFSET_MAX;
1989 	inode->i_mapping->a_ops = &btree_aops;
1990 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
1991 
1992 	btrfs_extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
1993 				  IO_TREE_BTREE_INODE_IO);
1994 	btrfs_extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
1995 
1996 	BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root);
1997 	set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
1998 	__insert_inode_hash(inode, hash);
1999 	set_bit(AS_KERNEL_FILE, &inode->i_mapping->flags);
2000 	fs_info->btree_inode = inode;
2001 
2002 	return 0;
2003 }
2004 
2005 static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
2006 {
2007 	mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
2008 	init_rwsem(&fs_info->dev_replace.rwsem);
2009 	init_waitqueue_head(&fs_info->dev_replace.replace_wait);
2010 }
2011 
2012 static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
2013 {
2014 	spin_lock_init(&fs_info->qgroup_lock);
2015 	mutex_init(&fs_info->qgroup_ioctl_lock);
2016 	fs_info->qgroup_tree = RB_ROOT;
2017 	INIT_LIST_HEAD(&fs_info->dirty_qgroups);
2018 	fs_info->qgroup_seq = 1;
2019 	fs_info->qgroup_rescan_running = false;
2020 	fs_info->qgroup_drop_subtree_thres = BTRFS_QGROUP_DROP_SUBTREE_THRES_DEFAULT;
2021 	mutex_init(&fs_info->qgroup_rescan_lock);
2022 }
2023 
2024 static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
2025 {
2026 	u32 max_active = fs_info->thread_pool_size;
2027 	unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
2028 	unsigned int ordered_flags = WQ_MEM_RECLAIM | WQ_FREEZABLE;
2029 
2030 	fs_info->workers =
2031 		btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16);
2032 
2033 	fs_info->delalloc_workers =
2034 		btrfs_alloc_workqueue(fs_info, "delalloc",
2035 				      flags, max_active, 2);
2036 
2037 	fs_info->flush_workers =
2038 		btrfs_alloc_workqueue(fs_info, "flush_delalloc",
2039 				      flags, max_active, 0);
2040 
2041 	fs_info->caching_workers =
2042 		btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
2043 
2044 	fs_info->fixup_workers =
2045 		alloc_ordered_workqueue("btrfs-fixup", ordered_flags);
2046 
2047 	fs_info->endio_workers =
2048 		alloc_workqueue("btrfs-endio", flags, max_active);
2049 	fs_info->endio_meta_workers =
2050 		alloc_workqueue("btrfs-endio-meta", flags, max_active);
2051 	fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);
2052 	fs_info->endio_write_workers =
2053 		btrfs_alloc_workqueue(fs_info, "endio-write", flags,
2054 				      max_active, 2);
2055 	fs_info->delayed_workers =
2056 		btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
2057 				      max_active, 0);
2058 	fs_info->qgroup_rescan_workers =
2059 		btrfs_alloc_ordered_workqueue(fs_info, "qgroup-rescan",
2060 					      ordered_flags);
2061 	fs_info->discard_ctl.discard_workers =
2062 		alloc_ordered_workqueue("btrfs-discard", WQ_FREEZABLE);
2063 
2064 	if (!(fs_info->workers &&
2065 	      fs_info->delalloc_workers && fs_info->flush_workers &&
2066 	      fs_info->endio_workers && fs_info->endio_meta_workers &&
2067 	      fs_info->endio_write_workers && fs_info->rmw_workers &&
2068 	      fs_info->caching_workers && fs_info->fixup_workers &&
2069 	      fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
2070 	      fs_info->discard_ctl.discard_workers)) {
2071 		return -ENOMEM;
2072 	}
2073 
2074 	return 0;
2075 }
2076 
2077 static void btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
2078 {
2079 	/* Check if the checksum implementation is a fast accelerated one. */
2080 	switch (csum_type) {
2081 	case BTRFS_CSUM_TYPE_CRC32:
2082 		if (crc32_optimizations() & CRC32C_OPTIMIZATION)
2083 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
2084 		break;
2085 	case BTRFS_CSUM_TYPE_XXHASH:
2086 		set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
2087 		break;
2088 	default:
2089 		break;
2090 	}
2091 
2092 	btrfs_info(fs_info, "using %s checksum algorithm",
2093 		   btrfs_super_csum_name(csum_type));
2094 }
2095 
2096 static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2097 			    struct btrfs_fs_devices *fs_devices)
2098 {
2099 	int ret;
2100 	struct btrfs_tree_parent_check check = { 0 };
2101 	struct btrfs_root *log_tree_root;
2102 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2103 	u64 bytenr = btrfs_super_log_root(disk_super);
2104 	int level = btrfs_super_log_root_level(disk_super);
2105 
2106 	if (unlikely(fs_devices->rw_devices == 0)) {
2107 		btrfs_err(fs_info, "log replay required on RO media");
2108 		return -EIO;
2109 	}
2110 
2111 	log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID,
2112 					 GFP_KERNEL);
2113 	if (!log_tree_root)
2114 		return -ENOMEM;
2115 
2116 	check.level = level;
2117 	check.transid = fs_info->generation + 1;
2118 	check.owner_root = BTRFS_TREE_LOG_OBJECTID;
2119 	log_tree_root->node = read_tree_block(fs_info, bytenr, &check);
2120 	if (IS_ERR(log_tree_root->node)) {
2121 		ret = PTR_ERR(log_tree_root->node);
2122 		log_tree_root->node = NULL;
2123 		btrfs_err(fs_info, "failed to read log tree with error: %pe", ERR_PTR(ret));
2124 		btrfs_put_root(log_tree_root);
2125 		return ret;
2126 	}
2127 
2128 	/* returns with log_tree_root freed on success */
2129 	ret = btrfs_recover_log_trees(log_tree_root);
2130 	btrfs_put_root(log_tree_root);
2131 	if (unlikely(ret)) {
2132 		ASSERT(BTRFS_FS_ERROR(fs_info) != 0);
2133 		btrfs_err(fs_info, "failed to recover log trees with error: %pe", ERR_PTR(ret));
2134 		return ret;
2135 	}
2136 
2137 	if (sb_rdonly(fs_info->sb)) {
2138 		ret = btrfs_commit_super(fs_info);
2139 		if (ret)
2140 			return ret;
2141 	}
2142 
2143 	return 0;
2144 }
2145 
2146 static int load_global_roots_objectid(struct btrfs_root *tree_root,
2147 				      struct btrfs_path *path, u64 objectid,
2148 				      const char *name)
2149 {
2150 	struct btrfs_fs_info *fs_info = tree_root->fs_info;
2151 	struct btrfs_root *root;
2152 	u64 max_global_id = 0;
2153 	int ret;
2154 	struct btrfs_key key = {
2155 		.objectid = objectid,
2156 		.type = BTRFS_ROOT_ITEM_KEY,
2157 		.offset = 0,
2158 	};
2159 	bool found = false;
2160 
2161 	/* If we have IGNOREDATACSUMS skip loading these roots. */
2162 	if (objectid == BTRFS_CSUM_TREE_OBJECTID &&
2163 	    btrfs_test_opt(fs_info, IGNOREDATACSUMS)) {
2164 		set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
2165 		return 0;
2166 	}
2167 
2168 	while (1) {
2169 		ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2170 		if (ret < 0)
2171 			break;
2172 
2173 		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2174 			ret = btrfs_next_leaf(tree_root, path);
2175 			if (ret) {
2176 				if (ret > 0)
2177 					ret = 0;
2178 				break;
2179 			}
2180 		}
2181 		ret = 0;
2182 
2183 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2184 		if (key.objectid != objectid)
2185 			break;
2186 		btrfs_release_path(path);
2187 
2188 		/*
2189 		 * Just worry about this for extent tree, it'll be the same for
2190 		 * everybody.
2191 		 */
2192 		if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2193 			max_global_id = max(max_global_id, key.offset);
2194 
2195 		found = true;
2196 		root = read_tree_root_path(tree_root, path, &key);
2197 		if (IS_ERR(root)) {
2198 			ret = PTR_ERR(root);
2199 			break;
2200 		}
2201 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2202 		ret = btrfs_global_root_insert(root);
2203 		if (ret) {
2204 			btrfs_put_root(root);
2205 			break;
2206 		}
2207 		key.offset++;
2208 	}
2209 	btrfs_release_path(path);
2210 
2211 	if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2212 		fs_info->nr_global_roots = max_global_id + 1;
2213 
2214 	if (!found || ret) {
2215 		if (objectid == BTRFS_CSUM_TREE_OBJECTID)
2216 			set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
2217 
2218 		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2219 			ret = ret ? ret : -ENOENT;
2220 		else
2221 			ret = 0;
2222 		btrfs_err(fs_info, "failed to load root %s", name);
2223 	}
2224 	return ret;
2225 }
2226 
2227 static int load_global_roots(struct btrfs_root *tree_root)
2228 {
2229 	BTRFS_PATH_AUTO_FREE(path);
2230 	int ret;
2231 
2232 	path = btrfs_alloc_path();
2233 	if (!path)
2234 		return -ENOMEM;
2235 
2236 	ret = load_global_roots_objectid(tree_root, path,
2237 					 BTRFS_EXTENT_TREE_OBJECTID, "extent");
2238 	if (ret)
2239 		return ret;
2240 	ret = load_global_roots_objectid(tree_root, path,
2241 					 BTRFS_CSUM_TREE_OBJECTID, "csum");
2242 	if (ret)
2243 		return ret;
2244 	if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE))
2245 		return ret;
2246 
2247 	return load_global_roots_objectid(tree_root, path,
2248 					  BTRFS_FREE_SPACE_TREE_OBJECTID,
2249 					  "free space");
2250 }
2251 
2252 static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
2253 {
2254 	struct btrfs_root *tree_root = fs_info->tree_root;
2255 	struct btrfs_root *root;
2256 	struct btrfs_key location;
2257 	int ret;
2258 
2259 	ASSERT(fs_info->tree_root);
2260 
2261 	ret = load_global_roots(tree_root);
2262 	if (ret)
2263 		return ret;
2264 
2265 	location.type = BTRFS_ROOT_ITEM_KEY;
2266 	location.offset = 0;
2267 
2268 	if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) {
2269 		location.objectid = BTRFS_BLOCK_GROUP_TREE_OBJECTID;
2270 		root = btrfs_read_tree_root(tree_root, &location);
2271 		if (IS_ERR(root)) {
2272 			if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2273 				ret = PTR_ERR(root);
2274 				goto out;
2275 			}
2276 		} else {
2277 			set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2278 			fs_info->block_group_root = root;
2279 		}
2280 	}
2281 
2282 	location.objectid = BTRFS_DEV_TREE_OBJECTID;
2283 	root = btrfs_read_tree_root(tree_root, &location);
2284 	if (IS_ERR(root)) {
2285 		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2286 			ret = PTR_ERR(root);
2287 			goto out;
2288 		}
2289 	} else {
2290 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2291 		fs_info->dev_root = root;
2292 	}
2293 	/* Initialize fs_info for all devices in any case */
2294 	ret = btrfs_init_devices_late(fs_info);
2295 	if (ret)
2296 		goto out;
2297 
2298 	if (btrfs_fs_incompat(fs_info, REMAP_TREE)) {
2299 		/* The remap_root has already been loaded in load_important_roots(). */
2300 		root = fs_info->remap_root;
2301 
2302 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2303 
2304 		root->root_key.objectid = BTRFS_REMAP_TREE_OBJECTID;
2305 		root->root_key.type = BTRFS_ROOT_ITEM_KEY;
2306 		root->root_key.offset = 0;
2307 
2308 		/* Check that data reloc tree doesn't also exist. */
2309 		location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
2310 		root = btrfs_read_tree_root(fs_info->tree_root, &location);
2311 		if (!IS_ERR(root)) {
2312 			btrfs_err(fs_info, "data reloc tree exists when remap-tree enabled");
2313 			btrfs_put_root(root);
2314 			return -EIO;
2315 		} else if (PTR_ERR(root) != -ENOENT) {
2316 			btrfs_warn(fs_info, "error %ld when checking for data reloc tree",
2317 				   PTR_ERR(root));
2318 		}
2319 	} else {
2320 		root = read_data_reloc_root(fs_info);
2321 		if (IS_ERR(root)) {
2322 			if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2323 				location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
2324 				ret = PTR_ERR(root);
2325 				goto out;
2326 			}
2327 		} else {
2328 			set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2329 			fs_info->data_reloc_root = root;
2330 		}
2331 	}
2332 
2333 	location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
2334 	root = btrfs_read_tree_root(tree_root, &location);
2335 	if (!IS_ERR(root)) {
2336 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2337 		fs_info->quota_root = root;
2338 	}
2339 
2340 	location.objectid = BTRFS_UUID_TREE_OBJECTID;
2341 	root = btrfs_read_tree_root(tree_root, &location);
2342 	if (IS_ERR(root)) {
2343 		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2344 			ret = PTR_ERR(root);
2345 			if (ret != -ENOENT)
2346 				goto out;
2347 		}
2348 	} else {
2349 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2350 		fs_info->uuid_root = root;
2351 	}
2352 
2353 	if (btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE)) {
2354 		location.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID;
2355 		root = btrfs_read_tree_root(tree_root, &location);
2356 		if (IS_ERR(root)) {
2357 			if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2358 				ret = PTR_ERR(root);
2359 				goto out;
2360 			}
2361 		} else {
2362 			set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2363 			fs_info->stripe_root = root;
2364 		}
2365 	}
2366 
2367 	return 0;
2368 out:
2369 	btrfs_warn(fs_info, "failed to read root (objectid=%llu): %pe",
2370 		   location.objectid, ERR_PTR(ret));
2371 	return ret;
2372 }
2373 
2374 static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info,
2375 				    const struct btrfs_super_block *sb)
2376 {
2377 	unsigned int cur = 0; /* Offset inside the sys chunk array */
2378 	/*
2379 	 * At sb read time, fs_info is not fully initialized. Thus we have
2380 	 * to use super block sectorsize, which should have been validated.
2381 	 */
2382 	const u32 sectorsize = btrfs_super_sectorsize(sb);
2383 	u32 sys_array_size = btrfs_super_sys_array_size(sb);
2384 
2385 	if (unlikely(sys_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)) {
2386 		btrfs_err(fs_info, "system chunk array too big %u > %u",
2387 			  sys_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
2388 		return -EUCLEAN;
2389 	}
2390 
2391 	/* It must hold at least one key and one chunk. */
2392 	if (unlikely(sys_array_size < sizeof(struct btrfs_disk_key) +
2393 		     sizeof(struct btrfs_chunk))) {
2394 		btrfs_err(fs_info, "system chunk array too small %u < %zu",
2395 			  sys_array_size,
2396 			  sizeof(struct btrfs_disk_key) + sizeof(struct btrfs_chunk));
2397 		return -EUCLEAN;
2398 	}
2399 
2400 	while (cur < sys_array_size) {
2401 		struct btrfs_disk_key *disk_key;
2402 		struct btrfs_chunk *chunk;
2403 		struct btrfs_key key;
2404 		u64 type;
2405 		u16 num_stripes;
2406 		u32 len;
2407 		int ret;
2408 
2409 		disk_key = (struct btrfs_disk_key *)(sb->sys_chunk_array + cur);
2410 		len = sizeof(*disk_key);
2411 
2412 		if (unlikely(cur + len > sys_array_size))
2413 			goto short_read;
2414 		cur += len;
2415 
2416 		btrfs_disk_key_to_cpu(&key, disk_key);
2417 		if (unlikely(key.type != BTRFS_CHUNK_ITEM_KEY)) {
2418 			btrfs_err(fs_info,
2419 			    "unexpected item type %u in sys_array at offset %u",
2420 				  key.type, cur);
2421 			return -EUCLEAN;
2422 		}
2423 
2424 		if (unlikely(cur + sizeof(*chunk) > sys_array_size))
2425 			goto short_read;
2426 
2427 		chunk = (struct btrfs_chunk *)(sb->sys_chunk_array + cur);
2428 		num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2429 		if (unlikely(cur + btrfs_chunk_item_size(num_stripes) > sys_array_size))
2430 			goto short_read;
2431 		type = btrfs_stack_chunk_type(chunk);
2432 		if (unlikely(!(type & BTRFS_BLOCK_GROUP_SYSTEM))) {
2433 			btrfs_err(fs_info,
2434 			"invalid chunk type %llu in sys_array at offset %u",
2435 				  type, cur);
2436 			return -EUCLEAN;
2437 		}
2438 		ret = btrfs_check_chunk_valid(fs_info, NULL, chunk, key.offset,
2439 					      sectorsize);
2440 		if (unlikely(ret < 0))
2441 			return ret;
2442 		cur += btrfs_chunk_item_size(num_stripes);
2443 	}
2444 	return 0;
2445 short_read:
2446 	btrfs_err(fs_info,
2447 	"super block sys chunk array short read, cur=%u sys_array_size=%u",
2448 		  cur, sys_array_size);
2449 	return -EUCLEAN;
2450 }
2451 
2452 /*
2453  * Real super block validation
2454  * NOTE: super csum type and incompat features will not be checked here.
2455  *
2456  * @sb:		super block to check
2457  * @mirror_num:	the super block number to check its bytenr:
2458  * 		0	the primary (1st) sb
2459  * 		1, 2	2nd and 3rd backup copy
2460  * 	       -1	skip bytenr check
2461  */
2462 int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
2463 			 const struct btrfs_super_block *sb, int mirror_num)
2464 {
2465 	const u32 nodesize = btrfs_super_nodesize(sb);
2466 	const u32 sectorsize = btrfs_super_sectorsize(sb);
2467 	int ret = 0;
2468 	const bool ignore_flags = btrfs_test_opt(fs_info, IGNORESUPERFLAGS);
2469 
2470 	if (unlikely(btrfs_super_magic(sb) != BTRFS_MAGIC)) {
2471 		btrfs_err(fs_info, "no valid FS found");
2472 		ret = -EINVAL;
2473 	}
2474 	if (unlikely(btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP)) {
2475 		if (!ignore_flags) {
2476 			btrfs_err(fs_info,
2477 			"unrecognized or unsupported super flag 0x%llx",
2478 				  btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
2479 			ret = -EINVAL;
2480 		} else {
2481 			btrfs_info(fs_info,
2482 			"unrecognized or unsupported super flags: 0x%llx, ignored",
2483 				   btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
2484 		}
2485 	}
2486 	if (unlikely(btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL)) {
2487 		btrfs_err(fs_info, "tree_root level too big: %d >= %d",
2488 				btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
2489 		ret = -EINVAL;
2490 	}
2491 	if (unlikely(btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL)) {
2492 		btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
2493 				btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
2494 		ret = -EINVAL;
2495 	}
2496 	if (unlikely(btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL)) {
2497 		btrfs_err(fs_info, "log_root level too big: %d >= %d",
2498 				btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
2499 		ret = -EINVAL;
2500 	}
2501 
2502 	/*
2503 	 * Check sectorsize and nodesize first, other check will need it.
2504 	 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
2505 	 */
2506 	if (unlikely(!is_power_of_2(sectorsize) || sectorsize < BTRFS_MIN_BLOCKSIZE ||
2507 		     sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE)) {
2508 		btrfs_err(fs_info, "invalid sectorsize %u", sectorsize);
2509 		ret = -EINVAL;
2510 	}
2511 
2512 	if (unlikely(!btrfs_supported_blocksize(sectorsize))) {
2513 		btrfs_err(fs_info,
2514 			"sectorsize %u not yet supported for page size %lu",
2515 			sectorsize, PAGE_SIZE);
2516 		ret = -EINVAL;
2517 	}
2518 
2519 	if (unlikely(!is_power_of_2(nodesize) || nodesize < sectorsize ||
2520 		     nodesize > BTRFS_MAX_METADATA_BLOCKSIZE)) {
2521 		btrfs_err(fs_info, "invalid nodesize %u", nodesize);
2522 		ret = -EINVAL;
2523 	}
2524 	if (unlikely(nodesize != le32_to_cpu(sb->__unused_leafsize))) {
2525 		btrfs_err(fs_info, "invalid leafsize %u, should be %u",
2526 			  le32_to_cpu(sb->__unused_leafsize), nodesize);
2527 		ret = -EINVAL;
2528 	}
2529 
2530 	/* Root alignment check */
2531 	if (unlikely(!IS_ALIGNED(btrfs_super_root(sb), sectorsize))) {
2532 		btrfs_err(fs_info, "tree_root block unaligned: %llu",
2533 			  btrfs_super_root(sb));
2534 		ret = -EINVAL;
2535 	}
2536 	if (unlikely(!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize))) {
2537 		btrfs_err(fs_info, "chunk_root block unaligned: %llu",
2538 			   btrfs_super_chunk_root(sb));
2539 		ret = -EINVAL;
2540 	}
2541 	if (unlikely(!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize))) {
2542 		btrfs_err(fs_info, "log_root block unaligned: %llu",
2543 			  btrfs_super_log_root(sb));
2544 		ret = -EINVAL;
2545 	}
2546 
2547 	if (unlikely(!fs_info->fs_devices->temp_fsid &&
2548 		     memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0)) {
2549 		btrfs_err(fs_info,
2550 		"superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
2551 			  sb->fsid, fs_info->fs_devices->fsid);
2552 		ret = -EINVAL;
2553 	}
2554 
2555 	if (unlikely(memcmp(fs_info->fs_devices->metadata_uuid, btrfs_sb_fsid_ptr(sb),
2556 			    BTRFS_FSID_SIZE) != 0)) {
2557 		btrfs_err(fs_info,
2558 "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU",
2559 			  btrfs_sb_fsid_ptr(sb), fs_info->fs_devices->metadata_uuid);
2560 		ret = -EINVAL;
2561 	}
2562 
2563 	if (unlikely(memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid,
2564 			    BTRFS_FSID_SIZE) != 0)) {
2565 		btrfs_err(fs_info,
2566 			"dev_item UUID does not match metadata fsid: %pU != %pU",
2567 			fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid);
2568 		ret = -EINVAL;
2569 	}
2570 
2571 	/*
2572 	 * Artificial requirement for block-group-tree to force newer features
2573 	 * (free-space-tree, no-holes) so the test matrix is smaller.
2574 	 */
2575 	if (unlikely(btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
2576 		     (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) ||
2577 		      !btrfs_fs_incompat(fs_info, NO_HOLES)))) {
2578 		btrfs_err(fs_info,
2579 		"block-group-tree feature requires free-space-tree and no-holes");
2580 		ret = -EINVAL;
2581 	}
2582 
2583 	if (btrfs_fs_incompat(fs_info, REMAP_TREE)) {
2584 		/*
2585 		 * Reduce test matrix for remap tree by requiring block-group-tree
2586 		 * and no-holes. Free-space-tree is a hard requirement.
2587 		 */
2588 		if (unlikely(!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) ||
2589 			     !btrfs_fs_incompat(fs_info, NO_HOLES) ||
2590 			     !btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE))) {
2591 			btrfs_err(fs_info,
2592 "remap-tree feature requires free-space-tree, no-holes, and block-group-tree");
2593 			ret = -EINVAL;
2594 		}
2595 
2596 		if (unlikely(btrfs_fs_incompat(fs_info, MIXED_GROUPS))) {
2597 			btrfs_err(fs_info, "remap-tree not supported with mixed-bg");
2598 			ret = -EINVAL;
2599 		}
2600 
2601 		if (unlikely(btrfs_fs_incompat(fs_info, ZONED))) {
2602 			btrfs_err(fs_info, "remap-tree not supported with zoned devices");
2603 			ret = -EINVAL;
2604 		}
2605 
2606 		if (unlikely(sectorsize > PAGE_SIZE)) {
2607 			btrfs_err(fs_info, "remap-tree not supported when block size > page size");
2608 			ret = -EINVAL;
2609 		}
2610 	}
2611 
2612 	/*
2613 	 * Hint to catch really bogus numbers, bitflips or so, more exact checks are
2614 	 * done later
2615 	 */
2616 	if (unlikely(btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb))) {
2617 		btrfs_err(fs_info, "bytes_used is too small %llu",
2618 			  btrfs_super_bytes_used(sb));
2619 		ret = -EINVAL;
2620 	}
2621 	if (unlikely(!is_power_of_2(btrfs_super_stripesize(sb)))) {
2622 		btrfs_err(fs_info, "invalid stripesize %u",
2623 			  btrfs_super_stripesize(sb));
2624 		ret = -EINVAL;
2625 	}
2626 	if (unlikely(btrfs_super_num_devices(sb) > (1UL << 31)))
2627 		btrfs_warn(fs_info, "suspicious number of devices: %llu",
2628 			   btrfs_super_num_devices(sb));
2629 	if (unlikely(btrfs_super_num_devices(sb) == 0)) {
2630 		btrfs_err(fs_info, "number of devices is 0");
2631 		ret = -EINVAL;
2632 	}
2633 
2634 	if (unlikely(mirror_num >= 0 &&
2635 		     btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num))) {
2636 		btrfs_err(fs_info, "super offset mismatch %llu != %llu",
2637 			  btrfs_super_bytenr(sb), btrfs_sb_offset(mirror_num));
2638 		ret = -EINVAL;
2639 	}
2640 
2641 	if (unlikely(ret))
2642 		return ret;
2643 
2644 	ret = validate_sys_chunk_array(fs_info, sb);
2645 
2646 	/*
2647 	 * The generation is a global counter, we'll trust it more than the others
2648 	 * but it's still possible that it's the one that's wrong.
2649 	 */
2650 	if (unlikely(btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)))
2651 		btrfs_warn(fs_info,
2652 			"suspicious: generation < chunk_root_generation: %llu < %llu",
2653 			btrfs_super_generation(sb),
2654 			btrfs_super_chunk_root_generation(sb));
2655 	if (unlikely(btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) &&
2656 		     btrfs_super_cache_generation(sb) != (u64)-1))
2657 		btrfs_warn(fs_info,
2658 			"suspicious: generation < cache_generation: %llu < %llu",
2659 			btrfs_super_generation(sb),
2660 			btrfs_super_cache_generation(sb));
2661 
2662 	return ret;
2663 }
2664 
2665 /*
2666  * Validation of super block at mount time.
2667  * Some checks already done early at mount time, like csum type and incompat
2668  * flags will be skipped.
2669  */
2670 static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
2671 {
2672 	return btrfs_validate_super(fs_info, fs_info->super_copy, 0);
2673 }
2674 
2675 /*
2676  * Validation of super block at write time.
2677  * Some checks like bytenr check will be skipped as their values will be
2678  * overwritten soon.
2679  * Extra checks like csum type and incompat flags will be done here.
2680  */
2681 static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
2682 				      struct btrfs_super_block *sb)
2683 {
2684 	int ret;
2685 
2686 	ret = btrfs_validate_super(fs_info, sb, -1);
2687 	if (unlikely(ret < 0))
2688 		goto out;
2689 	if (unlikely(!btrfs_supported_super_csum(btrfs_super_csum_type(sb)))) {
2690 		ret = -EUCLEAN;
2691 		btrfs_err(fs_info, "invalid csum type, has %u want %u",
2692 			  btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
2693 		goto out;
2694 	}
2695 	if (unlikely(btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP)) {
2696 		ret = -EUCLEAN;
2697 		btrfs_err(fs_info,
2698 		"invalid incompat flags, has 0x%llx valid mask 0x%llx",
2699 			  btrfs_super_incompat_flags(sb),
2700 			  (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
2701 		goto out;
2702 	}
2703 out:
2704 	if (unlikely(ret < 0))
2705 		btrfs_err(fs_info,
2706 		"super block corruption detected before writing it to disk");
2707 	return ret;
2708 }
2709 
2710 static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level)
2711 {
2712 	struct btrfs_tree_parent_check check = {
2713 		.level = level,
2714 		.transid = gen,
2715 		.owner_root = btrfs_root_id(root)
2716 	};
2717 	int ret = 0;
2718 
2719 	root->node = read_tree_block(root->fs_info, bytenr, &check);
2720 	if (IS_ERR(root->node)) {
2721 		ret = PTR_ERR(root->node);
2722 		root->node = NULL;
2723 		return ret;
2724 	}
2725 
2726 	btrfs_set_root_node(&root->root_item, root->node);
2727 	root->commit_root = btrfs_root_node(root);
2728 	btrfs_set_root_refs(&root->root_item, 1);
2729 	return ret;
2730 }
2731 
2732 static int load_important_roots(struct btrfs_fs_info *fs_info)
2733 {
2734 	struct btrfs_super_block *sb = fs_info->super_copy;
2735 	u64 gen, bytenr;
2736 	int level, ret;
2737 
2738 	bytenr = btrfs_super_root(sb);
2739 	gen = btrfs_super_generation(sb);
2740 	level = btrfs_super_root_level(sb);
2741 	ret = load_super_root(fs_info->tree_root, bytenr, gen, level);
2742 	if (ret) {
2743 		btrfs_warn(fs_info, "couldn't read tree root");
2744 		return ret;
2745 	}
2746 
2747 	if (btrfs_fs_incompat(fs_info, REMAP_TREE)) {
2748 		bytenr = btrfs_super_remap_root(sb);
2749 		gen = btrfs_super_remap_root_generation(sb);
2750 		level = btrfs_super_remap_root_level(sb);
2751 		ret = load_super_root(fs_info->remap_root, bytenr, gen, level);
2752 		if (ret) {
2753 			btrfs_warn(fs_info, "couldn't read remap root");
2754 			return ret;
2755 		}
2756 	}
2757 
2758 	return 0;
2759 }
2760 
2761 static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
2762 {
2763 	int backup_index = find_newest_super_backup(fs_info);
2764 	struct btrfs_super_block *sb = fs_info->super_copy;
2765 	struct btrfs_root *tree_root = fs_info->tree_root;
2766 	bool use_backup = btrfs_test_opt(fs_info, USEBACKUPROOT);
2767 	int ret = 0;
2768 
2769 	if (use_backup) {
2770 		ASSERT(fs_info->use_backup_slot >= 0 &&
2771 		       fs_info->use_backup_slot < BTRFS_NUM_BACKUP_ROOTS);
2772 
2773 		ret = read_backup_root(fs_info, fs_info->use_backup_slot);
2774 		if (ret < 0) {
2775 			btrfs_err(fs_info,
2776 				  "failed to load backup roots at slot %d",
2777 				  fs_info->use_backup_slot);
2778 			return ret;
2779 		}
2780 		/*
2781 		 * Don't use the log in recovery mode, it won't be
2782 		 * valid
2783 		 */
2784 		btrfs_set_super_log_root(sb, 0);
2785 		backup_index = ret;
2786 		btrfs_warn(fs_info, "loaded backup roots at slot %d",
2787 			   fs_info->use_backup_slot);
2788 	}
2789 
2790 	ret = load_important_roots(fs_info);
2791 	if (ret)
2792 		return ret;
2793 
2794 	/*
2795 	 * No need to hold btrfs_root::objectid_mutex since the fs
2796 	 * hasn't been fully initialised and we are the only user
2797 	 */
2798 	ret = btrfs_init_root_free_objectid(tree_root);
2799 	if (ret < 0)
2800 		return ret;
2801 
2802 	ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
2803 
2804 	ret = btrfs_read_roots(fs_info);
2805 	if (ret < 0)
2806 		return ret;
2807 
2808 	/* All successful */
2809 	fs_info->generation = btrfs_header_generation(tree_root->node);
2810 	btrfs_set_last_trans_committed(fs_info, fs_info->generation);
2811 	fs_info->last_reloc_trans = 0;
2812 
2813 	/* Always begin writing backup roots after the one being used */
2814 	if (backup_index < 0) {
2815 		fs_info->backup_root_index = 0;
2816 	} else {
2817 		fs_info->backup_root_index = backup_index + 1;
2818 		fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS;
2819 	}
2820 	return ret;
2821 }
2822 
2823 /*
2824  * Lockdep gets confused between our buffer_tree which requires IRQ locking because
2825  * we modify marks in the IRQ context, and our delayed inode xarray which doesn't
2826  * have these requirements. Use a class key so lockdep doesn't get them mixed up.
2827  */
2828 static struct lock_class_key buffer_xa_class;
2829 
2830 void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
2831 {
2832 	INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
2833 
2834 	/* Use the same flags as mapping->i_pages. */
2835 	xa_init_flags(&fs_info->buffer_tree, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
2836 	lockdep_set_class(&fs_info->buffer_tree.xa_lock, &buffer_xa_class);
2837 
2838 	INIT_LIST_HEAD(&fs_info->trans_list);
2839 	INIT_LIST_HEAD(&fs_info->dead_roots);
2840 	INIT_LIST_HEAD(&fs_info->delayed_iputs);
2841 	INIT_LIST_HEAD(&fs_info->delalloc_roots);
2842 	INIT_LIST_HEAD(&fs_info->caching_block_groups);
2843 	spin_lock_init(&fs_info->delalloc_root_lock);
2844 	spin_lock_init(&fs_info->trans_lock);
2845 	spin_lock_init(&fs_info->fs_roots_radix_lock);
2846 	spin_lock_init(&fs_info->delayed_iput_lock);
2847 	spin_lock_init(&fs_info->defrag_inodes_lock);
2848 	spin_lock_init(&fs_info->super_lock);
2849 	spin_lock_init(&fs_info->unused_bgs_lock);
2850 	spin_lock_init(&fs_info->treelog_bg_lock);
2851 	spin_lock_init(&fs_info->zone_active_bgs_lock);
2852 	spin_lock_init(&fs_info->relocation_bg_lock);
2853 	rwlock_init(&fs_info->tree_mod_log_lock);
2854 	rwlock_init(&fs_info->global_root_lock);
2855 	mutex_init(&fs_info->unused_bg_unpin_mutex);
2856 	mutex_init(&fs_info->reclaim_bgs_lock);
2857 	mutex_init(&fs_info->reloc_mutex);
2858 	spin_lock_init(&fs_info->reloc_ctl_lock);
2859 	mutex_init(&fs_info->delalloc_root_mutex);
2860 	mutex_init(&fs_info->zoned_meta_io_lock);
2861 	mutex_init(&fs_info->zoned_data_reloc_io_lock);
2862 	seqlock_init(&fs_info->profiles_lock);
2863 
2864 	btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers);
2865 	btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters);
2866 	btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered);
2867 	btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent);
2868 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_prep,
2869 				     BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
2870 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked,
2871 				     BTRFS_LOCKDEP_TRANS_UNBLOCKED);
2872 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_super_committed,
2873 				     BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
2874 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_completed,
2875 				     BTRFS_LOCKDEP_TRANS_COMPLETED);
2876 
2877 	INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
2878 	INIT_LIST_HEAD(&fs_info->space_info);
2879 	INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
2880 	INIT_LIST_HEAD(&fs_info->unused_bgs);
2881 	INIT_LIST_HEAD(&fs_info->reclaim_bgs);
2882 	INIT_LIST_HEAD(&fs_info->fully_remapped_bgs);
2883 	INIT_LIST_HEAD(&fs_info->zone_active_bgs);
2884 #ifdef CONFIG_BTRFS_DEBUG
2885 	INIT_LIST_HEAD(&fs_info->allocated_roots);
2886 	INIT_LIST_HEAD(&fs_info->allocated_ebs);
2887 	spin_lock_init(&fs_info->eb_leak_lock);
2888 #endif
2889 	fs_info->mapping_tree = RB_ROOT_CACHED;
2890 	rwlock_init(&fs_info->mapping_tree_lock);
2891 	btrfs_init_block_rsv(&fs_info->global_block_rsv,
2892 			     BTRFS_BLOCK_RSV_GLOBAL);
2893 	btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
2894 	btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
2895 	btrfs_init_block_rsv(&fs_info->remap_block_rsv, BTRFS_BLOCK_RSV_REMAP);
2896 	btrfs_init_block_rsv(&fs_info->treelog_rsv, BTRFS_BLOCK_RSV_TREELOG);
2897 	btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
2898 	btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
2899 			     BTRFS_BLOCK_RSV_DELOPS);
2900 	btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
2901 			     BTRFS_BLOCK_RSV_DELREFS);
2902 
2903 	atomic_set(&fs_info->async_delalloc_pages, 0);
2904 	atomic_set(&fs_info->defrag_running, 0);
2905 	atomic_set(&fs_info->nr_delayed_iputs, 0);
2906 	atomic64_set(&fs_info->tree_mod_seq, 0);
2907 	fs_info->global_root_tree = RB_ROOT;
2908 	fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
2909 	fs_info->metadata_ratio = 0;
2910 	fs_info->defrag_inodes = RB_ROOT;
2911 	atomic64_set(&fs_info->free_chunk_space, 0);
2912 	fs_info->tree_mod_log = RB_ROOT;
2913 	fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
2914 	btrfs_init_ref_verify(fs_info);
2915 
2916 	fs_info->thread_pool_size = min_t(unsigned long,
2917 					  num_online_cpus() + 2, 8);
2918 
2919 	INIT_LIST_HEAD(&fs_info->ordered_roots);
2920 	spin_lock_init(&fs_info->ordered_root_lock);
2921 
2922 	btrfs_init_scrub(fs_info);
2923 	btrfs_init_balance(fs_info);
2924 	btrfs_init_async_reclaim_work(fs_info);
2925 	btrfs_init_extent_map_shrinker_work(fs_info);
2926 
2927 	rwlock_init(&fs_info->block_group_cache_lock);
2928 	fs_info->block_group_cache_tree = RB_ROOT_CACHED;
2929 
2930 	btrfs_extent_io_tree_init(fs_info, &fs_info->excluded_extents,
2931 				  IO_TREE_FS_EXCLUDED_EXTENTS);
2932 
2933 	mutex_init(&fs_info->ordered_operations_mutex);
2934 	mutex_init(&fs_info->tree_log_mutex);
2935 	mutex_init(&fs_info->chunk_mutex);
2936 	mutex_init(&fs_info->transaction_kthread_mutex);
2937 	mutex_init(&fs_info->cleaner_mutex);
2938 	mutex_init(&fs_info->remap_mutex);
2939 	mutex_init(&fs_info->ro_block_group_mutex);
2940 	init_rwsem(&fs_info->commit_root_sem);
2941 	init_rwsem(&fs_info->cleanup_work_sem);
2942 	init_rwsem(&fs_info->subvol_sem);
2943 	sema_init(&fs_info->uuid_tree_rescan_sem, 1);
2944 
2945 	btrfs_init_dev_replace_locks(fs_info);
2946 	btrfs_init_qgroup(fs_info);
2947 	btrfs_discard_init(fs_info);
2948 
2949 	btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2950 	btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2951 
2952 	init_waitqueue_head(&fs_info->transaction_throttle);
2953 	init_waitqueue_head(&fs_info->transaction_wait);
2954 	init_waitqueue_head(&fs_info->transaction_blocked_wait);
2955 	init_waitqueue_head(&fs_info->async_submit_wait);
2956 	init_waitqueue_head(&fs_info->delayed_iputs_wait);
2957 
2958 	fs_info->use_backup_slot = -1;
2959 	/* Usable values until the real ones are cached from the superblock */
2960 	fs_info->nodesize = 4096;
2961 	fs_info->sectorsize = 4096;
2962 	fs_info->sectorsize_bits = ilog2(4096);
2963 
2964 	/* Default compress algorithm when user does -o compress */
2965 	fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2966 
2967 	fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;
2968 
2969 	spin_lock_init(&fs_info->swapfile_pins_lock);
2970 	fs_info->swapfile_pins = RB_ROOT;
2971 
2972 	fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH;
2973 	INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work);
2974 }
2975 
2976 static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
2977 {
2978 	int ret;
2979 
2980 	fs_info->sb = sb;
2981 	/* Temporary fixed values for block size until we read the superblock. */
2982 	sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
2983 	sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
2984 
2985 	ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL);
2986 	if (ret)
2987 		return ret;
2988 
2989 	ret = percpu_counter_init(&fs_info->evictable_extent_maps, 0, GFP_KERNEL);
2990 	if (ret)
2991 		return ret;
2992 
2993 	ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
2994 	if (ret)
2995 		return ret;
2996 
2997 	ret = percpu_counter_init(&fs_info->stats_read_blocks, 0, GFP_KERNEL);
2998 	if (ret)
2999 		return ret;
3000 
3001 	fs_info->dirty_metadata_batch = PAGE_SIZE *
3002 					(1 + ilog2(nr_cpu_ids));
3003 
3004 	ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
3005 	if (ret)
3006 		return ret;
3007 
3008 	ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0,
3009 			GFP_KERNEL);
3010 	if (ret)
3011 		return ret;
3012 
3013 	btrfs_init_delayed_root(&fs_info->delayed_root);
3014 
3015 	if (sb_rdonly(sb))
3016 		set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
3017 	if (btrfs_test_opt(fs_info, IGNOREMETACSUMS))
3018 		set_bit(BTRFS_FS_STATE_SKIP_META_CSUMS, &fs_info->fs_state);
3019 
3020 	return btrfs_alloc_stripe_hash_table(fs_info);
3021 }
3022 
3023 static int btrfs_uuid_rescan_kthread(void *data)
3024 {
3025 	struct btrfs_fs_info *fs_info = data;
3026 	int ret;
3027 
3028 	/*
3029 	 * 1st step is to iterate through the existing UUID tree and
3030 	 * to delete all entries that contain outdated data.
3031 	 * 2nd step is to add all missing entries to the UUID tree.
3032 	 */
3033 	ret = btrfs_uuid_tree_iterate(fs_info);
3034 	if (ret < 0) {
3035 		if (ret != -EINTR)
3036 			btrfs_warn(fs_info, "iterating uuid_tree failed %pe",
3037 				   ERR_PTR(ret));
3038 		up(&fs_info->uuid_tree_rescan_sem);
3039 		return ret;
3040 	}
3041 	return btrfs_uuid_scan_kthread(data);
3042 }
3043 
3044 static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3045 {
3046 	struct task_struct *task;
3047 
3048 	down(&fs_info->uuid_tree_rescan_sem);
3049 	task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3050 	if (IS_ERR(task)) {
3051 		/* fs_info->update_uuid_tree_gen remains 0 in all error case */
3052 		up(&fs_info->uuid_tree_rescan_sem);
3053 		return PTR_ERR(task);
3054 	}
3055 
3056 	return 0;
3057 }
3058 
3059 static int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
3060 {
3061 	u64 root_objectid = 0;
3062 	struct btrfs_root *gang[8];
3063 	int ret = 0;
3064 
3065 	while (1) {
3066 		unsigned int found;
3067 
3068 		spin_lock(&fs_info->fs_roots_radix_lock);
3069 		found = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
3070 					     (void **)gang, root_objectid,
3071 					     ARRAY_SIZE(gang));
3072 		if (!found) {
3073 			spin_unlock(&fs_info->fs_roots_radix_lock);
3074 			break;
3075 		}
3076 		root_objectid = btrfs_root_id(gang[found - 1]) + 1;
3077 
3078 		for (int i = 0; i < found; i++) {
3079 			/* Avoid to grab roots in dead_roots. */
3080 			if (btrfs_root_refs(&gang[i]->root_item) == 0) {
3081 				gang[i] = NULL;
3082 				continue;
3083 			}
3084 			/* Grab all the search result for later use. */
3085 			gang[i] = btrfs_grab_root(gang[i]);
3086 		}
3087 		spin_unlock(&fs_info->fs_roots_radix_lock);
3088 
3089 		for (int i = 0; i < found; i++) {
3090 			if (!gang[i])
3091 				continue;
3092 			root_objectid = btrfs_root_id(gang[i]);
3093 			/*
3094 			 * Continue to release the remaining roots after the first
3095 			 * error without cleanup and preserve the first error
3096 			 * for the return.
3097 			 */
3098 			if (!ret)
3099 				ret = btrfs_orphan_cleanup(gang[i]);
3100 			btrfs_put_root(gang[i]);
3101 		}
3102 		if (ret)
3103 			break;
3104 
3105 		root_objectid++;
3106 	}
3107 	return ret;
3108 }
3109 
3110 /*
3111  * Mounting logic specific to read-write file systems. Shared by open_ctree
3112  * and btrfs_remount when remounting from read-only to read-write.
3113  */
3114 int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
3115 {
3116 	int ret;
3117 	bool rebuild_free_space_tree = false;
3118 
3119 	if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
3120 	    btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
3121 		if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
3122 			btrfs_warn(fs_info,
3123 				   "'clear_cache' option is ignored with extent tree v2");
3124 		else if (btrfs_fs_incompat(fs_info, REMAP_TREE))
3125 			btrfs_warn(fs_info, "'clear_cache' option is ignored with remap tree");
3126 		else
3127 			rebuild_free_space_tree = true;
3128 	} else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
3129 		   !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
3130 		btrfs_warn(fs_info, "free space tree is invalid");
3131 		rebuild_free_space_tree = true;
3132 	}
3133 
3134 	if (rebuild_free_space_tree) {
3135 		btrfs_info(fs_info, "rebuilding free space tree");
3136 		ret = btrfs_rebuild_free_space_tree(fs_info);
3137 		if (ret) {
3138 			btrfs_warn(fs_info,
3139 				   "failed to rebuild free space tree: %pe", ERR_PTR(ret));
3140 			return ret;
3141 		}
3142 	}
3143 
3144 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
3145 	    !btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
3146 		btrfs_info(fs_info, "disabling free space tree");
3147 		ret = btrfs_delete_free_space_tree(fs_info);
3148 		if (ret) {
3149 			btrfs_warn(fs_info,
3150 				   "failed to disable free space tree: %pe", ERR_PTR(ret));
3151 			return ret;
3152 		}
3153 	}
3154 
3155 	/*
3156 	 * Before btrfs-progs v6.16.1 mkfs.btrfs can leave free space entries
3157 	 * for deleted temporary chunks. Delete them if they exist.
3158 	 */
3159 	ret = btrfs_delete_orphan_free_space_entries(fs_info);
3160 	if (ret < 0) {
3161 		btrfs_err(fs_info, "failed to delete orphan free space tree entries: %pe",
3162 			  ERR_PTR(ret));
3163 		return ret;
3164 	}
3165 	/*
3166 	 * btrfs_find_orphan_roots() is responsible for finding all the dead
3167 	 * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load
3168 	 * them into the fs_info->fs_roots_radix tree. This must be done before
3169 	 * calling btrfs_orphan_cleanup() on the tree root. If we don't do it
3170 	 * first, then btrfs_orphan_cleanup() will delete a dead root's orphan
3171 	 * item before the root's tree is deleted - this means that if we unmount
3172 	 * or crash before the deletion completes, on the next mount we will not
3173 	 * delete what remains of the tree because the orphan item does not
3174 	 * exists anymore, which is what tells us we have a pending deletion.
3175 	 */
3176 	ret = btrfs_find_orphan_roots(fs_info);
3177 	if (ret)
3178 		return ret;
3179 
3180 	ret = btrfs_cleanup_fs_roots(fs_info);
3181 	if (ret)
3182 		return ret;
3183 
3184 	down_read(&fs_info->cleanup_work_sem);
3185 	if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
3186 	    (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
3187 		up_read(&fs_info->cleanup_work_sem);
3188 		return ret;
3189 	}
3190 	up_read(&fs_info->cleanup_work_sem);
3191 
3192 	mutex_lock(&fs_info->cleaner_mutex);
3193 	ret = btrfs_recover_relocation(fs_info);
3194 	mutex_unlock(&fs_info->cleaner_mutex);
3195 	if (ret < 0) {
3196 		btrfs_warn(fs_info, "failed to recover relocation: %pe", ERR_PTR(ret));
3197 		return ret;
3198 	}
3199 
3200 	if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
3201 	    !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
3202 		btrfs_info(fs_info, "creating free space tree");
3203 		ret = btrfs_create_free_space_tree(fs_info);
3204 		if (ret) {
3205 			btrfs_warn(fs_info,
3206 				"failed to create free space tree: %pe", ERR_PTR(ret));
3207 			return ret;
3208 		}
3209 	}
3210 
3211 	if (btrfs_free_space_cache_v1_active(fs_info)) {
3212 		ret = btrfs_cleanup_free_space_cache_v1(fs_info);
3213 		if (ret)
3214 			return ret;
3215 	}
3216 
3217 	ret = btrfs_resume_balance_async(fs_info);
3218 	if (ret)
3219 		return ret;
3220 
3221 	ret = btrfs_resume_dev_replace_async(fs_info);
3222 	if (ret) {
3223 		btrfs_warn(fs_info, "failed to resume dev_replace");
3224 		return ret;
3225 	}
3226 
3227 	btrfs_qgroup_rescan_resume(fs_info);
3228 
3229 	if (!fs_info->uuid_root) {
3230 		btrfs_info(fs_info, "creating UUID tree");
3231 		ret = btrfs_create_uuid_tree(fs_info);
3232 		if (ret) {
3233 			btrfs_warn(fs_info,
3234 				   "failed to create the UUID tree %pe", ERR_PTR(ret));
3235 			return ret;
3236 		}
3237 	}
3238 
3239 	return 0;
3240 }
3241 
3242 /*
3243  * Do various sanity and dependency checks of different features.
3244  *
3245  * @is_rw_mount:	If the mount is read-write.
3246  *
3247  * This is the place for less strict checks (like for subpage or artificial
3248  * feature dependencies).
3249  *
3250  * For strict checks or possible corruption detection, see
3251  * btrfs_validate_super().
3252  *
3253  * This should be called after btrfs_parse_options(), as some mount options
3254  * (space cache related) can modify on-disk format like free space tree and
3255  * screw up certain feature dependencies.
3256  */
3257 int btrfs_check_features(struct btrfs_fs_info *fs_info, bool is_rw_mount)
3258 {
3259 	struct btrfs_super_block *disk_super = fs_info->super_copy;
3260 	u64 incompat = btrfs_super_incompat_flags(disk_super);
3261 	const u64 compat_ro = btrfs_super_compat_ro_flags(disk_super);
3262 	const u64 compat_ro_unsupp = (compat_ro & ~BTRFS_FEATURE_COMPAT_RO_SUPP);
3263 
3264 	if (incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
3265 		btrfs_err(fs_info,
3266 		"cannot mount because of unknown incompat features (0x%llx)",
3267 		    incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP);
3268 		return -EINVAL;
3269 	}
3270 
3271 	/* Runtime limitation for mixed block groups. */
3272 	if ((incompat & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
3273 	    (fs_info->sectorsize != fs_info->nodesize)) {
3274 		btrfs_err(fs_info,
3275 "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
3276 			fs_info->nodesize, fs_info->sectorsize);
3277 		return -EINVAL;
3278 	}
3279 
3280 	/* Mixed backref is an always-enabled feature. */
3281 	incompat |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
3282 
3283 	/* Set compression related flags just in case. */
3284 	if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
3285 		incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
3286 	else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
3287 		incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
3288 
3289 	/*
3290 	 * An ancient flag, which should really be marked deprecated.
3291 	 * Such runtime limitation doesn't really need a incompat flag.
3292 	 */
3293 	if (btrfs_super_nodesize(disk_super) > PAGE_SIZE)
3294 		incompat |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
3295 
3296 	if (compat_ro_unsupp && is_rw_mount) {
3297 		btrfs_err(fs_info,
3298 	"cannot mount read-write because of unknown compat_ro features (0x%llx)",
3299 		       compat_ro_unsupp);
3300 		return -EINVAL;
3301 	}
3302 
3303 	/*
3304 	 * We have unsupported RO compat features, although RO mounted, we
3305 	 * should not cause any metadata writes, including log replay.
3306 	 * Or we could screw up whatever the new feature requires.
3307 	 */
3308 	if (compat_ro_unsupp && btrfs_super_log_root(disk_super) &&
3309 	    !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3310 		btrfs_err(fs_info,
3311 "cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay",
3312 			  compat_ro_unsupp);
3313 		return -EINVAL;
3314 	}
3315 
3316 	/*
3317 	 * Artificial limitations for block group tree, to force
3318 	 * block-group-tree to rely on no-holes and free-space-tree.
3319 	 */
3320 	if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
3321 	    (!btrfs_fs_incompat(fs_info, NO_HOLES) ||
3322 	     !btrfs_test_opt(fs_info, FREE_SPACE_TREE))) {
3323 		btrfs_err(fs_info,
3324 "block-group-tree feature requires no-holes and free-space-tree features");
3325 		return -EINVAL;
3326 	}
3327 
3328 	/* This can be called by remount, we need to protect the super block. */
3329 	spin_lock(&fs_info->super_lock);
3330 	btrfs_set_super_incompat_flags(disk_super, incompat);
3331 	spin_unlock(&fs_info->super_lock);
3332 
3333 	return 0;
3334 }
3335 
3336 /*
3337  * Try to wait for any metadata readahead, and invalidate all btree folios.
3338  *
3339  * If the invalidation failed, report any dirty/held extent buffers.
3340  */
3341 static void invalidate_and_check_btree_folios(struct btrfs_fs_info *fs_info)
3342 {
3343 	unsigned long index = 0;
3344 	struct extent_buffer *eb;
3345 	int ret;
3346 
3347 	ret = invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
3348 	if (likely(ret == 0))
3349 		return;
3350 
3351 	/*
3352 	 * Some btree pages can not be invalidated, this happens when some tree
3353 	 * blocks are still held (either by readahead or some task is holding a ref).
3354 	 */
3355 	rcu_read_lock();
3356 	xa_for_each(&fs_info->buffer_tree, index, eb) {
3357 		unsigned int refs;
3358 
3359 		/* Increase the ref so that the eb won't disappear. */
3360 		if (!refcount_inc_not_zero(&eb->refs))
3361 			continue;
3362 		rcu_read_unlock();
3363 
3364 		/* Wait for any readahead first. */
3365 		if (test_bit(EXTENT_BUFFER_READING, &eb->bflags))
3366 			wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING,
3367 				       TASK_UNINTERRUPTIBLE);
3368 		/*
3369 		 * We hold the spinlock to make sure above
3370 		 * EXTENT_BUFFER_READING flag is cleared with the held
3371 		 * ref dropped.
3372 		 * Or we can hit a race window and lead to false alerts.
3373 		 */
3374 		spin_lock(&eb->refs_lock);
3375 		refs = refcount_read(&eb->refs);
3376 		spin_unlock(&eb->refs_lock);
3377 
3378 		/*
3379 		 * The refs threshold is 2, one held by us at the beginning
3380 		 * of the loop, one for the ownership in the buffer tree.
3381 		 */
3382 		if (unlikely(refs > 2 || extent_buffer_under_io(eb))) {
3383 			WARN_ON_ONCE(IS_ENABLED(CONFIG_BTRFS_DEBUG));
3384 			btrfs_warn(fs_info,
3385 			"unable to release extent buffer %llu owner %llu gen %llu refs %u flags 0x%lx",
3386 				   eb->start, btrfs_header_owner(eb),
3387 				   btrfs_header_generation(eb),
3388 				   refs, eb->bflags);
3389 		}
3390 		free_extent_buffer(eb);
3391 		rcu_read_lock();
3392 	}
3393 	rcu_read_unlock();
3394 	invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
3395 }
3396 
3397 static u32 calc_block_max_order(u32 sectorsize_bits)
3398 {
3399 	u32 max_size;
3400 
3401 	max_size = min(BTRFS_MAX_BLOCKS_PER_FOLIO << sectorsize_bits,
3402 		       BTRFS_MAX_FOLIO_SIZE);
3403 	return ilog2(round_up(max_size, PAGE_SIZE) >> PAGE_SHIFT);
3404 }
3405 
3406 int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices)
3407 {
3408 	u32 sectorsize;
3409 	u32 nodesize;
3410 	u64 generation;
3411 	u16 csum_type;
3412 	struct btrfs_super_block *disk_super;
3413 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
3414 	struct btrfs_root *tree_root;
3415 	struct btrfs_root *chunk_root;
3416 	struct btrfs_root *remap_root;
3417 	int ret;
3418 	int level;
3419 
3420 	ret = init_mount_fs_info(fs_info, sb);
3421 	if (ret)
3422 		goto fail;
3423 
3424 	/* These need to be init'ed before we start creating inodes and such. */
3425 	tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID,
3426 				     GFP_KERNEL);
3427 	fs_info->tree_root = tree_root;
3428 	chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID,
3429 				      GFP_KERNEL);
3430 	fs_info->chunk_root = chunk_root;
3431 	if (!tree_root || !chunk_root) {
3432 		ret = -ENOMEM;
3433 		goto fail;
3434 	}
3435 
3436 	ret = btrfs_init_btree_inode(sb);
3437 	if (ret)
3438 		goto fail;
3439 
3440 	invalidate_bdev(fs_devices->latest_dev->bdev);
3441 
3442 	/*
3443 	 * Read super block and check the signature bytes only
3444 	 */
3445 	disk_super = btrfs_read_disk_super(fs_devices->latest_dev->bdev, 0, false);
3446 	if (IS_ERR(disk_super)) {
3447 		ret = PTR_ERR(disk_super);
3448 		goto fail_alloc;
3449 	}
3450 
3451 	btrfs_info(fs_info, "first mount of filesystem %pU", disk_super->fsid);
3452 	/*
3453 	 * Verify the type first, if that or the checksum value are
3454 	 * corrupted, we'll find out
3455 	 */
3456 	csum_type = btrfs_super_csum_type(disk_super);
3457 	if (!btrfs_supported_super_csum(csum_type)) {
3458 		btrfs_err(fs_info, "unsupported checksum algorithm: %u",
3459 			  csum_type);
3460 		ret = -EINVAL;
3461 		btrfs_release_disk_super(disk_super);
3462 		goto fail_alloc;
3463 	}
3464 
3465 	fs_info->csum_size = btrfs_super_csum_size(disk_super);
3466 	fs_info->csum_type = csum_type;
3467 
3468 	btrfs_init_csum_hash(fs_info, csum_type);
3469 
3470 	/*
3471 	 * We want to check superblock checksum, the type is stored inside.
3472 	 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
3473 	 */
3474 	if (btrfs_check_super_csum(fs_info, disk_super)) {
3475 		btrfs_err(fs_info, "superblock checksum mismatch");
3476 		ret = -EINVAL;
3477 		btrfs_release_disk_super(disk_super);
3478 		goto fail_alloc;
3479 	}
3480 
3481 	/*
3482 	 * super_copy is zeroed at allocation time and we never touch the
3483 	 * following bytes up to INFO_SIZE, the checksum is calculated from
3484 	 * the whole block of INFO_SIZE
3485 	 */
3486 	memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy));
3487 	btrfs_release_disk_super(disk_super);
3488 
3489 	disk_super = fs_info->super_copy;
3490 
3491 	memcpy(fs_info->super_for_commit, fs_info->super_copy,
3492 	       sizeof(*fs_info->super_for_commit));
3493 
3494 	ret = btrfs_validate_mount_super(fs_info);
3495 	if (ret) {
3496 		btrfs_err(fs_info, "superblock contains fatal errors");
3497 		ret = -EINVAL;
3498 		goto fail_alloc;
3499 	}
3500 
3501 	if (!btrfs_super_root(disk_super)) {
3502 		btrfs_err(fs_info, "invalid superblock tree root bytenr");
3503 		ret = -EINVAL;
3504 		goto fail_alloc;
3505 	}
3506 
3507 	/* check FS state, whether FS is broken. */
3508 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
3509 		WRITE_ONCE(fs_info->fs_error, -EUCLEAN);
3510 
3511 	/* If the fs has any rescue options, no transaction is allowed. */
3512 	if (btrfs_is_full_ro(fs_info))
3513 		WRITE_ONCE(fs_info->fs_error, -EROFS);
3514 
3515 	/* Set up fs_info before parsing mount options */
3516 	nodesize = btrfs_super_nodesize(disk_super);
3517 	sectorsize = btrfs_super_sectorsize(disk_super);
3518 	fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
3519 	fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
3520 
3521 	fs_info->nodesize = nodesize;
3522 	fs_info->nodesize_bits = ilog2(nodesize);
3523 	fs_info->sectorsize = sectorsize;
3524 	fs_info->sectorsize_bits = ilog2(sectorsize);
3525 	fs_info->block_min_order = ilog2(round_up(sectorsize, PAGE_SIZE) >> PAGE_SHIFT);
3526 	/*
3527 	 * For HIGHMEM, a large folio cannot be mapped in one go, breaking a lot
3528 	 * of basic assumptions for btrfs IOs.
3529 	 * Disable large folios for such 32-bit systems.
3530 	 */
3531 	if (IS_ENABLED(CONFIG_HIGHMEM))
3532 		fs_info->block_max_order = fs_info->block_min_order;
3533 	else
3534 		fs_info->block_max_order = calc_block_max_order(fs_info->sectorsize_bits);
3535 	fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
3536 	fs_info->fs_devices->fs_info = fs_info;
3537 
3538 	if (fs_info->sectorsize > PAGE_SIZE)
3539 		btrfs_warn(fs_info,
3540 			   "support for block size %u with page size %lu is experimental, some features may be missing",
3541 			   fs_info->sectorsize, PAGE_SIZE);
3542 	/*
3543 	 * Handle the space caching options appropriately now that we have the
3544 	 * super block loaded and validated.
3545 	 */
3546 	btrfs_set_free_space_cache_settings(fs_info);
3547 
3548 	if (!btrfs_check_options(fs_info, &fs_info->mount_opt, sb->s_flags)) {
3549 		ret = -EINVAL;
3550 		goto fail_alloc;
3551 	}
3552 
3553 	ret = btrfs_check_features(fs_info, !sb_rdonly(sb));
3554 	if (ret < 0)
3555 		goto fail_alloc;
3556 
3557 	if (btrfs_super_incompat_flags(disk_super) & BTRFS_FEATURE_INCOMPAT_REMAP_TREE) {
3558 		remap_root = btrfs_alloc_root(fs_info, BTRFS_REMAP_TREE_OBJECTID,
3559 					      GFP_KERNEL);
3560 		fs_info->remap_root = remap_root;
3561 		if (!remap_root) {
3562 			ret = -ENOMEM;
3563 			goto fail_alloc;
3564 		}
3565 	}
3566 
3567 	/*
3568 	 * At this point our mount options are validated, if we set ->max_inline
3569 	 * to something non-standard make sure we truncate it to sectorsize.
3570 	 */
3571 	fs_info->max_inline = min_t(u64, fs_info->max_inline, fs_info->sectorsize);
3572 
3573 	ret = btrfs_alloc_compress_wsm(fs_info);
3574 	if (ret)
3575 		goto fail_sb_buffer;
3576 	ret = btrfs_init_workqueues(fs_info);
3577 	if (ret)
3578 		goto fail_sb_buffer;
3579 
3580 	sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
3581 	sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
3582 
3583 	/* Update the values for the current filesystem. */
3584 	sb->s_blocksize = sectorsize;
3585 	sb->s_blocksize_bits = blksize_bits(sectorsize);
3586 	/*
3587 	 * When temp_fsid is active, fs_devices->fsid is assigned a random UUID
3588 	 * at mount. This inconsistent UUID causes issues for layered filesystems
3589 	 * like OverlayFS. Since metadata_uuid may or may not be set, provide the
3590 	 * on-disk UUID directly from the super_copy.
3591 	 */
3592 	if (fs_info->fs_devices->temp_fsid)
3593 		memcpy(&sb->s_uuid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
3594 	else
3595 		memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE);
3596 
3597 	mutex_lock(&fs_info->chunk_mutex);
3598 	ret = btrfs_read_sys_array(fs_info);
3599 	mutex_unlock(&fs_info->chunk_mutex);
3600 	if (ret) {
3601 		btrfs_err(fs_info, "failed to read the system array: %pe", ERR_PTR(ret));
3602 		goto fail_sb_buffer;
3603 	}
3604 
3605 	generation = btrfs_super_chunk_root_generation(disk_super);
3606 	level = btrfs_super_chunk_root_level(disk_super);
3607 	ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super),
3608 			      generation, level);
3609 	if (ret) {
3610 		btrfs_err(fs_info, "failed to read chunk root");
3611 		goto fail_tree_roots;
3612 	}
3613 
3614 	read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
3615 			   offsetof(struct btrfs_header, chunk_tree_uuid),
3616 			   BTRFS_UUID_SIZE);
3617 
3618 	ret = btrfs_read_chunk_tree(fs_info);
3619 	if (ret) {
3620 		btrfs_err(fs_info, "failed to read chunk tree: %pe", ERR_PTR(ret));
3621 		goto fail_tree_roots;
3622 	}
3623 
3624 	/*
3625 	 * At this point we know all the devices that make this filesystem,
3626 	 * including the seed devices but we don't know yet if the replace
3627 	 * target is required. So free devices that are not part of this
3628 	 * filesystem but skip the replace target device which is checked
3629 	 * below in btrfs_init_dev_replace().
3630 	 */
3631 	btrfs_free_extra_devids(fs_devices);
3632 	if (unlikely(!fs_devices->latest_dev->bdev)) {
3633 		btrfs_err(fs_info, "failed to read devices");
3634 		ret = -EIO;
3635 		goto fail_tree_roots;
3636 	}
3637 
3638 	ret = init_tree_roots(fs_info);
3639 	if (ret)
3640 		goto fail_tree_roots;
3641 
3642 	/*
3643 	 * Get zone type information of zoned block devices. This will also
3644 	 * handle emulation of a zoned filesystem if a regular device has the
3645 	 * zoned incompat feature flag set.
3646 	 */
3647 	ret = btrfs_get_dev_zone_info_all_devices(fs_info);
3648 	if (ret) {
3649 		btrfs_err(fs_info,
3650 			  "zoned: failed to read device zone info: %pe", ERR_PTR(ret));
3651 		goto fail_block_groups;
3652 	}
3653 
3654 	/*
3655 	 * If we have a uuid root and we're not being told to rescan we need to
3656 	 * check the generation here so we can set the
3657 	 * BTRFS_FS_UPDATE_UUID_TREE_GEN bit.  Otherwise we could commit the
3658 	 * transaction during a balance or the log replay without updating the
3659 	 * uuid generation, and then if we crash we would rescan the uuid tree,
3660 	 * even though it was perfectly fine.
3661 	 */
3662 	if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
3663 	    fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
3664 		set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
3665 
3666 	if (unlikely(btrfs_verify_dev_items(fs_info))) {
3667 		ret = -EUCLEAN;
3668 		goto fail_block_groups;
3669 	}
3670 	ret = btrfs_verify_dev_extents(fs_info);
3671 	if (ret) {
3672 		btrfs_err(fs_info,
3673 			  "failed to verify dev extents against chunks: %pe",
3674 			  ERR_PTR(ret));
3675 		goto fail_block_groups;
3676 	}
3677 	ret = btrfs_recover_balance(fs_info);
3678 	if (ret) {
3679 		btrfs_err(fs_info, "failed to recover balance: %pe", ERR_PTR(ret));
3680 		goto fail_block_groups;
3681 	}
3682 
3683 	ret = btrfs_init_dev_stats(fs_info);
3684 	if (ret) {
3685 		btrfs_err(fs_info, "failed to init dev_stats: %pe", ERR_PTR(ret));
3686 		goto fail_block_groups;
3687 	}
3688 
3689 	ret = btrfs_init_dev_replace(fs_info);
3690 	if (ret) {
3691 		btrfs_err(fs_info, "failed to init dev_replace: %pe", ERR_PTR(ret));
3692 		goto fail_block_groups;
3693 	}
3694 
3695 	ret = btrfs_check_zoned_mode(fs_info);
3696 	if (ret) {
3697 		btrfs_err(fs_info, "failed to initialize zoned mode: %pe",
3698 			  ERR_PTR(ret));
3699 		goto fail_block_groups;
3700 	}
3701 
3702 	ret = btrfs_sysfs_add_fsid(fs_devices);
3703 	if (ret) {
3704 		btrfs_err(fs_info, "failed to init sysfs fsid interface: %pe",
3705 			  ERR_PTR(ret));
3706 		goto fail_block_groups;
3707 	}
3708 
3709 	ret = btrfs_sysfs_add_mounted(fs_info);
3710 	if (ret) {
3711 		btrfs_err(fs_info, "failed to init sysfs interface: %pe", ERR_PTR(ret));
3712 		goto fail_fsdev_sysfs;
3713 	}
3714 
3715 	ret = btrfs_init_space_info(fs_info);
3716 	if (ret) {
3717 		btrfs_err(fs_info, "failed to initialize space info: %pe", ERR_PTR(ret));
3718 		goto fail_sysfs;
3719 	}
3720 
3721 	ret = btrfs_read_block_groups(fs_info);
3722 	if (ret) {
3723 		btrfs_err(fs_info, "failed to read block groups: %pe", ERR_PTR(ret));
3724 		goto fail_sysfs;
3725 	}
3726 
3727 	if (btrfs_fs_incompat(fs_info, REMAP_TREE)) {
3728 		ret = btrfs_populate_fully_remapped_bgs_list(fs_info);
3729 		if (ret) {
3730 			btrfs_err(fs_info, "failed to populate fully_remapped_bgs list: %pe",
3731 				  ERR_PTR(ret));
3732 			goto fail_sysfs;
3733 		}
3734 	}
3735 
3736 	ret = btrfs_init_writeback_bio_size(fs_info);
3737 	if (ret) {
3738 		btrfs_err(fs_info, "failed to get optimum writeback size: %pe",
3739 			  ERR_PTR(ret));
3740 		goto fail_sysfs;
3741 	}
3742 
3743 	btrfs_free_zone_cache(fs_info);
3744 
3745 	btrfs_check_active_zone_reservation(fs_info);
3746 
3747 	if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices &&
3748 	    !btrfs_check_rw_degradable(fs_info, NULL)) {
3749 		btrfs_warn(fs_info,
3750 		"writable mount is not allowed due to too many missing devices");
3751 		ret = -EINVAL;
3752 		goto fail_sysfs;
3753 	}
3754 
3755 	fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
3756 					       "btrfs-cleaner");
3757 	if (IS_ERR(fs_info->cleaner_kthread)) {
3758 		ret = PTR_ERR(fs_info->cleaner_kthread);
3759 		goto fail_sysfs;
3760 	}
3761 
3762 	fs_info->transaction_kthread = kthread_run(transaction_kthread,
3763 						   tree_root,
3764 						   "btrfs-transaction");
3765 	if (IS_ERR(fs_info->transaction_kthread)) {
3766 		ret = PTR_ERR(fs_info->transaction_kthread);
3767 		goto fail_cleaner;
3768 	}
3769 
3770 	/*
3771 	 * Starts a transaction, must be called after the transaction kthread
3772 	 * is initialized.
3773 	 */
3774 	btrfs_zoned_reserve_data_reloc_bg(fs_info);
3775 
3776 	ret = btrfs_read_qgroup_config(fs_info);
3777 	if (ret)
3778 		goto fail_trans_kthread;
3779 
3780 	if (btrfs_build_ref_tree(fs_info))
3781 		btrfs_err(fs_info, "couldn't build ref tree");
3782 
3783 	/* do not make disk changes in broken FS or nologreplay is given */
3784 	if (btrfs_super_log_root(disk_super) != 0 &&
3785 	    !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3786 		btrfs_info(fs_info, "start tree-log replay");
3787 		ret = btrfs_replay_log(fs_info, fs_devices);
3788 		if (ret)
3789 			goto fail_qgroup;
3790 	}
3791 
3792 	fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
3793 	if (IS_ERR(fs_info->fs_root)) {
3794 		ret = PTR_ERR(fs_info->fs_root);
3795 		btrfs_err(fs_info, "failed to read fs tree: %pe", ERR_PTR(ret));
3796 		fs_info->fs_root = NULL;
3797 		goto fail_qgroup;
3798 	}
3799 
3800 	if (sb_rdonly(sb))
3801 		return 0;
3802 
3803 	ret = btrfs_start_pre_rw_mount(fs_info);
3804 	if (ret) {
3805 		close_ctree(fs_info);
3806 		return ret;
3807 	}
3808 	btrfs_discard_resume(fs_info);
3809 
3810 	if (fs_info->uuid_root &&
3811 	    (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
3812 	     !test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))) {
3813 		btrfs_info(fs_info, "checking UUID tree");
3814 		ret = btrfs_check_uuid_tree(fs_info);
3815 		if (ret) {
3816 			btrfs_err(fs_info, "failed to check the UUID tree: %pe", ERR_PTR(ret));
3817 			close_ctree(fs_info);
3818 			return ret;
3819 		}
3820 	}
3821 
3822 	set_bit(BTRFS_FS_OPEN, &fs_info->flags);
3823 
3824 	/* Kick the cleaner thread so it'll start deleting snapshots. */
3825 	if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
3826 		wake_up_process(fs_info->cleaner_kthread);
3827 
3828 	return 0;
3829 
3830 fail_qgroup:
3831 	btrfs_free_qgroup_config(fs_info);
3832 fail_trans_kthread:
3833 	kthread_stop(fs_info->transaction_kthread);
3834 	btrfs_cleanup_transaction(fs_info);
3835 	btrfs_free_fs_roots(fs_info);
3836 fail_cleaner:
3837 	kthread_stop(fs_info->cleaner_kthread);
3838 
3839 	/*
3840 	 * make sure we're done with the btree inode before we stop our
3841 	 * kthreads
3842 	 */
3843 	filemap_write_and_wait(fs_info->btree_inode->i_mapping);
3844 
3845 fail_sysfs:
3846 	btrfs_sysfs_remove_mounted(fs_info);
3847 
3848 fail_fsdev_sysfs:
3849 	btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3850 
3851 fail_block_groups:
3852 	btrfs_put_block_group_cache(fs_info);
3853 
3854 fail_tree_roots:
3855 	free_root_pointers(fs_info, true);
3856 	invalidate_and_check_btree_folios(fs_info);
3857 
3858 fail_sb_buffer:
3859 	btrfs_stop_all_workers(fs_info);
3860 	btrfs_free_block_groups(fs_info);
3861 fail_alloc:
3862 	btrfs_mapping_tree_free(fs_info);
3863 
3864 	iput(fs_info->btree_inode);
3865 fail:
3866 	ASSERT(ret < 0);
3867 	return ret;
3868 }
3869 ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
3870 
3871 static void btrfs_end_super_write(struct bio *bio)
3872 {
3873 	struct btrfs_device *device = bio->bi_private;
3874 	struct folio_iter fi;
3875 
3876 	bio_for_each_folio_all(fi, bio) {
3877 		if (bio->bi_status) {
3878 			btrfs_warn_rl(device->fs_info,
3879 				"lost super block write due to IO error on %s (%d)",
3880 				btrfs_dev_name(device),
3881 				blk_status_to_errno(bio->bi_status));
3882 			btrfs_dev_stat_inc_and_print(device,
3883 						     BTRFS_DEV_STAT_WRITE_ERRS);
3884 			/* Ensure failure if the primary sb fails. */
3885 			if (bio->bi_opf & REQ_FUA)
3886 				atomic_add(BTRFS_SUPER_PRIMARY_WRITE_ERROR,
3887 					   &device->sb_write_errors);
3888 			else
3889 				atomic_inc(&device->sb_write_errors);
3890 		}
3891 		folio_unlock(fi.folio);
3892 		folio_put(fi.folio);
3893 	}
3894 
3895 	bio_put(bio);
3896 }
3897 
3898 /*
3899  * Write superblock @sb to the @device. Do not wait for completion, all the
3900  * folios we use for writing are locked.
3901  *
3902  * Write @max_mirrors copies of the superblock. Note that max_mirrors must be
3903  * same for write and wait phases.
3904  *
3905  * Return number of errors when folio is not found or submission fails.
3906  */
3907 static int write_dev_supers(struct btrfs_device *device,
3908 			    struct btrfs_super_block *sb, int max_mirrors)
3909 {
3910 	struct btrfs_fs_info *fs_info = device->fs_info;
3911 	struct address_space *mapping = device->bdev->bd_mapping;
3912 	int i;
3913 	int ret;
3914 	u64 bytenr, bytenr_orig;
3915 
3916 	atomic_set(&device->sb_write_errors, 0);
3917 
3918 	for (i = 0; i < max_mirrors; i++) {
3919 		struct folio *folio;
3920 		struct bio *bio;
3921 		struct btrfs_super_block *disk_super;
3922 		size_t offset;
3923 
3924 		bytenr_orig = btrfs_sb_offset(i);
3925 		ret = btrfs_sb_log_location(device, i, WRITE, &bytenr);
3926 		if (ret == -ENOENT) {
3927 			continue;
3928 		} else if (ret < 0) {
3929 			btrfs_err(device->fs_info,
3930 			  "couldn't get super block location for mirror %d error %pe",
3931 			  i, ERR_PTR(ret));
3932 			atomic_inc(&device->sb_write_errors);
3933 			continue;
3934 		}
3935 		if (bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes)
3936 			break;
3937 
3938 		btrfs_set_super_bytenr(sb, bytenr_orig);
3939 
3940 		btrfs_csum(fs_info->csum_type, (const u8 *)sb + BTRFS_CSUM_SIZE,
3941 			   BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, sb->csum);
3942 
3943 		folio = __filemap_get_folio(mapping, bytenr >> PAGE_SHIFT,
3944 					    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
3945 					    GFP_NOFS);
3946 		if (IS_ERR(folio)) {
3947 			btrfs_err(device->fs_info,
3948 			  "couldn't get super block page for bytenr %llu error %pe",
3949 			  bytenr, folio);
3950 			atomic_inc(&device->sb_write_errors);
3951 			continue;
3952 		}
3953 
3954 		offset = offset_in_folio(folio, bytenr);
3955 		disk_super = folio_address(folio) + offset;
3956 		memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE);
3957 
3958 		/*
3959 		 * Directly use bios here instead of relying on the page cache
3960 		 * to do I/O, so we don't lose the ability to do integrity
3961 		 * checking.
3962 		 */
3963 		bio = bio_alloc(device->bdev, 1,
3964 				REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO,
3965 				GFP_NOFS);
3966 		bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT;
3967 		bio->bi_private = device;
3968 		bio->bi_end_io = btrfs_end_super_write;
3969 		bio_add_folio_nofail(bio, folio, BTRFS_SUPER_INFO_SIZE, offset);
3970 
3971 		/*
3972 		 * We FUA only the first super block.  The others we allow to
3973 		 * go down lazy and there's a short window where the on-disk
3974 		 * copies might still contain the older version.
3975 		 */
3976 		if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
3977 			bio->bi_opf |= REQ_FUA;
3978 		submit_bio(bio);
3979 
3980 		if (btrfs_advance_sb_log(device, i))
3981 			atomic_inc(&device->sb_write_errors);
3982 	}
3983 	return atomic_read(&device->sb_write_errors) < i ? 0 : -1;
3984 }
3985 
3986 /*
3987  * Wait for write completion of superblocks done by write_dev_supers,
3988  * @max_mirrors same for write and wait phases.
3989  *
3990  * Return -1 if primary super block write failed or when there were no super block
3991  * copies written. Otherwise 0.
3992  */
3993 static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
3994 {
3995 	int i;
3996 	int errors = 0;
3997 	bool primary_failed = false;
3998 	int ret;
3999 	u64 bytenr;
4000 
4001 	for (i = 0; i < max_mirrors; i++) {
4002 		struct folio *folio;
4003 
4004 		ret = btrfs_sb_log_location(device, i, READ, &bytenr);
4005 		if (ret == -ENOENT) {
4006 			break;
4007 		} else if (unlikely(ret < 0)) {
4008 			errors++;
4009 			if (i == 0)
4010 				primary_failed = true;
4011 			continue;
4012 		}
4013 		if (bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes)
4014 			break;
4015 
4016 		folio = filemap_get_folio(device->bdev->bd_mapping,
4017 					  bytenr >> PAGE_SHIFT);
4018 		/* If the folio has been removed, then we know it completed. */
4019 		if (IS_ERR(folio))
4020 			continue;
4021 
4022 		/* Folio will be unlocked once the write completes. */
4023 		folio_wait_locked(folio);
4024 		folio_put(folio);
4025 	}
4026 
4027 	errors += atomic_read(&device->sb_write_errors);
4028 
4029 	if (unlikely(primary_failed || errors >= BTRFS_SUPER_PRIMARY_WRITE_ERROR)) {
4030 		btrfs_err(device->fs_info, "error writing primary super block to device %llu",
4031 			  device->devid);
4032 		return -1;
4033 	}
4034 
4035 	return errors < i ? 0 : -1;
4036 }
4037 
4038 /*
4039  * endio for the write_dev_flush, this will wake anyone waiting
4040  * for the barrier when it is done
4041  */
4042 static void btrfs_end_empty_barrier(struct bio *bio)
4043 {
4044 	bio_uninit(bio);
4045 	complete(bio->bi_private);
4046 }
4047 
4048 /*
4049  * Submit a flush request to the device if it supports it. Error handling is
4050  * done in the waiting counterpart.
4051  */
4052 static void write_dev_flush(struct btrfs_device *device)
4053 {
4054 	struct bio *bio = &device->flush_bio;
4055 
4056 	clear_bit(BTRFS_DEV_STATE_FLUSH_FAILED, &device->dev_state);
4057 
4058 	bio_init(bio, device->bdev, NULL, 0,
4059 		 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
4060 	bio->bi_end_io = btrfs_end_empty_barrier;
4061 	init_completion(&device->flush_wait);
4062 	bio->bi_private = &device->flush_wait;
4063 	submit_bio(bio);
4064 	set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
4065 }
4066 
4067 /*
4068  * If the flush bio has been submitted by write_dev_flush, wait for it.
4069  * Return true for any error, and false otherwise.
4070  */
4071 static bool wait_dev_flush(struct btrfs_device *device)
4072 {
4073 	struct bio *bio = &device->flush_bio;
4074 
4075 	if (!test_and_clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
4076 		return false;
4077 
4078 	wait_for_completion_io(&device->flush_wait);
4079 
4080 	if (unlikely(bio->bi_status)) {
4081 		set_bit(BTRFS_DEV_STATE_FLUSH_FAILED, &device->dev_state);
4082 		btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_FLUSH_ERRS);
4083 		return true;
4084 	}
4085 
4086 	return false;
4087 }
4088 
4089 /*
4090  * send an empty flush down to each device in parallel,
4091  * then wait for them
4092  */
4093 static int barrier_all_devices(struct btrfs_fs_info *info)
4094 {
4095 	struct list_head *head;
4096 	struct btrfs_device *dev;
4097 	int errors_wait = 0;
4098 
4099 	lockdep_assert_held(&info->fs_devices->device_list_mutex);
4100 	/* send down all the barriers */
4101 	head = &info->fs_devices->devices;
4102 	list_for_each_entry(dev, head, dev_list) {
4103 		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
4104 			continue;
4105 		if (!dev->bdev)
4106 			continue;
4107 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4108 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4109 			continue;
4110 
4111 		write_dev_flush(dev);
4112 	}
4113 
4114 	/* wait for all the barriers */
4115 	list_for_each_entry(dev, head, dev_list) {
4116 		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
4117 			continue;
4118 		if (unlikely(!dev->bdev)) {
4119 			errors_wait++;
4120 			continue;
4121 		}
4122 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4123 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4124 			continue;
4125 
4126 		if (unlikely(wait_dev_flush(dev)))
4127 			errors_wait++;
4128 	}
4129 
4130 	/*
4131 	 * Checks flush failure of disks in order to determine the device
4132 	 * state.
4133 	 */
4134 	if (unlikely(errors_wait && !btrfs_check_rw_degradable(info, NULL)))
4135 		return -EIO;
4136 
4137 	return 0;
4138 }
4139 
4140 int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
4141 {
4142 	int raid_type;
4143 	int min_tolerated = INT_MAX;
4144 
4145 	if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
4146 	    (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
4147 		min_tolerated = min_t(int, min_tolerated,
4148 				    btrfs_raid_array[BTRFS_RAID_SINGLE].
4149 				    tolerated_failures);
4150 
4151 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4152 		if (raid_type == BTRFS_RAID_SINGLE)
4153 			continue;
4154 		if (!(flags & btrfs_raid_array[raid_type].bg_flag))
4155 			continue;
4156 		min_tolerated = min_t(int, min_tolerated,
4157 				    btrfs_raid_array[raid_type].
4158 				    tolerated_failures);
4159 	}
4160 
4161 	if (min_tolerated == INT_MAX) {
4162 		btrfs_warn(NULL, "unknown raid flag: %llu", flags);
4163 		min_tolerated = 0;
4164 	}
4165 
4166 	return min_tolerated;
4167 }
4168 
4169 int write_all_supers(struct btrfs_trans_handle *trans)
4170 {
4171 	struct btrfs_fs_info *fs_info = trans->fs_info;
4172 	struct list_head *head;
4173 	struct btrfs_device *dev;
4174 	struct btrfs_super_block *sb;
4175 	struct btrfs_dev_item *dev_item;
4176 	int max_mirrors;
4177 	int ret;
4178 	int do_barriers;
4179 	int max_errors;
4180 	int total_errors = 0;
4181 
4182 	do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
4183 
4184 	if (trans->transaction->state < TRANS_STATE_UNBLOCKED) {
4185 		/* We are called from fsync. */
4186 		max_mirrors = 1;
4187 	} else {
4188 		/* We are called from transaction commit. */
4189 		max_mirrors = BTRFS_SUPER_MIRROR_MAX;
4190 		ret = backup_super_roots(fs_info);
4191 		if (ret < 0)
4192 			return ret;
4193 	}
4194 
4195 	sb = fs_info->super_for_commit;
4196 	dev_item = &sb->dev_item;
4197 
4198 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
4199 	head = &fs_info->fs_devices->devices;
4200 	max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
4201 
4202 	if (do_barriers) {
4203 		ret = barrier_all_devices(fs_info);
4204 		if (unlikely(ret)) {
4205 			mutex_unlock(
4206 				&fs_info->fs_devices->device_list_mutex);
4207 			btrfs_abort_transaction(trans, ret);
4208 			btrfs_err(fs_info, "error while submitting device barriers");
4209 			return ret;
4210 		}
4211 	}
4212 
4213 	btrfs_set_super_flags(sb, btrfs_super_flags(sb) | BTRFS_HEADER_FLAG_WRITTEN);
4214 
4215 	list_for_each_entry(dev, head, dev_list) {
4216 		if (unlikely(!dev->bdev)) {
4217 			total_errors++;
4218 			continue;
4219 		}
4220 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4221 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4222 			continue;
4223 
4224 		btrfs_set_stack_device_generation(dev_item, 0);
4225 		btrfs_set_stack_device_type(dev_item, dev->type);
4226 		btrfs_set_stack_device_id(dev_item, dev->devid);
4227 		btrfs_set_stack_device_total_bytes(dev_item,
4228 						   dev->commit_total_bytes);
4229 		btrfs_set_stack_device_bytes_used(dev_item,
4230 						  dev->commit_bytes_used);
4231 		btrfs_set_stack_device_io_align(dev_item, dev->io_align);
4232 		btrfs_set_stack_device_io_width(dev_item, dev->io_width);
4233 		btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
4234 		memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
4235 		memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
4236 		       BTRFS_FSID_SIZE);
4237 
4238 		ret = btrfs_validate_write_super(fs_info, sb);
4239 		if (unlikely(ret < 0)) {
4240 			mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4241 			btrfs_abort_transaction(trans, ret);
4242 			btrfs_err(fs_info,
4243 			  "unexpected superblock corruption before writing it");
4244 			return ret;
4245 		}
4246 
4247 		ret = write_dev_supers(dev, sb, max_mirrors);
4248 		if (unlikely(ret))
4249 			total_errors++;
4250 	}
4251 	if (unlikely(total_errors > max_errors)) {
4252 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4253 
4254 		/* FUA is masked off if unsupported and can't be the reason */
4255 		btrfs_abort_transaction(trans, -EIO);
4256 		btrfs_err(fs_info, "%d errors while writing supers", total_errors);
4257 		return -EIO;
4258 	}
4259 
4260 	total_errors = 0;
4261 	list_for_each_entry(dev, head, dev_list) {
4262 		if (unlikely(!dev->bdev))
4263 			continue;
4264 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4265 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4266 			continue;
4267 
4268 		ret = wait_dev_supers(dev, max_mirrors);
4269 		if (unlikely(ret))
4270 			total_errors++;
4271 	}
4272 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4273 	if (unlikely(total_errors > max_errors)) {
4274 		btrfs_abort_transaction(trans, -EIO);
4275 		btrfs_err(fs_info, "%d errors while writing supers", total_errors);
4276 		return -EIO;
4277 	}
4278 	return 0;
4279 }
4280 
4281 /* Drop a fs root from the radix tree and free it. */
4282 void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
4283 				  struct btrfs_root *root)
4284 {
4285 	bool drop_ref = false;
4286 
4287 	spin_lock(&fs_info->fs_roots_radix_lock);
4288 	radix_tree_delete(&fs_info->fs_roots_radix,
4289 			  (unsigned long)btrfs_root_id(root));
4290 	if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state))
4291 		drop_ref = true;
4292 	spin_unlock(&fs_info->fs_roots_radix_lock);
4293 
4294 	if (unlikely(BTRFS_FS_ERROR(fs_info))) {
4295 		ASSERT(root->log_root == NULL);
4296 		if (root->reloc_root) {
4297 			btrfs_put_root(root->reloc_root);
4298 			root->reloc_root = NULL;
4299 		}
4300 	}
4301 
4302 	if (drop_ref)
4303 		btrfs_put_root(root);
4304 }
4305 
4306 int btrfs_commit_super(struct btrfs_fs_info *fs_info)
4307 {
4308 	mutex_lock(&fs_info->cleaner_mutex);
4309 	btrfs_run_delayed_iputs(fs_info);
4310 	mutex_unlock(&fs_info->cleaner_mutex);
4311 	wake_up_process(fs_info->cleaner_kthread);
4312 
4313 	/* wait until ongoing cleanup work done */
4314 	down_write(&fs_info->cleanup_work_sem);
4315 	up_write(&fs_info->cleanup_work_sem);
4316 
4317 	return btrfs_commit_current_transaction(fs_info->tree_root);
4318 }
4319 
4320 static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
4321 {
4322 	struct btrfs_transaction *trans;
4323 	struct btrfs_transaction *tmp;
4324 	bool found = false;
4325 
4326 	/*
4327 	 * This function is only called at the very end of close_ctree(),
4328 	 * thus no other running transaction, no need to take trans_lock.
4329 	 */
4330 	ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags));
4331 	list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) {
4332 		struct extent_state *cached = NULL;
4333 		u64 dirty_bytes = 0;
4334 		u64 cur = 0;
4335 		u64 found_start;
4336 		u64 found_end;
4337 
4338 		found = true;
4339 		while (btrfs_find_first_extent_bit(&trans->dirty_pages, cur,
4340 						   &found_start, &found_end,
4341 						   EXTENT_DIRTY, &cached)) {
4342 			dirty_bytes += found_end + 1 - found_start;
4343 			cur = found_end + 1;
4344 		}
4345 		btrfs_warn(fs_info,
4346 	"transaction %llu (with %llu dirty metadata bytes) is not committed",
4347 			   trans->transid, dirty_bytes);
4348 		btrfs_cleanup_one_transaction(trans);
4349 
4350 		if (trans == fs_info->running_transaction)
4351 			fs_info->running_transaction = NULL;
4352 		list_del_init(&trans->list);
4353 
4354 		btrfs_put_transaction(trans);
4355 	}
4356 	ASSERT(!found);
4357 }
4358 
4359 void __cold close_ctree(struct btrfs_fs_info *fs_info)
4360 {
4361 	int ret;
4362 
4363 	set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
4364 
4365 	/*
4366 	 * If we had UNFINISHED_DROPS we could still be processing them, so
4367 	 * clear that bit and wake up relocation so it can stop.
4368 	 * We must do this before stopping the block group reclaim task, because
4369 	 * at btrfs_relocate_block_group() we wait for this bit, and after the
4370 	 * wait we stop with -EINTR if btrfs_fs_closing() returns non-zero - we
4371 	 * have just set BTRFS_FS_CLOSING_START, so btrfs_fs_closing() will
4372 	 * return 1.
4373 	 */
4374 	btrfs_wake_unfinished_drop(fs_info);
4375 
4376 	/*
4377 	 * We may have the reclaim task running and relocating a data block group,
4378 	 * in which case it may create delayed iputs. So stop it before we park
4379 	 * the cleaner kthread otherwise we can get new delayed iputs after
4380 	 * parking the cleaner, and that can make the async reclaim task to hang
4381 	 * if it's waiting for delayed iputs to complete, since the cleaner is
4382 	 * parked and can not run delayed iputs - this will make us hang when
4383 	 * trying to stop the async reclaim task.
4384 	 */
4385 	cancel_work_sync(&fs_info->reclaim_bgs_work);
4386 	/*
4387 	 * We don't want the cleaner to start new transactions, add more delayed
4388 	 * iputs, etc. while we're closing. We can't use kthread_stop() yet
4389 	 * because that frees the task_struct, and the transaction kthread might
4390 	 * still try to wake up the cleaner.
4391 	 */
4392 	kthread_park(fs_info->cleaner_kthread);
4393 
4394 	/* wait for the qgroup rescan worker to stop */
4395 	btrfs_qgroup_wait_for_completion(fs_info, false);
4396 
4397 	/* wait for the uuid_scan task to finish */
4398 	down(&fs_info->uuid_tree_rescan_sem);
4399 	/* avoid complains from lockdep et al., set sem back to initial state */
4400 	up(&fs_info->uuid_tree_rescan_sem);
4401 
4402 	/* pause restriper - we want to resume on mount */
4403 	btrfs_pause_balance(fs_info);
4404 
4405 	btrfs_dev_replace_suspend_for_unmount(fs_info);
4406 
4407 	btrfs_scrub_cancel(fs_info);
4408 
4409 	/* wait for any defraggers to finish */
4410 	wait_event(fs_info->transaction_wait,
4411 		   (atomic_read(&fs_info->defrag_running) == 0));
4412 
4413 	/* clear out the rbtree of defraggable inodes */
4414 	btrfs_cleanup_defrag_inodes(fs_info);
4415 
4416 	/*
4417 	 * Before the unmount, we sync down all the writeback which can
4418 	 * generate fixup work. We are about to run delalloc for autodefrag so
4419 	 * piggy back on that by also flushing the fixup work which can also
4420 	 * generate delalloc we would like to get run.
4421 	 *
4422 	 * After this, it is still possible that some thread doing writeback is
4423 	 * in btrfs_queue_writepage_fixup() and might finish queueing some final
4424 	 * work, racing the btrfs_fs_closing() check there.
4425 	 */
4426 	flush_workqueue(fs_info->fixup_workers);
4427 
4428 	/*
4429 	 * After we entered close_ctree() autodefrag could be running and before
4430 	 * we parked the cleaner kthread, it dirtied folios of some inode.
4431 	 * We don't want to leave any delalloc here, it may be flushed any time
4432 	 * after this point and result in ordered extents that create delayed
4433 	 * iputs after flushed the ordered extent queues further below, run
4434 	 * delayed iputs and set BTRFS_FS_STATE_NO_DELAYED_IPUT. If we are
4435 	 * mounted with flushoncommit, then btrfs_commit_super() called below
4436 	 * will flush delalloc and wait for ordered extents but we end up
4437 	 * getting delayed iputs than are never run. So flush delalloc and wait
4438 	 * for ordered extents.
4439 	 */
4440 	btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
4441 	btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
4442 
4443 	/*
4444 	 * Handle the error fs first, as it will flush and wait for all ordered
4445 	 * extents.  This will generate delayed iputs, thus we want to handle
4446 	 * it first.
4447 	 */
4448 	if (unlikely(BTRFS_FS_ERROR(fs_info)))
4449 		btrfs_error_commit_super(fs_info);
4450 
4451 	/*
4452 	 * Similar case here, we have to wait for delalloc workers before we
4453 	 * proceed below and stop the cleaner kthread, otherwise we trigger a
4454 	 * use-after-tree on the cleaner kthread task_struct when a delalloc
4455 	 * worker running submit_compressed_extents() adds a delayed iput, which
4456 	 * does a wake up on the cleaner kthread, which was already freed below
4457 	 * when we call kthread_stop().
4458 	 */
4459 	btrfs_flush_workqueue(fs_info->delalloc_workers);
4460 
4461 	/*
4462 	 * We can have ordered extents getting their last reference dropped from
4463 	 * the fs_info->workers queue because for async writes for data bios we
4464 	 * queue a work for that queue, at btrfs_wq_submit_bio(), that runs
4465 	 * run_one_async_done() which calls btrfs_bio_end_io() in case the bio
4466 	 * has an error, and that later function can do the final
4467 	 * btrfs_put_ordered_extent() on the ordered extent attached to the bio,
4468 	 * which adds a delayed iput for the inode. So we must flush the queue
4469 	 * so that we don't have delayed iputs after committing the current
4470 	 * transaction below and stopping the cleaner and transaction kthreads.
4471 	 */
4472 	btrfs_flush_workqueue(fs_info->workers);
4473 
4474 	/*
4475 	 * When finishing a compressed write bio we schedule a work queue item
4476 	 * to finish an ordered extent - end_bbio_compressed_write()
4477 	 * calls btrfs_finish_ordered_extent() which in turns does a call to
4478 	 * btrfs_queue_ordered_fn(), and that queues the ordered extent
4479 	 * completion in the endio_write_workers work queue. We flush that
4480 	 * queue below, so before we flush it we must flush this queue for the
4481 	 * workers of compressed writes.
4482 	 */
4483 	flush_workqueue(fs_info->endio_workers);
4484 
4485 	/*
4486 	 * After we parked the cleaner kthread, ordered extents may have
4487 	 * completed and created new delayed iputs. If one of the async reclaim
4488 	 * tasks is running and in the RUN_DELAYED_IPUTS flush state, then we
4489 	 * can hang forever trying to stop it, because if a delayed iput is
4490 	 * added after it ran btrfs_run_delayed_iputs() and before it called
4491 	 * btrfs_wait_on_delayed_iputs(), it will hang forever since there is
4492 	 * no one else to run iputs.
4493 	 *
4494 	 * So wait for all ongoing ordered extents to complete and then run
4495 	 * delayed iputs. This works because once we reach this point no one
4496 	 * can create new ordered extents, but delayed iputs can still be added
4497 	 * by a reclaim worker (see comments further below).
4498 	 *
4499 	 * Also note that btrfs_wait_ordered_roots() is not safe here, because
4500 	 * it waits for BTRFS_ORDERED_COMPLETE to be set on an ordered extent,
4501 	 * but the delayed iput for the respective inode is made only when doing
4502 	 * the final btrfs_put_ordered_extent() (which must happen at
4503 	 * btrfs_finish_ordered_io() when we are unmounting).
4504 	 */
4505 	btrfs_flush_workqueue(fs_info->endio_write_workers);
4506 	/*
4507 	 * Run delayed iputs in case an async reclaim worker is waiting for them
4508 	 * to be run as mentioned above.
4509 	 */
4510 	btrfs_run_delayed_iputs(fs_info);
4511 
4512 	cancel_work_sync(&fs_info->async_reclaim_work);
4513 	cancel_work_sync(&fs_info->async_data_reclaim_work);
4514 	cancel_work_sync(&fs_info->preempt_reclaim_work);
4515 	cancel_work_sync(&fs_info->em_shrinker_work);
4516 
4517 	/*
4518 	 * Reclaim workers can run writeback which can queue fixup.
4519 	 * After the above cancel_work_sync() calls, any such queueing attempts are
4520 	 * guaranteed to see btrfs_fs_closing(), so at this point we can genuinely fully
4521 	 * flush the fixup workqueue. This relies on the belief that *now* no thread can
4522 	 * still be sitting in btrfs_queue_writepage_fixup().
4523 	 */
4524 	flush_workqueue(fs_info->fixup_workers);
4525 
4526 	/*
4527 	 * Run delayed iputs again because an async reclaim worker may have
4528 	 * added new ones if it was flushing delalloc:
4529 	 *
4530 	 * shrink_delalloc() -> btrfs_start_delalloc_roots() ->
4531 	 *    start_delalloc_inodes() -> btrfs_add_delayed_iput()
4532 	 */
4533 	btrfs_run_delayed_iputs(fs_info);
4534 
4535 	/* There should be no more workload to generate new delayed iputs. */
4536 	set_bit(BTRFS_FS_STATE_NO_DELAYED_IPUT, &fs_info->fs_state);
4537 
4538 	/* Cancel or finish ongoing discard work */
4539 	btrfs_discard_cleanup(fs_info);
4540 
4541 	if (!sb_rdonly(fs_info->sb)) {
4542 		/*
4543 		 * The cleaner kthread is stopped, so do one final pass over
4544 		 * unused block groups.
4545 		 */
4546 		btrfs_delete_unused_bgs(fs_info);
4547 
4548 		/*
4549 		 * There might be existing delayed inode workers still running
4550 		 * and holding an empty delayed inode item. We must wait for
4551 		 * them to complete first because they can create a transaction.
4552 		 * This happens when someone calls btrfs_balance_delayed_items()
4553 		 * and then a transaction commit runs the same delayed nodes
4554 		 * before any delayed worker has done something with the nodes.
4555 		 * We must wait for any worker here and not at transaction
4556 		 * commit time since that could cause a deadlock.
4557 		 * This is a very rare case.
4558 		 */
4559 		btrfs_flush_workqueue(fs_info->delayed_workers);
4560 
4561 		/*
4562 		 * If the filesystem is shutdown, then an attempt to commit the
4563 		 * super block (or any write) will just fail. Since we freeze
4564 		 * the filesystem before shutting it down, the filesystem is in
4565 		 * a consistent state and we don't need to commit super blocks.
4566 		 */
4567 		if (!btrfs_is_shutdown(fs_info)) {
4568 			ret = btrfs_commit_super(fs_info);
4569 			if (ret)
4570 				btrfs_err(fs_info, "commit super block returned %pe", ERR_PTR(ret));
4571 		}
4572 	}
4573 
4574 	kthread_stop(fs_info->transaction_kthread);
4575 	kthread_stop(fs_info->cleaner_kthread);
4576 
4577 	ASSERT(list_empty(&fs_info->delayed_iputs));
4578 	set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
4579 
4580 	if (unlikely(btrfs_check_quota_leak(fs_info))) {
4581 		DEBUG_WARN("qgroup reserved space leaked");
4582 		btrfs_err(fs_info, "qgroup reserved space leaked");
4583 	}
4584 
4585 	btrfs_free_qgroup_config(fs_info);
4586 	ASSERT(list_empty(&fs_info->delalloc_roots));
4587 
4588 	if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
4589 		btrfs_info(fs_info, "at unmount delalloc count %lld",
4590 		       percpu_counter_sum(&fs_info->delalloc_bytes));
4591 	}
4592 
4593 	if (percpu_counter_sum(&fs_info->ordered_bytes))
4594 		btrfs_info(fs_info, "at unmount dio bytes count %lld",
4595 			   percpu_counter_sum(&fs_info->ordered_bytes));
4596 
4597 	btrfs_sysfs_remove_mounted(fs_info);
4598 	btrfs_sysfs_remove_fsid(fs_info->fs_devices);
4599 
4600 	btrfs_put_block_group_cache(fs_info);
4601 
4602 	/* We shouldn't have any transaction open at this point */
4603 	warn_about_uncommitted_trans(fs_info);
4604 
4605 	clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
4606 	free_root_pointers(fs_info, true);
4607 	btrfs_free_fs_roots(fs_info);
4608 
4609 	/*
4610 	 * Drop metadata left stranded ahead of a zone write pointer while the
4611 	 * endio workqueues are still up, so the final iput() of the btree inode
4612 	 * below does not hang submitting a write that can no longer complete.
4613 	 */
4614 	btrfs_zoned_release_dirty_metadata(fs_info);
4615 
4616 	/*
4617 	 * We must make sure there is not any read request to
4618 	 * submit after we stop all workers.
4619 	 */
4620 	invalidate_and_check_btree_folios(fs_info);
4621 	btrfs_stop_all_workers(fs_info);
4622 
4623 	/*
4624 	 * We must free the block groups after dropping the fs_roots as we could
4625 	 * have had an IO error and have left over tree log blocks that aren't
4626 	 * cleaned up until the fs roots are freed.  This makes the block group
4627 	 * accounting appear to be wrong because there's pending reserved bytes,
4628 	 * so make sure we do the block group cleanup afterwards.
4629 	 */
4630 	btrfs_free_block_groups(fs_info);
4631 
4632 	iput(fs_info->btree_inode);
4633 
4634 	btrfs_mapping_tree_free(fs_info);
4635 }
4636 
4637 void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
4638 			     struct extent_buffer *buf)
4639 {
4640 	struct btrfs_fs_info *fs_info = buf->fs_info;
4641 	u64 transid = btrfs_header_generation(buf);
4642 
4643 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4644 	/*
4645 	 * This is a fast path so only do this check if we have sanity tests
4646 	 * enabled.  Normal people shouldn't be using unmapped buffers as dirty
4647 	 * outside of the sanity tests.
4648 	 */
4649 	if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
4650 		return;
4651 #endif
4652 	/* This is an active transaction (its state < TRANS_STATE_UNBLOCKED). */
4653 	ASSERT(trans->transid == fs_info->generation);
4654 	btrfs_assert_tree_write_locked(buf);
4655 	if (unlikely(transid != fs_info->generation)) {
4656 		btrfs_abort_transaction(trans, -EUCLEAN);
4657 		btrfs_crit(fs_info,
4658 "dirty buffer transid mismatch, logical %llu found transid %llu running transid %llu",
4659 			   buf->start, transid, fs_info->generation);
4660 	}
4661 	set_extent_buffer_dirty(buf);
4662 }
4663 
4664 static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
4665 					int flush_delayed)
4666 {
4667 	/*
4668 	 * looks as though older kernels can get into trouble with
4669 	 * this code, they end up stuck in balance_dirty_pages forever
4670 	 */
4671 	int ret;
4672 
4673 	if (current->flags & PF_MEMALLOC)
4674 		return;
4675 
4676 	if (flush_delayed)
4677 		btrfs_balance_delayed_items(fs_info);
4678 
4679 	ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4680 				     BTRFS_DIRTY_METADATA_THRESH,
4681 				     fs_info->dirty_metadata_batch);
4682 	if (ret > 0) {
4683 		balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
4684 	}
4685 }
4686 
4687 void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
4688 {
4689 	__btrfs_btree_balance_dirty(fs_info, 1);
4690 }
4691 
4692 void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
4693 {
4694 	__btrfs_btree_balance_dirty(fs_info, 0);
4695 }
4696 
4697 static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
4698 {
4699 	/* cleanup FS via transaction */
4700 	btrfs_cleanup_transaction(fs_info);
4701 
4702 	down_write(&fs_info->cleanup_work_sem);
4703 	up_write(&fs_info->cleanup_work_sem);
4704 }
4705 
4706 static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info)
4707 {
4708 	struct btrfs_root *gang[8];
4709 	u64 root_objectid = 0;
4710 	int ret;
4711 
4712 	spin_lock(&fs_info->fs_roots_radix_lock);
4713 	while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
4714 					     (void **)gang, root_objectid,
4715 					     ARRAY_SIZE(gang))) != 0) {
4716 		int i;
4717 
4718 		for (i = 0; i < ret; i++)
4719 			gang[i] = btrfs_grab_root(gang[i]);
4720 		spin_unlock(&fs_info->fs_roots_radix_lock);
4721 
4722 		for (i = 0; i < ret; i++) {
4723 			if (!gang[i])
4724 				continue;
4725 			root_objectid = btrfs_root_id(gang[i]);
4726 			btrfs_free_log(NULL, gang[i]);
4727 			btrfs_put_root(gang[i]);
4728 		}
4729 		root_objectid++;
4730 		spin_lock(&fs_info->fs_roots_radix_lock);
4731 	}
4732 	spin_unlock(&fs_info->fs_roots_radix_lock);
4733 	btrfs_free_log_root_tree(NULL, fs_info);
4734 }
4735 
4736 static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
4737 {
4738 	struct btrfs_ordered_extent *ordered;
4739 
4740 	spin_lock(&root->ordered_extent_lock);
4741 	/*
4742 	 * This will just short circuit the ordered completion stuff which will
4743 	 * make sure the ordered extent gets properly cleaned up.
4744 	 */
4745 	list_for_each_entry(ordered, &root->ordered_extents,
4746 			    root_extent_list)
4747 		set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
4748 	spin_unlock(&root->ordered_extent_lock);
4749 }
4750 
4751 static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4752 {
4753 	struct btrfs_root *root;
4754 	LIST_HEAD(splice);
4755 
4756 	spin_lock(&fs_info->ordered_root_lock);
4757 	list_splice_init(&fs_info->ordered_roots, &splice);
4758 	while (!list_empty(&splice)) {
4759 		root = list_first_entry(&splice, struct btrfs_root,
4760 					ordered_root);
4761 		list_move_tail(&root->ordered_root,
4762 			       &fs_info->ordered_roots);
4763 
4764 		spin_unlock(&fs_info->ordered_root_lock);
4765 		btrfs_destroy_ordered_extents(root);
4766 
4767 		cond_resched();
4768 		spin_lock(&fs_info->ordered_root_lock);
4769 	}
4770 	spin_unlock(&fs_info->ordered_root_lock);
4771 
4772 	/*
4773 	 * We need this here because if we've been flipped read-only we won't
4774 	 * get sync() from the umount, so we need to make sure any ordered
4775 	 * extents that haven't had their dirty pages IO start writeout yet
4776 	 * actually get run and error out properly.
4777 	 */
4778 	btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
4779 }
4780 
4781 static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
4782 {
4783 	struct btrfs_inode *btrfs_inode;
4784 	LIST_HEAD(splice);
4785 
4786 	spin_lock(&root->delalloc_lock);
4787 	list_splice_init(&root->delalloc_inodes, &splice);
4788 
4789 	while (!list_empty(&splice)) {
4790 		struct inode *inode = NULL;
4791 		btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
4792 					       delalloc_inodes);
4793 		btrfs_del_delalloc_inode(btrfs_inode);
4794 		spin_unlock(&root->delalloc_lock);
4795 
4796 		/*
4797 		 * Make sure we get a live inode and that it'll not disappear
4798 		 * meanwhile.
4799 		 */
4800 		inode = igrab(&btrfs_inode->vfs_inode);
4801 		if (inode) {
4802 			unsigned int nofs_flag;
4803 
4804 			nofs_flag = memalloc_nofs_save();
4805 			invalidate_inode_pages2(inode->i_mapping);
4806 			memalloc_nofs_restore(nofs_flag);
4807 			iput(inode);
4808 		}
4809 		spin_lock(&root->delalloc_lock);
4810 	}
4811 	spin_unlock(&root->delalloc_lock);
4812 }
4813 
4814 static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4815 {
4816 	struct btrfs_root *root;
4817 	LIST_HEAD(splice);
4818 
4819 	spin_lock(&fs_info->delalloc_root_lock);
4820 	list_splice_init(&fs_info->delalloc_roots, &splice);
4821 	while (!list_empty(&splice)) {
4822 		root = list_first_entry(&splice, struct btrfs_root,
4823 					 delalloc_root);
4824 		root = btrfs_grab_root(root);
4825 		BUG_ON(!root);
4826 		spin_unlock(&fs_info->delalloc_root_lock);
4827 
4828 		btrfs_destroy_delalloc_inodes(root);
4829 		btrfs_put_root(root);
4830 
4831 		spin_lock(&fs_info->delalloc_root_lock);
4832 	}
4833 	spin_unlock(&fs_info->delalloc_root_lock);
4834 }
4835 
4836 static void btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
4837 					 struct extent_io_tree *dirty_pages,
4838 					 int mark)
4839 {
4840 	struct extent_buffer *eb;
4841 	u64 start = 0;
4842 	u64 end;
4843 
4844 	while (btrfs_find_first_extent_bit(dirty_pages, start, &start, &end,
4845 					   mark, NULL)) {
4846 		btrfs_clear_extent_bit(dirty_pages, start, end, mark, NULL);
4847 		while (start <= end) {
4848 			eb = find_extent_buffer(fs_info, start);
4849 			start += fs_info->nodesize;
4850 			if (!eb)
4851 				continue;
4852 
4853 			btrfs_tree_lock(eb);
4854 			wait_on_extent_buffer_writeback(eb);
4855 			btrfs_clear_buffer_dirty(NULL, eb);
4856 			btrfs_tree_unlock(eb);
4857 
4858 			free_extent_buffer_stale(eb);
4859 		}
4860 	}
4861 	btrfs_extent_io_tree_release(dirty_pages);
4862 }
4863 
4864 static void btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
4865 					struct extent_io_tree *unpin)
4866 {
4867 	u64 start;
4868 	u64 end;
4869 
4870 	while (1) {
4871 		struct extent_state *cached_state = NULL;
4872 
4873 		/*
4874 		 * The btrfs_finish_extent_commit() may get the same range as
4875 		 * ours between find_first_extent_bit and clear_extent_dirty.
4876 		 * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
4877 		 * the same extent range.
4878 		 */
4879 		mutex_lock(&fs_info->unused_bg_unpin_mutex);
4880 		if (!btrfs_find_first_extent_bit(unpin, 0, &start, &end,
4881 						 EXTENT_DIRTY, &cached_state)) {
4882 			mutex_unlock(&fs_info->unused_bg_unpin_mutex);
4883 			break;
4884 		}
4885 
4886 		btrfs_clear_extent_dirty(unpin, start, end, &cached_state);
4887 		btrfs_free_extent_state(cached_state);
4888 		btrfs_error_unpin_extent_range(fs_info, start, end);
4889 		mutex_unlock(&fs_info->unused_bg_unpin_mutex);
4890 		cond_resched();
4891 	}
4892 }
4893 
4894 void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4895 			     struct btrfs_fs_info *fs_info)
4896 {
4897 	struct btrfs_block_group *cache;
4898 
4899 	spin_lock(&cur_trans->dirty_bgs_lock);
4900 	while (!list_empty(&cur_trans->dirty_bgs)) {
4901 		cache = list_first_entry(&cur_trans->dirty_bgs,
4902 					 struct btrfs_block_group,
4903 					 dirty_list);
4904 
4905 		list_del_init(&cache->dirty_list);
4906 		spin_unlock(&cur_trans->dirty_bgs_lock);
4907 		btrfs_put_block_group(cache);
4908 		btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
4909 		spin_lock(&cur_trans->dirty_bgs_lock);
4910 	}
4911 	spin_unlock(&cur_trans->dirty_bgs_lock);
4912 }
4913 
4914 static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
4915 {
4916 	struct btrfs_root *gang[8];
4917 	int i;
4918 	int ret;
4919 
4920 	spin_lock(&fs_info->fs_roots_radix_lock);
4921 	while (1) {
4922 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
4923 						 (void **)gang, 0,
4924 						 ARRAY_SIZE(gang),
4925 						 BTRFS_ROOT_TRANS_TAG);
4926 		if (ret == 0)
4927 			break;
4928 		for (i = 0; i < ret; i++) {
4929 			struct btrfs_root *root = gang[i];
4930 
4931 			btrfs_qgroup_free_meta_all_pertrans(root);
4932 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
4933 					(unsigned long)btrfs_root_id(root),
4934 					BTRFS_ROOT_TRANS_TAG);
4935 		}
4936 	}
4937 	spin_unlock(&fs_info->fs_roots_radix_lock);
4938 }
4939 
4940 void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans)
4941 {
4942 	struct btrfs_fs_info *fs_info = cur_trans->fs_info;
4943 	struct btrfs_device *dev, *tmp;
4944 
4945 	btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
4946 	ASSERT(list_empty(&cur_trans->dirty_bgs));
4947 
4948 	list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
4949 				 post_commit_list) {
4950 		list_del_init(&dev->post_commit_list);
4951 	}
4952 
4953 	while (!list_empty(&cur_trans->switch_commits)) {
4954 		struct btrfs_root *root;
4955 
4956 		root = list_first_entry(&cur_trans->switch_commits,
4957 					struct btrfs_root, dirty_list);
4958 		list_del_init(&root->dirty_list);
4959 		btrfs_extent_io_tree_release(&root->dirty_log_pages);
4960 		btrfs_qgroup_clean_swapped_blocks(root);
4961 	}
4962 
4963 	btrfs_destroy_delayed_refs(cur_trans);
4964 
4965 	cur_trans->state = TRANS_STATE_COMMIT_START;
4966 	wake_up(&fs_info->transaction_blocked_wait);
4967 
4968 	cur_trans->state = TRANS_STATE_UNBLOCKED;
4969 	wake_up(&fs_info->transaction_wait);
4970 
4971 	btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
4972 				     EXTENT_DIRTY);
4973 	btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
4974 
4975 	cur_trans->state =TRANS_STATE_COMPLETED;
4976 	wake_up(&cur_trans->commit_wait);
4977 }
4978 
4979 static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
4980 {
4981 	struct btrfs_transaction *t;
4982 
4983 	mutex_lock(&fs_info->transaction_kthread_mutex);
4984 
4985 	spin_lock(&fs_info->trans_lock);
4986 	while (!list_empty(&fs_info->trans_list)) {
4987 		t = list_first_entry(&fs_info->trans_list,
4988 				     struct btrfs_transaction, list);
4989 		if (t->state >= TRANS_STATE_COMMIT_PREP) {
4990 			refcount_inc(&t->use_count);
4991 			spin_unlock(&fs_info->trans_lock);
4992 			btrfs_wait_for_commit(fs_info, t->transid);
4993 			btrfs_put_transaction(t);
4994 			spin_lock(&fs_info->trans_lock);
4995 			continue;
4996 		}
4997 		if (t == fs_info->running_transaction) {
4998 			t->state = TRANS_STATE_COMMIT_DOING;
4999 			spin_unlock(&fs_info->trans_lock);
5000 			/*
5001 			 * We wait for 0 num_writers since we don't hold a trans
5002 			 * handle open currently for this transaction.
5003 			 */
5004 			wait_event(t->writer_wait,
5005 				   atomic_read(&t->num_writers) == 0);
5006 		} else {
5007 			spin_unlock(&fs_info->trans_lock);
5008 		}
5009 		btrfs_cleanup_one_transaction(t);
5010 
5011 		spin_lock(&fs_info->trans_lock);
5012 		if (t == fs_info->running_transaction)
5013 			fs_info->running_transaction = NULL;
5014 		list_del_init(&t->list);
5015 		spin_unlock(&fs_info->trans_lock);
5016 
5017 		btrfs_put_transaction(t);
5018 		spin_lock(&fs_info->trans_lock);
5019 	}
5020 	spin_unlock(&fs_info->trans_lock);
5021 	btrfs_destroy_all_ordered_extents(fs_info);
5022 	btrfs_destroy_delayed_inodes(fs_info);
5023 	btrfs_assert_delayed_root_empty(fs_info);
5024 	btrfs_destroy_all_delalloc_inodes(fs_info);
5025 	btrfs_drop_all_logs(fs_info);
5026 	btrfs_zoned_release_dirty_metadata(fs_info);
5027 	btrfs_free_all_qgroup_pertrans(fs_info);
5028 	mutex_unlock(&fs_info->transaction_kthread_mutex);
5029 
5030 	return 0;
5031 }
5032 
5033 int btrfs_init_root_free_objectid(struct btrfs_root *root)
5034 {
5035 	BTRFS_PATH_AUTO_FREE(path);
5036 	int ret;
5037 	struct extent_buffer *l;
5038 	struct btrfs_key search_key;
5039 	struct btrfs_key found_key;
5040 	int slot;
5041 
5042 	path = btrfs_alloc_path();
5043 	if (!path)
5044 		return -ENOMEM;
5045 
5046 	search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
5047 	search_key.type = -1;
5048 	search_key.offset = (u64)-1;
5049 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5050 	if (ret < 0)
5051 		return ret;
5052 	if (unlikely(ret == 0)) {
5053 		/*
5054 		 * Key with offset -1 found, there would have to exist a root
5055 		 * with such id, but this is out of valid range.
5056 		 */
5057 		return -EUCLEAN;
5058 	}
5059 	if (path->slots[0] > 0) {
5060 		slot = path->slots[0] - 1;
5061 		l = path->nodes[0];
5062 		btrfs_item_key_to_cpu(l, &found_key, slot);
5063 		root->free_objectid = max_t(u64, found_key.objectid + 1,
5064 					    BTRFS_FIRST_FREE_OBJECTID);
5065 	} else {
5066 		root->free_objectid = BTRFS_FIRST_FREE_OBJECTID;
5067 	}
5068 
5069 	return 0;
5070 }
5071 
5072 int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid)
5073 {
5074 	int ret;
5075 	mutex_lock(&root->objectid_mutex);
5076 
5077 	if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
5078 		btrfs_warn(root->fs_info,
5079 			   "the objectid of root %llu reaches its highest value",
5080 			   btrfs_root_id(root));
5081 		ret = -ENOSPC;
5082 		goto out;
5083 	}
5084 
5085 	*objectid = root->free_objectid++;
5086 	ret = 0;
5087 out:
5088 	mutex_unlock(&root->objectid_mutex);
5089 	return ret;
5090 }
5091