1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
7 #include <linux/blkdev.h>
8 #include <linux/writeback.h>
9 #include <linux/workqueue.h>
10 #include <linux/kthread.h>
11 #include <linux/slab.h>
12 #include <linux/migrate.h>
13 #include <linux/ratelimit.h>
14 #include <linux/uuid.h>
15 #include <linux/semaphore.h>
16 #include <linux/error-injection.h>
17 #include <linux/crc32c.h>
18 #include <linux/sched/mm.h>
19 #include <asm/unaligned.h>
20 #include <crypto/hash.h>
23 #include "transaction.h"
24 #include "btrfs_inode.h"
26 #include "print-tree.h"
29 #include "free-space-cache.h"
30 #include "free-space-tree.h"
31 #include "check-integrity.h"
32 #include "rcu-string.h"
33 #include "dev-replace.h"
37 #include "compression.h"
38 #include "tree-checker.h"
39 #include "ref-verify.h"
40 #include "block-group.h"
42 #include "space-info.h"
46 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
47 BTRFS_HEADER_FLAG_RELOC |\
48 BTRFS_SUPER_FLAG_ERROR |\
49 BTRFS_SUPER_FLAG_SEEDING |\
50 BTRFS_SUPER_FLAG_METADUMP |\
51 BTRFS_SUPER_FLAG_METADUMP_V2)
53 static void end_workqueue_fn(struct btrfs_work *work);
54 static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
55 static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
56 struct btrfs_fs_info *fs_info);
57 static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
58 static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
59 struct extent_io_tree *dirty_pages,
61 static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
62 struct extent_io_tree *pinned_extents);
63 static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
64 static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
67 * btrfs_end_io_wq structs are used to do processing in task context when an IO
68 * is complete. This is used during reads to verify checksums, and it is used
69 * by writes to insert metadata for new file extents after IO is complete.
71 struct btrfs_end_io_wq {
75 struct btrfs_fs_info *info;
77 enum btrfs_wq_endio_type metadata;
78 struct btrfs_work work;
81 static struct kmem_cache *btrfs_end_io_wq_cache;
83 int __init btrfs_end_io_wq_init(void)
85 btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq",
86 sizeof(struct btrfs_end_io_wq),
90 if (!btrfs_end_io_wq_cache)
95 void __cold btrfs_end_io_wq_exit(void)
97 kmem_cache_destroy(btrfs_end_io_wq_cache);
100 static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
102 if (fs_info->csum_shash)
103 crypto_free_shash(fs_info->csum_shash);
107 * async submit bios are used to offload expensive checksumming
108 * onto the worker threads. They checksum file and metadata bios
109 * just before they are sent down the IO stack.
111 struct async_submit_bio {
114 extent_submit_bio_start_t *submit_bio_start;
117 /* Optional parameter for submit_bio_start used by direct io */
119 struct btrfs_work work;
124 * Lockdep class keys for extent_buffer->lock's in this root. For a given
125 * eb, the lockdep key is determined by the btrfs_root it belongs to and
126 * the level the eb occupies in the tree.
128 * Different roots are used for different purposes and may nest inside each
129 * other and they require separate keysets. As lockdep keys should be
130 * static, assign keysets according to the purpose of the root as indicated
131 * by btrfs_root->root_key.objectid. This ensures that all special purpose
132 * roots have separate keysets.
134 * Lock-nesting across peer nodes is always done with the immediate parent
135 * node locked thus preventing deadlock. As lockdep doesn't know this, use
136 * subclass to avoid triggering lockdep warning in such cases.
138 * The key is set by the readpage_end_io_hook after the buffer has passed
139 * csum validation but before the pages are unlocked. It is also set by
140 * btrfs_init_new_buffer on freshly allocated blocks.
142 * We also add a check to make sure the highest level of the tree is the
143 * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
144 * needs update as well.
146 #ifdef CONFIG_DEBUG_LOCK_ALLOC
147 # if BTRFS_MAX_LEVEL != 8
151 #define DEFINE_LEVEL(stem, level) \
152 .names[level] = "btrfs-" stem "-0" #level,
154 #define DEFINE_NAME(stem) \
155 DEFINE_LEVEL(stem, 0) \
156 DEFINE_LEVEL(stem, 1) \
157 DEFINE_LEVEL(stem, 2) \
158 DEFINE_LEVEL(stem, 3) \
159 DEFINE_LEVEL(stem, 4) \
160 DEFINE_LEVEL(stem, 5) \
161 DEFINE_LEVEL(stem, 6) \
162 DEFINE_LEVEL(stem, 7)
164 static struct btrfs_lockdep_keyset {
165 u64 id; /* root objectid */
166 /* Longest entry: btrfs-free-space-00 */
167 char names[BTRFS_MAX_LEVEL][20];
168 struct lock_class_key keys[BTRFS_MAX_LEVEL];
169 } btrfs_lockdep_keysets[] = {
170 { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") },
171 { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") },
172 { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") },
173 { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") },
174 { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") },
175 { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") },
176 { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") },
177 { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") },
178 { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") },
179 { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") },
180 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") },
181 { .id = 0, DEFINE_NAME("tree") },
187 void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb,
190 struct btrfs_lockdep_keyset *ks;
192 BUG_ON(level >= ARRAY_SIZE(ks->keys));
194 /* find the matching keyset, id 0 is the default entry */
195 for (ks = btrfs_lockdep_keysets; ks->id; ks++)
196 if (ks->id == objectid)
199 lockdep_set_class_and_name(&eb->lock,
200 &ks->keys[level], ks->names[level]);
206 * Compute the csum of a btree block and store the result to provided buffer.
208 static void csum_tree_block(struct extent_buffer *buf, u8 *result)
210 struct btrfs_fs_info *fs_info = buf->fs_info;
211 const int num_pages = num_extent_pages(buf);
212 const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
213 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
217 shash->tfm = fs_info->csum_shash;
218 crypto_shash_init(shash);
219 kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start);
220 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
221 first_page_part - BTRFS_CSUM_SIZE);
223 for (i = 1; i < num_pages; i++) {
224 kaddr = page_address(buf->pages[i]);
225 crypto_shash_update(shash, kaddr, PAGE_SIZE);
227 memset(result, 0, BTRFS_CSUM_SIZE);
228 crypto_shash_final(shash, result);
232 * we can't consider a given block up to date unless the transid of the
233 * block matches the transid in the parent node's pointer. This is how we
234 * detect blocks that either didn't get written at all or got written
235 * in the wrong place.
237 static int verify_parent_transid(struct extent_io_tree *io_tree,
238 struct extent_buffer *eb, u64 parent_transid,
241 struct extent_state *cached_state = NULL;
244 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
250 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
252 if (extent_buffer_uptodate(eb) &&
253 btrfs_header_generation(eb) == parent_transid) {
257 btrfs_err_rl(eb->fs_info,
258 "parent transid verify failed on %llu wanted %llu found %llu",
260 parent_transid, btrfs_header_generation(eb));
262 clear_extent_buffer_uptodate(eb);
264 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
269 static bool btrfs_supported_super_csum(u16 csum_type)
272 case BTRFS_CSUM_TYPE_CRC32:
273 case BTRFS_CSUM_TYPE_XXHASH:
274 case BTRFS_CSUM_TYPE_SHA256:
275 case BTRFS_CSUM_TYPE_BLAKE2:
283 * Return 0 if the superblock checksum type matches the checksum value of that
284 * algorithm. Pass the raw disk superblock data.
286 static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
289 struct btrfs_super_block *disk_sb =
290 (struct btrfs_super_block *)raw_disk_sb;
291 char result[BTRFS_CSUM_SIZE];
292 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
294 shash->tfm = fs_info->csum_shash;
297 * The super_block structure does not span the whole
298 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
299 * filled with zeros and is included in the checksum.
301 crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE,
302 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
304 if (memcmp(disk_sb->csum, result, fs_info->csum_size))
310 int btrfs_verify_level_key(struct extent_buffer *eb, int level,
311 struct btrfs_key *first_key, u64 parent_transid)
313 struct btrfs_fs_info *fs_info = eb->fs_info;
315 struct btrfs_key found_key;
318 found_level = btrfs_header_level(eb);
319 if (found_level != level) {
320 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
321 KERN_ERR "BTRFS: tree level check failed\n");
323 "tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
324 eb->start, level, found_level);
332 * For live tree block (new tree blocks in current transaction),
333 * we need proper lock context to avoid race, which is impossible here.
334 * So we only checks tree blocks which is read from disk, whose
335 * generation <= fs_info->last_trans_committed.
337 if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
340 /* We have @first_key, so this @eb must have at least one item */
341 if (btrfs_header_nritems(eb) == 0) {
343 "invalid tree nritems, bytenr=%llu nritems=0 expect >0",
345 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
350 btrfs_node_key_to_cpu(eb, &found_key, 0);
352 btrfs_item_key_to_cpu(eb, &found_key, 0);
353 ret = btrfs_comp_cpu_keys(first_key, &found_key);
356 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
357 KERN_ERR "BTRFS: tree first key check failed\n");
359 "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
360 eb->start, parent_transid, first_key->objectid,
361 first_key->type, first_key->offset,
362 found_key.objectid, found_key.type,
369 * helper to read a given tree block, doing retries as required when
370 * the checksums don't match and we have alternate mirrors to try.
372 * @parent_transid: expected transid, skip check if 0
373 * @level: expected level, mandatory check
374 * @first_key: expected key of first slot, skip check if NULL
376 int btrfs_read_extent_buffer(struct extent_buffer *eb,
377 u64 parent_transid, int level,
378 struct btrfs_key *first_key)
380 struct btrfs_fs_info *fs_info = eb->fs_info;
381 struct extent_io_tree *io_tree;
386 int failed_mirror = 0;
388 io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
390 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
391 ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num);
393 if (verify_parent_transid(io_tree, eb,
396 else if (btrfs_verify_level_key(eb, level,
397 first_key, parent_transid))
403 num_copies = btrfs_num_copies(fs_info,
408 if (!failed_mirror) {
410 failed_mirror = eb->read_mirror;
414 if (mirror_num == failed_mirror)
417 if (mirror_num > num_copies)
421 if (failed && !ret && failed_mirror)
422 btrfs_repair_eb_io_failure(eb, failed_mirror);
427 static int csum_one_extent_buffer(struct extent_buffer *eb)
429 struct btrfs_fs_info *fs_info = eb->fs_info;
430 u8 result[BTRFS_CSUM_SIZE];
433 ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
434 offsetof(struct btrfs_header, fsid),
435 BTRFS_FSID_SIZE) == 0);
436 csum_tree_block(eb, result);
438 if (btrfs_header_level(eb))
439 ret = btrfs_check_node(eb);
441 ret = btrfs_check_leaf_full(eb);
447 * Also check the generation, the eb reached here must be newer than
448 * last committed. Or something seriously wrong happened.
450 if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) {
453 "block=%llu bad generation, have %llu expect > %llu",
454 eb->start, btrfs_header_generation(eb),
455 fs_info->last_trans_committed);
458 write_extent_buffer(eb, result, 0, fs_info->csum_size);
463 btrfs_print_tree(eb, 0);
464 btrfs_err(fs_info, "block=%llu write time tree block corruption detected",
466 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
470 /* Checksum all dirty extent buffers in one bio_vec */
471 static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info,
472 struct bio_vec *bvec)
474 struct page *page = bvec->bv_page;
475 u64 bvec_start = page_offset(page) + bvec->bv_offset;
479 for (cur = bvec_start; cur < bvec_start + bvec->bv_len;
480 cur += fs_info->nodesize) {
481 struct extent_buffer *eb;
484 eb = find_extent_buffer(fs_info, cur);
485 uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur,
488 /* A dirty eb shouldn't disappear from extent_buffers */
492 if (WARN_ON(cur != btrfs_header_bytenr(eb))) {
493 free_extent_buffer(eb);
496 if (WARN_ON(!uptodate)) {
497 free_extent_buffer(eb);
501 ret = csum_one_extent_buffer(eb);
502 free_extent_buffer(eb);
510 * Checksum a dirty tree block before IO. This has extra checks to make sure
511 * we only fill in the checksum field in the first page of a multi-page block.
512 * For subpage extent buffers we need bvec to also read the offset in the page.
514 static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec)
516 struct page *page = bvec->bv_page;
517 u64 start = page_offset(page);
519 struct extent_buffer *eb;
521 if (fs_info->nodesize < PAGE_SIZE)
522 return csum_dirty_subpage_buffers(fs_info, bvec);
524 eb = (struct extent_buffer *)page->private;
525 if (page != eb->pages[0])
528 found_start = btrfs_header_bytenr(eb);
530 if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) {
531 WARN_ON(found_start != 0);
536 * Please do not consolidate these warnings into a single if.
537 * It is useful to know what went wrong.
539 if (WARN_ON(found_start != start))
541 if (WARN_ON(!PageUptodate(page)))
544 return csum_one_extent_buffer(eb);
547 static int check_tree_block_fsid(struct extent_buffer *eb)
549 struct btrfs_fs_info *fs_info = eb->fs_info;
550 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
551 u8 fsid[BTRFS_FSID_SIZE];
554 read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid),
557 * Checking the incompat flag is only valid for the current fs. For
558 * seed devices it's forbidden to have their uuid changed so reading
559 * ->fsid in this case is fine
561 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
562 metadata_uuid = fs_devices->metadata_uuid;
564 metadata_uuid = fs_devices->fsid;
566 if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE))
569 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list)
570 if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE))
576 /* Do basic extent buffer checks at read time */
577 static int validate_extent_buffer(struct extent_buffer *eb)
579 struct btrfs_fs_info *fs_info = eb->fs_info;
581 const u32 csum_size = fs_info->csum_size;
583 u8 result[BTRFS_CSUM_SIZE];
584 const u8 *header_csum;
587 found_start = btrfs_header_bytenr(eb);
588 if (found_start != eb->start) {
589 btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu",
590 eb->start, found_start);
594 if (check_tree_block_fsid(eb)) {
595 btrfs_err_rl(fs_info, "bad fsid on block %llu",
600 found_level = btrfs_header_level(eb);
601 if (found_level >= BTRFS_MAX_LEVEL) {
602 btrfs_err(fs_info, "bad tree block level %d on %llu",
603 (int)btrfs_header_level(eb), eb->start);
608 csum_tree_block(eb, result);
609 header_csum = page_address(eb->pages[0]) +
610 get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum));
612 if (memcmp(result, header_csum, csum_size) != 0) {
613 btrfs_warn_rl(fs_info,
614 "checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d",
616 CSUM_FMT_VALUE(csum_size, header_csum),
617 CSUM_FMT_VALUE(csum_size, result),
618 btrfs_header_level(eb));
624 * If this is a leaf block and it is corrupt, set the corrupt bit so
625 * that we don't try and read the other copies of this block, just
628 if (found_level == 0 && btrfs_check_leaf_full(eb)) {
629 set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
633 if (found_level > 0 && btrfs_check_node(eb))
637 set_extent_buffer_uptodate(eb);
640 "block=%llu read time tree block corruption detected",
646 static int validate_subpage_buffer(struct page *page, u64 start, u64 end,
649 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
650 struct extent_buffer *eb;
655 * We don't allow bio merge for subpage metadata read, so we should
656 * only get one eb for each endio hook.
658 ASSERT(end == start + fs_info->nodesize - 1);
659 ASSERT(PagePrivate(page));
661 eb = find_extent_buffer(fs_info, start);
663 * When we are reading one tree block, eb must have been inserted into
664 * the radix tree. If not, something is wrong.
668 reads_done = atomic_dec_and_test(&eb->io_pages);
669 /* Subpage read must finish in page read */
672 eb->read_mirror = mirror;
673 if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
677 ret = validate_extent_buffer(eb);
681 set_extent_buffer_uptodate(eb);
683 free_extent_buffer(eb);
687 * end_bio_extent_readpage decrements io_pages in case of error,
688 * make sure it has something to decrement.
690 atomic_inc(&eb->io_pages);
691 clear_extent_buffer_uptodate(eb);
692 free_extent_buffer(eb);
696 int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio,
697 struct page *page, u64 start, u64 end,
700 struct extent_buffer *eb;
704 ASSERT(page->private);
706 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)
707 return validate_subpage_buffer(page, start, end, mirror);
709 eb = (struct extent_buffer *)page->private;
712 * The pending IO might have been the only thing that kept this buffer
713 * in memory. Make sure we have a ref for all this other checks
715 atomic_inc(&eb->refs);
717 reads_done = atomic_dec_and_test(&eb->io_pages);
721 eb->read_mirror = mirror;
722 if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
726 ret = validate_extent_buffer(eb);
730 * our io error hook is going to dec the io pages
731 * again, we have to make sure it has something
734 atomic_inc(&eb->io_pages);
735 clear_extent_buffer_uptodate(eb);
737 free_extent_buffer(eb);
742 static void end_workqueue_bio(struct bio *bio)
744 struct btrfs_end_io_wq *end_io_wq = bio->bi_private;
745 struct btrfs_fs_info *fs_info;
746 struct btrfs_workqueue *wq;
748 fs_info = end_io_wq->info;
749 end_io_wq->status = bio->bi_status;
751 if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
752 if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA)
753 wq = fs_info->endio_meta_write_workers;
754 else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE)
755 wq = fs_info->endio_freespace_worker;
756 else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56)
757 wq = fs_info->endio_raid56_workers;
759 wq = fs_info->endio_write_workers;
761 if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56)
762 wq = fs_info->endio_raid56_workers;
763 else if (end_io_wq->metadata)
764 wq = fs_info->endio_meta_workers;
766 wq = fs_info->endio_workers;
769 btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL);
770 btrfs_queue_work(wq, &end_io_wq->work);
773 blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
774 enum btrfs_wq_endio_type metadata)
776 struct btrfs_end_io_wq *end_io_wq;
778 end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS);
780 return BLK_STS_RESOURCE;
782 end_io_wq->private = bio->bi_private;
783 end_io_wq->end_io = bio->bi_end_io;
784 end_io_wq->info = info;
785 end_io_wq->status = 0;
786 end_io_wq->bio = bio;
787 end_io_wq->metadata = metadata;
789 bio->bi_private = end_io_wq;
790 bio->bi_end_io = end_workqueue_bio;
794 static void run_one_async_start(struct btrfs_work *work)
796 struct async_submit_bio *async;
799 async = container_of(work, struct async_submit_bio, work);
800 ret = async->submit_bio_start(async->inode, async->bio,
801 async->dio_file_offset);
807 * In order to insert checksums into the metadata in large chunks, we wait
808 * until bio submission time. All the pages in the bio are checksummed and
809 * sums are attached onto the ordered extent record.
811 * At IO completion time the csums attached on the ordered extent record are
812 * inserted into the tree.
814 static void run_one_async_done(struct btrfs_work *work)
816 struct async_submit_bio *async;
820 async = container_of(work, struct async_submit_bio, work);
821 inode = async->inode;
823 /* If an error occurred we just want to clean up the bio and move on */
825 async->bio->bi_status = async->status;
826 bio_endio(async->bio);
831 * All of the bios that pass through here are from async helpers.
832 * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context.
833 * This changes nothing when cgroups aren't in use.
835 async->bio->bi_opf |= REQ_CGROUP_PUNT;
836 ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num);
838 async->bio->bi_status = ret;
839 bio_endio(async->bio);
843 static void run_one_async_free(struct btrfs_work *work)
845 struct async_submit_bio *async;
847 async = container_of(work, struct async_submit_bio, work);
851 blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio,
852 int mirror_num, u64 dio_file_offset,
853 extent_submit_bio_start_t *submit_bio_start)
855 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
856 struct async_submit_bio *async;
858 async = kmalloc(sizeof(*async), GFP_NOFS);
860 return BLK_STS_RESOURCE;
862 async->inode = inode;
864 async->mirror_num = mirror_num;
865 async->submit_bio_start = submit_bio_start;
867 btrfs_init_work(&async->work, run_one_async_start, run_one_async_done,
870 async->dio_file_offset = dio_file_offset;
874 if (op_is_sync(bio->bi_opf))
875 btrfs_queue_work(fs_info->hipri_workers, &async->work);
877 btrfs_queue_work(fs_info->workers, &async->work);
881 static blk_status_t btree_csum_one_bio(struct bio *bio)
883 struct bio_vec *bvec;
884 struct btrfs_root *root;
886 struct bvec_iter_all iter_all;
888 ASSERT(!bio_flagged(bio, BIO_CLONED));
889 bio_for_each_segment_all(bvec, bio, iter_all) {
890 root = BTRFS_I(bvec->bv_page->mapping->host)->root;
891 ret = csum_dirty_buffer(root->fs_info, bvec);
896 return errno_to_blk_status(ret);
899 static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio,
903 * when we're called for a write, we're already in the async
904 * submission context. Just jump into btrfs_map_bio
906 return btree_csum_one_bio(bio);
909 static bool should_async_write(struct btrfs_fs_info *fs_info,
910 struct btrfs_inode *bi)
912 if (btrfs_is_zoned(fs_info))
914 if (atomic_read(&bi->sync_writers))
916 if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags))
921 void btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, int mirror_num)
923 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
926 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
928 * called for a read, do the setup so that checksum validation
929 * can happen in the async kernel threads
931 ret = btrfs_bio_wq_end_io(fs_info, bio,
932 BTRFS_WQ_ENDIO_METADATA);
934 ret = btrfs_map_bio(fs_info, bio, mirror_num);
935 } else if (!should_async_write(fs_info, BTRFS_I(inode))) {
936 ret = btree_csum_one_bio(bio);
938 ret = btrfs_map_bio(fs_info, bio, mirror_num);
941 * kthread helpers are used to submit writes so that
942 * checksumming can happen in parallel across all CPUs
944 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0,
945 btree_submit_bio_start);
949 bio->bi_status = ret;
954 #ifdef CONFIG_MIGRATION
955 static int btree_migratepage(struct address_space *mapping,
956 struct page *newpage, struct page *page,
957 enum migrate_mode mode)
960 * we can't safely write a btree page from here,
961 * we haven't done the locking hook
966 * Buffers may be managed in a filesystem specific way.
967 * We must have no buffers or drop them.
969 if (page_has_private(page) &&
970 !try_to_release_page(page, GFP_KERNEL))
972 return migrate_page(mapping, newpage, page, mode);
977 static int btree_writepages(struct address_space *mapping,
978 struct writeback_control *wbc)
980 struct btrfs_fs_info *fs_info;
983 if (wbc->sync_mode == WB_SYNC_NONE) {
985 if (wbc->for_kupdate)
988 fs_info = BTRFS_I(mapping->host)->root->fs_info;
989 /* this is a bit racy, but that's ok */
990 ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
991 BTRFS_DIRTY_METADATA_THRESH,
992 fs_info->dirty_metadata_batch);
996 return btree_write_cache_pages(mapping, wbc);
999 static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags)
1001 if (folio_test_writeback(folio) || folio_test_dirty(folio))
1004 return try_release_extent_buffer(&folio->page);
1007 static void btree_invalidate_folio(struct folio *folio, size_t offset,
1010 struct extent_io_tree *tree;
1011 tree = &BTRFS_I(folio->mapping->host)->io_tree;
1012 extent_invalidate_folio(tree, folio, offset);
1013 btree_release_folio(folio, GFP_NOFS);
1014 if (folio_get_private(folio)) {
1015 btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info,
1016 "folio private not zero on folio %llu",
1017 (unsigned long long)folio_pos(folio));
1018 folio_detach_private(folio);
1023 static bool btree_dirty_folio(struct address_space *mapping,
1024 struct folio *folio)
1026 struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb);
1027 struct btrfs_subpage *subpage;
1028 struct extent_buffer *eb;
1030 u64 page_start = folio_pos(folio);
1032 if (fs_info->sectorsize == PAGE_SIZE) {
1033 eb = folio_get_private(folio);
1035 BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
1036 BUG_ON(!atomic_read(&eb->refs));
1037 btrfs_assert_tree_write_locked(eb);
1038 return filemap_dirty_folio(mapping, folio);
1040 subpage = folio_get_private(folio);
1042 ASSERT(subpage->dirty_bitmap);
1043 while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) {
1044 unsigned long flags;
1046 u16 tmp = (1 << cur_bit);
1048 spin_lock_irqsave(&subpage->lock, flags);
1049 if (!(tmp & subpage->dirty_bitmap)) {
1050 spin_unlock_irqrestore(&subpage->lock, flags);
1054 spin_unlock_irqrestore(&subpage->lock, flags);
1055 cur = page_start + cur_bit * fs_info->sectorsize;
1057 eb = find_extent_buffer(fs_info, cur);
1059 ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
1060 ASSERT(atomic_read(&eb->refs));
1061 btrfs_assert_tree_write_locked(eb);
1062 free_extent_buffer(eb);
1064 cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits);
1066 return filemap_dirty_folio(mapping, folio);
1069 #define btree_dirty_folio filemap_dirty_folio
1072 static const struct address_space_operations btree_aops = {
1073 .writepages = btree_writepages,
1074 .release_folio = btree_release_folio,
1075 .invalidate_folio = btree_invalidate_folio,
1076 #ifdef CONFIG_MIGRATION
1077 .migratepage = btree_migratepage,
1079 .dirty_folio = btree_dirty_folio,
1082 struct extent_buffer *btrfs_find_create_tree_block(
1083 struct btrfs_fs_info *fs_info,
1084 u64 bytenr, u64 owner_root,
1087 if (btrfs_is_testing(fs_info))
1088 return alloc_test_extent_buffer(fs_info, bytenr);
1089 return alloc_extent_buffer(fs_info, bytenr, owner_root, level);
1093 * Read tree block at logical address @bytenr and do variant basic but critical
1096 * @owner_root: the objectid of the root owner for this block.
1097 * @parent_transid: expected transid of this tree block, skip check if 0
1098 * @level: expected level, mandatory check
1099 * @first_key: expected key in slot 0, skip check if NULL
1101 struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
1102 u64 owner_root, u64 parent_transid,
1103 int level, struct btrfs_key *first_key)
1105 struct extent_buffer *buf = NULL;
1108 buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
1112 ret = btrfs_read_extent_buffer(buf, parent_transid, level, first_key);
1114 free_extent_buffer_stale(buf);
1115 return ERR_PTR(ret);
1117 if (btrfs_check_eb_owner(buf, owner_root)) {
1118 free_extent_buffer_stale(buf);
1119 return ERR_PTR(-EUCLEAN);
1125 void btrfs_clean_tree_block(struct extent_buffer *buf)
1127 struct btrfs_fs_info *fs_info = buf->fs_info;
1128 if (btrfs_header_generation(buf) ==
1129 fs_info->running_transaction->transid) {
1130 btrfs_assert_tree_write_locked(buf);
1132 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
1133 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
1135 fs_info->dirty_metadata_batch);
1136 clear_extent_buffer_dirty(buf);
1141 static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
1144 bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
1146 memset(&root->root_key, 0, sizeof(root->root_key));
1147 memset(&root->root_item, 0, sizeof(root->root_item));
1148 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
1149 root->fs_info = fs_info;
1150 root->root_key.objectid = objectid;
1152 root->commit_root = NULL;
1154 RB_CLEAR_NODE(&root->rb_node);
1156 root->last_trans = 0;
1157 root->free_objectid = 0;
1158 root->nr_delalloc_inodes = 0;
1159 root->nr_ordered_extents = 0;
1160 root->inode_tree = RB_ROOT;
1161 xa_init_flags(&root->delayed_nodes, GFP_ATOMIC);
1163 btrfs_init_root_block_rsv(root);
1165 INIT_LIST_HEAD(&root->dirty_list);
1166 INIT_LIST_HEAD(&root->root_list);
1167 INIT_LIST_HEAD(&root->delalloc_inodes);
1168 INIT_LIST_HEAD(&root->delalloc_root);
1169 INIT_LIST_HEAD(&root->ordered_extents);
1170 INIT_LIST_HEAD(&root->ordered_root);
1171 INIT_LIST_HEAD(&root->reloc_dirty_list);
1172 INIT_LIST_HEAD(&root->logged_list[0]);
1173 INIT_LIST_HEAD(&root->logged_list[1]);
1174 spin_lock_init(&root->inode_lock);
1175 spin_lock_init(&root->delalloc_lock);
1176 spin_lock_init(&root->ordered_extent_lock);
1177 spin_lock_init(&root->accounting_lock);
1178 spin_lock_init(&root->log_extents_lock[0]);
1179 spin_lock_init(&root->log_extents_lock[1]);
1180 spin_lock_init(&root->qgroup_meta_rsv_lock);
1181 mutex_init(&root->objectid_mutex);
1182 mutex_init(&root->log_mutex);
1183 mutex_init(&root->ordered_extent_mutex);
1184 mutex_init(&root->delalloc_mutex);
1185 init_waitqueue_head(&root->qgroup_flush_wait);
1186 init_waitqueue_head(&root->log_writer_wait);
1187 init_waitqueue_head(&root->log_commit_wait[0]);
1188 init_waitqueue_head(&root->log_commit_wait[1]);
1189 INIT_LIST_HEAD(&root->log_ctxs[0]);
1190 INIT_LIST_HEAD(&root->log_ctxs[1]);
1191 atomic_set(&root->log_commit[0], 0);
1192 atomic_set(&root->log_commit[1], 0);
1193 atomic_set(&root->log_writers, 0);
1194 atomic_set(&root->log_batch, 0);
1195 refcount_set(&root->refs, 1);
1196 atomic_set(&root->snapshot_force_cow, 0);
1197 atomic_set(&root->nr_swapfiles, 0);
1198 root->log_transid = 0;
1199 root->log_transid_committed = -1;
1200 root->last_log_commit = 0;
1203 extent_io_tree_init(fs_info, &root->dirty_log_pages,
1204 IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL);
1205 extent_io_tree_init(fs_info, &root->log_csum_range,
1206 IO_TREE_LOG_CSUM_RANGE, NULL);
1209 spin_lock_init(&root->root_item_lock);
1210 btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
1211 #ifdef CONFIG_BTRFS_DEBUG
1212 INIT_LIST_HEAD(&root->leak_list);
1213 spin_lock(&fs_info->fs_roots_lock);
1214 list_add_tail(&root->leak_list, &fs_info->allocated_roots);
1215 spin_unlock(&fs_info->fs_roots_lock);
1219 static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
1220 u64 objectid, gfp_t flags)
1222 struct btrfs_root *root = kzalloc(sizeof(*root), flags);
1224 __setup_root(root, fs_info, objectid);
1228 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1229 /* Should only be used by the testing infrastructure */
1230 struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
1232 struct btrfs_root *root;
1235 return ERR_PTR(-EINVAL);
1237 root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
1239 return ERR_PTR(-ENOMEM);
1241 /* We don't use the stripesize in selftest, set it as sectorsize */
1242 root->alloc_bytenr = 0;
1248 static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node)
1250 const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node);
1251 const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node);
1253 return btrfs_comp_cpu_keys(&a->root_key, &b->root_key);
1256 static int global_root_key_cmp(const void *k, const struct rb_node *node)
1258 const struct btrfs_key *key = k;
1259 const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node);
1261 return btrfs_comp_cpu_keys(key, &root->root_key);
1264 int btrfs_global_root_insert(struct btrfs_root *root)
1266 struct btrfs_fs_info *fs_info = root->fs_info;
1267 struct rb_node *tmp;
1269 write_lock(&fs_info->global_root_lock);
1270 tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp);
1271 write_unlock(&fs_info->global_root_lock);
1274 return tmp ? -EEXIST : 0;
1277 void btrfs_global_root_delete(struct btrfs_root *root)
1279 struct btrfs_fs_info *fs_info = root->fs_info;
1281 write_lock(&fs_info->global_root_lock);
1282 rb_erase(&root->rb_node, &fs_info->global_root_tree);
1283 write_unlock(&fs_info->global_root_lock);
1286 struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
1287 struct btrfs_key *key)
1289 struct rb_node *node;
1290 struct btrfs_root *root = NULL;
1292 read_lock(&fs_info->global_root_lock);
1293 node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp);
1295 root = container_of(node, struct btrfs_root, rb_node);
1296 read_unlock(&fs_info->global_root_lock);
1301 static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
1303 struct btrfs_block_group *block_group;
1306 if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
1310 block_group = btrfs_lookup_block_group(fs_info, bytenr);
1312 block_group = btrfs_lookup_first_block_group(fs_info, bytenr);
1313 ASSERT(block_group);
1316 ret = block_group->global_root_id;
1317 btrfs_put_block_group(block_group);
1322 struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr)
1324 struct btrfs_key key = {
1325 .objectid = BTRFS_CSUM_TREE_OBJECTID,
1326 .type = BTRFS_ROOT_ITEM_KEY,
1327 .offset = btrfs_global_root_id(fs_info, bytenr),
1330 return btrfs_global_root(fs_info, &key);
1333 struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr)
1335 struct btrfs_key key = {
1336 .objectid = BTRFS_EXTENT_TREE_OBJECTID,
1337 .type = BTRFS_ROOT_ITEM_KEY,
1338 .offset = btrfs_global_root_id(fs_info, bytenr),
1341 return btrfs_global_root(fs_info, &key);
1344 struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
1347 struct btrfs_fs_info *fs_info = trans->fs_info;
1348 struct extent_buffer *leaf;
1349 struct btrfs_root *tree_root = fs_info->tree_root;
1350 struct btrfs_root *root;
1351 struct btrfs_key key;
1352 unsigned int nofs_flag;
1356 * We're holding a transaction handle, so use a NOFS memory allocation
1357 * context to avoid deadlock if reclaim happens.
1359 nofs_flag = memalloc_nofs_save();
1360 root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL);
1361 memalloc_nofs_restore(nofs_flag);
1363 return ERR_PTR(-ENOMEM);
1365 root->root_key.objectid = objectid;
1366 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1367 root->root_key.offset = 0;
1369 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
1370 BTRFS_NESTING_NORMAL);
1372 ret = PTR_ERR(leaf);
1378 btrfs_mark_buffer_dirty(leaf);
1380 root->commit_root = btrfs_root_node(root);
1381 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
1383 btrfs_set_root_flags(&root->root_item, 0);
1384 btrfs_set_root_limit(&root->root_item, 0);
1385 btrfs_set_root_bytenr(&root->root_item, leaf->start);
1386 btrfs_set_root_generation(&root->root_item, trans->transid);
1387 btrfs_set_root_level(&root->root_item, 0);
1388 btrfs_set_root_refs(&root->root_item, 1);
1389 btrfs_set_root_used(&root->root_item, leaf->len);
1390 btrfs_set_root_last_snapshot(&root->root_item, 0);
1391 btrfs_set_root_dirid(&root->root_item, 0);
1392 if (is_fstree(objectid))
1393 generate_random_guid(root->root_item.uuid);
1395 export_guid(root->root_item.uuid, &guid_null);
1396 btrfs_set_root_drop_level(&root->root_item, 0);
1398 btrfs_tree_unlock(leaf);
1400 key.objectid = objectid;
1401 key.type = BTRFS_ROOT_ITEM_KEY;
1403 ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
1411 btrfs_tree_unlock(leaf);
1413 btrfs_put_root(root);
1415 return ERR_PTR(ret);
1418 static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1419 struct btrfs_fs_info *fs_info)
1421 struct btrfs_root *root;
1423 root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS);
1425 return ERR_PTR(-ENOMEM);
1427 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
1428 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1429 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
1434 int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
1435 struct btrfs_root *root)
1437 struct extent_buffer *leaf;
1440 * DON'T set SHAREABLE bit for log trees.
1442 * Log trees are not exposed to user space thus can't be snapshotted,
1443 * and they go away before a real commit is actually done.
1445 * They do store pointers to file data extents, and those reference
1446 * counts still get updated (along with back refs to the log tree).
1449 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
1450 NULL, 0, 0, 0, BTRFS_NESTING_NORMAL);
1452 return PTR_ERR(leaf);
1456 btrfs_mark_buffer_dirty(root->node);
1457 btrfs_tree_unlock(root->node);
1462 int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1463 struct btrfs_fs_info *fs_info)
1465 struct btrfs_root *log_root;
1467 log_root = alloc_log_tree(trans, fs_info);
1468 if (IS_ERR(log_root))
1469 return PTR_ERR(log_root);
1471 if (!btrfs_is_zoned(fs_info)) {
1472 int ret = btrfs_alloc_log_tree_node(trans, log_root);
1475 btrfs_put_root(log_root);
1480 WARN_ON(fs_info->log_root_tree);
1481 fs_info->log_root_tree = log_root;
1485 int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1486 struct btrfs_root *root)
1488 struct btrfs_fs_info *fs_info = root->fs_info;
1489 struct btrfs_root *log_root;
1490 struct btrfs_inode_item *inode_item;
1493 log_root = alloc_log_tree(trans, fs_info);
1494 if (IS_ERR(log_root))
1495 return PTR_ERR(log_root);
1497 ret = btrfs_alloc_log_tree_node(trans, log_root);
1499 btrfs_put_root(log_root);
1503 log_root->last_trans = trans->transid;
1504 log_root->root_key.offset = root->root_key.objectid;
1506 inode_item = &log_root->root_item.inode;
1507 btrfs_set_stack_inode_generation(inode_item, 1);
1508 btrfs_set_stack_inode_size(inode_item, 3);
1509 btrfs_set_stack_inode_nlink(inode_item, 1);
1510 btrfs_set_stack_inode_nbytes(inode_item,
1512 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
1514 btrfs_set_root_node(&log_root->root_item, log_root->node);
1516 WARN_ON(root->log_root);
1517 root->log_root = log_root;
1518 root->log_transid = 0;
1519 root->log_transid_committed = -1;
1520 root->last_log_commit = 0;
1524 static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
1525 struct btrfs_path *path,
1526 struct btrfs_key *key)
1528 struct btrfs_root *root;
1529 struct btrfs_fs_info *fs_info = tree_root->fs_info;
1534 root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
1536 return ERR_PTR(-ENOMEM);
1538 ret = btrfs_find_root(tree_root, key, path,
1539 &root->root_item, &root->root_key);
1546 generation = btrfs_root_generation(&root->root_item);
1547 level = btrfs_root_level(&root->root_item);
1548 root->node = read_tree_block(fs_info,
1549 btrfs_root_bytenr(&root->root_item),
1550 key->objectid, generation, level, NULL);
1551 if (IS_ERR(root->node)) {
1552 ret = PTR_ERR(root->node);
1556 if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
1562 * For real fs, and not log/reloc trees, root owner must
1563 * match its root node owner
1565 if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) &&
1566 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID &&
1567 root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1568 root->root_key.objectid != btrfs_header_owner(root->node)) {
1570 "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
1571 root->root_key.objectid, root->node->start,
1572 btrfs_header_owner(root->node),
1573 root->root_key.objectid);
1577 root->commit_root = btrfs_root_node(root);
1580 btrfs_put_root(root);
1581 return ERR_PTR(ret);
1584 struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
1585 struct btrfs_key *key)
1587 struct btrfs_root *root;
1588 struct btrfs_path *path;
1590 path = btrfs_alloc_path();
1592 return ERR_PTR(-ENOMEM);
1593 root = read_tree_root_path(tree_root, path, key);
1594 btrfs_free_path(path);
1600 * Initialize subvolume root in-memory structure
1602 * @anon_dev: anonymous device to attach to the root, if zero, allocate new
1604 static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev)
1607 unsigned int nofs_flag;
1610 * We might be called under a transaction (e.g. indirect backref
1611 * resolution) which could deadlock if it triggers memory reclaim
1613 nofs_flag = memalloc_nofs_save();
1614 ret = btrfs_drew_lock_init(&root->snapshot_lock);
1615 memalloc_nofs_restore(nofs_flag);
1619 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID &&
1620 !btrfs_is_data_reloc_root(root)) {
1621 set_bit(BTRFS_ROOT_SHAREABLE, &root->state);
1622 btrfs_check_and_init_root_item(&root->root_item);
1626 * Don't assign anonymous block device to roots that are not exposed to
1627 * userspace, the id pool is limited to 1M
1629 if (is_fstree(root->root_key.objectid) &&
1630 btrfs_root_refs(&root->root_item) > 0) {
1632 ret = get_anon_bdev(&root->anon_dev);
1636 root->anon_dev = anon_dev;
1640 mutex_lock(&root->objectid_mutex);
1641 ret = btrfs_init_root_free_objectid(root);
1643 mutex_unlock(&root->objectid_mutex);
1647 ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
1649 mutex_unlock(&root->objectid_mutex);
1653 /* The caller is responsible to call btrfs_free_fs_root */
1657 static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1660 struct btrfs_root *root;
1662 spin_lock(&fs_info->fs_roots_lock);
1663 root = xa_load(&fs_info->fs_roots, (unsigned long)root_id);
1665 root = btrfs_grab_root(root);
1666 spin_unlock(&fs_info->fs_roots_lock);
1670 static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info,
1673 struct btrfs_key key = {
1674 .objectid = objectid,
1675 .type = BTRFS_ROOT_ITEM_KEY,
1679 if (objectid == BTRFS_ROOT_TREE_OBJECTID)
1680 return btrfs_grab_root(fs_info->tree_root);
1681 if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
1682 return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1683 if (objectid == BTRFS_CHUNK_TREE_OBJECTID)
1684 return btrfs_grab_root(fs_info->chunk_root);
1685 if (objectid == BTRFS_DEV_TREE_OBJECTID)
1686 return btrfs_grab_root(fs_info->dev_root);
1687 if (objectid == BTRFS_CSUM_TREE_OBJECTID)
1688 return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1689 if (objectid == BTRFS_QUOTA_TREE_OBJECTID)
1690 return btrfs_grab_root(fs_info->quota_root) ?
1691 fs_info->quota_root : ERR_PTR(-ENOENT);
1692 if (objectid == BTRFS_UUID_TREE_OBJECTID)
1693 return btrfs_grab_root(fs_info->uuid_root) ?
1694 fs_info->uuid_root : ERR_PTR(-ENOENT);
1695 if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) {
1696 struct btrfs_root *root = btrfs_global_root(fs_info, &key);
1698 return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT);
1703 int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1704 struct btrfs_root *root)
1708 spin_lock(&fs_info->fs_roots_lock);
1709 ret = xa_insert(&fs_info->fs_roots, (unsigned long)root->root_key.objectid,
1712 btrfs_grab_root(root);
1713 set_bit(BTRFS_ROOT_REGISTERED, &root->state);
1715 spin_unlock(&fs_info->fs_roots_lock);
1720 void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
1722 #ifdef CONFIG_BTRFS_DEBUG
1723 struct btrfs_root *root;
1725 while (!list_empty(&fs_info->allocated_roots)) {
1726 char buf[BTRFS_ROOT_NAME_BUF_LEN];
1728 root = list_first_entry(&fs_info->allocated_roots,
1729 struct btrfs_root, leak_list);
1730 btrfs_err(fs_info, "leaked root %s refcount %d",
1731 btrfs_root_name(&root->root_key, buf),
1732 refcount_read(&root->refs));
1733 while (refcount_read(&root->refs) > 1)
1734 btrfs_put_root(root);
1735 btrfs_put_root(root);
1740 static void free_global_roots(struct btrfs_fs_info *fs_info)
1742 struct btrfs_root *root;
1743 struct rb_node *node;
1745 while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) {
1746 root = rb_entry(node, struct btrfs_root, rb_node);
1747 rb_erase(&root->rb_node, &fs_info->global_root_tree);
1748 btrfs_put_root(root);
1752 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
1754 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
1755 percpu_counter_destroy(&fs_info->delalloc_bytes);
1756 percpu_counter_destroy(&fs_info->ordered_bytes);
1757 percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
1758 btrfs_free_csum_hash(fs_info);
1759 btrfs_free_stripe_hash_table(fs_info);
1760 btrfs_free_ref_cache(fs_info);
1761 kfree(fs_info->balance_ctl);
1762 kfree(fs_info->delayed_root);
1763 free_global_roots(fs_info);
1764 btrfs_put_root(fs_info->tree_root);
1765 btrfs_put_root(fs_info->chunk_root);
1766 btrfs_put_root(fs_info->dev_root);
1767 btrfs_put_root(fs_info->quota_root);
1768 btrfs_put_root(fs_info->uuid_root);
1769 btrfs_put_root(fs_info->fs_root);
1770 btrfs_put_root(fs_info->data_reloc_root);
1771 btrfs_put_root(fs_info->block_group_root);
1772 btrfs_check_leaked_roots(fs_info);
1773 btrfs_extent_buffer_leak_debug_check(fs_info);
1774 kfree(fs_info->super_copy);
1775 kfree(fs_info->super_for_commit);
1776 kfree(fs_info->subpage_info);
1782 * Get an in-memory reference of a root structure.
1784 * For essential trees like root/extent tree, we grab it from fs_info directly.
1785 * For subvolume trees, we check the cached filesystem roots first. If not
1786 * found, then read it from disk and add it to cached fs roots.
1788 * Caller should release the root by calling btrfs_put_root() after the usage.
1790 * NOTE: Reloc and log trees can't be read by this function as they share the
1791 * same root objectid.
1793 * @objectid: root id
1794 * @anon_dev: preallocated anonymous block device number for new roots,
1795 * pass 0 for new allocation.
1796 * @check_ref: whether to check root item references, If true, return -ENOENT
1799 static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
1800 u64 objectid, dev_t anon_dev,
1803 struct btrfs_root *root;
1804 struct btrfs_path *path;
1805 struct btrfs_key key;
1808 root = btrfs_get_global_root(fs_info, objectid);
1812 root = btrfs_lookup_fs_root(fs_info, objectid);
1814 /* Shouldn't get preallocated anon_dev for cached roots */
1816 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1817 btrfs_put_root(root);
1818 return ERR_PTR(-ENOENT);
1823 key.objectid = objectid;
1824 key.type = BTRFS_ROOT_ITEM_KEY;
1825 key.offset = (u64)-1;
1826 root = btrfs_read_tree_root(fs_info->tree_root, &key);
1830 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1835 ret = btrfs_init_fs_root(root, anon_dev);
1839 path = btrfs_alloc_path();
1844 key.objectid = BTRFS_ORPHAN_OBJECTID;
1845 key.type = BTRFS_ORPHAN_ITEM_KEY;
1846 key.offset = objectid;
1848 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1849 btrfs_free_path(path);
1853 set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
1855 ret = btrfs_insert_fs_root(fs_info, root);
1857 if (ret == -EEXIST) {
1858 btrfs_put_root(root);
1866 * If our caller provided us an anonymous device, then it's his
1867 * responsability to free it in case we fail. So we have to set our
1868 * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root()
1869 * and once again by our caller.
1873 btrfs_put_root(root);
1874 return ERR_PTR(ret);
1878 * Get in-memory reference of a root structure
1880 * @objectid: tree objectid
1881 * @check_ref: if set, verify that the tree exists and the item has at least
1884 struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1885 u64 objectid, bool check_ref)
1887 return btrfs_get_root_ref(fs_info, objectid, 0, check_ref);
1891 * Get in-memory reference of a root structure, created as new, optionally pass
1892 * the anonymous block device id
1894 * @objectid: tree objectid
1895 * @anon_dev: if zero, allocate a new anonymous block device or use the
1898 struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info,
1899 u64 objectid, dev_t anon_dev)
1901 return btrfs_get_root_ref(fs_info, objectid, anon_dev, true);
1905 * btrfs_get_fs_root_commit_root - return a root for the given objectid
1906 * @fs_info: the fs_info
1907 * @objectid: the objectid we need to lookup
1909 * This is exclusively used for backref walking, and exists specifically because
1910 * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref
1911 * creation time, which means we may have to read the tree_root in order to look
1912 * up a fs root that is not in memory. If the root is not in memory we will
1913 * read the tree root commit root and look up the fs root from there. This is a
1914 * temporary root, it will not be inserted into the radix tree as it doesn't
1915 * have the most uptodate information, it'll simply be discarded once the
1916 * backref code is finished using the root.
1918 struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info,
1919 struct btrfs_path *path,
1922 struct btrfs_root *root;
1923 struct btrfs_key key;
1925 ASSERT(path->search_commit_root && path->skip_locking);
1928 * This can return -ENOENT if we ask for a root that doesn't exist, but
1929 * since this is called via the backref walking code we won't be looking
1930 * up a root that doesn't exist, unless there's corruption. So if root
1931 * != NULL just return it.
1933 root = btrfs_get_global_root(fs_info, objectid);
1937 root = btrfs_lookup_fs_root(fs_info, objectid);
1941 key.objectid = objectid;
1942 key.type = BTRFS_ROOT_ITEM_KEY;
1943 key.offset = (u64)-1;
1944 root = read_tree_root_path(fs_info->tree_root, path, &key);
1945 btrfs_release_path(path);
1951 * called by the kthread helper functions to finally call the bio end_io
1952 * functions. This is where read checksum verification actually happens
1954 static void end_workqueue_fn(struct btrfs_work *work)
1957 struct btrfs_end_io_wq *end_io_wq;
1959 end_io_wq = container_of(work, struct btrfs_end_io_wq, work);
1960 bio = end_io_wq->bio;
1962 bio->bi_status = end_io_wq->status;
1963 bio->bi_private = end_io_wq->private;
1964 bio->bi_end_io = end_io_wq->end_io;
1966 kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
1969 static int cleaner_kthread(void *arg)
1971 struct btrfs_fs_info *fs_info = arg;
1977 set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1979 /* Make the cleaner go to sleep early. */
1980 if (btrfs_need_cleaner_sleep(fs_info))
1984 * Do not do anything if we might cause open_ctree() to block
1985 * before we have finished mounting the filesystem.
1987 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1990 if (!mutex_trylock(&fs_info->cleaner_mutex))
1994 * Avoid the problem that we change the status of the fs
1995 * during the above check and trylock.
1997 if (btrfs_need_cleaner_sleep(fs_info)) {
1998 mutex_unlock(&fs_info->cleaner_mutex);
2002 btrfs_run_delayed_iputs(fs_info);
2004 again = btrfs_clean_one_deleted_snapshot(fs_info);
2005 mutex_unlock(&fs_info->cleaner_mutex);
2008 * The defragger has dealt with the R/O remount and umount,
2009 * needn't do anything special here.
2011 btrfs_run_defrag_inodes(fs_info);
2014 * Acquires fs_info->reclaim_bgs_lock to avoid racing
2015 * with relocation (btrfs_relocate_chunk) and relocation
2016 * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
2017 * after acquiring fs_info->reclaim_bgs_lock. So we
2018 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
2019 * unused block groups.
2021 btrfs_delete_unused_bgs(fs_info);
2024 * Reclaim block groups in the reclaim_bgs list after we deleted
2025 * all unused block_groups. This possibly gives us some more free
2028 btrfs_reclaim_bgs(fs_info);
2030 clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
2031 if (kthread_should_park())
2033 if (kthread_should_stop())
2036 set_current_state(TASK_INTERRUPTIBLE);
2038 __set_current_state(TASK_RUNNING);
2043 static int transaction_kthread(void *arg)
2045 struct btrfs_root *root = arg;
2046 struct btrfs_fs_info *fs_info = root->fs_info;
2047 struct btrfs_trans_handle *trans;
2048 struct btrfs_transaction *cur;
2051 unsigned long delay;
2055 cannot_commit = false;
2056 delay = msecs_to_jiffies(fs_info->commit_interval * 1000);
2057 mutex_lock(&fs_info->transaction_kthread_mutex);
2059 spin_lock(&fs_info->trans_lock);
2060 cur = fs_info->running_transaction;
2062 spin_unlock(&fs_info->trans_lock);
2066 delta = ktime_get_seconds() - cur->start_time;
2067 if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) &&
2068 cur->state < TRANS_STATE_COMMIT_START &&
2069 delta < fs_info->commit_interval) {
2070 spin_unlock(&fs_info->trans_lock);
2071 delay -= msecs_to_jiffies((delta - 1) * 1000);
2073 msecs_to_jiffies(fs_info->commit_interval * 1000));
2076 transid = cur->transid;
2077 spin_unlock(&fs_info->trans_lock);
2079 /* If the file system is aborted, this will always fail. */
2080 trans = btrfs_attach_transaction(root);
2081 if (IS_ERR(trans)) {
2082 if (PTR_ERR(trans) != -ENOENT)
2083 cannot_commit = true;
2086 if (transid == trans->transid) {
2087 btrfs_commit_transaction(trans);
2089 btrfs_end_transaction(trans);
2092 wake_up_process(fs_info->cleaner_kthread);
2093 mutex_unlock(&fs_info->transaction_kthread_mutex);
2095 if (BTRFS_FS_ERROR(fs_info))
2096 btrfs_cleanup_transaction(fs_info);
2097 if (!kthread_should_stop() &&
2098 (!btrfs_transaction_blocked(fs_info) ||
2100 schedule_timeout_interruptible(delay);
2101 } while (!kthread_should_stop());
2106 * This will find the highest generation in the array of root backups. The
2107 * index of the highest array is returned, or -EINVAL if we can't find
2110 * We check to make sure the array is valid by comparing the
2111 * generation of the latest root in the array with the generation
2112 * in the super block. If they don't match we pitch it.
2114 static int find_newest_super_backup(struct btrfs_fs_info *info)
2116 const u64 newest_gen = btrfs_super_generation(info->super_copy);
2118 struct btrfs_root_backup *root_backup;
2121 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
2122 root_backup = info->super_copy->super_roots + i;
2123 cur = btrfs_backup_tree_root_gen(root_backup);
2124 if (cur == newest_gen)
2132 * copy all the root pointers into the super backup array.
2133 * this will bump the backup pointer by one when it is
2136 static void backup_super_roots(struct btrfs_fs_info *info)
2138 const int next_backup = info->backup_root_index;
2139 struct btrfs_root_backup *root_backup;
2141 root_backup = info->super_for_commit->super_roots + next_backup;
2144 * make sure all of our padding and empty slots get zero filled
2145 * regardless of which ones we use today
2147 memset(root_backup, 0, sizeof(*root_backup));
2149 info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
2151 btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
2152 btrfs_set_backup_tree_root_gen(root_backup,
2153 btrfs_header_generation(info->tree_root->node));
2155 btrfs_set_backup_tree_root_level(root_backup,
2156 btrfs_header_level(info->tree_root->node));
2158 btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
2159 btrfs_set_backup_chunk_root_gen(root_backup,
2160 btrfs_header_generation(info->chunk_root->node));
2161 btrfs_set_backup_chunk_root_level(root_backup,
2162 btrfs_header_level(info->chunk_root->node));
2164 if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) {
2165 btrfs_set_backup_block_group_root(root_backup,
2166 info->block_group_root->node->start);
2167 btrfs_set_backup_block_group_root_gen(root_backup,
2168 btrfs_header_generation(info->block_group_root->node));
2169 btrfs_set_backup_block_group_root_level(root_backup,
2170 btrfs_header_level(info->block_group_root->node));
2172 struct btrfs_root *extent_root = btrfs_extent_root(info, 0);
2173 struct btrfs_root *csum_root = btrfs_csum_root(info, 0);
2175 btrfs_set_backup_extent_root(root_backup,
2176 extent_root->node->start);
2177 btrfs_set_backup_extent_root_gen(root_backup,
2178 btrfs_header_generation(extent_root->node));
2179 btrfs_set_backup_extent_root_level(root_backup,
2180 btrfs_header_level(extent_root->node));
2182 btrfs_set_backup_csum_root(root_backup, csum_root->node->start);
2183 btrfs_set_backup_csum_root_gen(root_backup,
2184 btrfs_header_generation(csum_root->node));
2185 btrfs_set_backup_csum_root_level(root_backup,
2186 btrfs_header_level(csum_root->node));
2190 * we might commit during log recovery, which happens before we set
2191 * the fs_root. Make sure it is valid before we fill it in.
2193 if (info->fs_root && info->fs_root->node) {
2194 btrfs_set_backup_fs_root(root_backup,
2195 info->fs_root->node->start);
2196 btrfs_set_backup_fs_root_gen(root_backup,
2197 btrfs_header_generation(info->fs_root->node));
2198 btrfs_set_backup_fs_root_level(root_backup,
2199 btrfs_header_level(info->fs_root->node));
2202 btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
2203 btrfs_set_backup_dev_root_gen(root_backup,
2204 btrfs_header_generation(info->dev_root->node));
2205 btrfs_set_backup_dev_root_level(root_backup,
2206 btrfs_header_level(info->dev_root->node));
2208 btrfs_set_backup_total_bytes(root_backup,
2209 btrfs_super_total_bytes(info->super_copy));
2210 btrfs_set_backup_bytes_used(root_backup,
2211 btrfs_super_bytes_used(info->super_copy));
2212 btrfs_set_backup_num_devices(root_backup,
2213 btrfs_super_num_devices(info->super_copy));
2216 * if we don't copy this out to the super_copy, it won't get remembered
2217 * for the next commit
2219 memcpy(&info->super_copy->super_roots,
2220 &info->super_for_commit->super_roots,
2221 sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
2225 * read_backup_root - Reads a backup root based on the passed priority. Prio 0
2226 * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
2228 * fs_info - filesystem whose backup roots need to be read
2229 * priority - priority of backup root required
2231 * Returns backup root index on success and -EINVAL otherwise.
2233 static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority)
2235 int backup_index = find_newest_super_backup(fs_info);
2236 struct btrfs_super_block *super = fs_info->super_copy;
2237 struct btrfs_root_backup *root_backup;
2239 if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) {
2241 return backup_index;
2243 backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority;
2244 backup_index %= BTRFS_NUM_BACKUP_ROOTS;
2249 root_backup = super->super_roots + backup_index;
2251 btrfs_set_super_generation(super,
2252 btrfs_backup_tree_root_gen(root_backup));
2253 btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
2254 btrfs_set_super_root_level(super,
2255 btrfs_backup_tree_root_level(root_backup));
2256 btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
2259 * Fixme: the total bytes and num_devices need to match or we should
2262 btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
2263 btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
2265 return backup_index;
2268 /* helper to cleanup workers */
2269 static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
2271 btrfs_destroy_workqueue(fs_info->fixup_workers);
2272 btrfs_destroy_workqueue(fs_info->delalloc_workers);
2273 btrfs_destroy_workqueue(fs_info->hipri_workers);
2274 btrfs_destroy_workqueue(fs_info->workers);
2275 btrfs_destroy_workqueue(fs_info->endio_workers);
2276 btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
2277 if (fs_info->rmw_workers)
2278 destroy_workqueue(fs_info->rmw_workers);
2279 btrfs_destroy_workqueue(fs_info->endio_write_workers);
2280 btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
2281 btrfs_destroy_workqueue(fs_info->delayed_workers);
2282 btrfs_destroy_workqueue(fs_info->caching_workers);
2283 btrfs_destroy_workqueue(fs_info->flush_workers);
2284 btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
2285 if (fs_info->discard_ctl.discard_workers)
2286 destroy_workqueue(fs_info->discard_ctl.discard_workers);
2288 * Now that all other work queues are destroyed, we can safely destroy
2289 * the queues used for metadata I/O, since tasks from those other work
2290 * queues can do metadata I/O operations.
2292 btrfs_destroy_workqueue(fs_info->endio_meta_workers);
2293 btrfs_destroy_workqueue(fs_info->endio_meta_write_workers);
2296 static void free_root_extent_buffers(struct btrfs_root *root)
2299 free_extent_buffer(root->node);
2300 free_extent_buffer(root->commit_root);
2302 root->commit_root = NULL;
2306 static void free_global_root_pointers(struct btrfs_fs_info *fs_info)
2308 struct btrfs_root *root, *tmp;
2310 rbtree_postorder_for_each_entry_safe(root, tmp,
2311 &fs_info->global_root_tree,
2313 free_root_extent_buffers(root);
2316 /* helper to cleanup tree roots */
2317 static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
2319 free_root_extent_buffers(info->tree_root);
2321 free_global_root_pointers(info);
2322 free_root_extent_buffers(info->dev_root);
2323 free_root_extent_buffers(info->quota_root);
2324 free_root_extent_buffers(info->uuid_root);
2325 free_root_extent_buffers(info->fs_root);
2326 free_root_extent_buffers(info->data_reloc_root);
2327 free_root_extent_buffers(info->block_group_root);
2328 if (free_chunk_root)
2329 free_root_extent_buffers(info->chunk_root);
2332 void btrfs_put_root(struct btrfs_root *root)
2337 if (refcount_dec_and_test(&root->refs)) {
2338 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
2339 WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
2341 free_anon_bdev(root->anon_dev);
2342 btrfs_drew_lock_destroy(&root->snapshot_lock);
2343 free_root_extent_buffers(root);
2344 #ifdef CONFIG_BTRFS_DEBUG
2345 spin_lock(&root->fs_info->fs_roots_lock);
2346 list_del_init(&root->leak_list);
2347 spin_unlock(&root->fs_info->fs_roots_lock);
2353 void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
2355 struct btrfs_root *root;
2356 unsigned long index = 0;
2358 while (!list_empty(&fs_info->dead_roots)) {
2359 root = list_entry(fs_info->dead_roots.next,
2360 struct btrfs_root, root_list);
2361 list_del(&root->root_list);
2363 if (test_bit(BTRFS_ROOT_REGISTERED, &root->state))
2364 btrfs_drop_and_free_fs_root(fs_info, root);
2365 btrfs_put_root(root);
2368 xa_for_each(&fs_info->fs_roots, index, root) {
2369 btrfs_drop_and_free_fs_root(fs_info, root);
2373 static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
2375 mutex_init(&fs_info->scrub_lock);
2376 atomic_set(&fs_info->scrubs_running, 0);
2377 atomic_set(&fs_info->scrub_pause_req, 0);
2378 atomic_set(&fs_info->scrubs_paused, 0);
2379 atomic_set(&fs_info->scrub_cancel_req, 0);
2380 init_waitqueue_head(&fs_info->scrub_pause_wait);
2381 refcount_set(&fs_info->scrub_workers_refcnt, 0);
2384 static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
2386 spin_lock_init(&fs_info->balance_lock);
2387 mutex_init(&fs_info->balance_mutex);
2388 atomic_set(&fs_info->balance_pause_req, 0);
2389 atomic_set(&fs_info->balance_cancel_req, 0);
2390 fs_info->balance_ctl = NULL;
2391 init_waitqueue_head(&fs_info->balance_wait_q);
2392 atomic_set(&fs_info->reloc_cancel_req, 0);
2395 static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
2397 struct inode *inode = fs_info->btree_inode;
2399 inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
2400 set_nlink(inode, 1);
2402 * we set the i_size on the btree inode to the max possible int.
2403 * the real end of the address space is determined by all of
2404 * the devices in the system
2406 inode->i_size = OFFSET_MAX;
2407 inode->i_mapping->a_ops = &btree_aops;
2409 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
2410 extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
2411 IO_TREE_BTREE_INODE_IO, inode);
2412 BTRFS_I(inode)->io_tree.track_uptodate = false;
2413 extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
2415 BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root);
2416 memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key));
2417 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
2418 btrfs_insert_inode_hash(inode);
2421 static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
2423 mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
2424 init_rwsem(&fs_info->dev_replace.rwsem);
2425 init_waitqueue_head(&fs_info->dev_replace.replace_wait);
2428 static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
2430 spin_lock_init(&fs_info->qgroup_lock);
2431 mutex_init(&fs_info->qgroup_ioctl_lock);
2432 fs_info->qgroup_tree = RB_ROOT;
2433 INIT_LIST_HEAD(&fs_info->dirty_qgroups);
2434 fs_info->qgroup_seq = 1;
2435 fs_info->qgroup_ulist = NULL;
2436 fs_info->qgroup_rescan_running = false;
2437 mutex_init(&fs_info->qgroup_rescan_lock);
2440 static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
2442 u32 max_active = fs_info->thread_pool_size;
2443 unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
2446 btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16);
2447 fs_info->hipri_workers =
2448 btrfs_alloc_workqueue(fs_info, "worker-high",
2449 flags | WQ_HIGHPRI, max_active, 16);
2451 fs_info->delalloc_workers =
2452 btrfs_alloc_workqueue(fs_info, "delalloc",
2453 flags, max_active, 2);
2455 fs_info->flush_workers =
2456 btrfs_alloc_workqueue(fs_info, "flush_delalloc",
2457 flags, max_active, 0);
2459 fs_info->caching_workers =
2460 btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
2462 fs_info->fixup_workers =
2463 btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0);
2466 * endios are largely parallel and should have a very
2469 fs_info->endio_workers =
2470 btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4);
2471 fs_info->endio_meta_workers =
2472 btrfs_alloc_workqueue(fs_info, "endio-meta", flags,
2474 fs_info->endio_meta_write_workers =
2475 btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags,
2477 fs_info->endio_raid56_workers =
2478 btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,
2480 fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);
2481 fs_info->endio_write_workers =
2482 btrfs_alloc_workqueue(fs_info, "endio-write", flags,
2484 fs_info->endio_freespace_worker =
2485 btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
2487 fs_info->delayed_workers =
2488 btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
2490 fs_info->qgroup_rescan_workers =
2491 btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0);
2492 fs_info->discard_ctl.discard_workers =
2493 alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1);
2495 if (!(fs_info->workers && fs_info->hipri_workers &&
2496 fs_info->delalloc_workers && fs_info->flush_workers &&
2497 fs_info->endio_workers && fs_info->endio_meta_workers &&
2498 fs_info->endio_meta_write_workers &&
2499 fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
2500 fs_info->endio_freespace_worker && fs_info->rmw_workers &&
2501 fs_info->caching_workers && fs_info->fixup_workers &&
2502 fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
2503 fs_info->discard_ctl.discard_workers)) {
2510 static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
2512 struct crypto_shash *csum_shash;
2513 const char *csum_driver = btrfs_super_csum_driver(csum_type);
2515 csum_shash = crypto_alloc_shash(csum_driver, 0, 0);
2517 if (IS_ERR(csum_shash)) {
2518 btrfs_err(fs_info, "error allocating %s hash for checksum",
2520 return PTR_ERR(csum_shash);
2523 fs_info->csum_shash = csum_shash;
2528 static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2529 struct btrfs_fs_devices *fs_devices)
2532 struct btrfs_root *log_tree_root;
2533 struct btrfs_super_block *disk_super = fs_info->super_copy;
2534 u64 bytenr = btrfs_super_log_root(disk_super);
2535 int level = btrfs_super_log_root_level(disk_super);
2537 if (fs_devices->rw_devices == 0) {
2538 btrfs_warn(fs_info, "log replay required on RO media");
2542 log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID,
2547 log_tree_root->node = read_tree_block(fs_info, bytenr,
2548 BTRFS_TREE_LOG_OBJECTID,
2549 fs_info->generation + 1, level,
2551 if (IS_ERR(log_tree_root->node)) {
2552 btrfs_warn(fs_info, "failed to read log tree");
2553 ret = PTR_ERR(log_tree_root->node);
2554 log_tree_root->node = NULL;
2555 btrfs_put_root(log_tree_root);
2558 if (!extent_buffer_uptodate(log_tree_root->node)) {
2559 btrfs_err(fs_info, "failed to read log tree");
2560 btrfs_put_root(log_tree_root);
2564 /* returns with log_tree_root freed on success */
2565 ret = btrfs_recover_log_trees(log_tree_root);
2567 btrfs_handle_fs_error(fs_info, ret,
2568 "Failed to recover log tree");
2569 btrfs_put_root(log_tree_root);
2573 if (sb_rdonly(fs_info->sb)) {
2574 ret = btrfs_commit_super(fs_info);
2582 static int load_global_roots_objectid(struct btrfs_root *tree_root,
2583 struct btrfs_path *path, u64 objectid,
2586 struct btrfs_fs_info *fs_info = tree_root->fs_info;
2587 struct btrfs_root *root;
2588 u64 max_global_id = 0;
2590 struct btrfs_key key = {
2591 .objectid = objectid,
2592 .type = BTRFS_ROOT_ITEM_KEY,
2597 /* If we have IGNOREDATACSUMS skip loading these roots. */
2598 if (objectid == BTRFS_CSUM_TREE_OBJECTID &&
2599 btrfs_test_opt(fs_info, IGNOREDATACSUMS)) {
2600 set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state);
2605 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2609 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2610 ret = btrfs_next_leaf(tree_root, path);
2619 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2620 if (key.objectid != objectid)
2622 btrfs_release_path(path);
2625 * Just worry about this for extent tree, it'll be the same for
2628 if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2629 max_global_id = max(max_global_id, key.offset);
2632 root = read_tree_root_path(tree_root, path, &key);
2634 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2635 ret = PTR_ERR(root);
2638 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2639 ret = btrfs_global_root_insert(root);
2641 btrfs_put_root(root);
2646 btrfs_release_path(path);
2648 if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2649 fs_info->nr_global_roots = max_global_id + 1;
2651 if (!found || ret) {
2652 if (objectid == BTRFS_CSUM_TREE_OBJECTID)
2653 set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state);
2655 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2656 ret = ret ? ret : -ENOENT;
2659 btrfs_err(fs_info, "failed to load root %s", name);
2664 static int load_global_roots(struct btrfs_root *tree_root)
2666 struct btrfs_path *path;
2669 path = btrfs_alloc_path();
2673 ret = load_global_roots_objectid(tree_root, path,
2674 BTRFS_EXTENT_TREE_OBJECTID, "extent");
2677 ret = load_global_roots_objectid(tree_root, path,
2678 BTRFS_CSUM_TREE_OBJECTID, "csum");
2681 if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE))
2683 ret = load_global_roots_objectid(tree_root, path,
2684 BTRFS_FREE_SPACE_TREE_OBJECTID,
2687 btrfs_free_path(path);
2691 static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
2693 struct btrfs_root *tree_root = fs_info->tree_root;
2694 struct btrfs_root *root;
2695 struct btrfs_key location;
2698 BUG_ON(!fs_info->tree_root);
2700 ret = load_global_roots(tree_root);
2704 location.objectid = BTRFS_DEV_TREE_OBJECTID;
2705 location.type = BTRFS_ROOT_ITEM_KEY;
2706 location.offset = 0;
2708 root = btrfs_read_tree_root(tree_root, &location);
2710 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2711 ret = PTR_ERR(root);
2715 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2716 fs_info->dev_root = root;
2718 /* Initialize fs_info for all devices in any case */
2719 btrfs_init_devices_late(fs_info);
2722 * This tree can share blocks with some other fs tree during relocation
2723 * and we need a proper setup by btrfs_get_fs_root
2725 root = btrfs_get_fs_root(tree_root->fs_info,
2726 BTRFS_DATA_RELOC_TREE_OBJECTID, true);
2728 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2729 ret = PTR_ERR(root);
2733 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2734 fs_info->data_reloc_root = root;
2737 location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
2738 root = btrfs_read_tree_root(tree_root, &location);
2739 if (!IS_ERR(root)) {
2740 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2741 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
2742 fs_info->quota_root = root;
2745 location.objectid = BTRFS_UUID_TREE_OBJECTID;
2746 root = btrfs_read_tree_root(tree_root, &location);
2748 if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2749 ret = PTR_ERR(root);
2754 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2755 fs_info->uuid_root = root;
2760 btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
2761 location.objectid, ret);
2766 * Real super block validation
2767 * NOTE: super csum type and incompat features will not be checked here.
2769 * @sb: super block to check
2770 * @mirror_num: the super block number to check its bytenr:
2771 * 0 the primary (1st) sb
2772 * 1, 2 2nd and 3rd backup copy
2773 * -1 skip bytenr check
2775 static int validate_super(struct btrfs_fs_info *fs_info,
2776 struct btrfs_super_block *sb, int mirror_num)
2778 u64 nodesize = btrfs_super_nodesize(sb);
2779 u64 sectorsize = btrfs_super_sectorsize(sb);
2782 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
2783 btrfs_err(fs_info, "no valid FS found");
2786 if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) {
2787 btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu",
2788 btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
2791 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
2792 btrfs_err(fs_info, "tree_root level too big: %d >= %d",
2793 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
2796 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
2797 btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
2798 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
2801 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
2802 btrfs_err(fs_info, "log_root level too big: %d >= %d",
2803 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
2808 * Check sectorsize and nodesize first, other check will need it.
2809 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
2811 if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
2812 sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
2813 btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
2818 * We only support at most two sectorsizes: 4K and PAGE_SIZE.
2820 * We can support 16K sectorsize with 64K page size without problem,
2821 * but such sectorsize/pagesize combination doesn't make much sense.
2822 * 4K will be our future standard, PAGE_SIZE is supported from the very
2825 if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) {
2827 "sectorsize %llu not yet supported for page size %lu",
2828 sectorsize, PAGE_SIZE);
2832 if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
2833 nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
2834 btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
2837 if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
2838 btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
2839 le32_to_cpu(sb->__unused_leafsize), nodesize);
2843 /* Root alignment check */
2844 if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
2845 btrfs_warn(fs_info, "tree_root block unaligned: %llu",
2846 btrfs_super_root(sb));
2849 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
2850 btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
2851 btrfs_super_chunk_root(sb));
2854 if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
2855 btrfs_warn(fs_info, "log_root block unaligned: %llu",
2856 btrfs_super_log_root(sb));
2860 if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid,
2863 "superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
2864 fs_info->super_copy->fsid, fs_info->fs_devices->fsid);
2868 if (btrfs_fs_incompat(fs_info, METADATA_UUID) &&
2869 memcmp(fs_info->fs_devices->metadata_uuid,
2870 fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) {
2872 "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU",
2873 fs_info->super_copy->metadata_uuid,
2874 fs_info->fs_devices->metadata_uuid);
2878 if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid,
2879 BTRFS_FSID_SIZE) != 0) {
2881 "dev_item UUID does not match metadata fsid: %pU != %pU",
2882 fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid);
2887 * Hint to catch really bogus numbers, bitflips or so, more exact checks are
2890 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
2891 btrfs_err(fs_info, "bytes_used is too small %llu",
2892 btrfs_super_bytes_used(sb));
2895 if (!is_power_of_2(btrfs_super_stripesize(sb))) {
2896 btrfs_err(fs_info, "invalid stripesize %u",
2897 btrfs_super_stripesize(sb));
2900 if (btrfs_super_num_devices(sb) > (1UL << 31))
2901 btrfs_warn(fs_info, "suspicious number of devices: %llu",
2902 btrfs_super_num_devices(sb));
2903 if (btrfs_super_num_devices(sb) == 0) {
2904 btrfs_err(fs_info, "number of devices is 0");
2908 if (mirror_num >= 0 &&
2909 btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
2910 btrfs_err(fs_info, "super offset mismatch %llu != %u",
2911 btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
2916 * Obvious sys_chunk_array corruptions, it must hold at least one key
2919 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
2920 btrfs_err(fs_info, "system chunk array too big %u > %u",
2921 btrfs_super_sys_array_size(sb),
2922 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
2925 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
2926 + sizeof(struct btrfs_chunk)) {
2927 btrfs_err(fs_info, "system chunk array too small %u < %zu",
2928 btrfs_super_sys_array_size(sb),
2929 sizeof(struct btrfs_disk_key)
2930 + sizeof(struct btrfs_chunk));
2935 * The generation is a global counter, we'll trust it more than the others
2936 * but it's still possible that it's the one that's wrong.
2938 if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
2940 "suspicious: generation < chunk_root_generation: %llu < %llu",
2941 btrfs_super_generation(sb),
2942 btrfs_super_chunk_root_generation(sb));
2943 if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
2944 && btrfs_super_cache_generation(sb) != (u64)-1)
2946 "suspicious: generation < cache_generation: %llu < %llu",
2947 btrfs_super_generation(sb),
2948 btrfs_super_cache_generation(sb));
2954 * Validation of super block at mount time.
2955 * Some checks already done early at mount time, like csum type and incompat
2956 * flags will be skipped.
2958 static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
2960 return validate_super(fs_info, fs_info->super_copy, 0);
2964 * Validation of super block at write time.
2965 * Some checks like bytenr check will be skipped as their values will be
2967 * Extra checks like csum type and incompat flags will be done here.
2969 static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
2970 struct btrfs_super_block *sb)
2974 ret = validate_super(fs_info, sb, -1);
2977 if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
2979 btrfs_err(fs_info, "invalid csum type, has %u want %u",
2980 btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
2983 if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
2986 "invalid incompat flags, has 0x%llx valid mask 0x%llx",
2987 btrfs_super_incompat_flags(sb),
2988 (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
2994 "super block corruption detected before writing it to disk");
2998 static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level)
3002 root->node = read_tree_block(root->fs_info, bytenr,
3003 root->root_key.objectid, gen, level, NULL);
3004 if (IS_ERR(root->node)) {
3005 ret = PTR_ERR(root->node);
3009 if (!extent_buffer_uptodate(root->node)) {
3010 free_extent_buffer(root->node);
3015 btrfs_set_root_node(&root->root_item, root->node);
3016 root->commit_root = btrfs_root_node(root);
3017 btrfs_set_root_refs(&root->root_item, 1);
3021 static int load_important_roots(struct btrfs_fs_info *fs_info)
3023 struct btrfs_super_block *sb = fs_info->super_copy;
3027 bytenr = btrfs_super_root(sb);
3028 gen = btrfs_super_generation(sb);
3029 level = btrfs_super_root_level(sb);
3030 ret = load_super_root(fs_info->tree_root, bytenr, gen, level);
3032 btrfs_warn(fs_info, "couldn't read tree root");
3036 if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
3039 bytenr = btrfs_super_block_group_root(sb);
3040 gen = btrfs_super_block_group_root_generation(sb);
3041 level = btrfs_super_block_group_root_level(sb);
3042 ret = load_super_root(fs_info->block_group_root, bytenr, gen, level);
3044 btrfs_warn(fs_info, "couldn't read block group root");
3048 static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
3050 int backup_index = find_newest_super_backup(fs_info);
3051 struct btrfs_super_block *sb = fs_info->super_copy;
3052 struct btrfs_root *tree_root = fs_info->tree_root;
3053 bool handle_error = false;
3057 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
3058 struct btrfs_root *root;
3060 root = btrfs_alloc_root(fs_info, BTRFS_BLOCK_GROUP_TREE_OBJECTID,
3064 fs_info->block_group_root = root;
3067 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
3069 if (!IS_ERR(tree_root->node))
3070 free_extent_buffer(tree_root->node);
3071 tree_root->node = NULL;
3073 if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
3076 free_root_pointers(fs_info, 0);
3079 * Don't use the log in recovery mode, it won't be
3082 btrfs_set_super_log_root(sb, 0);
3084 /* We can't trust the free space cache either */
3085 btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
3087 ret = read_backup_root(fs_info, i);
3093 ret = load_important_roots(fs_info);
3095 handle_error = true;
3100 * No need to hold btrfs_root::objectid_mutex since the fs
3101 * hasn't been fully initialised and we are the only user
3103 ret = btrfs_init_root_free_objectid(tree_root);
3105 handle_error = true;
3109 ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
3111 ret = btrfs_read_roots(fs_info);
3113 handle_error = true;
3117 /* All successful */
3118 fs_info->generation = btrfs_header_generation(tree_root->node);
3119 fs_info->last_trans_committed = fs_info->generation;
3120 fs_info->last_reloc_trans = 0;
3122 /* Always begin writing backup roots after the one being used */
3123 if (backup_index < 0) {
3124 fs_info->backup_root_index = 0;
3126 fs_info->backup_root_index = backup_index + 1;
3127 fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS;
3135 void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
3137 xa_init_flags(&fs_info->fs_roots, GFP_ATOMIC);
3138 xa_init_flags(&fs_info->extent_buffers, GFP_ATOMIC);
3139 INIT_LIST_HEAD(&fs_info->trans_list);
3140 INIT_LIST_HEAD(&fs_info->dead_roots);
3141 INIT_LIST_HEAD(&fs_info->delayed_iputs);
3142 INIT_LIST_HEAD(&fs_info->delalloc_roots);
3143 INIT_LIST_HEAD(&fs_info->caching_block_groups);
3144 spin_lock_init(&fs_info->delalloc_root_lock);
3145 spin_lock_init(&fs_info->trans_lock);
3146 spin_lock_init(&fs_info->fs_roots_lock);
3147 spin_lock_init(&fs_info->delayed_iput_lock);
3148 spin_lock_init(&fs_info->defrag_inodes_lock);
3149 spin_lock_init(&fs_info->super_lock);
3150 spin_lock_init(&fs_info->buffer_lock);
3151 spin_lock_init(&fs_info->unused_bgs_lock);
3152 spin_lock_init(&fs_info->treelog_bg_lock);
3153 spin_lock_init(&fs_info->zone_active_bgs_lock);
3154 spin_lock_init(&fs_info->relocation_bg_lock);
3155 rwlock_init(&fs_info->tree_mod_log_lock);
3156 rwlock_init(&fs_info->global_root_lock);
3157 mutex_init(&fs_info->unused_bg_unpin_mutex);
3158 mutex_init(&fs_info->reclaim_bgs_lock);
3159 mutex_init(&fs_info->reloc_mutex);
3160 mutex_init(&fs_info->delalloc_root_mutex);
3161 mutex_init(&fs_info->zoned_meta_io_lock);
3162 mutex_init(&fs_info->zoned_data_reloc_io_lock);
3163 seqlock_init(&fs_info->profiles_lock);
3165 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
3166 INIT_LIST_HEAD(&fs_info->space_info);
3167 INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
3168 INIT_LIST_HEAD(&fs_info->unused_bgs);
3169 INIT_LIST_HEAD(&fs_info->reclaim_bgs);
3170 INIT_LIST_HEAD(&fs_info->zone_active_bgs);
3171 #ifdef CONFIG_BTRFS_DEBUG
3172 INIT_LIST_HEAD(&fs_info->allocated_roots);
3173 INIT_LIST_HEAD(&fs_info->allocated_ebs);
3174 spin_lock_init(&fs_info->eb_leak_lock);
3176 extent_map_tree_init(&fs_info->mapping_tree);
3177 btrfs_init_block_rsv(&fs_info->global_block_rsv,
3178 BTRFS_BLOCK_RSV_GLOBAL);
3179 btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
3180 btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
3181 btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
3182 btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
3183 BTRFS_BLOCK_RSV_DELOPS);
3184 btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
3185 BTRFS_BLOCK_RSV_DELREFS);
3187 atomic_set(&fs_info->async_delalloc_pages, 0);
3188 atomic_set(&fs_info->defrag_running, 0);
3189 atomic_set(&fs_info->nr_delayed_iputs, 0);
3190 atomic64_set(&fs_info->tree_mod_seq, 0);
3191 fs_info->global_root_tree = RB_ROOT;
3192 fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
3193 fs_info->metadata_ratio = 0;
3194 fs_info->defrag_inodes = RB_ROOT;
3195 atomic64_set(&fs_info->free_chunk_space, 0);
3196 fs_info->tree_mod_log = RB_ROOT;
3197 fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
3198 fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */
3199 btrfs_init_ref_verify(fs_info);
3201 fs_info->thread_pool_size = min_t(unsigned long,
3202 num_online_cpus() + 2, 8);
3204 INIT_LIST_HEAD(&fs_info->ordered_roots);
3205 spin_lock_init(&fs_info->ordered_root_lock);
3207 btrfs_init_scrub(fs_info);
3208 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3209 fs_info->check_integrity_print_mask = 0;
3211 btrfs_init_balance(fs_info);
3212 btrfs_init_async_reclaim_work(fs_info);
3214 rwlock_init(&fs_info->block_group_cache_lock);
3215 fs_info->block_group_cache_tree = RB_ROOT_CACHED;
3217 extent_io_tree_init(fs_info, &fs_info->excluded_extents,
3218 IO_TREE_FS_EXCLUDED_EXTENTS, NULL);
3220 mutex_init(&fs_info->ordered_operations_mutex);
3221 mutex_init(&fs_info->tree_log_mutex);
3222 mutex_init(&fs_info->chunk_mutex);
3223 mutex_init(&fs_info->transaction_kthread_mutex);
3224 mutex_init(&fs_info->cleaner_mutex);
3225 mutex_init(&fs_info->ro_block_group_mutex);
3226 init_rwsem(&fs_info->commit_root_sem);
3227 init_rwsem(&fs_info->cleanup_work_sem);
3228 init_rwsem(&fs_info->subvol_sem);
3229 sema_init(&fs_info->uuid_tree_rescan_sem, 1);
3231 btrfs_init_dev_replace_locks(fs_info);
3232 btrfs_init_qgroup(fs_info);
3233 btrfs_discard_init(fs_info);
3235 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
3236 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
3238 init_waitqueue_head(&fs_info->transaction_throttle);
3239 init_waitqueue_head(&fs_info->transaction_wait);
3240 init_waitqueue_head(&fs_info->transaction_blocked_wait);
3241 init_waitqueue_head(&fs_info->async_submit_wait);
3242 init_waitqueue_head(&fs_info->delayed_iputs_wait);
3244 /* Usable values until the real ones are cached from the superblock */
3245 fs_info->nodesize = 4096;
3246 fs_info->sectorsize = 4096;
3247 fs_info->sectorsize_bits = ilog2(4096);
3248 fs_info->stripesize = 4096;
3250 spin_lock_init(&fs_info->swapfile_pins_lock);
3251 fs_info->swapfile_pins = RB_ROOT;
3253 fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH;
3254 INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work);
3257 static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
3262 sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
3263 sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
3265 ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL);
3269 ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
3273 fs_info->dirty_metadata_batch = PAGE_SIZE *
3274 (1 + ilog2(nr_cpu_ids));
3276 ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
3280 ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0,
3285 fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
3287 if (!fs_info->delayed_root)
3289 btrfs_init_delayed_root(fs_info->delayed_root);
3292 set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
3294 return btrfs_alloc_stripe_hash_table(fs_info);
3297 static int btrfs_uuid_rescan_kthread(void *data)
3299 struct btrfs_fs_info *fs_info = data;
3303 * 1st step is to iterate through the existing UUID tree and
3304 * to delete all entries that contain outdated data.
3305 * 2nd step is to add all missing entries to the UUID tree.
3307 ret = btrfs_uuid_tree_iterate(fs_info);
3310 btrfs_warn(fs_info, "iterating uuid_tree failed %d",
3312 up(&fs_info->uuid_tree_rescan_sem);
3315 return btrfs_uuid_scan_kthread(data);
3318 static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3320 struct task_struct *task;
3322 down(&fs_info->uuid_tree_rescan_sem);
3323 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3325 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3326 btrfs_warn(fs_info, "failed to start uuid_rescan task");
3327 up(&fs_info->uuid_tree_rescan_sem);
3328 return PTR_ERR(task);
3335 * Some options only have meaning at mount time and shouldn't persist across
3336 * remounts, or be displayed. Clear these at the end of mount and remount
3339 void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
3341 btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
3342 btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
3346 * Mounting logic specific to read-write file systems. Shared by open_ctree
3347 * and btrfs_remount when remounting from read-only to read-write.
3349 int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
3352 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
3353 bool clear_free_space_tree = false;
3355 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
3356 btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
3357 clear_free_space_tree = true;
3358 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
3359 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
3360 btrfs_warn(fs_info, "free space tree is invalid");
3361 clear_free_space_tree = true;
3364 if (clear_free_space_tree) {
3365 btrfs_info(fs_info, "clearing free space tree");
3366 ret = btrfs_clear_free_space_tree(fs_info);
3369 "failed to clear free space tree: %d", ret);
3375 * btrfs_find_orphan_roots() is responsible for finding all the dead
3376 * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load
3377 * them into the fs_info->fs_roots. This must be done before
3378 * calling btrfs_orphan_cleanup() on the tree root. If we don't do it
3379 * first, then btrfs_orphan_cleanup() will delete a dead root's orphan
3380 * item before the root's tree is deleted - this means that if we unmount
3381 * or crash before the deletion completes, on the next mount we will not
3382 * delete what remains of the tree because the orphan item does not
3383 * exists anymore, which is what tells us we have a pending deletion.
3385 ret = btrfs_find_orphan_roots(fs_info);
3389 ret = btrfs_cleanup_fs_roots(fs_info);
3393 down_read(&fs_info->cleanup_work_sem);
3394 if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
3395 (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
3396 up_read(&fs_info->cleanup_work_sem);
3399 up_read(&fs_info->cleanup_work_sem);
3401 mutex_lock(&fs_info->cleaner_mutex);
3402 ret = btrfs_recover_relocation(fs_info);
3403 mutex_unlock(&fs_info->cleaner_mutex);
3405 btrfs_warn(fs_info, "failed to recover relocation: %d", ret);
3409 if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
3410 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
3411 btrfs_info(fs_info, "creating free space tree");
3412 ret = btrfs_create_free_space_tree(fs_info);
3415 "failed to create free space tree: %d", ret);
3420 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) {
3421 ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
3426 ret = btrfs_resume_balance_async(fs_info);
3430 ret = btrfs_resume_dev_replace_async(fs_info);
3432 btrfs_warn(fs_info, "failed to resume dev_replace");
3436 btrfs_qgroup_rescan_resume(fs_info);
3438 if (!fs_info->uuid_root) {
3439 btrfs_info(fs_info, "creating UUID tree");
3440 ret = btrfs_create_uuid_tree(fs_info);
3443 "failed to create the UUID tree %d", ret);
3452 int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices,
3461 struct btrfs_super_block *disk_super;
3462 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
3463 struct btrfs_root *tree_root;
3464 struct btrfs_root *chunk_root;
3469 ret = init_mount_fs_info(fs_info, sb);
3475 /* These need to be init'ed before we start creating inodes and such. */
3476 tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID,
3478 fs_info->tree_root = tree_root;
3479 chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID,
3481 fs_info->chunk_root = chunk_root;
3482 if (!tree_root || !chunk_root) {
3487 fs_info->btree_inode = new_inode(sb);
3488 if (!fs_info->btree_inode) {
3492 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
3493 btrfs_init_btree_inode(fs_info);
3495 invalidate_bdev(fs_devices->latest_dev->bdev);
3498 * Read super block and check the signature bytes only
3500 disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev);
3501 if (IS_ERR(disk_super)) {
3502 err = PTR_ERR(disk_super);
3507 * Verify the type first, if that or the checksum value are
3508 * corrupted, we'll find out
3510 csum_type = btrfs_super_csum_type(disk_super);
3511 if (!btrfs_supported_super_csum(csum_type)) {
3512 btrfs_err(fs_info, "unsupported checksum algorithm: %u",
3515 btrfs_release_disk_super(disk_super);
3519 fs_info->csum_size = btrfs_super_csum_size(disk_super);
3521 ret = btrfs_init_csum_hash(fs_info, csum_type);
3524 btrfs_release_disk_super(disk_super);
3529 * We want to check superblock checksum, the type is stored inside.
3530 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
3532 if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) {
3533 btrfs_err(fs_info, "superblock checksum mismatch");
3535 btrfs_release_disk_super(disk_super);
3540 * super_copy is zeroed at allocation time and we never touch the
3541 * following bytes up to INFO_SIZE, the checksum is calculated from
3542 * the whole block of INFO_SIZE
3544 memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy));
3545 btrfs_release_disk_super(disk_super);
3547 disk_super = fs_info->super_copy;
3550 features = btrfs_super_flags(disk_super);
3551 if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) {
3552 features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2;
3553 btrfs_set_super_flags(disk_super, features);
3555 "found metadata UUID change in progress flag, clearing");
3558 memcpy(fs_info->super_for_commit, fs_info->super_copy,
3559 sizeof(*fs_info->super_for_commit));
3561 ret = btrfs_validate_mount_super(fs_info);
3563 btrfs_err(fs_info, "superblock contains fatal errors");
3568 if (!btrfs_super_root(disk_super))
3571 /* check FS state, whether FS is broken. */
3572 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
3573 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
3576 * In the long term, we'll store the compression type in the super
3577 * block, and it'll be used for per file compression control.
3579 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
3582 * Flag our filesystem as having big metadata blocks if they are bigger
3583 * than the page size.
3585 if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) {
3586 if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA))
3588 "flagging fs with big metadata feature");
3589 features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
3592 /* Set up fs_info before parsing mount options */
3593 nodesize = btrfs_super_nodesize(disk_super);
3594 sectorsize = btrfs_super_sectorsize(disk_super);
3595 stripesize = sectorsize;
3596 fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
3597 fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
3599 fs_info->nodesize = nodesize;
3600 fs_info->sectorsize = sectorsize;
3601 fs_info->sectorsize_bits = ilog2(sectorsize);
3602 fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
3603 fs_info->stripesize = stripesize;
3605 ret = btrfs_parse_options(fs_info, options, sb->s_flags);
3611 features = btrfs_super_incompat_flags(disk_super) &
3612 ~BTRFS_FEATURE_INCOMPAT_SUPP;
3615 "cannot mount because of unsupported optional features (0x%llx)",
3621 features = btrfs_super_incompat_flags(disk_super);
3622 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
3623 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
3624 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
3625 else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
3626 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
3628 if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
3629 btrfs_info(fs_info, "has skinny extents");
3632 * mixed block groups end up with duplicate but slightly offset
3633 * extent buffers for the same range. It leads to corruptions
3635 if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
3636 (sectorsize != nodesize)) {
3638 "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
3639 nodesize, sectorsize);
3644 * Needn't use the lock because there is no other task which will
3647 btrfs_set_super_incompat_flags(disk_super, features);
3649 features = btrfs_super_compat_ro_flags(disk_super) &
3650 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
3651 if (!sb_rdonly(sb) && features) {
3653 "cannot mount read-write because of unsupported optional features (0x%llx)",
3659 if (sectorsize < PAGE_SIZE) {
3660 struct btrfs_subpage_info *subpage_info;
3663 * V1 space cache has some hardcoded PAGE_SIZE usage, and is
3664 * going to be deprecated.
3666 * Force to use v2 cache for subpage case.
3668 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
3669 btrfs_set_and_info(fs_info, FREE_SPACE_TREE,
3670 "forcing free space tree for sector size %u with page size %lu",
3671 sectorsize, PAGE_SIZE);
3674 "read-write for sector size %u with page size %lu is experimental",
3675 sectorsize, PAGE_SIZE);
3676 subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL);
3679 btrfs_init_subpage_info(subpage_info, sectorsize);
3680 fs_info->subpage_info = subpage_info;
3683 ret = btrfs_init_workqueues(fs_info);
3686 goto fail_sb_buffer;
3689 sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
3690 sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
3692 sb->s_blocksize = sectorsize;
3693 sb->s_blocksize_bits = blksize_bits(sectorsize);
3694 memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE);
3696 mutex_lock(&fs_info->chunk_mutex);
3697 ret = btrfs_read_sys_array(fs_info);
3698 mutex_unlock(&fs_info->chunk_mutex);
3700 btrfs_err(fs_info, "failed to read the system array: %d", ret);
3701 goto fail_sb_buffer;
3704 generation = btrfs_super_chunk_root_generation(disk_super);
3705 level = btrfs_super_chunk_root_level(disk_super);
3706 ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super),
3709 btrfs_err(fs_info, "failed to read chunk root");
3710 goto fail_tree_roots;
3713 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
3714 offsetof(struct btrfs_header, chunk_tree_uuid),
3717 ret = btrfs_read_chunk_tree(fs_info);
3719 btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
3720 goto fail_tree_roots;
3724 * At this point we know all the devices that make this filesystem,
3725 * including the seed devices but we don't know yet if the replace
3726 * target is required. So free devices that are not part of this
3727 * filesystem but skip the replace target device which is checked
3728 * below in btrfs_init_dev_replace().
3730 btrfs_free_extra_devids(fs_devices);
3731 if (!fs_devices->latest_dev->bdev) {
3732 btrfs_err(fs_info, "failed to read devices");
3733 goto fail_tree_roots;
3736 ret = init_tree_roots(fs_info);
3738 goto fail_tree_roots;
3741 * Get zone type information of zoned block devices. This will also
3742 * handle emulation of a zoned filesystem if a regular device has the
3743 * zoned incompat feature flag set.
3745 ret = btrfs_get_dev_zone_info_all_devices(fs_info);
3748 "zoned: failed to read device zone info: %d",
3750 goto fail_block_groups;
3754 * If we have a uuid root and we're not being told to rescan we need to
3755 * check the generation here so we can set the
3756 * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the
3757 * transaction during a balance or the log replay without updating the
3758 * uuid generation, and then if we crash we would rescan the uuid tree,
3759 * even though it was perfectly fine.
3761 if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
3762 fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
3763 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
3765 ret = btrfs_verify_dev_extents(fs_info);
3768 "failed to verify dev extents against chunks: %d",
3770 goto fail_block_groups;
3772 ret = btrfs_recover_balance(fs_info);
3774 btrfs_err(fs_info, "failed to recover balance: %d", ret);
3775 goto fail_block_groups;
3778 ret = btrfs_init_dev_stats(fs_info);
3780 btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
3781 goto fail_block_groups;
3784 ret = btrfs_init_dev_replace(fs_info);
3786 btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
3787 goto fail_block_groups;
3790 ret = btrfs_check_zoned_mode(fs_info);
3792 btrfs_err(fs_info, "failed to initialize zoned mode: %d",
3794 goto fail_block_groups;
3797 ret = btrfs_sysfs_add_fsid(fs_devices);
3799 btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
3801 goto fail_block_groups;
3804 ret = btrfs_sysfs_add_mounted(fs_info);
3806 btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
3807 goto fail_fsdev_sysfs;
3810 ret = btrfs_init_space_info(fs_info);
3812 btrfs_err(fs_info, "failed to initialize space info: %d", ret);
3816 ret = btrfs_read_block_groups(fs_info);
3818 btrfs_err(fs_info, "failed to read block groups: %d", ret);
3822 btrfs_free_zone_cache(fs_info);
3824 if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices &&
3825 !btrfs_check_rw_degradable(fs_info, NULL)) {
3827 "writable mount is not allowed due to too many missing devices");
3831 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
3833 if (IS_ERR(fs_info->cleaner_kthread))
3836 fs_info->transaction_kthread = kthread_run(transaction_kthread,
3838 "btrfs-transaction");
3839 if (IS_ERR(fs_info->transaction_kthread))
3842 if (!btrfs_test_opt(fs_info, NOSSD) &&
3843 !fs_info->fs_devices->rotating) {
3844 btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
3848 * Mount does not set all options immediately, we can do it now and do
3849 * not have to wait for transaction commit
3851 btrfs_apply_pending_changes(fs_info);
3853 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3854 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
3855 ret = btrfsic_mount(fs_info, fs_devices,
3856 btrfs_test_opt(fs_info,
3857 CHECK_INTEGRITY_DATA) ? 1 : 0,
3858 fs_info->check_integrity_print_mask);
3861 "failed to initialize integrity check module: %d",
3865 ret = btrfs_read_qgroup_config(fs_info);
3867 goto fail_trans_kthread;
3869 if (btrfs_build_ref_tree(fs_info))
3870 btrfs_err(fs_info, "couldn't build ref tree");
3872 /* do not make disk changes in broken FS or nologreplay is given */
3873 if (btrfs_super_log_root(disk_super) != 0 &&
3874 !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3875 btrfs_info(fs_info, "start tree-log replay");
3876 ret = btrfs_replay_log(fs_info, fs_devices);
3883 fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
3884 if (IS_ERR(fs_info->fs_root)) {
3885 err = PTR_ERR(fs_info->fs_root);
3886 btrfs_warn(fs_info, "failed to read fs tree: %d", err);
3887 fs_info->fs_root = NULL;
3894 ret = btrfs_start_pre_rw_mount(fs_info);
3896 close_ctree(fs_info);
3899 btrfs_discard_resume(fs_info);
3901 if (fs_info->uuid_root &&
3902 (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
3903 fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) {
3904 btrfs_info(fs_info, "checking UUID tree");
3905 ret = btrfs_check_uuid_tree(fs_info);
3908 "failed to check the UUID tree: %d", ret);
3909 close_ctree(fs_info);
3914 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
3916 /* Kick the cleaner thread so it'll start deleting snapshots. */
3917 if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
3918 wake_up_process(fs_info->cleaner_kthread);
3921 btrfs_clear_oneshot_options(fs_info);
3925 btrfs_free_qgroup_config(fs_info);
3927 kthread_stop(fs_info->transaction_kthread);
3928 btrfs_cleanup_transaction(fs_info);
3929 btrfs_free_fs_roots(fs_info);
3931 kthread_stop(fs_info->cleaner_kthread);
3934 * make sure we're done with the btree inode before we stop our
3937 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
3940 btrfs_sysfs_remove_mounted(fs_info);
3943 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3946 btrfs_put_block_group_cache(fs_info);
3949 if (fs_info->data_reloc_root)
3950 btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root);
3951 free_root_pointers(fs_info, true);
3952 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
3955 btrfs_stop_all_workers(fs_info);
3956 btrfs_free_block_groups(fs_info);
3958 btrfs_mapping_tree_free(&fs_info->mapping_tree);
3960 iput(fs_info->btree_inode);
3962 btrfs_close_devices(fs_info->fs_devices);
3965 ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
3967 static void btrfs_end_super_write(struct bio *bio)
3969 struct btrfs_device *device = bio->bi_private;
3970 struct bio_vec *bvec;
3971 struct bvec_iter_all iter_all;
3974 bio_for_each_segment_all(bvec, bio, iter_all) {
3975 page = bvec->bv_page;
3977 if (bio->bi_status) {
3978 btrfs_warn_rl_in_rcu(device->fs_info,
3979 "lost page write due to IO error on %s (%d)",
3980 rcu_str_deref(device->name),
3981 blk_status_to_errno(bio->bi_status));
3982 ClearPageUptodate(page);
3984 btrfs_dev_stat_inc_and_print(device,
3985 BTRFS_DEV_STAT_WRITE_ERRS);
3987 SetPageUptodate(page);
3997 struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev,
4000 struct btrfs_super_block *super;
4002 u64 bytenr, bytenr_orig;
4003 struct address_space *mapping = bdev->bd_inode->i_mapping;
4006 bytenr_orig = btrfs_sb_offset(copy_num);
4007 ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr);
4009 return ERR_PTR(-EINVAL);
4011 return ERR_PTR(ret);
4013 if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev))
4014 return ERR_PTR(-EINVAL);
4016 page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS);
4018 return ERR_CAST(page);
4020 super = page_address(page);
4021 if (btrfs_super_magic(super) != BTRFS_MAGIC) {
4022 btrfs_release_disk_super(super);
4023 return ERR_PTR(-ENODATA);
4026 if (btrfs_super_bytenr(super) != bytenr_orig) {
4027 btrfs_release_disk_super(super);
4028 return ERR_PTR(-EINVAL);
4035 struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev)
4037 struct btrfs_super_block *super, *latest = NULL;
4041 /* we would like to check all the supers, but that would make
4042 * a btrfs mount succeed after a mkfs from a different FS.
4043 * So, we need to add a special mount option to scan for
4044 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
4046 for (i = 0; i < 1; i++) {
4047 super = btrfs_read_dev_one_super(bdev, i);
4051 if (!latest || btrfs_super_generation(super) > transid) {
4053 btrfs_release_disk_super(super);
4056 transid = btrfs_super_generation(super);
4064 * Write superblock @sb to the @device. Do not wait for completion, all the
4065 * pages we use for writing are locked.
4067 * Write @max_mirrors copies of the superblock, where 0 means default that fit
4068 * the expected device size at commit time. Note that max_mirrors must be
4069 * same for write and wait phases.
4071 * Return number of errors when page is not found or submission fails.
4073 static int write_dev_supers(struct btrfs_device *device,
4074 struct btrfs_super_block *sb, int max_mirrors)
4076 struct btrfs_fs_info *fs_info = device->fs_info;
4077 struct address_space *mapping = device->bdev->bd_inode->i_mapping;
4078 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
4082 u64 bytenr, bytenr_orig;
4084 if (max_mirrors == 0)
4085 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
4087 shash->tfm = fs_info->csum_shash;
4089 for (i = 0; i < max_mirrors; i++) {
4092 struct btrfs_super_block *disk_super;
4094 bytenr_orig = btrfs_sb_offset(i);
4095 ret = btrfs_sb_log_location(device, i, WRITE, &bytenr);
4096 if (ret == -ENOENT) {
4098 } else if (ret < 0) {
4099 btrfs_err(device->fs_info,
4100 "couldn't get super block location for mirror %d",
4105 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
4106 device->commit_total_bytes)
4109 btrfs_set_super_bytenr(sb, bytenr_orig);
4111 crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE,
4112 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE,
4115 page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT,
4118 btrfs_err(device->fs_info,
4119 "couldn't get super block page for bytenr %llu",
4125 /* Bump the refcount for wait_dev_supers() */
4128 disk_super = page_address(page);
4129 memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE);
4132 * Directly use bios here instead of relying on the page cache
4133 * to do I/O, so we don't lose the ability to do integrity
4136 bio = bio_alloc(device->bdev, 1,
4137 REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO,
4139 bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT;
4140 bio->bi_private = device;
4141 bio->bi_end_io = btrfs_end_super_write;
4142 __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE,
4143 offset_in_page(bytenr));
4146 * We FUA only the first super block. The others we allow to
4147 * go down lazy and there's a short window where the on-disk
4148 * copies might still contain the older version.
4150 if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
4151 bio->bi_opf |= REQ_FUA;
4153 btrfsic_check_bio(bio);
4156 if (btrfs_advance_sb_log(device, i))
4159 return errors < i ? 0 : -1;
4163 * Wait for write completion of superblocks done by write_dev_supers,
4164 * @max_mirrors same for write and wait phases.
4166 * Return number of errors when page is not found or not marked up to
4169 static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
4173 bool primary_failed = false;
4177 if (max_mirrors == 0)
4178 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
4180 for (i = 0; i < max_mirrors; i++) {
4183 ret = btrfs_sb_log_location(device, i, READ, &bytenr);
4184 if (ret == -ENOENT) {
4186 } else if (ret < 0) {
4189 primary_failed = true;
4192 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
4193 device->commit_total_bytes)
4196 page = find_get_page(device->bdev->bd_inode->i_mapping,
4197 bytenr >> PAGE_SHIFT);
4201 primary_failed = true;
4204 /* Page is submitted locked and unlocked once the IO completes */
4205 wait_on_page_locked(page);
4206 if (PageError(page)) {
4209 primary_failed = true;
4212 /* Drop our reference */
4215 /* Drop the reference from the writing run */
4219 /* log error, force error return */
4220 if (primary_failed) {
4221 btrfs_err(device->fs_info, "error writing primary super block to device %llu",
4226 return errors < i ? 0 : -1;
4230 * endio for the write_dev_flush, this will wake anyone waiting
4231 * for the barrier when it is done
4233 static void btrfs_end_empty_barrier(struct bio *bio)
4236 complete(bio->bi_private);
4240 * Submit a flush request to the device if it supports it. Error handling is
4241 * done in the waiting counterpart.
4243 static void write_dev_flush(struct btrfs_device *device)
4245 struct bio *bio = &device->flush_bio;
4247 #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4249 * When a disk has write caching disabled, we skip submission of a bio
4250 * with flush and sync requests before writing the superblock, since
4251 * it's not needed. However when the integrity checker is enabled, this
4252 * results in reports that there are metadata blocks referred by a
4253 * superblock that were not properly flushed. So don't skip the bio
4254 * submission only when the integrity checker is enabled for the sake
4255 * of simplicity, since this is a debug tool and not meant for use in
4258 if (!bdev_write_cache(device->bdev))
4262 bio_init(bio, device->bdev, NULL, 0,
4263 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
4264 bio->bi_end_io = btrfs_end_empty_barrier;
4265 init_completion(&device->flush_wait);
4266 bio->bi_private = &device->flush_wait;
4268 btrfsic_check_bio(bio);
4270 set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
4274 * If the flush bio has been submitted by write_dev_flush, wait for it.
4276 static blk_status_t wait_dev_flush(struct btrfs_device *device)
4278 struct bio *bio = &device->flush_bio;
4280 if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
4283 clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
4284 wait_for_completion_io(&device->flush_wait);
4286 return bio->bi_status;
4289 static int check_barrier_error(struct btrfs_fs_info *fs_info)
4291 if (!btrfs_check_rw_degradable(fs_info, NULL))
4297 * send an empty flush down to each device in parallel,
4298 * then wait for them
4300 static int barrier_all_devices(struct btrfs_fs_info *info)
4302 struct list_head *head;
4303 struct btrfs_device *dev;
4304 int errors_wait = 0;
4307 lockdep_assert_held(&info->fs_devices->device_list_mutex);
4308 /* send down all the barriers */
4309 head = &info->fs_devices->devices;
4310 list_for_each_entry(dev, head, dev_list) {
4311 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
4315 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4316 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4319 write_dev_flush(dev);
4320 dev->last_flush_error = BLK_STS_OK;
4323 /* wait for all the barriers */
4324 list_for_each_entry(dev, head, dev_list) {
4325 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
4331 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4332 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4335 ret = wait_dev_flush(dev);
4337 dev->last_flush_error = ret;
4338 btrfs_dev_stat_inc_and_print(dev,
4339 BTRFS_DEV_STAT_FLUSH_ERRS);
4346 * At some point we need the status of all disks
4347 * to arrive at the volume status. So error checking
4348 * is being pushed to a separate loop.
4350 return check_barrier_error(info);
4355 int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
4358 int min_tolerated = INT_MAX;
4360 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
4361 (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
4362 min_tolerated = min_t(int, min_tolerated,
4363 btrfs_raid_array[BTRFS_RAID_SINGLE].
4364 tolerated_failures);
4366 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4367 if (raid_type == BTRFS_RAID_SINGLE)
4369 if (!(flags & btrfs_raid_array[raid_type].bg_flag))
4371 min_tolerated = min_t(int, min_tolerated,
4372 btrfs_raid_array[raid_type].
4373 tolerated_failures);
4376 if (min_tolerated == INT_MAX) {
4377 pr_warn("BTRFS: unknown raid flag: %llu", flags);
4381 return min_tolerated;
4384 int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
4386 struct list_head *head;
4387 struct btrfs_device *dev;
4388 struct btrfs_super_block *sb;
4389 struct btrfs_dev_item *dev_item;
4393 int total_errors = 0;
4396 do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
4399 * max_mirrors == 0 indicates we're from commit_transaction,
4400 * not from fsync where the tree roots in fs_info have not
4401 * been consistent on disk.
4403 if (max_mirrors == 0)
4404 backup_super_roots(fs_info);
4406 sb = fs_info->super_for_commit;
4407 dev_item = &sb->dev_item;
4409 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4410 head = &fs_info->fs_devices->devices;
4411 max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
4414 ret = barrier_all_devices(fs_info);
4417 &fs_info->fs_devices->device_list_mutex);
4418 btrfs_handle_fs_error(fs_info, ret,
4419 "errors while submitting device barriers.");
4424 list_for_each_entry(dev, head, dev_list) {
4429 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4430 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4433 btrfs_set_stack_device_generation(dev_item, 0);
4434 btrfs_set_stack_device_type(dev_item, dev->type);
4435 btrfs_set_stack_device_id(dev_item, dev->devid);
4436 btrfs_set_stack_device_total_bytes(dev_item,
4437 dev->commit_total_bytes);
4438 btrfs_set_stack_device_bytes_used(dev_item,
4439 dev->commit_bytes_used);
4440 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
4441 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
4442 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
4443 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
4444 memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
4447 flags = btrfs_super_flags(sb);
4448 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
4450 ret = btrfs_validate_write_super(fs_info, sb);
4452 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4453 btrfs_handle_fs_error(fs_info, -EUCLEAN,
4454 "unexpected superblock corruption detected");
4458 ret = write_dev_supers(dev, sb, max_mirrors);
4462 if (total_errors > max_errors) {
4463 btrfs_err(fs_info, "%d errors while writing supers",
4465 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4467 /* FUA is masked off if unsupported and can't be the reason */
4468 btrfs_handle_fs_error(fs_info, -EIO,
4469 "%d errors while writing supers",
4475 list_for_each_entry(dev, head, dev_list) {
4478 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4479 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4482 ret = wait_dev_supers(dev, max_mirrors);
4486 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4487 if (total_errors > max_errors) {
4488 btrfs_handle_fs_error(fs_info, -EIO,
4489 "%d errors while writing supers",
4496 /* Drop a fs root from the radix tree and free it. */
4497 void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
4498 struct btrfs_root *root)
4500 bool drop_ref = false;
4502 spin_lock(&fs_info->fs_roots_lock);
4503 xa_erase(&fs_info->fs_roots, (unsigned long)root->root_key.objectid);
4504 if (test_and_clear_bit(BTRFS_ROOT_REGISTERED, &root->state))
4506 spin_unlock(&fs_info->fs_roots_lock);
4508 if (BTRFS_FS_ERROR(fs_info)) {
4509 ASSERT(root->log_root == NULL);
4510 if (root->reloc_root) {
4511 btrfs_put_root(root->reloc_root);
4512 root->reloc_root = NULL;
4517 btrfs_put_root(root);
4520 int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
4522 struct btrfs_root *roots[8];
4523 unsigned long index = 0;
4529 struct btrfs_root *root;
4531 spin_lock(&fs_info->fs_roots_lock);
4532 if (!xa_find(&fs_info->fs_roots, &index, ULONG_MAX, XA_PRESENT)) {
4533 spin_unlock(&fs_info->fs_roots_lock);
4538 xa_for_each_start(&fs_info->fs_roots, index, root, index) {
4539 /* Avoid grabbing roots in dead_roots */
4540 if (btrfs_root_refs(&root->root_item) > 0)
4541 roots[grabbed++] = btrfs_grab_root(root);
4542 if (grabbed >= ARRAY_SIZE(roots))
4545 spin_unlock(&fs_info->fs_roots_lock);
4547 for (i = 0; i < grabbed; i++) {
4550 index = roots[i]->root_key.objectid;
4551 err = btrfs_orphan_cleanup(roots[i]);
4554 btrfs_put_root(roots[i]);
4560 /* Release the roots that remain uncleaned due to error */
4561 for (; i < grabbed; i++) {
4563 btrfs_put_root(roots[i]);
4568 int btrfs_commit_super(struct btrfs_fs_info *fs_info)
4570 struct btrfs_root *root = fs_info->tree_root;
4571 struct btrfs_trans_handle *trans;
4573 mutex_lock(&fs_info->cleaner_mutex);
4574 btrfs_run_delayed_iputs(fs_info);
4575 mutex_unlock(&fs_info->cleaner_mutex);
4576 wake_up_process(fs_info->cleaner_kthread);
4578 /* wait until ongoing cleanup work done */
4579 down_write(&fs_info->cleanup_work_sem);
4580 up_write(&fs_info->cleanup_work_sem);
4582 trans = btrfs_join_transaction(root);
4584 return PTR_ERR(trans);
4585 return btrfs_commit_transaction(trans);
4588 static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
4590 struct btrfs_transaction *trans;
4591 struct btrfs_transaction *tmp;
4594 if (list_empty(&fs_info->trans_list))
4598 * This function is only called at the very end of close_ctree(),
4599 * thus no other running transaction, no need to take trans_lock.
4601 ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags));
4602 list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) {
4603 struct extent_state *cached = NULL;
4604 u64 dirty_bytes = 0;
4610 while (!find_first_extent_bit(&trans->dirty_pages, cur,
4611 &found_start, &found_end, EXTENT_DIRTY, &cached)) {
4612 dirty_bytes += found_end + 1 - found_start;
4613 cur = found_end + 1;
4616 "transaction %llu (with %llu dirty metadata bytes) is not committed",
4617 trans->transid, dirty_bytes);
4618 btrfs_cleanup_one_transaction(trans, fs_info);
4620 if (trans == fs_info->running_transaction)
4621 fs_info->running_transaction = NULL;
4622 list_del_init(&trans->list);
4624 btrfs_put_transaction(trans);
4625 trace_btrfs_transaction_commit(fs_info);
4630 void __cold close_ctree(struct btrfs_fs_info *fs_info)
4634 set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
4636 * We don't want the cleaner to start new transactions, add more delayed
4637 * iputs, etc. while we're closing. We can't use kthread_stop() yet
4638 * because that frees the task_struct, and the transaction kthread might
4639 * still try to wake up the cleaner.
4641 kthread_park(fs_info->cleaner_kthread);
4644 * If we had UNFINISHED_DROPS we could still be processing them, so
4645 * clear that bit and wake up relocation so it can stop.
4647 btrfs_wake_unfinished_drop(fs_info);
4649 /* wait for the qgroup rescan worker to stop */
4650 btrfs_qgroup_wait_for_completion(fs_info, false);
4652 /* wait for the uuid_scan task to finish */
4653 down(&fs_info->uuid_tree_rescan_sem);
4654 /* avoid complains from lockdep et al., set sem back to initial state */
4655 up(&fs_info->uuid_tree_rescan_sem);
4657 /* pause restriper - we want to resume on mount */
4658 btrfs_pause_balance(fs_info);
4660 btrfs_dev_replace_suspend_for_unmount(fs_info);
4662 btrfs_scrub_cancel(fs_info);
4664 /* wait for any defraggers to finish */
4665 wait_event(fs_info->transaction_wait,
4666 (atomic_read(&fs_info->defrag_running) == 0));
4668 /* clear out the rbtree of defraggable inodes */
4669 btrfs_cleanup_defrag_inodes(fs_info);
4671 cancel_work_sync(&fs_info->async_reclaim_work);
4672 cancel_work_sync(&fs_info->async_data_reclaim_work);
4673 cancel_work_sync(&fs_info->preempt_reclaim_work);
4675 cancel_work_sync(&fs_info->reclaim_bgs_work);
4677 /* Cancel or finish ongoing discard work */
4678 btrfs_discard_cleanup(fs_info);
4680 if (!sb_rdonly(fs_info->sb)) {
4682 * The cleaner kthread is stopped, so do one final pass over
4683 * unused block groups.
4685 btrfs_delete_unused_bgs(fs_info);
4688 * There might be existing delayed inode workers still running
4689 * and holding an empty delayed inode item. We must wait for
4690 * them to complete first because they can create a transaction.
4691 * This happens when someone calls btrfs_balance_delayed_items()
4692 * and then a transaction commit runs the same delayed nodes
4693 * before any delayed worker has done something with the nodes.
4694 * We must wait for any worker here and not at transaction
4695 * commit time since that could cause a deadlock.
4696 * This is a very rare case.
4698 btrfs_flush_workqueue(fs_info->delayed_workers);
4700 ret = btrfs_commit_super(fs_info);
4702 btrfs_err(fs_info, "commit super ret %d", ret);
4705 if (BTRFS_FS_ERROR(fs_info))
4706 btrfs_error_commit_super(fs_info);
4708 kthread_stop(fs_info->transaction_kthread);
4709 kthread_stop(fs_info->cleaner_kthread);
4711 ASSERT(list_empty(&fs_info->delayed_iputs));
4712 set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
4714 if (btrfs_check_quota_leak(fs_info)) {
4715 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4716 btrfs_err(fs_info, "qgroup reserved space leaked");
4719 btrfs_free_qgroup_config(fs_info);
4720 ASSERT(list_empty(&fs_info->delalloc_roots));
4722 if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
4723 btrfs_info(fs_info, "at unmount delalloc count %lld",
4724 percpu_counter_sum(&fs_info->delalloc_bytes));
4727 if (percpu_counter_sum(&fs_info->ordered_bytes))
4728 btrfs_info(fs_info, "at unmount dio bytes count %lld",
4729 percpu_counter_sum(&fs_info->ordered_bytes));
4731 btrfs_sysfs_remove_mounted(fs_info);
4732 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
4734 btrfs_put_block_group_cache(fs_info);
4737 * we must make sure there is not any read request to
4738 * submit after we stopping all workers.
4740 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
4741 btrfs_stop_all_workers(fs_info);
4743 /* We shouldn't have any transaction open at this point */
4744 warn_about_uncommitted_trans(fs_info);
4746 clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
4747 free_root_pointers(fs_info, true);
4748 btrfs_free_fs_roots(fs_info);
4751 * We must free the block groups after dropping the fs_roots as we could
4752 * have had an IO error and have left over tree log blocks that aren't
4753 * cleaned up until the fs roots are freed. This makes the block group
4754 * accounting appear to be wrong because there's pending reserved bytes,
4755 * so make sure we do the block group cleanup afterwards.
4757 btrfs_free_block_groups(fs_info);
4759 iput(fs_info->btree_inode);
4761 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4762 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
4763 btrfsic_unmount(fs_info->fs_devices);
4766 btrfs_mapping_tree_free(&fs_info->mapping_tree);
4767 btrfs_close_devices(fs_info->fs_devices);
4770 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
4774 struct inode *btree_inode = buf->pages[0]->mapping->host;
4776 ret = extent_buffer_uptodate(buf);
4780 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
4781 parent_transid, atomic);
4787 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4789 struct btrfs_fs_info *fs_info = buf->fs_info;
4790 u64 transid = btrfs_header_generation(buf);
4793 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4795 * This is a fast path so only do this check if we have sanity tests
4796 * enabled. Normal people shouldn't be using unmapped buffers as dirty
4797 * outside of the sanity tests.
4799 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
4802 btrfs_assert_tree_write_locked(buf);
4803 if (transid != fs_info->generation)
4804 WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
4805 buf->start, transid, fs_info->generation);
4806 was_dirty = set_extent_buffer_dirty(buf);
4808 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4810 fs_info->dirty_metadata_batch);
4811 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4813 * Since btrfs_mark_buffer_dirty() can be called with item pointer set
4814 * but item data not updated.
4815 * So here we should only check item pointers, not item data.
4817 if (btrfs_header_level(buf) == 0 &&
4818 btrfs_check_leaf_relaxed(buf)) {
4819 btrfs_print_leaf(buf);
4825 static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
4829 * looks as though older kernels can get into trouble with
4830 * this code, they end up stuck in balance_dirty_pages forever
4834 if (current->flags & PF_MEMALLOC)
4838 btrfs_balance_delayed_items(fs_info);
4840 ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4841 BTRFS_DIRTY_METADATA_THRESH,
4842 fs_info->dirty_metadata_batch);
4844 balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
4848 void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
4850 __btrfs_btree_balance_dirty(fs_info, 1);
4853 void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
4855 __btrfs_btree_balance_dirty(fs_info, 0);
4858 static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
4860 /* cleanup FS via transaction */
4861 btrfs_cleanup_transaction(fs_info);
4863 mutex_lock(&fs_info->cleaner_mutex);
4864 btrfs_run_delayed_iputs(fs_info);
4865 mutex_unlock(&fs_info->cleaner_mutex);
4867 down_write(&fs_info->cleanup_work_sem);
4868 up_write(&fs_info->cleanup_work_sem);
4871 static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info)
4873 unsigned long index = 0;
4875 struct btrfs_root *roots[8];
4877 spin_lock(&fs_info->fs_roots_lock);
4878 while ((grabbed = xa_extract(&fs_info->fs_roots, (void **)roots, index,
4879 ULONG_MAX, 8, XA_PRESENT))) {
4880 for (int i = 0; i < grabbed; i++)
4881 roots[i] = btrfs_grab_root(roots[i]);
4882 spin_unlock(&fs_info->fs_roots_lock);
4884 for (int i = 0; i < grabbed; i++) {
4887 index = roots[i]->root_key.objectid;
4888 btrfs_free_log(NULL, roots[i]);
4889 btrfs_put_root(roots[i]);
4892 spin_lock(&fs_info->fs_roots_lock);
4894 spin_unlock(&fs_info->fs_roots_lock);
4895 btrfs_free_log_root_tree(NULL, fs_info);
4898 static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
4900 struct btrfs_ordered_extent *ordered;
4902 spin_lock(&root->ordered_extent_lock);
4904 * This will just short circuit the ordered completion stuff which will
4905 * make sure the ordered extent gets properly cleaned up.
4907 list_for_each_entry(ordered, &root->ordered_extents,
4909 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
4910 spin_unlock(&root->ordered_extent_lock);
4913 static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4915 struct btrfs_root *root;
4916 struct list_head splice;
4918 INIT_LIST_HEAD(&splice);
4920 spin_lock(&fs_info->ordered_root_lock);
4921 list_splice_init(&fs_info->ordered_roots, &splice);
4922 while (!list_empty(&splice)) {
4923 root = list_first_entry(&splice, struct btrfs_root,
4925 list_move_tail(&root->ordered_root,
4926 &fs_info->ordered_roots);
4928 spin_unlock(&fs_info->ordered_root_lock);
4929 btrfs_destroy_ordered_extents(root);
4932 spin_lock(&fs_info->ordered_root_lock);
4934 spin_unlock(&fs_info->ordered_root_lock);
4937 * We need this here because if we've been flipped read-only we won't
4938 * get sync() from the umount, so we need to make sure any ordered
4939 * extents that haven't had their dirty pages IO start writeout yet
4940 * actually get run and error out properly.
4942 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
4945 static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4946 struct btrfs_fs_info *fs_info)
4948 struct rb_node *node;
4949 struct btrfs_delayed_ref_root *delayed_refs;
4950 struct btrfs_delayed_ref_node *ref;
4953 delayed_refs = &trans->delayed_refs;
4955 spin_lock(&delayed_refs->lock);
4956 if (atomic_read(&delayed_refs->num_entries) == 0) {
4957 spin_unlock(&delayed_refs->lock);
4958 btrfs_debug(fs_info, "delayed_refs has NO entry");
4962 while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
4963 struct btrfs_delayed_ref_head *head;
4965 bool pin_bytes = false;
4967 head = rb_entry(node, struct btrfs_delayed_ref_head,
4969 if (btrfs_delayed_ref_lock(delayed_refs, head))
4972 spin_lock(&head->lock);
4973 while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
4974 ref = rb_entry(n, struct btrfs_delayed_ref_node,
4977 rb_erase_cached(&ref->ref_node, &head->ref_tree);
4978 RB_CLEAR_NODE(&ref->ref_node);
4979 if (!list_empty(&ref->add_list))
4980 list_del(&ref->add_list);
4981 atomic_dec(&delayed_refs->num_entries);
4982 btrfs_put_delayed_ref(ref);
4984 if (head->must_insert_reserved)
4986 btrfs_free_delayed_extent_op(head->extent_op);
4987 btrfs_delete_ref_head(delayed_refs, head);
4988 spin_unlock(&head->lock);
4989 spin_unlock(&delayed_refs->lock);
4990 mutex_unlock(&head->mutex);
4993 struct btrfs_block_group *cache;
4995 cache = btrfs_lookup_block_group(fs_info, head->bytenr);
4998 spin_lock(&cache->space_info->lock);
4999 spin_lock(&cache->lock);
5000 cache->pinned += head->num_bytes;
5001 btrfs_space_info_update_bytes_pinned(fs_info,
5002 cache->space_info, head->num_bytes);
5003 cache->reserved -= head->num_bytes;
5004 cache->space_info->bytes_reserved -= head->num_bytes;
5005 spin_unlock(&cache->lock);
5006 spin_unlock(&cache->space_info->lock);
5008 btrfs_put_block_group(cache);
5010 btrfs_error_unpin_extent_range(fs_info, head->bytenr,
5011 head->bytenr + head->num_bytes - 1);
5013 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
5014 btrfs_put_delayed_ref_head(head);
5016 spin_lock(&delayed_refs->lock);
5018 btrfs_qgroup_destroy_extent_records(trans);
5020 spin_unlock(&delayed_refs->lock);
5025 static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
5027 struct btrfs_inode *btrfs_inode;
5028 struct list_head splice;
5030 INIT_LIST_HEAD(&splice);
5032 spin_lock(&root->delalloc_lock);
5033 list_splice_init(&root->delalloc_inodes, &splice);
5035 while (!list_empty(&splice)) {
5036 struct inode *inode = NULL;
5037 btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
5039 __btrfs_del_delalloc_inode(root, btrfs_inode);
5040 spin_unlock(&root->delalloc_lock);
5043 * Make sure we get a live inode and that it'll not disappear
5046 inode = igrab(&btrfs_inode->vfs_inode);
5048 invalidate_inode_pages2(inode->i_mapping);
5051 spin_lock(&root->delalloc_lock);
5053 spin_unlock(&root->delalloc_lock);
5056 static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
5058 struct btrfs_root *root;
5059 struct list_head splice;
5061 INIT_LIST_HEAD(&splice);
5063 spin_lock(&fs_info->delalloc_root_lock);
5064 list_splice_init(&fs_info->delalloc_roots, &splice);
5065 while (!list_empty(&splice)) {
5066 root = list_first_entry(&splice, struct btrfs_root,
5068 root = btrfs_grab_root(root);
5070 spin_unlock(&fs_info->delalloc_root_lock);
5072 btrfs_destroy_delalloc_inodes(root);
5073 btrfs_put_root(root);
5075 spin_lock(&fs_info->delalloc_root_lock);
5077 spin_unlock(&fs_info->delalloc_root_lock);
5080 static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
5081 struct extent_io_tree *dirty_pages,
5085 struct extent_buffer *eb;
5090 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
5095 clear_extent_bits(dirty_pages, start, end, mark);
5096 while (start <= end) {
5097 eb = find_extent_buffer(fs_info, start);
5098 start += fs_info->nodesize;
5101 wait_on_extent_buffer_writeback(eb);
5103 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
5105 clear_extent_buffer_dirty(eb);
5106 free_extent_buffer_stale(eb);
5113 static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
5114 struct extent_io_tree *unpin)
5121 struct extent_state *cached_state = NULL;
5124 * The btrfs_finish_extent_commit() may get the same range as
5125 * ours between find_first_extent_bit and clear_extent_dirty.
5126 * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
5127 * the same extent range.
5129 mutex_lock(&fs_info->unused_bg_unpin_mutex);
5130 ret = find_first_extent_bit(unpin, 0, &start, &end,
5131 EXTENT_DIRTY, &cached_state);
5133 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
5137 clear_extent_dirty(unpin, start, end, &cached_state);
5138 free_extent_state(cached_state);
5139 btrfs_error_unpin_extent_range(fs_info, start, end);
5140 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
5147 static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
5149 struct inode *inode;
5151 inode = cache->io_ctl.inode;
5153 invalidate_inode_pages2(inode->i_mapping);
5154 BTRFS_I(inode)->generation = 0;
5155 cache->io_ctl.inode = NULL;
5158 ASSERT(cache->io_ctl.pages == NULL);
5159 btrfs_put_block_group(cache);
5162 void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
5163 struct btrfs_fs_info *fs_info)
5165 struct btrfs_block_group *cache;
5167 spin_lock(&cur_trans->dirty_bgs_lock);
5168 while (!list_empty(&cur_trans->dirty_bgs)) {
5169 cache = list_first_entry(&cur_trans->dirty_bgs,
5170 struct btrfs_block_group,
5173 if (!list_empty(&cache->io_list)) {
5174 spin_unlock(&cur_trans->dirty_bgs_lock);
5175 list_del_init(&cache->io_list);
5176 btrfs_cleanup_bg_io(cache);
5177 spin_lock(&cur_trans->dirty_bgs_lock);
5180 list_del_init(&cache->dirty_list);
5181 spin_lock(&cache->lock);
5182 cache->disk_cache_state = BTRFS_DC_ERROR;
5183 spin_unlock(&cache->lock);
5185 spin_unlock(&cur_trans->dirty_bgs_lock);
5186 btrfs_put_block_group(cache);
5187 btrfs_delayed_refs_rsv_release(fs_info, 1);
5188 spin_lock(&cur_trans->dirty_bgs_lock);
5190 spin_unlock(&cur_trans->dirty_bgs_lock);
5193 * Refer to the definition of io_bgs member for details why it's safe
5194 * to use it without any locking
5196 while (!list_empty(&cur_trans->io_bgs)) {
5197 cache = list_first_entry(&cur_trans->io_bgs,
5198 struct btrfs_block_group,
5201 list_del_init(&cache->io_list);
5202 spin_lock(&cache->lock);
5203 cache->disk_cache_state = BTRFS_DC_ERROR;
5204 spin_unlock(&cache->lock);
5205 btrfs_cleanup_bg_io(cache);
5209 void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
5210 struct btrfs_fs_info *fs_info)
5212 struct btrfs_device *dev, *tmp;
5214 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
5215 ASSERT(list_empty(&cur_trans->dirty_bgs));
5216 ASSERT(list_empty(&cur_trans->io_bgs));
5218 list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
5220 list_del_init(&dev->post_commit_list);
5223 btrfs_destroy_delayed_refs(cur_trans, fs_info);
5225 cur_trans->state = TRANS_STATE_COMMIT_START;
5226 wake_up(&fs_info->transaction_blocked_wait);
5228 cur_trans->state = TRANS_STATE_UNBLOCKED;
5229 wake_up(&fs_info->transaction_wait);
5231 btrfs_destroy_delayed_inodes(fs_info);
5233 btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
5235 btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
5237 btrfs_free_redirty_list(cur_trans);
5239 cur_trans->state =TRANS_STATE_COMPLETED;
5240 wake_up(&cur_trans->commit_wait);
5243 static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
5245 struct btrfs_transaction *t;
5247 mutex_lock(&fs_info->transaction_kthread_mutex);
5249 spin_lock(&fs_info->trans_lock);
5250 while (!list_empty(&fs_info->trans_list)) {
5251 t = list_first_entry(&fs_info->trans_list,
5252 struct btrfs_transaction, list);
5253 if (t->state >= TRANS_STATE_COMMIT_START) {
5254 refcount_inc(&t->use_count);
5255 spin_unlock(&fs_info->trans_lock);
5256 btrfs_wait_for_commit(fs_info, t->transid);
5257 btrfs_put_transaction(t);
5258 spin_lock(&fs_info->trans_lock);
5261 if (t == fs_info->running_transaction) {
5262 t->state = TRANS_STATE_COMMIT_DOING;
5263 spin_unlock(&fs_info->trans_lock);
5265 * We wait for 0 num_writers since we don't hold a trans
5266 * handle open currently for this transaction.
5268 wait_event(t->writer_wait,
5269 atomic_read(&t->num_writers) == 0);
5271 spin_unlock(&fs_info->trans_lock);
5273 btrfs_cleanup_one_transaction(t, fs_info);
5275 spin_lock(&fs_info->trans_lock);
5276 if (t == fs_info->running_transaction)
5277 fs_info->running_transaction = NULL;
5278 list_del_init(&t->list);
5279 spin_unlock(&fs_info->trans_lock);
5281 btrfs_put_transaction(t);
5282 trace_btrfs_transaction_commit(fs_info);
5283 spin_lock(&fs_info->trans_lock);
5285 spin_unlock(&fs_info->trans_lock);
5286 btrfs_destroy_all_ordered_extents(fs_info);
5287 btrfs_destroy_delayed_inodes(fs_info);
5288 btrfs_assert_delayed_root_empty(fs_info);
5289 btrfs_destroy_all_delalloc_inodes(fs_info);
5290 btrfs_drop_all_logs(fs_info);
5291 mutex_unlock(&fs_info->transaction_kthread_mutex);
5296 int btrfs_init_root_free_objectid(struct btrfs_root *root)
5298 struct btrfs_path *path;
5300 struct extent_buffer *l;
5301 struct btrfs_key search_key;
5302 struct btrfs_key found_key;
5305 path = btrfs_alloc_path();
5309 search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
5310 search_key.type = -1;
5311 search_key.offset = (u64)-1;
5312 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5315 BUG_ON(ret == 0); /* Corruption */
5316 if (path->slots[0] > 0) {
5317 slot = path->slots[0] - 1;
5319 btrfs_item_key_to_cpu(l, &found_key, slot);
5320 root->free_objectid = max_t(u64, found_key.objectid + 1,
5321 BTRFS_FIRST_FREE_OBJECTID);
5323 root->free_objectid = BTRFS_FIRST_FREE_OBJECTID;
5327 btrfs_free_path(path);
5331 int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid)
5334 mutex_lock(&root->objectid_mutex);
5336 if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
5337 btrfs_warn(root->fs_info,
5338 "the objectid of root %llu reaches its highest value",
5339 root->root_key.objectid);
5344 *objectid = root->free_objectid++;
5347 mutex_unlock(&root->objectid_mutex);