Btrfs: fixes and cleanups for 3.1
cover Btrfs: fixes and cleanups for 3.1
The first 4 patches are bug-fixes, and the remaining are small cleanups that have sit in my git tree for some time.
The first 3 patches have been sent to the list before.
We save some bytes after this patchset:
text data bss dec hex filename 426387 3854 1024 431265 694a1 fs/btrfs/btrfs.o.orig 425651 3854 1024 430529 691c1 fs/btrfs/btrfs.o
I've run xfstests for testing.
===
Li Zefan (12):
Btrfs: copy string correctly in INO_LOOKUP ioctl
Btrfs: fix space leak when skipping small extents during trimming
Btrfs: fix space leak when trimming free extents
Btrfs: check the nodatasum flag when writing compressed files
Btrfs: use wait_event()
Btrfs: remove a BUG_ON() in btrfs_commit_transaction()
Btrfs: remove remaining ref-cache code
Btrfs: make acl functions really no-op if acl is not enabled
Btrfs: remove redundant code for dir item lookup
Btrfs: clean up search_extent_mapping()
Btrfs: clean up code for extent_map lookup
Btrfs: clean up code for merging extent maps
Xiao Guangrong (4):
Btrfs: remove unused members from struct extent_state
Btrfs: clean up for insert_state()
Btrfs: clean up for wait_extent_bit()
Btrfs: clean up for find_first_extent_bit()
fs/btrfs/Makefile | 4 +- fs/btrfs/acl.c | 17 ----- fs/btrfs/compression.c | 14 +++- fs/btrfs/ctree.h | 15 ++++- fs/btrfs/dir-item.c | 30 +-------- fs/btrfs/extent_io.c | 80 ++++++++-------------- fs/btrfs/extent_io.h | 2 - fs/btrfs/extent_map.c | 155 ++++++++++++++----------------------------- fs/btrfs/free-space-cache.c | 43 +++++++----- fs/btrfs/ioctl.c | 3 +- fs/btrfs/ref-cache.c | 68 ------------------- fs/btrfs/ref-cache.h | 52 -------------- fs/btrfs/transaction.c | 65 +++---------------- 13 files changed, 142 insertions(+), 406 deletions(-) delete mode 100644 fs/btrfs/ref-cache.c delete mode 100644 fs/btrfs/ref-cache.h
PATCH 1/16 Btrfs: copy string correctly in INO_LOOKUP ioctl
Memory areas [ptr, ptr+total_len] and [name, name+total_len] may overlap, so it's wrong to use memcpy().
--- fs/btrfs/ioctl.c | 3 +-- 1 files changed, 1 insertions(+), 2 deletions(-)
fs/btrfs/ioctl.c +1 -2
@@ -1755,11 +1755,10 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, key.objectid = key.offset; key.offset = (u64)-1 ; dirid = key.objectid; - }if (ptr < name) goto out; -memcpy (name, ptr, total_len); +memmove (name, ptr, total_len); name[total_len]='\0' ; ret =0 ; out:
Signature · 1 line
1.7.3.1
PATCH 2/16 Btrfs: fix space leak when skipping small extents during trimming
In reply to Li ZefanWe're taking a free space extent out of the free space cache, trimming it and then putting it back into the cache.
However for an extent that is smaller than the specified minimum length, it's taken out but won't be put back, which causes space leak.
--- fs/btrfs/free-space-cache.c | 34 +++++++++++++++++----------------- 1 files changed, 17 insertions(+), 17 deletions(-)
fs/btrfs/free-space-cache.c +17 -17
@@ -2463,6 +2463,7 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,u64 bytes =0 ;u64 actually_trimmed;int ret =0 ; +int update_ret; *trimmed =0 ;
@@ -2486,6 +2487,7 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, }if (entry->bitmap) { + bytes =0 ; ret =search_bitmap (ctl, entry, &start, &bytes);if (!ret) {if (start >= end) {
@@ -2493,6 +2495,8 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,break ; } bytes =min (bytes, end - start); +if (bytes < minlen) + goto next;bitmap_clear_bits (ctl, entry, start, bytes);if (entry->bytes ==0 )free_bitmap (ctl, entry);
@@ -2506,33 +2510,29 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, }else { start = entry->offset; bytes =min (entry->bytes, end - start); +if (bytes < minlen) + goto next;unlink_free_space (ctl, entry);kmem_cache_free (btrfs_free_space_cachep, entry); }spin_unlock (&ctl->tree_lock); -if (bytes >= minlen) { -int update_ret; - update_ret =btrfs_update_reserved_bytes (block_group, - bytes,1 ,1 ); + update_ret =btrfs_update_reserved_bytes (block_group, + bytes,1 ,1 ); - ret =btrfs_error_discard_extent (fs_info->extent_root, - start, - bytes, - &actually_trimmed); + ret =btrfs_error_discard_extent (fs_info->extent_root, start, + bytes, &actually_trimmed); -btrfs_add_free_space (block_group, start, bytes); -if (!update_ret) -btrfs_update_reserved_bytes (block_group, - bytes,0 ,1 ); +btrfs_add_free_space (block_group, start, bytes); +if (!update_ret) +btrfs_update_reserved_bytes (block_group, bytes,0 ,1 ); -if (ret) -break ; - *trimmed += actually_trimmed; - } +if (ret) +break ; + *trimmed += actually_trimmed; +next: start += bytes; - bytes =0 ;if (fatal_signal_pending (current)) { ret = -ERESTARTSYS ;
Signature · 1 line
1.7.3.1
PATCH 3/16 Btrfs: fix space leak when trimming free extents
In reply to Li ZefanWhen the end of an extent exceeds the end of the specified range, the extent will be accidentally truncated.
--- fs/btrfs/free-space-cache.c | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)
fs/btrfs/free-space-cache.c +8 -1
@@ -2512,8 +2512,15 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, bytes =min (entry->bytes, end - start);if (bytes < minlen) goto next; +unlink_free_space (ctl, entry); -kmem_cache_free (btrfs_free_space_cachep, entry); +if (bytes < entry->bytes) { + entry->offset = entry->offset + bytes; + entry->bytes = entry->bytes - bytes; +link_free_space (ctl, entry); + }else { +kmem_cache_free (btrfs_free_space_cachep, entry); + } }spin_unlock (&ctl->tree_lock);
Signature · 1 line
1.7.3.1
PATCH 4/16 Btrfs: check the nodatasum flag when writing compressed files
In reply to Li ZefanIf mounting with nodatasum option, we won't csum file data for general write or direct-io write, and this rule should also be applied when writing compressed files.
--- fs/btrfs/compression.c | 14 ++++++++++---- 1 files changed, 10 insertions(+), 4 deletions(-)
fs/btrfs/compression.c +10 -4
@@ -338,6 +338,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,u64 first_byte = disk_start;struct block_device *bdev;int ret; +int skip_sum =BTRFS_I (inode)->flags &BTRFS_INODE_NODATASUM ;WARN_ON (start & ((u64 )PAGE_CACHE_SIZE -1 )); cb =kmalloc (compressed_bio_size (root, compressed_len),GFP_NOFS );
@@ -392,8 +393,11 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start, ret =btrfs_bio_wq_end_io (root->fs_info, bio,0 );BUG_ON (ret); - ret =btrfs_csum_one_bio (root, inode, bio, start,1 ); -BUG_ON (ret); +if (!skip_sum) { + ret =btrfs_csum_one_bio (root, inode, bio, + start,1 ); +BUG_ON (ret); + } ret =btrfs_map_bio (root,WRITE , bio,0 ,1 );BUG_ON (ret);
@@ -418,8 +422,10 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start, ret =btrfs_bio_wq_end_io (root->fs_info, bio,0 );BUG_ON (ret); - ret =btrfs_csum_one_bio (root, inode, bio, start,1 ); -BUG_ON (ret); +if (!skip_sum) { + ret =btrfs_csum_one_bio (root, inode, bio, start,1 ); +BUG_ON (ret); + } ret =btrfs_map_bio (root,WRITE , bio,0 ,1 );BUG_ON (ret);
Signature · 1 line
1.7.3.1
PATCH 5/16 Btrfs: use wait_event()
In reply to Li ZefanUse wait_event() when possible to avoid code duplication.
--- fs/btrfs/transaction.c | 59 +++++------------------------------------------ 1 files changed, 7 insertions(+), 52 deletions(-)
fs/btrfs/transaction.c +7 -52
@@ -216,17 +216,11 @@ static void wait_current_trans(struct btrfs_root *root)spin_lock (&root->fs_info->trans_lock); cur_trans = root->fs_info->running_transaction;if (cur_trans && cur_trans->blocked) { -DEFINE_WAIT (wait);atomic_inc (&cur_trans->use_count);spin_unlock (&root->fs_info->trans_lock); -while (1 ) { -prepare_to_wait (&root->fs_info->transaction_wait, &wait, -TASK_UNINTERRUPTIBLE ); -if (!cur_trans->blocked) -break ; -schedule (); - } -finish_wait (&root->fs_info->transaction_wait, &wait); + +wait_event (root->fs_info->transaction_wait, + !cur_trans->blocked);put_transaction (cur_trans); }else {spin_unlock (&root->fs_info->trans_lock);
@@ -362,15 +356,7 @@ struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root static noinline int wait_for_commit(struct btrfs_root *root,struct btrfs_transaction *commit) { -DEFINE_WAIT (wait); -while (!commit->commit_done) { -prepare_to_wait (&commit->commit_wait, &wait, -TASK_UNINTERRUPTIBLE ); -if (commit->commit_done) -break ; -schedule (); - } -finish_wait (&commit->commit_wait, &wait); +wait_event (commit->commit_wait, commit->commit_done);return 0 ; }
@@ -1080,22 +1066,7 @@ int btrfs_transaction_blocked(struct btrfs_fs_info *info)static void wait_current_trans_commit_start (struct btrfs_root *root,struct btrfs_transaction *trans) { -DEFINE_WAIT (wait); - -if (trans->in_commit) -return ; - -while (1 ) { -prepare_to_wait (&root->fs_info->transaction_blocked_wait, &wait, -TASK_UNINTERRUPTIBLE ); -if (trans->in_commit) { -finish_wait (&root->fs_info->transaction_blocked_wait, - &wait); -break ; - } -schedule (); -finish_wait (&root->fs_info->transaction_blocked_wait, &wait); - } +wait_event (root->fs_info->transaction_blocked_wait, trans->in_commit); }/*
@@ -1105,24 +1076,8 @@ static void wait_current_trans_commit_start(struct btrfs_root *root,static void wait_current_trans_commit_start_and_unblock (struct btrfs_root *root,struct btrfs_transaction *trans) { -DEFINE_WAIT (wait); - -if (trans->commit_done || (trans->in_commit && !trans->blocked)) -return ; - -while (1 ) { -prepare_to_wait (&root->fs_info->transaction_wait, &wait, -TASK_UNINTERRUPTIBLE ); -if (trans->commit_done || - (trans->in_commit && !trans->blocked)) { -finish_wait (&root->fs_info->transaction_wait, - &wait); -break ; - } -schedule (); -finish_wait (&root->fs_info->transaction_wait, - &wait); - } +wait_event (root->fs_info->transaction_wait, + trans->commit_done || (trans->in_commit && !trans->blocked)); }/*
Signature · 1 line
1.7.3.1
PATCH 6/16 Btrfs: remove a BUG_ON() in btrfs_commit_transaction()
In reply to Li Zefanwait_for_commit() always returns 0.
--- fs/btrfs/transaction.c | 6 ++---- 1 files changed, 2 insertions(+), 4 deletions(-)
fs/btrfs/transaction.c +2 -4
@@ -353,11 +353,10 @@ struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root }/* wait for a transaction commit to be fully complete */ -static noinline int wait_for_commit(struct btrfs_root *root, +static noinline void wait_for_commit(struct btrfs_root *root,struct btrfs_transaction *commit) {wait_event (commit->commit_wait, commit->commit_done); -return 0 ; }int btrfs_wait_for_commit (struct btrfs_root *root,u64 transid)
@@ -1184,8 +1183,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,atomic_inc (&cur_trans->use_count);btrfs_end_transaction (trans, root); - ret =wait_for_commit (root, cur_trans); -BUG_ON (ret); +wait_for_commit (root, cur_trans);put_transaction (cur_trans);
Signature · 1 line
1.7.3.1
PATCH 7/16 Btrfs: remove remaining ref-cache code
In reply to Li ZefanSince commit f2a97a9dbd86eb1ef956bdf20e05c507b32beb96 ("btrfs: remove all unused functions"), there's no extern functions at all in ref-cache.c, so just remove the remaining dead code.
--- fs/btrfs/ref-cache.c | 68 -------------------------------------------------- fs/btrfs/ref-cache.h | 52 -------------------------------------- 2 files changed, 0 insertions(+), 120 deletions(-) delete mode 100644 fs/btrfs/ref-cache.c delete mode 100644 fs/btrfs/ref-cache.h
fs/btrfs/ref-cache.c deleted +0 -68
@@ -1,68 +0,0 @@ -/* -* Copyright (C) 2008 Oracle. All rights reserved. -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public -* License v2 as published by the Free Software Foundation. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* General Public License for more details. -* -* You should have received a copy of the GNU General Public -* License along with this program; if not, write to the -* Free Software Foundation, Inc., 59 Temple Place - Suite 330, -* Boston, MA 021110-1307, USA. -*/ - -#include <linux/sched.h> -#include <linux/slab.h> -#include <linux/sort.h> -#include "ctree.h" -#include "ref-cache.h" -#include "transaction.h" - -static struct rb_node *tree_insert (struct rb_root *root,u64 bytenr, -struct rb_node *node) -{ -struct rb_node **p = &root->rb_node; -struct rb_node *parent =NULL ; -struct btrfs_leaf_ref *entry; - -while (*p) { - parent = *p; - entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node); - -if (bytenr < entry->bytenr) - p = &(*p)->rb_left; -else if (bytenr > entry->bytenr) - p = &(*p)->rb_right; -else -return parent; - } - - entry = rb_entry(node, struct btrfs_leaf_ref, rb_node); -rb_link_node (node, parent, p); -rb_insert_color (node, root); -return NULL ; -} - -static struct rb_node *tree_search (struct rb_root *root,u64 bytenr) -{ -struct rb_node *n = root->rb_node; -struct btrfs_leaf_ref *entry; - -while (n) { - entry = rb_entry(n, struct btrfs_leaf_ref, rb_node); -WARN_ON (!entry->in_tree); - -if (bytenr < entry->bytenr) - n = n->rb_left; -else if (bytenr > entry->bytenr) - n = n->rb_right; -else -return n; - } -return NULL ; -}
fs/btrfs/ref-cache.h deleted +0 -52
52 lines not on this page · raw message
Signature · 1 line
1.7.3.1
PATCH 8/16 Btrfs: make acl functions really no-op if acl is not enabled
In reply to Li ZefanSo there's no overhead for something we don't use.
--- fs/btrfs/Makefile | 4 +++- fs/btrfs/acl.c | 17 ----------------- fs/btrfs/ctree.h | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 21 deletions(-)
fs/btrfs/Makefile +3 -1
@@ -6,5 +6,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ transaction.o inode.o file.o tree-defrag.o \ extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \ extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ - export.o tree-log.o acl.o free-space-cache.o zlib.o lzo.o \ + export.o tree-log.o free-space-cache.o zlib.o lzo.o \ compression.o delayed-ref.o relocation.o delayed-inode.o scrub.o + +btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o
fs/btrfs/acl.c +0 -17
@@ -28,8 +28,6 @@#include "btrfs_inode.h" #include "xattr.h" -#ifdef CONFIG_BTRFS_FS_POSIX_ACL -static struct posix_acl *btrfs_get_acl (struct inode *inode,int type) {int size;
@@ -318,18 +316,3 @@ const struct xattr_handler btrfs_xattr_acl_access_handler = { .get = btrfs_xattr_acl_get, .set = btrfs_xattr_acl_set, }; - -#else /* CONFIG_BTRFS_FS_POSIX_ACL */ - -int btrfs_acl_chmod (struct inode *inode) -{ -return 0 ; -} - -int btrfs_init_acl (struct btrfs_trans_handle *trans, -struct inode *inode,struct inode *dir) -{ -return 0 ; -} - -#endif /* CONFIG_BTRFS_FS_POSIX_ACL */
fs/btrfs/ctree.h +12 -3
@@ -2644,12 +2644,21 @@ do { \/* acl.c */ #ifdef CONFIG_BTRFS_FS_POSIX_ACL int btrfs_check_acl (struct inode *inode,int mask,unsigned int flags); -#else -#define btrfs_check_acl NULL -#endif int btrfs_init_acl (struct btrfs_trans_handle *trans,struct inode *inode,struct inode *dir);int btrfs_acl_chmod (struct inode *inode); +#else +#define btrfs_check_acl NULL +static inline int btrfs_init_acl (struct btrfs_trans_handle *trans, +struct inode *inode,struct inode *dir) +{ +return 0 ; +} +static inline int btrfs_acl_chmod (struct inode *inode) +{ +return 0 ; +} +#endif /* relocation.c */ int btrfs_relocate_block_group (struct btrfs_root *root,u64 group_start);
Signature · 1 line
1.7.3.1
PATCH 9/16 Btrfs: remove redundant code for dir item lookup
In reply to Li ZefanWhen we search a dir item with a specific hash code, we can just return NULL without further checking if btrfs_search_slot() returns 1.
--- fs/btrfs/dir-item.c | 30 ++---------------------------- 1 files changed, 2 insertions(+), 28 deletions(-)
fs/btrfs/dir-item.c +2 -28
@@ -203,8 +203,6 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,struct btrfs_key key;int ins_len = mod <0 ?-1 :0 ;int cow = mod !=0 ; -struct btrfs_key found_key; -struct extent_buffer *leaf; key.objectid = dir;btrfs_set_key_type (&key,BTRFS_DIR_ITEM_KEY );
@@ -214,18 +212,7 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, ret =btrfs_search_slot (trans, root, &key, path, ins_len, cow);if (ret <0 )return ERR_PTR (ret); -if (ret >0 ) { -if (path->slots[0 ] ==0 ) -return NULL ; - path->slots[0 ]--; - } - - leaf = path->nodes[0 ]; -btrfs_item_key_to_cpu (leaf, &found_key, path->slots[0 ]); - -if (found_key.objectid != dir || -btrfs_key_type (&found_key) !=BTRFS_DIR_ITEM_KEY || - found_key.offset != key.offset) +if (ret >0 )return NULL ;return btrfs_match_dir_item_name (root, path, name, name_len);
@@ -320,8 +307,6 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,struct btrfs_key key;int ins_len = mod <0 ?-1 :0 ;int cow = mod !=0 ; -struct btrfs_key found_key; -struct extent_buffer *leaf; key.objectid = dir;btrfs_set_key_type (&key,BTRFS_XATTR_ITEM_KEY );
@@ -329,18 +314,7 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans, ret =btrfs_search_slot (trans, root, &key, path, ins_len, cow);if (ret <0 )return ERR_PTR (ret); -if (ret >0 ) { -if (path->slots[0 ] ==0 ) -return NULL ; - path->slots[0 ]--; - } - - leaf = path->nodes[0 ]; -btrfs_item_key_to_cpu (leaf, &found_key, path->slots[0 ]); - -if (found_key.objectid != dir || -btrfs_key_type (&found_key) !=BTRFS_XATTR_ITEM_KEY || - found_key.offset != key.offset) +if (ret >0 )return NULL ;return btrfs_match_dir_item_name (root, path, name, name_len);
Signature · 1 line
1.7.3.1
PATCH 10/16 Btrfs: clean up search_extent_mapping()
In reply to Li Zefanrb_node returned by __tree_search() can be a valid pointer or NULL, but won't be some errno.
--- fs/btrfs/extent_map.c | 17 +++-------------- 1 files changed, 3 insertions(+), 14 deletions(-)
fs/btrfs/extent_map.c +3 -14
@@ -379,23 +379,12 @@ struct extent_map *search_extent_mapping(struct extent_map_tree *tree, em = rb_entry(next, struct extent_map, rb_node); goto found; } -if (!rb_node) { - em =NULL ; - goto out; - } -if (IS_ERR (rb_node)) { - em =ERR_CAST (rb_node); - goto out; - } - em = rb_entry(rb_node, struct extent_map, rb_node); - goto found; - - em =NULL ; - goto out; +if (!rb_node) +return NULL ; + em = rb_entry(rb_node, struct extent_map, rb_node); found:atomic_inc (&em->refs); -out:return em; }
Signature · 1 line
1.7.3.1
PATCH 11/16 Btrfs: clean up code for extent_map lookup
In reply to Li Zefanlookup_extent_map() and search_extent_map() can share most of code.
--- fs/btrfs/extent_map.c | 85 +++++++++++++++++-------------------------------- 1 files changed, 29 insertions(+), 56 deletions(-)
fs/btrfs/extent_map.c +29 -56
@@ -299,19 +299,8 @@ static u64 range_end(u64 start, u64 len)return start + len; } -/** -* lookup_extent_mapping - lookup extent_map -* @tree: tree to lookup in -* @start: byte offset to start the search -* @len: length of the lookup range -* -* Find and return the first extent_map struct in @tree that intersects the -* [start, len] range. There may be additional objects in the tree that -* intersect, so check the object returned carefully to make sure that no -* additional lookups are needed. -*/ -struct extent_map *lookup_extent_mapping (struct extent_map_tree *tree, -u64 start,u64 len) +struct extent_map *__lookup_extent_mapping (struct extent_map_tree *tree, +u64 start,u64 len,int strict) {struct extent_map *em;struct rb_node *rb_node;
@@ -320,38 +309,42 @@ struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,u64 end =range_end (start, len); rb_node =__tree_search (&tree->map, start, &prev, &next); -if (!rb_node && prev) { - em = rb_entry(prev, struct extent_map, rb_node); -if (end > em->start && start <extent_map_end (em)) - goto found; - } -if (!rb_node && next) { - em = rb_entry(next, struct extent_map, rb_node); -if (end > em->start && start <extent_map_end (em)) - goto found; - }if (!rb_node) { - em =NULL ; - goto out; - } -if (IS_ERR (rb_node)) { - em =ERR_CAST (rb_node); - goto out; +if (prev) + rb_node = prev; +else if (next) + rb_node = next; +else +return NULL ; } + em = rb_entry(rb_node, struct extent_map, rb_node); -if (end > em->start && start <extent_map_end (em)) - goto found; - em =NULL ; - goto out; +if (strict && !(end > em->start && start <extent_map_end (em))) +return NULL ; -found:atomic_inc (&em->refs); -out:return em; }/** +* lookup_extent_mapping - lookup extent_map +* @tree: tree to lookup in +* @start: byte offset to start the search +* @len: length of the lookup range +* +* Find and return the first extent_map struct in @tree that intersects the +* [start, len] range. There may be additional objects in the tree that +* intersect, so check the object returned carefully to make sure that no +* additional lookups are needed. +*/ +struct extent_map *lookup_extent_mapping (struct extent_map_tree *tree, +u64 start,u64 len) +{ +return __lookup_extent_mapping (tree, start, len,1 ); +} + +/** * search_extent_mapping - find a nearby extent map * @tree: tree to lookup in * @start: byte offset to start the search
@@ -365,27 +358,7 @@ out:struct extent_map *search_extent_mapping (struct extent_map_tree *tree,u64 start,u64 len) { -struct extent_map *em; -struct rb_node *rb_node; -struct rb_node *prev =NULL ; -struct rb_node *next =NULL ; - - rb_node =__tree_search (&tree->map, start, &prev, &next); -if (!rb_node && prev) { - em = rb_entry(prev, struct extent_map, rb_node); - goto found; - } -if (!rb_node && next) { - em = rb_entry(next, struct extent_map, rb_node); - goto found; - } -if (!rb_node) -return NULL ; - - em = rb_entry(rb_node, struct extent_map, rb_node); -found: -atomic_inc (&em->refs); -return em; +return __lookup_extent_mapping (tree, start, len,0 ); }/**
Signature · 1 line
1.7.3.1
PATCH 12/16 Btrfs: clean up code for merging extent maps
In reply to Li Zefanunpin_extent_cache() and add_extent_mapping() shares the same code that merges extent maps.
--- fs/btrfs/extent_map.c | 59 +++++++++++++++++------------------------------- 1 files changed, 21 insertions(+), 38 deletions(-)
fs/btrfs/extent_map.c +21 -38
@@ -183,22 +183,10 @@ static int mergable_maps(struct extent_map *prev, struct extent_map *next)return 0 ; } -int unpin_extent_cache (struct extent_map_tree *tree,u64 start,u64 len) +static void try_merge_map (struct extent_map_tree *tree,struct extent_map *em) { -int ret =0 ;struct extent_map *merge =NULL ;struct rb_node *rb; -struct extent_map *em; - -write_lock (&tree->lock); - em =lookup_extent_mapping (tree, start, len); - -WARN_ON (!em || em->start != start); - -if (!em) - goto out; - -clear_bit (EXTENT_FLAG_PINNED , &em->flags);if (em->start !=0 ) { rb =rb_prev (&em->rb_node);
@@ -225,6 +213,24 @@ int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len) merge->in_tree =0 ;free_extent_map (merge); } +} + +int unpin_extent_cache (struct extent_map_tree *tree,u64 start,u64 len) +{ +int ret =0 ; +struct extent_map *em; + +write_lock (&tree->lock); + em =lookup_extent_mapping (tree, start, len); + +WARN_ON (!em || em->start != start); + +if (!em) + goto out; + +clear_bit (EXTENT_FLAG_PINNED , &em->flags); + +try_merge_map (tree, em);free_extent_map (em); out:
@@ -247,7 +253,6 @@ int add_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) {int ret =0 ; -struct extent_map *merge =NULL ;struct rb_node *rb;struct extent_map *exist;
@@ -263,30 +268,8 @@ int add_extent_mapping(struct extent_map_tree *tree, goto out; }atomic_inc (&em->refs); -if (em->start !=0 ) { - rb =rb_prev (&em->rb_node); -if (rb) - merge = rb_entry(rb, struct extent_map, rb_node); -if (rb &&mergable_maps (merge, em)) { - em->start = merge->start; - em->len += merge->len; - em->block_len += merge->block_len; - em->block_start = merge->block_start; - merge->in_tree =0 ; -rb_erase (&merge->rb_node, &tree->map); -free_extent_map (merge); - } - } - rb =rb_next (&em->rb_node); -if (rb) - merge = rb_entry(rb, struct extent_map, rb_node); -if (rb &&mergable_maps (em, merge)) { - em->len += merge->len; - em->block_len += merge->len; -rb_erase (&merge->rb_node, &tree->map); - merge->in_tree =0 ; -free_extent_map (merge); - } + +try_merge_map (tree, em); out:return ret; }
Signature · 1 line
1.7.3.1
PATCH 13/16 Btrfs: remove unused members from struct extent_state
In reply to Li ZefanFrom: Xiao Guangrong <[email protected]>
These members are not used at all.
--- fs/btrfs/extent_io.h | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.h +0 -2
@@ -108,8 +108,6 @@ struct extent_state {wait_queue_head_t wq;atomic_t refs;unsigned long state; -u64 split_start; -u64 split_end;/* for use by the FS */ u64 private;
Signature · 1 line
1.7.3.1
PATCH 14/16 Btrfs: clean up for insert_state()
In reply to Li ZefanFrom: Xiao Guangrong <[email protected]>
Don't duplicate set_state_bits().
--- fs/btrfs/extent_io.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/extent_io.c +5 -5
@@ -308,6 +308,9 @@ static void clear_state_cb(struct extent_io_tree *tree, tree->ops->clear_bit_hook (tree->mapping->host, state, bits); } +static int set_state_bits (struct extent_io_tree *tree, +struct extent_state *state,int *bits); +/* * insert an extent_state struct into the tree. 'bits' are set on the * struct before it is inserted.
@@ -323,7 +326,6 @@ static int insert_state(struct extent_io_tree *tree, int *bits) {struct rb_node *node; -int bits_to_set = *bits & ~EXTENT_CTLBITS ;int ret;if (end < start) {
@@ -334,13 +336,11 @@ static int insert_state(struct extent_io_tree *tree, } state->start = start; state->end = end; - ret =set_state_cb (tree, state, bits); + + ret =set_state_bits (tree, state, bits);if (ret)return ret; -if (bits_to_set &EXTENT_DIRTY ) - tree->dirty_bytes += end - start +1 ; - state->state |= bits_to_set; node =tree_insert (&tree->state, end, &state->rb_node);if (node) {struct extent_state *found;
Signature · 1 line
1.7.3.1
PATCH 15/16 Btrfs: clean up for wait_extent_bit()
In reply to Li ZefanFrom: Xiao Guangrong <[email protected]>
We can just use cond_resched_lock().
--- fs/btrfs/extent_io.c | 6 +----- 1 files changed, 1 insertions(+), 5 deletions(-)
fs/btrfs/extent_io.c +1 -5
@@ -658,11 +658,7 @@ again:if (start > end)break ; -if (need_resched ()) { -spin_unlock (&tree->lock); -cond_resched (); -spin_lock (&tree->lock); - } +cond_resched_lock (&tree->lock); } out:spin_unlock (&tree->lock);
Signature · 1 line
1.7.3.1
PATCH 16/16 Btrfs: clean up for find_first_extent_bit()
In reply to Li ZefanFrom: Xiao Guangrong <[email protected]>
find_first_extent_bit() and find_first_extent_bit_state() share most of the code, and we can just make the former call the latter.
--- fs/btrfs/extent_io.c | 64 ++++++++++++++++++------------------------------- 1 files changed, 24 insertions(+), 40 deletions(-)
fs/btrfs/extent_io.c +24 -40
@@ -1055,46 +1055,6 @@ static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)return 0 ; } -/* -* find the first offset in the io tree with 'bits' set. zero is -* returned if we find something, and *start_ret and *end_ret are -* set to reflect the state struct that was found. -* -* If nothing was found, 1 is returned, < 0 on error -*/ -int find_first_extent_bit (struct extent_io_tree *tree,u64 start, -u64 *start_ret,u64 *end_ret,int bits) -{ -struct rb_node *node; -struct extent_state *state; -int ret =1 ; - -spin_lock (&tree->lock); -/* -* this search will find all the extents that end after -* our range starts. -*/ - node =tree_search (tree, start); -if (!node) - goto out; - -while (1 ) { - state = rb_entry(node, struct extent_state, rb_node); -if (state->end >= start && (state->state & bits)) { - *start_ret = state->start; - *end_ret = state->end; - ret =0 ; -break ; - } - node =rb_next (node); -if (!node) -break ; - } -out: -spin_unlock (&tree->lock); -return ret; -} -/* find the first state struct with 'bits' set after 'start', and * return it. tree->lock must be held. NULL will returned if * nothing was found after 'start'
@@ -1127,6 +1087,30 @@ out: }/* +* find the first offset in the io tree with 'bits' set. zero is +* returned if we find something, and *start_ret and *end_ret are +* set to reflect the state struct that was found. +* +* If nothing was found, 1 is returned, < 0 on error +*/ +int find_first_extent_bit (struct extent_io_tree *tree,u64 start, +u64 *start_ret,u64 *end_ret,int bits) +{ +struct extent_state *state; +int ret =1 ; + +spin_lock (&tree->lock); + state =find_first_extent_bit_state (tree, start, bits); +if (state) { + *start_ret = state->start; + *end_ret = state->end; + ret =0 ; + } +spin_unlock (&tree->lock); +return ret; +} + +/* * find a contiguous range of bytes in the file marked as delalloc, not * more than 'max_bytes'. start and end are used to return the range, *
Signature · 1 line
1.7.3.1
Quoted 5 lines · Li Zefan
The first 4 patches are bug-fixes, and the remaining are small cleanups that have sit in my git tree for some time.
The first 3 patches have been sent to the list before.
To pull the patchset:
git://repo.or.cz/linux-btrfs-devel.git for-chris
It's based on the for-linus branch of btrfs-unstable tree.
Re: [PATCH 11/16] Btrfs: clean up code for extent_map lookup
In reply to Li ZefanQuoted 20 lines · Li Zefan
lookup_extent_map()andsearch_extent_map()can share most of code.Signed-off-by Li Zefan <[email protected]>--- fs/btrfs/extent_map.c | 85 +++++++++++++++++-------------------------------- 1 files changed, 29 insertions(+), 56 deletions(-)fs/btrfs/extent_map.c +0
-5@@ -299,19 +299,8 @@ static u64 range_end(u64 start, u64 len)return start + len; } -/** -* lookup_extent_mapping - lookup extent_map -* @tree: tree to lookup in -* @start: byte offset to start the search -* @len: length of the lookup range
fs/btrfs/extent_map.c static u64 range_end(u64 start, u64 len) · line 303
Quoted 10 lines
fs/btrfs/extent_map.c +2
-8-* -* Find and return the first extent_map struct in @tree that intersects the -* [start, len] range. There may be additional objects in the tree that -* intersect, so check the object returned carefully to make sure that no -* additional lookups are needed. -*/ -struct extent_map *lookup_extent_mapping (struct extent_map_tree *tree, -u64 start,u64 len) +struct extent_map *__lookup_extent_mapping (struct extent_map_tree *tree, +u64 start,u64 len,int strict)
just minor thing: can be defined static
Quoted 103 lines
fs/btrfs/extent_map.c +27
-43{struct extent_map *em;struct rb_node *rb_node;@@ -320,38 +309,42 @@ struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,u64 end =range_end (start, len); rb_node =__tree_search (&tree->map, start, &prev, &next); -if (!rb_node && prev) { - em = rb_entry(prev, struct extent_map, rb_node); -if (end > em->start && start <extent_map_end (em)) - goto found; - } -if (!rb_node && next) { - em = rb_entry(next, struct extent_map, rb_node); -if (end > em->start && start <extent_map_end (em)) - goto found; - }if (!rb_node) { - em =NULL ; - goto out; - } -if (IS_ERR (rb_node)) { - em =ERR_CAST (rb_node); - goto out; +if (prev) + rb_node = prev; +else if (next) + rb_node = next; +else +return NULL ; } + em = rb_entry(rb_node, struct extent_map, rb_node); -if (end > em->start && start <extent_map_end (em)) - goto found; - em =NULL ; - goto out; +if (strict && !(end > em->start && start <extent_map_end (em))) +return NULL ; -found:atomic_inc (&em->refs); -out:return em; }/** +* lookup_extent_mapping - lookup extent_map +* @tree: tree to lookup in +* @start: byte offset to start the search +* @len: length of the lookup range +* +* Find and return the first extent_map struct in @tree that intersects the +* [start, len] range. There may be additional objects in the tree that +* intersect, so check the object returned carefully to make sure that no +* additional lookups are needed. +*/ +struct extent_map *lookup_extent_mapping (struct extent_map_tree *tree, +u64 start,u64 len) +{ +return __lookup_extent_mapping (tree, start, len,1 ); +} + +/** * search_extent_mapping - find a nearby extent map * @tree: tree to lookup in * @start: byte offset to start the search @@ -365,27 +358,7 @@ out:struct extent_map *search_extent_mapping (struct extent_map_tree *tree,u64 start,u64 len) { -struct extent_map *em; -struct rb_node *rb_node; -struct rb_node *prev =NULL ; -struct rb_node *next =NULL ; - - rb_node =__tree_search (&tree->map, start, &prev, &next); -if (!rb_node && prev) { - em = rb_entry(prev, struct extent_map, rb_node); - goto found; - } -if (!rb_node && next) { - em = rb_entry(next, struct extent_map, rb_node); - goto found; - } -if (!rb_node) -return NULL ; - - em = rb_entry(rb_node, struct extent_map, rb_node); -found: -atomic_inc (&em->refs); -return em; +return __lookup_extent_mapping (tree, start, len,0 ); }/** Signature · 5 lines
1.7.3.1 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html