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