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)) {
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 clear_bit(segno, dirty_i->victim_segmap[FG_GC]);
73 clear_bit(segno, dirty_i->victim_segmap[BG_GC]);
78 * Should not occur error such as -ENOMEM.
79 * Adding dirty entry into seglist is not critical operation.
80 * If a given segment is one of current working segments, it won't be added.
82 void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
84 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
85 unsigned short valid_blocks;
87 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
90 mutex_lock(&dirty_i->seglist_lock);
92 valid_blocks = get_valid_blocks(sbi, segno, 0);
94 if (valid_blocks == 0) {
95 __locate_dirty_segment(sbi, segno, PRE);
96 __remove_dirty_segment(sbi, segno, DIRTY);
97 } else if (valid_blocks < sbi->blocks_per_seg) {
98 __locate_dirty_segment(sbi, segno, DIRTY);
100 /* Recovery routine with SSR needs this */
101 __remove_dirty_segment(sbi, segno, DIRTY);
104 mutex_unlock(&dirty_i->seglist_lock);
109 * Should call clear_prefree_segments after checkpoint is done.
111 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
113 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
114 unsigned int segno, offset = 0;
115 unsigned int total_segs = TOTAL_SEGS(sbi);
117 mutex_lock(&dirty_i->seglist_lock);
119 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
121 if (segno >= total_segs)
123 __set_test_and_free(sbi, segno);
126 mutex_unlock(&dirty_i->seglist_lock);
129 void clear_prefree_segments(struct f2fs_sb_info *sbi)
131 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
132 unsigned int segno, offset = 0;
133 unsigned int total_segs = TOTAL_SEGS(sbi);
135 mutex_lock(&dirty_i->seglist_lock);
137 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
139 if (segno >= total_segs)
143 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
144 dirty_i->nr_dirty[PRE]--;
147 if (test_opt(sbi, DISCARD))
148 blkdev_issue_discard(sbi->sb->s_bdev,
149 START_BLOCK(sbi, segno) <<
150 sbi->log_sectors_per_block,
151 1 << (sbi->log_sectors_per_block +
152 sbi->log_blocks_per_seg),
155 mutex_unlock(&dirty_i->seglist_lock);
158 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
160 struct sit_info *sit_i = SIT_I(sbi);
161 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
162 sit_i->dirty_sentries++;
165 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
166 unsigned int segno, int modified)
168 struct seg_entry *se = get_seg_entry(sbi, segno);
171 __mark_sit_entry_dirty(sbi, segno);
174 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
176 struct seg_entry *se;
177 unsigned int segno, offset;
178 long int new_vblocks;
180 segno = GET_SEGNO(sbi, blkaddr);
182 se = get_seg_entry(sbi, segno);
183 new_vblocks = se->valid_blocks + del;
184 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
186 BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
187 (new_vblocks > sbi->blocks_per_seg)));
189 se->valid_blocks = new_vblocks;
190 se->mtime = get_mtime(sbi);
191 SIT_I(sbi)->max_mtime = se->mtime;
193 /* Update valid block bitmap */
195 if (f2fs_set_bit(offset, se->cur_valid_map))
198 if (!f2fs_clear_bit(offset, se->cur_valid_map))
201 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
202 se->ckpt_valid_blocks += del;
204 __mark_sit_entry_dirty(sbi, segno);
206 /* update total number of valid blocks to be written in ckpt area */
207 SIT_I(sbi)->written_valid_blocks += del;
209 if (sbi->segs_per_sec > 1)
210 get_sec_entry(sbi, segno)->valid_blocks += del;
213 static void refresh_sit_entry(struct f2fs_sb_info *sbi,
214 block_t old_blkaddr, block_t new_blkaddr)
216 update_sit_entry(sbi, new_blkaddr, 1);
217 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
218 update_sit_entry(sbi, old_blkaddr, -1);
221 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
223 unsigned int segno = GET_SEGNO(sbi, addr);
224 struct sit_info *sit_i = SIT_I(sbi);
226 BUG_ON(addr == NULL_ADDR);
227 if (addr == NEW_ADDR)
230 /* add it into sit main buffer */
231 mutex_lock(&sit_i->sentry_lock);
233 update_sit_entry(sbi, addr, -1);
235 /* add it into dirty seglist */
236 locate_dirty_segment(sbi, segno);
238 mutex_unlock(&sit_i->sentry_lock);
242 * This function should be resided under the curseg_mutex lock
244 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
245 struct f2fs_summary *sum, unsigned short offset)
247 struct curseg_info *curseg = CURSEG_I(sbi, type);
248 void *addr = curseg->sum_blk;
249 addr += offset * sizeof(struct f2fs_summary);
250 memcpy(addr, sum, sizeof(struct f2fs_summary));
255 * Calculate the number of current summary pages for writing
257 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
259 int total_size_bytes = 0;
260 int valid_sum_count = 0;
263 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
264 if (sbi->ckpt->alloc_type[i] == SSR)
265 valid_sum_count += sbi->blocks_per_seg;
267 valid_sum_count += curseg_blkoff(sbi, i);
270 total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
271 + sizeof(struct nat_journal) + 2
272 + sizeof(struct sit_journal) + 2;
273 sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
274 if (total_size_bytes < sum_space)
276 else if (total_size_bytes < 2 * sum_space)
282 * Caller should put this summary page
284 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
286 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
289 static void write_sum_page(struct f2fs_sb_info *sbi,
290 struct f2fs_summary_block *sum_blk, block_t blk_addr)
292 struct page *page = grab_meta_page(sbi, blk_addr);
293 void *kaddr = page_address(page);
294 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
295 set_page_dirty(page);
296 f2fs_put_page(page, 1);
299 static unsigned int check_prefree_segments(struct f2fs_sb_info *sbi,
300 int ofs_unit, int type)
302 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
303 unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
304 unsigned int segno, next_segno, i;
308 * If there is not enough reserved sections,
309 * we should not reuse prefree segments.
311 if (has_not_enough_free_secs(sbi))
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 = ((segno / ofs_unit) * ofs_unit) + ofs_unit;
323 if (segno < TOTAL_SEGS(sbi)) {
324 /* skip intermediate segments in a section */
325 if (segno % ofs_unit)
328 /* skip if whole section is not prefree */
329 next_segno = find_next_zero_bit(prefree_segmap,
330 TOTAL_SEGS(sbi), segno + 1);
331 if (next_segno - segno < ofs_unit)
334 /* skip if whole section was not free at the last checkpoint */
335 for (i = 0; i < ofs_unit; i++)
336 if (get_seg_entry(sbi, segno)->ckpt_valid_blocks)
344 * Find a new segment from the free segments bitmap to right order
345 * This function should be returned with success, otherwise BUG
347 static void get_new_segment(struct f2fs_sb_info *sbi,
348 unsigned int *newseg, bool new_sec, int dir)
350 struct free_segmap_info *free_i = FREE_I(sbi);
351 unsigned int total_secs = sbi->total_sections;
352 unsigned int segno, secno, zoneno;
353 unsigned int total_zones = sbi->total_sections / sbi->secs_per_zone;
354 unsigned int hint = *newseg / sbi->segs_per_sec;
355 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
356 unsigned int left_start = hint;
361 write_lock(&free_i->segmap_lock);
363 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
364 segno = find_next_zero_bit(free_i->free_segmap,
365 TOTAL_SEGS(sbi), *newseg + 1);
366 if (segno < TOTAL_SEGS(sbi))
370 secno = find_next_zero_bit(free_i->free_secmap, total_secs, hint);
371 if (secno >= total_secs) {
372 if (dir == ALLOC_RIGHT) {
373 secno = find_next_zero_bit(free_i->free_secmap,
375 BUG_ON(secno >= total_secs);
378 left_start = hint - 1;
384 while (test_bit(left_start, free_i->free_secmap)) {
385 if (left_start > 0) {
389 left_start = find_next_zero_bit(free_i->free_secmap,
391 BUG_ON(left_start >= total_secs);
397 segno = secno * sbi->segs_per_sec;
398 zoneno = secno / sbi->secs_per_zone;
400 /* give up on finding another zone */
403 if (sbi->secs_per_zone == 1)
405 if (zoneno == old_zoneno)
407 if (dir == ALLOC_LEFT) {
408 if (!go_left && zoneno + 1 >= total_zones)
410 if (go_left && zoneno == 0)
413 for (i = 0; i < NR_CURSEG_TYPE; i++)
414 if (CURSEG_I(sbi, i)->zone == zoneno)
417 if (i < NR_CURSEG_TYPE) {
418 /* zone is in user, try another */
420 hint = zoneno * sbi->secs_per_zone - 1;
421 else if (zoneno + 1 >= total_zones)
424 hint = (zoneno + 1) * sbi->secs_per_zone;
426 goto find_other_zone;
429 /* set it as dirty segment in free segmap */
430 BUG_ON(test_bit(segno, free_i->free_segmap));
431 __set_inuse(sbi, segno);
433 write_unlock(&free_i->segmap_lock);
436 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
438 struct curseg_info *curseg = CURSEG_I(sbi, type);
439 struct summary_footer *sum_footer;
441 curseg->segno = curseg->next_segno;
442 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
443 curseg->next_blkoff = 0;
444 curseg->next_segno = NULL_SEGNO;
446 sum_footer = &(curseg->sum_blk->footer);
447 memset(sum_footer, 0, sizeof(struct summary_footer));
448 if (IS_DATASEG(type))
449 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
450 if (IS_NODESEG(type))
451 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
452 __set_sit_entry_type(sbi, type, curseg->segno, modified);
456 * Allocate a current working segment.
457 * This function always allocates a free segment in LFS manner.
459 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
461 struct curseg_info *curseg = CURSEG_I(sbi, type);
462 unsigned int segno = curseg->segno;
463 int dir = ALLOC_LEFT;
465 write_sum_page(sbi, curseg->sum_blk,
466 GET_SUM_BLOCK(sbi, curseg->segno));
467 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
470 if (test_opt(sbi, NOHEAP))
473 get_new_segment(sbi, &segno, new_sec, dir);
474 curseg->next_segno = segno;
475 reset_curseg(sbi, type, 1);
476 curseg->alloc_type = LFS;
479 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
480 struct curseg_info *seg, block_t start)
482 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
484 for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
485 if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
486 && !f2fs_test_bit(ofs, se->cur_valid_map))
489 seg->next_blkoff = ofs;
493 * If a segment is written by LFS manner, next block offset is just obtained
494 * by increasing the current block offset. However, if a segment is written by
495 * SSR manner, next block offset obtained by calling __next_free_blkoff
497 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
498 struct curseg_info *seg)
500 if (seg->alloc_type == SSR)
501 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
507 * This function always allocates a used segment (from dirty seglist) by SSR
508 * manner, so it should recover the existing segment information of valid blocks
510 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
512 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
513 struct curseg_info *curseg = CURSEG_I(sbi, type);
514 unsigned int new_segno = curseg->next_segno;
515 struct f2fs_summary_block *sum_node;
516 struct page *sum_page;
518 write_sum_page(sbi, curseg->sum_blk,
519 GET_SUM_BLOCK(sbi, curseg->segno));
520 __set_test_and_inuse(sbi, new_segno);
522 mutex_lock(&dirty_i->seglist_lock);
523 __remove_dirty_segment(sbi, new_segno, PRE);
524 __remove_dirty_segment(sbi, new_segno, DIRTY);
525 mutex_unlock(&dirty_i->seglist_lock);
527 reset_curseg(sbi, type, 1);
528 curseg->alloc_type = SSR;
529 __next_free_blkoff(sbi, curseg, 0);
532 sum_page = get_sum_page(sbi, new_segno);
533 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
534 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
535 f2fs_put_page(sum_page, 1);
540 * flush out current segment and replace it with new segment
541 * This function should be returned with success, otherwise BUG
543 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
544 int type, bool force)
546 struct curseg_info *curseg = CURSEG_I(sbi, type);
547 unsigned int ofs_unit;
550 new_curseg(sbi, type, true);
554 ofs_unit = need_SSR(sbi) ? 1 : sbi->segs_per_sec;
555 curseg->next_segno = check_prefree_segments(sbi, ofs_unit, type);
557 if (curseg->next_segno != NULL_SEGNO)
558 change_curseg(sbi, type, false);
559 else if (type == CURSEG_WARM_NODE)
560 new_curseg(sbi, type, false);
561 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
562 change_curseg(sbi, type, true);
564 new_curseg(sbi, type, false);
566 sbi->segment_count[curseg->alloc_type]++;
569 void allocate_new_segments(struct f2fs_sb_info *sbi)
571 struct curseg_info *curseg;
572 unsigned int old_curseg;
575 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
576 curseg = CURSEG_I(sbi, i);
577 old_curseg = curseg->segno;
578 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
579 locate_dirty_segment(sbi, old_curseg);
583 static const struct segment_allocation default_salloc_ops = {
584 .allocate_segment = allocate_segment_by_default,
587 static void f2fs_end_io_write(struct bio *bio, int err)
589 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
590 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
591 struct bio_private *p = bio->bi_private;
594 struct page *page = bvec->bv_page;
596 if (--bvec >= bio->bi_io_vec)
597 prefetchw(&bvec->bv_page->flags);
601 set_bit(AS_EIO, &page->mapping->flags);
602 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
603 p->sbi->sb->s_flags |= MS_RDONLY;
605 end_page_writeback(page);
606 dec_page_count(p->sbi, F2FS_WRITEBACK);
607 } while (bvec >= bio->bi_io_vec);
615 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
618 struct bio_private *priv;
620 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
626 /* No failure on bio allocation */
627 bio = bio_alloc(GFP_NOIO, npages);
629 bio->bi_private = priv;
633 static void do_submit_bio(struct f2fs_sb_info *sbi,
634 enum page_type type, bool sync)
636 int rw = sync ? WRITE_SYNC : WRITE;
637 enum page_type btype = type > META ? META : type;
639 if (type >= META_FLUSH)
640 rw = WRITE_FLUSH_FUA;
642 if (sbi->bio[btype]) {
643 struct bio_private *p = sbi->bio[btype]->bi_private;
645 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
646 if (type == META_FLUSH) {
647 DECLARE_COMPLETION_ONSTACK(wait);
650 submit_bio(rw, sbi->bio[btype]);
651 wait_for_completion(&wait);
654 submit_bio(rw, sbi->bio[btype]);
656 sbi->bio[btype] = NULL;
660 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
662 down_write(&sbi->bio_sem);
663 do_submit_bio(sbi, type, sync);
664 up_write(&sbi->bio_sem);
667 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
668 block_t blk_addr, enum page_type type)
670 struct block_device *bdev = sbi->sb->s_bdev;
672 verify_block_addr(sbi, blk_addr);
674 down_write(&sbi->bio_sem);
676 inc_page_count(sbi, F2FS_WRITEBACK);
678 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
679 do_submit_bio(sbi, type, false);
681 if (sbi->bio[type] == NULL) {
682 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_get_nr_vecs(bdev));
683 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
685 * The end_io will be assigned at the sumbission phase.
686 * Until then, let bio_add_page() merge consecutive IOs as much
691 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
693 do_submit_bio(sbi, type, false);
697 sbi->last_block_in_bio[type] = blk_addr;
699 up_write(&sbi->bio_sem);
702 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
704 struct curseg_info *curseg = CURSEG_I(sbi, type);
705 if (curseg->next_blkoff < sbi->blocks_per_seg)
710 static int __get_segment_type_2(struct page *page, enum page_type p_type)
713 return CURSEG_HOT_DATA;
715 return CURSEG_HOT_NODE;
718 static int __get_segment_type_4(struct page *page, enum page_type p_type)
720 if (p_type == DATA) {
721 struct inode *inode = page->mapping->host;
723 if (S_ISDIR(inode->i_mode))
724 return CURSEG_HOT_DATA;
726 return CURSEG_COLD_DATA;
728 if (IS_DNODE(page) && !is_cold_node(page))
729 return CURSEG_HOT_NODE;
731 return CURSEG_COLD_NODE;
735 static int __get_segment_type_6(struct page *page, enum page_type p_type)
737 if (p_type == DATA) {
738 struct inode *inode = page->mapping->host;
740 if (S_ISDIR(inode->i_mode))
741 return CURSEG_HOT_DATA;
742 else if (is_cold_data(page) || is_cold_file(inode))
743 return CURSEG_COLD_DATA;
745 return CURSEG_WARM_DATA;
748 return is_cold_node(page) ? CURSEG_WARM_NODE :
751 return CURSEG_COLD_NODE;
755 static int __get_segment_type(struct page *page, enum page_type p_type)
757 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
758 switch (sbi->active_logs) {
760 return __get_segment_type_2(page, p_type);
762 return __get_segment_type_4(page, p_type);
764 /* NR_CURSEG_TYPE(6) logs by default */
765 BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
766 return __get_segment_type_6(page, p_type);
769 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
770 block_t old_blkaddr, block_t *new_blkaddr,
771 struct f2fs_summary *sum, enum page_type p_type)
773 struct sit_info *sit_i = SIT_I(sbi);
774 struct curseg_info *curseg;
775 unsigned int old_cursegno;
778 type = __get_segment_type(page, p_type);
779 curseg = CURSEG_I(sbi, type);
781 mutex_lock(&curseg->curseg_mutex);
783 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
784 old_cursegno = curseg->segno;
787 * __add_sum_entry should be resided under the curseg_mutex
788 * because, this function updates a summary entry in the
789 * current summary block.
791 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
793 mutex_lock(&sit_i->sentry_lock);
794 __refresh_next_blkoff(sbi, curseg);
795 sbi->block_count[curseg->alloc_type]++;
798 * SIT information should be updated before segment allocation,
799 * since SSR needs latest valid block information.
801 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
803 if (!__has_curseg_space(sbi, type))
804 sit_i->s_ops->allocate_segment(sbi, type, false);
806 locate_dirty_segment(sbi, old_cursegno);
807 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
808 mutex_unlock(&sit_i->sentry_lock);
811 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
813 /* writeout dirty page into bdev */
814 submit_write_page(sbi, page, *new_blkaddr, p_type);
816 mutex_unlock(&curseg->curseg_mutex);
819 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
821 set_page_writeback(page);
822 submit_write_page(sbi, page, page->index, META);
825 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
826 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
828 struct f2fs_summary sum;
829 set_summary(&sum, nid, 0, 0);
830 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
833 void write_data_page(struct inode *inode, struct page *page,
834 struct dnode_of_data *dn, block_t old_blkaddr,
835 block_t *new_blkaddr)
837 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
838 struct f2fs_summary sum;
841 BUG_ON(old_blkaddr == NULL_ADDR);
842 get_node_info(sbi, dn->nid, &ni);
843 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
845 do_write_page(sbi, page, old_blkaddr,
846 new_blkaddr, &sum, DATA);
849 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
850 block_t old_blk_addr)
852 submit_write_page(sbi, page, old_blk_addr, DATA);
855 void recover_data_page(struct f2fs_sb_info *sbi,
856 struct page *page, struct f2fs_summary *sum,
857 block_t old_blkaddr, block_t new_blkaddr)
859 struct sit_info *sit_i = SIT_I(sbi);
860 struct curseg_info *curseg;
861 unsigned int segno, old_cursegno;
862 struct seg_entry *se;
865 segno = GET_SEGNO(sbi, new_blkaddr);
866 se = get_seg_entry(sbi, segno);
869 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
870 if (old_blkaddr == NULL_ADDR)
871 type = CURSEG_COLD_DATA;
873 type = CURSEG_WARM_DATA;
875 curseg = CURSEG_I(sbi, type);
877 mutex_lock(&curseg->curseg_mutex);
878 mutex_lock(&sit_i->sentry_lock);
880 old_cursegno = curseg->segno;
882 /* change the current segment */
883 if (segno != curseg->segno) {
884 curseg->next_segno = segno;
885 change_curseg(sbi, type, true);
888 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
889 (sbi->blocks_per_seg - 1);
890 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
892 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
894 locate_dirty_segment(sbi, old_cursegno);
895 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
897 mutex_unlock(&sit_i->sentry_lock);
898 mutex_unlock(&curseg->curseg_mutex);
901 void rewrite_node_page(struct f2fs_sb_info *sbi,
902 struct page *page, struct f2fs_summary *sum,
903 block_t old_blkaddr, block_t new_blkaddr)
905 struct sit_info *sit_i = SIT_I(sbi);
906 int type = CURSEG_WARM_NODE;
907 struct curseg_info *curseg;
908 unsigned int segno, old_cursegno;
909 block_t next_blkaddr = next_blkaddr_of_node(page);
910 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
912 curseg = CURSEG_I(sbi, type);
914 mutex_lock(&curseg->curseg_mutex);
915 mutex_lock(&sit_i->sentry_lock);
917 segno = GET_SEGNO(sbi, new_blkaddr);
918 old_cursegno = curseg->segno;
920 /* change the current segment */
921 if (segno != curseg->segno) {
922 curseg->next_segno = segno;
923 change_curseg(sbi, type, true);
925 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
926 (sbi->blocks_per_seg - 1);
927 __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
929 /* change the current log to the next block addr in advance */
930 if (next_segno != segno) {
931 curseg->next_segno = next_segno;
932 change_curseg(sbi, type, true);
934 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
935 (sbi->blocks_per_seg - 1);
937 /* rewrite node page */
938 set_page_writeback(page);
939 submit_write_page(sbi, page, new_blkaddr, NODE);
940 f2fs_submit_bio(sbi, NODE, true);
941 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
943 locate_dirty_segment(sbi, old_cursegno);
944 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
946 mutex_unlock(&sit_i->sentry_lock);
947 mutex_unlock(&curseg->curseg_mutex);
950 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
952 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
953 struct curseg_info *seg_i;
954 unsigned char *kaddr;
959 start = start_sum_block(sbi);
961 page = get_meta_page(sbi, start++);
962 kaddr = (unsigned char *)page_address(page);
964 /* Step 1: restore nat cache */
965 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
966 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
968 /* Step 2: restore sit cache */
969 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
970 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
972 offset = 2 * SUM_JOURNAL_SIZE;
974 /* Step 3: restore summary entries */
975 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
976 unsigned short blk_off;
979 seg_i = CURSEG_I(sbi, i);
980 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
981 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
982 seg_i->next_segno = segno;
983 reset_curseg(sbi, i, 0);
984 seg_i->alloc_type = ckpt->alloc_type[i];
985 seg_i->next_blkoff = blk_off;
987 if (seg_i->alloc_type == SSR)
988 blk_off = sbi->blocks_per_seg;
990 for (j = 0; j < blk_off; j++) {
991 struct f2fs_summary *s;
992 s = (struct f2fs_summary *)(kaddr + offset);
993 seg_i->sum_blk->entries[j] = *s;
994 offset += SUMMARY_SIZE;
995 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
999 f2fs_put_page(page, 1);
1002 page = get_meta_page(sbi, start++);
1003 kaddr = (unsigned char *)page_address(page);
1007 f2fs_put_page(page, 1);
1011 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1013 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1014 struct f2fs_summary_block *sum;
1015 struct curseg_info *curseg;
1017 unsigned short blk_off;
1018 unsigned int segno = 0;
1019 block_t blk_addr = 0;
1021 /* get segment number and block addr */
1022 if (IS_DATASEG(type)) {
1023 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1024 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1026 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1027 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1029 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1031 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1033 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1035 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1036 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1037 type - CURSEG_HOT_NODE);
1039 blk_addr = GET_SUM_BLOCK(sbi, segno);
1042 new = get_meta_page(sbi, blk_addr);
1043 sum = (struct f2fs_summary_block *)page_address(new);
1045 if (IS_NODESEG(type)) {
1046 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1047 struct f2fs_summary *ns = &sum->entries[0];
1049 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1051 ns->ofs_in_node = 0;
1054 if (restore_node_summary(sbi, segno, sum)) {
1055 f2fs_put_page(new, 1);
1061 /* set uncompleted segment to curseg */
1062 curseg = CURSEG_I(sbi, type);
1063 mutex_lock(&curseg->curseg_mutex);
1064 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1065 curseg->next_segno = segno;
1066 reset_curseg(sbi, type, 0);
1067 curseg->alloc_type = ckpt->alloc_type[type];
1068 curseg->next_blkoff = blk_off;
1069 mutex_unlock(&curseg->curseg_mutex);
1070 f2fs_put_page(new, 1);
1074 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1076 int type = CURSEG_HOT_DATA;
1078 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1079 /* restore for compacted data summary */
1080 if (read_compacted_summaries(sbi))
1082 type = CURSEG_HOT_NODE;
1085 for (; type <= CURSEG_COLD_NODE; type++)
1086 if (read_normal_summaries(sbi, type))
1091 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1094 unsigned char *kaddr;
1095 struct f2fs_summary *summary;
1096 struct curseg_info *seg_i;
1097 int written_size = 0;
1100 page = grab_meta_page(sbi, blkaddr++);
1101 kaddr = (unsigned char *)page_address(page);
1103 /* Step 1: write nat cache */
1104 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1105 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1106 written_size += SUM_JOURNAL_SIZE;
1108 /* Step 2: write sit cache */
1109 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1110 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1112 written_size += SUM_JOURNAL_SIZE;
1114 set_page_dirty(page);
1116 /* Step 3: write summary entries */
1117 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1118 unsigned short blkoff;
1119 seg_i = CURSEG_I(sbi, i);
1120 if (sbi->ckpt->alloc_type[i] == SSR)
1121 blkoff = sbi->blocks_per_seg;
1123 blkoff = curseg_blkoff(sbi, i);
1125 for (j = 0; j < blkoff; j++) {
1127 page = grab_meta_page(sbi, blkaddr++);
1128 kaddr = (unsigned char *)page_address(page);
1131 summary = (struct f2fs_summary *)(kaddr + written_size);
1132 *summary = seg_i->sum_blk->entries[j];
1133 written_size += SUMMARY_SIZE;
1134 set_page_dirty(page);
1136 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1140 f2fs_put_page(page, 1);
1145 f2fs_put_page(page, 1);
1148 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1149 block_t blkaddr, int type)
1152 if (IS_DATASEG(type))
1153 end = type + NR_CURSEG_DATA_TYPE;
1155 end = type + NR_CURSEG_NODE_TYPE;
1157 for (i = type; i < end; i++) {
1158 struct curseg_info *sum = CURSEG_I(sbi, i);
1159 mutex_lock(&sum->curseg_mutex);
1160 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1161 mutex_unlock(&sum->curseg_mutex);
1165 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1167 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1168 write_compacted_summaries(sbi, start_blk);
1170 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1173 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1175 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1176 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1180 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1181 unsigned int val, int alloc)
1185 if (type == NAT_JOURNAL) {
1186 for (i = 0; i < nats_in_cursum(sum); i++) {
1187 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1190 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1191 return update_nats_in_cursum(sum, 1);
1192 } else if (type == SIT_JOURNAL) {
1193 for (i = 0; i < sits_in_cursum(sum); i++)
1194 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1196 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1197 return update_sits_in_cursum(sum, 1);
1202 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1205 struct sit_info *sit_i = SIT_I(sbi);
1206 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1207 block_t blk_addr = sit_i->sit_base_addr + offset;
1209 check_seg_range(sbi, segno);
1211 /* calculate sit block address */
1212 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1213 blk_addr += sit_i->sit_blocks;
1215 return get_meta_page(sbi, blk_addr);
1218 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1221 struct sit_info *sit_i = SIT_I(sbi);
1222 struct page *src_page, *dst_page;
1223 pgoff_t src_off, dst_off;
1224 void *src_addr, *dst_addr;
1226 src_off = current_sit_addr(sbi, start);
1227 dst_off = next_sit_addr(sbi, src_off);
1229 /* get current sit block page without lock */
1230 src_page = get_meta_page(sbi, src_off);
1231 dst_page = grab_meta_page(sbi, dst_off);
1232 BUG_ON(PageDirty(src_page));
1234 src_addr = page_address(src_page);
1235 dst_addr = page_address(dst_page);
1236 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1238 set_page_dirty(dst_page);
1239 f2fs_put_page(src_page, 1);
1241 set_to_next_sit(sit_i, start);
1246 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1248 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1249 struct f2fs_summary_block *sum = curseg->sum_blk;
1253 * If the journal area in the current summary is full of sit entries,
1254 * all the sit entries will be flushed. Otherwise the sit entries
1255 * are not able to replace with newly hot sit entries.
1257 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1258 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1260 segno = le32_to_cpu(segno_in_journal(sum, i));
1261 __mark_sit_entry_dirty(sbi, segno);
1263 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1270 * CP calls this function, which flushes SIT entries including sit_journal,
1271 * and moves prefree segs to free segs.
1273 void flush_sit_entries(struct f2fs_sb_info *sbi)
1275 struct sit_info *sit_i = SIT_I(sbi);
1276 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1277 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1278 struct f2fs_summary_block *sum = curseg->sum_blk;
1279 unsigned long nsegs = TOTAL_SEGS(sbi);
1280 struct page *page = NULL;
1281 struct f2fs_sit_block *raw_sit = NULL;
1282 unsigned int start = 0, end = 0;
1283 unsigned int segno = -1;
1286 mutex_lock(&curseg->curseg_mutex);
1287 mutex_lock(&sit_i->sentry_lock);
1290 * "flushed" indicates whether sit entries in journal are flushed
1291 * to the SIT area or not.
1293 flushed = flush_sits_in_journal(sbi);
1295 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1296 struct seg_entry *se = get_seg_entry(sbi, segno);
1297 int sit_offset, offset;
1299 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1304 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1306 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1307 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1311 if (!page || (start > segno) || (segno > end)) {
1313 f2fs_put_page(page, 1);
1317 start = START_SEGNO(sit_i, segno);
1318 end = start + SIT_ENTRY_PER_BLOCK - 1;
1320 /* read sit block that will be updated */
1321 page = get_next_sit_page(sbi, start);
1322 raw_sit = page_address(page);
1325 /* udpate entry in SIT block */
1326 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1328 __clear_bit(segno, bitmap);
1329 sit_i->dirty_sentries--;
1331 mutex_unlock(&sit_i->sentry_lock);
1332 mutex_unlock(&curseg->curseg_mutex);
1334 /* writeout last modified SIT block */
1335 f2fs_put_page(page, 1);
1337 set_prefree_as_free_segments(sbi);
1340 static int build_sit_info(struct f2fs_sb_info *sbi)
1342 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1343 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1344 struct sit_info *sit_i;
1345 unsigned int sit_segs, start;
1346 char *src_bitmap, *dst_bitmap;
1347 unsigned int bitmap_size;
1349 /* allocate memory for SIT information */
1350 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1354 SM_I(sbi)->sit_info = sit_i;
1356 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1357 if (!sit_i->sentries)
1360 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1361 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1362 if (!sit_i->dirty_sentries_bitmap)
1365 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1366 sit_i->sentries[start].cur_valid_map
1367 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1368 sit_i->sentries[start].ckpt_valid_map
1369 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1370 if (!sit_i->sentries[start].cur_valid_map
1371 || !sit_i->sentries[start].ckpt_valid_map)
1375 if (sbi->segs_per_sec > 1) {
1376 sit_i->sec_entries = vzalloc(sbi->total_sections *
1377 sizeof(struct sec_entry));
1378 if (!sit_i->sec_entries)
1382 /* get information related with SIT */
1383 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1385 /* setup SIT bitmap from ckeckpoint pack */
1386 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1387 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1389 dst_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1392 memcpy(dst_bitmap, src_bitmap, bitmap_size);
1394 /* init SIT information */
1395 sit_i->s_ops = &default_salloc_ops;
1397 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1398 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1399 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1400 sit_i->sit_bitmap = dst_bitmap;
1401 sit_i->bitmap_size = bitmap_size;
1402 sit_i->dirty_sentries = 0;
1403 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1404 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1405 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1406 mutex_init(&sit_i->sentry_lock);
1410 static int build_free_segmap(struct f2fs_sb_info *sbi)
1412 struct f2fs_sm_info *sm_info = SM_I(sbi);
1413 struct free_segmap_info *free_i;
1414 unsigned int bitmap_size, sec_bitmap_size;
1416 /* allocate memory for free segmap information */
1417 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1421 SM_I(sbi)->free_info = free_i;
1423 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1424 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1425 if (!free_i->free_segmap)
1428 sec_bitmap_size = f2fs_bitmap_size(sbi->total_sections);
1429 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1430 if (!free_i->free_secmap)
1433 /* set all segments as dirty temporarily */
1434 memset(free_i->free_segmap, 0xff, bitmap_size);
1435 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1437 /* init free segmap information */
1438 free_i->start_segno =
1439 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1440 free_i->free_segments = 0;
1441 free_i->free_sections = 0;
1442 rwlock_init(&free_i->segmap_lock);
1446 static int build_curseg(struct f2fs_sb_info *sbi)
1448 struct curseg_info *array;
1451 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1455 SM_I(sbi)->curseg_array = array;
1457 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1458 mutex_init(&array[i].curseg_mutex);
1459 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1460 if (!array[i].sum_blk)
1462 array[i].segno = NULL_SEGNO;
1463 array[i].next_blkoff = 0;
1465 return restore_curseg_summaries(sbi);
1468 static void build_sit_entries(struct f2fs_sb_info *sbi)
1470 struct sit_info *sit_i = SIT_I(sbi);
1471 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1472 struct f2fs_summary_block *sum = curseg->sum_blk;
1475 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1476 struct seg_entry *se = &sit_i->sentries[start];
1477 struct f2fs_sit_block *sit_blk;
1478 struct f2fs_sit_entry sit;
1482 mutex_lock(&curseg->curseg_mutex);
1483 for (i = 0; i < sits_in_cursum(sum); i++) {
1484 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1485 sit = sit_in_journal(sum, i);
1486 mutex_unlock(&curseg->curseg_mutex);
1490 mutex_unlock(&curseg->curseg_mutex);
1491 page = get_current_sit_page(sbi, start);
1492 sit_blk = (struct f2fs_sit_block *)page_address(page);
1493 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1494 f2fs_put_page(page, 1);
1496 check_block_count(sbi, start, &sit);
1497 seg_info_from_raw_sit(se, &sit);
1498 if (sbi->segs_per_sec > 1) {
1499 struct sec_entry *e = get_sec_entry(sbi, start);
1500 e->valid_blocks += se->valid_blocks;
1505 static void init_free_segmap(struct f2fs_sb_info *sbi)
1510 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1511 struct seg_entry *sentry = get_seg_entry(sbi, start);
1512 if (!sentry->valid_blocks)
1513 __set_free(sbi, start);
1516 /* set use the current segments */
1517 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1518 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1519 __set_test_and_inuse(sbi, curseg_t->segno);
1523 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1525 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1526 struct free_segmap_info *free_i = FREE_I(sbi);
1527 unsigned int segno = 0, offset = 0;
1528 unsigned short valid_blocks;
1530 while (segno < TOTAL_SEGS(sbi)) {
1531 /* find dirty segment based on free segmap */
1532 segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1533 if (segno >= TOTAL_SEGS(sbi))
1536 valid_blocks = get_valid_blocks(sbi, segno, 0);
1537 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1539 mutex_lock(&dirty_i->seglist_lock);
1540 __locate_dirty_segment(sbi, segno, DIRTY);
1541 mutex_unlock(&dirty_i->seglist_lock);
1545 static int init_victim_segmap(struct f2fs_sb_info *sbi)
1547 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1548 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1550 dirty_i->victim_segmap[FG_GC] = kzalloc(bitmap_size, GFP_KERNEL);
1551 dirty_i->victim_segmap[BG_GC] = kzalloc(bitmap_size, GFP_KERNEL);
1552 if (!dirty_i->victim_segmap[FG_GC] || !dirty_i->victim_segmap[BG_GC])
1557 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1559 struct dirty_seglist_info *dirty_i;
1560 unsigned int bitmap_size, i;
1562 /* allocate memory for dirty segments list information */
1563 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1567 SM_I(sbi)->dirty_info = dirty_i;
1568 mutex_init(&dirty_i->seglist_lock);
1570 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1572 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1573 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1574 if (!dirty_i->dirty_segmap[i])
1578 init_dirty_segmap(sbi);
1579 return init_victim_segmap(sbi);
1583 * Update min, max modified time for cost-benefit GC algorithm
1585 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1587 struct sit_info *sit_i = SIT_I(sbi);
1590 mutex_lock(&sit_i->sentry_lock);
1592 sit_i->min_mtime = LLONG_MAX;
1594 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1596 unsigned long long mtime = 0;
1598 for (i = 0; i < sbi->segs_per_sec; i++)
1599 mtime += get_seg_entry(sbi, segno + i)->mtime;
1601 mtime = div_u64(mtime, sbi->segs_per_sec);
1603 if (sit_i->min_mtime > mtime)
1604 sit_i->min_mtime = mtime;
1606 sit_i->max_mtime = get_mtime(sbi);
1607 mutex_unlock(&sit_i->sentry_lock);
1610 int build_segment_manager(struct f2fs_sb_info *sbi)
1612 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1613 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1614 struct f2fs_sm_info *sm_info;
1617 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1622 sbi->sm_info = sm_info;
1623 INIT_LIST_HEAD(&sm_info->wblist_head);
1624 spin_lock_init(&sm_info->wblist_lock);
1625 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1626 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1627 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1628 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1629 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1630 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1631 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1633 err = build_sit_info(sbi);
1636 err = build_free_segmap(sbi);
1639 err = build_curseg(sbi);
1643 /* reinit free segmap based on SIT */
1644 build_sit_entries(sbi);
1646 init_free_segmap(sbi);
1647 err = build_dirty_segmap(sbi);
1651 init_min_max_mtime(sbi);
1655 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1656 enum dirty_type dirty_type)
1658 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1660 mutex_lock(&dirty_i->seglist_lock);
1661 kfree(dirty_i->dirty_segmap[dirty_type]);
1662 dirty_i->nr_dirty[dirty_type] = 0;
1663 mutex_unlock(&dirty_i->seglist_lock);
1666 void reset_victim_segmap(struct f2fs_sb_info *sbi)
1668 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1669 memset(DIRTY_I(sbi)->victim_segmap[FG_GC], 0, bitmap_size);
1672 static void destroy_victim_segmap(struct f2fs_sb_info *sbi)
1674 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1676 kfree(dirty_i->victim_segmap[FG_GC]);
1677 kfree(dirty_i->victim_segmap[BG_GC]);
1680 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1682 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1688 /* discard pre-free/dirty segments list */
1689 for (i = 0; i < NR_DIRTY_TYPE; i++)
1690 discard_dirty_segmap(sbi, i);
1692 destroy_victim_segmap(sbi);
1693 SM_I(sbi)->dirty_info = NULL;
1697 static void destroy_curseg(struct f2fs_sb_info *sbi)
1699 struct curseg_info *array = SM_I(sbi)->curseg_array;
1704 SM_I(sbi)->curseg_array = NULL;
1705 for (i = 0; i < NR_CURSEG_TYPE; i++)
1706 kfree(array[i].sum_blk);
1710 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1712 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1715 SM_I(sbi)->free_info = NULL;
1716 kfree(free_i->free_segmap);
1717 kfree(free_i->free_secmap);
1721 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1723 struct sit_info *sit_i = SIT_I(sbi);
1729 if (sit_i->sentries) {
1730 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1731 kfree(sit_i->sentries[start].cur_valid_map);
1732 kfree(sit_i->sentries[start].ckpt_valid_map);
1735 vfree(sit_i->sentries);
1736 vfree(sit_i->sec_entries);
1737 kfree(sit_i->dirty_sentries_bitmap);
1739 SM_I(sbi)->sit_info = NULL;
1740 kfree(sit_i->sit_bitmap);
1744 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1746 struct f2fs_sm_info *sm_info = SM_I(sbi);
1747 destroy_dirty_segmap(sbi);
1748 destroy_curseg(sbi);
1749 destroy_free_segmap(sbi);
1750 destroy_sit_info(sbi);
1751 sbi->sm_info = NULL;