aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaegeuk Kim <jaegeuk@kernel.org>2014-09-23 14:23:01 -0400
committerJaegeuk Kim <jaegeuk@kernel.org>2014-09-30 18:34:47 -0400
commit7cd8558baa4e4588a80ecb31cb30784195763cdd (patch)
treef6c9e24be9fbd339bab3bae92b5b7749d28a1636
parent309cc2b6e7ae6672ff9744fe07735ed234a8994e (diff)
f2fs: check the use of macros on block counts and addresses
This patch cleans up the existing and new macros for readability. Rule is like this. ,-----------------------------------------> MAX_BLKADDR -, | ,------------- TOTAL_BLKS ----------------------------, | | | | ,- seg0_blkaddr ,----- sit/nat/ssa/main blkaddress | block | | (SEG0_BLKADDR) | | | | (e.g., MAIN_BLKADDR) | address 0..x................ a b c d ............................. | | global seg# 0...................... m ............................. | | | | `------- MAIN_SEGS -----------' `-------------- TOTAL_SEGS ---------------------------' | | seg# 0..........xx.................. = Note = o GET_SEGNO_FROM_SEG0 : blk address -> global segno o GET_SEGNO : blk address -> segno o START_BLOCK : segno -> starting block address Reviewed-by: Chao Yu <chao2.yu@samsung.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
-rw-r--r--fs/f2fs/checkpoint.c5
-rw-r--r--fs/f2fs/debug.c20
-rw-r--r--fs/f2fs/gc.c7
-rw-r--r--fs/f2fs/recovery.c8
-rw-r--r--fs/f2fs/segment.c85
-rw-r--r--fs/f2fs/segment.h59
6 files changed, 81 insertions, 103 deletions
diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index 4abf0ba01525..dd10a031c052 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -98,7 +98,7 @@ static inline block_t get_max_meta_blks(struct f2fs_sb_info *sbi, int type)
98 case META_CP: 98 case META_CP:
99 return 0; 99 return 0;
100 case META_POR: 100 case META_POR:
101 return SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi); 101 return MAX_BLKADDR(sbi);
102 default: 102 default:
103 BUG(); 103 BUG();
104 } 104 }
@@ -113,7 +113,6 @@ int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, int type
113 struct page *page; 113 struct page *page;
114 block_t blkno = start; 114 block_t blkno = start;
115 block_t max_blks = get_max_meta_blks(sbi, type); 115 block_t max_blks = get_max_meta_blks(sbi, type);
116 block_t min_blks = SM_I(sbi)->seg0_blkaddr;
117 116
118 struct f2fs_io_info fio = { 117 struct f2fs_io_info fio = {
119 .type = META, 118 .type = META,
@@ -146,7 +145,7 @@ int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, int type
146 case META_POR: 145 case META_POR:
147 if (unlikely(blkno >= max_blks)) 146 if (unlikely(blkno >= max_blks))
148 goto out; 147 goto out;
149 if (unlikely(blkno < min_blks)) 148 if (unlikely(blkno < SEG0_BLKADDR(sbi)))
150 goto out; 149 goto out;
151 blk_addr = blkno; 150 blk_addr = blkno;
152 break; 151 break;
diff --git a/fs/f2fs/debug.c b/fs/f2fs/debug.c
index fecebdbfd781..0a91ab813a9e 100644
--- a/fs/f2fs/debug.c
+++ b/fs/f2fs/debug.c
@@ -93,7 +93,7 @@ static void update_sit_info(struct f2fs_sb_info *sbi)
93 total_vblocks = 0; 93 total_vblocks = 0;
94 blks_per_sec = sbi->segs_per_sec * (1 << sbi->log_blocks_per_seg); 94 blks_per_sec = sbi->segs_per_sec * (1 << sbi->log_blocks_per_seg);
95 hblks_per_sec = blks_per_sec / 2; 95 hblks_per_sec = blks_per_sec / 2;
96 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) { 96 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
97 vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec); 97 vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec);
98 dist = abs(vblocks - hblks_per_sec); 98 dist = abs(vblocks - hblks_per_sec);
99 bimodal += dist * dist; 99 bimodal += dist * dist;
@@ -103,7 +103,7 @@ static void update_sit_info(struct f2fs_sb_info *sbi)
103 ndirty++; 103 ndirty++;
104 } 104 }
105 } 105 }
106 dist = TOTAL_SECS(sbi) * hblks_per_sec * hblks_per_sec / 100; 106 dist = MAIN_SECS(sbi) * hblks_per_sec * hblks_per_sec / 100;
107 si->bimodal = bimodal / dist; 107 si->bimodal = bimodal / dist;
108 if (si->dirty_count) 108 if (si->dirty_count)
109 si->avg_vblocks = total_vblocks / ndirty; 109 si->avg_vblocks = total_vblocks / ndirty;
@@ -131,17 +131,17 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
131 131
132 /* build sit */ 132 /* build sit */
133 si->base_mem += sizeof(struct sit_info); 133 si->base_mem += sizeof(struct sit_info);
134 si->base_mem += TOTAL_SEGS(sbi) * sizeof(struct seg_entry); 134 si->base_mem += MAIN_SEGS(sbi) * sizeof(struct seg_entry);
135 si->base_mem += f2fs_bitmap_size(TOTAL_SEGS(sbi)); 135 si->base_mem += f2fs_bitmap_size(MAIN_SEGS(sbi));
136 si->base_mem += 2 * SIT_VBLOCK_MAP_SIZE * TOTAL_SEGS(sbi); 136 si->base_mem += 2 * SIT_VBLOCK_MAP_SIZE * MAIN_SEGS(sbi);
137 if (sbi->segs_per_sec > 1) 137 if (sbi->segs_per_sec > 1)
138 si->base_mem += TOTAL_SECS(sbi) * sizeof(struct sec_entry); 138 si->base_mem += MAIN_SECS(sbi) * sizeof(struct sec_entry);
139 si->base_mem += __bitmap_size(sbi, SIT_BITMAP); 139 si->base_mem += __bitmap_size(sbi, SIT_BITMAP);
140 140
141 /* build free segmap */ 141 /* build free segmap */
142 si->base_mem += sizeof(struct free_segmap_info); 142 si->base_mem += sizeof(struct free_segmap_info);
143 si->base_mem += f2fs_bitmap_size(TOTAL_SEGS(sbi)); 143 si->base_mem += f2fs_bitmap_size(MAIN_SEGS(sbi));
144 si->base_mem += f2fs_bitmap_size(TOTAL_SECS(sbi)); 144 si->base_mem += f2fs_bitmap_size(MAIN_SECS(sbi));
145 145
146 /* build curseg */ 146 /* build curseg */
147 si->base_mem += sizeof(struct curseg_info) * NR_CURSEG_TYPE; 147 si->base_mem += sizeof(struct curseg_info) * NR_CURSEG_TYPE;
@@ -149,8 +149,8 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
149 149
150 /* build dirty segmap */ 150 /* build dirty segmap */
151 si->base_mem += sizeof(struct dirty_seglist_info); 151 si->base_mem += sizeof(struct dirty_seglist_info);
152 si->base_mem += NR_DIRTY_TYPE * f2fs_bitmap_size(TOTAL_SEGS(sbi)); 152 si->base_mem += NR_DIRTY_TYPE * f2fs_bitmap_size(MAIN_SEGS(sbi));
153 si->base_mem += f2fs_bitmap_size(TOTAL_SECS(sbi)); 153 si->base_mem += f2fs_bitmap_size(MAIN_SECS(sbi));
154 154
155 /* build nm */ 155 /* build nm */
156 si->base_mem += sizeof(struct f2fs_nm_info); 156 si->base_mem += sizeof(struct f2fs_nm_info);
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index e88fcf65aa7f..2a8f4acdb86b 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -193,7 +193,7 @@ static unsigned int check_bg_victims(struct f2fs_sb_info *sbi)
193 * selected by background GC before. 193 * selected by background GC before.
194 * Those segments guarantee they have small valid blocks. 194 * Those segments guarantee they have small valid blocks.
195 */ 195 */
196 for_each_set_bit(secno, dirty_i->victim_secmap, TOTAL_SECS(sbi)) { 196 for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) {
197 if (sec_usage_check(sbi, secno)) 197 if (sec_usage_check(sbi, secno))
198 continue; 198 continue;
199 clear_bit(secno, dirty_i->victim_secmap); 199 clear_bit(secno, dirty_i->victim_secmap);
@@ -281,9 +281,8 @@ static int get_victim_by_default(struct f2fs_sb_info *sbi,
281 unsigned long cost; 281 unsigned long cost;
282 unsigned int segno; 282 unsigned int segno;
283 283
284 segno = find_next_bit(p.dirty_segmap, 284 segno = find_next_bit(p.dirty_segmap, MAIN_SEGS(sbi), p.offset);
285 TOTAL_SEGS(sbi), p.offset); 285 if (segno >= MAIN_SEGS(sbi)) {
286 if (segno >= TOTAL_SEGS(sbi)) {
287 if (sbi->last_victim[p.gc_mode]) { 286 if (sbi->last_victim[p.gc_mode]) {
288 sbi->last_victim[p.gc_mode] = 0; 287 sbi->last_victim[p.gc_mode] = 0;
289 p.offset = 0; 288 p.offset = 0;
diff --git a/fs/f2fs/recovery.c b/fs/f2fs/recovery.c
index 947b92273d08..ebd013225788 100644
--- a/fs/f2fs/recovery.c
+++ b/fs/f2fs/recovery.c
@@ -173,8 +173,7 @@ static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
173 while (1) { 173 while (1) {
174 struct fsync_inode_entry *entry; 174 struct fsync_inode_entry *entry;
175 175
176 if (blkaddr < SM_I(sbi)->main_blkaddr || 176 if (blkaddr < MAIN_BLKADDR(sbi) || blkaddr >= MAX_BLKADDR(sbi))
177 blkaddr >= (SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi)))
178 return 0; 177 return 0;
179 178
180 page = get_meta_page_ra(sbi, blkaddr); 179 page = get_meta_page_ra(sbi, blkaddr);
@@ -434,8 +433,7 @@ static int recover_data(struct f2fs_sb_info *sbi,
434 while (1) { 433 while (1) {
435 struct fsync_inode_entry *entry; 434 struct fsync_inode_entry *entry;
436 435
437 if (blkaddr < SM_I(sbi)->main_blkaddr || 436 if (blkaddr < MAIN_BLKADDR(sbi) || blkaddr >= MAX_BLKADDR(sbi))
438 blkaddr >= (SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi)))
439 break; 437 break;
440 438
441 page = get_meta_page_ra(sbi, blkaddr); 439 page = get_meta_page_ra(sbi, blkaddr);
@@ -525,7 +523,7 @@ out:
525 523
526 /* truncate meta pages to be used by the recovery */ 524 /* truncate meta pages to be used by the recovery */
527 truncate_inode_pages_range(META_MAPPING(sbi), 525 truncate_inode_pages_range(META_MAPPING(sbi),
528 SM_I(sbi)->main_blkaddr << PAGE_CACHE_SHIFT, -1); 526 MAIN_BLKADDR(sbi) << PAGE_CACHE_SHIFT, -1);
529 527
530 if (err) { 528 if (err) {
531 truncate_inode_pages_final(NODE_MAPPING(sbi)); 529 truncate_inode_pages_final(NODE_MAPPING(sbi));
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index fc87da189884..4d1c49a55e0c 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -476,10 +476,9 @@ static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
476{ 476{
477 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 477 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
478 unsigned int segno; 478 unsigned int segno;
479 unsigned int total_segs = TOTAL_SEGS(sbi);
480 479
481 mutex_lock(&dirty_i->seglist_lock); 480 mutex_lock(&dirty_i->seglist_lock);
482 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs) 481 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
483 __set_test_and_free(sbi, segno); 482 __set_test_and_free(sbi, segno);
484 mutex_unlock(&dirty_i->seglist_lock); 483 mutex_unlock(&dirty_i->seglist_lock);
485} 484}
@@ -490,17 +489,17 @@ void clear_prefree_segments(struct f2fs_sb_info *sbi)
490 struct discard_entry *entry, *this; 489 struct discard_entry *entry, *this;
491 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 490 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
492 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; 491 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
493 unsigned int total_segs = TOTAL_SEGS(sbi);
494 unsigned int start = 0, end = -1; 492 unsigned int start = 0, end = -1;
495 493
496 mutex_lock(&dirty_i->seglist_lock); 494 mutex_lock(&dirty_i->seglist_lock);
497 495
498 while (1) { 496 while (1) {
499 int i; 497 int i;
500 start = find_next_bit(prefree_map, total_segs, end + 1); 498 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
501 if (start >= total_segs) 499 if (start >= MAIN_SEGS(sbi))
502 break; 500 break;
503 end = find_next_zero_bit(prefree_map, total_segs, start + 1); 501 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
502 start + 1);
504 503
505 for (i = start; i < end; i++) 504 for (i = start; i < end; i++)
506 clear_bit(i, prefree_map); 505 clear_bit(i, prefree_map);
@@ -675,7 +674,7 @@ static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
675 unsigned int segno = curseg->segno + 1; 674 unsigned int segno = curseg->segno + 1;
676 struct free_segmap_info *free_i = FREE_I(sbi); 675 struct free_segmap_info *free_i = FREE_I(sbi);
677 676
678 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec) 677 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
679 return !test_bit(segno, free_i->free_segmap); 678 return !test_bit(segno, free_i->free_segmap);
680 return 0; 679 return 0;
681} 680}
@@ -689,7 +688,7 @@ static void get_new_segment(struct f2fs_sb_info *sbi,
689{ 688{
690 struct free_segmap_info *free_i = FREE_I(sbi); 689 struct free_segmap_info *free_i = FREE_I(sbi);
691 unsigned int segno, secno, zoneno; 690 unsigned int segno, secno, zoneno;
692 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone; 691 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
693 unsigned int hint = *newseg / sbi->segs_per_sec; 692 unsigned int hint = *newseg / sbi->segs_per_sec;
694 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg); 693 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
695 unsigned int left_start = hint; 694 unsigned int left_start = hint;
@@ -701,18 +700,18 @@ static void get_new_segment(struct f2fs_sb_info *sbi,
701 700
702 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { 701 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
703 segno = find_next_zero_bit(free_i->free_segmap, 702 segno = find_next_zero_bit(free_i->free_segmap,
704 TOTAL_SEGS(sbi), *newseg + 1); 703 MAIN_SEGS(sbi), *newseg + 1);
705 if (segno - *newseg < sbi->segs_per_sec - 704 if (segno - *newseg < sbi->segs_per_sec -
706 (*newseg % sbi->segs_per_sec)) 705 (*newseg % sbi->segs_per_sec))
707 goto got_it; 706 goto got_it;
708 } 707 }
709find_other_zone: 708find_other_zone:
710 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint); 709 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
711 if (secno >= TOTAL_SECS(sbi)) { 710 if (secno >= MAIN_SECS(sbi)) {
712 if (dir == ALLOC_RIGHT) { 711 if (dir == ALLOC_RIGHT) {
713 secno = find_next_zero_bit(free_i->free_secmap, 712 secno = find_next_zero_bit(free_i->free_secmap,
714 TOTAL_SECS(sbi), 0); 713 MAIN_SECS(sbi), 0);
715 f2fs_bug_on(sbi, secno >= TOTAL_SECS(sbi)); 714 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
716 } else { 715 } else {
717 go_left = 1; 716 go_left = 1;
718 left_start = hint - 1; 717 left_start = hint - 1;
@@ -727,8 +726,8 @@ find_other_zone:
727 continue; 726 continue;
728 } 727 }
729 left_start = find_next_zero_bit(free_i->free_secmap, 728 left_start = find_next_zero_bit(free_i->free_secmap,
730 TOTAL_SECS(sbi), 0); 729 MAIN_SECS(sbi), 0);
731 f2fs_bug_on(sbi, left_start >= TOTAL_SECS(sbi)); 730 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
732 break; 731 break;
733 } 732 }
734 secno = left_start; 733 secno = left_start;
@@ -941,26 +940,22 @@ static const struct segment_allocation default_salloc_ops = {
941 940
942int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) 941int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
943{ 942{
944 block_t start_addr = SM_I(sbi)->main_blkaddr;
945 __u64 start = range->start >> sbi->log_blocksize; 943 __u64 start = range->start >> sbi->log_blocksize;
946 __u64 end = start + (range->len >> sbi->log_blocksize) - 1; 944 __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
947 __u64 segment = 1 << (sbi->log_blocksize + sbi->log_blocks_per_seg);
948 unsigned int start_segno, end_segno; 945 unsigned int start_segno, end_segno;
949 struct cp_control cpc; 946 struct cp_control cpc;
950 947
951 if (range->minlen > segment || 948 if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
952 start >= SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi) || 949 range->len < sbi->blocksize)
953 range->len < sbi->blocksize)
954 return -EINVAL; 950 return -EINVAL;
955 951
956 if (end <= start_addr) 952 if (end <= MAIN_BLKADDR(sbi))
957 goto out; 953 goto out;
958 954
959 /* start/end segment number in main_area */ 955 /* start/end segment number in main_area */
960 start_segno = (start <= start_addr) ? 0 : GET_SEGNO(sbi, start); 956 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
961 end_segno = (end >= SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi)) ? 957 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
962 TOTAL_SEGS(sbi) - 1 : GET_SEGNO(sbi, end); 958 GET_SEGNO(sbi, end);
963
964 cpc.reason = CP_DISCARD; 959 cpc.reason = CP_DISCARD;
965 cpc.trim_start = start_segno; 960 cpc.trim_start = start_segno;
966 cpc.trim_end = end_segno; 961 cpc.trim_end = end_segno;
@@ -1571,10 +1566,9 @@ static void add_sits_in_set(struct f2fs_sb_info *sbi)
1571 struct f2fs_sm_info *sm_info = SM_I(sbi); 1566 struct f2fs_sm_info *sm_info = SM_I(sbi);
1572 struct list_head *set_list = &sm_info->sit_entry_set; 1567 struct list_head *set_list = &sm_info->sit_entry_set;
1573 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; 1568 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
1574 unsigned long nsegs = TOTAL_SEGS(sbi);
1575 unsigned int segno; 1569 unsigned int segno;
1576 1570
1577 for_each_set_bit(segno, bitmap, nsegs) 1571 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
1578 add_sit_entry(segno, set_list); 1572 add_sit_entry(segno, set_list);
1579} 1573}
1580 1574
@@ -1609,7 +1603,6 @@ void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1609 struct f2fs_summary_block *sum = curseg->sum_blk; 1603 struct f2fs_summary_block *sum = curseg->sum_blk;
1610 struct sit_entry_set *ses, *tmp; 1604 struct sit_entry_set *ses, *tmp;
1611 struct list_head *head = &SM_I(sbi)->sit_entry_set; 1605 struct list_head *head = &SM_I(sbi)->sit_entry_set;
1612 unsigned long nsegs = TOTAL_SEGS(sbi);
1613 bool to_journal = true; 1606 bool to_journal = true;
1614 struct seg_entry *se; 1607 struct seg_entry *se;
1615 1608
@@ -1643,7 +1636,7 @@ void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1643 struct f2fs_sit_block *raw_sit = NULL; 1636 struct f2fs_sit_block *raw_sit = NULL;
1644 unsigned int start_segno = ses->start_segno; 1637 unsigned int start_segno = ses->start_segno;
1645 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, 1638 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
1646 nsegs); 1639 (unsigned long)MAIN_SEGS(sbi));
1647 unsigned int segno = start_segno; 1640 unsigned int segno = start_segno;
1648 1641
1649 if (to_journal && 1642 if (to_journal &&
@@ -1722,16 +1715,16 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
1722 1715
1723 SM_I(sbi)->sit_info = sit_i; 1716 SM_I(sbi)->sit_info = sit_i;
1724 1717
1725 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry)); 1718 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
1726 if (!sit_i->sentries) 1719 if (!sit_i->sentries)
1727 return -ENOMEM; 1720 return -ENOMEM;
1728 1721
1729 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi)); 1722 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
1730 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL); 1723 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1731 if (!sit_i->dirty_sentries_bitmap) 1724 if (!sit_i->dirty_sentries_bitmap)
1732 return -ENOMEM; 1725 return -ENOMEM;
1733 1726
1734 for (start = 0; start < TOTAL_SEGS(sbi); start++) { 1727 for (start = 0; start < MAIN_SEGS(sbi); start++) {
1735 sit_i->sentries[start].cur_valid_map 1728 sit_i->sentries[start].cur_valid_map
1736 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 1729 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1737 sit_i->sentries[start].ckpt_valid_map 1730 sit_i->sentries[start].ckpt_valid_map
@@ -1742,7 +1735,7 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
1742 } 1735 }
1743 1736
1744 if (sbi->segs_per_sec > 1) { 1737 if (sbi->segs_per_sec > 1) {
1745 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) * 1738 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
1746 sizeof(struct sec_entry)); 1739 sizeof(struct sec_entry));
1747 if (!sit_i->sec_entries) 1740 if (!sit_i->sec_entries)
1748 return -ENOMEM; 1741 return -ENOMEM;
@@ -1777,7 +1770,6 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
1777 1770
1778static int build_free_segmap(struct f2fs_sb_info *sbi) 1771static int build_free_segmap(struct f2fs_sb_info *sbi)
1779{ 1772{
1780 struct f2fs_sm_info *sm_info = SM_I(sbi);
1781 struct free_segmap_info *free_i; 1773 struct free_segmap_info *free_i;
1782 unsigned int bitmap_size, sec_bitmap_size; 1774 unsigned int bitmap_size, sec_bitmap_size;
1783 1775
@@ -1788,12 +1780,12 @@ static int build_free_segmap(struct f2fs_sb_info *sbi)
1788 1780
1789 SM_I(sbi)->free_info = free_i; 1781 SM_I(sbi)->free_info = free_i;
1790 1782
1791 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi)); 1783 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
1792 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL); 1784 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1793 if (!free_i->free_segmap) 1785 if (!free_i->free_segmap)
1794 return -ENOMEM; 1786 return -ENOMEM;
1795 1787
1796 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi)); 1788 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
1797 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL); 1789 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1798 if (!free_i->free_secmap) 1790 if (!free_i->free_secmap)
1799 return -ENOMEM; 1791 return -ENOMEM;
@@ -1803,8 +1795,7 @@ static int build_free_segmap(struct f2fs_sb_info *sbi)
1803 memset(free_i->free_secmap, 0xff, sec_bitmap_size); 1795 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1804 1796
1805 /* init free segmap information */ 1797 /* init free segmap information */
1806 free_i->start_segno = 1798 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
1807 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1808 free_i->free_segments = 0; 1799 free_i->free_segments = 0;
1809 free_i->free_sections = 0; 1800 free_i->free_sections = 0;
1810 rwlock_init(&free_i->segmap_lock); 1801 rwlock_init(&free_i->segmap_lock);
@@ -1849,7 +1840,7 @@ static void build_sit_entries(struct f2fs_sb_info *sbi)
1849 start = start_blk * sit_i->sents_per_block; 1840 start = start_blk * sit_i->sents_per_block;
1850 end = (start_blk + readed) * sit_i->sents_per_block; 1841 end = (start_blk + readed) * sit_i->sents_per_block;
1851 1842
1852 for (; start < end && start < TOTAL_SEGS(sbi); start++) { 1843 for (; start < end && start < MAIN_SEGS(sbi); start++) {
1853 struct seg_entry *se = &sit_i->sentries[start]; 1844 struct seg_entry *se = &sit_i->sentries[start];
1854 struct f2fs_sit_block *sit_blk; 1845 struct f2fs_sit_block *sit_blk;
1855 struct f2fs_sit_entry sit; 1846 struct f2fs_sit_entry sit;
@@ -1887,7 +1878,7 @@ static void init_free_segmap(struct f2fs_sb_info *sbi)
1887 unsigned int start; 1878 unsigned int start;
1888 int type; 1879 int type;
1889 1880
1890 for (start = 0; start < TOTAL_SEGS(sbi); start++) { 1881 for (start = 0; start < MAIN_SEGS(sbi); start++) {
1891 struct seg_entry *sentry = get_seg_entry(sbi, start); 1882 struct seg_entry *sentry = get_seg_entry(sbi, start);
1892 if (!sentry->valid_blocks) 1883 if (!sentry->valid_blocks)
1893 __set_free(sbi, start); 1884 __set_free(sbi, start);
@@ -1904,13 +1895,13 @@ static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1904{ 1895{
1905 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 1896 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1906 struct free_segmap_info *free_i = FREE_I(sbi); 1897 struct free_segmap_info *free_i = FREE_I(sbi);
1907 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi); 1898 unsigned int segno = 0, offset = 0;
1908 unsigned short valid_blocks; 1899 unsigned short valid_blocks;
1909 1900
1910 while (1) { 1901 while (1) {
1911 /* find dirty segment based on free segmap */ 1902 /* find dirty segment based on free segmap */
1912 segno = find_next_inuse(free_i, total_segs, offset); 1903 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
1913 if (segno >= total_segs) 1904 if (segno >= MAIN_SEGS(sbi))
1914 break; 1905 break;
1915 offset = segno + 1; 1906 offset = segno + 1;
1916 valid_blocks = get_valid_blocks(sbi, segno, 0); 1907 valid_blocks = get_valid_blocks(sbi, segno, 0);
@@ -1929,7 +1920,7 @@ static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1929static int init_victim_secmap(struct f2fs_sb_info *sbi) 1920static int init_victim_secmap(struct f2fs_sb_info *sbi)
1930{ 1921{
1931 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 1922 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1932 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi)); 1923 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
1933 1924
1934 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL); 1925 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1935 if (!dirty_i->victim_secmap) 1926 if (!dirty_i->victim_secmap)
@@ -1950,7 +1941,7 @@ static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1950 SM_I(sbi)->dirty_info = dirty_i; 1941 SM_I(sbi)->dirty_info = dirty_i;
1951 mutex_init(&dirty_i->seglist_lock); 1942 mutex_init(&dirty_i->seglist_lock);
1952 1943
1953 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi)); 1944 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
1954 1945
1955 for (i = 0; i < NR_DIRTY_TYPE; i++) { 1946 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1956 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL); 1947 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
@@ -1974,7 +1965,7 @@ static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1974 1965
1975 sit_i->min_mtime = LLONG_MAX; 1966 sit_i->min_mtime = LLONG_MAX;
1976 1967
1977 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) { 1968 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
1978 unsigned int i; 1969 unsigned int i;
1979 unsigned long long mtime = 0; 1970 unsigned long long mtime = 0;
1980 1971
@@ -2117,7 +2108,7 @@ static void destroy_sit_info(struct f2fs_sb_info *sbi)
2117 return; 2108 return;
2118 2109
2119 if (sit_i->sentries) { 2110 if (sit_i->sentries) {
2120 for (start = 0; start < TOTAL_SEGS(sbi); start++) { 2111 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2121 kfree(sit_i->sentries[start].cur_valid_map); 2112 kfree(sit_i->sentries[start].cur_valid_map);
2122 kfree(sit_i->sentries[start].ckpt_valid_map); 2113 kfree(sit_i->sentries[start].ckpt_valid_map);
2123 } 2114 }
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
index 4588545b3030..a7e3e9112100 100644
--- a/fs/f2fs/segment.h
+++ b/fs/f2fs/segment.h
@@ -45,16 +45,26 @@
45 (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / \ 45 (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / \
46 sbi->segs_per_sec)) \ 46 sbi->segs_per_sec)) \
47 47
48#define START_BLOCK(sbi, segno) \ 48#define MAIN_BLKADDR(sbi) (SM_I(sbi)->main_blkaddr)
49 (SM_I(sbi)->seg0_blkaddr + \ 49#define SEG0_BLKADDR(sbi) (SM_I(sbi)->seg0_blkaddr)
50
51#define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
52#define MAIN_SECS(sbi) (sbi->total_sections)
53
54#define TOTAL_SEGS(sbi) (SM_I(sbi)->segment_count)
55#define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << sbi->log_blocks_per_seg)
56
57#define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
58#define SEGMENT_SIZE(sbi) (1 << (sbi->log_blocksize + \
59 sbi->log_blocks_per_seg))
60
61#define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) + \
50 (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg)) 62 (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
63
51#define NEXT_FREE_BLKADDR(sbi, curseg) \ 64#define NEXT_FREE_BLKADDR(sbi, curseg) \
52 (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff) 65 (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
53 66
54#define MAIN_BASE_BLOCK(sbi) (SM_I(sbi)->main_blkaddr) 67#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr) - SEG0_BLKADDR(sbi))
55
56#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) \
57 ((blk_addr) - SM_I(sbi)->seg0_blkaddr)
58#define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \ 68#define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
59 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg) 69 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
60#define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \ 70#define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
@@ -82,12 +92,9 @@
82#define START_SEGNO(segno) \ 92#define START_SEGNO(segno) \
83 (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK) 93 (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
84#define SIT_BLK_CNT(sbi) \ 94#define SIT_BLK_CNT(sbi) \
85 ((TOTAL_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK) 95 ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
86#define f2fs_bitmap_size(nr) \ 96#define f2fs_bitmap_size(nr) \
87 (BITS_TO_LONGS(nr) * sizeof(unsigned long)) 97 (BITS_TO_LONGS(nr) * sizeof(unsigned long))
88#define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
89#define TOTAL_SECS(sbi) (sbi->total_sections)
90#define TOTAL_BLKS(sbi) (SM_I(sbi)->segment_count << sbi->log_blocks_per_seg)
91 98
92#define SECTOR_FROM_BLOCK(blk_addr) \ 99#define SECTOR_FROM_BLOCK(blk_addr) \
93 (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK) 100 (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
@@ -323,7 +330,7 @@ static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
323 clear_bit(segno, free_i->free_segmap); 330 clear_bit(segno, free_i->free_segmap);
324 free_i->free_segments++; 331 free_i->free_segments++;
325 332
326 next = find_next_bit(free_i->free_segmap, TOTAL_SEGS(sbi), start_segno); 333 next = find_next_bit(free_i->free_segmap, MAIN_SEGS(sbi), start_segno);
327 if (next >= start_segno + sbi->segs_per_sec) { 334 if (next >= start_segno + sbi->segs_per_sec) {
328 clear_bit(secno, free_i->free_secmap); 335 clear_bit(secno, free_i->free_secmap);
329 free_i->free_sections++; 336 free_i->free_sections++;
@@ -542,18 +549,13 @@ static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
542#ifdef CONFIG_F2FS_CHECK_FS 549#ifdef CONFIG_F2FS_CHECK_FS
543static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno) 550static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
544{ 551{
545 unsigned int end_segno = SM_I(sbi)->segment_count - 1; 552 BUG_ON(segno > TOTAL_SEGS(sbi) - 1);
546 BUG_ON(segno > end_segno);
547} 553}
548 554
549static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr) 555static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
550{ 556{
551 struct f2fs_sm_info *sm_info = SM_I(sbi); 557 BUG_ON(blk_addr < SEG0_BLKADDR(sbi));
552 block_t total_blks = TOTAL_BLKS(sbi); 558 BUG_ON(blk_addr >= MAX_BLKADDR(sbi));
553 block_t start_addr = sm_info->seg0_blkaddr;
554 block_t end_addr = start_addr + total_blks - 1;
555 BUG_ON(blk_addr < start_addr);
556 BUG_ON(blk_addr > end_addr);
557} 559}
558 560
559/* 561/*
@@ -562,8 +564,6 @@ static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
562static inline void check_block_count(struct f2fs_sb_info *sbi, 564static inline void check_block_count(struct f2fs_sb_info *sbi,
563 int segno, struct f2fs_sit_entry *raw_sit) 565 int segno, struct f2fs_sit_entry *raw_sit)
564{ 566{
565 struct f2fs_sm_info *sm_info = SM_I(sbi);
566 unsigned int end_segno = sm_info->segment_count - 1;
567 bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false; 567 bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
568 int valid_blocks = 0; 568 int valid_blocks = 0;
569 int cur_pos = 0, next_pos; 569 int cur_pos = 0, next_pos;
@@ -572,7 +572,7 @@ static inline void check_block_count(struct f2fs_sb_info *sbi,
572 BUG_ON(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg); 572 BUG_ON(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg);
573 573
574 /* check boundary of a given segment number */ 574 /* check boundary of a given segment number */
575 BUG_ON(segno > end_segno); 575 BUG_ON(segno > TOTAL_SEGS(sbi) - 1);
576 576
577 /* check bitmap with valid block count */ 577 /* check bitmap with valid block count */
578 do { 578 do {
@@ -593,20 +593,13 @@ static inline void check_block_count(struct f2fs_sb_info *sbi,
593#else 593#else
594static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno) 594static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
595{ 595{
596 unsigned int end_segno = SM_I(sbi)->segment_count - 1; 596 if (segno > TOTAL_SEGS(sbi) - 1)
597
598 if (segno > end_segno)
599 sbi->need_fsck = true; 597 sbi->need_fsck = true;
600} 598}
601 599
602static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr) 600static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
603{ 601{
604 struct f2fs_sm_info *sm_info = SM_I(sbi); 602 if (blk_addr < SEG0_BLKADDR(sbi) || blk_addr >= MAX_BLKADDR(sbi))
605 block_t total_blks = TOTAL_BLKS(sbi);
606 block_t start_addr = sm_info->seg0_blkaddr;
607 block_t end_addr = start_addr + total_blks - 1;
608
609 if (blk_addr < start_addr || blk_addr > end_addr)
610 sbi->need_fsck = true; 603 sbi->need_fsck = true;
611} 604}
612 605
@@ -616,14 +609,12 @@ static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
616static inline void check_block_count(struct f2fs_sb_info *sbi, 609static inline void check_block_count(struct f2fs_sb_info *sbi,
617 int segno, struct f2fs_sit_entry *raw_sit) 610 int segno, struct f2fs_sit_entry *raw_sit)
618{ 611{
619 unsigned int end_segno = SM_I(sbi)->segment_count - 1;
620
621 /* check segment usage */ 612 /* check segment usage */
622 if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg) 613 if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg)
623 sbi->need_fsck = true; 614 sbi->need_fsck = true;
624 615
625 /* check boundary of a given segment number */ 616 /* check boundary of a given segment number */
626 if (segno > end_segno) 617 if (segno > TOTAL_SEGS(sbi) - 1)
627 sbi->need_fsck = true; 618 sbi->need_fsck = true;
628} 619}
629#endif 620#endif