Skip to content

Commit 581bb05

Browse files
author
Li Zefan
committed
Btrfs: Cache free inode numbers in memory
Currently btrfs stores the highest objectid of the fs tree, and it always returns (highest+1) inode number when we create a file, so inode numbers won't be reclaimed when we delete files, so we'll run out of inode numbers as we keep create/delete files in 32bits machines. This fixes it, and it works similarly to how we cache free space in block cgroups. We start a kernel thread to read the file tree. By scanning inode items, we know which chunks of inode numbers are free, and we cache them in an rb-tree. Because we are searching the commit root, we have to carefully handle the cross-transaction case. The rb-tree is a hybrid extent+bitmap tree, so if we have too many small chunks of inode numbers, we'll use bitmaps. Initially we allow 16K ram of extents, and a bitmap will be used if we exceed this threshold. The extents threshold is adjusted in runtime. Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
1 parent 34d52cb commit 581bb05

10 files changed

Lines changed: 500 additions & 53 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,15 @@ struct btrfs_root {
11021102
spinlock_t accounting_lock;
11031103
struct btrfs_block_rsv *block_rsv;
11041104

1105+
/* free ino cache stuff */
1106+
struct mutex fs_commit_mutex;
1107+
struct btrfs_free_space_ctl *free_ino_ctl;
1108+
enum btrfs_caching_type cached;
1109+
spinlock_t cache_lock;
1110+
wait_queue_head_t cache_wait;
1111+
struct btrfs_free_space_ctl *free_ino_pinned;
1112+
u64 cache_progress;
1113+
11051114
struct mutex log_mutex;
11061115
wait_queue_head_t log_writer_wait;
11071116
wait_queue_head_t log_commit_wait[2];
@@ -2408,12 +2417,6 @@ int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
24082417
struct btrfs_root *root, u64 offset);
24092418
int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
24102419

2411-
/* inode-map.c */
2412-
int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
2413-
struct btrfs_root *fs_root,
2414-
u64 dirid, u64 *objectid);
2415-
int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
2416-
24172420
/* inode-item.c */
24182421
int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
24192422
struct btrfs_root *root,

‎fs/btrfs/disk-io.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "locking.h"
4242
#include "tree-log.h"
4343
#include "free-space-cache.h"
44+
#include "inode-map.h"
4445

4546
static struct extent_io_ops btree_extent_io_ops;
4647
static void end_workqueue_fn(struct btrfs_work *work);
@@ -1327,6 +1328,19 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
13271328
if (IS_ERR(root))
13281329
return root;
13291330

1331+
root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
1332+
if (!root->free_ino_ctl)
1333+
goto fail;
1334+
root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
1335+
GFP_NOFS);
1336+
if (!root->free_ino_pinned)
1337+
goto fail;
1338+
1339+
btrfs_init_free_ino_ctl(root);
1340+
mutex_init(&root->fs_commit_mutex);
1341+
spin_lock_init(&root->cache_lock);
1342+
init_waitqueue_head(&root->cache_wait);
1343+
13301344
set_anon_super(&root->anon_super, NULL);
13311345

13321346
if (btrfs_root_refs(&root->root_item) == 0) {
@@ -2483,6 +2497,8 @@ int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
24832497
if (btrfs_root_refs(&root->root_item) == 0)
24842498
synchronize_srcu(&fs_info->subvol_srcu);
24852499

2500+
__btrfs_remove_free_space_cache(root->free_ino_pinned);
2501+
__btrfs_remove_free_space_cache(root->free_ino_ctl);
24862502
free_fs_root(root);
24872503
return 0;
24882504
}
@@ -2496,6 +2512,8 @@ static void free_fs_root(struct btrfs_root *root)
24962512
}
24972513
free_extent_buffer(root->node);
24982514
free_extent_buffer(root->commit_root);
2515+
kfree(root->free_ino_ctl);
2516+
kfree(root->free_ino_pinned);
24992517
kfree(root->name);
25002518
kfree(root);
25012519
}

‎fs/btrfs/free-space-cache.c‎

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "transaction.h"
2626
#include "disk-io.h"
2727
#include "extent_io.h"
28+
#include "inode-map.h"
2829

2930
#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
3031
#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
@@ -105,7 +106,7 @@ int create_free_space_inode(struct btrfs_root *root,
105106
u64 objectid;
106107
int ret;
107108

108-
ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
109+
ret = btrfs_find_free_objectid(root, &objectid);
109110
if (ret < 0)
110111
return ret;
111112

@@ -1496,10 +1497,9 @@ bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
14961497
return merged;
14971498
}
14981499

1499-
int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
1500-
u64 offset, u64 bytes)
1500+
int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1501+
u64 offset, u64 bytes)
15011502
{
1502-
struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
15031503
struct btrfs_free_space *info;
15041504
int ret = 0;
15051505

@@ -1751,11 +1751,29 @@ __btrfs_return_cluster_to_free_space(
17511751
return 0;
17521752
}
17531753

1754-
void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1754+
void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
17551755
{
1756-
struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
17571756
struct btrfs_free_space *info;
17581757
struct rb_node *node;
1758+
1759+
spin_lock(&ctl->tree_lock);
1760+
while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1761+
info = rb_entry(node, struct btrfs_free_space, offset_index);
1762+
unlink_free_space(ctl, info);
1763+
kfree(info->bitmap);
1764+
kmem_cache_free(btrfs_free_space_cachep, info);
1765+
if (need_resched()) {
1766+
spin_unlock(&ctl->tree_lock);
1767+
cond_resched();
1768+
spin_lock(&ctl->tree_lock);
1769+
}
1770+
}
1771+
spin_unlock(&ctl->tree_lock);
1772+
}
1773+
1774+
void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1775+
{
1776+
struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
17591777
struct btrfs_free_cluster *cluster;
17601778
struct list_head *head;
17611779

@@ -1773,21 +1791,9 @@ void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
17731791
spin_lock(&ctl->tree_lock);
17741792
}
17751793
}
1776-
1777-
while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1778-
info = rb_entry(node, struct btrfs_free_space, offset_index);
1779-
unlink_free_space(ctl, info);
1780-
if (info->bitmap)
1781-
kfree(info->bitmap);
1782-
kmem_cache_free(btrfs_free_space_cachep, info);
1783-
if (need_resched()) {
1784-
spin_unlock(&ctl->tree_lock);
1785-
cond_resched();
1786-
spin_lock(&ctl->tree_lock);
1787-
}
1788-
}
1789-
17901794
spin_unlock(&ctl->tree_lock);
1795+
1796+
__btrfs_remove_free_space_cache(ctl);
17911797
}
17921798

17931799
u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
@@ -2352,3 +2358,53 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
23522358

23532359
return ret;
23542360
}
2361+
2362+
/*
2363+
* Find the left-most item in the cache tree, and then return the
2364+
* smallest inode number in the item.
2365+
*
2366+
* Note: the returned inode number may not be the smallest one in
2367+
* the tree, if the left-most item is a bitmap.
2368+
*/
2369+
u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2370+
{
2371+
struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2372+
struct btrfs_free_space *entry = NULL;
2373+
u64 ino = 0;
2374+
2375+
spin_lock(&ctl->tree_lock);
2376+
2377+
if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2378+
goto out;
2379+
2380+
entry = rb_entry(rb_first(&ctl->free_space_offset),
2381+
struct btrfs_free_space, offset_index);
2382+
2383+
if (!entry->bitmap) {
2384+
ino = entry->offset;
2385+
2386+
unlink_free_space(ctl, entry);
2387+
entry->offset++;
2388+
entry->bytes--;
2389+
if (!entry->bytes)
2390+
kmem_cache_free(btrfs_free_space_cachep, entry);
2391+
else
2392+
link_free_space(ctl, entry);
2393+
} else {
2394+
u64 offset = 0;
2395+
u64 count = 1;
2396+
int ret;
2397+
2398+
ret = search_bitmap(ctl, entry, &offset, &count);
2399+
BUG_ON(ret);
2400+
2401+
ino = offset;
2402+
bitmap_clear_bits(ctl, entry, offset, 1);
2403+
if (entry->bytes == 0)
2404+
free_bitmap(ctl, entry);
2405+
}
2406+
out:
2407+
spin_unlock(&ctl->tree_lock);
2408+
2409+
return ino;
2410+
}

‎fs/btrfs/free-space-cache.h‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,25 @@ int btrfs_write_out_cache(struct btrfs_root *root,
6464
struct btrfs_trans_handle *trans,
6565
struct btrfs_block_group_cache *block_group,
6666
struct btrfs_path *path);
67+
6768
void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group);
68-
int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
69-
u64 bytenr, u64 size);
69+
int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
70+
u64 bytenr, u64 size);
71+
static inline int
72+
btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
73+
u64 bytenr, u64 size)
74+
{
75+
return __btrfs_add_free_space(block_group->free_space_ctl,
76+
bytenr, size);
77+
}
7078
int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
7179
u64 bytenr, u64 size);
80+
void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl);
7281
void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
73-
*block_group);
82+
*block_group);
7483
u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
7584
u64 offset, u64 bytes, u64 empty_size);
85+
u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root);
7686
void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
7787
u64 bytes);
7888
int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,

0 commit comments

Comments
 (0)