diff options
Diffstat (limited to 'fs/btrfs/free-space-cache.c')
-rw-r--r-- | fs/btrfs/free-space-cache.c | 192 |
1 files changed, 126 insertions, 66 deletions
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 372b05ff1943..2b0a627cb5f9 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 | ||
@@ -666,6 +680,13 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, | |||
666 | generation = btrfs_free_space_generation(leaf, header); | 680 | generation = btrfs_free_space_generation(leaf, header); |
667 | btrfs_release_path(path); | 681 | btrfs_release_path(path); |
668 | 682 | ||
683 | if (!BTRFS_I(inode)->generation) { | ||
684 | btrfs_info(root->fs_info, | ||
685 | "The free space cache file (%llu) is invalid. skip it\n", | ||
686 | offset); | ||
687 | return 0; | ||
688 | } | ||
689 | |||
669 | if (BTRFS_I(inode)->generation != generation) { | 690 | if (BTRFS_I(inode)->generation != generation) { |
670 | btrfs_err(root->fs_info, | 691 | btrfs_err(root->fs_info, |
671 | "free space inode generation (%llu) " | 692 | "free space inode generation (%llu) " |
@@ -677,7 +698,7 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, | |||
677 | if (!num_entries) | 698 | if (!num_entries) |
678 | return 0; | 699 | return 0; |
679 | 700 | ||
680 | ret = io_ctl_init(&io_ctl, inode, root); | 701 | ret = io_ctl_init(&io_ctl, inode, root, 0); |
681 | if (ret) | 702 | if (ret) |
682 | return ret; | 703 | return ret; |
683 | 704 | ||
@@ -957,19 +978,18 @@ fail: | |||
957 | } | 978 | } |
958 | 979 | ||
959 | static noinline_for_stack int | 980 | static noinline_for_stack int |
960 | add_ioctl_entries(struct btrfs_root *root, | 981 | write_pinned_extent_entries(struct btrfs_root *root, |
961 | struct inode *inode, | 982 | struct btrfs_block_group_cache *block_group, |
962 | struct btrfs_block_group_cache *block_group, | 983 | struct io_ctl *io_ctl, |
963 | struct io_ctl *io_ctl, | 984 | int *entries) |
964 | struct extent_state **cached_state, | ||
965 | struct list_head *bitmap_list, | ||
966 | int *entries) | ||
967 | { | 985 | { |
968 | u64 start, extent_start, extent_end, len; | 986 | u64 start, extent_start, extent_end, len; |
969 | struct list_head *pos, *n; | ||
970 | struct extent_io_tree *unpin = NULL; | 987 | struct extent_io_tree *unpin = NULL; |
971 | int ret; | 988 | int ret; |
972 | 989 | ||
990 | if (!block_group) | ||
991 | return 0; | ||
992 | |||
973 | /* | 993 | /* |
974 | * We want to add any pinned extents to our free space cache | 994 | * We want to add any pinned extents to our free space cache |
975 | * so we don't leak the space | 995 | * so we don't leak the space |
@@ -979,23 +999,19 @@ add_ioctl_entries(struct btrfs_root *root, | |||
979 | */ | 999 | */ |
980 | unpin = root->fs_info->pinned_extents; | 1000 | unpin = root->fs_info->pinned_extents; |
981 | 1001 | ||
982 | if (block_group) | 1002 | start = block_group->key.objectid; |
983 | start = block_group->key.objectid; | ||
984 | 1003 | ||
985 | while (block_group && (start < block_group->key.objectid + | 1004 | while (start < block_group->key.objectid + block_group->key.offset) { |
986 | block_group->key.offset)) { | ||
987 | ret = find_first_extent_bit(unpin, start, | 1005 | ret = find_first_extent_bit(unpin, start, |
988 | &extent_start, &extent_end, | 1006 | &extent_start, &extent_end, |
989 | EXTENT_DIRTY, NULL); | 1007 | EXTENT_DIRTY, NULL); |
990 | if (ret) { | 1008 | if (ret) |
991 | ret = 0; | 1009 | return 0; |
992 | break; | ||
993 | } | ||
994 | 1010 | ||
995 | /* This pinned extent is out of our range */ | 1011 | /* This pinned extent is out of our range */ |
996 | if (extent_start >= block_group->key.objectid + | 1012 | if (extent_start >= block_group->key.objectid + |
997 | block_group->key.offset) | 1013 | block_group->key.offset) |
998 | break; | 1014 | return 0; |
999 | 1015 | ||
1000 | extent_start = max(extent_start, start); | 1016 | extent_start = max(extent_start, start); |
1001 | extent_end = min(block_group->key.objectid + | 1017 | extent_end = min(block_group->key.objectid + |
@@ -1005,11 +1021,20 @@ add_ioctl_entries(struct btrfs_root *root, | |||
1005 | *entries += 1; | 1021 | *entries += 1; |
1006 | ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL); | 1022 | ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL); |
1007 | if (ret) | 1023 | if (ret) |
1008 | goto out_nospc; | 1024 | return -ENOSPC; |
1009 | 1025 | ||
1010 | start = extent_end; | 1026 | start = extent_end; |
1011 | } | 1027 | } |
1012 | 1028 | ||
1029 | return 0; | ||
1030 | } | ||
1031 | |||
1032 | static noinline_for_stack int | ||
1033 | write_bitmap_entries(struct io_ctl *io_ctl, struct list_head *bitmap_list) | ||
1034 | { | ||
1035 | struct list_head *pos, *n; | ||
1036 | int ret; | ||
1037 | |||
1013 | /* Write out the bitmaps */ | 1038 | /* Write out the bitmaps */ |
1014 | list_for_each_safe(pos, n, bitmap_list) { | 1039 | list_for_each_safe(pos, n, bitmap_list) { |
1015 | struct btrfs_free_space *entry = | 1040 | struct btrfs_free_space *entry = |
@@ -1017,36 +1042,24 @@ add_ioctl_entries(struct btrfs_root *root, | |||
1017 | 1042 | ||
1018 | ret = io_ctl_add_bitmap(io_ctl, entry->bitmap); | 1043 | ret = io_ctl_add_bitmap(io_ctl, entry->bitmap); |
1019 | if (ret) | 1044 | if (ret) |
1020 | goto out_nospc; | 1045 | return -ENOSPC; |
1021 | list_del_init(&entry->list); | 1046 | list_del_init(&entry->list); |
1022 | } | 1047 | } |
1023 | 1048 | ||
1024 | /* Zero out the rest of the pages just to make sure */ | 1049 | return 0; |
1025 | io_ctl_zero_remaining_pages(io_ctl); | 1050 | } |
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 | 1051 | ||
1033 | if (ret) | 1052 | static int flush_dirty_cache(struct inode *inode) |
1034 | goto fail; | 1053 | { |
1054 | int ret; | ||
1035 | 1055 | ||
1036 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); | 1056 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
1037 | if (ret) { | 1057 | if (ret) |
1038 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, | 1058 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
1039 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL, | 1059 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL, |
1040 | GFP_NOFS); | 1060 | GFP_NOFS); |
1041 | goto fail; | ||
1042 | } | ||
1043 | return 0; | ||
1044 | 1061 | ||
1045 | fail: | 1062 | return ret; |
1046 | return -1; | ||
1047 | |||
1048 | out_nospc: | ||
1049 | return -ENOSPC; | ||
1050 | } | 1063 | } |
1051 | 1064 | ||
1052 | static void noinline_for_stack | 1065 | static void noinline_for_stack |
@@ -1056,6 +1069,7 @@ cleanup_write_cache_enospc(struct inode *inode, | |||
1056 | struct list_head *bitmap_list) | 1069 | struct list_head *bitmap_list) |
1057 | { | 1070 | { |
1058 | struct list_head *pos, *n; | 1071 | struct list_head *pos, *n; |
1072 | |||
1059 | list_for_each_safe(pos, n, bitmap_list) { | 1073 | list_for_each_safe(pos, n, bitmap_list) { |
1060 | struct btrfs_free_space *entry = | 1074 | struct btrfs_free_space *entry = |
1061 | list_entry(pos, struct btrfs_free_space, list); | 1075 | list_entry(pos, struct btrfs_free_space, list); |
@@ -1088,64 +1102,104 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, | |||
1088 | { | 1102 | { |
1089 | struct extent_state *cached_state = NULL; | 1103 | struct extent_state *cached_state = NULL; |
1090 | struct io_ctl io_ctl; | 1104 | struct io_ctl io_ctl; |
1091 | struct list_head bitmap_list; | 1105 | LIST_HEAD(bitmap_list); |
1092 | int entries = 0; | 1106 | int entries = 0; |
1093 | int bitmaps = 0; | 1107 | int bitmaps = 0; |
1094 | int ret; | 1108 | int ret; |
1095 | int err = -1; | ||
1096 | |||
1097 | INIT_LIST_HEAD(&bitmap_list); | ||
1098 | 1109 | ||
1099 | if (!i_size_read(inode)) | 1110 | if (!i_size_read(inode)) |
1100 | return -1; | 1111 | return -1; |
1101 | 1112 | ||
1102 | ret = io_ctl_init(&io_ctl, inode, root); | 1113 | ret = io_ctl_init(&io_ctl, inode, root, 1); |
1103 | if (ret) | 1114 | if (ret) |
1104 | return -1; | 1115 | return -1; |
1105 | 1116 | ||
1117 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) { | ||
1118 | down_write(&block_group->data_rwsem); | ||
1119 | spin_lock(&block_group->lock); | ||
1120 | if (block_group->delalloc_bytes) { | ||
1121 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; | ||
1122 | spin_unlock(&block_group->lock); | ||
1123 | up_write(&block_group->data_rwsem); | ||
1124 | BTRFS_I(inode)->generation = 0; | ||
1125 | ret = 0; | ||
1126 | goto out; | ||
1127 | } | ||
1128 | spin_unlock(&block_group->lock); | ||
1129 | } | ||
1130 | |||
1106 | /* Lock all pages first so we can lock the extent safely. */ | 1131 | /* Lock all pages first so we can lock the extent safely. */ |
1107 | io_ctl_prepare_pages(&io_ctl, inode, 0); | 1132 | io_ctl_prepare_pages(&io_ctl, inode, 0); |
1108 | 1133 | ||
1109 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, | 1134 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, |
1110 | 0, &cached_state); | 1135 | 0, &cached_state); |
1111 | 1136 | ||
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); | 1137 | io_ctl_set_generation(&io_ctl, trans->transid); |
1119 | 1138 | ||
1139 | /* Write out the extent entries in the free space cache */ | ||
1120 | ret = write_cache_extent_entries(&io_ctl, ctl, | 1140 | ret = write_cache_extent_entries(&io_ctl, ctl, |
1121 | block_group, &entries, &bitmaps, | 1141 | block_group, &entries, &bitmaps, |
1122 | &bitmap_list); | 1142 | &bitmap_list); |
1123 | if (ret) | 1143 | if (ret) |
1124 | goto out_nospc; | 1144 | goto out_nospc; |
1125 | 1145 | ||
1126 | ret = add_ioctl_entries(root, inode, block_group, &io_ctl, | 1146 | /* |
1127 | &cached_state, &bitmap_list, &entries); | 1147 | * Some spaces that are freed in the current transaction are pinned, |
1148 | * they will be added into free space cache after the transaction is | ||
1149 | * committed, we shouldn't lose them. | ||
1150 | */ | ||
1151 | ret = write_pinned_extent_entries(root, block_group, &io_ctl, &entries); | ||
1152 | if (ret) | ||
1153 | goto out_nospc; | ||
1128 | 1154 | ||
1129 | if (ret == -ENOSPC) | 1155 | /* At last, we write out all the bitmaps. */ |
1156 | ret = write_bitmap_entries(&io_ctl, &bitmap_list); | ||
1157 | if (ret) | ||
1130 | goto out_nospc; | 1158 | goto out_nospc; |
1131 | else if (ret) | 1159 | |
1160 | /* Zero out the rest of the pages just to make sure */ | ||
1161 | io_ctl_zero_remaining_pages(&io_ctl); | ||
1162 | |||
1163 | /* Everything is written out, now we dirty the pages in the file. */ | ||
1164 | ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages, | ||
1165 | 0, i_size_read(inode), &cached_state); | ||
1166 | if (ret) | ||
1167 | goto out_nospc; | ||
1168 | |||
1169 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) | ||
1170 | up_write(&block_group->data_rwsem); | ||
1171 | /* | ||
1172 | * Release the pages and unlock the extent, we will flush | ||
1173 | * them out later | ||
1174 | */ | ||
1175 | io_ctl_drop_pages(&io_ctl); | ||
1176 | |||
1177 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, | ||
1178 | i_size_read(inode) - 1, &cached_state, GFP_NOFS); | ||
1179 | |||
1180 | /* Flush the dirty pages in the cache file. */ | ||
1181 | ret = flush_dirty_cache(inode); | ||
1182 | if (ret) | ||
1132 | goto out; | 1183 | goto out; |
1133 | 1184 | ||
1134 | err = update_cache_item(trans, root, inode, path, offset, | 1185 | /* Update the cache item to tell everyone this cache file is valid. */ |
1186 | ret = update_cache_item(trans, root, inode, path, offset, | ||
1135 | entries, bitmaps); | 1187 | entries, bitmaps); |
1136 | |||
1137 | out: | 1188 | out: |
1138 | io_ctl_free(&io_ctl); | 1189 | io_ctl_free(&io_ctl); |
1139 | if (err) { | 1190 | if (ret) { |
1140 | invalidate_inode_pages2(inode->i_mapping); | 1191 | invalidate_inode_pages2(inode->i_mapping); |
1141 | BTRFS_I(inode)->generation = 0; | 1192 | BTRFS_I(inode)->generation = 0; |
1142 | } | 1193 | } |
1143 | btrfs_update_inode(trans, root, inode); | 1194 | btrfs_update_inode(trans, root, inode); |
1144 | return err; | 1195 | return ret; |
1145 | 1196 | ||
1146 | out_nospc: | 1197 | out_nospc: |
1147 | |||
1148 | cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list); | 1198 | cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list); |
1199 | |||
1200 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) | ||
1201 | up_write(&block_group->data_rwsem); | ||
1202 | |||
1149 | goto out; | 1203 | goto out; |
1150 | } | 1204 | } |
1151 | 1205 | ||
@@ -1165,6 +1219,12 @@ int btrfs_write_out_cache(struct btrfs_root *root, | |||
1165 | spin_unlock(&block_group->lock); | 1219 | spin_unlock(&block_group->lock); |
1166 | return 0; | 1220 | return 0; |
1167 | } | 1221 | } |
1222 | |||
1223 | if (block_group->delalloc_bytes) { | ||
1224 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; | ||
1225 | spin_unlock(&block_group->lock); | ||
1226 | return 0; | ||
1227 | } | ||
1168 | spin_unlock(&block_group->lock); | 1228 | spin_unlock(&block_group->lock); |
1169 | 1229 | ||
1170 | inode = lookup_free_space_inode(root, block_group, path); | 1230 | inode = lookup_free_space_inode(root, block_group, path); |