@@ -1764,24 +1764,21 @@ static inline bool btrfs_should_reclaim(struct btrfs_fs_info *fs_info)
17641764
17651765static bool should_reclaim_block_group (struct btrfs_block_group * bg , u64 bytes_freed )
17661766{
1767- const struct btrfs_space_info * space_info = bg -> space_info ;
1768- const int reclaim_thresh = READ_ONCE ( space_info -> bg_reclaim_threshold );
1767+ const int thresh_pct = btrfs_calc_reclaim_threshold ( bg -> space_info ) ;
1768+ u64 thresh_bytes = mult_perc ( bg -> length , thresh_pct );
17691769 const u64 new_val = bg -> used ;
17701770 const u64 old_val = new_val + bytes_freed ;
1771- u64 thresh ;
17721771
1773- if (reclaim_thresh == 0 )
1772+ if (thresh_bytes == 0 )
17741773 return false;
17751774
1776- thresh = mult_perc (bg -> length , reclaim_thresh );
1777-
17781775 /*
17791776 * If we were below the threshold before don't reclaim, we are likely a
17801777 * brand new block group and we don't want to relocate new block groups.
17811778 */
1782- if (old_val < thresh )
1779+ if (old_val < thresh_bytes )
17831780 return false;
1784- if (new_val >= thresh )
1781+ if (new_val >= thresh_bytes )
17851782 return false;
17861783 return true;
17871784}
@@ -1828,6 +1825,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
18281825 list_sort (NULL , & fs_info -> reclaim_bgs , reclaim_bgs_cmp );
18291826 while (!list_empty (& fs_info -> reclaim_bgs )) {
18301827 u64 zone_unusable ;
1828+ u64 reclaimed ;
18311829 int ret = 0 ;
18321830
18331831 bg = list_first_entry (& fs_info -> reclaim_bgs ,
@@ -1841,6 +1839,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
18411839 /* Don't race with allocators so take the groups_sem */
18421840 down_write (& space_info -> groups_sem );
18431841
1842+ spin_lock (& space_info -> lock );
18441843 spin_lock (& bg -> lock );
18451844 if (bg -> reserved || bg -> pinned || bg -> ro ) {
18461845 /*
@@ -1850,6 +1849,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
18501849 * this block group.
18511850 */
18521851 spin_unlock (& bg -> lock );
1852+ spin_unlock (& space_info -> lock );
18531853 up_write (& space_info -> groups_sem );
18541854 goto next ;
18551855 }
@@ -1868,6 +1868,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
18681868 if (!btrfs_test_opt (fs_info , DISCARD_ASYNC ))
18691869 btrfs_mark_bg_unused (bg );
18701870 spin_unlock (& bg -> lock );
1871+ spin_unlock (& space_info -> lock );
18711872 up_write (& space_info -> groups_sem );
18721873 goto next ;
18731874
@@ -1884,10 +1885,12 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
18841885 */
18851886 if (!should_reclaim_block_group (bg , bg -> length )) {
18861887 spin_unlock (& bg -> lock );
1888+ spin_unlock (& space_info -> lock );
18871889 up_write (& space_info -> groups_sem );
18881890 goto next ;
18891891 }
18901892 spin_unlock (& bg -> lock );
1893+ spin_unlock (& space_info -> lock );
18911894
18921895 /*
18931896 * Get out fast, in case we're read-only or unmounting the
@@ -1920,15 +1923,26 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
19201923 div64_u64 (bg -> used * 100 , bg -> length ),
19211924 div64_u64 (zone_unusable * 100 , bg -> length ));
19221925 trace_btrfs_reclaim_block_group (bg );
1926+ reclaimed = bg -> used ;
19231927 ret = btrfs_relocate_chunk (fs_info , bg -> start );
19241928 if (ret ) {
19251929 btrfs_dec_block_group_ro (bg );
19261930 btrfs_err (fs_info , "error relocating chunk %llu" ,
19271931 bg -> start );
1932+ spin_lock (& space_info -> lock );
1933+ space_info -> reclaim_count ++ ;
1934+ if (READ_ONCE (space_info -> periodic_reclaim ))
1935+ space_info -> periodic_reclaim_ready = false;
1936+ spin_unlock (& space_info -> lock );
1937+ } else {
1938+ spin_lock (& space_info -> lock );
1939+ space_info -> reclaim_count ++ ;
1940+ space_info -> reclaim_bytes += reclaimed ;
1941+ spin_unlock (& space_info -> lock );
19281942 }
19291943
19301944next :
1931- if (ret )
1945+ if (ret && ! READ_ONCE ( space_info -> periodic_reclaim ) )
19321946 btrfs_mark_bg_to_reclaim (bg );
19331947 btrfs_put_block_group (bg );
19341948
@@ -1955,6 +1969,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
19551969
19561970void btrfs_reclaim_bgs (struct btrfs_fs_info * fs_info )
19571971{
1972+ btrfs_reclaim_sweep (fs_info );
19581973 spin_lock (& fs_info -> unused_bgs_lock );
19591974 if (!list_empty (& fs_info -> reclaim_bgs ))
19601975 queue_work (system_unbound_wq , & fs_info -> reclaim_bgs_work );
@@ -3653,9 +3668,12 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
36533668 old_val += num_bytes ;
36543669 cache -> used = old_val ;
36553670 cache -> reserved -= num_bytes ;
3671+ cache -> reclaim_mark = 0 ;
36563672 space_info -> bytes_reserved -= num_bytes ;
36573673 space_info -> bytes_used += num_bytes ;
36583674 space_info -> disk_used += num_bytes * factor ;
3675+ if (READ_ONCE (space_info -> periodic_reclaim ))
3676+ btrfs_space_info_update_reclaimable (space_info , - num_bytes );
36593677 spin_unlock (& cache -> lock );
36603678 spin_unlock (& space_info -> lock );
36613679 } else {
@@ -3665,8 +3683,10 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
36653683 btrfs_space_info_update_bytes_pinned (info , space_info , num_bytes );
36663684 space_info -> bytes_used -= num_bytes ;
36673685 space_info -> disk_used -= num_bytes * factor ;
3668-
3669- reclaim = should_reclaim_block_group (cache , num_bytes );
3686+ if (READ_ONCE (space_info -> periodic_reclaim ))
3687+ btrfs_space_info_update_reclaimable (space_info , num_bytes );
3688+ else
3689+ reclaim = should_reclaim_block_group (cache , num_bytes );
36703690
36713691 spin_unlock (& cache -> lock );
36723692 spin_unlock (& space_info -> lock );
0 commit comments