aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/free-space-cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/free-space-cache.c')
-rw-r--r--fs/btrfs/free-space-cache.c172
1 files changed, 136 insertions, 36 deletions
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index ad144736a5fd..bf0d61567f3d 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -250,7 +250,7 @@ int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
250 pgoff_t index = 0; 250 pgoff_t index = 0;
251 unsigned long first_page_offset; 251 unsigned long first_page_offset;
252 int num_checksums; 252 int num_checksums;
253 int ret = 0, ret2; 253 int ret = 0;
254 254
255 INIT_LIST_HEAD(&bitmaps); 255 INIT_LIST_HEAD(&bitmaps);
256 256
@@ -421,11 +421,10 @@ int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
421 goto free_cache; 421 goto free_cache;
422 } 422 }
423 spin_lock(&ctl->tree_lock); 423 spin_lock(&ctl->tree_lock);
424 ret2 = link_free_space(ctl, e); 424 ret = link_free_space(ctl, e);
425 ctl->total_bitmaps++; 425 ctl->total_bitmaps++;
426 ctl->op->recalc_thresholds(ctl); 426 ctl->op->recalc_thresholds(ctl);
427 spin_unlock(&ctl->tree_lock); 427 spin_unlock(&ctl->tree_lock);
428 list_add_tail(&e->list, &bitmaps);
429 if (ret) { 428 if (ret) {
430 printk(KERN_ERR "Duplicate entries in " 429 printk(KERN_ERR "Duplicate entries in "
431 "free space cache, dumping\n"); 430 "free space cache, dumping\n");
@@ -434,6 +433,7 @@ int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
434 page_cache_release(page); 433 page_cache_release(page);
435 goto free_cache; 434 goto free_cache;
436 } 435 }
436 list_add_tail(&e->list, &bitmaps);
437 } 437 }
438 438
439 num_entries--; 439 num_entries--;
@@ -1417,6 +1417,23 @@ again:
1417 return 0; 1417 return 0;
1418} 1418}
1419 1419
1420static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1421 struct btrfs_free_space *info, u64 offset,
1422 u64 bytes)
1423{
1424 u64 bytes_to_set = 0;
1425 u64 end;
1426
1427 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1428
1429 bytes_to_set = min(end - offset, bytes);
1430
1431 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1432
1433 return bytes_to_set;
1434
1435}
1436
1420static bool use_bitmap(struct btrfs_free_space_ctl *ctl, 1437static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1421 struct btrfs_free_space *info) 1438 struct btrfs_free_space *info)
1422{ 1439{
@@ -1453,12 +1470,18 @@ static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1453 return true; 1470 return true;
1454} 1471}
1455 1472
1473static struct btrfs_free_space_op free_space_op = {
1474 .recalc_thresholds = recalculate_thresholds,
1475 .use_bitmap = use_bitmap,
1476};
1477
1456static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, 1478static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1457 struct btrfs_free_space *info) 1479 struct btrfs_free_space *info)
1458{ 1480{
1459 struct btrfs_free_space *bitmap_info; 1481 struct btrfs_free_space *bitmap_info;
1482 struct btrfs_block_group_cache *block_group = NULL;
1460 int added = 0; 1483 int added = 0;
1461 u64 bytes, offset, end; 1484 u64 bytes, offset, bytes_added;
1462 int ret; 1485 int ret;
1463 1486
1464 bytes = info->bytes; 1487 bytes = info->bytes;
@@ -1467,7 +1490,49 @@ static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1467 if (!ctl->op->use_bitmap(ctl, info)) 1490 if (!ctl->op->use_bitmap(ctl, info))
1468 return 0; 1491 return 0;
1469 1492
1493 if (ctl->op == &free_space_op)
1494 block_group = ctl->private;
1470again: 1495again:
1496 /*
1497 * Since we link bitmaps right into the cluster we need to see if we
1498 * have a cluster here, and if so and it has our bitmap we need to add
1499 * the free space to that bitmap.
1500 */
1501 if (block_group && !list_empty(&block_group->cluster_list)) {
1502 struct btrfs_free_cluster *cluster;
1503 struct rb_node *node;
1504 struct btrfs_free_space *entry;
1505
1506 cluster = list_entry(block_group->cluster_list.next,
1507 struct btrfs_free_cluster,
1508 block_group_list);
1509 spin_lock(&cluster->lock);
1510 node = rb_first(&cluster->root);
1511 if (!node) {
1512 spin_unlock(&cluster->lock);
1513 goto no_cluster_bitmap;
1514 }
1515
1516 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1517 if (!entry->bitmap) {
1518 spin_unlock(&cluster->lock);
1519 goto no_cluster_bitmap;
1520 }
1521
1522 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1523 bytes_added = add_bytes_to_bitmap(ctl, entry,
1524 offset, bytes);
1525 bytes -= bytes_added;
1526 offset += bytes_added;
1527 }
1528 spin_unlock(&cluster->lock);
1529 if (!bytes) {
1530 ret = 1;
1531 goto out;
1532 }
1533 }
1534
1535no_cluster_bitmap:
1471 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 1536 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1472 1, 0); 1537 1, 0);
1473 if (!bitmap_info) { 1538 if (!bitmap_info) {
@@ -1475,19 +1540,10 @@ again:
1475 goto new_bitmap; 1540 goto new_bitmap;
1476 } 1541 }
1477 1542
1478 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); 1543 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1479 1544 bytes -= bytes_added;
1480 if (offset >= bitmap_info->offset && offset + bytes > end) { 1545 offset += bytes_added;
1481 bitmap_set_bits(ctl, bitmap_info, offset, end - offset); 1546 added = 0;
1482 bytes -= end - offset;
1483 offset = end;
1484 added = 0;
1485 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
1486 bitmap_set_bits(ctl, bitmap_info, offset, bytes);
1487 bytes = 0;
1488 } else {
1489 BUG();
1490 }
1491 1547
1492 if (!bytes) { 1548 if (!bytes) {
1493 ret = 1; 1549 ret = 1;
@@ -1766,11 +1822,6 @@ void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1766 "\n", count); 1822 "\n", count);
1767} 1823}
1768 1824
1769static struct btrfs_free_space_op free_space_op = {
1770 .recalc_thresholds = recalculate_thresholds,
1771 .use_bitmap = use_bitmap,
1772};
1773
1774void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group) 1825void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
1775{ 1826{
1776 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 1827 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
@@ -1842,9 +1893,12 @@ void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
1842 1893
1843 while ((node = rb_last(&ctl->free_space_offset)) != NULL) { 1894 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1844 info = rb_entry(node, struct btrfs_free_space, offset_index); 1895 info = rb_entry(node, struct btrfs_free_space, offset_index);
1845 unlink_free_space(ctl, info); 1896 if (!info->bitmap) {
1846 kfree(info->bitmap); 1897 unlink_free_space(ctl, info);
1847 kmem_cache_free(btrfs_free_space_cachep, info); 1898 kmem_cache_free(btrfs_free_space_cachep, info);
1899 } else {
1900 free_bitmap(ctl, info);
1901 }
1848 if (need_resched()) { 1902 if (need_resched()) {
1849 spin_unlock(&ctl->tree_lock); 1903 spin_unlock(&ctl->tree_lock);
1850 cond_resched(); 1904 cond_resched();
@@ -2142,9 +2196,11 @@ again:
2142/* 2196/*
2143 * This searches the block group for just extents to fill the cluster with. 2197 * This searches the block group for just extents to fill the cluster with.
2144 */ 2198 */
2145static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group, 2199static noinline int
2146 struct btrfs_free_cluster *cluster, 2200setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2147 u64 offset, u64 bytes, u64 min_bytes) 2201 struct btrfs_free_cluster *cluster,
2202 struct list_head *bitmaps, u64 offset, u64 bytes,
2203 u64 min_bytes)
2148{ 2204{
2149 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 2205 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2150 struct btrfs_free_space *first = NULL; 2206 struct btrfs_free_space *first = NULL;
@@ -2166,6 +2222,8 @@ static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2166 * extent entry. 2222 * extent entry.
2167 */ 2223 */
2168 while (entry->bitmap) { 2224 while (entry->bitmap) {
2225 if (list_empty(&entry->list))
2226 list_add_tail(&entry->list, bitmaps);
2169 node = rb_next(&entry->offset_index); 2227 node = rb_next(&entry->offset_index);
2170 if (!node) 2228 if (!node)
2171 return -ENOSPC; 2229 return -ENOSPC;
@@ -2185,8 +2243,12 @@ static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2185 return -ENOSPC; 2243 return -ENOSPC;
2186 entry = rb_entry(node, struct btrfs_free_space, offset_index); 2244 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2187 2245
2188 if (entry->bitmap) 2246 if (entry->bitmap) {
2247 if (list_empty(&entry->list))
2248 list_add_tail(&entry->list, bitmaps);
2189 continue; 2249 continue;
2250 }
2251
2190 /* 2252 /*
2191 * we haven't filled the empty size and the window is 2253 * we haven't filled the empty size and the window is
2192 * very large. reset and try again 2254 * very large. reset and try again
@@ -2238,9 +2300,11 @@ static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2238 * This specifically looks for bitmaps that may work in the cluster, we assume 2300 * This specifically looks for bitmaps that may work in the cluster, we assume
2239 * that we have already failed to find extents that will work. 2301 * that we have already failed to find extents that will work.
2240 */ 2302 */
2241static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group, 2303static noinline int
2242 struct btrfs_free_cluster *cluster, 2304setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2243 u64 offset, u64 bytes, u64 min_bytes) 2305 struct btrfs_free_cluster *cluster,
2306 struct list_head *bitmaps, u64 offset, u64 bytes,
2307 u64 min_bytes)
2244{ 2308{
2245 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 2309 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2246 struct btrfs_free_space *entry; 2310 struct btrfs_free_space *entry;
@@ -2250,10 +2314,39 @@ static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2250 if (ctl->total_bitmaps == 0) 2314 if (ctl->total_bitmaps == 0)
2251 return -ENOSPC; 2315 return -ENOSPC;
2252 2316
2317 /*
2318 * First check our cached list of bitmaps and see if there is an entry
2319 * here that will work.
2320 */
2321 list_for_each_entry(entry, bitmaps, list) {
2322 if (entry->bytes < min_bytes)
2323 continue;
2324 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2325 bytes, min_bytes);
2326 if (!ret)
2327 return 0;
2328 }
2329
2330 /*
2331 * If we do have entries on our list and we are here then we didn't find
2332 * anything, so go ahead and get the next entry after the last entry in
2333 * this list and start the search from there.
2334 */
2335 if (!list_empty(bitmaps)) {
2336 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2337 list);
2338 node = rb_next(&entry->offset_index);
2339 if (!node)
2340 return -ENOSPC;
2341 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2342 goto search;
2343 }
2344
2253 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1); 2345 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
2254 if (!entry) 2346 if (!entry)
2255 return -ENOSPC; 2347 return -ENOSPC;
2256 2348
2349search:
2257 node = &entry->offset_index; 2350 node = &entry->offset_index;
2258 do { 2351 do {
2259 entry = rb_entry(node, struct btrfs_free_space, offset_index); 2352 entry = rb_entry(node, struct btrfs_free_space, offset_index);
@@ -2284,6 +2377,8 @@ int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2284 u64 offset, u64 bytes, u64 empty_size) 2377 u64 offset, u64 bytes, u64 empty_size)
2285{ 2378{
2286 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 2379 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2380 struct list_head bitmaps;
2381 struct btrfs_free_space *entry, *tmp;
2287 u64 min_bytes; 2382 u64 min_bytes;
2288 int ret; 2383 int ret;
2289 2384
@@ -2322,11 +2417,16 @@ int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2322 goto out; 2417 goto out;
2323 } 2418 }
2324 2419
2325 ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes, 2420 INIT_LIST_HEAD(&bitmaps);
2326 min_bytes); 2421 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2422 bytes, min_bytes);
2327 if (ret) 2423 if (ret)
2328 ret = setup_cluster_bitmap(block_group, cluster, offset, 2424 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2329 bytes, min_bytes); 2425 offset, bytes, min_bytes);
2426
2427 /* Clear our temporary list */
2428 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2429 list_del_init(&entry->list);
2330 2430
2331 if (!ret) { 2431 if (!ret) {
2332 atomic_inc(&block_group->count); 2432 atomic_inc(&block_group->count);