block-group.c (a94794d50d788d4735fd8f656ac8c0510117457d) block-group.c (169e0da91a21a571093feb8ff84c7e9229e64c08)
1// SPDX-License-Identifier: GPL-2.0
2
3#include "misc.h"
4#include "ctree.h"
5#include "block-group.h"
6#include "space-info.h"
7#include "disk-io.h"
8#include "free-space-cache.h"

--- 995 unchanged lines hidden (view full) ---

1004
1005 spin_lock(&block_group->space_info->lock);
1006 list_del_init(&block_group->ro_list);
1007
1008 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1009 WARN_ON(block_group->space_info->total_bytes
1010 < block_group->length);
1011 WARN_ON(block_group->space_info->bytes_readonly
1// SPDX-License-Identifier: GPL-2.0
2
3#include "misc.h"
4#include "ctree.h"
5#include "block-group.h"
6#include "space-info.h"
7#include "disk-io.h"
8#include "free-space-cache.h"

--- 995 unchanged lines hidden (view full) ---

1004
1005 spin_lock(&block_group->space_info->lock);
1006 list_del_init(&block_group->ro_list);
1007
1008 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
1009 WARN_ON(block_group->space_info->total_bytes
1010 < block_group->length);
1011 WARN_ON(block_group->space_info->bytes_readonly
1012 < block_group->length);
1012 < block_group->length - block_group->zone_unusable);
1013 WARN_ON(block_group->space_info->bytes_zone_unusable
1014 < block_group->zone_unusable);
1013 WARN_ON(block_group->space_info->disk_total
1014 < block_group->length * factor);
1015 }
1016 block_group->space_info->total_bytes -= block_group->length;
1015 WARN_ON(block_group->space_info->disk_total
1016 < block_group->length * factor);
1017 }
1018 block_group->space_info->total_bytes -= block_group->length;
1017 block_group->space_info->bytes_readonly -= block_group->length;
1019 block_group->space_info->bytes_readonly -=
1020 (block_group->length - block_group->zone_unusable);
1021 block_group->space_info->bytes_zone_unusable -=
1022 block_group->zone_unusable;
1018 block_group->space_info->disk_total -= block_group->length * factor;
1019
1020 spin_unlock(&block_group->space_info->lock);
1021
1022 /*
1023 * Remove the free space for the block group from the free space tree
1024 * and the block group's item from the extent tree before marking the
1025 * block group as removed. This is to prevent races with tasks that

--- 127 unchanged lines hidden (view full) ---

1153
1154 if (cache->ro) {
1155 cache->ro++;
1156 ret = 0;
1157 goto out;
1158 }
1159
1160 num_bytes = cache->length - cache->reserved - cache->pinned -
1023 block_group->space_info->disk_total -= block_group->length * factor;
1024
1025 spin_unlock(&block_group->space_info->lock);
1026
1027 /*
1028 * Remove the free space for the block group from the free space tree
1029 * and the block group's item from the extent tree before marking the
1030 * block group as removed. This is to prevent races with tasks that

--- 127 unchanged lines hidden (view full) ---

1158
1159 if (cache->ro) {
1160 cache->ro++;
1161 ret = 0;
1162 goto out;
1163 }
1164
1165 num_bytes = cache->length - cache->reserved - cache->pinned -
1161 cache->bytes_super - cache->used;
1166 cache->bytes_super - cache->zone_unusable - cache->used;
1162
1163 /*
1164 * Data never overcommits, even in mixed mode, so do just the straight
1165 * check of left over space in how much we have allocated.
1166 */
1167 if (force) {
1168 ret = 0;
1169 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {

--- 14 unchanged lines hidden (view full) ---

1184 */
1185 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1186 BTRFS_RESERVE_NO_FLUSH))
1187 ret = 0;
1188 }
1189
1190 if (!ret) {
1191 sinfo->bytes_readonly += num_bytes;
1167
1168 /*
1169 * Data never overcommits, even in mixed mode, so do just the straight
1170 * check of left over space in how much we have allocated.
1171 */
1172 if (force) {
1173 ret = 0;
1174 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) {

--- 14 unchanged lines hidden (view full) ---

1189 */
1190 if (btrfs_can_overcommit(cache->fs_info, sinfo, num_bytes,
1191 BTRFS_RESERVE_NO_FLUSH))
1192 ret = 0;
1193 }
1194
1195 if (!ret) {
1196 sinfo->bytes_readonly += num_bytes;
1197 if (btrfs_is_zoned(cache->fs_info)) {
1198 /* Migrate zone_unusable bytes to readonly */
1199 sinfo->bytes_readonly += cache->zone_unusable;
1200 sinfo->bytes_zone_unusable -= cache->zone_unusable;
1201 cache->zone_unusable = 0;
1202 }
1192 cache->ro++;
1193 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
1194 }
1195out:
1196 spin_unlock(&cache->lock);
1197 spin_unlock(&sinfo->lock);
1198 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
1199 btrfs_info(cache->fs_info,

--- 671 unchanged lines hidden (view full) ---

1871 ret = exclude_super_stripes(cache);
1872 if (ret) {
1873 /* We may have excluded something, so call this just in case. */
1874 btrfs_free_excluded_extents(cache);
1875 goto error;
1876 }
1877
1878 /*
1203 cache->ro++;
1204 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
1205 }
1206out:
1207 spin_unlock(&cache->lock);
1208 spin_unlock(&sinfo->lock);
1209 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
1210 btrfs_info(cache->fs_info,

--- 671 unchanged lines hidden (view full) ---

1882 ret = exclude_super_stripes(cache);
1883 if (ret) {
1884 /* We may have excluded something, so call this just in case. */
1885 btrfs_free_excluded_extents(cache);
1886 goto error;
1887 }
1888
1889 /*
1879 * Check for two cases, either we are full, and therefore don't need
1880 * to bother with the caching work since we won't find any space, or we
1881 * are empty, and we can just add all the space in and be done with it.
1882 * This saves us _a_lot_ of time, particularly in the full case.
1890 * For zoned filesystem, space after the allocation offset is the only
1891 * free space for a block group. So, we don't need any caching work.
1892 * btrfs_calc_zone_unusable() will set the amount of free space and
1893 * zone_unusable space.
1894 *
1895 * For regular filesystem, check for two cases, either we are full, and
1896 * therefore don't need to bother with the caching work since we won't
1897 * find any space, or we are empty, and we can just add all the space
1898 * in and be done with it. This saves us _a_lot_ of time, particularly
1899 * in the full case.
1883 */
1900 */
1884 if (cache->length == cache->used) {
1901 if (btrfs_is_zoned(info)) {
1902 btrfs_calc_zone_unusable(cache);
1903 } else if (cache->length == cache->used) {
1885 cache->last_byte_to_unpin = (u64)-1;
1886 cache->cached = BTRFS_CACHE_FINISHED;
1887 btrfs_free_excluded_extents(cache);
1888 } else if (cache->used == 0) {
1889 cache->last_byte_to_unpin = (u64)-1;
1890 cache->cached = BTRFS_CACHE_FINISHED;
1891 add_new_free_space(cache, cache->start,
1892 cache->start + cache->length);
1893 btrfs_free_excluded_extents(cache);
1894 }
1895
1896 ret = btrfs_add_block_group_cache(info, cache);
1897 if (ret) {
1898 btrfs_remove_free_space_cache(cache);
1899 goto error;
1900 }
1901 trace_btrfs_add_block_group(info, cache, 0);
1902 btrfs_update_space_info(info, cache->flags, cache->length,
1904 cache->last_byte_to_unpin = (u64)-1;
1905 cache->cached = BTRFS_CACHE_FINISHED;
1906 btrfs_free_excluded_extents(cache);
1907 } else if (cache->used == 0) {
1908 cache->last_byte_to_unpin = (u64)-1;
1909 cache->cached = BTRFS_CACHE_FINISHED;
1910 add_new_free_space(cache, cache->start,
1911 cache->start + cache->length);
1912 btrfs_free_excluded_extents(cache);
1913 }
1914
1915 ret = btrfs_add_block_group_cache(info, cache);
1916 if (ret) {
1917 btrfs_remove_free_space_cache(cache);
1918 goto error;
1919 }
1920 trace_btrfs_add_block_group(info, cache, 0);
1921 btrfs_update_space_info(info, cache->flags, cache->length,
1903 cache->used, cache->bytes_super, &space_info);
1922 cache->used, cache->bytes_super,
1923 cache->zone_unusable, &space_info);
1904
1905 cache->space_info = space_info;
1906
1907 link_block_group(cache);
1908
1909 set_avail_alloc_bits(info, cache->flags);
1910 if (btrfs_chunk_readonly(info, cache->start)) {
1911 inc_block_group_ro(cache, 1);

--- 39 unchanged lines hidden (view full) ---

1951 bg->flags = map->type;
1952 ret = btrfs_add_block_group_cache(fs_info, bg);
1953 if (ret) {
1954 btrfs_remove_free_space_cache(bg);
1955 btrfs_put_block_group(bg);
1956 break;
1957 }
1958 btrfs_update_space_info(fs_info, bg->flags, em->len, em->len,
1924
1925 cache->space_info = space_info;
1926
1927 link_block_group(cache);
1928
1929 set_avail_alloc_bits(info, cache->flags);
1930 if (btrfs_chunk_readonly(info, cache->start)) {
1931 inc_block_group_ro(cache, 1);

--- 39 unchanged lines hidden (view full) ---

1971 bg->flags = map->type;
1972 ret = btrfs_add_block_group_cache(fs_info, bg);
1973 if (ret) {
1974 btrfs_remove_free_space_cache(bg);
1975 btrfs_put_block_group(bg);
1976 break;
1977 }
1978 btrfs_update_space_info(fs_info, bg->flags, em->len, em->len,
1959 0, &space_info);
1979 0, 0, &space_info);
1960 bg->space_info = space_info;
1961 link_block_group(bg);
1962
1963 set_avail_alloc_bits(fs_info, bg->flags);
1964 }
1965 if (!ret)
1966 btrfs_init_global_block_rsv(fs_info);
1967 return ret;

--- 224 unchanged lines hidden (view full) ---

2192 }
2193
2194 /*
2195 * Now that our block group has its ->space_info set and is inserted in
2196 * the rbtree, update the space info's counters.
2197 */
2198 trace_btrfs_add_block_group(fs_info, cache, 1);
2199 btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
1980 bg->space_info = space_info;
1981 link_block_group(bg);
1982
1983 set_avail_alloc_bits(fs_info, bg->flags);
1984 }
1985 if (!ret)
1986 btrfs_init_global_block_rsv(fs_info);
1987 return ret;

--- 224 unchanged lines hidden (view full) ---

2212 }
2213
2214 /*
2215 * Now that our block group has its ->space_info set and is inserted in
2216 * the rbtree, update the space info's counters.
2217 */
2218 trace_btrfs_add_block_group(fs_info, cache, 1);
2219 btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
2200 cache->bytes_super, &cache->space_info);
2220 cache->bytes_super, 0, &cache->space_info);
2201 btrfs_update_global_block_rsv(fs_info);
2202
2203 link_block_group(cache);
2204
2205 list_add_tail(&cache->bg_list, &trans->new_bgs);
2206 trans->delayed_ref_updates++;
2207 btrfs_update_delayed_refs_rsv(trans);
2208

--- 91 unchanged lines hidden (view full) ---

2300 u64 num_bytes;
2301
2302 BUG_ON(!cache->ro);
2303
2304 spin_lock(&sinfo->lock);
2305 spin_lock(&cache->lock);
2306 if (!--cache->ro) {
2307 num_bytes = cache->length - cache->reserved -
2221 btrfs_update_global_block_rsv(fs_info);
2222
2223 link_block_group(cache);
2224
2225 list_add_tail(&cache->bg_list, &trans->new_bgs);
2226 trans->delayed_ref_updates++;
2227 btrfs_update_delayed_refs_rsv(trans);
2228

--- 91 unchanged lines hidden (view full) ---

2320 u64 num_bytes;
2321
2322 BUG_ON(!cache->ro);
2323
2324 spin_lock(&sinfo->lock);
2325 spin_lock(&cache->lock);
2326 if (!--cache->ro) {
2327 num_bytes = cache->length - cache->reserved -
2308 cache->pinned - cache->bytes_super - cache->used;
2328 cache->pinned - cache->bytes_super -
2329 cache->zone_unusable - cache->used;
2309 sinfo->bytes_readonly -= num_bytes;
2330 sinfo->bytes_readonly -= num_bytes;
2331 if (btrfs_is_zoned(cache->fs_info)) {
2332 /* Migrate zone_unusable bytes back */
2333 cache->zone_unusable = cache->alloc_offset - cache->used;
2334 sinfo->bytes_zone_unusable += cache->zone_unusable;
2335 sinfo->bytes_readonly -= cache->zone_unusable;
2336 }
2310 list_del_init(&cache->ro_list);
2311 }
2312 spin_unlock(&cache->lock);
2313 spin_unlock(&sinfo->lock);
2314}
2315
2316static int update_block_group_item(struct btrfs_trans_handle *trans,
2317 struct btrfs_path *path,

--- 1114 unchanged lines hidden ---
2337 list_del_init(&cache->ro_list);
2338 }
2339 spin_unlock(&cache->lock);
2340 spin_unlock(&sinfo->lock);
2341}
2342
2343static int update_block_group_item(struct btrfs_trans_handle *trans,
2344 struct btrfs_path *path,

--- 1114 unchanged lines hidden ---