diff options
| author | Miao Xie <miaox@cn.fujitsu.com> | 2014-06-18 22:42:49 -0400 |
|---|---|---|
| committer | Chris Mason <clm@fb.com> | 2014-06-19 17:20:54 -0400 |
| commit | 5349d6c3ffead27d693fdac21270541fa95ef33d (patch) | |
| tree | 7531b4ce53185804c6dbc18fc5c605c0ab46572a | |
| parent | 46fefe41b5360106ebfed228fbfba62f75ad4fcd (diff) | |
Btrfs: make free space cache write out functions more readable
This patch makes the free space cache write out functions more readable,
and beisdes that, it also reduces the stack space that the function --
__btrfs_write_out_cache uses from 194bytes to 144bytes.
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
| -rw-r--r-- | fs/btrfs/free-space-cache.c | 159 |
1 files changed, 93 insertions, 66 deletions
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 372b05ff1943..a852e15173e5 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c | |||
| @@ -274,18 +274,32 @@ struct io_ctl { | |||
| 274 | }; | 274 | }; |
| 275 | 275 | ||
| 276 | static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode, | 276 | static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode, |
| 277 | struct btrfs_root *root) | 277 | struct btrfs_root *root, int write) |
| 278 | { | 278 | { |
| 279 | int num_pages; | ||
| 280 | int check_crcs = 0; | ||
| 281 | |||
| 282 | num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | ||
| 283 | PAGE_CACHE_SHIFT; | ||
| 284 | |||
| 285 | if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID) | ||
| 286 | check_crcs = 1; | ||
| 287 | |||
| 288 | /* Make sure we can fit our crcs into the first page */ | ||
| 289 | if (write && check_crcs && | ||
| 290 | (num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) | ||
| 291 | return -ENOSPC; | ||
| 292 | |||
| 279 | memset(io_ctl, 0, sizeof(struct io_ctl)); | 293 | memset(io_ctl, 0, sizeof(struct io_ctl)); |
| 280 | io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | 294 | |
| 281 | PAGE_CACHE_SHIFT; | 295 | io_ctl->pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS); |
| 282 | io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages, | ||
| 283 | GFP_NOFS); | ||
| 284 | if (!io_ctl->pages) | 296 | if (!io_ctl->pages) |
| 285 | return -ENOMEM; | 297 | return -ENOMEM; |
| 298 | |||
| 299 | io_ctl->num_pages = num_pages; | ||
| 286 | io_ctl->root = root; | 300 | io_ctl->root = root; |
| 287 | if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID) | 301 | io_ctl->check_crcs = check_crcs; |
| 288 | io_ctl->check_crcs = 1; | 302 | |
| 289 | return 0; | 303 | return 0; |
| 290 | } | 304 | } |
| 291 | 305 | ||
| @@ -677,7 +691,7 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, | |||
| 677 | if (!num_entries) | 691 | if (!num_entries) |
| 678 | return 0; | 692 | return 0; |
| 679 | 693 | ||
| 680 | ret = io_ctl_init(&io_ctl, inode, root); | 694 | ret = io_ctl_init(&io_ctl, inode, root, 0); |
| 681 | if (ret) | 695 | if (ret) |
| 682 | return ret; | 696 | return ret; |
| 683 | 697 | ||
| @@ -957,19 +971,18 @@ fail: | |||
| 957 | } | 971 | } |
| 958 | 972 | ||
| 959 | static noinline_for_stack int | 973 | static noinline_for_stack int |
| 960 | add_ioctl_entries(struct btrfs_root *root, | 974 | write_pinned_extent_entries(struct btrfs_root *root, |
| 961 | struct inode *inode, | 975 | struct btrfs_block_group_cache *block_group, |
| 962 | struct btrfs_block_group_cache *block_group, | 976 | struct io_ctl *io_ctl, |
| 963 | struct io_ctl *io_ctl, | 977 | int *entries) |
| 964 | struct extent_state **cached_state, | ||
| 965 | struct list_head *bitmap_list, | ||
| 966 | int *entries) | ||
| 967 | { | 978 | { |
| 968 | u64 start, extent_start, extent_end, len; | 979 | u64 start, extent_start, extent_end, len; |
| 969 | struct list_head *pos, *n; | ||
| 970 | struct extent_io_tree *unpin = NULL; | 980 | struct extent_io_tree *unpin = NULL; |
| 971 | int ret; | 981 | int ret; |
| 972 | 982 | ||
| 983 | if (!block_group) | ||
| 984 | return 0; | ||
| 985 | |||
| 973 | /* | 986 | /* |
| 974 | * We want to add any pinned extents to our free space cache | 987 | * We want to add any pinned extents to our free space cache |
| 975 | * so we don't leak the space | 988 | * so we don't leak the space |
| @@ -979,23 +992,19 @@ add_ioctl_entries(struct btrfs_root *root, | |||
| 979 | */ | 992 | */ |
| 980 | unpin = root->fs_info->pinned_extents; | 993 | unpin = root->fs_info->pinned_extents; |
| 981 | 994 | ||
| 982 | if (block_group) | 995 | start = block_group->key.objectid; |
| 983 | start = block_group->key.objectid; | ||
| 984 | 996 | ||
| 985 | while (block_group && (start < block_group->key.objectid + | 997 | while (start < block_group->key.objectid + block_group->key.offset) { |
| 986 | block_group->key.offset)) { | ||
| 987 | ret = find_first_extent_bit(unpin, start, | 998 | ret = find_first_extent_bit(unpin, start, |
| 988 | &extent_start, &extent_end, | 999 | &extent_start, &extent_end, |
| 989 | EXTENT_DIRTY, NULL); | 1000 | EXTENT_DIRTY, NULL); |
| 990 | if (ret) { | 1001 | if (ret) |
| 991 | ret = 0; | 1002 | return 0; |
| 992 | break; | ||
| 993 | } | ||
| 994 | 1003 | ||
| 995 | /* This pinned extent is out of our range */ | 1004 | /* This pinned extent is out of our range */ |
| 996 | if (extent_start >= block_group->key.objectid + | 1005 | if (extent_start >= block_group->key.objectid + |
| 997 | block_group->key.offset) | 1006 | block_group->key.offset) |
| 998 | break; | 1007 | return 0; |
| 999 | 1008 | ||
| 1000 | extent_start = max(extent_start, start); | 1009 | extent_start = max(extent_start, start); |
| 1001 | extent_end = min(block_group->key.objectid + | 1010 | extent_end = min(block_group->key.objectid + |
| @@ -1005,11 +1014,20 @@ add_ioctl_entries(struct btrfs_root *root, | |||
| 1005 | *entries += 1; | 1014 | *entries += 1; |
| 1006 | ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL); | 1015 | ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL); |
| 1007 | if (ret) | 1016 | if (ret) |
| 1008 | goto out_nospc; | 1017 | return -ENOSPC; |
| 1009 | 1018 | ||
| 1010 | start = extent_end; | 1019 | start = extent_end; |
| 1011 | } | 1020 | } |
| 1012 | 1021 | ||
| 1022 | return 0; | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | static noinline_for_stack int | ||
| 1026 | write_bitmap_entries(struct io_ctl *io_ctl, struct list_head *bitmap_list) | ||
| 1027 | { | ||
| 1028 | struct list_head *pos, *n; | ||
| 1029 | int ret; | ||
| 1030 | |||
| 1013 | /* Write out the bitmaps */ | 1031 | /* Write out the bitmaps */ |
| 1014 | list_for_each_safe(pos, n, bitmap_list) { | 1032 | list_for_each_safe(pos, n, bitmap_list) { |
| 1015 | struct btrfs_free_space *entry = | 1033 | struct btrfs_free_space *entry = |
| @@ -1017,36 +1035,24 @@ add_ioctl_entries(struct btrfs_root *root, | |||
| 1017 | 1035 | ||
| 1018 | ret = io_ctl_add_bitmap(io_ctl, entry->bitmap); | 1036 | ret = io_ctl_add_bitmap(io_ctl, entry->bitmap); |
| 1019 | if (ret) | 1037 | if (ret) |
| 1020 | goto out_nospc; | 1038 | return -ENOSPC; |
| 1021 | list_del_init(&entry->list); | 1039 | list_del_init(&entry->list); |
| 1022 | } | 1040 | } |
| 1023 | 1041 | ||
| 1024 | /* Zero out the rest of the pages just to make sure */ | 1042 | return 0; |
| 1025 | io_ctl_zero_remaining_pages(io_ctl); | 1043 | } |
| 1026 | |||
| 1027 | ret = btrfs_dirty_pages(root, inode, io_ctl->pages, io_ctl->num_pages, | ||
| 1028 | 0, i_size_read(inode), cached_state); | ||
| 1029 | io_ctl_drop_pages(io_ctl); | ||
| 1030 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, | ||
| 1031 | i_size_read(inode) - 1, cached_state, GFP_NOFS); | ||
| 1032 | 1044 | ||
| 1033 | if (ret) | 1045 | static int flush_dirty_cache(struct inode *inode) |
| 1034 | goto fail; | 1046 | { |
| 1047 | int ret; | ||
| 1035 | 1048 | ||
| 1036 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | 1049 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 1037 | if (ret) { | 1050 | if (ret) |
| 1038 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, | 1051 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
| 1039 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL, | 1052 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL, |
| 1040 | GFP_NOFS); | 1053 | GFP_NOFS); |
| 1041 | goto fail; | ||
| 1042 | } | ||
| 1043 | return 0; | ||
| 1044 | 1054 | ||
| 1045 | fail: | 1055 | return ret; |
| 1046 | return -1; | ||
| 1047 | |||
