| // SPDX-License-Identifier: GPL-2.0 |
| #include <linux/bitops.h> |
| #include <linux/slab.h> |
| #include <linux/bio.h> |
| #include <linux/mm.h> |
| #include <linux/pagemap.h> |
| #include <linux/page-flags.h> |
| #include <linux/spinlock.h> |
| #include <linux/blkdev.h> |
| #include <linux/swap.h> |
| #include <linux/writeback.h> |
| #include <linux/pagevec.h> |
| #include <linux/prefetch.h> |
| #include <linux/cleancache.h> |
| #include "extent_io.h" |
| #include "extent_map.h" |
| #include "ctree.h" |
| #include "btrfs_inode.h" |
| #include "volumes.h" |
| #include "check-integrity.h" |
| #include "locking.h" |
| #include "rcu-string.h" |
| #include "backref.h" |
| |
| static struct kmem_cache *extent_state_cache; |
| static struct kmem_cache *extent_buffer_cache; |
| static struct bio_set *btrfs_bioset; |
| |
| static inline bool extent_state_in_tree(const struct extent_state *state) |
| { |
| return !RB_EMPTY_NODE(&state->rb_node); |
| } |
| |
| #ifdef CONFIG_BTRFS_DEBUG |
| static LIST_HEAD(buffers); |
| static LIST_HEAD(states); |
| |
| static DEFINE_SPINLOCK(leak_lock); |
| |
| static inline |
| void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) |
| { |
| unsigned long flags; |
| |
| spin_lock_irqsave(&leak_lock, flags); |
| list_add(new, head); |
| spin_unlock_irqrestore(&leak_lock, flags); |
| } |
| |
| static inline |
| void btrfs_leak_debug_del(struct list_head *entry) |
| { |
| unsigned long flags; |
| |
| spin_lock_irqsave(&leak_lock, flags); |
| list_del(entry); |
| spin_unlock_irqrestore(&leak_lock, flags); |
| } |
| |
| static inline |
| void btrfs_leak_debug_check(void) |
| { |
| struct extent_state *state; |
| struct extent_buffer *eb; |
| |
| while (!list_empty(&states)) { |
| state = list_entry(states.next, struct extent_state, leak_list); |
| pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n", |
| state->start, state->end, state->state, |
| extent_state_in_tree(state), |
| refcount_read(&state->refs)); |
| list_del(&state->leak_list); |
| kmem_cache_free(extent_state_cache, state); |
| } |
| |
| while (!list_empty(&buffers)) { |
| eb = list_entry(buffers.next, struct extent_buffer, leak_list); |
| pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n", |
| eb->start, eb->len, atomic_read(&eb->refs)); |
| list_del(&eb->leak_list); |
| kmem_cache_free(extent_buffer_cache, eb); |
| } |
| } |
| |
| #define btrfs_debug_check_extent_io_range(tree, start, end) \ |
| __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) |
| static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
| struct extent_io_tree *tree, u64 start, u64 end) |
| { |
| if (tree->ops && tree->ops->check_extent_io_range) |
| tree->ops->check_extent_io_range(tree->private_data, caller, |
| start, end); |
| } |
| #else |
| #define btrfs_leak_debug_add(new, head) do {} while (0) |
| #define btrfs_leak_debug_del(entry) do {} while (0) |
| #define btrfs_leak_debug_check() do {} while (0) |
| #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0) |
| #endif |
| |
| #define BUFFER_LRU_MAX 64 |
| |
| struct tree_entry { |
| u64 start; |
| u64 end; |
| struct rb_node rb_node; |
| }; |
| |
| struct extent_page_data { |
| struct bio *bio; |
| struct extent_io_tree *tree; |
| get_extent_t *get_extent; |
| unsigned long bio_flags; |
| |
| /* tells writepage not to lock the state bits for this range |
| * it still does the unlocking |
| */ |
| unsigned int extent_locked:1; |
| |
| /* tells the submit_bio code to use REQ_SYNC */ |
| unsigned int sync_io:1; |
| }; |
| |
| static void add_extent_changeset(struct extent_state *state, unsigned bits, |
| struct extent_changeset *changeset, |
| int set) |
| { |
| int ret; |
| |
| if (!changeset) |
| return; |
| if (set && (state->state & bits) == bits) |
| return; |
| if (!set && (state->state & bits) == 0) |
| return; |
| changeset->bytes_changed += state->end - state->start + 1; |
| ret = ulist_add(&changeset->range_changed, state->start, state->end, |
| GFP_ATOMIC); |
| /* ENOMEM */ |
| BUG_ON(ret < 0); |
| } |
| |
| static noinline void flush_write_bio(void *data); |
| static inline struct btrfs_fs_info * |
| tree_fs_info(struct extent_io_tree *tree) |
| { |
| if (tree->ops) |
| return tree->ops->tree_fs_info(tree->private_data); |
| return NULL; |
| } |
| |
| int __init extent_io_init(void) |
| { |
| extent_state_cache = kmem_cache_create("btrfs_extent_state", |
| sizeof(struct extent_state), 0, |
| SLAB_MEM_SPREAD, NULL); |
| if (!extent_state_cache) |
| return -ENOMEM; |
| |
| extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
| sizeof(struct extent_buffer), 0, |
| SLAB_MEM_SPREAD, NULL); |
| if (!extent_buffer_cache) |
| goto free_state_cache; |
| |
| btrfs_bioset = bioset_create(BIO_POOL_SIZE, |
| offsetof(struct btrfs_io_bio, bio), |
| BIOSET_NEED_BVECS); |
| if (!btrfs_bioset) |
| goto free_buffer_cache; |
| |
| if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE)) |
| goto free_bioset; |
| |
| return 0; |
| |
| free_bioset: |
| bioset_free(btrfs_bioset); |
| btrfs_bioset = NULL; |
| |
| free_buffer_cache: |
| kmem_cache_destroy(extent_buffer_cache); |
| extent_buffer_cache = NULL; |
| |
| free_state_cache: |
| kmem_cache_destroy(extent_state_cache); |
| extent_state_cache = NULL; |
| return -ENOMEM; |
| } |
| |
| void extent_io_exit(void) |
| { |
| btrfs_leak_debug_check(); |
| |
| /* |
| * Make sure all delayed rcu free are flushed before we |
| * destroy caches. |
| */ |
| rcu_barrier(); |
| kmem_cache_destroy(extent_state_cache); |
| kmem_cache_destroy(extent_buffer_cache); |
| if (btrfs_bioset) |
| bioset_free(btrfs_bioset); |
| } |
| |
| void extent_io_tree_init(struct extent_io_tree *tree, |
| void *private_data) |
| { |
| tree->state = RB_ROOT; |
| tree->ops = NULL; |
| tree->dirty_bytes = 0; |
| spin_lock_init(&tree->lock); |
| tree->private_data = private_data; |
| } |
| |
| static struct extent_state *alloc_extent_state(gfp_t mask) |
| { |
| struct extent_state *state; |
| |
| /* |
| * The given mask might be not appropriate for the slab allocator, |
| * drop the unsupported bits |
| */ |
| mask &= ~(__GFP_DMA32|__GFP_HIGHMEM); |
| state = kmem_cache_alloc(extent_state_cache, mask); |
| if (!state) |
| return state; |
| state->state = 0; |
| state->failrec = NULL; |
| RB_CLEAR_NODE(&state->rb_node); |
| btrfs_leak_debug_add(&state->leak_list, &states); |
| refcount_set(&state->refs, 1); |
| init_waitqueue_head(&state->wq); |
| trace_alloc_extent_state(state, mask, _RET_IP_); |
| return state; |
| } |
| |
| void free_extent_state(struct extent_state *state) |
| { |
| if (!state) |
| return; |
| if (refcount_dec_and_test(&state->refs)) { |
| WARN_ON(extent_state_in_tree(state)); |
| btrfs_leak_debug_del(&state->leak_list); |
| trace_free_extent_state(state, _RET_IP_); |
| kmem_cache_free(extent_state_cache, state); |
| } |
| } |
| |
| static struct rb_node *tree_insert(struct rb_root *root, |
| struct rb_node *search_start, |
| u64 offset, |
| struct rb_node *node, |
| struct rb_node ***p_in, |
| struct rb_node **parent_in) |
| { |
| struct rb_node **p; |
| struct rb_node *parent = NULL; |
| struct tree_entry *entry; |
| |
| if (p_in && parent_in) { |
| p = *p_in; |
| parent = *parent_in; |
| goto do_insert; |
| } |
| |
| p = search_start ? &search_start : &root->rb_node; |
| while (*p) { |
| parent = *p; |
| entry = rb_entry(parent, struct tree_entry, rb_node); |
| |
| if (offset < entry->start) |
| p = &(*p)->rb_left; |
| else if (offset > entry->end) |
| p = &(*p)->rb_right; |
| else |
| return parent; |
| } |
| |
| do_insert: |
| rb_link_node(node, parent, p); |
| rb_insert_color(node, root); |
| return NULL; |
| } |
| |
| static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset, |
| struct rb_node **prev_ret, |
| struct rb_node **next_ret, |
| struct rb_node ***p_ret, |
| struct rb_node **parent_ret) |
| { |
| struct rb_root *root = &tree->state; |
| struct rb_node **n = &root->rb_node; |
| struct rb_node *prev = NULL; |
| struct rb_node *orig_prev = NULL; |
| struct tree_entry *entry; |
| struct tree_entry *prev_entry = NULL; |
| |
| while (*n) { |
| prev = *n; |
| entry = rb_entry(prev, struct tree_entry, rb_node); |
| prev_entry = entry; |
| |
| if (offset < entry->start) |
| n = &(*n)->rb_left; |
| else if (offset > entry->end) |
| n = &(*n)->rb_right; |
| else |
| return *n; |
| } |
| |
| if (p_ret) |
| *p_ret = n; |
| if (parent_ret) |
| *parent_ret = prev; |
| |
| if (prev_ret) { |
| orig_prev = prev; |
| while (prev && offset > prev_entry->end) { |
| prev = rb_next(prev); |
| prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| } |
| *prev_ret = prev; |
| prev = orig_prev; |
| } |
| |
| if (next_ret) { |
| prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| while (prev && offset < prev_entry->start) { |
| prev = rb_prev(prev); |
| prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| } |
| *next_ret = prev; |
| } |
| return NULL; |
| } |
| |
| static inline struct rb_node * |
| tree_search_for_insert(struct extent_io_tree *tree, |
| u64 offset, |
| struct rb_node ***p_ret, |
| struct rb_node **parent_ret) |
| { |
| struct rb_node *prev = NULL; |
| struct rb_node *ret; |
| |
| ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret); |
| if (!ret) |
| return prev; |
| return ret; |
| } |
| |
| static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| u64 offset) |
| { |
| return tree_search_for_insert(tree, offset, NULL, NULL); |
| } |
| |
| static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
| struct extent_state *other) |
| { |
| if (tree->ops && tree->ops->merge_extent_hook) |
| tree->ops->merge_extent_hook(tree->private_data, new, other); |
| } |
| |
| /* |
| * utility function to look for merge candidates inside a given range. |
| * Any extents with matching state are merged together into a single |
| * extent in the tree. Extents with EXTENT_IO in their state field |
| * are not merged because the end_io handlers need to be able to do |
| * operations on them without sleeping (or doing allocations/splits). |
| * |
| * This should be called with the tree lock held. |
| */ |
| static void merge_state(struct extent_io_tree *tree, |
| struct extent_state *state) |
| { |
| struct extent_state *other; |
| struct rb_node *other_node; |
| |
| if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| return; |
| |
| other_node = rb_prev(&state->rb_node); |
| if (other_node) { |
| other = rb_entry(other_node, struct extent_state, rb_node); |
| if (other->end == state->start - 1 && |
| other->state == state->state) { |
| merge_cb(tree, state, other); |
| state->start = other->start; |
| rb_erase(&other->rb_node, &tree->state); |
| RB_CLEAR_NODE(&other->rb_node); |
| free_extent_state(other); |
| } |
| } |
| other_node = rb_next(&state->rb_node); |
| if (other_node) { |
| other = rb_entry(other_node, struct extent_state, rb_node); |
| if (other->start == state->end + 1 && |
| other->state == state->state) { |
| merge_cb(tree, state, other); |
| state->end = other->end; |
| rb_erase(&other->rb_node, &tree->state); |
| RB_CLEAR_NODE(&other->rb_node); |
| free_extent_state(other); |
| } |
| } |
| } |
| |
| static void set_state_cb(struct extent_io_tree *tree, |
| struct extent_state *state, unsigned *bits) |
| { |
| if (tree->ops && tree->ops->set_bit_hook) |
| tree->ops->set_bit_hook(tree->private_data, state, bits); |
| } |
| |
| static void clear_state_cb(struct extent_io_tree *tree, |
| struct extent_state *state, unsigned *bits) |
| { |
| if (tree->ops && tree->ops->clear_bit_hook) |
| tree->ops->clear_bit_hook(tree->private_data, state, bits); |
| } |
| |
| static void set_state_bits(struct extent_io_tree *tree, |
| struct extent_state *state, unsigned *bits, |
| struct extent_changeset *changeset); |
| |
| /* |
| * insert an extent_state struct into the tree. 'bits' are set on the |
| * struct before it is inserted. |
| * |
| * This may return -EEXIST if the extent is already there, in which case the |
| * state struct is freed. |
| * |
| * The tree lock is not taken internally. This is a utility function and |
| * probably isn't what you want to call (see set/clear_extent_bit). |
| */ |
| static int insert_state(struct extent_io_tree *tree, |
| struct extent_state *state, u64 start, u64 end, |
| struct rb_node ***p, |
| struct rb_node **parent, |
| unsigned *bits, struct extent_changeset *changeset) |
| { |
| struct rb_node *node; |
| |
| if (end < start) |
| WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n", |
| end, start); |
| state->start = start; |
| state->end = end; |
| |
| set_state_bits(tree, state, bits, changeset); |
| |
| node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
| if (node) { |
| struct extent_state *found; |
| found = rb_entry(node, struct extent_state, rb_node); |
| pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n", |
| found->start, found->end, start, end); |
| return -EEXIST; |
| } |
| merge_state(tree, state); |
| return 0; |
| } |
| |
| static void split_cb(struct extent_io_tree *tree, struct extent_state *orig, |
| u64 split) |
| { |
| if (tree->ops && tree->ops->split_extent_hook) |
| tree->ops->split_extent_hook(tree->private_data, orig, split); |
| } |
| |
| /* |
| * split a given extent state struct in two, inserting the preallocated |
| * struct 'prealloc' as the newly created second half. 'split' indicates an |
| * offset inside 'orig' where it should be split. |
| * |
| * Before calling, |
| * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| * are two extent state structs in the tree: |
| * prealloc: [orig->start, split - 1] |
| * orig: [ split, orig->end ] |
| * |
| * The tree locks are not taken by this function. They need to be held |
| * by the caller. |
| */ |
| static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| struct extent_state *prealloc, u64 split) |
| { |
| struct rb_node *node; |
| |
| split_cb(tree, orig, split); |
| |
| prealloc->start = orig->start; |
| prealloc->end = split - 1; |
| prealloc->state = orig->state; |
| orig->start = split; |
| |
| node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| &prealloc->rb_node, NULL, NULL); |
| if (node) { |
| free_extent_state(prealloc); |
| return -EEXIST; |
| } |
| return 0; |
| } |
| |
| static struct extent_state *next_state(struct extent_state *state) |
| { |
| struct rb_node *next = rb_next(&state->rb_node); |
| if (next) |
| return rb_entry(next, struct extent_state, rb_node); |
| else |
| return NULL; |
| } |
| |
| /* |
| * utility function to clear some bits in an extent state struct. |
| * it will optionally wake up any one waiting on this state (wake == 1). |
| * |
| * If no bits are set on the state struct after clearing things, the |
| * struct is freed and removed from the tree |
| */ |
| static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| struct extent_state *state, |
| unsigned *bits, int wake, |
| struct extent_changeset *changeset) |
| { |
| struct extent_state *next; |
| unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
| |
| if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
| u64 range = state->end - state->start + 1; |
| WARN_ON(range > tree->dirty_bytes); |
| tree->dirty_bytes -= range; |
| } |
| clear_state_cb(tree, state, bits); |
| add_extent_changeset(state, bits_to_clear, changeset, 0); |
| state->state &= ~bits_to_clear; |
| if (wake) |
| wake_up(&state->wq); |
| if (state->state == 0) { |
| next = next_state(state); |
| if (extent_state_in_tree(state)) { |
| rb_erase(&state->rb_node, &tree->state); |
| RB_CLEAR_NODE(&state->rb_node); |
| free_extent_state(state); |
| } else { |
| WARN_ON(1); |
| } |
| } else { |
| merge_state(tree, state); |
| next = next_state(state); |
| } |
| return next; |
| } |
| |
| static struct extent_state * |
| alloc_extent_state_atomic(struct extent_state *prealloc) |
| { |
| if (!prealloc) |
| prealloc = alloc_extent_state(GFP_ATOMIC); |
| |
| return prealloc; |
| } |
| |
| static void extent_io_tree_panic(struct extent_io_tree *tree, int err) |
| { |
| btrfs_panic(tree_fs_info(tree), err, |
| "Locking error: Extent tree was modified by another thread while locked."); |
| } |
| |
| /* |
| * clear some bits on a range in the tree. This may require splitting |
| * or inserting elements in the tree, so the gfp mask is used to |
| * indicate which allocations or sleeping are allowed. |
| * |
| * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| * the given range from the tree regardless of state (ie for truncate). |
| * |
| * the range [start, end] is inclusive. |
| * |
| * This takes the tree lock, and returns 0 on success and < 0 on error. |
| */ |
| static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, int wake, int delete, |
| struct extent_state **cached_state, |
| gfp_t mask, struct extent_changeset *changeset) |
| { |
| struct extent_state *state; |
| struct extent_state *cached; |
| struct extent_state *prealloc = NULL; |
| struct rb_node *node; |
| u64 last_end; |
| int err; |
| int clear = 0; |
| |
| btrfs_debug_check_extent_io_range(tree, start, end); |
| |
| if (bits & EXTENT_DELALLOC) |
| bits |= EXTENT_NORESERVE; |
| |
| if (delete) |
| bits |= ~EXTENT_CTLBITS; |
| bits |= EXTENT_FIRST_DELALLOC; |
| |
| if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
| clear = 1; |
| again: |
| if (!prealloc && gfpflags_allow_blocking(mask)) { |
| /* |
| * Don't care for allocation failure here because we might end |
| * up not needing the pre-allocated extent state at all, which |
| * is the case if we only have in the tree extent states that |
| * cover our input range and don't cover too any other range. |
| * If we end up needing a new extent state we allocate it later. |
| */ |
| prealloc = alloc_extent_state(mask); |
| } |
| |
| spin_lock(&tree->lock); |
| if (cached_state) { |
| cached = *cached_state; |
| |
| if (clear) { |
| *cached_state = NULL; |
| cached_state = NULL; |
| } |
| |
| if (cached && extent_state_in_tree(cached) && |
| cached->start <= start && cached->end > start) { |
| if (clear) |
| refcount_dec(&cached->refs); |
| state = cached; |
| goto hit_next; |
| } |
| if (clear) |
| free_extent_state(cached); |
| } |
| /* |
| * this search will find the extents that end after |
| * our range starts |
| */ |
| node = tree_search(tree, start); |
| if (!node) |
| goto out; |
| state = rb_entry(node, struct extent_state, rb_node); |
| hit_next: |
| if (state->start > end) |
| goto out; |
| WARN_ON(state->end < start); |
| last_end = state->end; |
| |
| /* the state doesn't have the wanted bits, go ahead */ |
| if (!(state->state & bits)) { |
| state = next_state(state); |
| goto next; |
| } |
| |
| /* |
| * | ---- desired range ---- | |
| * | state | or |
| * | ------------- state -------------- | |
| * |
| * We need to split the extent we found, and may flip |
| * bits on second half. |
| * |
| * If the extent we found extends past our range, we |
| * just split and search again. It'll get split again |
| * the next time though. |
| * |
| * If the extent we found is inside our range, we clear |
| * the desired bit on it. |
| */ |
| |
| if (state->start < start) { |
| prealloc = alloc_extent_state_atomic(prealloc); |
| BUG_ON(!prealloc); |
| err = split_state(tree, state, prealloc, start); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| |
| prealloc = NULL; |
| if (err) |
| goto out; |
| if (state->end <= end) { |
| state = clear_state_bit(tree, state, &bits, wake, |
| changeset); |
| goto next; |
| } |
| goto search_again; |
| } |
| /* |
| * | ---- desired range ---- | |
| * | state | |
| * We need to split the extent, and clear the bit |
| * on the first half |
| */ |
| if (state->start <= end && state->end > end) { |
| prealloc = alloc_extent_state_atomic(prealloc); |
| BUG_ON(!prealloc); |
| err = split_state(tree, state, prealloc, end + 1); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| |
| if (wake) |
| wake_up(&state->wq); |
| |
| clear_state_bit(tree, prealloc, &bits, wake, changeset); |
| |
| prealloc = NULL; |
| goto out; |
| } |
| |
| state = clear_state_bit(tree, state, &bits, wake, changeset); |
| next: |
| if (last_end == (u64)-1) |
| goto out; |
| start = last_end + 1; |
| if (start <= end && state && !need_resched()) |
| goto hit_next; |
| |
| search_again: |
| if (start > end) |
| goto out; |
| spin_unlock(&tree->lock); |
| if (gfpflags_allow_blocking(mask)) |
| cond_resched(); |
| goto again; |
| |
| out: |
| spin_unlock(&tree->lock); |
| if (prealloc) |
| free_extent_state(prealloc); |
| |
| return 0; |
| |
| } |
| |
| static void wait_on_state(struct extent_io_tree *tree, |
| struct extent_state *state) |
| __releases(tree->lock) |
| __acquires(tree->lock) |
| { |
| DEFINE_WAIT(wait); |
| prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
| spin_unlock(&tree->lock); |
| schedule(); |
| spin_lock(&tree->lock); |
| finish_wait(&state->wq, &wait); |
| } |
| |
| /* |
| * waits for one or more bits to clear on a range in the state tree. |
| * The range [start, end] is inclusive. |
| * The tree lock is taken by this function |
| */ |
| static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned long bits) |
| { |
| struct extent_state *state; |
| struct rb_node *node; |
| |
| btrfs_debug_check_extent_io_range(tree, start, end); |
| |
| spin_lock(&tree->lock); |
| again: |
| while (1) { |
| /* |
| * this search will find all the extents that end after |
| * our range starts |
| */ |
| node = tree_search(tree, start); |
| process_node: |
| if (!node) |
| break; |
| |
| state = rb_entry(node, struct extent_state, rb_node); |
| |
| if (state->start > end) |
| goto out; |
| |
| if (state->state & bits) { |
| start = state->start; |
| refcount_inc(&state->refs); |
| wait_on_state(tree, state); |
| free_extent_state(state); |
| goto again; |
| } |
| start = state->end + 1; |
| |
| if (start > end) |
| break; |
| |
| if (!cond_resched_lock(&tree->lock)) { |
| node = rb_next(node); |
| goto process_node; |
| } |
| } |
| out: |
| spin_unlock(&tree->lock); |
| } |
| |
| static void set_state_bits(struct extent_io_tree *tree, |
| struct extent_state *state, |
| unsigned *bits, struct extent_changeset *changeset) |
| { |
| unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
| |
| set_state_cb(tree, state, bits); |
| if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
| u64 range = state->end - state->start + 1; |
| tree->dirty_bytes += range; |
| } |
| add_extent_changeset(state, bits_to_set, changeset, 1); |
| state->state |= bits_to_set; |
| } |
| |
| static void cache_state_if_flags(struct extent_state *state, |
| struct extent_state **cached_ptr, |
| unsigned flags) |
| { |
| if (cached_ptr && !(*cached_ptr)) { |
| if (!flags || (state->state & flags)) { |
| *cached_ptr = state; |
| refcount_inc(&state->refs); |
| } |
| } |
| } |
| |
| static void cache_state(struct extent_state *state, |
| struct extent_state **cached_ptr) |
| { |
| return cache_state_if_flags(state, cached_ptr, |
| EXTENT_IOBITS | EXTENT_BOUNDARY); |
| } |
| |
| /* |
| * set some bits on a range in the tree. This may require allocations or |
| * sleeping, so the gfp mask is used to indicate what is allowed. |
| * |
| * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| * part of the range already has the desired bits set. The start of the |
| * existing range is returned in failed_start in this case. |
| * |
| * [start, end] is inclusive This takes the tree lock. |
| */ |
| |
| static int __must_check |
| __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, unsigned exclusive_bits, |
| u64 *failed_start, struct extent_state **cached_state, |
| gfp_t mask, struct extent_changeset *changeset) |
| { |
| struct extent_state *state; |
| struct extent_state *prealloc = NULL; |
| struct rb_node *node; |
| struct rb_node **p; |
| struct rb_node *parent; |
| int err = 0; |
| u64 last_start; |
| u64 last_end; |
| |
| btrfs_debug_check_extent_io_range(tree, start, end); |
| |
| bits |= EXTENT_FIRST_DELALLOC; |
| again: |
| if (!prealloc && gfpflags_allow_blocking(mask)) { |
| /* |
| * Don't care for allocation failure here because we might end |
| * up not needing the pre-allocated extent state at all, which |
| * is the case if we only have in the tree extent states that |
| * cover our input range and don't cover too any other range. |
| * If we end up needing a new extent state we allocate it later. |
| */ |
| prealloc = alloc_extent_state(mask); |
| } |
| |
| spin_lock(&tree->lock); |
| if (cached_state && *cached_state) { |
| state = *cached_state; |
| if (state->start <= start && state->end > start && |
| extent_state_in_tree(state)) { |
| node = &state->rb_node; |
| goto hit_next; |
| } |
| } |
| /* |
| * this search will find all the extents that end after |
| * our range starts. |
| */ |
| node = tree_search_for_insert(tree, start, &p, &parent); |
| if (!node) { |
| prealloc = alloc_extent_state_atomic(prealloc); |
| BUG_ON(!prealloc); |
| err = insert_state(tree, prealloc, start, end, |
| &p, &parent, &bits, changeset); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| |
| cache_state(prealloc, cached_state); |
| prealloc = NULL; |
| goto out; |
| } |
| state = rb_entry(node, struct extent_state, rb_node); |
| hit_next: |
| last_start = state->start; |
| last_end = state->end; |
| |
| /* |
| * | ---- desired range ---- | |
| * | state | |
| * |
| * Just lock what we found and keep going |
| */ |
| if (state->start == start && state->end <= end) { |
| if (state->state & exclusive_bits) { |
| *failed_start = state->start; |
| err = -EEXIST; |
| goto out; |
| } |
| |
| set_state_bits(tree, state, &bits, changeset); |
| cache_state(state, cached_state); |
| merge_state(tree, state); |
| if (last_end == (u64)-1) |
| goto out; |
| start = last_end + 1; |
| state = next_state(state); |
| if (start < end && state && state->start == start && |
| !need_resched()) |
| goto hit_next; |
| goto search_again; |
| } |
| |
| /* |
| * | ---- desired range ---- | |
| * | state | |
| * or |
| * | ------------- state -------------- | |
| * |
| * We need to split the extent we found, and may flip bits on |
| * second half. |
| * |
| * If the extent we found extends past our |
| * range, we just split and search again. It'll get split |
| * again the next time though. |
| * |
| * If the extent we found is inside our range, we set the |
| * desired bit on it. |
| */ |
| if (state->start < start) { |
| if (state->state & exclusive_bits) { |
| *failed_start = start; |
| err = -EEXIST; |
| goto out; |
| } |
| |
| prealloc = alloc_extent_state_atomic(prealloc); |
| BUG_ON(!prealloc); |
| err = split_state(tree, state, prealloc, start); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| |
| prealloc = NULL; |
| if (err) |
| goto out; |
| if (state->end <= end) { |
| set_state_bits(tree, state, &bits, changeset); |
| cache_state(state, cached_state); |
| merge_state(tree, state); |
| if (last_end == (u64)-1) |
| goto out; |
| start = last_end + 1; |
| state = next_state(state); |
| if (start < end && state && state->start == start && |
| !need_resched()) |
| goto hit_next; |
| } |
| goto search_again; |
| } |
| /* |
| * | ---- desired range ---- | |
| * | state | or | state | |
| * |
| * There's a hole, we need to insert something in it and |
| * ignore the extent we found. |
| */ |
| if (state->start > start) { |
| u64 this_end; |
| if (end < last_start) |
| this_end = end; |
| else |
| this_end = last_start - 1; |
| |
| prealloc = alloc_extent_state_atomic(prealloc); |
| BUG_ON(!prealloc); |
| |
| /* |
| * Avoid to free 'prealloc' if it can be merged with |
| * the later extent. |
| */ |
| err = insert_state(tree, prealloc, start, this_end, |
| NULL, NULL, &bits, changeset); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| |
| cache_state(prealloc, cached_state); |
| prealloc = NULL; |
| start = this_end + 1; |
| goto search_again; |
| } |
| /* |
| * | ---- desired range ---- | |
| * | state | |
| * We need to split the extent, and set the bit |
| * on the first half |
| */ |
| if (state->start <= end && state->end > end) { |
| if (state->state & exclusive_bits) { |
| *failed_start = start; |
| err = -EEXIST; |
| goto out; |
| } |
| |
| prealloc = alloc_extent_state_atomic(prealloc); |
| BUG_ON(!prealloc); |
| err = split_state(tree, state, prealloc, end + 1); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| |
| set_state_bits(tree, prealloc, &bits, changeset); |
| cache_state(prealloc, cached_state); |
| merge_state(tree, prealloc); |
| prealloc = NULL; |
| goto out; |
| } |
| |
| search_again: |
| if (start > end) |
| goto out; |
| spin_unlock(&tree->lock); |
| if (gfpflags_allow_blocking(mask)) |
| cond_resched(); |
| goto again; |
| |
| out: |
| spin_unlock(&tree->lock); |
| if (prealloc) |
| free_extent_state(prealloc); |
| |
| return err; |
| |
| } |
| |
| int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, u64 * failed_start, |
| struct extent_state **cached_state, gfp_t mask) |
| { |
| return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
| cached_state, mask, NULL); |
| } |
| |
| |
| /** |
| * convert_extent_bit - convert all bits in a given range from one bit to |
| * another |
| * @tree: the io tree to search |
| * @start: the start offset in bytes |
| * @end: the end offset in bytes (inclusive) |
| * @bits: the bits to set in this range |
| * @clear_bits: the bits to clear in this range |
| * @cached_state: state that we're going to cache |
| * |
| * This will go through and set bits for the given range. If any states exist |
| * already in this range they are set with the given bit and cleared of the |
| * clear_bits. This is only meant to be used by things that are mergeable, ie |
| * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| * boundary bits like LOCK. |
| * |
| * All allocations are done with GFP_NOFS. |
| */ |
| int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, unsigned clear_bits, |
| struct extent_state **cached_state) |
| { |
| struct extent_state *state; |
| struct extent_state *prealloc = NULL; |
| struct rb_node *node; |
| struct rb_node **p; |
| struct rb_node *parent; |
| int err = 0; |
| u64 last_start; |
| u64 last_end; |
| bool first_iteration = true; |
| |
| btrfs_debug_check_extent_io_range(tree, start, end); |
| |
| again: |
| if (!prealloc) { |
| /* |
| * Best effort, don't worry if extent state allocation fails |
| * here for the first iteration. We might have a cached state |
| * that matches exactly the target range, in which case no |
| * extent state allocations are needed. We'll only know this |
| * after locking the tree. |
| */ |
| prealloc = alloc_extent_state(GFP_NOFS); |
| if (!prealloc && !first_iteration) |
| return -ENOMEM; |
| } |
| |
| spin_lock(&tree->lock); |
| if (cached_state && *cached_state) { |
| state = *cached_state; |
| if (state->start <= start && state->end > start && |
| extent_state_in_tree(state)) { |
| node = &state->rb_node; |
| goto hit_next; |
| } |
| } |
| |
| /* |
| * this search will find all the extents that end after |
| * our range starts. |
| */ |
| node = tree_search_for_insert(tree, start, &p, &parent); |
| if (!node) { |
| prealloc = alloc_extent_state_atomic(prealloc); |
| if (!prealloc) { |
| err = -ENOMEM; |
| goto out; |
| } |
| err = insert_state(tree, prealloc, start, end, |
| &p, &parent, &bits, NULL); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| cache_state(prealloc, cached_state); |
| prealloc = NULL; |
| goto out; |
| } |
| state = rb_entry(node, struct extent_state, rb_node); |
| hit_next: |
| last_start = state->start; |
| last_end = state->end; |
| |
| /* |
| * | ---- desired range ---- | |
| * | state | |
| * |
| * Just lock what we found and keep going |
| */ |
| if (state->start == start && state->end <= end) { |
| set_state_bits(tree, state, &bits, NULL); |
| cache_state(state, cached_state); |
| state = clear_state_bit(tree, state, &clear_bits, 0, NULL); |
| if (last_end == (u64)-1) |
| goto out; |
| start = last_end + 1; |
| if (start < end && state && state->start == start && |
| !need_resched()) |
| goto hit_next; |
| goto search_again; |
| } |
| |
| /* |
| * | ---- desired range ---- | |
| * | state | |
| * or |
| * | ------------- state -------------- | |
| * |
| * We need to split the extent we found, and may flip bits on |
| * second half. |
| * |
| * If the extent we found extends past our |
| * range, we just split and search again. It'll get split |
| * again the next time though. |
| * |
| * If the extent we found is inside our range, we set the |
| * desired bit on it. |
| */ |
| if (state->start < start) { |
| prealloc = alloc_extent_state_atomic(prealloc); |
| if (!prealloc) { |
| err = -ENOMEM; |
| goto out; |
| } |
| err = split_state(tree, state, prealloc, start); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| prealloc = NULL; |
| if (err) |
| goto out; |
| if (state->end <= end) { |
| set_state_bits(tree, state, &bits, NULL); |
| cache_state(state, cached_state); |
| state = clear_state_bit(tree, state, &clear_bits, 0, |
| NULL); |
| if (last_end == (u64)-1) |
| goto out; |
| start = last_end + 1; |
| if (start < end && state && state->start == start && |
| !need_resched()) |
| goto hit_next; |
| } |
| goto search_again; |
| } |
| /* |
| * | ---- desired range ---- | |
| * | state | or | state | |
| * |
| * There's a hole, we need to insert something in it and |
| * ignore the extent we found. |
| */ |
| if (state->start > start) { |
| u64 this_end; |
| if (end < last_start) |
| this_end = end; |
| else |
| this_end = last_start - 1; |
| |
| prealloc = alloc_extent_state_atomic(prealloc); |
| if (!prealloc) { |
| err = -ENOMEM; |
| goto out; |
| } |
| |
| /* |
| * Avoid to free 'prealloc' if it can be merged with |
| * the later extent. |
| */ |
| err = insert_state(tree, prealloc, start, this_end, |
| NULL, NULL, &bits, NULL); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| cache_state(prealloc, cached_state); |
| prealloc = NULL; |
| start = this_end + 1; |
| goto search_again; |
| } |
| /* |
| * | ---- desired range ---- | |
| * | state | |
| * We need to split the extent, and set the bit |
| * on the first half |
| */ |
| if (state->start <= end && state->end > end) { |
| prealloc = alloc_extent_state_atomic(prealloc); |
| if (!prealloc) { |
| err = -ENOMEM; |
| goto out; |
| } |
| |
| err = split_state(tree, state, prealloc, end + 1); |
| if (err) |
| extent_io_tree_panic(tree, err); |
| |
| set_state_bits(tree, prealloc, &bits, NULL); |
| cache_state(prealloc, cached_state); |
| clear_state_bit(tree, prealloc, &clear_bits, 0, NULL); |
| prealloc = NULL; |
| goto out; |
| } |
| |
| search_again: |
| if (start > end) |
| goto out; |
| spin_unlock(&tree->lock); |
| cond_resched(); |
| first_iteration = false; |
| goto again; |
| |
| out: |
| spin_unlock(&tree->lock); |
| if (prealloc) |
| free_extent_state(prealloc); |
| |
| return err; |
| } |
| |
| /* wrappers around set/clear extent bit */ |
| int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, struct extent_changeset *changeset) |
| { |
| /* |
| * We don't support EXTENT_LOCKED yet, as current changeset will |
| * record any bits changed, so for EXTENT_LOCKED case, it will |
| * either fail with -EEXIST or changeset will record the whole |
| * range. |
| */ |
| BUG_ON(bits & EXTENT_LOCKED); |
| |
| return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS, |
| changeset); |
| } |
| |
| int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, int wake, int delete, |
| struct extent_state **cached, gfp_t mask) |
| { |
| return __clear_extent_bit(tree, start, end, bits, wake, delete, |
| cached, mask, NULL); |
| } |
| |
| int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, struct extent_changeset *changeset) |
| { |
| /* |
| * Don't support EXTENT_LOCKED case, same reason as |
| * set_record_extent_bits(). |
| */ |
| BUG_ON(bits & EXTENT_LOCKED); |
| |
| return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS, |
| changeset); |
| } |
| |
| /* |
| * either insert or lock state struct between start and end use mask to tell |
| * us if waiting is desired. |
| */ |
| int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
| struct extent_state **cached_state) |
| { |
| int err; |
| u64 failed_start; |
| |
| while (1) { |
| err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, |
| EXTENT_LOCKED, &failed_start, |
| cached_state, GFP_NOFS, NULL); |
| if (err == -EEXIST) { |
| wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| start = failed_start; |
| } else |
| break; |
| WARN_ON(start > end); |
| } |
| return err; |
| } |
| |
| int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
| { |
| int err; |
| u64 failed_start; |
| |
| err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
| &failed_start, NULL, GFP_NOFS, NULL); |
| if (err == -EEXIST) { |
| if (failed_start > start) |
| clear_extent_bit(tree, start, failed_start - 1, |
| EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
| return 0; |
| } |
| return 1; |
| } |
| |
| void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
| { |
| unsigned long index = start >> PAGE_SHIFT; |
| unsigned long end_index = end >> PAGE_SHIFT; |
| struct page *page; |
| |
| while (index <= end_index) { |
| page = find_get_page(inode->i_mapping, index); |
| BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| clear_page_dirty_for_io(page); |
| put_page(page); |
| index++; |
| } |
| } |
| |
| void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) |
| { |
| unsigned long index = start >> PAGE_SHIFT; |
| unsigned long end_index = end >> PAGE_SHIFT; |
| struct page *page; |
| |
| while (index <= end_index) { |
| page = find_get_page(inode->i_mapping, index); |
| BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| __set_page_dirty_nobuffers(page); |
| account_page_redirty(page); |
| put_page(page); |
| index++; |
| } |
| } |
| |
| /* |
| * helper function to set both pages and extents in the tree writeback |
| */ |
| static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
| { |
| tree->ops->set_range_writeback(tree->private_data, start, end); |
| } |
| |
| /* find the first state struct with 'bits' set after 'start', and |
| * return it. tree->lock must be held. NULL will returned if |
| * nothing was found after 'start' |
| */ |
| static struct extent_state * |
| find_first_extent_bit_state(struct extent_io_tree *tree, |
| u64 start, unsigned bits) |
| { |
| struct rb_node *node; |
| struct extent_state *state; |
| |
| /* |
| * this search will find all the extents that end after |
| * our range starts. |
| */ |
| node = tree_search(tree, start); |
| if (!node) |
| goto out; |
| |
| while (1) { |
| state = rb_entry(node, struct extent_state, rb_node); |
| if (state->end >= start && (state->state & bits)) |
| return state; |
| |
| node = rb_next(node); |
| if (!node) |
| break; |
| } |
| out: |
| return NULL; |
| } |
| |
| /* |
| * find the first offset in the io tree with 'bits' set. zero is |
| * returned if we find something, and *start_ret and *end_ret are |
| * set to reflect the state struct that was found. |
| * |
| * If nothing was found, 1 is returned. If found something, return 0. |
| */ |
| int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
| u64 *start_ret, u64 *end_ret, unsigned bits, |
| struct extent_state **cached_state) |
| { |
| struct extent_state *state; |
| struct rb_node *n; |
| int ret = 1; |
| |
| spin_lock(&tree->lock); |
| if (cached_state && *cached_state) { |
| state = *cached_state; |
| if (state->end == start - 1 && extent_state_in_tree(state)) { |
| n = rb_next(&state->rb_node); |
| while (n) { |
| state = rb_entry(n, struct extent_state, |
| rb_node); |
| if (state->state & bits) |
| goto got_it; |
| n = rb_next(n); |
| } |
| free_extent_state(*cached_state); |
| *cached_state = NULL; |
| goto out; |
| } |
| free_extent_state(*cached_state); |
| *cached_state = NULL; |
| } |
| |
| state = find_first_extent_bit_state(tree, start, bits); |
| got_it: |
| if (state) { |
| cache_state_if_flags(state, cached_state, 0); |
| *start_ret = state->start; |
| *end_ret = state->end; |
| ret = 0; |
| } |
| out: |
| spin_unlock(&tree->lock); |
| return ret; |
| } |
| |
| /* |
| * find a contiguous range of bytes in the file marked as delalloc, not |
| * more than 'max_bytes'. start and end are used to return the range, |
| * |
| * 1 is returned if we find something, 0 if nothing was in the tree |
| */ |
| static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
| u64 *start, u64 *end, u64 max_bytes, |
| struct extent_state **cached_state) |
| { |
| struct rb_node *node; |
| struct extent_state *state; |
| u64 cur_start = *start; |
| u64 found = 0; |
| u64 total_bytes = 0; |
| |
| spin_lock(&tree->lock); |
| |
| /* |
| * this search will find all the extents that end after |
| * our range starts. |
| */ |
| node = tree_search(tree, cur_start); |
| if (!node) { |
| if (!found) |
| *end = (u64)-1; |
| goto out; |
| } |
| |
| while (1) { |
| state = rb_entry(node, struct extent_state, rb_node); |
| if (found && (state->start != cur_start || |
| (state->state & EXTENT_BOUNDARY))) { |
| goto out; |
| } |
| if (!(state->state & EXTENT_DELALLOC)) { |
| if (!found) |
| *end = state->end; |
| goto out; |
| } |
| if (!found) { |
| *start = state->start; |
| *cached_state = state; |
| refcount_inc(&state->refs); |
| } |
| found++; |
| *end = state->end; |
| cur_start = state->end + 1; |
| node = rb_next(node); |
| total_bytes += state->end - state->start + 1; |
| if (total_bytes >= max_bytes) |
| break; |
| if (!node) |
| break; |
| } |
| out: |
| spin_unlock(&tree->lock); |
| return found; |
| } |
| |
| static int __process_pages_contig(struct address_space *mapping, |
| struct page *locked_page, |
| pgoff_t start_index, pgoff_t end_index, |
| unsigned long page_ops, pgoff_t *index_ret); |
| |
| static noinline void __unlock_for_delalloc(struct inode *inode, |
| struct page *locked_page, |
| u64 start, u64 end) |
| { |
| unsigned long index = start >> PAGE_SHIFT; |
| unsigned long end_index = end >> PAGE_SHIFT; |
| |
| ASSERT(locked_page); |
| if (index == locked_page->index && end_index == index) |
| return; |
| |
| __process_pages_contig(inode->i_mapping, locked_page, index, end_index, |
| PAGE_UNLOCK, NULL); |
| } |
| |
| static noinline int lock_delalloc_pages(struct inode *inode, |
| struct page *locked_page, |
| u64 delalloc_start, |
| u64 delalloc_end) |
| { |
| unsigned long index = delalloc_start >> PAGE_SHIFT; |
| unsigned long index_ret = index; |
| unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
| int ret; |
| |
| ASSERT(locked_page); |
| if (index == locked_page->index && index == end_index) |
| return 0; |
| |
| ret = __process_pages_contig(inode->i_mapping, locked_page, index, |
| end_index, PAGE_LOCK, &index_ret); |
| if (ret == -EAGAIN) |
| __unlock_for_delalloc(inode, locked_page, delalloc_start, |
| (u64)index_ret << PAGE_SHIFT); |
| return ret; |
| } |
| |
| /* |
| * find a contiguous range of bytes in the file marked as delalloc, not |
| * more than 'max_bytes'. start and end are used to return the range, |
| * |
| * 1 is returned if we find something, 0 if nothing was in the tree |
| */ |
| STATIC u64 find_lock_delalloc_range(struct inode *inode, |
| struct extent_io_tree *tree, |
| struct page *locked_page, u64 *start, |
| u64 *end, u64 max_bytes) |
| { |
| u64 delalloc_start; |
| u64 delalloc_end; |
| u64 found; |
| struct extent_state *cached_state = NULL; |
| int ret; |
| int loops = 0; |
| |
| again: |
| /* step one, find a bunch of delalloc bytes starting at start */ |
| delalloc_start = *start; |
| delalloc_end = 0; |
| found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
| max_bytes, &cached_state); |
| if (!found || delalloc_end <= *start) { |
| *start = delalloc_start; |
| *end = delalloc_end; |
| free_extent_state(cached_state); |
| return 0; |
| } |
| |
| /* |
| * start comes from the offset of locked_page. We have to lock |
| * pages in order, so we can't process delalloc bytes before |
| * locked_page |
| */ |
| if (delalloc_start < *start) |
| delalloc_start = *start; |
| |
| /* |
| * make sure to limit the number of pages we try to lock down |
| */ |
| if (delalloc_end + 1 - delalloc_start > max_bytes) |
| delalloc_end = delalloc_start + max_bytes - 1; |
| |
| /* step two, lock all the pages after the page that has start */ |
| ret = lock_delalloc_pages(inode, locked_page, |
| delalloc_start, delalloc_end); |
| if (ret == -EAGAIN) { |
| /* some of the pages are gone, lets avoid looping by |
| * shortening the size of the delalloc range we're searching |
| */ |
| free_extent_state(cached_state); |
| cached_state = NULL; |
| if (!loops) { |
| max_bytes = PAGE_SIZE; |
| loops = 1; |
| goto again; |
| } else { |
| found = 0; |
| goto out_failed; |
| } |
| } |
| BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
| |
| /* step three, lock the state bits for the whole range */ |
| lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
| |
| /* then test to make sure it is all still delalloc */ |
| ret = test_range_bit(tree, delalloc_start, delalloc_end, |
| EXTENT_DELALLOC, 1, cached_state); |
| if (!ret) { |
| unlock_extent_cached(tree, delalloc_start, delalloc_end, |
| &cached_state, GFP_NOFS); |
| __unlock_for_delalloc(inode, locked_page, |
| delalloc_start, delalloc_end); |
| cond_resched(); |
| goto again; |
| } |
| free_extent_state(cached_state); |
| *start = delalloc_start; |
| *end = delalloc_end; |
| out_failed: |
| return found; |
| } |
| |
| static int __process_pages_contig(struct address_space *mapping, |
| struct page *locked_page, |
| pgoff_t start_index, pgoff_t end_index, |
| unsigned long page_ops, pgoff_t *index_ret) |
| { |
| unsigned long nr_pages = end_index - start_index + 1; |
| unsigned long pages_locked = 0; |
| pgoff_t index = start_index; |
| struct page *pages[16]; |
| unsigned ret; |
| int err = 0; |
| int i; |
| |
| if (page_ops & PAGE_LOCK) { |
| ASSERT(page_ops == PAGE_LOCK); |
| ASSERT(index_ret && *index_ret == start_index); |
| } |
| |
| if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
| mapping_set_error(mapping, -EIO); |
| |
| while (nr_pages > 0) { |
| ret = find_get_pages_contig(mapping, index, |
| min_t(unsigned long, |
| nr_pages, ARRAY_SIZE(pages)), pages); |
| if (ret == 0) { |
| /* |
| * Only if we're going to lock these pages, |
| * can we find nothing at @index. |
| */ |
| ASSERT(page_ops & PAGE_LOCK); |
| err = -EAGAIN; |
| goto out; |
| } |
| |
| for (i = 0; i < ret; i++) { |
| if (page_ops & PAGE_SET_PRIVATE2) |
| SetPagePrivate2(pages[i]); |
| |
| if (pages[i] == locked_page) { |
| put_page(pages[i]); |
| pages_locked++; |
| continue; |
| } |
| if (page_ops & PAGE_CLEAR_DIRTY) |
| clear_page_dirty_for_io(pages[i]); |
| if (page_ops & PAGE_SET_WRITEBACK) |
| set_page_writeback(pages[i]); |
| if (page_ops & PAGE_SET_ERROR) |
| SetPageError(pages[i]); |
| if (page_ops & PAGE_END_WRITEBACK) |
| end_page_writeback(pages[i]); |
| if (page_ops & PAGE_UNLOCK) |
| unlock_page(pages[i]); |
| if (page_ops & PAGE_LOCK) { |
| lock_page(pages[i]); |
| if (!PageDirty(pages[i]) || |
| pages[i]->mapping != mapping) { |
| unlock_page(pages[i]); |
| put_page(pages[i]); |
| err = -EAGAIN; |
| goto out; |
| } |
| } |
| put_page(pages[i]); |
| pages_locked++; |
| } |
| nr_pages -= ret; |
| index += ret; |
| cond_resched(); |
| } |
| out: |
| if (err && index_ret) |
| *index_ret = start_index + pages_locked - 1; |
| return err; |
| } |
| |
| void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
| u64 delalloc_end, struct page *locked_page, |
| unsigned clear_bits, |
| unsigned long page_ops) |
| { |
| clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0, |
| NULL, GFP_NOFS); |
| |
| __process_pages_contig(inode->i_mapping, locked_page, |
| start >> PAGE_SHIFT, end >> PAGE_SHIFT, |
| page_ops, NULL); |
| } |
| |
| /* |
| * count the number of bytes in the tree that have a given bit(s) |
| * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| * cached. The total number found is returned. |
| */ |
| u64 count_range_bits(struct extent_io_tree *tree, |
| u64 *start, u64 search_end, u64 max_bytes, |
| unsigned bits, int contig) |
| { |
| struct rb_node *node; |
| struct extent_state *state; |
| u64 cur_start = *start; |
| u64 total_bytes = 0; |
| u64 last = 0; |
| int found = 0; |
| |
| if (WARN_ON(search_end <= cur_start)) |
| return 0; |
| |
| spin_lock(&tree->lock); |
| if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| total_bytes = tree->dirty_bytes; |
| goto out; |
| } |
| /* |
| * this search will find all the extents that end after |
| * our range starts. |
| */ |
| node = tree_search(tree, cur_start); |
| if (!node) |
| goto out; |
| |
| while (1) { |
| state = rb_entry(node, struct extent_state, rb_node); |
| if (state->start > search_end) |
| break; |
| if (contig && found && state->start > last + 1) |
| break; |
| if (state->end >= cur_start && (state->state & bits) == bits) { |
| total_bytes += min(search_end, state->end) + 1 - |
| max(cur_start, state->start); |
| if (total_bytes >= max_bytes) |
| break; |
| if (!found) { |
| *start = max(cur_start, state->start); |
| found = 1; |
| } |
| last = state->end; |
| } else if (contig && found) { |
| break; |
| } |
| node = rb_next(node); |
| if (!node) |
| break; |
| } |
| out: |
| spin_unlock(&tree->lock); |
| return total_bytes; |
| } |
| |
| /* |
| * set the private field for a given byte offset in the tree. If there isn't |
| * an extent_state there already, this does nothing. |
| */ |
| static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start, |
| struct io_failure_record *failrec) |
| { |
| struct rb_node *node; |
| struct extent_state *state; |
| int ret = 0; |
| |
| spin_lock(&tree->lock); |
| /* |
| * this search will find all the extents that end after |
| * our range starts. |
| */ |
| node = tree_search(tree, start); |
| if (!node) { |
| ret = -ENOENT; |
| goto out; |
| } |
| state = rb_entry(node, struct extent_state, rb_node); |
| if (state->start != start) { |
| ret = -ENOENT; |
| goto out; |
| } |
| state->failrec = failrec; |
| out: |
| spin_unlock(&tree->lock); |
| return ret; |
| } |
| |
| static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start, |
| struct io_failure_record **failrec) |
| { |
| struct rb_node *node; |
| struct extent_state *state; |
| int ret = 0; |
| |
| spin_lock(&tree->lock); |
| /* |
| * this search will find all the extents that end after |
| * our range starts. |
| */ |
| node = tree_search(tree, start); |
| if (!node) { |
| ret = -ENOENT; |
| goto out; |
| } |
| state = rb_entry(node, struct extent_state, rb_node); |
| if (state->start != start) { |
| ret = -ENOENT; |
| goto out; |
| } |
| *failrec = state->failrec; |
| out: |
| spin_unlock(&tree->lock); |
| return ret; |
| } |
| |
| /* |
| * searches a range in the state tree for a given mask. |
| * If 'filled' == 1, this returns 1 only if every extent in the tree |
| * has the bits set. Otherwise, 1 is returned if any bit in the |
| * range is found set. |
| */ |
| int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| unsigned bits, int filled, struct extent_state *cached) |
| { |
| struct extent_state *state = NULL; |
| struct rb_node *node; |
| int bitset = 0; |
| |
| spin_lock(&tree->lock); |
| if (cached && extent_state_in_tree(cached) && cached->start <= start && |
| cached->end > start) |
| node = &cached->rb_node; |
| else |
| node = tree_search(tree, start); |
| while (node && start <= end) { |
| state = rb_entry(node, struct extent_state, rb_node); |
| |
| if (filled && state->start > start) { |
| bitset = 0; |
| break; |
| } |
| |
| if (state->start > end) |
| break; |
| |
| if (state->state & bits) { |
| bitset = 1; |
| if (!filled) |
| break; |
| } else if (filled) { |
| bitset = 0; |
| break; |
| } |
| |
| if (state->end == (u64)-1) |
| break; |
| |
| start = state->end + 1; |
| if (start > end) |
| break; |
| node = rb_next(node); |
| if (!node) { |
| if (filled) |
| bitset = 0; |
| break; |
| } |
| } |
| spin_unlock(&tree->lock); |
| return bitset; |
| } |
| |
| /* |
| * helper function to set a given page up to date if all the |
| * extents in the tree for that page are up to date |
| */ |
| static void check_page_uptodate(struct extent_io_tree *tree, struct page *page) |
| { |
| u64 start = page_offset(page); |
| u64 end = start + PAGE_SIZE - 1; |
| if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
| SetPageUptodate(page); |
| } |
| |
| int free_io_failure(struct extent_io_tree *failure_tree, |
| struct extent_io_tree *io_tree, |
| struct io_failure_record *rec) |
| { |
| int ret; |
| int err = 0; |
| |
| set_state_failrec(failure_tree, rec->start, NULL); |
| ret = clear_extent_bits(failure_tree, rec->start, |
| rec->start + rec->len - 1, |
| EXTENT_LOCKED | EXTENT_DIRTY); |
| if (ret) |
| err = ret; |
| |
| ret = clear_extent_bits(io_tree, rec->start, |
| rec->start + rec->len - 1, |
| EXTENT_DAMAGED); |
| if (ret && !err) |
| err = ret; |
| |
| kfree(rec); |
| return err; |
| } |
| |
| /* |
| * this bypasses the standard btrfs submit functions deliberately, as |
| * the standard behavior is to write all copies in a raid setup. here we only |
| * want to write the one bad copy. so we do the mapping for ourselves and issue |
| * submit_bio directly. |
| * to avoid any synchronization issues, wait for the data after writing, which |
| * actually prevents the read that triggered the error from finishing. |
| * currently, there can be no more than two copies of every data bit. thus, |
| * exactly one rewrite is required. |
| */ |
| int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, |
| u64 length, u64 logical, struct page *page, |
| unsigned int pg_offset, int mirror_num) |
| { |
| struct bio *bio; |
| struct btrfs_device *dev; |
| u64 map_length = 0; |
| u64 sector; |
| struct btrfs_bio *bbio = NULL; |
| int ret; |
| |
| ASSERT(!(fs_info->sb->s_flags & MS_RDONLY)); |
| BUG_ON(!mirror_num); |
| |
| bio = btrfs_io_bio_alloc(1); |
| bio->bi_iter.bi_size = 0; |
| map_length = length; |
| |
| /* |
| * Avoid races with device replace and make sure our bbio has devices |
| * associated to its stripes that don't go away while we are doing the |
| * read repair operation. |
| */ |
| btrfs_bio_counter_inc_blocked(fs_info); |
| if (btrfs_is_parity_mirror(fs_info, logical, length)) { |
| /* |
| * Note that we don't use BTRFS_MAP_WRITE because it's supposed |
| * to update all raid stripes, but here we just want to correct |
| * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad |
| * stripe's dev and sector. |
| */ |
| ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, |
| &map_length, &bbio, 0); |
| if (ret) { |
| btrfs_bio_counter_dec(fs_info); |
| bio_put(bio); |
| return -EIO; |
| } |
| ASSERT(bbio->mirror_num == 1); |
| } else { |
| ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, |
| &map_length, &bbio, mirror_num); |
| if (ret) { |
| btrfs_bio_counter_dec(fs_info); |
| bio_put(bio); |
| return -EIO; |
| } |
| BUG_ON(mirror_num != bbio->mirror_num); |
| } |
| |
| sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9; |
| bio->bi_iter.bi_sector = sector; |
| dev = bbio->stripes[bbio->mirror_num - 1].dev; |
| btrfs_put_bbio(bbio); |
| if (!dev || !dev->bdev || !dev->writeable) { |
| btrfs_bio_counter_dec(fs_info); |
| bio_put(bio); |
| return -EIO; |
| } |
| bio_set_dev(bio, dev->bdev); |
| bio->bi_opf = REQ_OP_WRITE | REQ_SYNC; |
| bio_add_page(bio, page, length, pg_offset); |
| |
| if (btrfsic_submit_bio_wait(bio)) { |
| /* try to remap that extent elsewhere? */ |
| btrfs_bio_counter_dec(fs_info); |
| bio_put(bio); |
| btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
| return -EIO; |
| } |
| |
| btrfs_info_rl_in_rcu(fs_info, |
| "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
| ino, start, |
| rcu_str_deref(dev->name), sector); |
| btrfs_bio_counter_dec(fs_info); |
| bio_put(bio); |
| return 0; |
| } |
| |
| int repair_eb_io_failure(struct btrfs_fs_info *fs_info, |
| struct extent_buffer *eb, int mirror_num) |
| { |
| u64 start = eb->start; |
| unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); |
| int ret = 0; |
| |
| if (sb_rdonly(fs_info->sb)) |
| return -EROFS; |
| |
| for (i = 0; i < num_pages; i++) { |
| struct page *p = eb->pages[i]; |
| |
| ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p, |
| start - page_offset(p), mirror_num); |
| if (ret) |
| break; |
| start += PAGE_SIZE; |
| } |
| |
| return ret; |
| } |
| |
| /* |
| * each time an IO finishes, we do a fast check in the IO failure tree |
| * to see if we need to process or clean up an io_failure_record |
| */ |
| int clean_io_failure(struct btrfs_fs_info *fs_info, |
| struct extent_io_tree *failure_tree, |
| struct extent_io_tree *io_tree, u64 start, |
| struct page *page, u64 ino, unsigned int pg_offset) |
| { |
| u64 private; |
| struct io_failure_record *failrec; |
| struct extent_state *state; |
| int num_copies; |
| int ret; |
| |
| private = 0; |
| ret = count_range_bits(failure_tree, &private, (u64)-1, 1, |
| EXTENT_DIRTY, 0); |
| if (!ret) |
| return 0; |
| |
| ret = get_state_failrec(failure_tree, start, &failrec); |
| if (ret) |
| return 0; |
| |
| BUG_ON(!failrec->this_mirror); |
| |
| if (failrec->in_validation) { |
| /* there was no real error, just free the record */ |
| btrfs_debug(fs_info, |
| "clean_io_failure: freeing dummy error at %llu", |
| failrec->start); |
| goto out; |
| } |
| if (sb_rdonly(fs_info->sb)) |
| goto out; |
| |
| spin_lock(&io_tree->lock); |
| state = find_first_extent_bit_state(io_tree, |
| failrec->start, |
| EXTENT_LOCKED); |
| spin_unlock(&io_tree->lock); |
| |
| if (state && state->start <= failrec->start && |
| state->end >= failrec->start + failrec->len - 1) { |
| num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| failrec->len); |
| if (num_copies > 1) { |
| repair_io_failure(fs_info, ino, start, failrec->len, |
| failrec->logical, page, pg_offset, |
| failrec->failed_mirror); |
| } |
| } |
| |
| out: |
| free_io_failure(failure_tree, io_tree, failrec); |
| |
| return 0; |
| } |
| |
| /* |
| * Can be called when |
| * - hold extent lock |
| * - under ordered extent |
| * - the inode is freeing |
| */ |
| void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end) |
| { |
| struct extent_io_tree *failure_tree = &inode->io_failure_tree; |
| struct io_failure_record *failrec; |
| struct extent_state *state, *next; |
| |
| if (RB_EMPTY_ROOT(&failure_tree->state)) |
| return; |
| |
| spin_lock(&failure_tree->lock); |
| state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| while (state) { |
| if (state->start > end) |
| break; |
| |
| ASSERT(state->end <= end); |
| |
| next = next_state(state); |
| |
| failrec = state->failrec; |
| free_extent_state(state); |
| kfree(failrec); |
| |
| state = next; |
| } |
| spin_unlock(&failure_tree->lock); |
| } |
| |
| int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end, |
| struct io_failure_record **failrec_ret) |
| { |
| struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| struct io_failure_record *failrec; |
| struct extent_map *em; |
| struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| int ret; |
| u64 logical; |
| |
| ret = get_state_failrec(failure_tree, start, &failrec); |
| if (ret) { |
| failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| if (!failrec) |
| return -ENOMEM; |
| |
| failrec->start = start; |
| failrec->len = end - start + 1; |
| failrec->this_mirror = 0; |
| failrec->bio_flags = 0; |
| failrec->in_validation = 0; |
| |
| read_lock(&em_tree->lock); |
| em = lookup_extent_mapping(em_tree, start, failrec->len); |
| if (!em) { |
| read_unlock(&em_tree->lock); |
| kfree(failrec); |
| return -EIO; |
| } |
| |
| if (em->start > start || em->start + em->len <= start) { |
| free_extent_map(em); |
| em = NULL; |
| } |
| read_unlock(&em_tree->lock); |
| if (!em) { |
| kfree(failrec); |
| return -EIO; |
| } |
| |
| logical = start - em->start; |
| logical = em->block_start + logical; |
| if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| logical = em->block_start; |
| failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| extent_set_compress_type(&failrec->bio_flags, |
| em->compress_type); |
| } |
| |
| btrfs_debug(fs_info, |
| "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu", |
| logical, start, failrec->len); |
| |
| failrec->logical = logical; |
| free_extent_map(em); |
| |
| /* set the bits in the private failure tree */ |
| ret = set_extent_bits(failure_tree, start, end, |
| EXTENT_LOCKED | EXTENT_DIRTY); |
| if (ret >= 0) |
| ret = set_state_failrec(failure_tree, start, failrec); |
| /* set the bits in the inode's tree */ |
| if (ret >= 0) |
| ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED); |
| if (ret < 0) { |
| kfree(failrec); |
| return ret; |
| } |
| } else { |
| btrfs_debug(fs_info, |
| "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d", |
| failrec->logical, failrec->start, failrec->len, |
| failrec->in_validation); |
| /* |
| * when data can be on disk more than twice, add to failrec here |
| * (e.g. with a list for failed_mirror) to make |
| * clean_io_failure() clean all those errors at once. |
| */ |
| } |
| |
| *failrec_ret = failrec; |
| |
| return 0; |
| } |
| |
| bool btrfs_check_repairable(struct inode *inode, struct bio *failed_bio, |
| struct io_failure_record *failrec, int failed_mirror) |
| { |
| struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| int num_copies; |
| |
| num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
| if (num_copies == 1) { |
| /* |
| * we only have a single copy of the data, so don't bother with |
| * all the retry and error correction code that follows. no |
| * matter what the error is, it is very likely to persist. |
| */ |
| btrfs_debug(fs_info, |
| "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| num_copies, failrec->this_mirror, failed_mirror); |
| return false; |
| } |
| |
| /* |
| * there are two premises: |
| * a) deliver good data to the caller |
| * b) correct the bad sectors on disk |
| */ |
| if (failed_bio->bi_vcnt > 1) { |
| /* |
| * to fulfill b), we need to know the exact failing sectors, as |
| * we don't want to rewrite any more than the failed ones. thus, |
| * we need separate read requests for the failed bio |
| * |
| * if the following BUG_ON triggers, our validation request got |
| * merged. we need separate requests for our algorithm to work. |
| */ |
| BUG_ON(failrec->in_validation); |
| failrec->in_validation = 1; |
| failrec->this_mirror = failed_mirror; |
| } else { |
| /* |
| * we're ready to fulfill a) and b) alongside. get a good copy |
| * of the failed sector and if we succeed, we have setup |
| * everything for repair_io_failure to do the rest for us. |
| */ |
| if (failrec->in_validation) { |
| BUG_ON(failrec->this_mirror != failed_mirror); |
| failrec->in_validation = 0; |
| failrec->this_mirror = 0; |
| } |
| failrec->failed_mirror = failed_mirror; |
| failrec->this_mirror++; |
| if (failrec->this_mirror == failed_mirror) |
| failrec->this_mirror++; |
| } |
| |
| if (failrec->this_mirror > num_copies) { |
| btrfs_debug(fs_info, |
| "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| num_copies, failrec->this_mirror, failed_mirror); |
| return false; |
| } |
| |
| return true; |
| } |
| |
| |
| struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| struct io_failure_record *failrec, |
| struct page *page, int pg_offset, int icsum, |
| bio_end_io_t *endio_func, void *data) |
| { |
| struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| struct bio *bio; |
| struct btrfs_io_bio *btrfs_failed_bio; |
| struct btrfs_io_bio *btrfs_bio; |
| |
| bio = btrfs_io_bio_alloc(1); |
| bio->bi_end_io = endio_func; |
| bio->bi_iter.bi_sector = failrec->logical >> 9; |
| bio_set_dev(bio, fs_info->fs_devices->latest_bdev); |
| bio->bi_iter.bi_size = 0; |
| bio->bi_private = data; |
| |
| btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| if (btrfs_failed_bio->csum) { |
| u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| |
| btrfs_bio = btrfs_io_bio(bio); |
| btrfs_bio->csum = btrfs_bio->csum_inline; |
| icsum *= csum_size; |
| memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
| csum_size); |
| } |
| |
| bio_add_page(bio, page, failrec->len, pg_offset); |
| |
| return bio; |
| } |
| |
| /* |
| * this is a generic handler for readpage errors (default |
| * readpage_io_failed_hook). if other copies exist, read those and write back |
| * good data to the failed position. does not investigate in remapping the |
| * failed extent elsewhere, hoping the device will be smart enough to do this as |
| * needed |
| */ |
| |
| static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| struct page *page, u64 start, u64 end, |
| int failed_mirror) |
| { |
| struct io_failure_record *failrec; |
| struct inode *inode = page->mapping->host; |
| struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| struct bio *bio; |
| int read_mode = 0; |
| blk_status_t status; |
| int ret; |
| |
| BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
| |
| ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| if (ret) |
| return ret; |
| |
| if (!btrfs_check_repairable(inode, failed_bio, failrec, |
| failed_mirror)) { |
| free_io_failure(failure_tree, tree, failrec); |
| return -EIO; |
| } |
| |
| if (failed_bio->bi_vcnt > 1) |
| read_mode |= REQ_FAILFAST_DEV; |
| |
| phy_offset >>= inode->i_sb->s_blocksize_bits; |
| bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| start - page_offset(page), |
| (int)phy_offset, failed_bio->bi_end_io, |
| NULL); |
| bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
| |
| btrfs_debug(btrfs_sb(inode->i_sb), |
| "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d", |
| read_mode, failrec->this_mirror, failrec->in_validation); |
| |
| status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror, |
| failrec->bio_flags, 0); |
| if (status) { |
| free_io_failure(failure_tree, tree, failrec); |
| bio_put(bio); |
| ret = blk_status_to_errno(status); |
| } |
| |
| return ret; |
| } |
| |
| /* lots and lots of room for performance fixes in the end_bio funcs */ |
| |
| void end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
| { |
| int uptodate = (err == 0); |
| struct extent_io_tree *tree; |
| int ret = 0; |
| |
| tree = &BTRFS_I(page->mapping->host)->io_tree; |
| |
| if (tree->ops && tree->ops->writepage_end_io_hook) |
| tree->ops->writepage_end_io_hook(page, start, end, NULL, |
| uptodate); |
| |
| if (!uptodate) { |
| ClearPageUptodate(page); |
| SetPageError(page); |
| ret = err < 0 ? err : -EIO; |
| mapping_set_error(page->mapping, ret); |
| } |
| } |
| |
| /* |
| * after a writepage IO is done, we need to: |
| * clear the uptodate bits on error |
| * clear the writeback bits in the extent tree for this IO |
| * end_page_writeback if the page has no more pending IO |
| * |
| * Scheduling is not allowed, so the extent state tree is expected |
| * to have one and only one object corresponding to this IO. |
| */ |
| static void end_bio_extent_writepage(struct bio *bio) |
| { |
| int error = blk_status_to_errno(bio->bi_status); |
| struct bio_vec *bvec; |
| u64 start; |
| u64 end; |
| int i; |
| |
| ASSERT(!bio_flagged(bio, BIO_CLONED)); |
| bio_for_each_segment_all(bvec, bio, i) { |
| struct page *page = bvec->bv_page; |
| struct inode *inode = page->mapping->host; |
| struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| |
| /* We always issue full-page reads, but if some block |
| * in a page fails to read, blk_update_request() will |
| * advance bv_offset and adjust bv_len to compensate. |
| * Print a warning for nonzero offsets, and an error |
| * if they don't add up to a full page. */ |
| if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
| btrfs_err(fs_info, |
| "partial page write in btrfs with offset %u and length %u", |
| bvec->bv_offset, bvec->bv_len); |
| else |
| btrfs_info(fs_info, |
| "incomplete page write in btrfs with offset %u and length %u", |
| bvec->bv_offset, bvec->bv_len); |
| } |
| |
| start = page_offset(page); |
| end = start + bvec->bv_offset + bvec->bv_len - 1; |
| |
| end_extent_writepage(page, error, start, end); |
| end_page_writeback(page); |
| } |
| |
| bio_put(bio); |
| } |
| |
| static void |
| endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| int uptodate) |
| { |
| struct extent_state *cached = NULL; |
| u64 end = start + len - 1; |
| |
| if (uptodate && tree->track_uptodate) |
| set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
| unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
| } |
| |
| /* |
| * after a readpage IO is done, we need to: |
| * clear the uptodate bits on error |
| * set the uptodate bits if things worked |
| * set the page up to date if all extents in the tree are uptodate |
| * clear the lock bit in the extent tree |
| * unlock the page if there are no other extents locked for it |
| * |
| * Scheduling is not allowed, so the extent state tree is expected |
| * to have one and only one object corresponding to this IO. |
| */ |
| static void end_bio_extent_readpage(struct bio *bio) |
| { |
| struct bio_vec *bvec; |
| int uptodate = !bio->bi_status; |
| struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| struct extent_io_tree *tree, *failure_tree; |
| u64 offset = 0; |
| u64 start; |
| u64 end; |
| u64 len; |
| u64 extent_start = 0; |
| u64 extent_len = 0; |
| int mirror; |
| int ret; |
| int i; |
| |
| ASSERT(!bio_flagged(bio, BIO_CLONED)); |
| bio_for_each_segment_all(bvec, bio, i) { |
| struct page *page = bvec->bv_page; |
| struct inode *inode = page->mapping->host; |
| struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| |
| btrfs_debug(fs_info, |
| "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", |
| (u64)bio->bi_iter.bi_sector, bio->bi_status, |
| io_bio->mirror_num); |
| tree = &BTRFS_I(inode)->io_tree; |
| failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| |
| /* We always issue full-page reads, but if some block |
| * in a page fails to read, blk_update_request() will |
| * advance bv_offset and adjust bv_len to compensate. |
| * Print a warning for nonzero offsets, and an error |
| * if they don't add up to a full page. */ |
| if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
| btrfs_err(fs_info, |
| "partial page read in btrfs with offset %u and length %u", |
| bvec->bv_offset, bvec->bv_len); |
| else |
| btrfs_info(fs_info, |
| "incomplete page read in btrfs with offset %u and length %u", |
| bvec->bv_offset, bvec->bv_len); |
| } |
| |
| start = page_offset(page); |
| end = start + bvec->bv_offset + bvec->bv_len - 1; |
| len = bvec->bv_len; |
| |
| mirror = io_bio->mirror_num; |
| if (likely(uptodate && tree->ops)) { |
| ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| page, start, end, |
| mirror); |
| if (ret) |
| uptodate = 0; |
| else |
| clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| failure_tree, tree, start, |
| page, |
| btrfs_ino(BTRFS_I(inode)), 0); |
| } |
| |
| if (likely(uptodate)) |
| goto readpage_ok; |
| |
| if (tree->ops) { |
| ret = tree->ops->readpage_io_failed_hook(page, mirror); |
| if (ret == -EAGAIN) { |
| /* |
| * Data inode's readpage_io_failed_hook() always |
| * returns -EAGAIN. |
| * |
| * The generic bio_readpage_error handles errors |
| * the following way: If possible, new read |
| * requests are created and submitted and will |
| * end up in end_bio_extent_readpage as well (if |
| * we're lucky, not in the !uptodate case). In |
| * that case it returns 0 and we just go on with |
| * the next page in our bio. If it can't handle |
| * the error it will return -EIO and we remain |
| * responsible for that page. |
| */ |
| ret = bio_readpage_error(bio, offset, page, |
| start, end, mirror); |
| if (ret == 0) { |
| uptodate = !bio->bi_status; |
| offset += len; |
| continue; |
| } |
| } |
| |
| /* |
| * metadata's readpage_io_failed_hook() always returns |
| * -EIO and fixes nothing. -EIO is also returned if |
| * data inode error could not be fixed. |
| */ |
| ASSERT(ret == -EIO); |
| } |
| readpage_ok: |
| if (likely(uptodate)) { |
| loff_t i_size = i_size_read(inode); |
| pgoff_t end_index = i_size >> PAGE_SHIFT; |
| unsigned off; |
| |
| /* Zero out the end if this page straddles i_size */ |
| off = i_size & (PAGE_SIZE-1); |
| if (page->index == end_index && off) |
| zero_user_segment(page, off, PAGE_SIZE); |
| SetPageUptodate(page); |
| } else { |
| ClearPageUptodate(page); |
| SetPageError(page); |
| } |
| unlock_page(page); |
| offset += len; |
| |
| if (unlikely(!uptodate)) { |
| if (extent_len) { |
| endio_readpage_release_extent(tree, |
| extent_start, |
| extent_len, 1); |
| extent_start = 0; |
| extent_len = 0; |
| } |
| endio_readpage_release_extent(tree, start, |
| end - start + 1, 0); |
| } else if (!extent_len) { |
| extent_start = start; |
| extent_len = end + 1 - start; |
| } else if (extent_start + extent_len == start) { |
| extent_len += end + 1 - start; |
| } else { |
| endio_readpage_release_extent(tree, extent_start, |
| extent_len, uptodate); |
| extent_start = start; |
| extent_len = end + 1 - start; |
| } |
| } |
| |
| if (extent_len) |
| endio_readpage_release_extent(tree, extent_start, extent_len, |
| uptodate); |
| if (io_bio->end_io) |
| io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status)); |
| bio_put(bio); |
| } |
| |
| /* |
| * Initialize the members up to but not including 'bio'. Use after allocating a |
| * new bio by bio_alloc_bioset as it does not initialize the bytes outside of |
| * 'bio' because use of __GFP_ZERO is not supported. |
| */ |
| static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio) |
| { |
| memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio)); |
| } |
| |
| /* |
| * The following helpers allocate a bio. As it's backed by a bioset, it'll |
| * never fail. We're returning a bio right now but you can call btrfs_io_bio |
| * for the appropriate container_of magic |
| */ |
| struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte) |
| { |
| struct bio *bio; |
| |
| bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, btrfs_bioset); |
| bio_set_dev(bio, bdev); |
| bio->bi_iter.bi_sector = first_byte >> 9; |
| btrfs_io_bio_init(btrfs_io_bio(bio)); |
| return bio; |
| } |
| |
| struct bio *btrfs_bio_clone(struct bio *bio) |
| { |
| struct btrfs_io_bio *btrfs_bio; |
| struct bio *new; |
| |
| /* Bio allocation backed by a bioset does not fail */ |
| new = bio_clone_fast(bio, GFP_NOFS, btrfs_bioset); |
| btrfs_bio = btrfs_io_bio(new); |
| btrfs_io_bio_init(btrfs_bio); |
| btrfs_bio->iter = bio->bi_iter; |
| return new; |
| } |
| |
| struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs) |
| { |
| struct bio *bio; |
| |
| /* Bio allocation backed by a bioset does not fail */ |
| bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, btrfs_bioset); |
| btrfs_io_bio_init(btrfs_io_bio(bio)); |
| return bio; |
| } |
| |
| struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size) |
| { |
| struct bio *bio; |
| struct btrfs_io_bio *btrfs_bio; |
| |
| /* this will never fail when it's backed by a bioset */ |
| bio = bio_clone_fast(orig, GFP_NOFS, btrfs_bioset); |
| ASSERT(bio); |
| |
| btrfs_bio = btrfs_io_bio(bio); |
| btrfs_io_bio_init(btrfs_bio); |
| |
| bio_trim(bio, offset >> 9, size >> 9); |
| btrfs_bio->iter = bio->bi_iter; |
| return bio; |
| } |
| |
| static int __must_check submit_one_bio(struct bio *bio, int mirror_num, |
| unsigned long bio_flags) |
| { |
| blk_status_t ret = 0; |
| struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| struct page *page = bvec->bv_page; |
| struct extent_io_tree *tree = bio->bi_private; |
| u64 start; |
| |
| start = page_offset(page) + bvec->bv_offset; |
| |
| bio->bi_private = NULL; |
| bio_get(bio); |
| |
| if (tree->ops) |
| ret = tree->ops->submit_bio_hook(tree->private_data, bio, |
| mirror_num, bio_flags, start); |
| else |
| btrfsic_submit_bio(bio); |
| |
| bio_put(bio); |
| return blk_status_to_errno(ret); |
| } |
| |
| static int merge_bio(struct extent_io_tree *tree, struct page *page, |
| unsigned long offset, size_t size, struct bio *bio, |
| unsigned long bio_flags) |
| { |
| int ret = 0; |
| if (tree->ops) |
| ret = tree->ops->merge_bio_hook(page, offset, size, bio, |
| bio_flags); |
| return ret; |
| |
| } |
| |
| /* |
| * @opf: bio REQ_OP_* and REQ_* flags as one value |
| */ |
| static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree, |
| struct writeback_control *wbc, |
| struct page *page, sector_t sector, |
| size_t size, unsigned long offset, |
| struct block_device *bdev, |
| struct bio **bio_ret, |
| bio_end_io_t end_io_func, |
| int mirror_num, |
| unsigned long prev_bio_flags, |
| unsigned long bio_flags, |
| bool force_bio_submit) |
| { |
| int ret = 0; |
| struct bio *bio; |
| int contig = 0; |
| int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; |
| size_t page_size = min_t(size_t, size, PAGE_SIZE); |
| |
| if (bio_ret && *bio_ret) { |
| bio = *bio_ret; |
| if (old_compressed) |
| contig = bio->bi_iter.bi_sector == sector; |
| else |
| contig = bio_end_sector(bio) == sector; |
| |
| if (prev_bio_flags != bio_flags || !contig || |
| force_bio_submit || |
| merge_bio(tree, page, offset, page_size, bio, bio_flags) || |
| bio_add_page(bio, page, page_size, offset) < page_size) { |
| ret = submit_one_bio(bio, mirror_num, prev_bio_flags); |
| if (ret < 0) { |
| *bio_ret = NULL; |
| return ret; |
| } |
| bio = NULL; |
| } else { |
| if (wbc) |
| wbc_account_io(wbc, page, page_size); |
| return 0; |
| } |
| } |
| |
| bio = btrfs_bio_alloc(bdev, (u64)sector << 9); |
| bio_add_page(bio, page, page_size, offset); |
| bio->bi_end_io = end_io_func; |
| bio->bi_private = tree; |
| bio->bi_write_hint = page->mapping->host->i_write_hint; |
| bio->bi_opf = opf; |
| if (wbc) { |
| wbc_init_bio(wbc, bio); |
| wbc_account_io(wbc, page, page_size); |
| } |
| |
| if (bio_ret) |
| *bio_ret = bio; |
| else |
| ret = submit_one_bio(bio, mirror_num, bio_flags); |
| |
| return ret; |
| } |
| |
| static void attach_extent_buffer_page(struct extent_buffer *eb, |
| struct page *page) |
| { |
| if (!PagePrivate(page)) { |
| SetPagePrivate(page); |
| get_page(page); |
| set_page_private(page, (unsigned long)eb); |
| } else { |
| WARN_ON(page->private != (unsigned long)eb); |
| } |
| } |
| |
| void set_page_extent_mapped(struct page *page) |
| { |
| if (!PagePrivate(page)) { |
| SetPagePrivate(page); |
| get_page(page); |
| set_page_private(page, EXTENT_PAGE_PRIVATE); |
| } |
| } |
| |
| static struct extent_map * |
| __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| u64 start, u64 len, get_extent_t *get_extent, |
| struct extent_map **em_cached) |
| { |
| struct extent_map *em; |
| |
| if (em_cached && *em_cached) { |
| em = *em_cached; |
| if (extent_map_in_tree(em) && start >= em->start && |
| start < extent_map_end(em)) { |
| refcount_inc(&em->refs); |
| return em; |
| } |
| |
| free_extent_map(em); |
| *em_cached = NULL; |
| } |
| |
| em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0); |
| if (em_cached && !IS_ERR_OR_NULL(em)) { |
| BUG_ON(*em_cached); |
| refcount_inc(&em->refs); |
| *em_cached = em; |
| } |
| return em; |
| } |
| /* |
| * basic readpage implementation. Locked extent state structs are inserted |
| * into the tree that are removed when the IO is done (by the end_io |
| * handlers) |
| * XXX JDM: This needs looking at to ensure proper page locking |
| * return 0 on success, otherwise return error |
| */ |
| static int __do_readpage(struct extent_io_tree *tree, |
| struct page *page, |
| get_extent_t *get_extent, |
| struct extent_map **em_cached, |
| struct bio **bio, int mirror_num, |
| unsigned long *bio_flags, unsigned int read_flags, |
| u64 *prev_em_start) |
| { |
| struct inode *inode = page->mapping->host; |
| u64 start = page_offset(page); |
| u64 page_end = start + PAGE_SIZE - 1; |
| u64 end; |
| u64 cur = start; |
| u64 extent_offset; |
| u64 last_byte = i_size_read(inode); |
| u64 block_start; |
| u64 cur_end; |
| sector_t sector; |
| struct extent_map *em; |
| struct block_device *bdev; |
| int ret = 0; |
| int nr = 0; |
| size_t pg_offset = 0; |
| size_t iosize; |
| size_t disk_io_size; |
| size_t blocksize = inode->i_sb->s_blocksize; |
| unsigned long this_bio_flag = 0; |
| |
| set_page_extent_mapped(page); |
| |
| end = page_end; |
| if (!PageUptodate(page)) { |
| if (cleancache_get_page(page) == 0) { |
| BUG_ON(blocksize != PAGE_SIZE); |
| unlock_extent(tree, start, end); |
| goto out; |
| } |
| } |
| |
| if (page->index == last_byte >> PAGE_SHIFT) { |
| char *userpage; |
| size_t zero_offset = last_byte & (PAGE_SIZE - 1); |
| |
| if (zero_offset) { |
| iosize = PAGE_SIZE - zero_offset; |
| userpage = kmap_atomic(page); |
| memset(userpage + zero_offset, 0, iosize); |
| flush_dcache_page(page); |
| kunmap_atomic(userpage); |
| } |
| } |
| while (cur <= end) { |
| bool force_bio_submit = false; |
| |
| if (cur >= last_byte) { |
| char *userpage; |
| struct extent_state *cached = NULL; |
| |
| iosize = PAGE_SIZE - pg_offset; |
| userpage = kmap_atomic(page); |
| memset(userpage + pg_offset, 0, iosize); |
| flush_dcache_page(page); |
| kunmap_atomic(userpage); |
| set_extent_uptodate(tree, cur, cur + iosize - 1, |
| &cached, GFP_NOFS); |
| unlock_extent_cached(tree, cur, |
| cur + iosize - 1, |
| &cached, GFP_NOFS); |
| break; |
| } |
| em = __get_extent_map(inode, page, pg_offset, cur, |
| end - cur + 1, get_extent, em_cached); |
| if (IS_ERR_OR_NULL(em)) { |
| SetPageError(page); |
| unlock_extent(tree, cur, end); |
| break; |
| } |
| extent_offset = cur - em->start; |
| BUG_ON(extent_map_end(em) <= cur); |
| BUG_ON(end < cur); |
| |
| if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| this_bio_flag |= EXTENT_BIO_COMPRESSED; |
| extent_set_compress_type(&this_bio_flag, |
| em->compress_type); |
| } |
| |
| iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| cur_end = min(extent_map_end(em) - 1, end); |
| iosize = ALIGN(iosize, blocksize); |
| if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| disk_io_size = em->block_len; |
| sector = em->block_start >> 9; |
| } else { |
| sector = (em->block_start + extent_offset) >> 9; |
| disk_io_size = iosize; |
| } |
| bdev = em->bdev; |
| block_start = em->block_start; |
| if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| block_start = EXTENT_MAP_HOLE; |
| |
| /* |
| * If we have a file range that points to a compressed extent |
| * and it's followed by a consecutive file range that points to |
| * to the same compressed extent (possibly with a different |
| * offset and/or length, so it either points to the whole extent |
| * or only part of it), we must make sure we do not submit a |
| * single bio to populate the pages for the 2 ranges because |
| * this makes the compressed extent read zero out the pages |
| * belonging to the 2nd range. Imagine the following scenario: |
| * |
| * File layout |
| * [0 - 8K] [8K - 24K] |
| * | | |
| * | | |
| * points to extent X, points to extent X, |
| * offset 4K, length of 8K offset 0, length 16K |
| * |
| * [extent X, compressed length = 4K uncompressed length = 16K] |
| * |
| * If the bio to read the compressed extent covers both ranges, |
| * it will decompress extent X into the pages belonging to the |
| * first range and then it will stop, zeroing out the remaining |
| * pages that belong to the other range that points to extent X. |
| * So here we make sure we submit 2 bios, one for the first |
| * range and another one for the third range. Both will target |
| * the same physical extent from disk, but we can't currently |
| * make the compressed bio endio callback populate the pages |
| * for both ranges because each compressed bio is tightly |
| * coupled with a single extent map, and each range can have |
| * an extent map with a different offset value relative to the |
| * uncompressed data of our extent and different lengths. This |
| * is a corner case so we prioritize correctness over |
| * non-optimal behavior (submitting 2 bios for the same extent). |
| */ |
| if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| prev_em_start && *prev_em_start != (u64)-1 && |
| *prev_em_start != em->orig_start) |
| force_bio_submit = true; |
| |
| if (prev_em_start) |
| *prev_em_start = em->orig_start; |
| |
| free_extent_map(em); |
| em = NULL; |
| |
| /* we've found a hole, just zero and go on */ |
| if (block_start == EXTENT_MAP_HOLE) { |
| char *userpage; |
| struct extent_state *cached = NULL; |
| |
| userpage = kmap_atomic(page); |
| memset(userpage + pg_offset, 0, iosize); |
| flush_dcache_page(page); |
| kunmap_atomic(userpage); |
| |
| set_extent_uptodate(tree, cur, cur + iosize - 1, |
| &cached, GFP_NOFS); |
| unlock_extent_cached(tree, cur, |
| cur + iosize - 1, |
| &cached, GFP_NOFS); |
| cur = cur + iosize; |
| pg_offset += iosize; |
| continue; |
| } |
| /* the get_extent function already copied into the page */ |
| if (test_range_bit(tree, cur, cur_end, |
| EXTENT_UPTODATE, 1, NULL)) { |
| check_page_uptodate(tree, page); |
| unlock_extent(tree, cur, cur + iosize - 1); |
| cur = cur + iosize; |
| pg_offset += iosize; |
| continue; |
| } |
| /* we have an inline extent but it didn't get marked up |
| * to date. Error out |
| */ |
| if (block_start == EXTENT_MAP_INLINE) { |
| SetPageError(page); |
| unlock_extent(tree, cur, cur + iosize - 1); |
| cur = cur + iosize; |
| pg_offset += iosize; |
| continue; |
| } |
| |
| ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL, |
| page, sector, disk_io_size, pg_offset, |
| bdev, bio, |
| end_bio_extent_readpage, mirror_num, |
| *bio_flags, |
| this_bio_flag, |
| force_bio_submit); |
| if (!ret) { |
| nr++; |
| *bio_flags = this_bio_flag; |
| } else { |
| SetPageError(page); |
| unlock_extent(tree, cur, cur + iosize - 1); |
| goto out; |
| } |
| cur = cur + iosize; |
| pg_offset += iosize; |
| } |
| out: |
| if (!nr) { |
| if (!PageError(page)) |
| SetPageUptodate(page); |
| unlock_page(page); |
| } |
| return ret; |
| } |
| |
| static inline void __do_contiguous_readpages(struct extent_io_tree *tree, |
| struct page *pages[], int nr_pages, |
| u64 start, u64 end, |
| get_extent_t *get_extent, |
| struct extent_map **em_cached, |
| struct bio **bio, int mirror_num, |
| |