Skip to content

Commit

Permalink
Btrfs: Support reading/writing on disk free ino cache
Browse files Browse the repository at this point in the history
This is similar to block group caching.

We dedicate a special inode in fs tree to save free ino cache.

At the very first time we create/delete a file after mount, the free ino
cache will be loaded from disk into memory. When the fs tree is commited,
the cache will be written back to disk.

To keep compatibility, we check the root generation against the generation
of the special inode when loading the cache, so the loading will fail
if the btrfs filesystem was mounted in an older kernel before.

Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
  • Loading branch information
Li Zefan committed Apr 25, 2011
1 parent 33345d0 commit 82d5902
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 19 deletions.
7 changes: 7 additions & 0 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ struct btrfs_ordered_sum;
/* For storing free space cache */
#define BTRFS_FREE_SPACE_OBJECTID -11ULL

/*
* The inode number assigned to the special inode for sotring
* free ino cache
*/
#define BTRFS_FREE_INO_OBJECTID -12ULL

/* dummy objectid represents multiple objectids */
#define BTRFS_MULTIPLE_OBJECTIDS -255ULL

Expand Down Expand Up @@ -1110,6 +1116,7 @@ struct btrfs_root {
wait_queue_head_t cache_wait;
struct btrfs_free_space_ctl *free_ino_pinned;
u64 cache_progress;
struct inode *cache_inode;

struct mutex log_mutex;
wait_queue_head_t log_writer_wait;
Expand Down
1 change: 1 addition & 0 deletions fs/btrfs/disk-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,7 @@ int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)

static void free_fs_root(struct btrfs_root *root)
{
iput(root->cache_inode);
WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
if (root->anon_super.s_dev) {
down_write(&root->anon_super.s_umount);
Expand Down
3 changes: 2 additions & 1 deletion fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3145,7 +3145,8 @@ int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
/* make sure bytes are sectorsize aligned */
bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);

if (root == root->fs_info->tree_root) {
if (root == root->fs_info->tree_root ||
BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
alloc_chunk = 0;
committed = 1;
}
Expand Down
97 changes: 96 additions & 1 deletion fs/btrfs/free-space-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ int btrfs_truncate_free_space_cache(struct btrfs_root *root,
return ret;
}

return btrfs_update_inode(trans, root, inode);
ret = btrfs_update_inode(trans, root, inode);
return ret;
}

static int readahead_cache(struct inode *inode)
Expand Down Expand Up @@ -525,6 +526,7 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info,
spin_lock(&block_group->lock);
block_group->disk_cache_state = BTRFS_DC_CLEAR;
spin_unlock(&block_group->lock);
ret = 0;

printk(KERN_ERR "btrfs: failed to load free space cache "
"for block group %llu\n", block_group->key.objectid);
Expand Down Expand Up @@ -893,6 +895,7 @@ int btrfs_write_out_cache(struct btrfs_root *root,
spin_lock(&block_group->lock);
block_group->disk_cache_state = BTRFS_DC_ERROR;
spin_unlock(&block_group->lock);
ret = 0;

printk(KERN_ERR "btrfs: failed to write free space cace "
"for block group %llu\n", block_group->key.objectid);
Expand Down Expand Up @@ -2458,3 +2461,95 @@ u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)

return ino;
}

struct inode *lookup_free_ino_inode(struct btrfs_root *root,
struct btrfs_path *path)
{
struct inode *inode = NULL;

spin_lock(&root->cache_lock);
if (root->cache_inode)
inode = igrab(root->cache_inode);
spin_unlock(&root->cache_lock);
if (inode)
return inode;

inode = __lookup_free_space_inode(root, path, 0);
if (IS_ERR(inode))
return inode;

spin_lock(&root->cache_lock);
if (!root->fs_info->closing)
root->cache_inode = igrab(inode);
spin_unlock(&root->cache_lock);

return inode;
}

int create_free_ino_inode(struct btrfs_root *root,
struct btrfs_trans_handle *trans,
struct btrfs_path *path)
{
return __create_free_space_inode(root, trans, path,
BTRFS_FREE_INO_OBJECTID, 0);
}

int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
{
struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
struct btrfs_path *path;
struct inode *inode;
int ret = 0;
u64 root_gen = btrfs_root_generation(&root->root_item);

/*
* If we're unmounting then just return, since this does a search on the
* normal root and not the commit root and we could deadlock.
*/
smp_mb();
if (fs_info->closing)
return 0;

path = btrfs_alloc_path();
if (!path)
return 0;

inode = lookup_free_ino_inode(root, path);
if (IS_ERR(inode))
goto out;

if (root_gen != BTRFS_I(inode)->generation)
goto out_put;

ret = __load_free_space_cache(root, inode, ctl, path, 0);

if (ret < 0)
printk(KERN_ERR "btrfs: failed to load free ino cache for "
"root %llu\n", root->root_key.objectid);
out_put:
iput(inode);
out:
btrfs_free_path(path);
return ret;
}

int btrfs_write_out_ino_cache(struct btrfs_root *root,
struct btrfs_trans_handle *trans,
struct btrfs_path *path)
{
struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
struct inode *inode;
int ret;

inode = lookup_free_ino_inode(root, path);
if (IS_ERR(inode))
return 0;

ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
if (ret < 0)
printk(KERN_ERR "btrfs: failed to write free ino cache "
"for root %llu\n", root->root_key.objectid);

iput(inode);
return ret;
}
11 changes: 11 additions & 0 deletions fs/btrfs/free-space-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ int btrfs_write_out_cache(struct btrfs_root *root,
struct btrfs_block_group_cache *block_group,
struct btrfs_path *path);

struct inode *lookup_free_ino_inode(struct btrfs_root *root,
struct btrfs_path *path);
int create_free_ino_inode(struct btrfs_root *root,
struct btrfs_trans_handle *trans,
struct btrfs_path *path);
int load_free_ino_cache(struct btrfs_fs_info *fs_info,
struct btrfs_root *root);
int btrfs_write_out_ino_cache(struct btrfs_root *root,
struct btrfs_trans_handle *trans,
struct btrfs_path *path);

void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group);
int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
u64 bytenr, u64 size);
Expand Down
87 changes: 87 additions & 0 deletions fs/btrfs/inode-map.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static int caching_kthread(void *data)
static void start_caching(struct btrfs_root *root)
{
struct task_struct *tsk;
int ret;

spin_lock(&root->cache_lock);
if (root->cached != BTRFS_CACHE_NO) {
Expand All @@ -147,6 +148,14 @@ static void start_caching(struct btrfs_root *root)
root->cached = BTRFS_CACHE_STARTED;
spin_unlock(&root->cache_lock);

ret = load_free_ino_cache(root->fs_info, root);
if (ret == 1) {
spin_lock(&root->cache_lock);
root->cached = BTRFS_CACHE_FINISHED;
spin_unlock(&root->cache_lock);
return;
}

tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu\n",
root->root_key.objectid);
BUG_ON(IS_ERR(tsk));
Expand Down Expand Up @@ -352,6 +361,84 @@ void btrfs_init_free_ino_ctl(struct btrfs_root *root)
pinned->op = &pinned_free_ino_op;
}

int btrfs_save_ino_cache(struct btrfs_root *root,
struct btrfs_trans_handle *trans)
{
struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
struct btrfs_path *path;
struct inode *inode;
u64 alloc_hint = 0;
int ret;
int prealloc;
bool retry = false;

path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
again:
inode = lookup_free_ino_inode(root, path);
if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
ret = PTR_ERR(inode);
goto out;
}

if (IS_ERR(inode)) {
BUG_ON(retry);
retry = true;

ret = create_free_ino_inode(root, trans, path);
if (ret)
goto out;
goto again;
}

BTRFS_I(inode)->generation = 0;
ret = btrfs_update_inode(trans, root, inode);
WARN_ON(ret);

if (i_size_read(inode) > 0) {
ret = btrfs_truncate_free_space_cache(root, trans, path, inode);
if (ret)
goto out_put;
}

spin_lock(&root->cache_lock);
if (root->cached != BTRFS_CACHE_FINISHED) {
ret = -1;
spin_unlock(&root->cache_lock);
goto out_put;
}
spin_unlock(&root->cache_lock);

spin_lock(&ctl->tree_lock);
prealloc = sizeof(struct btrfs_free_space) * ctl->free_extents;
prealloc = ALIGN(prealloc, PAGE_CACHE_SIZE);
prealloc += ctl->total_bitmaps * PAGE_CACHE_SIZE;
spin_unlock(&ctl->tree_lock);

/* Just to make sure we have enough space */
prealloc += 8 * PAGE_CACHE_SIZE;

ret = btrfs_check_data_free_space(inode, prealloc);
if (ret)
goto out_put;

ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
prealloc, prealloc, &alloc_hint);
if (ret)
goto out_put;
btrfs_free_reserved_data_space(inode, prealloc);

out_put:
iput(inode);
out:
if (ret == 0)
ret = btrfs_write_out_ino_cache(root, trans, path);

btrfs_free_path(path);
return ret;
}

static int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
{
struct btrfs_path *path;
Expand Down
2 changes: 2 additions & 0 deletions fs/btrfs/inode-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ void btrfs_init_free_ino_ctl(struct btrfs_root *root);
void btrfs_unpin_free_ino(struct btrfs_root *root);
void btrfs_return_ino(struct btrfs_root *root, u64 objectid);
int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid);
int btrfs_save_ino_cache(struct btrfs_root *root,
struct btrfs_trans_handle *trans);

int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid);

Expand Down
Loading

0 comments on commit 82d5902

Please sign in to comment.