{
        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
        unsigned int segno;
-       unsigned int total_segs = TOTAL_SEGS(sbi);
 
        mutex_lock(&dirty_i->seglist_lock);
-       for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], total_segs)
+       for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
                __set_test_and_free(sbi, segno);
        mutex_unlock(&dirty_i->seglist_lock);
 }
        struct discard_entry *entry, *this;
        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
        unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
-       unsigned int total_segs = TOTAL_SEGS(sbi);
        unsigned int start = 0, end = -1;
 
        mutex_lock(&dirty_i->seglist_lock);
 
        while (1) {
                int i;
-               start = find_next_bit(prefree_map, total_segs, end + 1);
-               if (start >= total_segs)
+               start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
+               if (start >= MAIN_SEGS(sbi))
                        break;
-               end = find_next_zero_bit(prefree_map, total_segs, start + 1);
+               end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
+                                                               start + 1);
 
                for (i = start; i < end; i++)
                        clear_bit(i, prefree_map);
        unsigned int segno = curseg->segno + 1;
        struct free_segmap_info *free_i = FREE_I(sbi);
 
-       if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
+       if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
                return !test_bit(segno, free_i->free_segmap);
        return 0;
 }
 {
        struct free_segmap_info *free_i = FREE_I(sbi);
        unsigned int segno, secno, zoneno;
-       unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
+       unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
        unsigned int hint = *newseg / sbi->segs_per_sec;
        unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
        unsigned int left_start = hint;
 
        if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
                segno = find_next_zero_bit(free_i->free_segmap,
-                                       TOTAL_SEGS(sbi), *newseg + 1);
+                                       MAIN_SEGS(sbi), *newseg + 1);
                if (segno - *newseg < sbi->segs_per_sec -
                                        (*newseg % sbi->segs_per_sec))
                        goto got_it;
        }
 find_other_zone:
-       secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
-       if (secno >= TOTAL_SECS(sbi)) {
+       secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
+       if (secno >= MAIN_SECS(sbi)) {
                if (dir == ALLOC_RIGHT) {
                        secno = find_next_zero_bit(free_i->free_secmap,
-                                                       TOTAL_SECS(sbi), 0);
-                       f2fs_bug_on(sbi, secno >= TOTAL_SECS(sbi));
+                                                       MAIN_SECS(sbi), 0);
+                       f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
                } else {
                        go_left = 1;
                        left_start = hint - 1;
                        continue;
                }
                left_start = find_next_zero_bit(free_i->free_secmap,
-                                                       TOTAL_SECS(sbi), 0);
-               f2fs_bug_on(sbi, left_start >= TOTAL_SECS(sbi));
+                                                       MAIN_SECS(sbi), 0);
+               f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
                break;
        }
        secno = left_start;
 
 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
 {
-       block_t start_addr = SM_I(sbi)->main_blkaddr;
        __u64 start = range->start >> sbi->log_blocksize;
        __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
-       __u64 segment = 1 << (sbi->log_blocksize + sbi->log_blocks_per_seg);
        unsigned int start_segno, end_segno;
        struct cp_control cpc;
 
-       if (range->minlen > segment ||
-                       start >= SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi) ||
-                       range->len < sbi->blocksize)
+       if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
+                                               range->len < sbi->blocksize)
                return -EINVAL;
 
-       if (end <= start_addr)
+       if (end <= MAIN_BLKADDR(sbi))
                goto out;
 
        /* start/end segment number in main_area */
-       start_segno = (start <= start_addr) ? 0 : GET_SEGNO(sbi, start);
-       end_segno = (end >= SM_I(sbi)->seg0_blkaddr + TOTAL_BLKS(sbi)) ?
-                               TOTAL_SEGS(sbi) - 1 : GET_SEGNO(sbi, end);
-
+       start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
+       end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
+                                               GET_SEGNO(sbi, end);
        cpc.reason = CP_DISCARD;
        cpc.trim_start = start_segno;
        cpc.trim_end = end_segno;
        struct f2fs_sm_info *sm_info = SM_I(sbi);
        struct list_head *set_list = &sm_info->sit_entry_set;
        unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
-       unsigned long nsegs = TOTAL_SEGS(sbi);
        unsigned int segno;
 
-       for_each_set_bit(segno, bitmap, nsegs)
+       for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
                add_sit_entry(segno, set_list);
 }
 
        struct f2fs_summary_block *sum = curseg->sum_blk;
        struct sit_entry_set *ses, *tmp;
        struct list_head *head = &SM_I(sbi)->sit_entry_set;
-       unsigned long nsegs = TOTAL_SEGS(sbi);
        bool to_journal = true;
        struct seg_entry *se;
 
                struct f2fs_sit_block *raw_sit = NULL;
                unsigned int start_segno = ses->start_segno;
                unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
-                                                               nsegs);
+                                               (unsigned long)MAIN_SEGS(sbi));
                unsigned int segno = start_segno;
 
                if (to_journal &&
 
        SM_I(sbi)->sit_info = sit_i;
 
-       sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
+       sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
        if (!sit_i->sentries)
                return -ENOMEM;
 
-       bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
+       bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
        sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
        if (!sit_i->dirty_sentries_bitmap)
                return -ENOMEM;
 
-       for (start = 0; start < TOTAL_SEGS(sbi); start++) {
+       for (start = 0; start < MAIN_SEGS(sbi); start++) {
                sit_i->sentries[start].cur_valid_map
                        = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
                sit_i->sentries[start].ckpt_valid_map
        }
 
        if (sbi->segs_per_sec > 1) {
-               sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
+               sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
                                        sizeof(struct sec_entry));
                if (!sit_i->sec_entries)
                        return -ENOMEM;
 
 static int build_free_segmap(struct f2fs_sb_info *sbi)
 {
-       struct f2fs_sm_info *sm_info = SM_I(sbi);
        struct free_segmap_info *free_i;
        unsigned int bitmap_size, sec_bitmap_size;
 
 
        SM_I(sbi)->free_info = free_i;
 
-       bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
+       bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
        free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
        if (!free_i->free_segmap)
                return -ENOMEM;
 
-       sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
+       sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
        free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
        if (!free_i->free_secmap)
                return -ENOMEM;
        memset(free_i->free_secmap, 0xff, sec_bitmap_size);
 
        /* init free segmap information */
-       free_i->start_segno =
-               (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
+       free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
        free_i->free_segments = 0;
        free_i->free_sections = 0;
        rwlock_init(&free_i->segmap_lock);
                start = start_blk * sit_i->sents_per_block;
                end = (start_blk + readed) * sit_i->sents_per_block;
 
-               for (; start < end && start < TOTAL_SEGS(sbi); start++) {
+               for (; start < end && start < MAIN_SEGS(sbi); start++) {
                        struct seg_entry *se = &sit_i->sentries[start];
                        struct f2fs_sit_block *sit_blk;
                        struct f2fs_sit_entry sit;
        unsigned int start;
        int type;
 
-       for (start = 0; start < TOTAL_SEGS(sbi); start++) {
+       for (start = 0; start < MAIN_SEGS(sbi); start++) {
                struct seg_entry *sentry = get_seg_entry(sbi, start);
                if (!sentry->valid_blocks)
                        __set_free(sbi, start);
 {
        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
        struct free_segmap_info *free_i = FREE_I(sbi);
-       unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
+       unsigned int segno = 0, offset = 0;
        unsigned short valid_blocks;
 
        while (1) {
                /* find dirty segment based on free segmap */
-               segno = find_next_inuse(free_i, total_segs, offset);
-               if (segno >= total_segs)
+               segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
+               if (segno >= MAIN_SEGS(sbi))
                        break;
                offset = segno + 1;
                valid_blocks = get_valid_blocks(sbi, segno, 0);
 static int init_victim_secmap(struct f2fs_sb_info *sbi)
 {
        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
-       unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
+       unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
 
        dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
        if (!dirty_i->victim_secmap)
        SM_I(sbi)->dirty_info = dirty_i;
        mutex_init(&dirty_i->seglist_lock);
 
-       bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
+       bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
 
        for (i = 0; i < NR_DIRTY_TYPE; i++) {
                dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
 
        sit_i->min_mtime = LLONG_MAX;
 
-       for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
+       for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
                unsigned int i;
                unsigned long long mtime = 0;
 
                return;
 
        if (sit_i->sentries) {
-               for (start = 0; start < TOTAL_SEGS(sbi); start++) {
+               for (start = 0; start < MAIN_SEGS(sbi); start++) {
                        kfree(sit_i->sentries[start].cur_valid_map);
                        kfree(sit_i->sentries[start].ckpt_valid_map);
                }
 
         (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno /             \
          sbi->segs_per_sec))   \
 
-#define START_BLOCK(sbi, segno)                                                \
-       (SM_I(sbi)->seg0_blkaddr +                                      \
+#define MAIN_BLKADDR(sbi)      (SM_I(sbi)->main_blkaddr)
+#define SEG0_BLKADDR(sbi)      (SM_I(sbi)->seg0_blkaddr)
+
+#define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
+#define MAIN_SECS(sbi) (sbi->total_sections)
+
+#define TOTAL_SEGS(sbi)        (SM_I(sbi)->segment_count)
+#define TOTAL_BLKS(sbi)        (TOTAL_SEGS(sbi) << sbi->log_blocks_per_seg)
+
+#define MAX_BLKADDR(sbi)       (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
+#define SEGMENT_SIZE(sbi)      (1 << (sbi->log_blocksize +             \
+                                       sbi->log_blocks_per_seg))
+
+#define START_BLOCK(sbi, segno)        (SEG0_BLKADDR(sbi) +                    \
         (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
+
 #define NEXT_FREE_BLKADDR(sbi, curseg)                                 \
        (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
 
-#define MAIN_BASE_BLOCK(sbi)   (SM_I(sbi)->main_blkaddr)
-
-#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)                            \
-       ((blk_addr) - SM_I(sbi)->seg0_blkaddr)
+#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)    ((blk_addr) - SEG0_BLKADDR(sbi))
 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)                             \
        (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)                            \
 #define        START_SEGNO(segno)              \
        (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
 #define SIT_BLK_CNT(sbi)                       \
-       ((TOTAL_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
+       ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
 #define f2fs_bitmap_size(nr)                   \
        (BITS_TO_LONGS(nr) * sizeof(unsigned long))
-#define TOTAL_SEGS(sbi)        (SM_I(sbi)->main_segments)
-#define TOTAL_SECS(sbi)        (sbi->total_sections)
-#define TOTAL_BLKS(sbi)        (SM_I(sbi)->segment_count << sbi->log_blocks_per_seg)
 
 #define SECTOR_FROM_BLOCK(blk_addr)                                    \
        (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
        clear_bit(segno, free_i->free_segmap);
        free_i->free_segments++;
 
-       next = find_next_bit(free_i->free_segmap, TOTAL_SEGS(sbi), start_segno);
+       next = find_next_bit(free_i->free_segmap, MAIN_SEGS(sbi), start_segno);
        if (next >= start_segno + sbi->segs_per_sec) {
                clear_bit(secno, free_i->free_secmap);
                free_i->free_sections++;
 #ifdef CONFIG_F2FS_CHECK_FS
 static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
 {
-       unsigned int end_segno = SM_I(sbi)->segment_count - 1;
-       BUG_ON(segno > end_segno);
+       BUG_ON(segno > TOTAL_SEGS(sbi) - 1);
 }
 
 static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
 {
-       struct f2fs_sm_info *sm_info = SM_I(sbi);
-       block_t total_blks = TOTAL_BLKS(sbi);
-       block_t start_addr = sm_info->seg0_blkaddr;
-       block_t end_addr = start_addr + total_blks - 1;
-       BUG_ON(blk_addr < start_addr);
-       BUG_ON(blk_addr > end_addr);
+       BUG_ON(blk_addr < SEG0_BLKADDR(sbi));
+       BUG_ON(blk_addr >= MAX_BLKADDR(sbi));
 }
 
 /*
 static inline void check_block_count(struct f2fs_sb_info *sbi,
                int segno, struct f2fs_sit_entry *raw_sit)
 {
-       struct f2fs_sm_info *sm_info = SM_I(sbi);
-       unsigned int end_segno = sm_info->segment_count - 1;
        bool is_valid  = test_bit_le(0, raw_sit->valid_map) ? true : false;
        int valid_blocks = 0;
        int cur_pos = 0, next_pos;
        BUG_ON(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg);
 
        /* check boundary of a given segment number */
-       BUG_ON(segno > end_segno);
+       BUG_ON(segno > TOTAL_SEGS(sbi) - 1);
 
        /* check bitmap with valid block count */
        do {
 #else
 static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
 {
-       unsigned int end_segno = SM_I(sbi)->segment_count - 1;
-
-       if (segno > end_segno)
+       if (segno > TOTAL_SEGS(sbi) - 1)
                sbi->need_fsck = true;
 }
 
 static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
 {
-       struct f2fs_sm_info *sm_info = SM_I(sbi);
-       block_t total_blks = TOTAL_BLKS(sbi);
-       block_t start_addr = sm_info->seg0_blkaddr;
-       block_t end_addr = start_addr + total_blks - 1;
-
-       if (blk_addr < start_addr || blk_addr > end_addr)
+       if (blk_addr < SEG0_BLKADDR(sbi) || blk_addr >= MAX_BLKADDR(sbi))
                sbi->need_fsck = true;
 }
 
 static inline void check_block_count(struct f2fs_sb_info *sbi,
                int segno, struct f2fs_sit_entry *raw_sit)
 {
-       unsigned int end_segno = SM_I(sbi)->segment_count - 1;
-
        /* check segment usage */
        if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg)
                sbi->need_fsck = true;
 
        /* check boundary of a given segment number */
-       if (segno > end_segno)
+       if (segno > TOTAL_SEGS(sbi) - 1)
                sbi->need_fsck = true;
 }
 #endif