Commit | Line | Data |
---|---|---|
7c1a000d | 1 | // SPDX-License-Identifier: GPL-2.0 |
0a8165d7 | 2 | /* |
127e670a JK |
3 | * fs/f2fs/checkpoint.c |
4 | * | |
5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. | |
6 | * http://www.samsung.com/ | |
127e670a JK |
7 | */ |
8 | #include <linux/fs.h> | |
9 | #include <linux/bio.h> | |
10 | #include <linux/mpage.h> | |
11 | #include <linux/writeback.h> | |
12 | #include <linux/blkdev.h> | |
13 | #include <linux/f2fs_fs.h> | |
14 | #include <linux/pagevec.h> | |
15 | #include <linux/swap.h> | |
261eeb9c | 16 | #include <linux/kthread.h> |
127e670a JK |
17 | |
18 | #include "f2fs.h" | |
19 | #include "node.h" | |
20 | #include "segment.h" | |
52118743 | 21 | #include "iostat.h" |
2af4bd6c | 22 | #include <trace/events/f2fs.h> |
127e670a | 23 | |
8a2d9f00 | 24 | #define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 3)) |
261eeb9c | 25 | |
6451e041 | 26 | static struct kmem_cache *ino_entry_slab; |
4d57b86d | 27 | struct kmem_cache *f2fs_inode_entry_slab; |
127e670a | 28 | |
a9cfee0e CY |
29 | void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io, |
30 | unsigned char reason) | |
38f91ca8 | 31 | { |
2be96c21 | 32 | f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL); |
b62e71be | 33 | if (!end_io) |
b9109b0e | 34 | f2fs_flush_merged_writes(sbi); |
f10a8903 | 35 | f2fs_handle_critical_error(sbi, reason); |
38f91ca8 JK |
36 | } |
37 | ||
0a8165d7 | 38 | /* |
127e670a JK |
39 | * We guarantee no failure on the returned page. |
40 | */ | |
b15ca185 | 41 | struct folio *f2fs_grab_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index) |
127e670a | 42 | { |
9df27d98 | 43 | struct address_space *mapping = META_MAPPING(sbi); |
b15ca185 | 44 | struct folio *folio; |
127e670a | 45 | repeat: |
b15ca185 MWO |
46 | folio = f2fs_grab_cache_folio(mapping, index, false); |
47 | if (IS_ERR(folio)) { | |
127e670a JK |
48 | cond_resched(); |
49 | goto repeat; | |
50 | } | |
b15ca185 MWO |
51 | f2fs_folio_wait_writeback(folio, META, true, true); |
52 | if (!folio_test_uptodate(folio)) | |
53 | folio_mark_uptodate(folio); | |
54 | return folio; | |
127e670a JK |
55 | } |
56 | ||
9030d55a | 57 | static struct folio *__get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index, |
2b947003 | 58 | bool is_meta) |
127e670a | 59 | { |
9df27d98 | 60 | struct address_space *mapping = META_MAPPING(sbi); |
922e24ac | 61 | struct folio *folio; |
cf04e8eb | 62 | struct f2fs_io_info fio = { |
05ca3632 | 63 | .sbi = sbi, |
cf04e8eb | 64 | .type = META, |
04d328de | 65 | .op = REQ_OP_READ, |
70fd7614 | 66 | .op_flags = REQ_META | REQ_PRIO, |
7a9d7548 CY |
67 | .old_blkaddr = index, |
68 | .new_blkaddr = index, | |
4375a336 | 69 | .encrypted_page = NULL, |
2eae077e | 70 | .is_por = !is_meta ? 1 : 0, |
cf04e8eb | 71 | }; |
7735730d | 72 | int err; |
2b947003 CY |
73 | |
74 | if (unlikely(!is_meta)) | |
04d328de | 75 | fio.op_flags &= ~REQ_META; |
127e670a | 76 | repeat: |
922e24ac MWO |
77 | folio = f2fs_grab_cache_folio(mapping, index, false); |
78 | if (IS_ERR(folio)) { | |
127e670a JK |
79 | cond_resched(); |
80 | goto repeat; | |
81 | } | |
922e24ac | 82 | if (folio_test_uptodate(folio)) |
393ff91f JK |
83 | goto out; |
84 | ||
922e24ac | 85 | fio.page = &folio->page; |
05ca3632 | 86 | |
7735730d CY |
87 | err = f2fs_submit_page_bio(&fio); |
88 | if (err) { | |
922e24ac | 89 | f2fs_folio_put(folio, true); |
7735730d | 90 | return ERR_PTR(err); |
86531d6b | 91 | } |
127e670a | 92 | |
34a23525 | 93 | f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE); |
8b83ac81 | 94 | |
922e24ac | 95 | folio_lock(folio); |
019a8912 | 96 | if (unlikely(!is_meta_folio(folio))) { |
922e24ac | 97 | f2fs_folio_put(folio, true); |
afcb7ca0 JK |
98 | goto repeat; |
99 | } | |
f3f338ca | 100 | |
922e24ac MWO |
101 | if (unlikely(!folio_test_uptodate(folio))) { |
102 | f2fs_handle_page_eio(sbi, folio, META); | |
103 | f2fs_folio_put(folio, true); | |
7735730d | 104 | return ERR_PTR(-EIO); |
81114baa | 105 | } |
393ff91f | 106 | out: |
9030d55a | 107 | return folio; |
127e670a JK |
108 | } |
109 | ||
d6f30663 | 110 | struct folio *f2fs_get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index) |
2b947003 | 111 | { |
d6f30663 | 112 | return __get_meta_folio(sbi, index, true); |
2b947003 CY |
113 | } |
114 | ||
350b8441 | 115 | struct folio *f2fs_get_meta_folio_retry(struct f2fs_sb_info *sbi, pgoff_t index) |
7735730d | 116 | { |
9030d55a | 117 | struct folio *folio; |
7735730d CY |
118 | int count = 0; |
119 | ||
120 | retry: | |
9030d55a MWO |
121 | folio = __get_meta_folio(sbi, index, true); |
122 | if (IS_ERR(folio)) { | |
123 | if (PTR_ERR(folio) == -EIO && | |
7735730d CY |
124 | ++count <= DEFAULT_RETRY_IO_COUNT) |
125 | goto retry; | |
a9cfee0e | 126 | f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE); |
7735730d | 127 | } |
350b8441 | 128 | return folio; |
7735730d CY |
129 | } |
130 | ||
2b947003 | 131 | /* for POR only */ |
937d6a4d | 132 | struct folio *f2fs_get_tmp_folio(struct f2fs_sb_info *sbi, pgoff_t index) |
2b947003 | 133 | { |
937d6a4d | 134 | return __get_meta_folio(sbi, index, false); |
2b947003 CY |
135 | } |
136 | ||
93770ab7 CY |
137 | static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr, |
138 | int type) | |
139 | { | |
140 | struct seg_entry *se; | |
141 | unsigned int segno, offset; | |
142 | bool exist; | |
143 | ||
0ef4ca04 | 144 | if (type == DATA_GENERIC) |
93770ab7 CY |
145 | return true; |
146 | ||
147 | segno = GET_SEGNO(sbi, blkaddr); | |
148 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); | |
149 | se = get_seg_entry(sbi, segno); | |
150 | ||
151 | exist = f2fs_test_bit(offset, se->cur_valid_map); | |
bd90c5cd JK |
152 | |
153 | /* skip data, if we already have an error in checkpoint. */ | |
154 | if (unlikely(f2fs_cp_error(sbi))) | |
155 | return exist; | |
156 | ||
31f85ccc ZN |
157 | if ((exist && type == DATA_GENERIC_ENHANCE_UPDATE) || |
158 | (!exist && type == DATA_GENERIC_ENHANCE)) | |
159 | goto out_err; | |
160 | if (!exist && type != DATA_GENERIC_ENHANCE_UPDATE) | |
161 | goto out_handle; | |
162 | return exist; | |
0ef4ca04 | 163 | |
31f85ccc ZN |
164 | out_err: |
165 | f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d", | |
166 | blkaddr, exist); | |
167 | set_sbi_flag(sbi, SBI_NEED_FSCK); | |
168 | dump_stack(); | |
169 | out_handle: | |
170 | f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); | |
93770ab7 CY |
171 | return exist; |
172 | } | |
173 | ||
c7115e09 | 174 | static bool __f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, |
4d57b86d | 175 | block_t blkaddr, int type) |
662befda CY |
176 | { |
177 | switch (type) { | |
178 | case META_NAT: | |
66b00c18 | 179 | break; |
662befda | 180 | case META_SIT: |
66b00c18 | 181 | if (unlikely(blkaddr >= SIT_BLK_CNT(sbi))) |
b864ddb5 | 182 | goto check_only; |
66b00c18 | 183 | break; |
81c1a0f1 | 184 | case META_SSA: |
66b00c18 CY |
185 | if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) || |
186 | blkaddr < SM_I(sbi)->ssa_blkaddr)) | |
b864ddb5 | 187 | goto check_only; |
66b00c18 | 188 | break; |
662befda | 189 | case META_CP: |
66b00c18 CY |
190 | if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr || |
191 | blkaddr < __start_cp_addr(sbi))) | |
b864ddb5 | 192 | goto check_only; |
66b00c18 | 193 | break; |
4c521f49 | 194 | case META_POR: |
93770ab7 CY |
195 | if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || |
196 | blkaddr < MAIN_BLKADDR(sbi))) | |
b864ddb5 | 197 | goto check_only; |
93770ab7 | 198 | break; |
e1da7872 | 199 | case DATA_GENERIC: |
93770ab7 CY |
200 | case DATA_GENERIC_ENHANCE: |
201 | case DATA_GENERIC_ENHANCE_READ: | |
0ef4ca04 | 202 | case DATA_GENERIC_ENHANCE_UPDATE: |
66b00c18 | 203 | if (unlikely(blkaddr >= MAX_BLKADDR(sbi) || |
93770ab7 | 204 | blkaddr < MAIN_BLKADDR(sbi))) { |
bd90c5cd JK |
205 | |
206 | /* Skip to emit an error message. */ | |
207 | if (unlikely(f2fs_cp_error(sbi))) | |
208 | return false; | |
209 | ||
dcbb4c10 JP |
210 | f2fs_warn(sbi, "access invalid blkaddr:%u", |
211 | blkaddr); | |
93770ab7 | 212 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
dc2f78e2 | 213 | dump_stack(); |
31f85ccc | 214 | goto err; |
93770ab7 CY |
215 | } else { |
216 | return __is_bitmap_valid(sbi, blkaddr, type); | |
c9b60788 | 217 | } |
66b00c18 | 218 | break; |
e1da7872 CY |
219 | case META_GENERIC: |
220 | if (unlikely(blkaddr < SEG0_BLKADDR(sbi) || | |
221 | blkaddr >= MAIN_BLKADDR(sbi))) | |
31f85ccc | 222 | goto err; |
e1da7872 | 223 | break; |
662befda CY |
224 | default: |
225 | BUG(); | |
226 | } | |
66b00c18 CY |
227 | |
228 | return true; | |
31f85ccc ZN |
229 | err: |
230 | f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); | |
b864ddb5 | 231 | check_only: |
31f85ccc | 232 | return false; |
662befda CY |
233 | } |
234 | ||
c7115e09 CY |
235 | bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, |
236 | block_t blkaddr, int type) | |
237 | { | |
238 | if (time_to_inject(sbi, FAULT_BLKADDR_VALIDITY)) | |
239 | return false; | |
240 | return __f2fs_is_valid_blkaddr(sbi, blkaddr, type); | |
241 | } | |
242 | ||
243 | bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi, | |
244 | block_t blkaddr, int type) | |
245 | { | |
246 | return __f2fs_is_valid_blkaddr(sbi, blkaddr, type); | |
247 | } | |
248 | ||
662befda | 249 | /* |
7a88ddb5 | 250 | * Readahead CP/NAT/SIT/SSA/POR pages |
662befda | 251 | */ |
4d57b86d | 252 | int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, |
26879fb1 | 253 | int type, bool sync) |
662befda | 254 | { |
4c521f49 | 255 | block_t blkno = start; |
662befda | 256 | struct f2fs_io_info fio = { |
05ca3632 | 257 | .sbi = sbi, |
662befda | 258 | .type = META, |
04d328de | 259 | .op = REQ_OP_READ, |
70fd7614 | 260 | .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD, |
4375a336 | 261 | .encrypted_page = NULL, |
2eae077e CY |
262 | .in_list = 0, |
263 | .is_por = (type == META_POR) ? 1 : 0, | |
662befda | 264 | }; |
e9f5b8b8 | 265 | struct blk_plug plug; |
ce4c638c | 266 | int err; |
662befda | 267 | |
2b947003 | 268 | if (unlikely(type == META_POR)) |
04d328de | 269 | fio.op_flags &= ~REQ_META; |
2b947003 | 270 | |
e9f5b8b8 | 271 | blk_start_plug(&plug); |
662befda | 272 | for (; nrpages-- > 0; blkno++) { |
95e31176 | 273 | struct folio *folio; |
662befda | 274 | |
e1da7872 | 275 | if (!f2fs_is_valid_blkaddr(sbi, blkno, type)) |
66b00c18 CY |
276 | goto out; |
277 | ||
662befda CY |
278 | switch (type) { |
279 | case META_NAT: | |
66b00c18 CY |
280 | if (unlikely(blkno >= |
281 | NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))) | |
662befda | 282 | blkno = 0; |
66b00c18 | 283 | /* get nat block addr */ |
7a9d7548 | 284 | fio.new_blkaddr = current_nat_addr(sbi, |
662befda CY |
285 | blkno * NAT_ENTRY_PER_BLOCK); |
286 | break; | |
287 | case META_SIT: | |
6a257471 CY |
288 | if (unlikely(blkno >= TOTAL_SEGS(sbi))) |
289 | goto out; | |
662befda | 290 | /* get sit block addr */ |
7a9d7548 | 291 | fio.new_blkaddr = current_sit_addr(sbi, |
662befda | 292 | blkno * SIT_ENTRY_PER_BLOCK); |
662befda | 293 | break; |
81c1a0f1 | 294 | case META_SSA: |
662befda | 295 | case META_CP: |
4c521f49 | 296 | case META_POR: |
7a9d7548 | 297 | fio.new_blkaddr = blkno; |
662befda CY |
298 | break; |
299 | default: | |
300 | BUG(); | |
301 | } | |
302 | ||
95e31176 | 303 | folio = f2fs_grab_cache_folio(META_MAPPING(sbi), |
300e129c | 304 | fio.new_blkaddr, false); |
95e31176 | 305 | if (IS_ERR(folio)) |
662befda | 306 | continue; |
95e31176 MWO |
307 | if (folio_test_uptodate(folio)) { |
308 | f2fs_folio_put(folio, true); | |
662befda CY |
309 | continue; |
310 | } | |
311 | ||
95e31176 | 312 | fio.page = &folio->page; |
ce4c638c | 313 | err = f2fs_submit_page_bio(&fio); |
95e31176 | 314 | f2fs_folio_put(folio, err ? true : false); |
8b83ac81 CY |
315 | |
316 | if (!err) | |
34a23525 CY |
317 | f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, |
318 | F2FS_BLKSIZE); | |
662befda CY |
319 | } |
320 | out: | |
e9f5b8b8 | 321 | blk_finish_plug(&plug); |
662befda CY |
322 | return blkno - start; |
323 | } | |
324 | ||
430f163b CY |
325 | void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index, |
326 | unsigned int ra_blocks) | |
635aee1f | 327 | { |
2525a784 | 328 | struct folio *folio; |
635aee1f CY |
329 | bool readahead = false; |
330 | ||
430f163b CY |
331 | if (ra_blocks == RECOVERY_MIN_RA_BLOCKS) |
332 | return; | |
333 | ||
2525a784 MWO |
334 | folio = filemap_get_folio(META_MAPPING(sbi), index); |
335 | if (IS_ERR(folio) || !folio_test_uptodate(folio)) | |
635aee1f | 336 | readahead = true; |
2525a784 | 337 | f2fs_folio_put(folio, false); |
635aee1f CY |
338 | |
339 | if (readahead) | |
430f163b | 340 | f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true); |
635aee1f CY |
341 | } |
342 | ||
39122e45 | 343 | static bool __f2fs_write_meta_folio(struct folio *folio, |
b0af6d49 CY |
344 | struct writeback_control *wbc, |
345 | enum iostat_type io_type) | |
127e670a | 346 | { |
a8d39738 | 347 | struct f2fs_sb_info *sbi = F2FS_F_SB(folio); |
127e670a | 348 | |
138a762e | 349 | trace_f2fs_writepage(folio, META); |
ecda0de3 | 350 | |
c9b3649a CY |
351 | if (unlikely(f2fs_cp_error(sbi))) { |
352 | if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) { | |
138a762e | 353 | folio_clear_uptodate(folio); |
c9b3649a | 354 | dec_page_count(sbi, F2FS_DIRTY_META); |
138a762e | 355 | folio_unlock(folio); |
39122e45 | 356 | return true; |
c9b3649a | 357 | } |
af697c0f | 358 | goto redirty_out; |
c9b3649a | 359 | } |
caf0047e | 360 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
cfb271d4 | 361 | goto redirty_out; |
127e670a | 362 | |
138a762e | 363 | f2fs_do_write_meta_page(sbi, folio, io_type); |
577e3495 | 364 | dec_page_count(sbi, F2FS_DIRTY_META); |
0c3a5797 | 365 | |
138a762e | 366 | folio_unlock(folio); |
857dc4e0 | 367 | |
0c3a5797 | 368 | if (unlikely(f2fs_cp_error(sbi))) |
b9109b0e | 369 | f2fs_submit_merged_write(sbi, META); |
0c3a5797 | 370 | |
39122e45 | 371 | return true; |
cfb271d4 CY |
372 | |
373 | redirty_out: | |
a8d39738 | 374 | folio_redirty_for_writepage(wbc, folio); |
39122e45 | 375 | return false; |
127e670a JK |
376 | } |
377 | ||
378 | static int f2fs_write_meta_pages(struct address_space *mapping, | |
379 | struct writeback_control *wbc) | |
380 | { | |
4081363f | 381 | struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); |
50c8cdb3 | 382 | long diff, written; |
127e670a | 383 | |
0771fcc7 CY |
384 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
385 | goto skip_write; | |
386 | ||
5459aa97 | 387 | /* collect a number of dirty meta pages and write together */ |
812a9597 JK |
388 | if (wbc->sync_mode != WB_SYNC_ALL && |
389 | get_pages(sbi, F2FS_DIRTY_META) < | |
390 | nr_pages_to_skip(sbi, META)) | |
d3baf95d | 391 | goto skip_write; |
127e670a | 392 | |
a29d0e0b | 393 | /* if locked failed, cp will flush dirty pages instead */ |
e4544b63 | 394 | if (!f2fs_down_write_trylock(&sbi->cp_global_sem)) |
a29d0e0b | 395 | goto skip_write; |
d31c7c3f | 396 | |
a29d0e0b | 397 | trace_f2fs_writepages(mapping->host, wbc, META); |
50c8cdb3 | 398 | diff = nr_pages_to_write(sbi, META, wbc); |
4d57b86d | 399 | written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); |
e4544b63 | 400 | f2fs_up_write(&sbi->cp_global_sem); |
50c8cdb3 | 401 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); |
127e670a | 402 | return 0; |
d3baf95d JK |
403 | |
404 | skip_write: | |
405 | wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META); | |
d31c7c3f | 406 | trace_f2fs_writepages(mapping->host, wbc, META); |
d3baf95d | 407 | return 0; |
127e670a JK |
408 | } |
409 | ||
4d57b86d | 410 | long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, |
b0af6d49 | 411 | long nr_to_write, enum iostat_type io_type) |
127e670a | 412 | { |
9df27d98 | 413 | struct address_space *mapping = META_MAPPING(sbi); |
028a63a6 | 414 | pgoff_t index = 0, prev = ULONG_MAX; |
580e7a49 | 415 | struct folio_batch fbatch; |
127e670a | 416 | long nwritten = 0; |
580e7a49 | 417 | int nr_folios; |
402dd9f0 | 418 | struct writeback_control wbc = {}; |
e9f5b8b8 | 419 | struct blk_plug plug; |
127e670a | 420 | |
580e7a49 | 421 | folio_batch_init(&fbatch); |
127e670a | 422 | |
e9f5b8b8 CY |
423 | blk_start_plug(&plug); |
424 | ||
580e7a49 VMO |
425 | while ((nr_folios = filemap_get_folios_tag(mapping, &index, |
426 | (pgoff_t)-1, | |
427 | PAGECACHE_TAG_DIRTY, &fbatch))) { | |
028a63a6 | 428 | int i; |
127e670a | 429 | |
580e7a49 VMO |
430 | for (i = 0; i < nr_folios; i++) { |
431 | struct folio *folio = fbatch.folios[i]; | |
203681f6 | 432 | |
580e7a49 VMO |
433 | if (nr_to_write != LONG_MAX && i != 0 && |
434 | folio->index != prev + | |
435 | folio_nr_pages(fbatch.folios[i-1])) { | |
436 | folio_batch_release(&fbatch); | |
6066d8cd JK |
437 | goto stop; |
438 | } | |
439 | ||
580e7a49 | 440 | folio_lock(folio); |
203681f6 | 441 | |
019a8912 | 442 | if (unlikely(!is_meta_folio(folio))) { |
203681f6 | 443 | continue_unlock: |
580e7a49 | 444 | folio_unlock(folio); |
203681f6 JK |
445 | continue; |
446 | } | |
580e7a49 | 447 | if (!folio_test_dirty(folio)) { |
203681f6 JK |
448 | /* someone wrote it for us */ |
449 | goto continue_unlock; | |
450 | } | |
451 | ||
46fd261c | 452 | f2fs_folio_wait_writeback(folio, META, true, true); |
fa3d2bdf | 453 | |
580e7a49 | 454 | if (!folio_clear_dirty_for_io(folio)) |
203681f6 JK |
455 | goto continue_unlock; |
456 | ||
39122e45 | 457 | if (!__f2fs_write_meta_folio(folio, &wbc, |
580e7a49 VMO |
458 | io_type)) { |
459 | folio_unlock(folio); | |
577e3495 JK |
460 | break; |
461 | } | |
580e7a49 VMO |
462 | nwritten += folio_nr_pages(folio); |
463 | prev = folio->index; | |
cfb271d4 | 464 | if (unlikely(nwritten >= nr_to_write)) |
127e670a JK |
465 | break; |
466 | } | |
580e7a49 | 467 | folio_batch_release(&fbatch); |
127e670a JK |
468 | cond_resched(); |
469 | } | |
6066d8cd | 470 | stop: |
127e670a | 471 | if (nwritten) |
b9109b0e | 472 | f2fs_submit_merged_write(sbi, type); |
127e670a | 473 | |
e9f5b8b8 CY |
474 | blk_finish_plug(&plug); |
475 | ||
127e670a JK |
476 | return nwritten; |
477 | } | |
478 | ||
1d9ac659 MWO |
479 | static bool f2fs_dirty_meta_folio(struct address_space *mapping, |
480 | struct folio *folio) | |
127e670a | 481 | { |
92f750d8 | 482 | trace_f2fs_set_page_dirty(folio, META); |
1d9ac659 MWO |
483 | |
484 | if (!folio_test_uptodate(folio)) | |
485 | folio_mark_uptodate(folio); | |
9b7eadd9 | 486 | if (filemap_dirty_folio(mapping, folio)) { |
29c87793 | 487 | inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META); |
1d9ac659 MWO |
488 | set_page_private_reference(&folio->page); |
489 | return true; | |
127e670a | 490 | } |
1d9ac659 | 491 | return false; |
127e670a JK |
492 | } |
493 | ||
494 | const struct address_space_operations f2fs_meta_aops = { | |
127e670a | 495 | .writepages = f2fs_write_meta_pages, |
1d9ac659 | 496 | .dirty_folio = f2fs_dirty_meta_folio, |
91503996 | 497 | .invalidate_folio = f2fs_invalidate_folio, |
c26cd045 | 498 | .release_folio = f2fs_release_folio, |
1d5b9bd6 | 499 | .migrate_folio = filemap_migrate_folio, |
127e670a JK |
500 | }; |
501 | ||
39d787be CY |
502 | static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, |
503 | unsigned int devidx, int type) | |
953e6cc6 | 504 | { |
67298804 | 505 | struct inode_management *im = &sbi->im[type]; |
4b106518 | 506 | struct ino_entry *e = NULL, *new = NULL; |
9b6fc988 | 507 | int ret; |
80c54505 | 508 | |
4b106518 CY |
509 | if (type == FLUSH_INO) { |
510 | rcu_read_lock(); | |
511 | e = radix_tree_lookup(&im->ino_root, ino); | |
512 | rcu_read_unlock(); | |
513 | } | |
514 | ||
515 | retry: | |
516 | if (!e) | |
32410577 CY |
517 | new = f2fs_kmem_cache_alloc(ino_entry_slab, |
518 | GFP_NOFS, true, NULL); | |
19526d74 | 519 | |
9b6fc988 CY |
520 | ret = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); |
521 | f2fs_bug_on(sbi, ret); | |
769ec6e5 | 522 | |
67298804 | 523 | spin_lock(&im->ino_lock); |
67298804 | 524 | e = radix_tree_lookup(&im->ino_root, ino); |
39efac41 | 525 | if (!e) { |
4b106518 CY |
526 | if (!new) { |
527 | spin_unlock(&im->ino_lock); | |
d09bd853 | 528 | radix_tree_preload_end(); |
4b106518 CY |
529 | goto retry; |
530 | } | |
531 | e = new; | |
19526d74 CY |
532 | if (unlikely(radix_tree_insert(&im->ino_root, ino, e))) |
533 | f2fs_bug_on(sbi, 1); | |
534 | ||
39efac41 JK |
535 | memset(e, 0, sizeof(struct ino_entry)); |
536 | e->ino = ino; | |
953e6cc6 | 537 | |
67298804 | 538 | list_add_tail(&e->list, &im->ino_list); |
8c402946 | 539 | if (type != ORPHAN_INO) |
67298804 | 540 | im->ino_num++; |
39efac41 | 541 | } |
39d787be CY |
542 | |
543 | if (type == FLUSH_INO) | |
544 | f2fs_set_bit(devidx, (char *)&e->dirty_device); | |
545 | ||
67298804 | 546 | spin_unlock(&im->ino_lock); |
769ec6e5 | 547 | radix_tree_preload_end(); |
80c54505 | 548 | |
4b106518 CY |
549 | if (new && e != new) |
550 | kmem_cache_free(ino_entry_slab, new); | |
953e6cc6 JK |
551 | } |
552 | ||
6451e041 | 553 | static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) |
953e6cc6 | 554 | { |
67298804 | 555 | struct inode_management *im = &sbi->im[type]; |
6451e041 | 556 | struct ino_entry *e; |
953e6cc6 | 557 | |
67298804 CY |
558 | spin_lock(&im->ino_lock); |
559 | e = radix_tree_lookup(&im->ino_root, ino); | |
39efac41 JK |
560 | if (e) { |
561 | list_del(&e->list); | |
67298804 CY |
562 | radix_tree_delete(&im->ino_root, ino); |
563 | im->ino_num--; | |
564 | spin_unlock(&im->ino_lock); | |
39efac41 JK |
565 | kmem_cache_free(ino_entry_slab, e); |
566 | return; | |
953e6cc6 | 567 | } |
67298804 | 568 | spin_unlock(&im->ino_lock); |
953e6cc6 JK |
569 | } |
570 | ||
4d57b86d | 571 | void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) |
fff04f90 JK |
572 | { |
573 | /* add new dirty ino entry into list */ | |
39d787be | 574 | __add_ino_entry(sbi, ino, 0, type); |
fff04f90 JK |
575 | } |
576 | ||
4d57b86d | 577 | void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type) |
fff04f90 JK |
578 | { |
579 | /* remove dirty ino entry from list */ | |
580 | __remove_ino_entry(sbi, ino, type); | |
581 | } | |
582 | ||
1f07cc58 | 583 | /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */ |
4d57b86d | 584 | bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode) |
fff04f90 | 585 | { |
67298804 | 586 | struct inode_management *im = &sbi->im[mode]; |
fff04f90 | 587 | struct ino_entry *e; |
67298804 CY |
588 | |
589 | spin_lock(&im->ino_lock); | |
590 | e = radix_tree_lookup(&im->ino_root, ino); | |
591 | spin_unlock(&im->ino_lock); | |
fff04f90 JK |
592 | return e ? true : false; |
593 | } | |
594 | ||
4d57b86d | 595 | void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all) |
fff04f90 JK |
596 | { |
597 | struct ino_entry *e, *tmp; | |
598 | int i; | |
599 | ||
39d787be | 600 | for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) { |
67298804 CY |
601 | struct inode_management *im = &sbi->im[i]; |
602 | ||
603 | spin_lock(&im->ino_lock); | |
604 | list_for_each_entry_safe(e, tmp, &im->ino_list, list) { | |
fff04f90 | 605 | list_del(&e->list); |
67298804 | 606 | radix_tree_delete(&im->ino_root, e->ino); |
fff04f90 | 607 | kmem_cache_free(ino_entry_slab, e); |
67298804 | 608 | im->ino_num--; |
fff04f90 | 609 | } |
67298804 | 610 | spin_unlock(&im->ino_lock); |
fff04f90 JK |
611 | } |
612 | } | |
613 | ||
4d57b86d | 614 | void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, |
39d787be CY |
615 | unsigned int devidx, int type) |
616 | { | |
617 | __add_ino_entry(sbi, ino, devidx, type); | |
618 | } | |
619 | ||
4d57b86d | 620 | bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, |
39d787be CY |
621 | unsigned int devidx, int type) |
622 | { | |
623 | struct inode_management *im = &sbi->im[type]; | |
624 | struct ino_entry *e; | |
625 | bool is_dirty = false; | |
626 | ||
627 | spin_lock(&im->ino_lock); | |
628 | e = radix_tree_lookup(&im->ino_root, ino); | |
629 | if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device)) | |
630 | is_dirty = true; | |
631 | spin_unlock(&im->ino_lock); | |
632 | return is_dirty; | |
633 | } | |
634 | ||
4d57b86d | 635 | int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi) |
127e670a | 636 | { |
67298804 | 637 | struct inode_management *im = &sbi->im[ORPHAN_INO]; |
127e670a JK |
638 | int err = 0; |
639 | ||
67298804 | 640 | spin_lock(&im->ino_lock); |
cb78942b | 641 | |
1ecc0c5c | 642 | if (time_to_inject(sbi, FAULT_ORPHAN)) { |
cb78942b JK |
643 | spin_unlock(&im->ino_lock); |
644 | return -ENOSPC; | |
645 | } | |
7fa750a1 | 646 | |
67298804 | 647 | if (unlikely(im->ino_num >= sbi->max_orphans)) |
127e670a | 648 | err = -ENOSPC; |
cbd56e7d | 649 | else |
67298804 CY |
650 | im->ino_num++; |
651 | spin_unlock(&im->ino_lock); | |
0d47c1ad | 652 | |
127e670a JK |
653 | return err; |
654 | } | |
655 | ||
4d57b86d | 656 | void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi) |
cbd56e7d | 657 | { |
67298804 CY |
658 | struct inode_management *im = &sbi->im[ORPHAN_INO]; |
659 | ||
660 | spin_lock(&im->ino_lock); | |
661 | f2fs_bug_on(sbi, im->ino_num == 0); | |
662 | im->ino_num--; | |
663 | spin_unlock(&im->ino_lock); | |
cbd56e7d JK |
664 | } |
665 | ||
4d57b86d | 666 | void f2fs_add_orphan_inode(struct inode *inode) |
127e670a | 667 | { |
39efac41 | 668 | /* add new orphan ino entry into list */ |
39d787be | 669 | __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO); |
4d57b86d | 670 | f2fs_update_inode_page(inode); |
127e670a JK |
671 | } |
672 | ||
4d57b86d | 673 | void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) |
127e670a | 674 | { |
953e6cc6 | 675 | /* remove orphan entry from orphan list */ |
6451e041 | 676 | __remove_ino_entry(sbi, ino, ORPHAN_INO); |
127e670a JK |
677 | } |
678 | ||
8c14bfad | 679 | static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino) |
127e670a | 680 | { |
8c14bfad | 681 | struct inode *inode; |
5905f9af | 682 | struct node_info ni; |
76a45e3c | 683 | int err; |
8c14bfad | 684 | |
5905f9af | 685 | inode = f2fs_iget_retry(sbi->sb, ino); |
8c14bfad CY |
686 | if (IS_ERR(inode)) { |
687 | /* | |
688 | * there should be a bug that we can't find the entry | |
689 | * to orphan inode. | |
690 | */ | |
691 | f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT); | |
692 | return PTR_ERR(inode); | |
693 | } | |
694 | ||
10a26878 | 695 | err = f2fs_dquot_initialize(inode); |
a515d12f SY |
696 | if (err) { |
697 | iput(inode); | |
0f9ec2a8 | 698 | goto err_out; |
a515d12f | 699 | } |
0f9ec2a8 | 700 | |
127e670a JK |
701 | clear_nlink(inode); |
702 | ||
703 | /* truncate all the data during iput */ | |
704 | iput(inode); | |
5905f9af | 705 | |
a9419b63 | 706 | err = f2fs_get_node_info(sbi, ino, &ni, false); |
7735730d CY |
707 | if (err) |
708 | goto err_out; | |
5905f9af JK |
709 | |
710 | /* ENOMEM was fully retried in f2fs_evict_inode. */ | |
711 | if (ni.blk_addr != NULL_ADDR) { | |
0f9ec2a8 JK |
712 | err = -EIO; |
713 | goto err_out; | |
5905f9af | 714 | } |
8c14bfad | 715 | return 0; |
0f9ec2a8 JK |
716 | |
717 | err_out: | |
718 | set_sbi_flag(sbi, SBI_NEED_FSCK); | |
dcbb4c10 JP |
719 | f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.", |
720 | __func__, ino); | |
0f9ec2a8 | 721 | return err; |
127e670a JK |
722 | } |
723 | ||
4d57b86d | 724 | int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi) |
127e670a | 725 | { |
3c642985 | 726 | block_t start_blk, orphan_blocks, i, j; |
4b2414d0 | 727 | int err = 0; |
127e670a | 728 | |
aaec2b1d | 729 | if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) |
8c14bfad | 730 | return 0; |
127e670a | 731 | |
68f0453d | 732 | if (f2fs_hw_is_readonly(sbi)) { |
dcbb4c10 | 733 | f2fs_info(sbi, "write access unavailable, skipping orphan cleanup"); |
b61af314 CY |
734 | return 0; |
735 | } | |
736 | ||
e1bb7d3d | 737 | if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) |
dcbb4c10 | 738 | f2fs_info(sbi, "orphan cleanup on readonly fs"); |
4b2414d0 | 739 | |
55141486 | 740 | start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); |
3c642985 | 741 | orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); |
127e670a | 742 | |
4d57b86d | 743 | f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true); |
662befda | 744 | |
3c642985 | 745 | for (i = 0; i < orphan_blocks; i++) { |
375452b5 | 746 | struct folio *folio; |
127e670a JK |
747 | struct f2fs_orphan_block *orphan_blk; |
748 | ||
375452b5 MWO |
749 | folio = f2fs_get_meta_folio(sbi, start_blk + i); |
750 | if (IS_ERR(folio)) { | |
751 | err = PTR_ERR(folio); | |
7735730d CY |
752 | goto out; |
753 | } | |
754 | ||
375452b5 | 755 | orphan_blk = folio_address(folio); |
127e670a JK |
756 | for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { |
757 | nid_t ino = le32_to_cpu(orphan_blk->ino[j]); | |
5f029c04 | 758 | |
8c14bfad CY |
759 | err = recover_orphan_inode(sbi, ino); |
760 | if (err) { | |
375452b5 | 761 | f2fs_folio_put(folio, true); |
4b2414d0 | 762 | goto out; |
8c14bfad | 763 | } |
127e670a | 764 | } |
375452b5 | 765 | f2fs_folio_put(folio, true); |
127e670a JK |
766 | } |
767 | /* clear Orphan Flag */ | |
aaec2b1d | 768 | clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); |
4b2414d0 | 769 | out: |
1378752b CY |
770 | set_sbi_flag(sbi, SBI_IS_RECOVERED); |
771 | ||
4b2414d0 | 772 | return err; |
127e670a JK |
773 | } |
774 | ||
775 | static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) | |
776 | { | |
502c6e0b | 777 | struct list_head *head; |
127e670a | 778 | struct f2fs_orphan_block *orphan_blk = NULL; |
127e670a | 779 | unsigned int nentries = 0; |
bd936f84 | 780 | unsigned short index = 1; |
8c402946 | 781 | unsigned short orphan_blocks; |
643d1668 | 782 | struct folio *folio = NULL; |
6451e041 | 783 | struct ino_entry *orphan = NULL; |
67298804 | 784 | struct inode_management *im = &sbi->im[ORPHAN_INO]; |
127e670a | 785 | |
67298804 | 786 | orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num); |
8c402946 | 787 | |
d6c67a4f JK |
788 | /* |
789 | * we don't need to do spin_lock(&im->ino_lock) here, since all the | |
790 | * orphan inode operations are covered under f2fs_lock_op(). | |
791 | * And, spin_lock should be avoided due to page operations below. | |
792 | */ | |
67298804 | 793 | head = &im->ino_list; |
127e670a | 794 | |
146949de | 795 | /* loop for each orphan inode entry and write them in journal block */ |
502c6e0b | 796 | list_for_each_entry(orphan, head, list) { |
643d1668 MWO |
797 | if (!folio) { |
798 | folio = f2fs_grab_meta_folio(sbi, start_blk++); | |
799 | orphan_blk = folio_address(folio); | |
502c6e0b GZ |
800 | memset(orphan_blk, 0, sizeof(*orphan_blk)); |
801 | } | |
127e670a | 802 | |
36795567 | 803 | orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino); |
127e670a | 804 | |
36795567 | 805 | if (nentries == F2FS_ORPHANS_PER_BLOCK) { |
127e670a JK |
806 | /* |
807 | * an orphan block is full of 1020 entries, | |
808 | * then we need to flush current orphan blocks | |
809 | * and bring another one in memory | |
810 | */ | |
811 | orphan_blk->blk_addr = cpu_to_le16(index); | |
812 | orphan_blk->blk_count = cpu_to_le16(orphan_blocks); | |
813 | orphan_blk->entry_count = cpu_to_le32(nentries); | |
643d1668 MWO |
814 | folio_mark_dirty(folio); |
815 | f2fs_folio_put(folio, true); | |
127e670a | 816 | index++; |
127e670a | 817 | nentries = 0; |
643d1668 | 818 | folio = NULL; |
127e670a | 819 | } |
502c6e0b | 820 | } |
127e670a | 821 | |
643d1668 | 822 | if (folio) { |
502c6e0b GZ |
823 | orphan_blk->blk_addr = cpu_to_le16(index); |
824 | orphan_blk->blk_count = cpu_to_le16(orphan_blocks); | |
825 | orphan_blk->entry_count = cpu_to_le32(nentries); | |
643d1668 MWO |
826 | folio_mark_dirty(folio); |
827 | f2fs_folio_put(folio, true); | |
127e670a | 828 | } |
127e670a JK |
829 | } |
830 | ||
d005af3b | 831 | static __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *ckpt) |
d7eb8f1c CY |
832 | { |
833 | unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset); | |
834 | __u32 chksum; | |
835 | ||
d005af3b | 836 | chksum = f2fs_crc32(ckpt, chksum_ofs); |
d7eb8f1c CY |
837 | if (chksum_ofs < CP_CHKSUM_OFFSET) { |
838 | chksum_ofs += sizeof(chksum); | |
d005af3b EB |
839 | chksum = f2fs_chksum(chksum, (__u8 *)ckpt + chksum_ofs, |
840 | F2FS_BLKSIZE - chksum_ofs); | |
d7eb8f1c CY |
841 | } |
842 | return chksum; | |
843 | } | |
844 | ||
fc0065ad | 845 | static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr, |
a2c746ea | 846 | struct f2fs_checkpoint **cp_block, struct folio **cp_folio, |
fc0065ad | 847 | unsigned long long *version) |
127e670a | 848 | { |
fc0065ad | 849 | size_t crc_offset = 0; |
d7eb8f1c | 850 | __u32 crc; |
127e670a | 851 | |
a2c746ea MWO |
852 | *cp_folio = f2fs_get_meta_folio(sbi, cp_addr); |
853 | if (IS_ERR(*cp_folio)) | |
854 | return PTR_ERR(*cp_folio); | |
7735730d | 855 | |
a2c746ea | 856 | *cp_block = folio_address(*cp_folio); |
127e670a | 857 | |
fc0065ad | 858 | crc_offset = le32_to_cpu((*cp_block)->checksum_offset); |
d7eb8f1c CY |
859 | if (crc_offset < CP_MIN_CHKSUM_OFFSET || |
860 | crc_offset > CP_CHKSUM_OFFSET) { | |
a2c746ea | 861 | f2fs_folio_put(*cp_folio, true); |
dcbb4c10 | 862 | f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset); |
fc0065ad TY |
863 | return -EINVAL; |
864 | } | |
127e670a | 865 | |
d005af3b | 866 | crc = f2fs_checkpoint_chksum(*cp_block); |
d7eb8f1c | 867 | if (crc != cur_cp_crc(*cp_block)) { |
a2c746ea | 868 | f2fs_folio_put(*cp_folio, true); |
dcbb4c10 | 869 | f2fs_warn(sbi, "invalid crc value"); |
fc0065ad TY |
870 | return -EINVAL; |
871 | } | |
127e670a | 872 | |
fc0065ad TY |
873 | *version = cur_cp_version(*cp_block); |
874 | return 0; | |
875 | } | |
127e670a | 876 | |
eb639c85 | 877 | static struct folio *validate_checkpoint(struct f2fs_sb_info *sbi, |
fc0065ad TY |
878 | block_t cp_addr, unsigned long long *version) |
879 | { | |
a2c746ea | 880 | struct folio *cp_folio_1 = NULL, *cp_folio_2 = NULL; |
fc0065ad TY |
881 | struct f2fs_checkpoint *cp_block = NULL; |
882 | unsigned long long cur_version = 0, pre_version = 0; | |
5b5b4f85 | 883 | unsigned int cp_blocks; |
fc0065ad | 884 | int err; |
127e670a | 885 | |
fc0065ad | 886 | err = get_checkpoint_version(sbi, cp_addr, &cp_block, |
a2c746ea | 887 | &cp_folio_1, version); |
fc0065ad | 888 | if (err) |
d3f07c04 | 889 | return NULL; |
c9b60788 | 890 | |
5b5b4f85 CY |
891 | cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count); |
892 | ||
a60108f7 | 893 | if (cp_blocks > BLKS_PER_SEG(sbi) || cp_blocks <= F2FS_CP_PACKS) { |
dcbb4c10 JP |
894 | f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", |
895 | le32_to_cpu(cp_block->cp_pack_total_block_count)); | |
d3f07c04 | 896 | goto invalid_cp; |
c9b60788 | 897 | } |
fc0065ad | 898 | pre_version = *version; |
127e670a | 899 | |
5b5b4f85 | 900 | cp_addr += cp_blocks - 1; |
fc0065ad | 901 | err = get_checkpoint_version(sbi, cp_addr, &cp_block, |
a2c746ea | 902 | &cp_folio_2, version); |
fc0065ad | 903 | if (err) |
d3f07c04 | 904 | goto invalid_cp; |
fc0065ad | 905 | cur_version = *version; |
127e670a JK |
906 | |
907 | if (cur_version == pre_version) { | |
908 | *version = cur_version; | |
a2c746ea | 909 | f2fs_folio_put(cp_folio_2, true); |
eb639c85 | 910 | return cp_folio_1; |
127e670a | 911 | } |
a2c746ea | 912 | f2fs_folio_put(cp_folio_2, true); |
d3f07c04 | 913 | invalid_cp: |
a2c746ea | 914 | f2fs_folio_put(cp_folio_1, true); |
127e670a JK |
915 | return NULL; |
916 | } | |
917 | ||
4d57b86d | 918 | int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi) |
127e670a JK |
919 | { |
920 | struct f2fs_checkpoint *cp_block; | |
921 | struct f2fs_super_block *fsb = sbi->raw_super; | |
eb639c85 | 922 | struct folio *cp1, *cp2, *cur_folio; |
127e670a JK |
923 | unsigned long blk_size = sbi->blocksize; |
924 | unsigned long long cp1_version = 0, cp2_version = 0; | |
925 | unsigned long long cp_start_blk_no; | |
55141486 | 926 | unsigned int cp_blks = 1 + __cp_payload(sbi); |
1dbe4152 CL |
927 | block_t cp_blk_no; |
928 | int i; | |
10f966bb | 929 | int err; |
127e670a | 930 | |
0b6d4ca0 EB |
931 | sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks), |
932 | GFP_KERNEL); | |
127e670a JK |
933 | if (!sbi->ckpt) |
934 | return -ENOMEM; | |
935 | /* | |
936 | * Finding out valid cp block involves read both | |
7a88ddb5 | 937 | * sets( cp pack 1 and cp pack 2) |
127e670a JK |
938 | */ |
939 | cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr); | |
940 | cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); | |
941 | ||
942 | /* The second checkpoint pack should start at the next segment */ | |
f9a4e6df JK |
943 | cp_start_blk_no += ((unsigned long long)1) << |
944 | le32_to_cpu(fsb->log_blocks_per_seg); | |
127e670a JK |
945 | cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); |
946 | ||
947 | if (cp1 && cp2) { | |
948 | if (ver_after(cp2_version, cp1_version)) | |
eb639c85 | 949 | cur_folio = cp2; |
127e670a | 950 | else |
eb639c85 | 951 | cur_folio = cp1; |
127e670a | 952 | } else if (cp1) { |
eb639c85 | 953 | cur_folio = cp1; |
127e670a | 954 | } else if (cp2) { |
eb639c85 | 955 | cur_folio = cp2; |
127e670a | 956 | } else { |
10f966bb | 957 | err = -EFSCORRUPTED; |
127e670a JK |
958 | goto fail_no_cp; |
959 | } | |
960 | ||
eb639c85 | 961 | cp_block = folio_address(cur_folio); |
127e670a JK |
962 | memcpy(sbi->ckpt, cp_block, blk_size); |
963 | ||
eb639c85 | 964 | if (cur_folio == cp1) |
8508e44a JK |
965 | sbi->cur_cp_pack = 1; |
966 | else | |
967 | sbi->cur_cp_pack = 2; | |
984ec63c | 968 | |
e494c2f9 | 969 | /* Sanity checking of checkpoint */ |
10f966bb CY |
970 | if (f2fs_sanity_check_ckpt(sbi)) { |
971 | err = -EFSCORRUPTED; | |
e494c2f9 | 972 | goto free_fail_no_cp; |
10f966bb | 973 | } |
e494c2f9 | 974 | |
1dbe4152 CL |
975 | if (cp_blks <= 1) |
976 | goto done; | |
977 | ||
978 | cp_blk_no = le32_to_cpu(fsb->cp_blkaddr); | |
eb639c85 | 979 | if (cur_folio == cp2) |
447286eb | 980 | cp_blk_no += BIT(le32_to_cpu(fsb->log_blocks_per_seg)); |
1dbe4152 CL |
981 | |
982 | for (i = 1; i < cp_blks; i++) { | |
983 | void *sit_bitmap_ptr; | |
984 | unsigned char *ckpt = (unsigned char *)sbi->ckpt; | |
985 | ||
eb639c85 MWO |
986 | cur_folio = f2fs_get_meta_folio(sbi, cp_blk_no + i); |
987 | if (IS_ERR(cur_folio)) { | |
988 | err = PTR_ERR(cur_folio); | |
7735730d | 989 | goto free_fail_no_cp; |
10f966bb | 990 | } |
eb639c85 | 991 | sit_bitmap_ptr = folio_address(cur_folio); |
1dbe4152 | 992 | memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size); |
eb639c85 | 993 | f2fs_folio_put(cur_folio, true); |
1dbe4152 CL |
994 | } |
995 | done: | |
eb639c85 MWO |
996 | f2fs_folio_put(cp1, true); |
997 | f2fs_folio_put(cp2, true); | |
127e670a JK |
998 | return 0; |
999 | ||
a2125ff7 | 1000 | free_fail_no_cp: |
eb639c85 MWO |
1001 | f2fs_folio_put(cp1, true); |
1002 | f2fs_folio_put(cp2, true); | |
127e670a | 1003 | fail_no_cp: |
5222595d | 1004 | kvfree(sbi->ckpt); |
10f966bb | 1005 | return err; |
127e670a JK |
1006 | } |
1007 | ||
c227f912 | 1008 | static void __add_dirty_inode(struct inode *inode, enum inode_type type) |
127e670a | 1009 | { |
4081363f | 1010 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
c227f912 | 1011 | int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; |
127e670a | 1012 | |
91942321 | 1013 | if (is_inode_flag_set(inode, flag)) |
2710fd7e | 1014 | return; |
2d7b822a | 1015 | |
91942321 | 1016 | set_inode_flag(inode, flag); |
7bc155fe | 1017 | list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]); |
33fbd510 | 1018 | stat_inc_dirty_inode(sbi, type); |
5deb8267 JK |
1019 | } |
1020 | ||
c227f912 | 1021 | static void __remove_dirty_inode(struct inode *inode, enum inode_type type) |
6ad7609a | 1022 | { |
c227f912 | 1023 | int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE; |
6ad7609a | 1024 | |
91942321 | 1025 | if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag)) |
6ad7609a CY |
1026 | return; |
1027 | ||
91942321 JK |
1028 | list_del_init(&F2FS_I(inode)->dirty_list); |
1029 | clear_inode_flag(inode, flag); | |
33fbd510 | 1030 | stat_dec_dirty_inode(F2FS_I_SB(inode), type); |
6ad7609a CY |
1031 | } |
1032 | ||
4f5e34f7 | 1033 | void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio) |
5deb8267 | 1034 | { |
4081363f | 1035 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
c227f912 | 1036 | enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; |
5deb8267 | 1037 | |
5ac9f36f CY |
1038 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && |
1039 | !S_ISLNK(inode->i_mode)) | |
127e670a | 1040 | return; |
7bd59381 | 1041 | |
1c4bf763 JK |
1042 | spin_lock(&sbi->inode_lock[type]); |
1043 | if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH)) | |
10aa97c3 | 1044 | __add_dirty_inode(inode, type); |
b951a4ec | 1045 | inode_inc_dirty_pages(inode); |
1c4bf763 JK |
1046 | spin_unlock(&sbi->inode_lock[type]); |
1047 | ||
4f5e34f7 | 1048 | set_page_private_reference(&folio->page); |
5deb8267 JK |
1049 | } |
1050 | ||
4d57b86d | 1051 | void f2fs_remove_dirty_inode(struct inode *inode) |
127e670a | 1052 | { |
4081363f | 1053 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
c227f912 | 1054 | enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE; |
127e670a | 1055 | |
c227f912 CY |
1056 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && |
1057 | !S_ISLNK(inode->i_mode)) | |
127e670a JK |
1058 | return; |
1059 | ||
10aa97c3 JK |
1060 | if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH)) |
1061 | return; | |
1062 | ||
c227f912 CY |
1063 | spin_lock(&sbi->inode_lock[type]); |
1064 | __remove_dirty_inode(inode, type); | |
1065 | spin_unlock(&sbi->inode_lock[type]); | |
74d0b917 JK |
1066 | } |
1067 | ||
d80afefb CY |
1068 | int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type, |
1069 | bool from_cp) | |
127e670a | 1070 | { |
ce3b7d80 | 1071 | struct list_head *head; |
127e670a | 1072 | struct inode *inode; |
2710fd7e | 1073 | struct f2fs_inode_info *fi; |
4cf18537 | 1074 | bool is_dir = (type == DIR_INODE); |
4db08d01 | 1075 | unsigned long ino = 0; |
4cf18537 CY |
1076 | |
1077 | trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir, | |
1078 | get_pages(sbi, is_dir ? | |
1079 | F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); | |
127e670a | 1080 | retry: |
9b664822 ZQ |
1081 | if (unlikely(f2fs_cp_error(sbi))) { |
1082 | trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, | |
1083 | get_pages(sbi, is_dir ? | |
1084 | F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); | |
6d5a1495 | 1085 | return -EIO; |
9b664822 | 1086 | } |
af41d3ee | 1087 | |
c227f912 | 1088 | spin_lock(&sbi->inode_lock[type]); |
ce3b7d80 | 1089 | |
c227f912 | 1090 | head = &sbi->inode_list[type]; |
127e670a | 1091 | if (list_empty(head)) { |
c227f912 | 1092 | spin_unlock(&sbi->inode_lock[type]); |
4cf18537 CY |
1093 | trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir, |
1094 | get_pages(sbi, is_dir ? | |
1095 | F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA)); | |
6d5a1495 | 1096 | return 0; |
127e670a | 1097 | } |
939afa94 | 1098 | fi = list_first_entry(head, struct f2fs_inode_info, dirty_list); |
2710fd7e | 1099 | inode = igrab(&fi->vfs_inode); |
c227f912 | 1100 | spin_unlock(&sbi->inode_lock[type]); |
127e670a | 1101 | if (inode) { |
4db08d01 JK |
1102 | unsigned long cur_ino = inode->i_ino; |
1103 | ||
d80afefb CY |
1104 | if (from_cp) |
1105 | F2FS_I(inode)->cp_task = current; | |
1106 | F2FS_I(inode)->wb_task = current; | |
b0af6d49 | 1107 | |
87d6f890 | 1108 | filemap_fdatawrite(inode->i_mapping); |
b0af6d49 | 1109 | |
d80afefb CY |
1110 | F2FS_I(inode)->wb_task = NULL; |
1111 | if (from_cp) | |
1112 | F2FS_I(inode)->cp_task = NULL; | |
b0af6d49 | 1113 | |
127e670a | 1114 | iput(inode); |
4db08d01 | 1115 | /* We need to give cpu to another writers. */ |
2a63531a | 1116 | if (ino == cur_ino) |
4db08d01 | 1117 | cond_resched(); |
2a63531a | 1118 | else |
4db08d01 | 1119 | ino = cur_ino; |
127e670a JK |
1120 | } else { |
1121 | /* | |
1122 | * We should submit bio, since it exists several | |
146949de | 1123 | * writebacking dentry pages in the freeing inode. |
127e670a | 1124 | */ |
b9109b0e | 1125 | f2fs_submit_merged_write(sbi, DATA); |
7ecebe5e | 1126 | cond_resched(); |
127e670a JK |
1127 | } |
1128 | goto retry; | |
1129 | } | |
1130 | ||
60630375 | 1131 | static int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi) |
0f18b462 JK |
1132 | { |
1133 | struct list_head *head = &sbi->inode_list[DIRTY_META]; | |
1134 | struct inode *inode; | |
1135 | struct f2fs_inode_info *fi; | |
1136 | s64 total = get_pages(sbi, F2FS_DIRTY_IMETA); | |
1137 | ||
1138 | while (total--) { | |
1139 | if (unlikely(f2fs_cp_error(sbi))) | |
1140 | return -EIO; | |
1141 | ||
1142 | spin_lock(&sbi->inode_lock[DIRTY_META]); | |
1143 | if (list_empty(head)) { | |
1144 | spin_unlock(&sbi->inode_lock[DIRTY_META]); | |
1145 | return 0; | |
1146 | } | |
939afa94 | 1147 | fi = list_first_entry(head, struct f2fs_inode_info, |
0f18b462 JK |
1148 | gdirty_list); |
1149 | inode = igrab(&fi->vfs_inode); | |
1150 | spin_unlock(&sbi->inode_lock[DIRTY_META]); | |
1151 | if (inode) { | |
18340edc JK |
1152 | sync_inode_metadata(inode, 0); |
1153 | ||
1154 | /* it's on eviction */ | |
1155 | if (is_inode_flag_set(inode, FI_DIRTY_INODE)) | |
4d57b86d | 1156 | f2fs_update_inode_page(inode); |
0f18b462 JK |
1157 | iput(inode); |
1158 | } | |
dee668c1 | 1159 | } |
0f18b462 JK |
1160 | return 0; |
1161 | } | |
1162 | ||
59c9081b YH |
1163 | static void __prepare_cp_block(struct f2fs_sb_info *sbi) |
1164 | { | |
1165 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
1166 | struct f2fs_nm_info *nm_i = NM_I(sbi); | |
1167 | nid_t last_nid = nm_i->next_scan_nid; | |
1168 | ||
1169 | next_free_nid(sbi, &last_nid); | |
1170 | ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi)); | |
1171 | ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); | |
1172 | ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); | |
1173 | ckpt->next_free_nid = cpu_to_le32(last_nid); | |
f06c0f82 CY |
1174 | |
1175 | /* update user_block_counts */ | |
1176 | sbi->last_valid_block_count = sbi->total_valid_block_count; | |
1177 | percpu_counter_set(&sbi->alloc_valid_block_count, 0); | |
1178 | percpu_counter_set(&sbi->rf_node_block_count, 0); | |
59c9081b YH |
1179 | } |
1180 | ||
af033b2a CY |
1181 | static bool __need_flush_quota(struct f2fs_sb_info *sbi) |
1182 | { | |
db6ec53b JK |
1183 | bool ret = false; |
1184 | ||
af033b2a CY |
1185 | if (!is_journalled_quota(sbi)) |
1186 | return false; | |
db6ec53b | 1187 | |
e4544b63 | 1188 | if (!f2fs_down_write_trylock(&sbi->quota_sem)) |
a5c00422 | 1189 | return true; |
db6ec53b JK |
1190 | if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) { |
1191 | ret = false; | |
1192 | } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) { | |
1193 | ret = false; | |
1194 | } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) { | |
1195 | clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); | |
1196 | ret = true; | |
1197 | } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) { | |
1198 | ret = true; | |
1199 | } | |
e4544b63 | 1200 | f2fs_up_write(&sbi->quota_sem); |
db6ec53b | 1201 | return ret; |
af033b2a CY |
1202 | } |
1203 | ||
0a8165d7 | 1204 | /* |
127e670a JK |
1205 | * Freeze all the FS-operations for checkpoint. |
1206 | */ | |
cf779cab | 1207 | static int block_operations(struct f2fs_sb_info *sbi) |
127e670a | 1208 | { |
127e670a JK |
1209 | struct writeback_control wbc = { |
1210 | .sync_mode = WB_SYNC_ALL, | |
1211 | .nr_to_write = LONG_MAX, | |
127e670a | 1212 | }; |
af033b2a | 1213 | int err = 0, cnt = 0; |
c718379b | 1214 | |
34c061ad SL |
1215 | /* |
1216 | * Let's flush inline_data in dirty node pages. | |
1217 | */ | |
1218 | f2fs_flush_inline_data(sbi); | |
1219 | ||
af033b2a | 1220 | retry_flush_quotas: |
db6ec53b | 1221 | f2fs_lock_all(sbi); |
af033b2a | 1222 | if (__need_flush_quota(sbi)) { |
eb85c241 | 1223 | bool need_lock = sbi->umount_lock_holder != current; |
af033b2a CY |
1224 | |
1225 | if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { | |
1226 | set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); | |
db6ec53b | 1227 | set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); |
af033b2a CY |
1228 | goto retry_flush_dents; |
1229 | } | |
db6ec53b | 1230 | f2fs_unlock_all(sbi); |
af033b2a | 1231 | |
eb85c241 CY |
1232 | /* don't grab s_umount lock during mount/umount/remount/freeze/quotactl */ |
1233 | if (!need_lock) { | |
1234 | f2fs_do_quota_sync(sbi->sb, -1); | |
1235 | } else if (down_read_trylock(&sbi->sb->s_umount)) { | |
1236 | f2fs_do_quota_sync(sbi->sb, -1); | |
af033b2a | 1237 | up_read(&sbi->sb->s_umount); |
eb85c241 | 1238 | } |
af033b2a CY |
1239 | cond_resched(); |
1240 | goto retry_flush_quotas; | |
1241 | } | |
1242 | ||
1243 | retry_flush_dents: | |
127e670a | 1244 | /* write all the dirty dentry pages */ |
127e670a | 1245 | if (get_pages(sbi, F2FS_DIRTY_DENTS)) { |
e479556b | 1246 | f2fs_unlock_all(sbi); |
d80afefb | 1247 | err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true); |
6d5a1495 | 1248 | if (err) |
1f5f11a3 | 1249 | return err; |
30973883 | 1250 | cond_resched(); |
af033b2a | 1251 | goto retry_flush_quotas; |
127e670a JK |
1252 | } |
1253 | ||
59c9081b YH |
1254 | /* |
1255 | * POR: we should ensure that there are no dirty node pages | |
1256 | * until finishing nat/sit flush. inode->i_blocks can be updated. | |
1257 | */ | |
e4544b63 | 1258 | f2fs_down_write(&sbi->node_change); |
59c9081b | 1259 | |
0f18b462 | 1260 | if (get_pages(sbi, F2FS_DIRTY_IMETA)) { |
e4544b63 | 1261 | f2fs_up_write(&sbi->node_change); |
0f18b462 JK |
1262 | f2fs_unlock_all(sbi); |
1263 | err = f2fs_sync_inode_meta(sbi); | |
1264 | if (err) | |
1f5f11a3 | 1265 | return err; |
30973883 | 1266 | cond_resched(); |
af033b2a | 1267 | goto retry_flush_quotas; |
0f18b462 JK |
1268 | } |
1269 | ||
39936837 | 1270 | retry_flush_nodes: |
e4544b63 | 1271 | f2fs_down_write(&sbi->node_write); |
127e670a JK |
1272 | |
1273 | if (get_pages(sbi, F2FS_DIRTY_NODES)) { | |
e4544b63 | 1274 | f2fs_up_write(&sbi->node_write); |
c29fd0c0 | 1275 | atomic_inc(&sbi->wb_sync_req[NODE]); |
4d57b86d | 1276 | err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO); |
c29fd0c0 | 1277 | atomic_dec(&sbi->wb_sync_req[NODE]); |
6d5a1495 | 1278 | if (err) { |
e4544b63 | 1279 | f2fs_up_write(&sbi->node_change); |
cf779cab | 1280 | f2fs_unlock_all(sbi); |
1f5f11a3 | 1281 | return err; |
cf779cab | 1282 | } |
30973883 | 1283 | cond_resched(); |
39936837 | 1284 | goto retry_flush_nodes; |
127e670a | 1285 | } |
59c9081b YH |
1286 | |
1287 | /* | |
1288 | * sbi->node_change is used only for AIO write_begin path which produces | |
1289 | * dirty node blocks and some checkpoint values by block allocation. | |
1290 | */ | |
1291 | __prepare_cp_block(sbi); | |
e4544b63 | 1292 | f2fs_up_write(&sbi->node_change); |
cf779cab | 1293 | return err; |
127e670a JK |
1294 | } |
1295 | ||
1296 | static void unblock_operations(struct f2fs_sb_info *sbi) | |
1297 | { | |
e4544b63 | 1298 | f2fs_up_write(&sbi->node_write); |
e479556b | 1299 | f2fs_unlock_all(sbi); |
127e670a JK |
1300 | } |
1301 | ||
bf22c3cc | 1302 | void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type) |
fb51b5ef CL |
1303 | { |
1304 | DEFINE_WAIT(wait); | |
1305 | ||
1306 | for (;;) { | |
bf22c3cc | 1307 | if (!get_pages(sbi, type)) |
fb51b5ef CL |
1308 | break; |
1309 | ||
c9b3649a CY |
1310 | if (unlikely(f2fs_cp_error(sbi) && |
1311 | !is_sbi_flag_set(sbi, SBI_IS_CLOSE))) | |
af697c0f JK |
1312 | break; |
1313 | ||
9c30df7c JK |
1314 | if (type == F2FS_DIRTY_META) |
1315 | f2fs_sync_meta_pages(sbi, META, LONG_MAX, | |
1316 | FS_CP_META_IO); | |
1fd28018 JK |
1317 | else if (type == F2FS_WB_CP_DATA) |
1318 | f2fs_submit_merged_write(sbi, DATA); | |
828add77 JK |
1319 | |
1320 | prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE); | |
5df7731f | 1321 | io_schedule_timeout(DEFAULT_IO_TIMEOUT); |
fb51b5ef CL |
1322 | } |
1323 | finish_wait(&sbi->cp_wait, &wait); | |
1324 | } | |
1325 | ||
e4c5d848 JK |
1326 | static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
1327 | { | |
1328 | unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num; | |
1329 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
d1aa2453 | 1330 | unsigned long flags; |
e4c5d848 | 1331 | |
94c821fb | 1332 | spin_lock_irqsave(&sbi->cp_lock, flags); |
22ad0b6a | 1333 | |
19426c49 CY |
1334 | if ((cpc->reason & CP_UMOUNT) && |
1335 | le32_to_cpu(ckpt->cp_pack_total_block_count) > | |
1336 | sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks) | |
1337 | disable_nat_bits(sbi, false); | |
1338 | ||
1f43e2ad CY |
1339 | if (cpc->reason & CP_TRIMMED) |
1340 | __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG); | |
cd36d7a1 CY |
1341 | else |
1342 | __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG); | |
1f43e2ad | 1343 | |
c473f1a9 | 1344 | if (cpc->reason & CP_UMOUNT) |
e4c5d848 JK |
1345 | __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG); |
1346 | else | |
1347 | __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG); | |
1348 | ||
c473f1a9 | 1349 | if (cpc->reason & CP_FASTBOOT) |
e4c5d848 JK |
1350 | __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); |
1351 | else | |
1352 | __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG); | |
1353 | ||
1354 | if (orphan_num) | |
1355 | __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); | |
1356 | else | |
1357 | __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG); | |
1358 | ||
c84ef3c5 | 1359 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) |
e4c5d848 JK |
1360 | __set_ckpt_flags(ckpt, CP_FSCK_FLAG); |
1361 | ||
c84ef3c5 ST |
1362 | if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS)) |
1363 | __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); | |
1364 | else | |
1365 | __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG); | |
1366 | ||
4354994f DR |
1367 | if (is_sbi_flag_set(sbi, SBI_CP_DISABLED)) |
1368 | __set_ckpt_flags(ckpt, CP_DISABLED_FLAG); | |
1369 | else | |
1370 | __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); | |
1371 | ||
db610a64 JK |
1372 | if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK)) |
1373 | __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); | |
1374 | else | |
1375 | __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG); | |
1376 | ||
af033b2a CY |
1377 | if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) |
1378 | __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); | |
bc88ac96 JK |
1379 | else |
1380 | __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); | |
af033b2a CY |
1381 | |
1382 | if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) | |
1383 | __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG); | |
1384 | ||
e4c5d848 JK |
1385 | /* set this flag to activate crc|cp_ver for recovery */ |
1386 | __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); | |
f2367923 | 1387 | __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); |
e4c5d848 | 1388 | |
d1aa2453 | 1389 | spin_unlock_irqrestore(&sbi->cp_lock, flags); |
e4c5d848 JK |
1390 | } |
1391 | ||
46706d59 GX |
1392 | static void commit_checkpoint(struct f2fs_sb_info *sbi, |
1393 | void *src, block_t blk_addr) | |
1394 | { | |
402dd9f0 | 1395 | struct writeback_control wbc = {}; |
46706d59 GX |
1396 | |
1397 | /* | |
668c7a56 | 1398 | * filemap_get_folios_tag and folio_lock again will take |
4d57b86d CY |
1399 | * some extra time. Therefore, f2fs_update_meta_pages and |
1400 | * f2fs_sync_meta_pages are combined in this function. | |
46706d59 | 1401 | */ |
668c7a56 | 1402 | struct folio *folio = f2fs_grab_meta_folio(sbi, blk_addr); |
46706d59 | 1403 | |
668c7a56 | 1404 | memcpy(folio_address(folio), src, PAGE_SIZE); |
8d64d365 | 1405 | |
668c7a56 MWO |
1406 | folio_mark_dirty(folio); |
1407 | if (unlikely(!folio_clear_dirty_for_io(folio))) | |
46706d59 GX |
1408 | f2fs_bug_on(sbi, 1); |
1409 | ||
1410 | /* writeout cp pack 2 page */ | |
39122e45 CH |
1411 | if (unlikely(!__f2fs_write_meta_folio(folio, &wbc, FS_CP_META_IO))) { |
1412 | if (f2fs_cp_error(sbi)) { | |
1413 | f2fs_folio_put(folio, true); | |
1414 | return; | |
1415 | } | |
1416 | f2fs_bug_on(sbi, true); | |
af697c0f | 1417 | } |
46706d59 | 1418 | |
668c7a56 | 1419 | f2fs_folio_put(folio, false); |
46706d59 GX |
1420 | |
1421 | /* submit checkpoint (with barrier if NOBARRIER is not set) */ | |
1422 | f2fs_submit_merged_write(sbi, META_FLUSH); | |
1423 | } | |
1424 | ||
3a0a9cbc CY |
1425 | static inline u64 get_sectors_written(struct block_device *bdev) |
1426 | { | |
ff49c86f | 1427 | return (u64)part_stat_read(bdev, sectors[STAT_WRITE]); |
3a0a9cbc CY |
1428 | } |
1429 | ||
1430 | u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi) | |
1431 | { | |
1432 | if (f2fs_is_multi_device(sbi)) { | |
1433 | u64 sectors = 0; | |
1434 | int i; | |
1435 | ||
1436 | for (i = 0; i < sbi->s_ndevs; i++) | |
1437 | sectors += get_sectors_written(FDEV(i).bdev); | |
1438 | ||
1439 | return sectors; | |
1440 | } | |
1441 | ||
1442 | return get_sectors_written(sbi->sb->s_bdev); | |
1443 | } | |
1444 | ||
c34f42e2 | 1445 | static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
127e670a JK |
1446 | { |
1447 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
77041823 | 1448 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
d1aa2453 | 1449 | unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags; |
127e670a | 1450 | block_t start_blk; |
127e670a | 1451 | unsigned int data_sum_blocks, orphan_blocks; |
7e586fa0 | 1452 | __u32 crc32 = 0; |
127e670a | 1453 | int i; |
55141486 | 1454 | int cp_payload_blks = __cp_payload(sbi); |
8f1dbbbb SL |
1455 | struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); |
1456 | u64 kbytes_written; | |
1228b482 | 1457 | int err; |
127e670a JK |
1458 | |
1459 | /* Flush all the NAT/SIT pages */ | |
8ec18bff | 1460 | f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); |
127e670a | 1461 | |
7a88ddb5 | 1462 | /* start to update checkpoint, cp ver is already updated previously */ |
a1f72ac2 | 1463 | ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true)); |
127e670a | 1464 | ckpt->free_segment_count = cpu_to_le32(free_segments(sbi)); |
b5b82205 | 1465 | for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { |
5a4fed7c CH |
1466 | struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_NODE); |
1467 | ||
1468 | ckpt->cur_node_segno[i] = cpu_to_le32(curseg->segno); | |
1469 | ckpt->cur_node_blkoff[i] = cpu_to_le16(curseg->next_blkoff); | |
1470 | ckpt->alloc_type[i + CURSEG_HOT_NODE] = curseg->alloc_type; | |
127e670a | 1471 | } |
b5b82205 | 1472 | for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { |
5a4fed7c CH |
1473 | struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_DATA); |
1474 | ||
1475 | ckpt->cur_data_segno[i] = cpu_to_le32(curseg->segno); | |
1476 | ckpt->cur_data_blkoff[i] = cpu_to_le16(curseg->next_blkoff); | |
1477 | ckpt->alloc_type[i + CURSEG_HOT_DATA] = curseg->alloc_type; | |
127e670a JK |
1478 | } |
1479 | ||
a87aff1d | 1480 | /* 2 cp + n data seg summary + orphan inode blocks */ |
4d57b86d | 1481 | data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false); |
d1aa2453 | 1482 | spin_lock_irqsave(&sbi->cp_lock, flags); |
b5b82205 | 1483 | if (data_sum_blocks < NR_CURSEG_DATA_TYPE) |
aaec2b1d | 1484 | __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); |
127e670a | 1485 | else |
aaec2b1d | 1486 | __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG); |
d1aa2453 | 1487 | spin_unlock_irqrestore(&sbi->cp_lock, flags); |
127e670a | 1488 | |
67298804 | 1489 | orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num); |
1dbe4152 CL |
1490 | ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks + |
1491 | orphan_blocks); | |
127e670a | 1492 | |
119ee914 | 1493 | if (__remain_node_summaries(cpc->reason)) |
2a4bd0c3 | 1494 | ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + |
1dbe4152 CL |
1495 | cp_payload_blks + data_sum_blocks + |
1496 | orphan_blocks + NR_CURSEG_NODE_TYPE); | |
119ee914 | 1497 | else |
b5b82205 | 1498 | ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS + |
1dbe4152 CL |
1499 | cp_payload_blks + data_sum_blocks + |
1500 | orphan_blocks); | |
119ee914 | 1501 | |
e4c5d848 JK |
1502 | /* update ckpt flag for checkpoint */ |
1503 | update_ckpt_flags(sbi, cpc); | |
a468f0ef | 1504 | |
127e670a JK |
1505 | /* update SIT/NAT bitmap */ |
1506 | get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP)); | |
1507 | get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP)); | |
1508 | ||
d005af3b | 1509 | crc32 = f2fs_checkpoint_chksum(ckpt); |
7e586fa0 JK |
1510 | *((__le32 *)((unsigned char *)ckpt + |
1511 | le32_to_cpu(ckpt->checksum_offset))) | |
127e670a JK |
1512 | = cpu_to_le32(crc32); |
1513 | ||
8508e44a | 1514 | start_blk = __start_cp_next_addr(sbi); |
127e670a | 1515 | |
22ad0b6a | 1516 | /* write nat bits */ |
19426c49 | 1517 | if (enabled_nat_bits(sbi, cpc)) { |
22ad0b6a | 1518 | __u64 cp_ver = cur_cp_version(ckpt); |
22ad0b6a JK |
1519 | block_t blk; |
1520 | ||
1521 | cp_ver |= ((__u64)crc32 << 32); | |
1522 | *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver); | |
1523 | ||
a60108f7 | 1524 | blk = start_blk + BLKS_PER_SEG(sbi) - nm_i->nat_bits_blocks; |
22ad0b6a | 1525 | for (i = 0; i < nm_i->nat_bits_blocks; i++) |
4d57b86d | 1526 | f2fs_update_meta_page(sbi, nm_i->nat_bits + |
8fb9f319 | 1527 | F2FS_BLK_TO_BYTES(i), blk + i); |
22ad0b6a JK |
1528 | } |
1529 | ||
127e670a | 1530 | /* write out checkpoint buffer at block 0 */ |
4d57b86d | 1531 | f2fs_update_meta_page(sbi, ckpt, start_blk++); |
381722d2 CY |
1532 | |
1533 | for (i = 1; i < 1 + cp_payload_blks; i++) | |
4d57b86d | 1534 | f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE, |
381722d2 | 1535 | start_blk++); |
1dbe4152 | 1536 | |
67298804 | 1537 | if (orphan_num) { |
127e670a JK |
1538 | write_orphan_inodes(sbi, start_blk); |
1539 | start_blk += orphan_blocks; | |
1540 | } | |
1541 | ||
4d57b86d | 1542 | f2fs_write_data_summaries(sbi, start_blk); |
127e670a | 1543 | start_blk += data_sum_blocks; |
8f1dbbbb SL |
1544 | |
1545 | /* Record write statistics in the hot node summary */ | |
1546 | kbytes_written = sbi->kbytes_written; | |
3a0a9cbc CY |
1547 | kbytes_written += (f2fs_get_sectors_written(sbi) - |
1548 | sbi->sectors_written_start) >> 1; | |
b7ad7512 | 1549 | seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); |
8f1dbbbb | 1550 | |
119ee914 | 1551 | if (__remain_node_summaries(cpc->reason)) { |
4d57b86d | 1552 | f2fs_write_node_summaries(sbi, start_blk); |
127e670a JK |
1553 | start_blk += NR_CURSEG_NODE_TYPE; |
1554 | } | |
1555 | ||
46706d59 | 1556 | /* Here, we have one bio having CP pack except cp pack 2 page */ |
4d57b86d | 1557 | f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); |
bf22c3cc ST |
1558 | /* Wait for all dirty meta pages to be submitted for IO */ |
1559 | f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META); | |
46706d59 GX |
1560 | |
1561 | /* wait for previous submitted meta pages writeback */ | |
bf22c3cc | 1562 | f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); |
127e670a | 1563 | |
46706d59 GX |
1564 | /* flush all device cache */ |
1565 | err = f2fs_flush_device_cache(sbi); | |
1566 | if (err) | |
1567 | return err; | |
127e670a | 1568 | |
46706d59 GX |
1569 | /* barrier and flush checkpoint cp pack 2 page if it can */ |
1570 | commit_checkpoint(sbi, ckpt, start_blk); | |
bf22c3cc | 1571 | f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); |
6a8f8ca5 | 1572 | |
18767e62 | 1573 | /* |
542989b6 | 1574 | * invalidate intermediate page cache borrowed from meta inode which are |
aff6fbbe | 1575 | * used for migration of encrypted, verity or compressed inode's blocks. |
18767e62 | 1576 | */ |
aff6fbbe CY |
1577 | if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) || |
1578 | f2fs_sb_has_compression(sbi)) | |
9f0c4a46 CY |
1579 | f2fs_bug_on(sbi, |
1580 | invalidate_inode_pages2_range(META_MAPPING(sbi), | |
1581 | MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1)); | |
18767e62 | 1582 | |
4d57b86d | 1583 | f2fs_release_ino_entry(sbi, false); |
cf779cab | 1584 | |
50fa53ec CY |
1585 | f2fs_reset_fsync_node_info(sbi); |
1586 | ||
caf0047e | 1587 | clear_sbi_flag(sbi, SBI_IS_DIRTY); |
bbf156f7 | 1588 | clear_sbi_flag(sbi, SBI_NEED_CP); |
af033b2a | 1589 | clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); |
c9c8ed50 CY |
1590 | |
1591 | spin_lock(&sbi->stat_lock); | |
4354994f | 1592 | sbi->unusable_block_count = 0; |
c9c8ed50 CY |
1593 | spin_unlock(&sbi->stat_lock); |
1594 | ||
8508e44a | 1595 | __set_cp_next_pack(sbi); |
c34f42e2 | 1596 | |
c2a080ae CY |
1597 | /* |
1598 | * redirty superblock if metadata like node page or inode cache is | |
1599 | * updated during writing checkpoint. | |
1600 | */ | |
1601 | if (get_pages(sbi, F2FS_DIRTY_NODES) || | |
1602 | get_pages(sbi, F2FS_DIRTY_IMETA)) | |
1603 | set_sbi_flag(sbi, SBI_IS_DIRTY); | |
1604 | ||
1605 | f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS)); | |
1606 | ||
af697c0f | 1607 | return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0; |
127e670a JK |
1608 | } |
1609 | ||
4d57b86d | 1610 | int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
127e670a JK |
1611 | { |
1612 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
1613 | unsigned long long ckpt_ver; | |
c34f42e2 | 1614 | int err = 0; |
127e670a | 1615 | |
f5a131bb CY |
1616 | if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi)) |
1617 | return -EROFS; | |
1618 | ||
4354994f DR |
1619 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { |
1620 | if (cpc->reason != CP_PAUSE) | |
1621 | return 0; | |
dcbb4c10 | 1622 | f2fs_warn(sbi, "Start checkpoint disabled!"); |
4354994f | 1623 | } |
b4b10061 | 1624 | if (cpc->reason != CP_RESIZE) |
e4544b63 | 1625 | f2fs_down_write(&sbi->cp_global_sem); |
8501017e | 1626 | |
caf0047e | 1627 | if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && |
c473f1a9 CY |
1628 | ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || |
1629 | ((cpc->reason & CP_DISCARD) && !sbi->discard_blks))) | |
8501017e | 1630 | goto out; |
c34f42e2 CY |
1631 | if (unlikely(f2fs_cp_error(sbi))) { |
1632 | err = -EIO; | |
cf779cab | 1633 | goto out; |
c34f42e2 | 1634 | } |
2bda542d WL |
1635 | |
1636 | trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops"); | |
1637 | ||
c34f42e2 CY |
1638 | err = block_operations(sbi); |
1639 | if (err) | |
cf779cab | 1640 | goto out; |
127e670a | 1641 | |
75ab4cb8 | 1642 | trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops"); |
2af4bd6c | 1643 | |
b9109b0e | 1644 | f2fs_flush_merged_writes(sbi); |
127e670a | 1645 | |
58cce381 | 1646 | /* this is the case of multiple fstrims without any changes */ |
c473f1a9 | 1647 | if (cpc->reason & CP_DISCARD) { |
4d57b86d | 1648 | if (!f2fs_exist_trim_candidates(sbi, cpc)) { |
25290fa5 JK |
1649 | unblock_operations(sbi); |
1650 | goto out; | |
1651 | } | |
1652 | ||
a95ba66a | 1653 | if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 && |
0333ad4e JK |
1654 | SIT_I(sbi)->dirty_sentries == 0 && |
1655 | prefree_segments(sbi) == 0) { | |
4d57b86d CY |
1656 | f2fs_flush_sit_entries(sbi, cpc); |
1657 | f2fs_clear_prefree_segments(sbi, cpc); | |
0333ad4e JK |
1658 | unblock_operations(sbi); |
1659 | goto out; | |
1660 | } | |
58cce381 YH |
1661 | } |
1662 | ||
127e670a JK |
1663 | /* |
1664 | * update checkpoint pack index | |
1665 | * Increase the version number so that | |
1666 | * SIT entries and seg summaries are written at correct place | |
1667 | */ | |
d71b5564 | 1668 | ckpt_ver = cur_cp_version(ckpt); |
127e670a JK |
1669 | ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver); |
1670 | ||
1671 | /* write cached NAT/SIT entries to NAT/SIT area */ | |
edc55aaf | 1672 | err = f2fs_flush_nat_entries(sbi, cpc); |
91803392 CY |
1673 | if (err) { |
1674 | f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err); | |
1675 | f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); | |
edc55aaf | 1676 | goto stop; |
91803392 | 1677 | } |
edc55aaf | 1678 | |
4d57b86d | 1679 | f2fs_flush_sit_entries(sbi, cpc); |
127e670a | 1680 | |
d0b9e42a | 1681 | /* save inmem log status */ |
093749e2 | 1682 | f2fs_save_inmem_curseg(sbi); |
d0b9e42a | 1683 | |
c34f42e2 | 1684 | err = do_checkpoint(sbi, cpc); |
91803392 CY |
1685 | if (err) { |
1686 | f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err); | |
1687 | f2fs_bug_on(sbi, !f2fs_cp_error(sbi)); | |
4d57b86d | 1688 | f2fs_release_discard_addrs(sbi); |
91803392 | 1689 | } else { |
4d57b86d | 1690 | f2fs_clear_prefree_segments(sbi, cpc); |
91803392 | 1691 | } |
d0b9e42a | 1692 | |
093749e2 | 1693 | f2fs_restore_inmem_curseg(sbi); |
6efc3a05 | 1694 | f2fs_reinit_atgc_curseg(sbi); |
eb61c2cc | 1695 | stat_inc_cp_count(sbi); |
edc55aaf | 1696 | stop: |
127e670a | 1697 | unblock_operations(sbi); |
10027551 | 1698 | |
c473f1a9 | 1699 | if (cpc->reason & CP_RECOVERY) |
dcbb4c10 | 1700 | f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver); |
60b99b48 | 1701 | |
7a88ddb5 | 1702 | /* update CP_TIME to trigger checkpoint periodically */ |
6beceb54 | 1703 | f2fs_update_time(sbi, CP_TIME); |
55d1cdb2 | 1704 | trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); |
8501017e | 1705 | out: |
b4b10061 | 1706 | if (cpc->reason != CP_RESIZE) |
e4544b63 | 1707 | f2fs_up_write(&sbi->cp_global_sem); |
c34f42e2 | 1708 | return err; |
127e670a JK |
1709 | } |
1710 | ||
4d57b86d | 1711 | void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi) |
127e670a | 1712 | { |
6451e041 JK |
1713 | int i; |
1714 | ||
1715 | for (i = 0; i < MAX_INO_ENTRY; i++) { | |
67298804 CY |
1716 | struct inode_management *im = &sbi->im[i]; |
1717 | ||
1718 | INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC); | |
1719 | spin_lock_init(&im->ino_lock); | |
1720 | INIT_LIST_HEAD(&im->ino_list); | |
1721 | im->ino_num = 0; | |
6451e041 JK |
1722 | } |
1723 | ||
a60108f7 | 1724 | sbi->max_orphans = (BLKS_PER_SEG(sbi) - F2FS_CP_PACKS - |
d0b9e42a | 1725 | NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) * |
a60108f7 | 1726 | F2FS_ORPHANS_PER_BLOCK; |
127e670a JK |
1727 | } |
1728 | ||
4d57b86d | 1729 | int __init f2fs_create_checkpoint_caches(void) |
127e670a | 1730 | { |
6451e041 JK |
1731 | ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry", |
1732 | sizeof(struct ino_entry)); | |
1733 | if (!ino_entry_slab) | |
127e670a | 1734 | return -ENOMEM; |
4d57b86d | 1735 | f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry", |
06292073 | 1736 | sizeof(struct inode_entry)); |
4d57b86d | 1737 | if (!f2fs_inode_entry_slab) { |
6451e041 | 1738 | kmem_cache_destroy(ino_entry_slab); |
127e670a JK |
1739 | return -ENOMEM; |
1740 | } | |
1741 | return 0; | |
1742 | } | |
1743 | ||
4d57b86d | 1744 | void f2fs_destroy_checkpoint_caches(void) |
127e670a | 1745 | { |
6451e041 | 1746 | kmem_cache_destroy(ino_entry_slab); |
4d57b86d | 1747 | kmem_cache_destroy(f2fs_inode_entry_slab); |
127e670a | 1748 | } |
261eeb9c DJ |
1749 | |
1750 | static int __write_checkpoint_sync(struct f2fs_sb_info *sbi) | |
1751 | { | |
1752 | struct cp_control cpc = { .reason = CP_SYNC, }; | |
1753 | int err; | |
1754 | ||
e4544b63 | 1755 | f2fs_down_write(&sbi->gc_lock); |
261eeb9c | 1756 | err = f2fs_write_checkpoint(sbi, &cpc); |
e4544b63 | 1757 | f2fs_up_write(&sbi->gc_lock); |
261eeb9c DJ |
1758 | |
1759 | return err; | |
1760 | } | |
1761 | ||
1762 | static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi) | |
1763 | { | |
1764 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
1765 | struct ckpt_req *req, *next; | |
1766 | struct llist_node *dispatch_list; | |
1767 | u64 sum_diff = 0, diff, count = 0; | |
1768 | int ret; | |
1769 | ||
1770 | dispatch_list = llist_del_all(&cprc->issue_list); | |
1771 | if (!dispatch_list) | |
1772 | return; | |
1773 | dispatch_list = llist_reverse_order(dispatch_list); | |
1774 | ||
1775 | ret = __write_checkpoint_sync(sbi); | |
1776 | atomic_inc(&cprc->issued_ckpt); | |
1777 | ||
1778 | llist_for_each_entry_safe(req, next, dispatch_list, llnode) { | |
1779 | diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time); | |
1780 | req->ret = ret; | |
1781 | complete(&req->wait); | |
1782 | ||
1783 | sum_diff += diff; | |
1784 | count++; | |
1785 | } | |
1786 | atomic_sub(count, &cprc->queued_ckpt); | |
1787 | atomic_add(count, &cprc->total_ckpt); | |
1788 | ||
1789 | spin_lock(&cprc->stat_lock); | |
1790 | cprc->cur_time = (unsigned int)div64_u64(sum_diff, count); | |
1791 | if (cprc->peak_time < cprc->cur_time) | |
1792 | cprc->peak_time = cprc->cur_time; | |
1793 | spin_unlock(&cprc->stat_lock); | |
1794 | } | |
1795 | ||
1796 | static int issue_checkpoint_thread(void *data) | |
1797 | { | |
1798 | struct f2fs_sb_info *sbi = data; | |
1799 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
1800 | wait_queue_head_t *q = &cprc->ckpt_wait_queue; | |
1801 | repeat: | |
1802 | if (kthread_should_stop()) | |
1803 | return 0; | |
1804 | ||
261eeb9c DJ |
1805 | if (!llist_empty(&cprc->issue_list)) |
1806 | __checkpoint_and_complete_reqs(sbi); | |
1807 | ||
261eeb9c DJ |
1808 | wait_event_interruptible(*q, |
1809 | kthread_should_stop() || !llist_empty(&cprc->issue_list)); | |
1810 | goto repeat; | |
1811 | } | |
1812 | ||
1813 | static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi, | |
1814 | struct ckpt_req *wait_req) | |
1815 | { | |
1816 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
1817 | ||
1818 | if (!llist_empty(&cprc->issue_list)) { | |
1819 | __checkpoint_and_complete_reqs(sbi); | |
1820 | } else { | |
1821 | /* already dispatched by issue_checkpoint_thread */ | |
1822 | if (wait_req) | |
1823 | wait_for_completion(&wait_req->wait); | |
1824 | } | |
1825 | } | |
1826 | ||
1827 | static void init_ckpt_req(struct ckpt_req *req) | |
1828 | { | |
1829 | memset(req, 0, sizeof(struct ckpt_req)); | |
1830 | ||
1831 | init_completion(&req->wait); | |
1832 | req->queue_time = ktime_get(); | |
1833 | } | |
1834 | ||
1835 | int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) | |
1836 | { | |
1837 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
1838 | struct ckpt_req req; | |
1839 | struct cp_control cpc; | |
1840 | ||
1841 | cpc.reason = __get_cp_reason(sbi); | |
eb85c241 CY |
1842 | if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC || |
1843 | sbi->umount_lock_holder == current) { | |
261eeb9c DJ |
1844 | int ret; |
1845 | ||
e4544b63 | 1846 | f2fs_down_write(&sbi->gc_lock); |
261eeb9c | 1847 | ret = f2fs_write_checkpoint(sbi, &cpc); |
e4544b63 | 1848 | f2fs_up_write(&sbi->gc_lock); |
261eeb9c DJ |
1849 | |
1850 | return ret; | |
1851 | } | |
1852 | ||
1853 | if (!cprc->f2fs_issue_ckpt) | |
1854 | return __write_checkpoint_sync(sbi); | |
1855 | ||
1856 | init_ckpt_req(&req); | |
1857 | ||
1858 | llist_add(&req.llnode, &cprc->issue_list); | |
1859 | atomic_inc(&cprc->queued_ckpt); | |
1860 | ||
3b42c741 CY |
1861 | /* |
1862 | * update issue_list before we wake up issue_checkpoint thread, | |
1863 | * this smp_mb() pairs with another barrier in ___wait_event(), | |
1864 | * see more details in comments of waitqueue_active(). | |
1865 | */ | |
261eeb9c DJ |
1866 | smp_mb(); |
1867 | ||
1868 | if (waitqueue_active(&cprc->ckpt_wait_queue)) | |
1869 | wake_up(&cprc->ckpt_wait_queue); | |
1870 | ||
1871 | if (cprc->f2fs_issue_ckpt) | |
1872 | wait_for_completion(&req.wait); | |
1873 | else | |
1874 | flush_remained_ckpt_reqs(sbi, &req); | |
1875 | ||
1876 | return req.ret; | |
1877 | } | |
1878 | ||
1879 | int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi) | |
1880 | { | |
1881 | dev_t dev = sbi->sb->s_bdev->bd_dev; | |
1882 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
1883 | ||
1884 | if (cprc->f2fs_issue_ckpt) | |
1885 | return 0; | |
1886 | ||
1887 | cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi, | |
1888 | "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev)); | |
1889 | if (IS_ERR(cprc->f2fs_issue_ckpt)) { | |
146dbcbf YL |
1890 | int err = PTR_ERR(cprc->f2fs_issue_ckpt); |
1891 | ||
261eeb9c | 1892 | cprc->f2fs_issue_ckpt = NULL; |
146dbcbf | 1893 | return err; |
261eeb9c DJ |
1894 | } |
1895 | ||
e6592066 | 1896 | set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio); |
261eeb9c DJ |
1897 | |
1898 | return 0; | |
1899 | } | |
1900 | ||
1901 | void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi) | |
1902 | { | |
1903 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
c7b58576 | 1904 | struct task_struct *ckpt_task; |
261eeb9c | 1905 | |
c7b58576 JK |
1906 | if (!cprc->f2fs_issue_ckpt) |
1907 | return; | |
261eeb9c | 1908 | |
c7b58576 JK |
1909 | ckpt_task = cprc->f2fs_issue_ckpt; |
1910 | cprc->f2fs_issue_ckpt = NULL; | |
1911 | kthread_stop(ckpt_task); | |
261eeb9c | 1912 | |
c7b58576 JK |
1913 | f2fs_flush_ckpt_thread(sbi); |
1914 | } | |
1915 | ||
1916 | void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi) | |
1917 | { | |
1918 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
1919 | ||
1920 | flush_remained_ckpt_reqs(sbi, NULL); | |
1921 | ||
1922 | /* Let's wait for the previous dispatched checkpoint. */ | |
1923 | while (atomic_read(&cprc->queued_ckpt)) | |
1924 | io_schedule_timeout(DEFAULT_IO_TIMEOUT); | |
261eeb9c DJ |
1925 | } |
1926 | ||
1927 | void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi) | |
1928 | { | |
1929 | struct ckpt_req_control *cprc = &sbi->cprc_info; | |
1930 | ||
1931 | atomic_set(&cprc->issued_ckpt, 0); | |
1932 | atomic_set(&cprc->total_ckpt, 0); | |
1933 | atomic_set(&cprc->queued_ckpt, 0); | |
e6592066 | 1934 | cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO; |
261eeb9c DJ |
1935 | init_waitqueue_head(&cprc->ckpt_wait_queue); |
1936 | init_llist_head(&cprc->issue_list); | |
1937 | spin_lock_init(&cprc->stat_lock); | |
1938 | } |