f2fs: add help function META_MAPPING
[platform/kernel/linux-starfive.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/events/f2fs.h>
24
25 static struct kmem_cache *orphan_entry_slab;
26 static struct kmem_cache *inode_entry_slab;
27
28 /*
29  * We guarantee no failure on the returned page.
30  */
31 struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
32 {
33         struct address_space *mapping = META_MAPPING(sbi);
34         struct page *page = NULL;
35 repeat:
36         page = grab_cache_page(mapping, index);
37         if (!page) {
38                 cond_resched();
39                 goto repeat;
40         }
41
42         /* We wait writeback only inside grab_meta_page() */
43         wait_on_page_writeback(page);
44         SetPageUptodate(page);
45         return page;
46 }
47
48 /*
49  * We guarantee no failure on the returned page.
50  */
51 struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
52 {
53         struct address_space *mapping = META_MAPPING(sbi);
54         struct page *page;
55 repeat:
56         page = grab_cache_page(mapping, index);
57         if (!page) {
58                 cond_resched();
59                 goto repeat;
60         }
61         if (PageUptodate(page))
62                 goto out;
63
64         if (f2fs_submit_page_bio(sbi, page, index,
65                                 READ_SYNC | REQ_META | REQ_PRIO))
66                 goto repeat;
67
68         lock_page(page);
69         if (unlikely(page->mapping != mapping)) {
70                 f2fs_put_page(page, 1);
71                 goto repeat;
72         }
73 out:
74         mark_page_accessed(page);
75         return page;
76 }
77
78 static int f2fs_write_meta_page(struct page *page,
79                                 struct writeback_control *wbc)
80 {
81         struct inode *inode = page->mapping->host;
82         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
83
84         /* Should not write any meta pages, if any IO error was occurred */
85         if (unlikely(sbi->por_doing ||
86                         is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG)))
87                 goto redirty_out;
88
89         if (wbc->for_reclaim)
90                 goto redirty_out;
91
92         wait_on_page_writeback(page);
93
94         write_meta_page(sbi, page);
95         dec_page_count(sbi, F2FS_DIRTY_META);
96         unlock_page(page);
97         return 0;
98
99 redirty_out:
100         dec_page_count(sbi, F2FS_DIRTY_META);
101         wbc->pages_skipped++;
102         set_page_dirty(page);
103         return AOP_WRITEPAGE_ACTIVATE;
104 }
105
106 static int f2fs_write_meta_pages(struct address_space *mapping,
107                                 struct writeback_control *wbc)
108 {
109         struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
110         int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
111         long written;
112
113         if (wbc->for_kupdate)
114                 return 0;
115
116         /* collect a number of dirty meta pages and write together */
117         if (get_pages(sbi, F2FS_DIRTY_META) < nrpages)
118                 return 0;
119
120         /* if mounting is failed, skip writing node pages */
121         mutex_lock(&sbi->cp_mutex);
122         written = sync_meta_pages(sbi, META, nrpages);
123         mutex_unlock(&sbi->cp_mutex);
124         wbc->nr_to_write -= written;
125         return 0;
126 }
127
128 long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
129                                                 long nr_to_write)
130 {
131         struct address_space *mapping = META_MAPPING(sbi);
132         pgoff_t index = 0, end = LONG_MAX;
133         struct pagevec pvec;
134         long nwritten = 0;
135         struct writeback_control wbc = {
136                 .for_reclaim = 0,
137         };
138
139         pagevec_init(&pvec, 0);
140
141         while (index <= end) {
142                 int i, nr_pages;
143                 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
144                                 PAGECACHE_TAG_DIRTY,
145                                 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
146                 if (unlikely(nr_pages == 0))
147                         break;
148
149                 for (i = 0; i < nr_pages; i++) {
150                         struct page *page = pvec.pages[i];
151                         lock_page(page);
152                         f2fs_bug_on(page->mapping != mapping);
153                         f2fs_bug_on(!PageDirty(page));
154                         clear_page_dirty_for_io(page);
155                         if (f2fs_write_meta_page(page, &wbc)) {
156                                 unlock_page(page);
157                                 break;
158                         }
159                         nwritten++;
160                         if (unlikely(nwritten >= nr_to_write))
161                                 break;
162                 }
163                 pagevec_release(&pvec);
164                 cond_resched();
165         }
166
167         if (nwritten)
168                 f2fs_submit_merged_bio(sbi, type, WRITE);
169
170         return nwritten;
171 }
172
173 static int f2fs_set_meta_page_dirty(struct page *page)
174 {
175         struct address_space *mapping = page->mapping;
176         struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
177
178         trace_f2fs_set_page_dirty(page, META);
179
180         SetPageUptodate(page);
181         if (!PageDirty(page)) {
182                 __set_page_dirty_nobuffers(page);
183                 inc_page_count(sbi, F2FS_DIRTY_META);
184                 return 1;
185         }
186         return 0;
187 }
188
189 const struct address_space_operations f2fs_meta_aops = {
190         .writepage      = f2fs_write_meta_page,
191         .writepages     = f2fs_write_meta_pages,
192         .set_page_dirty = f2fs_set_meta_page_dirty,
193 };
194
195 int acquire_orphan_inode(struct f2fs_sb_info *sbi)
196 {
197         int err = 0;
198
199         spin_lock(&sbi->orphan_inode_lock);
200         if (unlikely(sbi->n_orphans >= sbi->max_orphans))
201                 err = -ENOSPC;
202         else
203                 sbi->n_orphans++;
204         spin_unlock(&sbi->orphan_inode_lock);
205
206         return err;
207 }
208
209 void release_orphan_inode(struct f2fs_sb_info *sbi)
210 {
211         spin_lock(&sbi->orphan_inode_lock);
212         f2fs_bug_on(sbi->n_orphans == 0);
213         sbi->n_orphans--;
214         spin_unlock(&sbi->orphan_inode_lock);
215 }
216
217 void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
218 {
219         struct list_head *head, *this;
220         struct orphan_inode_entry *new = NULL, *orphan = NULL;
221
222         new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC);
223         new->ino = ino;
224
225         spin_lock(&sbi->orphan_inode_lock);
226         head = &sbi->orphan_inode_list;
227         list_for_each(this, head) {
228                 orphan = list_entry(this, struct orphan_inode_entry, list);
229                 if (orphan->ino == ino) {
230                         spin_unlock(&sbi->orphan_inode_lock);
231                         kmem_cache_free(orphan_entry_slab, new);
232                         return;
233                 }
234
235                 if (orphan->ino > ino)
236                         break;
237                 orphan = NULL;
238         }
239
240         /* add new_oentry into list which is sorted by inode number */
241         if (orphan)
242                 list_add(&new->list, this->prev);
243         else
244                 list_add_tail(&new->list, head);
245         spin_unlock(&sbi->orphan_inode_lock);
246 }
247
248 void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
249 {
250         struct list_head *head;
251         struct orphan_inode_entry *orphan;
252
253         spin_lock(&sbi->orphan_inode_lock);
254         head = &sbi->orphan_inode_list;
255         list_for_each_entry(orphan, head, list) {
256                 if (orphan->ino == ino) {
257                         list_del(&orphan->list);
258                         kmem_cache_free(orphan_entry_slab, orphan);
259                         f2fs_bug_on(sbi->n_orphans == 0);
260                         sbi->n_orphans--;
261                         break;
262                 }
263         }
264         spin_unlock(&sbi->orphan_inode_lock);
265 }
266
267 static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
268 {
269         struct inode *inode = f2fs_iget(sbi->sb, ino);
270         f2fs_bug_on(IS_ERR(inode));
271         clear_nlink(inode);
272
273         /* truncate all the data during iput */
274         iput(inode);
275 }
276
277 void recover_orphan_inodes(struct f2fs_sb_info *sbi)
278 {
279         block_t start_blk, orphan_blkaddr, i, j;
280
281         if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
282                 return;
283
284         sbi->por_doing = true;
285         start_blk = __start_cp_addr(sbi) + 1;
286         orphan_blkaddr = __start_sum_addr(sbi) - 1;
287
288         for (i = 0; i < orphan_blkaddr; i++) {
289                 struct page *page = get_meta_page(sbi, start_blk + i);
290                 struct f2fs_orphan_block *orphan_blk;
291
292                 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
293                 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
294                         nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
295                         recover_orphan_inode(sbi, ino);
296                 }
297                 f2fs_put_page(page, 1);
298         }
299         /* clear Orphan Flag */
300         clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
301         sbi->por_doing = false;
302         return;
303 }
304
305 static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
306 {
307         struct list_head *head;
308         struct f2fs_orphan_block *orphan_blk = NULL;
309         unsigned int nentries = 0;
310         unsigned short index;
311         unsigned short orphan_blocks = (unsigned short)((sbi->n_orphans +
312                 (F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
313         struct page *page = NULL;
314         struct page *pages[orphan_blocks];
315         struct orphan_inode_entry *orphan = NULL;
316
317         for (index = 0; index < orphan_blocks; index++)
318                 pages[index] = grab_meta_page(sbi, start_blk + index);
319
320         index = 1;
321         spin_lock(&sbi->orphan_inode_lock);
322         head = &sbi->orphan_inode_list;
323
324         /* loop for each orphan inode entry and write them in Jornal block */
325         list_for_each_entry(orphan, head, list) {
326                 if (!page) {
327                         page = pages[index - 1];
328                         orphan_blk =
329                                 (struct f2fs_orphan_block *)page_address(page);
330                         memset(orphan_blk, 0, sizeof(*orphan_blk));
331                 }
332
333                 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
334
335                 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
336                         /*
337                          * an orphan block is full of 1020 entries,
338                          * then we need to flush current orphan blocks
339                          * and bring another one in memory
340                          */
341                         orphan_blk->blk_addr = cpu_to_le16(index);
342                         orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
343                         orphan_blk->entry_count = cpu_to_le32(nentries);
344                         set_page_dirty(page);
345                         f2fs_put_page(page, 1);
346                         index++;
347                         nentries = 0;
348                         page = NULL;
349                 }
350         }
351
352         if (page) {
353                 orphan_blk->blk_addr = cpu_to_le16(index);
354                 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
355                 orphan_blk->entry_count = cpu_to_le32(nentries);
356                 set_page_dirty(page);
357                 f2fs_put_page(page, 1);
358         }
359
360         spin_unlock(&sbi->orphan_inode_lock);
361 }
362
363 static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
364                                 block_t cp_addr, unsigned long long *version)
365 {
366         struct page *cp_page_1, *cp_page_2 = NULL;
367         unsigned long blk_size = sbi->blocksize;
368         struct f2fs_checkpoint *cp_block;
369         unsigned long long cur_version = 0, pre_version = 0;
370         size_t crc_offset;
371         __u32 crc = 0;
372
373         /* Read the 1st cp block in this CP pack */
374         cp_page_1 = get_meta_page(sbi, cp_addr);
375
376         /* get the version number */
377         cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
378         crc_offset = le32_to_cpu(cp_block->checksum_offset);
379         if (crc_offset >= blk_size)
380                 goto invalid_cp1;
381
382         crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
383         if (!f2fs_crc_valid(crc, cp_block, crc_offset))
384                 goto invalid_cp1;
385
386         pre_version = cur_cp_version(cp_block);
387
388         /* Read the 2nd cp block in this CP pack */
389         cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
390         cp_page_2 = get_meta_page(sbi, cp_addr);
391
392         cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
393         crc_offset = le32_to_cpu(cp_block->checksum_offset);
394         if (crc_offset >= blk_size)
395                 goto invalid_cp2;
396
397         crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
398         if (!f2fs_crc_valid(crc, cp_block, crc_offset))
399                 goto invalid_cp2;
400
401         cur_version = cur_cp_version(cp_block);
402
403         if (cur_version == pre_version) {
404                 *version = cur_version;
405                 f2fs_put_page(cp_page_2, 1);
406                 return cp_page_1;
407         }
408 invalid_cp2:
409         f2fs_put_page(cp_page_2, 1);
410 invalid_cp1:
411         f2fs_put_page(cp_page_1, 1);
412         return NULL;
413 }
414
415 int get_valid_checkpoint(struct f2fs_sb_info *sbi)
416 {
417         struct f2fs_checkpoint *cp_block;
418         struct f2fs_super_block *fsb = sbi->raw_super;
419         struct page *cp1, *cp2, *cur_page;
420         unsigned long blk_size = sbi->blocksize;
421         unsigned long long cp1_version = 0, cp2_version = 0;
422         unsigned long long cp_start_blk_no;
423
424         sbi->ckpt = kzalloc(blk_size, GFP_KERNEL);
425         if (!sbi->ckpt)
426                 return -ENOMEM;
427         /*
428          * Finding out valid cp block involves read both
429          * sets( cp pack1 and cp pack 2)
430          */
431         cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
432         cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
433
434         /* The second checkpoint pack should start at the next segment */
435         cp_start_blk_no += ((unsigned long long)1) <<
436                                 le32_to_cpu(fsb->log_blocks_per_seg);
437         cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
438
439         if (cp1 && cp2) {
440                 if (ver_after(cp2_version, cp1_version))
441                         cur_page = cp2;
442                 else
443                         cur_page = cp1;
444         } else if (cp1) {
445                 cur_page = cp1;
446         } else if (cp2) {
447                 cur_page = cp2;
448         } else {
449                 goto fail_no_cp;
450         }
451
452         cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
453         memcpy(sbi->ckpt, cp_block, blk_size);
454
455         f2fs_put_page(cp1, 1);
456         f2fs_put_page(cp2, 1);
457         return 0;
458
459 fail_no_cp:
460         kfree(sbi->ckpt);
461         return -EINVAL;
462 }
463
464 static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new)
465 {
466         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
467         struct list_head *head = &sbi->dir_inode_list;
468         struct list_head *this;
469
470         list_for_each(this, head) {
471                 struct dir_inode_entry *entry;
472                 entry = list_entry(this, struct dir_inode_entry, list);
473                 if (unlikely(entry->inode == inode))
474                         return -EEXIST;
475         }
476         list_add_tail(&new->list, head);
477         stat_inc_dirty_dir(sbi);
478         return 0;
479 }
480
481 void set_dirty_dir_page(struct inode *inode, struct page *page)
482 {
483         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
484         struct dir_inode_entry *new;
485
486         if (!S_ISDIR(inode->i_mode))
487                 return;
488
489         new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
490         new->inode = inode;
491         INIT_LIST_HEAD(&new->list);
492
493         spin_lock(&sbi->dir_inode_lock);
494         if (__add_dirty_inode(inode, new))
495                 kmem_cache_free(inode_entry_slab, new);
496
497         inc_page_count(sbi, F2FS_DIRTY_DENTS);
498         inode_inc_dirty_dents(inode);
499         SetPagePrivate(page);
500         spin_unlock(&sbi->dir_inode_lock);
501 }
502
503 void add_dirty_dir_inode(struct inode *inode)
504 {
505         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
506         struct dir_inode_entry *new =
507                         f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
508
509         new->inode = inode;
510         INIT_LIST_HEAD(&new->list);
511
512         spin_lock(&sbi->dir_inode_lock);
513         if (__add_dirty_inode(inode, new))
514                 kmem_cache_free(inode_entry_slab, new);
515         spin_unlock(&sbi->dir_inode_lock);
516 }
517
518 void remove_dirty_dir_inode(struct inode *inode)
519 {
520         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
521
522         struct list_head *this, *head;
523
524         if (!S_ISDIR(inode->i_mode))
525                 return;
526
527         spin_lock(&sbi->dir_inode_lock);
528         if (atomic_read(&F2FS_I(inode)->dirty_dents)) {
529                 spin_unlock(&sbi->dir_inode_lock);
530                 return;
531         }
532
533         head = &sbi->dir_inode_list;
534         list_for_each(this, head) {
535                 struct dir_inode_entry *entry;
536                 entry = list_entry(this, struct dir_inode_entry, list);
537                 if (entry->inode == inode) {
538                         list_del(&entry->list);
539                         kmem_cache_free(inode_entry_slab, entry);
540                         stat_dec_dirty_dir(sbi);
541                         break;
542                 }
543         }
544         spin_unlock(&sbi->dir_inode_lock);
545
546         /* Only from the recovery routine */
547         if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
548                 clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
549                 iput(inode);
550         }
551 }
552
553 struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino)
554 {
555
556         struct list_head *this, *head;
557         struct inode *inode = NULL;
558
559         spin_lock(&sbi->dir_inode_lock);
560
561         head = &sbi->dir_inode_list;
562         list_for_each(this, head) {
563                 struct dir_inode_entry *entry;
564                 entry = list_entry(this, struct dir_inode_entry, list);
565                 if (entry->inode->i_ino == ino) {
566                         inode = entry->inode;
567                         break;
568                 }
569         }
570         spin_unlock(&sbi->dir_inode_lock);
571         return inode;
572 }
573
574 void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
575 {
576         struct list_head *head;
577         struct dir_inode_entry *entry;
578         struct inode *inode;
579 retry:
580         spin_lock(&sbi->dir_inode_lock);
581
582         head = &sbi->dir_inode_list;
583         if (list_empty(head)) {
584                 spin_unlock(&sbi->dir_inode_lock);
585                 return;
586         }
587         entry = list_entry(head->next, struct dir_inode_entry, list);
588         inode = igrab(entry->inode);
589         spin_unlock(&sbi->dir_inode_lock);
590         if (inode) {
591                 filemap_flush(inode->i_mapping);
592                 iput(inode);
593         } else {
594                 /*
595                  * We should submit bio, since it exists several
596                  * wribacking dentry pages in the freeing inode.
597                  */
598                 f2fs_submit_merged_bio(sbi, DATA, WRITE);
599         }
600         goto retry;
601 }
602
603 /*
604  * Freeze all the FS-operations for checkpoint.
605  */
606 static void block_operations(struct f2fs_sb_info *sbi)
607 {
608         struct writeback_control wbc = {
609                 .sync_mode = WB_SYNC_ALL,
610                 .nr_to_write = LONG_MAX,
611                 .for_reclaim = 0,
612         };
613         struct blk_plug plug;
614
615         blk_start_plug(&plug);
616
617 retry_flush_dents:
618         f2fs_lock_all(sbi);
619         /* write all the dirty dentry pages */
620         if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
621                 f2fs_unlock_all(sbi);
622                 sync_dirty_dir_inodes(sbi);
623                 goto retry_flush_dents;
624         }
625
626         /*
627          * POR: we should ensure that there is no dirty node pages
628          * until finishing nat/sit flush.
629          */
630 retry_flush_nodes:
631         mutex_lock(&sbi->node_write);
632
633         if (get_pages(sbi, F2FS_DIRTY_NODES)) {
634                 mutex_unlock(&sbi->node_write);
635                 sync_node_pages(sbi, 0, &wbc);
636                 goto retry_flush_nodes;
637         }
638         blk_finish_plug(&plug);
639 }
640
641 static void unblock_operations(struct f2fs_sb_info *sbi)
642 {
643         mutex_unlock(&sbi->node_write);
644         f2fs_unlock_all(sbi);
645 }
646
647 static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
648 {
649         DEFINE_WAIT(wait);
650
651         for (;;) {
652                 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
653
654                 if (!get_pages(sbi, F2FS_WRITEBACK))
655                         break;
656
657                 io_schedule();
658         }
659         finish_wait(&sbi->cp_wait, &wait);
660 }
661
662 static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
663 {
664         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
665         nid_t last_nid = 0;
666         block_t start_blk;
667         struct page *cp_page;
668         unsigned int data_sum_blocks, orphan_blocks;
669         __u32 crc32 = 0;
670         void *kaddr;
671         int i;
672
673         /* Flush all the NAT/SIT pages */
674         while (get_pages(sbi, F2FS_DIRTY_META))
675                 sync_meta_pages(sbi, META, LONG_MAX);
676
677         next_free_nid(sbi, &last_nid);
678
679         /*
680          * modify checkpoint
681          * version number is already updated
682          */
683         ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
684         ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
685         ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
686         for (i = 0; i < 3; i++) {
687                 ckpt->cur_node_segno[i] =
688                         cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
689                 ckpt->cur_node_blkoff[i] =
690                         cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
691                 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
692                                 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
693         }
694         for (i = 0; i < 3; i++) {
695                 ckpt->cur_data_segno[i] =
696                         cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
697                 ckpt->cur_data_blkoff[i] =
698                         cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
699                 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
700                                 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
701         }
702
703         ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
704         ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
705         ckpt->next_free_nid = cpu_to_le32(last_nid);
706
707         /* 2 cp  + n data seg summary + orphan inode blocks */
708         data_sum_blocks = npages_for_summary_flush(sbi);
709         if (data_sum_blocks < 3)
710                 set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
711         else
712                 clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
713
714         orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
715                                         / F2FS_ORPHANS_PER_BLOCK;
716         ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks);
717
718         if (is_umount) {
719                 set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
720                 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
721                         data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE);
722         } else {
723                 clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
724                 ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
725                         data_sum_blocks + orphan_blocks);
726         }
727
728         if (sbi->n_orphans)
729                 set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
730         else
731                 clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
732
733         /* update SIT/NAT bitmap */
734         get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
735         get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
736
737         crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
738         *((__le32 *)((unsigned char *)ckpt +
739                                 le32_to_cpu(ckpt->checksum_offset)))
740                                 = cpu_to_le32(crc32);
741
742         start_blk = __start_cp_addr(sbi);
743
744         /* write out checkpoint buffer at block 0 */
745         cp_page = grab_meta_page(sbi, start_blk++);
746         kaddr = page_address(cp_page);
747         memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
748         set_page_dirty(cp_page);
749         f2fs_put_page(cp_page, 1);
750
751         if (sbi->n_orphans) {
752                 write_orphan_inodes(sbi, start_blk);
753                 start_blk += orphan_blocks;
754         }
755
756         write_data_summaries(sbi, start_blk);
757         start_blk += data_sum_blocks;
758         if (is_umount) {
759                 write_node_summaries(sbi, start_blk);
760                 start_blk += NR_CURSEG_NODE_TYPE;
761         }
762
763         /* writeout checkpoint block */
764         cp_page = grab_meta_page(sbi, start_blk);
765         kaddr = page_address(cp_page);
766         memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
767         set_page_dirty(cp_page);
768         f2fs_put_page(cp_page, 1);
769
770         /* wait for previous submitted node/meta pages writeback */
771         wait_on_all_pages_writeback(sbi);
772
773         filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX);
774         filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
775
776         /* update user_block_counts */
777         sbi->last_valid_block_count = sbi->total_valid_block_count;
778         sbi->alloc_valid_block_count = 0;
779
780         /* Here, we only have one bio having CP pack */
781         sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
782
783         if (unlikely(!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG))) {
784                 clear_prefree_segments(sbi);
785                 F2FS_RESET_SB_DIRT(sbi);
786         }
787 }
788
789 /*
790  * We guarantee that this checkpoint procedure should not fail.
791  */
792 void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
793 {
794         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
795         unsigned long long ckpt_ver;
796
797         trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops");
798
799         mutex_lock(&sbi->cp_mutex);
800         block_operations(sbi);
801
802         trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops");
803
804         f2fs_submit_merged_bio(sbi, DATA, WRITE);
805         f2fs_submit_merged_bio(sbi, NODE, WRITE);
806         f2fs_submit_merged_bio(sbi, META, WRITE);
807
808         /*
809          * update checkpoint pack index
810          * Increase the version number so that
811          * SIT entries and seg summaries are written at correct place
812          */
813         ckpt_ver = cur_cp_version(ckpt);
814         ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
815
816         /* write cached NAT/SIT entries to NAT/SIT area */
817         flush_nat_entries(sbi);
818         flush_sit_entries(sbi);
819
820         /* unlock all the fs_lock[] in do_checkpoint() */
821         do_checkpoint(sbi, is_umount);
822
823         unblock_operations(sbi);
824         mutex_unlock(&sbi->cp_mutex);
825
826         trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint");
827 }
828
829 void init_orphan_info(struct f2fs_sb_info *sbi)
830 {
831         spin_lock_init(&sbi->orphan_inode_lock);
832         INIT_LIST_HEAD(&sbi->orphan_inode_list);
833         sbi->n_orphans = 0;
834         /*
835          * considering 512 blocks in a segment 8 blocks are needed for cp
836          * and log segment summaries. Remaining blocks are used to keep
837          * orphan entries with the limitation one reserved segment
838          * for cp pack we can have max 1020*504 orphan entries
839          */
840         sbi->max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE)
841                                 * F2FS_ORPHANS_PER_BLOCK;
842 }
843
844 int __init create_checkpoint_caches(void)
845 {
846         orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
847                         sizeof(struct orphan_inode_entry), NULL);
848         if (!orphan_entry_slab)
849                 return -ENOMEM;
850         inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
851                         sizeof(struct dir_inode_entry), NULL);
852         if (!inode_entry_slab) {
853                 kmem_cache_destroy(orphan_entry_slab);
854                 return -ENOMEM;
855         }
856         return 0;
857 }
858
859 void destroy_checkpoint_caches(void)
860 {
861         kmem_cache_destroy(orphan_entry_slab);
862         kmem_cache_destroy(inode_entry_slab);
863 }