blob: 60f57c2d3524cf27ab08b200b6e4b3653b577dae [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * fs/f2fs/checkpoint.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/bio.h>
13#include <linux/mpage.h>
14#include <linux/writeback.h>
15#include <linux/blkdev.h>
16#include <linux/f2fs_fs.h>
17#include <linux/pagevec.h>
18#include <linux/swap.h>
19
20#include "f2fs.h"
21#include "node.h"
22#include "segment.h"
23#include "trace.h"
24#include <trace/events/f2fs.h>
25
26static struct kmem_cache *ino_entry_slab;
27struct kmem_cache *inode_entry_slab;
28
29void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
30{
31 set_ckpt_flags(sbi, CP_ERROR_FLAG);
32 if (!end_io)
33 f2fs_flush_merged_writes(sbi);
34}
35
36/*
37 * We guarantee no failure on the returned page.
38 */
39struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
40{
41 struct address_space *mapping = META_MAPPING(sbi);
42 struct page *page = NULL;
43repeat:
44 page = f2fs_grab_cache_page(mapping, index, false);
45 if (!page) {
46 cond_resched();
47 goto repeat;
48 }
49 f2fs_wait_on_page_writeback(page, META, true);
50 if (!PageUptodate(page))
51 SetPageUptodate(page);
52 return page;
53}
54
55/*
56 * We guarantee no failure on the returned page.
57 */
58static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
59 bool is_meta)
60{
61 struct address_space *mapping = META_MAPPING(sbi);
62 struct page *page;
63 struct f2fs_io_info fio = {
64 .sbi = sbi,
65 .type = META,
66 .op = REQ_OP_READ,
67 .op_flags = REQ_META | REQ_PRIO,
68 .old_blkaddr = index,
69 .new_blkaddr = index,
70 .encrypted_page = NULL,
71 .is_meta = is_meta,
72 };
73
74 if (unlikely(!is_meta))
75 fio.op_flags &= ~REQ_META;
76repeat:
77 page = f2fs_grab_cache_page(mapping, index, false);
78 if (!page) {
79 cond_resched();
80 goto repeat;
81 }
82 if (PageUptodate(page))
83 goto out;
84
85 fio.page = page;
86
87 if (f2fs_submit_page_bio(&fio)) {
88 memset(page_address(page), 0, PAGE_SIZE);
89 f2fs_stop_checkpoint(sbi, false);
90 f2fs_bug_on(sbi, 1);
91 return page;
92 }
93
94 lock_page(page);
95 if (unlikely(page->mapping != mapping)) {
96 f2fs_put_page(page, 1);
97 goto repeat;
98 }
99
100 /*
101 * if there is any IO error when accessing device, make our filesystem
102 * readonly and make sure do not write checkpoint with non-uptodate
103 * meta page.
104 */
105 if (unlikely(!PageUptodate(page)))
106 f2fs_stop_checkpoint(sbi, false);
107out:
108 return page;
109}
110
111struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
112{
113 return __get_meta_page(sbi, index, true);
114}
115
116/* for POR only */
117struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
118{
119 return __get_meta_page(sbi, index, false);
120}
121
122bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
123 block_t blkaddr, int type)
124{
125 switch (type) {
126 case META_NAT:
127 break;
128 case META_SIT:
129 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
130 return false;
131 break;
132 case META_SSA:
133 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
134 blkaddr < SM_I(sbi)->ssa_blkaddr))
135 return false;
136 break;
137 case META_CP:
138 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
139 blkaddr < __start_cp_addr(sbi)))
140 return false;
141 break;
142 case META_POR:
143 case DATA_GENERIC:
144 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
145 blkaddr < MAIN_BLKADDR(sbi))) {
146 if (type == DATA_GENERIC) {
147 f2fs_msg(sbi->sb, KERN_WARNING,
148 "access invalid blkaddr:%u", blkaddr);
149 WARN_ON(1);
150 }
151 return false;
152 }
153 break;
154 case META_GENERIC:
155 if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
156 blkaddr >= MAIN_BLKADDR(sbi)))
157 return false;
158 break;
159 default:
160 BUG();
161 }
162
163 return true;
164}
165
166/*
167 * Readahead CP/NAT/SIT/SSA pages
168 */
169int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
170 int type, bool sync)
171{
172 struct page *page;
173 block_t blkno = start;
174 struct f2fs_io_info fio = {
175 .sbi = sbi,
176 .type = META,
177 .op = REQ_OP_READ,
178 .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
179 .encrypted_page = NULL,
180 .in_list = false,
181 .is_meta = (type != META_POR),
182 };
183 struct blk_plug plug;
184
185 if (unlikely(type == META_POR))
186 fio.op_flags &= ~REQ_META;
187
188 blk_start_plug(&plug);
189 for (; nrpages-- > 0; blkno++) {
190
191 if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
192 goto out;
193
194 switch (type) {
195 case META_NAT:
196 if (unlikely(blkno >=
197 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
198 blkno = 0;
199 /* get nat block addr */
200 fio.new_blkaddr = current_nat_addr(sbi,
201 blkno * NAT_ENTRY_PER_BLOCK);
202 break;
203 case META_SIT:
204 /* get sit block addr */
205 fio.new_blkaddr = current_sit_addr(sbi,
206 blkno * SIT_ENTRY_PER_BLOCK);
207 break;
208 case META_SSA:
209 case META_CP:
210 case META_POR:
211 fio.new_blkaddr = blkno;
212 break;
213 default:
214 BUG();
215 }
216
217 page = f2fs_grab_cache_page(META_MAPPING(sbi),
218 fio.new_blkaddr, false);
219 if (!page)
220 continue;
221 if (PageUptodate(page)) {
222 f2fs_put_page(page, 1);
223 continue;
224 }
225
226 fio.page = page;
227 f2fs_submit_page_bio(&fio);
228 f2fs_put_page(page, 0);
229 }
230out:
231 blk_finish_plug(&plug);
232 return blkno - start;
233}
234
235void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
236{
237 struct page *page;
238 bool readahead = false;
239
240 page = find_get_page(META_MAPPING(sbi), index);
241 if (!page || !PageUptodate(page))
242 readahead = true;
243 f2fs_put_page(page, 0);
244
245 if (readahead)
246 ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
247}
248
249static int __f2fs_write_meta_page(struct page *page,
250 struct writeback_control *wbc,
251 enum iostat_type io_type)
252{
253 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
254
255 trace_f2fs_writepage(page, META);
256
257 if (unlikely(f2fs_cp_error(sbi))) {
258 dec_page_count(sbi, F2FS_DIRTY_META);
259 unlock_page(page);
260 return 0;
261 }
262 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
263 goto redirty_out;
264 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
265 goto redirty_out;
266
267 write_meta_page(sbi, page, io_type);
268 dec_page_count(sbi, F2FS_DIRTY_META);
269
270 if (wbc->for_reclaim)
271 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
272 0, page->index, META);
273
274 unlock_page(page);
275
276 if (unlikely(f2fs_cp_error(sbi)))
277 f2fs_submit_merged_write(sbi, META);
278
279 return 0;
280
281redirty_out:
282 redirty_page_for_writepage(wbc, page);
283 return AOP_WRITEPAGE_ACTIVATE;
284}
285
286static int f2fs_write_meta_page(struct page *page,
287 struct writeback_control *wbc)
288{
289 return __f2fs_write_meta_page(page, wbc, FS_META_IO);
290}
291
292static int f2fs_write_meta_pages(struct address_space *mapping,
293 struct writeback_control *wbc)
294{
295 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
296 long diff, written;
297
298 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
299 goto skip_write;
300
301 /* collect a number of dirty meta pages and write together */
302 if (wbc->for_kupdate ||
303 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
304 goto skip_write;
305
306 /* if locked failed, cp will flush dirty pages instead */
307 if (!mutex_trylock(&sbi->cp_mutex))
308 goto skip_write;
309
310 trace_f2fs_writepages(mapping->host, wbc, META);
311 diff = nr_pages_to_write(sbi, META, wbc);
312 written = sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
313 mutex_unlock(&sbi->cp_mutex);
314 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
315 return 0;
316
317skip_write:
318 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
319 trace_f2fs_writepages(mapping->host, wbc, META);
320 return 0;
321}
322
323long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
324 long nr_to_write, enum iostat_type io_type)
325{
326 struct address_space *mapping = META_MAPPING(sbi);
327 pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
328 struct pagevec pvec;
329 long nwritten = 0;
330 struct writeback_control wbc = {
331 .for_reclaim = 0,
332 };
333 struct blk_plug plug;
334
335 pagevec_init(&pvec, 0);
336
337 blk_start_plug(&plug);
338
339 while (index <= end) {
340 int i, nr_pages;
341 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
342 PAGECACHE_TAG_DIRTY,
343 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
344 if (unlikely(nr_pages == 0))
345 break;
346
347 for (i = 0; i < nr_pages; i++) {
348 struct page *page = pvec.pages[i];
349
350 if (prev == ULONG_MAX)
351 prev = page->index - 1;
352 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
353 pagevec_release(&pvec);
354 goto stop;
355 }
356
357 lock_page(page);
358
359 if (unlikely(page->mapping != mapping)) {
360continue_unlock:
361 unlock_page(page);
362 continue;
363 }
364 if (!PageDirty(page)) {
365 /* someone wrote it for us */
366 goto continue_unlock;
367 }
368
369 f2fs_wait_on_page_writeback(page, META, true);
370
371 BUG_ON(PageWriteback(page));
372 if (!clear_page_dirty_for_io(page))
373 goto continue_unlock;
374
375 if (__f2fs_write_meta_page(page, &wbc, io_type)) {
376 unlock_page(page);
377 break;
378 }
379 nwritten++;
380 prev = page->index;
381 if (unlikely(nwritten >= nr_to_write))
382 break;
383 }
384 pagevec_release(&pvec);
385 cond_resched();
386 }
387stop:
388 if (nwritten)
389 f2fs_submit_merged_write(sbi, type);
390
391 blk_finish_plug(&plug);
392
393 return nwritten;
394}
395
396static int f2fs_set_meta_page_dirty(struct page *page)
397{
398 trace_f2fs_set_page_dirty(page, META);
399
400 if (!PageUptodate(page))
401 SetPageUptodate(page);
402 if (!PageDirty(page)) {
403 __set_page_dirty_nobuffers(page);
404 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
405 SetPagePrivate(page);
406 f2fs_trace_pid(page);
407 return 1;
408 }
409 return 0;
410}
411
412const struct address_space_operations f2fs_meta_aops = {
413 .writepage = f2fs_write_meta_page,
414 .writepages = f2fs_write_meta_pages,
415 .set_page_dirty = f2fs_set_meta_page_dirty,
416 .invalidatepage = f2fs_invalidate_page,
417 .releasepage = f2fs_release_page,
418#ifdef CONFIG_MIGRATION
419 .migratepage = f2fs_migrate_page,
420#endif
421};
422
423static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
424 unsigned int devidx, int type)
425{
426 struct inode_management *im = &sbi->im[type];
427 struct ino_entry *e, *tmp;
428
429 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
430
431 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
432
433 spin_lock(&im->ino_lock);
434 e = radix_tree_lookup(&im->ino_root, ino);
435 if (!e) {
436 e = tmp;
437 if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
438 f2fs_bug_on(sbi, 1);
439
440 memset(e, 0, sizeof(struct ino_entry));
441 e->ino = ino;
442
443 list_add_tail(&e->list, &im->ino_list);
444 if (type != ORPHAN_INO)
445 im->ino_num++;
446 }
447
448 if (type == FLUSH_INO)
449 f2fs_set_bit(devidx, (char *)&e->dirty_device);
450
451 spin_unlock(&im->ino_lock);
452 radix_tree_preload_end();
453
454 if (e != tmp)
455 kmem_cache_free(ino_entry_slab, tmp);
456}
457
458static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
459{
460 struct inode_management *im = &sbi->im[type];
461 struct ino_entry *e;
462
463 spin_lock(&im->ino_lock);
464 e = radix_tree_lookup(&im->ino_root, ino);
465 if (e) {
466 list_del(&e->list);
467 radix_tree_delete(&im->ino_root, ino);
468 im->ino_num--;
469 spin_unlock(&im->ino_lock);
470 kmem_cache_free(ino_entry_slab, e);
471 return;
472 }
473 spin_unlock(&im->ino_lock);
474}
475
476void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
477{
478 /* add new dirty ino entry into list */
479 __add_ino_entry(sbi, ino, 0, type);
480}
481
482void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
483{
484 /* remove dirty ino entry from list */
485 __remove_ino_entry(sbi, ino, type);
486}
487
488/* mode should be APPEND_INO or UPDATE_INO */
489bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
490{
491 struct inode_management *im = &sbi->im[mode];
492 struct ino_entry *e;
493
494 spin_lock(&im->ino_lock);
495 e = radix_tree_lookup(&im->ino_root, ino);
496 spin_unlock(&im->ino_lock);
497 return e ? true : false;
498}
499
500void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
501{
502 struct ino_entry *e, *tmp;
503 int i;
504
505 for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
506 struct inode_management *im = &sbi->im[i];
507
508 spin_lock(&im->ino_lock);
509 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
510 list_del(&e->list);
511 radix_tree_delete(&im->ino_root, e->ino);
512 kmem_cache_free(ino_entry_slab, e);
513 im->ino_num--;
514 }
515 spin_unlock(&im->ino_lock);
516 }
517}
518
519void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
520 unsigned int devidx, int type)
521{
522 __add_ino_entry(sbi, ino, devidx, type);
523}
524
525bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
526 unsigned int devidx, int type)
527{
528 struct inode_management *im = &sbi->im[type];
529 struct ino_entry *e;
530 bool is_dirty = false;
531
532 spin_lock(&im->ino_lock);
533 e = radix_tree_lookup(&im->ino_root, ino);
534 if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
535 is_dirty = true;
536 spin_unlock(&im->ino_lock);
537 return is_dirty;
538}
539
540int acquire_orphan_inode(struct f2fs_sb_info *sbi)
541{
542 struct inode_management *im = &sbi->im[ORPHAN_INO];
543 int err = 0;
544
545 spin_lock(&im->ino_lock);
546
547#ifdef CONFIG_F2FS_FAULT_INJECTION
548 if (time_to_inject(sbi, FAULT_ORPHAN)) {
549 spin_unlock(&im->ino_lock);
550 f2fs_show_injection_info(FAULT_ORPHAN);
551 return -ENOSPC;
552 }
553#endif
554 if (unlikely(im->ino_num >= sbi->max_orphans))
555 err = -ENOSPC;
556 else
557 im->ino_num++;
558 spin_unlock(&im->ino_lock);
559
560 return err;
561}
562
563void release_orphan_inode(struct f2fs_sb_info *sbi)
564{
565 struct inode_management *im = &sbi->im[ORPHAN_INO];
566
567 spin_lock(&im->ino_lock);
568 f2fs_bug_on(sbi, im->ino_num == 0);
569 im->ino_num--;
570 spin_unlock(&im->ino_lock);
571}
572
573void add_orphan_inode(struct inode *inode)
574{
575 /* add new orphan ino entry into list */
576 __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
577 update_inode_page(inode);
578}
579
580void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
581{
582 /* remove orphan entry from orphan list */
583 __remove_ino_entry(sbi, ino, ORPHAN_INO);
584}
585
586static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
587{
588 struct inode *inode;
589 struct node_info ni;
590 int err = acquire_orphan_inode(sbi);
591
592 if (err)
593 goto err_out;
594
595 __add_ino_entry(sbi, ino, 0, ORPHAN_INO);
596
597 inode = f2fs_iget_retry(sbi->sb, ino);
598 if (IS_ERR(inode)) {
599 /*
600 * there should be a bug that we can't find the entry
601 * to orphan inode.
602 */
603 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
604 return PTR_ERR(inode);
605 }
606
607 err = dquot_initialize(inode);
608 if (err)
609 goto err_out;
610
611 dquot_initialize(inode);
612 clear_nlink(inode);
613
614 /* truncate all the data during iput */
615 iput(inode);
616
617 get_node_info(sbi, ino, &ni);
618
619 /* ENOMEM was fully retried in f2fs_evict_inode. */
620 if (ni.blk_addr != NULL_ADDR) {
621 err = -EIO;
622 goto err_out;
623 }
624 __remove_ino_entry(sbi, ino, ORPHAN_INO);
625 return 0;
626
627err_out:
628 set_sbi_flag(sbi, SBI_NEED_FSCK);
629 f2fs_msg(sbi->sb, KERN_WARNING,
630 "%s: orphan failed (ino=%x), run fsck to fix.",
631 __func__, ino);
632 return err;
633}
634
635int recover_orphan_inodes(struct f2fs_sb_info *sbi)
636{
637 block_t start_blk, orphan_blocks, i, j;
638 unsigned int s_flags = sbi->sb->s_flags;
639 int err = 0;
640#ifdef CONFIG_QUOTA
641 int quota_enabled;
642#endif
643
644 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
645 return 0;
646
647 if (s_flags & MS_RDONLY) {
648 f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
649 sbi->sb->s_flags &= ~MS_RDONLY;
650 }
651
652#ifdef CONFIG_QUOTA
653 /* Needed for iput() to work correctly and not trash data */
654 sbi->sb->s_flags |= MS_ACTIVE;
655
656 /* Turn on quotas so that they are updated correctly */
657 quota_enabled = f2fs_enable_quota_files(sbi, s_flags & MS_RDONLY);
658#endif
659
660 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
661 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
662
663 ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
664
665 for (i = 0; i < orphan_blocks; i++) {
666 struct page *page = get_meta_page(sbi, start_blk + i);
667 struct f2fs_orphan_block *orphan_blk;
668
669 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
670 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
671 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
672 err = recover_orphan_inode(sbi, ino);
673 if (err) {
674 f2fs_put_page(page, 1);
675 goto out;
676 }
677 }
678 f2fs_put_page(page, 1);
679 }
680 /* clear Orphan Flag */
681 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
682out:
683#ifdef CONFIG_QUOTA
684 /* Turn quotas off */
685 if (quota_enabled)
686 f2fs_quota_off_umount(sbi->sb);
687#endif
688 sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
689
690 return err;
691}
692
693static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
694{
695 struct list_head *head;
696 struct f2fs_orphan_block *orphan_blk = NULL;
697 unsigned int nentries = 0;
698 unsigned short index = 1;
699 unsigned short orphan_blocks;
700 struct page *page = NULL;
701 struct ino_entry *orphan = NULL;
702 struct inode_management *im = &sbi->im[ORPHAN_INO];
703
704 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
705
706 /*
707 * we don't need to do spin_lock(&im->ino_lock) here, since all the
708 * orphan inode operations are covered under f2fs_lock_op().
709 * And, spin_lock should be avoided due to page operations below.
710 */
711 head = &im->ino_list;
712
713 /* loop for each orphan inode entry and write them in Jornal block */
714 list_for_each_entry(orphan, head, list) {
715 if (!page) {
716 page = grab_meta_page(sbi, start_blk++);
717 orphan_blk =
718 (struct f2fs_orphan_block *)page_address(page);
719 memset(orphan_blk, 0, sizeof(*orphan_blk));
720 }
721
722 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
723
724 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
725 /*
726 * an orphan block is full of 1020 entries,
727 * then we need to flush current orphan blocks
728 * and bring another one in memory
729 */
730 orphan_blk->blk_addr = cpu_to_le16(index);
731 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
732 orphan_blk->entry_count = cpu_to_le32(nentries);
733 set_page_dirty(page);
734 f2fs_put_page(page, 1);
735 index++;
736 nentries = 0;
737 page = NULL;
738 }
739 }
740
741 if (page) {
742 orphan_blk->blk_addr = cpu_to_le16(index);
743 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
744 orphan_blk->entry_count = cpu_to_le32(nentries);
745 set_page_dirty(page);
746 f2fs_put_page(page, 1);
747 }
748}
749
750static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
751 struct f2fs_checkpoint **cp_block, struct page **cp_page,
752 unsigned long long *version)
753{
754 unsigned long blk_size = sbi->blocksize;
755 size_t crc_offset = 0;
756 __u32 crc = 0;
757
758 *cp_page = get_meta_page(sbi, cp_addr);
759 *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
760
761 crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
762 if (crc_offset > (blk_size - sizeof(__le32))) {
763 f2fs_put_page(*cp_page, 1);
764 f2fs_msg(sbi->sb, KERN_WARNING,
765 "invalid crc_offset: %zu", crc_offset);
766 return -EINVAL;
767 }
768
769 crc = cur_cp_crc(*cp_block);
770 if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
771 f2fs_put_page(*cp_page, 1);
772 f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
773 return -EINVAL;
774 }
775
776 *version = cur_cp_version(*cp_block);
777 return 0;
778}
779
780static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
781 block_t cp_addr, unsigned long long *version)
782{
783 struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
784 struct f2fs_checkpoint *cp_block = NULL;
785 unsigned long long cur_version = 0, pre_version = 0;
786 int err;
787
788 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
789 &cp_page_1, version);
790 if (err)
791 return NULL;
792
793 if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
794 sbi->blocks_per_seg) {
795 f2fs_msg(sbi->sb, KERN_WARNING,
796 "invalid cp_pack_total_block_count:%u",
797 le32_to_cpu(cp_block->cp_pack_total_block_count));
798 goto invalid_cp;
799 }
800 pre_version = *version;
801
802 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
803 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
804 &cp_page_2, version);
805 if (err)
806 goto invalid_cp;
807 cur_version = *version;
808
809 if (cur_version == pre_version) {
810 *version = cur_version;
811 f2fs_put_page(cp_page_2, 1);
812 return cp_page_1;
813 }
814 f2fs_put_page(cp_page_2, 1);
815invalid_cp:
816 f2fs_put_page(cp_page_1, 1);
817 return NULL;
818}
819
820int get_valid_checkpoint(struct f2fs_sb_info *sbi)
821{
822 struct f2fs_checkpoint *cp_block;
823 struct f2fs_super_block *fsb = sbi->raw_super;
824 struct page *cp1, *cp2, *cur_page;
825 unsigned long blk_size = sbi->blocksize;
826 unsigned long long cp1_version = 0, cp2_version = 0;
827 unsigned long long cp_start_blk_no;
828 unsigned int cp_blks = 1 + __cp_payload(sbi);
829 block_t cp_blk_no;
830 int i;
831 int err;
832
833 sbi->ckpt = f2fs_kzalloc(sbi, cp_blks * blk_size, GFP_KERNEL);
834 if (!sbi->ckpt)
835 return -ENOMEM;
836 /*
837 * Finding out valid cp block involves read both
838 * sets( cp pack1 and cp pack 2)
839 */
840 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
841 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
842
843 /* The second checkpoint pack should start at the next segment */
844 cp_start_blk_no += ((unsigned long long)1) <<
845 le32_to_cpu(fsb->log_blocks_per_seg);
846 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
847
848 if (cp1 && cp2) {
849 if (ver_after(cp2_version, cp1_version))
850 cur_page = cp2;
851 else
852 cur_page = cp1;
853 } else if (cp1) {
854 cur_page = cp1;
855 } else if (cp2) {
856 cur_page = cp2;
857 } else {
858 err = -EFSCORRUPTED;
859 goto fail_no_cp;
860 }
861
862 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
863 memcpy(sbi->ckpt, cp_block, blk_size);
864
865 if (cur_page == cp1)
866 sbi->cur_cp_pack = 1;
867 else
868 sbi->cur_cp_pack = 2;
869
870 /* Sanity checking of checkpoint */
871 if (sanity_check_ckpt(sbi)) {
872 err = -EFSCORRUPTED;
873 goto free_fail_no_cp;
874 }
875
876 if (cp_blks <= 1)
877 goto done;
878
879 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
880 if (cur_page == cp2)
881 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
882
883 for (i = 1; i < cp_blks; i++) {
884 void *sit_bitmap_ptr;
885 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
886
887 cur_page = get_meta_page(sbi, cp_blk_no + i);
888 sit_bitmap_ptr = page_address(cur_page);
889 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
890 f2fs_put_page(cur_page, 1);
891 }
892done:
893 f2fs_put_page(cp1, 1);
894 f2fs_put_page(cp2, 1);
895 return 0;
896
897free_fail_no_cp:
898 f2fs_put_page(cp1, 1);
899 f2fs_put_page(cp2, 1);
900fail_no_cp:
901 kfree(sbi->ckpt);
902 return err;
903}
904
905static void __add_dirty_inode(struct inode *inode, enum inode_type type)
906{
907 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
908 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
909
910 if (is_inode_flag_set(inode, flag))
911 return;
912
913 set_inode_flag(inode, flag);
914 if (!f2fs_is_volatile_file(inode))
915 list_add_tail(&F2FS_I(inode)->dirty_list,
916 &sbi->inode_list[type]);
917 stat_inc_dirty_inode(sbi, type);
918}
919
920static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
921{
922 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
923
924 if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
925 return;
926
927 list_del_init(&F2FS_I(inode)->dirty_list);
928 clear_inode_flag(inode, flag);
929 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
930}
931
932void update_dirty_page(struct inode *inode, struct page *page)
933{
934 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
935 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
936
937 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
938 !S_ISLNK(inode->i_mode))
939 return;
940
941 spin_lock(&sbi->inode_lock[type]);
942 if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
943 __add_dirty_inode(inode, type);
944 inode_inc_dirty_pages(inode);
945 spin_unlock(&sbi->inode_lock[type]);
946
947 SetPagePrivate(page);
948 f2fs_trace_pid(page);
949}
950
951void remove_dirty_inode(struct inode *inode)
952{
953 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
954 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
955
956 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
957 !S_ISLNK(inode->i_mode))
958 return;
959
960 if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
961 return;
962
963 spin_lock(&sbi->inode_lock[type]);
964 __remove_dirty_inode(inode, type);
965 spin_unlock(&sbi->inode_lock[type]);
966}
967
968int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
969{
970 struct list_head *head;
971 struct inode *inode;
972 struct f2fs_inode_info *fi;
973 bool is_dir = (type == DIR_INODE);
974 unsigned long ino = 0;
975
976 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
977 get_pages(sbi, is_dir ?
978 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
979retry:
980 if (unlikely(f2fs_cp_error(sbi)))
981 return -EIO;
982
983 spin_lock(&sbi->inode_lock[type]);
984
985 head = &sbi->inode_list[type];
986 if (list_empty(head)) {
987 spin_unlock(&sbi->inode_lock[type]);
988 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
989 get_pages(sbi, is_dir ?
990 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
991 return 0;
992 }
993 fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
994 inode = igrab(&fi->vfs_inode);
995 spin_unlock(&sbi->inode_lock[type]);
996 if (inode) {
997 unsigned long cur_ino = inode->i_ino;
998
999 if (is_dir)
1000 F2FS_I(inode)->cp_task = current;
1001
1002 filemap_fdatawrite(inode->i_mapping);
1003
1004 if (is_dir)
1005 F2FS_I(inode)->cp_task = NULL;
1006
1007 iput(inode);
1008 /* We need to give cpu to another writers. */
1009 if (ino == cur_ino) {
1010 congestion_wait(BLK_RW_ASYNC, HZ/50);
1011 cond_resched();
1012 } else {
1013 ino = cur_ino;
1014 }
1015 } else {
1016 /*
1017 * We should submit bio, since it exists several
1018 * wribacking dentry pages in the freeing inode.
1019 */
1020 f2fs_submit_merged_write(sbi, DATA);
1021 cond_resched();
1022 }
1023 goto retry;
1024}
1025
1026int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
1027{
1028 struct list_head *head = &sbi->inode_list[DIRTY_META];
1029 struct inode *inode;
1030 struct f2fs_inode_info *fi;
1031 s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
1032
1033 while (total--) {
1034 if (unlikely(f2fs_cp_error(sbi)))
1035 return -EIO;
1036
1037 spin_lock(&sbi->inode_lock[DIRTY_META]);
1038 if (list_empty(head)) {
1039 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1040 return 0;
1041 }
1042 fi = list_first_entry(head, struct f2fs_inode_info,
1043 gdirty_list);
1044 inode = igrab(&fi->vfs_inode);
1045 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1046 if (inode) {
1047 sync_inode_metadata(inode, 0);
1048
1049 /* it's on eviction */
1050 if (is_inode_flag_set(inode, FI_DIRTY_INODE))
1051 update_inode_page(inode);
1052 iput(inode);
1053 }
1054 }
1055 return 0;
1056}
1057
1058static void __prepare_cp_block(struct f2fs_sb_info *sbi)
1059{
1060 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1061 struct f2fs_nm_info *nm_i = NM_I(sbi);
1062 nid_t last_nid = nm_i->next_scan_nid;
1063
1064 next_free_nid(sbi, &last_nid);
1065 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1066 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1067 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1068 ckpt->next_free_nid = cpu_to_le32(last_nid);
1069}
1070
1071/*
1072 * Freeze all the FS-operations for checkpoint.
1073 */
1074static int block_operations(struct f2fs_sb_info *sbi)
1075{
1076 struct writeback_control wbc = {
1077 .sync_mode = WB_SYNC_ALL,
1078 .nr_to_write = LONG_MAX,
1079 .for_reclaim = 0,
1080 };
1081 struct blk_plug plug;
1082 int err = 0;
1083
1084 blk_start_plug(&plug);
1085
1086retry_flush_dents:
1087 f2fs_lock_all(sbi);
1088 /* write all the dirty dentry pages */
1089 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1090 f2fs_unlock_all(sbi);
1091 err = sync_dirty_inodes(sbi, DIR_INODE);
1092 if (err)
1093 goto out;
1094 cond_resched();
1095 goto retry_flush_dents;
1096 }
1097
1098 /*
1099 * POR: we should ensure that there are no dirty node pages
1100 * until finishing nat/sit flush. inode->i_blocks can be updated.
1101 */
1102 down_write(&sbi->node_change);
1103
1104 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1105 up_write(&sbi->node_change);
1106 f2fs_unlock_all(sbi);
1107 err = f2fs_sync_inode_meta(sbi);
1108 if (err)
1109 goto out;
1110 cond_resched();
1111 goto retry_flush_dents;
1112 }
1113
1114retry_flush_nodes:
1115 down_write(&sbi->node_write);
1116
1117 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1118 up_write(&sbi->node_write);
1119 err = sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1120 if (err) {
1121 up_write(&sbi->node_change);
1122 f2fs_unlock_all(sbi);
1123 goto out;
1124 }
1125 cond_resched();
1126 goto retry_flush_nodes;
1127 }
1128
1129 /*
1130 * sbi->node_change is used only for AIO write_begin path which produces
1131 * dirty node blocks and some checkpoint values by block allocation.
1132 */
1133 __prepare_cp_block(sbi);
1134 up_write(&sbi->node_change);
1135out:
1136 blk_finish_plug(&plug);
1137 return err;
1138}
1139
1140static void unblock_operations(struct f2fs_sb_info *sbi)
1141{
1142 up_write(&sbi->node_write);
1143 f2fs_unlock_all(sbi);
1144}
1145
1146static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1147{
1148 DEFINE_WAIT(wait);
1149
1150 for (;;) {
1151 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1152
1153 if (!get_pages(sbi, F2FS_WB_CP_DATA))
1154 break;
1155
1156 io_schedule_timeout(5*HZ);
1157 }
1158 finish_wait(&sbi->cp_wait, &wait);
1159}
1160
1161static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1162{
1163 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1164 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1165 unsigned long flags;
1166
1167 spin_lock_irqsave(&sbi->cp_lock, flags);
1168
1169 if ((cpc->reason & CP_UMOUNT) &&
1170 le32_to_cpu(ckpt->cp_pack_total_block_count) >
1171 sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1172 disable_nat_bits(sbi, false);
1173
1174 if (cpc->reason & CP_TRIMMED)
1175 __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1176 else
1177 __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1178
1179 if (cpc->reason & CP_UMOUNT)
1180 __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1181 else
1182 __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1183
1184 if (cpc->reason & CP_FASTBOOT)
1185 __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1186 else
1187 __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1188
1189 if (orphan_num)
1190 __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1191 else
1192 __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1193
1194 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1195 __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1196
1197 /* set this flag to activate crc|cp_ver for recovery */
1198 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1199 __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1200
1201 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1202}
1203
1204static void commit_checkpoint(struct f2fs_sb_info *sbi,
1205 void *src, block_t blk_addr)
1206{
1207 struct writeback_control wbc = {
1208 .for_reclaim = 0,
1209 };
1210
1211 /*
1212 * pagevec_lookup_tag and lock_page again will take
1213 * some extra time. Therefore, update_meta_pages and
1214 * sync_meta_pages are combined in this function.
1215 */
1216 struct page *page = grab_meta_page(sbi, blk_addr);
1217 int err;
1218
1219 memcpy(page_address(page), src, PAGE_SIZE);
1220 set_page_dirty(page);
1221
1222 f2fs_wait_on_page_writeback(page, META, true);
1223 f2fs_bug_on(sbi, PageWriteback(page));
1224 if (unlikely(!clear_page_dirty_for_io(page)))
1225 f2fs_bug_on(sbi, 1);
1226
1227 /* writeout cp pack 2 page */
1228 err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1229 f2fs_bug_on(sbi, err);
1230
1231 f2fs_put_page(page, 0);
1232
1233 /* submit checkpoint (with barrier if NOBARRIER is not set) */
1234 f2fs_submit_merged_write(sbi, META_FLUSH);
1235}
1236
1237static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1238{
1239 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1240 struct f2fs_nm_info *nm_i = NM_I(sbi);
1241 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1242 block_t start_blk;
1243 unsigned int data_sum_blocks, orphan_blocks;
1244 __u32 crc32 = 0;
1245 int i;
1246 int cp_payload_blks = __cp_payload(sbi);
1247 struct super_block *sb = sbi->sb;
1248 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1249 u64 kbytes_written;
1250 int err;
1251
1252 /* Flush all the NAT/SIT pages */
1253 while (get_pages(sbi, F2FS_DIRTY_META)) {
1254 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1255 if (unlikely(f2fs_cp_error(sbi)))
1256 return -EIO;
1257 }
1258
1259 /*
1260 * modify checkpoint
1261 * version number is already updated
1262 */
1263 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
1264 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1265 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1266 ckpt->cur_node_segno[i] =
1267 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1268 ckpt->cur_node_blkoff[i] =
1269 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1270 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1271 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1272 }
1273 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1274 ckpt->cur_data_segno[i] =
1275 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1276 ckpt->cur_data_blkoff[i] =
1277 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1278 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1279 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1280 }
1281
1282 /* 2 cp + n data seg summary + orphan inode blocks */
1283 data_sum_blocks = npages_for_summary_flush(sbi, false);
1284 spin_lock_irqsave(&sbi->cp_lock, flags);
1285 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1286 __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1287 else
1288 __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1289 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1290
1291 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1292 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1293 orphan_blocks);
1294
1295 if (__remain_node_summaries(cpc->reason))
1296 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1297 cp_payload_blks + data_sum_blocks +
1298 orphan_blocks + NR_CURSEG_NODE_TYPE);
1299 else
1300 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1301 cp_payload_blks + data_sum_blocks +
1302 orphan_blocks);
1303
1304 /* update ckpt flag for checkpoint */
1305 update_ckpt_flags(sbi, cpc);
1306
1307 /* update SIT/NAT bitmap */
1308 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1309 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1310
1311 crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
1312 *((__le32 *)((unsigned char *)ckpt +
1313 le32_to_cpu(ckpt->checksum_offset)))
1314 = cpu_to_le32(crc32);
1315
1316 start_blk = __start_cp_next_addr(sbi);
1317
1318 /* write nat bits */
1319 if (enabled_nat_bits(sbi, cpc)) {
1320 __u64 cp_ver = cur_cp_version(ckpt);
1321 block_t blk;
1322
1323 cp_ver |= ((__u64)crc32 << 32);
1324 *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
1325
1326 blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
1327 for (i = 0; i < nm_i->nat_bits_blocks; i++)
1328 update_meta_page(sbi, nm_i->nat_bits +
1329 (i << F2FS_BLKSIZE_BITS), blk + i);
1330
1331 /* Flush all the NAT BITS pages */
1332 while (get_pages(sbi, F2FS_DIRTY_META)) {
1333 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1334 if (unlikely(f2fs_cp_error(sbi)))
1335 return -EIO;
1336 }
1337 }
1338
1339 /* write out checkpoint buffer at block 0 */
1340 update_meta_page(sbi, ckpt, start_blk++);
1341
1342 for (i = 1; i < 1 + cp_payload_blks; i++)
1343 update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1344 start_blk++);
1345
1346 if (orphan_num) {
1347 write_orphan_inodes(sbi, start_blk);
1348 start_blk += orphan_blocks;
1349 }
1350
1351 write_data_summaries(sbi, start_blk);
1352 start_blk += data_sum_blocks;
1353
1354 /* Record write statistics in the hot node summary */
1355 kbytes_written = sbi->kbytes_written;
1356 if (sb->s_bdev->bd_part)
1357 kbytes_written += BD_PART_WRITTEN(sbi);
1358
1359 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1360
1361 if (__remain_node_summaries(cpc->reason)) {
1362 write_node_summaries(sbi, start_blk);
1363 start_blk += NR_CURSEG_NODE_TYPE;
1364 }
1365
1366 /* update user_block_counts */
1367 sbi->last_valid_block_count = sbi->total_valid_block_count;
1368 percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1369
1370 /* Here, we have one bio having CP pack except cp pack 2 page */
1371 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1372
1373 /* wait for previous submitted meta pages writeback */
1374 wait_on_all_pages_writeback(sbi);
1375
1376 if (unlikely(f2fs_cp_error(sbi)))
1377 return -EIO;
1378
1379 /* flush all device cache */
1380 err = f2fs_flush_device_cache(sbi);
1381 if (err)
1382 return err;
1383
1384 /* barrier and flush checkpoint cp pack 2 page if it can */
1385 commit_checkpoint(sbi, ckpt, start_blk);
1386 wait_on_all_pages_writeback(sbi);
1387
1388 release_ino_entry(sbi, false);
1389
1390 if (unlikely(f2fs_cp_error(sbi)))
1391 return -EIO;
1392
1393 clear_sbi_flag(sbi, SBI_IS_DIRTY);
1394 clear_sbi_flag(sbi, SBI_NEED_CP);
1395 __set_cp_next_pack(sbi);
1396
1397 /*
1398 * redirty superblock if metadata like node page or inode cache is
1399 * updated during writing checkpoint.
1400 */
1401 if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1402 get_pages(sbi, F2FS_DIRTY_IMETA))
1403 set_sbi_flag(sbi, SBI_IS_DIRTY);
1404
1405 f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1406
1407 return 0;
1408}
1409
1410/*
1411 * We guarantee that this checkpoint procedure will not fail.
1412 */
1413int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1414{
1415 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1416 unsigned long long ckpt_ver;
1417 int err = 0;
1418
1419 mutex_lock(&sbi->cp_mutex);
1420
1421 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1422 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1423 ((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
1424 goto out;
1425 if (unlikely(f2fs_cp_error(sbi))) {
1426 err = -EIO;
1427 goto out;
1428 }
1429 if (f2fs_readonly(sbi->sb)) {
1430 err = -EROFS;
1431 goto out;
1432 }
1433
1434 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1435
1436 err = block_operations(sbi);
1437 if (err)
1438 goto out;
1439
1440 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1441
1442 f2fs_flush_merged_writes(sbi);
1443
1444 /* this is the case of multiple fstrims without any changes */
1445 if (cpc->reason & CP_DISCARD) {
1446 if (!exist_trim_candidates(sbi, cpc)) {
1447 unblock_operations(sbi);
1448 goto out;
1449 }
1450
1451 if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1452 SIT_I(sbi)->dirty_sentries == 0 &&
1453 prefree_segments(sbi) == 0) {
1454 flush_sit_entries(sbi, cpc);
1455 clear_prefree_segments(sbi, cpc);
1456 unblock_operations(sbi);
1457 goto out;
1458 }
1459 }
1460
1461 /*
1462 * update checkpoint pack index
1463 * Increase the version number so that
1464 * SIT entries and seg summaries are written at correct place
1465 */
1466 ckpt_ver = cur_cp_version(ckpt);
1467 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1468
1469 /* write cached NAT/SIT entries to NAT/SIT area */
1470 flush_nat_entries(sbi, cpc);
1471 flush_sit_entries(sbi, cpc);
1472
1473 /* unlock all the fs_lock[] in do_checkpoint() */
1474 err = do_checkpoint(sbi, cpc);
1475 if (err)
1476 release_discard_addrs(sbi);
1477 else
1478 clear_prefree_segments(sbi, cpc);
1479
1480 unblock_operations(sbi);
1481 stat_inc_cp_count(sbi->stat_info);
1482
1483 if (cpc->reason & CP_RECOVERY)
1484 f2fs_msg(sbi->sb, KERN_NOTICE,
1485 "checkpoint: version = %llx", ckpt_ver);
1486
1487 /* do checkpoint periodically */
1488 f2fs_update_time(sbi, CP_TIME);
1489 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1490out:
1491 mutex_unlock(&sbi->cp_mutex);
1492 return err;
1493}
1494
1495void init_ino_entry_info(struct f2fs_sb_info *sbi)
1496{
1497 int i;
1498
1499 for (i = 0; i < MAX_INO_ENTRY; i++) {
1500 struct inode_management *im = &sbi->im[i];
1501
1502 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1503 spin_lock_init(&im->ino_lock);
1504 INIT_LIST_HEAD(&im->ino_list);
1505 im->ino_num = 0;
1506 }
1507
1508 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1509 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1510 F2FS_ORPHANS_PER_BLOCK;
1511}
1512
1513int __init create_checkpoint_caches(void)
1514{
1515 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1516 sizeof(struct ino_entry));
1517 if (!ino_entry_slab)
1518 return -ENOMEM;
1519 inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1520 sizeof(struct inode_entry));
1521 if (!inode_entry_slab) {
1522 kmem_cache_destroy(ino_entry_slab);
1523 return -ENOMEM;
1524 }
1525 return 0;
1526}
1527
1528void destroy_checkpoint_caches(void)
1529{
1530 kmem_cache_destroy(ino_entry_slab);
1531 kmem_cache_destroy(inode_entry_slab);
1532}