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