struct extent_buffer *eb;
eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
bytenr, blocksize, GFP_NOFS);
- if (eb)
- eb->alloc_addr = (unsigned long)__builtin_return_address(0);
return eb;
}
eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
bytenr, blocksize, GFP_NOFS);
- eb->alloc_addr = (unsigned long)__builtin_return_address(0);
return eb;
}
return NULL;
read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
buf, 1);
- buf->alloc_addr = (unsigned long)__builtin_return_address(0);
return buf;
}
btrfs_free_block_groups(root->fs_info);
del_fs_roots(fs_info);
+ extent_map_tree_cleanup(&BTRFS_I(fs_info->btree_inode)->extent_tree);
truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
iput(fs_info->btree_inode);
kfree(fs_info->extent_root);
int btrfs_buffer_uptodate(struct extent_buffer *buf)
{
- struct inode *btree_inode = buf->pages[0]->mapping->host;
+ struct inode *btree_inode = buf->last_page->mapping->host;
return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
}
int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
{
- struct inode *btree_inode = buf->pages[0]->mapping->host;
+ struct inode *btree_inode = buf->last_page->mapping->host;
return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
buf);
}
void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
u64 transid = btrfs_header_generation(buf);
struct inode *btree_inode = root->fs_info->btree_inode;
void btrfs_set_buffer_defrag(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
struct inode *btree_inode = root->fs_info->btree_inode;
set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
struct inode *btree_inode = root->fs_info->btree_inode;
set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
int btrfs_buffer_defrag(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
struct inode *btree_inode = root->fs_info->btree_inode;
return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
int btrfs_buffer_defrag_done(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
struct inode *btree_inode = root->fs_info->btree_inode;
return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
buf->start, buf->start + buf->len - 1,
int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
struct inode *btree_inode = root->fs_info->btree_inode;
return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
buf->start, buf->start + buf->len - 1,
int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
struct inode *btree_inode = root->fs_info->btree_inode;
return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
buf->start, buf->start + buf->len - 1,
int btrfs_read_buffer(struct extent_buffer *buf)
{
- struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
+ struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
struct inode *btree_inode = root->fs_info->btree_inode;
return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
buf, 1);
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/blkdev.h>
+#include <linux/swap.h>
#include "extent_map.h"
/* temporary define until extent_map moves out of btrfs */
static struct kmem_cache *extent_state_cache;
static struct kmem_cache *extent_buffer_cache;
-static LIST_HEAD(extent_buffers);
static LIST_HEAD(buffers);
static LIST_HEAD(states);
-static spinlock_t extent_buffers_lock;
static spinlock_t state_lock = SPIN_LOCK_UNLOCKED;
-static int nr_extent_buffers;
-#define MAX_EXTENT_BUFFER_CACHE 128
+#define BUFFER_LRU_MAX 64
struct tree_entry {
u64 start;
extent_buffer_cache = btrfs_cache_create("extent_buffers",
sizeof(struct extent_buffer), 0,
NULL);
- spin_lock_init(&extent_buffers_lock);
}
void __exit extent_map_exit(void)
{
- struct extent_buffer *eb;
struct extent_state *state;
- while (!list_empty(&extent_buffers)) {
- eb = list_entry(extent_buffers.next,
- struct extent_buffer, list);
- list_del(&eb->list);
- kmem_cache_free(extent_buffer_cache, eb);
- }
while (!list_empty(&states)) {
state = list_entry(states.next, struct extent_state, list);
printk("state leak: start %Lu end %Lu state %lu in tree %d refs %d\n", state->start, state->end, state->state, state->in_tree, atomic_read(&state->refs));
kmem_cache_free(extent_state_cache, state);
}
- while (!list_empty(&buffers)) {
- eb = list_entry(buffers.next,
- struct extent_buffer, leak_list);
- printk("buffer leak start %Lu len %lu return %lX\n", eb->start, eb->len, eb->alloc_addr);
- list_del(&eb->leak_list);
- kmem_cache_free(extent_buffer_cache, eb);
- }
-
if (extent_map_cache)
kmem_cache_destroy(extent_map_cache);
tree->state.rb_node = NULL;
tree->ops = NULL;
rwlock_init(&tree->lock);
+ spin_lock_init(&tree->lru_lock);
tree->mapping = mapping;
+ INIT_LIST_HEAD(&tree->buffer_lru);
+ tree->lru_size = 0;
}
EXPORT_SYMBOL(extent_map_tree_init);
+void extent_map_tree_cleanup(struct extent_map_tree *tree)
+{
+ struct extent_buffer *eb;
+ while(!list_empty(&tree->buffer_lru)) {
+ eb = list_entry(tree->buffer_lru.next, struct extent_buffer,
+ lru);
+ list_del(&eb->lru);
+ free_extent_buffer(eb);
+ }
+}
+EXPORT_SYMBOL(extent_map_tree_cleanup);
+
struct extent_map *alloc_extent_map(gfp_t mask)
{
struct extent_map *em;
return (em->block_start + start - em->start) >> inode->i_blkbits;
}
-static struct extent_buffer *__alloc_extent_buffer(gfp_t mask)
+static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb)
{
- struct extent_buffer *eb = NULL;
-
- spin_lock(&extent_buffers_lock);
- if (!list_empty(&extent_buffers)) {
- eb = list_entry(extent_buffers.next, struct extent_buffer,
- list);
- list_del(&eb->list);
- WARN_ON(nr_extent_buffers == 0);
- nr_extent_buffers--;
- }
- spin_unlock(&extent_buffers_lock);
+ if (list_empty(&eb->lru)) {
+ extent_buffer_get(eb);
+ list_add(&eb->lru, &tree->buffer_lru);
+ tree->lru_size++;
+ if (tree->lru_size >= BUFFER_LRU_MAX) {
+ struct extent_buffer *rm;
+ rm = list_entry(tree->buffer_lru.prev,
+ struct extent_buffer, lru);
+ tree->lru_size--;
+ list_del(&rm->lru);
+ free_extent_buffer(rm);
+ }
+ } else
+ list_move(&eb->lru, &tree->buffer_lru);
+ return 0;
+}
+static struct extent_buffer *find_lru(struct extent_map_tree *tree,
+ u64 start, unsigned long len)
+{
+ struct list_head *lru = &tree->buffer_lru;
+ struct list_head *cur = lru->next;
+ struct extent_buffer *eb;
- if (eb) {
- memset(eb, 0, sizeof(*eb));
- } else {
- eb = kmem_cache_zalloc(extent_buffer_cache, mask);
- }
- spin_lock(&extent_buffers_lock);
- list_add(&eb->leak_list, &buffers);
- spin_unlock(&extent_buffers_lock);
+ if (list_empty(lru))
+ return NULL;
- return eb;
+ do {
+ eb = list_entry(cur, struct extent_buffer, lru);
+ if (eb->start == start && eb->len == len) {
+ extent_buffer_get(eb);
+ return eb;
+ }
+ cur = cur->next;
+ } while (cur != lru);
+ return NULL;
}
-static void __free_extent_buffer(struct extent_buffer *eb)
+static inline unsigned long num_extent_pages(u64 start, u64 len)
{
-
- spin_lock(&extent_buffers_lock);
- list_del_init(&eb->leak_list);
- spin_unlock(&extent_buffers_lock);
-
- if (nr_extent_buffers >= MAX_EXTENT_BUFFER_CACHE) {
- kmem_cache_free(extent_buffer_cache, eb);
- } else {
- spin_lock(&extent_buffers_lock);
- list_add(&eb->list, &extent_buffers);
- nr_extent_buffers++;
- spin_unlock(&extent_buffers_lock);
- }
+ return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
+ (start >> PAGE_CACHE_SHIFT);
}
-static inline struct page *extent_buffer_page(struct extent_buffer *eb, int i)
+static inline struct page *extent_buffer_page(struct extent_buffer *eb,
+ unsigned long i)
{
struct page *p;
- if (i < EXTENT_INLINE_PAGES)
- return eb->pages[i];
+ if (i == 0)
+ return eb->last_page;
i += eb->start >> PAGE_CACHE_SHIFT;
- p = find_get_page(eb->pages[0]->mapping, i);
+ p = find_get_page(eb->last_page->mapping, i);
page_cache_release(p);
return p;
}
-static inline unsigned long num_extent_pages(u64 start, u64 len)
+static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree,
+ u64 start,
+ unsigned long len,
+ gfp_t mask)
{
- return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
- (start >> PAGE_CACHE_SHIFT);
+ struct extent_buffer *eb = NULL;
+
+ spin_lock(&tree->lru_lock);
+ eb = find_lru(tree, start, len);
+ if (eb)
+ goto lru_add;
+ spin_unlock(&tree->lru_lock);
+
+ if (eb) {
+ memset(eb, 0, sizeof(*eb));
+ } else {
+ eb = kmem_cache_zalloc(extent_buffer_cache, mask);
+ }
+ INIT_LIST_HEAD(&eb->lru);
+ eb->start = start;
+ eb->len = len;
+ atomic_set(&eb->refs, 1);
+
+ spin_lock(&tree->lru_lock);
+lru_add:
+ add_lru(tree, eb);
+ spin_unlock(&tree->lru_lock);
+ return eb;
+}
+
+static void __free_extent_buffer(struct extent_buffer *eb)
+{
+ kmem_cache_free(extent_buffer_cache, eb);
}
+
struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
u64 start, unsigned long len,
gfp_t mask)
struct address_space *mapping = tree->mapping;
int uptodate = 0;
- eb = __alloc_extent_buffer(mask);
+ eb = __alloc_extent_buffer(tree, start, len, mask);
if (!eb || IS_ERR(eb))
return NULL;
- eb->alloc_addr = (unsigned long)__builtin_return_address(0);
- eb->start = start;
- eb->len = len;
- atomic_set(&eb->refs, 1);
+ if (eb->flags & EXTENT_BUFFER_FILLED)
+ return eb;
for (i = 0; i < num_pages; i++, index++) {
p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
goto fail;
}
set_page_extent_mapped(p);
- if (i < EXTENT_INLINE_PAGES)
- eb->pages[i] = p;
+ if (i == 0)
+ eb->last_page = p;
if (!PageUptodate(p))
uptodate = 0;
unlock_page(p);
}
if (uptodate)
eb->flags |= EXTENT_UPTODATE;
+ eb->flags |= EXTENT_BUFFER_FILLED;
return eb;
fail:
free_extent_buffer(eb);
struct address_space *mapping = tree->mapping;
int uptodate = 1;
- eb = __alloc_extent_buffer(mask);
+ eb = __alloc_extent_buffer(tree, start, len, mask);
if (!eb || IS_ERR(eb))
return NULL;
- eb->alloc_addr = (unsigned long)__builtin_return_address(0);
- eb->start = start;
- eb->len = len;
- atomic_set(&eb->refs, 1);
+ if (eb->flags & EXTENT_BUFFER_FILLED)
+ return eb;
for (i = 0; i < num_pages; i++, index++) {
p = find_lock_page(mapping, index);
goto fail;
}
set_page_extent_mapped(p);
- if (i < EXTENT_INLINE_PAGES)
- eb->pages[i] = p;
+ if (i == 0)
+ eb->last_page = p;
if (!PageUptodate(p))
uptodate = 0;
unlock_page(p);
}
if (uptodate)
eb->flags |= EXTENT_UPTODATE;
+ eb->flags |= EXTENT_BUFFER_FILLED;
return eb;
fail:
free_extent_buffer(eb);
ret = -EIO;
}
}
- eb->flags |= EXTENT_UPTODATE;
+ if (!ret)
+ eb->flags |= EXTENT_UPTODATE;
return ret;
}
EXPORT_SYMBOL(read_extent_buffer_pages);