@@ -3681,6 +3681,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
u64 to_reserve;
+ u64 max_reserve;
int nr_extents;
int retries = 0;
int ret;
@@ -3717,7 +3718,11 @@ again:
block_rsv_add_bytes(block_rsv, to_reserve, 1);
- if (block_rsv->size > 512 * 1024 * 1024)
+ /* 10% or 2GB */
+ max_reserve = min_t(u64, 2ULL * 1024 * 1024 * 1024,
+ div_factor(root->fs_info->fs_devices->total_rw_bytes, 1));
+
+ if (block_rsv->size > max_reserve)
shrink_delalloc(NULL, root, to_reserve);
return 0;