diff options
Diffstat (limited to 'fs/btrfs/extent-tree.c')
| -rw-r--r-- | fs/btrfs/extent-tree.c | 252 | 
1 files changed, 237 insertions, 15 deletions
| diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 0a5d796c9f7e..6b5966aacf44 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
| @@ -60,6 +60,10 @@ static int update_block_group(struct btrfs_trans_handle *trans, | |||
| 60 | u64 bytenr, u64 num_bytes, int alloc, | 60 | u64 bytenr, u64 num_bytes, int alloc, | 
| 61 | int mark_free); | 61 | int mark_free); | 
| 62 | 62 | ||
| 63 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, | ||
| 64 | struct btrfs_root *extent_root, u64 alloc_bytes, | ||
| 65 | u64 flags, int force); | ||
| 66 | |||
| 63 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) | 67 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) | 
| 64 | { | 68 | { | 
| 65 | return (cache->flags & bits) == bits; | 69 | return (cache->flags & bits) == bits; | 
| @@ -1909,6 +1913,7 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags, | |||
| 1909 | found->bytes_pinned = 0; | 1913 | found->bytes_pinned = 0; | 
| 1910 | found->bytes_reserved = 0; | 1914 | found->bytes_reserved = 0; | 
| 1911 | found->bytes_readonly = 0; | 1915 | found->bytes_readonly = 0; | 
| 1916 | found->bytes_delalloc = 0; | ||
| 1912 | found->full = 0; | 1917 | found->full = 0; | 
| 1913 | found->force_alloc = 0; | 1918 | found->force_alloc = 0; | 
| 1914 | *space_info = found; | 1919 | *space_info = found; | 
| @@ -1972,6 +1977,233 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) | |||
| 1972 | return flags; | 1977 | return flags; | 
| 1973 | } | 1978 | } | 
| 1974 | 1979 | ||
| 1980 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) | ||
| 1981 | { | ||
| 1982 | struct btrfs_fs_info *info = root->fs_info; | ||
| 1983 | u64 alloc_profile; | ||
| 1984 | |||
| 1985 | if (data) { | ||
| 1986 | alloc_profile = info->avail_data_alloc_bits & | ||
| 1987 | info->data_alloc_profile; | ||
| 1988 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; | ||
| 1989 | } else if (root == root->fs_info->chunk_root) { | ||
| 1990 | alloc_profile = info->avail_system_alloc_bits & | ||
| 1991 | info->system_alloc_profile; | ||
| 1992 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; | ||
| 1993 | } else { | ||
| 1994 | alloc_profile = info->avail_metadata_alloc_bits & | ||
| 1995 | info->metadata_alloc_profile; | ||
| 1996 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; | ||
| 1997 | } | ||
| 1998 | |||
| 1999 | return btrfs_reduce_alloc_profile(root, data); | ||
| 2000 | } | ||
| 2001 | |||
| 2002 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) | ||
| 2003 | { | ||
| 2004 | u64 alloc_target; | ||
| 2005 | |||
| 2006 | alloc_target = btrfs_get_alloc_profile(root, 1); | ||
| 2007 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, | ||
| 2008 | alloc_target); | ||
| 2009 | } | ||
| 2010 | |||
| 2011 | /* | ||
| 2012 | * for now this just makes sure we have at least 5% of our metadata space free | ||
| 2013 | * for use. | ||
| 2014 | */ | ||
| 2015 | int btrfs_check_metadata_free_space(struct btrfs_root *root) | ||
| 2016 | { | ||
| 2017 | struct btrfs_fs_info *info = root->fs_info; | ||
| 2018 | struct btrfs_space_info *meta_sinfo; | ||
| 2019 | u64 alloc_target, thresh; | ||
| 2020 | int committed = 0, ret; | ||
| 2021 | |||
| 2022 | /* get the space info for where the metadata will live */ | ||
| 2023 | alloc_target = btrfs_get_alloc_profile(root, 0); | ||
| 2024 | meta_sinfo = __find_space_info(info, alloc_target); | ||
| 2025 | |||
| 2026 | again: | ||
| 2027 | spin_lock(&meta_sinfo->lock); | ||
| 2028 | if (!meta_sinfo->full) | ||
| 2029 | thresh = meta_sinfo->total_bytes * 80; | ||
| 2030 | else | ||
| 2031 | thresh = meta_sinfo->total_bytes * 95; | ||
| 2032 | |||
| 2033 | do_div(thresh, 100); | ||
| 2034 | |||
| 2035 | if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + | ||
| 2036 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) { | ||
| 2037 | struct btrfs_trans_handle *trans; | ||
| 2038 | if (!meta_sinfo->full) { | ||
| 2039 | meta_sinfo->force_alloc = 1; | ||
| 2040 | spin_unlock(&meta_sinfo->lock); | ||
| 2041 | |||
| 2042 | trans = btrfs_start_transaction(root, 1); | ||
| 2043 | if (!trans) | ||
| 2044 | return -ENOMEM; | ||
| 2045 | |||
| 2046 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, | ||
| 2047 | 2 * 1024 * 1024, alloc_target, 0); | ||
| 2048 | btrfs_end_transaction(trans, root); | ||
| 2049 | goto again; | ||
| 2050 | } | ||
| 2051 | spin_unlock(&meta_sinfo->lock); | ||
| 2052 | |||
| 2053 | if (!committed) { | ||
| 2054 | committed = 1; | ||
| 2055 | trans = btrfs_join_transaction(root, 1); | ||
| 2056 | if (!trans) | ||
| 2057 | return -ENOMEM; | ||
| 2058 | ret = btrfs_commit_transaction(trans, root); | ||
| 2059 | if (ret) | ||
| 2060 | return ret; | ||
| 2061 | goto again; | ||
| 2062 | } | ||
| 2063 | return -ENOSPC; | ||
| 2064 | } | ||
| 2065 | spin_unlock(&meta_sinfo->lock); | ||
| 2066 | |||
| 2067 | return 0; | ||
| 2068 | } | ||
| 2069 | |||
| 2070 | /* | ||
| 2071 | * This will check the space that the inode allocates from to make sure we have | ||
| 2072 | * enough space for bytes. | ||
| 2073 | */ | ||
| 2074 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, | ||
| 2075 | u64 bytes) | ||
| 2076 | { | ||
| 2077 | struct btrfs_space_info *data_sinfo; | ||
| 2078 | int ret = 0, committed = 0; | ||
| 2079 | |||
| 2080 | /* make sure bytes are sectorsize aligned */ | ||
| 2081 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); | ||
| 2082 | |||
| 2083 | data_sinfo = BTRFS_I(inode)->space_info; | ||
| 2084 | again: | ||
| 2085 | /* make sure we have enough space to handle the data first */ | ||
| 2086 | spin_lock(&data_sinfo->lock); | ||
| 2087 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - | ||
| 2088 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - | ||
| 2089 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - | ||
| 2090 | data_sinfo->bytes_may_use < bytes) { | ||
| 2091 | struct btrfs_trans_handle *trans; | ||
| 2092 | |||
| 2093 | /* | ||
| 2094 | * if we don't have enough free bytes in this space then we need | ||
| 2095 | * to alloc a new chunk. | ||
| 2096 | */ | ||
| 2097 | if (!data_sinfo->full) { | ||
| 2098 | u64 alloc_target; | ||
| 2099 | |||
| 2100 | data_sinfo->force_alloc = 1; | ||
| 2101 | spin_unlock(&data_sinfo->lock); | ||
| 2102 | |||
| 2103 | alloc_target = btrfs_get_alloc_profile(root, 1); | ||
| 2104 | trans = btrfs_start_transaction(root, 1); | ||
| 2105 | if (!trans) | ||
| 2106 | return -ENOMEM; | ||
| 2107 | |||
| 2108 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, | ||
| 2109 | bytes + 2 * 1024 * 1024, | ||
| 2110 | alloc_target, 0); | ||
| 2111 | btrfs_end_transaction(trans, root); | ||
| 2112 | if (ret) | ||
| 2113 | return ret; | ||
| 2114 | goto again; | ||
| 2115 | } | ||
| 2116 | spin_unlock(&data_sinfo->lock); | ||
| 2117 | |||
| 2118 | /* commit the current transaction and try again */ | ||
| 2119 | if (!committed) { | ||
| 2120 | committed = 1; | ||
| 2121 | trans = btrfs_join_transaction(root, 1); | ||
| 2122 | if (!trans) | ||
| 2123 | return -ENOMEM; | ||
| 2124 | ret = btrfs_commit_transaction(trans, root); | ||
| 2125 | if (ret) | ||
| 2126 | return ret; | ||
| 2127 | goto again; | ||
| 2128 | } | ||
| 2129 | |||
| 2130 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" | ||
| 2131 | ", %llu bytes_used, %llu bytes_reserved, " | ||
| 2132 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use" | ||
| 2133 | "%llu total\n", bytes, data_sinfo->bytes_delalloc, | ||
| 2134 | data_sinfo->bytes_used, data_sinfo->bytes_reserved, | ||
| 2135 | data_sinfo->bytes_pinned, data_sinfo->bytes_readonly, | ||
| 2136 | data_sinfo->bytes_may_use, data_sinfo->total_bytes); | ||
| 2137 | return -ENOSPC; | ||
| 2138 | } | ||
| 2139 | data_sinfo->bytes_may_use += bytes; | ||
| 2140 | BTRFS_I(inode)->reserved_bytes += bytes; | ||
| 2141 | spin_unlock(&data_sinfo->lock); | ||
| 2142 | |||
| 2143 | return btrfs_check_metadata_free_space(root); | ||
| 2144 | } | ||
| 2145 | |||
| 2146 | /* | ||
| 2147 | * if there was an error for whatever reason after calling | ||
| 2148 | * btrfs_check_data_free_space, call this so we can cleanup the counters. | ||
| 2149 | */ | ||
| 2150 | void btrfs_free_reserved_data_space(struct btrfs_root *root, | ||
| 2151 | struct inode *inode, u64 bytes) | ||
| 2152 | { | ||
| 2153 | struct btrfs_space_info *data_sinfo; | ||
| 2154 | |||
| 2155 | /* make sure bytes are sectorsize aligned */ | ||
| 2156 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); | ||
| 2157 | |||
| 2158 | data_sinfo = BTRFS_I(inode)->space_info; | ||
| 2159 | spin_lock(&data_sinfo->lock); | ||
| 2160 | data_sinfo->bytes_may_use -= bytes; | ||
| 2161 | BTRFS_I(inode)->reserved_bytes -= bytes; | ||
| 2162 | spin_unlock(&data_sinfo->lock); | ||
| 2163 | } | ||
| 2164 | |||
| 2165 | /* called when we are adding a delalloc extent to the inode's io_tree */ | ||
| 2166 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, | ||
| 2167 | u64 bytes) | ||
| 2168 | { | ||
| 2169 | struct btrfs_space_info *data_sinfo; | ||
| 2170 | |||
| 2171 | /* get the space info for where this inode will be storing its data */ | ||
| 2172 | data_sinfo = BTRFS_I(inode)->space_info; | ||
| 2173 | |||
| 2174 | /* make sure we have enough space to handle the data first */ | ||
| 2175 | spin_lock(&data_sinfo->lock); | ||
| 2176 | data_sinfo->bytes_delalloc += bytes; | ||
| 2177 | |||
| 2178 | /* | ||
| 2179 | * we are adding a delalloc extent without calling | ||
| 2180 | * btrfs_check_data_free_space first. This happens on a weird | ||
| 2181 | * writepage condition, but shouldn't hurt our accounting | ||
| 2182 | */ | ||
| 2183 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { | ||
| 2184 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; | ||
| 2185 | BTRFS_I(inode)->reserved_bytes = 0; | ||
| 2186 | } else { | ||
| 2187 | data_sinfo->bytes_may_use -= bytes; | ||
| 2188 | BTRFS_I(inode)->reserved_bytes -= bytes; | ||
| 2189 | } | ||
| 2190 | |||
| 2191 | spin_unlock(&data_sinfo->lock); | ||
| 2192 | } | ||
| 2193 | |||
| 2194 | /* called when we are clearing an delalloc extent from the inode's io_tree */ | ||
| 2195 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, | ||
| 2196 | u64 bytes) | ||
| 2197 | { | ||
| 2198 | struct btrfs_space_info *info; | ||
| 2199 | |||
| 2200 | info = BTRFS_I(inode)->space_info; | ||
| 2201 | |||
| 2202 | spin_lock(&info->lock); | ||
| 2203 | info->bytes_delalloc -= bytes; | ||
| 2204 | spin_unlock(&info->lock); | ||
| 2205 | } | ||
| 2206 | |||
| 1975 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, | 2207 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, | 
| 1976 | struct btrfs_root *extent_root, u64 alloc_bytes, | 2208 | struct btrfs_root *extent_root, u64 alloc_bytes, | 
| 1977 | u64 flags, int force) | 2209 | u64 flags, int force) | 
| @@ -3105,6 +3337,10 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes) | |||
| 3105 | (unsigned long long)(info->total_bytes - info->bytes_used - | 3337 | (unsigned long long)(info->total_bytes - info->bytes_used - | 
| 3106 | info->bytes_pinned - info->bytes_reserved), | 3338 | info->bytes_pinned - info->bytes_reserved), | 
| 3107 | (info->full) ? "" : "not "); | 3339 | (info->full) ? "" : "not "); | 
| 3340 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," | ||
| 3341 | " may_use=%llu, used=%llu\n", info->total_bytes, | ||
| 3342 | info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use, | ||
| 3343 | info->bytes_used); | ||
| 3108 | 3344 | ||
| 3109 | down_read(&info->groups_sem); | 3345 | down_read(&info->groups_sem); | 
| 3110 | list_for_each_entry(cache, &info->block_groups, list) { | 3346 | list_for_each_entry(cache, &info->block_groups, list) { | 
| @@ -3131,24 +3367,10 @@ static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, | |||
| 3131 | { | 3367 | { | 
| 3132 | int ret; | 3368 | int ret; | 
| 3133 | u64 search_start = 0; | 3369 | u64 search_start = 0; | 
| 3134 | u64 alloc_profile; | ||
| 3135 | struct btrfs_fs_info *info = root->fs_info; | 3370 | struct btrfs_fs_info *info = root->fs_info; | 
| 3136 | 3371 | ||
| 3137 | if (data) { | 3372 | data = btrfs_get_alloc_profile(root, data); | 
| 3138 | alloc_profile = info->avail_data_alloc_bits & | ||
| 3139 | info->data_alloc_profile; | ||
| 3140 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; | ||
| 3141 | } else if (root == root->fs_info->chunk_root) { | ||
| 3142 | alloc_profile = info->avail_system_alloc_bits & | ||
| 3143 | info->system_alloc_profile; | ||
| 3144 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; | ||
| 3145 | } else { | ||
| 3146 | alloc_profile = info->avail_metadata_alloc_bits & | ||
| 3147 | info->metadata_alloc_profile; | ||
| 3148 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; | ||
| 3149 | } | ||
| 3150 | again: | 3373 | again: | 
| 3151 | data = btrfs_reduce_alloc_profile(root, data); | ||
| 3152 | /* | 3374 | /* | 
| 3153 | * the only place that sets empty_size is btrfs_realloc_node, which | 3375 | * the only place that sets empty_size is btrfs_realloc_node, which | 
| 3154 | * is not called recursively on allocations | 3376 | * is not called recursively on allocations | 
