@@ -749,24 +749,24 @@ ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
749749 * to find a free region that is of my size and has not
750750 * been reserved.
751751 *
752- * on succeed, it returns the reservation window to be appended to.
753- * failed, return NULL.
754752 */
755- static struct ext3_reserve_window_node * find_next_reservable_window (
753+ static int find_next_reservable_window (
756754 struct ext3_reserve_window_node * search_head ,
757- unsigned long size , int * start_block ,
755+ struct ext3_reserve_window_node * my_rsv ,
756+ struct super_block * sb , int start_block ,
758757 int last_block )
759758{
760759 struct rb_node * next ;
761760 struct ext3_reserve_window_node * rsv , * prev ;
762761 int cur ;
762+ int size = my_rsv -> rsv_goal_size ;
763763
764764 /* TODO: make the start of the reservation window byte-aligned */
765765 /* cur = *start_block & ~7;*/
766- cur = * start_block ;
766+ cur = start_block ;
767767 rsv = search_head ;
768768 if (!rsv )
769- return NULL ;
769+ return -1 ;
770770
771771 while (1 ) {
772772 if (cur <= rsv -> rsv_end )
@@ -782,11 +782,11 @@ static struct ext3_reserve_window_node *find_next_reservable_window(
782782 * space with expected-size (or more)...
783783 */
784784 if (cur > last_block )
785- return NULL ; /* fail */
785+ return -1 ; /* fail */
786786
787787 prev = rsv ;
788788 next = rb_next (& rsv -> rsv_node );
789- rsv = list_entry (next , struct ext3_reserve_window_node , rsv_node );
789+ rsv = list_entry (next ,struct ext3_reserve_window_node ,rsv_node );
790790
791791 /*
792792 * Reached the last reservation, we can just append to the
@@ -813,8 +813,25 @@ static struct ext3_reserve_window_node *find_next_reservable_window(
813813 * return the reservation window that we could append to.
814814 * succeed.
815815 */
816- * start_block = cur ;
817- return prev ;
816+
817+ if ((prev != my_rsv ) && (!rsv_is_empty (& my_rsv -> rsv_window )))
818+ rsv_window_remove (sb , my_rsv );
819+
820+ /*
821+ * Let's book the whole avaliable window for now. We will check the
822+ * disk bitmap later and then, if there are free blocks then we adjust
823+ * the window size if it's larger than requested.
824+ * Otherwise, we will remove this node from the tree next time
825+ * call find_next_reservable_window.
826+ */
827+ my_rsv -> rsv_start = cur ;
828+ my_rsv -> rsv_end = cur + size - 1 ;
829+ my_rsv -> rsv_alloc_hit = 0 ;
830+
831+ if (prev != my_rsv )
832+ ext3_rsv_window_add (sb , my_rsv );
833+
834+ return 0 ;
818835}
819836
820837/**
@@ -852,6 +869,7 @@ static struct ext3_reserve_window_node *find_next_reservable_window(
852869 * @sb: the super block
853870 * @group: the group we are trying to allocate in
854871 * @bitmap_bh: the block group block bitmap
872+ *
855873 */
856874static int alloc_new_reservation (struct ext3_reserve_window_node * my_rsv ,
857875 int goal , struct super_block * sb ,
@@ -860,10 +878,10 @@ static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
860878 struct ext3_reserve_window_node * search_head ;
861879 int group_first_block , group_end_block , start_block ;
862880 int first_free_block ;
863- int reservable_space_start ;
864- struct ext3_reserve_window_node * prev_rsv ;
865881 struct rb_root * fs_rsv_root = & EXT3_SB (sb )-> s_rsv_window_root ;
866882 unsigned long size ;
883+ int ret ;
884+ spinlock_t * rsv_lock = & EXT3_SB (sb )-> s_rsv_window_lock ;
867885
868886 group_first_block = le32_to_cpu (EXT3_SB (sb )-> s_es -> s_first_data_block ) +
869887 group * EXT3_BLOCKS_PER_GROUP (sb );
@@ -875,6 +893,7 @@ static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
875893 start_block = goal + group_first_block ;
876894
877895 size = my_rsv -> rsv_goal_size ;
896+
878897 if (!rsv_is_empty (& my_rsv -> rsv_window )) {
879898 /*
880899 * if the old reservation is cross group boundary
@@ -908,6 +927,8 @@ static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
908927 my_rsv -> rsv_goal_size = size ;
909928 }
910929 }
930+
931+ spin_lock (rsv_lock );
911932 /*
912933 * shift the search start to the window near the goal block
913934 */
@@ -921,11 +942,16 @@ static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
921942 * need to check the bitmap after we found a reservable window.
922943 */
923944retry :
924- prev_rsv = find_next_reservable_window (search_head , size ,
925- & start_block , group_end_block );
926- if (prev_rsv == NULL )
927- goto failed ;
928- reservable_space_start = start_block ;
945+ ret = find_next_reservable_window (search_head , my_rsv , sb ,
946+ start_block , group_end_block );
947+
948+ if (ret == -1 ) {
949+ if (!rsv_is_empty (& my_rsv -> rsv_window ))
950+ rsv_window_remove (sb , my_rsv );
951+ spin_unlock (rsv_lock );
952+ return -1 ;
953+ }
954+
929955 /*
930956 * On success, find_next_reservable_window() returns the
931957 * reservation window where there is a reservable space after it.
@@ -937,63 +963,39 @@ static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
937963 * block. Search start from the start block of the reservable space
938964 * we just found.
939965 */
966+ spin_unlock (rsv_lock );
940967 first_free_block = bitmap_search_next_usable_block (
941- reservable_space_start - group_first_block ,
968+ my_rsv -> rsv_start - group_first_block ,
942969 bitmap_bh , group_end_block - group_first_block + 1 );
943970
944971 if (first_free_block < 0 ) {
945972 /*
946973 * no free block left on the bitmap, no point
947974 * to reserve the space. return failed.
948975 */
949- goto failed ;
976+ spin_lock (rsv_lock );
977+ if (!rsv_is_empty (& my_rsv -> rsv_window ))
978+ rsv_window_remove (sb , my_rsv );
979+ spin_unlock (rsv_lock );
980+ return -1 ; /* failed */
950981 }
982+
951983 start_block = first_free_block + group_first_block ;
952984 /*
953985 * check if the first free block is within the
954- * free space we just found
986+ * free space we just reserved
955987 */
956- if ((start_block >= reservable_space_start ) &&
957- (start_block < reservable_space_start + size ))
958- goto found_rsv_window ;
988+ if (start_block >= my_rsv -> rsv_start && start_block < my_rsv -> rsv_end )
989+ return 0 ; /* success */
959990 /*
960991 * if the first free bit we found is out of the reservable space
961- * this means there is no free block on the reservable space
962- * we should continue search for next reservable space,
992+ * continue search for next reservable space,
963993 * start from where the free block is,
964994 * we also shift the list head to where we stopped last time
965995 */
966- search_head = prev_rsv ;
996+ search_head = my_rsv ;
997+ spin_lock (rsv_lock );
967998 goto retry ;
968-
969- found_rsv_window :
970- /*
971- * great! the reservable space contains some free blocks.
972- * if the search returns that we should add the new
973- * window just next to where the old window, we don't
974- * need to remove the old window first then add it to the
975- * same place, just update the new start and new end.
976- */
977- if (my_rsv != prev_rsv ) {
978- if (!rsv_is_empty (& my_rsv -> rsv_window ))
979- rsv_window_remove (sb , my_rsv );
980- }
981- my_rsv -> rsv_start = reservable_space_start ;
982- my_rsv -> rsv_end = my_rsv -> rsv_start + size - 1 ;
983- my_rsv -> rsv_alloc_hit = 0 ;
984- if (my_rsv != prev_rsv ) {
985- ext3_rsv_window_add (sb , my_rsv );
986- }
987- return 0 ; /* succeed */
988- failed :
989- /*
990- * failed to find a new reservation window in the current
991- * group, remove the current(stale) reservation window
992- * if there is any
993- */
994- if (!rsv_is_empty (& my_rsv -> rsv_window ))
995- rsv_window_remove (sb , my_rsv );
996- return -1 ; /* failed */
997999}
9981000
9991001/*
@@ -1023,7 +1025,6 @@ ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
10231025 int goal , struct ext3_reserve_window_node * my_rsv ,
10241026 int * errp )
10251027{
1026- spinlock_t * rsv_lock ;
10271028 unsigned long group_first_block ;
10281029 int ret = 0 ;
10291030 int fatal ;
@@ -1052,7 +1053,6 @@ ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
10521053 ret = ext3_try_to_allocate (sb , handle , group , bitmap_bh , goal , NULL );
10531054 goto out ;
10541055 }
1055- rsv_lock = & EXT3_SB (sb )-> s_rsv_window_lock ;
10561056 /*
10571057 * goal is a group relative block number (if there is a goal)
10581058 * 0 < goal < EXT3_BLOCKS_PER_GROUP(sb)
@@ -1078,30 +1078,21 @@ ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
10781078 * then we could go to allocate from the reservation window directly.
10791079 */
10801080 while (1 ) {
1081- struct ext3_reserve_window rsv_copy ;
1082-
1083- rsv_copy ._rsv_start = my_rsv -> rsv_start ;
1084- rsv_copy ._rsv_end = my_rsv -> rsv_end ;
1085-
1086- if (rsv_is_empty (& rsv_copy ) || (ret < 0 ) ||
1087- !goal_in_my_reservation (& rsv_copy , goal , group , sb )) {
1088- spin_lock (rsv_lock );
1081+ if (rsv_is_empty (& my_rsv -> rsv_window ) || (ret < 0 ) ||
1082+ !goal_in_my_reservation (& my_rsv -> rsv_window , goal , group , sb )) {
10891083 ret = alloc_new_reservation (my_rsv , goal , sb ,
10901084 group , bitmap_bh );
1091- rsv_copy ._rsv_start = my_rsv -> rsv_start ;
1092- rsv_copy ._rsv_end = my_rsv -> rsv_end ;
1093- spin_unlock (rsv_lock );
10941085 if (ret < 0 )
10951086 break ; /* failed */
10961087
1097- if (!goal_in_my_reservation (& rsv_copy , goal , group , sb ))
1088+ if (!goal_in_my_reservation (& my_rsv -> rsv_window , goal , group , sb ))
10981089 goal = -1 ;
10991090 }
1100- if ((rsv_copy . _rsv_start >= group_first_block + EXT3_BLOCKS_PER_GROUP (sb ))
1101- || (rsv_copy . _rsv_end < group_first_block ))
1091+ if ((my_rsv -> rsv_start >= group_first_block + EXT3_BLOCKS_PER_GROUP (sb ))
1092+ || (my_rsv -> rsv_end < group_first_block ))
11021093 BUG ();
11031094 ret = ext3_try_to_allocate (sb , handle , group , bitmap_bh , goal ,
1104- & rsv_copy );
1095+ & my_rsv -> rsv_window );
11051096 if (ret >= 0 ) {
11061097 my_rsv -> rsv_alloc_hit ++ ;
11071098 break ; /* succeed */
0 commit comments