extent-tree.c (0ca1f7ceb1991099ed5273885ebcf4323948c72e) extent-tree.c (8929ecfa50f266163832eeacfbc3642ed5eb83b6)
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

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

2890
2891 data_sinfo = BTRFS_I(inode)->space_info;
2892 if (!data_sinfo)
2893 goto alloc;
2894
2895again:
2896 /* make sure we have enough space to handle the data first */
2897 spin_lock(&data_sinfo->lock);
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

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

2890
2891 data_sinfo = BTRFS_I(inode)->space_info;
2892 if (!data_sinfo)
2893 goto alloc;
2894
2895again:
2896 /* make sure we have enough space to handle the data first */
2897 spin_lock(&data_sinfo->lock);
2898 used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
2899 data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
2900 data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
2901 data_sinfo->bytes_super;
2898 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
2899 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
2900 data_sinfo->bytes_may_use;
2902
2903 if (used + bytes > data_sinfo->total_bytes) {
2904 struct btrfs_trans_handle *trans;
2905
2906 /*
2907 * if we don't have enough free bytes in this space then we need
2908 * to alloc a new chunk.
2909 */

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

2917 trans = btrfs_join_transaction(root, 1);
2918 if (IS_ERR(trans))
2919 return PTR_ERR(trans);
2920
2921 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2922 bytes + 2 * 1024 * 1024,
2923 alloc_target, 0);
2924 btrfs_end_transaction(trans, root);
2901
2902 if (used + bytes > data_sinfo->total_bytes) {
2903 struct btrfs_trans_handle *trans;
2904
2905 /*
2906 * if we don't have enough free bytes in this space then we need
2907 * to alloc a new chunk.
2908 */

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

2916 trans = btrfs_join_transaction(root, 1);
2917 if (IS_ERR(trans))
2918 return PTR_ERR(trans);
2919
2920 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2921 bytes + 2 * 1024 * 1024,
2922 alloc_target, 0);
2923 btrfs_end_transaction(trans, root);
2925 if (ret)
2924 if (ret < 0)
2926 return ret;
2927
2928 if (!data_sinfo) {
2929 btrfs_set_inode_space_info(root, inode);
2930 data_sinfo = BTRFS_I(inode)->space_info;
2931 }
2932 goto again;
2933 }

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

2940 if (IS_ERR(trans))
2941 return PTR_ERR(trans);
2942 ret = btrfs_commit_transaction(trans, root);
2943 if (ret)
2944 return ret;
2945 goto again;
2946 }
2947
2925 return ret;
2926
2927 if (!data_sinfo) {
2928 btrfs_set_inode_space_info(root, inode);
2929 data_sinfo = BTRFS_I(inode)->space_info;
2930 }
2931 goto again;
2932 }

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

2939 if (IS_ERR(trans))
2940 return PTR_ERR(trans);
2941 ret = btrfs_commit_transaction(trans, root);
2942 if (ret)
2943 return ret;
2944 goto again;
2945 }
2946
2948 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2949 ", %llu bytes_used, %llu bytes_reserved, "
2950 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
2951 "%llu total\n", (unsigned long long)bytes,
2952 (unsigned long long)data_sinfo->bytes_delalloc,
2947 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
2948 "%llu bytes_reserved, " "%llu bytes_pinned, "
2949 "%llu bytes_readonly, %llu may use %llu total\n",
2950 (unsigned long long)bytes,
2953 (unsigned long long)data_sinfo->bytes_used,
2954 (unsigned long long)data_sinfo->bytes_reserved,
2955 (unsigned long long)data_sinfo->bytes_pinned,
2956 (unsigned long long)data_sinfo->bytes_readonly,
2957 (unsigned long long)data_sinfo->bytes_may_use,
2958 (unsigned long long)data_sinfo->total_bytes);
2959 return -ENOSPC;
2960 }

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

3459{
3460 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3461 if (global_rsv->full || global_rsv == block_rsv ||
3462 block_rsv->space_info != global_rsv->space_info)
3463 global_rsv = NULL;
3464 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3465}
3466
2951 (unsigned long long)data_sinfo->bytes_used,
2952 (unsigned long long)data_sinfo->bytes_reserved,
2953 (unsigned long long)data_sinfo->bytes_pinned,
2954 (unsigned long long)data_sinfo->bytes_readonly,
2955 (unsigned long long)data_sinfo->bytes_may_use,
2956 (unsigned long long)data_sinfo->total_bytes);
2957 return -ENOSPC;
2958 }

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

3457{
3458 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3459 if (global_rsv->full || global_rsv == block_rsv ||
3460 block_rsv->space_info != global_rsv->space_info)
3461 global_rsv = NULL;
3462 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3463}
3464
3465/*
3466 * helper to calculate size of global block reservation.
3467 * the desired value is sum of space used by extent tree,
3468 * checksum tree and root tree
3469 */
3470static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3471{
3472 struct btrfs_space_info *sinfo;
3473 u64 num_bytes;
3474 u64 meta_used;
3475 u64 data_used;
3476 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3477#if 0
3478 /*
3479 * per tree used space accounting can be inaccuracy, so we
3480 * can't rely on it.
3481 */
3482 spin_lock(&fs_info->extent_root->accounting_lock);
3483 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3484 spin_unlock(&fs_info->extent_root->accounting_lock);
3485
3486 spin_lock(&fs_info->csum_root->accounting_lock);
3487 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3488 spin_unlock(&fs_info->csum_root->accounting_lock);
3489
3490 spin_lock(&fs_info->tree_root->accounting_lock);
3491 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3492 spin_unlock(&fs_info->tree_root->accounting_lock);
3493#endif
3494 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3495 spin_lock(&sinfo->lock);
3496 data_used = sinfo->bytes_used;
3497 spin_unlock(&sinfo->lock);
3498
3499 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3500 spin_lock(&sinfo->lock);
3501 meta_used = sinfo->bytes_used;
3502 spin_unlock(&sinfo->lock);
3503
3504 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3505 csum_size * 2;
3506 num_bytes += div64_u64(data_used + meta_used, 50);
3507
3508 if (num_bytes * 3 > meta_used)
3509 num_bytes = div64_u64(meta_used, 3);
3510
3511 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3512}
3513
3514static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3515{
3516 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3517 struct btrfs_space_info *sinfo = block_rsv->space_info;
3518 u64 num_bytes;
3519
3520 num_bytes = calc_global_metadata_size(fs_info);
3521
3522 spin_lock(&block_rsv->lock);
3523 spin_lock(&sinfo->lock);
3524
3525 block_rsv->size = num_bytes;
3526
3527 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3528 sinfo->bytes_reserved + sinfo->bytes_readonly;
3529
3530 if (sinfo->total_bytes > num_bytes) {
3531 num_bytes = sinfo->total_bytes - num_bytes;
3532 block_rsv->reserved += num_bytes;
3533 sinfo->bytes_reserved += num_bytes;
3534 }
3535
3536 if (block_rsv->reserved >= block_rsv->size) {
3537 num_bytes = block_rsv->reserved - block_rsv->size;
3538 sinfo->bytes_reserved -= num_bytes;
3539 block_rsv->reserved = block_rsv->size;
3540 block_rsv->full = 1;
3541 }
3542#if 0
3543 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3544 block_rsv->size, block_rsv->reserved);
3545#endif
3546 spin_unlock(&sinfo->lock);
3547 spin_unlock(&block_rsv->lock);
3548}
3549
3467static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3468{
3469 struct btrfs_space_info *space_info;
3470
3471 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3472 fs_info->chunk_block_rsv.space_info = space_info;
3473 fs_info->chunk_block_rsv.priority = 10;
3474
3475 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3550static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3551{
3552 struct btrfs_space_info *space_info;
3553
3554 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3555 fs_info->chunk_block_rsv.space_info = space_info;
3556 fs_info->chunk_block_rsv.priority = 10;
3557
3558 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3559 fs_info->global_block_rsv.space_info = space_info;
3560 fs_info->global_block_rsv.priority = 10;
3561 fs_info->global_block_rsv.refill_used = 1;
3562 fs_info->delalloc_block_rsv.space_info = space_info;
3476 fs_info->trans_block_rsv.space_info = space_info;
3477 fs_info->empty_block_rsv.space_info = space_info;
3478 fs_info->empty_block_rsv.priority = 10;
3479
3563 fs_info->trans_block_rsv.space_info = space_info;
3564 fs_info->empty_block_rsv.space_info = space_info;
3565 fs_info->empty_block_rsv.priority = 10;
3566
3567 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3568 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3569 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3570 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
3480 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3571 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3572
3573 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3574
3575 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3576
3577 update_global_block_rsv(fs_info);
3481}
3482
3578}
3579
3580static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3581{
3582 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3583 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3584 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3585 WARN_ON(fs_info->trans_block_rsv.size > 0);
3586 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3587 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3588 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
3589}
3590
3483static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3484{
3485 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3486 3 * num_items;
3487}
3488
3489int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3490 struct btrfs_root *root,

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

3821 }
3822
3823 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3824 fs_info->pinned_extents = &fs_info->freed_extents[1];
3825 else
3826 fs_info->pinned_extents = &fs_info->freed_extents[0];
3827
3828 up_write(&fs_info->extent_commit_sem);
3591static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3592{
3593 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3594 3 * num_items;
3595}
3596
3597int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3598 struct btrfs_root *root,

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

3929 }
3930
3931 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3932 fs_info->pinned_extents = &fs_info->freed_extents[1];
3933 else
3934 fs_info->pinned_extents = &fs_info->freed_extents[0];
3935
3936 up_write(&fs_info->extent_commit_sem);
3937
3938 update_global_block_rsv(fs_info);
3829 return 0;
3830}
3831
3832static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3833{
3834 struct btrfs_fs_info *fs_info = root->fs_info;
3835 struct btrfs_block_group_cache *cache = NULL;
3836 u64 len;

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

4813{
4814 struct btrfs_block_group_cache *cache;
4815 int index = 0;
4816
4817 spin_lock(&info->lock);
4818 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4819 (unsigned long long)(info->total_bytes - info->bytes_used -
4820 info->bytes_pinned - info->bytes_reserved -
3939 return 0;
3940}
3941
3942static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3943{
3944 struct btrfs_fs_info *fs_info = root->fs_info;
3945 struct btrfs_block_group_cache *cache = NULL;
3946 u64 len;

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

4923{
4924 struct btrfs_block_group_cache *cache;
4925 int index = 0;
4926
4927 spin_lock(&info->lock);
4928 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4929 (unsigned long long)(info->total_bytes - info->bytes_used -
4930 info->bytes_pinned - info->bytes_reserved -
4821 info->bytes_super),
4931 info->bytes_readonly),
4822 (info->full) ? "" : "not ");
4932 (info->full) ? "" : "not ");
4823 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
4824 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4825 "\n",
4933 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
4934 "reserved=%llu, may_use=%llu, readonly=%llu\n",
4826 (unsigned long long)info->total_bytes,
4935 (unsigned long long)info->total_bytes,
4936 (unsigned long long)info->bytes_used,
4827 (unsigned long long)info->bytes_pinned,
4937 (unsigned long long)info->bytes_pinned,
4828 (unsigned long long)info->bytes_delalloc,
4938 (unsigned long long)info->bytes_reserved,
4829 (unsigned long long)info->bytes_may_use,
4939 (unsigned long long)info->bytes_may_use,
4830 (unsigned long long)info->bytes_used,
4831 (unsigned long long)info->bytes_root,
4832 (unsigned long long)info->bytes_super,
4833 (unsigned long long)info->bytes_reserved);
4940 (unsigned long long)info->bytes_readonly);
4834 spin_unlock(&info->lock);
4835
4836 if (!dump_block_groups)
4837 return;
4838
4839 down_read(&info->groups_sem);
4840again:
4841 list_for_each_entry(cache, &info->block_groups[index], list) {

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

7722 /* now that all the block groups are freed, go through and
7723 * free all the space_info structs. This is only called during
7724 * the final stages of unmount, and so we know nobody is
7725 * using them. We call synchronize_rcu() once before we start,
7726 * just to be on the safe side.
7727 */
7728 synchronize_rcu();
7729
4941 spin_unlock(&info->lock);
4942
4943 if (!dump_block_groups)
4944 return;
4945
4946 down_read(&info->groups_sem);
4947again:
4948 list_for_each_entry(cache, &info->block_groups[index], list) {

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

7829 /* now that all the block groups are freed, go through and
7830 * free all the space_info structs. This is only called during
7831 * the final stages of unmount, and so we know nobody is
7832 * using them. We call synchronize_rcu() once before we start,
7833 * just to be on the safe side.
7834 */
7835 synchronize_rcu();
7836
7837 release_global_block_rsv(info);
7838
7730 while(!list_empty(&info->space_info)) {
7731 space_info = list_entry(info->space_info.next,
7732 struct btrfs_space_info,
7733 list);
7734 if (space_info->bytes_pinned > 0 ||
7735 space_info->bytes_reserved > 0) {
7736 WARN_ON(1);
7737 dump_space_info(space_info, 0, 0);

--- 286 unchanged lines hidden ---
7839 while(!list_empty(&info->space_info)) {
7840 space_info = list_entry(info->space_info.next,
7841 struct btrfs_space_info,
7842 list);
7843 if (space_info->bytes_pinned > 0 ||
7844 space_info->bytes_reserved > 0) {
7845 WARN_ON(1);
7846 dump_space_info(space_info, 0, 0);

--- 286 unchanged lines hidden ---