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>
21 #include <trace/events/f2fs.h>
24 * This function balances dirty node and dentry pages.
25 * In addition, it controls garbage collection.
27 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
30 * We should do GC or end up with checkpoint, if there are so many dirty
31 * dir/node pages without enough free segments.
33 if (has_not_enough_free_secs(sbi, 0)) {
34 mutex_lock(&sbi->gc_mutex);
39 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
40 enum dirty_type dirty_type)
42 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
44 /* need not be added */
45 if (IS_CURSEG(sbi, segno))
48 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
49 dirty_i->nr_dirty[dirty_type]++;
51 if (dirty_type == DIRTY) {
52 struct seg_entry *sentry = get_seg_entry(sbi, segno);
53 enum dirty_type t = DIRTY_HOT_DATA;
55 dirty_type = sentry->type;
57 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
58 dirty_i->nr_dirty[dirty_type]++;
60 /* Only one bitmap should be set */
61 for (; t <= DIRTY_COLD_NODE; t++) {
64 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
65 dirty_i->nr_dirty[t]--;
70 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
71 enum dirty_type dirty_type)
73 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
75 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
76 dirty_i->nr_dirty[dirty_type]--;
78 if (dirty_type == DIRTY) {
79 enum dirty_type t = DIRTY_HOT_DATA;
81 /* clear all the bitmaps */
82 for (; t <= DIRTY_COLD_NODE; t++)
83 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
84 dirty_i->nr_dirty[t]--;
86 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
87 clear_bit(GET_SECNO(sbi, segno),
88 dirty_i->victim_secmap);
93 * Should not occur error such as -ENOMEM.
94 * Adding dirty entry into seglist is not critical operation.
95 * If a given segment is one of current working segments, it won't be added.
97 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
99 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
100 unsigned short valid_blocks;
102 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
105 mutex_lock(&dirty_i->seglist_lock);
107 valid_blocks = get_valid_blocks(sbi, segno, 0);
109 if (valid_blocks == 0) {
110 __locate_dirty_segment(sbi, segno, PRE);
111 __remove_dirty_segment(sbi, segno, DIRTY);
112 } else if (valid_blocks < sbi->blocks_per_seg) {
113 __locate_dirty_segment(sbi, segno, DIRTY);
115 /* Recovery routine with SSR needs this */
116 __remove_dirty_segment(sbi, segno, DIRTY);
119 mutex_unlock(&dirty_i->seglist_lock);
124 * Should call clear_prefree_segments after checkpoint is done.
126 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
128 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
129 unsigned int segno = -1;
130 unsigned int total_segs = TOTAL_SEGS(sbi);
132 mutex_lock(&dirty_i->seglist_lock);
134 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
136 if (segno >= total_segs)
138 __set_test_and_free(sbi, segno);
140 mutex_unlock(&dirty_i->seglist_lock);
143 void clear_prefree_segments(struct f2fs_sb_info *sbi)
145 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
146 unsigned int segno = -1;
147 unsigned int total_segs = TOTAL_SEGS(sbi);
149 mutex_lock(&dirty_i->seglist_lock);
151 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
153 if (segno >= total_segs)
156 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
157 dirty_i->nr_dirty[PRE]--;
160 if (test_opt(sbi, DISCARD))
161 blkdev_issue_discard(sbi->sb->s_bdev,
162 START_BLOCK(sbi, segno) <<
163 sbi->log_sectors_per_block,
164 1 << (sbi->log_sectors_per_block +
165 sbi->log_blocks_per_seg),
168 mutex_unlock(&dirty_i->seglist_lock);
171 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
173 struct sit_info *sit_i = SIT_I(sbi);
174 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
175 sit_i->dirty_sentries++;
178 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
179 unsigned int segno, int modified)
181 struct seg_entry *se = get_seg_entry(sbi, segno);
184 __mark_sit_entry_dirty(sbi, segno);
187 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
189 struct seg_entry *se;
190 unsigned int segno, offset;
191 long int new_vblocks;
193 segno = GET_SEGNO(sbi, blkaddr);
195 se = get_seg_entry(sbi, segno);
196 new_vblocks = se->valid_blocks + del;
197 offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
199 BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
200 (new_vblocks > sbi->blocks_per_seg)));
202 se->valid_blocks = new_vblocks;
203 se->mtime = get_mtime(sbi);
204 SIT_I(sbi)->max_mtime = se->mtime;
206 /* Update valid block bitmap */
208 if (f2fs_set_bit(offset, se->cur_valid_map))
211 if (!f2fs_clear_bit(offset, se->cur_valid_map))
214 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
215 se->ckpt_valid_blocks += del;
217 __mark_sit_entry_dirty(sbi, segno);
219 /* update total number of valid blocks to be written in ckpt area */
220 SIT_I(sbi)->written_valid_blocks += del;
222 if (sbi->segs_per_sec > 1)
223 get_sec_entry(sbi, segno)->valid_blocks += del;
226 static void refresh_sit_entry(struct f2fs_sb_info *sbi,
227 block_t old_blkaddr, block_t new_blkaddr)
229 update_sit_entry(sbi, new_blkaddr, 1);
230 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
231 update_sit_entry(sbi, old_blkaddr, -1);
234 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
236 unsigned int segno = GET_SEGNO(sbi, addr);
237 struct sit_info *sit_i = SIT_I(sbi);
239 BUG_ON(addr == NULL_ADDR);
240 if (addr == NEW_ADDR)
243 /* add it into sit main buffer */
244 mutex_lock(&sit_i->sentry_lock);
246 update_sit_entry(sbi, addr, -1);
248 /* add it into dirty seglist */
249 locate_dirty_segment(sbi, segno);
251 mutex_unlock(&sit_i->sentry_lock);
255 * This function should be resided under the curseg_mutex lock
257 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
258 struct f2fs_summary *sum)
260 struct curseg_info *curseg = CURSEG_I(sbi, type);
261 void *addr = curseg->sum_blk;
262 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
263 memcpy(addr, sum, sizeof(struct f2fs_summary));
268 * Calculate the number of current summary pages for writing
270 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
272 int total_size_bytes = 0;
273 int valid_sum_count = 0;
276 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
277 if (sbi->ckpt->alloc_type[i] == SSR)
278 valid_sum_count += sbi->blocks_per_seg;
280 valid_sum_count += curseg_blkoff(sbi, i);
283 total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
284 + sizeof(struct nat_journal) + 2
285 + sizeof(struct sit_journal) + 2;
286 sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
287 if (total_size_bytes < sum_space)
289 else if (total_size_bytes < 2 * sum_space)
295 * Caller should put this summary page
297 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
299 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
302 static void write_sum_page(struct f2fs_sb_info *sbi,
303 struct f2fs_summary_block *sum_blk, block_t blk_addr)
305 struct page *page = grab_meta_page(sbi, blk_addr);
306 void *kaddr = page_address(page);
307 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
308 set_page_dirty(page);
309 f2fs_put_page(page, 1);
312 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
314 struct curseg_info *curseg = CURSEG_I(sbi, type);
315 unsigned int segno = curseg->segno + 1;
316 struct free_segmap_info *free_i = FREE_I(sbi);
318 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
319 return !test_bit(segno, free_i->free_segmap);
324 * Find a new segment from the free segments bitmap to right order
325 * This function should be returned with success, otherwise BUG
327 static void get_new_segment(struct f2fs_sb_info *sbi,
328 unsigned int *newseg, bool new_sec, int dir)
330 struct free_segmap_info *free_i = FREE_I(sbi);
331 unsigned int segno, secno, zoneno;
332 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
333 unsigned int hint = *newseg / sbi->segs_per_sec;
334 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
335 unsigned int left_start = hint;
340 write_lock(&free_i->segmap_lock);
342 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
343 segno = find_next_zero_bit(free_i->free_segmap,
344 TOTAL_SEGS(sbi), *newseg + 1);
345 if (segno - *newseg < sbi->segs_per_sec -
346 (*newseg % sbi->segs_per_sec))
350 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
351 if (secno >= TOTAL_SECS(sbi)) {
352 if (dir == ALLOC_RIGHT) {
353 secno = find_next_zero_bit(free_i->free_secmap,
355 BUG_ON(secno >= TOTAL_SECS(sbi));
358 left_start = hint - 1;
364 while (test_bit(left_start, free_i->free_secmap)) {
365 if (left_start > 0) {
369 left_start = find_next_zero_bit(free_i->free_secmap,
371 BUG_ON(left_start >= TOTAL_SECS(sbi));
377 segno = secno * sbi->segs_per_sec;
378 zoneno = secno / sbi->secs_per_zone;
380 /* give up on finding another zone */
383 if (sbi->secs_per_zone == 1)
385 if (zoneno == old_zoneno)
387 if (dir == ALLOC_LEFT) {
388 if (!go_left && zoneno + 1 >= total_zones)
390 if (go_left && zoneno == 0)
393 for (i = 0; i < NR_CURSEG_TYPE; i++)
394 if (CURSEG_I(sbi, i)->zone == zoneno)
397 if (i < NR_CURSEG_TYPE) {
398 /* zone is in user, try another */
400 hint = zoneno * sbi->secs_per_zone - 1;
401 else if (zoneno + 1 >= total_zones)
404 hint = (zoneno + 1) * sbi->secs_per_zone;
406 goto find_other_zone;
409 /* set it as dirty segment in free segmap */
410 BUG_ON(test_bit(segno, free_i->free_segmap));
411 __set_inuse(sbi, segno);
413 write_unlock(&free_i->segmap_lock);
416 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
418 struct curseg_info *curseg = CURSEG_I(sbi, type);
419 struct summary_footer *sum_footer;
421 curseg->segno = curseg->next_segno;
422 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
423 curseg->next_blkoff = 0;
424 curseg->next_segno = NULL_SEGNO;
426 sum_footer = &(curseg->sum_blk->footer);
427 memset(sum_footer, 0, sizeof(struct summary_footer));
428 if (IS_DATASEG(type))
429 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
430 if (IS_NODESEG(type))
431 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
432 __set_sit_entry_type(sbi, type, curseg->segno, modified);
436 * Allocate a current working segment.
437 * This function always allocates a free segment in LFS manner.
439 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
441 struct curseg_info *curseg = CURSEG_I(sbi, type);
442 unsigned int segno = curseg->segno;
443 int dir = ALLOC_LEFT;
445 write_sum_page(sbi, curseg->sum_blk,
446 GET_SUM_BLOCK(sbi, segno));
447 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
450 if (test_opt(sbi, NOHEAP))
453 get_new_segment(sbi, &segno, new_sec, dir);
454 curseg->next_segno = segno;
455 reset_curseg(sbi, type, 1);
456 curseg->alloc_type = LFS;
459 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
460 struct curseg_info *seg, block_t start)
462 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
464 for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
465 if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
466 && !f2fs_test_bit(ofs, se->cur_valid_map))
469 seg->next_blkoff = ofs;
473 * If a segment is written by LFS manner, next block offset is just obtained
474 * by increasing the current block offset. However, if a segment is written by
475 * SSR manner, next block offset obtained by calling __next_free_blkoff
477 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
478 struct curseg_info *seg)
480 if (seg->alloc_type == SSR)
481 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
487 * This function always allocates a used segment (from dirty seglist) by SSR
488 * manner, so it should recover the existing segment information of valid blocks
490 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
492 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
493 struct curseg_info *curseg = CURSEG_I(sbi, type);
494 unsigned int new_segno = curseg->next_segno;
495 struct f2fs_summary_block *sum_node;
496 struct page *sum_page;
498 write_sum_page(sbi, curseg->sum_blk,
499 GET_SUM_BLOCK(sbi, curseg->segno));
500 __set_test_and_inuse(sbi, new_segno);
502 mutex_lock(&dirty_i->seglist_lock);
503 __remove_dirty_segment(sbi, new_segno, PRE);
504 __remove_dirty_segment(sbi, new_segno, DIRTY);
505 mutex_unlock(&dirty_i->seglist_lock);
507 reset_curseg(sbi, type, 1);
508 curseg->alloc_type = SSR;
509 __next_free_blkoff(sbi, curseg, 0);
512 sum_page = get_sum_page(sbi, new_segno);
513 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
514 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
515 f2fs_put_page(sum_page, 1);
519 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
521 struct curseg_info *curseg = CURSEG_I(sbi, type);
522 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
524 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
525 return v_ops->get_victim(sbi,
526 &(curseg)->next_segno, BG_GC, type, SSR);
528 /* For data segments, let's do SSR more intensively */
529 for (; type >= CURSEG_HOT_DATA; type--)
530 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
537 * flush out current segment and replace it with new segment
538 * This function should be returned with success, otherwise BUG
540 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
541 int type, bool force)
543 struct curseg_info *curseg = CURSEG_I(sbi, type);
546 new_curseg(sbi, type, true);
550 if (type == CURSEG_WARM_NODE)
551 new_curseg(sbi, type, false);
552 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
553 new_curseg(sbi, type, false);
554 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
555 change_curseg(sbi, type, true);
557 new_curseg(sbi, type, false);
559 #ifdef CONFIG_F2FS_STAT_FS
560 sbi->segment_count[curseg->alloc_type]++;
565 void allocate_new_segments(struct f2fs_sb_info *sbi)
567 struct curseg_info *curseg;
568 unsigned int old_curseg;
571 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
572 curseg = CURSEG_I(sbi, i);
573 old_curseg = curseg->segno;
574 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
575 locate_dirty_segment(sbi, old_curseg);
579 static const struct segment_allocation default_salloc_ops = {
580 .allocate_segment = allocate_segment_by_default,
583 static void f2fs_end_io_write(struct bio *bio, int err)
585 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
586 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
587 struct bio_private *p = bio->bi_private;
590 struct page *page = bvec->bv_page;
592 if (--bvec >= bio->bi_io_vec)
593 prefetchw(&bvec->bv_page->flags);
597 set_bit(AS_EIO, &page->mapping->flags);
598 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
599 p->sbi->sb->s_flags |= MS_RDONLY;
601 end_page_writeback(page);
602 dec_page_count(p->sbi, F2FS_WRITEBACK);
603 } while (bvec >= bio->bi_io_vec);
611 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
615 /* No failure on bio allocation */
616 bio = bio_alloc(GFP_NOIO, npages);
618 bio->bi_private = NULL;
623 static void do_submit_bio(struct f2fs_sb_info *sbi,
624 enum page_type type, bool sync)
626 int rw = sync ? WRITE_SYNC : WRITE;
627 enum page_type btype = type > META ? META : type;
629 if (type >= META_FLUSH)
630 rw = WRITE_FLUSH_FUA;
635 if (sbi->bio[btype]) {
636 struct bio_private *p = sbi->bio[btype]->bi_private;
638 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
640 trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
642 if (type == META_FLUSH) {
643 DECLARE_COMPLETION_ONSTACK(wait);
646 submit_bio(rw, sbi->bio[btype]);
647 wait_for_completion(&wait);
650 submit_bio(rw, sbi->bio[btype]);
652 sbi->bio[btype] = NULL;
656 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
658 down_write(&sbi->bio_sem);
659 do_submit_bio(sbi, type, sync);
660 up_write(&sbi->bio_sem);
663 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
664 block_t blk_addr, enum page_type type)
666 struct block_device *bdev = sbi->sb->s_bdev;
668 verify_block_addr(sbi, blk_addr);
670 down_write(&sbi->bio_sem);
672 inc_page_count(sbi, F2FS_WRITEBACK);
674 if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
675 do_submit_bio(sbi, type, false);
677 if (sbi->bio[type] == NULL) {
678 struct bio_private *priv;
680 priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
686 sbi->bio[type] = f2fs_bio_alloc(bdev, max_hw_blocks(sbi));
687 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
688 sbi->bio[type]->bi_private = priv;
690 * The end_io will be assigned at the sumbission phase.
691 * Until then, let bio_add_page() merge consecutive IOs as much
696 if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
698 do_submit_bio(sbi, type, false);
702 sbi->last_block_in_bio[type] = blk_addr;
704 up_write(&sbi->bio_sem);
705 trace_f2fs_submit_write_page(page, blk_addr, type);
708 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
710 struct curseg_info *curseg = CURSEG_I(sbi, type);
711 if (curseg->next_blkoff < sbi->blocks_per_seg)
716 static int __get_segment_type_2(struct page *page, enum page_type p_type)
719 return CURSEG_HOT_DATA;
721 return CURSEG_HOT_NODE;
724 static int __get_segment_type_4(struct page *page, enum page_type p_type)
726 if (p_type == DATA) {
727 struct inode *inode = page->mapping->host;
729 if (S_ISDIR(inode->i_mode))
730 return CURSEG_HOT_DATA;
732 return CURSEG_COLD_DATA;
734 if (IS_DNODE(page) && !is_cold_node(page))
735 return CURSEG_HOT_NODE;
737 return CURSEG_COLD_NODE;
741 static int __get_segment_type_6(struct page *page, enum page_type p_type)
743 if (p_type == DATA) {
744 struct inode *inode = page->mapping->host;
746 if (S_ISDIR(inode->i_mode))
747 return CURSEG_HOT_DATA;
748 else if (is_cold_data(page) || file_is_cold(inode))
749 return CURSEG_COLD_DATA;
751 return CURSEG_WARM_DATA;
754 return is_cold_node(page) ? CURSEG_WARM_NODE :
757 return CURSEG_COLD_NODE;
761 static int __get_segment_type(struct page *page, enum page_type p_type)
763 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
764 switch (sbi->active_logs) {
766 return __get_segment_type_2(page, p_type);
768 return __get_segment_type_4(page, p_type);
770 /* NR_CURSEG_TYPE(6) logs by default */
771 BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
772 return __get_segment_type_6(page, p_type);
775 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
776 block_t old_blkaddr, block_t *new_blkaddr,
777 struct f2fs_summary *sum, enum page_type p_type)
779 struct sit_info *sit_i = SIT_I(sbi);
780 struct curseg_info *curseg;
781 unsigned int old_cursegno;
784 type = __get_segment_type(page, p_type);
785 curseg = CURSEG_I(sbi, type);
787 mutex_lock(&curseg->curseg_mutex);
789 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
790 old_cursegno = curseg->segno;
793 * __add_sum_entry should be resided under the curseg_mutex
794 * because, this function updates a summary entry in the
795 * current summary block.
797 __add_sum_entry(sbi, type, sum);
799 mutex_lock(&sit_i->sentry_lock);
800 __refresh_next_blkoff(sbi, curseg);
801 #ifdef CONFIG_F2FS_STAT_FS
802 sbi->block_count[curseg->alloc_type]++;
806 * SIT information should be updated before segment allocation,
807 * since SSR needs latest valid block information.
809 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
811 if (!__has_curseg_space(sbi, type))
812 sit_i->s_ops->allocate_segment(sbi, type, false);
814 locate_dirty_segment(sbi, old_cursegno);
815 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
816 mutex_unlock(&sit_i->sentry_lock);
819 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
821 /* writeout dirty page into bdev */
822 submit_write_page(sbi, page, *new_blkaddr, p_type);
824 mutex_unlock(&curseg->curseg_mutex);
827 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
829 set_page_writeback(page);
830 submit_write_page(sbi, page, page->index, META);
833 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
834 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
836 struct f2fs_summary sum;
837 set_summary(&sum, nid, 0, 0);
838 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
841 void write_data_page(struct inode *inode, struct page *page,
842 struct dnode_of_data *dn, block_t old_blkaddr,
843 block_t *new_blkaddr)
845 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
846 struct f2fs_summary sum;
849 BUG_ON(old_blkaddr == NULL_ADDR);
850 get_node_info(sbi, dn->nid, &ni);
851 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
853 do_write_page(sbi, page, old_blkaddr,
854 new_blkaddr, &sum, DATA);
857 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
858 block_t old_blk_addr)
860 submit_write_page(sbi, page, old_blk_addr, DATA);
863 void recover_data_page(struct f2fs_sb_info *sbi,
864 struct page *page, struct f2fs_summary *sum,
865 block_t old_blkaddr, block_t new_blkaddr)
867 struct sit_info *sit_i = SIT_I(sbi);
868 struct curseg_info *curseg;
869 unsigned int segno, old_cursegno;
870 struct seg_entry *se;
873 segno = GET_SEGNO(sbi, new_blkaddr);
874 se = get_seg_entry(sbi, segno);
877 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
878 if (old_blkaddr == NULL_ADDR)
879 type = CURSEG_COLD_DATA;
881 type = CURSEG_WARM_DATA;
883 curseg = CURSEG_I(sbi, type);
885 mutex_lock(&curseg->curseg_mutex);
886 mutex_lock(&sit_i->sentry_lock);
888 old_cursegno = curseg->segno;
890 /* change the current segment */
891 if (segno != curseg->segno) {
892 curseg->next_segno = segno;
893 change_curseg(sbi, type, true);
896 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
897 (sbi->blocks_per_seg - 1);
898 __add_sum_entry(sbi, type, sum);
900 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
902 locate_dirty_segment(sbi, old_cursegno);
903 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
905 mutex_unlock(&sit_i->sentry_lock);
906 mutex_unlock(&curseg->curseg_mutex);
909 void rewrite_node_page(struct f2fs_sb_info *sbi,
910 struct page *page, struct f2fs_summary *sum,
911 block_t old_blkaddr, block_t new_blkaddr)
913 struct sit_info *sit_i = SIT_I(sbi);
914 int type = CURSEG_WARM_NODE;
915 struct curseg_info *curseg;
916 unsigned int segno, old_cursegno;
917 block_t next_blkaddr = next_blkaddr_of_node(page);
918 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
920 curseg = CURSEG_I(sbi, type);
922 mutex_lock(&curseg->curseg_mutex);
923 mutex_lock(&sit_i->sentry_lock);
925 segno = GET_SEGNO(sbi, new_blkaddr);
926 old_cursegno = curseg->segno;
928 /* change the current segment */
929 if (segno != curseg->segno) {
930 curseg->next_segno = segno;
931 change_curseg(sbi, type, true);
933 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
934 (sbi->blocks_per_seg - 1);
935 __add_sum_entry(sbi, type, sum);
937 /* change the current log to the next block addr in advance */
938 if (next_segno != segno) {
939 curseg->next_segno = next_segno;
940 change_curseg(sbi, type, true);
942 curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
943 (sbi->blocks_per_seg - 1);
945 /* rewrite node page */
946 set_page_writeback(page);
947 submit_write_page(sbi, page, new_blkaddr, NODE);
948 f2fs_submit_bio(sbi, NODE, true);
949 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
951 locate_dirty_segment(sbi, old_cursegno);
952 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
954 mutex_unlock(&sit_i->sentry_lock);
955 mutex_unlock(&curseg->curseg_mutex);
958 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
960 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
961 struct curseg_info *seg_i;
962 unsigned char *kaddr;
967 start = start_sum_block(sbi);
969 page = get_meta_page(sbi, start++);
970 kaddr = (unsigned char *)page_address(page);
972 /* Step 1: restore nat cache */
973 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
974 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
976 /* Step 2: restore sit cache */
977 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
978 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
980 offset = 2 * SUM_JOURNAL_SIZE;
982 /* Step 3: restore summary entries */
983 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
984 unsigned short blk_off;
987 seg_i = CURSEG_I(sbi, i);
988 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
989 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
990 seg_i->next_segno = segno;
991 reset_curseg(sbi, i, 0);
992 seg_i->alloc_type = ckpt->alloc_type[i];
993 seg_i->next_blkoff = blk_off;
995 if (seg_i->alloc_type == SSR)
996 blk_off = sbi->blocks_per_seg;
998 for (j = 0; j < blk_off; j++) {
999 struct f2fs_summary *s;
1000 s = (struct f2fs_summary *)(kaddr + offset);
1001 seg_i->sum_blk->entries[j] = *s;
1002 offset += SUMMARY_SIZE;
1003 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1007 f2fs_put_page(page, 1);
1010 page = get_meta_page(sbi, start++);
1011 kaddr = (unsigned char *)page_address(page);
1015 f2fs_put_page(page, 1);
1019 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1021 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1022 struct f2fs_summary_block *sum;
1023 struct curseg_info *curseg;
1025 unsigned short blk_off;
1026 unsigned int segno = 0;
1027 block_t blk_addr = 0;
1029 /* get segment number and block addr */
1030 if (IS_DATASEG(type)) {
1031 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1032 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1034 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1035 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1037 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1039 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1041 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1043 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1044 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1045 type - CURSEG_HOT_NODE);
1047 blk_addr = GET_SUM_BLOCK(sbi, segno);
1050 new = get_meta_page(sbi, blk_addr);
1051 sum = (struct f2fs_summary_block *)page_address(new);
1053 if (IS_NODESEG(type)) {
1054 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1055 struct f2fs_summary *ns = &sum->entries[0];
1057 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1059 ns->ofs_in_node = 0;
1062 if (restore_node_summary(sbi, segno, sum)) {
1063 f2fs_put_page(new, 1);
1069 /* set uncompleted segment to curseg */
1070 curseg = CURSEG_I(sbi, type);
1071 mutex_lock(&curseg->curseg_mutex);
1072 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1073 curseg->next_segno = segno;
1074 reset_curseg(sbi, type, 0);
1075 curseg->alloc_type = ckpt->alloc_type[type];
1076 curseg->next_blkoff = blk_off;
1077 mutex_unlock(&curseg->curseg_mutex);
1078 f2fs_put_page(new, 1);
1082 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1084 int type = CURSEG_HOT_DATA;
1086 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1087 /* restore for compacted data summary */
1088 if (read_compacted_summaries(sbi))
1090 type = CURSEG_HOT_NODE;
1093 for (; type <= CURSEG_COLD_NODE; type++)
1094 if (read_normal_summaries(sbi, type))
1099 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1102 unsigned char *kaddr;
1103 struct f2fs_summary *summary;
1104 struct curseg_info *seg_i;
1105 int written_size = 0;
1108 page = grab_meta_page(sbi, blkaddr++);
1109 kaddr = (unsigned char *)page_address(page);
1111 /* Step 1: write nat cache */
1112 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1113 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1114 written_size += SUM_JOURNAL_SIZE;
1116 /* Step 2: write sit cache */
1117 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1118 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1120 written_size += SUM_JOURNAL_SIZE;
1122 set_page_dirty(page);
1124 /* Step 3: write summary entries */
1125 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1126 unsigned short blkoff;
1127 seg_i = CURSEG_I(sbi, i);
1128 if (sbi->ckpt->alloc_type[i] == SSR)
1129 blkoff = sbi->blocks_per_seg;
1131 blkoff = curseg_blkoff(sbi, i);
1133 for (j = 0; j < blkoff; j++) {
1135 page = grab_meta_page(sbi, blkaddr++);
1136 kaddr = (unsigned char *)page_address(page);
1139 summary = (struct f2fs_summary *)(kaddr + written_size);
1140 *summary = seg_i->sum_blk->entries[j];
1141 written_size += SUMMARY_SIZE;
1142 set_page_dirty(page);
1144 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1148 f2fs_put_page(page, 1);
1153 f2fs_put_page(page, 1);
1156 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1157 block_t blkaddr, int type)
1160 if (IS_DATASEG(type))
1161 end = type + NR_CURSEG_DATA_TYPE;
1163 end = type + NR_CURSEG_NODE_TYPE;
1165 for (i = type; i < end; i++) {
1166 struct curseg_info *sum = CURSEG_I(sbi, i);
1167 mutex_lock(&sum->curseg_mutex);
1168 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1169 mutex_unlock(&sum->curseg_mutex);
1173 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1175 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1176 write_compacted_summaries(sbi, start_blk);
1178 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1181 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1183 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1184 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1188 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1189 unsigned int val, int alloc)
1193 if (type == NAT_JOURNAL) {
1194 for (i = 0; i < nats_in_cursum(sum); i++) {
1195 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1198 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1199 return update_nats_in_cursum(sum, 1);
1200 } else if (type == SIT_JOURNAL) {
1201 for (i = 0; i < sits_in_cursum(sum); i++)
1202 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1204 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1205 return update_sits_in_cursum(sum, 1);
1210 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1213 struct sit_info *sit_i = SIT_I(sbi);
1214 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1215 block_t blk_addr = sit_i->sit_base_addr + offset;
1217 check_seg_range(sbi, segno);
1219 /* calculate sit block address */
1220 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1221 blk_addr += sit_i->sit_blocks;
1223 return get_meta_page(sbi, blk_addr);
1226 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1229 struct sit_info *sit_i = SIT_I(sbi);
1230 struct page *src_page, *dst_page;
1231 pgoff_t src_off, dst_off;
1232 void *src_addr, *dst_addr;
1234 src_off = current_sit_addr(sbi, start);
1235 dst_off = next_sit_addr(sbi, src_off);
1237 /* get current sit block page without lock */
1238 src_page = get_meta_page(sbi, src_off);
1239 dst_page = grab_meta_page(sbi, dst_off);
1240 BUG_ON(PageDirty(src_page));
1242 src_addr = page_address(src_page);
1243 dst_addr = page_address(dst_page);
1244 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1246 set_page_dirty(dst_page);
1247 f2fs_put_page(src_page, 1);
1249 set_to_next_sit(sit_i, start);
1254 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1256 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1257 struct f2fs_summary_block *sum = curseg->sum_blk;
1261 * If the journal area in the current summary is full of sit entries,
1262 * all the sit entries will be flushed. Otherwise the sit entries
1263 * are not able to replace with newly hot sit entries.
1265 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1266 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1268 segno = le32_to_cpu(segno_in_journal(sum, i));
1269 __mark_sit_entry_dirty(sbi, segno);
1271 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1278 * CP calls this function, which flushes SIT entries including sit_journal,
1279 * and moves prefree segs to free segs.
1281 void flush_sit_entries(struct f2fs_sb_info *sbi)
1283 struct sit_info *sit_i = SIT_I(sbi);
1284 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1285 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1286 struct f2fs_summary_block *sum = curseg->sum_blk;
1287 unsigned long nsegs = TOTAL_SEGS(sbi);
1288 struct page *page = NULL;
1289 struct f2fs_sit_block *raw_sit = NULL;
1290 unsigned int start = 0, end = 0;
1291 unsigned int segno = -1;
1294 mutex_lock(&curseg->curseg_mutex);
1295 mutex_lock(&sit_i->sentry_lock);
1298 * "flushed" indicates whether sit entries in journal are flushed
1299 * to the SIT area or not.
1301 flushed = flush_sits_in_journal(sbi);
1303 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1304 struct seg_entry *se = get_seg_entry(sbi, segno);
1305 int sit_offset, offset;
1307 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1312 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1314 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1315 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1319 if (!page || (start > segno) || (segno > end)) {
1321 f2fs_put_page(page, 1);
1325 start = START_SEGNO(sit_i, segno);
1326 end = start + SIT_ENTRY_PER_BLOCK - 1;
1328 /* read sit block that will be updated */
1329 page = get_next_sit_page(sbi, start);
1330 raw_sit = page_address(page);
1333 /* udpate entry in SIT block */
1334 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1336 __clear_bit(segno, bitmap);
1337 sit_i->dirty_sentries--;
1339 mutex_unlock(&sit_i->sentry_lock);
1340 mutex_unlock(&curseg->curseg_mutex);
1342 /* writeout last modified SIT block */
1343 f2fs_put_page(page, 1);
1345 set_prefree_as_free_segments(sbi);
1348 static int build_sit_info(struct f2fs_sb_info *sbi)
1350 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1351 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1352 struct sit_info *sit_i;
1353 unsigned int sit_segs, start;
1354 char *src_bitmap, *dst_bitmap;
1355 unsigned int bitmap_size;
1357 /* allocate memory for SIT information */
1358 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1362 SM_I(sbi)->sit_info = sit_i;
1364 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1365 if (!sit_i->sentries)
1368 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1369 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1370 if (!sit_i->dirty_sentries_bitmap)
1373 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1374 sit_i->sentries[start].cur_valid_map
1375 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1376 sit_i->sentries[start].ckpt_valid_map
1377 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1378 if (!sit_i->sentries[start].cur_valid_map
1379 || !sit_i->sentries[start].ckpt_valid_map)
1383 if (sbi->segs_per_sec > 1) {
1384 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1385 sizeof(struct sec_entry));
1386 if (!sit_i->sec_entries)
1390 /* get information related with SIT */
1391 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1393 /* setup SIT bitmap from ckeckpoint pack */
1394 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1395 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1397 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1401 /* init SIT information */
1402 sit_i->s_ops = &default_salloc_ops;
1404 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1405 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1406 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1407 sit_i->sit_bitmap = dst_bitmap;
1408 sit_i->bitmap_size = bitmap_size;
1409 sit_i->dirty_sentries = 0;
1410 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1411 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1412 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1413 mutex_init(&sit_i->sentry_lock);
1417 static int build_free_segmap(struct f2fs_sb_info *sbi)
1419 struct f2fs_sm_info *sm_info = SM_I(sbi);
1420 struct free_segmap_info *free_i;
1421 unsigned int bitmap_size, sec_bitmap_size;
1423 /* allocate memory for free segmap information */
1424 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1428 SM_I(sbi)->free_info = free_i;
1430 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1431 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1432 if (!free_i->free_segmap)
1435 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1436 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1437 if (!free_i->free_secmap)
1440 /* set all segments as dirty temporarily */
1441 memset(free_i->free_segmap, 0xff, bitmap_size);
1442 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1444 /* init free segmap information */
1445 free_i->start_segno =
1446 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1447 free_i->free_segments = 0;
1448 free_i->free_sections = 0;
1449 rwlock_init(&free_i->segmap_lock);
1453 static int build_curseg(struct f2fs_sb_info *sbi)
1455 struct curseg_info *array;
1458 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1462 SM_I(sbi)->curseg_array = array;
1464 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1465 mutex_init(&array[i].curseg_mutex);
1466 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1467 if (!array[i].sum_blk)
1469 array[i].segno = NULL_SEGNO;
1470 array[i].next_blkoff = 0;
1472 return restore_curseg_summaries(sbi);
1475 static void build_sit_entries(struct f2fs_sb_info *sbi)
1477 struct sit_info *sit_i = SIT_I(sbi);
1478 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1479 struct f2fs_summary_block *sum = curseg->sum_blk;
1482 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1483 struct seg_entry *se = &sit_i->sentries[start];
1484 struct f2fs_sit_block *sit_blk;
1485 struct f2fs_sit_entry sit;
1489 mutex_lock(&curseg->curseg_mutex);
1490 for (i = 0; i < sits_in_cursum(sum); i++) {
1491 if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1492 sit = sit_in_journal(sum, i);
1493 mutex_unlock(&curseg->curseg_mutex);
1497 mutex_unlock(&curseg->curseg_mutex);
1498 page = get_current_sit_page(sbi, start);
1499 sit_blk = (struct f2fs_sit_block *)page_address(page);
1500 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1501 f2fs_put_page(page, 1);
1503 check_block_count(sbi, start, &sit);
1504 seg_info_from_raw_sit(se, &sit);
1505 if (sbi->segs_per_sec > 1) {
1506 struct sec_entry *e = get_sec_entry(sbi, start);
1507 e->valid_blocks += se->valid_blocks;
1512 static void init_free_segmap(struct f2fs_sb_info *sbi)
1517 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1518 struct seg_entry *sentry = get_seg_entry(sbi, start);
1519 if (!sentry->valid_blocks)
1520 __set_free(sbi, start);
1523 /* set use the current segments */
1524 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1525 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1526 __set_test_and_inuse(sbi, curseg_t->segno);
1530 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1532 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1533 struct free_segmap_info *free_i = FREE_I(sbi);
1534 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1535 unsigned short valid_blocks;
1538 /* find dirty segment based on free segmap */
1539 segno = find_next_inuse(free_i, total_segs, offset);
1540 if (segno >= total_segs)
1543 valid_blocks = get_valid_blocks(sbi, segno, 0);
1544 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1546 mutex_lock(&dirty_i->seglist_lock);
1547 __locate_dirty_segment(sbi, segno, DIRTY);
1548 mutex_unlock(&dirty_i->seglist_lock);
1552 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1554 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1555 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1557 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1558 if (!dirty_i->victim_secmap)
1563 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1565 struct dirty_seglist_info *dirty_i;
1566 unsigned int bitmap_size, i;
1568 /* allocate memory for dirty segments list information */
1569 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1573 SM_I(sbi)->dirty_info = dirty_i;
1574 mutex_init(&dirty_i->seglist_lock);
1576 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1578 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1579 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1580 if (!dirty_i->dirty_segmap[i])
1584 init_dirty_segmap(sbi);
1585 return init_victim_secmap(sbi);
1589 * Update min, max modified time for cost-benefit GC algorithm
1591 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1593 struct sit_info *sit_i = SIT_I(sbi);
1596 mutex_lock(&sit_i->sentry_lock);
1598 sit_i->min_mtime = LLONG_MAX;
1600 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1602 unsigned long long mtime = 0;
1604 for (i = 0; i < sbi->segs_per_sec; i++)
1605 mtime += get_seg_entry(sbi, segno + i)->mtime;
1607 mtime = div_u64(mtime, sbi->segs_per_sec);
1609 if (sit_i->min_mtime > mtime)
1610 sit_i->min_mtime = mtime;
1612 sit_i->max_mtime = get_mtime(sbi);
1613 mutex_unlock(&sit_i->sentry_lock);
1616 int build_segment_manager(struct f2fs_sb_info *sbi)
1618 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1619 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1620 struct f2fs_sm_info *sm_info;
1623 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1628 sbi->sm_info = sm_info;
1629 INIT_LIST_HEAD(&sm_info->wblist_head);
1630 spin_lock_init(&sm_info->wblist_lock);
1631 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1632 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1633 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1634 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1635 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1636 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1637 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1639 err = build_sit_info(sbi);
1642 err = build_free_segmap(sbi);
1645 err = build_curseg(sbi);
1649 /* reinit free segmap based on SIT */
1650 build_sit_entries(sbi);
1652 init_free_segmap(sbi);
1653 err = build_dirty_segmap(sbi);
1657 init_min_max_mtime(sbi);
1661 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1662 enum dirty_type dirty_type)
1664 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1666 mutex_lock(&dirty_i->seglist_lock);
1667 kfree(dirty_i->dirty_segmap[dirty_type]);
1668 dirty_i->nr_dirty[dirty_type] = 0;
1669 mutex_unlock(&dirty_i->seglist_lock);
1672 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1674 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1675 kfree(dirty_i->victim_secmap);
1678 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1680 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1686 /* discard pre-free/dirty segments list */
1687 for (i = 0; i < NR_DIRTY_TYPE; i++)
1688 discard_dirty_segmap(sbi, i);
1690 destroy_victim_secmap(sbi);
1691 SM_I(sbi)->dirty_info = NULL;
1695 static void destroy_curseg(struct f2fs_sb_info *sbi)
1697 struct curseg_info *array = SM_I(sbi)->curseg_array;
1702 SM_I(sbi)->curseg_array = NULL;
1703 for (i = 0; i < NR_CURSEG_TYPE; i++)
1704 kfree(array[i].sum_blk);
1708 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1710 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1713 SM_I(sbi)->free_info = NULL;
1714 kfree(free_i->free_segmap);
1715 kfree(free_i->free_secmap);
1719 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1721 struct sit_info *sit_i = SIT_I(sbi);
1727 if (sit_i->sentries) {
1728 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1729 kfree(sit_i->sentries[start].cur_valid_map);
1730 kfree(sit_i->sentries[start].ckpt_valid_map);
1733 vfree(sit_i->sentries);
1734 vfree(sit_i->sec_entries);
1735 kfree(sit_i->dirty_sentries_bitmap);
1737 SM_I(sbi)->sit_info = NULL;
1738 kfree(sit_i->sit_bitmap);
1742 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1744 struct f2fs_sm_info *sm_info = SM_I(sbi);
1745 destroy_dirty_segmap(sbi);
1746 destroy_curseg(sbi);
1747 destroy_free_segmap(sbi);
1748 destroy_sit_info(sbi);
1749 sbi->sm_info = NULL;