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