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
*orphan_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
= sbi
->meta_inode
->i_mapping
;
34 struct page
*page
= NULL
;
36 page
= grab_cache_page(mapping
, index
);
42 /* We wait writeback only inside grab_meta_page() */
43 wait_on_page_writeback(page
);
44 SetPageUptodate(page
);
49 * We guarantee no failure on the returned page.
51 struct page
*get_meta_page(struct f2fs_sb_info
*sbi
, pgoff_t index
)
53 struct address_space
*mapping
= sbi
->meta_inode
->i_mapping
;
56 page
= grab_cache_page(mapping
, index
);
61 if (PageUptodate(page
))
64 if (f2fs_submit_page_bio(sbi
, page
, index
,
65 READ_SYNC
| REQ_META
| REQ_PRIO
))
69 if (unlikely(page
->mapping
!= mapping
)) {
70 f2fs_put_page(page
, 1);
74 mark_page_accessed(page
);
78 static int f2fs_write_meta_page(struct page
*page
,
79 struct writeback_control
*wbc
)
81 struct inode
*inode
= page
->mapping
->host
;
82 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
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
)))
92 wait_on_page_writeback(page
);
94 write_meta_page(sbi
, page
);
95 dec_page_count(sbi
, F2FS_DIRTY_META
);
100 dec_page_count(sbi
, F2FS_DIRTY_META
);
101 wbc
->pages_skipped
++;
102 set_page_dirty(page
);
103 return AOP_WRITEPAGE_ACTIVATE
;
106 static int f2fs_write_meta_pages(struct address_space
*mapping
,
107 struct writeback_control
*wbc
)
109 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
110 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
113 if (wbc
->for_kupdate
)
116 if (get_pages(sbi
, F2FS_DIRTY_META
) == 0)
119 /* if mounting is failed, skip writing node pages */
120 mutex_lock(&sbi
->cp_mutex
);
121 written
= sync_meta_pages(sbi
, META
, bio_get_nr_vecs(bdev
));
122 mutex_unlock(&sbi
->cp_mutex
);
123 wbc
->nr_to_write
-= written
;
127 long sync_meta_pages(struct f2fs_sb_info
*sbi
, enum page_type type
,
130 struct address_space
*mapping
= sbi
->meta_inode
->i_mapping
;
131 pgoff_t index
= 0, end
= LONG_MAX
;
134 struct writeback_control wbc
= {
138 pagevec_init(&pvec
, 0);
140 while (index
<= end
) {
142 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
144 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1);
145 if (unlikely(nr_pages
== 0))
148 for (i
= 0; i
< nr_pages
; i
++) {
149 struct page
*page
= pvec
.pages
[i
];
151 f2fs_bug_on(page
->mapping
!= mapping
);
152 f2fs_bug_on(!PageDirty(page
));
153 clear_page_dirty_for_io(page
);
154 if (f2fs_write_meta_page(page
, &wbc
)) {
159 if (unlikely(nwritten
>= nr_to_write
))
162 pagevec_release(&pvec
);
167 f2fs_submit_merged_bio(sbi
, type
, WRITE
);
172 static int f2fs_set_meta_page_dirty(struct page
*page
)
174 struct address_space
*mapping
= page
->mapping
;
175 struct f2fs_sb_info
*sbi
= F2FS_SB(mapping
->host
->i_sb
);
177 trace_f2fs_set_page_dirty(page
, META
);
179 SetPageUptodate(page
);
180 if (!PageDirty(page
)) {
181 __set_page_dirty_nobuffers(page
);
182 inc_page_count(sbi
, F2FS_DIRTY_META
);
188 const struct address_space_operations f2fs_meta_aops
= {
189 .writepage
= f2fs_write_meta_page
,
190 .writepages
= f2fs_write_meta_pages
,
191 .set_page_dirty
= f2fs_set_meta_page_dirty
,
194 int acquire_orphan_inode(struct f2fs_sb_info
*sbi
)
196 unsigned int max_orphans
;
200 * considering 512 blocks in a segment 8 blocks are needed for cp
201 * and log segment summaries. Remaining blocks are used to keep
202 * orphan entries with the limitation one reserved segment
203 * for cp pack we can have max 1020*504 orphan entries
205 max_orphans
= (sbi
->blocks_per_seg
- 2 - NR_CURSEG_TYPE
)
206 * F2FS_ORPHANS_PER_BLOCK
;
207 mutex_lock(&sbi
->orphan_inode_mutex
);
208 if (unlikely(sbi
->n_orphans
>= max_orphans
))
212 mutex_unlock(&sbi
->orphan_inode_mutex
);
216 void release_orphan_inode(struct f2fs_sb_info
*sbi
)
218 mutex_lock(&sbi
->orphan_inode_mutex
);
219 f2fs_bug_on(sbi
->n_orphans
== 0);
221 mutex_unlock(&sbi
->orphan_inode_mutex
);
224 void add_orphan_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
226 struct list_head
*head
, *this;
227 struct orphan_inode_entry
*new = NULL
, *orphan
= NULL
;
229 mutex_lock(&sbi
->orphan_inode_mutex
);
230 head
= &sbi
->orphan_inode_list
;
231 list_for_each(this, head
) {
232 orphan
= list_entry(this, struct orphan_inode_entry
, list
);
233 if (orphan
->ino
== ino
)
235 if (orphan
->ino
> ino
)
240 new = f2fs_kmem_cache_alloc(orphan_entry_slab
, GFP_ATOMIC
);
243 /* add new_oentry into list which is sorted by inode number */
245 list_add(&new->list
, this->prev
);
247 list_add_tail(&new->list
, head
);
249 mutex_unlock(&sbi
->orphan_inode_mutex
);
252 void remove_orphan_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
254 struct list_head
*head
;
255 struct orphan_inode_entry
*orphan
;
257 mutex_lock(&sbi
->orphan_inode_mutex
);
258 head
= &sbi
->orphan_inode_list
;
259 list_for_each_entry(orphan
, head
, list
) {
260 if (orphan
->ino
== ino
) {
261 list_del(&orphan
->list
);
262 kmem_cache_free(orphan_entry_slab
, orphan
);
263 f2fs_bug_on(sbi
->n_orphans
== 0);
268 mutex_unlock(&sbi
->orphan_inode_mutex
);
271 static void recover_orphan_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
273 struct inode
*inode
= f2fs_iget(sbi
->sb
, ino
);
274 f2fs_bug_on(IS_ERR(inode
));
277 /* truncate all the data during iput */
281 void recover_orphan_inodes(struct f2fs_sb_info
*sbi
)
283 block_t start_blk
, orphan_blkaddr
, i
, j
;
285 if (!is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_ORPHAN_PRESENT_FLAG
))
288 sbi
->por_doing
= true;
289 start_blk
= __start_cp_addr(sbi
) + 1;
290 orphan_blkaddr
= __start_sum_addr(sbi
) - 1;
292 for (i
= 0; i
< orphan_blkaddr
; i
++) {
293 struct page
*page
= get_meta_page(sbi
, start_blk
+ i
);
294 struct f2fs_orphan_block
*orphan_blk
;
296 orphan_blk
= (struct f2fs_orphan_block
*)page_address(page
);
297 for (j
= 0; j
< le32_to_cpu(orphan_blk
->entry_count
); j
++) {
298 nid_t ino
= le32_to_cpu(orphan_blk
->ino
[j
]);
299 recover_orphan_inode(sbi
, ino
);
301 f2fs_put_page(page
, 1);
303 /* clear Orphan Flag */
304 clear_ckpt_flags(F2FS_CKPT(sbi
), CP_ORPHAN_PRESENT_FLAG
);
305 sbi
->por_doing
= false;
309 static void write_orphan_inodes(struct f2fs_sb_info
*sbi
, block_t start_blk
)
311 struct list_head
*head
;
312 struct f2fs_orphan_block
*orphan_blk
= NULL
;
313 struct page
*page
= NULL
;
314 unsigned int nentries
= 0;
315 unsigned short index
= 1;
316 unsigned short orphan_blocks
;
317 struct orphan_inode_entry
*orphan
= NULL
;
319 orphan_blocks
= (unsigned short)((sbi
->n_orphans
+
320 (F2FS_ORPHANS_PER_BLOCK
- 1)) / F2FS_ORPHANS_PER_BLOCK
);
322 mutex_lock(&sbi
->orphan_inode_mutex
);
323 head
= &sbi
->orphan_inode_list
;
325 /* loop for each orphan inode entry and write them in Jornal block */
326 list_for_each_entry(orphan
, head
, list
) {
328 page
= grab_meta_page(sbi
, start_blk
);
330 (struct f2fs_orphan_block
*)page_address(page
);
331 memset(orphan_blk
, 0, sizeof(*orphan_blk
));
334 orphan_blk
->ino
[nentries
++] = cpu_to_le32(orphan
->ino
);
336 if (nentries
== F2FS_ORPHANS_PER_BLOCK
) {
338 * an orphan block is full of 1020 entries,
339 * then we need to flush current orphan blocks
340 * and bring another one in memory
342 orphan_blk
->blk_addr
= cpu_to_le16(index
);
343 orphan_blk
->blk_count
= cpu_to_le16(orphan_blocks
);
344 orphan_blk
->entry_count
= cpu_to_le32(nentries
);
345 set_page_dirty(page
);
346 f2fs_put_page(page
, 1);
355 orphan_blk
->blk_addr
= cpu_to_le16(index
);
356 orphan_blk
->blk_count
= cpu_to_le16(orphan_blocks
);
357 orphan_blk
->entry_count
= cpu_to_le32(nentries
);
358 set_page_dirty(page
);
359 f2fs_put_page(page
, 1);
362 mutex_unlock(&sbi
->orphan_inode_mutex
);
365 static struct page
*validate_checkpoint(struct f2fs_sb_info
*sbi
,
366 block_t cp_addr
, unsigned long long *version
)
368 struct page
*cp_page_1
, *cp_page_2
= NULL
;
369 unsigned long blk_size
= sbi
->blocksize
;
370 struct f2fs_checkpoint
*cp_block
;
371 unsigned long long cur_version
= 0, pre_version
= 0;
375 /* Read the 1st cp block in this CP pack */
376 cp_page_1
= get_meta_page(sbi
, cp_addr
);
378 /* get the version number */
379 cp_block
= (struct f2fs_checkpoint
*)page_address(cp_page_1
);
380 crc_offset
= le32_to_cpu(cp_block
->checksum_offset
);
381 if (crc_offset
>= blk_size
)
384 crc
= le32_to_cpu(*((__u32
*)((unsigned char *)cp_block
+ crc_offset
)));
385 if (!f2fs_crc_valid(crc
, cp_block
, crc_offset
))
388 pre_version
= cur_cp_version(cp_block
);
390 /* Read the 2nd cp block in this CP pack */
391 cp_addr
+= le32_to_cpu(cp_block
->cp_pack_total_block_count
) - 1;
392 cp_page_2
= get_meta_page(sbi
, cp_addr
);
394 cp_block
= (struct f2fs_checkpoint
*)page_address(cp_page_2
);
395 crc_offset
= le32_to_cpu(cp_block
->checksum_offset
);
396 if (crc_offset
>= blk_size
)
399 crc
= le32_to_cpu(*((__u32
*)((unsigned char *)cp_block
+ crc_offset
)));
400 if (!f2fs_crc_valid(crc
, cp_block
, crc_offset
))
403 cur_version
= cur_cp_version(cp_block
);
405 if (cur_version
== pre_version
) {
406 *version
= cur_version
;
407 f2fs_put_page(cp_page_2
, 1);
411 f2fs_put_page(cp_page_2
, 1);
413 f2fs_put_page(cp_page_1
, 1);
417 int get_valid_checkpoint(struct f2fs_sb_info
*sbi
)
419 struct f2fs_checkpoint
*cp_block
;
420 struct f2fs_super_block
*fsb
= sbi
->raw_super
;
421 struct page
*cp1
, *cp2
, *cur_page
;
422 unsigned long blk_size
= sbi
->blocksize
;
423 unsigned long long cp1_version
= 0, cp2_version
= 0;
424 unsigned long long cp_start_blk_no
;
426 sbi
->ckpt
= kzalloc(blk_size
, GFP_KERNEL
);
430 * Finding out valid cp block involves read both
431 * sets( cp pack1 and cp pack 2)
433 cp_start_blk_no
= le32_to_cpu(fsb
->cp_blkaddr
);
434 cp1
= validate_checkpoint(sbi
, cp_start_blk_no
, &cp1_version
);
436 /* The second checkpoint pack should start at the next segment */
437 cp_start_blk_no
+= ((unsigned long long)1) <<
438 le32_to_cpu(fsb
->log_blocks_per_seg
);
439 cp2
= validate_checkpoint(sbi
, cp_start_blk_no
, &cp2_version
);
442 if (ver_after(cp2_version
, cp1_version
))
454 cp_block
= (struct f2fs_checkpoint
*)page_address(cur_page
);
455 memcpy(sbi
->ckpt
, cp_block
, blk_size
);
457 f2fs_put_page(cp1
, 1);
458 f2fs_put_page(cp2
, 1);
466 static int __add_dirty_inode(struct inode
*inode
, struct dir_inode_entry
*new)
468 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
469 struct list_head
*head
= &sbi
->dir_inode_list
;
470 struct list_head
*this;
472 list_for_each(this, head
) {
473 struct dir_inode_entry
*entry
;
474 entry
= list_entry(this, struct dir_inode_entry
, list
);
475 if (unlikely(entry
->inode
== inode
))
478 list_add_tail(&new->list
, head
);
479 stat_inc_dirty_dir(sbi
);
483 void set_dirty_dir_page(struct inode
*inode
, struct page
*page
)
485 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
486 struct dir_inode_entry
*new;
488 if (!S_ISDIR(inode
->i_mode
))
491 new = f2fs_kmem_cache_alloc(inode_entry_slab
, GFP_NOFS
);
493 INIT_LIST_HEAD(&new->list
);
495 spin_lock(&sbi
->dir_inode_lock
);
496 if (__add_dirty_inode(inode
, new))
497 kmem_cache_free(inode_entry_slab
, new);
499 inc_page_count(sbi
, F2FS_DIRTY_DENTS
);
500 inode_inc_dirty_dents(inode
);
501 SetPagePrivate(page
);
502 spin_unlock(&sbi
->dir_inode_lock
);
505 void add_dirty_dir_inode(struct inode
*inode
)
507 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
508 struct dir_inode_entry
*new =
509 f2fs_kmem_cache_alloc(inode_entry_slab
, GFP_NOFS
);
512 INIT_LIST_HEAD(&new->list
);
514 spin_lock(&sbi
->dir_inode_lock
);
515 if (__add_dirty_inode(inode
, new))
516 kmem_cache_free(inode_entry_slab
, new);
517 spin_unlock(&sbi
->dir_inode_lock
);
520 void remove_dirty_dir_inode(struct inode
*inode
)
522 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
524 struct list_head
*this, *head
;
526 if (!S_ISDIR(inode
->i_mode
))
529 spin_lock(&sbi
->dir_inode_lock
);
530 if (atomic_read(&F2FS_I(inode
)->dirty_dents
)) {
531 spin_unlock(&sbi
->dir_inode_lock
);
535 head
= &sbi
->dir_inode_list
;
536 list_for_each(this, head
) {
537 struct dir_inode_entry
*entry
;
538 entry
= list_entry(this, struct dir_inode_entry
, list
);
539 if (entry
->inode
== inode
) {
540 list_del(&entry
->list
);
541 kmem_cache_free(inode_entry_slab
, entry
);
542 stat_dec_dirty_dir(sbi
);
546 spin_unlock(&sbi
->dir_inode_lock
);
548 /* Only from the recovery routine */
549 if (is_inode_flag_set(F2FS_I(inode
), FI_DELAY_IPUT
)) {
550 clear_inode_flag(F2FS_I(inode
), FI_DELAY_IPUT
);
555 struct inode
*check_dirty_dir_inode(struct f2fs_sb_info
*sbi
, nid_t ino
)
558 struct list_head
*this, *head
;
559 struct inode
*inode
= NULL
;
561 spin_lock(&sbi
->dir_inode_lock
);
563 head
= &sbi
->dir_inode_list
;
564 list_for_each(this, head
) {
565 struct dir_inode_entry
*entry
;
566 entry
= list_entry(this, struct dir_inode_entry
, list
);
567 if (entry
->inode
->i_ino
== ino
) {
568 inode
= entry
->inode
;
572 spin_unlock(&sbi
->dir_inode_lock
);
576 void sync_dirty_dir_inodes(struct f2fs_sb_info
*sbi
)
578 struct list_head
*head
;
579 struct dir_inode_entry
*entry
;
582 spin_lock(&sbi
->dir_inode_lock
);
584 head
= &sbi
->dir_inode_list
;
585 if (list_empty(head
)) {
586 spin_unlock(&sbi
->dir_inode_lock
);
589 entry
= list_entry(head
->next
, struct dir_inode_entry
, list
);
590 inode
= igrab(entry
->inode
);
591 spin_unlock(&sbi
->dir_inode_lock
);
593 filemap_flush(inode
->i_mapping
);
597 * We should submit bio, since it exists several
598 * wribacking dentry pages in the freeing inode.
600 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
606 * Freeze all the FS-operations for checkpoint.
608 static void block_operations(struct f2fs_sb_info
*sbi
)
610 struct writeback_control wbc
= {
611 .sync_mode
= WB_SYNC_ALL
,
612 .nr_to_write
= LONG_MAX
,
615 struct blk_plug plug
;
617 blk_start_plug(&plug
);
621 /* write all the dirty dentry pages */
622 if (get_pages(sbi
, F2FS_DIRTY_DENTS
)) {
623 f2fs_unlock_all(sbi
);
624 sync_dirty_dir_inodes(sbi
);
625 goto retry_flush_dents
;
629 * POR: we should ensure that there is no dirty node pages
630 * until finishing nat/sit flush.
633 mutex_lock(&sbi
->node_write
);
635 if (get_pages(sbi
, F2FS_DIRTY_NODES
)) {
636 mutex_unlock(&sbi
->node_write
);
637 sync_node_pages(sbi
, 0, &wbc
);
638 goto retry_flush_nodes
;
640 blk_finish_plug(&plug
);
643 static void unblock_operations(struct f2fs_sb_info
*sbi
)
645 mutex_unlock(&sbi
->node_write
);
646 f2fs_unlock_all(sbi
);
649 static void wait_on_all_pages_writeback(struct f2fs_sb_info
*sbi
)
654 prepare_to_wait(&sbi
->cp_wait
, &wait
, TASK_UNINTERRUPTIBLE
);
656 if (!get_pages(sbi
, F2FS_WRITEBACK
))
661 finish_wait(&sbi
->cp_wait
, &wait
);
664 static void do_checkpoint(struct f2fs_sb_info
*sbi
, bool is_umount
)
666 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
669 struct page
*cp_page
;
670 unsigned int data_sum_blocks
, orphan_blocks
;
675 /* Flush all the NAT/SIT pages */
676 while (get_pages(sbi
, F2FS_DIRTY_META
))
677 sync_meta_pages(sbi
, META
, LONG_MAX
);
679 next_free_nid(sbi
, &last_nid
);
683 * version number is already updated
685 ckpt
->elapsed_time
= cpu_to_le64(get_mtime(sbi
));
686 ckpt
->valid_block_count
= cpu_to_le64(valid_user_blocks(sbi
));
687 ckpt
->free_segment_count
= cpu_to_le32(free_segments(sbi
));
688 for (i
= 0; i
< 3; i
++) {
689 ckpt
->cur_node_segno
[i
] =
690 cpu_to_le32(curseg_segno(sbi
, i
+ CURSEG_HOT_NODE
));
691 ckpt
->cur_node_blkoff
[i
] =
692 cpu_to_le16(curseg_blkoff(sbi
, i
+ CURSEG_HOT_NODE
));
693 ckpt
->alloc_type
[i
+ CURSEG_HOT_NODE
] =
694 curseg_alloc_type(sbi
, i
+ CURSEG_HOT_NODE
);
696 for (i
= 0; i
< 3; i
++) {
697 ckpt
->cur_data_segno
[i
] =
698 cpu_to_le32(curseg_segno(sbi
, i
+ CURSEG_HOT_DATA
));
699 ckpt
->cur_data_blkoff
[i
] =
700 cpu_to_le16(curseg_blkoff(sbi
, i
+ CURSEG_HOT_DATA
));
701 ckpt
->alloc_type
[i
+ CURSEG_HOT_DATA
] =
702 curseg_alloc_type(sbi
, i
+ CURSEG_HOT_DATA
);
705 ckpt
->valid_node_count
= cpu_to_le32(valid_node_count(sbi
));
706 ckpt
->valid_inode_count
= cpu_to_le32(valid_inode_count(sbi
));
707 ckpt
->next_free_nid
= cpu_to_le32(last_nid
);
709 /* 2 cp + n data seg summary + orphan inode blocks */
710 data_sum_blocks
= npages_for_summary_flush(sbi
);
711 if (data_sum_blocks
< 3)
712 set_ckpt_flags(ckpt
, CP_COMPACT_SUM_FLAG
);
714 clear_ckpt_flags(ckpt
, CP_COMPACT_SUM_FLAG
);
716 orphan_blocks
= (sbi
->n_orphans
+ F2FS_ORPHANS_PER_BLOCK
- 1)
717 / F2FS_ORPHANS_PER_BLOCK
;
718 ckpt
->cp_pack_start_sum
= cpu_to_le32(1 + orphan_blocks
);
721 set_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
);
722 ckpt
->cp_pack_total_block_count
= cpu_to_le32(2 +
723 data_sum_blocks
+ orphan_blocks
+ NR_CURSEG_NODE_TYPE
);
725 clear_ckpt_flags(ckpt
, CP_UMOUNT_FLAG
);
726 ckpt
->cp_pack_total_block_count
= cpu_to_le32(2 +
727 data_sum_blocks
+ orphan_blocks
);
731 set_ckpt_flags(ckpt
, CP_ORPHAN_PRESENT_FLAG
);
733 clear_ckpt_flags(ckpt
, CP_ORPHAN_PRESENT_FLAG
);
735 /* update SIT/NAT bitmap */
736 get_sit_bitmap(sbi
, __bitmap_ptr(sbi
, SIT_BITMAP
));
737 get_nat_bitmap(sbi
, __bitmap_ptr(sbi
, NAT_BITMAP
));
739 crc32
= f2fs_crc32(ckpt
, le32_to_cpu(ckpt
->checksum_offset
));
740 *((__le32
*)((unsigned char *)ckpt
+
741 le32_to_cpu(ckpt
->checksum_offset
)))
742 = cpu_to_le32(crc32
);
744 start_blk
= __start_cp_addr(sbi
);
746 /* write out checkpoint buffer at block 0 */
747 cp_page
= grab_meta_page(sbi
, start_blk
++);
748 kaddr
= page_address(cp_page
);
749 memcpy(kaddr
, ckpt
, (1 << sbi
->log_blocksize
));
750 set_page_dirty(cp_page
);
751 f2fs_put_page(cp_page
, 1);
753 if (sbi
->n_orphans
) {
754 write_orphan_inodes(sbi
, start_blk
);
755 start_blk
+= orphan_blocks
;
758 write_data_summaries(sbi
, start_blk
);
759 start_blk
+= data_sum_blocks
;
761 write_node_summaries(sbi
, start_blk
);
762 start_blk
+= NR_CURSEG_NODE_TYPE
;
765 /* writeout checkpoint block */
766 cp_page
= grab_meta_page(sbi
, start_blk
);
767 kaddr
= page_address(cp_page
);
768 memcpy(kaddr
, ckpt
, (1 << sbi
->log_blocksize
));
769 set_page_dirty(cp_page
);
770 f2fs_put_page(cp_page
, 1);
772 /* wait for previous submitted node/meta pages writeback */
773 wait_on_all_pages_writeback(sbi
);
775 filemap_fdatawait_range(sbi
->node_inode
->i_mapping
, 0, LONG_MAX
);
776 filemap_fdatawait_range(sbi
->meta_inode
->i_mapping
, 0, LONG_MAX
);
778 /* update user_block_counts */
779 sbi
->last_valid_block_count
= sbi
->total_valid_block_count
;
780 sbi
->alloc_valid_block_count
= 0;
782 /* Here, we only have one bio having CP pack */
783 sync_meta_pages(sbi
, META_FLUSH
, LONG_MAX
);
785 if (unlikely(!is_set_ckpt_flags(ckpt
, CP_ERROR_FLAG
))) {
786 clear_prefree_segments(sbi
);
787 F2FS_RESET_SB_DIRT(sbi
);
792 * We guarantee that this checkpoint procedure should not fail.
794 void write_checkpoint(struct f2fs_sb_info
*sbi
, bool is_umount
)
796 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
797 unsigned long long ckpt_ver
;
799 trace_f2fs_write_checkpoint(sbi
->sb
, is_umount
, "start block_ops");
801 mutex_lock(&sbi
->cp_mutex
);
802 block_operations(sbi
);
804 trace_f2fs_write_checkpoint(sbi
->sb
, is_umount
, "finish block_ops");
806 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
807 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
808 f2fs_submit_merged_bio(sbi
, META
, WRITE
);
811 * update checkpoint pack index
812 * Increase the version number so that
813 * SIT entries and seg summaries are written at correct place
815 ckpt_ver
= cur_cp_version(ckpt
);
816 ckpt
->checkpoint_ver
= cpu_to_le64(++ckpt_ver
);
818 /* write cached NAT/SIT entries to NAT/SIT area */
819 flush_nat_entries(sbi
);
820 flush_sit_entries(sbi
);
822 /* unlock all the fs_lock[] in do_checkpoint() */
823 do_checkpoint(sbi
, is_umount
);
825 unblock_operations(sbi
);
826 mutex_unlock(&sbi
->cp_mutex
);
828 trace_f2fs_write_checkpoint(sbi
->sb
, is_umount
, "finish checkpoint");
831 void init_orphan_info(struct f2fs_sb_info
*sbi
)
833 mutex_init(&sbi
->orphan_inode_mutex
);
834 INIT_LIST_HEAD(&sbi
->orphan_inode_list
);
838 int __init
create_checkpoint_caches(void)
840 orphan_entry_slab
= f2fs_kmem_cache_create("f2fs_orphan_entry",
841 sizeof(struct orphan_inode_entry
), NULL
);
842 if (!orphan_entry_slab
)
844 inode_entry_slab
= f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
845 sizeof(struct dir_inode_entry
), NULL
);
846 if (!inode_entry_slab
) {
847 kmem_cache_destroy(orphan_entry_slab
);
853 void destroy_checkpoint_caches(void)
855 kmem_cache_destroy(orphan_entry_slab
);
856 kmem_cache_destroy(inode_entry_slab
);