@@ -3644,7 +3644,7 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
36443644
36453645static int can_overcommit (struct btrfs_root * root ,
36463646 struct btrfs_space_info * space_info , u64 bytes ,
3647- int flush )
3647+ enum btrfs_reserve_flush_enum flush )
36483648{
36493649 u64 profile = btrfs_get_alloc_profile (root , 0 );
36503650 u64 avail ;
@@ -3672,7 +3672,7 @@ static int can_overcommit(struct btrfs_root *root,
36723672 * 1/2th of the space. If we can flush, don't let us overcommit
36733673 * too much, let it overcommit up to 1/8 of the space.
36743674 */
3675- if (flush )
3675+ if (flush == BTRFS_RESERVE_FLUSH_ALL )
36763676 avail >>= 3 ;
36773677 else
36783678 avail >>= 1 ;
@@ -3696,6 +3696,7 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
36963696 long time_left ;
36973697 unsigned long nr_pages = (2 * 1024 * 1024 ) >> PAGE_CACHE_SHIFT ;
36983698 int loops = 0 ;
3699+ enum btrfs_reserve_flush_enum flush ;
36993700
37003701 trans = (struct btrfs_trans_handle * )current -> journal_info ;
37013702 block_rsv = & root -> fs_info -> delalloc_block_rsv ;
@@ -3723,8 +3724,12 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
37233724 wait_event (root -> fs_info -> async_submit_wait ,
37243725 !atomic_read (& root -> fs_info -> async_delalloc_pages ));
37253726
3727+ if (!trans )
3728+ flush = BTRFS_RESERVE_FLUSH_ALL ;
3729+ else
3730+ flush = BTRFS_RESERVE_NO_FLUSH ;
37263731 spin_lock (& space_info -> lock );
3727- if (can_overcommit (root , space_info , orig , ! trans )) {
3732+ if (can_overcommit (root , space_info , orig , flush )) {
37283733 spin_unlock (& space_info -> lock );
37293734 break ;
37303735 }
@@ -3882,7 +3887,8 @@ static int flush_space(struct btrfs_root *root,
38823887 */
38833888static int reserve_metadata_bytes (struct btrfs_root * root ,
38843889 struct btrfs_block_rsv * block_rsv ,
3885- u64 orig_bytes , int flush )
3890+ u64 orig_bytes ,
3891+ enum btrfs_reserve_flush_enum flush )
38863892{
38873893 struct btrfs_space_info * space_info = block_rsv -> space_info ;
38883894 u64 used ;
@@ -3895,10 +3901,11 @@ static int reserve_metadata_bytes(struct btrfs_root *root,
38953901 ret = 0 ;
38963902 spin_lock (& space_info -> lock );
38973903 /*
3898- * We only want to wait if somebody other than us is flushing and we are
3899- * actually alloed to flush.
3904+ * We only want to wait if somebody other than us is flushing and we
3905+ * are actually allowed to flush all things .
39003906 */
3901- while (flush && !flushing && space_info -> flush ) {
3907+ while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
3908+ space_info -> flush ) {
39023909 spin_unlock (& space_info -> lock );
39033910 /*
39043911 * If we have a trans handle we can't wait because the flusher
@@ -3964,23 +3971,40 @@ static int reserve_metadata_bytes(struct btrfs_root *root,
39643971 * Couldn't make our reservation, save our place so while we're trying
39653972 * to reclaim space we can actually use it instead of somebody else
39663973 * stealing it from us.
3974+ *
3975+ * We make the other tasks wait for the flush only when we can flush
3976+ * all things.
39673977 */
3968- if (ret && flush ) {
3978+ if (ret && flush == BTRFS_RESERVE_FLUSH_ALL ) {
39693979 flushing = true;
39703980 space_info -> flush = 1 ;
39713981 }
39723982
39733983 spin_unlock (& space_info -> lock );
39743984
3975- if (!ret || ! flush )
3985+ if (!ret || flush == BTRFS_RESERVE_NO_FLUSH )
39763986 goto out ;
39773987
39783988 ret = flush_space (root , space_info , num_bytes , orig_bytes ,
39793989 flush_state );
39803990 flush_state ++ ;
3991+
3992+ /*
3993+ * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
3994+ * would happen. So skip delalloc flush.
3995+ */
3996+ if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
3997+ (flush_state == FLUSH_DELALLOC ||
3998+ flush_state == FLUSH_DELALLOC_WAIT ))
3999+ flush_state = ALLOC_CHUNK ;
4000+
39814001 if (!ret )
39824002 goto again ;
3983- else if (flush_state <= COMMIT_TRANS )
4003+ else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4004+ flush_state < COMMIT_TRANS )
4005+ goto again ;
4006+ else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4007+ flush_state <= COMMIT_TRANS )
39844008 goto again ;
39854009
39864010out :
@@ -4131,9 +4155,9 @@ void btrfs_free_block_rsv(struct btrfs_root *root,
41314155 kfree (rsv );
41324156}
41334157
4134- static inline int __block_rsv_add (struct btrfs_root * root ,
4135- struct btrfs_block_rsv * block_rsv ,
4136- u64 num_bytes , int flush )
4158+ int btrfs_block_rsv_add (struct btrfs_root * root ,
4159+ struct btrfs_block_rsv * block_rsv , u64 num_bytes ,
4160+ enum btrfs_reserve_flush_enum flush )
41374161{
41384162 int ret ;
41394163
@@ -4149,20 +4173,6 @@ static inline int __block_rsv_add(struct btrfs_root *root,
41494173 return ret ;
41504174}
41514175
4152- int btrfs_block_rsv_add (struct btrfs_root * root ,
4153- struct btrfs_block_rsv * block_rsv ,
4154- u64 num_bytes )
4155- {
4156- return __block_rsv_add (root , block_rsv , num_bytes , 1 );
4157- }
4158-
4159- int btrfs_block_rsv_add_noflush (struct btrfs_root * root ,
4160- struct btrfs_block_rsv * block_rsv ,
4161- u64 num_bytes )
4162- {
4163- return __block_rsv_add (root , block_rsv , num_bytes , 0 );
4164- }
4165-
41664176int btrfs_block_rsv_check (struct btrfs_root * root ,
41674177 struct btrfs_block_rsv * block_rsv , int min_factor )
41684178{
@@ -4181,9 +4191,9 @@ int btrfs_block_rsv_check(struct btrfs_root *root,
41814191 return ret ;
41824192}
41834193
4184- static inline int __btrfs_block_rsv_refill (struct btrfs_root * root ,
4185- struct btrfs_block_rsv * block_rsv ,
4186- u64 min_reserved , int flush )
4194+ int btrfs_block_rsv_refill (struct btrfs_root * root ,
4195+ struct btrfs_block_rsv * block_rsv , u64 min_reserved ,
4196+ enum btrfs_reserve_flush_enum flush )
41874197{
41884198 u64 num_bytes = 0 ;
41894199 int ret = - ENOSPC ;
@@ -4211,20 +4221,6 @@ static inline int __btrfs_block_rsv_refill(struct btrfs_root *root,
42114221 return ret ;
42124222}
42134223
4214- int btrfs_block_rsv_refill (struct btrfs_root * root ,
4215- struct btrfs_block_rsv * block_rsv ,
4216- u64 min_reserved )
4217- {
4218- return __btrfs_block_rsv_refill (root , block_rsv , min_reserved , 1 );
4219- }
4220-
4221- int btrfs_block_rsv_refill_noflush (struct btrfs_root * root ,
4222- struct btrfs_block_rsv * block_rsv ,
4223- u64 min_reserved )
4224- {
4225- return __btrfs_block_rsv_refill (root , block_rsv , min_reserved , 0 );
4226- }
4227-
42284224int btrfs_block_rsv_migrate (struct btrfs_block_rsv * src_rsv ,
42294225 struct btrfs_block_rsv * dst_rsv ,
42304226 u64 num_bytes )
@@ -4515,14 +4511,15 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
45154511 u64 csum_bytes ;
45164512 unsigned nr_extents = 0 ;
45174513 int extra_reserve = 0 ;
4518- int flush = 1 ;
4514+ enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL ;
45194515 int ret ;
45204516
45214517 /* Need to be holding the i_mutex here if we aren't free space cache */
45224518 if (btrfs_is_free_space_inode (inode ))
4523- flush = 0 ;
4519+ flush = BTRFS_RESERVE_NO_FLUSH ;
45244520
4525- if (flush && btrfs_transaction_in_commit (root -> fs_info ))
4521+ if (flush != BTRFS_RESERVE_NO_FLUSH &&
4522+ btrfs_transaction_in_commit (root -> fs_info ))
45264523 schedule_timeout (1 );
45274524
45284525 mutex_lock (& BTRFS_I (inode )-> delalloc_mutex );
@@ -6252,7 +6249,8 @@ use_block_rsv(struct btrfs_trans_handle *trans,
62526249 block_rsv = get_block_rsv (trans , root );
62536250
62546251 if (block_rsv -> size == 0 ) {
6255- ret = reserve_metadata_bytes (root , block_rsv , blocksize , 0 );
6252+ ret = reserve_metadata_bytes (root , block_rsv , blocksize ,
6253+ BTRFS_RESERVE_NO_FLUSH );
62566254 /*
62576255 * If we couldn't reserve metadata bytes try and use some from
62586256 * the global reserve.
@@ -6279,7 +6277,8 @@ use_block_rsv(struct btrfs_trans_handle *trans,
62796277 printk (KERN_DEBUG "btrfs: block rsv returned %d\n" , ret );
62806278 WARN_ON (1 );
62816279 }
6282- ret = reserve_metadata_bytes (root , block_rsv , blocksize , 0 );
6280+ ret = reserve_metadata_bytes (root , block_rsv , blocksize ,
6281+ BTRFS_RESERVE_NO_FLUSH );
62836282 if (!ret ) {
62846283 return block_rsv ;
62856284 } else if (ret && block_rsv != global_rsv ) {
0 commit comments