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/f2fs_fs.h>
13 #include <linux/bio.h>
14 #include <linux/blkdev.h>
15 #include <linux/prefetch.h>
16 #include <linux/vmalloc.h>
23 * This function balances dirty node and dentry pages.
24 * In addition, it controls garbage collection.
26 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
29 * We should do GC or end up with checkpoint, if there are so many dirty
30 * dir/node pages without enough free segments.
32 if (has_not_enough_free_secs(sbi, 0)) {
33 mutex_lock(&sbi->gc_mutex);
38 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
39 enum dirty_type dirty_type)
41 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
43 /* need not be added */
44 if (IS_CURSEG(sbi, segno))
47 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
48 dirty_i->nr_dirty[dirty_type]++;
50 if (dirty_type == DIRTY) {
51 struct seg_entry *sentry = get_seg_entry(sbi, segno);
52 dirty_type = sentry->type;
53 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
54 dirty_i->nr_dirty[dirty_type]++;
58 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
59 enum dirty_type dirty_type)
61 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
63 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
64 dirty_i->nr_dirty[dirty_type]--;
66 if (dirty_type == DIRTY) {
67 struct seg_entry *sentry = get_seg_entry(sbi, segno);
68 dirty_type = sentry->type;
69 if (test_and_clear_bit(segno,
70 dirty_i->dirty_segmap[dirty_type]))
71 dirty_i->nr_dirty[dirty_type]--;
72 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
73 clear_bit(GET_SECNO(sbi, segno),
74 dirty_i->victim_secmap);
79 * Should not occur error such as -ENOMEM.
80 * Adding dirty entry into seglist is not critical operation.
81 * If a given segment is one of current working segments, it won't be added.
83 void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
85 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
86 unsigned short valid_blocks;
88 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
91 mutex_lock(&dirty_i->seglist_lock);
93 valid_blocks = get_valid_blocks(sbi, segno, 0);
95 if (valid_blocks == 0) {
96 __locate_dirty_segment(sbi, segno, PRE);
97 __remove_dirty_segment(sbi, segno, DIRTY);
98 } else if (valid_blocks < sbi->blocks_per_seg) {
99 __locate_dirty_segment(sbi, segno, DIRTY);
101 /* Recovery routine with SSR needs this */
102 __remove_dirty_segment(sbi, segno, DIRTY);
105 mutex_unlock(&dirty_i->seglist_lock);
110 * Should call clear_prefree_segments after checkpoint is done.
112 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
114 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
115 unsigned int segno, offset = 0;
116 unsigned int total_segs = TOTAL_SEGS(sbi);
118 mutex_lock(&dirty_i->seglist_lock);
120 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
122 if (segno >= total_segs)
124 __set_test_and_free(sbi, segno);
127 mutex_unlock(&dirty_i->seglist_lock);
130 void clear_prefree_segments(struct f2fs_sb_info *sbi)
132 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
133 unsigned int segno, offset = 0;
134 unsigned int total_segs = TOTAL_SEGS(sbi);
136 mutex_lock(&dirty_i->seglist_lock);
138 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
140 if (segno >= total_segs)
144 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
145 dirty_i->nr_dirty[PRE]--;
148 if (test_opt(sbi, DISCARD))
149 blkdev_issue_discard(sbi->sb->s_bdev,
150 START_BLOCK(sbi, segno) <<
151 sbi->log_sectors_per_block,
152 1 << (sbi->log_sectors_per_block +
153 sbi->log_blocks_per_seg),
156 mutex_unlock(&dirty_i->seglist_lock);
159 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
161 struct sit_info *sit_i = SIT_I(sbi);
162 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
163 sit_i->dirty_sentries++;
166 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
167 unsigned int segno, int modified)
169 struct seg_entry *se = get_seg_entry(sbi, segno);
172 __mark_sit_entry_dirty(sbi, segno);
175 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
177 struct seg_entry *se;
178 unsigned int segno, offset;
179 long int new_vblocks;
181 segno = GET_SEGNO(sbi, blkaddr);
183 se = get_seg_entry(sbi, segno);
184 new_vblocks = se->valid_blocks + del;
185 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
187 BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
188 (new_vblocks > sbi->blocks_per_seg)));
190 se->valid_blocks = new_vblocks;
191 se->mtime = get_mtime(sbi);
192 SIT_I(sbi)->max_mtime = se->mtime;
194 /* Update valid block bitmap */
196 if (f2fs_set_bit(offset, se->cur_valid_map))
199 if (!f2fs_clear_bit(offset, se->cur_valid_map))
202 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
203 se->ckpt_valid_blocks += del;
205 __mark_sit_entry_dirty(sbi, segno);
207 /* update total number of valid blocks to be written in ckpt area */
208 SIT_I(sbi)->written_valid_blocks += del;
210 if (sbi->segs_per_sec > 1)
211 get_sec_entry(sbi, segno)->valid_blocks += del;
214 static void refresh_sit_entry(struct f2fs_sb_info *sbi,
215 block_t old_blkaddr, block_t new_blkaddr)
217 update_sit_entry(sbi, new_blkaddr, 1);
218 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
219 update_sit_entry(sbi, old_blkaddr, -1);
222 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
224 unsigned int segno = GET_SEGNO(sbi, addr);
225 struct sit_info *sit_i = SIT_I(sbi);
227 BUG_ON(addr == NULL_ADDR);
228 if (addr == NEW_ADDR)
231 /* add it into sit main buffer */
232 mutex_lock(&sit_i->sentry_lock);
234 update_sit_entry(sbi, addr, -1);
236 /* add it into dirty seglist */
237 locate_dirty_segment(sbi, segno);
239 mutex_unlock(&sit_i->sentry_lock);
243 * This function should be resided under the curseg_mutex lock
245 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
246 struct f2fs_summary *sum, unsigned short offset)
248 struct curseg_info *curseg = CURSEG_I(sbi, type);
249 void *addr = curseg->sum_blk;
250 addr += offset * sizeof(struct f2fs_summary);
251 memcpy(addr, sum, sizeof(struct f2fs_summary));
256 * Calculate the number of current summary pages for writing
258 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
260 int total_size_bytes = 0;
261 int valid_sum_count = 0;
264 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
265 if (sbi->ckpt->alloc_type[i] == SSR)
266 valid_sum_count += sbi->blocks_per_seg;
268 valid_sum_count += curseg_blkoff(sbi, i);
271 total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
272 + sizeof(struct nat_journal) + 2
273 + sizeof(struct sit_journal) + 2;
274 sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
275 if (total_size_bytes < sum_space)
277 else if (total_size_bytes < 2 * sum_space)
283 * Caller should put this summary page
285 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
287 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
290 static void write_sum_page(struct f2fs_sb_info *sbi,
291 struct f2fs_summary_block *sum_blk, block_t blk_addr)
293 struct page *page = grab_meta_page(sbi, blk_addr);
294 void *kaddr = page_address(page);
295 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
296 set_page_dirty(page);
297 f2fs_put_page(page, 1);
300 static unsigned int check_prefree_segments(struct f2fs_sb_info *sbi, int type)
302 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
303 unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
305 unsigned int ofs = 0;
308 * If there is not enough reserved sections,
309 * we should not reuse prefree segments.
311 if (has_not_enough_free_secs(sbi, 0))
315 * NODE page should not reuse prefree segment,
316 * since those information is used for SPOR.
318 if (IS_NODESEG(type))
321 segno = find_next_bit(prefree_segmap, TOTAL_SEGS(sbi), ofs);
322 ofs += sbi->segs_per_sec;
324 if (segno < TOTAL_SEGS(sbi)) {
327 /* skip intermediate segments in a section */
328 if (segno % sbi->segs_per_sec)
331 /* skip if the section is currently used */
332 if (sec_usage_check(sbi, GET_SECNO(sbi, segno)))
335 /* skip if whole section is not prefree */
336 for (i = 1; i < sbi->segs_per_sec; i++)
337 if (!test_bit(segno + i, prefree_segmap))
340 /* skip if whole section was not free at the last checkpoint */
341 for (i = 0; i < sbi->segs_per_sec; i++)
342 if (get_seg_entry(sbi, segno + i)->ckpt_valid_blocks)
351 * Find a new segment from the free segments bitmap to right order
352 * This function should be returned with success, otherwise BUG
354 static void get_new_segment(struct f2fs_sb_info *sbi,
355 unsigned int *newseg, bool new_sec, int dir)
357 struct free_segmap_info *free_i = FREE_I(sbi);
358 unsigned int segno, secno, zoneno;
359 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
360 unsigned int hint = *newseg / sbi->segs_per_sec;
361 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
362 unsigned int left_start = hint;
367 write_lock(&free_i->segmap_lock);
369 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
370 segno = find_next_zero_bit(free_i->free_segmap,
371 TOTAL_SEGS(sbi), *newseg + 1);
372 if (segno - *newseg < sbi->segs_per_sec -
373 (*newseg % sbi->segs_per_sec))
377 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
378 if (secno >= TOTAL_SECS(sbi)) {
379 if (dir == ALLOC_RIGHT) {
380 secno = find_next_zero_bit(free_i->free_secmap,
382 BUG_ON(secno >= TOTAL_SECS(sbi));
385 left_start = hint - 1;
391 while (test_bit(left_start, free_i->free_secmap)) {
392 if (left_start > 0) {
396 left_start = find_next_zero_bit(free_i->free_secmap,
398 BUG_ON(left_start >= TOTAL_SECS(sbi));
404 segno = secno * sbi->segs_per_sec;
405 zoneno = secno / sbi->secs_per_zone;
407 /* give up on finding another zone */
410 if (sbi->secs_per_zone == 1)
412 if (zoneno == old_zoneno)
414 if (dir == ALLOC_LEFT) {
415 if (!go_left && zoneno + 1 >= total_zones)
417 if (go_left && zoneno == 0)
420 for (i = 0; i < NR_CURSEG_TYPE; i++)
421 if (CURSEG_I(sbi, i)->zone == zoneno)
424 if (i < NR_CURSEG_TYPE) {
425 /* zone is in user, try another */
427 hint = zoneno * sbi->secs_per_zone - 1;
428 else if (zoneno + 1 >= total_zones)
431 hint = (zoneno + 1) * sbi->secs_per_zone;
433 goto find_other_zone;
436 /* set it as dirty segment in free segmap */
437 BUG_ON(test_bit(segno, free_i->free_segmap));
438 __set_inuse(sbi, segno);
440 write_unlock(&free_i->segmap_lock);
443 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
445 struct curseg_info *curseg = CURSEG_I(sbi, type);
446 struct summary_footer *sum_footer;
448 curseg->segno = curseg->next_segno;
449 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
450 curseg->next_blkoff = 0;
451 curseg->next_segno = NULL_SEGNO;
453 sum_footer = &(curseg->sum_blk->footer);
454 memset(sum_footer, 0, sizeof(struct summary_footer));
455 if (IS_DATASEG(type))
456 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
457 if (IS_NODESEG(type))
458 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
459 __set_sit_entry_type(sbi, type, curseg->segno, modified);
463 * Allocate a current working segment.
464 * This function always allocates a free segment in LFS manner.
466 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
468 struct curseg_info *curseg = CURSEG_I(sbi, type);
469 unsigned int segno = curseg->segno;
470 int dir = ALLOC_LEFT;
472 write_sum_page(sbi, curseg->sum_blk,
473 GET_SUM_BLOCK(sbi, curseg->segno));
474 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
477 if (test_opt(sbi, NOHEAP))
480 get_new_segment(sbi, &segno, new_sec, dir);
481 curseg->next_segno = segno;
482 reset_curseg(sbi, type, 1);
483 curseg->alloc_type = LFS;
486 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
487 struct curseg_info *seg, block_t start)
489 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
491 for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
492 if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
493 && !f2fs_test_bit(ofs, se->cur_valid_map))
496 seg->next_blkoff = ofs;
500 * If a segment is written by LFS manner, next block offset is just obtained
501 * by increasing the current block offset. However, if a segment is written by
502 * SSR manner, next block offset obtained by calling __next_free_blkoff
504 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
505 struct curseg_info *seg)
507 if (seg->alloc_type == SSR)
508 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
514 * This function always allocates a used segment (from dirty seglist) by SSR
515 * manner, so it should recover the existing segment information of valid blocks
517 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
519 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
520 struct curseg_info *curseg = CURSEG_I(sbi, type);
521 unsigned int new_segno = curseg->next_segno;
522 struct f2fs_summary_block *sum_node;
523 struct page *sum_page;
525 write_sum_page(sbi, curseg->sum_blk,
526 GET_SUM_BLOCK(sbi, curseg->segno));
527 __set_test_and_inuse(sbi, new_segno);
529 mutex_lock(&dirty_i->seglist_lock);
530 __remove_dirty_segment(sbi, new_segno, PRE);
531 __remove_dirty_segment(sbi, new_segno, DIRTY);
532 mutex_unlock(&dirty_i->seglist_lock);
534 reset_curseg(sbi, type, 1);
535 curseg->alloc_type = SSR;
536 __next_free_blkoff(sbi, curseg, 0);
539 sum_page = get_sum_page(sbi, new_segno);
540 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
541 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
542 f2fs_put_page(sum_page, 1);
546 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
548 struct curseg_info *curseg = CURSEG_I(sbi, type);
549 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
551 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
552 return v_ops->get_victim(sbi,
553 &(curseg)->next_segno, BG_GC, type, SSR);
555 /* For data segments, let's do SSR more intensively */
556 for (; type >= CURSEG_HOT_DATA; type--)
557 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
564 * flush out current segment and replace it with new segment
565 * This function should be returned with success, otherwise BUG
567 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
568 int type, bool force)
570 struct curseg_info *curseg = CURSEG_I(sbi, type);
573 new_curseg(sbi, type, true);
577 curseg->next_segno = check_prefree_segments(sbi, type);
579 if (curseg->next_segno != NULL_SEGNO)
580 change_curseg(sbi, type, false);
581 else if (type == CURSEG_WARM_NODE)
582 new_curseg(sbi, type, false);
583 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
584 change_curseg(sbi, type, true);
586 new_curseg(sbi, type, false);
588 sbi->segment_count[curseg->alloc_type]++;
591 void allocate_new_segments(struct f2fs_sb_info *sbi)
593 struct curseg_info *curseg;
594 unsigned int old_curseg;
597 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
598 curseg = CURSEG_I(sbi, i);
599 old_curseg = curseg->segno;
600 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
601 locate_dirty_segment(sbi, old_curseg);
605 static const struct segment_allocation default_salloc_ops = {
606 .allocate_segment = allocate_segment_by_default,
609 static void f2fs_end_io_write(struct bio *bio, int err)
611 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
612 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
613 struct bio_private *p = bio->bi_private;
616 struct page *page = bvec->bv_page;
618 if (--bvec >= bio->bi_io_vec)
619 prefetchw(&bvec->bv_page->flags);
623 set_bit(AS_EIO, &page->mapping->flags);
624 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
625 p->sbi->sb->s_flags |= MS_RDONLY;
627 end_page_writeback(page);
628 dec_page_count(p->sbi, F2FS_WRITEBACK);
629 } while (bvec >= bio->bi_io_vec);
637 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
640 struct bio_private *priv;
642 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
648 /* No failure on bio allocation */
649 bio = bio_alloc(GFP_NOIO, npages);
651 bio->bi_private = priv;
655 static void do_submit_bio(struct f2fs_sb_info *sbi,
656 enum page_type type, bool sync)
658 int rw = sync ? WRITE_SYNC : WRITE;
659 enum page_type btype = type > META ? META : type;
661 if (type >= META_FLUSH)
662 rw = WRITE_FLUSH_FUA;
664 if (sbi->bio[btype]) {
665 struct bio_private *p = sbi->bio[btype]->bi_private;
667 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
668 if (type == META_FLUSH) {
669 DECLARE_COMPLETION_ONSTACK(wait);
672 submit_bio(rw, sbi->bio[btype]);
673 wait_for_completion(&wait);
676 submit_bio(rw, sbi->bio[btype]);
678 sbi->bio[btype] = NULL;
682 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
684 down_write(&sbi->bio_sem);
685 do_submit_bio(sbi, type, sync);
686 up_write(&sbi->bio_sem);
689 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
690 block_t blk_addr, enum page_type type)
692 struct block_device *bdev = sbi->sb->s_bdev;
694 verify_block_addr(sbi, blk_addr);
696 down_write(&sbi->bio_sem);
698 inc_page_count(sbi, F2FS_WRITEBACK);
700 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
701 do_submit_bio(sbi, type, false);
703 if (sbi->bio[type] == NULL) {
704 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_get_nr_vecs(bdev));
705 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
707 * The end_io will be assigned at the sumbission phase.
708 * Until then, let bio_add_page() merge consecutive IOs as much
713 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
715 do_submit_bio(sbi, type, false);
719 sbi->last_block_in_bio[type] = blk_addr;
721 up_write(&sbi->bio_sem);
724 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
726 struct curseg_info *curseg = CURSEG_I(sbi, type);
727 if (curseg->next_blkoff < sbi->blocks_per_seg)
732 static int __get_segment_type_2(struct page *page, enum page_type p_type)
735 return CURSEG_HOT_DATA;
737 return CURSEG_HOT_NODE;
740 static int __get_segment_type_4(struct page *page, enum page_type p_type)
742 if (p_type == DATA) {
743 struct inode *inode = page->mapping->host;
745 if (S_ISDIR(inode->i_mode))
746 return CURSEG_HOT_DATA;
748 return CURSEG_COLD_DATA;
750 if (IS_DNODE(page) && !is_cold_node(page))
751 return CURSEG_HOT_NODE;
753 return CURSEG_COLD_NODE;
757 static int __get_segment_type_6(struct page *page, enum page_type p_type)
759 if (p_type == DATA) {
760 struct inode *inode = page->mapping->host;
762 if (S_ISDIR(inode->i_mode))
763 return CURSEG_HOT_DATA;
764 else if (is_cold_data(page) || is_cold_file(inode))
765 return CURSEG_COLD_DATA;
767 return CURSEG_WARM_DATA;
770 return is_cold_node(page) ? CURSEG_WARM_NODE :
773 return CURSEG_COLD_NODE;
777 static int __get_segment_type(struct page *page, enum page_type p_type)
779 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
780 switch (sbi->active_logs) {
782 return __get_segment_type_2(page, p_type);
784 return __get_segment_type_4(page, p_type);
786 /* NR_CURSEG_TYPE(6) logs by default */
787 BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
788 return __get_segment_type_6(page, p_type);
791 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
792 block_t old_blkaddr, block_t *new_blkaddr,
793 struct f2fs_summary *sum, enum page_type p_type)
795 struct sit_info *sit_i = SIT_I(sbi);
796 struct curseg_info *curseg;
797 unsigned int old_cursegno;
800 type = __get_segment_type(page, p_type);
801 curseg = CURSEG_I(sbi, type);
803 mutex_lock(&curseg->curseg_mutex);
805 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
806 old_cursegno = curseg->segno;
809 * __add_sum_entry should be resided under the curseg_mutex
810 * because, this function updates a summary entry in the
811 * current summary block.
813 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
815 mutex_lock(&sit_i->sentry_lock);
816 __refresh_next_blkoff(sbi, curseg);
817 sbi->block_count[curseg->alloc_type]++;
820 * SIT information should be updated before segment allocation,
821 * since SSR needs latest valid block information.
823 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
825 if (!__has_curseg_space(sbi, type))
826 sit_i->s_ops->allocate_segment(sbi, type, false);
828 locate_dirty_segment(sbi, old_cursegno);
829 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
830 mutex_unlock(&sit_i->sentry_lock);
833 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
835 /* writeout dirty page into bdev */
836 submit_write_page(sbi, page, *new_blkaddr, p_type);
838 mutex_unlock(&curseg->curseg_mutex);
841 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
843 set_page_writeback(page);
844 submit_write_page(sbi, page, page->index, META);
847 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
848 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
850 struct f2fs_summary sum;
851 set_summary(&sum, nid, 0, 0);
852 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
855 void write_data_page(struct inode *inode, struct page *page,
856 struct dnode_of_data *dn, block_t old_blkaddr,
857 block_t *new_blkaddr)
859 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
860 struct f2fs_summary sum;
863 BUG_ON(old_blkaddr == NULL_ADDR);
864 get_node_info(sbi, dn->nid, &ni);
865 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
867 do_write_page(sbi, page, old_blkaddr,
868 new_blkaddr, &sum, DATA);
871 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
872 block_t old_blk_addr)
874 submit_write_page(sbi, page, old_blk_addr, DATA);
877 void recover_data_page(struct f2fs_sb_info *sbi,
878 struct page *page, struct f2fs_summary *sum,
879 block_t old_blkaddr, block_t new_blkaddr)
881 struct sit_info *sit_i = SIT_I(sbi);
882 struct curseg_info *curseg;
883 unsigned int segno, old_cursegno;
884 struct seg_entry *se;
887 segno = GET_SEGNO(sbi, new_blkaddr);
888 se = get_seg_entry(sbi, segno);
891 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
892 if (old_blkaddr == NULL_ADDR)
893 type = CURSEG_COLD_DATA;
895 type = CURSEG_WARM_DATA;
897 curseg = CURSEG_I(sbi, type);
899 mutex_lock(&curseg->curseg_mutex);
900 mutex_lock(&sit_i->sentry_lock);
902 old_cursegno = curseg->segno;
904 /* change the current segment */
905 if (segno != curseg->segno) {
906 curseg->next_segno = segno;
907 change_curseg(sbi, type, true);
910 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
911 (sbi->blocks_per_seg - 1);
912 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
914 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
916 locate_dirty_segment(sbi, old_cursegno);
917 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
919 mutex_unlock(&sit_i->sentry_lock);
920 mutex_unlock(&curseg->curseg_mutex);
923 void rewrite_node_page(struct f2fs_sb_info *sbi,
924 struct page *page, struct f2fs_summary *sum,
925 block_t old_blkaddr, block_t new_blkaddr)
927 struct sit_info *sit_i = SIT_I(sbi);
928 int type = CURSEG_WARM_NODE;
929 struct curseg_info *curseg;
930 unsigned int segno, old_cursegno;
931 block_t next_blkaddr = next_blkaddr_of_node(page);
932 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
934 curseg = CURSEG_I(sbi, type);
936 mutex_lock(&curseg->curseg_mutex);
937 mutex_lock(&sit_i->sentry_lock);
939 segno = GET_SEGNO(sbi, new_blkaddr);
940 old_cursegno = curseg->segno;
942 /* change the current segment */
943 if (segno != curseg->segno) {
944 curseg->next_segno = segno;
945 change_curseg(sbi, type, true);
947 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
948 (sbi->blocks_per_seg - 1);
949 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
951 /* change the current log to the next block addr in advance */
952 if (next_segno != segno) {
953 curseg->next_segno = next_segno;
954 change_curseg(sbi, type, true);
956 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
957 (sbi->blocks_per_seg - 1);
959 /* rewrite node page */
960 set_page_writeback(page);
961 submit_write_page(sbi, page, new_blkaddr, NODE);
962 f2fs_submit_bio(sbi, NODE, true);
963 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
965 locate_dirty_segment(sbi, old_cursegno);
966 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
968 mutex_unlock(&sit_i->sentry_lock);
969 mutex_unlock(&curseg->curseg_mutex);
972 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
974 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
975 struct curseg_info *seg_i;
976 unsigned char *kaddr;
981 start = start_sum_block(sbi);
983 page = get_meta_page(sbi, start++);
984 kaddr = (unsigned char *)page_address(page);
986 /* Step 1: restore nat cache */
987 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
988 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
990 /* Step 2: restore sit cache */
991 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
992 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
994 offset = 2 * SUM_JOURNAL_SIZE;
996 /* Step 3: restore summary entries */
997 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
998 unsigned short blk_off;
1001 seg_i = CURSEG_I(sbi, i);
1002 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1003 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1004 seg_i->next_segno = segno;
1005 reset_curseg(sbi, i, 0);
1006 seg_i->alloc_type = ckpt->alloc_type[i];
1007 seg_i->next_blkoff = blk_off;
1009 if (seg_i->alloc_type == SSR)
1010 blk_off = sbi->blocks_per_seg;
1012 for (j = 0; j < blk_off; j++) {
1013 struct f2fs_summary *s;
1014 s = (struct f2fs_summary *)(kaddr + offset);
1015 seg_i->sum_blk->entries[j] = *s;
1016 offset += SUMMARY_SIZE;
1017 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1021 f2fs_put_page(page, 1);
1024 page = get_meta_page(sbi, start++);
1025 kaddr = (unsigned char *)page_address(page);
1029 f2fs_put_page(page, 1);
1033 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1035 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1036 struct f2fs_summary_block *sum;
1037 struct curseg_info *curseg;
1039 unsigned short blk_off;
1040 unsigned int segno = 0;
1041 block_t blk_addr = 0;
1043 /* get segment number and block addr */
1044 if (IS_DATASEG(type)) {
1045 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1046 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1048 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1049 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1051 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1053 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1055 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1057 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1058 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1059 type - CURSEG_HOT_NODE);
1061 blk_addr = GET_SUM_BLOCK(sbi, segno);
1064 new = get_meta_page(sbi, blk_addr);
1065 sum = (struct f2fs_summary_block *)page_address(new);
1067 if (IS_NODESEG(type)) {
1068 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1069 struct f2fs_summary *ns = &sum->entries[0];
1071 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1073 ns->ofs_in_node = 0;
1076 if (restore_node_summary(sbi, segno, sum)) {
1077 f2fs_put_page(new, 1);
1083 /* set uncompleted segment to curseg */
1084 curseg = CURSEG_I(sbi, type);
1085 mutex_lock(&curseg->curseg_mutex);
1086 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1087 curseg->next_segno = segno;
1088 reset_curseg(sbi, type, 0);
1089 curseg->alloc_type = ckpt->alloc_type[type];
1090 curseg->next_blkoff = blk_off;
1091 mutex_unlock(&curseg->curseg_mutex);
1092 f2fs_put_page(new, 1);
1096 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1098 int type = CURSEG_HOT_DATA;
1100 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1101 /* restore for compacted data summary */
1102 if (read_compacted_summaries(sbi))
1104 type = CURSEG_HOT_NODE;
1107 for (; type <= CURSEG_COLD_NODE; type++)
1108 if (read_normal_summaries(sbi, type))
1113 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1116 unsigned char *kaddr;
1117 struct f2fs_summary *summary;
1118 struct curseg_info *seg_i;
1119 int written_size = 0;
1122 page = grab_meta_page(sbi, blkaddr++);
1123 kaddr = (unsigned char *)page_address(page);
1125 /* Step 1: write nat cache */
1126 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1127 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1128 written_size += SUM_JOURNAL_SIZE;
1130 /* Step 2: write sit cache */
1131 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1132 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1134 written_size += SUM_JOURNAL_SIZE;
1136 set_page_dirty(page);
1138 /* Step 3: write summary entries */
1139 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1140 unsigned short blkoff;
1141 seg_i = CURSEG_I(sbi, i);
1142 if (sbi->ckpt->alloc_type[i] == SSR)
1143 blkoff = sbi->blocks_per_seg;
1145 blkoff = curseg_blkoff(sbi, i);
1147 for (j = 0; j < blkoff; j++) {
1149 page = grab_meta_page(sbi, blkaddr++);
1150 kaddr = (unsigned char *)page_address(page);
1153 summary = (struct f2fs_summary *)(kaddr + written_size);
1154 *summary = seg_i->sum_blk->entries[j];
1155 written_size += SUMMARY_SIZE;
1156 set_page_dirty(page);
1158 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1162 f2fs_put_page(page, 1);
1167 f2fs_put_page(page, 1);
1170 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1171 block_t blkaddr, int type)
1174 if (IS_DATASEG(type))
1175 end = type + NR_CURSEG_DATA_TYPE;
1177 end = type + NR_CURSEG_NODE_TYPE;
1179 for (i = type; i < end; i++) {
1180 struct curseg_info *sum = CURSEG_I(sbi, i);
1181 mutex_lock(&sum->curseg_mutex);
1182 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1183 mutex_unlock(&sum->curseg_mutex);
1187 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1189 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1190 write_compacted_summaries(sbi, start_blk);
1192 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1195 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1197 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1198 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1202 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1203 unsigned int val, int alloc)
1207 if (type == NAT_JOURNAL) {
1208 for (i = 0; i < nats_in_cursum(sum); i++) {
1209 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1212 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1213 return update_nats_in_cursum(sum, 1);
1214 } else if (type == SIT_JOURNAL) {
1215 for (i = 0; i < sits_in_cursum(sum); i++)
1216 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1218 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1219 return update_sits_in_cursum(sum, 1);
1224 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1227 struct sit_info *sit_i = SIT_I(sbi);
1228 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1229 block_t blk_addr = sit_i->sit_base_addr + offset;
1231 check_seg_range(sbi, segno);
1233 /* calculate sit block address */
1234 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1235 blk_addr += sit_i->sit_blocks;
1237 return get_meta_page(sbi, blk_addr);
1240 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1243 struct sit_info *sit_i = SIT_I(sbi);
1244 struct page *src_page, *dst_page;
1245 pgoff_t src_off, dst_off;
1246 void *src_addr, *dst_addr;
1248 src_off = current_sit_addr(sbi, start);
1249 dst_off = next_sit_addr(sbi, src_off);
1251 /* get current sit block page without lock */
1252 src_page = get_meta_page(sbi, src_off);
1253 dst_page = grab_meta_page(sbi, dst_off);
1254 BUG_ON(PageDirty(src_page));
1256 src_addr = page_address(src_page);
1257 dst_addr = page_address(dst_page);
1258 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1260 set_page_dirty(dst_page);
1261 f2fs_put_page(src_page, 1);
1263 set_to_next_sit(sit_i, start);
1268 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1270 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1271 struct f2fs_summary_block *sum = curseg->sum_blk;
1275 * If the journal area in the current summary is full of sit entries,
1276 * all the sit entries will be flushed. Otherwise the sit entries
1277 * are not able to replace with newly hot sit entries.
1279 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1280 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1282 segno = le32_to_cpu(segno_in_journal(sum, i));
1283 __mark_sit_entry_dirty(sbi, segno);
1285 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1292 * CP calls this function, which flushes SIT entries including sit_journal,
1293 * and moves prefree segs to free segs.
1295 void flush_sit_entries(struct f2fs_sb_info *sbi)
1297 struct sit_info *sit_i = SIT_I(sbi);
1298 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1299 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1300 struct f2fs_summary_block *sum = curseg->sum_blk;
1301 unsigned long nsegs = TOTAL_SEGS(sbi);
1302 struct page *page = NULL;
1303 struct f2fs_sit_block *raw_sit = NULL;
1304 unsigned int start = 0, end = 0;
1305 unsigned int segno = -1;
1308 mutex_lock(&curseg->curseg_mutex);
1309 mutex_lock(&sit_i->sentry_lock);
1312 * "flushed" indicates whether sit entries in journal are flushed
1313 * to the SIT area or not.
1315 flushed = flush_sits_in_journal(sbi);
1317 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1318 struct seg_entry *se = get_seg_entry(sbi, segno);
1319 int sit_offset, offset;
1321 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1326 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1328 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1329 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1333 if (!page || (start > segno) || (segno > end)) {
1335 f2fs_put_page(page, 1);
1339 start = START_SEGNO(sit_i, segno);
1340 end = start + SIT_ENTRY_PER_BLOCK - 1;
1342 /* read sit block that will be updated */
1343 page = get_next_sit_page(sbi, start);
1344 raw_sit = page_address(page);
1347 /* udpate entry in SIT block */
1348 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1350 __clear_bit(segno, bitmap);
1351 sit_i->dirty_sentries--;
1353 mutex_unlock(&sit_i->sentry_lock);
1354 mutex_unlock(&curseg->curseg_mutex);
1356 /* writeout last modified SIT block */
1357 f2fs_put_page(page, 1);
1359 set_prefree_as_free_segments(sbi);
1362 static int build_sit_info(struct f2fs_sb_info *sbi)
1364 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1365 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1366 struct sit_info *sit_i;
1367 unsigned int sit_segs, start;
1368 char *src_bitmap, *dst_bitmap;
1369 unsigned int bitmap_size;
1371 /* allocate memory for SIT information */
1372 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1376 SM_I(sbi)->sit_info = sit_i;
1378 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1379 if (!sit_i->sentries)
1382 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1383 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1384 if (!sit_i->dirty_sentries_bitmap)
1387 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1388 sit_i->sentries[start].cur_valid_map
1389 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1390 sit_i->sentries[start].ckpt_valid_map
1391 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1392 if (!sit_i->sentries[start].cur_valid_map
1393 || !sit_i->sentries[start].ckpt_valid_map)
1397 if (sbi->segs_per_sec > 1) {
1398 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1399 sizeof(struct sec_entry));
1400 if (!sit_i->sec_entries)
1404 /* get information related with SIT */
1405 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1407 /* setup SIT bitmap from ckeckpoint pack */
1408 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1409 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1411 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1415 /* init SIT information */
1416 sit_i->s_ops = &default_salloc_ops;
1418 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1419 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1420 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1421 sit_i->sit_bitmap = dst_bitmap;
1422 sit_i->bitmap_size = bitmap_size;
1423 sit_i->dirty_sentries = 0;
1424 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1425 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1426 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1427 mutex_init(&sit_i->sentry_lock);
1431 static int build_free_segmap(struct f2fs_sb_info *sbi)
1433 struct f2fs_sm_info *sm_info = SM_I(sbi);
1434 struct free_segmap_info *free_i;
1435 unsigned int bitmap_size, sec_bitmap_size;
1437 /* allocate memory for free segmap information */
1438 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1442 SM_I(sbi)->free_info = free_i;
1444 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1445 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1446 if (!free_i->free_segmap)
1449 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1450 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1451 if (!free_i->free_secmap)
1454 /* set all segments as dirty temporarily */
1455 memset(free_i->free_segmap, 0xff, bitmap_size);
1456 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1458 /* init free segmap information */
1459 free_i->start_segno =
1460 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1461 free_i->free_segments = 0;
1462 free_i->free_sections = 0;
1463 rwlock_init(&free_i->segmap_lock);
1467 static int build_curseg(struct f2fs_sb_info *sbi)
1469 struct curseg_info *array;
1472 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1476 SM_I(sbi)->curseg_array = array;
1478 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1479 mutex_init(&array[i].curseg_mutex);
1480 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1481 if (!array[i].sum_blk)
1483 array[i].segno = NULL_SEGNO;
1484 array[i].next_blkoff = 0;
1486 return restore_curseg_summaries(sbi);
1489 static void build_sit_entries(struct f2fs_sb_info *sbi)
1491 struct sit_info *sit_i = SIT_I(sbi);
1492 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1493 struct f2fs_summary_block *sum = curseg->sum_blk;
1496 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1497 struct seg_entry *se = &sit_i->sentries[start];
1498 struct f2fs_sit_block *sit_blk;
1499 struct f2fs_sit_entry sit;
1503 mutex_lock(&curseg->curseg_mutex);
1504 for (i = 0; i < sits_in_cursum(sum); i++) {
1505 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1506 sit = sit_in_journal(sum, i);
1507 mutex_unlock(&curseg->curseg_mutex);
1511 mutex_unlock(&curseg->curseg_mutex);
1512 page = get_current_sit_page(sbi, start);
1513 sit_blk = (struct f2fs_sit_block *)page_address(page);
1514 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1515 f2fs_put_page(page, 1);
1517 check_block_count(sbi, start, &sit);
1518 seg_info_from_raw_sit(se, &sit);
1519 if (sbi->segs_per_sec > 1) {
1520 struct sec_entry *e = get_sec_entry(sbi, start);
1521 e->valid_blocks += se->valid_blocks;
1526 static void init_free_segmap(struct f2fs_sb_info *sbi)
1531 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1532 struct seg_entry *sentry = get_seg_entry(sbi, start);
1533 if (!sentry->valid_blocks)
1534 __set_free(sbi, start);
1537 /* set use the current segments */
1538 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1539 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1540 __set_test_and_inuse(sbi, curseg_t->segno);
1544 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1546 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1547 struct free_segmap_info *free_i = FREE_I(sbi);
1548 unsigned int segno = 0, offset = 0;
1549 unsigned short valid_blocks;
1551 while (segno < TOTAL_SEGS(sbi)) {
1552 /* find dirty segment based on free segmap */
1553 segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1554 if (segno >= TOTAL_SEGS(sbi))
1557 valid_blocks = get_valid_blocks(sbi, segno, 0);
1558 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1560 mutex_lock(&dirty_i->seglist_lock);
1561 __locate_dirty_segment(sbi, segno, DIRTY);
1562 mutex_unlock(&dirty_i->seglist_lock);
1566 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1568 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1569 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1571 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1572 if (!dirty_i->victim_secmap)
1577 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1579 struct dirty_seglist_info *dirty_i;
1580 unsigned int bitmap_size, i;
1582 /* allocate memory for dirty segments list information */
1583 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1587 SM_I(sbi)->dirty_info = dirty_i;
1588 mutex_init(&dirty_i->seglist_lock);
1590 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1592 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1593 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1594 if (!dirty_i->dirty_segmap[i])
1598 init_dirty_segmap(sbi);
1599 return init_victim_secmap(sbi);
1603 * Update min, max modified time for cost-benefit GC algorithm
1605 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1607 struct sit_info *sit_i = SIT_I(sbi);
1610 mutex_lock(&sit_i->sentry_lock);
1612 sit_i->min_mtime = LLONG_MAX;
1614 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1616 unsigned long long mtime = 0;
1618 for (i = 0; i < sbi->segs_per_sec; i++)
1619 mtime += get_seg_entry(sbi, segno + i)->mtime;
1621 mtime = div_u64(mtime, sbi->segs_per_sec);
1623 if (sit_i->min_mtime > mtime)
1624 sit_i->min_mtime = mtime;
1626 sit_i->max_mtime = get_mtime(sbi);
1627 mutex_unlock(&sit_i->sentry_lock);
1630 int build_segment_manager(struct f2fs_sb_info *sbi)
1632 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1633 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1634 struct f2fs_sm_info *sm_info;
1637 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1642 sbi->sm_info = sm_info;
1643 INIT_LIST_HEAD(&sm_info->wblist_head);
1644 spin_lock_init(&sm_info->wblist_lock);
1645 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1646 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1647 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1648 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1649 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1650 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1651 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1653 err = build_sit_info(sbi);
1656 err = build_free_segmap(sbi);
1659 err = build_curseg(sbi);
1663 /* reinit free segmap based on SIT */
1664 build_sit_entries(sbi);
1666 init_free_segmap(sbi);
1667 err = build_dirty_segmap(sbi);
1671 init_min_max_mtime(sbi);
1675 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1676 enum dirty_type dirty_type)
1678 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1680 mutex_lock(&dirty_i->seglist_lock);
1681 kfree(dirty_i->dirty_segmap[dirty_type]);
1682 dirty_i->nr_dirty[dirty_type] = 0;
1683 mutex_unlock(&dirty_i->seglist_lock);
1686 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1688 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1689 kfree(dirty_i->victim_secmap);
1692 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1694 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1700 /* discard pre-free/dirty segments list */
1701 for (i = 0; i < NR_DIRTY_TYPE; i++)
1702 discard_dirty_segmap(sbi, i);
1704 destroy_victim_secmap(sbi);
1705 SM_I(sbi)->dirty_info = NULL;
1709 static void destroy_curseg(struct f2fs_sb_info *sbi)
1711 struct curseg_info *array = SM_I(sbi)->curseg_array;
1716 SM_I(sbi)->curseg_array = NULL;
1717 for (i = 0; i < NR_CURSEG_TYPE; i++)
1718 kfree(array[i].sum_blk);
1722 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1724 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1727 SM_I(sbi)->free_info = NULL;
1728 kfree(free_i->free_segmap);
1729 kfree(free_i->free_secmap);
1733 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1735 struct sit_info *sit_i = SIT_I(sbi);
1741 if (sit_i->sentries) {
1742 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1743 kfree(sit_i->sentries[start].cur_valid_map);
1744 kfree(sit_i->sentries[start].ckpt_valid_map);
1747 vfree(sit_i->sentries);
1748 vfree(sit_i->sec_entries);
1749 kfree(sit_i->dirty_sentries_bitmap);
1751 SM_I(sbi)->sit_info = NULL;
1752 kfree(sit_i->sit_bitmap);
1756 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1758 struct f2fs_sm_info *sm_info = SM_I(sbi);
1759 destroy_dirty_segmap(sbi);
1760 destroy_curseg(sbi);
1761 destroy_free_segmap(sbi);
1762 destroy_sit_info(sbi);
1763 sbi->sm_info = NULL;