| .. | .. |
|---|
| 13 | 13 | #include <linux/f2fs_fs.h> |
|---|
| 14 | 14 | #include <linux/pagevec.h> |
|---|
| 15 | 15 | #include <linux/swap.h> |
|---|
| 16 | +#include <linux/kthread.h> |
|---|
| 16 | 17 | |
|---|
| 17 | 18 | #include "f2fs.h" |
|---|
| 18 | 19 | #include "node.h" |
|---|
| 19 | 20 | #include "segment.h" |
|---|
| 20 | | -#include "trace.h" |
|---|
| 21 | 21 | #include <trace/events/f2fs.h> |
|---|
| 22 | + |
|---|
| 23 | +#define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) |
|---|
| 22 | 24 | |
|---|
| 23 | 25 | static struct kmem_cache *ino_entry_slab; |
|---|
| 24 | 26 | struct kmem_cache *f2fs_inode_entry_slab; |
|---|
| 25 | 27 | |
|---|
| 26 | | -void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io) |
|---|
| 28 | +void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io, |
|---|
| 29 | + unsigned char reason) |
|---|
| 27 | 30 | { |
|---|
| 28 | 31 | f2fs_build_fault_attr(sbi, 0, 0); |
|---|
| 29 | 32 | set_ckpt_flags(sbi, CP_ERROR_FLAG); |
|---|
| 30 | | - if (!end_io) |
|---|
| 33 | + if (!end_io) { |
|---|
| 31 | 34 | f2fs_flush_merged_writes(sbi); |
|---|
| 35 | + |
|---|
| 36 | + f2fs_handle_stop(sbi, reason); |
|---|
| 37 | + } |
|---|
| 32 | 38 | } |
|---|
| 33 | 39 | |
|---|
| 34 | 40 | /* |
|---|
| .. | .. |
|---|
| 37 | 43 | struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) |
|---|
| 38 | 44 | { |
|---|
| 39 | 45 | struct address_space *mapping = META_MAPPING(sbi); |
|---|
| 40 | | - struct page *page = NULL; |
|---|
| 46 | + struct page *page; |
|---|
| 41 | 47 | repeat: |
|---|
| 42 | 48 | page = f2fs_grab_cache_page(mapping, index, false); |
|---|
| 43 | 49 | if (!page) { |
|---|
| .. | .. |
|---|
| 107 | 113 | return __get_meta_page(sbi, index, true); |
|---|
| 108 | 114 | } |
|---|
| 109 | 115 | |
|---|
| 110 | | -struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index) |
|---|
| 116 | +struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index) |
|---|
| 111 | 117 | { |
|---|
| 112 | 118 | struct page *page; |
|---|
| 113 | 119 | int count = 0; |
|---|
| .. | .. |
|---|
| 118 | 124 | if (PTR_ERR(page) == -EIO && |
|---|
| 119 | 125 | ++count <= DEFAULT_RETRY_IO_COUNT) |
|---|
| 120 | 126 | goto retry; |
|---|
| 121 | | - f2fs_stop_checkpoint(sbi, false); |
|---|
| 127 | + f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE); |
|---|
| 122 | 128 | } |
|---|
| 123 | 129 | return page; |
|---|
| 124 | 130 | } |
|---|
| .. | .. |
|---|
| 136 | 142 | unsigned int segno, offset; |
|---|
| 137 | 143 | bool exist; |
|---|
| 138 | 144 | |
|---|
| 139 | | - if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ) |
|---|
| 145 | + if (type == DATA_GENERIC) |
|---|
| 140 | 146 | return true; |
|---|
| 141 | 147 | |
|---|
| 142 | 148 | segno = GET_SEGNO(sbi, blkaddr); |
|---|
| .. | .. |
|---|
| 144 | 150 | se = get_seg_entry(sbi, segno); |
|---|
| 145 | 151 | |
|---|
| 146 | 152 | exist = f2fs_test_bit(offset, se->cur_valid_map); |
|---|
| 153 | + if (exist && type == DATA_GENERIC_ENHANCE_UPDATE) { |
|---|
| 154 | + f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", |
|---|
| 155 | + blkaddr, exist); |
|---|
| 156 | + set_sbi_flag(sbi, SBI_NEED_FSCK); |
|---|
| 157 | + return exist; |
|---|
| 158 | + } |
|---|
| 159 | + |
|---|
| 147 | 160 | if (!exist && type == DATA_GENERIC_ENHANCE) { |
|---|
| 148 | 161 | f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", |
|---|
| 149 | 162 | blkaddr, exist); |
|---|
| 150 | 163 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
|---|
| 151 | | - WARN_ON(1); |
|---|
| 164 | + dump_stack(); |
|---|
| 152 | 165 | } |
|---|
| 153 | 166 | return exist; |
|---|
| 154 | 167 | } |
|---|
| .. | .. |
|---|
| 181 | 194 | case DATA_GENERIC: |
|---|
| 182 | 195 | case DATA_GENERIC_ENHANCE: |
|---|
| 183 | 196 | case DATA_GENERIC_ENHANCE_READ: |
|---|
| 197 | + case DATA_GENERIC_ENHANCE_UPDATE: |
|---|
| 184 | 198 | if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || |
|---|
| 185 | 199 | blkaddr < MAIN_BLKADDR(sbi))) { |
|---|
| 186 | 200 | f2fs_warn(sbi, "access invalid blkaddr:%u", |
|---|
| 187 | 201 | blkaddr); |
|---|
| 188 | 202 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
|---|
| 189 | | - WARN_ON(1); |
|---|
| 203 | + dump_stack(); |
|---|
| 190 | 204 | return false; |
|---|
| 191 | 205 | } else { |
|---|
| 192 | 206 | return __is_bitmap_valid(sbi, blkaddr, type); |
|---|
| .. | .. |
|---|
| 301 | 315 | |
|---|
| 302 | 316 | trace_f2fs_writepage(page, META); |
|---|
| 303 | 317 | |
|---|
| 304 | | - if (unlikely(f2fs_cp_error(sbi))) |
|---|
| 318 | + if (unlikely(f2fs_cp_error(sbi))) { |
|---|
| 319 | + if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) { |
|---|
| 320 | + ClearPageUptodate(page); |
|---|
| 321 | + dec_page_count(sbi, F2FS_DIRTY_META); |
|---|
| 322 | + unlock_page(page); |
|---|
| 323 | + return 0; |
|---|
| 324 | + } |
|---|
| 305 | 325 | goto redirty_out; |
|---|
| 326 | + } |
|---|
| 306 | 327 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
|---|
| 307 | 328 | goto redirty_out; |
|---|
| 308 | 329 | if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0)) |
|---|
| .. | .. |
|---|
| 348 | 369 | goto skip_write; |
|---|
| 349 | 370 | |
|---|
| 350 | 371 | /* if locked failed, cp will flush dirty pages instead */ |
|---|
| 351 | | - if (!mutex_trylock(&sbi->cp_mutex)) |
|---|
| 372 | + if (!f2fs_down_write_trylock(&sbi->cp_global_sem)) |
|---|
| 352 | 373 | goto skip_write; |
|---|
| 353 | 374 | |
|---|
| 354 | 375 | trace_f2fs_writepages(mapping->host, wbc, META); |
|---|
| 355 | 376 | diff = nr_pages_to_write(sbi, META, wbc); |
|---|
| 356 | 377 | written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); |
|---|
| 357 | | - mutex_unlock(&sbi->cp_mutex); |
|---|
| 378 | + f2fs_up_write(&sbi->cp_global_sem); |
|---|
| 358 | 379 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); |
|---|
| 359 | 380 | return 0; |
|---|
| 360 | 381 | |
|---|
| .. | .. |
|---|
| 442 | 463 | if (!PageDirty(page)) { |
|---|
| 443 | 464 | __set_page_dirty_nobuffers(page); |
|---|
| 444 | 465 | inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META); |
|---|
| 445 | | - f2fs_set_page_private(page, 0); |
|---|
| 446 | | - f2fs_trace_pid(page); |
|---|
| 466 | + set_page_private_reference(page); |
|---|
| 447 | 467 | return 1; |
|---|
| 448 | 468 | } |
|---|
| 449 | 469 | return 0; |
|---|
| .. | .. |
|---|
| 525 | 545 | __remove_ino_entry(sbi, ino, type); |
|---|
| 526 | 546 | } |
|---|
| 527 | 547 | |
|---|
| 528 | | -/* mode should be APPEND_INO or UPDATE_INO */ |
|---|
| 548 | +/* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */ |
|---|
| 529 | 549 | bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) |
|---|
| 530 | 550 | { |
|---|
| 531 | 551 | struct inode_management *im = &sbi->im[mode]; |
|---|
| .. | .. |
|---|
| 649 | 669 | /* truncate all the data during iput */ |
|---|
| 650 | 670 | iput(inode); |
|---|
| 651 | 671 | |
|---|
| 652 | | - err = f2fs_get_node_info(sbi, ino, &ni); |
|---|
| 672 | + err = f2fs_get_node_info(sbi, ino, &ni, false); |
|---|
| 653 | 673 | if (err) |
|---|
| 654 | 674 | goto err_out; |
|---|
| 655 | 675 | |
|---|
| .. | .. |
|---|
| 718 | 738 | orphan_blk = (struct f2fs_orphan_block *)page_address(page); |
|---|
| 719 | 739 | for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { |
|---|
| 720 | 740 | nid_t ino = le32_to_cpu(orphan_blk->ino[j]); |
|---|
| 741 | + |
|---|
| 721 | 742 | err = recover_orphan_inode(sbi, ino); |
|---|
| 722 | 743 | if (err) { |
|---|
| 723 | 744 | f2fs_put_page(page, 1); |
|---|
| .. | .. |
|---|
| 851 | 872 | struct page *cp_page_1 = NULL, *cp_page_2 = NULL; |
|---|
| 852 | 873 | struct f2fs_checkpoint *cp_block = NULL; |
|---|
| 853 | 874 | unsigned long long cur_version = 0, pre_version = 0; |
|---|
| 875 | + unsigned int cp_blocks; |
|---|
| 854 | 876 | int err; |
|---|
| 855 | 877 | |
|---|
| 856 | 878 | err = get_checkpoint_version(sbi, cp_addr, &cp_block, |
|---|
| .. | .. |
|---|
| 858 | 880 | if (err) |
|---|
| 859 | 881 | return NULL; |
|---|
| 860 | 882 | |
|---|
| 861 | | - if (le32_to_cpu(cp_block->cp_pack_total_block_count) > |
|---|
| 862 | | - sbi->blocks_per_seg) { |
|---|
| 883 | + cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count); |
|---|
| 884 | + |
|---|
| 885 | + if (cp_blocks > sbi->blocks_per_seg || cp_blocks <= F2FS_CP_PACKS) { |
|---|
| 863 | 886 | f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", |
|---|
| 864 | 887 | le32_to_cpu(cp_block->cp_pack_total_block_count)); |
|---|
| 865 | 888 | goto invalid_cp; |
|---|
| 866 | 889 | } |
|---|
| 867 | 890 | pre_version = *version; |
|---|
| 868 | 891 | |
|---|
| 869 | | - cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; |
|---|
| 892 | + cp_addr += cp_blocks - 1; |
|---|
| 870 | 893 | err = get_checkpoint_version(sbi, cp_addr, &cp_block, |
|---|
| 871 | 894 | &cp_page_2, version); |
|---|
| 872 | 895 | if (err) |
|---|
| .. | .. |
|---|
| 1016 | 1039 | inode_inc_dirty_pages(inode); |
|---|
| 1017 | 1040 | spin_unlock(&sbi->inode_lock[type]); |
|---|
| 1018 | 1041 | |
|---|
| 1019 | | - f2fs_set_page_private(page, 0); |
|---|
| 1020 | | - f2fs_trace_pid(page); |
|---|
| 1042 | + set_page_private_reference(page); |
|---|
| 1021 | 1043 | } |
|---|
| 1022 | 1044 | |
|---|
| 1023 | 1045 | void f2fs_remove_dirty_inode(struct inode *inode) |
|---|
| .. | .. |
|---|
| 1037 | 1059 | spin_unlock(&sbi->inode_lock[type]); |
|---|
| 1038 | 1060 | } |
|---|
| 1039 | 1061 | |
|---|
| 1040 | | -int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type) |
|---|
| 1062 | +int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type, |
|---|
| 1063 | + bool from_cp) |
|---|
| 1041 | 1064 | { |
|---|
| 1042 | 1065 | struct list_head *head; |
|---|
| 1043 | 1066 | struct inode *inode; |
|---|
| .. | .. |
|---|
| 1072 | 1095 | if (inode) { |
|---|
| 1073 | 1096 | unsigned long cur_ino = inode->i_ino; |
|---|
| 1074 | 1097 | |
|---|
| 1075 | | - F2FS_I(inode)->cp_task = current; |
|---|
| 1098 | + if (from_cp) |
|---|
| 1099 | + F2FS_I(inode)->cp_task = current; |
|---|
| 1100 | + F2FS_I(inode)->wb_task = current; |
|---|
| 1076 | 1101 | |
|---|
| 1077 | 1102 | filemap_fdatawrite(inode->i_mapping); |
|---|
| 1078 | 1103 | |
|---|
| 1079 | | - F2FS_I(inode)->cp_task = NULL; |
|---|
| 1104 | + F2FS_I(inode)->wb_task = NULL; |
|---|
| 1105 | + if (from_cp) |
|---|
| 1106 | + F2FS_I(inode)->cp_task = NULL; |
|---|
| 1080 | 1107 | |
|---|
| 1081 | 1108 | iput(inode); |
|---|
| 1082 | 1109 | /* We need to give cpu to another writers. */ |
|---|
| .. | .. |
|---|
| 1147 | 1174 | if (!is_journalled_quota(sbi)) |
|---|
| 1148 | 1175 | return false; |
|---|
| 1149 | 1176 | |
|---|
| 1150 | | - down_write(&sbi->quota_sem); |
|---|
| 1177 | + if (!f2fs_down_write_trylock(&sbi->quota_sem)) |
|---|
| 1178 | + return true; |
|---|
| 1151 | 1179 | if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { |
|---|
| 1152 | 1180 | ret = false; |
|---|
| 1153 | 1181 | } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { |
|---|
| .. | .. |
|---|
| 1158 | 1186 | } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { |
|---|
| 1159 | 1187 | ret = true; |
|---|
| 1160 | 1188 | } |
|---|
| 1161 | | - up_write(&sbi->quota_sem); |
|---|
| 1189 | + f2fs_up_write(&sbi->quota_sem); |
|---|
| 1162 | 1190 | return ret; |
|---|
| 1163 | 1191 | } |
|---|
| 1164 | 1192 | |
|---|
| .. | .. |
|---|
| 1204 | 1232 | /* write all the dirty dentry pages */ |
|---|
| 1205 | 1233 | if (get_pages(sbi, F2FS_DIRTY_DENTS)) { |
|---|
| 1206 | 1234 | f2fs_unlock_all(sbi); |
|---|
| 1207 | | - err = f2fs_sync_dirty_inodes(sbi, DIR_INODE); |
|---|
| 1235 | + err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true); |
|---|
| 1208 | 1236 | if (err) |
|---|
| 1209 | 1237 | return err; |
|---|
| 1210 | 1238 | cond_resched(); |
|---|
| .. | .. |
|---|
| 1215 | 1243 | * POR: we should ensure that there are no dirty node pages |
|---|
| 1216 | 1244 | * until finishing nat/sit flush. inode->i_blocks can be updated. |
|---|
| 1217 | 1245 | */ |
|---|
| 1218 | | - down_write(&sbi->node_change); |
|---|
| 1246 | + f2fs_down_write(&sbi->node_change); |
|---|
| 1219 | 1247 | |
|---|
| 1220 | 1248 | if (get_pages(sbi, F2FS_DIRTY_IMETA)) { |
|---|
| 1221 | | - up_write(&sbi->node_change); |
|---|
| 1249 | + f2fs_up_write(&sbi->node_change); |
|---|
| 1222 | 1250 | f2fs_unlock_all(sbi); |
|---|
| 1223 | 1251 | err = f2fs_sync_inode_meta(sbi); |
|---|
| 1224 | 1252 | if (err) |
|---|
| .. | .. |
|---|
| 1228 | 1256 | } |
|---|
| 1229 | 1257 | |
|---|
| 1230 | 1258 | retry_flush_nodes: |
|---|
| 1231 | | - down_write(&sbi->node_write); |
|---|
| 1259 | + f2fs_down_write(&sbi->node_write); |
|---|
| 1232 | 1260 | |
|---|
| 1233 | 1261 | if (get_pages(sbi, F2FS_DIRTY_NODES)) { |
|---|
| 1234 | | - up_write(&sbi->node_write); |
|---|
| 1262 | + f2fs_up_write(&sbi->node_write); |
|---|
| 1235 | 1263 | atomic_inc(&sbi->wb_sync_req[NODE]); |
|---|
| 1236 | 1264 | err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); |
|---|
| 1237 | 1265 | atomic_dec(&sbi->wb_sync_req[NODE]); |
|---|
| 1238 | 1266 | if (err) { |
|---|
| 1239 | | - up_write(&sbi->node_change); |
|---|
| 1267 | + f2fs_up_write(&sbi->node_change); |
|---|
| 1240 | 1268 | f2fs_unlock_all(sbi); |
|---|
| 1241 | 1269 | return err; |
|---|
| 1242 | 1270 | } |
|---|
| .. | .. |
|---|
| 1249 | 1277 | * dirty node blocks and some checkpoint values by block allocation. |
|---|
| 1250 | 1278 | */ |
|---|
| 1251 | 1279 | __prepare_cp_block(sbi); |
|---|
| 1252 | | - up_write(&sbi->node_change); |
|---|
| 1280 | + f2fs_up_write(&sbi->node_change); |
|---|
| 1253 | 1281 | return err; |
|---|
| 1254 | 1282 | } |
|---|
| 1255 | 1283 | |
|---|
| 1256 | 1284 | static void unblock_operations(struct f2fs_sb_info *sbi) |
|---|
| 1257 | 1285 | { |
|---|
| 1258 | | - up_write(&sbi->node_write); |
|---|
| 1286 | + f2fs_up_write(&sbi->node_write); |
|---|
| 1259 | 1287 | f2fs_unlock_all(sbi); |
|---|
| 1260 | 1288 | } |
|---|
| 1261 | 1289 | |
|---|
| .. | .. |
|---|
| 1264 | 1292 | DEFINE_WAIT(wait); |
|---|
| 1265 | 1293 | |
|---|
| 1266 | 1294 | for (;;) { |
|---|
| 1267 | | - prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); |
|---|
| 1268 | | - |
|---|
| 1269 | 1295 | if (!get_pages(sbi, type)) |
|---|
| 1270 | 1296 | break; |
|---|
| 1271 | 1297 | |
|---|
| 1272 | | - if (unlikely(f2fs_cp_error(sbi))) |
|---|
| 1298 | + if (unlikely(f2fs_cp_error(sbi) && |
|---|
| 1299 | + !is_sbi_flag_set(sbi, SBI_IS_CLOSE))) |
|---|
| 1273 | 1300 | break; |
|---|
| 1274 | 1301 | |
|---|
| 1275 | 1302 | if (type == F2FS_DIRTY_META) |
|---|
| 1276 | 1303 | f2fs_sync_meta_pages(sbi, META, LONG_MAX, |
|---|
| 1277 | 1304 | FS_CP_META_IO); |
|---|
| 1305 | + else if (type == F2FS_WB_CP_DATA) |
|---|
| 1306 | + f2fs_submit_merged_write(sbi, DATA); |
|---|
| 1307 | + |
|---|
| 1308 | + prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); |
|---|
| 1278 | 1309 | io_schedule_timeout(DEFAULT_IO_TIMEOUT); |
|---|
| 1279 | 1310 | } |
|---|
| 1280 | 1311 | finish_wait(&sbi->cp_wait, &wait); |
|---|
| .. | .. |
|---|
| 1383 | 1414 | f2fs_submit_merged_write(sbi, META_FLUSH); |
|---|
| 1384 | 1415 | } |
|---|
| 1385 | 1416 | |
|---|
| 1417 | +static inline u64 get_sectors_written(struct block_device *bdev) |
|---|
| 1418 | +{ |
|---|
| 1419 | + return (u64)part_stat_read(bdev->bd_part, sectors[STAT_WRITE]); |
|---|
| 1420 | +} |
|---|
| 1421 | + |
|---|
| 1422 | +u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi) |
|---|
| 1423 | +{ |
|---|
| 1424 | + if (f2fs_is_multi_device(sbi)) { |
|---|
| 1425 | + u64 sectors = 0; |
|---|
| 1426 | + int i; |
|---|
| 1427 | + |
|---|
| 1428 | + for (i = 0; i < sbi->s_ndevs; i++) |
|---|
| 1429 | + sectors += get_sectors_written(FDEV(i).bdev); |
|---|
| 1430 | + |
|---|
| 1431 | + return sectors; |
|---|
| 1432 | + } |
|---|
| 1433 | + |
|---|
| 1434 | + return get_sectors_written(sbi->sb->s_bdev); |
|---|
| 1435 | +} |
|---|
| 1436 | + |
|---|
| 1386 | 1437 | static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
|---|
| 1387 | 1438 | { |
|---|
| 1388 | 1439 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
|---|
| .. | .. |
|---|
| 1393 | 1444 | __u32 crc32 = 0; |
|---|
| 1394 | 1445 | int i; |
|---|
| 1395 | 1446 | int cp_payload_blks = __cp_payload(sbi); |
|---|
| 1396 | | - struct super_block *sb = sbi->sb; |
|---|
| 1397 | 1447 | struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); |
|---|
| 1398 | 1448 | u64 kbytes_written; |
|---|
| 1399 | 1449 | int err; |
|---|
| .. | .. |
|---|
| 1421 | 1471 | curseg_alloc_type(sbi, i + CURSEG_HOT_DATA); |
|---|
| 1422 | 1472 | } |
|---|
| 1423 | 1473 | |
|---|
| 1424 | | - /* 2 cp + n data seg summary + orphan inode blocks */ |
|---|
| 1474 | + /* 2 cp + n data seg summary + orphan inode blocks */ |
|---|
| 1425 | 1475 | data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); |
|---|
| 1426 | 1476 | spin_lock_irqsave(&sbi->cp_lock, flags); |
|---|
| 1427 | 1477 | if (data_sum_blocks < NR_CURSEG_DATA_TYPE) |
|---|
| .. | .. |
|---|
| 1435 | 1485 | orphan_blocks); |
|---|
| 1436 | 1486 | |
|---|
| 1437 | 1487 | if (__remain_node_summaries(cpc->reason)) |
|---|
| 1438 | | - ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+ |
|---|
| 1488 | + ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + |
|---|
| 1439 | 1489 | cp_payload_blks + data_sum_blocks + |
|---|
| 1440 | 1490 | orphan_blocks + NR_CURSEG_NODE_TYPE); |
|---|
| 1441 | 1491 | else |
|---|
| .. | .. |
|---|
| 1488 | 1538 | |
|---|
| 1489 | 1539 | /* Record write statistics in the hot node summary */ |
|---|
| 1490 | 1540 | kbytes_written = sbi->kbytes_written; |
|---|
| 1491 | | - if (sb->s_bdev->bd_part) |
|---|
| 1492 | | - kbytes_written += BD_PART_WRITTEN(sbi); |
|---|
| 1493 | | - |
|---|
| 1541 | + kbytes_written += (f2fs_get_sectors_written(sbi) - |
|---|
| 1542 | + sbi->sectors_written_start) >> 1; |
|---|
| 1494 | 1543 | seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); |
|---|
| 1495 | 1544 | |
|---|
| 1496 | 1545 | if (__remain_node_summaries(cpc->reason)) { |
|---|
| .. | .. |
|---|
| 1521 | 1570 | |
|---|
| 1522 | 1571 | /* |
|---|
| 1523 | 1572 | * invalidate intermediate page cache borrowed from meta inode which are |
|---|
| 1524 | | - * used for migration of encrypted or verity inode's blocks. |
|---|
| 1573 | + * used for migration of encrypted, verity or compressed inode's blocks. |
|---|
| 1525 | 1574 | */ |
|---|
| 1526 | | - if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi)) |
|---|
| 1575 | + if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) || |
|---|
| 1576 | + f2fs_sb_has_compression(sbi)) |
|---|
| 1527 | 1577 | invalidate_mapping_pages(META_MAPPING(sbi), |
|---|
| 1528 | 1578 | MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1); |
|---|
| 1529 | 1579 | |
|---|
| .. | .. |
|---|
| 1569 | 1619 | f2fs_warn(sbi, "Start checkpoint disabled!"); |
|---|
| 1570 | 1620 | } |
|---|
| 1571 | 1621 | if (cpc->reason != CP_RESIZE) |
|---|
| 1572 | | - mutex_lock(&sbi->cp_mutex); |
|---|
| 1622 | + f2fs_down_write(&sbi->cp_global_sem); |
|---|
| 1573 | 1623 | |
|---|
| 1574 | 1624 | if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && |
|---|
| 1575 | 1625 | ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || |
|---|
| .. | .. |
|---|
| 1597 | 1647 | goto out; |
|---|
| 1598 | 1648 | } |
|---|
| 1599 | 1649 | |
|---|
| 1600 | | - if (NM_I(sbi)->dirty_nat_cnt == 0 && |
|---|
| 1650 | + if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 && |
|---|
| 1601 | 1651 | SIT_I(sbi)->dirty_sentries == 0 && |
|---|
| 1602 | 1652 | prefree_segments(sbi) == 0) { |
|---|
| 1603 | 1653 | f2fs_flush_sit_entries(sbi, cpc); |
|---|
| .. | .. |
|---|
| 1622 | 1672 | |
|---|
| 1623 | 1673 | f2fs_flush_sit_entries(sbi, cpc); |
|---|
| 1624 | 1674 | |
|---|
| 1675 | + /* save inmem log status */ |
|---|
| 1676 | + f2fs_save_inmem_curseg(sbi); |
|---|
| 1677 | + |
|---|
| 1625 | 1678 | err = do_checkpoint(sbi, cpc); |
|---|
| 1626 | 1679 | if (err) |
|---|
| 1627 | 1680 | f2fs_release_discard_addrs(sbi); |
|---|
| 1628 | 1681 | else |
|---|
| 1629 | 1682 | f2fs_clear_prefree_segments(sbi, cpc); |
|---|
| 1683 | + |
|---|
| 1684 | + f2fs_restore_inmem_curseg(sbi); |
|---|
| 1630 | 1685 | stop: |
|---|
| 1631 | 1686 | unblock_operations(sbi); |
|---|
| 1632 | 1687 | stat_inc_cp_count(sbi->stat_info); |
|---|
| .. | .. |
|---|
| 1639 | 1694 | trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); |
|---|
| 1640 | 1695 | out: |
|---|
| 1641 | 1696 | if (cpc->reason != CP_RESIZE) |
|---|
| 1642 | | - mutex_unlock(&sbi->cp_mutex); |
|---|
| 1697 | + f2fs_up_write(&sbi->cp_global_sem); |
|---|
| 1643 | 1698 | return err; |
|---|
| 1644 | 1699 | } |
|---|
| 1645 | 1700 | |
|---|
| .. | .. |
|---|
| 1657 | 1712 | } |
|---|
| 1658 | 1713 | |
|---|
| 1659 | 1714 | sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS - |
|---|
| 1660 | | - NR_CURSEG_TYPE - __cp_payload(sbi)) * |
|---|
| 1715 | + NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) * |
|---|
| 1661 | 1716 | F2FS_ORPHANS_PER_BLOCK; |
|---|
| 1662 | 1717 | } |
|---|
| 1663 | 1718 | |
|---|
| .. | .. |
|---|
| 1681 | 1736 | kmem_cache_destroy(ino_entry_slab); |
|---|
| 1682 | 1737 | kmem_cache_destroy(f2fs_inode_entry_slab); |
|---|
| 1683 | 1738 | } |
|---|
| 1739 | + |
|---|
| 1740 | +static int __write_checkpoint_sync(struct f2fs_sb_info *sbi) |
|---|
| 1741 | +{ |
|---|
| 1742 | + struct cp_control cpc = { .reason = CP_SYNC, }; |
|---|
| 1743 | + int err; |
|---|
| 1744 | + |
|---|
| 1745 | + f2fs_down_write(&sbi->gc_lock); |
|---|
| 1746 | + err = f2fs_write_checkpoint(sbi, &cpc); |
|---|
| 1747 | + f2fs_up_write(&sbi->gc_lock); |
|---|
| 1748 | + |
|---|
| 1749 | + return err; |
|---|
| 1750 | +} |
|---|
| 1751 | + |
|---|
| 1752 | +static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi) |
|---|
| 1753 | +{ |
|---|
| 1754 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1755 | + struct ckpt_req *req, *next; |
|---|
| 1756 | + struct llist_node *dispatch_list; |
|---|
| 1757 | + u64 sum_diff = 0, diff, count = 0; |
|---|
| 1758 | + int ret; |
|---|
| 1759 | + |
|---|
| 1760 | + dispatch_list = llist_del_all(&cprc->issue_list); |
|---|
| 1761 | + if (!dispatch_list) |
|---|
| 1762 | + return; |
|---|
| 1763 | + dispatch_list = llist_reverse_order(dispatch_list); |
|---|
| 1764 | + |
|---|
| 1765 | + ret = __write_checkpoint_sync(sbi); |
|---|
| 1766 | + atomic_inc(&cprc->issued_ckpt); |
|---|
| 1767 | + |
|---|
| 1768 | + llist_for_each_entry_safe(req, next, dispatch_list, llnode) { |
|---|
| 1769 | + diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time); |
|---|
| 1770 | + req->ret = ret; |
|---|
| 1771 | + complete(&req->wait); |
|---|
| 1772 | + |
|---|
| 1773 | + sum_diff += diff; |
|---|
| 1774 | + count++; |
|---|
| 1775 | + } |
|---|
| 1776 | + atomic_sub(count, &cprc->queued_ckpt); |
|---|
| 1777 | + atomic_add(count, &cprc->total_ckpt); |
|---|
| 1778 | + |
|---|
| 1779 | + spin_lock(&cprc->stat_lock); |
|---|
| 1780 | + cprc->cur_time = (unsigned int)div64_u64(sum_diff, count); |
|---|
| 1781 | + if (cprc->peak_time < cprc->cur_time) |
|---|
| 1782 | + cprc->peak_time = cprc->cur_time; |
|---|
| 1783 | + spin_unlock(&cprc->stat_lock); |
|---|
| 1784 | +} |
|---|
| 1785 | + |
|---|
| 1786 | +static int issue_checkpoint_thread(void *data) |
|---|
| 1787 | +{ |
|---|
| 1788 | + struct f2fs_sb_info *sbi = data; |
|---|
| 1789 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1790 | + wait_queue_head_t *q = &cprc->ckpt_wait_queue; |
|---|
| 1791 | +repeat: |
|---|
| 1792 | + if (kthread_should_stop()) |
|---|
| 1793 | + return 0; |
|---|
| 1794 | + |
|---|
| 1795 | + if (!llist_empty(&cprc->issue_list)) |
|---|
| 1796 | + __checkpoint_and_complete_reqs(sbi); |
|---|
| 1797 | + |
|---|
| 1798 | + wait_event_interruptible(*q, |
|---|
| 1799 | + kthread_should_stop() || !llist_empty(&cprc->issue_list)); |
|---|
| 1800 | + goto repeat; |
|---|
| 1801 | +} |
|---|
| 1802 | + |
|---|
| 1803 | +static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi, |
|---|
| 1804 | + struct ckpt_req *wait_req) |
|---|
| 1805 | +{ |
|---|
| 1806 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1807 | + |
|---|
| 1808 | + if (!llist_empty(&cprc->issue_list)) { |
|---|
| 1809 | + __checkpoint_and_complete_reqs(sbi); |
|---|
| 1810 | + } else { |
|---|
| 1811 | + /* already dispatched by issue_checkpoint_thread */ |
|---|
| 1812 | + if (wait_req) |
|---|
| 1813 | + wait_for_completion(&wait_req->wait); |
|---|
| 1814 | + } |
|---|
| 1815 | +} |
|---|
| 1816 | + |
|---|
| 1817 | +static void init_ckpt_req(struct ckpt_req *req) |
|---|
| 1818 | +{ |
|---|
| 1819 | + memset(req, 0, sizeof(struct ckpt_req)); |
|---|
| 1820 | + |
|---|
| 1821 | + init_completion(&req->wait); |
|---|
| 1822 | + req->queue_time = ktime_get(); |
|---|
| 1823 | +} |
|---|
| 1824 | + |
|---|
| 1825 | +int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) |
|---|
| 1826 | +{ |
|---|
| 1827 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1828 | + struct ckpt_req req; |
|---|
| 1829 | + struct cp_control cpc; |
|---|
| 1830 | + |
|---|
| 1831 | + cpc.reason = __get_cp_reason(sbi); |
|---|
| 1832 | + if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) { |
|---|
| 1833 | + int ret; |
|---|
| 1834 | + |
|---|
| 1835 | + f2fs_down_write(&sbi->gc_lock); |
|---|
| 1836 | + ret = f2fs_write_checkpoint(sbi, &cpc); |
|---|
| 1837 | + f2fs_up_write(&sbi->gc_lock); |
|---|
| 1838 | + |
|---|
| 1839 | + return ret; |
|---|
| 1840 | + } |
|---|
| 1841 | + |
|---|
| 1842 | + if (!cprc->f2fs_issue_ckpt) |
|---|
| 1843 | + return __write_checkpoint_sync(sbi); |
|---|
| 1844 | + |
|---|
| 1845 | + init_ckpt_req(&req); |
|---|
| 1846 | + |
|---|
| 1847 | + llist_add(&req.llnode, &cprc->issue_list); |
|---|
| 1848 | + atomic_inc(&cprc->queued_ckpt); |
|---|
| 1849 | + |
|---|
| 1850 | + /* |
|---|
| 1851 | + * update issue_list before we wake up issue_checkpoint thread, |
|---|
| 1852 | + * this smp_mb() pairs with another barrier in ___wait_event(), |
|---|
| 1853 | + * see more details in comments of waitqueue_active(). |
|---|
| 1854 | + */ |
|---|
| 1855 | + smp_mb(); |
|---|
| 1856 | + |
|---|
| 1857 | + if (waitqueue_active(&cprc->ckpt_wait_queue)) |
|---|
| 1858 | + wake_up(&cprc->ckpt_wait_queue); |
|---|
| 1859 | + |
|---|
| 1860 | + if (cprc->f2fs_issue_ckpt) |
|---|
| 1861 | + wait_for_completion(&req.wait); |
|---|
| 1862 | + else |
|---|
| 1863 | + flush_remained_ckpt_reqs(sbi, &req); |
|---|
| 1864 | + |
|---|
| 1865 | + return req.ret; |
|---|
| 1866 | +} |
|---|
| 1867 | + |
|---|
| 1868 | +int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi) |
|---|
| 1869 | +{ |
|---|
| 1870 | + dev_t dev = sbi->sb->s_bdev->bd_dev; |
|---|
| 1871 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1872 | + |
|---|
| 1873 | + if (cprc->f2fs_issue_ckpt) |
|---|
| 1874 | + return 0; |
|---|
| 1875 | + |
|---|
| 1876 | + cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi, |
|---|
| 1877 | + "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev)); |
|---|
| 1878 | + if (IS_ERR(cprc->f2fs_issue_ckpt)) { |
|---|
| 1879 | + cprc->f2fs_issue_ckpt = NULL; |
|---|
| 1880 | + return -ENOMEM; |
|---|
| 1881 | + } |
|---|
| 1882 | + |
|---|
| 1883 | + set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio); |
|---|
| 1884 | + |
|---|
| 1885 | + return 0; |
|---|
| 1886 | +} |
|---|
| 1887 | + |
|---|
| 1888 | +void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi) |
|---|
| 1889 | +{ |
|---|
| 1890 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1891 | + struct task_struct *ckpt_task; |
|---|
| 1892 | + |
|---|
| 1893 | + if (!cprc->f2fs_issue_ckpt) |
|---|
| 1894 | + return; |
|---|
| 1895 | + |
|---|
| 1896 | + ckpt_task = cprc->f2fs_issue_ckpt; |
|---|
| 1897 | + cprc->f2fs_issue_ckpt = NULL; |
|---|
| 1898 | + kthread_stop(ckpt_task); |
|---|
| 1899 | + |
|---|
| 1900 | + f2fs_flush_ckpt_thread(sbi); |
|---|
| 1901 | +} |
|---|
| 1902 | + |
|---|
| 1903 | +void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi) |
|---|
| 1904 | +{ |
|---|
| 1905 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1906 | + |
|---|
| 1907 | + flush_remained_ckpt_reqs(sbi, NULL); |
|---|
| 1908 | + |
|---|
| 1909 | + /* Let's wait for the previous dispatched checkpoint. */ |
|---|
| 1910 | + while (atomic_read(&cprc->queued_ckpt)) |
|---|
| 1911 | + io_schedule_timeout(DEFAULT_IO_TIMEOUT); |
|---|
| 1912 | +} |
|---|
| 1913 | + |
|---|
| 1914 | +void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi) |
|---|
| 1915 | +{ |
|---|
| 1916 | + struct ckpt_req_control *cprc = &sbi->cprc_info; |
|---|
| 1917 | + |
|---|
| 1918 | + atomic_set(&cprc->issued_ckpt, 0); |
|---|
| 1919 | + atomic_set(&cprc->total_ckpt, 0); |
|---|
| 1920 | + atomic_set(&cprc->queued_ckpt, 0); |
|---|
| 1921 | + cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO; |
|---|
| 1922 | + init_waitqueue_head(&cprc->ckpt_wait_queue); |
|---|
| 1923 | + init_llist_head(&cprc->issue_list); |
|---|
| 1924 | + spin_lock_init(&cprc->stat_lock); |
|---|
| 1925 | +} |
|---|