Btrfs-progs: start at kernel/userspace syncup
cover Btrfs-progs: start at kernel/userspace syncup
A first round of small, hopefully obviously-correct and/or no-op patches that start to make the userspace copies of kernelspace code look a bit more similar.
Many of these are direct ports from userspace, a couple others are just my cleanups of various bits here and there.
There'll be a fair bit more after this, but I'll do it in chunks.
(Thanks David!) :)
Thanks,
-Eric
[PATCH 1/9] Btrfs-progs: remove dead btrfs-defrag.c
[PATCH 2/9] Btrfs-progs: remove cut & paste btrfs_next_leaf from restore [PATCH 3/9] Btrfs-progs: fix array bound checking
[PATCH 4/9] Btrfs-progs: move btrfs_fsck_reinit_root to cmds-check.c [PATCH 5/9] Btrfs-progs: remove some dead/unbuilt code
[PATCH 6/9] Btrfs-progs: cleanup duplicated division functions
[PATCH 7/9] Btrfs-progs: make extent_ref_type() match kernelspace [PATCH 8/9] Btrfs-progs: remove the unnecessary 'return -1;' at the end of bin_search [PATCH 9/9] Btrfs-progs: remove btrfs_init_path calls from ctree.c
PATCH 1/9 Btrfs-progs: remove dead btrfs-defrag.c
Files with only #include directives are boring. :)
This is just a leftover after the move to the btrfs tool.
--- btrfs-defrag.c | 39 --------------------------------------- 1 files changed, 0 insertions(+), 39 deletions(-) delete mode 100644 btrfs-defrag.c
btrfs-defrag.c deleted +0 -39
@@ -1,39 +0,0 @@ -/* -* Copyright (C) 2010 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. -*/ - -#ifndef __CHECKER__ -#include <sys/ioctl.h> -#include <sys/mount.h> -#include "ioctl.h" -#endif -#include <stdio.h> -#include <stdlib.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <ctype.h> -#include <unistd.h> -#include <dirent.h> -#include <libgen.h> -#include <getopt.h> -#include "kerncompat.h" -#include "ctree.h" -#include "transaction.h" -#include "utils.h" -#include "version.h" -
Signature · 1 line
1.7.1
PATCH 2/9 Btrfs-progs: remove cut & paste btrfs_next_leaf from restore
In reply to Eric Sandeencmds-restore.c cut & paste btrfs_next_leaf w/ a little extra; we can just call btrfs_next_leaf from there, after the bit of additional pre-checking that it does.
Strangely, every caller in restore checks for a negative return as an error, but the copy never returned negative. Calling btrfs_next_leaf does return -EIO in cases where read_node_slot fails.
Also, remove the slot assignment which was overwritten in every case.
--- cmds-restore.c | 41 ++--------------------------------------- ctree.c | 5 +++-- ctree.h | 4 ---- 3 files changed, 5 insertions(+), 45 deletions(-)
cmds-restore.c +2 -39
@@ -147,12 +147,9 @@ static int decompress(char *inbuf, char *outbuf, u64 compress_len,return -1 ; } -int next_leaf (struct btrfs_root *root,struct btrfs_path *path) +static int next_leaf (struct btrfs_root *root,struct btrfs_path *path) { -int slot;int level =1 ; -struct extent_buffer *c; -struct extent_buffer *next =NULL ;for (; level <BTRFS_MAX_LEVEL ; level++) {if (path->nodes[level])
@@ -162,41 +159,7 @@ int next_leaf(struct btrfs_root *root, struct btrfs_path *path)if (level ==BTRFS_MAX_LEVEL )return 1 ; - slot = path->slots[level] +1 ; - -while (level <BTRFS_MAX_LEVEL ) { -if (!path->nodes[level]) -return 1 ; - - slot = path->slots[level] +1 ; - c = path->nodes[level]; -if (slot >=btrfs_header_nritems (c)) { - level++; -if (level ==BTRFS_MAX_LEVEL ) -return 1 ; -continue ; - } - -if (path->reada) -reada_for_search (root, path, level, slot,0 ); - - next =read_node_slot (root, c, slot); -break ; - } - path->slots[level] = slot; -while (1 ) { - level--; - c = path->nodes[level]; -free_extent_buffer (c); - path->nodes[level] = next; - path->slots[level] =0 ; -if (!level) -break ; -if (path->reada) -reada_for_search (root, path, level,0 ,0 ); - next =read_node_slot (root, next,0 ); - } -return 0 ; +return btrfs_next_leaf (root, path); }static int copy_one_inline (int fd,struct btrfs_path *path,u64 pos)
ctree.c +3 -2
@@ -777,7 +777,7 @@ static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,return -1 ; } -struct extent_buffer *read_node_slot (struct btrfs_root *root, +static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,struct extent_buffer *parent,int slot) {int level =btrfs_header_level (parent);
@@ -1104,7 +1104,8 @@ static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,/* * readahead one full node of leaves */ -void reada_for_search (struct btrfs_root *root,struct btrfs_path *path, +static void reada_for_search (struct btrfs_root *root, +struct btrfs_path *path,int level,int slot,u64 objectid) {struct extent_buffer *node;
ctree.h +0 -4
@@ -2177,10 +2177,6 @@ int btrfs_check_leaf(struct btrfs_root *root, struct extent_buffer *buf);int btrfs_fsck_reinit_root (struct btrfs_trans_handle *trans,struct btrfs_root *root); -void reada_for_search (struct btrfs_root *root,struct btrfs_path *path, -int level,int slot,u64 objectid); -struct extent_buffer *read_node_slot (struct btrfs_root *root, -struct extent_buffer *parent,int slot);int btrfs_previous_item (struct btrfs_root *root,struct btrfs_path *path,u64 min_objectid,int type);
Signature · 1 line
1.7.1
PATCH 3/9 Btrfs-progs: fix array bound checking
In reply to Eric SandeenOtherwise we can execced the array bound of path->slots[].
[Eric Sandeen: port kernel commit a05a9bb to userspace]
--- ctree.c | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-)
ctree.c +6 -4
@@ -815,9 +815,10 @@ static int balance_level(struct btrfs_trans_handle *trans, orig_ptr =btrfs_node_blockptr (mid, orig_slot); -if (level <BTRFS_MAX_LEVEL -1 ) +if (level <BTRFS_MAX_LEVEL -1 ) { parent = path->nodes[level +1 ]; - pslot = path->slots[level +1 ]; + pslot = path->slots[level +1 ]; + }/* * deal with the case where there is only one pointer in the root
@@ -1007,9 +1008,10 @@ static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans, mid = path->nodes[level];WARN_ON (btrfs_header_generation (mid) != trans->transid); -if (level <BTRFS_MAX_LEVEL -1 ) +if (level <BTRFS_MAX_LEVEL -1 ) { parent = path->nodes[level +1 ]; - pslot = path->slots[level +1 ]; + pslot = path->slots[level +1 ]; + }if (!parent)return 1 ;
Signature · 1 line
1.7.1
PATCH 4/9 Btrfs-progs: move btrfs_fsck_reinit_root to cmds-check.c
In reply to Eric Sandeencmds-check.c contains the only caller of btrfs_fsck_reinit_root; moving it to the caller's source file gets ctree.c a little closer to kernelspace, although it does require exporting add_root_to_dirty_list(), which is not done in kernelspace.
--- cmds-check.c | 43 +++++++++++++++++++++++++++++++++++++++++++ ctree.c | 44 +------------------------------------------- ctree.h | 3 +-- 3 files changed, 45 insertions(+), 45 deletions(-)
cmds-check.c +43 -0
@@ -3981,6 +3981,49 @@ static int check_extents(struct btrfs_trans_handle *trans,return ret; } + +static int btrfs_fsck_reinit_root (struct btrfs_trans_handle *trans, +struct btrfs_root *root) +{ +struct extent_buffer *c; +struct extent_buffer *old = root->node; +int level; +struct btrfs_disk_key disk_key = {0 ,0 ,0 }; + + level =0 ; + + c =btrfs_alloc_free_block (trans, root, +btrfs_level_size (root,0 ), + root->root_key.objectid, + &disk_key, level,0 ,0 ); +if (IS_ERR (c)) { + c = old; +extent_buffer_get (c); + } + +memset_extent_buffer (c,0 ,0 ,sizeof (struct btrfs_header )); +btrfs_set_header_level (c, level); +btrfs_set_header_bytenr (c, c->start); +btrfs_set_header_generation (c, trans->transid); +btrfs_set_header_backref_rev (c,BTRFS_MIXED_BACKREF_REV ); +btrfs_set_header_owner (c, root->root_key.objectid); + +write_extent_buffer (c, root->fs_info->fsid, + (unsigned long )btrfs_header_fsid (c), +BTRFS_FSID_SIZE ); + +write_extent_buffer (c, root->fs_info->chunk_tree_uuid, + (unsigned long )btrfs_header_chunk_tree_uuid (c), +BTRFS_UUID_SIZE ); + +btrfs_mark_buffer_dirty (c); + +free_extent_buffer (old); + root->node = c; +add_root_to_dirty_list (root); +return 0 ; +} +static struct option long_options[] = { {"super" ,1 ,NULL ,'s' }, {"repair" ,0 ,NULL ,0 },
ctree.c +1 -43
56 lines not on this page · raw message
ctree.h +1 -2
@@ -2175,8 +2175,6 @@ int btrfs_check_node(struct btrfs_root *root,int btrfs_check_leaf (struct btrfs_root *root,struct btrfs_disk_key *parent_key,struct extent_buffer *buf); -int btrfs_fsck_reinit_root (struct btrfs_trans_handle *trans, -struct btrfs_root *root);int btrfs_previous_item (struct btrfs_root *root,struct btrfs_path *path,u64 min_objectid,int type);
@@ -2214,6 +2212,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, int start_slot, int cache_only, u64 *last_ret, struct btrfs_key *progress);void btrfs_release_path (struct btrfs_root *root,struct btrfs_path *p); +void add_root_to_dirty_list (struct btrfs_root *root);struct btrfs_path *btrfs_alloc_path (void );void btrfs_free_path (struct btrfs_path *p);void btrfs_init_path (struct btrfs_path *p);
Signature · 1 line
1.7.1
PATCH 5/9 Btrfs-progs: remove some dead/unbuilt code
In reply to Eric SandeenRemove some commented-out & #if 0'd code:
* close_blocks()
* btrfs_drop_snapshot()
* btrfs_realloc_node()
* btrfs_find_dead_roots()
There are still some #if 0'd functions in there, but I'm hedging on those for now, they have been copied to cmds-check.c and I want to see if they can be brough back into ctree.c eventually.
--- ctree.c | 140 --------------------------------------------------------- ctree.h | 8 --- extent-tree.c | 71 ----------------------------- root-tree.c | 71 ----------------------------- 4 files changed, 0 insertions(+), 290 deletions(-)
ctree.c +0 -140
@@ -373,17 +373,6 @@ int btrfs_cow_block(struct btrfs_trans_handle *trans, }/* -static int close_blocks(u64 blocknr, u64 other, u32 blocksize) -{ -if (blocknr < other && other - (blocknr + blocksize) < 32768) -return 1; -if (blocknr > other && blocknr - (other + blocksize) < 32768) -return 1; -return 0; -} -*/ - -/* * compare two keys in a memcmp fashion */ int btrfs_comp_keys (struct btrfs_disk_key *disk,struct btrfs_key *k2)
@@ -407,135 +396,6 @@ int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)return 0 ; } - -#if 0 -int btrfs_realloc_node (struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct extent_buffer *parent, -int start_slot,int cache_only,u64 *last_ret, -struct btrfs_key *progress) -{ -struct extent_buffer *cur; -struct extent_buffer *tmp; -u64 blocknr; -u64 gen; -u64 search_start = *last_ret; -u64 last_block =0 ; -u64 other; -u32 parent_nritems; -int end_slot; -int i; -int err =0 ; -int parent_level; -int uptodate; -u32 blocksize; -int progress_passed =0 ; -struct btrfs_disk_key disk_key; - - parent_level =btrfs_header_level (parent); -if (cache_only && parent_level !=1 ) -return 0 ; - -if (trans->transaction != root->fs_info->running_transaction) { -printk (KERN_CRIT "trans %Lu running %Lu\n" , trans->transid, - root->fs_info->running_transaction->transid); -WARN_ON (1 ); - } -if (trans->transid != root->fs_info->generation) { -printk (KERN_CRIT "trans %Lu running %Lu\n" , trans->transid, - root->fs_info->generation); -WARN_ON (1 ); - } - - parent_nritems =btrfs_header_nritems (parent); - blocksize =btrfs_level_size (root, parent_level -1 ); - end_slot = parent_nritems; - -if (parent_nritems ==1 ) -return 0 ; - -for (i = start_slot; i < end_slot; i++) { -int close =1 ; - -if (!parent->map_token) { -map_extent_buffer (parent, -btrfs_node_key_ptr_offset (i), -sizeof (struct btrfs_key_ptr ), - &parent->map_token, &parent->kaddr, - &parent->map_start, &parent->map_len, -KM_USER1 ); - } -btrfs_node_key (parent, &disk_key, i); -if (!progress_passed &&comp_keys (&disk_key, progress) <0 ) -continue ; - - progress_passed =1 ; - blocknr =btrfs_node_blockptr (parent, i); - gen =btrfs_node_ptr_generation (parent, i); -if (last_block ==0 ) - last_block = blocknr; - -if (i >0 ) { - other =btrfs_node_blockptr (parent, i -1 ); - close =close_blocks (blocknr, other, blocksize); - } -if (close && i < end_slot -2 ) { - other =btrfs_node_blockptr (parent, i +1 ); - close =close_blocks (blocknr, other, blocksize); - } -if (close) { - last_block = blocknr; -continue ; - } -if (parent->map_token) { -unmap_extent_buffer (parent, parent->map_token, -KM_USER1 ); - parent->map_token =NULL ; - } - - cur =btrfs_find_tree_block (root, blocknr, blocksize); -if (cur) - uptodate =btrfs_buffer_uptodate (cur, gen); -else - uptodate =0 ; -if (!cur || !uptodate) { -if (cache_only) { -free_extent_buffer (cur); -continue ; - } -if (!cur) { - cur =read_tree_block (root, blocknr, - blocksize, gen); - }else if (!uptodate) { -btrfs_read_buffer (cur, gen); - } - } -if (search_start ==0 ) - search_start = last_block; - - err =__btrfs_cow_block (trans, root, cur, parent, i, - &tmp, search_start, -min (16 * blocksize, - (end_slot - i) * blocksize)); -if (err) { -free_extent_buffer (cur); -break ; - } - search_start = tmp->start; - last_block = tmp->start; - *last_ret = search_start; -if (parent_level ==1 ) -btrfs_clear_buffer_defrag (tmp); -free_extent_buffer (tmp); - } -if (parent->map_token) { -unmap_extent_buffer (parent, parent->map_token, -KM_USER1 ); - parent->map_token =NULL ; - } -return err; -} -#endif -/* * The leaf data grows from end-to-front in the node. * this returns the address of the start of the last item,
ctree.h +0 -8
@@ -2207,10 +2207,6 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,int btrfs_search_slot (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct btrfs_key *key,struct btrfs_path *p,int ins_len,int cow); -int btrfs_realloc_node (struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct extent_buffer *parent, -int start_slot,int cache_only,u64 *last_ret, -struct btrfs_key *progress);void btrfs_release_path (struct btrfs_root *root,struct btrfs_path *p);void add_root_to_dirty_list (struct btrfs_root *root);struct btrfs_path *btrfs_alloc_path (void );
@@ -2245,8 +2241,6 @@ static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,int btrfs_next_leaf (struct btrfs_root *root,struct btrfs_path *path);int btrfs_prev_leaf (struct btrfs_root *root,struct btrfs_path *path);int btrfs_leaf_free_space (struct btrfs_root *root,struct extent_buffer *leaf); -int btrfs_drop_snapshot (struct btrfs_trans_handle *trans,struct btrfs_root - *root);int btrfs_set_item_key_safe (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct btrfs_path *path,struct btrfs_key *new_key);
@@ -2267,8 +2261,6 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root *item);int btrfs_find_last_root (struct btrfs_root *root,u64 objectid,struct btrfs_root_item *item,struct btrfs_key *key); -int btrfs_find_dead_roots (struct btrfs_root *root,u64 objectid, -struct btrfs_root *latest_root);/* dir-item.c */ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, const char *name, int name_len, u64 dir,
extent-tree.c +0 -71
77 lines not on this page · raw message
root-tree.c +0 -71
77 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH 6/9 Btrfs-progs: cleanup duplicated division functions
In reply to Eric Sandeendiv_factor has been implemented for two times, cleanup it. And I move them into a independent file named math.h because they are common math functions.
[Eric Sandeen: port kernel commit 3fed40c to userspace]
--- extent-tree.c | 10 +--------- math.h | 33 +++++++++++++++++++++++++++++++++ volumes.c | 9 +-------- 3 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 math.h
extent-tree.c +1 -9
@@ -27,6 +27,7 @@#include "crc32c.h" #include "volumes.h" #include "free-space-cache.h" +#include "math.h" #define BLOCK_GROUP_DATA EXTENT_WRITEBACK#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
@@ -313,15 +314,6 @@ no_cache: goto again; } -static u64 div_factor (u64 num,int factor) -{ -if (factor ==10 ) -return num; - num *= factor; - num /=10 ; -return num; -} -static int block_group_state_bits (u64 flags) {int bits =0 ;
math.h new file +33 -0
@@ -0,0 +1,33 @@ + +/* +* Copyright (C) 2012 Fujitsu. All rights reserved. +* Written by Miao Xie <[email protected]> +* +* 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. +*/ + +#ifndef __BTRFS_MATH_H +#define __BTRFS_MATH_H + +static inline u64 div_factor (u64 num,int factor) +{ +if (factor ==10 ) +return num; + num *= factor; + num /=10 ; +return num; +} + +#endif
volumes.c +1 -8
@@ -29,6 +29,7 @@#include "transaction.h" #include "print-tree.h" #include "volumes.h" +#include "math.h" struct stripe {struct btrfs_device *dev;
@@ -623,14 +624,6 @@ int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,return 0 ; } -static u64 div_factor (u64 num,int factor) -{ -if (factor ==10 ) -return num; - num *= factor; -return num /10 ; -} -static u64 chunk_bytes_by_type (u64 type,u64 calc_size,int num_stripes,int sub_stripes) {
Signature · 1 line
1.7.1
PATCH 7/9 Btrfs-progs: make extent_ref_type() match kernelspace
In reply to Eric Sandeenextent_ref_type() contains inconsequential differences between kernelspace and userspace, and has since the initial commits to each. Just make userspace look like kernelspace.
--- extent-tree.c | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-)
extent-tree.c +6 -4
@@ -967,17 +967,19 @@ static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,static inline int extent_ref_type (u64 parent,u64 owner) { +int type;if (owner <BTRFS_FIRST_FREE_OBJECTID ) {if (parent >0 ) -return BTRFS_SHARED_BLOCK_REF_KEY ; + type =BTRFS_SHARED_BLOCK_REF_KEY ;else -return BTRFS_TREE_BLOCK_REF_KEY ; + type =BTRFS_TREE_BLOCK_REF_KEY ; }else {if (parent >0 ) -return BTRFS_SHARED_DATA_REF_KEY ; + type =BTRFS_SHARED_DATA_REF_KEY ;else -return BTRFS_EXTENT_DATA_REF_KEY ; + type =BTRFS_EXTENT_DATA_REF_KEY ; } +return type; }static int find_next_key (struct btrfs_path *path,struct btrfs_key *key)
Signature · 1 line
1.7.1
PATCH 8/9 Btrfs-progs: remove the unnecessary 'return -1;' at the end of bin_search
In reply to Eric SandeenThe code path should not reach there. Remove it.
[Eric Sandeen: port kernel commit 3fed40c to userspace]
--- ctree.c | 6 ++---- 1 files changed, 2 insertions(+), 4 deletions(-)
ctree.c +2 -4
@@ -579,20 +579,18 @@ static int generic_bin_search(struct extent_buffer *eb, unsigned long p,static int bin_search (struct extent_buffer *eb,struct btrfs_key *key,int level,int *slot) { -if (level ==0 ) { +if (level ==0 )return generic_bin_search (eb, offsetof(struct btrfs_leaf , items),sizeof (struct btrfs_item ), key,btrfs_header_nritems (eb), slot); - }else { +else return generic_bin_search (eb, offsetof(struct btrfs_node , ptrs),sizeof (struct btrfs_key_ptr ), key,btrfs_header_nritems (eb), slot); - } -return -1 ; } static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Signature · 1 line
1.7.1
PATCH 9/9 Btrfs-progs: remove btrfs_init_path calls from ctree.c
In reply to Eric Sandeenbtrfs_init_path was initially used when the path objects were on the stack. Now all the work is done by btrfs_alloc_path and btrfs_init_path isn't required.
This patch removes it, and just uses kmem_cache_zalloc to zero out the object.
[Eric Sandeen: port kernel commit e00f730 to userspace]
(Note, the rest of userspace has an on-path stack, so the actual function remains for now).
--- ctree.c | 6 +----- 1 files changed, 1 insertions(+), 5 deletions(-)
ctree.c +1 -5
@@ -42,11 +42,7 @@ inline void btrfs_init_path(struct btrfs_path *p)struct btrfs_path *btrfs_alloc_path (void ) {struct btrfs_path *path; - path =kmalloc (sizeof (struct btrfs_path ),GFP_NOFS ); -if (path) { -btrfs_init_path (path); - path->reada =0 ; - } + path =kzalloc (sizeof (struct btrfs_path ),GFP_NOFS );return path; }
Signature · 1 line
1.7.1