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