diff options
author | Patrick McHardy <kaber@trash.net> | 2011-04-13 07:32:28 -0400 |
---|---|---|
committer | Patrick McHardy <kaber@trash.net> | 2011-04-13 07:32:28 -0400 |
commit | b32e3dc7860d00124fa432dba09667e647cb9bcc (patch) | |
tree | 2fa6e56f389431dfb84609d3d7572cad76e88e71 /fs/btrfs/free-space-cache.c | |
parent | 6604271c5bc658a6067ed0c3deba4d89e0e50382 (diff) | |
parent | 96120d86fe302c006259baee9061eea9e1b9e486 (diff) |
Merge branch 'master' of ssh://master.kernel.org/pub/scm/linux/kernel/git/kaber/nf-2.6
Diffstat (limited to 'fs/btrfs/free-space-cache.c')
-rw-r--r-- | fs/btrfs/free-space-cache.c | 510 |
1 files changed, 313 insertions, 197 deletions
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index a0390657451b..0037427d8a9d 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c | |||
@@ -393,7 +393,8 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info, | |||
393 | break; | 393 | break; |
394 | 394 | ||
395 | need_loop = 1; | 395 | need_loop = 1; |
396 | e = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS); | 396 | e = kmem_cache_zalloc(btrfs_free_space_cachep, |
397 | GFP_NOFS); | ||
397 | if (!e) { | 398 | if (!e) { |
398 | kunmap(page); | 399 | kunmap(page); |
399 | unlock_page(page); | 400 | unlock_page(page); |
@@ -405,7 +406,7 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info, | |||
405 | e->bytes = le64_to_cpu(entry->bytes); | 406 | e->bytes = le64_to_cpu(entry->bytes); |
406 | if (!e->bytes) { | 407 | if (!e->bytes) { |
407 | kunmap(page); | 408 | kunmap(page); |
408 | kfree(e); | 409 | kmem_cache_free(btrfs_free_space_cachep, e); |
409 | unlock_page(page); | 410 | unlock_page(page); |
410 | page_cache_release(page); | 411 | page_cache_release(page); |
411 | goto free_cache; | 412 | goto free_cache; |
@@ -420,7 +421,8 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info, | |||
420 | e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); | 421 | e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); |
421 | if (!e->bitmap) { | 422 | if (!e->bitmap) { |
422 | kunmap(page); | 423 | kunmap(page); |
423 | kfree(e); | 424 | kmem_cache_free( |
425 | btrfs_free_space_cachep, e); | ||
424 | unlock_page(page); | 426 | unlock_page(page); |
425 | page_cache_release(page); | 427 | page_cache_release(page); |
426 | goto free_cache; | 428 | goto free_cache; |
@@ -1187,7 +1189,7 @@ static void free_bitmap(struct btrfs_block_group_cache *block_group, | |||
1187 | { | 1189 | { |
1188 | unlink_free_space(block_group, bitmap_info); | 1190 | unlink_free_space(block_group, bitmap_info); |
1189 | kfree(bitmap_info->bitmap); | 1191 | kfree(bitmap_info->bitmap); |
1190 | kfree(bitmap_info); | 1192 | kmem_cache_free(btrfs_free_space_cachep, bitmap_info); |
1191 | block_group->total_bitmaps--; | 1193 | block_group->total_bitmaps--; |
1192 | recalculate_thresholds(block_group); | 1194 | recalculate_thresholds(block_group); |
1193 | } | 1195 | } |
@@ -1285,9 +1287,22 @@ static int insert_into_bitmap(struct btrfs_block_group_cache *block_group, | |||
1285 | * If we are below the extents threshold then we can add this as an | 1287 | * If we are below the extents threshold then we can add this as an |
1286 | * extent, and don't have to deal with the bitmap | 1288 | * extent, and don't have to deal with the bitmap |
1287 | */ | 1289 | */ |
1288 | if (block_group->free_extents < block_group->extents_thresh && | 1290 | if (block_group->free_extents < block_group->extents_thresh) { |
1289 | info->bytes > block_group->sectorsize * 4) | 1291 | /* |
1290 | return 0; | 1292 | * If this block group has some small extents we don't want to |
1293 | * use up all of our free slots in the cache with them, we want | ||
1294 | * to reserve them to larger extents, however if we have plent | ||
1295 | * of cache left then go ahead an dadd them, no sense in adding | ||
1296 | * the overhead of a bitmap if we don't have to. | ||
1297 | */ | ||
1298 | if (info->bytes <= block_group->sectorsize * 4) { | ||
1299 | if (block_group->free_extents * 2 <= | ||
1300 | block_group->extents_thresh) | ||
1301 | return 0; | ||
1302 | } else { | ||
1303 | return 0; | ||
1304 | } | ||
1305 | } | ||
1291 | 1306 | ||
1292 | /* | 1307 | /* |
1293 | * some block groups are so tiny they can't be enveloped by a bitmap, so | 1308 | * some block groups are so tiny they can't be enveloped by a bitmap, so |
@@ -1342,8 +1357,8 @@ new_bitmap: | |||
1342 | 1357 | ||
1343 | /* no pre-allocated info, allocate a new one */ | 1358 | /* no pre-allocated info, allocate a new one */ |
1344 | if (!info) { | 1359 | if (!info) { |
1345 | info = kzalloc(sizeof(struct btrfs_free_space), | 1360 | info = kmem_cache_zalloc(btrfs_free_space_cachep, |
1346 | GFP_NOFS); | 1361 | GFP_NOFS); |
1347 | if (!info) { | 1362 | if (!info) { |
1348 | spin_lock(&block_group->tree_lock); | 1363 | spin_lock(&block_group->tree_lock); |
1349 | ret = -ENOMEM; | 1364 | ret = -ENOMEM; |
@@ -1365,7 +1380,7 @@ out: | |||
1365 | if (info) { | 1380 | if (info) { |
1366 | if (info->bitmap) | 1381 | if (info->bitmap) |
1367 | kfree(info->bitmap); | 1382 | kfree(info->bitmap); |
1368 | kfree(info); | 1383 | kmem_cache_free(btrfs_free_space_cachep, info); |
1369 | } | 1384 | } |
1370 | 1385 | ||
1371 | return ret; | 1386 | return ret; |
@@ -1398,7 +1413,7 @@ bool try_merge_free_space(struct btrfs_block_group_cache *block_group, | |||
1398 | else | 1413 | else |
1399 | __unlink_free_space(block_group, right_info); | 1414 | __unlink_free_space(block_group, right_info); |
1400 | info->bytes += right_info->bytes; | 1415 | info->bytes += right_info->bytes; |
1401 | kfree(right_info); | 1416 | kmem_cache_free(btrfs_free_space_cachep, right_info); |
1402 | merged = true; | 1417 | merged = true; |
1403 | } | 1418 | } |
1404 | 1419 | ||
@@ -1410,7 +1425,7 @@ bool try_merge_free_space(struct btrfs_block_group_cache *block_group, | |||
1410 | __unlink_free_space(block_group, left_info); | 1425 | __unlink_free_space(block_group, left_info); |
1411 | info->offset = left_info->offset; | 1426 | info->offset = left_info->offset; |
1412 | info->bytes += left_info->bytes; | 1427 | info->bytes += left_info->bytes; |
1413 | kfree(left_info); | 1428 | kmem_cache_free(btrfs_free_space_cachep, left_info); |
1414 | merged = true; | 1429 | merged = true; |
1415 | } | 1430 | } |
1416 | 1431 | ||
@@ -1423,7 +1438,7 @@ int btrfs_add_free_space(struct btrfs_block_group_cache *block_group, | |||
1423 | struct btrfs_free_space *info; | 1438 | struct btrfs_free_space *info; |
1424 | int ret = 0; | 1439 | int ret = 0; |
1425 | 1440 | ||
1426 | info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS); | 1441 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
1427 | if (!info) | 1442 | if (!info) |
1428 | return -ENOMEM; | 1443 | return -ENOMEM; |
1429 | 1444 | ||
@@ -1450,7 +1465,7 @@ int btrfs_add_free_space(struct btrfs_block_group_cache *block_group, | |||
1450 | link: | 1465 | link: |
1451 | ret = link_free_space(block_group, info); | 1466 | ret = link_free_space(block_group, info); |
1452 | if (ret) | 1467 | if (ret) |
1453 | kfree(info); | 1468 | kmem_cache_free(btrfs_free_space_cachep, info); |
1454 | out: | 1469 | out: |
1455 | spin_unlock(&block_group->tree_lock); | 1470 | spin_unlock(&block_group->tree_lock); |
1456 | 1471 | ||
@@ -1520,7 +1535,7 @@ again: | |||
1520 | kfree(info->bitmap); | 1535 | kfree(info->bitmap); |
1521 | block_group->total_bitmaps--; | 1536 | block_group->total_bitmaps--; |
1522 | } | 1537 | } |
1523 | kfree(info); | 1538 | kmem_cache_free(btrfs_free_space_cachep, info); |
1524 | goto out_lock; | 1539 | goto out_lock; |
1525 | } | 1540 | } |
1526 | 1541 | ||
@@ -1556,7 +1571,7 @@ again: | |||
1556 | /* the hole we're creating ends at the end | 1571 | /* the hole we're creating ends at the end |
1557 | * of the info struct, just free the info | 1572 | * of the info struct, just free the info |
1558 | */ | 1573 | */ |
1559 | kfree(info); | 1574 | kmem_cache_free(btrfs_free_space_cachep, info); |
1560 | } | 1575 | } |
1561 | spin_unlock(&block_group->tree_lock); | 1576 | spin_unlock(&block_group->tree_lock); |
1562 | 1577 | ||
@@ -1629,30 +1644,28 @@ __btrfs_return_cluster_to_free_space( | |||
1629 | { | 1644 | { |
1630 | struct btrfs_free_space *entry; | 1645 | struct btrfs_free_space *entry; |
1631 | struct rb_node *node; | 1646 | struct rb_node *node; |
1632 | bool bitmap; | ||
1633 | 1647 | ||
1634 | spin_lock(&cluster->lock); | 1648 | spin_lock(&cluster->lock); |
1635 | if (cluster->block_group != block_group) | 1649 | if (cluster->block_group != block_group) |
1636 | goto out; | 1650 | goto out; |
1637 | 1651 | ||
1638 | bitmap = cluster->points_to_bitmap; | ||
1639 | cluster->block_group = NULL; | 1652 | cluster->block_group = NULL; |
1640 | cluster->window_start = 0; | 1653 | cluster->window_start = 0; |
1641 | list_del_init(&cluster->block_group_list); | 1654 | list_del_init(&cluster->block_group_list); |
1642 | cluster->points_to_bitmap = false; | ||
1643 | |||
1644 | if (bitmap) | ||
1645 | goto out; | ||
1646 | 1655 | ||
1647 | node = rb_first(&cluster->root); | 1656 | node = rb_first(&cluster->root); |
1648 | while (node) { | 1657 | while (node) { |
1658 | bool bitmap; | ||
1659 | |||
1649 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | 1660 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
1650 | node = rb_next(&entry->offset_index); | 1661 | node = rb_next(&entry->offset_index); |
1651 | rb_erase(&entry->offset_index, &cluster->root); | 1662 | rb_erase(&entry->offset_index, &cluster->root); |
1652 | BUG_ON(entry->bitmap); | 1663 | |
1653 | try_merge_free_space(block_group, entry, false); | 1664 | bitmap = (entry->bitmap != NULL); |
1665 | if (!bitmap) | ||
1666 | try_merge_free_space(block_group, entry, false); | ||
1654 | tree_insert_offset(&block_group->free_space_offset, | 1667 | tree_insert_offset(&block_group->free_space_offset, |
1655 | entry->offset, &entry->offset_index, 0); | 1668 | entry->offset, &entry->offset_index, bitmap); |
1656 | } | 1669 | } |
1657 | cluster->root = RB_ROOT; | 1670 | cluster->root = RB_ROOT; |
1658 | 1671 | ||
@@ -1689,7 +1702,7 @@ void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group) | |||
1689 | unlink_free_space(block_group, info); | 1702 | unlink_free_space(block_group, info); |
1690 | if (info->bitmap) | 1703 | if (info->bitmap) |
1691 | kfree(info->bitmap); | 1704 | kfree(info->bitmap); |
1692 | kfree(info); | 1705 | kmem_cache_free(btrfs_free_space_cachep, info); |
1693 | if (need_resched()) { | 1706 | if (need_resched()) { |
1694 | spin_unlock(&block_group->tree_lock); | 1707 | spin_unlock(&block_group->tree_lock); |
1695 | cond_resched(); | 1708 | cond_resched(); |
@@ -1722,7 +1735,7 @@ u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group, | |||
1722 | entry->offset += bytes; | 1735 | entry->offset += bytes; |
1723 | entry->bytes -= bytes; | 1736 | entry->bytes -= bytes; |
1724 | if (!entry->bytes) | 1737 | if (!entry->bytes) |
1725 | kfree(entry); | 1738 | kmem_cache_free(btrfs_free_space_cachep, entry); |
1726 | else | 1739 | else |
1727 | link_free_space(block_group, entry); | 1740 | link_free_space(block_group, entry); |
1728 | } | 1741 | } |
@@ -1775,50 +1788,24 @@ int btrfs_return_cluster_to_free_space( | |||
1775 | 1788 | ||
1776 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, | 1789 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, |
1777 | struct btrfs_free_cluster *cluster, | 1790 | struct btrfs_free_cluster *cluster, |
1791 | struct btrfs_free_space *entry, | ||
1778 | u64 bytes, u64 min_start) | 1792 | u64 bytes, u64 min_start) |
1779 | { | 1793 | { |
1780 | struct btrfs_free_space *entry; | ||
1781 | int err; | 1794 | int err; |
1782 | u64 search_start = cluster->window_start; | 1795 | u64 search_start = cluster->window_start; |
1783 | u64 search_bytes = bytes; | 1796 | u64 search_bytes = bytes; |
1784 | u64 ret = 0; | 1797 | u64 ret = 0; |
1785 | 1798 | ||
1786 | spin_lock(&block_group->tree_lock); | ||
1787 | spin_lock(&cluster->lock); | ||
1788 | |||
1789 | if (!cluster->points_to_bitmap) | ||
1790 | goto out; | ||
1791 | |||
1792 | if (cluster->block_group != block_group) | ||
1793 | goto out; | ||
1794 | |||
1795 | /* | ||
1796 | * search_start is the beginning of the bitmap, but at some point it may | ||
1797 | * be a good idea to point to the actual start of the free area in the | ||
1798 | * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only | ||
1799 | * to 1 to make sure we get the bitmap entry | ||
1800 | */ | ||
1801 | entry = tree_search_offset(block_group, | ||
1802 | offset_to_bitmap(block_group, search_start), | ||
1803 | 1, 0); | ||
1804 | if (!entry || !entry->bitmap) | ||
1805 | goto out; | ||
1806 | |||
1807 | search_start = min_start; | 1799 | search_start = min_start; |
1808 | search_bytes = bytes; | 1800 | search_bytes = bytes; |
1809 | 1801 | ||
1810 | err = search_bitmap(block_group, entry, &search_start, | 1802 | err = search_bitmap(block_group, entry, &search_start, |
1811 | &search_bytes); | 1803 | &search_bytes); |
1812 | if (err) | 1804 | if (err) |
1813 | goto out; | 1805 | return 0; |
1814 | 1806 | ||
1815 | ret = search_start; | 1807 | ret = search_start; |
1816 | bitmap_clear_bits(block_group, entry, ret, bytes); | 1808 | bitmap_clear_bits(block_group, entry, ret, bytes); |
1817 | if (entry->bytes == 0) | ||
1818 | free_bitmap(block_group, entry); | ||
1819 | out: | ||
1820 | spin_unlock(&cluster->lock); | ||
1821 | spin_unlock(&block_group->tree_lock); | ||
1822 | 1809 | ||
1823 | return ret; | 1810 | return ret; |
1824 | } | 1811 | } |
@@ -1836,10 +1823,6 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group, | |||
1836 | struct rb_node *node; | 1823 | struct rb_node *node; |
1837 | u64 ret = 0; | 1824 | u64 ret = 0; |
1838 | 1825 | ||
1839 | if (cluster->points_to_bitmap) | ||
1840 | return btrfs_alloc_from_bitmap(block_group, cluster, bytes, | ||
1841 | min_start); | ||
1842 | |||
1843 | spin_lock(&cluster->lock); | 1826 | spin_lock(&cluster->lock); |
1844 | if (bytes > cluster->max_size) | 1827 | if (bytes > cluster->max_size) |
1845 | goto out; | 1828 | goto out; |
@@ -1852,9 +1835,9 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group, | |||
1852 | goto out; | 1835 | goto out; |
1853 | 1836 | ||
1854 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | 1837 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
1855 | |||
1856 | while(1) { | 1838 | while(1) { |
1857 | if (entry->bytes < bytes || entry->offset < min_start) { | 1839 | if (entry->bytes < bytes || |
1840 | (!entry->bitmap && entry->offset < min_start)) { | ||
1858 | struct rb_node *node; | 1841 | struct rb_node *node; |
1859 | 1842 | ||
1860 | node = rb_next(&entry->offset_index); | 1843 | node = rb_next(&entry->offset_index); |
@@ -1864,10 +1847,27 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group, | |||
1864 | offset_index); | 1847 | offset_index); |
1865 | continue; | 1848 | continue; |
1866 | } | 1849 | } |
1867 | ret = entry->offset; | ||
1868 | 1850 | ||
1869 | entry->offset += bytes; | 1851 | if (entry->bitmap) { |
1870 | entry->bytes -= bytes; | 1852 | ret = btrfs_alloc_from_bitmap(block_group, |
1853 | cluster, entry, bytes, | ||
1854 | min_start); | ||
1855 | if (ret == 0) { | ||
1856 | struct rb_node *node; | ||
1857 | node = rb_next(&entry->offset_index); | ||
1858 | if (!node) | ||
1859 | break; | ||
1860 | entry = rb_entry(node, struct btrfs_free_space, | ||
1861 | offset_index); | ||
1862 | continue; | ||
1863 | } | ||
1864 | } else { | ||
1865 | |||
1866 | ret = entry->offset; | ||
1867 | |||
1868 | entry->offset += bytes; | ||
1869 | entry->bytes -= bytes; | ||
1870 | } | ||
1871 | 1871 | ||
1872 | if (entry->bytes == 0) | 1872 | if (entry->bytes == 0) |
1873 | rb_erase(&entry->offset_index, &cluster->root); | 1873 | rb_erase(&entry->offset_index, &cluster->root); |
@@ -1884,7 +1884,12 @@ out: | |||
1884 | block_group->free_space -= bytes; | 1884 | block_group->free_space -= bytes; |
1885 | if (entry->bytes == 0) { | 1885 | if (entry->bytes == 0) { |
1886 | block_group->free_extents--; | 1886 | block_group->free_extents--; |
1887 | kfree(entry); | 1887 | if (entry->bitmap) { |
1888 | kfree(entry->bitmap); | ||
1889 | block_group->total_bitmaps--; | ||
1890 | recalculate_thresholds(block_group); | ||
1891 | } | ||
1892 | kmem_cache_free(btrfs_free_space_cachep, entry); | ||
1888 | } | 1893 | } |
1889 | 1894 | ||
1890 | spin_unlock(&block_group->tree_lock); | 1895 | spin_unlock(&block_group->tree_lock); |
@@ -1904,12 +1909,13 @@ static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group, | |||
1904 | unsigned long found_bits; | 1909 | unsigned long found_bits; |
1905 | unsigned long start = 0; | 1910 | unsigned long start = 0; |
1906 | unsigned long total_found = 0; | 1911 | unsigned long total_found = 0; |
1912 | int ret; | ||
1907 | bool found = false; | 1913 | bool found = false; |
1908 | 1914 | ||
1909 | i = offset_to_bit(entry->offset, block_group->sectorsize, | 1915 | i = offset_to_bit(entry->offset, block_group->sectorsize, |
1910 | max_t(u64, offset, entry->offset)); | 1916 | max_t(u64, offset, entry->offset)); |
1911 | search_bits = bytes_to_bits(min_bytes, block_group->sectorsize); | 1917 | search_bits = bytes_to_bits(bytes, block_group->sectorsize); |
1912 | total_bits = bytes_to_bits(bytes, block_group->sectorsize); | 1918 | total_bits = bytes_to_bits(min_bytes, block_group->sectorsize); |
1913 | 1919 | ||
1914 | again: | 1920 | again: |
1915 | found_bits = 0; | 1921 | found_bits = 0; |
@@ -1926,7 +1932,7 @@ again: | |||
1926 | } | 1932 | } |
1927 | 1933 | ||
1928 | if (!found_bits) | 1934 | if (!found_bits) |
1929 | return -1; | 1935 | return -ENOSPC; |
1930 | 1936 | ||
1931 | if (!found) { | 1937 | if (!found) { |
1932 | start = i; | 1938 | start = i; |
@@ -1950,189 +1956,208 @@ again: | |||
1950 | 1956 | ||
1951 | cluster->window_start = start * block_group->sectorsize + | 1957 | cluster->window_start = start * block_group->sectorsize + |
1952 | entry->offset; | 1958 | entry->offset; |
1953 | cluster->points_to_bitmap = true; | 1959 | rb_erase(&entry->offset_index, &block_group->free_space_offset); |
1960 | ret = tree_insert_offset(&cluster->root, entry->offset, | ||
1961 | &entry->offset_index, 1); | ||
1962 | BUG_ON(ret); | ||
1954 | 1963 | ||
1955 | return 0; | 1964 | return 0; |
1956 | } | 1965 | } |
1957 | 1966 | ||
1958 | /* | 1967 | /* |
1959 | * here we try to find a cluster of blocks in a block group. The goal | 1968 | * This searches the block group for just extents to fill the cluster with. |
1960 | * is to find at least bytes free and up to empty_size + bytes free. | ||
1961 | * We might not find them all in one contiguous area. | ||
1962 | * | ||
1963 | * returns zero and sets up cluster if things worked out, otherwise | ||
1964 | * it returns -enospc | ||
1965 | */ | 1969 | */ |
1966 | int btrfs_find_space_cluster(struct btrfs_trans_handle *trans, | 1970 | static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group, |
1967 | struct btrfs_root *root, | 1971 | struct btrfs_free_cluster *cluster, |
1968 | struct btrfs_block_group_cache *block_group, | 1972 | u64 offset, u64 bytes, u64 min_bytes) |
1969 | struct btrfs_free_cluster *cluster, | ||
1970 | u64 offset, u64 bytes, u64 empty_size) | ||
1971 | { | 1973 | { |
1974 | struct btrfs_free_space *first = NULL; | ||
1972 | struct btrfs_free_space *entry = NULL; | 1975 | struct btrfs_free_space *entry = NULL; |
1976 | struct btrfs_free_space *prev = NULL; | ||
1977 | struct btrfs_free_space *last; | ||
1973 | struct rb_node *node; | 1978 | struct rb_node *node; |
1974 | struct btrfs_free_space *next; | ||
1975 | struct btrfs_free_space *last = NULL; | ||
1976 | u64 min_bytes; | ||
1977 | u64 window_start; | 1979 | u64 window_start; |
1978 | u64 window_free; | 1980 | u64 window_free; |
1979 | u64 max_extent = 0; | 1981 | u64 max_extent; |
1980 | bool found_bitmap = false; | 1982 | u64 max_gap = 128 * 1024; |
1981 | int ret; | ||
1982 | 1983 | ||
1983 | /* for metadata, allow allocates with more holes */ | 1984 | entry = tree_search_offset(block_group, offset, 0, 1); |
1984 | if (btrfs_test_opt(root, SSD_SPREAD)) { | 1985 | if (!entry) |
1985 | min_bytes = bytes + empty_size; | 1986 | return -ENOSPC; |
1986 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { | ||
1987 | /* | ||
1988 | * we want to do larger allocations when we are | ||
1989 | * flushing out the delayed refs, it helps prevent | ||
1990 | * making more work as we go along. | ||
1991 | */ | ||
1992 | if (trans->transaction->delayed_refs.flushing) | ||
1993 | min_bytes = max(bytes, (bytes + empty_size) >> 1); | ||
1994 | else | ||
1995 | min_bytes = max(bytes, (bytes + empty_size) >> 4); | ||
1996 | } else | ||
1997 | min_bytes = max(bytes, (bytes + empty_size) >> 2); | ||
1998 | |||
1999 | spin_lock(&block_group->tree_lock); | ||
2000 | spin_lock(&cluster->lock); | ||
2001 | |||
2002 | /* someone already found a cluster, hooray */ | ||
2003 | if (cluster->block_group) { | ||
2004 | ret = 0; | ||
2005 | goto out; | ||
2006 | } | ||
2007 | again: | ||
2008 | entry = tree_search_offset(block_group, offset, found_bitmap, 1); | ||
2009 | if (!entry) { | ||
2010 | ret = -ENOSPC; | ||
2011 | goto out; | ||
2012 | } | ||
2013 | 1987 | ||
2014 | /* | 1988 | /* |
2015 | * If found_bitmap is true, we exhausted our search for extent entries, | 1989 | * We don't want bitmaps, so just move along until we find a normal |
2016 | * and we just want to search all of the bitmaps that we can find, and | 1990 | * extent entry. |
2017 | * ignore any extent entries we find. | ||
2018 | */ | 1991 | */ |
2019 | while (entry->bitmap || found_bitmap || | 1992 | while (entry->bitmap) { |
2020 | (!entry->bitmap && entry->bytes < min_bytes)) { | 1993 | node = rb_next(&entry->offset_index); |
2021 | struct rb_node *node = rb_next(&entry->offset_index); | 1994 | if (!node) |
2022 | 1995 | return -ENOSPC; | |
2023 | if (entry->bitmap && entry->bytes > bytes + empty_size) { | ||
2024 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, | ||
2025 | offset, bytes + empty_size, | ||
2026 | min_bytes); | ||
2027 | if (!ret) | ||
2028 | goto got_it; | ||
2029 | } | ||
2030 | |||
2031 | if (!node) { | ||
2032 | ret = -ENOSPC; | ||
2033 | goto out; | ||
2034 | } | ||
2035 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | 1996 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
2036 | } | 1997 | } |
2037 | 1998 | ||
2038 | /* | ||
2039 | * We already searched all the extent entries from the passed in offset | ||
2040 | * to the end and didn't find enough space for the cluster, and we also | ||
2041 | * didn't find any bitmaps that met our criteria, just go ahead and exit | ||
2042 | */ | ||
2043 | if (found_bitmap) { | ||
2044 | ret = -ENOSPC; | ||
2045 | goto out; | ||
2046 | } | ||
2047 | |||
2048 | cluster->points_to_bitmap = false; | ||
2049 | window_start = entry->offset; | 1999 | window_start = entry->offset; |
2050 | window_free = entry->bytes; | 2000 | window_free = entry->bytes; |
2051 | last = entry; | ||
2052 | max_extent = entry->bytes; | 2001 | max_extent = entry->bytes; |
2002 | first = entry; | ||
2003 | last = entry; | ||
2004 | prev = entry; | ||
2053 | 2005 | ||
2054 | while (1) { | 2006 | while (window_free <= min_bytes) { |
2055 | /* out window is just right, lets fill it */ | 2007 | node = rb_next(&entry->offset_index); |
2056 | if (window_free >= bytes + empty_size) | 2008 | if (!node) |
2057 | break; | 2009 | return -ENOSPC; |
2058 | 2010 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
2059 | node = rb_next(&last->offset_index); | ||
2060 | if (!node) { | ||
2061 | if (found_bitmap) | ||
2062 | goto again; | ||
2063 | ret = -ENOSPC; | ||
2064 | goto out; | ||
2065 | } | ||
2066 | next = rb_entry(node, struct btrfs_free_space, offset_index); | ||
2067 | 2011 | ||
2068 | /* | 2012 | if (entry->bitmap) |
2069 | * we found a bitmap, so if this search doesn't result in a | ||
2070 | * cluster, we know to go and search again for the bitmaps and | ||
2071 | * start looking for space there | ||
2072 | */ | ||
2073 | if (next->bitmap) { | ||
2074 | if (!found_bitmap) | ||
2075 | offset = next->offset; | ||
2076 | found_bitmap = true; | ||
2077 | last = next; | ||
2078 | continue; | 2013 | continue; |
2079 | } | ||
2080 | |||
2081 | /* | 2014 | /* |
2082 | * we haven't filled the empty size and the window is | 2015 | * we haven't filled the empty size and the window is |
2083 | * very large. reset and try again | 2016 | * very large. reset and try again |
2084 | */ | 2017 | */ |
2085 | if (next->offset - (last->offset + last->bytes) > 128 * 1024 || | 2018 | if (entry->offset - (prev->offset + prev->bytes) > max_gap || |
2086 | next->offset - window_start > (bytes + empty_size) * 2) { | 2019 | entry->offset - window_start > (min_bytes * 2)) { |
2087 | entry = next; | 2020 | first = entry; |
2088 | window_start = entry->offset; | 2021 | window_start = entry->offset; |
2089 | window_free = entry->bytes; | 2022 | window_free = entry->bytes; |
2090 | last = entry; | 2023 | last = entry; |
2091 | max_extent = entry->bytes; | 2024 | max_extent = entry->bytes; |
2092 | } else { | 2025 | } else { |
2093 | last = next; | 2026 | last = entry; |
2094 | window_free += next->bytes; | 2027 | window_free += entry->bytes; |
2095 | if (entry->bytes > max_extent) | 2028 | if (entry->bytes > max_extent) |
2096 | max_extent = entry->bytes; | 2029 | max_extent = entry->bytes; |
2097 | } | 2030 | } |
2031 | prev = entry; | ||
2098 | } | 2032 | } |
2099 | 2033 | ||
2100 | cluster->window_start = entry->offset; | 2034 | cluster->window_start = first->offset; |
2035 | |||
2036 | node = &first->offset_index; | ||
2101 | 2037 | ||
2102 | /* | 2038 | /* |
2103 | * now we've found our entries, pull them out of the free space | 2039 | * now we've found our entries, pull them out of the free space |
2104 | * cache and put them into the cluster rbtree | 2040 | * cache and put them into the cluster rbtree |
2105 | * | ||
2106 | * The cluster includes an rbtree, but only uses the offset index | ||
2107 | * of each free space cache entry. | ||
2108 | */ | 2041 | */ |
2109 | while (1) { | 2042 | do { |
2043 | int ret; | ||
2044 | |||
2045 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | ||
2110 | node = rb_next(&entry->offset_index); | 2046 | node = rb_next(&entry->offset_index); |
2111 | if (entry->bitmap && node) { | 2047 | if (entry->bitmap) |
2112 | entry = rb_entry(node, struct btrfs_free_space, | ||
2113 | offset_index); | ||
2114 | continue; | 2048 | continue; |
2115 | } else if (entry->bitmap && !node) { | ||
2116 | break; | ||
2117 | } | ||
2118 | 2049 | ||
2119 | rb_erase(&entry->offset_index, &block_group->free_space_offset); | 2050 | rb_erase(&entry->offset_index, &block_group->free_space_offset); |
2120 | ret = tree_insert_offset(&cluster->root, entry->offset, | 2051 | ret = tree_insert_offset(&cluster->root, entry->offset, |
2121 | &entry->offset_index, 0); | 2052 | &entry->offset_index, 0); |
2122 | BUG_ON(ret); | 2053 | BUG_ON(ret); |
2054 | } while (node && entry != last); | ||
2123 | 2055 | ||
2124 | if (!node || entry == last) | 2056 | cluster->max_size = max_extent; |
2125 | break; | 2057 | |
2058 | return 0; | ||
2059 | } | ||
2060 | |||
2061 | /* | ||
2062 | * This specifically looks for bitmaps that may work in the cluster, we assume | ||
2063 | * that we have already failed to find extents that will work. | ||
2064 | */ | ||
2065 | static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group, | ||
2066 | struct btrfs_free_cluster *cluster, | ||
2067 | u64 offset, u64 bytes, u64 min_bytes) | ||
2068 | { | ||
2069 | struct btrfs_free_space *entry; | ||
2070 | struct rb_node *node; | ||
2071 | int ret = -ENOSPC; | ||
2072 | |||
2073 | if (block_group->total_bitmaps == 0) | ||
2074 | return -ENOSPC; | ||
2126 | 2075 | ||
2076 | entry = tree_search_offset(block_group, | ||
2077 | offset_to_bitmap(block_group, offset), | ||
2078 | 0, 1); | ||
2079 | if (!entry) | ||
2080 | return -ENOSPC; | ||
2081 | |||
2082 | node = &entry->offset_index; | ||
2083 | do { | ||
2127 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | 2084 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
2085 | node = rb_next(&entry->offset_index); | ||
2086 | if (!entry->bitmap) | ||
2087 | continue; | ||
2088 | if (entry->bytes < min_bytes) | ||
2089 | continue; | ||
2090 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, | ||
2091 | bytes, min_bytes); | ||
2092 | } while (ret && node); | ||
2093 | |||
2094 | return ret; | ||
2095 | } | ||
2096 | |||
2097 | /* | ||
2098 | * here we try to find a cluster of blocks in a block group. The goal | ||
2099 | * is to find at least bytes free and up to empty_size + bytes free. | ||
2100 | * We might not find them all in one contiguous area. | ||
2101 | * | ||
2102 | * returns zero and sets up cluster if things worked out, otherwise | ||
2103 | * it returns -enospc | ||
2104 | */ | ||
2105 | int btrfs_find_space_cluster(struct btrfs_trans_handle *trans, | ||
2106 | struct btrfs_root *root, | ||
2107 | struct btrfs_block_group_cache *block_group, | ||
2108 | struct btrfs_free_cluster *cluster, | ||
2109 | u64 offset, u64 bytes, u64 empty_size) | ||
2110 | { | ||
2111 | u64 min_bytes; | ||
2112 | int ret; | ||
2113 | |||
2114 | /* for metadata, allow allocates with more holes */ | ||
2115 | if (btrfs_test_opt(root, SSD_SPREAD)) { | ||
2116 | min_bytes = bytes + empty_size; | ||
2117 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { | ||
2118 | /* | ||
2119 | * we want to do larger allocations when we are | ||
2120 | * flushing out the delayed refs, it helps prevent | ||
2121 | * making more work as we go along. | ||
2122 | */ | ||
2123 | if (trans->transaction->delayed_refs.flushing) | ||
2124 | min_bytes = max(bytes, (bytes + empty_size) >> 1); | ||
2125 | else | ||
2126 | min_bytes = max(bytes, (bytes + empty_size) >> 4); | ||
2127 | } else | ||
2128 | min_bytes = max(bytes, (bytes + empty_size) >> 2); | ||
2129 | |||
2130 | spin_lock(&block_group->tree_lock); | ||
2131 | |||
2132 | /* | ||
2133 | * If we know we don't have enough space to make a cluster don't even | ||
2134 | * bother doing all the work to try and find one. | ||
2135 | */ | ||
2136 | if (block_group->free_space < min_bytes) { | ||
2137 | spin_unlock(&block_group->tree_lock); | ||
2138 | return -ENOSPC; | ||
2128 | } | 2139 | } |
2129 | 2140 | ||
2130 | cluster->max_size = max_extent; | 2141 | spin_lock(&cluster->lock); |
2131 | got_it: | 2142 | |
2132 | ret = 0; | 2143 | /* someone already found a cluster, hooray */ |
2133 | atomic_inc(&block_group->count); | 2144 | if (cluster->block_group) { |
2134 | list_add_tail(&cluster->block_group_list, &block_group->cluster_list); | 2145 | ret = 0; |
2135 | cluster->block_group = block_group; | 2146 | goto out; |
2147 | } | ||
2148 | |||
2149 | ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes, | ||
2150 | min_bytes); | ||
2151 | if (ret) | ||
2152 | ret = setup_cluster_bitmap(block_group, cluster, offset, | ||
2153 | bytes, min_bytes); | ||
2154 | |||
2155 | if (!ret) { | ||
2156 | atomic_inc(&block_group->count); | ||
2157 | list_add_tail(&cluster->block_group_list, | ||
2158 | &block_group->cluster_list); | ||
2159 | cluster->block_group = block_group; | ||
2160 | } | ||
2136 | out: | 2161 | out: |
2137 | spin_unlock(&cluster->lock); | 2162 | spin_unlock(&cluster->lock); |
2138 | spin_unlock(&block_group->tree_lock); | 2163 | spin_unlock(&block_group->tree_lock); |
@@ -2149,8 +2174,99 @@ void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster) | |||
2149 | spin_lock_init(&cluster->refill_lock); | 2174 | spin_lock_init(&cluster->refill_lock); |
2150 | cluster->root = RB_ROOT; | 2175 | cluster->root = RB_ROOT; |
2151 | cluster->max_size = 0; | 2176 | cluster->max_size = 0; |
2152 | cluster->points_to_bitmap = false; | ||
2153 | INIT_LIST_HEAD(&cluster->block_group_list); | 2177 | INIT_LIST_HEAD(&cluster->block_group_list); |
2154 | cluster->block_group = NULL; | 2178 | cluster->block_group = NULL; |
2155 | } | 2179 | } |
2156 | 2180 | ||
2181 | int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, | ||
2182 | u64 *trimmed, u64 start, u64 end, u64 minlen) | ||
2183 | { | ||
2184 | struct btrfs_free_space *entry = NULL; | ||
2185 | struct btrfs_fs_info *fs_info = block_group->fs_info; | ||
2186 | u64 bytes = 0; | ||
2187 | u64 actually_trimmed; | ||
2188 | int ret = 0; | ||
2189 | |||
2190 | *trimmed = 0; | ||
2191 | |||
2192 | while (start < end) { | ||
2193 | spin_lock(&block_group->tree_lock); | ||
2194 | |||
2195 | if (block_group->free_space < minlen) { | ||
2196 | spin_unlock(&block_group->tree_lock); | ||
2197 | break; | ||
2198 | } | ||
2199 | |||
2200 | entry = tree_search_offset(block_group, start, 0, 1); | ||
2201 | if (!entry) | ||
2202 | entry = tree_search_offset(block_group, | ||
2203 | offset_to_bitmap(block_group, | ||
2204 | start), | ||
2205 | 1, 1); | ||
2206 | |||
2207 | if (!entry || entry->offset >= end) { | ||
2208 | spin_unlock(&block_group->tree_lock); | ||
2209 | break; | ||
2210 | } | ||
2211 | |||
2212 | if (entry->bitmap) { | ||
2213 | ret = search_bitmap(block_group, entry, &start, &bytes); | ||
2214 | if (!ret) { | ||
2215 | if (start >= end) { | ||
2216 | spin_unlock(&block_group->tree_lock); | ||
2217 | break; | ||
2218 | } | ||
2219 | bytes = min(bytes, end - start); | ||
2220 | bitmap_clear_bits(block_group, entry, | ||
2221 | start, bytes); | ||
2222 | if (entry->bytes == 0) | ||
2223 | free_bitmap(block_group, entry); | ||
2224 | } else { | ||
2225 | start = entry->offset + BITS_PER_BITMAP * | ||
2226 | block_group->sectorsize; | ||
2227 | spin_unlock(&block_group->tree_lock); | ||
2228 | ret = 0; | ||
2229 | continue; | ||
2230 | } | ||
2231 | } else { | ||
2232 | start = entry->offset; | ||
2233 | bytes = min(entry->bytes, end - start); | ||
2234 | unlink_free_space(block_group, entry); | ||
2235 | kfree(entry); | ||
2236 | } | ||
2237 | |||
2238 | spin_unlock(&block_group->tree_lock); | ||
2239 | |||
2240 | if (bytes >= minlen) { | ||
2241 | int update_ret; | ||
2242 | update_ret = btrfs_update_reserved_bytes(block_group, | ||
2243 | bytes, 1, 1); | ||
2244 | |||
2245 | ret = btrfs_error_discard_extent(fs_info->extent_root, | ||
2246 | start, | ||
2247 | bytes, | ||
2248 | &actually_trimmed); | ||
2249 | |||
2250 | btrfs_add_free_space(block_group, | ||
2251 | start, bytes); | ||
2252 | if (!update_ret) | ||
2253 | btrfs_update_reserved_bytes(block_group, | ||
2254 | bytes, 0, 1); | ||
2255 | |||
2256 | if (ret) | ||
2257 | break; | ||
2258 | *trimmed += actually_trimmed; | ||
2259 | } | ||
2260 | start += bytes; | ||
2261 | bytes = 0; | ||
2262 | |||
2263 | if (fatal_signal_pending(current)) { | ||
2264 | ret = -ERESTARTSYS; | ||
2265 | break; | ||
2266 | } | ||
2267 | |||
2268 | cond_resched(); | ||
2269 | } | ||
2270 | |||
2271 | return ret; | ||
2272 | } | ||