arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / f2fs / checkpoint.c
blob0b061bbf1639a6914d2935f9f3ff591fddce35ee
1 /*
2 * fs/f2fs/checkpoint.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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>
20 #include "f2fs.h"
21 #include "node.h"
22 #include "segment.h"
23 #include "trace.h"
24 #include <trace/events/f2fs.h>
26 static struct kmem_cache *ino_entry_slab;
27 struct kmem_cache *inode_entry_slab;
29 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
31 set_ckpt_flags(sbi, CP_ERROR_FLAG);
32 sbi->sb->s_flags |= MS_RDONLY;
33 if (!end_io)
34 f2fs_flush_merged_bios(sbi);
38 * We guarantee no failure on the returned page.
40 struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
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;
50 f2fs_wait_on_page_writeback(page, META, true);
51 if (!PageUptodate(page))
52 SetPageUptodate(page);
53 return page;
57 * We guarantee no failure on the returned page.
59 static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
60 bool is_meta)
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 = READ_SYNC | REQ_META | REQ_PRIO,
69 .old_blkaddr = index,
70 .new_blkaddr = index,
71 .encrypted_page = NULL,
72 .is_meta = is_meta,
75 if (unlikely(!is_meta))
76 fio.op_flags &= ~REQ_META;
77 repeat:
78 page = f2fs_grab_cache_page(mapping, index, false);
79 if (!page) {
80 cond_resched();
81 goto repeat;
83 if (PageUptodate(page))
84 goto out;
86 fio.page = page;
88 if (f2fs_submit_page_bio(&fio)) {
89 memset(page_address(page), 0, PAGE_SIZE);
90 f2fs_stop_checkpoint(sbi, false);
91 f2fs_bug_on(sbi, 1);
92 return page;
95 lock_page(page);
96 if (unlikely(page->mapping != mapping)) {
97 f2fs_put_page(page, 1);
98 goto repeat;
102 * if there is any IO error when accessing device, make our filesystem
103 * readonly and make sure do not write checkpoint with non-uptodate
104 * meta page.
106 if (unlikely(!PageUptodate(page)))
107 f2fs_stop_checkpoint(sbi, false);
108 out:
109 return page;
112 struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
114 return __get_meta_page(sbi, index, true);
117 /* for POR only */
118 struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
120 return __get_meta_page(sbi, index, false);
123 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
124 block_t blkaddr, int type)
126 switch (type) {
127 case META_NAT:
128 break;
129 case META_SIT:
130 if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
131 return false;
132 break;
133 case META_SSA:
134 if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
135 blkaddr < SM_I(sbi)->ssa_blkaddr))
136 return false;
137 break;
138 case META_CP:
139 if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
140 blkaddr < __start_cp_addr(sbi)))
141 return false;
142 break;
143 case META_POR:
144 case DATA_GENERIC:
145 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
146 blkaddr < MAIN_BLKADDR(sbi))) {
147 if (type == DATA_GENERIC) {
148 f2fs_msg(sbi->sb, KERN_WARNING,
149 "access invalid blkaddr:%u", blkaddr);
150 WARN_ON(1);
152 return false;
154 break;
155 case META_GENERIC:
156 if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
157 blkaddr >= MAIN_BLKADDR(sbi)))
158 return false;
159 break;
160 default:
161 BUG();
164 return true;
168 * Readahead CP/NAT/SIT/SSA pages
170 int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
171 int type, bool sync)
173 struct page *page;
174 block_t blkno = start;
175 struct f2fs_io_info fio = {
176 .sbi = sbi,
177 .type = META,
178 .op = REQ_OP_READ,
179 .op_flags = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : REQ_RAHEAD,
180 .encrypted_page = NULL,
181 .is_meta = (type != META_POR),
183 struct blk_plug plug;
185 if (unlikely(type == META_POR))
186 fio.op_flags &= ~REQ_META;
188 blk_start_plug(&plug);
189 for (; nrpages-- > 0; blkno++) {
191 if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
192 goto out;
194 switch (type) {
195 case META_NAT:
196 if (unlikely(blkno >=
197 NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
198 blkno = 0;
199 /* get nat block addr */
200 fio.new_blkaddr = current_nat_addr(sbi,
201 blkno * NAT_ENTRY_PER_BLOCK);
202 break;
203 case META_SIT:
204 /* get sit block addr */
205 fio.new_blkaddr = current_sit_addr(sbi,
206 blkno * SIT_ENTRY_PER_BLOCK);
207 break;
208 case META_SSA:
209 case META_CP:
210 case META_POR:
211 fio.new_blkaddr = blkno;
212 break;
213 default:
214 BUG();
217 page = f2fs_grab_cache_page(META_MAPPING(sbi),
218 fio.new_blkaddr, false);
219 if (!page)
220 continue;
221 if (PageUptodate(page)) {
222 f2fs_put_page(page, 1);
223 continue;
226 fio.page = page;
227 fio.old_blkaddr = fio.new_blkaddr;
228 f2fs_submit_page_mbio(&fio);
229 f2fs_put_page(page, 0);
231 out:
232 f2fs_submit_merged_bio(sbi, META, READ);
233 blk_finish_plug(&plug);
234 return blkno - start;
237 void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
239 struct page *page;
240 bool readahead = false;
242 page = find_get_page(META_MAPPING(sbi), index);
243 if (!page || !PageUptodate(page))
244 readahead = true;
245 f2fs_put_page(page, 0);
247 if (readahead)
248 ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true);
251 static int f2fs_write_meta_page(struct page *page,
252 struct writeback_control *wbc)
254 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
256 trace_f2fs_writepage(page, META);
258 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
259 goto redirty_out;
260 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
261 goto redirty_out;
262 if (unlikely(f2fs_cp_error(sbi)))
263 goto redirty_out;
265 write_meta_page(sbi, page);
266 dec_page_count(sbi, F2FS_DIRTY_META);
268 if (wbc->for_reclaim)
269 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE);
271 unlock_page(page);
273 if (unlikely(f2fs_cp_error(sbi)))
274 f2fs_submit_merged_bio(sbi, META, WRITE);
276 return 0;
278 redirty_out:
279 redirty_page_for_writepage(wbc, page);
280 return AOP_WRITEPAGE_ACTIVATE;
283 static int f2fs_write_meta_pages(struct address_space *mapping,
284 struct writeback_control *wbc)
286 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
287 long diff, written;
289 /* collect a number of dirty meta pages and write together */
290 if (wbc->for_kupdate ||
291 get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
292 goto skip_write;
294 trace_f2fs_writepages(mapping->host, wbc, META);
296 /* if mounting is failed, skip writing node pages */
297 mutex_lock(&sbi->cp_mutex);
298 diff = nr_pages_to_write(sbi, META, wbc);
299 written = sync_meta_pages(sbi, META, wbc->nr_to_write);
300 mutex_unlock(&sbi->cp_mutex);
301 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
302 return 0;
304 skip_write:
305 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
306 trace_f2fs_writepages(mapping->host, wbc, META);
307 return 0;
310 long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
311 long nr_to_write)
313 struct address_space *mapping = META_MAPPING(sbi);
314 pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
315 struct pagevec pvec;
316 long nwritten = 0;
317 struct writeback_control wbc = {
318 .for_reclaim = 0,
320 struct blk_plug plug;
322 pagevec_init(&pvec, 0);
324 blk_start_plug(&plug);
326 while (index <= end) {
327 int i, nr_pages;
328 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
329 PAGECACHE_TAG_DIRTY,
330 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
331 if (unlikely(nr_pages == 0))
332 break;
334 for (i = 0; i < nr_pages; i++) {
335 struct page *page = pvec.pages[i];
337 if (prev == ULONG_MAX)
338 prev = page->index - 1;
339 if (nr_to_write != LONG_MAX && page->index != prev + 1) {
340 pagevec_release(&pvec);
341 goto stop;
344 lock_page(page);
346 if (unlikely(page->mapping != mapping)) {
347 continue_unlock:
348 unlock_page(page);
349 continue;
351 if (!PageDirty(page)) {
352 /* someone wrote it for us */
353 goto continue_unlock;
356 f2fs_wait_on_page_writeback(page, META, true);
358 BUG_ON(PageWriteback(page));
359 if (!clear_page_dirty_for_io(page))
360 goto continue_unlock;
362 if (mapping->a_ops->writepage(page, &wbc)) {
363 unlock_page(page);
364 break;
366 nwritten++;
367 prev = page->index;
368 if (unlikely(nwritten >= nr_to_write))
369 break;
371 pagevec_release(&pvec);
372 cond_resched();
374 stop:
375 if (nwritten)
376 f2fs_submit_merged_bio(sbi, type, WRITE);
378 blk_finish_plug(&plug);
380 return nwritten;
383 static int f2fs_set_meta_page_dirty(struct page *page)
385 trace_f2fs_set_page_dirty(page, META);
387 if (!PageUptodate(page))
388 SetPageUptodate(page);
389 if (!PageDirty(page)) {
390 f2fs_set_page_dirty_nobuffers(page);
391 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
392 SetPagePrivate(page);
393 f2fs_trace_pid(page);
394 return 1;
396 return 0;
399 const struct address_space_operations f2fs_meta_aops = {
400 .writepage = f2fs_write_meta_page,
401 .writepages = f2fs_write_meta_pages,
402 .set_page_dirty = f2fs_set_meta_page_dirty,
403 .invalidatepage = f2fs_invalidate_page,
404 .releasepage = f2fs_release_page,
405 #ifdef CONFIG_MIGRATION
406 .migratepage = f2fs_migrate_page,
407 #endif
410 static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
412 struct inode_management *im = &sbi->im[type];
413 struct ino_entry *e, *tmp;
415 tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
416 retry:
417 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
419 spin_lock(&im->ino_lock);
420 e = radix_tree_lookup(&im->ino_root, ino);
421 if (!e) {
422 e = tmp;
423 if (radix_tree_insert(&im->ino_root, ino, e)) {
424 spin_unlock(&im->ino_lock);
425 radix_tree_preload_end();
426 goto retry;
428 memset(e, 0, sizeof(struct ino_entry));
429 e->ino = ino;
431 list_add_tail(&e->list, &im->ino_list);
432 if (type != ORPHAN_INO)
433 im->ino_num++;
435 spin_unlock(&im->ino_lock);
436 radix_tree_preload_end();
438 if (e != tmp)
439 kmem_cache_free(ino_entry_slab, tmp);
442 static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
444 struct inode_management *im = &sbi->im[type];
445 struct ino_entry *e;
447 spin_lock(&im->ino_lock);
448 e = radix_tree_lookup(&im->ino_root, ino);
449 if (e) {
450 list_del(&e->list);
451 radix_tree_delete(&im->ino_root, ino);
452 im->ino_num--;
453 spin_unlock(&im->ino_lock);
454 kmem_cache_free(ino_entry_slab, e);
455 return;
457 spin_unlock(&im->ino_lock);
460 void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
462 /* add new dirty ino entry into list */
463 __add_ino_entry(sbi, ino, type);
466 void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
468 /* remove dirty ino entry from list */
469 __remove_ino_entry(sbi, ino, type);
472 /* mode should be APPEND_INO or UPDATE_INO */
473 bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
475 struct inode_management *im = &sbi->im[mode];
476 struct ino_entry *e;
478 spin_lock(&im->ino_lock);
479 e = radix_tree_lookup(&im->ino_root, ino);
480 spin_unlock(&im->ino_lock);
481 return e ? true : false;
484 void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
486 struct ino_entry *e, *tmp;
487 int i;
489 for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) {
490 struct inode_management *im = &sbi->im[i];
492 spin_lock(&im->ino_lock);
493 list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
494 list_del(&e->list);
495 radix_tree_delete(&im->ino_root, e->ino);
496 kmem_cache_free(ino_entry_slab, e);
497 im->ino_num--;
499 spin_unlock(&im->ino_lock);
503 int acquire_orphan_inode(struct f2fs_sb_info *sbi)
505 struct inode_management *im = &sbi->im[ORPHAN_INO];
506 int err = 0;
508 spin_lock(&im->ino_lock);
510 #ifdef CONFIG_F2FS_FAULT_INJECTION
511 if (time_to_inject(sbi, FAULT_ORPHAN)) {
512 spin_unlock(&im->ino_lock);
513 return -ENOSPC;
515 #endif
516 if (unlikely(im->ino_num >= sbi->max_orphans))
517 err = -ENOSPC;
518 else
519 im->ino_num++;
520 spin_unlock(&im->ino_lock);
522 return err;
525 void release_orphan_inode(struct f2fs_sb_info *sbi)
527 struct inode_management *im = &sbi->im[ORPHAN_INO];
529 spin_lock(&im->ino_lock);
530 f2fs_bug_on(sbi, im->ino_num == 0);
531 im->ino_num--;
532 spin_unlock(&im->ino_lock);
535 void add_orphan_inode(struct inode *inode)
537 /* add new orphan ino entry into list */
538 __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO);
539 update_inode_page(inode);
542 void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
544 /* remove orphan entry from orphan list */
545 __remove_ino_entry(sbi, ino, ORPHAN_INO);
548 static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
550 struct inode *inode;
551 struct node_info ni;
552 int err = acquire_orphan_inode(sbi);
554 if (err) {
555 set_sbi_flag(sbi, SBI_NEED_FSCK);
556 f2fs_msg(sbi->sb, KERN_WARNING,
557 "%s: orphan failed (ino=%x), run fsck to fix.",
558 __func__, ino);
559 return err;
562 __add_ino_entry(sbi, ino, ORPHAN_INO);
564 inode = f2fs_iget_retry(sbi->sb, ino);
565 if (IS_ERR(inode)) {
567 * there should be a bug that we can't find the entry
568 * to orphan inode.
570 f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
571 return PTR_ERR(inode);
574 clear_nlink(inode);
576 /* truncate all the data during iput */
577 iput(inode);
579 get_node_info(sbi, ino, &ni);
581 /* ENOMEM was fully retried in f2fs_evict_inode. */
582 if (ni.blk_addr != NULL_ADDR) {
583 set_sbi_flag(sbi, SBI_NEED_FSCK);
584 f2fs_msg(sbi->sb, KERN_WARNING,
585 "%s: orphan failed (ino=%x), run fsck to fix.",
586 __func__, ino);
587 return -EIO;
589 __remove_ino_entry(sbi, ino, ORPHAN_INO);
590 return 0;
593 int recover_orphan_inodes(struct f2fs_sb_info *sbi)
595 block_t start_blk, orphan_blocks, i, j;
596 int err;
598 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
599 return 0;
601 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
602 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
604 ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
606 for (i = 0; i < orphan_blocks; i++) {
607 struct page *page = get_meta_page(sbi, start_blk + i);
608 struct f2fs_orphan_block *orphan_blk;
610 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
611 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
612 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
613 err = recover_orphan_inode(sbi, ino);
614 if (err) {
615 f2fs_put_page(page, 1);
616 return err;
619 f2fs_put_page(page, 1);
621 /* clear Orphan Flag */
622 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
623 return 0;
626 static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
628 struct list_head *head;
629 struct f2fs_orphan_block *orphan_blk = NULL;
630 unsigned int nentries = 0;
631 unsigned short index = 1;
632 unsigned short orphan_blocks;
633 struct page *page = NULL;
634 struct ino_entry *orphan = NULL;
635 struct inode_management *im = &sbi->im[ORPHAN_INO];
637 orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
640 * we don't need to do spin_lock(&im->ino_lock) here, since all the
641 * orphan inode operations are covered under f2fs_lock_op().
642 * And, spin_lock should be avoided due to page operations below.
644 head = &im->ino_list;
646 /* loop for each orphan inode entry and write them in Jornal block */
647 list_for_each_entry(orphan, head, list) {
648 if (!page) {
649 page = grab_meta_page(sbi, start_blk++);
650 orphan_blk =
651 (struct f2fs_orphan_block *)page_address(page);
652 memset(orphan_blk, 0, sizeof(*orphan_blk));
655 orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
657 if (nentries == F2FS_ORPHANS_PER_BLOCK) {
659 * an orphan block is full of 1020 entries,
660 * then we need to flush current orphan blocks
661 * and bring another one in memory
663 orphan_blk->blk_addr = cpu_to_le16(index);
664 orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
665 orphan_blk->entry_count = cpu_to_le32(nentries);
666 set_page_dirty(page);
667 f2fs_put_page(page, 1);
668 index++;
669 nentries = 0;
670 page = NULL;
674 if (page) {
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);
683 static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
684 struct f2fs_checkpoint **cp_block, struct page **cp_page,
685 unsigned long long *version)
687 unsigned long blk_size = sbi->blocksize;
688 size_t crc_offset = 0;
689 __u32 crc = 0;
691 *cp_page = get_meta_page(sbi, cp_addr);
692 *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
694 crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
695 if (crc_offset >= blk_size) {
696 f2fs_put_page(*cp_page, 1);
697 f2fs_msg(sbi->sb, KERN_WARNING,
698 "invalid crc_offset: %zu", crc_offset);
699 return -EINVAL;
702 crc = le32_to_cpu(*((__le32 *)((unsigned char *)*cp_block
703 + crc_offset)));
704 if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
705 f2fs_put_page(*cp_page, 1);
706 f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
707 return -EINVAL;
710 *version = cur_cp_version(*cp_block);
711 return 0;
714 static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
715 block_t cp_addr, unsigned long long *version)
717 struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
718 struct f2fs_checkpoint *cp_block = NULL;
719 unsigned long long cur_version = 0, pre_version = 0;
720 int err;
722 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
723 &cp_page_1, version);
724 if (err)
725 return NULL;
727 if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
728 sbi->blocks_per_seg) {
729 f2fs_msg(sbi->sb, KERN_WARNING,
730 "invalid cp_pack_total_block_count:%u",
731 le32_to_cpu(cp_block->cp_pack_total_block_count));
732 goto invalid_cp;
734 pre_version = *version;
736 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
737 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
738 &cp_page_2, version);
739 if (err)
740 goto invalid_cp;
741 cur_version = *version;
743 if (cur_version == pre_version) {
744 *version = cur_version;
745 f2fs_put_page(cp_page_2, 1);
746 return cp_page_1;
748 f2fs_put_page(cp_page_2, 1);
749 invalid_cp:
750 f2fs_put_page(cp_page_1, 1);
751 return NULL;
754 int get_valid_checkpoint(struct f2fs_sb_info *sbi)
756 struct f2fs_checkpoint *cp_block;
757 struct f2fs_super_block *fsb = sbi->raw_super;
758 struct page *cp1, *cp2, *cur_page;
759 unsigned long blk_size = sbi->blocksize;
760 unsigned long long cp1_version = 0, cp2_version = 0;
761 unsigned long long cp_start_blk_no;
762 unsigned int cp_blks = 1 + __cp_payload(sbi);
763 block_t cp_blk_no;
764 int i;
766 sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
767 if (!sbi->ckpt)
768 return -ENOMEM;
770 * Finding out valid cp block involves read both
771 * sets( cp pack1 and cp pack 2)
773 cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
774 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
776 /* The second checkpoint pack should start at the next segment */
777 cp_start_blk_no += ((unsigned long long)1) <<
778 le32_to_cpu(fsb->log_blocks_per_seg);
779 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
781 if (cp1 && cp2) {
782 if (ver_after(cp2_version, cp1_version))
783 cur_page = cp2;
784 else
785 cur_page = cp1;
786 } else if (cp1) {
787 cur_page = cp1;
788 } else if (cp2) {
789 cur_page = cp2;
790 } else {
791 goto fail_no_cp;
794 cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
795 memcpy(sbi->ckpt, cp_block, blk_size);
797 if (cur_page == cp1)
798 sbi->cur_cp_pack = 1;
799 else
800 sbi->cur_cp_pack = 2;
802 /* Sanity checking of checkpoint */
803 if (sanity_check_ckpt(sbi))
804 goto free_fail_no_cp;
806 if (cp_blks <= 1)
807 goto done;
809 cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
810 if (cur_page == cp2)
811 cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
813 for (i = 1; i < cp_blks; i++) {
814 void *sit_bitmap_ptr;
815 unsigned char *ckpt = (unsigned char *)sbi->ckpt;
817 cur_page = get_meta_page(sbi, cp_blk_no + i);
818 sit_bitmap_ptr = page_address(cur_page);
819 memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
820 f2fs_put_page(cur_page, 1);
822 done:
823 f2fs_put_page(cp1, 1);
824 f2fs_put_page(cp2, 1);
825 return 0;
827 free_fail_no_cp:
828 f2fs_put_page(cp1, 1);
829 f2fs_put_page(cp2, 1);
830 fail_no_cp:
831 kfree(sbi->ckpt);
832 return -EINVAL;
835 static void __add_dirty_inode(struct inode *inode, enum inode_type type)
837 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
838 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
840 if (is_inode_flag_set(inode, flag))
841 return;
843 set_inode_flag(inode, flag);
844 list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
845 stat_inc_dirty_inode(sbi, type);
848 static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
850 int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
852 if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
853 return;
855 list_del_init(&F2FS_I(inode)->dirty_list);
856 clear_inode_flag(inode, flag);
857 stat_dec_dirty_inode(F2FS_I_SB(inode), type);
860 void update_dirty_page(struct inode *inode, struct page *page)
862 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
863 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
865 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
866 !S_ISLNK(inode->i_mode))
867 return;
869 spin_lock(&sbi->inode_lock[type]);
870 if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
871 __add_dirty_inode(inode, type);
872 inode_inc_dirty_pages(inode);
873 spin_unlock(&sbi->inode_lock[type]);
875 SetPagePrivate(page);
876 f2fs_trace_pid(page);
879 void remove_dirty_inode(struct inode *inode)
881 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
882 enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
884 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
885 !S_ISLNK(inode->i_mode))
886 return;
888 if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
889 return;
891 spin_lock(&sbi->inode_lock[type]);
892 __remove_dirty_inode(inode, type);
893 spin_unlock(&sbi->inode_lock[type]);
896 int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
898 struct list_head *head;
899 struct inode *inode;
900 struct f2fs_inode_info *fi;
901 bool is_dir = (type == DIR_INODE);
903 trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
904 get_pages(sbi, is_dir ?
905 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
906 retry:
907 if (unlikely(f2fs_cp_error(sbi)))
908 return -EIO;
910 spin_lock(&sbi->inode_lock[type]);
912 head = &sbi->inode_list[type];
913 if (list_empty(head)) {
914 spin_unlock(&sbi->inode_lock[type]);
915 trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
916 get_pages(sbi, is_dir ?
917 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
918 return 0;
920 fi = list_entry(head->next, struct f2fs_inode_info, dirty_list);
921 inode = igrab(&fi->vfs_inode);
922 spin_unlock(&sbi->inode_lock[type]);
923 if (inode) {
924 filemap_fdatawrite(inode->i_mapping);
925 iput(inode);
926 } else {
928 * We should submit bio, since it exists several
929 * wribacking dentry pages in the freeing inode.
931 f2fs_submit_merged_bio(sbi, DATA, WRITE);
932 cond_resched();
934 goto retry;
937 int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
939 struct list_head *head = &sbi->inode_list[DIRTY_META];
940 struct inode *inode;
941 struct f2fs_inode_info *fi;
942 s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
944 while (total--) {
945 if (unlikely(f2fs_cp_error(sbi)))
946 return -EIO;
948 spin_lock(&sbi->inode_lock[DIRTY_META]);
949 if (list_empty(head)) {
950 spin_unlock(&sbi->inode_lock[DIRTY_META]);
951 return 0;
953 fi = list_entry(head->next, struct f2fs_inode_info,
954 gdirty_list);
955 inode = igrab(&fi->vfs_inode);
956 spin_unlock(&sbi->inode_lock[DIRTY_META]);
957 if (inode) {
958 update_inode_page(inode);
959 iput(inode);
962 return 0;
966 * Freeze all the FS-operations for checkpoint.
968 static int block_operations(struct f2fs_sb_info *sbi)
970 struct writeback_control wbc = {
971 .sync_mode = WB_SYNC_ALL,
972 .nr_to_write = LONG_MAX,
973 .for_reclaim = 0,
975 struct blk_plug plug;
976 int err = 0;
978 blk_start_plug(&plug);
980 retry_flush_dents:
981 f2fs_lock_all(sbi);
982 /* write all the dirty dentry pages */
983 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
984 f2fs_unlock_all(sbi);
985 err = sync_dirty_inodes(sbi, DIR_INODE);
986 if (err)
987 goto out;
988 goto retry_flush_dents;
991 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
992 f2fs_unlock_all(sbi);
993 err = f2fs_sync_inode_meta(sbi);
994 if (err)
995 goto out;
996 goto retry_flush_dents;
1000 * POR: we should ensure that there are no dirty node pages
1001 * until finishing nat/sit flush.
1003 retry_flush_nodes:
1004 down_write(&sbi->node_write);
1006 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1007 up_write(&sbi->node_write);
1008 err = sync_node_pages(sbi, &wbc);
1009 if (err) {
1010 f2fs_unlock_all(sbi);
1011 goto out;
1013 goto retry_flush_nodes;
1015 out:
1016 blk_finish_plug(&plug);
1017 return err;
1020 static void unblock_operations(struct f2fs_sb_info *sbi)
1022 up_write(&sbi->node_write);
1024 build_free_nids(sbi);
1025 f2fs_unlock_all(sbi);
1028 static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1030 DEFINE_WAIT(wait);
1032 for (;;) {
1033 prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1035 if (!atomic_read(&sbi->nr_wb_bios))
1036 break;
1038 io_schedule_timeout(5*HZ);
1040 finish_wait(&sbi->cp_wait, &wait);
1043 static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1045 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1046 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1048 spin_lock(&sbi->cp_lock);
1050 if (cpc->reason == CP_UMOUNT)
1051 __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1052 else
1053 __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1055 if (cpc->reason == CP_FASTBOOT)
1056 __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1057 else
1058 __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1060 if (orphan_num)
1061 __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1062 else
1063 __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1065 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1066 __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1068 /* set this flag to activate crc|cp_ver for recovery */
1069 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1071 spin_unlock(&sbi->cp_lock);
1074 static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1076 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1077 struct f2fs_nm_info *nm_i = NM_I(sbi);
1078 unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1079 nid_t last_nid = nm_i->next_scan_nid;
1080 block_t start_blk;
1081 unsigned int data_sum_blocks, orphan_blocks;
1082 __u32 crc32 = 0;
1083 int i;
1084 int cp_payload_blks = __cp_payload(sbi);
1085 struct super_block *sb = sbi->sb;
1086 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1087 u64 kbytes_written;
1089 /* Flush all the NAT/SIT pages */
1090 while (get_pages(sbi, F2FS_DIRTY_META)) {
1091 sync_meta_pages(sbi, META, LONG_MAX);
1092 if (unlikely(f2fs_cp_error(sbi)))
1093 return -EIO;
1096 next_free_nid(sbi, &last_nid);
1099 * modify checkpoint
1100 * version number is already updated
1102 ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
1103 ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1104 ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1105 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1106 ckpt->cur_node_segno[i] =
1107 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1108 ckpt->cur_node_blkoff[i] =
1109 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1110 ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1111 curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1113 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1114 ckpt->cur_data_segno[i] =
1115 cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1116 ckpt->cur_data_blkoff[i] =
1117 cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1118 ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1119 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1122 ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1123 ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1124 ckpt->next_free_nid = cpu_to_le32(last_nid);
1126 /* 2 cp + n data seg summary + orphan inode blocks */
1127 data_sum_blocks = npages_for_summary_flush(sbi, false);
1128 spin_lock(&sbi->cp_lock);
1129 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1130 __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1131 else
1132 __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1133 spin_unlock(&sbi->cp_lock);
1135 orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1136 ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1137 orphan_blocks);
1139 if (__remain_node_summaries(cpc->reason))
1140 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1141 cp_payload_blks + data_sum_blocks +
1142 orphan_blocks + NR_CURSEG_NODE_TYPE);
1143 else
1144 ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1145 cp_payload_blks + data_sum_blocks +
1146 orphan_blocks);
1148 /* update ckpt flag for checkpoint */
1149 update_ckpt_flags(sbi, cpc);
1151 /* update SIT/NAT bitmap */
1152 get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1153 get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1155 crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
1156 *((__le32 *)((unsigned char *)ckpt +
1157 le32_to_cpu(ckpt->checksum_offset)))
1158 = cpu_to_le32(crc32);
1160 start_blk = __start_cp_next_addr(sbi);
1162 /* need to wait for end_io results */
1163 wait_on_all_pages_writeback(sbi);
1164 if (unlikely(f2fs_cp_error(sbi)))
1165 return -EIO;
1167 /* write out checkpoint buffer at block 0 */
1168 update_meta_page(sbi, ckpt, start_blk++);
1170 for (i = 1; i < 1 + cp_payload_blks; i++)
1171 update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1172 start_blk++);
1174 if (orphan_num) {
1175 write_orphan_inodes(sbi, start_blk);
1176 start_blk += orphan_blocks;
1179 write_data_summaries(sbi, start_blk);
1180 start_blk += data_sum_blocks;
1182 /* Record write statistics in the hot node summary */
1183 kbytes_written = sbi->kbytes_written;
1184 if (sb->s_bdev->bd_part)
1185 kbytes_written += BD_PART_WRITTEN(sbi);
1187 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1189 if (__remain_node_summaries(cpc->reason)) {
1190 write_node_summaries(sbi, start_blk);
1191 start_blk += NR_CURSEG_NODE_TYPE;
1194 /* writeout checkpoint block */
1195 update_meta_page(sbi, ckpt, start_blk);
1197 /* wait for previous submitted node/meta pages writeback */
1198 wait_on_all_pages_writeback(sbi);
1200 if (unlikely(f2fs_cp_error(sbi)))
1201 return -EIO;
1203 filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX);
1204 filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX);
1206 /* update user_block_counts */
1207 sbi->last_valid_block_count = sbi->total_valid_block_count;
1208 percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1210 /* Here, we only have one bio having CP pack */
1211 sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1213 /* wait for previous submitted meta pages writeback */
1214 wait_on_all_pages_writeback(sbi);
1216 release_ino_entry(sbi, false);
1218 if (unlikely(f2fs_cp_error(sbi)))
1219 return -EIO;
1221 clear_prefree_segments(sbi, cpc);
1222 clear_sbi_flag(sbi, SBI_IS_DIRTY);
1223 clear_sbi_flag(sbi, SBI_NEED_CP);
1224 __set_cp_next_pack(sbi);
1227 * redirty superblock if metadata like node page or inode cache is
1228 * updated during writing checkpoint.
1230 if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1231 get_pages(sbi, F2FS_DIRTY_IMETA))
1232 set_sbi_flag(sbi, SBI_IS_DIRTY);
1234 f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1236 return 0;
1240 * We guarantee that this checkpoint procedure will not fail.
1242 int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1244 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1245 unsigned long long ckpt_ver;
1246 int err = 0;
1248 mutex_lock(&sbi->cp_mutex);
1250 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1251 (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
1252 (cpc->reason == CP_DISCARD && !sbi->discard_blks)))
1253 goto out;
1254 if (unlikely(f2fs_cp_error(sbi))) {
1255 err = -EIO;
1256 goto out;
1258 if (f2fs_readonly(sbi->sb)) {
1259 err = -EROFS;
1260 goto out;
1263 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1265 err = block_operations(sbi);
1266 if (err)
1267 goto out;
1269 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1271 f2fs_flush_merged_bios(sbi);
1273 /* this is the case of multiple fstrims without any changes */
1274 if (cpc->reason == CP_DISCARD && !is_sbi_flag_set(sbi, SBI_IS_DIRTY)) {
1275 f2fs_bug_on(sbi, NM_I(sbi)->dirty_nat_cnt);
1276 f2fs_bug_on(sbi, SIT_I(sbi)->dirty_sentries);
1277 f2fs_bug_on(sbi, prefree_segments(sbi));
1278 flush_sit_entries(sbi, cpc);
1279 clear_prefree_segments(sbi, cpc);
1280 f2fs_wait_all_discard_bio(sbi);
1281 unblock_operations(sbi);
1282 goto out;
1286 * update checkpoint pack index
1287 * Increase the version number so that
1288 * SIT entries and seg summaries are written at correct place
1290 ckpt_ver = cur_cp_version(ckpt);
1291 ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1293 /* write cached NAT/SIT entries to NAT/SIT area */
1294 flush_nat_entries(sbi);
1295 flush_sit_entries(sbi, cpc);
1297 /* unlock all the fs_lock[] in do_checkpoint() */
1298 err = do_checkpoint(sbi, cpc);
1300 f2fs_wait_all_discard_bio(sbi);
1302 unblock_operations(sbi);
1303 stat_inc_cp_count(sbi->stat_info);
1305 if (cpc->reason == CP_RECOVERY)
1306 f2fs_msg(sbi->sb, KERN_NOTICE,
1307 "checkpoint: version = %llx", ckpt_ver);
1309 /* do checkpoint periodically */
1310 f2fs_update_time(sbi, CP_TIME);
1311 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1312 out:
1313 mutex_unlock(&sbi->cp_mutex);
1314 return err;
1317 void init_ino_entry_info(struct f2fs_sb_info *sbi)
1319 int i;
1321 for (i = 0; i < MAX_INO_ENTRY; i++) {
1322 struct inode_management *im = &sbi->im[i];
1324 INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1325 spin_lock_init(&im->ino_lock);
1326 INIT_LIST_HEAD(&im->ino_list);
1327 im->ino_num = 0;
1330 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1331 NR_CURSEG_TYPE - __cp_payload(sbi)) *
1332 F2FS_ORPHANS_PER_BLOCK;
1335 int __init create_checkpoint_caches(void)
1337 ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1338 sizeof(struct ino_entry));
1339 if (!ino_entry_slab)
1340 return -ENOMEM;
1341 inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1342 sizeof(struct inode_entry));
1343 if (!inode_entry_slab) {
1344 kmem_cache_destroy(ino_entry_slab);
1345 return -ENOMEM;
1347 return 0;
1350 void destroy_checkpoint_caches(void)
1352 kmem_cache_destroy(ino_entry_slab);
1353 kmem_cache_destroy(inode_entry_slab);