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