2008-01-24 21:13:08 +00:00
|
|
|
#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/module.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/swap.h>
|
|
|
|
#include <linux/writeback.h>
|
|
|
|
#include <linux/pagevec.h>
|
2011-05-20 19:50:29 +00:00
|
|
|
#include <linux/prefetch.h>
|
2011-05-26 16:01:56 +00:00
|
|
|
#include <linux/cleancache.h>
|
2008-01-24 21:13:08 +00:00
|
|
|
#include "extent_io.h"
|
|
|
|
#include "extent_map.h"
|
2008-08-07 15:19:43 +00:00
|
|
|
#include "compat.h"
|
2008-08-20 12:51:49 +00:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "btrfs_inode.h"
|
2011-07-22 13:41:52 +00:00
|
|
|
#include "volumes.h"
|
2011-11-09 12:44:05 +00:00
|
|
|
#include "check-integrity.h"
|
2012-03-13 13:38:00 +00:00
|
|
|
#include "locking.h"
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
static struct kmem_cache *extent_state_cache;
|
|
|
|
static struct kmem_cache *extent_buffer_cache;
|
|
|
|
|
|
|
|
static LIST_HEAD(buffers);
|
|
|
|
static LIST_HEAD(states);
|
2008-09-08 15:18:08 +00:00
|
|
|
|
2008-11-10 17:34:40 +00:00
|
|
|
#define LEAK_DEBUG 0
|
2009-02-04 14:24:05 +00:00
|
|
|
#if LEAK_DEBUG
|
2009-01-06 02:25:51 +00:00
|
|
|
static DEFINE_SPINLOCK(leak_lock);
|
2008-09-08 15:18:08 +00:00
|
|
|
#endif
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
#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;
|
2008-11-07 03:02:51 +00:00
|
|
|
|
|
|
|
/* tells writepage not to lock the state bits for this range
|
|
|
|
* it still does the unlocking
|
|
|
|
*/
|
2009-04-20 19:50:09 +00:00
|
|
|
unsigned int extent_locked:1;
|
|
|
|
|
|
|
|
/* tells the submit_bio code to use a WRITE_SYNC */
|
|
|
|
unsigned int sync_io:1;
|
2008-01-24 21:13:08 +00:00
|
|
|
};
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
static noinline void flush_write_bio(void *data);
|
2011-10-04 03:22:32 +00:00
|
|
|
static inline struct btrfs_fs_info *
|
|
|
|
tree_fs_info(struct extent_io_tree *tree)
|
|
|
|
{
|
|
|
|
return btrfs_sb(tree->mapping->host->i_sb);
|
|
|
|
}
|
2012-03-13 13:38:00 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
int __init extent_io_init(void)
|
|
|
|
{
|
2009-04-13 13:33:09 +00:00
|
|
|
extent_state_cache = kmem_cache_create("extent_state",
|
|
|
|
sizeof(struct extent_state), 0,
|
|
|
|
SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!extent_state_cache)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-04-13 13:33:09 +00:00
|
|
|
extent_buffer_cache = kmem_cache_create("extent_buffers",
|
|
|
|
sizeof(struct extent_buffer), 0,
|
|
|
|
SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!extent_buffer_cache)
|
|
|
|
goto free_state_cache;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
free_state_cache:
|
|
|
|
kmem_cache_destroy(extent_state_cache);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
void extent_io_exit(void)
|
|
|
|
{
|
|
|
|
struct extent_state *state;
|
2008-03-26 20:24:23 +00:00
|
|
|
struct extent_buffer *eb;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
while (!list_empty(&states)) {
|
2008-03-26 20:24:23 +00:00
|
|
|
state = list_entry(states.next, struct extent_state, leak_list);
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs state leak: start %llu end %llu "
|
|
|
|
"state %lu in tree %p refs %d\n",
|
|
|
|
(unsigned long long)state->start,
|
|
|
|
(unsigned long long)state->end,
|
|
|
|
state->state, state->tree, atomic_read(&state->refs));
|
2008-03-26 20:24:23 +00:00
|
|
|
list_del(&state->leak_list);
|
2008-01-24 21:13:08 +00:00
|
|
|
kmem_cache_free(extent_state_cache, state);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-03-26 20:24:23 +00:00
|
|
|
while (!list_empty(&buffers)) {
|
|
|
|
eb = list_entry(buffers.next, struct extent_buffer, leak_list);
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
|
|
|
|
"refs %d\n", (unsigned long long)eb->start,
|
|
|
|
eb->len, atomic_read(&eb->refs));
|
2008-03-26 20:24:23 +00:00
|
|
|
list_del(&eb->leak_list);
|
|
|
|
kmem_cache_free(extent_buffer_cache, eb);
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
if (extent_state_cache)
|
|
|
|
kmem_cache_destroy(extent_state_cache);
|
|
|
|
if (extent_buffer_cache)
|
|
|
|
kmem_cache_destroy(extent_buffer_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void extent_io_tree_init(struct extent_io_tree *tree,
|
2011-04-20 21:35:57 +00:00
|
|
|
struct address_space *mapping)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2010-02-23 19:43:04 +00:00
|
|
|
tree->state = RB_ROOT;
|
2010-10-27 00:57:29 +00:00
|
|
|
INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
|
2008-01-24 21:13:08 +00:00
|
|
|
tree->ops = NULL;
|
|
|
|
tree->dirty_bytes = 0;
|
2008-01-29 14:59:12 +00:00
|
|
|
spin_lock_init(&tree->lock);
|
2008-07-22 15:18:07 +00:00
|
|
|
spin_lock_init(&tree->buffer_lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
tree->mapping = mapping;
|
|
|
|
}
|
|
|
|
|
2008-12-02 14:54:17 +00:00
|
|
|
static struct extent_state *alloc_extent_state(gfp_t mask)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_state *state;
|
2009-02-04 14:24:05 +00:00
|
|
|
#if LEAK_DEBUG
|
2008-03-26 20:24:23 +00:00
|
|
|
unsigned long flags;
|
2008-09-08 15:18:08 +00:00
|
|
|
#endif
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
state = kmem_cache_alloc(extent_state_cache, mask);
|
2008-04-01 15:21:40 +00:00
|
|
|
if (!state)
|
2008-01-24 21:13:08 +00:00
|
|
|
return state;
|
|
|
|
state->state = 0;
|
|
|
|
state->private = 0;
|
2008-01-29 14:59:12 +00:00
|
|
|
state->tree = NULL;
|
2009-02-04 14:24:05 +00:00
|
|
|
#if LEAK_DEBUG
|
2008-03-26 20:24:23 +00:00
|
|
|
spin_lock_irqsave(&leak_lock, flags);
|
|
|
|
list_add(&state->leak_list, &states);
|
|
|
|
spin_unlock_irqrestore(&leak_lock, flags);
|
2008-09-08 15:18:08 +00:00
|
|
|
#endif
|
2008-01-24 21:13:08 +00:00
|
|
|
atomic_set(&state->refs, 1);
|
|
|
|
init_waitqueue_head(&state->wq);
|
2012-03-01 13:56:26 +00:00
|
|
|
trace_alloc_extent_state(state, mask, _RET_IP_);
|
2008-01-24 21:13:08 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2010-05-26 00:56:50 +00:00
|
|
|
void free_extent_state(struct extent_state *state)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
if (atomic_dec_and_test(&state->refs)) {
|
2009-02-04 14:24:05 +00:00
|
|
|
#if LEAK_DEBUG
|
2008-03-26 20:24:23 +00:00
|
|
|
unsigned long flags;
|
2008-09-08 15:18:08 +00:00
|
|
|
#endif
|
2008-01-29 14:59:12 +00:00
|
|
|
WARN_ON(state->tree);
|
2009-02-04 14:24:05 +00:00
|
|
|
#if LEAK_DEBUG
|
2008-03-26 20:24:23 +00:00
|
|
|
spin_lock_irqsave(&leak_lock, flags);
|
|
|
|
list_del(&state->leak_list);
|
|
|
|
spin_unlock_irqrestore(&leak_lock, flags);
|
2008-09-08 15:18:08 +00:00
|
|
|
#endif
|
2012-03-01 13:56:26 +00:00
|
|
|
trace_free_extent_state(state, _RET_IP_);
|
2008-01-24 21:13:08 +00:00
|
|
|
kmem_cache_free(extent_state_cache, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
|
|
|
|
struct rb_node *node)
|
|
|
|
{
|
2009-01-06 02:25:51 +00:00
|
|
|
struct rb_node **p = &root->rb_node;
|
|
|
|
struct rb_node *parent = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct tree_entry *entry;
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (*p) {
|
2008-01-24 21:13:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = rb_entry(node, struct tree_entry, rb_node);
|
|
|
|
rb_link_node(node, parent, p);
|
|
|
|
rb_insert_color(node, root);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-01 19:51:59 +00:00
|
|
|
static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
|
2008-01-24 21:13:08 +00:00
|
|
|
struct rb_node **prev_ret,
|
|
|
|
struct rb_node **next_ret)
|
|
|
|
{
|
2008-02-01 19:51:59 +00:00
|
|
|
struct rb_root *root = &tree->state;
|
2009-01-06 02:25:51 +00:00
|
|
|
struct rb_node *n = root->rb_node;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct rb_node *prev = NULL;
|
|
|
|
struct rb_node *orig_prev = NULL;
|
|
|
|
struct tree_entry *entry;
|
|
|
|
struct tree_entry *prev_entry = NULL;
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (n) {
|
2008-01-24 21:13:08 +00:00
|
|
|
entry = rb_entry(n, struct tree_entry, rb_node);
|
|
|
|
prev = n;
|
|
|
|
prev_entry = entry;
|
|
|
|
|
|
|
|
if (offset < entry->start)
|
|
|
|
n = n->rb_left;
|
|
|
|
else if (offset > entry->end)
|
|
|
|
n = n->rb_right;
|
2009-01-06 02:25:51 +00:00
|
|
|
else
|
2008-01-24 21:13:08 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prev_ret) {
|
|
|
|
orig_prev = prev;
|
2009-01-06 02:25:51 +00:00
|
|
|
while (prev && offset > prev_entry->end) {
|
2008-01-24 21:13:08 +00:00
|
|
|
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);
|
2009-01-06 02:25:51 +00:00
|
|
|
while (prev && offset < prev_entry->start) {
|
2008-01-24 21:13:08 +00:00
|
|
|
prev = rb_prev(prev);
|
|
|
|
prev_entry = rb_entry(prev, struct tree_entry, rb_node);
|
|
|
|
}
|
|
|
|
*next_ret = prev;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-01 19:51:59 +00:00
|
|
|
static inline struct rb_node *tree_search(struct extent_io_tree *tree,
|
|
|
|
u64 offset)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2008-01-29 14:59:12 +00:00
|
|
|
struct rb_node *prev = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct rb_node *ret;
|
2008-01-29 14:59:12 +00:00
|
|
|
|
2008-02-01 19:51:59 +00:00
|
|
|
ret = __etree_search(tree, offset, &prev, NULL);
|
2009-01-06 02:25:51 +00:00
|
|
|
if (!ret)
|
2008-01-24 21:13:08 +00:00
|
|
|
return prev;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-11 20:12:44 +00:00
|
|
|
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->mapping->host, new,
|
|
|
|
other);
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-07-21 16:56:09 +00:00
|
|
|
static void merge_state(struct extent_io_tree *tree,
|
|
|
|
struct extent_state *state)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_state *other;
|
|
|
|
struct rb_node *other_node;
|
|
|
|
|
2008-09-26 14:05:38 +00:00
|
|
|
if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
|
2011-07-21 16:56:09 +00:00
|
|
|
return;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
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) {
|
2009-09-11 20:12:44 +00:00
|
|
|
merge_cb(tree, state, other);
|
2008-01-24 21:13:08 +00:00
|
|
|
state->start = other->start;
|
2008-01-29 14:59:12 +00:00
|
|
|
other->tree = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
rb_erase(&other->rb_node, &tree->state);
|
|
|
|
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) {
|
2009-09-11 20:12:44 +00:00
|
|
|
merge_cb(tree, state, other);
|
2011-06-20 18:53:48 +00:00
|
|
|
state->end = other->end;
|
|
|
|
other->tree = NULL;
|
|
|
|
rb_erase(&other->rb_node, &tree->state);
|
|
|
|
free_extent_state(other);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-21 16:56:09 +00:00
|
|
|
static void set_state_cb(struct extent_io_tree *tree,
|
2010-05-16 14:48:47 +00:00
|
|
|
struct extent_state *state, int *bits)
|
2008-01-29 20:55:23 +00:00
|
|
|
{
|
2011-07-21 16:56:09 +00:00
|
|
|
if (tree->ops && tree->ops->set_bit_hook)
|
|
|
|
tree->ops->set_bit_hook(tree->mapping->host, state, bits);
|
2008-01-29 20:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_state_cb(struct extent_io_tree *tree,
|
2010-05-16 14:48:47 +00:00
|
|
|
struct extent_state *state, int *bits)
|
2008-01-29 20:55:23 +00:00
|
|
|
{
|
2009-09-11 20:12:44 +00:00
|
|
|
if (tree->ops && tree->ops->clear_bit_hook)
|
|
|
|
tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
|
2008-01-29 20:55:23 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 03:19:08 +00:00
|
|
|
static void set_state_bits(struct extent_io_tree *tree,
|
|
|
|
struct extent_state *state, int *bits);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* 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,
|
2010-05-16 14:48:47 +00:00
|
|
|
int *bits)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct rb_node *node;
|
|
|
|
|
|
|
|
if (end < start) {
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs end < start %llu %llu\n",
|
|
|
|
(unsigned long long)end,
|
|
|
|
(unsigned long long)start);
|
2008-01-24 21:13:08 +00:00
|
|
|
WARN_ON(1);
|
|
|
|
}
|
|
|
|
state->start = start;
|
|
|
|
state->end = end;
|
2009-09-11 20:12:44 +00:00
|
|
|
|
2011-07-14 03:19:08 +00:00
|
|
|
set_state_bits(tree, state, bits);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
node = tree_insert(&tree->state, end, &state->rb_node);
|
|
|
|
if (node) {
|
|
|
|
struct extent_state *found;
|
|
|
|
found = rb_entry(node, struct extent_state, rb_node);
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs found node %llu %llu on insert of "
|
|
|
|
"%llu %llu\n", (unsigned long long)found->start,
|
|
|
|
(unsigned long long)found->end,
|
|
|
|
(unsigned long long)start, (unsigned long long)end);
|
2008-01-24 21:13:08 +00:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
2008-01-29 14:59:12 +00:00
|
|
|
state->tree = tree;
|
2008-01-24 21:13:08 +00:00
|
|
|
merge_state(tree, state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-21 16:56:09 +00:00
|
|
|
static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
|
2009-09-11 20:12:44 +00:00
|
|
|
u64 split)
|
|
|
|
{
|
|
|
|
if (tree->ops && tree->ops->split_extent_hook)
|
2011-07-21 16:56:09 +00:00
|
|
|
tree->ops->split_extent_hook(tree->mapping->host, orig, split);
|
2009-09-11 20:12:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2009-09-11 20:12:44 +00:00
|
|
|
|
|
|
|
split_cb(tree, orig, split);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
prealloc->start = orig->start;
|
|
|
|
prealloc->end = split - 1;
|
|
|
|
prealloc->state = orig->state;
|
|
|
|
orig->start = split;
|
|
|
|
|
|
|
|
node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
|
|
|
|
if (node) {
|
|
|
|
free_extent_state(prealloc);
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
2008-01-29 14:59:12 +00:00
|
|
|
prealloc->tree = tree;
|
2008-01-24 21:13:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* utility function to clear some bits in an extent state struct.
|
|
|
|
* it will optionally wake up any one waiting on this state (wake == 1), or
|
|
|
|
* forcibly remove the state from the tree (delete == 1).
|
|
|
|
*
|
|
|
|
* If no bits are set on the state struct after clearing things, the
|
|
|
|
* struct is freed and removed from the tree
|
|
|
|
*/
|
|
|
|
static int clear_state_bit(struct extent_io_tree *tree,
|
2010-05-16 14:48:47 +00:00
|
|
|
struct extent_state *state,
|
|
|
|
int *bits, int wake)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2010-05-16 14:48:47 +00:00
|
|
|
int bits_to_clear = *bits & ~EXTENT_CTLBITS;
|
2009-10-08 17:34:05 +00:00
|
|
|
int ret = state->state & bits_to_clear;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2010-05-16 14:48:47 +00:00
|
|
|
if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
|
2008-01-24 21:13:08 +00:00
|
|
|
u64 range = state->end - state->start + 1;
|
|
|
|
WARN_ON(range > tree->dirty_bytes);
|
|
|
|
tree->dirty_bytes -= range;
|
|
|
|
}
|
2008-01-29 20:55:23 +00:00
|
|
|
clear_state_cb(tree, state, bits);
|
2009-10-08 17:34:05 +00:00
|
|
|
state->state &= ~bits_to_clear;
|
2008-01-24 21:13:08 +00:00
|
|
|
if (wake)
|
|
|
|
wake_up(&state->wq);
|
2010-05-16 14:48:47 +00:00
|
|
|
if (state->state == 0) {
|
2008-01-29 14:59:12 +00:00
|
|
|
if (state->tree) {
|
2008-01-24 21:13:08 +00:00
|
|
|
rb_erase(&state->rb_node, &tree->state);
|
2008-01-29 14:59:12 +00:00
|
|
|
state->tree = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
free_extent_state(state);
|
|
|
|
} else {
|
|
|
|
WARN_ON(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
merge_state(tree, state);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-20 06:44:57 +00:00
|
|
|
static struct extent_state *
|
|
|
|
alloc_extent_state_atomic(struct extent_state *prealloc)
|
|
|
|
{
|
|
|
|
if (!prealloc)
|
|
|
|
prealloc = alloc_extent_state(GFP_ATOMIC);
|
|
|
|
|
|
|
|
return prealloc;
|
|
|
|
}
|
|
|
|
|
2011-10-04 03:22:32 +00:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-03-01 13:56:29 +00:00
|
|
|
* This takes the tree lock, and returns 0 on success and < 0 on error.
|
2008-01-24 21:13:08 +00:00
|
|
|
*/
|
|
|
|
int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
|
2009-09-02 19:04:12 +00:00
|
|
|
int bits, int wake, int delete,
|
|
|
|
struct extent_state **cached_state,
|
|
|
|
gfp_t mask)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_state *state;
|
2009-09-02 19:04:12 +00:00
|
|
|
struct extent_state *cached;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct extent_state *prealloc = NULL;
|
2009-09-02 19:04:12 +00:00
|
|
|
struct rb_node *next_node;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct rb_node *node;
|
2009-05-27 13:16:03 +00:00
|
|
|
u64 last_end;
|
2008-01-24 21:13:08 +00:00
|
|
|
int err;
|
2010-02-03 19:33:23 +00:00
|
|
|
int clear = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2010-05-16 14:48:47 +00:00
|
|
|
if (delete)
|
|
|
|
bits |= ~EXTENT_CTLBITS;
|
|
|
|
bits |= EXTENT_FIRST_DELALLOC;
|
|
|
|
|
2010-02-03 19:33:23 +00:00
|
|
|
if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
|
|
|
|
clear = 1;
|
2008-01-24 21:13:08 +00:00
|
|
|
again:
|
|
|
|
if (!prealloc && (mask & __GFP_WAIT)) {
|
|
|
|
prealloc = alloc_extent_state(mask);
|
|
|
|
if (!prealloc)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2009-09-02 19:04:12 +00:00
|
|
|
if (cached_state) {
|
|
|
|
cached = *cached_state;
|
2010-02-03 19:33:23 +00:00
|
|
|
|
|
|
|
if (clear) {
|
|
|
|
*cached_state = NULL;
|
|
|
|
cached_state = NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-20 18:53:48 +00:00
|
|
|
if (cached && cached->tree && cached->start <= start &&
|
|
|
|
cached->end > start) {
|
2010-02-03 19:33:23 +00:00
|
|
|
if (clear)
|
|
|
|
atomic_dec(&cached->refs);
|
2009-09-02 19:04:12 +00:00
|
|
|
state = cached;
|
2009-09-23 23:51:09 +00:00
|
|
|
goto hit_next;
|
2009-09-02 19:04:12 +00:00
|
|
|
}
|
2010-02-03 19:33:23 +00:00
|
|
|
if (clear)
|
|
|
|
free_extent_state(cached);
|
2009-09-02 19:04:12 +00:00
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* this search will find the extents that end after
|
|
|
|
* our range starts
|
|
|
|
*/
|
2008-02-01 19:51:59 +00:00
|
|
|
node = tree_search(tree, start);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!node)
|
|
|
|
goto out;
|
|
|
|
state = rb_entry(node, struct extent_state, rb_node);
|
2009-09-02 19:04:12 +00:00
|
|
|
hit_next:
|
2008-01-24 21:13:08 +00:00
|
|
|
if (state->start > end)
|
|
|
|
goto out;
|
|
|
|
WARN_ON(state->end < start);
|
2009-05-27 13:16:03 +00:00
|
|
|
last_end = state->end;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2012-02-16 10:34:37 +00:00
|
|
|
if (state->end < end && !need_resched())
|
|
|
|
next_node = rb_next(&state->rb_node);
|
|
|
|
else
|
|
|
|
next_node = NULL;
|
|
|
|
|
|
|
|
/* the state doesn't have the wanted bits, go ahead */
|
|
|
|
if (!(state->state & bits))
|
|
|
|
goto next;
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* | ---- 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) {
|
2011-04-20 06:44:57 +00:00
|
|
|
prealloc = alloc_extent_state_atomic(prealloc);
|
|
|
|
BUG_ON(!prealloc);
|
2008-01-24 21:13:08 +00:00
|
|
|
err = split_state(tree, state, prealloc, start);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
prealloc = NULL;
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
if (state->end <= end) {
|
2012-03-01 13:56:29 +00:00
|
|
|
clear_state_bit(tree, state, &bits, wake);
|
2009-05-27 13:16:03 +00:00
|
|
|
if (last_end == (u64)-1)
|
|
|
|
goto out;
|
|
|
|
start = last_end + 1;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
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) {
|
2011-04-20 06:44:57 +00:00
|
|
|
prealloc = alloc_extent_state_atomic(prealloc);
|
|
|
|
BUG_ON(!prealloc);
|
2008-01-24 21:13:08 +00:00
|
|
|
err = split_state(tree, state, prealloc, end + 1);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
if (wake)
|
|
|
|
wake_up(&state->wq);
|
2009-09-23 23:51:09 +00:00
|
|
|
|
2012-03-01 13:56:29 +00:00
|
|
|
clear_state_bit(tree, prealloc, &bits, wake);
|
2009-09-11 20:12:44 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
prealloc = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-09-23 23:51:09 +00:00
|
|
|
|
2012-03-01 13:56:29 +00:00
|
|
|
clear_state_bit(tree, state, &bits, wake);
|
2012-02-16 10:34:37 +00:00
|
|
|
next:
|
2009-05-27 13:16:03 +00:00
|
|
|
if (last_end == (u64)-1)
|
|
|
|
goto out;
|
|
|
|
start = last_end + 1;
|
2009-09-02 19:04:12 +00:00
|
|
|
if (start <= end && next_node) {
|
|
|
|
state = rb_entry(next_node, struct extent_state,
|
|
|
|
rb_node);
|
2012-02-16 10:34:36 +00:00
|
|
|
goto hit_next;
|
2009-09-02 19:04:12 +00:00
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
goto search_again;
|
|
|
|
|
|
|
|
out:
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (prealloc)
|
|
|
|
free_extent_state(prealloc);
|
|
|
|
|
2012-03-01 13:56:29 +00:00
|
|
|
return 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
search_again:
|
|
|
|
if (start > end)
|
|
|
|
goto out;
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (mask & __GFP_WAIT)
|
|
|
|
cond_resched();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2012-03-01 13:56:26 +00:00
|
|
|
static void wait_on_state(struct extent_io_tree *tree,
|
|
|
|
struct extent_state *state)
|
2008-12-02 11:36:10 +00:00
|
|
|
__releases(tree->lock)
|
|
|
|
__acquires(tree->lock)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
schedule();
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
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
|
|
|
|
*/
|
2012-03-01 13:56:26 +00:00
|
|
|
void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_state *state;
|
|
|
|
struct rb_node *node;
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
again:
|
|
|
|
while (1) {
|
|
|
|
/*
|
|
|
|
* this search will find all the extents that end after
|
|
|
|
* our range starts
|
|
|
|
*/
|
2008-02-01 19:51:59 +00:00
|
|
|
node = tree_search(tree, start);
|
2008-01-24 21:13:08 +00:00
|
|
|
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;
|
|
|
|
atomic_inc(&state->refs);
|
|
|
|
wait_on_state(tree, state);
|
|
|
|
free_extent_state(state);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
start = state->end + 1;
|
|
|
|
|
|
|
|
if (start > end)
|
|
|
|
break;
|
|
|
|
|
2011-07-14 03:19:27 +00:00
|
|
|
cond_resched_lock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
out:
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 16:56:09 +00:00
|
|
|
static void set_state_bits(struct extent_io_tree *tree,
|
2008-01-24 21:13:08 +00:00
|
|
|
struct extent_state *state,
|
2010-05-16 14:48:47 +00:00
|
|
|
int *bits)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2010-05-16 14:48:47 +00:00
|
|
|
int bits_to_set = *bits & ~EXTENT_CTLBITS;
|
2009-09-11 20:12:44 +00:00
|
|
|
|
2011-07-21 16:56:09 +00:00
|
|
|
set_state_cb(tree, state, bits);
|
2010-05-16 14:48:47 +00:00
|
|
|
if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
|
2008-01-24 21:13:08 +00:00
|
|
|
u64 range = state->end - state->start + 1;
|
|
|
|
tree->dirty_bytes += range;
|
|
|
|
}
|
2010-05-16 14:48:47 +00:00
|
|
|
state->state |= bits_to_set;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-02 19:04:12 +00:00
|
|
|
static void cache_state(struct extent_state *state,
|
|
|
|
struct extent_state **cached_ptr)
|
|
|
|
{
|
|
|
|
if (cached_ptr && !(*cached_ptr)) {
|
|
|
|
if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
|
|
|
|
*cached_ptr = state;
|
|
|
|
atomic_inc(&state->refs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 10:02:20 +00:00
|
|
|
static void uncache_state(struct extent_state **cached_ptr)
|
|
|
|
{
|
|
|
|
if (cached_ptr && (*cached_ptr)) {
|
|
|
|
struct extent_state *state = *cached_ptr;
|
2011-04-12 17:57:39 +00:00
|
|
|
*cached_ptr = NULL;
|
|
|
|
free_extent_state(state);
|
2011-04-06 10:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
2009-09-02 17:24:36 +00:00
|
|
|
* 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.
|
2008-01-24 21:13:08 +00:00
|
|
|
*
|
2009-09-02 17:24:36 +00:00
|
|
|
* 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.
|
2008-01-24 21:13:08 +00:00
|
|
|
*
|
2009-09-02 17:24:36 +00:00
|
|
|
* [start, end] is inclusive This takes the tree lock.
|
2008-01-24 21:13:08 +00:00
|
|
|
*/
|
2009-09-02 17:24:36 +00:00
|
|
|
|
2012-03-01 13:57:19 +00:00
|
|
|
static int __must_check
|
|
|
|
__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
int bits, int exclusive_bits, u64 *failed_start,
|
|
|
|
struct extent_state **cached_state, gfp_t mask)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_state *state;
|
|
|
|
struct extent_state *prealloc = NULL;
|
|
|
|
struct rb_node *node;
|
|
|
|
int err = 0;
|
|
|
|
u64 last_start;
|
|
|
|
u64 last_end;
|
2009-09-23 23:51:09 +00:00
|
|
|
|
2010-05-16 14:48:47 +00:00
|
|
|
bits |= EXTENT_FIRST_DELALLOC;
|
2008-01-24 21:13:08 +00:00
|
|
|
again:
|
|
|
|
if (!prealloc && (mask & __GFP_WAIT)) {
|
|
|
|
prealloc = alloc_extent_state(mask);
|
2011-04-20 06:44:57 +00:00
|
|
|
BUG_ON(!prealloc);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2009-09-02 19:22:30 +00:00
|
|
|
if (cached_state && *cached_state) {
|
|
|
|
state = *cached_state;
|
2011-06-20 18:53:48 +00:00
|
|
|
if (state->start <= start && state->end > start &&
|
|
|
|
state->tree) {
|
2009-09-02 19:22:30 +00:00
|
|
|
node = &state->rb_node;
|
|
|
|
goto hit_next;
|
|
|
|
}
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* this search will find all the extents that end after
|
|
|
|
* our range starts.
|
|
|
|
*/
|
2008-02-01 19:51:59 +00:00
|
|
|
node = tree_search(tree, start);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!node) {
|
2011-04-20 06:44:57 +00:00
|
|
|
prealloc = alloc_extent_state_atomic(prealloc);
|
|
|
|
BUG_ON(!prealloc);
|
2010-05-16 14:48:47 +00:00
|
|
|
err = insert_state(tree, prealloc, start, end, &bits);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
prealloc = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
state = rb_entry(node, struct extent_state, rb_node);
|
2009-08-05 16:57:59 +00:00
|
|
|
hit_next:
|
2008-01-24 21:13:08 +00:00
|
|
|
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) {
|
2009-08-05 16:57:59 +00:00
|
|
|
struct rb_node *next_node;
|
2009-09-02 17:24:36 +00:00
|
|
|
if (state->state & exclusive_bits) {
|
2008-01-24 21:13:08 +00:00
|
|
|
*failed_start = state->start;
|
|
|
|
err = -EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-09-23 23:51:09 +00:00
|
|
|
|
2011-07-21 16:56:09 +00:00
|
|
|
set_state_bits(tree, state, &bits);
|
2009-09-11 20:12:44 +00:00
|
|
|
|
2009-09-02 19:04:12 +00:00
|
|
|
cache_state(state, cached_state);
|
2008-01-24 21:13:08 +00:00
|
|
|
merge_state(tree, state);
|
2009-05-27 13:16:03 +00:00
|
|
|
if (last_end == (u64)-1)
|
|
|
|
goto out;
|
2009-08-05 16:57:59 +00:00
|
|
|
|
2009-05-27 13:16:03 +00:00
|
|
|
start = last_end + 1;
|
2011-06-20 18:53:48 +00:00
|
|
|
next_node = rb_next(&state->rb_node);
|
2011-04-20 06:45:49 +00:00
|
|
|
if (next_node && start < end && prealloc && !need_resched()) {
|
|
|
|
state = rb_entry(next_node, struct extent_state,
|
|
|
|
rb_node);
|
|
|
|
if (state->start == start)
|
|
|
|
goto hit_next;
|
2009-08-05 16:57:59 +00:00
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
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) {
|
2009-09-02 17:24:36 +00:00
|
|
|
if (state->state & exclusive_bits) {
|
2008-01-24 21:13:08 +00:00
|
|
|
*failed_start = start;
|
|
|
|
err = -EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-04-20 06:44:57 +00:00
|
|
|
|
|
|
|
prealloc = alloc_extent_state_atomic(prealloc);
|
|
|
|
BUG_ON(!prealloc);
|
2008-01-24 21:13:08 +00:00
|
|
|
err = split_state(tree, state, prealloc, start);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
prealloc = NULL;
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
if (state->end <= end) {
|
2011-07-21 16:56:09 +00:00
|
|
|
set_state_bits(tree, state, &bits);
|
2009-09-02 19:04:12 +00:00
|
|
|
cache_state(state, cached_state);
|
2008-01-24 21:13:08 +00:00
|
|
|
merge_state(tree, state);
|
2009-05-27 13:16:03 +00:00
|
|
|
if (last_end == (u64)-1)
|
|
|
|
goto out;
|
|
|
|
start = last_end + 1;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
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
|
2009-01-06 02:25:51 +00:00
|
|
|
this_end = last_start - 1;
|
2011-04-20 06:44:57 +00:00
|
|
|
|
|
|
|
prealloc = alloc_extent_state_atomic(prealloc);
|
|
|
|
BUG_ON(!prealloc);
|
2011-04-20 06:45:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid to free 'prealloc' if it can be merged with
|
|
|
|
* the later extent.
|
|
|
|
*/
|
2008-01-24 21:13:08 +00:00
|
|
|
err = insert_state(tree, prealloc, start, this_end,
|
2010-05-16 14:48:47 +00:00
|
|
|
&bits);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
|
|
|
|
2009-09-11 20:12:44 +00:00
|
|
|
cache_state(prealloc, cached_state);
|
|
|
|
prealloc = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
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) {
|
2009-09-02 17:24:36 +00:00
|
|
|
if (state->state & exclusive_bits) {
|
2008-01-24 21:13:08 +00:00
|
|
|
*failed_start = start;
|
|
|
|
err = -EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-04-20 06:44:57 +00:00
|
|
|
|
|
|
|
prealloc = alloc_extent_state_atomic(prealloc);
|
|
|
|
BUG_ON(!prealloc);
|
2008-01-24 21:13:08 +00:00
|
|
|
err = split_state(tree, state, prealloc, end + 1);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2011-07-21 16:56:09 +00:00
|
|
|
set_state_bits(tree, prealloc, &bits);
|
2009-09-02 19:04:12 +00:00
|
|
|
cache_state(prealloc, cached_state);
|
2008-01-24 21:13:08 +00:00
|
|
|
merge_state(tree, prealloc);
|
|
|
|
prealloc = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto search_again;
|
|
|
|
|
|
|
|
out:
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (prealloc)
|
|
|
|
free_extent_state(prealloc);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
search_again:
|
|
|
|
if (start > end)
|
|
|
|
goto out;
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (mask & __GFP_WAIT)
|
|
|
|
cond_resched();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2012-03-01 13:57:19 +00:00
|
|
|
int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-26 17:56:12 +00:00
|
|
|
/**
|
|
|
|
* convert_extent - 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
|
|
|
|
* @mask: the allocation mask
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
int bits, int clear_bits, gfp_t mask)
|
|
|
|
{
|
|
|
|
struct extent_state *state;
|
|
|
|
struct extent_state *prealloc = NULL;
|
|
|
|
struct rb_node *node;
|
|
|
|
int err = 0;
|
|
|
|
u64 last_start;
|
|
|
|
u64 last_end;
|
|
|
|
|
|
|
|
again:
|
|
|
|
if (!prealloc && (mask & __GFP_WAIT)) {
|
|
|
|
prealloc = alloc_extent_state(mask);
|
|
|
|
if (!prealloc)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&tree->lock);
|
|
|
|
/*
|
|
|
|
* this search will find all the extents that end after
|
|
|
|
* our range starts.
|
|
|
|
*/
|
|
|
|
node = tree_search(tree, start);
|
|
|
|
if (!node) {
|
|
|
|
prealloc = alloc_extent_state_atomic(prealloc);
|
2011-12-08 01:08:40 +00:00
|
|
|
if (!prealloc) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-09-26 17:56:12 +00:00
|
|
|
err = insert_state(tree, prealloc, start, end, &bits);
|
|
|
|
prealloc = NULL;
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
2011-09-26 17:56:12 +00:00
|
|
|
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) {
|
|
|
|
struct rb_node *next_node;
|
|
|
|
|
|
|
|
set_state_bits(tree, state, &bits);
|
|
|
|
clear_state_bit(tree, state, &clear_bits, 0);
|
|
|
|
if (last_end == (u64)-1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
start = last_end + 1;
|
|
|
|
next_node = rb_next(&state->rb_node);
|
|
|
|
if (next_node && start < end && prealloc && !need_resched()) {
|
|
|
|
state = rb_entry(next_node, struct extent_state,
|
|
|
|
rb_node);
|
|
|
|
if (state->start == start)
|
|
|
|
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);
|
2011-12-08 01:08:40 +00:00
|
|
|
if (!prealloc) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-09-26 17:56:12 +00:00
|
|
|
err = split_state(tree, state, prealloc, start);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
2011-09-26 17:56:12 +00:00
|
|
|
prealloc = NULL;
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
if (state->end <= end) {
|
|
|
|
set_state_bits(tree, state, &bits);
|
|
|
|
clear_state_bit(tree, state, &clear_bits, 0);
|
|
|
|
if (last_end == (u64)-1)
|
|
|
|
goto out;
|
|
|
|
start = last_end + 1;
|
|
|
|
}
|
|
|
|
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);
|
2011-12-08 01:08:40 +00:00
|
|
|
if (!prealloc) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-09-26 17:56:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid to free 'prealloc' if it can be merged with
|
|
|
|
* the later extent.
|
|
|
|
*/
|
|
|
|
err = insert_state(tree, prealloc, start, this_end,
|
|
|
|
&bits);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
2011-09-26 17:56:12 +00:00
|
|
|
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);
|
2011-12-08 01:08:40 +00:00
|
|
|
if (!prealloc) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-09-26 17:56:12 +00:00
|
|
|
|
|
|
|
err = split_state(tree, state, prealloc, end + 1);
|
2011-10-04 03:22:32 +00:00
|
|
|
if (err)
|
|
|
|
extent_io_tree_panic(tree, err);
|
2011-09-26 17:56:12 +00:00
|
|
|
|
|
|
|
set_state_bits(tree, prealloc, &bits);
|
|
|
|
clear_state_bit(tree, prealloc, &clear_bits, 0);
|
|
|
|
prealloc = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto search_again;
|
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
if (prealloc)
|
|
|
|
free_extent_state(prealloc);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
search_again:
|
|
|
|
if (start > end)
|
|
|
|
goto out;
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
if (mask & __GFP_WAIT)
|
|
|
|
cond_resched();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/* wrappers around set/clear extent bit */
|
|
|
|
int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
gfp_t mask)
|
|
|
|
{
|
2012-03-01 13:57:19 +00:00
|
|
|
return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
|
2009-09-02 19:04:12 +00:00
|
|
|
NULL, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
int bits, gfp_t mask)
|
|
|
|
{
|
2012-03-01 13:57:19 +00:00
|
|
|
return set_extent_bit(tree, start, end, bits, NULL,
|
2009-09-02 19:04:12 +00:00
|
|
|
NULL, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
int bits, gfp_t mask)
|
|
|
|
{
|
2009-09-02 19:04:12 +00:00
|
|
|
return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
|
2010-02-03 19:33:23 +00:00
|
|
|
struct extent_state **cached_state, gfp_t mask)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
return set_extent_bit(tree, start, end,
|
2011-09-29 07:55:28 +00:00
|
|
|
EXTENT_DELALLOC | EXTENT_UPTODATE,
|
2012-03-01 13:57:19 +00:00
|
|
|
NULL, cached_state, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
gfp_t mask)
|
|
|
|
{
|
|
|
|
return clear_extent_bit(tree, start, end,
|
2009-10-08 17:34:05 +00:00
|
|
|
EXTENT_DIRTY | EXTENT_DELALLOC |
|
2010-05-16 14:48:47 +00:00
|
|
|
EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
gfp_t mask)
|
|
|
|
{
|
2012-03-01 13:57:19 +00:00
|
|
|
return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
|
2009-09-02 19:04:12 +00:00
|
|
|
NULL, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
|
2011-04-06 10:02:20 +00:00
|
|
|
struct extent_state **cached_state, gfp_t mask)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2011-04-06 10:02:20 +00:00
|
|
|
return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
|
2012-03-01 13:57:19 +00:00
|
|
|
cached_state, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
|
2010-02-03 19:33:23 +00:00
|
|
|
u64 end, struct extent_state **cached_state,
|
|
|
|
gfp_t mask)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2009-09-02 19:04:12 +00:00
|
|
|
return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
|
2010-02-03 19:33:23 +00:00
|
|
|
cached_state, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-29 19:18:18 +00:00
|
|
|
/*
|
|
|
|
* either insert or lock state struct between start and end use mask to tell
|
|
|
|
* us if waiting is desired.
|
|
|
|
*/
|
2009-09-02 17:24:36 +00:00
|
|
|
int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
|
2012-03-01 13:57:19 +00:00
|
|
|
int bits, struct extent_state **cached_state)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
u64 failed_start;
|
|
|
|
while (1) {
|
2012-03-01 13:57:19 +00:00
|
|
|
err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
|
|
|
|
EXTENT_LOCKED, &failed_start,
|
|
|
|
cached_state, GFP_NOFS);
|
2012-03-01 13:57:19 +00:00
|
|
|
if (err == -EEXIST) {
|
2008-01-24 21:13:08 +00:00
|
|
|
wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
|
|
|
|
start = failed_start;
|
2012-03-01 13:57:19 +00:00
|
|
|
} else
|
2008-01-24 21:13:08 +00:00
|
|
|
break;
|
|
|
|
WARN_ON(start > end);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-03-01 13:57:19 +00:00
|
|
|
int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
|
2009-09-02 17:24:36 +00:00
|
|
|
{
|
2012-03-01 13:57:19 +00:00
|
|
|
return lock_extent_bits(tree, start, end, 0, NULL);
|
2009-09-02 17:24:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 13:57:19 +00:00
|
|
|
int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
|
Btrfs: nuke fs wide allocation mutex V2
This patch removes the giant fs_info->alloc_mutex and replaces it with a bunch
of little locks.
There is now a pinned_mutex, which is used when messing with the pinned_extents
extent io tree, and the extent_ins_mutex which is used with the pending_del and
extent_ins extent io trees.
The locking for the extent tree stuff was inspired by a patch that Yan Zheng
wrote to fix a race condition, I cleaned it up some and changed the locking
around a little bit, but the idea remains the same. Basically instead of
holding the extent_ins_mutex throughout the processing of an extent on the
extent_ins or pending_del trees, we just hold it while we're searching and when
we clear the bits on those trees, and lock the extent for the duration of the
operations on the extent.
Also to keep from getting hung up waiting to lock an extent, I've added a
try_lock_extent so if we cannot lock the extent, move on to the next one in the
tree and we'll come back to that one. I have tested this heavily and it does
not appear to break anything. This has to be applied on top of my
find_free_extent redo patch.
I tested this patch on top of Yan's space reblancing code and it worked fine.
The only thing that has changed since the last version is I pulled out all my
debugging stuff, apparently I forgot to run guilt refresh before I sent the
last patch out. Thank you,
Signed-off-by: Josef Bacik <jbacik@redhat.com>
2008-10-29 18:49:05 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
u64 failed_start;
|
|
|
|
|
2012-03-01 13:57:19 +00:00
|
|
|
err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
|
|
|
|
&failed_start, NULL, GFP_NOFS);
|
2008-10-30 18:19:50 +00:00
|
|
|
if (err == -EEXIST) {
|
|
|
|
if (failed_start > start)
|
|
|
|
clear_extent_bit(tree, start, failed_start - 1,
|
2012-03-01 13:57:19 +00:00
|
|
|
EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
|
Btrfs: nuke fs wide allocation mutex V2
This patch removes the giant fs_info->alloc_mutex and replaces it with a bunch
of little locks.
There is now a pinned_mutex, which is used when messing with the pinned_extents
extent io tree, and the extent_ins_mutex which is used with the pending_del and
extent_ins extent io trees.
The locking for the extent tree stuff was inspired by a patch that Yan Zheng
wrote to fix a race condition, I cleaned it up some and changed the locking
around a little bit, but the idea remains the same. Basically instead of
holding the extent_ins_mutex throughout the processing of an extent on the
extent_ins or pending_del trees, we just hold it while we're searching and when
we clear the bits on those trees, and lock the extent for the duration of the
operations on the extent.
Also to keep from getting hung up waiting to lock an extent, I've added a
try_lock_extent so if we cannot lock the extent, move on to the next one in the
tree and we'll come back to that one. I have tested this heavily and it does
not appear to break anything. This has to be applied on top of my
find_free_extent redo patch.
I tested this patch on top of Yan's space reblancing code and it worked fine.
The only thing that has changed since the last version is I pulled out all my
debugging stuff, apparently I forgot to run guilt refresh before I sent the
last patch out. Thank you,
Signed-off-by: Josef Bacik <jbacik@redhat.com>
2008-10-29 18:49:05 +00:00
|
|
|
return 0;
|
2008-10-30 18:19:50 +00:00
|
|
|
}
|
Btrfs: nuke fs wide allocation mutex V2
This patch removes the giant fs_info->alloc_mutex and replaces it with a bunch
of little locks.
There is now a pinned_mutex, which is used when messing with the pinned_extents
extent io tree, and the extent_ins_mutex which is used with the pending_del and
extent_ins extent io trees.
The locking for the extent tree stuff was inspired by a patch that Yan Zheng
wrote to fix a race condition, I cleaned it up some and changed the locking
around a little bit, but the idea remains the same. Basically instead of
holding the extent_ins_mutex throughout the processing of an extent on the
extent_ins or pending_del trees, we just hold it while we're searching and when
we clear the bits on those trees, and lock the extent for the duration of the
operations on the extent.
Also to keep from getting hung up waiting to lock an extent, I've added a
try_lock_extent so if we cannot lock the extent, move on to the next one in the
tree and we'll come back to that one. I have tested this heavily and it does
not appear to break anything. This has to be applied on top of my
find_free_extent redo patch.
I tested this patch on top of Yan's space reblancing code and it worked fine.
The only thing that has changed since the last version is I pulled out all my
debugging stuff, apparently I forgot to run guilt refresh before I sent the
last patch out. Thank you,
Signed-off-by: Josef Bacik <jbacik@redhat.com>
2008-10-29 18:49:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-02 19:04:12 +00:00
|
|
|
int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
|
|
|
|
struct extent_state **cached, gfp_t mask)
|
|
|
|
{
|
|
|
|
return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
|
|
|
|
mask);
|
|
|
|
}
|
|
|
|
|
2012-03-01 13:57:19 +00:00
|
|
|
int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2009-09-02 19:04:12 +00:00
|
|
|
return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
|
2012-03-01 13:57:19 +00:00
|
|
|
GFP_NOFS);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* helper function to set both pages and extents in the tree writeback
|
|
|
|
*/
|
2008-12-02 14:54:17 +00:00
|
|
|
static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
unsigned long index = start >> PAGE_CACHE_SHIFT;
|
|
|
|
unsigned long end_index = end >> PAGE_CACHE_SHIFT;
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
while (index <= end_index) {
|
|
|
|
page = find_get_page(tree->mapping, index);
|
2012-03-12 15:03:00 +00:00
|
|
|
BUG_ON(!page); /* Pages should be in the extent_io_tree */
|
2008-01-24 21:13:08 +00:00
|
|
|
set_page_writeback(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-29 19:18:18 +00:00
|
|
|
/* 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'
|
|
|
|
*/
|
2008-02-18 17:12:38 +00:00
|
|
|
struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
|
|
|
|
u64 start, int 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);
|
2009-01-06 02:25:51 +00:00
|
|
|
if (!node)
|
2008-02-18 17:12:38 +00:00
|
|
|
goto out;
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (1) {
|
2008-02-18 17:12:38 +00:00
|
|
|
state = rb_entry(node, struct extent_state, rb_node);
|
2009-01-06 02:25:51 +00:00
|
|
|
if (state->end >= start && (state->state & bits))
|
2008-02-18 17:12:38 +00:00
|
|
|
return state;
|
2009-01-06 02:25:51 +00:00
|
|
|
|
2008-02-18 17:12:38 +00:00
|
|
|
node = rb_next(node);
|
|
|
|
if (!node)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-14 03:19:45 +00:00
|
|
|
/*
|
|
|
|
* 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, < 0 on error
|
|
|
|
*/
|
|
|
|
int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
|
|
|
|
u64 *start_ret, u64 *end_ret, int bits)
|
|
|
|
{
|
|
|
|
struct extent_state *state;
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
spin_lock(&tree->lock);
|
|
|
|
state = find_first_extent_bit_state(tree, start, bits);
|
|
|
|
if (state) {
|
|
|
|
*start_ret = state->start;
|
|
|
|
*end_ret = state->end;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-29 19:18:18 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
|
2010-02-02 21:19:11 +00:00
|
|
|
u64 *start, u64 *end, u64 max_bytes,
|
|
|
|
struct extent_state **cached_state)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct rb_node *node;
|
|
|
|
struct extent_state *state;
|
|
|
|
u64 cur_start = *start;
|
|
|
|
u64 found = 0;
|
|
|
|
u64 total_bytes = 0;
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* this search will find all the extents that end after
|
|
|
|
* our range starts.
|
|
|
|
*/
|
2008-02-01 19:51:59 +00:00
|
|
|
node = tree_search(tree, cur_start);
|
2008-04-01 15:21:40 +00:00
|
|
|
if (!node) {
|
2008-04-17 15:29:12 +00:00
|
|
|
if (!found)
|
|
|
|
*end = (u64)-1;
|
2008-01-24 21:13:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (1) {
|
2008-01-24 21:13:08 +00:00
|
|
|
state = rb_entry(node, struct extent_state, rb_node);
|
2008-09-26 14:05:38 +00:00
|
|
|
if (found && (state->start != cur_start ||
|
|
|
|
(state->state & EXTENT_BOUNDARY))) {
|
2008-01-24 21:13:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!(state->state & EXTENT_DELALLOC)) {
|
|
|
|
if (!found)
|
|
|
|
*end = state->end;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-02-02 21:19:11 +00:00
|
|
|
if (!found) {
|
2008-01-24 21:13:08 +00:00
|
|
|
*start = state->start;
|
2010-02-02 21:19:11 +00:00
|
|
|
*cached_state = state;
|
|
|
|
atomic_inc(&state->refs);
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
found++;
|
|
|
|
*end = state->end;
|
|
|
|
cur_start = state->end + 1;
|
|
|
|
node = rb_next(node);
|
|
|
|
if (!node)
|
|
|
|
break;
|
|
|
|
total_bytes += state->end - state->start + 1;
|
|
|
|
if (total_bytes >= max_bytes)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2012-03-01 13:56:26 +00:00
|
|
|
static noinline void __unlock_for_delalloc(struct inode *inode,
|
|
|
|
struct page *locked_page,
|
|
|
|
u64 start, u64 end)
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct page *pages[16];
|
|
|
|
unsigned long index = start >> PAGE_CACHE_SHIFT;
|
|
|
|
unsigned long end_index = end >> PAGE_CACHE_SHIFT;
|
|
|
|
unsigned long nr_pages = end_index - index + 1;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (index == locked_page->index && end_index == index)
|
2012-03-01 13:56:26 +00:00
|
|
|
return;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (nr_pages > 0) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
ret = find_get_pages_contig(inode->i_mapping, index,
|
2008-11-11 14:34:41 +00:00
|
|
|
min_t(unsigned long, nr_pages,
|
|
|
|
ARRAY_SIZE(pages)), pages);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
for (i = 0; i < ret; i++) {
|
|
|
|
if (pages[i] != locked_page)
|
|
|
|
unlock_page(pages[i]);
|
|
|
|
page_cache_release(pages[i]);
|
|
|
|
}
|
|
|
|
nr_pages -= ret;
|
|
|
|
index += ret;
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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_CACHE_SHIFT;
|
|
|
|
unsigned long start_index = index;
|
|
|
|
unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
|
|
|
|
unsigned long pages_locked = 0;
|
|
|
|
struct page *pages[16];
|
|
|
|
unsigned long nrpages;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* the caller is responsible for locking the start index */
|
|
|
|
if (index == locked_page->index && index == end_index)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* skip the page at the start index */
|
|
|
|
nrpages = end_index - index + 1;
|
2009-01-06 02:25:51 +00:00
|
|
|
while (nrpages > 0) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
ret = find_get_pages_contig(inode->i_mapping, index,
|
2008-11-11 14:34:41 +00:00
|
|
|
min_t(unsigned long,
|
|
|
|
nrpages, ARRAY_SIZE(pages)), pages);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* now we have an array of pages, lock them all */
|
|
|
|
for (i = 0; i < ret; i++) {
|
|
|
|
/*
|
|
|
|
* the caller is taking responsibility for
|
|
|
|
* locked_page
|
|
|
|
*/
|
2008-11-07 03:02:51 +00:00
|
|
|
if (pages[i] != locked_page) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
lock_page(pages[i]);
|
2008-11-10 12:31:30 +00:00
|
|
|
if (!PageDirty(pages[i]) ||
|
|
|
|
pages[i]->mapping != inode->i_mapping) {
|
2008-11-07 03:02:51 +00:00
|
|
|
ret = -EAGAIN;
|
|
|
|
unlock_page(pages[i]);
|
|
|
|
page_cache_release(pages[i]);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
page_cache_release(pages[i]);
|
2008-11-07 03:02:51 +00:00
|
|
|
pages_locked++;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
}
|
|
|
|
nrpages -= ret;
|
|
|
|
index += ret;
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
done:
|
|
|
|
if (ret && pages_locked) {
|
|
|
|
__unlock_for_delalloc(inode, locked_page,
|
|
|
|
delalloc_start,
|
|
|
|
((u64)(start_index + pages_locked - 1)) <<
|
|
|
|
PAGE_CACHE_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 noinline 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;
|
2009-09-02 19:22:30 +00:00
|
|
|
struct extent_state *cached_state = NULL;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
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,
|
2010-02-02 21:19:11 +00:00
|
|
|
max_bytes, &cached_state);
|
2008-10-31 16:46:39 +00:00
|
|
|
if (!found || delalloc_end <= *start) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
*start = delalloc_start;
|
|
|
|
*end = delalloc_end;
|
2010-02-02 21:19:11 +00:00
|
|
|
free_extent_state(cached_state);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-10-31 16:46:39 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2009-01-06 02:25:51 +00:00
|
|
|
if (delalloc_start < *start)
|
2008-10-31 16:46:39 +00:00
|
|
|
delalloc_start = *start;
|
|
|
|
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
/*
|
|
|
|
* make sure to limit the number of pages we try to lock down
|
|
|
|
* if we're looping.
|
|
|
|
*/
|
2009-01-06 02:25:51 +00:00
|
|
|
if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
|
2008-11-07 03:02:51 +00:00
|
|
|
delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
|
2009-01-06 02:25:51 +00:00
|
|
|
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
/* 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
|
|
|
|
*/
|
2009-09-02 19:22:30 +00:00
|
|
|
free_extent_state(cached_state);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
if (!loops) {
|
|
|
|
unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
|
|
|
|
max_bytes = PAGE_CACHE_SIZE - offset;
|
|
|
|
loops = 1;
|
|
|
|
goto again;
|
|
|
|
} else {
|
|
|
|
found = 0;
|
|
|
|
goto out_failed;
|
|
|
|
}
|
|
|
|
}
|
2012-03-12 15:03:00 +00:00
|
|
|
BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
|
|
|
/* step three, lock the state bits for the whole range */
|
2012-03-01 13:57:19 +00:00
|
|
|
lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
|
|
|
/* then test to make sure it is all still delalloc */
|
|
|
|
ret = test_range_bit(tree, delalloc_start, delalloc_end,
|
2009-09-02 19:22:30 +00:00
|
|
|
EXTENT_DELALLOC, 1, cached_state);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
if (!ret) {
|
2009-09-02 19:22:30 +00:00
|
|
|
unlock_extent_cached(tree, delalloc_start, delalloc_end,
|
|
|
|
&cached_state, GFP_NOFS);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
__unlock_for_delalloc(inode, locked_page,
|
|
|
|
delalloc_start, delalloc_end);
|
|
|
|
cond_resched();
|
|
|
|
goto again;
|
|
|
|
}
|
2009-09-02 19:22:30 +00:00
|
|
|
free_extent_state(cached_state);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
*start = delalloc_start;
|
|
|
|
*end = delalloc_end;
|
|
|
|
out_failed:
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
int extent_clear_unlock_delalloc(struct inode *inode,
|
|
|
|
struct extent_io_tree *tree,
|
|
|
|
u64 start, u64 end, struct page *locked_page,
|
2009-10-08 15:27:10 +00:00
|
|
|
unsigned long op)
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct page *pages[16];
|
|
|
|
unsigned long index = start >> PAGE_CACHE_SHIFT;
|
|
|
|
unsigned long end_index = end >> PAGE_CACHE_SHIFT;
|
|
|
|
unsigned long nr_pages = end_index - index + 1;
|
|
|
|
int i;
|
2008-11-07 03:02:51 +00:00
|
|
|
int clear_bits = 0;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_CLEAR_UNLOCK)
|
2008-11-07 03:02:51 +00:00
|
|
|
clear_bits |= EXTENT_LOCKED;
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_CLEAR_DIRTY)
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
clear_bits |= EXTENT_DIRTY;
|
|
|
|
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_CLEAR_DELALLOC)
|
2008-11-07 03:02:51 +00:00
|
|
|
clear_bits |= EXTENT_DELALLOC;
|
|
|
|
|
2009-09-02 19:04:12 +00:00
|
|
|
clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
|
2009-10-08 17:34:05 +00:00
|
|
|
if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
|
|
|
|
EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
|
|
|
|
EXTENT_SET_PRIVATE2)))
|
2008-11-07 03:02:51 +00:00
|
|
|
return 0;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (nr_pages > 0) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
ret = find_get_pages_contig(inode->i_mapping, index,
|
2008-11-11 14:34:41 +00:00
|
|
|
min_t(unsigned long,
|
|
|
|
nr_pages, ARRAY_SIZE(pages)), pages);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
for (i = 0; i < ret; i++) {
|
2009-09-02 20:53:46 +00:00
|
|
|
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_SET_PRIVATE2)
|
2009-09-02 20:53:46 +00:00
|
|
|
SetPagePrivate2(pages[i]);
|
|
|
|
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
if (pages[i] == locked_page) {
|
|
|
|
page_cache_release(pages[i]);
|
|
|
|
continue;
|
|
|
|
}
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_CLEAR_DIRTY)
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
clear_page_dirty_for_io(pages[i]);
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_SET_WRITEBACK)
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
set_page_writeback(pages[i]);
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_END_WRITEBACK)
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
end_page_writeback(pages[i]);
|
2009-10-08 15:27:10 +00:00
|
|
|
if (op & EXTENT_CLEAR_UNLOCK_PAGE)
|
2008-11-07 03:02:51 +00:00
|
|
|
unlock_page(pages[i]);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
page_cache_release(pages[i]);
|
|
|
|
}
|
|
|
|
nr_pages -= ret;
|
|
|
|
index += ret;
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-29 19:18:18 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2008-01-24 21:13:08 +00:00
|
|
|
u64 count_range_bits(struct extent_io_tree *tree,
|
|
|
|
u64 *start, u64 search_end, u64 max_bytes,
|
2011-02-23 21:23:20 +00:00
|
|
|
unsigned long bits, int contig)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct rb_node *node;
|
|
|
|
struct extent_state *state;
|
|
|
|
u64 cur_start = *start;
|
|
|
|
u64 total_bytes = 0;
|
2011-02-23 21:23:20 +00:00
|
|
|
u64 last = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
if (search_end <= cur_start) {
|
|
|
|
WARN_ON(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
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.
|
|
|
|
*/
|
2008-02-01 19:51:59 +00:00
|
|
|
node = tree_search(tree, cur_start);
|
2009-01-06 02:25:51 +00:00
|
|
|
if (!node)
|
2008-01-24 21:13:08 +00:00
|
|
|
goto out;
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (1) {
|
2008-01-24 21:13:08 +00:00
|
|
|
state = rb_entry(node, struct extent_state, rb_node);
|
|
|
|
if (state->start > search_end)
|
|
|
|
break;
|
2011-02-23 21:23:20 +00:00
|
|
|
if (contig && found && state->start > last + 1)
|
|
|
|
break;
|
|
|
|
if (state->end >= cur_start && (state->state & bits) == bits) {
|
2008-01-24 21:13:08 +00:00
|
|
|
total_bytes += min(search_end, state->end) + 1 -
|
|
|
|
max(cur_start, state->start);
|
|
|
|
if (total_bytes >= max_bytes)
|
|
|
|
break;
|
|
|
|
if (!found) {
|
2011-05-04 15:11:17 +00:00
|
|
|
*start = max(cur_start, state->start);
|
2008-01-24 21:13:08 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
2011-02-23 21:23:20 +00:00
|
|
|
last = state->end;
|
|
|
|
} else if (contig && found) {
|
|
|
|
break;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
node = rb_next(node);
|
|
|
|
if (!node)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
return total_bytes;
|
|
|
|
}
|
2008-12-02 14:54:17 +00:00
|
|
|
|
2008-09-29 19:18:18 +00:00
|
|
|
/*
|
|
|
|
* set the private field for a given byte offset in the tree. If there isn't
|
|
|
|
* an extent_state there already, this does nothing.
|
|
|
|
*/
|
2008-01-24 21:13:08 +00:00
|
|
|
int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
|
|
|
|
{
|
|
|
|
struct rb_node *node;
|
|
|
|
struct extent_state *state;
|
|
|
|
int ret = 0;
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* this search will find all the extents that end after
|
|
|
|
* our range starts.
|
|
|
|
*/
|
2008-02-01 19:51:59 +00:00
|
|
|
node = tree_search(tree, start);
|
2008-04-01 15:21:40 +00:00
|
|
|
if (!node) {
|
2008-01-24 21:13:08 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
state = rb_entry(node, struct extent_state, rb_node);
|
|
|
|
if (state->start != start) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
state->private = private;
|
|
|
|
out:
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
|
|
|
|
{
|
|
|
|
struct rb_node *node;
|
|
|
|
struct extent_state *state;
|
|
|
|
int ret = 0;
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* this search will find all the extents that end after
|
|
|
|
* our range starts.
|
|
|
|
*/
|
2008-02-01 19:51:59 +00:00
|
|
|
node = tree_search(tree, start);
|
2008-04-01 15:21:40 +00:00
|
|
|
if (!node) {
|
2008-01-24 21:13:08 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
state = rb_entry(node, struct extent_state, rb_node);
|
|
|
|
if (state->start != start) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*private = state->private;
|
|
|
|
out:
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* searches a range in the state tree for a given mask.
|
2008-01-29 14:59:12 +00:00
|
|
|
* If 'filled' == 1, this returns 1 only if every extent in the tree
|
2008-01-24 21:13:08 +00:00
|
|
|
* 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,
|
2009-09-02 19:22:30 +00:00
|
|
|
int bits, int filled, struct extent_state *cached)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_state *state = NULL;
|
|
|
|
struct rb_node *node;
|
|
|
|
int bitset = 0;
|
|
|
|
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_lock(&tree->lock);
|
2011-06-20 18:53:48 +00:00
|
|
|
if (cached && cached->tree && cached->start <= start &&
|
|
|
|
cached->end > start)
|
2009-09-02 19:22:30 +00:00
|
|
|
node = &cached->rb_node;
|
|
|
|
else
|
|
|
|
node = tree_search(tree, start);
|
2008-01-24 21:13:08 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-09-24 00:23:16 +00:00
|
|
|
|
|
|
|
if (state->end == (u64)-1)
|
|
|
|
break;
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
start = state->end + 1;
|
|
|
|
if (start > end)
|
|
|
|
break;
|
|
|
|
node = rb_next(node);
|
|
|
|
if (!node) {
|
|
|
|
if (filled)
|
|
|
|
bitset = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-17 19:51:42 +00:00
|
|
|
spin_unlock(&tree->lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
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
|
|
|
|
*/
|
2012-03-01 13:56:26 +00:00
|
|
|
static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
|
|
|
|
u64 end = start + PAGE_CACHE_SIZE - 1;
|
2009-09-02 19:22:30 +00:00
|
|
|
if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
|
2008-01-24 21:13:08 +00:00
|
|
|
SetPageUptodate(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* helper function to unlock a page if all the extents in the tree
|
|
|
|
* for that page are unlocked
|
|
|
|
*/
|
2012-03-01 13:56:26 +00:00
|
|
|
static void check_page_locked(struct extent_io_tree *tree, struct page *page)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
|
|
|
|
u64 end = start + PAGE_CACHE_SIZE - 1;
|
2009-09-02 19:22:30 +00:00
|
|
|
if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
|
2008-01-24 21:13:08 +00:00
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* helper function to end page writeback if all the extents
|
|
|
|
* in the tree for that page are done with writeback
|
|
|
|
*/
|
2012-03-01 13:56:26 +00:00
|
|
|
static void check_page_writeback(struct extent_io_tree *tree,
|
|
|
|
struct page *page)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2009-09-02 17:24:36 +00:00
|
|
|
end_page_writeback(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 13:41:52 +00:00
|
|
|
/*
|
|
|
|
* When IO fails, either with EIO or csum verification fails, we
|
|
|
|
* try other mirrors that might have a good copy of the data. This
|
|
|
|
* io_failure_record is used to record state as we go through all the
|
|
|
|
* mirrors. If another mirror has good data, the page is set up to date
|
|
|
|
* and things continue. If a good mirror can't be found, the original
|
|
|
|
* bio end_io callback is called to indicate things have failed.
|
|
|
|
*/
|
|
|
|
struct io_failure_record {
|
|
|
|
struct page *page;
|
|
|
|
u64 start;
|
|
|
|
u64 len;
|
|
|
|
u64 logical;
|
|
|
|
unsigned long bio_flags;
|
|
|
|
int this_mirror;
|
|
|
|
int failed_mirror;
|
|
|
|
int in_validation;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
|
|
|
|
int did_repair)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int err = 0;
|
|
|
|
struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
|
|
|
|
|
|
|
|
set_state_private(failure_tree, rec->start, 0);
|
|
|
|
ret = clear_extent_bits(failure_tree, rec->start,
|
|
|
|
rec->start + rec->len - 1,
|
|
|
|
EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
|
|
|
|
if (ret)
|
|
|
|
err = ret;
|
|
|
|
|
|
|
|
if (did_repair) {
|
|
|
|
ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
|
|
|
|
rec->start + rec->len - 1,
|
|
|
|
EXTENT_DAMAGED, GFP_NOFS);
|
|
|
|
if (ret && !err)
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(rec);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void repair_io_failure_callback(struct bio *bio, int err)
|
|
|
|
{
|
|
|
|
complete(bio->bi_private);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 synchonization 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_mapping_tree *map_tree, u64 start,
|
|
|
|
u64 length, u64 logical, struct page *page,
|
|
|
|
int mirror_num)
|
|
|
|
{
|
|
|
|
struct bio *bio;
|
|
|
|
struct btrfs_device *dev;
|
|
|
|
DECLARE_COMPLETION_ONSTACK(compl);
|
|
|
|
u64 map_length = 0;
|
|
|
|
u64 sector;
|
|
|
|
struct btrfs_bio *bbio = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
BUG_ON(!mirror_num);
|
|
|
|
|
|
|
|
bio = bio_alloc(GFP_NOFS, 1);
|
|
|
|
if (!bio)
|
|
|
|
return -EIO;
|
|
|
|
bio->bi_private = &compl;
|
|
|
|
bio->bi_end_io = repair_io_failure_callback;
|
|
|
|
bio->bi_size = 0;
|
|
|
|
map_length = length;
|
|
|
|
|
|
|
|
ret = btrfs_map_block(map_tree, WRITE, logical,
|
|
|
|
&map_length, &bbio, mirror_num);
|
|
|
|
if (ret) {
|
|
|
|
bio_put(bio);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
BUG_ON(mirror_num != bbio->mirror_num);
|
|
|
|
sector = bbio->stripes[mirror_num-1].physical >> 9;
|
|
|
|
bio->bi_sector = sector;
|
|
|
|
dev = bbio->stripes[mirror_num-1].dev;
|
|
|
|
kfree(bbio);
|
|
|
|
if (!dev || !dev->bdev || !dev->writeable) {
|
|
|
|
bio_put(bio);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
bio->bi_bdev = dev->bdev;
|
|
|
|
bio_add_page(bio, page, length, start-page_offset(page));
|
2011-11-09 12:44:05 +00:00
|
|
|
btrfsic_submit_bio(WRITE_SYNC, bio);
|
2011-07-22 13:41:52 +00:00
|
|
|
wait_for_completion(&compl);
|
|
|
|
|
|
|
|
if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
|
|
|
|
/* try to remap that extent elsewhere? */
|
|
|
|
bio_put(bio);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
|
|
|
|
"sector %llu)\n", page->mapping->host->i_ino, start,
|
|
|
|
dev->name, sector);
|
|
|
|
|
|
|
|
bio_put(bio);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:57:36 +00:00
|
|
|
int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
|
|
|
|
int mirror_num)
|
|
|
|
{
|
|
|
|
struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
|
|
|
|
u64 start = eb->start;
|
|
|
|
unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
struct page *p = extent_buffer_page(eb, i);
|
|
|
|
ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
|
|
|
|
start, p, mirror_num);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
start += PAGE_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-22 13:41:52 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
static int clean_io_failure(u64 start, struct page *page)
|
|
|
|
{
|
|
|
|
u64 private;
|
|
|
|
u64 private_failure;
|
|
|
|
struct io_failure_record *failrec;
|
|
|
|
struct btrfs_mapping_tree *map_tree;
|
|
|
|
struct extent_state *state;
|
|
|
|
int num_copies;
|
|
|
|
int did_repair = 0;
|
|
|
|
int ret;
|
|
|
|
struct inode *inode = page->mapping->host;
|
|
|
|
|
|
|
|
private = 0;
|
|
|
|
ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
|
|
|
|
(u64)-1, 1, EXTENT_DIRTY, 0);
|
|
|
|
if (!ret)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
|
|
|
|
&private_failure);
|
|
|
|
if (ret)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failrec = (struct io_failure_record *)(unsigned long) private_failure;
|
|
|
|
BUG_ON(!failrec->this_mirror);
|
|
|
|
|
|
|
|
if (failrec->in_validation) {
|
|
|
|
/* there was no real error, just free the record */
|
|
|
|
pr_debug("clean_io_failure: freeing dummy error at %llu\n",
|
|
|
|
failrec->start);
|
|
|
|
did_repair = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&BTRFS_I(inode)->io_tree.lock);
|
|
|
|
state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
|
|
|
|
failrec->start,
|
|
|
|
EXTENT_LOCKED);
|
|
|
|
spin_unlock(&BTRFS_I(inode)->io_tree.lock);
|
|
|
|
|
|
|
|
if (state && state->start == failrec->start) {
|
|
|
|
map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
|
|
|
|
num_copies = btrfs_num_copies(map_tree, failrec->logical,
|
|
|
|
failrec->len);
|
|
|
|
if (num_copies > 1) {
|
|
|
|
ret = repair_io_failure(map_tree, start, failrec->len,
|
|
|
|
failrec->logical, page,
|
|
|
|
failrec->failed_mirror);
|
|
|
|
did_repair = !ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!ret)
|
|
|
|
ret = free_io_failure(inode, failrec, did_repair);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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, struct page *page,
|
|
|
|
u64 start, u64 end, int failed_mirror,
|
|
|
|
struct extent_state *state)
|
|
|
|
{
|
|
|
|
struct io_failure_record *failrec = NULL;
|
|
|
|
u64 private;
|
|
|
|
struct extent_map *em;
|
|
|
|
struct inode *inode = page->mapping->host;
|
|
|
|
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;
|
|
|
|
struct bio *bio;
|
|
|
|
int num_copies;
|
|
|
|
int ret;
|
|
|
|
int read_mode;
|
|
|
|
u64 logical;
|
|
|
|
|
|
|
|
BUG_ON(failed_bio->bi_rw & REQ_WRITE);
|
|
|
|
|
|
|
|
ret = get_state_private(failure_tree, start, &private);
|
|
|
|
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 || IS_ERR(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);
|
|
|
|
}
|
|
|
|
pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
|
|
|
|
"len=%llu\n", 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, GFP_NOFS);
|
|
|
|
if (ret >= 0)
|
|
|
|
ret = set_state_private(failure_tree, start,
|
|
|
|
(u64)(unsigned long)failrec);
|
|
|
|
/* set the bits in the inode's tree */
|
|
|
|
if (ret >= 0)
|
|
|
|
ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
|
|
|
|
GFP_NOFS);
|
|
|
|
if (ret < 0) {
|
|
|
|
kfree(failrec);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
failrec = (struct io_failure_record *)(unsigned long)private;
|
|
|
|
pr_debug("bio_readpage_error: (found) logical=%llu, "
|
|
|
|
"start=%llu, len=%llu, validation=%d\n",
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
num_copies = btrfs_num_copies(
|
|
|
|
&BTRFS_I(inode)->root->fs_info->mapping_tree,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
|
|
|
|
"state=%p, num_copies=%d, next_mirror %d, "
|
|
|
|
"failed_mirror %d\n", state, num_copies,
|
|
|
|
failrec->this_mirror, failed_mirror);
|
|
|
|
free_io_failure(inode, failrec, 0);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
spin_lock(&tree->lock);
|
|
|
|
state = find_first_extent_bit_state(tree, failrec->start,
|
|
|
|
EXTENT_LOCKED);
|
|
|
|
if (state && state->start != failrec->start)
|
|
|
|
state = NULL;
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
read_mode = READ_SYNC | REQ_FAILFAST_DEV;
|
|
|
|
} 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++;
|
|
|
|
read_mode = READ_SYNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state || failrec->this_mirror > num_copies) {
|
|
|
|
pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
|
|
|
|
"next_mirror %d, failed_mirror %d\n", state,
|
|
|
|
num_copies, failrec->this_mirror, failed_mirror);
|
|
|
|
free_io_failure(inode, failrec, 0);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio = bio_alloc(GFP_NOFS, 1);
|
|
|
|
bio->bi_private = state;
|
|
|
|
bio->bi_end_io = failed_bio->bi_end_io;
|
|
|
|
bio->bi_sector = failrec->logical >> 9;
|
|
|
|
bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
|
|
|
|
bio->bi_size = 0;
|
|
|
|
|
|
|
|
bio_add_page(bio, page, failrec->len, start - page_offset(page));
|
|
|
|
|
|
|
|
pr_debug("bio_readpage_error: submitting new read[%#x] to "
|
|
|
|
"this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
|
|
|
|
failrec->this_mirror, num_copies, failrec->in_validation);
|
|
|
|
|
2012-02-16 01:11:40 +00:00
|
|
|
ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
|
|
|
|
failrec->this_mirror,
|
|
|
|
failrec->bio_flags, 0);
|
|
|
|
return ret;
|
2011-07-22 13:41:52 +00:00
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/* lots and lots of room for performance fixes in the end_bio funcs */
|
|
|
|
|
2012-02-15 15:23:57 +00:00
|
|
|
int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
|
|
|
|
{
|
|
|
|
int uptodate = (err == 0);
|
|
|
|
struct extent_io_tree *tree;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
tree = &BTRFS_I(page->mapping->host)->io_tree;
|
|
|
|
|
|
|
|
if (tree->ops && tree->ops->writepage_end_io_hook) {
|
|
|
|
ret = tree->ops->writepage_end_io_hook(page, start,
|
|
|
|
end, NULL, uptodate);
|
|
|
|
if (ret)
|
|
|
|
uptodate = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uptodate && tree->ops &&
|
|
|
|
tree->ops->writepage_io_failed_hook) {
|
|
|
|
ret = tree->ops->writepage_io_failed_hook(NULL, page,
|
|
|
|
start, end, NULL);
|
|
|
|
/* Writeback already completed */
|
|
|
|
if (ret == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uptodate) {
|
|
|
|
clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
|
|
|
|
ClearPageUptodate(page);
|
|
|
|
SetPageError(page);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* 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 err)
|
|
|
|
{
|
|
|
|
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
|
2008-08-20 12:51:49 +00:00
|
|
|
struct extent_io_tree *tree;
|
2008-01-24 21:13:08 +00:00
|
|
|
u64 start;
|
|
|
|
u64 end;
|
|
|
|
int whole_page;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct page *page = bvec->bv_page;
|
2008-08-20 12:51:49 +00:00
|
|
|
tree = &BTRFS_I(page->mapping->host)->io_tree;
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
start = ((u64)page->index << PAGE_CACHE_SHIFT) +
|
|
|
|
bvec->bv_offset;
|
|
|
|
end = start + bvec->bv_len - 1;
|
|
|
|
|
|
|
|
if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
|
|
|
|
whole_page = 1;
|
|
|
|
else
|
|
|
|
whole_page = 0;
|
|
|
|
|
|
|
|
if (--bvec >= bio->bi_io_vec)
|
|
|
|
prefetchw(&bvec->bv_page->flags);
|
2008-05-12 17:39:03 +00:00
|
|
|
|
2012-02-15 15:23:57 +00:00
|
|
|
if (end_extent_writepage(page, err, start, end))
|
|
|
|
continue;
|
2008-01-29 14:59:12 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
if (whole_page)
|
|
|
|
end_page_writeback(page);
|
|
|
|
else
|
|
|
|
check_page_writeback(tree, page);
|
|
|
|
} while (bvec >= bio->bi_io_vec);
|
2008-09-24 15:48:04 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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, int err)
|
|
|
|
{
|
|
|
|
int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
|
2010-02-03 18:18:45 +00:00
|
|
|
struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
|
|
|
|
struct bio_vec *bvec = bio->bi_io_vec;
|
2008-08-20 12:51:49 +00:00
|
|
|
struct extent_io_tree *tree;
|
2008-01-24 21:13:08 +00:00
|
|
|
u64 start;
|
|
|
|
u64 end;
|
|
|
|
int whole_page;
|
2012-03-27 01:57:36 +00:00
|
|
|
int failed_mirror;
|
2008-01-24 21:13:08 +00:00
|
|
|
int ret;
|
|
|
|
|
Btrfs: move data checksumming into a dedicated tree
Btrfs stores checksums for each data block. Until now, they have
been stored in the subvolume trees, indexed by the inode that is
referencing the data block. This means that when we read the inode,
we've probably read in at least some checksums as well.
But, this has a few problems:
* The checksums are indexed by logical offset in the file. When
compression is on, this means we have to do the expensive checksumming
on the uncompressed data. It would be faster if we could checksum
the compressed data instead.
* If we implement encryption, we'll be checksumming the plain text and
storing that on disk. This is significantly less secure.
* For either compression or encryption, we have to get the plain text
back before we can verify the checksum as correct. This makes the raid
layer balancing and extent moving much more expensive.
* It makes the front end caching code more complex, as we have touch
the subvolume and inodes as we cache extents.
* There is potentitally one copy of the checksum in each subvolume
referencing an extent.
The solution used here is to store the extent checksums in a dedicated
tree. This allows us to index the checksums by phyiscal extent
start and length. It means:
* The checksum is against the data stored on disk, after any compression
or encryption is done.
* The checksum is stored in a central location, and can be verified without
following back references, or reading inodes.
This makes compression significantly faster by reducing the amount of
data that needs to be checksummed. It will also allow much faster
raid management code in general.
The checksums are indexed by a key with a fixed objectid (a magic value
in ctree.h) and offset set to the starting byte of the extent. This
allows us to copy the checksum items into the fsync log tree directly (or
any other tree), without having to invent a second format for them.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-12-08 21:58:54 +00:00
|
|
|
if (err)
|
|
|
|
uptodate = 0;
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
do {
|
|
|
|
struct page *page = bvec->bv_page;
|
2011-04-06 10:02:20 +00:00
|
|
|
struct extent_state *cached = NULL;
|
|
|
|
struct extent_state *state;
|
|
|
|
|
2011-07-22 13:41:52 +00:00
|
|
|
pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
|
|
|
|
"mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
|
|
|
|
(long int)bio->bi_bdev);
|
2008-08-20 12:51:49 +00:00
|
|
|
tree = &BTRFS_I(page->mapping->host)->io_tree;
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
start = ((u64)page->index << PAGE_CACHE_SHIFT) +
|
|
|
|
bvec->bv_offset;
|
|
|
|
end = start + bvec->bv_len - 1;
|
|
|
|
|
|
|
|
if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
|
|
|
|
whole_page = 1;
|
|
|
|
else
|
|
|
|
whole_page = 0;
|
|
|
|
|
2010-02-03 18:18:45 +00:00
|
|
|
if (++bvec <= bvec_end)
|
2008-01-24 21:13:08 +00:00
|
|
|
prefetchw(&bvec->bv_page->flags);
|
|
|
|
|
2011-04-06 10:02:20 +00:00
|
|
|
spin_lock(&tree->lock);
|
2011-04-16 10:55:39 +00:00
|
|
|
state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
|
2011-04-12 17:57:39 +00:00
|
|
|
if (state && state->start == start) {
|
2011-04-06 10:02:20 +00:00
|
|
|
/*
|
|
|
|
* take a reference on the state, unlock will drop
|
|
|
|
* the ref
|
|
|
|
*/
|
|
|
|
cache_state(state, &cached);
|
|
|
|
}
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
|
2008-01-29 14:59:12 +00:00
|
|
|
ret = tree->ops->readpage_end_io_hook(page, start, end,
|
2011-04-06 10:02:20 +00:00
|
|
|
state);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (ret)
|
|
|
|
uptodate = 0;
|
2011-07-22 13:41:52 +00:00
|
|
|
else
|
|
|
|
clean_io_failure(start, page);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
2012-03-27 01:57:36 +00:00
|
|
|
|
|
|
|
if (!uptodate)
|
2011-11-20 12:33:38 +00:00
|
|
|
failed_mirror = (int)(unsigned long)bio->bi_bdev;
|
2012-03-27 01:57:36 +00:00
|
|
|
|
|
|
|
if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
|
|
|
|
ret = tree->ops->readpage_io_failed_hook(page, failed_mirror);
|
|
|
|
if (!ret && !err &&
|
|
|
|
test_bit(BIO_UPTODATE, &bio->bi_flags))
|
|
|
|
uptodate = 1;
|
|
|
|
} else if (!uptodate) {
|
2011-12-01 14:30:36 +00:00
|
|
|
/*
|
|
|
|
* 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, page, start, end,
|
|
|
|
failed_mirror, NULL);
|
2008-04-09 20:28:12 +00:00
|
|
|
if (ret == 0) {
|
2008-04-17 15:29:12 +00:00
|
|
|
uptodate =
|
|
|
|
test_bit(BIO_UPTODATE, &bio->bi_flags);
|
Btrfs: move data checksumming into a dedicated tree
Btrfs stores checksums for each data block. Until now, they have
been stored in the subvolume trees, indexed by the inode that is
referencing the data block. This means that when we read the inode,
we've probably read in at least some checksums as well.
But, this has a few problems:
* The checksums are indexed by logical offset in the file. When
compression is on, this means we have to do the expensive checksumming
on the uncompressed data. It would be faster if we could checksum
the compressed data instead.
* If we implement encryption, we'll be checksumming the plain text and
storing that on disk. This is significantly less secure.
* For either compression or encryption, we have to get the plain text
back before we can verify the checksum as correct. This makes the raid
layer balancing and extent moving much more expensive.
* It makes the front end caching code more complex, as we have touch
the subvolume and inodes as we cache extents.
* There is potentitally one copy of the checksum in each subvolume
referencing an extent.
The solution used here is to store the extent checksums in a dedicated
tree. This allows us to index the checksums by phyiscal extent
start and length. It means:
* The checksum is against the data stored on disk, after any compression
or encryption is done.
* The checksum is stored in a central location, and can be verified without
following back references, or reading inodes.
This makes compression significantly faster by reducing the amount of
data that needs to be checksummed. It will also allow much faster
raid management code in general.
The checksums are indexed by a key with a fixed objectid (a magic value
in ctree.h) and offset set to the starting byte of the extent. This
allows us to copy the checksum items into the fsync log tree directly (or
any other tree), without having to invent a second format for them.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-12-08 21:58:54 +00:00
|
|
|
if (err)
|
|
|
|
uptodate = 0;
|
2011-04-06 10:02:20 +00:00
|
|
|
uncache_state(&cached);
|
2008-04-09 20:28:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
if (uptodate && tree->track_uptodate) {
|
2011-04-06 10:02:20 +00:00
|
|
|
set_extent_uptodate(tree, start, end, &cached,
|
2008-08-20 12:51:49 +00:00
|
|
|
GFP_ATOMIC);
|
2008-11-07 03:02:51 +00:00
|
|
|
}
|
2011-04-06 10:02:20 +00:00
|
|
|
unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2008-01-29 14:59:12 +00:00
|
|
|
if (whole_page) {
|
|
|
|
if (uptodate) {
|
|
|
|
SetPageUptodate(page);
|
|
|
|
} else {
|
|
|
|
ClearPageUptodate(page);
|
|
|
|
SetPageError(page);
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
unlock_page(page);
|
2008-01-29 14:59:12 +00:00
|
|
|
} else {
|
|
|
|
if (uptodate) {
|
|
|
|
check_page_uptodate(tree, page);
|
|
|
|
} else {
|
|
|
|
ClearPageUptodate(page);
|
|
|
|
SetPageError(page);
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
check_page_locked(tree, page);
|
2008-01-29 14:59:12 +00:00
|
|
|
}
|
2010-02-03 18:18:45 +00:00
|
|
|
} while (bvec <= bvec_end);
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
2010-11-22 03:02:55 +00:00
|
|
|
struct bio *
|
|
|
|
btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
|
|
|
|
gfp_t gfp_flags)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct bio *bio;
|
|
|
|
|
|
|
|
bio = bio_alloc(gfp_flags, nr_vecs);
|
|
|
|
|
|
|
|
if (bio == NULL && (current->flags & PF_MEMALLOC)) {
|
|
|
|
while (!bio && (nr_vecs /= 2))
|
|
|
|
bio = bio_alloc(gfp_flags, nr_vecs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bio) {
|
2008-04-22 17:26:46 +00:00
|
|
|
bio->bi_size = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
bio->bi_bdev = bdev;
|
|
|
|
bio->bi_sector = first_sector;
|
|
|
|
}
|
|
|
|
return bio;
|
|
|
|
}
|
|
|
|
|
2012-03-12 15:03:00 +00:00
|
|
|
/*
|
|
|
|
* Since writes are async, they will only return -ENOMEM.
|
|
|
|
* Reads can return the full range of I/O error conditions.
|
|
|
|
*/
|
2011-10-04 03:23:14 +00:00
|
|
|
static int __must_check submit_one_bio(int rw, struct bio *bio,
|
|
|
|
int mirror_num, unsigned long bio_flags)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2008-01-29 14:59:12 +00:00
|
|
|
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 = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
|
|
|
|
|
2008-08-20 12:51:49 +00:00
|
|
|
bio->bi_private = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
bio_get(bio);
|
|
|
|
|
2008-02-20 17:07:25 +00:00
|
|
|
if (tree->ops && tree->ops->submit_bio_hook)
|
2011-01-26 06:21:39 +00:00
|
|
|
ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
|
2010-05-25 13:48:28 +00:00
|
|
|
mirror_num, bio_flags, start);
|
2008-03-24 19:01:56 +00:00
|
|
|
else
|
2011-11-09 12:44:05 +00:00
|
|
|
btrfsic_submit_bio(rw, bio);
|
2011-07-22 13:41:52 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
if (bio_flagged(bio, BIO_EOPNOTSUPP))
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
bio_put(bio);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-04 03:23:13 +00:00
|
|
|
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 && tree->ops->merge_bio_hook)
|
|
|
|
ret = tree->ops->merge_bio_hook(page, offset, size, bio,
|
|
|
|
bio_flags);
|
|
|
|
BUG_ON(ret < 0);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
static int submit_extent_page(int rw, struct extent_io_tree *tree,
|
|
|
|
struct page *page, sector_t sector,
|
|
|
|
size_t size, unsigned long offset,
|
|
|
|
struct block_device *bdev,
|
|
|
|
struct bio **bio_ret,
|
|
|
|
unsigned long max_pages,
|
2008-04-09 20:28:12 +00:00
|
|
|
bio_end_io_t end_io_func,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
int mirror_num,
|
|
|
|
unsigned long prev_bio_flags,
|
|
|
|
unsigned long bio_flags)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct bio *bio;
|
|
|
|
int nr;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
int contig = 0;
|
|
|
|
int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
|
|
|
|
int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
|
2008-11-11 14:34:41 +00:00
|
|
|
size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
if (bio_ret && *bio_ret) {
|
|
|
|
bio = *bio_ret;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
if (old_compressed)
|
|
|
|
contig = bio->bi_sector == sector;
|
|
|
|
else
|
|
|
|
contig = bio->bi_sector + (bio->bi_size >> 9) ==
|
|
|
|
sector;
|
|
|
|
|
|
|
|
if (prev_bio_flags != bio_flags || !contig ||
|
2011-10-04 03:23:13 +00:00
|
|
|
merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
bio_add_page(bio, page, page_size, offset) < page_size) {
|
|
|
|
ret = submit_one_bio(rw, bio, mirror_num,
|
|
|
|
prev_bio_flags);
|
2012-03-12 15:03:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2008-01-24 21:13:08 +00:00
|
|
|
bio = NULL;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
if (this_compressed)
|
|
|
|
nr = BIO_MAX_PAGES;
|
|
|
|
else
|
|
|
|
nr = bio_get_nr_vecs(bdev);
|
|
|
|
|
2010-11-22 03:02:55 +00:00
|
|
|
bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
|
2011-02-01 09:17:35 +00:00
|
|
|
if (!bio)
|
|
|
|
return -ENOMEM;
|
2008-01-29 14:59:12 +00:00
|
|
|
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
bio_add_page(bio, page, page_size, offset);
|
2008-01-24 21:13:08 +00:00
|
|
|
bio->bi_end_io = end_io_func;
|
|
|
|
bio->bi_private = tree;
|
2008-01-29 14:59:12 +00:00
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
if (bio_ret)
|
2008-01-24 21:13:08 +00:00
|
|
|
*bio_ret = bio;
|
2009-01-06 02:25:51 +00:00
|
|
|
else
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-07 21:20:05 +00:00
|
|
|
void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
if (!PagePrivate(page)) {
|
|
|
|
SetPagePrivate(page);
|
|
|
|
page_cache_get(page);
|
2012-03-07 21:20:05 +00:00
|
|
|
set_page_private(page, (unsigned long)eb);
|
|
|
|
} else {
|
|
|
|
WARN_ON(page->private != (unsigned long)eb);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 21:20:05 +00:00
|
|
|
void set_page_extent_mapped(struct page *page)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2012-03-07 21:20:05 +00:00
|
|
|
if (!PagePrivate(page)) {
|
|
|
|
SetPagePrivate(page);
|
|
|
|
page_cache_get(page);
|
|
|
|
set_page_private(page, EXTENT_PAGE_PRIVATE);
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2012-03-12 15:03:00 +00:00
|
|
|
* XXX JDM: This needs looking at to ensure proper page locking
|
2008-01-24 21:13:08 +00:00
|
|
|
*/
|
|
|
|
static int __extent_read_full_page(struct extent_io_tree *tree,
|
|
|
|
struct page *page,
|
|
|
|
get_extent_t *get_extent,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
struct bio **bio, int mirror_num,
|
|
|
|
unsigned long *bio_flags)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct inode *inode = page->mapping->host;
|
|
|
|
u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
|
|
|
|
u64 page_end = start + PAGE_CACHE_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;
|
2010-05-23 15:07:21 +00:00
|
|
|
struct btrfs_ordered_extent *ordered;
|
2008-01-24 21:13:08 +00:00
|
|
|
int ret;
|
|
|
|
int nr = 0;
|
2011-04-19 12:29:38 +00:00
|
|
|
size_t pg_offset = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
size_t iosize;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
size_t disk_io_size;
|
2008-01-24 21:13:08 +00:00
|
|
|
size_t blocksize = inode->i_sb->s_blocksize;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
unsigned long this_bio_flag = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
set_page_extent_mapped(page);
|
|
|
|
|
2011-05-26 16:01:56 +00:00
|
|
|
if (!PageUptodate(page)) {
|
|
|
|
if (cleancache_get_page(page) == 0) {
|
|
|
|
BUG_ON(blocksize != PAGE_SIZE);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
end = page_end;
|
2010-05-23 15:07:21 +00:00
|
|
|
while (1) {
|
2012-03-01 13:57:19 +00:00
|
|
|
lock_extent(tree, start, end);
|
2010-05-23 15:07:21 +00:00
|
|
|
ordered = btrfs_lookup_ordered_extent(inode, start);
|
|
|
|
if (!ordered)
|
|
|
|
break;
|
2012-03-01 13:57:19 +00:00
|
|
|
unlock_extent(tree, start, end);
|
2010-05-23 15:07:21 +00:00
|
|
|
btrfs_start_ordered_extent(inode, ordered, 1);
|
|
|
|
btrfs_put_ordered_extent(ordered);
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
|
|
|
|
char *userpage;
|
|
|
|
size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
|
|
|
|
|
|
|
|
if (zero_offset) {
|
|
|
|
iosize = PAGE_CACHE_SIZE - zero_offset;
|
|
|
|
userpage = kmap_atomic(page, KM_USER0);
|
|
|
|
memset(userpage + zero_offset, 0, iosize);
|
|
|
|
flush_dcache_page(page);
|
|
|
|
kunmap_atomic(userpage, KM_USER0);
|
|
|
|
}
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
while (cur <= end) {
|
|
|
|
if (cur >= last_byte) {
|
|
|
|
char *userpage;
|
2011-04-06 10:02:20 +00:00
|
|
|
struct extent_state *cached = NULL;
|
|
|
|
|
2011-04-19 12:29:38 +00:00
|
|
|
iosize = PAGE_CACHE_SIZE - pg_offset;
|
2008-01-24 21:13:08 +00:00
|
|
|
userpage = kmap_atomic(page, KM_USER0);
|
2011-04-19 12:29:38 +00:00
|
|
|
memset(userpage + pg_offset, 0, iosize);
|
2008-01-24 21:13:08 +00:00
|
|
|
flush_dcache_page(page);
|
|
|
|
kunmap_atomic(userpage, KM_USER0);
|
|
|
|
set_extent_uptodate(tree, cur, cur + iosize - 1,
|
2011-04-06 10:02:20 +00:00
|
|
|
&cached, GFP_NOFS);
|
|
|
|
unlock_extent_cached(tree, cur, cur + iosize - 1,
|
|
|
|
&cached, GFP_NOFS);
|
2008-01-24 21:13:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-04-19 12:29:38 +00:00
|
|
|
em = get_extent(inode, page, pg_offset, cur,
|
2008-01-24 21:13:08 +00:00
|
|
|
end - cur + 1, 0);
|
2011-04-19 16:00:01 +00:00
|
|
|
if (IS_ERR_OR_NULL(em)) {
|
2008-01-24 21:13:08 +00:00
|
|
|
SetPageError(page);
|
2012-03-01 13:57:19 +00:00
|
|
|
unlock_extent(tree, cur, end);
|
2008-01-24 21:13:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extent_offset = cur - em->start;
|
|
|
|
BUG_ON(extent_map_end(em) <= cur);
|
|
|
|
BUG_ON(end < cur);
|
|
|
|
|
2010-12-17 06:21:50 +00:00
|
|
|
if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
this_bio_flag = EXTENT_BIO_COMPRESSED;
|
2010-12-17 06:21:50 +00:00
|
|
|
extent_set_compress_type(&this_bio_flag,
|
|
|
|
em->compress_type);
|
|
|
|
}
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
iosize = min(extent_map_end(em) - cur, end - cur + 1);
|
|
|
|
cur_end = min(extent_map_end(em) - 1, end);
|
|
|
|
iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
bdev = em->bdev;
|
|
|
|
block_start = em->block_start;
|
2008-10-30 18:25:28 +00:00
|
|
|
if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
|
|
|
|
block_start = EXTENT_MAP_HOLE;
|
2008-01-24 21:13:08 +00:00
|
|
|
free_extent_map(em);
|
|
|
|
em = NULL;
|
|
|
|
|
|
|
|
/* we've found a hole, just zero and go on */
|
|
|
|
if (block_start == EXTENT_MAP_HOLE) {
|
|
|
|
char *userpage;
|
2011-04-06 10:02:20 +00:00
|
|
|
struct extent_state *cached = NULL;
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
userpage = kmap_atomic(page, KM_USER0);
|
2011-04-19 12:29:38 +00:00
|
|
|
memset(userpage + pg_offset, 0, iosize);
|
2008-01-24 21:13:08 +00:00
|
|
|
flush_dcache_page(page);
|
|
|
|
kunmap_atomic(userpage, KM_USER0);
|
|
|
|
|
|
|
|
set_extent_uptodate(tree, cur, cur + iosize - 1,
|
2011-04-06 10:02:20 +00:00
|
|
|
&cached, GFP_NOFS);
|
|
|
|
unlock_extent_cached(tree, cur, cur + iosize - 1,
|
|
|
|
&cached, GFP_NOFS);
|
2008-01-24 21:13:08 +00:00
|
|
|
cur = cur + iosize;
|
2011-04-19 12:29:38 +00:00
|
|
|
pg_offset += iosize;
|
2008-01-24 21:13:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* the get_extent function already copied into the page */
|
2009-09-02 19:22:30 +00:00
|
|
|
if (test_range_bit(tree, cur, cur_end,
|
|
|
|
EXTENT_UPTODATE, 1, NULL)) {
|
2008-09-05 20:09:51 +00:00
|
|
|
check_page_uptodate(tree, page);
|
2012-03-01 13:57:19 +00:00
|
|
|
unlock_extent(tree, cur, cur + iosize - 1);
|
2008-01-24 21:13:08 +00:00
|
|
|
cur = cur + iosize;
|
2011-04-19 12:29:38 +00:00
|
|
|
pg_offset += iosize;
|
2008-01-24 21:13:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-29 14:59:12 +00:00
|
|
|
/* we have an inline extent but it didn't get marked up
|
|
|
|
* to date. Error out
|
|
|
|
*/
|
|
|
|
if (block_start == EXTENT_MAP_INLINE) {
|
|
|
|
SetPageError(page);
|
2012-03-01 13:57:19 +00:00
|
|
|
unlock_extent(tree, cur, cur + iosize - 1);
|
2008-01-29 14:59:12 +00:00
|
|
|
cur = cur + iosize;
|
2011-04-19 12:29:38 +00:00
|
|
|
pg_offset += iosize;
|
2008-01-29 14:59:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
if (tree->ops && tree->ops->readpage_io_hook) {
|
|
|
|
ret = tree->ops->readpage_io_hook(page, cur,
|
|
|
|
cur + iosize - 1);
|
|
|
|
}
|
|
|
|
if (!ret) {
|
2008-07-24 13:41:53 +00:00
|
|
|
unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
|
|
|
|
pnr -= page->index;
|
2008-01-24 21:13:08 +00:00
|
|
|
ret = submit_extent_page(READ, tree, page,
|
2011-04-19 12:29:38 +00:00
|
|
|
sector, disk_io_size, pg_offset,
|
2008-07-24 13:41:53 +00:00
|
|
|
bdev, bio, pnr,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
end_bio_extent_readpage, mirror_num,
|
|
|
|
*bio_flags,
|
|
|
|
this_bio_flag);
|
2012-03-12 15:03:00 +00:00
|
|
|
BUG_ON(ret == -ENOMEM);
|
2008-07-24 13:41:53 +00:00
|
|
|
nr++;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
*bio_flags = this_bio_flag;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
SetPageError(page);
|
|
|
|
cur = cur + iosize;
|
2011-04-19 12:29:38 +00:00
|
|
|
pg_offset += iosize;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
2011-05-26 16:01:56 +00:00
|
|
|
out:
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!nr) {
|
|
|
|
if (!PageError(page))
|
|
|
|
SetPageUptodate(page);
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
|
2011-06-13 18:02:58 +00:00
|
|
|
get_extent_t *get_extent, int mirror_num)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct bio *bio = NULL;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
unsigned long bio_flags = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2011-06-13 18:02:58 +00:00
|
|
|
ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
&bio_flags);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (bio)
|
2011-06-13 18:02:58 +00:00
|
|
|
ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
|
2008-01-24 21:13:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
static noinline void update_nr_written(struct page *page,
|
|
|
|
struct writeback_control *wbc,
|
|
|
|
unsigned long nr_written)
|
|
|
|
{
|
|
|
|
wbc->nr_to_write -= nr_written;
|
|
|
|
if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
|
|
|
|
wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
|
|
|
|
page->mapping->writeback_index = page->index + nr_written;
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* the writepage semantics are similar to regular writepage. extent
|
|
|
|
* records are inserted to lock ranges in the tree, and as dirty areas
|
|
|
|
* are found, they are marked writeback. Then the lock bits are removed
|
|
|
|
* and the end_io handler clears the writeback ranges
|
|
|
|
*/
|
|
|
|
static int __extent_writepage(struct page *page, struct writeback_control *wbc,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct inode *inode = page->mapping->host;
|
|
|
|
struct extent_page_data *epd = data;
|
|
|
|
struct extent_io_tree *tree = epd->tree;
|
|
|
|
u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
|
|
|
|
u64 delalloc_start;
|
|
|
|
u64 page_end = start + PAGE_CACHE_SIZE - 1;
|
|
|
|
u64 end;
|
|
|
|
u64 cur = start;
|
|
|
|
u64 extent_offset;
|
|
|
|
u64 last_byte = i_size_read(inode);
|
|
|
|
u64 block_start;
|
|
|
|
u64 iosize;
|
|
|
|
sector_t sector;
|
2009-09-02 19:04:12 +00:00
|
|
|
struct extent_state *cached_state = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct extent_map *em;
|
|
|
|
struct block_device *bdev;
|
|
|
|
int ret;
|
|
|
|
int nr = 0;
|
2008-07-18 16:01:11 +00:00
|
|
|
size_t pg_offset = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
size_t blocksize;
|
|
|
|
loff_t i_size = i_size_read(inode);
|
|
|
|
unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
|
|
|
|
u64 nr_delalloc;
|
|
|
|
u64 delalloc_end;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
int page_started;
|
|
|
|
int compressed;
|
2009-04-20 19:50:09 +00:00
|
|
|
int write_flags;
|
2008-11-07 03:02:51 +00:00
|
|
|
unsigned long nr_written = 0;
|
2011-08-01 16:08:18 +00:00
|
|
|
bool fill_delalloc = true;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
if (wbc->sync_mode == WB_SYNC_ALL)
|
2011-03-09 10:56:30 +00:00
|
|
|
write_flags = WRITE_SYNC;
|
2009-04-20 19:50:09 +00:00
|
|
|
else
|
|
|
|
write_flags = WRITE;
|
|
|
|
|
Btrfs: add initial tracepoint support for btrfs
Tracepoints can provide insight into why btrfs hits bugs and be greatly
helpful for debugging, e.g
dd-7822 [000] 2121.641088: btrfs_inode_request: root = 5(FS_TREE), gen = 4, ino = 256, blocks = 8, disk_i_size = 0, last_trans = 8, logged_trans = 0
dd-7822 [000] 2121.641100: btrfs_inode_new: root = 5(FS_TREE), gen = 8, ino = 257, blocks = 0, disk_i_size = 0, last_trans = 0, logged_trans = 0
btrfs-transacti-7804 [001] 2146.935420: btrfs_cow_block: root = 2(EXTENT_TREE), refs = 2, orig_buf = 29368320 (orig_level = 0), cow_buf = 29388800 (cow_level = 0)
btrfs-transacti-7804 [001] 2146.935473: btrfs_cow_block: root = 1(ROOT_TREE), refs = 2, orig_buf = 29364224 (orig_level = 0), cow_buf = 29392896 (cow_level = 0)
btrfs-transacti-7804 [001] 2146.972221: btrfs_transaction_commit: root = 1(ROOT_TREE), gen = 8
flush-btrfs-2-7821 [001] 2155.824210: btrfs_chunk_alloc: root = 3(CHUNK_TREE), offset = 1103101952, size = 1073741824, num_stripes = 1, sub_stripes = 0, type = DATA
flush-btrfs-2-7821 [001] 2155.824241: btrfs_cow_block: root = 2(EXTENT_TREE), refs = 2, orig_buf = 29388800 (orig_level = 0), cow_buf = 29396992 (cow_level = 0)
flush-btrfs-2-7821 [001] 2155.824255: btrfs_cow_block: root = 4(DEV_TREE), refs = 2, orig_buf = 29372416 (orig_level = 0), cow_buf = 29401088 (cow_level = 0)
flush-btrfs-2-7821 [000] 2155.824329: btrfs_cow_block: root = 3(CHUNK_TREE), refs = 2, orig_buf = 20971520 (orig_level = 0), cow_buf = 20975616 (cow_level = 0)
btrfs-endio-wri-7800 [001] 2155.898019: btrfs_cow_block: root = 5(FS_TREE), refs = 2, orig_buf = 29384704 (orig_level = 0), cow_buf = 29405184 (cow_level = 0)
btrfs-endio-wri-7800 [001] 2155.898043: btrfs_cow_block: root = 7(CSUM_TREE), refs = 2, orig_buf = 29376512 (orig_level = 0), cow_buf = 29409280 (cow_level = 0)
Here is what I have added:
1) ordere_extent:
btrfs_ordered_extent_add
btrfs_ordered_extent_remove
btrfs_ordered_extent_start
btrfs_ordered_extent_put
These provide critical information to understand how ordered_extents are
updated.
2) extent_map:
btrfs_get_extent
extent_map is used in both read and write cases, and it is useful for tracking
how btrfs specific IO is running.
3) writepage:
__extent_writepage
btrfs_writepage_end_io_hook
Pages are cirtical resourses and produce a lot of corner cases during writeback,
so it is valuable to know how page is written to disk.
4) inode:
btrfs_inode_new
btrfs_inode_request
btrfs_inode_evict
These can show where and when a inode is created, when a inode is evicted.
5) sync:
btrfs_sync_file
btrfs_sync_fs
These show sync arguments.
6) transaction:
btrfs_transaction_commit
In transaction based filesystem, it will be useful to know the generation and
who does commit.
7) back reference and cow:
btrfs_delayed_tree_ref
btrfs_delayed_data_ref
btrfs_delayed_ref_head
btrfs_cow_block
Btrfs natively supports back references, these tracepoints are helpful on
understanding btrfs's COW mechanism.
8) chunk:
btrfs_chunk_alloc
btrfs_chunk_free
Chunk is a link between physical offset and logical offset, and stands for space
infomation in btrfs, and these are helpful on tracing space things.
9) reserved_extent:
btrfs_reserved_extent_alloc
btrfs_reserved_extent_free
These can show how btrfs uses its space.
Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2011-03-24 11:18:59 +00:00
|
|
|
trace___extent_writepage(page, inode, wbc);
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
WARN_ON(!PageLocked(page));
|
2011-11-04 16:29:37 +00:00
|
|
|
|
|
|
|
ClearPageError(page);
|
|
|
|
|
2008-07-18 16:01:11 +00:00
|
|
|
pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
|
2008-05-15 13:13:45 +00:00
|
|
|
if (page->index > end_index ||
|
2008-07-18 16:01:11 +00:00
|
|
|
(page->index == end_index && !pg_offset)) {
|
2008-11-10 16:50:50 +00:00
|
|
|
page->mapping->a_ops->invalidatepage(page, 0);
|
2008-01-24 21:13:08 +00:00
|
|
|
unlock_page(page);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page->index == end_index) {
|
|
|
|
char *userpage;
|
|
|
|
|
|
|
|
userpage = kmap_atomic(page, KM_USER0);
|
2008-07-18 16:01:11 +00:00
|
|
|
memset(userpage + pg_offset, 0,
|
|
|
|
PAGE_CACHE_SIZE - pg_offset);
|
2008-01-24 21:13:08 +00:00
|
|
|
kunmap_atomic(userpage, KM_USER0);
|
2008-05-15 13:13:45 +00:00
|
|
|
flush_dcache_page(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
2008-07-18 16:01:11 +00:00
|
|
|
pg_offset = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
set_page_extent_mapped(page);
|
|
|
|
|
2011-08-01 16:08:18 +00:00
|
|
|
if (!tree->ops || !tree->ops->fill_delalloc)
|
|
|
|
fill_delalloc = false;
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
delalloc_start = start;
|
|
|
|
delalloc_end = 0;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
page_started = 0;
|
2011-08-01 16:08:18 +00:00
|
|
|
if (!epd->extent_locked && fill_delalloc) {
|
2009-09-18 20:03:16 +00:00
|
|
|
u64 delalloc_to_write = 0;
|
2009-04-20 19:50:09 +00:00
|
|
|
/*
|
|
|
|
* make sure the wbc mapping index is at least updated
|
|
|
|
* to this page.
|
|
|
|
*/
|
|
|
|
update_nr_written(page, wbc, 0);
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (delalloc_end < page_end) {
|
2008-11-07 03:02:51 +00:00
|
|
|
nr_delalloc = find_lock_delalloc_range(inode, tree,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
page,
|
|
|
|
&delalloc_start,
|
2008-01-24 21:13:08 +00:00
|
|
|
&delalloc_end,
|
|
|
|
128 * 1024 * 1024);
|
2008-11-07 03:02:51 +00:00
|
|
|
if (nr_delalloc == 0) {
|
|
|
|
delalloc_start = delalloc_end + 1;
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-16 01:11:40 +00:00
|
|
|
ret = tree->ops->fill_delalloc(inode, page,
|
|
|
|
delalloc_start,
|
|
|
|
delalloc_end,
|
|
|
|
&page_started,
|
|
|
|
&nr_written);
|
2012-03-12 15:03:00 +00:00
|
|
|
/* File system has been set read-only */
|
|
|
|
if (ret) {
|
|
|
|
SetPageError(page);
|
|
|
|
goto done;
|
|
|
|
}
|
2009-09-18 20:03:16 +00:00
|
|
|
/*
|
|
|
|
* delalloc_end is already one less than the total
|
|
|
|
* length, so we don't subtract one from
|
|
|
|
* PAGE_CACHE_SIZE
|
|
|
|
*/
|
|
|
|
delalloc_to_write += (delalloc_end - delalloc_start +
|
|
|
|
PAGE_CACHE_SIZE) >>
|
|
|
|
PAGE_CACHE_SHIFT;
|
2008-01-24 21:13:08 +00:00
|
|
|
delalloc_start = delalloc_end + 1;
|
|
|
|
}
|
2009-09-18 20:03:16 +00:00
|
|
|
if (wbc->nr_to_write < delalloc_to_write) {
|
|
|
|
int thresh = 8192;
|
|
|
|
|
|
|
|
if (delalloc_to_write < thresh * 2)
|
|
|
|
thresh = delalloc_to_write;
|
|
|
|
wbc->nr_to_write = min_t(u64, delalloc_to_write,
|
|
|
|
thresh);
|
|
|
|
}
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
2008-11-07 03:02:51 +00:00
|
|
|
/* did the fill delalloc function already unlock and start
|
|
|
|
* the IO?
|
|
|
|
*/
|
|
|
|
if (page_started) {
|
|
|
|
ret = 0;
|
2009-04-20 19:50:09 +00:00
|
|
|
/*
|
|
|
|
* we've unlocked the page, so we can't update
|
|
|
|
* the mapping's writeback index, just update
|
|
|
|
* nr_to_write.
|
|
|
|
*/
|
|
|
|
wbc->nr_to_write -= nr_written;
|
|
|
|
goto done_unlocked;
|
2008-11-07 03:02:51 +00:00
|
|
|
}
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
}
|
2008-07-17 16:53:51 +00:00
|
|
|
if (tree->ops && tree->ops->writepage_start_hook) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
ret = tree->ops->writepage_start_hook(page, start,
|
|
|
|
page_end);
|
2012-02-15 15:23:57 +00:00
|
|
|
if (ret) {
|
|
|
|
/* Fixup worker will requeue */
|
|
|
|
if (ret == -EBUSY)
|
|
|
|
wbc->pages_skipped++;
|
|
|
|
else
|
|
|
|
redirty_page_for_writepage(wbc, page);
|
2009-04-20 19:50:09 +00:00
|
|
|
update_nr_written(page, wbc, nr_written);
|
2008-07-17 16:53:51 +00:00
|
|
|
unlock_page(page);
|
2008-11-07 03:02:51 +00:00
|
|
|
ret = 0;
|
2009-04-20 19:50:09 +00:00
|
|
|
goto done_unlocked;
|
2008-07-17 16:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
/*
|
|
|
|
* we don't want to touch the inode after unlocking the page,
|
|
|
|
* so we update the mapping writeback index now
|
|
|
|
*/
|
|
|
|
update_nr_written(page, wbc, nr_written + 1);
|
2008-11-07 03:02:51 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
end = page_end;
|
|
|
|
if (last_byte <= start) {
|
2008-07-17 16:53:50 +00:00
|
|
|
if (tree->ops && tree->ops->writepage_end_io_hook)
|
|
|
|
tree->ops->writepage_end_io_hook(page, start,
|
|
|
|
page_end, NULL, 1);
|
2008-01-24 21:13:08 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
blocksize = inode->i_sb->s_blocksize;
|
|
|
|
|
|
|
|
while (cur <= end) {
|
|
|
|
if (cur >= last_byte) {
|
2008-07-17 16:53:50 +00:00
|
|
|
if (tree->ops && tree->ops->writepage_end_io_hook)
|
|
|
|
tree->ops->writepage_end_io_hook(page, cur,
|
|
|
|
page_end, NULL, 1);
|
2008-01-24 21:13:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-07-18 16:01:11 +00:00
|
|
|
em = epd->get_extent(inode, page, pg_offset, cur,
|
2008-01-24 21:13:08 +00:00
|
|
|
end - cur + 1, 1);
|
2011-04-19 16:00:01 +00:00
|
|
|
if (IS_ERR_OR_NULL(em)) {
|
2008-01-24 21:13:08 +00:00
|
|
|
SetPageError(page);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
extent_offset = cur - em->start;
|
|
|
|
BUG_ON(extent_map_end(em) <= cur);
|
|
|
|
BUG_ON(end < cur);
|
|
|
|
iosize = min(extent_map_end(em) - cur, end - cur + 1);
|
|
|
|
iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
|
|
|
|
sector = (em->block_start + extent_offset) >> 9;
|
|
|
|
bdev = em->bdev;
|
|
|
|
block_start = em->block_start;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
|
2008-01-24 21:13:08 +00:00
|
|
|
free_extent_map(em);
|
|
|
|
em = NULL;
|
|
|
|
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
/*
|
|
|
|
* compressed and inline extents are written through other
|
|
|
|
* paths in the FS
|
|
|
|
*/
|
|
|
|
if (compressed || block_start == EXTENT_MAP_HOLE ||
|
2008-01-24 21:13:08 +00:00
|
|
|
block_start == EXTENT_MAP_INLINE) {
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
/*
|
|
|
|
* end_io notification does not happen here for
|
|
|
|
* compressed extents
|
|
|
|
*/
|
|
|
|
if (!compressed && tree->ops &&
|
|
|
|
tree->ops->writepage_end_io_hook)
|
2008-07-17 16:53:50 +00:00
|
|
|
tree->ops->writepage_end_io_hook(page, cur,
|
|
|
|
cur + iosize - 1,
|
|
|
|
NULL, 1);
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
else if (compressed) {
|
|
|
|
/* we don't want to end_page_writeback on
|
|
|
|
* a compressed extent. this happens
|
|
|
|
* elsewhere
|
|
|
|
*/
|
|
|
|
nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur += iosize;
|
2008-07-18 16:01:11 +00:00
|
|
|
pg_offset += iosize;
|
2008-01-24 21:13:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* leave this out until we have a page_mkwrite call */
|
|
|
|
if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
|
2009-09-02 19:22:30 +00:00
|
|
|
EXTENT_DIRTY, 0, NULL)) {
|
2008-01-24 21:13:08 +00:00
|
|
|
cur = cur + iosize;
|
2008-07-18 16:01:11 +00:00
|
|
|
pg_offset += iosize;
|
2008-01-24 21:13:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
if (tree->ops && tree->ops->writepage_io_hook) {
|
|
|
|
ret = tree->ops->writepage_io_hook(page, cur,
|
|
|
|
cur + iosize - 1);
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
2008-05-12 17:39:03 +00:00
|
|
|
if (ret) {
|
2008-01-24 21:13:08 +00:00
|
|
|
SetPageError(page);
|
2008-05-12 17:39:03 +00:00
|
|
|
} else {
|
2008-01-24 21:13:08 +00:00
|
|
|
unsigned long max_nr = end_index + 1;
|
2008-07-18 16:01:11 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
set_range_writeback(tree, cur, cur + iosize - 1);
|
|
|
|
if (!PageWriteback(page)) {
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs warning page %lu not "
|
|
|
|
"writeback, cur %llu end %llu\n",
|
|
|
|
page->index, (unsigned long long)cur,
|
2008-01-24 21:13:08 +00:00
|
|
|
(unsigned long long)end);
|
|
|
|
}
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
ret = submit_extent_page(write_flags, tree, page,
|
|
|
|
sector, iosize, pg_offset,
|
|
|
|
bdev, &epd->bio, max_nr,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
end_bio_extent_writepage,
|
|
|
|
0, 0, 0);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (ret)
|
|
|
|
SetPageError(page);
|
|
|
|
}
|
|
|
|
cur = cur + iosize;
|
2008-07-18 16:01:11 +00:00
|
|
|
pg_offset += iosize;
|
2008-01-24 21:13:08 +00:00
|
|
|
nr++;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if (nr == 0) {
|
|
|
|
/* make sure the mapping tag for page dirty gets cleared */
|
|
|
|
set_page_writeback(page);
|
|
|
|
end_page_writeback(page);
|
|
|
|
}
|
|
|
|
unlock_page(page);
|
2008-11-07 03:02:51 +00:00
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
done_unlocked:
|
|
|
|
|
2009-09-02 19:04:12 +00:00
|
|
|
/* drop our reference on any cached states */
|
|
|
|
free_extent_state(cached_state);
|
2008-01-24 21:13:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
static int eb_wait(void *word)
|
|
|
|
{
|
|
|
|
io_schedule();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lock_extent_buffer_for_io(struct extent_buffer *eb,
|
|
|
|
struct btrfs_fs_info *fs_info,
|
|
|
|
struct extent_page_data *epd)
|
|
|
|
{
|
|
|
|
unsigned long i, num_pages;
|
|
|
|
int flush = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!btrfs_try_tree_write_lock(eb)) {
|
|
|
|
flush = 1;
|
|
|
|
flush_write_bio(epd);
|
|
|
|
btrfs_tree_lock(eb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
|
|
|
|
btrfs_tree_unlock(eb);
|
|
|
|
if (!epd->sync_io)
|
|
|
|
return 0;
|
|
|
|
if (!flush) {
|
|
|
|
flush_write_bio(epd);
|
|
|
|
flush = 1;
|
|
|
|
}
|
2012-03-21 16:09:56 +00:00
|
|
|
while (1) {
|
|
|
|
wait_on_extent_buffer_writeback(eb);
|
|
|
|
btrfs_tree_lock(eb);
|
|
|
|
if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
|
|
|
|
break;
|
2012-03-13 13:38:00 +00:00
|
|
|
btrfs_tree_unlock(eb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
|
|
|
|
set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
|
|
|
|
btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
|
|
|
|
spin_lock(&fs_info->delalloc_lock);
|
|
|
|
if (fs_info->dirty_metadata_bytes >= eb->len)
|
|
|
|
fs_info->dirty_metadata_bytes -= eb->len;
|
|
|
|
else
|
|
|
|
WARN_ON(1);
|
|
|
|
spin_unlock(&fs_info->delalloc_lock);
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
btrfs_tree_unlock(eb);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
struct page *p = extent_buffer_page(eb, i);
|
|
|
|
|
|
|
|
if (!trylock_page(p)) {
|
|
|
|
if (!flush) {
|
|
|
|
flush_write_bio(epd);
|
|
|
|
flush = 1;
|
|
|
|
}
|
|
|
|
lock_page(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void end_extent_buffer_writeback(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
|
|
|
|
smp_mb__after_clear_bit();
|
|
|
|
wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
|
|
|
|
{
|
|
|
|
int uptodate = err == 0;
|
|
|
|
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
|
|
|
|
struct extent_buffer *eb;
|
|
|
|
int done;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct page *page = bvec->bv_page;
|
|
|
|
|
|
|
|
bvec--;
|
|
|
|
eb = (struct extent_buffer *)page->private;
|
|
|
|
BUG_ON(!eb);
|
|
|
|
done = atomic_dec_and_test(&eb->io_pages);
|
|
|
|
|
|
|
|
if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
|
|
|
|
set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
|
|
|
|
ClearPageUptodate(page);
|
|
|
|
SetPageError(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
end_page_writeback(page);
|
|
|
|
|
|
|
|
if (!done)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
end_extent_buffer_writeback(eb);
|
|
|
|
} while (bvec >= bio->bi_io_vec);
|
|
|
|
|
|
|
|
bio_put(bio);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_one_eb(struct extent_buffer *eb,
|
|
|
|
struct btrfs_fs_info *fs_info,
|
|
|
|
struct writeback_control *wbc,
|
|
|
|
struct extent_page_data *epd)
|
|
|
|
{
|
|
|
|
struct block_device *bdev = fs_info->fs_devices->latest_bdev;
|
|
|
|
u64 offset = eb->start;
|
|
|
|
unsigned long i, num_pages;
|
|
|
|
int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
|
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
|
|
|
atomic_set(&eb->io_pages, num_pages);
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
struct page *p = extent_buffer_page(eb, i);
|
|
|
|
|
|
|
|
clear_page_dirty_for_io(p);
|
|
|
|
set_page_writeback(p);
|
|
|
|
ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
|
|
|
|
PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
|
|
|
|
-1, end_bio_extent_buffer_writepage,
|
|
|
|
0, 0, 0);
|
|
|
|
if (ret) {
|
|
|
|
set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
|
|
|
|
SetPageError(p);
|
|
|
|
if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
|
|
|
|
end_extent_buffer_writeback(eb);
|
|
|
|
ret = -EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += PAGE_CACHE_SIZE;
|
|
|
|
update_nr_written(p, wbc, 1);
|
|
|
|
unlock_page(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(ret)) {
|
|
|
|
for (; i < num_pages; i++) {
|
|
|
|
struct page *p = extent_buffer_page(eb, i);
|
|
|
|
unlock_page(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int btree_write_cache_pages(struct address_space *mapping,
|
|
|
|
struct writeback_control *wbc)
|
|
|
|
{
|
|
|
|
struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
|
|
|
|
struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
|
|
|
|
struct extent_buffer *eb, *prev_eb = NULL;
|
|
|
|
struct extent_page_data epd = {
|
|
|
|
.bio = NULL,
|
|
|
|
.tree = tree,
|
|
|
|
.extent_locked = 0,
|
|
|
|
.sync_io = wbc->sync_mode == WB_SYNC_ALL,
|
|
|
|
};
|
|
|
|
int ret = 0;
|
|
|
|
int done = 0;
|
|
|
|
int nr_to_write_done = 0;
|
|
|
|
struct pagevec pvec;
|
|
|
|
int nr_pages;
|
|
|
|
pgoff_t index;
|
|
|
|
pgoff_t end; /* Inclusive */
|
|
|
|
int scanned = 0;
|
|
|
|
int tag;
|
|
|
|
|
|
|
|
pagevec_init(&pvec, 0);
|
|
|
|
if (wbc->range_cyclic) {
|
|
|
|
index = mapping->writeback_index; /* Start from prev offset */
|
|
|
|
end = -1;
|
|
|
|
} else {
|
|
|
|
index = wbc->range_start >> PAGE_CACHE_SHIFT;
|
|
|
|
end = wbc->range_end >> PAGE_CACHE_SHIFT;
|
|
|
|
scanned = 1;
|
|
|
|
}
|
|
|
|
if (wbc->sync_mode == WB_SYNC_ALL)
|
|
|
|
tag = PAGECACHE_TAG_TOWRITE;
|
|
|
|
else
|
|
|
|
tag = PAGECACHE_TAG_DIRTY;
|
|
|
|
retry:
|
|
|
|
if (wbc->sync_mode == WB_SYNC_ALL)
|
|
|
|
tag_pages_for_writeback(mapping, index, end);
|
|
|
|
while (!done && !nr_to_write_done && (index <= end) &&
|
|
|
|
(nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
|
|
|
|
min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
scanned = 1;
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
struct page *page = pvec.pages[i];
|
|
|
|
|
|
|
|
if (!PagePrivate(page))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!wbc->range_cyclic && page->index > end) {
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
eb = (struct extent_buffer *)page->private;
|
|
|
|
if (!eb) {
|
|
|
|
WARN_ON(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eb == prev_eb)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!atomic_inc_not_zero(&eb->refs)) {
|
|
|
|
WARN_ON(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_eb = eb;
|
|
|
|
ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
|
|
|
|
if (!ret) {
|
|
|
|
free_extent_buffer(eb);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write_one_eb(eb, fs_info, wbc, &epd);
|
|
|
|
if (ret) {
|
|
|
|
done = 1;
|
|
|
|
free_extent_buffer(eb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free_extent_buffer(eb);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the filesystem may choose to bump up nr_to_write.
|
|
|
|
* We have to make sure to honor the new nr_to_write
|
|
|
|
* at any time
|
|
|
|
*/
|
|
|
|
nr_to_write_done = wbc->nr_to_write <= 0;
|
|
|
|
}
|
|
|
|
pagevec_release(&pvec);
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
if (!scanned && !done) {
|
|
|
|
/*
|
|
|
|
* We hit the last page and there is more work to be done: wrap
|
|
|
|
* back to the start of the file
|
|
|
|
*/
|
|
|
|
scanned = 1;
|
|
|
|
index = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
flush_write_bio(&epd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/**
|
2008-09-08 15:18:08 +00:00
|
|
|
* write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
|
2008-01-24 21:13:08 +00:00
|
|
|
* @mapping: address space structure to write
|
|
|
|
* @wbc: subtract the number of written pages from *@wbc->nr_to_write
|
|
|
|
* @writepage: function called for each page
|
|
|
|
* @data: data passed to writepage function
|
|
|
|
*
|
|
|
|
* If a page is already under I/O, write_cache_pages() skips it, even
|
|
|
|
* if it's dirty. This is desirable behaviour for memory-cleaning writeback,
|
|
|
|
* but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
|
|
|
|
* and msync() need to guarantee that all the data which was dirty at the time
|
|
|
|
* the call was made get new I/O started against them. If wbc->sync_mode is
|
|
|
|
* WB_SYNC_ALL then we were called for data integrity and we must wait for
|
|
|
|
* existing IO to complete.
|
|
|
|
*/
|
2008-12-02 14:54:17 +00:00
|
|
|
static int extent_write_cache_pages(struct extent_io_tree *tree,
|
2008-09-08 15:18:08 +00:00
|
|
|
struct address_space *mapping,
|
|
|
|
struct writeback_control *wbc,
|
2008-11-19 17:44:22 +00:00
|
|
|
writepage_t writepage, void *data,
|
|
|
|
void (*flush_fn)(void *))
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int done = 0;
|
2009-09-18 20:03:16 +00:00
|
|
|
int nr_to_write_done = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct pagevec pvec;
|
|
|
|
int nr_pages;
|
|
|
|
pgoff_t index;
|
|
|
|
pgoff_t end; /* Inclusive */
|
|
|
|
int scanned = 0;
|
2011-07-15 21:26:38 +00:00
|
|
|
int tag;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
pagevec_init(&pvec, 0);
|
|
|
|
if (wbc->range_cyclic) {
|
|
|
|
index = mapping->writeback_index; /* Start from prev offset */
|
|
|
|
end = -1;
|
|
|
|
} else {
|
|
|
|
index = wbc->range_start >> PAGE_CACHE_SHIFT;
|
|
|
|
end = wbc->range_end >> PAGE_CACHE_SHIFT;
|
|
|
|
scanned = 1;
|
|
|
|
}
|
2011-07-15 21:26:38 +00:00
|
|
|
if (wbc->sync_mode == WB_SYNC_ALL)
|
|
|
|
tag = PAGECACHE_TAG_TOWRITE;
|
|
|
|
else
|
|
|
|
tag = PAGECACHE_TAG_DIRTY;
|
2008-01-24 21:13:08 +00:00
|
|
|
retry:
|
2011-07-15 21:26:38 +00:00
|
|
|
if (wbc->sync_mode == WB_SYNC_ALL)
|
|
|
|
tag_pages_for_writeback(mapping, index, end);
|
2009-09-18 20:03:16 +00:00
|
|
|
while (!done && !nr_to_write_done && (index <= end) &&
|
2011-07-15 21:26:38 +00:00
|
|
|
(nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
|
|
|
|
min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
|
2008-01-24 21:13:08 +00:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
scanned = 1;
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
struct page *page = pvec.pages[i];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we hold neither mapping->tree_lock nor
|
|
|
|
* lock on the page itself: the page may be truncated or
|
|
|
|
* invalidated (changing page->mapping to NULL), or even
|
|
|
|
* swizzled back from swapper_space to tmpfs file
|
|
|
|
* mapping
|
|
|
|
*/
|
2011-11-01 14:08:06 +00:00
|
|
|
if (tree->ops &&
|
|
|
|
tree->ops->write_cache_pages_lock_hook) {
|
|
|
|
tree->ops->write_cache_pages_lock_hook(page,
|
|
|
|
data, flush_fn);
|
|
|
|
} else {
|
|
|
|
if (!trylock_page(page)) {
|
|
|
|
flush_fn(data);
|
|
|
|
lock_page(page);
|
|
|
|
}
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
if (unlikely(page->mapping != mapping)) {
|
|
|
|
unlock_page(page);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wbc->range_cyclic && page->index > end) {
|
|
|
|
done = 1;
|
|
|
|
unlock_page(page);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-19 17:44:22 +00:00
|
|
|
if (wbc->sync_mode != WB_SYNC_NONE) {
|
2008-11-20 15:46:35 +00:00
|
|
|
if (PageWriteback(page))
|
|
|
|
flush_fn(data);
|
2008-01-24 21:13:08 +00:00
|
|
|
wait_on_page_writeback(page);
|
2008-11-19 17:44:22 +00:00
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
if (PageWriteback(page) ||
|
|
|
|
!clear_page_dirty_for_io(page)) {
|
|
|
|
unlock_page(page);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (*writepage)(page, wbc, data);
|
|
|
|
|
|
|
|
if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
|
|
|
|
unlock_page(page);
|
|
|
|
ret = 0;
|
|
|
|
}
|
2009-09-18 20:03:16 +00:00
|
|
|
if (ret)
|
2008-01-24 21:13:08 +00:00
|
|
|
done = 1;
|
2009-09-18 20:03:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* the filesystem may choose to bump up nr_to_write.
|
|
|
|
* We have to make sure to honor the new nr_to_write
|
|
|
|
* at any time
|
|
|
|
*/
|
|
|
|
nr_to_write_done = wbc->nr_to_write <= 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
pagevec_release(&pvec);
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
if (!scanned && !done) {
|
|
|
|
/*
|
|
|
|
* We hit the last page and there is more work to be done: wrap
|
|
|
|
* back to the start of the file
|
|
|
|
*/
|
|
|
|
scanned = 1;
|
|
|
|
index = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
static void flush_epd_write_bio(struct extent_page_data *epd)
|
2008-11-19 17:44:22 +00:00
|
|
|
{
|
|
|
|
if (epd->bio) {
|
2011-10-04 03:23:14 +00:00
|
|
|
int rw = WRITE;
|
|
|
|
int ret;
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
if (epd->sync_io)
|
2011-10-04 03:23:14 +00:00
|
|
|
rw = WRITE_SYNC;
|
|
|
|
|
|
|
|
ret = submit_one_bio(rw, epd->bio, 0, 0);
|
2012-03-12 15:03:00 +00:00
|
|
|
BUG_ON(ret < 0); /* -ENOMEM */
|
2008-11-19 17:44:22 +00:00
|
|
|
epd->bio = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
static noinline void flush_write_bio(void *data)
|
|
|
|
{
|
|
|
|
struct extent_page_data *epd = data;
|
|
|
|
flush_epd_write_bio(epd);
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
|
|
|
|
get_extent_t *get_extent,
|
|
|
|
struct writeback_control *wbc)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct extent_page_data epd = {
|
|
|
|
.bio = NULL,
|
|
|
|
.tree = tree,
|
|
|
|
.get_extent = get_extent,
|
2008-11-07 03:02:51 +00:00
|
|
|
.extent_locked = 0,
|
2009-04-20 19:50:09 +00:00
|
|
|
.sync_io = wbc->sync_mode == WB_SYNC_ALL,
|
2008-01-24 21:13:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = __extent_writepage(page, wbc, &epd);
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
flush_epd_write_bio(&epd);
|
2008-01-24 21:13:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-07 03:02:51 +00:00
|
|
|
int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
|
|
|
|
u64 start, u64 end, get_extent_t *get_extent,
|
|
|
|
int mode)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct address_space *mapping = inode->i_mapping;
|
|
|
|
struct page *page;
|
|
|
|
unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
|
|
|
|
PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
struct extent_page_data epd = {
|
|
|
|
.bio = NULL,
|
|
|
|
.tree = tree,
|
|
|
|
.get_extent = get_extent,
|
|
|
|
.extent_locked = 1,
|
2009-04-20 19:50:09 +00:00
|
|
|
.sync_io = mode == WB_SYNC_ALL,
|
2008-11-07 03:02:51 +00:00
|
|
|
};
|
|
|
|
struct writeback_control wbc_writepages = {
|
|
|
|
.sync_mode = mode,
|
|
|
|
.nr_to_write = nr_pages * 2,
|
|
|
|
.range_start = start,
|
|
|
|
.range_end = end + 1,
|
|
|
|
};
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (start <= end) {
|
2008-11-07 03:02:51 +00:00
|
|
|
page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
|
|
|
|
if (clear_page_dirty_for_io(page))
|
|
|
|
ret = __extent_writepage(page, &wbc_writepages, &epd);
|
|
|
|
else {
|
|
|
|
if (tree->ops && tree->ops->writepage_end_io_hook)
|
|
|
|
tree->ops->writepage_end_io_hook(page, start,
|
|
|
|
start + PAGE_CACHE_SIZE - 1,
|
|
|
|
NULL, 1);
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
page_cache_release(page);
|
|
|
|
start += PAGE_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
|
2009-04-20 19:50:09 +00:00
|
|
|
flush_epd_write_bio(&epd);
|
2008-11-07 03:02:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
int extent_writepages(struct extent_io_tree *tree,
|
|
|
|
struct address_space *mapping,
|
|
|
|
get_extent_t *get_extent,
|
|
|
|
struct writeback_control *wbc)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct extent_page_data epd = {
|
|
|
|
.bio = NULL,
|
|
|
|
.tree = tree,
|
|
|
|
.get_extent = get_extent,
|
2008-11-07 03:02:51 +00:00
|
|
|
.extent_locked = 0,
|
2009-04-20 19:50:09 +00:00
|
|
|
.sync_io = wbc->sync_mode == WB_SYNC_ALL,
|
2008-01-24 21:13:08 +00:00
|
|
|
};
|
|
|
|
|
2008-09-08 15:18:08 +00:00
|
|
|
ret = extent_write_cache_pages(tree, mapping, wbc,
|
2008-11-19 17:44:22 +00:00
|
|
|
__extent_writepage, &epd,
|
|
|
|
flush_write_bio);
|
2009-04-20 19:50:09 +00:00
|
|
|
flush_epd_write_bio(&epd);
|
2008-01-24 21:13:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int extent_readpages(struct extent_io_tree *tree,
|
|
|
|
struct address_space *mapping,
|
|
|
|
struct list_head *pages, unsigned nr_pages,
|
|
|
|
get_extent_t get_extent)
|
|
|
|
{
|
|
|
|
struct bio *bio = NULL;
|
|
|
|
unsigned page_idx;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
unsigned long bio_flags = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
for (page_idx = 0; page_idx < nr_pages; page_idx++) {
|
|
|
|
struct page *page = list_entry(pages->prev, struct page, lru);
|
|
|
|
|
|
|
|
prefetchw(&page->flags);
|
|
|
|
list_del(&page->lru);
|
2010-03-17 13:31:04 +00:00
|
|
|
if (!add_to_page_cache_lru(page, mapping,
|
2011-04-25 23:43:51 +00:00
|
|
|
page->index, GFP_NOFS)) {
|
2008-04-09 20:28:12 +00:00
|
|
|
__extent_read_full_page(tree, page, get_extent,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
&bio, 0, &bio_flags);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
page_cache_release(page);
|
|
|
|
}
|
|
|
|
BUG_ON(!list_empty(pages));
|
|
|
|
if (bio)
|
2012-03-12 15:03:00 +00:00
|
|
|
return submit_one_bio(READ, bio, 0, bio_flags);
|
2008-01-24 21:13:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* basic invalidatepage code, this waits on any locked or writeback
|
|
|
|
* ranges corresponding to the page, and then deletes any extent state
|
|
|
|
* records from the tree
|
|
|
|
*/
|
|
|
|
int extent_invalidatepage(struct extent_io_tree *tree,
|
|
|
|
struct page *page, unsigned long offset)
|
|
|
|
{
|
2010-02-03 19:33:23 +00:00
|
|
|
struct extent_state *cached_state = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
|
|
|
|
u64 end = start + PAGE_CACHE_SIZE - 1;
|
|
|
|
size_t blocksize = page->mapping->host->i_sb->s_blocksize;
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
start += (offset + blocksize - 1) & ~(blocksize - 1);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (start > end)
|
|
|
|
return 0;
|
|
|
|
|
2012-03-01 13:57:19 +00:00
|
|
|
lock_extent_bits(tree, start, end, 0, &cached_state);
|
2009-09-02 17:24:36 +00:00
|
|
|
wait_on_page_writeback(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
clear_extent_bit(tree, start, end,
|
2009-10-08 17:34:05 +00:00
|
|
|
EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
|
|
|
|
EXTENT_DO_ACCOUNTING,
|
2010-02-03 19:33:23 +00:00
|
|
|
1, 1, &cached_state, GFP_NOFS);
|
2008-01-24 21:13:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-18 14:29:50 +00:00
|
|
|
/*
|
|
|
|
* a helper for releasepage, this tests for areas of the page that
|
|
|
|
* are locked or under IO and drops the related state bits if it is safe
|
|
|
|
* to drop the page.
|
|
|
|
*/
|
|
|
|
int try_release_extent_state(struct extent_map_tree *map,
|
|
|
|
struct extent_io_tree *tree, struct page *page,
|
|
|
|
gfp_t mask)
|
|
|
|
{
|
|
|
|
u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
|
|
|
|
u64 end = start + PAGE_CACHE_SIZE - 1;
|
|
|
|
int ret = 1;
|
|
|
|
|
2008-07-18 15:56:15 +00:00
|
|
|
if (test_range_bit(tree, start, end,
|
2009-09-02 20:53:46 +00:00
|
|
|
EXTENT_IOBITS, 0, NULL))
|
2008-04-18 14:29:50 +00:00
|
|
|
ret = 0;
|
|
|
|
else {
|
|
|
|
if ((mask & GFP_NOFS) == GFP_NOFS)
|
|
|
|
mask = GFP_NOFS;
|
2009-09-24 00:28:46 +00:00
|
|
|
/*
|
|
|
|
* at this point we can safely clear everything except the
|
|
|
|
* locked bit and the nodatasum bit
|
|
|
|
*/
|
2011-02-14 17:52:08 +00:00
|
|
|
ret = clear_extent_bit(tree, start, end,
|
2009-09-24 00:28:46 +00:00
|
|
|
~(EXTENT_LOCKED | EXTENT_NODATASUM),
|
|
|
|
0, 0, NULL, mask);
|
2011-02-14 17:52:08 +00:00
|
|
|
|
|
|
|
/* if clear_extent_bit failed for enomem reasons,
|
|
|
|
* we can't allow the release to continue.
|
|
|
|
*/
|
|
|
|
if (ret < 0)
|
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = 1;
|
2008-04-18 14:29:50 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
/*
|
|
|
|
* a helper for releasepage. As long as there are no locked extents
|
|
|
|
* in the range corresponding to the page, both state records and extent
|
|
|
|
* map records are removed
|
|
|
|
*/
|
|
|
|
int try_release_extent_mapping(struct extent_map_tree *map,
|
2008-01-29 14:59:12 +00:00
|
|
|
struct extent_io_tree *tree, struct page *page,
|
|
|
|
gfp_t mask)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_map *em;
|
|
|
|
u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
|
|
|
|
u64 end = start + PAGE_CACHE_SIZE - 1;
|
2008-04-18 14:29:50 +00:00
|
|
|
|
2008-01-29 14:59:12 +00:00
|
|
|
if ((mask & __GFP_WAIT) &&
|
|
|
|
page->mapping->host->i_size > 16 * 1024 * 1024) {
|
2008-02-15 15:40:50 +00:00
|
|
|
u64 len;
|
2008-01-29 14:59:12 +00:00
|
|
|
while (start <= end) {
|
2008-02-15 15:40:50 +00:00
|
|
|
len = end - start + 1;
|
2009-09-02 20:24:52 +00:00
|
|
|
write_lock(&map->lock);
|
2008-02-15 15:40:50 +00:00
|
|
|
em = lookup_extent_mapping(map, start, len);
|
2012-02-16 07:23:58 +00:00
|
|
|
if (!em) {
|
2009-09-02 20:24:52 +00:00
|
|
|
write_unlock(&map->lock);
|
2008-01-29 14:59:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-07-18 16:01:11 +00:00
|
|
|
if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
|
|
|
|
em->start != start) {
|
2009-09-02 20:24:52 +00:00
|
|
|
write_unlock(&map->lock);
|
2008-01-29 14:59:12 +00:00
|
|
|
free_extent_map(em);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!test_range_bit(tree, em->start,
|
|
|
|
extent_map_end(em) - 1,
|
2009-09-02 20:53:46 +00:00
|
|
|
EXTENT_LOCKED | EXTENT_WRITEBACK,
|
2009-09-02 19:22:30 +00:00
|
|
|
0, NULL)) {
|
2008-01-29 14:59:12 +00:00
|
|
|
remove_extent_mapping(map, em);
|
|
|
|
/* once for the rb tree */
|
|
|
|
free_extent_map(em);
|
|
|
|
}
|
|
|
|
start = extent_map_end(em);
|
2009-09-02 20:24:52 +00:00
|
|
|
write_unlock(&map->lock);
|
2008-01-29 14:59:12 +00:00
|
|
|
|
|
|
|
/* once for us */
|
2008-01-24 21:13:08 +00:00
|
|
|
free_extent_map(em);
|
|
|
|
}
|
|
|
|
}
|
2008-04-18 14:29:50 +00:00
|
|
|
return try_release_extent_state(map, tree, page, mask);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 21:23:20 +00:00
|
|
|
/*
|
|
|
|
* helper function for fiemap, which doesn't want to see any holes.
|
|
|
|
* This maps until we find something past 'last'
|
|
|
|
*/
|
|
|
|
static struct extent_map *get_extent_skip_holes(struct inode *inode,
|
|
|
|
u64 offset,
|
|
|
|
u64 last,
|
|
|
|
get_extent_t *get_extent)
|
|
|
|
{
|
|
|
|
u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
|
|
|
|
struct extent_map *em;
|
|
|
|
u64 len;
|
|
|
|
|
|
|
|
if (offset >= last)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
len = last - offset;
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
len = (len + sectorsize - 1) & ~(sectorsize - 1);
|
|
|
|
em = get_extent(inode, NULL, 0, offset, len, 0);
|
2011-04-19 16:00:01 +00:00
|
|
|
if (IS_ERR_OR_NULL(em))
|
2011-02-23 21:23:20 +00:00
|
|
|
return em;
|
|
|
|
|
|
|
|
/* if this isn't a hole return it */
|
|
|
|
if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
|
|
|
|
em->block_start != EXTENT_MAP_HOLE) {
|
|
|
|
return em;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is a hole, advance to the next extent */
|
|
|
|
offset = extent_map_end(em);
|
|
|
|
free_extent_map(em);
|
|
|
|
if (offset >= last)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-21 19:39:14 +00:00
|
|
|
int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
|
|
|
|
__u64 start, __u64 len, get_extent_t *get_extent)
|
|
|
|
{
|
2010-11-23 19:36:57 +00:00
|
|
|
int ret = 0;
|
2009-01-21 19:39:14 +00:00
|
|
|
u64 off = start;
|
|
|
|
u64 max = start + len;
|
|
|
|
u32 flags = 0;
|
2010-11-23 19:36:57 +00:00
|
|
|
u32 found_type;
|
|
|
|
u64 last;
|
2011-02-23 21:23:20 +00:00
|
|
|
u64 last_for_get_extent = 0;
|
2009-01-21 19:39:14 +00:00
|
|
|
u64 disko = 0;
|
2011-02-23 21:23:20 +00:00
|
|
|
u64 isize = i_size_read(inode);
|
2010-11-23 19:36:57 +00:00
|
|
|
struct btrfs_key found_key;
|
2009-01-21 19:39:14 +00:00
|
|
|
struct extent_map *em = NULL;
|
2010-02-03 19:33:23 +00:00
|
|
|
struct extent_state *cached_state = NULL;
|
2010-11-23 19:36:57 +00:00
|
|
|
struct btrfs_path *path;
|
|
|
|
struct btrfs_file_extent_item *item;
|
2009-01-21 19:39:14 +00:00
|
|
|
int end = 0;
|
2011-02-23 21:23:20 +00:00
|
|
|
u64 em_start = 0;
|
|
|
|
u64 em_len = 0;
|
|
|
|
u64 em_end = 0;
|
2009-01-21 19:39:14 +00:00
|
|
|
unsigned long emflags;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-11-23 19:36:57 +00:00
|
|
|
path = btrfs_alloc_path();
|
|
|
|
if (!path)
|
|
|
|
return -ENOMEM;
|
|
|
|
path->leave_spinning = 1;
|
|
|
|
|
2011-11-17 16:34:31 +00:00
|
|
|
start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
|
|
|
|
len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
|
|
|
|
|
2011-02-23 21:23:20 +00:00
|
|
|
/*
|
|
|
|
* lookup the last file extent. We're not using i_size here
|
|
|
|
* because there might be preallocation past i_size
|
|
|
|
*/
|
2010-11-23 19:36:57 +00:00
|
|
|
ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
|
2011-04-20 02:31:50 +00:00
|
|
|
path, btrfs_ino(inode), -1, 0);
|
2010-11-23 19:36:57 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
btrfs_free_path(path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
WARN_ON(!ret);
|
|
|
|
path->slots[0]--;
|
|
|
|
item = btrfs_item_ptr(path->nodes[0], path->slots[0],
|
|
|
|
struct btrfs_file_extent_item);
|
|
|
|
btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
|
|
|
|
found_type = btrfs_key_type(&found_key);
|
|
|
|
|
2011-02-23 21:23:20 +00:00
|
|
|
/* No extents, but there might be delalloc bits */
|
2011-04-20 02:31:50 +00:00
|
|
|
if (found_key.objectid != btrfs_ino(inode) ||
|
2010-11-23 19:36:57 +00:00
|
|
|
found_type != BTRFS_EXTENT_DATA_KEY) {
|
2011-02-23 21:23:20 +00:00
|
|
|
/* have to trust i_size as the end */
|
|
|
|
last = (u64)-1;
|
|
|
|
last_for_get_extent = isize;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* remember the start of the last extent. There are a
|
|
|
|
* bunch of different factors that go into the length of the
|
|
|
|
* extent, so its much less complex to remember where it started
|
|
|
|
*/
|
|
|
|
last = found_key.offset;
|
|
|
|
last_for_get_extent = last + 1;
|
2010-11-23 19:36:57 +00:00
|
|
|
}
|
|
|
|
btrfs_free_path(path);
|
|
|
|
|
2011-02-23 21:23:20 +00:00
|
|
|
/*
|
|
|
|
* we might have some extents allocated but more delalloc past those
|
|
|
|
* extents. so, we trust isize unless the start of the last extent is
|
|
|
|
* beyond isize
|
|
|
|
*/
|
|
|
|
if (last < isize) {
|
|
|
|
last = (u64)-1;
|
|
|
|
last_for_get_extent = isize;
|
|
|
|
}
|
|
|
|
|
2010-02-03 19:33:23 +00:00
|
|
|
lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
|
2012-03-01 13:57:19 +00:00
|
|
|
&cached_state);
|
2011-02-23 21:23:20 +00:00
|
|
|
|
2011-11-17 16:34:31 +00:00
|
|
|
em = get_extent_skip_holes(inode, start, last_for_get_extent,
|
2011-02-23 21:23:20 +00:00
|
|
|
get_extent);
|
2009-01-21 19:39:14 +00:00
|
|
|
if (!em)
|
|
|
|
goto out;
|
|
|
|
if (IS_ERR(em)) {
|
|
|
|
ret = PTR_ERR(em);
|
|
|
|
goto out;
|
|
|
|
}
|
2010-11-23 19:36:57 +00:00
|
|
|
|
2009-01-21 19:39:14 +00:00
|
|
|
while (!end) {
|
2011-03-08 16:54:40 +00:00
|
|
|
u64 offset_in_extent;
|
|
|
|
|
|
|
|
/* break if the extent we found is outside the range */
|
|
|
|
if (em->start >= max || extent_map_end(em) < off)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_extent may return an extent that starts before our
|
|
|
|
* requested range. We have to make sure the ranges
|
|
|
|
* we return to fiemap always move forward and don't
|
|
|
|
* overlap, so adjust the offsets here
|
|
|
|
*/
|
|
|
|
em_start = max(em->start, off);
|
2009-01-21 19:39:14 +00:00
|
|
|
|
2011-03-08 16:54:40 +00:00
|
|
|
/*
|
|
|
|
* record the offset from the start of the extent
|
|
|
|
* for adjusting the disk offset below
|
|
|
|
*/
|
|
|
|
offset_in_extent = em_start - em->start;
|
2011-02-23 21:23:20 +00:00
|
|
|
em_end = extent_map_end(em);
|
2011-03-08 16:54:40 +00:00
|
|
|
em_len = em_end - em_start;
|
2011-02-23 21:23:20 +00:00
|
|
|
emflags = em->flags;
|
2009-01-21 19:39:14 +00:00
|
|
|
disko = 0;
|
|
|
|
flags = 0;
|
|
|
|
|
2011-03-08 16:54:40 +00:00
|
|
|
/*
|
|
|
|
* bump off for our next call to get_extent
|
|
|
|
*/
|
|
|
|
off = extent_map_end(em);
|
|
|
|
if (off >= max)
|
|
|
|
end = 1;
|
|
|
|
|
2009-04-03 14:33:45 +00:00
|
|
|
if (em->block_start == EXTENT_MAP_LAST_BYTE) {
|
2009-01-21 19:39:14 +00:00
|
|
|
end = 1;
|
|
|
|
flags |= FIEMAP_EXTENT_LAST;
|
2009-04-03 14:33:45 +00:00
|
|
|
} else if (em->block_start == EXTENT_MAP_INLINE) {
|
2009-01-21 19:39:14 +00:00
|
|
|
flags |= (FIEMAP_EXTENT_DATA_INLINE |
|
|
|
|
FIEMAP_EXTENT_NOT_ALIGNED);
|
2009-04-03 14:33:45 +00:00
|
|
|
} else if (em->block_start == EXTENT_MAP_DELALLOC) {
|
2009-01-21 19:39:14 +00:00
|
|
|
flags |= (FIEMAP_EXTENT_DELALLOC |
|
|
|
|
FIEMAP_EXTENT_UNKNOWN);
|
2009-04-03 14:33:45 +00:00
|
|
|
} else {
|
2011-03-08 16:54:40 +00:00
|
|
|
disko = em->block_start + offset_in_extent;
|
2009-01-21 19:39:14 +00:00
|
|
|
}
|
|
|
|
if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
|
|
|
|
flags |= FIEMAP_EXTENT_ENCODED;
|
|
|
|
|
|
|
|
free_extent_map(em);
|
|
|
|
em = NULL;
|
2011-02-23 21:23:20 +00:00
|
|
|
if ((em_start >= last) || em_len == (u64)-1 ||
|
|
|
|
(last == (u64)-1 && isize <= em_end)) {
|
2009-01-21 19:39:14 +00:00
|
|
|
flags |= FIEMAP_EXTENT_LAST;
|
|
|
|
end = 1;
|
|
|
|
}
|
|
|
|
|
2011-02-23 21:23:20 +00:00
|
|
|
/* now scan forward to see if this is really the last extent. */
|
|
|
|
em = get_extent_skip_holes(inode, off, last_for_get_extent,
|
|
|
|
get_extent);
|
|
|
|
if (IS_ERR(em)) {
|
|
|
|
ret = PTR_ERR(em);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!em) {
|
2010-11-23 19:36:57 +00:00
|
|
|
flags |= FIEMAP_EXTENT_LAST;
|
|
|
|
end = 1;
|
|
|
|
}
|
2011-02-23 21:23:20 +00:00
|
|
|
ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
|
|
|
|
em_len, flags);
|
|
|
|
if (ret)
|
|
|
|
goto out_free;
|
2009-01-21 19:39:14 +00:00
|
|
|
}
|
|
|
|
out_free:
|
|
|
|
free_extent_map(em);
|
|
|
|
out:
|
2010-02-03 19:33:23 +00:00
|
|
|
unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
|
|
|
|
&cached_state, GFP_NOFS);
|
2009-01-21 19:39:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-22 13:41:52 +00:00
|
|
|
inline struct page *extent_buffer_page(struct extent_buffer *eb,
|
2008-01-24 21:13:08 +00:00
|
|
|
unsigned long i)
|
|
|
|
{
|
2010-08-06 17:21:20 +00:00
|
|
|
return eb->pages[i];
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 13:41:52 +00:00
|
|
|
inline unsigned long num_extent_pages(u64 start, u64 len)
|
2008-04-09 20:28:12 +00:00
|
|
|
{
|
2008-07-22 15:18:07 +00:00
|
|
|
return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
|
|
|
|
(start >> PAGE_CACHE_SHIFT);
|
2008-04-09 20:28:12 +00:00
|
|
|
}
|
|
|
|
|
2010-08-06 17:21:20 +00:00
|
|
|
static void __free_extent_buffer(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
#if LEAK_DEBUG
|
|
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&leak_lock, flags);
|
|
|
|
list_del(&eb->leak_list);
|
|
|
|
spin_unlock_irqrestore(&leak_lock, flags);
|
|
|
|
#endif
|
|
|
|
if (eb->pages && eb->pages != eb->inline_pages)
|
|
|
|
kfree(eb->pages);
|
|
|
|
kmem_cache_free(extent_buffer_cache, eb);
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
|
|
|
|
u64 start,
|
|
|
|
unsigned long len,
|
|
|
|
gfp_t mask)
|
|
|
|
{
|
|
|
|
struct extent_buffer *eb = NULL;
|
2009-02-04 14:24:05 +00:00
|
|
|
#if LEAK_DEBUG
|
2008-03-26 20:24:23 +00:00
|
|
|
unsigned long flags;
|
2008-09-08 15:18:08 +00:00
|
|
|
#endif
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
eb = kmem_cache_zalloc(extent_buffer_cache, mask);
|
2011-01-05 02:32:22 +00:00
|
|
|
if (eb == NULL)
|
|
|
|
return NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
eb->start = start;
|
|
|
|
eb->len = len;
|
2012-03-07 21:20:05 +00:00
|
|
|
eb->tree = tree;
|
2011-07-16 19:23:14 +00:00
|
|
|
rwlock_init(&eb->lock);
|
|
|
|
atomic_set(&eb->write_locks, 0);
|
|
|
|
atomic_set(&eb->read_locks, 0);
|
|
|
|
atomic_set(&eb->blocking_readers, 0);
|
|
|
|
atomic_set(&eb->blocking_writers, 0);
|
|
|
|
atomic_set(&eb->spinning_readers, 0);
|
|
|
|
atomic_set(&eb->spinning_writers, 0);
|
2011-09-13 08:55:48 +00:00
|
|
|
eb->lock_nested = 0;
|
2011-07-16 19:23:14 +00:00
|
|
|
init_waitqueue_head(&eb->write_lock_wq);
|
|
|
|
init_waitqueue_head(&eb->read_lock_wq);
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 14:25:08 +00:00
|
|
|
|
2009-02-04 14:24:05 +00:00
|
|
|
#if LEAK_DEBUG
|
2008-03-26 20:24:23 +00:00
|
|
|
spin_lock_irqsave(&leak_lock, flags);
|
|
|
|
list_add(&eb->leak_list, &buffers);
|
|
|
|
spin_unlock_irqrestore(&leak_lock, flags);
|
2008-09-08 15:18:08 +00:00
|
|
|
#endif
|
2012-03-09 21:01:49 +00:00
|
|
|
spin_lock_init(&eb->refs_lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
atomic_set(&eb->refs, 1);
|
2012-03-13 13:38:00 +00:00
|
|
|
atomic_set(&eb->io_pages, 0);
|
2010-08-06 17:21:20 +00:00
|
|
|
|
|
|
|
if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
|
|
|
|
struct page **pages;
|
|
|
|
int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
|
|
|
|
PAGE_CACHE_SHIFT;
|
|
|
|
pages = kzalloc(num_pages, mask);
|
|
|
|
if (!pages) {
|
|
|
|
__free_extent_buffer(eb);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
eb->pages = pages;
|
|
|
|
} else {
|
|
|
|
eb->pages = eb->inline_pages;
|
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
|
|
|
return eb;
|
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
static int extent_buffer_under_io(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
return (atomic_read(&eb->io_pages) ||
|
|
|
|
test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
|
|
|
|
test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
|
|
|
|
}
|
|
|
|
|
2010-10-27 00:57:29 +00:00
|
|
|
/*
|
|
|
|
* Helper for releasing extent buffer page.
|
|
|
|
*/
|
|
|
|
static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
|
|
|
|
unsigned long start_idx)
|
|
|
|
{
|
|
|
|
unsigned long index;
|
|
|
|
struct page *page;
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
BUG_ON(extent_buffer_under_io(eb));
|
|
|
|
|
2010-10-27 00:57:29 +00:00
|
|
|
index = num_extent_pages(eb->start, eb->len);
|
|
|
|
if (start_idx >= index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
|
|
|
index--;
|
|
|
|
page = extent_buffer_page(eb, index);
|
2012-03-07 21:20:05 +00:00
|
|
|
if (page) {
|
|
|
|
spin_lock(&page->mapping->private_lock);
|
|
|
|
/*
|
|
|
|
* We do this since we'll remove the pages after we've
|
|
|
|
* removed the eb from the radix tree, so we could race
|
|
|
|
* and have this page now attached to the new eb. So
|
|
|
|
* only clear page_private if it's still connected to
|
|
|
|
* this eb.
|
|
|
|
*/
|
|
|
|
if (PagePrivate(page) &&
|
|
|
|
page->private == (unsigned long)eb) {
|
2012-03-13 13:38:00 +00:00
|
|
|
BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
|
2012-03-09 21:01:49 +00:00
|
|
|
BUG_ON(PageDirty(page));
|
|
|
|
BUG_ON(PageWriteback(page));
|
2012-03-07 21:20:05 +00:00
|
|
|
/*
|
|
|
|
* We need to make sure we haven't be attached
|
|
|
|
* to a new eb.
|
|
|
|
*/
|
|
|
|
ClearPagePrivate(page);
|
|
|
|
set_page_private(page, 0);
|
|
|
|
/* One for the page private */
|
|
|
|
page_cache_release(page);
|
|
|
|
}
|
|
|
|
spin_unlock(&page->mapping->private_lock);
|
|
|
|
|
|
|
|
/* One for when we alloced the page */
|
2010-10-27 00:57:29 +00:00
|
|
|
page_cache_release(page);
|
2012-03-07 21:20:05 +00:00
|
|
|
}
|
2010-10-27 00:57:29 +00:00
|
|
|
} while (index != start_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for releasing the extent buffer.
|
|
|
|
*/
|
|
|
|
static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
btrfs_release_extent_buffer_page(eb, 0);
|
|
|
|
__free_extent_buffer(eb);
|
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
static void check_buffer_tree_ref(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
/* the ref bit is tricky. We have to make sure it is set
|
|
|
|
* if we have the buffer dirty. Otherwise the
|
|
|
|
* code to free a buffer can end up dropping a dirty
|
|
|
|
* page
|
|
|
|
*
|
|
|
|
* Once the ref bit is set, it won't go away while the
|
|
|
|
* buffer is dirty or in writeback, and it also won't
|
|
|
|
* go away while we have the reference count on the
|
|
|
|
* eb bumped.
|
|
|
|
*
|
|
|
|
* We can't just set the ref bit without bumping the
|
|
|
|
* ref on the eb because free_extent_buffer might
|
|
|
|
* see the ref bit and try to clear it. If this happens
|
|
|
|
* free_extent_buffer might end up dropping our original
|
|
|
|
* ref by mistake and freeing the page before we are able
|
|
|
|
* to add one more ref.
|
|
|
|
*
|
|
|
|
* So bump the ref count first, then set the bit. If someone
|
|
|
|
* beat us to it, drop the ref we added.
|
|
|
|
*/
|
|
|
|
if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
|
|
|
|
atomic_inc(&eb->refs);
|
|
|
|
if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
|
|
|
|
atomic_dec(&eb->refs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 22:24:42 +00:00
|
|
|
static void mark_extent_buffer_accessed(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
unsigned long num_pages, i;
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
check_buffer_tree_ref(eb);
|
|
|
|
|
2012-03-15 22:24:42 +00:00
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
struct page *p = extent_buffer_page(eb, i);
|
|
|
|
mark_page_accessed(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
|
2010-08-06 17:21:20 +00:00
|
|
|
u64 start, unsigned long len)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
unsigned long num_pages = num_extent_pages(start, len);
|
|
|
|
unsigned long i;
|
|
|
|
unsigned long index = start >> PAGE_CACHE_SHIFT;
|
|
|
|
struct extent_buffer *eb;
|
2008-07-22 15:18:07 +00:00
|
|
|
struct extent_buffer *exists = NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
struct page *p;
|
|
|
|
struct address_space *mapping = tree->mapping;
|
|
|
|
int uptodate = 1;
|
2010-10-27 00:57:29 +00:00
|
|
|
int ret;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2010-10-27 00:57:29 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
|
|
|
|
if (eb && atomic_inc_not_zero(&eb->refs)) {
|
|
|
|
rcu_read_unlock();
|
2012-03-15 22:24:42 +00:00
|
|
|
mark_extent_buffer_accessed(eb);
|
2008-07-22 15:18:07 +00:00
|
|
|
return eb;
|
|
|
|
}
|
2010-10-27 00:57:29 +00:00
|
|
|
rcu_read_unlock();
|
2008-07-22 15:18:07 +00:00
|
|
|
|
2011-04-20 23:12:06 +00:00
|
|
|
eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
|
2008-04-01 15:21:40 +00:00
|
|
|
if (!eb)
|
2008-01-24 21:13:08 +00:00
|
|
|
return NULL;
|
|
|
|
|
2010-08-06 17:21:20 +00:00
|
|
|
for (i = 0; i < num_pages; i++, index++) {
|
2011-07-19 16:04:14 +00:00
|
|
|
p = find_or_create_page(mapping, index, GFP_NOFS);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!p) {
|
|
|
|
WARN_ON(1);
|
2008-07-22 15:18:07 +00:00
|
|
|
goto free_eb;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
2012-03-07 21:20:05 +00:00
|
|
|
|
|
|
|
spin_lock(&mapping->private_lock);
|
|
|
|
if (PagePrivate(p)) {
|
|
|
|
/*
|
|
|
|
* We could have already allocated an eb for this page
|
|
|
|
* and attached one so lets see if we can get a ref on
|
|
|
|
* the existing eb, and if we can we know it's good and
|
|
|
|
* we can just return that one, else we know we can just
|
|
|
|
* overwrite page->private.
|
|
|
|
*/
|
|
|
|
exists = (struct extent_buffer *)p->private;
|
|
|
|
if (atomic_inc_not_zero(&exists->refs)) {
|
|
|
|
spin_unlock(&mapping->private_lock);
|
|
|
|
unlock_page(p);
|
2012-03-15 22:24:42 +00:00
|
|
|
mark_extent_buffer_accessed(exists);
|
2012-03-07 21:20:05 +00:00
|
|
|
goto free_eb;
|
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
/*
|
2012-03-07 21:20:05 +00:00
|
|
|
* Do this so attach doesn't complain and we need to
|
|
|
|
* drop the ref the old guy had.
|
|
|
|
*/
|
|
|
|
ClearPagePrivate(p);
|
2012-03-13 13:38:00 +00:00
|
|
|
WARN_ON(PageDirty(p));
|
2012-03-07 21:20:05 +00:00
|
|
|
page_cache_release(p);
|
|
|
|
}
|
|
|
|
attach_extent_buffer_page(eb, p);
|
|
|
|
spin_unlock(&mapping->private_lock);
|
2012-03-13 13:38:00 +00:00
|
|
|
WARN_ON(PageDirty(p));
|
2008-01-24 21:13:08 +00:00
|
|
|
mark_page_accessed(p);
|
2010-08-06 17:21:20 +00:00
|
|
|
eb->pages[i] = p;
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!PageUptodate(p))
|
|
|
|
uptodate = 0;
|
2011-02-10 17:35:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* see below about how we avoid a nasty race with release page
|
|
|
|
* and why we unlock later
|
|
|
|
*/
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
if (uptodate)
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 14:25:08 +00:00
|
|
|
set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
|
2012-03-09 14:51:43 +00:00
|
|
|
again:
|
2010-10-27 00:57:29 +00:00
|
|
|
ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
|
|
|
|
if (ret)
|
|
|
|
goto free_eb;
|
|
|
|
|
2008-07-22 15:18:07 +00:00
|
|
|
spin_lock(&tree->buffer_lock);
|
2010-10-27 00:57:29 +00:00
|
|
|
ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
|
|
|
|
if (ret == -EEXIST) {
|
|
|
|
exists = radix_tree_lookup(&tree->buffer,
|
|
|
|
start >> PAGE_CACHE_SHIFT);
|
2012-03-09 14:51:43 +00:00
|
|
|
if (!atomic_inc_not_zero(&exists->refs)) {
|
|
|
|
spin_unlock(&tree->buffer_lock);
|
|
|
|
radix_tree_preload_end();
|
|
|
|
exists = NULL;
|
|
|
|
goto again;
|
|
|
|
}
|
2008-07-22 15:18:07 +00:00
|
|
|
spin_unlock(&tree->buffer_lock);
|
2010-10-27 00:57:29 +00:00
|
|
|
radix_tree_preload_end();
|
2012-03-15 22:24:42 +00:00
|
|
|
mark_extent_buffer_accessed(exists);
|
2008-07-22 15:18:07 +00:00
|
|
|
goto free_eb;
|
|
|
|
}
|
|
|
|
/* add one reference for the tree */
|
2012-03-09 21:01:49 +00:00
|
|
|
spin_lock(&eb->refs_lock);
|
2012-03-13 13:38:00 +00:00
|
|
|
check_buffer_tree_ref(eb);
|
2012-03-09 21:01:49 +00:00
|
|
|
spin_unlock(&eb->refs_lock);
|
2010-02-04 08:46:56 +00:00
|
|
|
spin_unlock(&tree->buffer_lock);
|
2010-10-27 00:57:29 +00:00
|
|
|
radix_tree_preload_end();
|
2011-02-10 17:35:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* there is a race where release page may have
|
|
|
|
* tried to find this extent buffer in the radix
|
|
|
|
* but failed. It will tell the VM it is safe to
|
|
|
|
* reclaim the, and it will clear the page private bit.
|
|
|
|
* We must make sure to set the page private bit properly
|
|
|
|
* after the extent buffer is in the radix tree so
|
|
|
|
* it doesn't get lost
|
|
|
|
*/
|
2010-08-06 17:21:20 +00:00
|
|
|
SetPageChecked(eb->pages[0]);
|
|
|
|
for (i = 1; i < num_pages; i++) {
|
|
|
|
p = extent_buffer_page(eb, i);
|
|
|
|
ClearPageChecked(p);
|
|
|
|
unlock_page(p);
|
|
|
|
}
|
|
|
|
unlock_page(eb->pages[0]);
|
2008-01-24 21:13:08 +00:00
|
|
|
return eb;
|
|
|
|
|
2008-07-22 15:18:07 +00:00
|
|
|
free_eb:
|
2010-08-06 17:21:20 +00:00
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
if (eb->pages[i])
|
|
|
|
unlock_page(eb->pages[i]);
|
|
|
|
}
|
2011-02-10 17:35:00 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!atomic_dec_and_test(&eb->refs))
|
2008-07-22 15:18:07 +00:00
|
|
|
return exists;
|
2010-10-27 00:57:29 +00:00
|
|
|
btrfs_release_extent_buffer(eb);
|
2008-07-22 15:18:07 +00:00
|
|
|
return exists;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
|
2011-04-20 23:08:01 +00:00
|
|
|
u64 start, unsigned long len)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
struct extent_buffer *eb;
|
|
|
|
|
2010-10-27 00:57:29 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
|
|
|
|
if (eb && atomic_inc_not_zero(&eb->refs)) {
|
|
|
|
rcu_read_unlock();
|
2012-03-15 22:24:42 +00:00
|
|
|
mark_extent_buffer_accessed(eb);
|
2010-10-27 00:57:29 +00:00
|
|
|
return eb;
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
Btrfs: free space accounting redo
1) replace the per fs_info extent_io_tree that tracked free space with two
rb-trees per block group to track free space areas via offset and size. The
reason to do this is because most allocations come with a hint byte where to
start, so we can usually find a chunk of free space at that hint byte to satisfy
the allocation and get good space packing. If we cannot find free space at or
after the given offset we fall back on looking for a chunk of the given size as
close to that given offset as possible. When we fall back on the size search we
also try to find a slot as close to the size we want as possible, to avoid
breaking small chunks off of huge areas if possible.
2) remove the extent_io_tree that tracked the block group cache from fs_info and
replaced it with an rb-tree thats tracks block group cache via offset. also
added a per space_info list that tracks the block group cache for the particular
space so we can lookup related block groups easily.
3) cleaned up the allocation code to make it a little easier to read and a
little less complicated. Basically there are 3 steps, first look from our
provided hint. If we couldn't find from that given hint, start back at our
original search start and look for space from there. If that fails try to
allocate space if we can and start looking again. If not we're screwed and need
to start over again.
4) small fixes. there were some issues in volumes.c where we wouldn't allocate
the rest of the disk. fixed cow_file_range to actually pass the alloc_hint,
which has helped a good bit in making the fs_mark test I run have semi-normal
results as we run out of space. Generally with data allocations we don't track
where we last allocated from, so everytime we did a data allocation we'd search
through every block group that we have looking for free space. Now searching a
block group with no free space isn't terribly time consuming, it was causing a
slight degradation as we got more data block groups. The alloc_hint has fixed
this slight degredation and made things semi-normal.
There is still one nagging problem I'm working on where we will get ENOSPC when
there is definitely plenty of space. This only happens with metadata
allocations, and only when we are almost full. So you generally hit the 85%
mark first, but sometimes you'll hit the BUG before you hit the 85% wall. I'm
still tracking it down, but until then this seems to be pretty stable and make a
significant performance gain.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-09-23 17:14:11 +00:00
|
|
|
|
2010-10-27 00:57:29 +00:00
|
|
|
return NULL;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 21:01:49 +00:00
|
|
|
static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
|
|
|
|
{
|
|
|
|
struct extent_buffer *eb =
|
|
|
|
container_of(head, struct extent_buffer, rcu_head);
|
|
|
|
|
|
|
|
__free_extent_buffer(eb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expects to have eb->eb_lock already held */
|
|
|
|
static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
|
|
|
|
{
|
|
|
|
WARN_ON(atomic_read(&eb->refs) == 0);
|
|
|
|
if (atomic_dec_and_test(&eb->refs)) {
|
|
|
|
struct extent_io_tree *tree = eb->tree;
|
|
|
|
|
|
|
|
spin_unlock(&eb->refs_lock);
|
|
|
|
|
|
|
|
spin_lock(&tree->buffer_lock);
|
|
|
|
radix_tree_delete(&tree->buffer,
|
|
|
|
eb->start >> PAGE_CACHE_SHIFT);
|
|
|
|
spin_unlock(&tree->buffer_lock);
|
|
|
|
|
|
|
|
/* Should be safe to release our pages at this point */
|
|
|
|
btrfs_release_extent_buffer_page(eb, 0);
|
|
|
|
|
|
|
|
call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
spin_unlock(&eb->refs_lock);
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
void free_extent_buffer(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
if (!eb)
|
|
|
|
return;
|
|
|
|
|
2012-03-09 21:01:49 +00:00
|
|
|
spin_lock(&eb->refs_lock);
|
|
|
|
if (atomic_read(&eb->refs) == 2 &&
|
|
|
|
test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
|
2012-03-13 13:38:00 +00:00
|
|
|
!extent_buffer_under_io(eb) &&
|
2012-03-09 21:01:49 +00:00
|
|
|
test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
|
|
|
|
atomic_dec(&eb->refs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I know this is terrible, but it's temporary until we stop tracking
|
|
|
|
* the uptodate bits and such for the extent buffers.
|
|
|
|
*/
|
|
|
|
release_extent_buffer(eb, GFP_ATOMIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_extent_buffer_stale(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
if (!eb)
|
2008-01-24 21:13:08 +00:00
|
|
|
return;
|
|
|
|
|
2012-03-09 21:01:49 +00:00
|
|
|
spin_lock(&eb->refs_lock);
|
|
|
|
set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
|
2012-03-09 21:01:49 +00:00
|
|
|
test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
|
|
|
|
atomic_dec(&eb->refs);
|
|
|
|
release_extent_buffer(eb, GFP_NOFS);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-29 00:31:37 +00:00
|
|
|
void clear_extent_buffer_dirty(struct extent_buffer *eb)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
unsigned long num_pages;
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
|
|
|
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
page = extent_buffer_page(eb, i);
|
2009-03-13 15:00:37 +00:00
|
|
|
if (!PageDirty(page))
|
2008-11-19 17:44:22 +00:00
|
|
|
continue;
|
|
|
|
|
2008-07-22 15:18:08 +00:00
|
|
|
lock_page(page);
|
2011-02-10 17:35:00 +00:00
|
|
|
WARN_ON(!PagePrivate(page));
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
clear_page_dirty_for_io(page);
|
2008-07-30 20:54:26 +00:00
|
|
|
spin_lock_irq(&page->mapping->tree_lock);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (!PageDirty(page)) {
|
|
|
|
radix_tree_tag_clear(&page->mapping->page_tree,
|
|
|
|
page_index(page),
|
|
|
|
PAGECACHE_TAG_DIRTY);
|
|
|
|
}
|
2008-07-30 20:54:26 +00:00
|
|
|
spin_unlock_irq(&page->mapping->tree_lock);
|
2011-11-04 16:29:37 +00:00
|
|
|
ClearPageError(page);
|
2008-07-22 15:18:08 +00:00
|
|
|
unlock_page(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
2012-03-13 13:38:00 +00:00
|
|
|
WARN_ON(atomic_read(&eb->refs) == 0);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
int set_extent_buffer_dirty(struct extent_buffer *eb)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
unsigned long num_pages;
|
2009-03-13 15:00:37 +00:00
|
|
|
int was_dirty = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
check_buffer_tree_ref(eb);
|
|
|
|
|
2009-03-13 15:00:37 +00:00
|
|
|
was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
|
2012-03-13 13:38:00 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
2012-03-09 21:01:49 +00:00
|
|
|
WARN_ON(atomic_read(&eb->refs) == 0);
|
2012-03-13 13:38:00 +00:00
|
|
|
WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
|
|
|
|
|
2009-03-13 15:00:37 +00:00
|
|
|
for (i = 0; i < num_pages; i++)
|
2012-03-13 13:38:00 +00:00
|
|
|
set_page_dirty(extent_buffer_page(eb, i));
|
2009-03-13 15:00:37 +00:00
|
|
|
return was_dirty;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
static int range_straddles_pages(u64 start, u64 len)
|
2011-07-25 10:50:50 +00:00
|
|
|
{
|
|
|
|
if (len < PAGE_CACHE_SIZE)
|
|
|
|
return 1;
|
|
|
|
if (start & (PAGE_CACHE_SIZE - 1))
|
|
|
|
return 1;
|
|
|
|
if ((start + len) & (PAGE_CACHE_SIZE - 1))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
int clear_extent_buffer_uptodate(struct extent_buffer *eb)
|
2008-05-12 17:39:03 +00:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
struct page *page;
|
|
|
|
unsigned long num_pages;
|
|
|
|
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 14:25:08 +00:00
|
|
|
clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
|
2012-03-13 13:38:00 +00:00
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
2008-05-12 17:39:03 +00:00
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
page = extent_buffer_page(eb, i);
|
2008-07-30 14:29:12 +00:00
|
|
|
if (page)
|
|
|
|
ClearPageUptodate(page);
|
2008-05-12 17:39:03 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
int set_extent_buffer_uptodate(struct extent_buffer *eb)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
struct page *page;
|
|
|
|
unsigned long num_pages;
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
|
2008-01-24 21:13:08 +00:00
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
SetPageUptodate(page);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-09 20:28:12 +00:00
|
|
|
int extent_range_uptodate(struct extent_io_tree *tree,
|
|
|
|
u64 start, u64 end)
|
|
|
|
{
|
|
|
|
struct page *page;
|
|
|
|
int ret;
|
|
|
|
int pg_uptodate = 1;
|
|
|
|
int uptodate;
|
|
|
|
unsigned long index;
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
if (range_straddles_pages(start, end - start + 1)) {
|
2011-07-25 10:50:50 +00:00
|
|
|
ret = test_range_bit(tree, start, end,
|
|
|
|
EXTENT_UPTODATE, 1, NULL);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
|
|
|
}
|
2009-01-06 02:25:51 +00:00
|
|
|
while (start <= end) {
|
2008-04-09 20:28:12 +00:00
|
|
|
index = start >> PAGE_CACHE_SHIFT;
|
|
|
|
page = find_get_page(tree->mapping, index);
|
2012-01-26 20:01:11 +00:00
|
|
|
if (!page)
|
|
|
|
return 1;
|
2008-04-09 20:28:12 +00:00
|
|
|
uptodate = PageUptodate(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
if (!uptodate) {
|
|
|
|
pg_uptodate = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
start += PAGE_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
return pg_uptodate;
|
|
|
|
}
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
int extent_buffer_uptodate(struct extent_buffer *eb)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
2012-03-13 13:38:00 +00:00
|
|
|
return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int read_extent_buffer_pages(struct extent_io_tree *tree,
|
2011-06-10 12:06:53 +00:00
|
|
|
struct extent_buffer *eb, u64 start, int wait,
|
2008-04-09 20:28:12 +00:00
|
|
|
get_extent_t *get_extent, int mirror_num)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
unsigned long start_i;
|
|
|
|
struct page *page;
|
|
|
|
int err;
|
|
|
|
int ret = 0;
|
2008-04-09 20:28:12 +00:00
|
|
|
int locked_pages = 0;
|
|
|
|
int all_uptodate = 1;
|
2008-01-24 21:13:08 +00:00
|
|
|
unsigned long num_pages;
|
2010-08-06 17:21:20 +00:00
|
|
|
unsigned long num_reads = 0;
|
2008-02-07 15:50:54 +00:00
|
|
|
struct bio *bio = NULL;
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
unsigned long bio_flags = 0;
|
2008-02-07 15:50:54 +00:00
|
|
|
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 14:25:08 +00:00
|
|
|
if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
|
2008-01-24 21:13:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (start) {
|
|
|
|
WARN_ON(start < eb->start);
|
|
|
|
start_i = (start >> PAGE_CACHE_SHIFT) -
|
|
|
|
(eb->start >> PAGE_CACHE_SHIFT);
|
|
|
|
} else {
|
|
|
|
start_i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_pages = num_extent_pages(eb->start, eb->len);
|
|
|
|
for (i = start_i; i < num_pages; i++) {
|
|
|
|
page = extent_buffer_page(eb, i);
|
2011-06-10 12:06:53 +00:00
|
|
|
if (wait == WAIT_NONE) {
|
2008-08-07 15:19:43 +00:00
|
|
|
if (!trylock_page(page))
|
2008-04-09 20:28:12 +00:00
|
|
|
goto unlock_exit;
|
2008-01-24 21:13:08 +00:00
|
|
|
} else {
|
|
|
|
lock_page(page);
|
|
|
|
}
|
2008-04-09 20:28:12 +00:00
|
|
|
locked_pages++;
|
2010-08-06 17:21:20 +00:00
|
|
|
if (!PageUptodate(page)) {
|
|
|
|
num_reads++;
|
2008-04-09 20:28:12 +00:00
|
|
|
all_uptodate = 0;
|
2010-08-06 17:21:20 +00:00
|
|
|
}
|
2008-04-09 20:28:12 +00:00
|
|
|
}
|
|
|
|
if (all_uptodate) {
|
|
|
|
if (start_i == 0)
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 14:25:08 +00:00
|
|
|
set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
|
2008-04-09 20:28:12 +00:00
|
|
|
goto unlock_exit;
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:57:36 +00:00
|
|
|
clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
|
|
|
|
eb->failed_mirror = 0;
|
2012-03-13 13:38:00 +00:00
|
|
|
atomic_set(&eb->io_pages, num_reads);
|
2008-04-09 20:28:12 +00:00
|
|
|
for (i = start_i; i < num_pages; i++) {
|
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
if (!PageUptodate(page)) {
|
2008-04-09 20:28:12 +00:00
|
|
|
ClearPageError(page);
|
2008-02-07 15:50:54 +00:00
|
|
|
err = __extent_read_full_page(tree, page,
|
2008-04-09 20:28:12 +00:00
|
|
|
get_extent, &bio,
|
Btrfs: Add zlib compression support
This is a large change for adding compression on reading and writing,
both for inline and regular extents. It does some fairly large
surgery to the writeback paths.
Compression is off by default and enabled by mount -o compress. Even
when the -o compress mount option is not used, it is possible to read
compressed extents off the disk.
If compression for a given set of pages fails to make them smaller, the
file is flagged to avoid future compression attempts later.
* While finding delalloc extents, the pages are locked before being sent down
to the delalloc handler. This allows the delalloc handler to do complex things
such as cleaning the pages, marking them writeback and starting IO on their
behalf.
* Inline extents are inserted at delalloc time now. This allows us to compress
the data before inserting the inline extent, and it allows us to insert
an inline extent that spans multiple pages.
* All of the in-memory extent representations (extent_map.c, ordered-data.c etc)
are changed to record both an in-memory size and an on disk size, as well
as a flag for compression.
From a disk format point of view, the extent pointers in the file are changed
to record the on disk size of a given extent and some encoding flags.
Space in the disk format is allocated for compression encoding, as well
as encryption and a generic 'other' field. Neither the encryption or the
'other' field are currently used.
In order to limit the amount of data read for a single random read in the
file, the size of a compressed extent is limited to 128k. This is a
software only limit, the disk format supports u64 sized compressed extents.
In order to limit the ram consumed while processing extents, the uncompressed
size of a compressed extent is limited to 256k. This is a software only limit
and will be subject to tuning later.
Checksumming is still done on compressed extents, and it is done on the
uncompressed version of the data. This way additional encodings can be
layered on without having to figure out which encoding to checksum.
Compression happens at delalloc time, which is basically singled threaded because
it is usually done by a single pdflush thread. This makes it tricky to
spread the compression load across all the cpus on the box. We'll have to
look at parallel pdflush walks of dirty inodes at a later time.
Decompression is hooked into readpages and it does spread across CPUs nicely.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-10-29 18:49:59 +00:00
|
|
|
mirror_num, &bio_flags);
|
2009-01-06 02:25:51 +00:00
|
|
|
if (err)
|
2008-01-24 21:13:08 +00:00
|
|
|
ret = err;
|
|
|
|
} else {
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 03:23:14 +00:00
|
|
|
if (bio) {
|
|
|
|
err = submit_one_bio(READ, bio, mirror_num, bio_flags);
|
2012-03-12 15:03:00 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2011-10-04 03:23:14 +00:00
|
|
|
}
|
2008-02-07 15:50:54 +00:00
|
|
|
|
2011-06-10 12:06:53 +00:00
|
|
|
if (ret || wait != WAIT_COMPLETE)
|
2008-01-24 21:13:08 +00:00
|
|
|
return ret;
|
2009-01-06 02:25:51 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
for (i = start_i; i < num_pages; i++) {
|
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
wait_on_page_locked(page);
|
2009-01-06 02:25:51 +00:00
|
|
|
if (!PageUptodate(page))
|
2008-01-24 21:13:08 +00:00
|
|
|
ret = -EIO;
|
|
|
|
}
|
2009-01-06 02:25:51 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
return ret;
|
2008-04-09 20:28:12 +00:00
|
|
|
|
|
|
|
unlock_exit:
|
|
|
|
i = start_i;
|
2009-01-06 02:25:51 +00:00
|
|
|
while (locked_pages > 0) {
|
2008-04-09 20:28:12 +00:00
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
i++;
|
|
|
|
unlock_page(page);
|
|
|
|
locked_pages--;
|
|
|
|
}
|
|
|
|
return ret;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_extent_buffer(struct extent_buffer *eb, void *dstv,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned long len)
|
|
|
|
{
|
|
|
|
size_t cur;
|
|
|
|
size_t offset;
|
|
|
|
struct page *page;
|
|
|
|
char *kaddr;
|
|
|
|
char *dst = (char *)dstv;
|
|
|
|
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
WARN_ON(start > eb->len);
|
|
|
|
WARN_ON(start + len > eb->start + eb->len);
|
|
|
|
|
|
|
|
offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (len > 0) {
|
2008-01-24 21:13:08 +00:00
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
|
|
|
|
cur = min(len, (PAGE_CACHE_SIZE - offset));
|
2011-07-19 16:04:14 +00:00
|
|
|
kaddr = page_address(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
memcpy(dst, kaddr + offset, cur);
|
|
|
|
|
|
|
|
dst += cur;
|
|
|
|
len -= cur;
|
|
|
|
offset = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
|
2011-07-19 16:04:14 +00:00
|
|
|
unsigned long min_len, char **map,
|
2008-01-24 21:13:08 +00:00
|
|
|
unsigned long *map_start,
|
2011-07-19 16:04:14 +00:00
|
|
|
unsigned long *map_len)
|
2008-01-24 21:13:08 +00:00
|
|
|
{
|
|
|
|
size_t offset = start & (PAGE_CACHE_SIZE - 1);
|
|
|
|
char *kaddr;
|
|
|
|
struct page *p;
|
|
|
|
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
|
|
|
unsigned long end_i = (start_offset + start + min_len - 1) >>
|
|
|
|
PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
if (i != end_i)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
offset = start_offset;
|
|
|
|
*map_start = 0;
|
|
|
|
} else {
|
|
|
|
offset = 0;
|
|
|
|
*map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
|
|
|
|
}
|
2009-01-06 02:25:51 +00:00
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
if (start + min_len > eb->len) {
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
|
|
|
|
"wanted %lu %lu\n", (unsigned long long)eb->start,
|
|
|
|
eb->len, start, min_len);
|
2008-01-24 21:13:08 +00:00
|
|
|
WARN_ON(1);
|
2011-03-15 18:52:12 +00:00
|
|
|
return -EINVAL;
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p = extent_buffer_page(eb, i);
|
2011-07-19 16:04:14 +00:00
|
|
|
kaddr = page_address(p);
|
2008-01-24 21:13:08 +00:00
|
|
|
*map = kaddr + offset;
|
|
|
|
*map_len = PAGE_CACHE_SIZE - offset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned long len)
|
|
|
|
{
|
|
|
|
size_t cur;
|
|
|
|
size_t offset;
|
|
|
|
struct page *page;
|
|
|
|
char *kaddr;
|
|
|
|
char *ptr = (char *)ptrv;
|
|
|
|
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
WARN_ON(start > eb->len);
|
|
|
|
WARN_ON(start + len > eb->start + eb->len);
|
|
|
|
|
|
|
|
offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (len > 0) {
|
2008-01-24 21:13:08 +00:00
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
|
|
|
|
cur = min(len, (PAGE_CACHE_SIZE - offset));
|
|
|
|
|
2011-07-19 16:04:14 +00:00
|
|
|
kaddr = page_address(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
ret = memcmp(ptr, kaddr + offset, cur);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ptr += cur;
|
|
|
|
len -= cur;
|
|
|
|
offset = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
|
|
|
|
unsigned long start, unsigned long len)
|
|
|
|
{
|
|
|
|
size_t cur;
|
|
|
|
size_t offset;
|
|
|
|
struct page *page;
|
|
|
|
char *kaddr;
|
|
|
|
char *src = (char *)srcv;
|
|
|
|
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
WARN_ON(start > eb->len);
|
|
|
|
WARN_ON(start + len > eb->start + eb->len);
|
|
|
|
|
|
|
|
offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (len > 0) {
|
2008-01-24 21:13:08 +00:00
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
WARN_ON(!PageUptodate(page));
|
|
|
|
|
|
|
|
cur = min(len, PAGE_CACHE_SIZE - offset);
|
2011-07-19 16:04:14 +00:00
|
|
|
kaddr = page_address(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
memcpy(kaddr + offset, src, cur);
|
|
|
|
|
|
|
|
src += cur;
|
|
|
|
len -= cur;
|
|
|
|
offset = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void memset_extent_buffer(struct extent_buffer *eb, char c,
|
|
|
|
unsigned long start, unsigned long len)
|
|
|
|
{
|
|
|
|
size_t cur;
|
|
|
|
size_t offset;
|
|
|
|
struct page *page;
|
|
|
|
char *kaddr;
|
|
|
|
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
WARN_ON(start > eb->len);
|
|
|
|
WARN_ON(start + len > eb->start + eb->len);
|
|
|
|
|
|
|
|
offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (len > 0) {
|
2008-01-24 21:13:08 +00:00
|
|
|
page = extent_buffer_page(eb, i);
|
|
|
|
WARN_ON(!PageUptodate(page));
|
|
|
|
|
|
|
|
cur = min(len, PAGE_CACHE_SIZE - offset);
|
2011-07-19 16:04:14 +00:00
|
|
|
kaddr = page_address(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
memset(kaddr + offset, c, cur);
|
|
|
|
|
|
|
|
len -= cur;
|
|
|
|
offset = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
|
|
|
|
unsigned long dst_offset, unsigned long src_offset,
|
|
|
|
unsigned long len)
|
|
|
|
{
|
|
|
|
u64 dst_len = dst->len;
|
|
|
|
size_t cur;
|
|
|
|
size_t offset;
|
|
|
|
struct page *page;
|
|
|
|
char *kaddr;
|
|
|
|
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
WARN_ON(src->len != dst_len);
|
|
|
|
|
|
|
|
offset = (start_offset + dst_offset) &
|
|
|
|
((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (len > 0) {
|
2008-01-24 21:13:08 +00:00
|
|
|
page = extent_buffer_page(dst, i);
|
|
|
|
WARN_ON(!PageUptodate(page));
|
|
|
|
|
|
|
|
cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
|
|
|
|
|
2011-07-19 16:04:14 +00:00
|
|
|
kaddr = page_address(page);
|
2008-01-24 21:13:08 +00:00
|
|
|
read_extent_buffer(src, kaddr + offset, src_offset, cur);
|
|
|
|
|
|
|
|
src_offset += cur;
|
|
|
|
len -= cur;
|
|
|
|
offset = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_pages(struct page *dst_page, struct page *src_page,
|
|
|
|
unsigned long dst_off, unsigned long src_off,
|
|
|
|
unsigned long len)
|
|
|
|
{
|
2011-07-19 16:04:14 +00:00
|
|
|
char *dst_kaddr = page_address(dst_page);
|
2008-01-24 21:13:08 +00:00
|
|
|
if (dst_page == src_page) {
|
|
|
|
memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
|
|
|
|
} else {
|
2011-07-19 16:04:14 +00:00
|
|
|
char *src_kaddr = page_address(src_page);
|
2008-01-24 21:13:08 +00:00
|
|
|
char *p = dst_kaddr + dst_off + len;
|
|
|
|
char *s = src_kaddr + src_off + len;
|
|
|
|
|
|
|
|
while (len--)
|
|
|
|
*--p = *--s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:52:52 +00:00
|
|
|
static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
|
|
|
|
{
|
|
|
|
unsigned long distance = (src > dst) ? src - dst : dst - src;
|
|
|
|
return distance < len;
|
|
|
|
}
|
|
|
|
|
2008-01-24 21:13:08 +00:00
|
|
|
static void copy_pages(struct page *dst_page, struct page *src_page,
|
|
|
|
unsigned long dst_off, unsigned long src_off,
|
|
|
|
unsigned long len)
|
|
|
|
{
|
2011-07-19 16:04:14 +00:00
|
|
|
char *dst_kaddr = page_address(dst_page);
|
2008-01-24 21:13:08 +00:00
|
|
|
char *src_kaddr;
|
2010-08-06 17:21:20 +00:00
|
|
|
int must_memmove = 0;
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2011-04-11 21:52:52 +00:00
|
|
|
if (dst_page != src_page) {
|
2011-07-19 16:04:14 +00:00
|
|
|
src_kaddr = page_address(src_page);
|
2011-04-11 21:52:52 +00:00
|
|
|
} else {
|
2008-01-24 21:13:08 +00:00
|
|
|
src_kaddr = dst_kaddr;
|
2010-08-06 17:21:20 +00:00
|
|
|
if (areas_overlap(src_off, dst_off, len))
|
|
|
|
must_memmove = 1;
|
2011-04-11 21:52:52 +00:00
|
|
|
}
|
2008-01-24 21:13:08 +00:00
|
|
|
|
2010-08-06 17:21:20 +00:00
|
|
|
if (must_memmove)
|
|
|
|
memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
|
|
|
|
else
|
|
|
|
memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
|
2008-01-24 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
|
|
|
|
unsigned long src_offset, unsigned long len)
|
|
|
|
{
|
|
|
|
size_t cur;
|
|
|
|
size_t dst_off_in_page;
|
|
|
|
size_t src_off_in_page;
|
|
|
|
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long dst_i;
|
|
|
|
unsigned long src_i;
|
|
|
|
|
|
|
|
if (src_offset + len > dst->len) {
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
|
|
|
|
"len %lu dst len %lu\n", src_offset, len, dst->len);
|
2008-01-24 21:13:08 +00:00
|
|
|
BUG_ON(1);
|
|
|
|
}
|
|
|
|
if (dst_offset + len > dst->len) {
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
|
|
|
|
"len %lu dst len %lu\n", dst_offset, len, dst->len);
|
2008-01-24 21:13:08 +00:00
|
|
|
BUG_ON(1);
|
|
|
|
}
|
|
|
|
|
2009-01-06 02:25:51 +00:00
|
|
|
while (len > 0) {
|
2008-01-24 21:13:08 +00:00
|
|
|
dst_off_in_page = (start_offset + dst_offset) &
|
|
|
|
((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
src_off_in_page = (start_offset + src_offset) &
|
|
|
|
((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
|
|
|
|
dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
|
|
|
|
src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
|
|
|
|
src_off_in_page));
|
|
|
|
cur = min_t(unsigned long, cur,
|
|
|
|
(unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
|
|
|
|
|
|
|
|
copy_pages(extent_buffer_page(dst, dst_i),
|
|
|
|
extent_buffer_page(dst, src_i),
|
|
|
|
dst_off_in_page, src_off_in_page, cur);
|
|
|
|
|
|
|
|
src_offset += cur;
|
|
|
|
dst_offset += cur;
|
|
|
|
len -= cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
|
|
|
|
unsigned long src_offset, unsigned long len)
|
|
|
|
{
|
|
|
|
size_t cur;
|
|
|
|
size_t dst_off_in_page;
|
|
|
|
size_t src_off_in_page;
|
|
|
|
unsigned long dst_end = dst_offset + len - 1;
|
|
|
|
unsigned long src_end = src_offset + len - 1;
|
|
|
|
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
|
|
|
|
unsigned long dst_i;
|
|
|
|
unsigned long src_i;
|
|
|
|
|
|
|
|
if (src_offset + len > dst->len) {
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
|
|
|
|
"len %lu len %lu\n", src_offset, len, dst->len);
|
2008-01-24 21:13:08 +00:00
|
|
|
BUG_ON(1);
|
|
|
|
}
|
|
|
|
if (dst_offset + len > dst->len) {
|
2009-01-06 02:25:51 +00:00
|
|
|
printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
|
|
|
|
"len %lu len %lu\n", dst_offset, len, dst->len);
|
2008-01-24 21:13:08 +00:00
|
|
|
BUG_ON(1);
|
|
|
|
}
|
2010-08-06 17:21:20 +00:00
|
|
|
if (dst_offset < src_offset) {
|
2008-01-24 21:13:08 +00:00
|
|
|
memcpy_extent_buffer(dst, dst_offset, src_offset, len);
|
|
|
|
return;
|
|
|
|
}
|
2009-01-06 02:25:51 +00:00
|
|
|
while (len > 0) {
|
2008-01-24 21:13:08 +00:00
|
|
|
dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
|
|
|
|
src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
dst_off_in_page = (start_offset + dst_end) &
|
|
|
|
((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
src_off_in_page = (start_offset + src_end) &
|
|
|
|
((unsigned long)PAGE_CACHE_SIZE - 1);
|
|
|
|
|
|
|
|
cur = min_t(unsigned long, len, src_off_in_page + 1);
|
|
|
|
cur = min(cur, dst_off_in_page + 1);
|
|
|
|
move_pages(extent_buffer_page(dst, dst_i),
|
|
|
|
extent_buffer_page(dst, src_i),
|
|
|
|
dst_off_in_page - cur + 1,
|
|
|
|
src_off_in_page - cur + 1, cur);
|
|
|
|
|
|
|
|
dst_end -= cur;
|
|
|
|
src_end -= cur;
|
|
|
|
len -= cur;
|
|
|
|
}
|
|
|
|
}
|
2008-07-22 15:18:07 +00:00
|
|
|
|
2012-03-09 21:01:49 +00:00
|
|
|
int try_release_extent_buffer(struct page *page, gfp_t mask)
|
2008-07-22 15:18:07 +00:00
|
|
|
{
|
2012-03-09 21:01:49 +00:00
|
|
|
struct extent_buffer *eb;
|
2008-07-22 15:18:07 +00:00
|
|
|
|
2012-03-09 21:01:49 +00:00
|
|
|
/*
|
|
|
|
* We need to make sure noboody is attaching this page to an eb right
|
|
|
|
* now.
|
|
|
|
*/
|
|
|
|
spin_lock(&page->mapping->private_lock);
|
|
|
|
if (!PagePrivate(page)) {
|
|
|
|
spin_unlock(&page->mapping->private_lock);
|
2012-03-07 21:20:05 +00:00
|
|
|
return 1;
|
2012-03-09 21:01:49 +00:00
|
|
|
}
|
2008-07-22 15:18:07 +00:00
|
|
|
|
2012-03-09 21:01:49 +00:00
|
|
|
eb = (struct extent_buffer *)page->private;
|
|
|
|
BUG_ON(!eb);
|
|
|
|
|
2012-03-13 13:38:00 +00:00
|
|
|
/*
|
2012-03-09 21:01:49 +00:00
|
|
|
* This is a little awful but should be ok, we need to make sure that
|
|
|
|
* the eb doesn't disappear out from under us while we're looking at
|
|
|
|
* this page.
|
|
|
|
*/
|
|
|
|
spin_lock(&eb->refs_lock);
|
2012-03-13 13:38:00 +00:00
|
|
|
if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
|
2012-03-09 21:01:49 +00:00
|
|
|
spin_unlock(&eb->refs_lock);
|
|
|
|
spin_unlock(&page->mapping->private_lock);
|
|
|
|
return 0;
|
2008-07-22 15:18:07 +00:00
|
|
|
}
|
2012-03-09 21:01:49 +00:00
|
|
|
spin_unlock(&page->mapping->private_lock);
|
|
|
|
|
|
|
|
if ((mask & GFP_NOFS) == GFP_NOFS)
|
|
|
|
mask = GFP_NOFS;
|
2010-10-27 00:57:29 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-09 21:01:49 +00:00
|
|
|
* If tree ref isn't set then we know the ref on this eb is a real ref,
|
|
|
|
* so just return, this page will likely be freed soon anyway.
|
2010-10-27 00:57:29 +00:00
|
|
|
*/
|
2012-03-09 21:01:49 +00:00
|
|
|
if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
|
|
|
|
spin_unlock(&eb->refs_lock);
|
|
|
|
return 0;
|
2009-03-13 15:00:37 +00:00
|
|
|
}
|
2012-03-09 21:01:49 +00:00
|
|
|
release_extent_buffer(eb, mask);
|
2010-10-27 00:57:29 +00:00
|
|
|
|
2012-03-09 21:01:49 +00:00
|
|
|
return 1;
|
2008-07-22 15:18:07 +00:00
|
|
|
}
|