aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorJosef Bacik <josef@redhat.com>2011-03-21 10:11:24 -0400
committerChris Mason <chris.mason@oracle.com>2011-03-25 19:08:08 -0400
commit4e69b598f6cfb0940b75abf7e179d6020e94ad1e (patch)
treeae14c7078bd7540200ffe32726e33dd8ff64f5d5 /fs
parent32cb0840ce8e13901fe71a9a8e834a531802ffc4 (diff)
Btrfs: cleanup how we setup free space clusters
This patch makes the free space cluster refilling code a little easier to understand, and fixes some things with the bitmap part of it. Currently we either want to refill a cluster with 1) All normal extent entries (those without bitmaps) 2) A bitmap entry with enough space The current code has this ugly jump around logic that will first try and fill up the cluster with extent entries and then if it can't do that it will try and find a bitmap to use. So instead split this out into two functions, one that tries to find only normal entries, and one that tries to find bitmaps. This also fixes a suboptimal thing we would do with bitmaps. If we used a bitmap we would just tell the cluster that we were pointing at a bitmap and it would do the tree search in the block group for that entry every time we tried to make an allocation. Instead of doing that now we just add it to the clusters group. I tested this with my ENOSPC tests and xfstests and it survived. Signed-off-by: Josef Bacik <josef@redhat.com>
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/ctree.h3
-rw-r--r--fs/btrfs/free-space-cache.c364
2 files changed, 182 insertions, 185 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 6036fdb88c53..0ee679b6c1b7 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -783,9 +783,6 @@ struct btrfs_free_cluster {
783 /* first extent starting offset */ 783 /* first extent starting offset */
784 u64 window_start; 784 u64 window_start;
785 785
786 /* if this cluster simply points at a bitmap in the block group */
787 bool points_to_bitmap;
788
789 struct btrfs_block_group_cache *block_group; 786 struct btrfs_block_group_cache *block_group;
790 /* 787 /*
791 * when a cluster is allocated from a block group, we put the 788 * when a cluster is allocated from a block group, we put the
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 4ab35ea0443f..f03ef97c3b21 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -1644,30 +1644,28 @@ __btrfs_return_cluster_to_free_space(
1644{ 1644{
1645 struct btrfs_free_space *entry; 1645 struct btrfs_free_space *entry;
1646 struct rb_node *node; 1646 struct rb_node *node;
1647 bool bitmap;
1648 1647
1649 spin_lock(&cluster->lock); 1648 spin_lock(&cluster->lock);
1650 if (cluster->block_group != block_group) 1649 if (cluster->block_group != block_group)
1651 goto out; 1650 goto out;
1652 1651
1653 bitmap = cluster->points_to_bitmap;
1654 cluster->block_group = NULL; 1652 cluster->block_group = NULL;
1655 cluster->window_start = 0; 1653 cluster->window_start = 0;
1656 list_del_init(&cluster->block_group_list); 1654 list_del_init(&cluster->block_group_list);
1657 cluster->points_to_bitmap = false;
1658
1659 if (bitmap)
1660 goto out;
1661 1655
1662 node = rb_first(&cluster->root); 1656 node = rb_first(&cluster->root);
1663 while (node) { 1657 while (node) {
1658 bool bitmap;
1659
1664 entry = rb_entry(node, struct btrfs_free_space, offset_index); 1660 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1665 node = rb_next(&entry->offset_index); 1661 node = rb_next(&entry->offset_index);
1666 rb_erase(&entry->offset_index, &cluster->root); 1662 rb_erase(&entry->offset_index, &cluster->root);
1667 BUG_ON(entry->bitmap); 1663
1668 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);
1669 tree_insert_offset(&block_group->free_space_offset, 1667 tree_insert_offset(&block_group->free_space_offset,
1670 entry->offset, &entry->offset_index, 0); 1668 entry->offset, &entry->offset_index, bitmap);
1671 } 1669 }
1672 cluster->root = RB_ROOT; 1670 cluster->root = RB_ROOT;
1673 1671
@@ -1790,50 +1788,24 @@ int btrfs_return_cluster_to_free_space(
1790 1788
1791static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, 1789static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1792 struct btrfs_free_cluster *cluster, 1790 struct btrfs_free_cluster *cluster,
1791 struct btrfs_free_space *entry,
1793 u64 bytes, u64 min_start) 1792 u64 bytes, u64 min_start)
1794{ 1793{
1795 struct btrfs_free_space *entry;
1796 int err; 1794 int err;
1797 u64 search_start = cluster->window_start; 1795 u64 search_start = cluster->window_start;
1798 u64 search_bytes = bytes; 1796 u64 search_bytes = bytes;
1799 u64 ret = 0; 1797 u64 ret = 0;
1800 1798
1801 spin_lock(&block_group->tree_lock);
1802 spin_lock(&cluster->lock);
1803
1804 if (!cluster->points_to_bitmap)
1805 goto out;
1806
1807 if (cluster->block_group != block_group)
1808 goto out;
1809
1810 /*
1811 * search_start is the beginning of the bitmap, but at some point it may
1812 * be a good idea to point to the actual start of the free area in the
1813 * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
1814 * to 1 to make sure we get the bitmap entry
1815 */
1816 entry = tree_search_offset(block_group,
1817 offset_to_bitmap(block_group, search_start),
1818 1, 0);
1819 if (!entry || !entry->bitmap)
1820 goto out;
1821
1822 search_start = min_start; 1799 search_start = min_start;
1823 search_bytes = bytes; 1800 search_bytes = bytes;
1824 1801
1825 err = search_bitmap(block_group, entry, &search_start, 1802 err = search_bitmap(block_group, entry, &search_start,
1826 &search_bytes); 1803 &search_bytes);
1827 if (err) 1804 if (err)
1828 goto out; 1805 return 0;
1829 1806
1830 ret = search_start; 1807 ret = search_start;
1831 bitmap_clear_bits(block_group, entry, ret, bytes); 1808 bitmap_clear_bits(block_group, entry, ret, bytes);
1832 if (entry->bytes == 0)
1833 free_bitmap(block_group, entry);
1834out:
1835 spin_unlock(&cluster->lock);
1836 spin_unlock(&block_group->tree_lock);
1837 1809
1838 return ret; 1810 return ret;
1839} 1811}
@@ -1851,10 +1823,6 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1851 struct rb_node *node; 1823 struct rb_node *node;
1852 u64 ret = 0; 1824 u64 ret = 0;
1853 1825
1854 if (cluster->points_to_bitmap)
1855 return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
1856 min_start);
1857
1858 spin_lock(&cluster->lock); 1826 spin_lock(&cluster->lock);
1859 if (bytes > cluster->max_size) 1827 if (bytes > cluster->max_size)
1860 goto out; 1828 goto out;
@@ -1867,9 +1835,9 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1867 goto out; 1835 goto out;
1868 1836
1869 entry = rb_entry(node, struct btrfs_free_space, offset_index); 1837 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1870
1871 while(1) { 1838 while(1) {
1872 if (entry->bytes < bytes || entry->offset < min_start) { 1839 if (entry->bytes < bytes ||
1840 (!entry->bitmap && entry->offset < min_start)) {
1873 struct rb_node *node; 1841 struct rb_node *node;
1874 1842
1875 node = rb_next(&entry->offset_index); 1843 node = rb_next(&entry->offset_index);
@@ -1879,10 +1847,27 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1879 offset_index); 1847 offset_index);
1880 continue; 1848 continue;
1881 } 1849 }
1882 ret = entry->offset;
1883 1850
1884 entry->offset += bytes; 1851 if (entry->bitmap) {
1885 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 }
1886 1871
1887 if (entry->bytes == 0) 1872 if (entry->bytes == 0)
1888 rb_erase(&entry->offset_index, &cluster->root); 1873 rb_erase(&entry->offset_index, &cluster->root);
@@ -1899,6 +1884,11 @@ out:
1899 block_group->free_space -= bytes; 1884 block_group->free_space -= bytes;
1900 if (entry->bytes == 0) { 1885 if (entry->bytes == 0) {
1901 block_group->free_extents--; 1886 block_group->free_extents--;
1887 if (entry->bitmap) {
1888 kfree(entry->bitmap);
1889 block_group->total_bitmaps--;
1890 recalculate_thresholds(block_group);
1891 }
1902 kmem_cache_free(btrfs_free_space_cachep, entry); 1892 kmem_cache_free(btrfs_free_space_cachep, entry);
1903 } 1893 }
1904 1894
@@ -1919,6 +1909,7 @@ static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1919 unsigned long found_bits; 1909 unsigned long found_bits;
1920 unsigned long start = 0; 1910 unsigned long start = 0;
1921 unsigned long total_found = 0; 1911 unsigned long total_found = 0;
1912 int ret;
1922 bool found = false; 1913 bool found = false;
1923 1914
1924 i = offset_to_bit(entry->offset, block_group->sectorsize, 1915 i = offset_to_bit(entry->offset, block_group->sectorsize,
@@ -1941,7 +1932,7 @@ again:
1941 } 1932 }
1942 1933
1943 if (!found_bits) 1934 if (!found_bits)
1944 return -1; 1935 return -ENOSPC;
1945 1936
1946 if (!found) { 1937 if (!found) {
1947 start = i; 1938 start = i;
@@ -1965,12 +1956,145 @@ again:
1965 1956
1966 cluster->window_start = start * block_group->sectorsize + 1957 cluster->window_start = start * block_group->sectorsize +
1967 entry->offset; 1958 entry->offset;
1968 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);
1969 1963
1970 return 0; 1964 return 0;
1971} 1965}
1972 1966
1973/* 1967/*
1968 * This searches the block group for just extents to fill the cluster with.
1969 */
1970static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
1971 struct btrfs_free_cluster *cluster,
1972 u64 offset, u64 bytes, u64 min_bytes)
1973{
1974 struct btrfs_free_space *first = NULL;
1975 struct btrfs_free_space *entry = NULL;
1976 struct btrfs_free_space *prev = NULL;
1977 struct btrfs_free_space *last;
1978 struct rb_node *node;
1979 u64 window_start;
1980 u64 window_free;
1981 u64 max_extent;
1982 u64 max_gap = 128 * 1024;
1983
1984 entry = tree_search_offset(block_group, offset, 0, 1);
1985 if (!entry)
1986 return -ENOSPC;
1987
1988 /*
1989 * We don't want bitmaps, so just move along until we find a normal
1990 * extent entry.
1991 */
1992 while (entry->bitmap) {
1993 node = rb_next(&entry->offset_index);
1994 if (!node)
1995 return -ENOSPC;
1996 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1997 }
1998
1999 window_start = entry->offset;
2000 window_free = entry->bytes;
2001 max_extent = entry->bytes;
2002 first = entry;
2003 last = entry;
2004 prev = entry;
2005
2006 while (window_free <= min_bytes) {
2007 node = rb_next(&entry->offset_index);
2008 if (!node)
2009 return -ENOSPC;
2010 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2011
2012 if (entry->bitmap)
2013 continue;
2014 /*
2015 * we haven't filled the empty size and the window is
2016 * very large. reset and try again
2017 */
2018 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2019 entry->offset - window_start > (min_bytes * 2)) {
2020 first = entry;
2021 window_start = entry->offset;
2022 window_free = entry->bytes;
2023 last = entry;
2024 max_extent = entry->bytes;
2025 } else {
2026 last = entry;
2027 window_free += entry->bytes;
2028 if (entry->bytes > max_extent)
2029 max_extent = entry->bytes;
2030 }
2031 prev = entry;
2032 }
2033
2034 cluster->window_start = first->offset;
2035
2036 node = &first->offset_index;
2037
2038 /*
2039 * now we've found our entries, pull them out of the free space
2040 * cache and put them into the cluster rbtree
2041 */
2042 do {
2043 int ret;
2044
2045 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2046 node = rb_next(&entry->offset_index);
2047 if (entry->bitmap)
2048 continue;
2049
2050 rb_erase(&entry->offset_index, &block_group->free_space_offset);
2051 ret = tree_insert_offset(&cluster->root, entry->offset,
2052 &entry->offset_index, 0);
2053 BUG_ON(ret);
2054 } while (node && entry != last);
2055
2056 cluster->max_size = max_extent;
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 */
2065static 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;
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 {
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/*
1974 * here we try to find a cluster of blocks in a block group. The goal 2098 * here we try to find a cluster of blocks in a block group. The goal
1975 * is to find at least bytes free and up to empty_size + bytes free. 2099 * is to find at least bytes free and up to empty_size + bytes free.
1976 * We might not find them all in one contiguous area. 2100 * We might not find them all in one contiguous area.
@@ -1984,15 +2108,7 @@ int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
1984 struct btrfs_free_cluster *cluster, 2108 struct btrfs_free_cluster *cluster,
1985 u64 offset, u64 bytes, u64 empty_size) 2109 u64 offset, u64 bytes, u64 empty_size)
1986{ 2110{
1987 struct btrfs_free_space *entry = NULL;
1988 struct rb_node *node;
1989 struct btrfs_free_space *next;
1990 struct btrfs_free_space *last = NULL;
1991 u64 min_bytes; 2111 u64 min_bytes;
1992 u64 window_start;
1993 u64 window_free;
1994 u64 max_extent = 0;
1995 bool found_bitmap = false;
1996 int ret; 2112 int ret;
1997 2113
1998 /* for metadata, allow allocates with more holes */ 2114 /* for metadata, allow allocates with more holes */
@@ -2029,134 +2145,19 @@ int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2029 ret = 0; 2145 ret = 0;
2030 goto out; 2146 goto out;
2031 } 2147 }
2032again:
2033 entry = tree_search_offset(block_group, offset, found_bitmap, 1);
2034 if (!entry) {
2035 ret = -ENOSPC;
2036 goto out;
2037 }
2038
2039 /*
2040 * If found_bitmap is true, we exhausted our search for extent entries,
2041 * and we just want to search all of the bitmaps that we can find, and
2042 * ignore any extent entries we find.
2043 */
2044 while (entry->bitmap || found_bitmap ||
2045 (!entry->bitmap && entry->bytes < min_bytes)) {
2046 struct rb_node *node = rb_next(&entry->offset_index);
2047
2048 if (entry->bitmap && entry->bytes > bytes + empty_size) {
2049 ret = btrfs_bitmap_cluster(block_group, entry, cluster,
2050 offset, bytes, min_bytes);
2051 if (!ret)
2052 goto got_it;
2053 }
2054
2055 if (!node) {
2056 ret = -ENOSPC;
2057 goto out;
2058 }
2059 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2060 }
2061
2062 /*
2063 * We already searched all the extent entries from the passed in offset
2064 * to the end and didn't find enough space for the cluster, and we also
2065 * didn't find any bitmaps that met our criteria, just go ahead and exit
2066 */
2067 if (found_bitmap) {
2068 ret = -ENOSPC;
2069 goto out;
2070 }
2071
2072 cluster->points_to_bitmap = false;
2073 window_start = entry->offset;
2074 window_free = entry->bytes;
2075 last = entry;
2076 max_extent = entry->bytes;
2077
2078 while (1) {
2079 /* out window is just right, lets fill it */
2080 if (window_free >= min_bytes)
2081 break;
2082
2083 node = rb_next(&last->offset_index);
2084 if (!node) {
2085 if (found_bitmap)
2086 goto again;
2087 ret = -ENOSPC;
2088 goto out;
2089 }
2090 next = rb_entry(node, struct btrfs_free_space, offset_index);
2091
2092 /*
2093 * we found a bitmap, so if this search doesn't result in a
2094 * cluster, we know to go and search again for the bitmaps and
2095 * start looking for space there
2096 */
2097 if (next->bitmap) {
2098 if (!found_bitmap)
2099 offset = next->offset;
2100 found_bitmap = true;
2101 last = next;
2102 continue;
2103 }
2104
2105 /*
2106 * we haven't filled the empty size and the window is
2107 * very large. reset and try again
2108 */
2109 if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
2110 next->offset - window_start > (bytes + empty_size) * 2) {
2111 entry = next;
2112 window_start = entry->offset;
2113 window_free = entry->bytes;
2114 last = entry;
2115 max_extent = entry->bytes;
2116 } else {
2117 last = next;
2118 window_free += next->bytes;
2119 if (entry->bytes > max_extent)
2120 max_extent = entry->bytes;
2121 }
2122 }
2123
2124 cluster->window_start = entry->offset;
2125
2126 /*
2127 * now we've found our entries, pull them out of the free space
2128 * cache and put them into the cluster rbtree
2129 *
2130 * The cluster includes an rbtree, but only uses the offset index
2131 * of each free space cache entry.
2132 */
2133 while (1) {
2134 node = rb_next(&entry->offset_index);
2135 if (entry->bitmap && node) {
2136 entry = rb_entry(node, struct btrfs_free_space,
2137 offset_index);
2138 continue;
2139 } else if (entry->bitmap && !node) {
2140 break;
2141 }
2142
2143 rb_erase(&entry->offset_index, &block_group->free_space_offset);
2144 ret = tree_insert_offset(&cluster->root, entry->offset,
2145 &entry->offset_index, 0);
2146 BUG_ON(ret);
2147 2148
2148 if (!node || entry == last) 2149 ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes,
2149 break; 2150 min_bytes);
2151 if (ret)
2152 ret = setup_cluster_bitmap(block_group, cluster, offset,
2153 bytes, min_bytes);
2150 2154
2151 entry = rb_entry(node, struct btrfs_free_space, offset_index); 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;
2152 } 2160 }
2153
2154 cluster->max_size = max_extent;
2155got_it:
2156 ret = 0;
2157 atomic_inc(&block_group->count);
2158 list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
2159 cluster->block_group = block_group;
2160out: 2161out:
2161 spin_unlock(&cluster->lock); 2162 spin_unlock(&cluster->lock);
2162 spin_unlock(&block_group->tree_lock); 2163 spin_unlock(&block_group->tree_lock);
@@ -2173,7 +2174,6 @@ void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2173 spin_lock_init(&cluster->refill_lock); 2174 spin_lock_init(&cluster->refill_lock);
2174 cluster->root = RB_ROOT; 2175 cluster->root = RB_ROOT;
2175 cluster->max_size = 0; 2176 cluster->max_size = 0;
2176 cluster->points_to_bitmap = false;
2177 INIT_LIST_HEAD(&cluster->block_group_list); 2177 INIT_LIST_HEAD(&cluster->block_group_list);
2178 cluster->block_group = NULL; 2178 cluster->block_group = NULL;
2179} 2179}