Skip to content

Commit 16cdcec

Browse files
Miao Xiechrismason-xx
authored andcommitted
btrfs: implement delayed inode items operation
Changelog V5 -> V6: - Fix oom when the memory load is high, by storing the delayed nodes into the root's radix tree, and letting btrfs inodes go. Changelog V4 -> V5: - Fix the race on adding the delayed node to the inode, which is spotted by Chris Mason. - Merge Chris Mason's incremental patch into this patch. - Fix deadlock between readdir() and memory fault, which is reported by Itaru Kitayama. Changelog V3 -> V4: - Fix nested lock, which is reported by Itaru Kitayama, by updating space cache inode in time. Changelog V2 -> V3: - Fix the race between the delayed worker and the task which does delayed items balance, which is reported by Tsutomu Itoh. - Modify the patch address David Sterba's comment. - Fix the bug of the cpu recursion spinlock, reported by Chris Mason Changelog V1 -> V2: - break up the global rb-tree, use a list to manage the delayed nodes, which is created for every directory and file, and used to manage the delayed directory name index items and the delayed inode item. - introduce a worker to deal with the delayed nodes. Compare with Ext3/4, the performance of file creation and deletion on btrfs is very poor. the reason is that btrfs must do a lot of b+ tree insertions, such as inode item, directory name item, directory name index and so on. If we can do some delayed b+ tree insertion or deletion, we can improve the performance, so we made this patch which implemented delayed directory name index insertion/deletion and delayed inode update. Implementation: - introduce a delayed root object into the filesystem, that use two lists to manage the delayed nodes which are created for every file/directory. One is used to manage all the delayed nodes that have delayed items. And the other is used to manage the delayed nodes which is waiting to be dealt with by the work thread. - Every delayed node has two rb-tree, one is used to manage the directory name index which is going to be inserted into b+ tree, and the other is used to manage the directory name index which is going to be deleted from b+ tree. - introduce a worker to deal with the delayed operation. This worker is used to deal with the works of the delayed directory name index items insertion and deletion and the delayed inode update. When the delayed items is beyond the lower limit, we create works for some delayed nodes and insert them into the work queue of the worker, and then go back. When the delayed items is beyond the upper bound, we create works for all the delayed nodes that haven't been dealt with, and insert them into the work queue of the worker, and then wait for that the untreated items is below some threshold value. - When we want to insert a directory name index into b+ tree, we just add the information into the delayed inserting rb-tree. And then we check the number of the delayed items and do delayed items balance. (The balance policy is above.) - When we want to delete a directory name index from the b+ tree, we search it in the inserting rb-tree at first. If we look it up, just drop it. If not, add the key of it into the delayed deleting rb-tree. Similar to the delayed inserting rb-tree, we also check the number of the delayed items and do delayed items balance. (The same to inserting manipulation) - When we want to update the metadata of some inode, we cached the data of the inode into the delayed node. the worker will flush it into the b+ tree after dealing with the delayed insertion and deletion. - We will move the delayed node to the tail of the list after we access the delayed node, By this way, we can cache more delayed items and merge more inode updates. - If we want to commit transaction, we will deal with all the delayed node. - the delayed node will be freed when we free the btrfs inode. - Before we log the inode items, we commit all the directory name index items and the delayed inode update. I did a quick test by the benchmark tool[1] and found we can improve the performance of file creation by ~15%, and file deletion by ~20%. Before applying this patch: Create files: Total files: 50000 Total time: 1.096108 Average time: 0.000022 Delete files: Total files: 50000 Total time: 1.510403 Average time: 0.000030 After applying this patch: Create files: Total files: 50000 Total time: 0.932899 Average time: 0.000019 Delete files: Total files: 50000 Total time: 1.215732 Average time: 0.000024 [1] http://marc.info/?l=linux-btrfs&m=128212635122920&q=p3 Many thanks for Kitayama-san's help! Signed-off-by: Miao Xie <miaox@cn.fujitsu.com> Reviewed-by: David Sterba <dave@jikos.cz> Tested-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com> Tested-by: Itaru Kitayama <kitayama@cl.bb4u.ne.jp> Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 61c4f2c commit 16cdcec

16 files changed

+2074
-91
lines changed

fs/btrfs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
77
extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \
88
extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \
99
export.o tree-log.o acl.o free-space-cache.o zlib.o lzo.o \
10-
compression.o delayed-ref.o relocation.o
10+
compression.o delayed-ref.o relocation.o delayed-inode.o

fs/btrfs/btrfs_inode.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "extent_map.h"
2323
#include "extent_io.h"
2424
#include "ordered-data.h"
25+
#include "delayed-inode.h"
2526

2627
/* in memory btrfs inode */
2728
struct btrfs_inode {
@@ -158,9 +159,13 @@ struct btrfs_inode {
158159
*/
159160
unsigned force_compress:4;
160161

162+
struct btrfs_delayed_node *delayed_node;
163+
161164
struct inode vfs_inode;
162165
};
163166

167+
extern unsigned char btrfs_filetype_table[];
168+
164169
static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
165170
{
166171
return container_of(inode, struct btrfs_inode, vfs_inode);

fs/btrfs/ctree.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
3838
struct extent_buffer *src_buf);
3939
static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4040
struct btrfs_path *path, int level, int slot);
41-
static int setup_items_for_insert(struct btrfs_trans_handle *trans,
42-
struct btrfs_root *root, struct btrfs_path *path,
43-
struct btrfs_key *cpu_key, u32 *data_size,
44-
u32 total_data, u32 total_size, int nr);
45-
4641

4742
struct btrfs_path *btrfs_alloc_path(void)
4843
{
@@ -3559,11 +3554,10 @@ int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
35593554
* to save stack depth by doing the bulk of the work in a function
35603555
* that doesn't call btrfs_search_slot
35613556
*/
3562-
static noinline_for_stack int
3563-
setup_items_for_insert(struct btrfs_trans_handle *trans,
3564-
struct btrfs_root *root, struct btrfs_path *path,
3565-
struct btrfs_key *cpu_key, u32 *data_size,
3566-
u32 total_data, u32 total_size, int nr)
3557+
int setup_items_for_insert(struct btrfs_trans_handle *trans,
3558+
struct btrfs_root *root, struct btrfs_path *path,
3559+
struct btrfs_key *cpu_key, u32 *data_size,
3560+
u32 total_data, u32 total_size, int nr)
35673561
{
35683562
struct btrfs_item *item;
35693563
int i;

fs/btrfs/ctree.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ struct btrfs_block_group_cache {
869869
struct reloc_control;
870870
struct btrfs_device;
871871
struct btrfs_fs_devices;
872+
struct btrfs_delayed_root;
872873
struct btrfs_fs_info {
873874
u8 fsid[BTRFS_FSID_SIZE];
874875
u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
@@ -895,7 +896,10 @@ struct btrfs_fs_info {
895896
/* logical->physical extent mapping */
896897
struct btrfs_mapping_tree mapping_tree;
897898

898-
/* block reservation for extent, checksum and root tree */
899+
/*
900+
* block reservation for extent, checksum, root tree and
901+
* delayed dir index item
902+
*/
899903
struct btrfs_block_rsv global_block_rsv;
900904
/* block reservation for delay allocation */
901905
struct btrfs_block_rsv delalloc_block_rsv;
@@ -1022,6 +1026,7 @@ struct btrfs_fs_info {
10221026
* for the sys_munmap function call path
10231027
*/
10241028
struct btrfs_workers fixup_workers;
1029+
struct btrfs_workers delayed_workers;
10251030
struct task_struct *transaction_kthread;
10261031
struct task_struct *cleaner_kthread;
10271032
int thread_pool_size;
@@ -1079,6 +1084,8 @@ struct btrfs_fs_info {
10791084

10801085
/* filesystem state */
10811086
u64 fs_state;
1087+
1088+
struct btrfs_delayed_root *delayed_root;
10821089
};
10831090

10841091
/*
@@ -1161,6 +1168,11 @@ struct btrfs_root {
11611168
/* red-black tree that keeps track of in-memory inodes */
11621169
struct rb_root inode_tree;
11631170

1171+
/*
1172+
* radix tree that keeps track of delayed nodes of every inode,
1173+
* protected by inode_lock
1174+
*/
1175+
struct radix_tree_root delayed_nodes_tree;
11641176
/*
11651177
* right now this just gets used so that a root has its own devid
11661178
* for stat. It may be used for more later
@@ -2099,6 +2111,13 @@ static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
20992111
}
21002112

21012113
/* extent-tree.c */
2114+
static inline u64 btrfs_calc_trans_metadata_size(struct btrfs_root *root,
2115+
int num_items)
2116+
{
2117+
return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
2118+
3 * num_items;
2119+
}
2120+
21022121
void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
21032122
int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
21042123
struct btrfs_root *root, unsigned long count);
@@ -2294,6 +2313,8 @@ void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
22942313
struct btrfs_path *btrfs_alloc_path(void);
22952314
void btrfs_free_path(struct btrfs_path *p);
22962315
void btrfs_set_path_blocking(struct btrfs_path *p);
2316+
void btrfs_clear_path_blocking(struct btrfs_path *p,
2317+
struct extent_buffer *held);
22972318
void btrfs_unlock_up_safe(struct btrfs_path *p, int level);
22982319

22992320
int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
@@ -2305,6 +2326,10 @@ static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
23052326
return btrfs_del_items(trans, root, path, path->slots[0], 1);
23062327
}
23072328

2329+
int setup_items_for_insert(struct btrfs_trans_handle *trans,
2330+
struct btrfs_root *root, struct btrfs_path *path,
2331+
struct btrfs_key *cpu_key, u32 *data_size,
2332+
u32 total_data, u32 total_size, int nr);
23082333
int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
23092334
*root, struct btrfs_key *key, void *data, u32 data_size);
23102335
int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
@@ -2368,7 +2393,7 @@ void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
23682393
/* dir-item.c */
23692394
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
23702395
struct btrfs_root *root, const char *name,
2371-
int name_len, u64 dir,
2396+
int name_len, struct inode *dir,
23722397
struct btrfs_key *location, u8 type, u64 index);
23732398
struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
23742399
struct btrfs_root *root,

0 commit comments

Comments
 (0)