2005-04-17 02:20:36 +04:00
/*
* Copyright 2000 by Hans Reiser , licensing governed by reiserfs / README
*/
# include <linux/time.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2005-04-17 02:20:36 +04:00
# include <linux/string.h>
2012-03-17 09:16:43 +04:00
# include "reiserfs.h"
2005-04-17 02:20:36 +04:00
# include <linux/buffer_head.h>
2014-04-23 18:00:36 +04:00
/*
* To make any changes in the tree we find a node that contains item
* to be changed / deleted or position in the node we insert a new item
* to . We call this node S . To do balancing we need to decide what we
* will shift to left / right neighbor , or to a new node , where new item
* will be etc . To make this analysis simpler we build virtual
* node . Virtual node is an array of items , that will replace items of
* node S . ( For instance if we are going to delete an item , virtual
* node does not contain it ) . Virtual node keeps information about
* item sizes and types , mergeability of first and last items , sizes
* of all entries in directory item . We use this array of items when
* calculating what we can shift to neighbors and how many nodes we
* have to have if we do not any shiftings , if we shift to left / right
* neighbor or to both .
*/
/*
* Takes item number in virtual node , returns number of item
* that it has in source buffer
*/
2005-07-13 07:21:28 +04:00
static inline int old_item_num ( int new_num , int affected_item_num , int mode )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
if ( mode = = M_PASTE | | mode = = M_CUT | | new_num < affected_item_num )
return new_num ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( mode = = M_INSERT ) {
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
RFALSE ( new_num = = 0 ,
" vs-8005: for INSERT mode and item number of inserted item " ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
return new_num - 1 ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
RFALSE ( mode ! = M_DELETE ,
" vs-8010: old_item_num: mode must be M_DELETE (mode = \' %c \' " ,
mode ) ;
/* delete mode */
return new_num + 1 ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
static void create_virtual_node ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct item_head * ih ;
struct virtual_node * vn = tb - > tb_vn ;
int new_num ;
struct buffer_head * Sh ; /* this comes from tb->S[h] */
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
Sh = PATH_H_PBUFFER ( tb - > tb_path , h ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* size of changed node */
vn - > vn_size =
MAX_CHILD_SIZE ( Sh ) - B_FREE_SPACE ( Sh ) + tb - > insert_size [ h ] ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* for internal nodes array if virtual items is not created */
if ( h ) {
vn - > vn_nr_item = ( vn - > vn_size - DC_SIZE ) / ( DC_SIZE + KEY_SIZE ) ;
return ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* number of items in virtual node */
vn - > vn_nr_item =
B_NR_ITEMS ( Sh ) + ( ( vn - > vn_mode = = M_INSERT ) ? 1 : 0 ) -
( ( vn - > vn_mode = = M_DELETE ) ? 1 : 0 ) ;
/* first virtual item */
vn - > vn_vi = ( struct virtual_item * ) ( tb - > tb_vn + 1 ) ;
memset ( vn - > vn_vi , 0 , vn - > vn_nr_item * sizeof ( struct virtual_item ) ) ;
vn - > vn_free_ptr + = vn - > vn_nr_item * sizeof ( struct virtual_item ) ;
/* first item in the node */
2014-04-23 18:00:35 +04:00
ih = item_head ( Sh , 0 ) ;
2005-07-13 07:21:28 +04:00
/* define the mergeability for 0-th item (if it is not being deleted) */
2014-04-23 18:00:42 +04:00
if ( op_is_left_mergeable ( & ih - > ih_key , Sh - > b_size )
2005-07-13 07:21:28 +04:00
& & ( vn - > vn_mode ! = M_DELETE | | vn - > vn_affected_item_num ) )
vn - > vn_vi [ 0 ] . vi_type | = VI_TYPE_LEFT_MERGEABLE ;
2014-04-23 18:00:36 +04:00
/*
* go through all items that remain in the virtual
* node ( except for the new ( inserted ) one )
*/
2005-07-13 07:21:28 +04:00
for ( new_num = 0 ; new_num < vn - > vn_nr_item ; new_num + + ) {
int j ;
struct virtual_item * vi = vn - > vn_vi + new_num ;
int is_affected =
( ( new_num ! = vn - > vn_affected_item_num ) ? 0 : 1 ) ;
if ( is_affected & & vn - > vn_mode = = M_INSERT )
continue ;
/* get item number in source node */
j = old_item_num ( new_num , vn - > vn_affected_item_num ,
vn - > vn_mode ) ;
vi - > vi_item_len + = ih_item_len ( ih + j ) + IH_SIZE ;
vi - > vi_ih = ih + j ;
2014-04-23 18:00:35 +04:00
vi - > vi_item = ih_item_body ( Sh , ih + j ) ;
2005-07-13 07:21:28 +04:00
vi - > vi_uarea = vn - > vn_free_ptr ;
2014-04-23 18:00:36 +04:00
/*
* FIXME : there is no check that item operation did not
* consume too much memory
*/
2005-07-13 07:21:28 +04:00
vn - > vn_free_ptr + =
op_create_vi ( vn , vi , is_affected , tb - > insert_size [ 0 ] ) ;
if ( tb - > vn_buf + tb - > vn_buf_size < vn - > vn_free_ptr )
2009-03-30 22:02:25 +04:00
reiserfs_panic ( tb - > tb_sb , " vs-8030 " ,
2005-07-13 07:21:28 +04:00
" virtual node space consumed " ) ;
if ( ! is_affected )
/* this is not being changed */
continue ;
if ( vn - > vn_mode = = M_PASTE | | vn - > vn_mode = = M_CUT ) {
vn - > vn_vi [ new_num ] . vi_item_len + = tb - > insert_size [ 0 ] ;
2014-04-23 18:00:36 +04:00
/* pointer to data which is going to be pasted */
vi - > vi_new_data = vn - > vn_data ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* virtual inserted item is not defined yet */
if ( vn - > vn_mode = = M_INSERT ) {
struct virtual_item * vi = vn - > vn_vi + vn - > vn_affected_item_num ;
2008-03-29 06:07:28 +03:00
RFALSE ( vn - > vn_ins_ih = = NULL ,
2005-07-13 07:21:28 +04:00
" vs-8040: item header of inserted item is not specified " ) ;
vi - > vi_item_len = tb - > insert_size [ 0 ] ;
vi - > vi_ih = vn - > vn_ins_ih ;
vi - > vi_item = vn - > vn_data ;
vi - > vi_uarea = vn - > vn_free_ptr ;
op_create_vi ( vn , vi , 0 /*not pasted or cut */ ,
tb - > insert_size [ 0 ] ) ;
}
2014-04-23 18:00:36 +04:00
/*
* set right merge flag we take right delimiting key and
* check whether it is a mergeable item
*/
2005-07-13 07:21:28 +04:00
if ( tb - > CFR [ 0 ] ) {
struct reiserfs_key * key ;
2014-04-23 18:00:35 +04:00
key = internal_key ( tb - > CFR [ 0 ] , tb - > rkey [ 0 ] ) ;
2005-07-13 07:21:28 +04:00
if ( op_is_left_mergeable ( key , Sh - > b_size )
& & ( vn - > vn_mode ! = M_DELETE
| | vn - > vn_affected_item_num ! = B_NR_ITEMS ( Sh ) - 1 ) )
vn - > vn_vi [ vn - > vn_nr_item - 1 ] . vi_type | =
VI_TYPE_RIGHT_MERGEABLE ;
# ifdef CONFIG_REISERFS_CHECK
if ( op_is_left_mergeable ( key , Sh - > b_size ) & &
! ( vn - > vn_mode ! = M_DELETE
| | vn - > vn_affected_item_num ! = B_NR_ITEMS ( Sh ) - 1 ) ) {
2014-04-23 18:00:36 +04:00
/*
* we delete last item and it could be merged
* with right neighbor ' s first item
*/
2005-07-13 07:21:28 +04:00
if ( !
( B_NR_ITEMS ( Sh ) = = 1
2014-04-23 18:00:35 +04:00
& & is_direntry_le_ih ( item_head ( Sh , 0 ) )
& & ih_entry_count ( item_head ( Sh , 0 ) ) = = 1 ) ) {
2014-04-23 18:00:36 +04:00
/*
* node contains more than 1 item , or item
* is not directory item , or this item
* contains more than 1 entry
*/
2005-07-13 07:21:28 +04:00
print_block ( Sh , 0 , - 1 , - 1 ) ;
2009-03-30 22:02:25 +04:00
reiserfs_panic ( tb - > tb_sb , " vs-8045 " ,
" rdkey %k, affected item==%d "
" (mode==%c) Must be %c " ,
2005-07-13 07:21:28 +04:00
key , vn - > vn_affected_item_num ,
vn - > vn_mode , M_DELETE ) ;
2006-03-25 14:07:15 +03:00
}
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
# endif
2005-07-13 07:21:28 +04:00
}
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* Using virtual node check , how many items can be
* shifted to left neighbor
*/
2005-07-13 07:21:28 +04:00
static void check_left ( struct tree_balance * tb , int h , int cur_free )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
int i ;
struct virtual_node * vn = tb - > tb_vn ;
struct virtual_item * vi ;
int d_size , ih_size ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
RFALSE ( cur_free < 0 , " vs-8050: cur_free (%d) < 0 " , cur_free ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* internal level */
if ( h > 0 ) {
tb - > lnum [ h ] = cur_free / ( DC_SIZE + KEY_SIZE ) ;
return ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* leaf level */
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( ! cur_free | | ! vn - > vn_nr_item ) {
/* no free space or nothing to move */
tb - > lnum [ h ] = 0 ;
tb - > lbytes = - 1 ;
return ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
RFALSE ( ! PATH_H_PPARENT ( tb - > tb_path , 0 ) ,
" vs-8055: parent does not exist or invalid " ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
vi = vn - > vn_vi ;
if ( ( unsigned int ) cur_free > =
( vn - > vn_size -
( ( vi - > vi_type & VI_TYPE_LEFT_MERGEABLE ) ? IH_SIZE : 0 ) ) ) {
/* all contents of S[0] fits into L[0] */
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
RFALSE ( vn - > vn_mode = = M_INSERT | | vn - > vn_mode = = M_PASTE ,
" vs-8055: invalid mode or balance condition failed " ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
tb - > lnum [ 0 ] = vn - > vn_nr_item ;
tb - > lbytes = - 1 ;
return ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
d_size = 0 , ih_size = IH_SIZE ;
/* first item may be merge with last item in left neighbor */
if ( vi - > vi_type & VI_TYPE_LEFT_MERGEABLE )
d_size = - ( ( int ) IH_SIZE ) , ih_size = 0 ;
tb - > lnum [ 0 ] = 0 ;
for ( i = 0 ; i < vn - > vn_nr_item ;
i + + , ih_size = IH_SIZE , d_size = 0 , vi + + ) {
d_size + = vi - > vi_item_len ;
if ( cur_free > = d_size ) {
/* the item can be shifted entirely */
cur_free - = d_size ;
tb - > lnum [ 0 ] + + ;
continue ;
}
/* the item cannot be shifted entirely, try to split it */
2014-04-23 18:00:36 +04:00
/*
* check whether L [ 0 ] can hold ih and at least one byte
* of the item body
*/
/* cannot shift even a part of the current item */
2005-07-13 07:21:28 +04:00
if ( cur_free < = ih_size ) {
tb - > lbytes = - 1 ;
return ;
}
cur_free - = ih_size ;
tb - > lbytes = op_check_left ( vi , cur_free , 0 , 0 ) ;
if ( tb - > lbytes ! = - 1 )
/* count partially shifted item */
tb - > lnum [ 0 ] + + ;
break ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
return ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* Using virtual node check , how many items can be
* shifted to right neighbor
*/
2005-07-13 07:21:28 +04:00
static void check_right ( struct tree_balance * tb , int h , int cur_free )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
int i ;
struct virtual_node * vn = tb - > tb_vn ;
struct virtual_item * vi ;
int d_size , ih_size ;
RFALSE ( cur_free < 0 , " vs-8070: cur_free < 0 " ) ;
/* internal level */
if ( h > 0 ) {
tb - > rnum [ h ] = cur_free / ( DC_SIZE + KEY_SIZE ) ;
return ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* leaf level */
if ( ! cur_free | | ! vn - > vn_nr_item ) {
/* no free space */
tb - > rnum [ h ] = 0 ;
tb - > rbytes = - 1 ;
return ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
RFALSE ( ! PATH_H_PPARENT ( tb - > tb_path , 0 ) ,
" vs-8075: parent does not exist or invalid " ) ;
vi = vn - > vn_vi + vn - > vn_nr_item - 1 ;
if ( ( unsigned int ) cur_free > =
( vn - > vn_size -
( ( vi - > vi_type & VI_TYPE_RIGHT_MERGEABLE ) ? IH_SIZE : 0 ) ) ) {
/* all contents of S[0] fits into R[0] */
RFALSE ( vn - > vn_mode = = M_INSERT | | vn - > vn_mode = = M_PASTE ,
" vs-8080: invalid mode or balance condition failed " ) ;
tb - > rnum [ h ] = vn - > vn_nr_item ;
tb - > rbytes = - 1 ;
return ;
}
d_size = 0 , ih_size = IH_SIZE ;
/* last item may be merge with first item in right neighbor */
if ( vi - > vi_type & VI_TYPE_RIGHT_MERGEABLE )
d_size = - ( int ) IH_SIZE , ih_size = 0 ;
tb - > rnum [ 0 ] = 0 ;
for ( i = vn - > vn_nr_item - 1 ; i > = 0 ;
i - - , d_size = 0 , ih_size = IH_SIZE , vi - - ) {
d_size + = vi - > vi_item_len ;
if ( cur_free > = d_size ) {
/* the item can be shifted entirely */
cur_free - = d_size ;
tb - > rnum [ 0 ] + + ;
continue ;
}
2014-04-23 18:00:36 +04:00
/*
* check whether R [ 0 ] can hold ih and at least one
* byte of the item body
*/
/* cannot shift even a part of the current item */
if ( cur_free < = ih_size ) {
2005-07-13 07:21:28 +04:00
tb - > rbytes = - 1 ;
return ;
}
2014-04-23 18:00:36 +04:00
/*
* R [ 0 ] can hold the header of the item and at least
* one byte of its body
*/
2005-07-13 07:21:28 +04:00
cur_free - = ih_size ; /* cur_free is still > 0 */
tb - > rbytes = op_check_right ( vi , cur_free ) ;
if ( tb - > rbytes ! = - 1 )
/* count partially shifted item */
tb - > rnum [ 0 ] + + ;
break ;
}
return ;
}
2005-04-17 02:20:36 +04:00
/*
* from - number of items , which are shifted to left neighbor entirely
* to - number of item , which are shifted to right neighbor entirely
2014-04-23 18:00:36 +04:00
* from_bytes - number of bytes of boundary item ( or directory entries )
* which are shifted to left neighbor
* to_bytes - number of bytes of boundary item ( or directory entries )
* which are shifted to right neighbor
*/
2005-07-13 07:21:28 +04:00
static int get_num_ver ( int mode , struct tree_balance * tb , int h ,
int from , int from_bytes ,
int to , int to_bytes , short * snum012 , int flow )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
int i ;
int cur_free ;
int units ;
struct virtual_node * vn = tb - > tb_vn ;
int total_node_size , max_node_size , current_item_size ;
int needed_nodes ;
2014-04-23 18:00:36 +04:00
/* position of item we start filling node from */
int start_item ;
/* position of item we finish filling node by */
int end_item ;
/*
* number of first bytes ( entries for directory ) of start_item - th item
* we do not include into node that is being filled
*/
int start_bytes ;
/*
* number of last bytes ( entries for directory ) of end_item - th item
* we do node include into node that is being filled
*/
int end_bytes ;
/*
* these are positions in virtual item of items , that are split
* between S [ 0 ] and S1new and S1new and S2new
*/
int split_item_positions [ 2 ] ;
2005-07-13 07:21:28 +04:00
split_item_positions [ 0 ] = - 1 ;
split_item_positions [ 1 ] = - 1 ;
2014-04-23 18:00:36 +04:00
/*
* We only create additional nodes if we are in insert or paste mode
* or we are in replace mode at the internal level . If h is 0 and
* the mode is M_REPLACE then in fix_nodes we change the mode to
* paste or insert before we get here in the code .
*/
2005-07-13 07:21:28 +04:00
RFALSE ( tb - > insert_size [ h ] < 0 | | ( mode ! = M_INSERT & & mode ! = M_PASTE ) ,
" vs-8100: insert_size < 0 in overflow " ) ;
max_node_size = MAX_CHILD_SIZE ( PATH_H_PBUFFER ( tb - > tb_path , h ) ) ;
2014-04-23 18:00:36 +04:00
/*
* snum012 [ 0 - 2 ] - number of items , that lay
* to S [ 0 ] , first new node and second new node
*/
2005-07-13 07:21:28 +04:00
snum012 [ 3 ] = - 1 ; /* s1bytes */
snum012 [ 4 ] = - 1 ; /* s2bytes */
/* internal level */
if ( h > 0 ) {
i = ( ( to - from ) * ( KEY_SIZE + DC_SIZE ) + DC_SIZE ) ;
if ( i = = max_node_size )
return 1 ;
return ( i / max_node_size + 1 ) ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* leaf level */
needed_nodes = 1 ;
total_node_size = 0 ;
cur_free = max_node_size ;
2014-04-23 18:00:36 +04:00
/* start from 'from'-th item */
2005-07-13 07:21:28 +04:00
start_item = from ;
2014-04-23 18:00:36 +04:00
/* skip its first 'start_bytes' units */
2005-07-13 07:21:28 +04:00
start_bytes = ( ( from_bytes ! = - 1 ) ? from_bytes : 0 ) ;
2014-04-23 18:00:36 +04:00
/* last included item is the 'end_item'-th one */
2005-07-13 07:21:28 +04:00
end_item = vn - > vn_nr_item - to - 1 ;
2014-04-23 18:00:36 +04:00
/* do not count last 'end_bytes' units of 'end_item'-th item */
2005-07-13 07:21:28 +04:00
end_bytes = ( to_bytes ! = - 1 ) ? to_bytes : 0 ;
2014-04-23 18:00:36 +04:00
/*
* go through all item beginning from the start_item - th item
* and ending by the end_item - th item . Do not count first
* ' start_bytes ' units of ' start_item ' - th item and last
* ' end_bytes ' of ' end_item ' - th item
*/
2005-07-13 07:21:28 +04:00
for ( i = start_item ; i < = end_item ; i + + ) {
struct virtual_item * vi = vn - > vn_vi + i ;
int skip_from_end = ( ( i = = end_item ) ? end_bytes : 0 ) ;
RFALSE ( needed_nodes > 3 , " vs-8105: too many nodes are needed " ) ;
/* get size of current item */
current_item_size = vi - > vi_item_len ;
2014-04-23 18:00:36 +04:00
/*
* do not take in calculation head part ( from_bytes )
* of from - th item
*/
2005-07-13 07:21:28 +04:00
current_item_size - =
op_part_size ( vi , 0 /*from start */ , start_bytes ) ;
/* do not take in calculation tail part of last item */
current_item_size - =
op_part_size ( vi , 1 /*from end */ , skip_from_end ) ;
/* if item fits into current node entierly */
if ( total_node_size + current_item_size < = max_node_size ) {
snum012 [ needed_nodes - 1 ] + + ;
total_node_size + = current_item_size ;
start_bytes = 0 ;
continue ;
}
2014-04-23 18:00:36 +04:00
/*
* virtual item length is longer , than max size of item in
* a node . It is impossible for direct item
*/
2005-07-13 07:21:28 +04:00
if ( current_item_size > max_node_size ) {
RFALSE ( is_direct_le_ih ( vi - > vi_ih ) ,
" vs-8110: "
" direct item length is %d. It can not be longer than %d " ,
current_item_size , max_node_size ) ;
/* we will try to split it */
flow = 1 ;
}
2014-04-23 18:00:36 +04:00
/* as we do not split items, take new node and continue */
2005-07-13 07:21:28 +04:00
if ( ! flow ) {
needed_nodes + + ;
i - - ;
total_node_size = 0 ;
continue ;
}
2014-04-23 18:00:36 +04:00
/*
* calculate number of item units which fit into node being
* filled
*/
2005-07-13 07:21:28 +04:00
{
int free_space ;
free_space = max_node_size - total_node_size - IH_SIZE ;
units =
op_check_left ( vi , free_space , start_bytes ,
skip_from_end ) ;
2014-04-23 18:00:36 +04:00
/*
* nothing fits into current node , take new
* node and continue
*/
2005-07-13 07:21:28 +04:00
if ( units = = - 1 ) {
needed_nodes + + , i - - , total_node_size = 0 ;
continue ;
}
}
/* something fits into the current node */
start_bytes + = units ;
snum012 [ needed_nodes - 1 + 3 ] = units ;
if ( needed_nodes > 2 )
2009-03-30 22:02:21 +04:00
reiserfs_warning ( tb - > tb_sb , " vs-8111 " ,
" split_item_position is out of range " ) ;
2005-07-13 07:21:28 +04:00
snum012 [ needed_nodes - 1 ] + + ;
split_item_positions [ needed_nodes - 1 ] = i ;
needed_nodes + + ;
/* continue from the same item with start_bytes != -1 */
start_item = i ;
i - - ;
total_node_size = 0 ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* sum012 [ 4 ] ( if it is not - 1 ) contains number of units of which
* are to be in S1new , snum012 [ 3 ] - to be in S0 . They are supposed
* to be S1bytes and S2bytes correspondingly , so recalculate
*/
2005-07-13 07:21:28 +04:00
if ( snum012 [ 4 ] > 0 ) {
int split_item_num ;
int bytes_to_r , bytes_to_l ;
int bytes_to_S1new ;
split_item_num = split_item_positions [ 1 ] ;
bytes_to_l =
( ( from = = split_item_num
& & from_bytes ! = - 1 ) ? from_bytes : 0 ) ;
bytes_to_r =
( ( end_item = = split_item_num
& & end_bytes ! = - 1 ) ? end_bytes : 0 ) ;
bytes_to_S1new =
( ( split_item_positions [ 0 ] = =
split_item_positions [ 1 ] ) ? snum012 [ 3 ] : 0 ) ;
2014-04-23 18:00:36 +04:00
/* s2bytes */
2005-07-13 07:21:28 +04:00
snum012 [ 4 ] =
op_unit_num ( & vn - > vn_vi [ split_item_num ] ) - snum012 [ 4 ] -
bytes_to_r - bytes_to_l - bytes_to_S1new ;
if ( vn - > vn_vi [ split_item_num ] . vi_index ! = TYPE_DIRENTRY & &
vn - > vn_vi [ split_item_num ] . vi_index ! = TYPE_INDIRECT )
2009-03-30 22:02:21 +04:00
reiserfs_warning ( tb - > tb_sb , " vs-8115 " ,
" not directory or indirect item " ) ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* now we know S2bytes, calculate S1bytes */
if ( snum012 [ 3 ] > 0 ) {
int split_item_num ;
int bytes_to_r , bytes_to_l ;
int bytes_to_S2new ;
split_item_num = split_item_positions [ 0 ] ;
bytes_to_l =
( ( from = = split_item_num
& & from_bytes ! = - 1 ) ? from_bytes : 0 ) ;
bytes_to_r =
( ( end_item = = split_item_num
& & end_bytes ! = - 1 ) ? end_bytes : 0 ) ;
bytes_to_S2new =
( ( split_item_positions [ 0 ] = = split_item_positions [ 1 ]
& & snum012 [ 4 ] ! = - 1 ) ? snum012 [ 4 ] : 0 ) ;
2014-04-23 18:00:36 +04:00
/* s1bytes */
2005-07-13 07:21:28 +04:00
snum012 [ 3 ] =
op_unit_num ( & vn - > vn_vi [ split_item_num ] ) - snum012 [ 3 ] -
bytes_to_r - bytes_to_l - bytes_to_S2new ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
return needed_nodes ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Set parameters for balancing .
2005-04-17 02:20:36 +04:00
* Performs write of results of analysis of balancing into structure tb ,
2009-03-30 22:02:44 +04:00
* where it will later be used by the functions that actually do the balancing .
2005-04-17 02:20:36 +04:00
* Parameters :
* tb tree_balance structure ;
* h current level of the node ;
* lnum number of items from S [ h ] that must be shifted to L [ h ] ;
* rnum number of items from S [ h ] that must be shifted to R [ h ] ;
* blk_num number of blocks that S [ h ] will be splitted into ;
* s012 number of items that fall into splitted nodes .
2014-04-23 18:00:36 +04:00
* lbytes number of bytes which flow to the left neighbor from the
* item that is not not shifted entirely
* rbytes number of bytes which flow to the right neighbor from the
* item that is not not shifted entirely
* s1bytes number of bytes which flow to the first new node when
* S [ 0 ] splits ( this number is contained in s012 array )
2005-04-17 02:20:36 +04:00
*/
2005-07-13 07:21:28 +04:00
static void set_parameters ( struct tree_balance * tb , int h , int lnum ,
int rnum , int blk_num , short * s012 , int lb , int rb )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
tb - > lnum [ h ] = lnum ;
tb - > rnum [ h ] = rnum ;
tb - > blknum [ h ] = blk_num ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/* only for leaf level */
if ( h = = 0 ) {
2005-07-13 07:21:28 +04:00
if ( s012 ! = NULL ) {
2014-04-23 18:00:46 +04:00
tb - > s0num = * s012 + + ;
tb - > snum [ 0 ] = * s012 + + ;
tb - > snum [ 1 ] = * s012 + + ;
tb - > sbytes [ 0 ] = * s012 + + ;
tb - > sbytes [ 1 ] = * s012 ;
2005-07-13 07:21:28 +04:00
}
tb - > lbytes = lb ;
tb - > rbytes = rb ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
PROC_INFO_ADD ( tb - > tb_sb , lnum [ h ] , lnum ) ;
PROC_INFO_ADD ( tb - > tb_sb , rnum [ h ] , rnum ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
PROC_INFO_ADD ( tb - > tb_sb , lbytes [ h ] , lb ) ;
PROC_INFO_ADD ( tb - > tb_sb , rbytes [ h ] , rb ) ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* check if node disappears if we shift tb - > lnum [ 0 ] items to left
* neighbor and tb - > rnum [ 0 ] to the right one .
*/
2005-07-13 07:21:28 +04:00
static int is_leaf_removable ( struct tree_balance * tb )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct virtual_node * vn = tb - > tb_vn ;
int to_left , to_right ;
int size ;
int remain_items ;
2014-04-23 18:00:36 +04:00
/*
* number of items that will be shifted to left ( right ) neighbor
* entirely
*/
2005-07-13 07:21:28 +04:00
to_left = tb - > lnum [ 0 ] - ( ( tb - > lbytes ! = - 1 ) ? 1 : 0 ) ;
to_right = tb - > rnum [ 0 ] - ( ( tb - > rbytes ! = - 1 ) ? 1 : 0 ) ;
remain_items = vn - > vn_nr_item ;
/* how many items remain in S[0] after shiftings to neighbors */
remain_items - = ( to_left + to_right ) ;
2014-04-23 18:00:36 +04:00
/* all content of node can be shifted to neighbors */
2005-07-13 07:21:28 +04:00
if ( remain_items < 1 ) {
set_parameters ( tb , 0 , to_left , vn - > vn_nr_item - to_left , 0 ,
NULL , - 1 , - 1 ) ;
return 1 ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/* S[0] is not removable */
2005-07-13 07:21:28 +04:00
if ( remain_items > 1 | | tb - > lbytes = = - 1 | | tb - > rbytes = = - 1 )
return 0 ;
2014-04-23 18:00:36 +04:00
/* check whether we can divide 1 remaining item between neighbors */
2005-07-13 07:21:28 +04:00
/* get size of remaining item (in item units) */
2014-04-23 18:00:42 +04:00
size = op_unit_num ( & vn - > vn_vi [ to_left ] ) ;
2005-07-13 07:21:28 +04:00
if ( tb - > lbytes + tb - > rbytes > = size ) {
set_parameters ( tb , 0 , to_left + 1 , to_right + 1 , 0 , NULL ,
tb - > lbytes , - 1 ) ;
return 1 ;
}
return 0 ;
}
2005-04-17 02:20:36 +04:00
/* check whether L, S, R can be joined in one node */
2005-07-13 07:21:28 +04:00
static int are_leaves_removable ( struct tree_balance * tb , int lfree , int rfree )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct virtual_node * vn = tb - > tb_vn ;
int ih_size ;
struct buffer_head * S0 ;
S0 = PATH_H_PBUFFER ( tb - > tb_path , 0 ) ;
ih_size = 0 ;
if ( vn - > vn_nr_item ) {
if ( vn - > vn_vi [ 0 ] . vi_type & VI_TYPE_LEFT_MERGEABLE )
ih_size + = IH_SIZE ;
if ( vn - > vn_vi [ vn - > vn_nr_item - 1 ] .
vi_type & VI_TYPE_RIGHT_MERGEABLE )
ih_size + = IH_SIZE ;
} else {
/* there was only one item and it will be deleted */
struct item_head * ih ;
RFALSE ( B_NR_ITEMS ( S0 ) ! = 1 ,
" vs-8125: item number must be 1: it is %d " ,
B_NR_ITEMS ( S0 ) ) ;
2014-04-23 18:00:35 +04:00
ih = item_head ( S0 , 0 ) ;
2005-07-13 07:21:28 +04:00
if ( tb - > CFR [ 0 ]
2014-04-23 18:00:42 +04:00
& & ! comp_short_le_keys ( & ih - > ih_key ,
2014-04-23 18:00:35 +04:00
internal_key ( tb - > CFR [ 0 ] ,
2005-07-13 07:21:28 +04:00
tb - > rkey [ 0 ] ) ) )
2014-04-23 18:00:36 +04:00
/*
* Directory must be in correct state here : that is
* somewhere at the left side should exist first
* directory item . But the item being deleted can
* not be that first one because its right neighbor
* is item of the same directory . ( But first item
* always gets deleted in last turn ) . So , neighbors
* of deleted item can be merged , so we can save
* ih_size
*/
2005-07-13 07:21:28 +04:00
if ( is_direntry_le_ih ( ih ) ) {
ih_size = IH_SIZE ;
2014-04-23 18:00:36 +04:00
/*
* we might check that left neighbor exists
* and is of the same directory
*/
2005-07-13 07:21:28 +04:00
RFALSE ( le_ih_k_offset ( ih ) = = DOT_OFFSET ,
" vs-8130: first directory item can not be removed until directory is not empty " ) ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
}
if ( MAX_CHILD_SIZE ( S0 ) + vn - > vn_size < = rfree + lfree + ih_size ) {
set_parameters ( tb , 0 , - 1 , - 1 , - 1 , NULL , - 1 , - 1 ) ;
PROC_INFO_INC ( tb - > tb_sb , leaves_removable ) ;
return 1 ;
}
return 0 ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
/* when we do not split item, lnum and rnum are numbers of entire items */
# define SET_PAR_SHIFT_LEFT \
if ( h ) \
{ \
int to_l ; \
\
to_l = ( MAX_NR_KEY ( Sh ) + 1 - lpar + vn - > vn_nr_item + 1 ) / 2 - \
( MAX_NR_KEY ( Sh ) + 1 - lpar ) ; \
\
set_parameters ( tb , h , to_l , 0 , lnver , NULL , - 1 , - 1 ) ; \
} \
else \
{ \
if ( lset = = LEFT_SHIFT_FLOW ) \
set_parameters ( tb , h , lpar , 0 , lnver , snum012 + lset , \
tb - > lbytes , - 1 ) ; \
else \
set_parameters ( tb , h , lpar - ( tb - > lbytes ! = - 1 ) , 0 , lnver , snum012 + lset , \
- 1 , - 1 ) ; \
}
# define SET_PAR_SHIFT_RIGHT \
if ( h ) \
{ \
int to_r ; \
\
to_r = ( MAX_NR_KEY ( Sh ) + 1 - rpar + vn - > vn_nr_item + 1 ) / 2 - ( MAX_NR_KEY ( Sh ) + 1 - rpar ) ; \
\
set_parameters ( tb , h , 0 , to_r , rnver , NULL , - 1 , - 1 ) ; \
} \
else \
{ \
if ( rset = = RIGHT_SHIFT_FLOW ) \
set_parameters ( tb , h , 0 , rpar , rnver , snum012 + rset , \
- 1 , tb - > rbytes ) ; \
else \
set_parameters ( tb , h , 0 , rpar - ( tb - > rbytes ! = - 1 ) , rnver , snum012 + rset , \
- 1 , - 1 ) ; \
}
2009-03-30 22:02:48 +04:00
static void free_buffers_in_tb ( struct tree_balance * tb )
2005-07-13 07:21:28 +04:00
{
2009-03-30 22:02:50 +04:00
int i ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:48 +04:00
pathrelse ( tb - > tb_path ) ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
for ( i = 0 ; i < MAX_HEIGHT ; i + + ) {
brelse ( tb - > L [ i ] ) ;
brelse ( tb - > R [ i ] ) ;
brelse ( tb - > FL [ i ] ) ;
brelse ( tb - > FR [ i ] ) ;
brelse ( tb - > CFL [ i ] ) ;
brelse ( tb - > CFR [ i ] ) ;
tb - > L [ i ] = NULL ;
tb - > R [ i ] = NULL ;
tb - > FL [ i ] = NULL ;
tb - > FR [ i ] = NULL ;
tb - > CFL [ i ] = NULL ;
tb - > CFR [ i ] = NULL ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Get new buffers for storing new nodes that are created while balancing .
2005-04-17 02:20:36 +04:00
* Returns : SCHEDULE_OCCURRED - schedule occurred while the function worked ;
* CARRY_ON - schedule didn ' t occur while the function worked ;
* NO_DISK_SPACE - no disk space .
*/
/* The function is NOT SCHEDULE-SAFE! */
2009-03-30 22:02:50 +04:00
static int get_empty_nodes ( struct tree_balance * tb , int h )
2005-07-13 07:21:28 +04:00
{
2014-04-23 18:00:36 +04:00
struct buffer_head * new_bh , * Sh = PATH_H_PBUFFER ( tb - > tb_path , h ) ;
2009-03-30 22:02:50 +04:00
b_blocknr_t * blocknr , blocknrs [ MAX_AMOUNT_NEEDED ] = { 0 , } ;
2014-04-23 18:00:36 +04:00
int counter , number_of_freeblk ;
int amount_needed ; /* number of needed empty blocks */
int retval = CARRY_ON ;
2009-03-30 22:02:48 +04:00
struct super_block * sb = tb - > tb_sb ;
2005-07-13 07:21:28 +04:00
2014-04-23 18:00:36 +04:00
/*
* number_of_freeblk is the number of empty blocks which have been
* acquired for use by the balancing algorithm minus the number of
* empty blocks used in the previous levels of the analysis ,
* number_of_freeblk = tb - > cur_blknum can be non - zero if a schedule
* occurs after empty blocks are acquired , and the balancing analysis
* is then restarted , amount_needed is the number needed by this
* level ( h ) of the balancing analysis .
*
* Note that for systems with many processes writing , it would be
* more layout optimal to calculate the total number needed by all
* levels and then to run reiserfs_new_blocks to get all of them at
* once .
*/
/*
* Initiate number_of_freeblk to the amount acquired prior to the
* restart of the analysis or 0 if not restarted , then subtract the
* amount needed by all of the levels of the tree below h .
*/
2009-03-30 22:02:50 +04:00
/* blknum includes S[h], so we subtract 1 in this calculation */
for ( counter = 0 , number_of_freeblk = tb - > cur_blknum ;
counter < h ; counter + + )
number_of_freeblk - =
( tb - > blknum [ counter ] ) ? ( tb - > blknum [ counter ] -
2005-07-13 07:21:28 +04:00
1 ) : 0 ;
/* Allocate missing empty blocks. */
2009-03-30 22:02:49 +04:00
/* if Sh == 0 then we are getting a new root */
2009-03-30 22:02:50 +04:00
amount_needed = ( Sh ) ? ( tb - > blknum [ h ] - 1 ) : 1 ;
2014-04-23 18:00:36 +04:00
/*
* Amount_needed = the amount that we need more than the
* amount that we have .
*/
2009-03-30 22:02:50 +04:00
if ( amount_needed > number_of_freeblk )
amount_needed - = number_of_freeblk ;
2014-04-23 18:00:36 +04:00
else /* If we have enough already then there is nothing to do. */
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
2014-04-23 18:00:36 +04:00
/*
* No need to check quota - is not allocated for blocks used
* for formatted nodes
*/
2009-03-30 22:02:50 +04:00
if ( reiserfs_new_form_blocknrs ( tb , blocknrs ,
amount_needed ) = = NO_DISK_SPACE )
2005-07-13 07:21:28 +04:00
return NO_DISK_SPACE ;
/* for each blocknumber we just got, get a buffer and stick it on FEB */
2009-03-30 22:02:50 +04:00
for ( blocknr = blocknrs , counter = 0 ;
counter < amount_needed ; blocknr + + , counter + + ) {
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:49 +04:00
RFALSE ( ! * blocknr ,
2005-07-13 07:21:28 +04:00
" PAP-8135: reiserfs_new_blocknrs failed when got new blocks " ) ;
2009-03-30 22:02:49 +04:00
new_bh = sb_getblk ( sb , * blocknr ) ;
RFALSE ( buffer_dirty ( new_bh ) | |
buffer_journaled ( new_bh ) | |
buffer_journal_dirty ( new_bh ) ,
2009-12-04 23:24:22 +03:00
" PAP-8140: journaled or dirty buffer %b for the new block " ,
2009-03-30 22:02:49 +04:00
new_bh ) ;
2005-07-13 07:21:28 +04:00
/* Put empty buffers into the array. */
2009-03-30 22:02:48 +04:00
RFALSE ( tb - > FEB [ tb - > cur_blknum ] ,
2005-07-13 07:21:28 +04:00
" PAP-8141: busy slot for new buffer " ) ;
2009-03-30 22:02:49 +04:00
set_buffer_journal_new ( new_bh ) ;
tb - > FEB [ tb - > cur_blknum + + ] = new_bh ;
2005-07-13 07:21:28 +04:00
}
2009-03-30 22:02:50 +04:00
if ( retval = = CARRY_ON & & FILESYSTEM_CHANGED_TB ( tb ) )
retval = REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:50 +04:00
return retval ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* Get free space of the left neighbor , which is stored in the parent
* node of the left neighbor .
*/
2005-07-13 07:21:28 +04:00
static int get_lfree ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct buffer_head * l , * f ;
int order ;
2005-04-17 02:20:36 +04:00
2008-03-29 06:07:28 +03:00
if ( ( f = PATH_H_PPARENT ( tb - > tb_path , h ) ) = = NULL | |
( l = tb - > FL [ h ] ) = = NULL )
2005-07-13 07:21:28 +04:00
return 0 ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( f = = l )
order = PATH_H_B_ITEM_ORDER ( tb - > tb_path , h ) - 1 ;
else {
order = B_NR_ITEMS ( l ) ;
f = l ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
return ( MAX_CHILD_SIZE ( f ) - dc_size ( B_N_CHILD ( f , order ) ) ) ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Get free space of the right neighbor ,
2005-04-17 02:20:36 +04:00
* which is stored in the parent node of the right neighbor .
*/
2005-07-13 07:21:28 +04:00
static int get_rfree ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct buffer_head * r , * f ;
int order ;
2005-04-17 02:20:36 +04:00
2008-03-29 06:07:28 +03:00
if ( ( f = PATH_H_PPARENT ( tb - > tb_path , h ) ) = = NULL | |
( r = tb - > FR [ h ] ) = = NULL )
2005-07-13 07:21:28 +04:00
return 0 ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( f = = r )
order = PATH_H_B_ITEM_ORDER ( tb - > tb_path , h ) + 1 ;
else {
order = 0 ;
f = r ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
return ( MAX_CHILD_SIZE ( f ) - dc_size ( B_N_CHILD ( f , order ) ) ) ;
2005-04-17 02:20:36 +04:00
}
/* Check whether left neighbor is in memory. */
2009-03-30 22:02:50 +04:00
static int is_left_neighbor_in_cache ( struct tree_balance * tb , int h )
2005-07-13 07:21:28 +04:00
{
2009-03-30 22:02:49 +04:00
struct buffer_head * father , * left ;
2009-03-30 22:02:48 +04:00
struct super_block * sb = tb - > tb_sb ;
2009-03-30 22:02:50 +04:00
b_blocknr_t left_neighbor_blocknr ;
int left_neighbor_position ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:48 +04:00
/* Father of the left neighbor does not exist. */
2009-03-30 22:02:50 +04:00
if ( ! tb - > FL [ h ] )
2005-07-13 07:21:28 +04:00
return 0 ;
/* Calculate father of the node to be balanced. */
2009-03-30 22:02:50 +04:00
father = PATH_H_PBUFFER ( tb - > tb_path , h + 1 ) ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:49 +04:00
RFALSE ( ! father | |
! B_IS_IN_TREE ( father ) | |
2009-03-30 22:02:50 +04:00
! B_IS_IN_TREE ( tb - > FL [ h ] ) | |
2009-03-30 22:02:49 +04:00
! buffer_uptodate ( father ) | |
2009-03-30 22:02:50 +04:00
! buffer_uptodate ( tb - > FL [ h ] ) ,
2005-07-13 07:21:28 +04:00
" vs-8165: F[h] (%b) or FL[h] (%b) is invalid " ,
2009-03-30 22:02:50 +04:00
father , tb - > FL [ h ] ) ;
2005-07-13 07:21:28 +04:00
2014-04-23 18:00:36 +04:00
/*
* Get position of the pointer to the left neighbor
* into the left father .
*/
2009-03-30 22:02:50 +04:00
left_neighbor_position = ( father = = tb - > FL [ h ] ) ?
tb - > lkey [ h ] : B_NR_ITEMS ( tb - > FL [ h ] ) ;
2005-07-13 07:21:28 +04:00
/* Get left neighbor block number. */
2009-03-30 22:02:50 +04:00
left_neighbor_blocknr =
B_N_CHILD_NUM ( tb - > FL [ h ] , left_neighbor_position ) ;
2005-07-13 07:21:28 +04:00
/* Look for the left neighbor in the cache. */
2009-03-30 22:02:50 +04:00
if ( ( left = sb_find_get_block ( sb , left_neighbor_blocknr ) ) ) {
2005-07-13 07:21:28 +04:00
RFALSE ( buffer_uptodate ( left ) & & ! B_IS_IN_TREE ( left ) ,
" vs-8170: left neighbor (%b %z) is not in the tree " ,
left , left ) ;
put_bh ( left ) ;
return 1 ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
return 0 ;
}
2005-04-17 02:20:36 +04:00
# define LEFT_PARENTS 'l'
# define RIGHT_PARENTS 'r'
2009-03-30 22:02:49 +04:00
static void decrement_key ( struct cpu_key * key )
2005-04-17 02:20:36 +04:00
{
2014-04-23 18:00:36 +04:00
/* call item specific function for this key */
2009-03-30 22:02:49 +04:00
item_ops [ cpu_key_k_type ( key ) ] - > decrement_key ( key ) ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Calculate far left / right parent of the left / right neighbor of the
* current node , that is calculate the left / right ( FL [ h ] / FR [ h ] ) neighbor
* of the parent F [ h ] .
2005-04-17 02:20:36 +04:00
* Calculate left / right common parent of the current node and L [ h ] / R [ h ] .
* Calculate left / right delimiting key position .
2014-04-23 18:00:36 +04:00
* Returns : PATH_INCORRECT - path in the tree is not correct
* SCHEDULE_OCCURRED - schedule occurred while the function worked
* CARRY_ON - schedule didn ' t occur while the function
* worked
2005-04-17 02:20:36 +04:00
*/
2009-03-30 22:02:48 +04:00
static int get_far_parent ( struct tree_balance * tb ,
2009-03-30 22:02:50 +04:00
int h ,
2009-03-30 22:02:49 +04:00
struct buffer_head * * pfather ,
struct buffer_head * * pcom_father , char c_lr_par )
2005-04-17 02:20:36 +04:00
{
2009-03-30 22:02:49 +04:00
struct buffer_head * parent ;
2005-07-13 07:21:28 +04:00
INITIALIZE_PATH ( s_path_to_neighbor_father ) ;
2009-03-30 22:02:49 +04:00
struct treepath * path = tb - > tb_path ;
2005-07-13 07:21:28 +04:00
struct cpu_key s_lr_father_key ;
2009-03-30 22:02:50 +04:00
int counter ,
position = INT_MAX ,
first_last_position = 0 ,
path_offset = PATH_H_PATH_OFFSET ( path , h ) ;
2005-07-13 07:21:28 +04:00
2014-04-23 18:00:36 +04:00
/*
* Starting from F [ h ] go upwards in the tree , and look for the common
* ancestor of F [ h ] , and its neighbor l / r , that should be obtained .
*/
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
counter = path_offset ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( counter < FIRST_PATH_ELEMENT_OFFSET ,
2005-07-13 07:21:28 +04:00
" PAP-8180: invalid path length " ) ;
2009-03-30 22:02:50 +04:00
for ( ; counter > FIRST_PATH_ELEMENT_OFFSET ; counter - - ) {
2014-04-23 18:00:36 +04:00
/*
* Check whether parent of the current buffer in the path
* is really parent in the tree .
*/
2005-07-13 07:21:28 +04:00
if ( ! B_IS_IN_TREE
2009-03-30 22:02:50 +04:00
( parent = PATH_OFFSET_PBUFFER ( path , counter - 1 ) ) )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2014-04-23 18:00:36 +04:00
2005-07-13 07:21:28 +04:00
/* Check whether position in the parent is correct. */
2009-03-30 22:02:50 +04:00
if ( ( position =
2009-03-30 22:02:49 +04:00
PATH_OFFSET_POSITION ( path ,
2009-03-30 22:02:50 +04:00
counter - 1 ) ) >
2009-03-30 22:02:49 +04:00
B_NR_ITEMS ( parent ) )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2014-04-23 18:00:36 +04:00
/*
* Check whether parent at the path really points
* to the child .
*/
2009-03-30 22:02:50 +04:00
if ( B_N_CHILD_NUM ( parent , position ) ! =
PATH_OFFSET_PBUFFER ( path , counter ) - > b_blocknr )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2014-04-23 18:00:36 +04:00
/*
* Return delimiting key if position in the parent is not
* equal to first / last one .
*/
2005-07-13 07:21:28 +04:00
if ( c_lr_par = = RIGHT_PARENTS )
2009-03-30 22:02:50 +04:00
first_last_position = B_NR_ITEMS ( parent ) ;
if ( position ! = first_last_position ) {
2009-03-30 22:02:49 +04:00
* pcom_father = parent ;
get_bh ( * pcom_father ) ;
/*(*pcom_father = parent)->b_count++; */
2005-07-13 07:21:28 +04:00
break ;
}
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* if we are in the root of the tree, then there is no common father */
2009-03-30 22:02:50 +04:00
if ( counter = = FIRST_PATH_ELEMENT_OFFSET ) {
2014-04-23 18:00:36 +04:00
/*
* Check whether first buffer in the path is the
* root of the tree .
*/
2005-07-13 07:21:28 +04:00
if ( PATH_OFFSET_PBUFFER
2009-03-30 22:02:48 +04:00
( tb - > tb_path ,
2005-07-13 07:21:28 +04:00
FIRST_PATH_ELEMENT_OFFSET ) - > b_blocknr = =
2009-03-30 22:02:48 +04:00
SB_ROOT_BLOCK ( tb - > tb_sb ) ) {
2009-03-30 22:02:49 +04:00
* pfather = * pcom_father = NULL ;
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
}
return REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
}
2009-03-30 22:02:49 +04:00
RFALSE ( B_LEVEL ( * pcom_father ) < = DISK_LEAF_NODE_LEVEL ,
2005-07-13 07:21:28 +04:00
" PAP-8185: (%b %z) level too small " ,
2009-03-30 22:02:49 +04:00
* pcom_father , * pcom_father ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* Check whether the common parent is locked. */
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:49 +04:00
if ( buffer_locked ( * pcom_father ) ) {
reiserfs: kill-the-BKL
This patch is an attempt to remove the Bkl based locking scheme from
reiserfs and is intended.
It is a bit inspired from an old attempt by Peter Zijlstra:
http://lkml.indiana.edu/hypermail/linux/kernel/0704.2/2174.html
The bkl is heavily used in this filesystem to prevent from
concurrent write accesses on the filesystem.
Reiserfs makes a deep use of the specific properties of the Bkl:
- It can be acqquired recursively by a same task
- It is released on the schedule() calls and reacquired when schedule() returns
The two properties above are a roadmap for the reiserfs write locking so it's
very hard to simply replace it with a common mutex.
- We need a recursive-able locking unless we want to restructure several blocks
of the code.
- We need to identify the sites where the bkl was implictly relaxed
(schedule, wait, sync, etc...) so that we can in turn release and
reacquire our new lock explicitly.
Such implicit releases of the lock are often required to let other
resources producer/consumer do their job or we can suffer unexpected
starvations or deadlocks.
So the new lock that replaces the bkl here is a per superblock mutex with a
specific property: it can be acquired recursively by a same task, like the
bkl.
For such purpose, we integrate a lock owner and a lock depth field on the
superblock information structure.
The first axis on this patch is to turn reiserfs_write_(un)lock() function
into a wrapper to manage this mutex. Also some explicit calls to
lock_kernel() have been converted to reiserfs_write_lock() helpers.
The second axis is to find the important blocking sites (schedule...(),
wait_on_buffer(), sync_dirty_buffer(), etc...) and then apply an explicit
release of the write lock on these locations before blocking. Then we can
safely wait for those who can give us resources or those who need some.
Typically this is a fight between the current writer, the reiserfs workqueue
(aka the async commiter) and the pdflush threads.
The third axis is a consequence of the second. The write lock is usually
on top of a lock dependency chain which can include the journal lock, the
flush lock or the commit lock. So it's dangerous to release and trying to
reacquire the write lock while we still hold other locks.
This is fine with the bkl:
T1 T2
lock_kernel()
mutex_lock(A)
unlock_kernel()
// do something
lock_kernel()
mutex_lock(A) -> already locked by T1
schedule() (and then unlock_kernel())
lock_kernel()
mutex_unlock(A)
....
This is not fine with a mutex:
T1 T2
mutex_lock(write)
mutex_lock(A)
mutex_unlock(write)
// do something
mutex_lock(write)
mutex_lock(A) -> already locked by T1
schedule()
mutex_lock(write) -> already locked by T2
deadlock
The solution in this patch is to provide a helper which releases the write
lock and sleep a bit if we can't lock a mutex that depend on it. It's another
simulation of the bkl behaviour.
The last axis is to locate the fs callbacks that are called with the bkl held,
according to Documentation/filesystem/Locking.
Those are:
- reiserfs_remount
- reiserfs_fill_super
- reiserfs_put_super
Reiserfs didn't need to explicitly lock because of the context of these callbacks.
But now we must take care of that with the new locking.
After this patch, reiserfs suffers from a slight performance regression (for now).
On UP, a high volume write with dd reports an average of 27 MB/s instead
of 30 MB/s without the patch applied.
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Reviewed-by: Ingo Molnar <mingo@elte.hu>
Cc: Jeff Mahoney <jeffm@suse.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Bron Gondwana <brong@fastmail.fm>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
LKML-Reference: <1239070789-13354-1-git-send-email-fweisbec@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-04-07 06:19:49 +04:00
/* Release the write lock while the buffer is busy */
2013-08-09 01:34:46 +04:00
int depth = reiserfs_write_unlock_nested ( tb - > tb_sb ) ;
2009-03-30 22:02:49 +04:00
__wait_on_buffer ( * pcom_father ) ;
2013-08-09 01:34:46 +04:00
reiserfs_write_lock_nested ( tb - > tb_sb , depth ) ;
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) ) {
2009-03-30 22:02:49 +04:00
brelse ( * pcom_father ) ;
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
}
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* So , we got common parent of the current node and its
* left / right neighbor . Now we are getting the parent of the
* left / right neighbor .
*/
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* Form key to get parent of the left/right neighbor. */
le_key2cpu_key ( & s_lr_father_key ,
2014-04-23 18:00:35 +04:00
internal_key ( * pcom_father ,
2005-07-13 07:21:28 +04:00
( c_lr_par = =
2009-03-30 22:02:50 +04:00
LEFT_PARENTS ) ? ( tb - > lkey [ h - 1 ] =
position -
1 ) : ( tb - > rkey [ h -
2005-07-13 07:21:28 +04:00
1 ] =
2009-03-30 22:02:50 +04:00
position ) ) ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( c_lr_par = = LEFT_PARENTS )
decrement_key ( & s_lr_father_key ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( search_by_key
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , & s_lr_father_key , & s_path_to_neighbor_father ,
2009-03-30 22:02:50 +04:00
h + 1 ) = = IO_ERROR )
2014-04-23 18:00:36 +04:00
/* path is released */
2005-07-13 07:21:28 +04:00
return IO_ERROR ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) ) {
2009-03-30 22:02:43 +04:00
pathrelse ( & s_path_to_neighbor_father ) ;
2009-03-30 22:02:49 +04:00
brelse ( * pcom_father ) ;
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
}
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:49 +04:00
* pfather = PATH_PLAST_BUFFER ( & s_path_to_neighbor_father ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( B_LEVEL ( * pfather ) ! = h + 1 ,
2009-03-30 22:02:49 +04:00
" PAP-8190: (%b %z) level too small " , * pfather , * pfather ) ;
2005-07-13 07:21:28 +04:00
RFALSE ( s_path_to_neighbor_father . path_length <
FIRST_PATH_ELEMENT_OFFSET , " PAP-8192: path length is too small " ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
s_path_to_neighbor_father . path_length - - ;
2009-03-30 22:02:43 +04:00
pathrelse ( & s_path_to_neighbor_father ) ;
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Get parents of neighbors of node in the path ( S [ path_offset ] ) and
* common parents of S [ path_offset ] and L [ path_offset ] / R [ path_offset ] :
* F [ path_offset ] , FL [ path_offset ] , FR [ path_offset ] , CFL [ path_offset ] ,
* CFR [ path_offset ] .
* Calculate numbers of left and right delimiting keys position :
* lkey [ path_offset ] , rkey [ path_offset ] .
* Returns : SCHEDULE_OCCURRED - schedule occurred while the function worked
* CARRY_ON - schedule didn ' t occur while the function worked
2005-04-17 02:20:36 +04:00
*/
2009-03-30 22:02:50 +04:00
static int get_parents ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2009-03-30 22:02:49 +04:00
struct treepath * path = tb - > tb_path ;
2009-03-30 22:02:50 +04:00
int position ,
ret ,
path_offset = PATH_H_PATH_OFFSET ( tb - > tb_path , h ) ;
2009-03-30 22:02:49 +04:00
struct buffer_head * curf , * curcf ;
2005-07-13 07:21:28 +04:00
/* Current node is the root of the tree or will be root of the tree */
2009-03-30 22:02:50 +04:00
if ( path_offset < = FIRST_PATH_ELEMENT_OFFSET ) {
2014-04-23 18:00:36 +04:00
/*
* The root can not have parents .
* Release nodes which previously were obtained as
* parents of the current node neighbors .
*/
2009-03-30 22:02:50 +04:00
brelse ( tb - > FL [ h ] ) ;
brelse ( tb - > CFL [ h ] ) ;
brelse ( tb - > FR [ h ] ) ;
brelse ( tb - > CFR [ h ] ) ;
tb - > FL [ h ] = NULL ;
tb - > CFL [ h ] = NULL ;
tb - > FR [ h ] = NULL ;
tb - > CFR [ h ] = NULL ;
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
}
2009-03-30 22:02:50 +04:00
/* Get parent FL[path_offset] of L[path_offset]. */
position = PATH_OFFSET_POSITION ( path , path_offset - 1 ) ;
if ( position ) {
2005-07-13 07:21:28 +04:00
/* Current node is not the first child of its parent. */
2009-03-30 22:02:50 +04:00
curf = PATH_OFFSET_PBUFFER ( path , path_offset - 1 ) ;
curcf = PATH_OFFSET_PBUFFER ( path , path_offset - 1 ) ;
2009-03-30 22:02:49 +04:00
get_bh ( curf ) ;
get_bh ( curf ) ;
2009-03-30 22:02:50 +04:00
tb - > lkey [ h ] = position - 1 ;
2005-07-13 07:21:28 +04:00
} else {
2014-04-23 18:00:36 +04:00
/*
* Calculate current parent of L [ path_offset ] , which is the
* left neighbor of the current node . Calculate current
* common parent of L [ path_offset ] and the current node .
* Note that CFL [ path_offset ] not equal FL [ path_offset ] and
* CFL [ path_offset ] not equal F [ path_offset ] .
* Calculate lkey [ path_offset ] .
*/
2009-03-30 22:02:50 +04:00
if ( ( ret = get_far_parent ( tb , h + 1 , & curf ,
2009-03-30 22:02:49 +04:00
& curcf ,
2005-07-13 07:21:28 +04:00
LEFT_PARENTS ) ) ! = CARRY_ON )
2009-03-30 22:02:50 +04:00
return ret ;
2005-07-13 07:21:28 +04:00
}
2009-03-30 22:02:50 +04:00
brelse ( tb - > FL [ h ] ) ;
tb - > FL [ h ] = curf ; /* New initialization of FL[h]. */
brelse ( tb - > CFL [ h ] ) ;
tb - > CFL [ h ] = curcf ; /* New initialization of CFL[h]. */
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:49 +04:00
RFALSE ( ( curf & & ! B_IS_IN_TREE ( curf ) ) | |
( curcf & & ! B_IS_IN_TREE ( curcf ) ) ,
" PAP-8195: FL (%b) or CFL (%b) is invalid " , curf , curcf ) ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/* Get parent FR[h] of R[h]. */
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/* Current node is the last child of F[h]. FR[h] != F[h]. */
2009-03-30 22:02:50 +04:00
if ( position = = B_NR_ITEMS ( PATH_H_PBUFFER ( path , h + 1 ) ) ) {
2014-04-23 18:00:36 +04:00
/*
* Calculate current parent of R [ h ] , which is the right
* neighbor of F [ h ] . Calculate current common parent of
* R [ h ] and current node . Note that CFR [ h ] not equal
* FR [ path_offset ] and CFR [ h ] not equal F [ h ] .
*/
2009-03-30 22:02:50 +04:00
if ( ( ret =
get_far_parent ( tb , h + 1 , & curf , & curcf ,
2005-07-13 07:21:28 +04:00
RIGHT_PARENTS ) ) ! = CARRY_ON )
2009-03-30 22:02:50 +04:00
return ret ;
2005-07-13 07:21:28 +04:00
} else {
2014-04-23 18:00:36 +04:00
/* Current node is not the last child of its parent F[h]. */
2009-03-30 22:02:50 +04:00
curf = PATH_OFFSET_PBUFFER ( path , path_offset - 1 ) ;
curcf = PATH_OFFSET_PBUFFER ( path , path_offset - 1 ) ;
2009-03-30 22:02:49 +04:00
get_bh ( curf ) ;
get_bh ( curf ) ;
2009-03-30 22:02:50 +04:00
tb - > rkey [ h ] = position ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:50 +04:00
brelse ( tb - > FR [ h ] ) ;
/* New initialization of FR[path_offset]. */
tb - > FR [ h ] = curf ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
brelse ( tb - > CFR [ h ] ) ;
/* New initialization of CFR[path_offset]. */
tb - > CFR [ h ] = curcf ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:49 +04:00
RFALSE ( ( curf & & ! B_IS_IN_TREE ( curf ) ) | |
( curcf & & ! B_IS_IN_TREE ( curcf ) ) ,
" PAP-8205: FR (%b) or CFR (%b) is invalid " , curf , curcf ) ;
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* it is possible to remove node as result of shiftings to
* neighbors even when we insert or paste item .
*/
2005-07-13 07:21:28 +04:00
static inline int can_node_be_removed ( int mode , int lfree , int sfree , int rfree ,
struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct buffer_head * Sh = PATH_H_PBUFFER ( tb - > tb_path , h ) ;
int levbytes = tb - > insert_size [ h ] ;
struct item_head * ih ;
struct reiserfs_key * r_key = NULL ;
2014-04-23 18:00:35 +04:00
ih = item_head ( Sh , 0 ) ;
2005-07-13 07:21:28 +04:00
if ( tb - > CFR [ h ] )
2014-04-23 18:00:35 +04:00
r_key = internal_key ( tb - > CFR [ h ] , tb - > rkey [ h ] ) ;
2005-07-13 07:21:28 +04:00
if ( lfree + rfree + sfree < MAX_CHILD_SIZE ( Sh ) + levbytes
/* shifting may merge items which might save space */
-
( ( ! h
2014-04-23 18:00:42 +04:00
& & op_is_left_mergeable ( & ih - > ih_key , Sh - > b_size ) ) ? IH_SIZE : 0 )
2005-07-13 07:21:28 +04:00
-
( ( ! h & & r_key
& & op_is_left_mergeable ( r_key , Sh - > b_size ) ) ? IH_SIZE : 0 )
+ ( ( h ) ? KEY_SIZE : 0 ) ) {
/* node can not be removed */
2014-04-23 18:00:36 +04:00
if ( sfree > = levbytes ) {
/* new item fits into node S[h] without any shifting */
2005-07-13 07:21:28 +04:00
if ( ! h )
tb - > s0num =
B_NR_ITEMS ( Sh ) +
( ( mode = = M_INSERT ) ? 1 : 0 ) ;
set_parameters ( tb , h , 0 , 0 , 1 , NULL , - 1 , - 1 ) ;
return NO_BALANCING_NEEDED ;
}
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
PROC_INFO_INC ( tb - > tb_sb , can_node_be_removed [ h ] ) ;
return ! NO_BALANCING_NEEDED ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Check whether current node S [ h ] is balanced when increasing its size by
2005-04-17 02:20:36 +04:00
* Inserting or Pasting .
* Calculate parameters for balancing for current level h .
* Parameters :
* tb tree_balance structure ;
* h current level of the node ;
* inum item number in S [ h ] ;
* mode i - insert , p - paste ;
2009-03-30 22:02:44 +04:00
* Returns : 1 - schedule occurred ;
2005-04-17 02:20:36 +04:00
* 0 - balancing for higher levels needed ;
* - 1 - no balancing for higher levels needed ;
* - 2 - no disk space .
*/
/* ip means Inserting or Pasting */
2005-07-13 07:21:28 +04:00
static int ip_check_balance ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct virtual_node * vn = tb - > tb_vn ;
2014-04-23 18:00:36 +04:00
/*
* Number of bytes that must be inserted into ( value is negative
* if bytes are deleted ) buffer which contains node being balanced .
* The mnemonic is that the attempted change in node space used
* level is levbytes bytes .
*/
int levbytes ;
int ret ;
2005-07-13 07:21:28 +04:00
int lfree , sfree , rfree /* free space in L, S and R */ ;
2014-04-23 18:00:36 +04:00
/*
* nver is short for number of vertixes , and lnver is the number if
* we shift to the left , rnver is the number if we shift to the
* right , and lrnver is the number if we shift in both directions .
* The goal is to minimize first the number of vertixes , and second ,
* the number of vertixes whose contents are changed by shifting ,
* and third the number of uncached vertixes whose contents are
* changed by shifting and must be read from disk .
*/
2005-07-13 07:21:28 +04:00
int nver , lnver , rnver , lrnver ;
2014-04-23 18:00:36 +04:00
/*
* used at leaf level only , S0 = S [ 0 ] is the node being balanced ,
* sInum [ I = 0 , 1 , 2 ] is the number of items that will
* remain in node SI after balancing . S1 and S2 are new
* nodes that might be created .
*/
2005-07-13 07:21:28 +04:00
2014-04-23 18:00:36 +04:00
/*
* we perform 8 calls to get_num_ver ( ) . For each call we
* calculate five parameters . where 4 th parameter is s1bytes
* and 5 th - s2bytes
*
* s0num , s1num , s2num for 8 cases
* 0 , 1 - do not shift and do not shift but bottle
* 2 - shift only whole item to left
* 3 - shift to left and bottle as much as possible
* 4 , 5 - shift to right ( whole items and as much as possible
* 6 , 7 - shift to both directions ( whole items and as much as possible )
2005-07-13 07:21:28 +04:00
*/
2014-04-23 18:00:36 +04:00
short snum012 [ 40 ] = { 0 , } ;
2005-07-13 07:21:28 +04:00
/* Sh is the node whose balance is currently being checked */
struct buffer_head * Sh ;
Sh = PATH_H_PBUFFER ( tb - > tb_path , h ) ;
levbytes = tb - > insert_size [ h ] ;
/* Calculate balance parameters for creating new root. */
if ( ! Sh ) {
if ( ! h )
2009-03-30 22:02:25 +04:00
reiserfs_panic ( tb - > tb_sb , " vs-8210 " ,
" S[0] can not be 0 " ) ;
2009-03-30 22:02:50 +04:00
switch ( ret = get_empty_nodes ( tb , h ) ) {
2014-04-23 18:00:36 +04:00
/* no balancing for higher levels needed */
2005-07-13 07:21:28 +04:00
case CARRY_ON :
set_parameters ( tb , h , 0 , 0 , 1 , NULL , - 1 , - 1 ) ;
2014-04-23 18:00:36 +04:00
return NO_BALANCING_NEEDED ;
2005-07-13 07:21:28 +04:00
case NO_DISK_SPACE :
case REPEAT_SEARCH :
2009-03-30 22:02:50 +04:00
return ret ;
2005-07-13 07:21:28 +04:00
default :
2009-03-30 22:02:25 +04:00
reiserfs_panic ( tb - > tb_sb , " vs-8215 " , " incorrect "
" return value of get_empty_nodes " ) ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/* get parents of S[h] neighbors. */
ret = get_parents ( tb , h ) ;
if ( ret ! = CARRY_ON )
2009-03-30 22:02:50 +04:00
return ret ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
sfree = B_FREE_SPACE ( Sh ) ;
/* get free space of neighbors */
rfree = get_rfree ( tb , h ) ;
lfree = get_lfree ( tb , h ) ;
2014-04-23 18:00:36 +04:00
/* and new item fits into node S[h] without any shifting */
2005-07-13 07:21:28 +04:00
if ( can_node_be_removed ( vn - > vn_mode , lfree , sfree , rfree , tb , h ) = =
NO_BALANCING_NEEDED )
return NO_BALANCING_NEEDED ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
create_virtual_node ( tb , h ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:44 +04:00
/*
2014-04-23 18:00:36 +04:00
* determine maximal number of items we can shift to the left
* neighbor ( in tb structure ) and the maximal number of bytes
* that can flow to the left neighbor from the left most liquid
* item that cannot be shifted from S [ 0 ] entirely ( returned value )
2005-04-17 02:20:36 +04:00
*/
2005-07-13 07:21:28 +04:00
check_left ( tb , h , lfree ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/*
2014-04-23 18:00:36 +04:00
* determine maximal number of items we can shift to the right
* neighbor ( in tb structure ) and the maximal number of bytes
* that can flow to the right neighbor from the right most liquid
* item that cannot be shifted from S [ 0 ] entirely ( returned value )
2005-07-13 07:21:28 +04:00
*/
check_right ( tb , h , rfree ) ;
2014-04-23 18:00:36 +04:00
/*
* all contents of internal node S [ h ] can be moved into its
* neighbors , S [ h ] will be removed after balancing
*/
2005-07-13 07:21:28 +04:00
if ( h & & ( tb - > rnum [ h ] + tb - > lnum [ h ] > = vn - > vn_nr_item + 1 ) ) {
int to_r ;
2014-04-23 18:00:36 +04:00
/*
* Since we are working on internal nodes , and our internal
* nodes have fixed size entries , then we can balance by the
* number of items rather than the space they consume . In this
* routine we set the left node equal to the right node ,
* allowing a difference of less than or equal to 1 child
* pointer .
*/
2005-07-13 07:21:28 +04:00
to_r =
( ( MAX_NR_KEY ( Sh ) < < 1 ) + 2 - tb - > lnum [ h ] - tb - > rnum [ h ] +
vn - > vn_nr_item + 1 ) / 2 - ( MAX_NR_KEY ( Sh ) + 1 -
tb - > rnum [ h ] ) ;
set_parameters ( tb , h , vn - > vn_nr_item + 1 - to_r , to_r , 0 , NULL ,
- 1 , - 1 ) ;
return CARRY_ON ;
}
2014-04-23 18:00:36 +04:00
/*
* this checks balance condition , that any two neighboring nodes
* can not fit in one node
*/
2005-07-13 07:21:28 +04:00
RFALSE ( h & &
( tb - > lnum [ h ] > = vn - > vn_nr_item + 1 | |
tb - > rnum [ h ] > = vn - > vn_nr_item + 1 ) ,
" vs-8220: tree is not balanced on internal level " ) ;
RFALSE ( ! h & & ( ( tb - > lnum [ h ] > = vn - > vn_nr_item & & ( tb - > lbytes = = - 1 ) ) | |
( tb - > rnum [ h ] > = vn - > vn_nr_item & & ( tb - > rbytes = = - 1 ) ) ) ,
" vs-8225: tree is not balanced on leaf level " ) ;
2014-04-23 18:00:36 +04:00
/*
* all contents of S [ 0 ] can be moved into its neighbors
* S [ 0 ] will be removed after balancing .
*/
2005-07-13 07:21:28 +04:00
if ( ! h & & is_leaf_removable ( tb ) )
return CARRY_ON ;
2014-04-23 18:00:36 +04:00
/*
* why do we perform this check here rather than earlier ? ?
* Answer : we can win 1 node in some cases above . Moreover we
* checked it above , when we checked , that S [ 0 ] is not removable
* in principle
*/
/* new item fits into node S[h] without any shifting */
if ( sfree > = levbytes ) {
2005-07-13 07:21:28 +04:00
if ( ! h )
tb - > s0num = vn - > vn_nr_item ;
set_parameters ( tb , h , 0 , 0 , 1 , NULL , - 1 , - 1 ) ;
return NO_BALANCING_NEEDED ;
}
{
int lpar , rpar , nset , lset , rset , lrset ;
2014-04-23 18:00:36 +04:00
/* regular overflowing of the node */
2005-07-13 07:21:28 +04:00
2014-04-23 18:00:36 +04:00
/*
* get_num_ver works in 2 modes ( FLOW & NO_FLOW )
* lpar , rpar - number of items we can shift to left / right
* neighbor ( including splitting item )
* nset , lset , rset , lrset - shows , whether flowing items
* give better packing
2005-07-13 07:21:28 +04:00
*/
2005-04-17 02:20:36 +04:00
# define FLOW 1
2005-07-13 07:21:28 +04:00
# define NO_FLOW 0 /* do not any splitting */
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/* we choose one of the following */
2005-04-17 02:20:36 +04:00
# define NOTHING_SHIFT_NO_FLOW 0
# define NOTHING_SHIFT_FLOW 5
# define LEFT_SHIFT_NO_FLOW 10
# define LEFT_SHIFT_FLOW 15
# define RIGHT_SHIFT_NO_FLOW 20
# define RIGHT_SHIFT_FLOW 25
# define LR_SHIFT_NO_FLOW 30
# define LR_SHIFT_FLOW 35
2005-07-13 07:21:28 +04:00
lpar = tb - > lnum [ h ] ;
rpar = tb - > rnum [ h ] ;
2014-04-23 18:00:36 +04:00
/*
* calculate number of blocks S [ h ] must be split into when
* nothing is shifted to the neighbors , as well as number of
* items in each part of the split node ( s012 numbers ) ,
* and number of bytes ( s1bytes ) of the shared drop which
* flow to S1 if any
*/
2005-07-13 07:21:28 +04:00
nset = NOTHING_SHIFT_NO_FLOW ;
nver = get_num_ver ( vn - > vn_mode , tb , h ,
0 , - 1 , h ? vn - > vn_nr_item : 0 , - 1 ,
snum012 , NO_FLOW ) ;
if ( ! h ) {
int nver1 ;
2014-04-23 18:00:36 +04:00
/*
* note , that in this case we try to bottle
* between S [ 0 ] and S1 ( S1 - the first new node )
*/
2005-07-13 07:21:28 +04:00
nver1 = get_num_ver ( vn - > vn_mode , tb , h ,
0 , - 1 , 0 , - 1 ,
snum012 + NOTHING_SHIFT_FLOW , FLOW ) ;
if ( nver > nver1 )
nset = NOTHING_SHIFT_FLOW , nver = nver1 ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* calculate number of blocks S [ h ] must be split into when
* l_shift_num first items and l_shift_bytes of the right
* most liquid item to be shifted are shifted to the left
* neighbor , as well as number of items in each part of the
* splitted node ( s012 numbers ) , and number of bytes
* ( s1bytes ) of the shared drop which flow to S1 if any
2005-07-13 07:21:28 +04:00
*/
lset = LEFT_SHIFT_NO_FLOW ;
lnver = get_num_ver ( vn - > vn_mode , tb , h ,
lpar - ( ( h | | tb - > lbytes = = - 1 ) ? 0 : 1 ) ,
- 1 , h ? vn - > vn_nr_item : 0 , - 1 ,
snum012 + LEFT_SHIFT_NO_FLOW , NO_FLOW ) ;
if ( ! h ) {
int lnver1 ;
lnver1 = get_num_ver ( vn - > vn_mode , tb , h ,
lpar -
( ( tb - > lbytes ! = - 1 ) ? 1 : 0 ) ,
tb - > lbytes , 0 , - 1 ,
snum012 + LEFT_SHIFT_FLOW , FLOW ) ;
if ( lnver > lnver1 )
lset = LEFT_SHIFT_FLOW , lnver = lnver1 ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* calculate number of blocks S [ h ] must be split into when
* r_shift_num first items and r_shift_bytes of the left most
* liquid item to be shifted are shifted to the right neighbor ,
* as well as number of items in each part of the splitted
* node ( s012 numbers ) , and number of bytes ( s1bytes ) of the
* shared drop which flow to S1 if any
2005-07-13 07:21:28 +04:00
*/
rset = RIGHT_SHIFT_NO_FLOW ;
rnver = get_num_ver ( vn - > vn_mode , tb , h ,
0 , - 1 ,
h ? ( vn - > vn_nr_item - rpar ) : ( rpar -
( ( tb - >
rbytes ! =
- 1 ) ? 1 :
0 ) ) , - 1 ,
snum012 + RIGHT_SHIFT_NO_FLOW , NO_FLOW ) ;
if ( ! h ) {
int rnver1 ;
rnver1 = get_num_ver ( vn - > vn_mode , tb , h ,
0 , - 1 ,
( rpar -
( ( tb - > rbytes ! = - 1 ) ? 1 : 0 ) ) ,
tb - > rbytes ,
snum012 + RIGHT_SHIFT_FLOW , FLOW ) ;
if ( rnver > rnver1 )
rset = RIGHT_SHIFT_FLOW , rnver = rnver1 ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* calculate number of blocks S [ h ] must be split into when
* items are shifted in both directions , as well as number
* of items in each part of the splitted node ( s012 numbers ) ,
* and number of bytes ( s1bytes ) of the shared drop which
* flow to S1 if any
2005-07-13 07:21:28 +04:00
*/
lrset = LR_SHIFT_NO_FLOW ;
lrnver = get_num_ver ( vn - > vn_mode , tb , h ,
lpar - ( ( h | | tb - > lbytes = = - 1 ) ? 0 : 1 ) ,
- 1 ,
h ? ( vn - > vn_nr_item - rpar ) : ( rpar -
( ( tb - >
rbytes ! =
- 1 ) ? 1 :
0 ) ) , - 1 ,
snum012 + LR_SHIFT_NO_FLOW , NO_FLOW ) ;
if ( ! h ) {
int lrnver1 ;
lrnver1 = get_num_ver ( vn - > vn_mode , tb , h ,
lpar -
( ( tb - > lbytes ! = - 1 ) ? 1 : 0 ) ,
tb - > lbytes ,
( rpar -
( ( tb - > rbytes ! = - 1 ) ? 1 : 0 ) ) ,
tb - > rbytes ,
snum012 + LR_SHIFT_FLOW , FLOW ) ;
if ( lrnver > lrnver1 )
lrset = LR_SHIFT_FLOW , lrnver = lrnver1 ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* Our general shifting strategy is :
* 1 ) to minimized number of new nodes ;
* 2 ) to minimized number of neighbors involved in shifting ;
* 3 ) to minimized number of disk reads ;
*/
2005-07-13 07:21:28 +04:00
/* we can win TWO or ONE nodes by shifting in both directions */
if ( lrnver < lnver & & lrnver < rnver ) {
RFALSE ( h & &
( tb - > lnum [ h ] ! = 1 | |
tb - > rnum [ h ] ! = 1 | |
lrnver ! = 1 | | rnver ! = 2 | | lnver ! = 2
| | h ! = 1 ) , " vs-8230: bad h " ) ;
if ( lrset = = LR_SHIFT_FLOW )
set_parameters ( tb , h , tb - > lnum [ h ] , tb - > rnum [ h ] ,
lrnver , snum012 + lrset ,
tb - > lbytes , tb - > rbytes ) ;
else
set_parameters ( tb , h ,
tb - > lnum [ h ] -
( ( tb - > lbytes = = - 1 ) ? 0 : 1 ) ,
tb - > rnum [ h ] -
( ( tb - > rbytes = = - 1 ) ? 0 : 1 ) ,
lrnver , snum012 + lrset , - 1 , - 1 ) ;
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* if shifting doesn ' t lead to better packing
* then don ' t shift
*/
2005-07-13 07:21:28 +04:00
if ( nver = = lrnver ) {
set_parameters ( tb , h , 0 , 0 , nver , snum012 + nset , - 1 ,
- 1 ) ;
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* now we know that for better packing shifting in only one
* direction either to the left or to the right is required
*/
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* if shifting to the left is better than
* shifting to the right
*/
2005-07-13 07:21:28 +04:00
if ( lnver < rnver ) {
SET_PAR_SHIFT_LEFT ;
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* if shifting to the right is better than
* shifting to the left
*/
2005-07-13 07:21:28 +04:00
if ( lnver > rnver ) {
SET_PAR_SHIFT_RIGHT ;
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* now shifting in either direction gives the same number
* of nodes and we can make use of the cached neighbors
*/
2005-07-13 07:21:28 +04:00
if ( is_left_neighbor_in_cache ( tb , h ) ) {
SET_PAR_SHIFT_LEFT ;
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* shift to the right independently on whether the
* right neighbor in cache or not
*/
2005-07-13 07:21:28 +04:00
SET_PAR_SHIFT_RIGHT ;
return CARRY_ON ;
2005-04-17 02:20:36 +04:00
}
}
2014-04-23 18:00:36 +04:00
/*
* Check whether current node S [ h ] is balanced when Decreasing its size by
2005-04-17 02:20:36 +04:00
* Deleting or Cutting for INTERNAL node of S + tree .
* Calculate parameters for balancing for current level h .
* Parameters :
* tb tree_balance structure ;
* h current level of the node ;
* inum item number in S [ h ] ;
* mode i - insert , p - paste ;
2009-03-30 22:02:44 +04:00
* Returns : 1 - schedule occurred ;
2005-04-17 02:20:36 +04:00
* 0 - balancing for higher levels needed ;
* - 1 - no balancing for higher levels needed ;
* - 2 - no disk space .
*
* Note : Items of internal nodes have fixed size , so the balance condition for
* the internal part of S + tree is as for the B - trees .
*/
2005-07-13 07:21:28 +04:00
static int dc_check_balance_internal ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct virtual_node * vn = tb - > tb_vn ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* Sh is the node whose balance is currently being checked ,
* and Fh is its father .
*/
2005-07-13 07:21:28 +04:00
struct buffer_head * Sh , * Fh ;
2009-03-30 22:02:50 +04:00
int maxsize , ret ;
2005-07-13 07:21:28 +04:00
int lfree , rfree /* free space in L and R */ ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
Sh = PATH_H_PBUFFER ( tb - > tb_path , h ) ;
Fh = PATH_H_PPARENT ( tb - > tb_path , h ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
maxsize = MAX_CHILD_SIZE ( Sh ) ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* using tb - > insert_size [ h ] , which is negative in this case ,
* create_virtual_node calculates :
* new_nr_item = number of items node would have if operation is
* performed without balancing ( new_nr_item ) ;
*/
2005-07-13 07:21:28 +04:00
create_virtual_node ( tb , h ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( ! Fh ) { /* S[h] is the root. */
2014-04-23 18:00:36 +04:00
/* no balancing for higher levels needed */
2005-07-13 07:21:28 +04:00
if ( vn - > vn_nr_item > 0 ) {
set_parameters ( tb , h , 0 , 0 , 1 , NULL , - 1 , - 1 ) ;
2014-04-23 18:00:36 +04:00
return NO_BALANCING_NEEDED ;
2005-07-13 07:21:28 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* new_nr_item = = 0.
2005-07-13 07:21:28 +04:00
* Current root will be deleted resulting in
2014-04-23 18:00:36 +04:00
* decrementing the tree height .
*/
2005-07-13 07:21:28 +04:00
set_parameters ( tb , h , 0 , 0 , 0 , NULL , - 1 , - 1 ) ;
return CARRY_ON ;
}
2009-03-30 22:02:50 +04:00
if ( ( ret = get_parents ( tb , h ) ) ! = CARRY_ON )
return ret ;
2005-07-13 07:21:28 +04:00
/* get free space of neighbors */
rfree = get_rfree ( tb , h ) ;
lfree = get_lfree ( tb , h ) ;
/* determine maximal number of items we can fit into neighbors */
check_left ( tb , h , lfree ) ;
check_right ( tb , h , rfree ) ;
2014-04-23 18:00:36 +04:00
/*
* Balance condition for the internal node is valid .
* In this case we balance only if it leads to better packing .
*/
if ( vn - > vn_nr_item > = MIN_NR_KEY ( Sh ) ) {
/*
* Here we join S [ h ] with one of its neighbors ,
* which is impossible with greater values of new_nr_item .
*/
if ( vn - > vn_nr_item = = MIN_NR_KEY ( Sh ) ) {
/* All contents of S[h] can be moved to L[h]. */
2005-07-13 07:21:28 +04:00
if ( tb - > lnum [ h ] > = vn - > vn_nr_item + 1 ) {
int n ;
int order_L ;
order_L =
( ( n =
PATH_H_B_ITEM_ORDER ( tb - > tb_path ,
h ) ) = =
0 ) ? B_NR_ITEMS ( tb - > FL [ h ] ) : n - 1 ;
n = dc_size ( B_N_CHILD ( tb - > FL [ h ] , order_L ) ) /
( DC_SIZE + KEY_SIZE ) ;
set_parameters ( tb , h , - n - 1 , 0 , 0 , NULL , - 1 ,
- 1 ) ;
return CARRY_ON ;
}
2014-04-23 18:00:36 +04:00
/* All contents of S[h] can be moved to R[h]. */
2005-07-13 07:21:28 +04:00
if ( tb - > rnum [ h ] > = vn - > vn_nr_item + 1 ) {
int n ;
int order_R ;
order_R =
( ( n =
PATH_H_B_ITEM_ORDER ( tb - > tb_path ,
h ) ) = =
B_NR_ITEMS ( Fh ) ) ? 0 : n + 1 ;
n = dc_size ( B_N_CHILD ( tb - > FR [ h ] , order_R ) ) /
( DC_SIZE + KEY_SIZE ) ;
set_parameters ( tb , h , 0 , - n - 1 , 0 , NULL , - 1 ,
- 1 ) ;
return CARRY_ON ;
}
}
2014-04-23 18:00:36 +04:00
/*
* All contents of S [ h ] can be moved to the neighbors
* ( L [ h ] & R [ h ] ) .
*/
2005-07-13 07:21:28 +04:00
if ( tb - > rnum [ h ] + tb - > lnum [ h ] > = vn - > vn_nr_item + 1 ) {
int to_r ;
to_r =
( ( MAX_NR_KEY ( Sh ) < < 1 ) + 2 - tb - > lnum [ h ] -
tb - > rnum [ h ] + vn - > vn_nr_item + 1 ) / 2 -
( MAX_NR_KEY ( Sh ) + 1 - tb - > rnum [ h ] ) ;
set_parameters ( tb , h , vn - > vn_nr_item + 1 - to_r , to_r ,
0 , NULL , - 1 , - 1 ) ;
return CARRY_ON ;
}
/* Balancing does not lead to better packing. */
set_parameters ( tb , h , 0 , 0 , 1 , NULL , - 1 , - 1 ) ;
return NO_BALANCING_NEEDED ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
2014-04-23 18:00:36 +04:00
/*
* Current node contain insufficient number of items .
* Balancing is required .
*/
2005-07-13 07:21:28 +04:00
/* Check whether we can merge S[h] with left neighbor. */
if ( tb - > lnum [ h ] > = vn - > vn_nr_item + 1 )
if ( is_left_neighbor_in_cache ( tb , h )
| | tb - > rnum [ h ] < vn - > vn_nr_item + 1 | | ! tb - > FR [ h ] ) {
int n ;
int order_L ;
order_L =
( ( n =
PATH_H_B_ITEM_ORDER ( tb - > tb_path ,
h ) ) = =
0 ) ? B_NR_ITEMS ( tb - > FL [ h ] ) : n - 1 ;
n = dc_size ( B_N_CHILD ( tb - > FL [ h ] , order_L ) ) / ( DC_SIZE +
KEY_SIZE ) ;
set_parameters ( tb , h , - n - 1 , 0 , 0 , NULL , - 1 , - 1 ) ;
return CARRY_ON ;
}
/* Check whether we can merge S[h] with right neighbor. */
if ( tb - > rnum [ h ] > = vn - > vn_nr_item + 1 ) {
int n ;
int order_R ;
order_R =
( ( n =
PATH_H_B_ITEM_ORDER ( tb - > tb_path ,
h ) ) = = B_NR_ITEMS ( Fh ) ) ? 0 : ( n + 1 ) ;
n = dc_size ( B_N_CHILD ( tb - > FR [ h ] , order_R ) ) / ( DC_SIZE +
KEY_SIZE ) ;
set_parameters ( tb , h , 0 , - n - 1 , 0 , NULL , - 1 , - 1 ) ;
return CARRY_ON ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* All contents of S[h] can be moved to the neighbors (L[h] & R[h]). */
if ( tb - > rnum [ h ] + tb - > lnum [ h ] > = vn - > vn_nr_item + 1 ) {
int to_r ;
to_r =
( ( MAX_NR_KEY ( Sh ) < < 1 ) + 2 - tb - > lnum [ h ] - tb - > rnum [ h ] +
vn - > vn_nr_item + 1 ) / 2 - ( MAX_NR_KEY ( Sh ) + 1 -
tb - > rnum [ h ] ) ;
set_parameters ( tb , h , vn - > vn_nr_item + 1 - to_r , to_r , 0 , NULL ,
- 1 , - 1 ) ;
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* For internal nodes try to borrow item from a neighbor */
RFALSE ( ! tb - > FL [ h ] & & ! tb - > FR [ h ] , " vs-8235: trying to borrow for root " ) ;
/* Borrow one or two items from caching neighbor */
if ( is_left_neighbor_in_cache ( tb , h ) | | ! tb - > FR [ h ] ) {
int from_l ;
from_l =
( MAX_NR_KEY ( Sh ) + 1 - tb - > lnum [ h ] + vn - > vn_nr_item +
1 ) / 2 - ( vn - > vn_nr_item + 1 ) ;
set_parameters ( tb , h , - from_l , 0 , 1 , NULL , - 1 , - 1 ) ;
return CARRY_ON ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
set_parameters ( tb , h , 0 ,
- ( ( MAX_NR_KEY ( Sh ) + 1 - tb - > rnum [ h ] + vn - > vn_nr_item +
1 ) / 2 - ( vn - > vn_nr_item + 1 ) ) , 1 , NULL , - 1 , - 1 ) ;
2005-04-17 02:20:36 +04:00
return CARRY_ON ;
}
2014-04-23 18:00:36 +04:00
/*
* Check whether current node S [ h ] is balanced when Decreasing its size by
2005-04-17 02:20:36 +04:00
* Deleting or Truncating for LEAF node of S + tree .
* Calculate parameters for balancing for current level h .
* Parameters :
* tb tree_balance structure ;
* h current level of the node ;
* inum item number in S [ h ] ;
* mode i - insert , p - paste ;
2009-03-30 22:02:44 +04:00
* Returns : 1 - schedule occurred ;
2005-04-17 02:20:36 +04:00
* 0 - balancing for higher levels needed ;
* - 1 - no balancing for higher levels needed ;
* - 2 - no disk space .
*/
2005-07-13 07:21:28 +04:00
static int dc_check_balance_leaf ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct virtual_node * vn = tb - > tb_vn ;
2014-04-23 18:00:36 +04:00
/*
* Number of bytes that must be deleted from
* ( value is negative if bytes are deleted ) buffer which
* contains node being balanced . The mnemonic is that the
* attempted change in node space used level is levbytes bytes .
*/
2005-07-13 07:21:28 +04:00
int levbytes ;
2014-04-23 18:00:36 +04:00
2005-07-13 07:21:28 +04:00
/* the maximal item size */
2009-03-30 22:02:50 +04:00
int maxsize , ret ;
2014-04-23 18:00:36 +04:00
/*
* S0 is the node whose balance is currently being checked ,
* and F0 is its father .
*/
2005-07-13 07:21:28 +04:00
struct buffer_head * S0 , * F0 ;
int lfree , rfree /* free space in L and R */ ;
S0 = PATH_H_PBUFFER ( tb - > tb_path , 0 ) ;
F0 = PATH_H_PPARENT ( tb - > tb_path , 0 ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
levbytes = tb - > insert_size [ h ] ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
maxsize = MAX_CHILD_SIZE ( S0 ) ; /* maximal possible size of an item */
if ( ! F0 ) { /* S[0] is the root now. */
RFALSE ( - levbytes > = maxsize - B_FREE_SPACE ( S0 ) ,
" vs-8240: attempt to create empty buffer tree " ) ;
set_parameters ( tb , h , 0 , 0 , 1 , NULL , - 1 , - 1 ) ;
return NO_BALANCING_NEEDED ;
}
2009-03-30 22:02:50 +04:00
if ( ( ret = get_parents ( tb , h ) ) ! = CARRY_ON )
return ret ;
2005-07-13 07:21:28 +04:00
/* get free space of neighbors */
rfree = get_rfree ( tb , h ) ;
lfree = get_lfree ( tb , h ) ;
create_virtual_node ( tb , h ) ;
/* if 3 leaves can be merge to one, set parameters and return */
if ( are_leaves_removable ( tb , lfree , rfree ) )
return CARRY_ON ;
2014-04-23 18:00:36 +04:00
/*
* determine maximal number of items we can shift to the left / right
* neighbor and the maximal number of bytes that can flow to the
* left / right neighbor from the left / right most liquid item that
* cannot be shifted from S [ 0 ] entirely
2005-07-13 07:21:28 +04:00
*/
check_left ( tb , h , lfree ) ;
check_right ( tb , h , rfree ) ;
/* check whether we can merge S with left neighbor. */
if ( tb - > lnum [ 0 ] > = vn - > vn_nr_item & & tb - > lbytes = = - 1 )
if ( is_left_neighbor_in_cache ( tb , h ) | | ( ( tb - > rnum [ 0 ] - ( ( tb - > rbytes = = - 1 ) ? 0 : 1 ) ) < vn - > vn_nr_item ) | | /* S can not be merged with R */
! tb - > FR [ h ] ) {
RFALSE ( ! tb - > FL [ h ] ,
" vs-8245: dc_check_balance_leaf: FL[h] must exist " ) ;
/* set parameter to merge S[0] with its left neighbor */
set_parameters ( tb , h , - 1 , 0 , 0 , NULL , - 1 , - 1 ) ;
return CARRY_ON ;
}
/* check whether we can merge S[0] with right neighbor. */
if ( tb - > rnum [ 0 ] > = vn - > vn_nr_item & & tb - > rbytes = = - 1 ) {
set_parameters ( tb , h , 0 , - 1 , 0 , NULL , - 1 , - 1 ) ;
return CARRY_ON ;
}
2014-04-23 18:00:36 +04:00
/*
* All contents of S [ 0 ] can be moved to the neighbors ( L [ 0 ] & R [ 0 ] ) .
* Set parameters and return
*/
2005-07-13 07:21:28 +04:00
if ( is_leaf_removable ( tb ) )
return CARRY_ON ;
/* Balancing is not required. */
tb - > s0num = vn - > vn_nr_item ;
set_parameters ( tb , h , 0 , 0 , 1 , NULL , - 1 , - 1 ) ;
return NO_BALANCING_NEEDED ;
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* Check whether current node S [ h ] is balanced when Decreasing its size by
2005-04-17 02:20:36 +04:00
* Deleting or Cutting .
* Calculate parameters for balancing for current level h .
* Parameters :
* tb tree_balance structure ;
* h current level of the node ;
* inum item number in S [ h ] ;
* mode d - delete , c - cut .
2009-03-30 22:02:44 +04:00
* Returns : 1 - schedule occurred ;
2005-04-17 02:20:36 +04:00
* 0 - balancing for higher levels needed ;
* - 1 - no balancing for higher levels needed ;
* - 2 - no disk space .
*/
2005-07-13 07:21:28 +04:00
static int dc_check_balance ( struct tree_balance * tb , int h )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
RFALSE ( ! ( PATH_H_PBUFFER ( tb - > tb_path , h ) ) ,
" vs-8250: S is not initialized " ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( h )
return dc_check_balance_internal ( tb , h ) ;
else
return dc_check_balance_leaf ( tb , h ) ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Check whether current node S [ h ] is balanced .
2005-04-17 02:20:36 +04:00
* Calculate parameters for balancing for current level h .
* Parameters :
*
* tb tree_balance structure :
*
2014-04-23 18:00:36 +04:00
* tb is a large structure that must be read about in the header
* file at the same time as this procedure if the reader is
* to successfully understand this procedure
2005-04-17 02:20:36 +04:00
*
* h current level of the node ;
* inum item number in S [ h ] ;
* mode i - insert , p - paste , d - delete , c - cut .
2009-03-30 22:02:44 +04:00
* Returns : 1 - schedule occurred ;
2005-04-17 02:20:36 +04:00
* 0 - balancing for higher levels needed ;
* - 1 - no balancing for higher levels needed ;
* - 2 - no disk space .
*/
2005-07-13 07:21:28 +04:00
static int check_balance ( int mode ,
struct tree_balance * tb ,
int h ,
int inum ,
int pos_in_item ,
struct item_head * ins_ih , const void * data )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
struct virtual_node * vn ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
vn = tb - > tb_vn = ( struct virtual_node * ) ( tb - > vn_buf ) ;
vn - > vn_free_ptr = ( char * ) ( tb - > tb_vn + 1 ) ;
vn - > vn_mode = mode ;
vn - > vn_affected_item_num = inum ;
vn - > vn_pos_in_item = pos_in_item ;
vn - > vn_ins_ih = ins_ih ;
vn - > vn_data = data ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
RFALSE ( mode = = M_INSERT & & ! vn - > vn_ins_ih ,
" vs-8255: ins_ih can not be 0 in insert mode " ) ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/* Calculate balance parameters when size of node is increasing. */
2005-07-13 07:21:28 +04:00
if ( tb - > insert_size [ h ] > 0 )
return ip_check_balance ( tb , h ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* Calculate balance parameters when size of node is decreasing. */
return dc_check_balance ( tb , h ) ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
/* Check whether parent at the path is the really parent of the current node.*/
2009-03-30 22:02:50 +04:00
static int get_direct_parent ( struct tree_balance * tb , int h )
2005-07-13 07:21:28 +04:00
{
2009-03-30 22:02:46 +04:00
struct buffer_head * bh ;
2009-03-30 22:02:49 +04:00
struct treepath * path = tb - > tb_path ;
2009-03-30 22:02:50 +04:00
int position ,
path_offset = PATH_H_PATH_OFFSET ( tb - > tb_path , h ) ;
2005-07-13 07:21:28 +04:00
/* We are in the root or in the new root. */
2009-03-30 22:02:50 +04:00
if ( path_offset < = FIRST_PATH_ELEMENT_OFFSET ) {
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( path_offset < FIRST_PATH_ELEMENT_OFFSET - 1 ,
2005-07-13 07:21:28 +04:00
" PAP-8260: invalid offset in the path " ) ;
2009-03-30 22:02:49 +04:00
if ( PATH_OFFSET_PBUFFER ( path , FIRST_PATH_ELEMENT_OFFSET ) - >
2009-03-30 22:02:48 +04:00
b_blocknr = = SB_ROOT_BLOCK ( tb - > tb_sb ) ) {
2005-07-13 07:21:28 +04:00
/* Root is not changed. */
2009-03-30 22:02:50 +04:00
PATH_OFFSET_PBUFFER ( path , path_offset - 1 ) = NULL ;
PATH_OFFSET_POSITION ( path , path_offset - 1 ) = 0 ;
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
}
2014-04-23 18:00:36 +04:00
/* Root is changed and we must recalculate the path. */
return REPEAT_SEARCH ;
2005-07-13 07:21:28 +04:00
}
2014-04-23 18:00:36 +04:00
/* Parent in the path is not in the tree. */
2005-07-13 07:21:28 +04:00
if ( ! B_IS_IN_TREE
2009-03-30 22:02:50 +04:00
( bh = PATH_OFFSET_PBUFFER ( path , path_offset - 1 ) ) )
2014-04-23 18:00:36 +04:00
return REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:50 +04:00
if ( ( position =
2009-03-30 22:02:49 +04:00
PATH_OFFSET_POSITION ( path ,
2009-03-30 22:02:50 +04:00
path_offset - 1 ) ) > B_NR_ITEMS ( bh ) )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/* Parent in the path is not parent of the current node in the tree. */
2009-03-30 22:02:50 +04:00
if ( B_N_CHILD_NUM ( bh , position ) ! =
PATH_OFFSET_PBUFFER ( path , path_offset ) - > b_blocknr )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2009-03-30 22:02:46 +04:00
if ( buffer_locked ( bh ) ) {
2013-08-09 01:34:46 +04:00
int depth = reiserfs_write_unlock_nested ( tb - > tb_sb ) ;
2009-03-30 22:02:46 +04:00
__wait_on_buffer ( bh ) ;
2013-08-09 01:34:46 +04:00
reiserfs_write_lock_nested ( tb - > tb_sb , depth ) ;
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Parent in the path is unlocked and really parent
* of the current node .
*/
return CARRY_ON ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* Using lnum [ h ] and rnum [ h ] we should determine what neighbors
2009-03-30 22:02:50 +04:00
* of S [ h ] we
* need in order to balance S [ h ] , and get them if necessary .
2005-04-17 02:20:36 +04:00
* Returns : SCHEDULE_OCCURRED - schedule occurred while the function worked ;
* CARRY_ON - schedule didn ' t occur while the function worked ;
*/
2009-03-30 22:02:50 +04:00
static int get_neighbors ( struct tree_balance * tb , int h )
2005-07-13 07:21:28 +04:00
{
2009-03-30 22:02:50 +04:00
int child_position ,
path_offset = PATH_H_PATH_OFFSET ( tb - > tb_path , h + 1 ) ;
unsigned long son_number ;
2009-03-30 22:02:48 +04:00
struct super_block * sb = tb - > tb_sb ;
2009-03-30 22:02:46 +04:00
struct buffer_head * bh ;
2013-08-09 01:34:46 +04:00
int depth ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
PROC_INFO_INC ( sb , get_neighbors [ h ] ) ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
if ( tb - > lnum [ h ] ) {
/* We need left neighbor to balance S[h]. */
PROC_INFO_INC ( sb , need_l_neighbor [ h ] ) ;
bh = PATH_OFFSET_PBUFFER ( tb - > tb_path , path_offset ) ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( bh = = tb - > FL [ h ] & &
! PATH_OFFSET_POSITION ( tb - > tb_path , path_offset ) ,
2005-07-13 07:21:28 +04:00
" PAP-8270: invalid position in the parent " ) ;
2009-03-30 22:02:50 +04:00
child_position =
2009-03-30 22:02:46 +04:00
( bh = =
2009-03-30 22:02:50 +04:00
tb - > FL [ h ] ) ? tb - > lkey [ h ] : B_NR_ITEMS ( tb - >
FL [ h ] ) ;
son_number = B_N_CHILD_NUM ( tb - > FL [ h ] , child_position ) ;
2013-08-09 01:34:46 +04:00
depth = reiserfs_write_unlock_nested ( tb - > tb_sb ) ;
2009-03-30 22:02:50 +04:00
bh = sb_bread ( sb , son_number ) ;
2013-08-09 01:34:46 +04:00
reiserfs_write_lock_nested ( tb - > tb_sb , depth ) ;
2009-03-30 22:02:46 +04:00
if ( ! bh )
2005-07-13 07:21:28 +04:00
return IO_ERROR ;
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) ) {
2009-03-30 22:02:46 +04:00
brelse ( bh ) ;
2009-03-30 22:02:50 +04:00
PROC_INFO_INC ( sb , get_neighbors_restart [ h ] ) ;
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
}
2009-03-30 22:02:50 +04:00
RFALSE ( ! B_IS_IN_TREE ( tb - > FL [ h ] ) | |
child_position > B_NR_ITEMS ( tb - > FL [ h ] ) | |
B_N_CHILD_NUM ( tb - > FL [ h ] , child_position ) ! =
2009-03-30 22:02:46 +04:00
bh - > b_blocknr , " PAP-8275: invalid parent " ) ;
RFALSE ( ! B_IS_IN_TREE ( bh ) , " PAP-8280: invalid child " ) ;
2009-03-30 22:02:50 +04:00
RFALSE ( ! h & &
2009-03-30 22:02:46 +04:00
B_FREE_SPACE ( bh ) ! =
MAX_CHILD_SIZE ( bh ) -
2009-03-30 22:02:50 +04:00
dc_size ( B_N_CHILD ( tb - > FL [ 0 ] , child_position ) ) ,
2005-07-13 07:21:28 +04:00
" PAP-8290: invalid child size of left neighbor " ) ;
2009-03-30 22:02:50 +04:00
brelse ( tb - > L [ h ] ) ;
tb - > L [ h ] = bh ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
/* We need right neighbor to balance S[path_offset]. */
2014-04-23 18:00:36 +04:00
if ( tb - > rnum [ h ] ) {
2009-03-30 22:02:50 +04:00
PROC_INFO_INC ( sb , need_r_neighbor [ h ] ) ;
bh = PATH_OFFSET_PBUFFER ( tb - > tb_path , path_offset ) ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( bh = = tb - > FR [ h ] & &
2009-03-30 22:02:48 +04:00
PATH_OFFSET_POSITION ( tb - > tb_path ,
2009-03-30 22:02:50 +04:00
path_offset ) > =
2009-03-30 22:02:46 +04:00
B_NR_ITEMS ( bh ) ,
2005-07-13 07:21:28 +04:00
" PAP-8295: invalid position in the parent " ) ;
2009-03-30 22:02:50 +04:00
child_position =
( bh = = tb - > FR [ h ] ) ? tb - > rkey [ h ] + 1 : 0 ;
son_number = B_N_CHILD_NUM ( tb - > FR [ h ] , child_position ) ;
2013-08-09 01:34:46 +04:00
depth = reiserfs_write_unlock_nested ( tb - > tb_sb ) ;
2009-03-30 22:02:50 +04:00
bh = sb_bread ( sb , son_number ) ;
2013-08-09 01:34:46 +04:00
reiserfs_write_lock_nested ( tb - > tb_sb , depth ) ;
2009-03-30 22:02:46 +04:00
if ( ! bh )
2005-07-13 07:21:28 +04:00
return IO_ERROR ;
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) ) {
2009-03-30 22:02:46 +04:00
brelse ( bh ) ;
2009-03-30 22:02:50 +04:00
PROC_INFO_INC ( sb , get_neighbors_restart [ h ] ) ;
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
}
2009-03-30 22:02:50 +04:00
brelse ( tb - > R [ h ] ) ;
tb - > R [ h ] = bh ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( ! h
2009-03-30 22:02:46 +04:00
& & B_FREE_SPACE ( bh ) ! =
MAX_CHILD_SIZE ( bh ) -
2009-03-30 22:02:50 +04:00
dc_size ( B_N_CHILD ( tb - > FR [ 0 ] , child_position ) ) ,
2005-07-13 07:21:28 +04:00
" PAP-8300: invalid child size of right neighbor (%d != %d - %d) " ,
2009-03-30 22:02:46 +04:00
B_FREE_SPACE ( bh ) , MAX_CHILD_SIZE ( bh ) ,
2009-03-30 22:02:50 +04:00
dc_size ( B_N_CHILD ( tb - > FR [ 0 ] , child_position ) ) ) ;
2005-07-13 07:21:28 +04:00
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
static int get_virtual_node_size ( struct super_block * sb , struct buffer_head * bh )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
int max_num_of_items ;
int max_num_of_entries ;
unsigned long blocksize = sb - > s_blocksize ;
2005-04-17 02:20:36 +04:00
# define MIN_NAME_LEN 1
2005-07-13 07:21:28 +04:00
max_num_of_items = ( blocksize - BLKH_SIZE ) / ( IH_SIZE + MIN_ITEM_LEN ) ;
max_num_of_entries = ( blocksize - BLKH_SIZE - IH_SIZE ) /
( DEH_SIZE + MIN_NAME_LEN ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
return sizeof ( struct virtual_node ) +
max ( max_num_of_items * sizeof ( struct virtual_item ) ,
sizeof ( struct virtual_item ) + sizeof ( struct direntry_uarea ) +
( max_num_of_entries - 1 ) * sizeof ( __u16 ) ) ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* maybe we should fail balancing we are going to perform when kmalloc
* fails several times . But now it will loop until kmalloc gets
* required memory
*/
2005-07-13 07:21:28 +04:00
static int get_mem_for_virtual_node ( struct tree_balance * tb )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
int check_fs = 0 ;
int size ;
char * buf ;
size = get_virtual_node_size ( tb - > tb_sb , PATH_PLAST_BUFFER ( tb - > tb_path ) ) ;
2014-04-23 18:00:36 +04:00
/* we have to allocate more memory for virtual node */
2005-07-13 07:21:28 +04:00
if ( size > tb - > vn_buf_size ) {
if ( tb - > vn_buf ) {
/* free memory allocated before */
2006-02-01 14:06:43 +03:00
kfree ( tb - > vn_buf ) ;
2005-07-13 07:21:28 +04:00
/* this is not needed if kfree is atomic */
check_fs = 1 ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* virtual node requires now more memory */
tb - > vn_buf_size = size ;
/* get memory for virtual item */
2006-02-01 14:06:43 +03:00
buf = kmalloc ( size , GFP_ATOMIC | __GFP_NOWARN ) ;
2005-07-13 07:21:28 +04:00
if ( ! buf ) {
2014-04-23 18:00:36 +04:00
/*
* getting memory with GFP_KERNEL priority may involve
* balancing now ( due to indirect_to_direct conversion
* on dcache shrinking ) . So , release path and collected
* resources here
*/
2005-07-13 07:21:28 +04:00
free_buffers_in_tb ( tb ) ;
2006-02-01 14:06:43 +03:00
buf = kmalloc ( size , GFP_NOFS ) ;
2005-07-13 07:21:28 +04:00
if ( ! buf ) {
tb - > vn_buf_size = 0 ;
}
tb - > vn_buf = buf ;
schedule ( ) ;
return REPEAT_SEARCH ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
tb - > vn_buf = buf ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
if ( check_fs & & FILESYSTEM_CHANGED_TB ( tb ) )
return REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
2005-04-17 02:20:36 +04:00
}
# ifdef CONFIG_REISERFS_CHECK
2009-03-30 22:02:45 +04:00
static void tb_buffer_sanity_check ( struct super_block * sb ,
2009-03-30 22:02:46 +04:00
struct buffer_head * bh ,
2005-07-13 07:21:28 +04:00
const char * descr , int level )
2005-04-17 02:20:36 +04:00
{
2009-03-30 22:02:46 +04:00
if ( bh ) {
if ( atomic_read ( & ( bh - > b_count ) ) < = 0 )
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:45 +04:00
reiserfs_panic ( sb , " jmacd-1 " , " negative or zero "
2009-03-30 22:02:25 +04:00
" reference counter for buffer %s[%d] "
2009-03-30 22:02:46 +04:00
" (%b) " , descr , level , bh ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:46 +04:00
if ( ! buffer_uptodate ( bh ) )
2009-03-30 22:02:45 +04:00
reiserfs_panic ( sb , " jmacd-2 " , " buffer is not up "
2009-03-30 22:02:25 +04:00
" to date %s[%d] (%b) " ,
2009-03-30 22:02:46 +04:00
descr , level , bh ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:46 +04:00
if ( ! B_IS_IN_TREE ( bh ) )
2009-03-30 22:02:45 +04:00
reiserfs_panic ( sb , " jmacd-3 " , " buffer is not "
2009-03-30 22:02:25 +04:00
" in tree %s[%d] (%b) " ,
2009-03-30 22:02:46 +04:00
descr , level , bh ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:46 +04:00
if ( bh - > b_bdev ! = sb - > s_bdev )
2009-03-30 22:02:45 +04:00
reiserfs_panic ( sb , " jmacd-4 " , " buffer has wrong "
2009-03-30 22:02:25 +04:00
" device %s[%d] (%b) " ,
2009-03-30 22:02:46 +04:00
descr , level , bh ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:46 +04:00
if ( bh - > b_size ! = sb - > s_blocksize )
2009-03-30 22:02:45 +04:00
reiserfs_panic ( sb , " jmacd-5 " , " buffer has wrong "
2009-03-30 22:02:25 +04:00
" blocksize %s[%d] (%b) " ,
2009-03-30 22:02:46 +04:00
descr , level , bh ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:46 +04:00
if ( bh - > b_blocknr > SB_BLOCK_COUNT ( sb ) )
2009-03-30 22:02:45 +04:00
reiserfs_panic ( sb , " jmacd-6 " , " buffer block "
2009-03-30 22:02:25 +04:00
" number too high %s[%d] (%b) " ,
2009-03-30 22:02:46 +04:00
descr , level , bh ) ;
2005-07-13 07:21:28 +04:00
}
}
# else
2009-03-30 22:02:45 +04:00
static void tb_buffer_sanity_check ( struct super_block * sb ,
2009-03-30 22:02:46 +04:00
struct buffer_head * bh ,
2005-07-13 07:21:28 +04:00
const char * descr , int level )
{ ;
}
# endif
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
static int clear_all_dirty_bits ( struct super_block * s , struct buffer_head * bh )
{
return reiserfs_prepare_for_journal ( s , bh , 0 ) ;
}
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:48 +04:00
static int wait_tb_buffers_until_unlocked ( struct tree_balance * tb )
2005-07-13 07:21:28 +04:00
{
struct buffer_head * locked ;
# ifdef CONFIG_REISERFS_CHECK
int repeat_counter = 0 ;
# endif
int i ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
do {
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
locked = NULL ;
2009-03-30 22:02:48 +04:00
for ( i = tb - > tb_path - > path_length ;
2005-07-13 07:21:28 +04:00
! locked & & i > ILLEGAL_PATH_ELEMENT_OFFSET ; i - - ) {
2009-03-30 22:02:48 +04:00
if ( PATH_OFFSET_PBUFFER ( tb - > tb_path , i ) ) {
2014-04-23 18:00:36 +04:00
/*
* if I understand correctly , we can only
* be sure the last buffer in the path is
* in the tree - - clm
2005-07-13 07:21:28 +04:00
*/
# ifdef CONFIG_REISERFS_CHECK
2009-03-30 22:02:48 +04:00
if ( PATH_PLAST_BUFFER ( tb - > tb_path ) = =
PATH_OFFSET_PBUFFER ( tb - > tb_path , i ) )
tb_buffer_sanity_check ( tb - > tb_sb ,
2005-07-13 07:21:28 +04:00
PATH_OFFSET_PBUFFER
2009-03-30 22:02:48 +04:00
( tb - > tb_path ,
2005-07-13 07:21:28 +04:00
i ) , " S " ,
2009-03-30 22:02:48 +04:00
tb - > tb_path - >
2005-07-13 07:21:28 +04:00
path_length - i ) ;
# endif
2009-03-30 22:02:48 +04:00
if ( ! clear_all_dirty_bits ( tb - > tb_sb ,
2005-07-13 07:21:28 +04:00
PATH_OFFSET_PBUFFER
2009-03-30 22:02:48 +04:00
( tb - > tb_path ,
2005-07-13 07:21:28 +04:00
i ) ) ) {
locked =
2009-03-30 22:02:48 +04:00
PATH_OFFSET_PBUFFER ( tb - > tb_path ,
2005-07-13 07:21:28 +04:00
i ) ;
}
}
2005-04-17 02:20:36 +04:00
}
2009-03-30 22:02:48 +04:00
for ( i = 0 ; ! locked & & i < MAX_HEIGHT & & tb - > insert_size [ i ] ;
2005-07-13 07:21:28 +04:00
i + + ) {
2009-03-30 22:02:48 +04:00
if ( tb - > lnum [ i ] ) {
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:48 +04:00
if ( tb - > L [ i ] ) {
tb_buffer_sanity_check ( tb - > tb_sb ,
tb - > L [ i ] ,
2005-07-13 07:21:28 +04:00
" L " , i ) ;
if ( ! clear_all_dirty_bits
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > L [ i ] ) )
locked = tb - > L [ i ] ;
2005-07-13 07:21:28 +04:00
}
2009-03-30 22:02:48 +04:00
if ( ! locked & & tb - > FL [ i ] ) {
tb_buffer_sanity_check ( tb - > tb_sb ,
tb - > FL [ i ] ,
2005-07-13 07:21:28 +04:00
" FL " , i ) ;
if ( ! clear_all_dirty_bits
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > FL [ i ] ) )
locked = tb - > FL [ i ] ;
2005-07-13 07:21:28 +04:00
}
2009-03-30 22:02:48 +04:00
if ( ! locked & & tb - > CFL [ i ] ) {
tb_buffer_sanity_check ( tb - > tb_sb ,
tb - > CFL [ i ] ,
2005-07-13 07:21:28 +04:00
" CFL " , i ) ;
if ( ! clear_all_dirty_bits
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > CFL [ i ] ) )
locked = tb - > CFL [ i ] ;
2005-07-13 07:21:28 +04:00
}
}
2009-03-30 22:02:48 +04:00
if ( ! locked & & ( tb - > rnum [ i ] ) ) {
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:48 +04:00
if ( tb - > R [ i ] ) {
tb_buffer_sanity_check ( tb - > tb_sb ,
tb - > R [ i ] ,
2005-07-13 07:21:28 +04:00
" R " , i ) ;
if ( ! clear_all_dirty_bits
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > R [ i ] ) )
locked = tb - > R [ i ] ;
2005-07-13 07:21:28 +04:00
}
2009-03-30 22:02:48 +04:00
if ( ! locked & & tb - > FR [ i ] ) {
tb_buffer_sanity_check ( tb - > tb_sb ,
tb - > FR [ i ] ,
2005-07-13 07:21:28 +04:00
" FR " , i ) ;
if ( ! clear_all_dirty_bits
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > FR [ i ] ) )
locked = tb - > FR [ i ] ;
2005-07-13 07:21:28 +04:00
}
2009-03-30 22:02:48 +04:00
if ( ! locked & & tb - > CFR [ i ] ) {
tb_buffer_sanity_check ( tb - > tb_sb ,
tb - > CFR [ i ] ,
2005-07-13 07:21:28 +04:00
" CFR " , i ) ;
if ( ! clear_all_dirty_bits
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > CFR [ i ] ) )
locked = tb - > CFR [ i ] ;
2005-07-13 07:21:28 +04:00
}
}
}
2014-04-23 18:00:36 +04:00
/*
* as far as I can tell , this is not required . The FEB list
* seems to be full of newly allocated nodes , which will
* never be locked , dirty , or anything else .
* To be safe , I ' m putting in the checks and waits in .
* For the moment , they are needed to keep the code in
* journal . c from complaining about the buffer .
* That code is inside CONFIG_REISERFS_CHECK as well . - - clm
2005-07-13 07:21:28 +04:00
*/
for ( i = 0 ; ! locked & & i < MAX_FEB_SIZE ; i + + ) {
2009-03-30 22:02:48 +04:00
if ( tb - > FEB [ i ] ) {
2005-07-13 07:21:28 +04:00
if ( ! clear_all_dirty_bits
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > FEB [ i ] ) )
locked = tb - > FEB [ i ] ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
if ( locked ) {
2013-08-09 01:34:46 +04:00
int depth ;
2005-04-17 02:20:36 +04:00
# ifdef CONFIG_REISERFS_CHECK
2005-07-13 07:21:28 +04:00
repeat_counter + + ;
if ( ( repeat_counter % 10000 ) = = 0 ) {
2009-03-30 22:02:48 +04:00
reiserfs_warning ( tb - > tb_sb , " reiserfs-8200 " ,
2009-03-30 22:02:21 +04:00
" too many iterations waiting "
" for buffer to unlock "
2005-07-13 07:21:28 +04:00
" (%b) " , locked ) ;
/* Don't loop forever. Try to recover from possible error. */
2009-03-30 22:02:48 +04:00
return ( FILESYSTEM_CHANGED_TB ( tb ) ) ?
2005-07-13 07:21:28 +04:00
REPEAT_SEARCH : CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
# endif
2013-08-09 01:34:46 +04:00
depth = reiserfs_write_unlock_nested ( tb - > tb_sb ) ;
2005-07-13 07:21:28 +04:00
__wait_on_buffer ( locked ) ;
2013-08-09 01:34:46 +04:00
reiserfs_write_lock_nested ( tb - > tb_sb , depth ) ;
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
} while ( locked ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
return CARRY_ON ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:36 +04:00
/*
* Prepare for balancing , that is
2005-04-17 02:20:36 +04:00
* get all necessary parents , and neighbors ;
* analyze what and where should be moved ;
* get sufficient number of new nodes ;
* Balancing will start only after all resources will be collected at a time .
2009-03-30 22:02:44 +04:00
*
2005-04-17 02:20:36 +04:00
* When ported to SMP kernels , only at the last moment after all needed nodes
* are collected in cache , will the resources be locked using the usual
* textbook ordered lock acquisition algorithms . Note that ensuring that
2014-04-23 18:00:36 +04:00
* this code neither write locks what it does not need to write lock nor locks
* out of order will be a pain in the butt that could have been avoided .
* Grumble grumble . - Hans
2009-03-30 22:02:44 +04:00
*
2005-04-17 02:20:36 +04:00
* fix is meant in the sense of render unchanging
2009-03-30 22:02:44 +04:00
*
2014-04-23 18:00:36 +04:00
* Latency might be improved by first gathering a list of what buffers
* are needed and then getting as many of them in parallel as possible ? - Hans
2005-04-17 02:20:36 +04:00
*
* Parameters :
* op_mode i - insert , d - delete , c - cut ( truncate ) , p - paste ( append )
* tb tree_balance structure ;
* inum item number in S [ h ] ;
* pos_in_item - comment this if you can
2009-03-30 22:02:48 +04:00
* ins_ih item head of item being inserted
* data inserted item or data to be pasted
2005-04-17 02:20:36 +04:00
* Returns : 1 - schedule occurred while the function worked ;
* 0 - schedule didn ' t occur while the function worked ;
2009-03-30 22:02:44 +04:00
* - 1 - if no_disk_space
2005-04-17 02:20:36 +04:00
*/
2009-03-30 22:02:50 +04:00
int fix_nodes ( int op_mode , struct tree_balance * tb ,
2009-03-30 22:02:49 +04:00
struct item_head * ins_ih , const void * data )
2005-07-13 07:21:28 +04:00
{
2009-03-30 22:02:50 +04:00
int ret , h , item_num = PATH_LAST_POSITION ( tb - > tb_path ) ;
int pos_in_item ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* we set wait_tb_buffers_run when we have to restore any dirty
* bits cleared during wait_tb_buffers_run
2005-07-13 07:21:28 +04:00
*/
int wait_tb_buffers_run = 0 ;
2009-03-30 22:02:48 +04:00
struct buffer_head * tbS0 = PATH_PLAST_BUFFER ( tb - > tb_path ) ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:48 +04:00
+ + REISERFS_SB ( tb - > tb_sb ) - > s_fix_nodes ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
pos_in_item = tb - > tb_path - > pos_in_item ;
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:48 +04:00
tb - > fs_gen = get_generation ( tb - > tb_sb ) ;
2005-04-17 02:20:36 +04:00
2014-04-23 18:00:36 +04:00
/*
* we prepare and log the super here so it will already be in the
* transaction when do_balance needs to change it .
* This way do_balance won ' t have to schedule when trying to prepare
* the super for logging
2005-07-13 07:21:28 +04:00
*/
2009-03-30 22:02:48 +04:00
reiserfs_prepare_for_journal ( tb - > tb_sb ,
SB_BUFFER_WITH_SB ( tb - > tb_sb ) , 1 ) ;
2014-04-23 18:00:39 +04:00
journal_mark_dirty ( tb - > transaction_handle ,
2009-03-30 22:02:48 +04:00
SB_BUFFER_WITH_SB ( tb - > tb_sb ) ) ;
if ( FILESYSTEM_CHANGED_TB ( tb ) )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* if it possible in indirect_to_direct conversion */
2009-03-30 22:02:48 +04:00
if ( buffer_locked ( tbS0 ) ) {
2013-08-09 01:34:46 +04:00
int depth = reiserfs_write_unlock_nested ( tb - > tb_sb ) ;
2009-03-30 22:02:48 +04:00
__wait_on_buffer ( tbS0 ) ;
2013-08-09 01:34:46 +04:00
reiserfs_write_lock_nested ( tb - > tb_sb , depth ) ;
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) )
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
}
# ifdef CONFIG_REISERFS_CHECK
kill-the-bkl/reiserfs: move the concurrent tree accesses checks per superblock
When do_balance() balances the tree, a trick is performed to
provide the ability for other tree writers/readers to check whether
do_balance() is executing concurrently (requires CONFIG_REISERFS_CHECK).
This is done to protect concurrent accesses to the tree. The trick
is the following:
When do_balance is called, a unique global variable called cur_tb
takes a pointer to the current tree to be rebalanced.
Once do_balance finishes its work, cur_tb takes the NULL value.
Then, concurrent tree readers/writers just have to check the value
of cur_tb to ensure do_balance isn't executing concurrently.
If it is, then it proves that schedule() occured on do_balance(),
which then relaxed the bkl that protected the tree.
Now that the bkl has be turned into a mutex, this check is still
fine even though do_balance() becomes preemptible: the write lock
will not be automatically released on schedule(), so the tree is
still protected.
But this is only fine if we have a single reiserfs mountpoint.
Indeed, because the bkl is a global lock, it didn't allowed
concurrent executions between a tree reader/writer in a mount point
and a do_balance() on another tree from another mountpoint.
So assuming all these readers/writers weren't supposed to be
reentrant, the current check now sometimes detect false positives with
the current per-superblock mutex which allows this reentrancy.
This patch keeps the concurrent tree accesses check but moves it
per superblock, so that only trees from a same mount point are
checked to be not accessed concurrently.
[ Impact: fix spurious panic while running several reiserfs mount-points ]
Cc: Jeff Mahoney <jeffm@suse.com>
Cc: Chris Mason <chris.mason@oracle.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Alexander Beregalov <a.beregalov@gmail.com>
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
2009-05-16 21:10:38 +04:00
if ( REISERFS_SB ( tb - > tb_sb ) - > cur_tb ) {
2005-07-13 07:21:28 +04:00
print_cur_tb ( " fix_nodes " ) ;
2009-03-30 22:02:48 +04:00
reiserfs_panic ( tb - > tb_sb , " PAP-8305 " ,
2009-03-30 22:02:25 +04:00
" there is pending do_balance " ) ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:48 +04:00
if ( ! buffer_uptodate ( tbS0 ) | | ! B_IS_IN_TREE ( tbS0 ) )
reiserfs_panic ( tb - > tb_sb , " PAP-8320 " , " S[0] (%b %z) is "
2009-03-30 22:02:25 +04:00
" not uptodate at the beginning of fix_nodes "
" or not in tree (mode %c) " ,
2009-03-30 22:02:50 +04:00
tbS0 , tbS0 , op_mode ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* Check parameters. */
2009-03-30 22:02:50 +04:00
switch ( op_mode ) {
2005-07-13 07:21:28 +04:00
case M_INSERT :
2009-03-30 22:02:50 +04:00
if ( item_num < = 0 | | item_num > B_NR_ITEMS ( tbS0 ) )
2009-03-30 22:02:48 +04:00
reiserfs_panic ( tb - > tb_sb , " PAP-8330 " , " Incorrect "
2009-03-30 22:02:25 +04:00
" item number %d (in S0 - %d) in case "
2009-03-30 22:02:50 +04:00
" of insert " , item_num ,
2009-03-30 22:02:48 +04:00
B_NR_ITEMS ( tbS0 ) ) ;
2005-07-13 07:21:28 +04:00
break ;
case M_PASTE :
case M_DELETE :
case M_CUT :
2009-03-30 22:02:50 +04:00
if ( item_num < 0 | | item_num > = B_NR_ITEMS ( tbS0 ) ) {
2009-03-30 22:02:48 +04:00
print_block ( tbS0 , 0 , - 1 , - 1 ) ;
reiserfs_panic ( tb - > tb_sb , " PAP-8335 " , " Incorrect "
2009-03-30 22:02:25 +04:00
" item number(%d); mode = %c "
" insert_size = %d " ,
2009-03-30 22:02:50 +04:00
item_num , op_mode ,
2009-03-30 22:02:48 +04:00
tb - > insert_size [ 0 ] ) ;
2005-04-17 02:20:36 +04:00
}
break ;
2005-07-13 07:21:28 +04:00
default :
2009-03-30 22:02:48 +04:00
reiserfs_panic ( tb - > tb_sb , " PAP-8340 " , " Incorrect mode "
2009-03-30 22:02:25 +04:00
" of operation " ) ;
2005-04-17 02:20:36 +04:00
}
2005-07-13 07:21:28 +04:00
# endif
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:48 +04:00
if ( get_mem_for_virtual_node ( tb ) = = REPEAT_SEARCH )
2014-04-23 18:00:36 +04:00
/* FIXME: maybe -ENOMEM when tb->vn_buf == 0? Now just repeat */
2005-07-13 07:21:28 +04:00
return REPEAT_SEARCH ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:50 +04:00
/* Starting from the leaf level; for all levels h of the tree. */
for ( h = 0 ; h < MAX_HEIGHT & & tb - > insert_size [ h ] ; h + + ) {
ret = get_direct_parent ( tb , h ) ;
if ( ret ! = CARRY_ON )
2005-07-13 07:21:28 +04:00
goto repeat ;
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:50 +04:00
ret = check_balance ( op_mode , tb , h , item_num ,
pos_in_item , ins_ih , data ) ;
if ( ret ! = CARRY_ON ) {
if ( ret = = NO_BALANCING_NEEDED ) {
2005-07-13 07:21:28 +04:00
/* No balancing for higher levels needed. */
2009-03-30 22:02:50 +04:00
ret = get_neighbors ( tb , h ) ;
if ( ret ! = CARRY_ON )
2005-07-13 07:21:28 +04:00
goto repeat ;
2009-03-30 22:02:50 +04:00
if ( h ! = MAX_HEIGHT - 1 )
tb - > insert_size [ h + 1 ] = 0 ;
2014-04-23 18:00:36 +04:00
/*
* ok , analysis and resource gathering
* are complete
*/
2005-07-13 07:21:28 +04:00
break ;
}
goto repeat ;
}
2005-04-17 02:20:36 +04:00
2009-03-30 22:02:50 +04:00
ret = get_neighbors ( tb , h ) ;
if ( ret ! = CARRY_ON )
2005-07-13 07:21:28 +04:00
goto repeat ;
2014-04-23 18:00:36 +04:00
/*
* No disk space , or schedule occurred and analysis may be
* invalid and needs to be redone .
*/
2009-03-30 22:02:50 +04:00
ret = get_empty_nodes ( tb , h ) ;
if ( ret ! = CARRY_ON )
2009-03-30 22:02:48 +04:00
goto repeat ;
2005-07-13 07:21:28 +04:00
2014-04-23 18:00:36 +04:00
/*
* We have a positive insert size but no nodes exist on this
* level , this means that we are creating a new root .
*/
2009-03-30 22:02:50 +04:00
if ( ! PATH_H_PBUFFER ( tb - > tb_path , h ) ) {
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( tb - > blknum [ h ] ! = 1 ,
2005-07-13 07:21:28 +04:00
" PAP-8350: creating new empty root " ) ;
2009-03-30 22:02:50 +04:00
if ( h < MAX_HEIGHT - 1 )
tb - > insert_size [ h + 1 ] = 0 ;
} else if ( ! PATH_H_PBUFFER ( tb - > tb_path , h + 1 ) ) {
2014-04-23 18:00:36 +04:00
/*
* The tree needs to be grown , so this node S [ h ]
* which is the root node is split into two nodes ,
* and a new node ( S [ h + 1 ] ) will be created to
* become the root node .
*/
2009-03-30 22:02:50 +04:00
if ( tb - > blknum [ h ] > 1 ) {
2005-07-13 07:21:28 +04:00
2009-03-30 22:02:50 +04:00
RFALSE ( h = = MAX_HEIGHT - 1 ,
2005-07-13 07:21:28 +04:00
" PAP-8355: attempt to create too high of a tree " ) ;
2009-03-30 22:02:50 +04:00
tb - > insert_size [ h + 1 ] =
2005-07-13 07:21:28 +04:00
( DC_SIZE +
2009-03-30 22:02:50 +04:00
KEY_SIZE ) * ( tb - > blknum [ h ] - 1 ) +
2005-07-13 07:21:28 +04:00
DC_SIZE ;
2009-03-30 22:02:50 +04:00
} else if ( h < MAX_HEIGHT - 1 )
tb - > insert_size [ h + 1 ] = 0 ;
2005-07-13 07:21:28 +04:00
} else
2009-03-30 22:02:50 +04:00
tb - > insert_size [ h + 1 ] =
( DC_SIZE + KEY_SIZE ) * ( tb - > blknum [ h ] - 1 ) ;
2005-04-17 02:20:36 +04:00
}
2009-03-30 22:02:50 +04:00
ret = wait_tb_buffers_until_unlocked ( tb ) ;
if ( ret = = CARRY_ON ) {
2009-03-30 22:02:48 +04:00
if ( FILESYSTEM_CHANGED_TB ( tb ) ) {
2005-07-13 07:21:28 +04:00
wait_tb_buffers_run = 1 ;
2009-03-30 22:02:50 +04:00
ret = REPEAT_SEARCH ;
2005-07-13 07:21:28 +04:00
goto repeat ;
} else {
return CARRY_ON ;
}
2005-04-17 02:20:36 +04:00
} else {
2005-07-13 07:21:28 +04:00
wait_tb_buffers_run = 1 ;
goto repeat ;
2005-04-17 02:20:36 +04:00
}
2014-04-23 18:00:41 +04:00
repeat :
2014-04-23 18:00:36 +04:00
/*
* fix_nodes was unable to perform its calculation due to
* filesystem got changed under us , lack of free disk space or i / o
* failure . If the first is the case - the search will be
* repeated . For now - free all resources acquired so far except
* for the new allocated nodes
*/
2005-07-13 07:21:28 +04:00
{
int i ;
/* Release path buffers. */
if ( wait_tb_buffers_run ) {
2009-03-30 22:02:48 +04:00
pathrelse_and_restore ( tb - > tb_sb , tb - > tb_path ) ;
2005-07-13 07:21:28 +04:00
} else {
2009-03-30 22:02:48 +04:00
pathrelse ( tb - > tb_path ) ;
2005-07-13 07:21:28 +04:00
}
/* brelse all resources collected for balancing */
for ( i = 0 ; i < MAX_HEIGHT ; i + + ) {
if ( wait_tb_buffers_run ) {
2009-03-30 22:02:48 +04:00
reiserfs_restore_prepared_buffer ( tb - > tb_sb ,
tb - > L [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb ,
tb - > R [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb ,
tb - > FL [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb ,
tb - > FR [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb ,
tb - >
2005-07-13 07:21:28 +04:00
CFL [ i ] ) ;
2009-03-30 22:02:48 +04:00
reiserfs_restore_prepared_buffer ( tb - > tb_sb ,
tb - >
2005-07-13 07:21:28 +04:00
CFR [ i ] ) ;
}
2009-03-30 22:02:48 +04:00
brelse ( tb - > L [ i ] ) ;
brelse ( tb - > R [ i ] ) ;
brelse ( tb - > FL [ i ] ) ;
brelse ( tb - > FR [ i ] ) ;
brelse ( tb - > CFL [ i ] ) ;
brelse ( tb - > CFR [ i ] ) ;
tb - > L [ i ] = NULL ;
tb - > R [ i ] = NULL ;
tb - > FL [ i ] = NULL ;
tb - > FR [ i ] = NULL ;
tb - > CFL [ i ] = NULL ;
tb - > CFR [ i ] = NULL ;
2005-07-13 07:21:28 +04:00
}
if ( wait_tb_buffers_run ) {
for ( i = 0 ; i < MAX_FEB_SIZE ; i + + ) {
2009-03-30 22:02:48 +04:00
if ( tb - > FEB [ i ] )
2005-07-13 07:21:28 +04:00
reiserfs_restore_prepared_buffer
2009-03-30 22:02:48 +04:00
( tb - > tb_sb , tb - > FEB [ i ] ) ;
2005-07-13 07:21:28 +04:00
}
2005-04-17 02:20:36 +04:00
}
2009-03-30 22:02:50 +04:00
return ret ;
2005-04-17 02:20:36 +04:00
}
}
2005-07-13 07:21:28 +04:00
void unfix_nodes ( struct tree_balance * tb )
2005-04-17 02:20:36 +04:00
{
2005-07-13 07:21:28 +04:00
int i ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* Release path buffers. */
pathrelse_and_restore ( tb - > tb_sb , tb - > tb_path ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* brelse all resources collected for balancing */
for ( i = 0 ; i < MAX_HEIGHT ; i + + ) {
reiserfs_restore_prepared_buffer ( tb - > tb_sb , tb - > L [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb , tb - > R [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb , tb - > FL [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb , tb - > FR [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb , tb - > CFL [ i ] ) ;
reiserfs_restore_prepared_buffer ( tb - > tb_sb , tb - > CFR [ i ] ) ;
brelse ( tb - > L [ i ] ) ;
brelse ( tb - > R [ i ] ) ;
brelse ( tb - > FL [ i ] ) ;
brelse ( tb - > FR [ i ] ) ;
brelse ( tb - > CFL [ i ] ) ;
brelse ( tb - > CFR [ i ] ) ;
}
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
/* deal with list of allocated (used and unused) nodes */
for ( i = 0 ; i < MAX_FEB_SIZE ; i + + ) {
if ( tb - > FEB [ i ] ) {
b_blocknr_t blocknr = tb - > FEB [ i ] - > b_blocknr ;
2014-04-23 18:00:36 +04:00
/*
* de - allocated block which was not used by
* balancing and bforget about buffer for it
*/
2005-07-13 07:21:28 +04:00
brelse ( tb - > FEB [ i ] ) ;
reiserfs_free_block ( tb - > transaction_handle , NULL ,
blocknr , 0 ) ;
}
if ( tb - > used [ i ] ) {
/* release used as new nodes including a new root */
brelse ( tb - > used [ i ] ) ;
}
}
2005-04-17 02:20:36 +04:00
2006-02-01 14:06:43 +03:00
kfree ( tb - > vn_buf ) ;
2005-04-17 02:20:36 +04:00
2005-07-13 07:21:28 +04:00
}