aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiao Xie <miaox@cn.fujitsu.com>2014-06-18 22:42:49 -0400
committerChris Mason <clm@fb.com>2014-06-19 17:20:54 -0400
commit5349d6c3ffead27d693fdac21270541fa95ef33d (patch)
tree7531b4ce53185804c6dbc18fc5c605c0ab46572a
parent46fefe41b5360106ebfed228fbfba62f75ad4fcd (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.c159
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
276static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode, 276static 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
959static noinline_for_stack int 973static noinline_for_stack int
960add_ioctl_entries(struct btrfs_root *root, 974write_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
1025static noinline_for_stack int
1026write_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) 1045static 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
1045fail: 1055 return ret;
1046 return -1;
1047
1048out_nospc:
1049 return -ENOSPC;
1050} 1056}
1051 1057
1052static void noinline_for_stack 1058static void noinline_for_stack
@@ -1056,6 +1062,7 @@ cleanup_write_cache_enospc(struct inode *inode,
1056 struct list_head *bitmap_list) 1062 struct list_head *bitmap_list)
1057{ 1063{
1058 struct list_head *pos, *n; 1064 struct list_head *pos, *n;
1065
1059 list_for_each_safe(pos, n, bitmap_list) { 1066 list_for_each_safe(pos, n, bitmap_list) {
1060 struct btrfs_free_space *entry = 1067 struct btrfs_free_space *entry =
1061 list_entry(pos, struct btrfs_free_space, list); 1068 list_entry(pos, struct btrfs_free_space, list);
@@ -1088,18 +1095,15 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1088{ 1095{
1089 struct extent_state *cached_state = NULL; 1096 struct extent_state *cached_state = NULL;
1090 struct io_ctl io_ctl; 1097 struct io_ctl io_ctl;
1091 struct list_head bitmap_list; 1098 LIST_HEAD(bitmap_list);
1092 int entries = 0; 1099 int entries = 0;
1093 int bitmaps = 0; 1100 int bitmaps = 0;
1094 int ret; 1101 int ret;
1095 int err = -1;
1096
1097 INIT_LIST_HEAD(&bitmap_list);
1098 1102
1099 if (!i_size_read(inode)) 1103 if (!i_size_read(inode))
1100 return -1; 1104 return -1;
1101 1105
1102 ret = io_ctl_init(&io_ctl, inode, root); 1106 ret = io_ctl_init(&io_ctl, inode, root, 1);
1103 if (ret) 1107 if (ret)
1104 return -1; 1108 return -1;
1105 1109
@@ -1109,42 +1113,65 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1109 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, 1113 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1110 0, &cached_state); 1114 0, &cached_state);
1111 1115
1112
1113 /* Make sure we can fit our crcs into the first page */
1114 if (io_ctl.check_crcs &&
1115 (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE)
1116 goto out_nospc;
1117
1118 io_ctl_set_generation(&io_ctl, trans->transid); 1116 io_ctl_set_generation(&io_ctl, trans->transid);
1119 1117
1118 /* Write out the extent entries in the free space cache */
1120 ret = write_cache_extent_entries(&io_ctl, ctl, 1119 ret = write_cache_extent_entries(&io_ctl, ctl,
1121 block_group, &entries, &bitmaps, 1120 block_group, &entries, &bitmaps,
1122 &bitmap_list); 1121 &bitmap_list);
1123 if (ret) 1122 if (ret)
1124 goto out_nospc; 1123 goto out_nospc;
1125 1124
1126 ret = add_ioctl_entries(root, inode, block_group, &io_ctl, 1125 /*
1127 &cached_state, &bitmap_list, &entries); 1126 * Some spaces that are freed in the current transaction are pinned,
1127 * they will be added into free space cache after the transaction is
1128 * committed, we shouldn't lose them.
1129 */
1130 ret = write_pinned_extent_entries(root, block_group, &io_ctl, &entries);
1131 if (ret)
1132 goto out_nospc;
1133
1134 /* At last, we write out all the bitmaps. */
1135 ret = write_bitmap_entries(&io_ctl, &bitmap_list);
1136 if (ret)
1137 goto out_nospc;
1128 1138
1129 if (ret == -ENOSPC) 1139 /* Zero out the rest of the pages just to make sure */
1140 io_ctl_zero_remaining_pages(&io_ctl);
1141
1142 /* Everything is written out, now we dirty the pages in the file. */
1143 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
1144 0, i_size_read(inode), &cached_state);
1145 if (ret)
1130 goto out_nospc; 1146 goto out_nospc;
1131 else if (ret) 1147
1148 /*
1149 * Release the pages and unlock the extent, we will flush
1150 * them out later
1151 */
1152 io_ctl_drop_pages(&io_ctl);
1153
1154 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1155 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1156
1157 /* Flush the dirty pages in the cache file. */
1158 ret = flush_dirty_cache(inode);
1159 if (ret)
1132 goto out; 1160 goto out;
1133 1161
1134 err = update_cache_item(trans, root, inode, path, offset, 1162 /* Update the cache item to tell everyone this cache file is valid. */
1163 ret = update_cache_item(trans, root, inode, path, offset,
1135 entries, bitmaps); 1164 entries, bitmaps);
1136
1137out: 1165out:
1138 io_ctl_free(&io_ctl); 1166 io_ctl_free(&io_ctl);
1139 if (err) { 1167 if (ret) {
1140 invalidate_inode_pages2(inode->i_mapping); 1168 invalidate_inode_pages2(inode->i_mapping);
1141 BTRFS_I(inode)->generation = 0; 1169 BTRFS_I(inode)->generation = 0;
1142 } 1170 }
1143 btrfs_update_inode(trans, root, inode); 1171 btrfs_update_inode(trans, root, inode);
1144 return err; 1172 return ret;
1145 1173
1146out_nospc: 1174out_nospc:
1147
1148 cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list); 1175 cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list);
1149 goto out; 1176 goto out;
1150} 1177}