u64 i;
u64 last = 0;
u64 hole_size;
- u64 limit;
int found = 0;
root = root->fs_info->extent_root;
return ret;
if (ret && path->slots[0] > 0)
path->slots[0]--;
- limit = block_group->key.objectid + block_group->key.offset;
while(1) {
leaf = btrfs_buffer_leaf(path->nodes[0]);
slot = path->slots[0];
struct btrfs_block_group_cache *block_group;
int full_scan = 0;
int wrapped = 0;
- u64 limit;
WARN_ON(num_blocks < 1);
ins->flags = 0;
l = btrfs_buffer_leaf(path->nodes[0]);
slot = path->slots[0];
if (slot >= btrfs_header_nritems(&l->header)) {
- if (start_found)
- limit = last_block +
- (block_group->key.offset >> 1);
- else
- limit = search_start +
- (block_group->key.offset >> 1);
ret = btrfs_next_leaf(root, path);
if (ret == 0)
continue;
#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
-static LIST_HEAD(all_states);
-spinlock_t state_lock = SPIN_LOCK_UNLOCKED;
-
void __init extent_map_init(void)
{
extent_map_cache = kmem_cache_create("extent_map",
void __exit extent_map_exit(void)
{
- while(!list_empty(&all_states)) {
- struct extent_state *state;
- struct list_head *cur = all_states.next;
- state = list_entry(cur, struct extent_state, list);
- printk("found leaked state %Lu %Lu state %d in_tree %d\n",
- state->start, state->end, state->state, state->in_tree);
- list_del(&state->list);
- kfree(state);
- }
if (extent_map_cache)
kmem_cache_destroy(extent_map_cache);
if (extent_state_cache)
{
tree->map.rb_node = NULL;
tree->state.rb_node = NULL;
+ tree->fill_delalloc = NULL;
rwlock_init(&tree->lock);
tree->mapping = mapping;
}
state->in_tree = 0;
atomic_set(&state->refs, 1);
init_waitqueue_head(&state->wq);
- spin_lock_irq(&state_lock);
- list_add(&state->list, &all_states);
- spin_unlock_irq(&state_lock);
return state;
}
EXPORT_SYMBOL(alloc_extent_state);
{
if (atomic_dec_and_test(&state->refs)) {
WARN_ON(state->in_tree);
- spin_lock_irq(&state_lock);
- list_del_init(&state->list);
- spin_unlock_irq(&state_lock);
kmem_cache_free(extent_state_cache, state);
}
}
if (node) {
struct extent_state *found;
found = rb_entry(node, struct extent_state, rb_node);
-printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
+ printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
free_extent_state(state);
return -EEXIST;
}
if (node) {
struct extent_state *found;
found = rb_entry(node, struct extent_state, rb_node);
-printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
+ printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
free_extent_state(prealloc);
return -EEXIST;
}
}
EXPORT_SYMBOL(set_extent_dirty);
+int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end,
+ gfp_t mask)
+{
+ return set_extent_bit(tree, start, end,
+ EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL,
+ mask);
+}
+EXPORT_SYMBOL(set_extent_delalloc);
+
int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
gfp_t mask)
{
- return clear_extent_bit(tree, start, end, EXTENT_DIRTY, 0, 0, mask);
+ return clear_extent_bit(tree, start, end,
+ EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
}
EXPORT_SYMBOL(clear_extent_dirty);
}
EXPORT_SYMBOL(set_range_writeback);
+u64 find_lock_delalloc_range(struct extent_map_tree *tree,
+ u64 start, u64 lock_start, u64 *end, u64 max_bytes)
+{
+ struct rb_node *node;
+ struct extent_state *state;
+ u64 cur_start = start;
+ u64 found = 0;
+ u64 total_bytes = 0;
+
+ write_lock_irq(&tree->lock);
+ /*
+ * this search will find all the extents that end after
+ * our range starts.
+ */
+search_again:
+ node = tree_search(&tree->state, cur_start);
+ if (!node || IS_ERR(node)) {
+ goto out;
+ }
+
+ while(1) {
+ state = rb_entry(node, struct extent_state, rb_node);
+ if (state->start != cur_start) {
+ goto out;
+ }
+ if (!(state->state & EXTENT_DELALLOC)) {
+ goto out;
+ }
+ if (state->start >= lock_start) {
+ if (state->state & EXTENT_LOCKED) {
+ DEFINE_WAIT(wait);
+ atomic_inc(&state->refs);
+ write_unlock_irq(&tree->lock);
+ schedule();
+ write_lock_irq(&tree->lock);
+ finish_wait(&state->wq, &wait);
+ free_extent_state(state);
+ goto search_again;
+ }
+ state->state |= EXTENT_LOCKED;
+ }
+ 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:
+ write_unlock_irq(&tree->lock);
+ return found;
+}
+
/*
* helper function to lock both pages and extents in the tree.
* pages must be locked first.
if (!PagePrivate(page)) {
SetPagePrivate(page);
set_page_private(page, 1);
+ WARN_ON(!page->mapping->a_ops->invalidatepage);
page_cache_get(page);
}
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;
+ WARN_ON(!PageLocked(page));
if (page->index > end_index) {
clear_extent_dirty(tree, start, page_end, GFP_NOFS);
unlock_page(page);
if (!PagePrivate(page)) {
SetPagePrivate(page);
set_page_private(page, 1);
+ WARN_ON(!page->mapping->a_ops->invalidatepage);
page_cache_get(page);
}
- end = page_end;
lock_extent(tree, start, page_end, GFP_NOFS);
+ nr_delalloc = find_lock_delalloc_range(tree, start, page_end + 1,
+ &delalloc_end,
+ 128 * 1024 * 1024);
+ if (nr_delalloc) {
+ tree->fill_delalloc(inode, start, delalloc_end);
+ if (delalloc_end >= page_end + 1) {
+ clear_extent_bit(tree, page_end + 1, delalloc_end,
+ EXTENT_LOCKED | EXTENT_DELALLOC,
+ 1, 0, GFP_NOFS);
+ }
+ clear_extent_bit(tree, start, page_end, EXTENT_DELALLOC,
+ 0, 0, GFP_NOFS);
+ if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
+ printk("found delalloc bits after clear extent_bit\n");
+ }
+ } else if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
+ printk("found delalloc bits after find_delalloc_range returns 0\n");
+ }
+
+ end = page_end;
+ if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
+ printk("found delalloc bits after lock_extent\n");
+ }
if (last_byte <= start) {
clear_extent_dirty(tree, start, page_end, GFP_NOFS);
clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
break;
}
- em = get_extent(inode, page, page_offset, cur, end, 1);
+ em = get_extent(inode, page, page_offset, cur, end, 0);
if (IS_ERR(em) || !em) {
SetPageError(page);
break;
if (!PagePrivate(page)) {
SetPagePrivate(page);
set_page_private(page, 1);
+ WARN_ON(!page->mapping->a_ops->invalidatepage);
page_cache_get(page);
}
if (!PagePrivate(page)) {
SetPagePrivate(page);
set_page_private(page, 1);
+ WARN_ON(!page->mapping->a_ops->invalidatepage);
page_cache_get(page);
}
block_start = (page_start + from) & ~((u64)blocksize - 1);
u64 start = page->index << PAGE_CACHE_SHIFT;
u64 end = start + PAGE_CACHE_SIZE - 1;
u64 orig_start = start;
+ int ret = 1;
while (start <= end) {
em = lookup_extent_mapping(tree, start, end);
if (!em || IS_ERR(em))
break;
- if (test_range_bit(tree, em->start, em->end,
- EXTENT_LOCKED, 0)) {
+ if (!test_range_bit(tree, em->start, em->end,
+ EXTENT_LOCKED, 0)) {
+ remove_extent_mapping(tree, em);
+ /* once for the rb tree */
free_extent_map(em);
- start = em->end + 1;
-printk("range still locked %Lu %Lu\n", em->start, em->end);
- break;
}
- remove_extent_mapping(tree, em);
start = em->end + 1;
- /* once for the rb tree */
- free_extent_map(em);
/* once for us */
free_extent_map(em);
}
- WARN_ON(test_range_bit(tree, orig_start, end, EXTENT_WRITEBACK, 0));
- clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE,
- 1, 1, GFP_NOFS);
- return 1;
+ if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0))
+ ret = 0;
+ else
+ clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE,
+ 1, 1, GFP_NOFS);
+ return ret;
}
EXPORT_SYMBOL(try_release_extent_mapping);
struct rb_root state;
struct address_space *mapping;
rwlock_t lock;
+ int (*fill_delalloc)(struct inode *inode, u64 start, u64 end);
};
/* note, this must start with the same fields as fs/extent_map.c:tree_entry */
gfp_t mask);
int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
gfp_t mask);
+int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end,
+ gfp_t mask);
int extent_invalidatepage(struct extent_map_tree *tree,
struct page *page, unsigned long offset);
int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
struct inode *inode = file->f_path.dentry->d_inode;
struct extent_map *em;
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
- struct btrfs_key ins;
u64 hint_block;
u64 num_blocks;
u64 start_pos;
inode->i_blkbits;
end_of_last_block = start_pos + (num_blocks << inode->i_blkbits) - 1;
+ lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
mutex_lock(&root->fs_info->fs_mutex);
trans = btrfs_start_transaction(root, 1);
if (!trans) {
/* FIXME...EIEIO, ENOSPC and more */
- /* step one, delete the existing extents in this range */
- /* FIXME blocksize != pagesize */
- if (start_pos < inode->i_size) {
- err = btrfs_drop_extents(trans, root, inode,
- start_pos, (pos + write_bytes + root->blocksize -1) &
- ~((u64)root->blocksize - 1), &hint_block);
- if (err)
- goto failed;
- }
-
/* insert any holes we need to create */
if (inode->i_size < start_pos) {
u64 last_pos_in_file;
*/
if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
pos + write_bytes - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
- err = btrfs_alloc_extent(trans, root, inode->i_ino,
- num_blocks, 0, hint_block, (u64)-1,
- &ins, 1);
- BUG_ON(err);
- err = btrfs_insert_file_extent(trans, root, inode->i_ino,
- start_pos, ins.objectid, ins.offset,
- ins.offset);
- BUG_ON(err);
- em->start = start_pos;
- em->end = end_of_last_block;
- em->block_start = ins.objectid << inode->i_blkbits;
- em->block_end = em->block_start +
- (ins.offset << inode->i_blkbits) - 1;
- set_extent_dirty(em_tree, start_pos, end_of_last_block,
- GFP_NOFS);
- err = add_extent_mapping(em_tree, em);
+ u64 last_end;
for (i = 0; i < num_pages; i++) {
struct page *p = pages[i];
SetPageUptodate(p);
- __set_page_dirty_nobuffers(p);
+ set_page_dirty(p);
}
+ last_end = pages[num_pages -1]->index << PAGE_CACHE_SHIFT;
+ last_end += PAGE_CACHE_SIZE - 1;
+ set_extent_delalloc(em_tree, start_pos, end_of_last_block,
+ GFP_NOFS);
} else {
struct page *p = pages[0];
+ /* step one, delete the existing extents in this range */
+ /* FIXME blocksize != pagesize */
+ if (start_pos < inode->i_size) {
+ err = btrfs_drop_extents(trans, root, inode, start_pos,
+ (pos + write_bytes + root->blocksize -1) &
+ ~((u64)root->blocksize - 1), &hint_block);
+ if (err)
+ goto failed;
+ }
+
err = insert_inline_extent(trans, root, inode, start_pos,
end_pos - start_pos, p, 0);
BUG_ON(err);
err = btrfs_end_transaction(trans, root);
out_unlock:
mutex_unlock(&root->fs_info->fs_mutex);
+ unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
free_extent_map(em);
return err;
}
u64 num_blocks;
u64 start_pos;
- start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
+ start_pos = pos & ~((u64)root->blocksize - 1);
num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
inode->i_blkbits;
if (!PagePrivate(pages[i])) {
SetPagePrivate(pages[i]);
set_page_private(pages[i], 1);
+ WARN_ON(!pages[i]->mapping->a_ops->invalidatepage);
page_cache_get(pages[i]);
}
+ WARN_ON(!PageLocked(pages[i]));
}
return 0;
}
[S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
};
+static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
+{
+ struct btrfs_root *root = BTRFS_I(inode)->root;
+ struct btrfs_trans_handle *trans;
+ struct btrfs_key ins;
+ u64 alloc_hint = 0;
+ u64 num_blocks;
+ int ret;
+ u64 blocksize = 1 << inode->i_blkbits;
+
+ mutex_lock(&root->fs_info->fs_mutex);
+ trans = btrfs_start_transaction(root, 1);
+ btrfs_set_trans_block_group(trans, inode);
+ BUG_ON(!trans);
+ num_blocks = (end - start + blocksize) & ~(blocksize - 1);
+ ret = btrfs_drop_extents(trans, root, inode,
+ start, start + num_blocks, &alloc_hint);
+ num_blocks = num_blocks >> inode->i_blkbits;
+ ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_blocks, 0,
+ alloc_hint, (u64)-1, &ins, 1);
+ if (ret) {
+ WARN_ON(1);
+ goto out;
+ }
+ ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
+ start, ins.objectid, ins.offset,
+ ins.offset);
+out:
+ btrfs_end_transaction(trans, root);
+ mutex_unlock(&root->fs_info->fs_mutex);
+ return ret;
+}
+
+
void btrfs_read_locked_inode(struct inode *inode)
{
struct btrfs_path *path;
switch (inode->i_mode & S_IFMT) {
case S_IFREG:
inode->i_mapping->a_ops = &btrfs_aops;
- extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
- inode->i_mapping, GFP_NOFS);
+ BTRFS_I(inode)->extent_tree.fill_delalloc = run_delalloc_range;
inode->i_fop = &btrfs_file_operations;
inode->i_op = &btrfs_file_inode_operations;
break;
BUG_ON(ret);
}
if (item_end < inode->i_size) {
- if (found_type) {
- btrfs_set_key_type(&key, found_type - 1);
- continue;
+ if (found_type == BTRFS_DIR_ITEM_KEY) {
+ found_type = BTRFS_INODE_ITEM_KEY;
+ } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
+ found_type = BTRFS_CSUM_ITEM_KEY;
+ } else if (found_type) {
+ found_type--;
+ } else {
+ break;
}
- break;
+ btrfs_set_key_type(&key, found_type - 1);
+ continue;
}
if (btrfs_disk_key_offset(found_key) >= inode->i_size)
del_item = 1;
return ret;
}
-static int btrfs_cow_one_page(struct btrfs_trans_handle *trans,
- struct inode *inode, struct page *page,
+static int btrfs_cow_one_page(struct inode *inode, struct page *page,
size_t zero_start)
{
char *kaddr;
int ret = 0;
- struct btrfs_root *root = BTRFS_I(inode)->root;
- u64 alloc_hint = 0;
+ struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
u64 page_start = page->index << PAGE_CACHE_SHIFT;
- struct btrfs_key ins;
+ u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
if (!PagePrivate(page)) {
SetPagePrivate(page);
set_page_private(page, 1);
+ WARN_ON(!page->mapping->a_ops->invalidatepage);
page_cache_get(page);
}
- btrfs_set_trans_block_group(trans, inode);
-
- ret = btrfs_drop_extents(trans, root, inode,
- page_start, page_start + PAGE_CACHE_SIZE,
- &alloc_hint);
- if (ret)
- goto out;
- ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1, 0,
- alloc_hint, (u64)-1, &ins, 1);
- if (ret)
- goto out;
- ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
- page_start, ins.objectid, 1, 1);
- if (ret)
- goto out;
- SetPageChecked(page);
- kaddr = kmap(page);
+ lock_extent(em_tree, page_start, page_end, GFP_NOFS);
+ set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
+ page_end, GFP_NOFS);
if (zero_start != PAGE_CACHE_SIZE) {
+ kaddr = kmap(page);
memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
flush_dcache_page(page);
+ kunmap(page);
}
- kunmap(page);
+ set_page_dirty(page);
+ unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
-out:
return ret;
}
static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
{
struct inode *inode = mapping->host;
- struct btrfs_root *root = BTRFS_I(inode)->root;
- struct btrfs_trans_handle *trans;
unsigned blocksize = 1 << inode->i_blkbits;
pgoff_t index = from >> PAGE_CACHE_SHIFT;
unsigned offset = from & (PAGE_CACHE_SIZE-1);
}
page_start = page->index << PAGE_CACHE_SHIFT;
- mutex_lock(&root->fs_info->fs_mutex);
- trans = btrfs_start_transaction(root, 1);
- ret = btrfs_cow_one_page(trans, inode, page, offset);
- if (!ret) {
- char *kaddr = kmap(page);
- ret = btrfs_csum_file_block(trans, root, inode->i_ino,
- page_start, kaddr, PAGE_CACHE_SIZE);
- kunmap(page);
- }
- set_extent_dirty(&BTRFS_I(inode)->extent_tree,
- page_start, page_start + PAGE_CACHE_SIZE - 1,
- GFP_NOFS);
- set_page_dirty(page);
- btrfs_end_transaction(trans, root);
- mutex_unlock(&root->fs_info->fs_mutex);
+ ret = btrfs_cow_one_page(inode, page, offset);
unlock_page(page);
page_cache_release(page);
struct btrfs_iget_args *args = p;
inode->i_ino = args->ino;
BTRFS_I(inode)->root = args->root;
+ extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
+ inode->i_mapping, GFP_NOFS);
return 0;
}
if (!inode)
return ERR_PTR(-ENOMEM);
+ extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
+ inode->i_mapping, GFP_NOFS);
BTRFS_I(inode)->root = root;
+
if (mode & S_IFDIR)
owner = 0;
else
inode->i_op = &btrfs_file_inode_operations;
extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
inode->i_mapping, GFP_NOFS);
+ BTRFS_I(inode)->extent_tree.fill_delalloc = run_delalloc_range;
}
dir->i_sb->s_dirt = 1;
btrfs_update_inode_block_group(trans, inode);
extent_end = extent_start +
(btrfs_file_extent_num_blocks(item) << inode->i_blkbits);
err = 0;
- if (start < extent_start || start > extent_end) {
+ if (start < extent_start || start >= extent_end) {
em->start = start;
if (start < extent_start) {
+ if (end < extent_start)
+ goto not_found;
em->end = extent_end - 1;
} else {
em->end = end;
size = btrfs_file_extent_inline_len(leaf->items +
path->slots[0]);
extent_end = extent_start + size;
- if (start < extent_start || start > extent_end) {
+ if (start < extent_start || start >= extent_end) {
em->start = start;
if (start < extent_start) {
+ if (end < extent_start)
+ goto not_found;
em->end = extent_end - 1;
} else {
em->end = end;
insert:
btrfs_release_path(root, path);
if (em->start > start || em->end < start) {
- printk("bad extent! %Lu %Lu start %Lu end %Lu\n", em->start, em->end, start, end);
- WARN_ON(1);
+ printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
err = -EIO;
goto out;
}
static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
{
struct extent_map_tree *tree;
+
+
+ if (current->flags & PF_MEMALLOC) {
+ redirty_page_for_writepage(wbc, page);
+ unlock_page(page);
+ return 0;
+ }
tree = &BTRFS_I(page->mapping->host)->extent_tree;
return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
}
int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
{
struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
- struct btrfs_root *root = BTRFS_I(inode)->root;
- struct btrfs_trans_handle *trans;
unsigned long end;
loff_t size;
int ret = -EINVAL;
else
end = PAGE_CACHE_SIZE;
- mutex_lock(&root->fs_info->fs_mutex);
- trans = btrfs_start_transaction(root, 1);
- ret = btrfs_cow_one_page(trans, inode, page, end);
- btrfs_end_transaction(trans, root);
- mutex_unlock(&root->fs_info->fs_mutex);
- set_extent_dirty(&BTRFS_I(inode)->extent_tree,
- page_start, page_start + PAGE_CACHE_SIZE - 1,
- GFP_NOFS);
- set_page_dirty(page);
+ ret = btrfs_cow_one_page(inode, page, end);
out_unlock:
unlock_page(page);
inode->i_op = &btrfs_file_inode_operations;
extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
inode->i_mapping, GFP_NOFS);
+ BTRFS_I(inode)->extent_tree.fill_delalloc = run_delalloc_range;
}
dir->i_sb->s_dirt = 1;
btrfs_update_inode_block_group(trans, inode);
bi->location.offset = 0;
bi->location.flags = 0;
bi->root = tree_root;
+
btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
if (!inode) {