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