diff options
author | Yan, Zheng <zheng.yan@oracle.com> | 2010-05-16 10:46:25 -0400 |
---|---|---|
committer | Chris Mason <chris.mason@oracle.com> | 2010-05-25 10:34:48 -0400 |
commit | 5da9d01b66458b180a6bee0e637a1d0a3effc622 (patch) | |
tree | 47eca61c0ad07ddc791cb7677c548d663fbac818 /fs/btrfs/extent-tree.c | |
parent | 424499dbd0c4d88742bf581b5714b27fb44b9fef (diff) |
Btrfs: Shrink delay allocated space in a synchronized
Shrink delayed allocation space in a synchronized manner is more
controllable than flushing all delay allocated space in an async
thread.
Signed-off-by: Yan Zheng <zheng.yan@oracle.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r-- | fs/btrfs/extent-tree.c | 165 |
1 files changed, 55 insertions, 110 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 2c95507c9abb..f32b1618ee6d 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -74,6 +74,9 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes, | |||
74 | static int maybe_allocate_chunk(struct btrfs_trans_handle *trans, | 74 | static int maybe_allocate_chunk(struct btrfs_trans_handle *trans, |
75 | struct btrfs_root *root, | 75 | struct btrfs_root *root, |
76 | struct btrfs_space_info *sinfo, u64 num_bytes); | 76 | struct btrfs_space_info *sinfo, u64 num_bytes); |
77 | static int shrink_delalloc(struct btrfs_trans_handle *trans, | ||
78 | struct btrfs_root *root, | ||
79 | struct btrfs_space_info *sinfo, u64 to_reclaim); | ||
77 | 80 | ||
78 | static noinline int | 81 | static noinline int |
79 | block_group_cache_done(struct btrfs_block_group_cache *cache) | 82 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
@@ -2693,7 +2696,6 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags, | |||
2693 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) | 2696 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
2694 | INIT_LIST_HEAD(&found->block_groups[i]); | 2697 | INIT_LIST_HEAD(&found->block_groups[i]); |
2695 | init_rwsem(&found->groups_sem); | 2698 | init_rwsem(&found->groups_sem); |
2696 | init_waitqueue_head(&found->flush_wait); | ||
2697 | spin_lock_init(&found->lock); | 2699 | spin_lock_init(&found->lock); |
2698 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | | 2700 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
2699 | BTRFS_BLOCK_GROUP_SYSTEM | | 2701 | BTRFS_BLOCK_GROUP_SYSTEM | |
@@ -2907,105 +2909,6 @@ static void check_force_delalloc(struct btrfs_space_info *meta_sinfo) | |||
2907 | meta_sinfo->force_delalloc = 0; | 2909 | meta_sinfo->force_delalloc = 0; |
2908 | } | 2910 | } |
2909 | 2911 | ||
2910 | struct async_flush { | ||
2911 | struct btrfs_root *root; | ||
2912 | struct btrfs_space_info *info; | ||
2913 | struct btrfs_work work; | ||
2914 | }; | ||
2915 | |||
2916 | static noinline void flush_delalloc_async(struct btrfs_work *work) | ||
2917 | { | ||
2918 | struct async_flush *async; | ||
2919 | struct btrfs_root *root; | ||
2920 | struct btrfs_space_info *info; | ||
2921 | |||
2922 | async = container_of(work, struct async_flush, work); | ||
2923 | root = async->root; | ||
2924 | info = async->info; | ||
2925 | |||
2926 | btrfs_start_delalloc_inodes(root, 0); | ||
2927 | wake_up(&info->flush_wait); | ||
2928 | btrfs_wait_ordered_extents(root, 0, 0); | ||
2929 | |||
2930 | spin_lock(&info->lock); | ||
2931 | info->flushing = 0; | ||
2932 | spin_unlock(&info->lock); | ||
2933 | wake_up(&info->flush_wait); | ||
2934 | |||
2935 | kfree(async); | ||
2936 | } | ||
2937 | |||
2938 | static void wait_on_flush(struct btrfs_space_info *info) | ||
2939 | { | ||
2940 | DEFINE_WAIT(wait); | ||
2941 | u64 used; | ||
2942 | |||
2943 | while (1) { | ||
2944 | prepare_to_wait(&info->flush_wait, &wait, | ||
2945 | TASK_UNINTERRUPTIBLE); | ||
2946 | spin_lock(&info->lock); | ||
2947 | if (!info->flushing) { | ||
2948 | spin_unlock(&info->lock); | ||
2949 | break; | ||
2950 | } | ||
2951 | |||
2952 | used = info->bytes_used + info->bytes_reserved + | ||
2953 | info->bytes_pinned + info->bytes_readonly + | ||
2954 | info->bytes_super + info->bytes_root + | ||
2955 | info->bytes_may_use + info->bytes_delalloc; | ||
2956 | if (used < info->total_bytes) { | ||
2957 | spin_unlock(&info->lock); | ||
2958 | break; | ||
2959 | } | ||
2960 | spin_unlock(&info->lock); | ||
2961 | schedule(); | ||
2962 | } | ||
2963 | finish_wait(&info->flush_wait, &wait); | ||
2964 | } | ||
2965 | |||
2966 | static void flush_delalloc(struct btrfs_root *root, | ||
2967 | struct btrfs_space_info *info) | ||
2968 | { | ||
2969 | struct async_flush *async; | ||
2970 | bool wait = false; | ||
2971 | |||
2972 | spin_lock(&info->lock); | ||
2973 | |||
2974 | if (!info->flushing) | ||
2975 | info->flushing = 1; | ||
2976 | else | ||
2977 | wait = true; | ||
2978 | |||
2979 | spin_unlock(&info->lock); | ||
2980 | |||
2981 | if (wait) { | ||
2982 | wait_on_flush(info); | ||
2983 | return; | ||
2984 | } | ||
2985 | |||
2986 | async = kzalloc(sizeof(*async), GFP_NOFS); | ||
2987 | if (!async) | ||
2988 | goto flush; | ||
2989 | |||
2990 | async->root = root; | ||
2991 | async->info = info; | ||
2992 | async->work.func = flush_delalloc_async; | ||
2993 | |||
2994 | btrfs_queue_worker(&root->fs_info->enospc_workers, | ||
2995 | &async->work); | ||
2996 | wait_on_flush(info); | ||
2997 | return; | ||
2998 | |||
2999 | flush: | ||
3000 | btrfs_start_delalloc_inodes(root, 0); | ||
3001 | btrfs_wait_ordered_extents(root, 0, 0); | ||
3002 | |||
3003 | spin_lock(&info->lock); | ||
3004 | info->flushing = 0; | ||
3005 | spin_unlock(&info->lock); | ||
3006 | wake_up(&info->flush_wait); | ||
3007 | } | ||
3008 | |||
3009 | /* | 2912 | /* |
3010 | * Reserve metadata space for delalloc. | 2913 | * Reserve metadata space for delalloc. |
3011 | */ | 2914 | */ |
@@ -3058,7 +2961,7 @@ again: | |||
3058 | filemap_flush(inode->i_mapping); | 2961 | filemap_flush(inode->i_mapping); |
3059 | goto again; | 2962 | goto again; |
3060 | } else if (flushed == 3) { | 2963 | } else if (flushed == 3) { |
3061 | flush_delalloc(root, meta_sinfo); | 2964 | shrink_delalloc(NULL, root, meta_sinfo, num_bytes); |
3062 | goto again; | 2965 | goto again; |
3063 | } | 2966 | } |
3064 | spin_lock(&meta_sinfo->lock); | 2967 | spin_lock(&meta_sinfo->lock); |
@@ -3171,7 +3074,7 @@ again: | |||
3171 | } | 3074 | } |
3172 | 3075 | ||
3173 | if (retries == 2) { | 3076 | if (retries == 2) { |
3174 | flush_delalloc(root, meta_sinfo); | 3077 | shrink_delalloc(NULL, root, meta_sinfo, num_bytes); |
3175 | goto again; | 3078 | goto again; |
3176 | } | 3079 | } |
3177 | spin_lock(&meta_sinfo->lock); | 3080 | spin_lock(&meta_sinfo->lock); |
@@ -3197,7 +3100,7 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, | |||
3197 | { | 3100 | { |
3198 | struct btrfs_space_info *data_sinfo; | 3101 | struct btrfs_space_info *data_sinfo; |
3199 | u64 used; | 3102 | u64 used; |
3200 | int ret = 0, committed = 0, flushed = 0; | 3103 | int ret = 0, committed = 0; |
3201 | 3104 | ||
3202 | /* make sure bytes are sectorsize aligned */ | 3105 | /* make sure bytes are sectorsize aligned */ |
3203 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); | 3106 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
@@ -3217,13 +3120,6 @@ again: | |||
3217 | if (used + bytes > data_sinfo->total_bytes) { | 3120 | if (used + bytes > data_sinfo->total_bytes) { |
3218 | struct btrfs_trans_handle *trans; | 3121 | struct btrfs_trans_handle *trans; |
3219 | 3122 | ||
3220 | if (!flushed) { | ||
3221 | spin_unlock(&data_sinfo->lock); | ||
3222 | flush_delalloc(root, data_sinfo); | ||
3223 | flushed = 1; | ||
3224 | goto again; | ||
3225 | } | ||
3226 | |||
3227 | /* | 3123 | /* |
3228 | * if we don't have enough free bytes in this space then we need | 3124 | * if we don't have enough free bytes in this space then we need |
3229 | * to alloc a new chunk. | 3125 | * to alloc a new chunk. |
@@ -3467,6 +3363,55 @@ static int maybe_allocate_chunk(struct btrfs_trans_handle *trans, | |||
3467 | return ret == 1 ? 1 : 0; | 3363 | return ret == 1 ? 1 : 0; |
3468 | } | 3364 | } |
3469 | 3365 | ||
3366 | /* | ||
3367 | * shrink metadata reservation for delalloc | ||
3368 | */ | ||
3369 | static int shrink_delalloc(struct btrfs_trans_handle *trans, | ||
3370 | struct btrfs_root *root, | ||
3371 | struct btrfs_space_info *sinfo, u64 to_reclaim) | ||
3372 | { | ||
3373 | u64 reserved; | ||
3374 | u64 max_reclaim; | ||
3375 | u64 reclaimed = 0; | ||
3376 | int pause = 1; | ||
3377 | int ret; | ||
3378 | |||
3379 | spin_lock(&sinfo->lock); | ||
3380 | reserved = sinfo->bytes_delalloc; | ||
3381 | spin_unlock(&sinfo->lock); | ||
3382 | |||
3383 | if (reserved == 0) | ||
3384 | return 0; | ||
3385 | |||
3386 | max_reclaim = min(reserved, to_reclaim); | ||
3387 | |||
3388 | while (1) { | ||
3389 | ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0); | ||
3390 | if (!ret) { | ||
3391 | __set_current_state(TASK_INTERRUPTIBLE); | ||
3392 | schedule_timeout(pause); | ||
3393 | pause <<= 1; | ||
3394 | if (pause > HZ / 10) | ||
3395 | pause = HZ / 10; | ||
3396 | } else { | ||
3397 | pause = 1; | ||
3398 | } | ||
3399 | |||
3400 | spin_lock(&sinfo->lock); | ||
3401 | if (reserved > sinfo->bytes_delalloc) | ||
3402 | reclaimed = reserved - sinfo->bytes_delalloc; | ||
3403 | reserved = sinfo->bytes_delalloc; | ||
3404 | spin_unlock(&sinfo->lock); | ||
3405 | |||
3406 | if (reserved == 0 || reclaimed >= max_reclaim) | ||
3407 | break; | ||
3408 | |||
3409 | if (trans && trans->transaction->blocked) | ||
3410 | return -EAGAIN; | ||
3411 | } | ||
3412 | return reclaimed >= to_reclaim; | ||
3413 | } | ||
3414 | |||
3470 | static int update_block_group(struct btrfs_trans_handle *trans, | 3415 | static int update_block_group(struct btrfs_trans_handle *trans, |
3471 | struct btrfs_root *root, | 3416 | struct btrfs_root *root, |
3472 | u64 bytenr, u64 num_bytes, int alloc, | 3417 | u64 bytenr, u64 num_bytes, int alloc, |