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.
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>
23 #include <trace/events/f2fs.h>
25 static struct kmem_cache
*ino_entry_slab
;
26 static struct kmem_cache
*inode_entry_slab
;
29 * We guarantee no failure on the returned page.
31 struct page
*grab_meta_page(struct f2fs_sb_info
*sbi
, pgoff_t index
)
33 struct address_space
*mapping
= META_MAPPING(sbi
);
34 struct page
*page
= NULL
;
36 page
= grab_cache_page(mapping
, index
);
41 f2fs_wait_on_page_writeback(page
, META
);
42 SetPageUptodate(page
);
47 * We guarantee no failure on the returned page.
49 struct page
*get_meta_page(struct f2fs_sb_info
*sbi
, pgoff_t index
)
51 struct address_space
*mapping
= META_MAPPING(sbi
);
54 page
= grab_cache_page(mapping
, index
);
59 if (PageUptodate(page
))
62 if (f2fs_submit_page_bio(sbi
, page
, index
,
63 READ_SYNC
| REQ_META
| REQ_PRIO
))
67 if (unlikely(page
->mapping
!= mapping
)) {
68 f2fs_put_page(page
, 1);
75 static inline int get_max_meta_blks(struct f2fs_sb_info
*sbi
, int type
)
79 return NM_I(sbi
)->max_nid
/ NAT_ENTRY_PER_BLOCK
;
81 return SIT_BLK_CNT(sbi
);
91 * Readahead CP/NAT/SIT/SSA pages
93 int ra_meta_pages(struct f2fs_sb_info
*sbi
, int start
, int nrpages
, int type
)
95 block_t prev_blk_addr
= 0;
98 int max_blks
= get_max_meta_blks(sbi
, type
);
100 struct f2fs_io_info fio
= {
102 .rw
= READ_SYNC
| REQ_META
| REQ_PRIO
105 for (; nrpages
-- > 0; blkno
++) {
110 /* get nat block addr */
111 if (unlikely(blkno
>= max_blks
))
113 blk_addr
= current_nat_addr(sbi
,
114 blkno
* NAT_ENTRY_PER_BLOCK
);
117 /* get sit block addr */
118 if (unlikely(blkno
>= max_blks
))
120 blk_addr
= current_sit_addr(sbi
,
121 blkno
* SIT_ENTRY_PER_BLOCK
);
122 if (blkno
!= start
&& prev_blk_addr
+ 1 != blk_addr
)
124 prev_blk_addr
= blk_addr
;
128 /* get ssa/cp block addr */
135 page
= grab_cache_page(META_MAPPING(sbi
), blk_addr
);
138 if (PageUptodate(page
)) {
139 f2fs_put_page(page
, 1);
143 f2fs_submit_page_mbio(sbi
, page
, blk_addr
, &fio
);
144 f2fs_put_page(page
, 0);
147 f2fs_submit_merged_bio(sbi
, META
, READ
);
148 return blkno
- start
;
151 static int f2fs_write_meta_page(struct page
*page
,
152 struct writeback_control
*wbc
)
154 struct inode
*inode
= page
->mapping
->host
;
155 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
157 trace_f2fs_writepage(page
, META
);
159 if (unlikely(sbi
->por_doing
))
161 if (wbc
->for_reclaim
)
163 if (unlikely(f2fs_cp_error(sbi
)))
166 f2fs_wait_on_page_writeback(page
, META
);
167 write_meta_page(sbi
, page
);
168 dec_page_count(sbi
, F2FS_DIRTY_META
);
173 redirty_page_for_writepage(wbc
, page
);
174 return AOP_WRITEPAGE_ACTIVATE
;
177 static int f2fs_write_meta_pages(struct address_space
*mapping
,
178 struct writeback_control
*wbc
)
180 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
183 trace_f2fs_writepages(mapping
->host
, wbc
, META
);
185 /* collect a number of dirty meta pages and write together */
186 if (wbc
->for_kupdate
||
187 get_pages(sbi
, F2FS_DIRTY_META
) < nr_pages_to_skip(sbi
, META
))
190 /* if mounting is failed, skip writing node pages */
191 mutex_lock(&sbi
->cp_mutex
);
192 diff
= nr_pages_to_write(sbi
, META
, wbc
);
193 written
= sync_meta_pages(sbi
, META
, wbc
->nr_to_write
);
194 mutex_unlock(&sbi
->cp_mutex
);
195 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- written
- diff
);
199 wbc
->pages_skipped
+= get_pages(sbi
, F2FS_DIRTY_META
);
203 long sync_meta_pages(struct f2fs_sb_info
*sbi
, enum page_type type
,
206 struct address_space
*mapping
= META_MAPPING(sbi
);
207 pgoff_t index
= 0, end
= LONG_MAX
;
210 struct writeback_control wbc
= {
214 pagevec_init(&pvec
, 0);
216 while (index
<= end
) {
218 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
220 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
221 if (unlikely(nr_pages
== 0))
224 for (i
= 0; i
< nr_pages
; i
++) {
225 struct page
*page
= pvec
.pages
[i
];
229 if (unlikely(page
->mapping
!= mapping
)) {
234 if (!PageDirty(page
)) {
235 /* someone wrote it for us */
236 goto continue_unlock
;
239 if (!clear_page_dirty_for_io(page
))
240 goto continue_unlock
;
242 if (f2fs_write_meta_page(page
, &wbc
)) {
247 if (unlikely(nwritten
>= nr_to_write
))
250 pagevec_release(&pvec
);
255 f2fs_submit_merged_bio(sbi
, type
, WRITE
);
260 static int f2fs_set_meta_page_dirty(struct page
*page
)
262 struct address_space
*mapping
= page
->mapping
;
263 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
265 trace_f2fs_set_page_dirty(page
, META
);
267 SetPageUptodate(page
);
268 if (!PageDirty(page
)) {
269 __set_page_dirty_nobuffers(page
);
270 inc_page_count(sbi
, F2FS_DIRTY_META
);
276 const struct address_space_operations f2fs_meta_aops
= {
277 .writepage
= f2fs_write_meta_page
,
278 .writepages
= f2fs_write_meta_pages
,
279 .set_page_dirty
= f2fs_set_meta_page_dirty
,
282 static void __add_ino_entry(struct f2fs_sb_info
*sbi
, nid_t ino
, int type
)
286 spin_lock(&sbi
->ino_lock
[type
]);
288 e
= radix_tree_lookup(&sbi
->ino_root
[type
], ino
);
290 e
= kmem_cache_alloc(ino_entry_slab
, GFP_ATOMIC
);
292 spin_unlock(&sbi
->ino_lock
[type
]);
295 if (radix_tree_insert(&sbi
->ino_root
[type
], ino
, e
)) {
296 spin_unlock(&sbi
->ino_lock
[type
]);
297 kmem_cache_free(ino_entry_slab
, e
);
300 memset(e
, 0, sizeof(struct ino_entry
));
303 list_add_tail(&e
->list
, &sbi
->ino_list
[type
]);
305 spin_unlock(&sbi
->ino_lock
[type
]);
308 static void __remove_ino_entry(struct f2fs_sb_info
*sbi
, nid_t ino
, int type
)
312 spin_lock(&sbi
->ino_lock
[type
]);
313 e
= radix_tree_lookup(&sbi
->ino_root
[type
], ino
);
316 radix_tree_delete(&sbi
->ino_root
[type
], ino
);
317 if (type
== ORPHAN_INO
)
319 spin_unlock(&sbi
->ino_lock
[type
]);
320 kmem_cache_free(ino_entry_slab
, e
);
323 spin_unlock(&sbi
->ino_lock
[type
]);
326 void add_dirty_inode(struct f2fs_sb_info
*sbi
, nid_t ino
, int type
)
328 /* add new dirty ino entry into list */
329 __add_ino_entry(sbi
, ino
, type
);
332 void remove_dirty_inode(struct f2fs_sb_info
*sbi
, nid_t ino
, int type
)
334 /* remove dirty ino entry from list */
335 __remove_ino_entry(sbi
, ino
, type
);
338 /* mode should be APPEND_INO or UPDATE_INO */
339 bool exist_written_data(struct f2fs_sb_info
*sbi
, nid_t ino
, int mode
)
342 spin_lock(&sbi
->ino_lock
[mode
]);
343 e
= radix_tree_lookup(&sbi
->ino_root
[mode
], ino
);
344 spin_unlock(&sbi
->ino_lock
[mode
]);
345 return e
? true : false;
348 void release_dirty_inode(struct f2fs_sb_info
*sbi
)
350 struct ino_entry
*e
, *tmp
;
353 for (i
= APPEND_INO
; i
<= UPDATE_INO
; i
++) {
354 spin_lock(&sbi
->ino_lock
[i
]);
355 list_for_each_entry_safe(e
, tmp
, &sbi
->ino_list
[i
], list
) {
357 radix_tree_delete(&sbi
->ino_root
[i
], e
->ino
);
358 kmem_cache_free(ino_entry_slab
, e
);
360 spin_unlock(&sbi
->ino_lock
[i
]);
364 int acquire_orphan_inode(struct f2fs_sb_info
*sbi
)
368 spin_lock(&sbi
->ino_lock
[ORPHAN_INO
]);
369 if (unlikely(sbi
->n_orphans
>= sbi
->max_orphans
))
373 spin_unlock(&sbi
->ino_lock
[ORPHAN_INO
]);
378 void release_orphan_inode(struct f2fs_sb_info
*sbi
)
380 spin_lock(&sbi
->ino_lock
[ORPHAN_INO
]);
381 f2fs_bug_on(sbi
->n_orphans
== 0);
383 spin_unlock(&sbi
->ino_lock
[ORPHAN_INO
]);
386 void add_orphan_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
388 /* add new orphan ino entry into list */
389 __add_ino_entry(sbi
, ino
, ORPHAN_INO
);
392 void remove_orphan_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
394 /* remove orphan entry from orphan list */
395 __remove_ino_entry(sbi
, ino
, ORPHAN_INO
);
398 static void recover_orphan_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
400 struct inode
*inode
= f2fs_iget(sbi
->sb
, ino
);
401 f2fs_bug_on(IS_ERR(inode
));
404 /* truncate all the data during iput */
408 void recover_orphan_inodes(struct f2fs_sb_info
*sbi
)
410 block_t start_blk
, orphan_blkaddr
, i
, j
;
412 if (!is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_ORPHAN_PRESENT_FLAG
))
415 sbi
->por_doing
= true;
417 start_blk
= __start_cp_addr(sbi
) + 1 +
418 le32_to_cpu(F2FS_RAW_SUPER(sbi
)->cp_payload
);
419 orphan_blkaddr
= __start_sum_addr(sbi
) - 1;
421 ra_meta_pages(sbi
, start_blk
, orphan_blkaddr
, META_CP
);
423 for (i
= 0; i
< orphan_blkaddr
; i
++) {
424 struct page
*page
= get_meta_page(sbi
, start_blk
+ i
);
425 struct f2fs_orphan_block
*orphan_blk
;
427 orphan_blk
= (struct f2fs_orphan_block
*)page_address(page
);
428 for (j
= 0; j
< le32_to_cpu(orphan_blk
->entry_count
); j
++) {
429 nid_t ino
= le32_to_cpu(orphan_blk
->ino
[j
]);
430 recover_orphan_inode(sbi
, ino
);
432 f2fs_put_page(page
, 1);
434 /* clear Orphan Flag */
435 clear_ckpt_flags(F2FS_CKPT(sbi
), CP_ORPHAN_PRESENT_FLAG
);
436 sbi
->por_doing
= false;
440 static void write_orphan_inodes(struct f2fs_sb_info
*sbi
, block_t start_blk
)
442 struct list_head
*head
;
443 struct f2fs_orphan_block
*orphan_blk
= NULL
;
444 unsigned int nentries
= 0;
445 unsigned short index
;
446 unsigned short orphan_blocks
= (unsigned short)((sbi
->n_orphans
+
447 (F2FS_ORPHANS_PER_BLOCK
- 1)) / F2FS_ORPHANS_PER_BLOCK
);
448 struct page
*page
= NULL
;
449 struct ino_entry
*orphan
= NULL
;
451 for (index
= 0; index
< orphan_blocks
; index
++)
452 grab_meta_page(sbi
, start_blk
+ index
);
455 spin_lock(&sbi
->ino_lock
[ORPHAN_INO
]);
456 head
= &sbi
->ino_list
[ORPHAN_INO
];
458 /* loop for each orphan inode entry and write them in Jornal block */
459 list_for_each_entry(orphan
, head
, list
) {
461 page
= find_get_page(META_MAPPING(sbi
), start_blk
++);
464 (struct f2fs_orphan_block
*)page_address(page
);
465 memset(orphan_blk
, 0, sizeof(*orphan_blk
));
466 f2fs_put_page(page
, 0);
469 orphan_blk
->ino
[nentries
++] = cpu_to_le32(orphan
->ino
);
471 if (nentries
== F2FS_ORPHANS_PER_BLOCK
) {
473 * an orphan block is full of 1020 entries,
474 * then we need to flush current orphan blocks
475 * and bring another one in memory
477 orphan_blk
->blk_addr
= cpu_to_le16(index
);
478 orphan_blk
->blk_count
= cpu_to_le16(orphan_blocks
);
479 orphan_blk
->entry_count
= cpu_to_le32(nentries
);
480 set_page_dirty(page
);
481 f2fs_put_page(page
, 1);
489 orphan_blk
->blk_addr
= cpu_to_le16(index
);
490 orphan_blk
->blk_count
= cpu_to_le16(orphan_blocks
);
491 orphan_blk
->entry_count
= cpu_to_le32(nentries
);
492 set_page_dirty(page
);
493 f2fs_put_page(page
, 1);
496 spin_unlock(&sbi
->ino_lock
[ORPHAN_INO
]);
499 static struct page
*validate_checkpoint(struct f2fs_sb_info
*sbi
,
500 block_t cp_addr
, unsigned long long *version
)
502 struct page
*cp_page_1
, *cp_page_2
= NULL
;
503 unsigned long blk_size
= sbi
->blocksize
;
504 struct f2fs_checkpoint
*cp_block
;
505 unsigned long long cur_version
= 0, pre_version
= 0;
509 /* Read the 1st cp block in this CP pack */
510 cp_page_1
= get_meta_page(sbi
, cp_addr
);
512 /* get the version number */
513 cp_block
= (struct f2fs_checkpoint
*)page_address(cp_page_1
);
514 crc_offset
= le32_to_cpu(cp_block
->checksum_offset
);
515 if (crc_offset
>= blk_size
)
518 crc
= le32_to_cpu(*((__u32
*)((unsigned char *)cp_block
+ crc_offset
)));
519 if (!f2fs_crc_valid(crc
, cp_block
, crc_offset
))
522 pre_version
= cur_cp_version(cp_block
);
524 /* Read the 2nd cp block in this CP pack */
525 cp_addr
+= le32_to_cpu(cp_block
->cp_pack_total_block_count
) - 1;
526 cp_page_2
= get_meta_page(sbi
, cp_addr
);
528 cp_block
= (struct f2fs_checkpoint
*)page_address(cp_page_2
);
529 crc_offset
= le32_to_cpu(cp_block
->checksum_offset
);
530 if (crc_offset
>= blk_size
)
533 crc
= le32_to_cpu(*((__u32
*)((unsigned char *)cp_block
+ crc_offset
)));
534 if (!f2fs_crc_valid(crc
, cp_block
, crc_offset
))
537 cur_version
= cur_cp_version(cp_block
);
539 if (cur_version
== pre_version
) {
540 *version
= cur_version
;
541 f2fs_put_page(cp_page_2
, 1);
545 f2fs_put_page(cp_page_2
, 1);
547 f2fs_put_page(cp_page_1
, 1);
551 int get_valid_checkpoint(struct f2fs_sb_info
*sbi
)
553 struct f2fs_checkpoint
*cp_block
;
554 struct f2fs_super_block
*fsb
= sbi
->raw_super
;
555 struct page
*cp1
, *cp2
, *cur_page
;
556 unsigned long blk_size
= sbi
->blocksize
;
557 unsigned long long cp1_version
= 0, cp2_version
= 0;
558 unsigned long long cp_start_blk_no
;
559 unsigned int cp_blks
= 1 + le32_to_cpu(F2FS_RAW_SUPER(sbi
)->cp_payload
);
563 sbi
->ckpt
= kzalloc(cp_blks
* blk_size
, GFP_KERNEL
);
567 * Finding out valid cp block involves read both
568 * sets( cp pack1 and cp pack 2)
570 cp_start_blk_no
= le32_to_cpu(fsb
->cp_blkaddr
);
571 cp1
= validate_checkpoint(sbi
, cp_start_blk_no
, &cp1_version
);
573 /* The second checkpoint pack should start at the next segment */
574 cp_start_blk_no
+= ((unsigned long long)1) <<
575 le32_to_cpu(fsb
->log_blocks_per_seg
);
576 cp2
= validate_checkpoint(sbi
, cp_start_blk_no
, &cp2_version
);
579 if (ver_after(cp2_version
, cp1_version
))
591 cp_block
= (struct f2fs_checkpoint
*)page_address(cur_page
);
592 memcpy(sbi
->ckpt
, cp_block
, blk_size
);
597 cp_blk_no
= le32_to_cpu(fsb
->cp_blkaddr
);
599 cp_blk_no
+= 1 << le32_to_cpu(fsb
->log_blocks_per_seg
);
601 for (i
= 1; i
< cp_blks
; i
++) {
602 void *sit_bitmap_ptr
;
603 unsigned char *ckpt
= (unsigned char *)sbi
->ckpt
;
605 cur_page
= get_meta_page(sbi
, cp_blk_no
+ i
);
606 sit_bitmap_ptr
= page_address(cur_page
);
607 memcpy(ckpt
+ i
* blk_size
, sit_bitmap_ptr
, blk_size
);
608 f2fs_put_page(cur_page
, 1);
611 f2fs_put_page(cp1
, 1);
612 f2fs_put_page(cp2
, 1);
620 static int __add_dirty_inode(struct inode
*inode
, struct dir_inode_entry
*new)
622 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
624 if (is_inode_flag_set(F2FS_I(inode
), FI_DIRTY_DIR
))
627 set_inode_flag(F2FS_I(inode
), FI_DIRTY_DIR
);
628 F2FS_I(inode
)->dirty_dir
= new;
629 list_add_tail(&new->list
, &sbi
->dir_inode_list
);
630 stat_inc_dirty_dir(sbi
);
634 void set_dirty_dir_page(struct inode
*inode
, struct page
*page
)
636 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
637 struct dir_inode_entry
*new;
640 if (!S_ISDIR(inode
->i_mode
))
643 new = f2fs_kmem_cache_alloc(inode_entry_slab
, GFP_NOFS
);
645 INIT_LIST_HEAD(&new->list
);
647 spin_lock(&sbi
->dir_inode_lock
);
648 ret
= __add_dirty_inode(inode
, new);
649 inode_inc_dirty_dents(inode
);
650 SetPagePrivate(page
);
651 spin_unlock(&sbi
->dir_inode_lock
);
654 kmem_cache_free(inode_entry_slab
, new);
657 void add_dirty_dir_inode(struct inode
*inode
)
659 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
660 struct dir_inode_entry
*new =
661 f2fs_kmem_cache_alloc(inode_entry_slab
, GFP_NOFS
);
665 INIT_LIST_HEAD(&new->list
);
667 spin_lock(&sbi
->dir_inode_lock
);
668 ret
= __add_dirty_inode(inode
, new);
669 spin_unlock(&sbi
->dir_inode_lock
);
672 kmem_cache_free(inode_entry_slab
, new);
675 void remove_dirty_dir_inode(struct inode
*inode
)
677 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
678 struct dir_inode_entry
*entry
;
680 if (!S_ISDIR(inode
->i_mode
))
683 spin_lock(&sbi
->dir_inode_lock
);
684 if (get_dirty_dents(inode
) ||
685 !is_inode_flag_set(F2FS_I(inode
), FI_DIRTY_DIR
)) {
686 spin_unlock(&sbi
->dir_inode_lock
);
690 entry
= F2FS_I(inode
)->dirty_dir
;
691 list_del(&entry
->list
);
692 F2FS_I(inode
)->dirty_dir
= NULL
;
693 clear_inode_flag(F2FS_I(inode
), FI_DIRTY_DIR
);
694 stat_dec_dirty_dir(sbi
);
695 spin_unlock(&sbi
->dir_inode_lock
);
696 kmem_cache_free(inode_entry_slab
, entry
);
698 /* Only from the recovery routine */
699 if (is_inode_flag_set(F2FS_I(inode
), FI_DELAY_IPUT
)) {
700 clear_inode_flag(F2FS_I(inode
), FI_DELAY_IPUT
);
705 void sync_dirty_dir_inodes(struct f2fs_sb_info
*sbi
)
707 struct list_head
*head
;
708 struct dir_inode_entry
*entry
;
711 spin_lock(&sbi
->dir_inode_lock
);
713 head
= &sbi
->dir_inode_list
;
714 if (list_empty(head
)) {
715 spin_unlock(&sbi
->dir_inode_lock
);
718 entry
= list_entry(head
->next
, struct dir_inode_entry
, list
);
719 inode
= igrab(entry
->inode
);
720 spin_unlock(&sbi
->dir_inode_lock
);
722 filemap_fdatawrite(inode
->i_mapping
);
726 * We should submit bio, since it exists several
727 * wribacking dentry pages in the freeing inode.
729 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
735 * Freeze all the FS-operations for checkpoint.
737 static int block_operations(struct f2fs_sb_info
*sbi
)
739 struct writeback_control wbc
= {
740 .sync_mode
= WB_SYNC_ALL
,
741 .nr_to_write
= LONG_MAX
,
744 struct blk_plug plug
;
747 blk_start_plug(&plug
);
751 /* write all the dirty dentry pages */
752 if (get_pages(sbi
, F2FS_DIRTY_DENTS
)) {
753 f2fs_unlock_all(sbi
);
754 sync_dirty_dir_inodes(sbi
);
755 if (unlikely(f2fs_cp_error(sbi
))) {
759 goto retry_flush_dents
;
763 * POR: we should ensure that there are no dirty node pages
764 * until finishing nat/sit flush.
767 down_write(&sbi
->node_write
);
769 if (get_pages(sbi
, F2FS_DIRTY_NODES
)) {
770 up_write(&sbi
->node_write
);
771 sync_node_pages(sbi
, 0, &wbc
);
772 if (unlikely(f2fs_cp_error(sbi
))) {
773 f2fs_unlock_all(sbi
);
777 goto retry_flush_nodes
;
780 blk_finish_plug(&plug
);
784 static void unblock_operations(struct f2fs_sb_info
*sbi
)
786 up_write(&sbi
->node_write
);
787 f2fs_unlock_all(sbi
);
790 static void wait_on_all_pages_writeback(struct f2fs_sb_info
*sbi
)
795 prepare_to_wait(&sbi
->cp_wait
, &wait
, TASK_UNINTERRUPTIBLE
);
797 if (!get_pages(sbi
, F2FS_WRITEBACK
))
802 finish_wait(&sbi
->cp_wait
, &wait
);
805 static void do_checkpoint(struct f2fs_sb_info
*sbi
, bool is_umount
)
807 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
808 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_WARM_NODE
);
811 struct page
*cp_page
;
812 unsigned int data_sum_blocks
, orphan_blocks
;
816 int cp_payload_blks
= le32_to_cpu(F2FS_RAW_SUPER(sbi
)->cp_payload
);
819 * This avoids to conduct wrong roll-forward operations and uses
820 * metapages, so should be called prior to sync_meta_pages below.
822 discard_next_dnode(sbi
, NEXT_FREE_BLKADDR(sbi
, curseg
));
824 /* Flush all the NAT/SIT pages */
825 while (get_pages(sbi
, F2FS_DIRTY_META
)) {
826 sync_meta_pages(sbi
, META
, LONG_MAX
);
827 if (unlikely(f2fs_cp_error(sbi
)))
831 next_free_nid(sbi
, &last_nid
);
835 * version number is already updated
837 ckpt
->elapsed_time
= cpu_to_le64(get_mtime(sbi
));
838 ckpt
->valid_block_count
= cpu_to_le64(valid_user_blocks(sbi
));
839 ckpt
->free_segment_count
= cpu_to_le32(free_segments(sbi
));
840 for (i
= 0; i
< 3; i
++) {
841 ckpt
->cur_node_segno
[i
] =
842 cpu_to_le32(curseg_segno(sbi
, i
+ CURSEG_HOT_NODE
));
843 ckpt
->cur_node_blkoff
[i
] =
844 cpu_to_le16(curseg_blkoff(sbi
, i
+ CURSEG_HOT_NODE
));
845 ckpt
->alloc_type
[i
+ CURSEG_HOT_NODE
] =
846 curseg_alloc_type(sbi
, i
+ CURSEG_HOT_NODE
);
848 for (i
= 0; i
< 3; i
++) {
849 ckpt
->cur_data_segno
[i
] =
850 cpu_to_le32(curseg_segno(sbi
, i
+ CURSEG_HOT_DATA
));
851 ckpt
->cur_data_blkoff
[i
] =
852 cpu_to_le16(curseg_blkoff(sbi
, i
+ CURSEG_HOT_DATA
));
853 ckpt
->alloc_type
[i
+ CURSEG_HOT_DATA
] =
854 curseg_alloc_type(sbi
, i
+ CURSEG_HOT_DATA
);
857 ckpt
->valid_node_count
= cpu_to_le32(valid_node_count(sbi
));
858 ckpt
->valid_inode_count
= cpu_to_le32(valid_inode_count(sbi
));
859 ckpt
->next_free_nid
= cpu_to_le32(last_nid
);
861 /* 2 cp + n data seg summary + orphan inode blocks */
862 data_sum_blocks
= npages_for_summary_flush(sbi
);
863 if (data_sum_blocks
< 3)
864 set_ckpt_flags(ckpt
, CP_COMPACT_SUM_FLAG
);
866 clear_ckpt_flags(ckpt
, CP_COMPACT_SUM_FLAG
);
868 orphan_blocks
= (sbi
->n_orphans
+ F2FS_ORPHANS_PER_BLOCK
- 1)
869 / F2FS_ORPHANS_PER_BLOCK
;
870 ckpt
->cp_pack_start_sum
= cpu_to_le32(1 + cp_payload_blks
+
874 set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
);
875 ckpt
->cp_pack_total_block_count
= cpu_to_le32(2 +
876 cp_payload_blks
+ data_sum_blocks
+
877 orphan_blocks
+ NR_CURSEG_NODE_TYPE
);
879 clear_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
);
880 ckpt
->cp_pack_total_block_count
= cpu_to_le32(2 +
881 cp_payload_blks
+ data_sum_blocks
+
886 set_ckpt_flags(ckpt
, CP_ORPHAN_PRESENT_FLAG
);
888 clear_ckpt_flags(ckpt
, CP_ORPHAN_PRESENT_FLAG
);
890 /* update SIT/NAT bitmap */
891 get_sit_bitmap(sbi
, __bitmap_ptr(sbi
, SIT_BITMAP
));
892 get_nat_bitmap(sbi
, __bitmap_ptr(sbi
, NAT_BITMAP
));
894 crc32
= f2fs_crc32(ckpt
, le32_to_cpu(ckpt
->checksum_offset
));
895 *((__le32
*)((unsigned char *)ckpt
+
896 le32_to_cpu(ckpt
->checksum_offset
)))
897 = cpu_to_le32(crc32
);
899 start_blk
= __start_cp_addr(sbi
);
901 /* write out checkpoint buffer at block 0 */
902 cp_page
= grab_meta_page(sbi
, start_blk
++);
903 kaddr
= page_address(cp_page
);
904 memcpy(kaddr
, ckpt
, (1 << sbi
->log_blocksize
));
905 set_page_dirty(cp_page
);
906 f2fs_put_page(cp_page
, 1);
908 for (i
= 1; i
< 1 + cp_payload_blks
; i
++) {
909 cp_page
= grab_meta_page(sbi
, start_blk
++);
910 kaddr
= page_address(cp_page
);
911 memcpy(kaddr
, (char *)ckpt
+ i
* F2FS_BLKSIZE
,
912 (1 << sbi
->log_blocksize
));
913 set_page_dirty(cp_page
);
914 f2fs_put_page(cp_page
, 1);
917 if (sbi
->n_orphans
) {
918 write_orphan_inodes(sbi
, start_blk
);
919 start_blk
+= orphan_blocks
;
922 write_data_summaries(sbi
, start_blk
);
923 start_blk
+= data_sum_blocks
;
925 write_node_summaries(sbi
, start_blk
);
926 start_blk
+= NR_CURSEG_NODE_TYPE
;
929 /* writeout checkpoint block */
930 cp_page
= grab_meta_page(sbi
, start_blk
);
931 kaddr
= page_address(cp_page
);
932 memcpy(kaddr
, ckpt
, (1 << sbi
->log_blocksize
));
933 set_page_dirty(cp_page
);
934 f2fs_put_page(cp_page
, 1);
936 /* wait for previous submitted node/meta pages writeback */
937 wait_on_all_pages_writeback(sbi
);
939 if (unlikely(f2fs_cp_error(sbi
)))
942 filemap_fdatawait_range(NODE_MAPPING(sbi
), 0, LONG_MAX
);
943 filemap_fdatawait_range(META_MAPPING(sbi
), 0, LONG_MAX
);
945 /* update user_block_counts */
946 sbi
->last_valid_block_count
= sbi
->total_valid_block_count
;
947 sbi
->alloc_valid_block_count
= 0;
949 /* Here, we only have one bio having CP pack */
950 sync_meta_pages(sbi
, META_FLUSH
, LONG_MAX
);
952 release_dirty_inode(sbi
);
954 if (unlikely(f2fs_cp_error(sbi
)))
957 clear_prefree_segments(sbi
);
958 F2FS_RESET_SB_DIRT(sbi
);
962 * We guarantee that this checkpoint procedure will not fail.
964 void write_checkpoint(struct f2fs_sb_info
*sbi
, bool is_umount
)
966 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
967 unsigned long long ckpt_ver
;
969 trace_f2fs_write_checkpoint(sbi
->sb
, is_umount
, "start block_ops");
971 mutex_lock(&sbi
->cp_mutex
);
975 if (unlikely(f2fs_cp_error(sbi
)))
977 if (block_operations(sbi
))
980 trace_f2fs_write_checkpoint(sbi
->sb
, is_umount
, "finish block_ops");
982 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
983 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
984 f2fs_submit_merged_bio(sbi
, META
, WRITE
);
987 * update checkpoint pack index
988 * Increase the version number so that
989 * SIT entries and seg summaries are written at correct place
991 ckpt_ver
= cur_cp_version(ckpt
);
992 ckpt
->checkpoint_ver
= cpu_to_le64(++ckpt_ver
);
994 /* write cached NAT/SIT entries to NAT/SIT area */
995 flush_nat_entries(sbi
);
996 flush_sit_entries(sbi
);
998 /* unlock all the fs_lock[] in do_checkpoint() */
999 do_checkpoint(sbi
, is_umount
);
1001 unblock_operations(sbi
);
1002 stat_inc_cp_count(sbi
->stat_info
);
1004 mutex_unlock(&sbi
->cp_mutex
);
1005 trace_f2fs_write_checkpoint(sbi
->sb
, is_umount
, "finish checkpoint");
1008 void init_ino_entry_info(struct f2fs_sb_info
*sbi
)
1012 for (i
= 0; i
< MAX_INO_ENTRY
; i
++) {
1013 INIT_RADIX_TREE(&sbi
->ino_root
[i
], GFP_ATOMIC
);
1014 spin_lock_init(&sbi
->ino_lock
[i
]);
1015 INIT_LIST_HEAD(&sbi
->ino_list
[i
]);
1019 * considering 512 blocks in a segment 8 blocks are needed for cp
1020 * and log segment summaries. Remaining blocks are used to keep
1021 * orphan entries with the limitation one reserved segment
1022 * for cp pack we can have max 1020*504 orphan entries
1025 sbi
->max_orphans
= (sbi
->blocks_per_seg
- 2 - NR_CURSEG_TYPE
)
1026 * F2FS_ORPHANS_PER_BLOCK
;
1029 int __init
create_checkpoint_caches(void)
1031 ino_entry_slab
= f2fs_kmem_cache_create("f2fs_ino_entry",
1032 sizeof(struct ino_entry
));
1033 if (!ino_entry_slab
)
1035 inode_entry_slab
= f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
1036 sizeof(struct dir_inode_entry
));
1037 if (!inode_entry_slab
) {
1038 kmem_cache_destroy(ino_entry_slab
);
1044 void destroy_checkpoint_caches(void)
1046 kmem_cache_destroy(ino_entry_slab
);
1047 kmem_cache_destroy(inode_entry_slab
);