^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/page-flags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pagevec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/prefetch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/cleancache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "extent_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "extent-io-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "extent_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "check-integrity.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "rcu-string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "backref.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct kmem_cache *extent_state_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct kmem_cache *extent_buffer_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct bio_set btrfs_bioset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static inline bool extent_state_in_tree(const struct extent_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return !RB_EMPTY_NODE(&state->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #ifdef CONFIG_BTRFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static LIST_HEAD(states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static DEFINE_SPINLOCK(leak_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static inline void btrfs_leak_debug_add(spinlock_t *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct list_head *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) spin_lock_irqsave(lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) list_add(new, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) spin_unlock_irqrestore(lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline void btrfs_leak_debug_del(spinlock_t *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct list_head *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) spin_lock_irqsave(lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) list_del(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) spin_unlock_irqrestore(lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * If we didn't get into open_ctree our allocated_ebs will not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * initialized, so just skip this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!fs_info->allocated_ebs.next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) while (!list_empty(&fs_info->allocated_ebs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) eb = list_first_entry(&fs_info->allocated_ebs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct extent_buffer, leak_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) eb->start, eb->len, atomic_read(&eb->refs), eb->bflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) btrfs_header_owner(eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) list_del(&eb->leak_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) kmem_cache_free(extent_buffer_cache, eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static inline void btrfs_extent_state_leak_debug_check(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) while (!list_empty(&states)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) state = list_entry(states.next, struct extent_state, leak_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) state->start, state->end, state->state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) extent_state_in_tree(state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) refcount_read(&state->refs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) list_del(&state->leak_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) kmem_cache_free(extent_state_cache, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define btrfs_debug_check_extent_io_range(tree, start, end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline void __btrfs_debug_check_extent_io_range(const char *caller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct extent_io_tree *tree, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct inode *inode = tree->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u64 isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!inode || !is_data_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) "%s: ino %llu isize %llu odd range [%llu,%llu]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define btrfs_leak_debug_add(lock, new, head) do {} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #define btrfs_leak_debug_del(lock, entry) do {} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #define btrfs_extent_state_leak_debug_check() do {} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct tree_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct rb_node rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct extent_page_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* tells writepage not to lock the state bits for this range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * it still does the unlocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned int extent_locked:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* tells the submit_bio code to use REQ_SYNC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) unsigned int sync_io:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int add_extent_changeset(struct extent_state *state, unsigned bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct extent_changeset *changeset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (!changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (set && (state->state & bits) == bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (!set && (state->state & bits) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) changeset->bytes_changed += state->end - state->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = ulist_add(&changeset->range_changed, state->start, state->end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int __must_check submit_one_bio(struct bio *bio, int mirror_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unsigned long bio_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) blk_status_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct extent_io_tree *tree = bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) bio->bi_private = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (is_data_inode(tree->private_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = btrfs_submit_data_bio(tree->private_data, bio, mirror_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) bio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ret = btrfs_submit_metadata_bio(tree->private_data, bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) mirror_num, bio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return blk_status_to_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* Cleanup unsubmitted bios */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static void end_write_bio(struct extent_page_data *epd, int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (epd->bio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) epd->bio->bi_status = errno_to_blk_status(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) bio_endio(epd->bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) epd->bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Submit bio from extent page data via submit_one_bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Return 0 if everything is OK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * Return <0 for error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int __must_check flush_write_bio(struct extent_page_data *epd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (epd->bio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ret = submit_one_bio(epd->bio, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * Clean up of epd->bio is handled by its endio function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * And endio is either triggered by successful bio execution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * or the error handler of submit bio hook.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * So at this point, no matter what happened, we don't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * to clean up epd->bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) epd->bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int __init extent_state_cache_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) extent_state_cache = kmem_cache_create("btrfs_extent_state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) sizeof(struct extent_state), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) SLAB_MEM_SPREAD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!extent_state_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int __init extent_io_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) sizeof(struct extent_buffer), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) SLAB_MEM_SPREAD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!extent_buffer_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) offsetof(struct btrfs_io_bio, bio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) BIOSET_NEED_BVECS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) goto free_buffer_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto free_bioset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) free_bioset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) bioset_exit(&btrfs_bioset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) free_buffer_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) kmem_cache_destroy(extent_buffer_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) extent_buffer_cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) void __cold extent_state_cache_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) btrfs_extent_state_leak_debug_check();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) kmem_cache_destroy(extent_state_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) void __cold extent_io_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * Make sure all delayed rcu free are flushed before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * destroy caches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) kmem_cache_destroy(extent_buffer_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) bioset_exit(&btrfs_bioset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * For the file_extent_tree, we want to hold the inode lock when we lookup and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * update the disk_i_size, but lockdep will complain because our io_tree we hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * the tree lock and get the inode lock when setting delalloc. These two things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * are unrelated, so make a class for the file_extent_tree so we don't get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * two locking patterns mixed up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static struct lock_class_key file_extent_tree_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) void extent_io_tree_init(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct extent_io_tree *tree, unsigned int owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) void *private_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) tree->fs_info = fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) tree->state = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) tree->dirty_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) spin_lock_init(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) tree->private_data = private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) tree->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (owner == IO_TREE_INODE_FILE_EXTENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) lockdep_set_class(&tree->lock, &file_extent_tree_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) void extent_io_tree_release(struct extent_io_tree *tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * Do a single barrier for the waitqueue_active check here, the state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * of the waitqueue should not change once extent_io_tree_release is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) while (!RB_EMPTY_ROOT(&tree->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) node = rb_first(&tree->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) rb_erase(&state->rb_node, &tree->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) RB_CLEAR_NODE(&state->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * btree io trees aren't supposed to have tasks waiting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * changes in the flags of extent states ever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ASSERT(!waitqueue_active(&state->wq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) free_extent_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) cond_resched_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct extent_state *alloc_extent_state(gfp_t mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * The given mask might be not appropriate for the slab allocator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * drop the unsupported bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) state = kmem_cache_alloc(extent_state_cache, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (!state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) state->state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) state->failrec = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) RB_CLEAR_NODE(&state->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) refcount_set(&state->refs, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) init_waitqueue_head(&state->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) trace_alloc_extent_state(state, mask, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) void free_extent_state(struct extent_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (!state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (refcount_dec_and_test(&state->refs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) WARN_ON(extent_state_in_tree(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) btrfs_leak_debug_del(&leak_lock, &state->leak_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) trace_free_extent_state(state, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) kmem_cache_free(extent_state_cache, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static struct rb_node *tree_insert(struct rb_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct rb_node *search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct rb_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct rb_node ***p_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct rb_node **parent_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct rb_node **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct tree_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (p_in && parent_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) p = *p_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) parent = *parent_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) goto do_insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) p = search_start ? &search_start : &root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) entry = rb_entry(parent, struct tree_entry, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (offset < entry->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) else if (offset > entry->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) do_insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) rb_link_node(node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) rb_insert_color(node, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * __etree_search - searche @tree for an entry that contains @offset. Such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * entry would have entry->start <= offset && entry->end >= offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * @tree - the tree to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * @offset - offset that should fall within an entry in @tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * @next_ret - pointer to the first entry whose range ends after @offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * @prev - pointer to the first entry whose range begins before @offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * @p_ret - pointer where new node should be anchored (used when inserting an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * entry in the tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * @parent_ret - points to entry which would have been the parent of the entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * containing @offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * This function returns a pointer to the entry that contains @offset byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * address. If no such entry exists, then NULL is returned and the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * pointer arguments to the function are filled, otherwise the found entry is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * returned and other pointers are left untouched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct rb_node **next_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct rb_node **prev_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) struct rb_node ***p_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct rb_node **parent_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct rb_root *root = &tree->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct rb_node **n = &root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct rb_node *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct rb_node *orig_prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct tree_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct tree_entry *prev_entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) while (*n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) prev = *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) entry = rb_entry(prev, struct tree_entry, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) prev_entry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (offset < entry->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) n = &(*n)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) else if (offset > entry->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) n = &(*n)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (p_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *p_ret = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (parent_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) *parent_ret = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (next_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) orig_prev = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) while (prev && offset > prev_entry->end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) prev = rb_next(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) prev_entry = rb_entry(prev, struct tree_entry, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) *next_ret = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) prev = orig_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (prev_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) prev_entry = rb_entry(prev, struct tree_entry, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) while (prev && offset < prev_entry->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) prev = rb_prev(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) prev_entry = rb_entry(prev, struct tree_entry, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) *prev_ret = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static inline struct rb_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) tree_search_for_insert(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct rb_node ***p_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct rb_node **parent_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct rb_node *next= NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) struct rb_node *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static inline struct rb_node *tree_search(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return tree_search_for_insert(tree, offset, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * utility function to look for merge candidates inside a given range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * Any extents with matching state are merged together into a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * extent in the tree. Extents with EXTENT_IO in their state field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * are not merged because the end_io handlers need to be able to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * operations on them without sleeping (or doing allocations/splits).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * This should be called with the tree lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static void merge_state(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct extent_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct extent_state *other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct rb_node *other_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) other_node = rb_prev(&state->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (other_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) other = rb_entry(other_node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (other->end == state->start - 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) other->state == state->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (tree->private_data &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) is_data_inode(tree->private_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) btrfs_merge_delalloc_extent(tree->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) state, other);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) state->start = other->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) rb_erase(&other->rb_node, &tree->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) RB_CLEAR_NODE(&other->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) free_extent_state(other);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) other_node = rb_next(&state->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (other_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) other = rb_entry(other_node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (other->start == state->end + 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) other->state == state->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (tree->private_data &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) is_data_inode(tree->private_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) btrfs_merge_delalloc_extent(tree->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) state, other);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) state->end = other->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) rb_erase(&other->rb_node, &tree->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) RB_CLEAR_NODE(&other->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) free_extent_state(other);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static void set_state_bits(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct extent_state *state, unsigned *bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) struct extent_changeset *changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * insert an extent_state struct into the tree. 'bits' are set on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * struct before it is inserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * This may return -EEXIST if the extent is already there, in which case the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * state struct is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * The tree lock is not taken internally. This is a utility function and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * probably isn't what you want to call (see set/clear_extent_bit).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static int insert_state(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct extent_state *state, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct rb_node ***p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct rb_node **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) unsigned *bits, struct extent_changeset *changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (end < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) btrfs_err(tree->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) "insert state: end < start %llu %llu", end, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) state->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) state->end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) set_state_bits(tree, state, bits, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct extent_state *found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) found = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) btrfs_err(tree->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) "found node %llu %llu on insert of %llu %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) found->start, found->end, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) merge_state(tree, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * split a given extent state struct in two, inserting the preallocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * struct 'prealloc' as the newly created second half. 'split' indicates an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * offset inside 'orig' where it should be split.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * Before calling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * the tree has 'orig' at [orig->start, orig->end]. After calling, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * are two extent state structs in the tree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * prealloc: [orig->start, split - 1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * orig: [ split, orig->end ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * The tree locks are not taken by this function. They need to be held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct extent_state *prealloc, u64 split)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (tree->private_data && is_data_inode(tree->private_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) btrfs_split_delalloc_extent(tree->private_data, orig, split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) prealloc->start = orig->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) prealloc->end = split - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) prealloc->state = orig->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) orig->start = split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) &prealloc->rb_node, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) free_extent_state(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) static struct extent_state *next_state(struct extent_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct rb_node *next = rb_next(&state->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return rb_entry(next, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * utility function to clear some bits in an extent state struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * it will optionally wake up anyone waiting on this state (wake == 1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * If no bits are set on the state struct after clearing things, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * struct is freed and removed from the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct extent_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) unsigned *bits, int wake,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct extent_changeset *changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct extent_state *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) u64 range = state->end - state->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) WARN_ON(range > tree->dirty_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) tree->dirty_bytes -= range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (tree->private_data && is_data_inode(tree->private_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) btrfs_clear_delalloc_extent(tree->private_data, state, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) state->state &= ~bits_to_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) wake_up(&state->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (state->state == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) next = next_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (extent_state_in_tree(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) rb_erase(&state->rb_node, &tree->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) RB_CLEAR_NODE(&state->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) free_extent_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) merge_state(tree, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) next = next_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static struct extent_state *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) alloc_extent_state_atomic(struct extent_state *prealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (!prealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) prealloc = alloc_extent_state(GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return prealloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) btrfs_panic(tree->fs_info, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) "locking error: extent tree was modified by another thread while locked");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * clear some bits on a range in the tree. This may require splitting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * or inserting elements in the tree, so the gfp mask is used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * indicate which allocations or sleeping are allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * the given range from the tree regardless of state (ie for truncate).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * the range [start, end] is inclusive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * This takes the tree lock, and returns 0 on success and < 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) unsigned bits, int wake, int delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) struct extent_state **cached_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) gfp_t mask, struct extent_changeset *changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) struct extent_state *cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) struct extent_state *prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) u64 last_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) int clear = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) btrfs_debug_check_extent_io_range(tree, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (bits & EXTENT_DELALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) bits |= EXTENT_NORESERVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (delete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) bits |= ~EXTENT_CTLBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) clear = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (!prealloc && gfpflags_allow_blocking(mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * Don't care for allocation failure here because we might end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * up not needing the pre-allocated extent state at all, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * is the case if we only have in the tree extent states that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * cover our input range and don't cover too any other range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * If we end up needing a new extent state we allocate it later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) prealloc = alloc_extent_state(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (cached_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) cached = *cached_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) if (clear) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (cached && extent_state_in_tree(cached) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) cached->start <= start && cached->end > start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) refcount_dec(&cached->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) state = cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) free_extent_state(cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * this search will find the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * our range starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) node = tree_search(tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) hit_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (state->start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) WARN_ON(state->end < start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) last_end = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) /* the state doesn't have the wanted bits, go ahead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (!(state->state & bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) state = next_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * | state | or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * | ------------- state -------------- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * We need to split the extent we found, and may flip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * bits on second half.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * If the extent we found extends past our range, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) * just split and search again. It'll get split again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * the next time though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * If the extent we found is inside our range, we clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * the desired bit on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (state->start < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) BUG_ON(!prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) err = split_state(tree, state, prealloc, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (state->end <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) state = clear_state_bit(tree, state, &bits, wake,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * We need to split the extent, and clear the bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * on the first half
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (state->start <= end && state->end > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) BUG_ON(!prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) err = split_state(tree, state, prealloc, end + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) wake_up(&state->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) clear_state_bit(tree, prealloc, &bits, wake, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) state = clear_state_bit(tree, state, &bits, wake, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (last_end == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) start = last_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (start <= end && state && !need_resched())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) search_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (gfpflags_allow_blocking(mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (prealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) free_extent_state(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) static void wait_on_state(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct extent_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) __releases(tree->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) __acquires(tree->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) finish_wait(&state->wq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * waits for one or more bits to clear on a range in the state tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * The range [start, end] is inclusive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * The tree lock is taken by this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) unsigned long bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) btrfs_debug_check_extent_io_range(tree, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * our range starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) node = tree_search(tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) process_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (state->start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (state->state & bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) start = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) refcount_inc(&state->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) wait_on_state(tree, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) free_extent_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) start = state->end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (!cond_resched_lock(&tree->lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) goto process_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) static void set_state_bits(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) struct extent_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) unsigned *bits, struct extent_changeset *changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) if (tree->private_data && is_data_inode(tree->private_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) btrfs_set_delalloc_extent(tree->private_data, state, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) u64 range = state->end - state->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) tree->dirty_bytes += range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) ret = add_extent_changeset(state, bits_to_set, changeset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) state->state |= bits_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) static void cache_state_if_flags(struct extent_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) struct extent_state **cached_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) if (cached_ptr && !(*cached_ptr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (!flags || (state->state & flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) *cached_ptr = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) refcount_inc(&state->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static void cache_state(struct extent_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) struct extent_state **cached_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return cache_state_if_flags(state, cached_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) EXTENT_LOCKED | EXTENT_BOUNDARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * set some bits on a range in the tree. This may require allocations or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * sleeping, so the gfp mask is used to indicate what is allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * If any of the exclusive bits are set, this will fail with -EEXIST if some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * part of the range already has the desired bits set. The start of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * existing range is returned in failed_start in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * [start, end] is inclusive This takes the tree lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) static int __must_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) unsigned bits, unsigned exclusive_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) u64 *failed_start, struct extent_state **cached_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) gfp_t mask, struct extent_changeset *changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) struct extent_state *prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct rb_node **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) struct rb_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) u64 last_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) u64 last_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) btrfs_debug_check_extent_io_range(tree, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) if (!prealloc && gfpflags_allow_blocking(mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * Don't care for allocation failure here because we might end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * up not needing the pre-allocated extent state at all, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) * is the case if we only have in the tree extent states that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * cover our input range and don't cover too any other range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) * If we end up needing a new extent state we allocate it later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) prealloc = alloc_extent_state(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (cached_state && *cached_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) state = *cached_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (state->start <= start && state->end > start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) extent_state_in_tree(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) node = &state->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * our range starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) node = tree_search_for_insert(tree, start, &p, &parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) BUG_ON(!prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) err = insert_state(tree, prealloc, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) &p, &parent, &bits, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) cache_state(prealloc, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) hit_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) last_start = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) last_end = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * Just lock what we found and keep going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (state->start == start && state->end <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (state->state & exclusive_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) *failed_start = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) set_state_bits(tree, state, &bits, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) cache_state(state, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) merge_state(tree, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (last_end == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) start = last_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) state = next_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (start < end && state && state->start == start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) !need_resched())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * | ------------- state -------------- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * We need to split the extent we found, and may flip bits on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) * second half.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) * If the extent we found extends past our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) * range, we just split and search again. It'll get split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * again the next time though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) * If the extent we found is inside our range, we set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) * desired bit on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (state->start < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) if (state->state & exclusive_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) *failed_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * If this extent already has all the bits we want set, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * skip it, not necessary to split it or do anything with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) if ((state->state & bits) == bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) start = state->end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) cache_state(state, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) BUG_ON(!prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) err = split_state(tree, state, prealloc, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) if (state->end <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) set_state_bits(tree, state, &bits, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) cache_state(state, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) merge_state(tree, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (last_end == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) start = last_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) state = next_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) if (start < end && state && state->start == start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) !need_resched())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) * | state | or | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * There's a hole, we need to insert something in it and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) * ignore the extent we found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) if (state->start > start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) u64 this_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (end < last_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) this_end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) this_end = last_start - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) BUG_ON(!prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * Avoid to free 'prealloc' if it can be merged with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * the later extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) err = insert_state(tree, prealloc, start, this_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) NULL, NULL, &bits, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) cache_state(prealloc, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) start = this_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) * | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) * We need to split the extent, and set the bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) * on the first half
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (state->start <= end && state->end > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (state->state & exclusive_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) *failed_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) BUG_ON(!prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) err = split_state(tree, state, prealloc, end + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) set_state_bits(tree, prealloc, &bits, changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) cache_state(prealloc, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) merge_state(tree, prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) search_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) if (start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (gfpflags_allow_blocking(mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) if (prealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) free_extent_state(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) unsigned bits, u64 * failed_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) struct extent_state **cached_state, gfp_t mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) return __set_extent_bit(tree, start, end, bits, 0, failed_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) cached_state, mask, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) * convert_extent_bit - convert all bits in a given range from one bit to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) * another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) * @tree: the io tree to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) * @start: the start offset in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) * @end: the end offset in bytes (inclusive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) * @bits: the bits to set in this range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) * @clear_bits: the bits to clear in this range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) * @cached_state: state that we're going to cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) * This will go through and set bits for the given range. If any states exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) * already in this range they are set with the given bit and cleared of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) * clear_bits. This is only meant to be used by things that are mergeable, ie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) * converting from say DELALLOC to DIRTY. This is not meant to be used with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * boundary bits like LOCK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * All allocations are done with GFP_NOFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) unsigned bits, unsigned clear_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) struct extent_state *prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) struct rb_node **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) struct rb_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) u64 last_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) u64 last_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) bool first_iteration = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) btrfs_debug_check_extent_io_range(tree, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) clear_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (!prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) * Best effort, don't worry if extent state allocation fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) * here for the first iteration. We might have a cached state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) * that matches exactly the target range, in which case no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) * extent state allocations are needed. We'll only know this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) * after locking the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) prealloc = alloc_extent_state(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (!prealloc && !first_iteration)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) if (cached_state && *cached_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) state = *cached_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) if (state->start <= start && state->end > start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) extent_state_in_tree(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) node = &state->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) * our range starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) node = tree_search_for_insert(tree, start, &p, &parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) if (!prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) err = insert_state(tree, prealloc, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) &p, &parent, &bits, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) cache_state(prealloc, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) hit_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) last_start = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) last_end = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) * | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) * Just lock what we found and keep going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) if (state->start == start && state->end <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) set_state_bits(tree, state, &bits, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) cache_state(state, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if (last_end == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) start = last_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) if (start < end && state && state->start == start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) !need_resched())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) * | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) * | ------------- state -------------- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) * We need to split the extent we found, and may flip bits on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) * second half.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) * If the extent we found extends past our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) * range, we just split and search again. It'll get split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) * again the next time though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) * If the extent we found is inside our range, we set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) * desired bit on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (state->start < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) if (!prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) err = split_state(tree, state, prealloc, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) if (state->end <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) set_state_bits(tree, state, &bits, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) cache_state(state, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) state = clear_state_bit(tree, state, &clear_bits, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) if (last_end == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) start = last_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) if (start < end && state && state->start == start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) !need_resched())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) goto hit_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) * | state | or | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) * There's a hole, we need to insert something in it and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) * ignore the extent we found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) if (state->start > start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) u64 this_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) if (end < last_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) this_end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) this_end = last_start - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) if (!prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) * Avoid to free 'prealloc' if it can be merged with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) * the later extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) err = insert_state(tree, prealloc, start, this_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) NULL, NULL, &bits, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) cache_state(prealloc, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) start = this_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) * | ---- desired range ---- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) * | state |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) * We need to split the extent, and set the bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) * on the first half
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) if (state->start <= end && state->end > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) prealloc = alloc_extent_state_atomic(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) if (!prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) err = split_state(tree, state, prealloc, end + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) extent_io_tree_panic(tree, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) set_state_bits(tree, prealloc, &bits, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) cache_state(prealloc, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) prealloc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) search_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) if (start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) first_iteration = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) if (prealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) free_extent_state(prealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /* wrappers around set/clear extent bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) unsigned bits, struct extent_changeset *changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) * We don't support EXTENT_LOCKED yet, as current changeset will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) * record any bits changed, so for EXTENT_LOCKED case, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) * either fail with -EEXIST or changeset will record the whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) * range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) BUG_ON(bits & EXTENT_LOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) unsigned bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) GFP_NOWAIT, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) unsigned bits, int wake, int delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) struct extent_state **cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) return __clear_extent_bit(tree, start, end, bits, wake, delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) cached, GFP_NOFS, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) unsigned bits, struct extent_changeset *changeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) * Don't support EXTENT_LOCKED case, same reason as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) * set_record_extent_bits().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) BUG_ON(bits & EXTENT_LOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) changeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) * either insert or lock state struct between start and end use mask to tell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) * us if waiting is desired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) u64 failed_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) EXTENT_LOCKED, &failed_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) cached_state, GFP_NOFS, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) if (err == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) start = failed_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) WARN_ON(start > end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) u64 failed_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) &failed_start, NULL, GFP_NOFS, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) if (err == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) if (failed_start > start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) clear_extent_bit(tree, start, failed_start - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) EXTENT_LOCKED, 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) unsigned long index = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) unsigned long end_index = end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) while (index <= end_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) page = find_get_page(inode->i_mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) BUG_ON(!page); /* Pages should be in the extent_io_tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) unsigned long index = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) unsigned long end_index = end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) while (index <= end_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) page = find_get_page(inode->i_mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) BUG_ON(!page); /* Pages should be in the extent_io_tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) __set_page_dirty_nobuffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) account_page_redirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) /* find the first state struct with 'bits' set after 'start', and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) * return it. tree->lock must be held. NULL will returned if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) * nothing was found after 'start'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) static struct extent_state *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) find_first_extent_bit_state(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) u64 start, unsigned bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) * our range starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) node = tree_search(tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) if (state->end >= start && (state->state & bits))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) * find the first offset in the io tree with 'bits' set. zero is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) * returned if we find something, and *start_ret and *end_ret are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) * set to reflect the state struct that was found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) * If nothing was found, 1 is returned. If found something, return 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) u64 *start_ret, u64 *end_ret, unsigned bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) if (cached_state && *cached_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) state = *cached_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if (state->end == start - 1 && extent_state_in_tree(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) while ((state = next_state(state)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) if (state->state & bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) goto got_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) free_extent_state(*cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) free_extent_state(*cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) state = find_first_extent_bit_state(tree, start, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) got_it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) cache_state_if_flags(state, cached_state, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) *start_ret = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) *end_ret = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) * find_contiguous_extent_bit: find a contiguous area of bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) * @tree - io tree to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) * @start - offset to start the search from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) * @start_ret - the first offset we found with the bits set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) * @end_ret - the final contiguous range of the bits that were set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) * @bits - bits to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) * to set bits appropriately, and then merge them again. During this time it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) * will drop the tree->lock, so use this helper if you want to find the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) * contiguous area for given bits. We will search to the first bit we find, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) * then walk down the tree until we find a non-contiguous area. The area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) * returned will be the full contiguous area with the bits set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) u64 *start_ret, u64 *end_ret, unsigned bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) state = find_first_extent_bit_state(tree, start, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) *start_ret = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) *end_ret = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) while ((state = next_state(state)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) if (state->start > (*end_ret + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) *end_ret = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) * find_first_clear_extent_bit - find the first range that has @bits not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) * This range could start before @start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) * @tree - the tree to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) * @start - the offset at/after which the found extent should start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) * @start_ret - records the beginning of the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) * @end_ret - records the end of the range (inclusive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) * @bits - the set of bits which must be unset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) * Since unallocated range is also considered one which doesn't have the bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) * set it's possible that @end_ret contains -1, this happens in case the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) * spans (last_range_end, end of device]. In this case it's up to the caller to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) * trim @end_ret to the appropriate size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) u64 *start_ret, u64 *end_ret, unsigned bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) struct rb_node *node, *prev = NULL, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) /* Find first extent with bits cleared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) node = __etree_search(tree, start, &next, &prev, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) if (!node && !next && !prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) * Tree is completely empty, send full range and let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) * caller deal with it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) *start_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) *end_ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) } else if (!node && !next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) * We are past the last allocated chunk, set start at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) * the end of the last extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) state = rb_entry(prev, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) *start_ret = state->end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) *end_ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) } else if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) node = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) * At this point 'node' either contains 'start' or start is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) * before 'node'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) if (in_range(start, state->start, state->end - state->start + 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (state->state & bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) * |--range with bits sets--|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) * start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) start = state->end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) * 'start' falls within a range that doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) * have the bits set, so take its start as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) * the beginning of the desired range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) * |--range with bits cleared----|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) * start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) *start_ret = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) * |---prev range---|---hole/unset---|---node range---|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) * start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) * |---hole/unset--||--first node--|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) * 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) * start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) if (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) state = rb_entry(prev, struct extent_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) *start_ret = state->end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) *start_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) * Find the longest stretch from start until an entry which has the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) * bits set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) if (state->end >= start && !(state->state & bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) *end_ret = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) *end_ret = state->start - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) * find a contiguous range of bytes in the file marked as delalloc, not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) * more than 'max_bytes'. start and end are used to return the range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) * true is returned if we find something, false if nothing was in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) u64 *end, u64 max_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) u64 cur_start = *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) u64 total_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) * our range starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) node = tree_search(tree, cur_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) *end = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (found && (state->start != cur_start ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) (state->state & EXTENT_BOUNDARY))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if (!(state->state & EXTENT_DELALLOC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) *end = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) *start = state->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) *cached_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) refcount_inc(&state->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) *end = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) cur_start = state->end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) total_bytes += state->end - state->start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) if (total_bytes >= max_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) static int __process_pages_contig(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) pgoff_t start_index, pgoff_t end_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) unsigned long page_ops, pgoff_t *index_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) static noinline void __unlock_for_delalloc(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) unsigned long index = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) unsigned long end_index = end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) ASSERT(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) if (index == locked_page->index && end_index == index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) PAGE_UNLOCK, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) static noinline int lock_delalloc_pages(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) u64 delalloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) u64 delalloc_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) unsigned long index = delalloc_start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) unsigned long index_ret = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) unsigned long end_index = delalloc_end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) ASSERT(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) if (index == locked_page->index && index == end_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) ret = __process_pages_contig(inode->i_mapping, locked_page, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) end_index, PAGE_LOCK, &index_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) __unlock_for_delalloc(inode, locked_page, delalloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) (u64)index_ret << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) * Find and lock a contiguous range of bytes in the file marked as delalloc, no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) * more than @max_bytes. @Start and @end are used to return the range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) * Return: true if we find something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) * false if nothing was in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) EXPORT_FOR_TESTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) struct page *locked_page, u64 *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) u64 *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) u64 delalloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) u64 delalloc_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) int loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) /* step one, find a bunch of delalloc bytes starting at start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) delalloc_start = *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) delalloc_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) max_bytes, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) if (!found || delalloc_end <= *start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) *start = delalloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) *end = delalloc_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) free_extent_state(cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) * start comes from the offset of locked_page. We have to lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) * pages in order, so we can't process delalloc bytes before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) * locked_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) if (delalloc_start < *start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) delalloc_start = *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) * make sure to limit the number of pages we try to lock down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) if (delalloc_end + 1 - delalloc_start > max_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) delalloc_end = delalloc_start + max_bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) /* step two, lock all the pages after the page that has start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) ret = lock_delalloc_pages(inode, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) delalloc_start, delalloc_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) ASSERT(!ret || ret == -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) /* some of the pages are gone, lets avoid looping by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) * shortening the size of the delalloc range we're searching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) free_extent_state(cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) if (!loops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) max_bytes = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) loops = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) goto out_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) /* step three, lock the state bits for the whole range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) /* then test to make sure it is all still delalloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) ret = test_range_bit(tree, delalloc_start, delalloc_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) EXTENT_DELALLOC, 1, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) unlock_extent_cached(tree, delalloc_start, delalloc_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) __unlock_for_delalloc(inode, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) delalloc_start, delalloc_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) free_extent_state(cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) *start = delalloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) *end = delalloc_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) out_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) static int __process_pages_contig(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) pgoff_t start_index, pgoff_t end_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) unsigned long page_ops, pgoff_t *index_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) unsigned long nr_pages = end_index - start_index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) unsigned long pages_locked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) pgoff_t index = start_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) struct page *pages[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) unsigned ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) if (page_ops & PAGE_LOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) ASSERT(page_ops == PAGE_LOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) ASSERT(index_ret && *index_ret == start_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) mapping_set_error(mapping, -EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) while (nr_pages > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) ret = find_get_pages_contig(mapping, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) min_t(unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) nr_pages, ARRAY_SIZE(pages)), pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) * Only if we're going to lock these pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) * can we find nothing at @index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) ASSERT(page_ops & PAGE_LOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) for (i = 0; i < ret; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) if (page_ops & PAGE_SET_PRIVATE2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) SetPagePrivate2(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) if (locked_page && pages[i] == locked_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) pages_locked++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) if (page_ops & PAGE_CLEAR_DIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) clear_page_dirty_for_io(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) if (page_ops & PAGE_SET_WRITEBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) set_page_writeback(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) if (page_ops & PAGE_SET_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) SetPageError(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) if (page_ops & PAGE_END_WRITEBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) end_page_writeback(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) if (page_ops & PAGE_UNLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) unlock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) if (page_ops & PAGE_LOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) lock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) if (!PageDirty(pages[i]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) pages[i]->mapping != mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) unlock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) for (; i < ret; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) pages_locked++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) nr_pages -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) index += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) if (err && index_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) *index_ret = start_index + pages_locked - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) struct page *locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) unsigned clear_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) unsigned long page_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) __process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) start >> PAGE_SHIFT, end >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) page_ops, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) * count the number of bytes in the tree that have a given bit(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) * set. This can be fairly slow, except for EXTENT_DIRTY which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) * cached. The total number found is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) u64 count_range_bits(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) u64 *start, u64 search_end, u64 max_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) unsigned bits, int contig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) u64 cur_start = *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) u64 total_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) u64 last = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) if (WARN_ON(search_end <= cur_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) if (cur_start == 0 && bits == EXTENT_DIRTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) total_bytes = tree->dirty_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) * our range starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) node = tree_search(tree, cur_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) if (state->start > search_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) if (contig && found && state->start > last + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) if (state->end >= cur_start && (state->state & bits) == bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) total_bytes += min(search_end, state->end) + 1 -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) max(cur_start, state->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) if (total_bytes >= max_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) *start = max(cur_start, state->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) last = state->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) } else if (contig && found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) return total_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) * set the private field for a given byte offset in the tree. If there isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) * an extent_state there already, this does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) int set_state_failrec(struct extent_io_tree *tree, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) struct io_failure_record *failrec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) * our range starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) node = tree_search(tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) if (state->start != start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) state->failrec = failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) struct io_failure_record *failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) * this search will find all the extents that end after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) * our range starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) node = tree_search(tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) failrec = ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) if (state->start != start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) failrec = ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) failrec = state->failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) return failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) * searches a range in the state tree for a given mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) * If 'filled' == 1, this returns 1 only if every extent in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) * has the bits set. Otherwise, 1 is returned if any bit in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) * range is found set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) unsigned bits, int filled, struct extent_state *cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) struct extent_state *state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) int bitset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) spin_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) if (cached && extent_state_in_tree(cached) && cached->start <= start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) cached->end > start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) node = &cached->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) node = tree_search(tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) while (node && start <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) state = rb_entry(node, struct extent_state, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) if (filled && state->start > start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) bitset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) if (state->start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) if (state->state & bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) bitset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) if (!filled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) } else if (filled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) bitset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) if (state->end == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) start = state->end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) if (start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) if (filled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) bitset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) spin_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) return bitset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) * helper function to set a given page up to date if all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) * extents in the tree for that page are up to date
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) u64 end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) int free_io_failure(struct extent_io_tree *failure_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) struct extent_io_tree *io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) struct io_failure_record *rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) set_state_failrec(failure_tree, rec->start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) ret = clear_extent_bits(failure_tree, rec->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) rec->start + rec->len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) EXTENT_LOCKED | EXTENT_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) ret = clear_extent_bits(io_tree, rec->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) rec->start + rec->len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) EXTENT_DAMAGED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) if (ret && !err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) kfree(rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) * this bypasses the standard btrfs submit functions deliberately, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) * the standard behavior is to write all copies in a raid setup. here we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) * want to write the one bad copy. so we do the mapping for ourselves and issue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) * submit_bio directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) * to avoid any synchronization issues, wait for the data after writing, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) * actually prevents the read that triggered the error from finishing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) * currently, there can be no more than two copies of every data bit. thus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) * exactly one rewrite is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) u64 length, u64 logical, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) unsigned int pg_offset, int mirror_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) struct btrfs_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) u64 map_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) u64 sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) struct btrfs_bio *bbio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) BUG_ON(!mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) bio = btrfs_io_bio_alloc(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) bio->bi_iter.bi_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) map_length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) * Avoid races with device replace and make sure our bbio has devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) * associated to its stripes that don't go away while we are doing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) * read repair operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) btrfs_bio_counter_inc_blocked(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) if (btrfs_is_parity_mirror(fs_info, logical, length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) * Note that we don't use BTRFS_MAP_WRITE because it's supposed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) * to update all raid stripes, but here we just want to correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) * stripe's dev and sector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) &map_length, &bbio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) btrfs_bio_counter_dec(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) ASSERT(bbio->mirror_num == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) &map_length, &bbio, mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) btrfs_bio_counter_dec(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) BUG_ON(mirror_num != bbio->mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) bio->bi_iter.bi_sector = sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) dev = bbio->stripes[bbio->mirror_num - 1].dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) btrfs_put_bbio(bbio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) if (!dev || !dev->bdev ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) btrfs_bio_counter_dec(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) bio_set_dev(bio, dev->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) bio_add_page(bio, page, length, pg_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) if (btrfsic_submit_bio_wait(bio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) /* try to remap that extent elsewhere? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) btrfs_bio_counter_dec(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) btrfs_info_rl_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) "read error corrected: ino %llu off %llu (dev %s sector %llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) ino, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) rcu_str_deref(dev->name), sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) btrfs_bio_counter_dec(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) struct btrfs_fs_info *fs_info = eb->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) u64 start = eb->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) int i, num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) if (sb_rdonly(fs_info->sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) struct page *p = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) start - page_offset(p), mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) start += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) * each time an IO finishes, we do a fast check in the IO failure tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) * to see if we need to process or clean up an io_failure_record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) int clean_io_failure(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) struct extent_io_tree *failure_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) struct extent_io_tree *io_tree, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) struct page *page, u64 ino, unsigned int pg_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) u64 private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) struct io_failure_record *failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) struct extent_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) int num_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) private = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) EXTENT_DIRTY, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) failrec = get_state_failrec(failure_tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) if (IS_ERR(failrec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) BUG_ON(!failrec->this_mirror);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) if (failrec->in_validation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) /* there was no real error, just free the record */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) "clean_io_failure: freeing dummy error at %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) failrec->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) if (sb_rdonly(fs_info->sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) spin_lock(&io_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) state = find_first_extent_bit_state(io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) failrec->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) EXTENT_LOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) spin_unlock(&io_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) if (state && state->start <= failrec->start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) state->end >= failrec->start + failrec->len - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) num_copies = btrfs_num_copies(fs_info, failrec->logical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) failrec->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) if (num_copies > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) repair_io_failure(fs_info, ino, start, failrec->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) failrec->logical, page, pg_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) failrec->failed_mirror);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) free_io_failure(failure_tree, io_tree, failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) * Can be called when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) * - hold extent lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) * - under ordered extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) * - the inode is freeing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) struct extent_io_tree *failure_tree = &inode->io_failure_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) struct io_failure_record *failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) struct extent_state *state, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) if (RB_EMPTY_ROOT(&failure_tree->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) spin_lock(&failure_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) while (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) if (state->start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) ASSERT(state->end <= end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) next = next_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) failrec = state->failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) free_extent_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) kfree(failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) state = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) spin_unlock(&failure_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) struct io_failure_record *failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) u64 logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) failrec = get_state_failrec(failure_tree, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) if (!IS_ERR(failrec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) failrec->logical, failrec->start, failrec->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) failrec->in_validation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) * when data can be on disk more than twice, add to failrec here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) * (e.g. with a list for failed_mirror) to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) * clean_io_failure() clean all those errors at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) return failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) if (!failrec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) failrec->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) failrec->len = end - start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) failrec->this_mirror = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) failrec->bio_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) failrec->in_validation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) read_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) em = lookup_extent_mapping(em_tree, start, failrec->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) read_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) kfree(failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) if (em->start > start || em->start + em->len <= start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) read_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) kfree(failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) logical = start - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) logical = em->block_start + logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) logical = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) failrec->bio_flags = EXTENT_BIO_COMPRESSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) extent_set_compress_type(&failrec->bio_flags, em->compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) logical, start, failrec->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) failrec->logical = logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) /* Set the bits in the private failure tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) ret = set_extent_bits(failure_tree, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) EXTENT_LOCKED | EXTENT_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) ret = set_state_failrec(failure_tree, start, failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) /* Set the bits in the inode's tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) } else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) kfree(failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) return failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) static bool btrfs_check_repairable(struct inode *inode, bool needs_validation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) struct io_failure_record *failrec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) int failed_mirror)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) int num_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) if (num_copies == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) * we only have a single copy of the data, so don't bother with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) * all the retry and error correction code that follows. no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) * matter what the error is, it is very likely to persist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) num_copies, failrec->this_mirror, failed_mirror);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) * there are two premises:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) * a) deliver good data to the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) * b) correct the bad sectors on disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) if (needs_validation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) * to fulfill b), we need to know the exact failing sectors, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) * we don't want to rewrite any more than the failed ones. thus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) * we need separate read requests for the failed bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) * if the following BUG_ON triggers, our validation request got
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) * merged. we need separate requests for our algorithm to work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) BUG_ON(failrec->in_validation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) failrec->in_validation = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) failrec->this_mirror = failed_mirror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) * we're ready to fulfill a) and b) alongside. get a good copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) * of the failed sector and if we succeed, we have setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) * everything for repair_io_failure to do the rest for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) if (failrec->in_validation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) BUG_ON(failrec->this_mirror != failed_mirror);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) failrec->in_validation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) failrec->this_mirror = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) failrec->failed_mirror = failed_mirror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) failrec->this_mirror++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) if (failrec->this_mirror == failed_mirror)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) failrec->this_mirror++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) if (failrec->this_mirror > num_copies) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) num_copies, failrec->this_mirror, failed_mirror);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) static bool btrfs_io_needs_validation(struct inode *inode, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) u64 len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) const u32 blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) * If bi_status is BLK_STS_OK, then this was a checksum error, not an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) * I/O error. In this case, we already know exactly which sector was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) * bad, so we don't need to validate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) if (bio->bi_status == BLK_STS_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) * We need to validate each sector individually if the failed I/O was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) * for multiple sectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) * There are a few possible bios that can end up here:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) * 1. A buffered read bio, which is not cloned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) * 2. A direct I/O read bio, which is cloned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) * 3. A (buffered or direct) repair bio, which is not cloned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) * For cloned bios (case 2), we can get the size from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) * btrfs_io_bio->iter; for non-cloned bios (cases 1 and 3), we can get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) * it from the bvecs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) if (bio_flagged(bio, BIO_CLONED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) if (btrfs_io_bio(bio)->iter.bi_size > blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) struct bio_vec *bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) bio_for_each_bvec_all(bvec, bio, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) len += bvec->bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) if (len > blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) blk_status_t btrfs_submit_read_repair(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) struct bio *failed_bio, u64 phy_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) struct page *page, unsigned int pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) u64 start, u64 end, int failed_mirror,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) submit_bio_hook_t *submit_bio_hook)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) struct io_failure_record *failrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) struct btrfs_io_bio *failed_io_bio = btrfs_io_bio(failed_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) const int icsum = phy_offset >> inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) bool need_validation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) struct bio *repair_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) struct btrfs_io_bio *repair_io_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) blk_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) "repair read error: read error at %llu", start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) failrec = btrfs_get_io_failure_record(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) if (IS_ERR(failrec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) return errno_to_blk_status(PTR_ERR(failrec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) need_validation = btrfs_io_needs_validation(inode, failed_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) if (!btrfs_check_repairable(inode, need_validation, failrec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) failed_mirror)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) free_io_failure(failure_tree, tree, failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) repair_bio = btrfs_io_bio_alloc(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) repair_io_bio = btrfs_io_bio(repair_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) repair_bio->bi_opf = REQ_OP_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) if (need_validation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) repair_bio->bi_opf |= REQ_FAILFAST_DEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) repair_bio->bi_end_io = failed_bio->bi_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) repair_bio->bi_iter.bi_sector = failrec->logical >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) repair_bio->bi_private = failed_bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) if (failed_io_bio->csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) repair_io_bio->csum = repair_io_bio->csum_inline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) memcpy(repair_io_bio->csum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) failed_io_bio->csum + csum_size * icsum, csum_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) bio_add_page(repair_bio, page, failrec->len, pgoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) repair_io_bio->logical = failrec->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) repair_io_bio->iter = repair_bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) btrfs_debug(btrfs_sb(inode->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) "repair read error: submitting new read to mirror %d, in_validation=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) failrec->this_mirror, failrec->in_validation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) status = submit_bio_hook(inode, repair_bio, failrec->this_mirror,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) failrec->bio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) free_io_failure(failure_tree, tree, failrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) bio_put(repair_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) /* lots and lots of room for performance fixes in the end_bio funcs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) int uptodate = (err == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) if (!uptodate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) ret = err < 0 ? err : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) mapping_set_error(page->mapping, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) * after a writepage IO is done, we need to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) * clear the uptodate bits on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) * clear the writeback bits in the extent tree for this IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) * end_page_writeback if the page has no more pending IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) * Scheduling is not allowed, so the extent state tree is expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) * to have one and only one object corresponding to this IO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) static void end_bio_extent_writepage(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) int error = blk_status_to_errno(bio->bi_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) struct bio_vec *bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) struct bvec_iter_all iter_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) ASSERT(!bio_flagged(bio, BIO_CLONED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) bio_for_each_segment_all(bvec, bio, iter_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) struct page *page = bvec->bv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) /* We always issue full-page reads, but if some block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) * in a page fails to read, blk_update_request() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) * advance bv_offset and adjust bv_len to compensate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) * Print a warning for nonzero offsets, and an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) * if they don't add up to a full page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) "partial page write in btrfs with offset %u and length %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) bvec->bv_offset, bvec->bv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) "incomplete page write in btrfs with offset %u and length %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) bvec->bv_offset, bvec->bv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) end = start + bvec->bv_offset + bvec->bv_len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) end_extent_writepage(page, error, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) int uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) struct extent_state *cached = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) u64 end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) if (uptodate && tree->track_uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) unlock_extent_cached_atomic(tree, start, end, &cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) * after a readpage IO is done, we need to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) * clear the uptodate bits on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) * set the uptodate bits if things worked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) * set the page up to date if all extents in the tree are uptodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) * clear the lock bit in the extent tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) * unlock the page if there are no other extents locked for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) * Scheduling is not allowed, so the extent state tree is expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) * to have one and only one object corresponding to this IO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) static void end_bio_extent_readpage(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) struct bio_vec *bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) int uptodate = !bio->bi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) struct extent_io_tree *tree, *failure_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) u64 offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) u64 extent_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) u64 extent_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) int mirror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) struct bvec_iter_all iter_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) ASSERT(!bio_flagged(bio, BIO_CLONED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) bio_for_each_segment_all(bvec, bio, iter_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) struct page *page = bvec->bv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) btrfs_debug(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) (u64)bio->bi_iter.bi_sector, bio->bi_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) io_bio->mirror_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) failure_tree = &BTRFS_I(inode)->io_failure_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) /* We always issue full-page reads, but if some block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) * in a page fails to read, blk_update_request() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) * advance bv_offset and adjust bv_len to compensate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) * Print a warning for nonzero offsets, and an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) * if they don't add up to a full page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) "partial page read in btrfs with offset %u and length %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) bvec->bv_offset, bvec->bv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) "incomplete page read in btrfs with offset %u and length %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) bvec->bv_offset, bvec->bv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) end = start + bvec->bv_offset + bvec->bv_len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) len = bvec->bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) mirror = io_bio->mirror_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) if (likely(uptodate)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) if (is_data_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) ret = btrfs_verify_data_csum(io_bio, offset, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) start, end, mirror);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) ret = btrfs_validate_metadata_buffer(io_bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) offset, page, start, end, mirror);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) uptodate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) clean_io_failure(BTRFS_I(inode)->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) failure_tree, tree, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) btrfs_ino(BTRFS_I(inode)), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) if (likely(uptodate))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) goto readpage_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) if (is_data_inode(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) * The generic bio_readpage_error handles errors the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) * following way: If possible, new read requests are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) * created and submitted and will end up in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) * end_bio_extent_readpage as well (if we're lucky,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) * not in the !uptodate case). In that case it returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) * 0 and we just go on with the next page in our bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) * If it can't handle the error it will return -EIO and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) * we remain responsible for that page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) if (!btrfs_submit_read_repair(inode, bio, offset, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) start - page_offset(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) start, end, mirror,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) btrfs_submit_data_bio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) uptodate = !bio->bi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) eb = (struct extent_buffer *)page->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) eb->read_mirror = mirror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) atomic_dec(&eb->io_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) btree_readahead_hook(eb, -EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) readpage_ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) if (likely(uptodate)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) loff_t i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) pgoff_t end_index = i_size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) unsigned off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) /* Zero out the end if this page straddles i_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) off = offset_in_page(i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) if (page->index == end_index && off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) zero_user_segment(page, off, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) if (unlikely(!uptodate)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) if (extent_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) endio_readpage_release_extent(tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) extent_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) extent_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) extent_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) extent_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) endio_readpage_release_extent(tree, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) end - start + 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) } else if (!extent_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) extent_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) extent_len = end + 1 - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) } else if (extent_start + extent_len == start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) extent_len += end + 1 - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) endio_readpage_release_extent(tree, extent_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) extent_len, uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) extent_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) extent_len = end + 1 - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) if (extent_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) endio_readpage_release_extent(tree, extent_start, extent_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) btrfs_io_bio_free_csum(io_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) * Initialize the members up to but not including 'bio'. Use after allocating a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) * 'bio' because use of __GFP_ZERO is not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) * The following helpers allocate a bio. As it's backed by a bioset, it'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) * never fail. We're returning a bio right now but you can call btrfs_io_bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) * for the appropriate container_of magic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) struct bio *btrfs_bio_alloc(u64 first_byte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) bio->bi_iter.bi_sector = first_byte >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) btrfs_io_bio_init(btrfs_io_bio(bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) return bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) struct bio *btrfs_bio_clone(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) struct btrfs_io_bio *btrfs_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) struct bio *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) /* Bio allocation backed by a bioset does not fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) btrfs_bio = btrfs_io_bio(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) btrfs_io_bio_init(btrfs_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) btrfs_bio->iter = bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) /* Bio allocation backed by a bioset does not fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) btrfs_io_bio_init(btrfs_io_bio(bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) return bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) struct btrfs_io_bio *btrfs_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) /* this will never fail when it's backed by a bioset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) ASSERT(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) btrfs_bio = btrfs_io_bio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) btrfs_io_bio_init(btrfs_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) bio_trim(bio, offset >> 9, size >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) btrfs_bio->iter = bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) return bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) * @opf: bio REQ_OP_* and REQ_* flags as one value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) * @wbc: optional writeback control for io accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) * @page: page to add to the bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) * @pg_offset: offset of the new bio or to check whether we are adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) * a contiguous page to the previous one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) * @size: portion of page that we want to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) * @offset: starting offset in the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) * @bio_ret: must be valid pointer, newly allocated bio will be stored there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) * @end_io_func: end_io callback for new bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) * @mirror_num: desired mirror to read/write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) * @prev_bio_flags: flags of previous bio to see if we can merge the current one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) * @bio_flags: flags of the current bio to see if we can merge them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) static int submit_extent_page(unsigned int opf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) struct page *page, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) size_t size, unsigned long pg_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) struct bio **bio_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) bio_end_io_t end_io_func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) int mirror_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) unsigned long prev_bio_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) unsigned long bio_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) bool force_bio_submit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) size_t page_size = min_t(size_t, size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) sector_t sector = offset >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) struct extent_io_tree *tree = &BTRFS_I(page->mapping->host)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) ASSERT(bio_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) if (*bio_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) bool contig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) bool can_merge = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) bio = *bio_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) contig = bio->bi_iter.bi_sector == sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) contig = bio_end_sector(bio) == sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) can_merge = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) if (prev_bio_flags != bio_flags || !contig || !can_merge ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) force_bio_submit ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) bio_add_page(bio, page, page_size, pg_offset) < page_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) *bio_ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) if (wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) wbc_account_cgroup_owner(wbc, page, page_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) bio = btrfs_bio_alloc(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) bio_add_page(bio, page, page_size, pg_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) bio->bi_end_io = end_io_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) bio->bi_private = tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) bio->bi_write_hint = page->mapping->host->i_write_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) bio->bi_opf = opf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) if (wbc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) bdev = BTRFS_I(page->mapping->host)->root->fs_info->fs_devices->latest_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) bio_set_dev(bio, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) wbc_init_bio(wbc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) wbc_account_cgroup_owner(wbc, page, page_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) *bio_ret = bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) static void attach_extent_buffer_page(struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) if (!PagePrivate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) attach_page_private(page, eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) WARN_ON(page->private != (unsigned long)eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) void set_page_extent_mapped(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) if (!PagePrivate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) static struct extent_map *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) u64 start, u64 len, struct extent_map **em_cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) if (em_cached && *em_cached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) em = *em_cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) if (extent_map_in_tree(em) && start >= em->start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) start < extent_map_end(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) refcount_inc(&em->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) *em_cached = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) if (em_cached && !IS_ERR_OR_NULL(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) BUG_ON(*em_cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) refcount_inc(&em->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) *em_cached = em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) * basic readpage implementation. Locked extent state structs are inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) * into the tree that are removed when the IO is done (by the end_io
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) * handlers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) * XXX JDM: This needs looking at to ensure proper page locking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) * return 0 on success, otherwise return error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) struct bio **bio, unsigned long *bio_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) unsigned int read_flags, u64 *prev_em_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) const u64 end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) u64 cur = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) u64 extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) u64 last_byte = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) u64 block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) u64 cur_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) int nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) size_t pg_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) size_t iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) size_t disk_io_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) size_t blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) unsigned long this_bio_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) set_page_extent_mapped(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) if (cleancache_get_page(page) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) BUG_ON(blocksize != PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) unlock_extent(tree, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) if (page->index == last_byte >> PAGE_SHIFT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) char *userpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) size_t zero_offset = offset_in_page(last_byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) if (zero_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) iosize = PAGE_SIZE - zero_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) userpage = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) memset(userpage + zero_offset, 0, iosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) kunmap_atomic(userpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) while (cur <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) bool force_bio_submit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) if (cur >= last_byte) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) char *userpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) struct extent_state *cached = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) iosize = PAGE_SIZE - pg_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) userpage = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) memset(userpage + pg_offset, 0, iosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) kunmap_atomic(userpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) set_extent_uptodate(tree, cur, cur + iosize - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) &cached, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) unlock_extent_cached(tree, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) cur + iosize - 1, &cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) em = __get_extent_map(inode, page, pg_offset, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) end - cur + 1, em_cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) if (IS_ERR_OR_NULL(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) unlock_extent(tree, cur, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) extent_offset = cur - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) BUG_ON(extent_map_end(em) <= cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) BUG_ON(end < cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) this_bio_flag |= EXTENT_BIO_COMPRESSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) extent_set_compress_type(&this_bio_flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) em->compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) iosize = min(extent_map_end(em) - cur, end - cur + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) cur_end = min(extent_map_end(em) - 1, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) iosize = ALIGN(iosize, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) disk_io_size = em->block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) offset = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) offset = em->block_start + extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) disk_io_size = iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) block_start = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) block_start = EXTENT_MAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) * If we have a file range that points to a compressed extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) * and it's followed by a consecutive file range that points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) * to the same compressed extent (possibly with a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) * offset and/or length, so it either points to the whole extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) * or only part of it), we must make sure we do not submit a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) * single bio to populate the pages for the 2 ranges because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) * this makes the compressed extent read zero out the pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) * belonging to the 2nd range. Imagine the following scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) * File layout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) * [0 - 8K] [8K - 24K]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) * | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) * | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) * points to extent X, points to extent X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) * offset 4K, length of 8K offset 0, length 16K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) * [extent X, compressed length = 4K uncompressed length = 16K]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) * If the bio to read the compressed extent covers both ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) * it will decompress extent X into the pages belonging to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) * first range and then it will stop, zeroing out the remaining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) * pages that belong to the other range that points to extent X.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) * So here we make sure we submit 2 bios, one for the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) * range and another one for the third range. Both will target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) * the same physical extent from disk, but we can't currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) * make the compressed bio endio callback populate the pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) * for both ranges because each compressed bio is tightly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) * coupled with a single extent map, and each range can have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) * an extent map with a different offset value relative to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) * uncompressed data of our extent and different lengths. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) * is a corner case so we prioritize correctness over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) * non-optimal behavior (submitting 2 bios for the same extent).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) prev_em_start && *prev_em_start != (u64)-1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) *prev_em_start != em->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) force_bio_submit = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) if (prev_em_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) *prev_em_start = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) /* we've found a hole, just zero and go on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) if (block_start == EXTENT_MAP_HOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) char *userpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) struct extent_state *cached = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) userpage = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) memset(userpage + pg_offset, 0, iosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) kunmap_atomic(userpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) set_extent_uptodate(tree, cur, cur + iosize - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) &cached, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) unlock_extent_cached(tree, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) cur + iosize - 1, &cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) cur = cur + iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) pg_offset += iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) /* the get_extent function already copied into the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) if (test_range_bit(tree, cur, cur_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) EXTENT_UPTODATE, 1, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) check_page_uptodate(tree, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) unlock_extent(tree, cur, cur + iosize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) cur = cur + iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) pg_offset += iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) /* we have an inline extent but it didn't get marked up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) * to date. Error out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) if (block_start == EXTENT_MAP_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) unlock_extent(tree, cur, cur + iosize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) cur = cur + iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) pg_offset += iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) ret = submit_extent_page(REQ_OP_READ | read_flags, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) page, offset, disk_io_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) pg_offset, bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) end_bio_extent_readpage, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) *bio_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) this_bio_flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) force_bio_submit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) *bio_flags = this_bio_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) unlock_extent(tree, cur, cur + iosize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) cur = cur + iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) pg_offset += iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) if (!nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) if (!PageError(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) static inline void contiguous_readpages(struct page *pages[], int nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) struct extent_map **em_cached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) struct bio **bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) unsigned long *bio_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) u64 *prev_em_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) for (index = 0; index < nr_pages; index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) btrfs_do_readpage(pages[index], em_cached, bio, bio_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) REQ_RAHEAD, prev_em_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) put_page(pages[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) static void update_nr_written(struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) unsigned long nr_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) wbc->nr_to_write -= nr_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) * helper for __extent_writepage, doing all of the delayed allocation setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) * This returns 1 if btrfs_run_delalloc_range function did all the work required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) * to write the page (copy into inline extent). In this case the IO has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) * been started and the page is already unlocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) * This returns 0 if all went well (page still locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) * This returns < 0 if there were errors (page still locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) struct page *page, struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) u64 delalloc_start, unsigned long *nr_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) u64 page_end = delalloc_start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) u64 delalloc_to_write = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) u64 delalloc_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) int page_started = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) while (delalloc_end < page_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) found = find_lock_delalloc_range(&inode->vfs_inode, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) &delalloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) &delalloc_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) delalloc_start = delalloc_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) delalloc_end, &page_started, nr_written, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) * btrfs_run_delalloc_range should return < 0 for error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) * but just in case, we use > 0 here meaning the IO is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) * started, so we don't want to return > 0 unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) * things are going well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) return ret < 0 ? ret : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) * delalloc_end is already one less than the total length, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) * we don't subtract one from PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) delalloc_to_write += (delalloc_end - delalloc_start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) PAGE_SIZE) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) delalloc_start = delalloc_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) if (wbc->nr_to_write < delalloc_to_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) int thresh = 8192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) if (delalloc_to_write < thresh * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) thresh = delalloc_to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) wbc->nr_to_write = min_t(u64, delalloc_to_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) thresh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) /* did the fill delalloc function already unlock and start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) * the IO?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) if (page_started) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) * we've unlocked the page, so we can't update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) * the mapping's writeback index, just update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) * nr_to_write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) wbc->nr_to_write -= *nr_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) * helper for __extent_writepage. This calls the writepage start hooks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) * and does the loop to map the page into extents and bios.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) * We return 1 if the IO is started and the page is unlocked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) * 0 if all went well (page still locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) * < 0 if there were errors (page still locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) struct extent_page_data *epd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) loff_t i_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) unsigned long nr_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) int *nr_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) struct extent_io_tree *tree = &inode->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) u64 page_end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) u64 cur = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) u64 extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) u64 block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) u64 iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) size_t pg_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) size_t blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) int nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) const unsigned int write_flags = wbc_to_write_flags(wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) bool compressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) ret = btrfs_writepage_cow_fixup(page, start, page_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) /* Fixup worker will requeue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) redirty_page_for_writepage(wbc, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) update_nr_written(wbc, nr_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) * we don't want to touch the inode after unlocking the page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) * so we update the mapping writeback index now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) update_nr_written(wbc, nr_written + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) end = page_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) blocksize = inode->vfs_inode.i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) while (cur <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) u64 em_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) if (cur >= i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) btrfs_writepage_endio_finish_ordered(page, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) page_end, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) if (IS_ERR_OR_NULL(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) ret = PTR_ERR_OR_ZERO(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) extent_offset = cur - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) em_end = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) BUG_ON(em_end <= cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) BUG_ON(end < cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) iosize = min(em_end - cur, end - cur + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) iosize = ALIGN(iosize, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) offset = em->block_start + extent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) block_start = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) * compressed and inline extents are written through other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) * paths in the FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) if (compressed || block_start == EXTENT_MAP_HOLE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) block_start == EXTENT_MAP_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) if (compressed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) btrfs_writepage_endio_finish_ordered(page, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) cur + iosize - 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) cur += iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) pg_offset += iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) if (!PageWriteback(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) btrfs_err(inode->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) "page %lu not writeback, cur %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) page->index, cur, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) page, offset, iosize, pg_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) &epd->bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) end_bio_extent_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 0, 0, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) if (PageWriteback(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) cur = cur + iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) pg_offset += iosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) *nr_ret = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) * the writepage semantics are similar to regular writepage. extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) * records are inserted to lock ranges in the tree, and as dirty areas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) * are found, they are marked writeback. Then the lock bits are removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) * and the end_io handler clears the writeback ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) * Return 0 if everything goes well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) * Return <0 for error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) static int __extent_writepage(struct page *page, struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) struct extent_page_data *epd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) u64 page_end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) int nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) size_t pg_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) loff_t i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) unsigned long end_index = i_size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) unsigned long nr_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) trace___extent_writepage(page, inode, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) WARN_ON(!PageLocked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) pg_offset = offset_in_page(i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) if (page->index > end_index ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) (page->index == end_index && !pg_offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) if (page->index == end_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) char *userpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) userpage = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) memset(userpage + pg_offset, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) PAGE_SIZE - pg_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) kunmap_atomic(userpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) set_page_extent_mapped(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) if (!epd->extent_locked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) ret = writepage_delalloc(BTRFS_I(inode), page, wbc, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) &nr_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) nr_written, &nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) if (nr == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) /* make sure the mapping tag for page dirty gets cleared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) set_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) if (PageError(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) ret = ret < 0 ? ret : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) end_extent_writepage(page, ret, start, page_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) ASSERT(ret <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) static void end_extent_buffer_writeback(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) smp_mb__after_atomic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) * Lock eb pages and flush the bio if we can't the locks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) * Return 0 if nothing went wrong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) * Return >0 is same as 0, except bio is not submitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) * Return <0 if something went wrong, no page is locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) struct extent_page_data *epd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) struct btrfs_fs_info *fs_info = eb->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) int i, num_pages, failed_page_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) int flush = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) if (!btrfs_try_tree_write_lock(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) ret = flush_write_bio(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) btrfs_tree_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) btrfs_tree_unlock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) if (!epd->sync_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) if (!flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) ret = flush_write_bio(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) wait_on_extent_buffer_writeback(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) btrfs_tree_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) btrfs_tree_unlock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) * We need to do this to prevent races in people who check if the eb is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) * under IO since we can end up having no IO bits set for a short period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) * of time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) spin_lock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) -eb->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) fs_info->dirty_metadata_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) btrfs_tree_unlock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) struct page *p = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) if (!trylock_page(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) if (!flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) err = flush_write_bio(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) failed_page_nr = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) lock_page(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) /* Unlock already locked pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) for (i = 0; i < failed_page_nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) unlock_page(eb->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) * Clear EXTENT_BUFFER_WRITEBACK and wake up anyone waiting on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) * Also set back EXTENT_BUFFER_DIRTY so future attempts to this eb can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) * be made and undo everything done before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) btrfs_tree_lock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) spin_lock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) end_extent_buffer_writeback(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, eb->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) fs_info->dirty_metadata_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) btrfs_clear_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) btrfs_tree_unlock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) static void set_btree_ioerr(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) struct extent_buffer *eb = (struct extent_buffer *)page->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) struct btrfs_fs_info *fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) * A read may stumble upon this buffer later, make sure that it gets an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) * error and knows there was an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) * If we error out, we should add back the dirty_metadata_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) * to make it consistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) fs_info = eb->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) eb->len, fs_info->dirty_metadata_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) * If writeback for a btree extent that doesn't belong to a log tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) * failed, increment the counter transaction->eb_write_errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) * We do this because while the transaction is running and before it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) * committing (when we call filemap_fdata[write|wait]_range against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) * the btree inode), we might have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) * returns an error or an error happens during writeback, when we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) * committing the transaction we wouldn't know about it, since the pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) * can be no longer dirty nor marked anymore for writeback (if a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) * subsequent modification to the extent buffer didn't happen before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) * transaction commit), which makes filemap_fdata[write|wait]_range not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) * able to find the pages tagged with SetPageError at transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) * commit time. So if this happens we must abort the transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) * otherwise we commit a super block with btree roots that point to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) * btree nodes/leafs whose content on disk is invalid - either garbage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) * or the content of some node/leaf from a past generation that got
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) * cowed or deleted and is no longer valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) * not be enough - we need to distinguish between log tree extents vs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) * non-log tree extents, and the next filemap_fdatawait_range() call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) * will catch and clear such errors in the mapping - and that call might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) * be from a log sync and not from a transaction commit. Also, checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) * not done and would not be reliable - the eb might have been released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) * from memory and reading it back again means that flag would not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) * set (since it's a runtime flag, not persisted on disk).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) * Using the flags below in the btree inode also makes us achieve the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) * writeback for all dirty pages and before filemap_fdatawait_range()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) * is called, the writeback for all dirty pages had already finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) * with errors - because we were not using AS_EIO/AS_ENOSPC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) * filemap_fdatawait_range() would return success, as it could not know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) * that writeback errors happened (the pages were no longer tagged for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) * writeback).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) switch (eb->log_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) case -1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) BUG(); /* unexpected, logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) static void end_bio_extent_buffer_writepage(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) struct bio_vec *bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) int done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) struct bvec_iter_all iter_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) ASSERT(!bio_flagged(bio, BIO_CLONED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) bio_for_each_segment_all(bvec, bio, iter_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) struct page *page = bvec->bv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) eb = (struct extent_buffer *)page->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) BUG_ON(!eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) done = atomic_dec_and_test(&eb->io_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) if (bio->bi_status ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) set_btree_ioerr(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) if (!done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) end_extent_buffer_writeback(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) struct extent_page_data *epd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) u64 offset = eb->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) int i, num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) atomic_set(&eb->io_pages, num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) /* set btree blocks beyond nritems with 0 to avoid stale content. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) nritems = btrfs_header_nritems(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) if (btrfs_header_level(eb) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) end = btrfs_node_key_ptr_offset(nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) memzero_extent_buffer(eb, end, eb->len - end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) * leaf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) start = btrfs_item_nr_offset(nritems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) memzero_extent_buffer(eb, start, end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) struct page *p = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) clear_page_dirty_for_io(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) set_page_writeback(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) p, offset, PAGE_SIZE, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) &epd->bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) end_bio_extent_buffer_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 0, 0, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) set_btree_ioerr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) if (PageWriteback(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) end_page_writeback(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) end_extent_buffer_writeback(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) offset += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) update_nr_written(wbc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) unlock_page(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) if (unlikely(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) for (; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) struct page *p = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) clear_page_dirty_for_io(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) unlock_page(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) int btree_write_cache_pages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) struct extent_buffer *eb, *prev_eb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) struct extent_page_data epd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) .bio = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) .extent_locked = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) .sync_io = wbc->sync_mode == WB_SYNC_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) int done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) int nr_to_write_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) int nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) pgoff_t index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) pgoff_t end; /* Inclusive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) int scanned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) xa_mark_t tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) if (wbc->range_cyclic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) index = mapping->writeback_index; /* Start from prev offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) end = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) * Start from the beginning does not need to cycle over the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) * range, mark it as scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) scanned = (index == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) index = wbc->range_start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) end = wbc->range_end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) scanned = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) if (wbc->sync_mode == WB_SYNC_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) tag = PAGECACHE_TAG_TOWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) tag = PAGECACHE_TAG_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) if (wbc->sync_mode == WB_SYNC_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) tag_pages_for_writeback(mapping, index, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) while (!done && !nr_to_write_done && (index <= end) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) tag))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) struct page *page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) if (!PagePrivate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) spin_lock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) if (!PagePrivate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) spin_unlock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) eb = (struct extent_buffer *)page->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) * Shouldn't happen and normally this would be a BUG_ON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) * but no sense in crashing the users box for something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) * we can survive anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) if (WARN_ON(!eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) spin_unlock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) if (eb == prev_eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) spin_unlock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) ret = atomic_inc_not_zero(&eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) spin_unlock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) prev_eb = eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) ret = lock_extent_buffer_for_io(eb, &epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) } else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) ret = write_one_eb(eb, wbc, &epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) * the filesystem may choose to bump up nr_to_write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) * We have to make sure to honor the new nr_to_write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) * at any time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) nr_to_write_done = wbc->nr_to_write <= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) if (!scanned && !done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) * We hit the last page and there is more work to be done: wrap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) * back to the start of the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) scanned = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) ASSERT(ret <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) end_write_bio(&epd, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) * If something went wrong, don't allow any metadata write bio to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) * submitted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) * This would prevent use-after-free if we had dirty pages not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) * cleaned up, which can still happen by fuzzed images.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) * - Bad extent tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) * Allowing existing tree block to be allocated for other trees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) * - Log tree operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) * Exiting tree blocks get allocated to log tree, bumps its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) * generation, then get cleaned in tree re-balance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) * Such tree block will not be written back, since it's clean,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) * thus no WRITTEN flag set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) * And after log writes back, this tree block is not traced by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) * any dirty extent_io_tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) * - Offending tree block gets re-dirtied from its original owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) * Since it has bumped generation, no WRITTEN flag, it can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) * reused without COWing. This tree block will not be traced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) * by btrfs_transaction::dirty_pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) * Now such dirty tree block will not be cleaned by any dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) * extent io tree. Thus we don't want to submit such wild eb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) * if the fs already has error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) ret = flush_write_bio(&epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) end_write_bio(&epd, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) * @mapping: address space structure to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) * @wbc: subtract the number of written pages from *@wbc->nr_to_write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) * @data: data passed to __extent_writepage function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) * If a page is already under I/O, write_cache_pages() skips it, even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) * and msync() need to guarantee that all the data which was dirty at the time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) * the call was made get new I/O started against them. If wbc->sync_mode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) * WB_SYNC_ALL then we were called for data integrity and we must wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) * existing IO to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) static int extent_write_cache_pages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) struct extent_page_data *epd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) int done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) int nr_to_write_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) int nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) pgoff_t index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) pgoff_t end; /* Inclusive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) pgoff_t done_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) int range_whole = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) int scanned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) xa_mark_t tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) * We have to hold onto the inode so that ordered extents can do their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) * work when the IO finishes. The alternative to this is failing to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) * an ordered extent if the igrab() fails there and that is a huge pain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) * to deal with, so instead just hold onto the inode throughout the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) * writepages operation. If it fails here we are freeing up the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) * anyway and we'd rather not waste our time writing out stuff that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) * going to be truncated anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) if (!igrab(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) if (wbc->range_cyclic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) index = mapping->writeback_index; /* Start from prev offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) end = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) * Start from the beginning does not need to cycle over the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) * range, mark it as scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) scanned = (index == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) index = wbc->range_start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) end = wbc->range_end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) range_whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) scanned = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) * We do the tagged writepage as long as the snapshot flush bit is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) * and we are the first one who do the filemap_flush() on this inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) * The nr_to_write == LONG_MAX is needed to make sure other flushers do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) * not race in and drop the bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) if (range_whole && wbc->nr_to_write == LONG_MAX &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) &BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) wbc->tagged_writepages = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) tag = PAGECACHE_TAG_TOWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) tag = PAGECACHE_TAG_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) tag_pages_for_writeback(mapping, index, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) done_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) while (!done && !nr_to_write_done && (index <= end) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) &index, end, tag))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) struct page *page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) done_index = page->index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) * At this point we hold neither the i_pages lock nor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) * the page lock: the page may be truncated or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) * invalidated (changing page->mapping to NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) * or even swizzled back from swapper_space to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) * tmpfs file mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) if (!trylock_page(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) ret = flush_write_bio(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) if (unlikely(page->mapping != mapping)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) if (wbc->sync_mode != WB_SYNC_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) if (PageWriteback(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) ret = flush_write_bio(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) if (PageWriteback(page) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) !clear_page_dirty_for_io(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) ret = __extent_writepage(page, wbc, epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) * the filesystem may choose to bump up nr_to_write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) * We have to make sure to honor the new nr_to_write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) * at any time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) nr_to_write_done = wbc->nr_to_write <= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) if (!scanned && !done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) * We hit the last page and there is more work to be done: wrap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) * back to the start of the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) scanned = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) * If we're looping we could run into a page that is locked by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) * writer and that writer could be waiting on writeback for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) * page in our current bio, and thus deadlock, so flush the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) * write bio here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) ret = flush_write_bio(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) mapping->writeback_index = done_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) btrfs_add_delayed_iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) int extent_write_full_page(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) struct extent_page_data epd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) .bio = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) .extent_locked = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) .sync_io = wbc->sync_mode == WB_SYNC_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) ret = __extent_writepage(page, wbc, &epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) ASSERT(ret <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) end_write_bio(&epd, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) ret = flush_write_bio(&epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) ASSERT(ret <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) unsigned long nr_pages = (end - start + PAGE_SIZE) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) struct extent_page_data epd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) .bio = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) .extent_locked = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) .sync_io = mode == WB_SYNC_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) struct writeback_control wbc_writepages = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) .sync_mode = mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) .nr_to_write = nr_pages * 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) .range_start = start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) .range_end = end + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) /* We're called from an async helper function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) .punt_to_cgroup = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) .no_cgroup_owner = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) wbc_attach_fdatawrite_inode(&wbc_writepages, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) while (start <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) page = find_get_page(mapping, start >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) if (clear_page_dirty_for_io(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) ret = __extent_writepage(page, &wbc_writepages, &epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) btrfs_writepage_endio_finish_ordered(page, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) start + PAGE_SIZE - 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) start += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) ASSERT(ret <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) ret = flush_write_bio(&epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) end_write_bio(&epd, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) wbc_detach_inode(&wbc_writepages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) int extent_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) struct extent_page_data epd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) .bio = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) .extent_locked = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) .sync_io = wbc->sync_mode == WB_SYNC_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) ret = extent_write_cache_pages(mapping, wbc, &epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) ASSERT(ret <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) end_write_bio(&epd, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) ret = flush_write_bio(&epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) void extent_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) struct bio *bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) unsigned long bio_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) struct page *pagepool[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) struct extent_map *em_cached = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) u64 prev_em_start = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) while ((nr = readahead_page_batch(rac, pagepool))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) u64 contig_start = page_offset(pagepool[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) u64 contig_end = page_offset(pagepool[nr - 1]) + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) contiguous_readpages(pagepool, nr, contig_start, contig_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) &em_cached, &bio, &bio_flags, &prev_em_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) if (em_cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) free_extent_map(em_cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) if (bio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) if (submit_one_bio(bio, 0, bio_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) * basic invalidatepage code, this waits on any locked or writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) * ranges corresponding to the page, and then deletes any extent state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) * records from the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) int extent_invalidatepage(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) struct page *page, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) u64 end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) size_t blocksize = page->mapping->host->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) start += ALIGN(offset, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) if (start > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) lock_extent_bits(tree, start, end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) clear_extent_bit(tree, start, end, EXTENT_LOCKED | EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) EXTENT_DO_ACCOUNTING, 1, 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) * a helper for releasepage, this tests for areas of the page that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) * are locked or under IO and drops the related state bits if it is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) * to drop the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) static int try_release_extent_state(struct extent_io_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) struct page *page, gfp_t mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) u64 end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) * at this point we can safely clear everything except the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) * locked bit and the nodatasum bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) ret = __clear_extent_bit(tree, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) ~(EXTENT_LOCKED | EXTENT_NODATASUM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 0, 0, NULL, mask, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) /* if clear_extent_bit failed for enomem reasons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) * we can't allow the release to continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) * a helper for releasepage. As long as there are no locked extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) * in the range corresponding to the page, both state records and extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) * map records are removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) int try_release_extent_mapping(struct page *page, gfp_t mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) u64 start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) u64 end = start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) struct extent_io_tree *tree = &btrfs_inode->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) struct extent_map_tree *map = &btrfs_inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) if (gfpflags_allow_blocking(mask) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) page->mapping->host->i_size > SZ_16M) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) while (start <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) struct btrfs_fs_info *fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) u64 cur_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) len = end - start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) write_lock(&map->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) em = lookup_extent_mapping(map, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) write_unlock(&map->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) em->start != start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) write_unlock(&map->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) if (test_range_bit(tree, em->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) extent_map_end(em) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) EXTENT_LOCKED, 0, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) * If it's not in the list of modified extents, used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) * by a fast fsync, we can remove it. If it's being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) * logged we can safely remove it since fsync took an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) * extra reference on the em.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) if (list_empty(&em->list) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) test_bit(EXTENT_FLAG_LOGGING, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) goto remove_em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) * If it's in the list of modified extents, remove it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) * only if its generation is older then the current one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) * in which case we don't need it for a fast fsync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) * Otherwise don't remove it, we could be racing with an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) * ongoing fast fsync that could miss the new extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) fs_info = btrfs_inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) spin_lock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) cur_gen = fs_info->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) spin_unlock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) if (em->generation >= cur_gen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) remove_em:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) * We only remove extent maps that are not in the list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) * modified extents or that are in the list but with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) * generation lower then the current generation, so there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) * is no need to set the full fsync flag on the inode (it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) * hurts the fsync performance for workloads with a data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) * size that exceeds or is close to the system's memory).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) remove_extent_mapping(map, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) /* once for the rb tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) start = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) write_unlock(&map->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) /* once for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) cond_resched(); /* Allow large-extent preemption. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) return try_release_extent_state(tree, page, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) * helper function for fiemap, which doesn't want to see any holes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) * This maps until we find something past 'last'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) static struct extent_map *get_extent_skip_holes(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) u64 offset, u64 last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) u64 sectorsize = btrfs_inode_sectorsize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) if (offset >= last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) len = last - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) len = ALIGN(len, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) em = btrfs_get_extent_fiemap(inode, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) if (IS_ERR_OR_NULL(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) /* if this isn't a hole return it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) if (em->block_start != EXTENT_MAP_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) /* this is a hole, advance to the next extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) offset = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) if (offset >= last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) * To cache previous fiemap extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) * Will be used for merging fiemap extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) struct fiemap_cache {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) u64 phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) bool cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) * Helper to submit fiemap extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) * Will try to merge current fiemap extent specified by @offset, @phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) * @len and @flags with cached one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) * And only when we fails to merge, cached one will be submitted as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) * fiemap extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) * Return value is the same as fiemap_fill_next_extent().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) struct fiemap_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) u64 offset, u64 phys, u64 len, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) if (!cache->cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) goto assign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) * Sanity check, extent_fiemap() should have ensured that new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) * fiemap extent won't overlap with cached one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) * Not recoverable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) * NOTE: Physical address can overlap, due to compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) if (cache->offset + cache->len > offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) * Only merges fiemap extents if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) * 1) Their logical addresses are continuous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) * 2) Their physical addresses are continuous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) * So truly compressed (physical size smaller than logical size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) * extents won't get merged with each other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) * 3) Share same flags except FIEMAP_EXTENT_LAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) * So regular extent won't get merged with prealloc extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) if (cache->offset + cache->len == offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) cache->phys + cache->len == phys &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) (cache->flags & ~FIEMAP_EXTENT_LAST) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) (flags & ~FIEMAP_EXTENT_LAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) cache->len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) cache->flags |= flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) goto try_submit_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) /* Not mergeable, need to submit cached one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) cache->len, cache->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) cache->cached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) assign:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) cache->cached = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) cache->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) cache->phys = phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) cache->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) cache->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) try_submit_last:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) if (cache->flags & FIEMAP_EXTENT_LAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) ret = fiemap_fill_next_extent(fieinfo, cache->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) cache->phys, cache->len, cache->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) cache->cached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) * Emit last fiemap cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) * The last fiemap cache may still be cached in the following case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) * 0 4k 8k
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) * |<- Fiemap range ->|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) * |<------------ First extent ----------->|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) * In this case, the first extent range will be cached but not emitted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) * So we must emit it before ending extent_fiemap().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) struct fiemap_cache *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) if (!cache->cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) cache->len, cache->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) cache->cached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) u64 off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) u64 max = start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) u32 found_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) u64 last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) u64 last_for_get_extent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) u64 disko = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) u64 isize = i_size_read(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) struct extent_map *em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) struct fiemap_cache cache = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) struct ulist *roots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) struct ulist *tmp_ulist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) int end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) u64 em_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) u64 em_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) u64 em_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) roots = ulist_alloc(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) tmp_ulist = ulist_alloc(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) if (!roots || !tmp_ulist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) goto out_free_ulist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) * We can't initialize that to 'start' as this could miss extents due
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) * to extent item merging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) start = round_down(start, btrfs_inode_sectorsize(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) * lookup the last file extent. We're not using i_size here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) * because there might be preallocation past i_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) goto out_free_ulist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) WARN_ON(!ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) found_type = found_key.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) /* No extents, but there might be delalloc bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) if (found_key.objectid != btrfs_ino(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) found_type != BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) /* have to trust i_size as the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) last = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) last_for_get_extent = isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) * remember the start of the last extent. There are a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) * bunch of different factors that go into the length of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) * extent, so its much less complex to remember where it started
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) last = found_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) last_for_get_extent = last + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) * we might have some extents allocated but more delalloc past those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) * extents. so, we trust isize unless the start of the last extent is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) * beyond isize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) if (last < isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) last = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) last_for_get_extent = isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) lock_extent_bits(&inode->io_tree, start, start + len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) em = get_extent_skip_holes(inode, start, last_for_get_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) if (!em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) while (!end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) u64 offset_in_extent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) /* break if the extent we found is outside the range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) if (em->start >= max || extent_map_end(em) < off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) * get_extent may return an extent that starts before our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) * requested range. We have to make sure the ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) * we return to fiemap always move forward and don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) * overlap, so adjust the offsets here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) em_start = max(em->start, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) * record the offset from the start of the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) * for adjusting the disk offset below. Only do this if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) * extent isn't compressed since our in ram offset may be past
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) * what we have actually allocated on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) offset_in_extent = em_start - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) em_end = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) em_len = em_end - em_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) if (em->block_start < EXTENT_MAP_LAST_BYTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) disko = em->block_start + offset_in_extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) disko = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) * bump off for our next call to get_extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) off = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) if (off >= max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) end = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) if (em->block_start == EXTENT_MAP_LAST_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) end = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) flags |= FIEMAP_EXTENT_LAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) } else if (em->block_start == EXTENT_MAP_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) flags |= (FIEMAP_EXTENT_DATA_INLINE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) FIEMAP_EXTENT_NOT_ALIGNED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) } else if (em->block_start == EXTENT_MAP_DELALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) flags |= (FIEMAP_EXTENT_DELALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) FIEMAP_EXTENT_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) } else if (fieinfo->fi_extents_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) u64 bytenr = em->block_start -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) (em->start - em->orig_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) * As btrfs supports shared space, this information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) * can be exported to userspace tools via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) * then we're just getting a count and we can skip the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) * lookup stuff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) ret = btrfs_check_shared(root, btrfs_ino(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) bytenr, roots, tmp_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) flags |= FIEMAP_EXTENT_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) flags |= FIEMAP_EXTENT_ENCODED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) flags |= FIEMAP_EXTENT_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) if ((em_start >= last) || em_len == (u64)-1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) (last == (u64)-1 && isize <= em_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) flags |= FIEMAP_EXTENT_LAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) end = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) /* now scan forward to see if this is really the last extent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) em = get_extent_skip_holes(inode, off, last_for_get_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) flags |= FIEMAP_EXTENT_LAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) end = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) em_len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) ret = emit_last_fiemap_cache(fieinfo, &cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) unlock_extent_cached(&inode->io_tree, start, start + len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) out_free_ulist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) ulist_free(roots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) ulist_free(tmp_ulist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) static void __free_extent_buffer(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) kmem_cache_free(extent_buffer_cache, eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) int extent_buffer_under_io(const struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) return (atomic_read(&eb->io_pages) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) * Release all pages attached to the extent buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) BUG_ON(extent_buffer_under_io(eb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) struct page *page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) if (mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) spin_lock(&page->mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) * We do this since we'll remove the pages after we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) * removed the eb from the radix tree, so we could race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) * and have this page now attached to the new eb. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) * only clear page_private if it's still connected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) * this eb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) if (PagePrivate(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) page->private == (unsigned long)eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) BUG_ON(PageDirty(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) BUG_ON(PageWriteback(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) * We need to make sure we haven't be attached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) * to a new eb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930) detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) if (mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) spin_unlock(&page->mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) /* One for when we allocated the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) * Helper for releasing the extent buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) btrfs_release_extent_buffer_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) __free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) static struct extent_buffer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) struct extent_buffer *eb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) eb->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) eb->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) eb->fs_info = fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) eb->bflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) rwlock_init(&eb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) atomic_set(&eb->blocking_readers, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) eb->blocking_writers = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) eb->lock_recursed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) init_waitqueue_head(&eb->write_lock_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) init_waitqueue_head(&eb->read_lock_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) &fs_info->allocated_ebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) spin_lock_init(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) atomic_set(&eb->refs, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) atomic_set(&eb->io_pages, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) > MAX_INLINE_EXTENT_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) #ifdef CONFIG_BTRFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) eb->spinning_writers = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) atomic_set(&eb->spinning_readers, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) atomic_set(&eb->read_locks, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) eb->write_locks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) struct page *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) struct extent_buffer *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) int num_pages = num_extent_pages(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) if (new == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) p = alloc_page(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) btrfs_release_extent_buffer(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) attach_extent_buffer_page(new, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) WARN_ON(PageDirty(p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) SetPageUptodate(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) new->pages[i] = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) copy_page(page_address(p), page_address(src->pages[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) u64 start, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) eb = __alloc_extent_buffer(fs_info, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) if (!eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) eb->pages[i] = alloc_page(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) if (!eb->pages[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) set_extent_buffer_uptodate(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) btrfs_set_header_nritems(eb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) for (; i > 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) __free_page(eb->pages[i - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) __free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) static void check_buffer_tree_ref(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) int refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) * The TREE_REF bit is first set when the extent_buffer is added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) * to the radix tree. It is also reset, if unset, when a new reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) * is created by find_extent_buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) * It is only cleared in two cases: freeing the last non-tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) * reference to the extent_buffer when its STALE bit is set or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) * calling releasepage when the tree reference is the only reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) * In both cases, care is taken to ensure that the extent_buffer's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) * pages are not under io. However, releasepage can be concurrently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) * called with creating new references, which is prone to race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) * conditions between the calls to check_buffer_tree_ref in those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) * codepaths and clearing TREE_REF in try_release_extent_buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) * The actual lifetime of the extent_buffer in the radix tree is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) * adequately protected by the refcount, but the TREE_REF bit and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) * its corresponding reference are not. To protect against this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) * class of races, we call check_buffer_tree_ref from the codepaths
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) * which trigger io after they set eb->io_pages. Note that once io is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) * initiated, TREE_REF can no longer be cleared, so that is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) * moment at which any such race is best fixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) refs = atomic_read(&eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088) spin_lock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) atomic_inc(&eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) static void mark_extent_buffer_accessed(struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) struct page *accessed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) int num_pages, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) check_buffer_tree_ref(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) struct page *p = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) if (p != accessed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) mark_page_accessed(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) eb = radix_tree_lookup(&fs_info->buffer_radix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) start >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) if (eb && atomic_inc_not_zero(&eb->refs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) * Lock our eb's refs_lock to avoid races with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) * free_extent_buffer. When we get our eb it might be flagged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) * with EXTENT_BUFFER_STALE and another task running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) * free_extent_buffer might have seen that flag set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) * eb->refs == 2, that the buffer isn't under IO (dirty and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) * writeback flags not set) and it's still in the tree (flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) * EXTENT_BUFFER_TREE_REF set), therefore being in the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) * of decrementing the extent buffer's reference count twice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) * So here we could race and increment the eb's reference count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) * clear its stale flag, mark it as dirty and drop our reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) * before the other task finishes executing free_extent_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) * which would later result in an attempt to free an extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) * buffer that is dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) spin_lock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) mark_extent_buffer_accessed(eb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) struct extent_buffer *eb, *exists = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) eb = find_extent_buffer(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) if (eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) eb = alloc_dummy_extent_buffer(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) if (!eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) eb->fs_info = fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) ret = radix_tree_preload(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) exists = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) goto free_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167) spin_lock(&fs_info->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) ret = radix_tree_insert(&fs_info->buffer_radix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) start >> PAGE_SHIFT, eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) spin_unlock(&fs_info->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) radix_tree_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) exists = find_extent_buffer(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) if (exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) goto free_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) check_buffer_tree_ref(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) free_eb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) btrfs_release_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) return exists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) unsigned long len = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) unsigned long index = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) struct extent_buffer *exists = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) struct page *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) struct address_space *mapping = fs_info->btree_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) int uptodate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) if (!IS_ALIGNED(start, fs_info->sectorsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) btrfs_err(fs_info, "bad tree block start %llu", start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) eb = find_extent_buffer(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) if (eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) eb = __alloc_extent_buffer(fs_info, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) if (!eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) for (i = 0; i < num_pages; i++, index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) exists = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) goto free_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) spin_lock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) if (PagePrivate(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) * We could have already allocated an eb for this page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) * and attached one so lets see if we can get a ref on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) * the existing eb, and if we can we know it's good and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) * we can just return that one, else we know we can just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) * overwrite page->private.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) exists = (struct extent_buffer *)p->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) if (atomic_inc_not_zero(&exists->refs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) spin_unlock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) unlock_page(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) put_page(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) mark_extent_buffer_accessed(exists, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239) goto free_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) exists = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) * Do this so attach doesn't complain and we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) * drop the ref the old guy had.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) ClearPagePrivate(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) WARN_ON(PageDirty(p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) put_page(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) attach_extent_buffer_page(eb, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) spin_unlock(&mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253) WARN_ON(PageDirty(p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254) eb->pages[i] = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) if (!PageUptodate(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256) uptodate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) * We can't unlock the pages just yet since the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) * hasn't been properly inserted in the radix tree, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261) * opens a race with btree_releasepage which can free a page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262) * while we are still filling in all pages for the buffer and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) * we could crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) if (uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) ret = radix_tree_preload(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) exists = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) goto free_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) spin_lock(&fs_info->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276) ret = radix_tree_insert(&fs_info->buffer_radix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) start >> PAGE_SHIFT, eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) spin_unlock(&fs_info->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279) radix_tree_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) exists = find_extent_buffer(fs_info, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) if (exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) goto free_eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) /* add one reference for the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) check_buffer_tree_ref(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) * Now it's safe to unlock the pages because any calls to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) * btree_releasepage will correctly detect that a page belongs to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) * live buffer and won't free them prematurely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) for (i = 0; i < num_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) unlock_page(eb->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) return eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) free_eb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) WARN_ON(!atomic_dec_and_test(&eb->refs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) if (eb->pages[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) unlock_page(eb->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) btrfs_release_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) return exists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) struct extent_buffer *eb =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) container_of(head, struct extent_buffer, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) __free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) static int release_extent_buffer(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) __releases(&eb->refs_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) lockdep_assert_held(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) WARN_ON(atomic_read(&eb->refs) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) if (atomic_dec_and_test(&eb->refs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) struct btrfs_fs_info *fs_info = eb->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) spin_lock(&fs_info->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) radix_tree_delete(&fs_info->buffer_radix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) eb->start >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) spin_unlock(&fs_info->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) /* Should be safe to release our pages at this point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) btrfs_release_extent_buffer_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) __free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) void free_extent_buffer(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) int refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359) int old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) if (!eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364) refs = atomic_read(&eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) refs == 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370) if (old == refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) spin_lock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) if (atomic_read(&eb->refs) == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) !extent_buffer_under_io(eb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) atomic_dec(&eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) * I know this is terrible, but it's temporary until we stop tracking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) * the uptodate bits and such for the extent buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) release_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) void free_extent_buffer_stale(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) if (!eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) spin_lock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) atomic_dec(&eb->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) release_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) void clear_extent_buffer_dirty(const struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) if (!PageDirty(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) WARN_ON(!PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) xa_lock_irq(&page->mapping->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) if (!PageDirty(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) __xa_clear_mark(&page->mapping->i_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) page_index(page), PAGECACHE_TAG_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) xa_unlock_irq(&page->mapping->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) WARN_ON(atomic_read(&eb->refs) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) bool set_extent_buffer_dirty(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) bool was_dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) check_buffer_tree_ref(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) WARN_ON(atomic_read(&eb->refs) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) if (!was_dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) for (i = 0; i < num_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) set_page_dirty(eb->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) #ifdef CONFIG_BTRFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) for (i = 0; i < num_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) ASSERT(PageDirty(eb->pages[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) return was_dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) void clear_extent_buffer_uptodate(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) if (page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) void set_extent_buffer_uptodate(struct extent_buffer *eb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) int locked_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) int all_uptodate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) unsigned long num_reads = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) struct bio *bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) unsigned long bio_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501) num_pages = num_extent_pages(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) if (wait == WAIT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) if (!trylock_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) goto unlock_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) locked_pages++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) * We need to firstly lock all pages to make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) * the uptodate bit of our pages won't be affected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) * clear_extent_buffer_uptodate().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) num_reads++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) all_uptodate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) if (all_uptodate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) goto unlock_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) eb->read_mirror = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) atomic_set(&eb->io_pages, num_reads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) * It is possible for releasepage to clear the TREE_REF bit before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) * set io_pages. See check_buffer_tree_ref for a more detailed comment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) check_buffer_tree_ref(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) atomic_dec(&eb->io_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) err = submit_extent_page(REQ_OP_READ | REQ_META, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) page, page_offset(page), PAGE_SIZE, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) &bio, end_bio_extent_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) mirror_num, 0, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) * We failed to submit the bio so it's the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) * caller's responsibility to perform cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) * i.e unlock page/set error bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) atomic_dec(&eb->io_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) if (bio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) err = submit_one_bio(bio, mirror_num, bio_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) if (ret || wait != WAIT_COMPLETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) wait_on_page_locked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) if (!PageUptodate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) unlock_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) while (locked_pages > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) locked_pages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) page = eb->pages[locked_pages];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) btrfs_warn(eb->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) "access to eb bytenr %llu len %lu out of range start %lu len %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) eb->start, eb->len, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) * Check if the [start, start + len) range is valid before reading/writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) * the eb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) * NOTE: @start and @len are offset inside the eb, not logical address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) * Caller should not touch the dst/src memory if this function returns error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) static inline int check_eb_range(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) unsigned long start, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) /* start, start + len should not go beyond eb->len nor overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) return report_eb_range(eb, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) unsigned long start, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) char *dst = (char *)dstv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) unsigned long i = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) if (check_eb_range(eb, start, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) offset = offset_in_page(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) cur = min(len, (PAGE_SIZE - offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) memcpy(dst, kaddr + offset, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) dst += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) void __user *dstv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) unsigned long start, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) char __user *dst = (char __user *)dstv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) unsigned long i = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) WARN_ON(start > eb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) WARN_ON(start + len > eb->start + eb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) offset = offset_in_page(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) cur = min(len, (PAGE_SIZE - offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) dst += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) unsigned long start, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) char *ptr = (char *)ptrv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) unsigned long i = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) if (check_eb_range(eb, start, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) offset = offset_in_page(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) cur = min(len, (PAGE_SIZE - offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) ret = memcmp(ptr, kaddr + offset, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) ptr += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) const void *srcv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) WARN_ON(!PageUptodate(eb->pages[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) kaddr = page_address(eb->pages[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) BTRFS_FSID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) WARN_ON(!PageUptodate(eb->pages[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741) kaddr = page_address(eb->pages[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) BTRFS_FSID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) unsigned long start, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753) char *src = (char *)srcv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) unsigned long i = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756) if (check_eb_range(eb, start, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759) offset = offset_in_page(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765) cur = min(len, PAGE_SIZE - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) memcpy(kaddr + offset, src, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769) src += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) unsigned long i = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) if (check_eb_range(eb, start, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) offset = offset_in_page(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794) cur = min(len, PAGE_SIZE - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796) memset(kaddr + offset, 0, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) void copy_extent_buffer_full(const struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805) const struct extent_buffer *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810) ASSERT(dst->len == src->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812) num_pages = num_extent_pages(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813) for (i = 0; i < num_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814) copy_page(page_address(dst->pages[i]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815) page_address(src->pages[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818) void copy_extent_buffer(const struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819) const struct extent_buffer *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820) unsigned long dst_offset, unsigned long src_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823) u64 dst_len = dst->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828) unsigned long i = dst_offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830) if (check_eb_range(dst, dst_offset, len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831) check_eb_range(src, src_offset, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834) WARN_ON(src->len != dst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836) offset = offset_in_page(dst_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839) page = dst->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842) cur = min(len, (unsigned long)(PAGE_SIZE - offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) read_extent_buffer(src, kaddr + offset, src_offset, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) src_offset += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) * eb_bitmap_offset() - calculate the page and offset of the byte containing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) * given bit number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) * @eb: the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) * @start: offset of the bitmap item in the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) * @nr: bit number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) * @page_index: return index of the page in the extent buffer that contains the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861) * given bit number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) * @page_offset: return offset into the page given by page_index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864) * This helper hides the ugliness of finding the byte in an extent buffer which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) * contains a given bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) static inline void eb_bitmap_offset(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) unsigned long start, unsigned long nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869) unsigned long *page_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870) size_t *page_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) size_t byte_offset = BIT_BYTE(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876) * The byte we want is the offset of the extent buffer + the offset of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) * the bitmap item in the extent buffer + the offset of the byte in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878) * bitmap item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) offset = start + byte_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882) *page_index = offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) *page_offset = offset_in_page(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887) * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) * @eb: the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889) * @start: offset of the bitmap item in the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) * @nr: bit number to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) unsigned long nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895) u8 *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900) eb_bitmap_offset(eb, start, nr, &i, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904) return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) * extent_buffer_bitmap_set - set an area of a bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) * @eb: the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5910) * @start: offset of the bitmap item in the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5911) * @pos: bit number of the first bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5912) * @len: number of bits to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5913) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5914) void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5915) unsigned long pos, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5917) u8 *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5918) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5919) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5920) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5921) const unsigned int size = pos + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5922) int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5923) u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5925) eb_bitmap_offset(eb, start, pos, &i, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5926) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5927) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5928) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5930) while (len >= bits_to_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5931) kaddr[offset] |= mask_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5932) len -= bits_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5933) bits_to_set = BITS_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5934) mask_to_set = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5935) if (++offset >= PAGE_SIZE && len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5936) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5937) page = eb->pages[++i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5938) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5939) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5942) if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5943) mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5944) kaddr[offset] |= mask_to_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5949) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5950) * extent_buffer_bitmap_clear - clear an area of a bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5951) * @eb: the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5952) * @start: offset of the bitmap item in the extent buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5953) * @pos: bit number of the first bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5954) * @len: number of bits to clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5955) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5956) void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5957) unsigned long start, unsigned long pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5958) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5960) u8 *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5961) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5962) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5963) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5964) const unsigned int size = pos + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5965) int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5966) u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5968) eb_bitmap_offset(eb, start, pos, &i, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5969) page = eb->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5970) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5971) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5973) while (len >= bits_to_clear) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5974) kaddr[offset] &= ~mask_to_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5975) len -= bits_to_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5976) bits_to_clear = BITS_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5977) mask_to_clear = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5978) if (++offset >= PAGE_SIZE && len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5979) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5980) page = eb->pages[++i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5981) WARN_ON(!PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5982) kaddr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5985) if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5986) mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5987) kaddr[offset] &= ~mask_to_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5991) static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5992) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5993) unsigned long distance = (src > dst) ? src - dst : dst - src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5994) return distance < len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5997) static void copy_pages(struct page *dst_page, struct page *src_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5998) unsigned long dst_off, unsigned long src_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5999) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6001) char *dst_kaddr = page_address(dst_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6002) char *src_kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6003) int must_memmove = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6005) if (dst_page != src_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6006) src_kaddr = page_address(src_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6007) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6008) src_kaddr = dst_kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6009) if (areas_overlap(src_off, dst_off, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6010) must_memmove = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6013) if (must_memmove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6014) memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6015) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6016) memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6019) void memcpy_extent_buffer(const struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6020) unsigned long dst_offset, unsigned long src_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6021) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6023) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6024) size_t dst_off_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6025) size_t src_off_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6026) unsigned long dst_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6027) unsigned long src_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6029) if (check_eb_range(dst, dst_offset, len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6030) check_eb_range(dst, src_offset, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6031) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6033) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6034) dst_off_in_page = offset_in_page(dst_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6035) src_off_in_page = offset_in_page(src_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6037) dst_i = dst_offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6038) src_i = src_offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6040) cur = min(len, (unsigned long)(PAGE_SIZE -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6041) src_off_in_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6042) cur = min_t(unsigned long, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6043) (unsigned long)(PAGE_SIZE - dst_off_in_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6045) copy_pages(dst->pages[dst_i], dst->pages[src_i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6046) dst_off_in_page, src_off_in_page, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6048) src_offset += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6049) dst_offset += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6050) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6054) void memmove_extent_buffer(const struct extent_buffer *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6055) unsigned long dst_offset, unsigned long src_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6056) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6058) size_t cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6059) size_t dst_off_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6060) size_t src_off_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6061) unsigned long dst_end = dst_offset + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6062) unsigned long src_end = src_offset + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6063) unsigned long dst_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6064) unsigned long src_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6066) if (check_eb_range(dst, dst_offset, len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6067) check_eb_range(dst, src_offset, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6068) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6069) if (dst_offset < src_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6070) memcpy_extent_buffer(dst, dst_offset, src_offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6071) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6073) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6074) dst_i = dst_end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6075) src_i = src_end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6077) dst_off_in_page = offset_in_page(dst_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6078) src_off_in_page = offset_in_page(src_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6080) cur = min_t(unsigned long, len, src_off_in_page + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6081) cur = min(cur, dst_off_in_page + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6082) copy_pages(dst->pages[dst_i], dst->pages[src_i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6083) dst_off_in_page - cur + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6084) src_off_in_page - cur + 1, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6086) dst_end -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6087) src_end -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6088) len -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6092) int try_release_extent_buffer(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6094) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6096) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6097) * We need to make sure nobody is attaching this page to an eb right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6098) * now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6099) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6100) spin_lock(&page->mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6101) if (!PagePrivate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6102) spin_unlock(&page->mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6103) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6106) eb = (struct extent_buffer *)page->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6107) BUG_ON(!eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6110) * This is a little awful but should be ok, we need to make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6111) * the eb doesn't disappear out from under us while we're looking at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6112) * this page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6114) spin_lock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6115) if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6116) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6117) spin_unlock(&page->mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6118) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6120) spin_unlock(&page->mapping->private_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6123) * If tree ref isn't set then we know the ref on this eb is a real ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6124) * so just return, this page will likely be freed soon anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6126) if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6127) spin_unlock(&eb->refs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6128) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6131) return release_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6132) }