linux/fs/f2fs/segment.c
<<
>>
Prefs
   1/*
   2 * fs/f2fs/segment.c
   3 *
   4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
   5 *             http://www.samsung.com/
   6 *
   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.
  10 */
  11#include <linux/fs.h>
  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>
  17
  18#include "f2fs.h"
  19#include "segment.h"
  20#include "node.h"
  21#include <trace/events/f2fs.h>
  22
  23/*
  24 * This function balances dirty node and dentry pages.
  25 * In addition, it controls garbage collection.
  26 */
  27void f2fs_balance_fs(struct f2fs_sb_info *sbi)
  28{
  29        /*
  30         * We should do GC or end up with checkpoint, if there are so many dirty
  31         * dir/node pages without enough free segments.
  32         */
  33        if (has_not_enough_free_secs(sbi, 0)) {
  34                mutex_lock(&sbi->gc_mutex);
  35                f2fs_gc(sbi);
  36        }
  37}
  38
  39static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  40                enum dirty_type dirty_type)
  41{
  42        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  43
  44        /* need not be added */
  45        if (IS_CURSEG(sbi, segno))
  46                return;
  47
  48        if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  49                dirty_i->nr_dirty[dirty_type]++;
  50
  51        if (dirty_type == DIRTY) {
  52                struct seg_entry *sentry = get_seg_entry(sbi, segno);
  53                enum dirty_type t = DIRTY_HOT_DATA;
  54
  55                dirty_type = sentry->type;
  56
  57                if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  58                        dirty_i->nr_dirty[dirty_type]++;
  59
  60                /* Only one bitmap should be set */
  61                for (; t <= DIRTY_COLD_NODE; t++) {
  62                        if (t == dirty_type)
  63                                continue;
  64                        if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  65                                dirty_i->nr_dirty[t]--;
  66                }
  67        }
  68}
  69
  70static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  71                enum dirty_type dirty_type)
  72{
  73        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  74
  75        if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  76                dirty_i->nr_dirty[dirty_type]--;
  77
  78        if (dirty_type == DIRTY) {
  79                enum dirty_type t = DIRTY_HOT_DATA;
  80
  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]--;
  85
  86                if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
  87                        clear_bit(GET_SECNO(sbi, segno),
  88                                                dirty_i->victim_secmap);
  89        }
  90}
  91
  92/*
  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.
  96 */
  97void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  98{
  99        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
 100        unsigned short valid_blocks;
 101
 102        if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
 103                return;
 104
 105        mutex_lock(&dirty_i->seglist_lock);
 106
 107        valid_blocks = get_valid_blocks(sbi, segno, 0);
 108
 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);
 114        } else {
 115                /* Recovery routine with SSR needs this */
 116                __remove_dirty_segment(sbi, segno, DIRTY);
 117        }
 118
 119        mutex_unlock(&dirty_i->seglist_lock);
 120        return;
 121}
 122
 123/*
 124 * Should call clear_prefree_segments after checkpoint is done.
 125 */
 126static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
 127{
 128        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
 129        unsigned int segno, offset = 0;
 130        unsigned int total_segs = TOTAL_SEGS(sbi);
 131
 132        mutex_lock(&dirty_i->seglist_lock);
 133        while (1) {
 134                segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
 135                                offset);
 136                if (segno >= total_segs)
 137                        break;
 138                __set_test_and_free(sbi, segno);
 139                offset = segno + 1;
 140        }
 141        mutex_unlock(&dirty_i->seglist_lock);
 142}
 143
 144void clear_prefree_segments(struct f2fs_sb_info *sbi)
 145{
 146        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
 147        unsigned int segno, offset = 0;
 148        unsigned int total_segs = TOTAL_SEGS(sbi);
 149
 150        mutex_lock(&dirty_i->seglist_lock);
 151        while (1) {
 152                segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
 153                                offset);
 154                if (segno >= total_segs)
 155                        break;
 156
 157                offset = segno + 1;
 158                if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
 159                        dirty_i->nr_dirty[PRE]--;
 160
 161                /* Let's use trim */
 162                if (test_opt(sbi, DISCARD))
 163                        blkdev_issue_discard(sbi->sb->s_bdev,
 164                                        START_BLOCK(sbi, segno) <<
 165                                        sbi->log_sectors_per_block,
 166                                        1 << (sbi->log_sectors_per_block +
 167                                                sbi->log_blocks_per_seg),
 168                                        GFP_NOFS, 0);
 169        }
 170        mutex_unlock(&dirty_i->seglist_lock);
 171}
 172
 173static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
 174{
 175        struct sit_info *sit_i = SIT_I(sbi);
 176        if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
 177                sit_i->dirty_sentries++;
 178}
 179
 180static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
 181                                        unsigned int segno, int modified)
 182{
 183        struct seg_entry *se = get_seg_entry(sbi, segno);
 184        se->type = type;
 185        if (modified)
 186                __mark_sit_entry_dirty(sbi, segno);
 187}
 188
 189static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
 190{
 191        struct seg_entry *se;
 192        unsigned int segno, offset;
 193        long int new_vblocks;
 194
 195        segno = GET_SEGNO(sbi, blkaddr);
 196
 197        se = get_seg_entry(sbi, segno);
 198        new_vblocks = se->valid_blocks + del;
 199        offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
 200
 201        BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
 202                                (new_vblocks > sbi->blocks_per_seg)));
 203
 204        se->valid_blocks = new_vblocks;
 205        se->mtime = get_mtime(sbi);
 206        SIT_I(sbi)->max_mtime = se->mtime;
 207
 208        /* Update valid block bitmap */
 209        if (del > 0) {
 210                if (f2fs_set_bit(offset, se->cur_valid_map))
 211                        BUG();
 212        } else {
 213                if (!f2fs_clear_bit(offset, se->cur_valid_map))
 214                        BUG();
 215        }
 216        if (!f2fs_test_bit(offset, se->ckpt_valid_map))
 217                se->ckpt_valid_blocks += del;
 218
 219        __mark_sit_entry_dirty(sbi, segno);
 220
 221        /* update total number of valid blocks to be written in ckpt area */
 222        SIT_I(sbi)->written_valid_blocks += del;
 223
 224        if (sbi->segs_per_sec > 1)
 225                get_sec_entry(sbi, segno)->valid_blocks += del;
 226}
 227
 228static void refresh_sit_entry(struct f2fs_sb_info *sbi,
 229                        block_t old_blkaddr, block_t new_blkaddr)
 230{
 231        update_sit_entry(sbi, new_blkaddr, 1);
 232        if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
 233                update_sit_entry(sbi, old_blkaddr, -1);
 234}
 235
 236void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
 237{
 238        unsigned int segno = GET_SEGNO(sbi, addr);
 239        struct sit_info *sit_i = SIT_I(sbi);
 240
 241        BUG_ON(addr == NULL_ADDR);
 242        if (addr == NEW_ADDR)
 243                return;
 244
 245        /* add it into sit main buffer */
 246        mutex_lock(&sit_i->sentry_lock);
 247
 248        update_sit_entry(sbi, addr, -1);
 249
 250        /* add it into dirty seglist */
 251        locate_dirty_segment(sbi, segno);
 252
 253        mutex_unlock(&sit_i->sentry_lock);
 254}
 255
 256/*
 257 * This function should be resided under the curseg_mutex lock
 258 */
 259static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
 260                struct f2fs_summary *sum, unsigned short offset)
 261{
 262        struct curseg_info *curseg = CURSEG_I(sbi, type);
 263        void *addr = curseg->sum_blk;
 264        addr += offset * sizeof(struct f2fs_summary);
 265        memcpy(addr, sum, sizeof(struct f2fs_summary));
 266        return;
 267}
 268
 269/*
 270 * Calculate the number of current summary pages for writing
 271 */
 272int npages_for_summary_flush(struct f2fs_sb_info *sbi)
 273{
 274        int total_size_bytes = 0;
 275        int valid_sum_count = 0;
 276        int i, sum_space;
 277
 278        for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
 279                if (sbi->ckpt->alloc_type[i] == SSR)
 280                        valid_sum_count += sbi->blocks_per_seg;
 281                else
 282                        valid_sum_count += curseg_blkoff(sbi, i);
 283        }
 284
 285        total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
 286                        + sizeof(struct nat_journal) + 2
 287                        + sizeof(struct sit_journal) + 2;
 288        sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
 289        if (total_size_bytes < sum_space)
 290                return 1;
 291        else if (total_size_bytes < 2 * sum_space)
 292                return 2;
 293        return 3;
 294}
 295
 296/*
 297 * Caller should put this summary page
 298 */
 299struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
 300{
 301        return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
 302}
 303
 304static void write_sum_page(struct f2fs_sb_info *sbi,
 305                        struct f2fs_summary_block *sum_blk, block_t blk_addr)
 306{
 307        struct page *page = grab_meta_page(sbi, blk_addr);
 308        void *kaddr = page_address(page);
 309        memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
 310        set_page_dirty(page);
 311        f2fs_put_page(page, 1);
 312}
 313
 314static unsigned int check_prefree_segments(struct f2fs_sb_info *sbi, int type)
 315{
 316        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
 317        unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
 318        unsigned int segno;
 319        unsigned int ofs = 0;
 320
 321        /*
 322         * If there is not enough reserved sections,
 323         * we should not reuse prefree segments.
 324         */
 325        if (has_not_enough_free_secs(sbi, 0))
 326                return NULL_SEGNO;
 327
 328        /*
 329         * NODE page should not reuse prefree segment,
 330         * since those information is used for SPOR.
 331         */
 332        if (IS_NODESEG(type))
 333                return NULL_SEGNO;
 334next:
 335        segno = find_next_bit(prefree_segmap, TOTAL_SEGS(sbi), ofs);
 336        ofs += sbi->segs_per_sec;
 337
 338        if (segno < TOTAL_SEGS(sbi)) {
 339                int i;
 340
 341                /* skip intermediate segments in a section */
 342                if (segno % sbi->segs_per_sec)
 343                        goto next;
 344
 345                /* skip if the section is currently used */
 346                if (sec_usage_check(sbi, GET_SECNO(sbi, segno)))
 347                        goto next;
 348
 349                /* skip if whole section is not prefree */
 350                for (i = 1; i < sbi->segs_per_sec; i++)
 351                        if (!test_bit(segno + i, prefree_segmap))
 352                                goto next;
 353
 354                /* skip if whole section was not free at the last checkpoint */
 355                for (i = 0; i < sbi->segs_per_sec; i++)
 356                        if (get_seg_entry(sbi, segno + i)->ckpt_valid_blocks)
 357                                goto next;
 358
 359                return segno;
 360        }
 361        return NULL_SEGNO;
 362}
 363
 364static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
 365{
 366        struct curseg_info *curseg = CURSEG_I(sbi, type);
 367        unsigned int segno = curseg->segno;
 368        struct free_segmap_info *free_i = FREE_I(sbi);
 369
 370        if (segno + 1 < TOTAL_SEGS(sbi) && (segno + 1) % sbi->segs_per_sec)
 371                return !test_bit(segno + 1, free_i->free_segmap);
 372        return 0;
 373}
 374
 375/*
 376 * Find a new segment from the free segments bitmap to right order
 377 * This function should be returned with success, otherwise BUG
 378 */
 379static void get_new_segment(struct f2fs_sb_info *sbi,
 380                        unsigned int *newseg, bool new_sec, int dir)
 381{
 382        struct free_segmap_info *free_i = FREE_I(sbi);
 383        unsigned int segno, secno, zoneno;
 384        unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
 385        unsigned int hint = *newseg / sbi->segs_per_sec;
 386        unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
 387        unsigned int left_start = hint;
 388        bool init = true;
 389        int go_left = 0;
 390        int i;
 391
 392        write_lock(&free_i->segmap_lock);
 393
 394        if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
 395                segno = find_next_zero_bit(free_i->free_segmap,
 396                                        TOTAL_SEGS(sbi), *newseg + 1);
 397                if (segno - *newseg < sbi->segs_per_sec -
 398                                        (*newseg % sbi->segs_per_sec))
 399                        goto got_it;
 400        }
 401find_other_zone:
 402        secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
 403        if (secno >= TOTAL_SECS(sbi)) {
 404                if (dir == ALLOC_RIGHT) {
 405                        secno = find_next_zero_bit(free_i->free_secmap,
 406                                                        TOTAL_SECS(sbi), 0);
 407                        BUG_ON(secno >= TOTAL_SECS(sbi));
 408                } else {
 409                        go_left = 1;
 410                        left_start = hint - 1;
 411                }
 412        }
 413        if (go_left == 0)
 414                goto skip_left;
 415
 416        while (test_bit(left_start, free_i->free_secmap)) {
 417                if (left_start > 0) {
 418                        left_start--;
 419                        continue;
 420                }
 421                left_start = find_next_zero_bit(free_i->free_secmap,
 422                                                        TOTAL_SECS(sbi), 0);
 423                BUG_ON(left_start >= TOTAL_SECS(sbi));
 424                break;
 425        }
 426        secno = left_start;
 427skip_left:
 428        hint = secno;
 429        segno = secno * sbi->segs_per_sec;
 430        zoneno = secno / sbi->secs_per_zone;
 431
 432        /* give up on finding another zone */
 433        if (!init)
 434                goto got_it;
 435        if (sbi->secs_per_zone == 1)
 436                goto got_it;
 437        if (zoneno == old_zoneno)
 438                goto got_it;
 439        if (dir == ALLOC_LEFT) {
 440                if (!go_left && zoneno + 1 >= total_zones)
 441                        goto got_it;
 442                if (go_left && zoneno == 0)
 443                        goto got_it;
 444        }
 445        for (i = 0; i < NR_CURSEG_TYPE; i++)
 446                if (CURSEG_I(sbi, i)->zone == zoneno)
 447                        break;
 448
 449        if (i < NR_CURSEG_TYPE) {
 450                /* zone is in user, try another */
 451                if (go_left)
 452                        hint = zoneno * sbi->secs_per_zone - 1;
 453                else if (zoneno + 1 >= total_zones)
 454                        hint = 0;
 455                else
 456                        hint = (zoneno + 1) * sbi->secs_per_zone;
 457                init = false;
 458                goto find_other_zone;
 459        }
 460got_it:
 461        /* set it as dirty segment in free segmap */
 462        BUG_ON(test_bit(segno, free_i->free_segmap));
 463        __set_inuse(sbi, segno);
 464        *newseg = segno;
 465        write_unlock(&free_i->segmap_lock);
 466}
 467
 468static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
 469{
 470        struct curseg_info *curseg = CURSEG_I(sbi, type);
 471        struct summary_footer *sum_footer;
 472
 473        curseg->segno = curseg->next_segno;
 474        curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
 475        curseg->next_blkoff = 0;
 476        curseg->next_segno = NULL_SEGNO;
 477
 478        sum_footer = &(curseg->sum_blk->footer);
 479        memset(sum_footer, 0, sizeof(struct summary_footer));
 480        if (IS_DATASEG(type))
 481                SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
 482        if (IS_NODESEG(type))
 483                SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
 484        __set_sit_entry_type(sbi, type, curseg->segno, modified);
 485}
 486
 487/*
 488 * Allocate a current working segment.
 489 * This function always allocates a free segment in LFS manner.
 490 */
 491static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
 492{
 493        struct curseg_info *curseg = CURSEG_I(sbi, type);
 494        unsigned int segno = curseg->segno;
 495        int dir = ALLOC_LEFT;
 496
 497        write_sum_page(sbi, curseg->sum_blk,
 498                                GET_SUM_BLOCK(sbi, curseg->segno));
 499        if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
 500                dir = ALLOC_RIGHT;
 501
 502        if (test_opt(sbi, NOHEAP))
 503                dir = ALLOC_RIGHT;
 504
 505        get_new_segment(sbi, &segno, new_sec, dir);
 506        curseg->next_segno = segno;
 507        reset_curseg(sbi, type, 1);
 508        curseg->alloc_type = LFS;
 509}
 510
 511static void __next_free_blkoff(struct f2fs_sb_info *sbi,
 512                        struct curseg_info *seg, block_t start)
 513{
 514        struct seg_entry *se = get_seg_entry(sbi, seg->segno);
 515        block_t ofs;
 516        for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
 517                if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
 518                        && !f2fs_test_bit(ofs, se->cur_valid_map))
 519                        break;
 520        }
 521        seg->next_blkoff = ofs;
 522}
 523
 524/*
 525 * If a segment is written by LFS manner, next block offset is just obtained
 526 * by increasing the current block offset. However, if a segment is written by
 527 * SSR manner, next block offset obtained by calling __next_free_blkoff
 528 */
 529static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
 530                                struct curseg_info *seg)
 531{
 532        if (seg->alloc_type == SSR)
 533                __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
 534        else
 535                seg->next_blkoff++;
 536}
 537
 538/*
 539 * This function always allocates a used segment (from dirty seglist) by SSR
 540 * manner, so it should recover the existing segment information of valid blocks
 541 */
 542static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
 543{
 544        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
 545        struct curseg_info *curseg = CURSEG_I(sbi, type);
 546        unsigned int new_segno = curseg->next_segno;
 547        struct f2fs_summary_block *sum_node;
 548        struct page *sum_page;
 549
 550        write_sum_page(sbi, curseg->sum_blk,
 551                                GET_SUM_BLOCK(sbi, curseg->segno));
 552        __set_test_and_inuse(sbi, new_segno);
 553
 554        mutex_lock(&dirty_i->seglist_lock);
 555        __remove_dirty_segment(sbi, new_segno, PRE);
 556        __remove_dirty_segment(sbi, new_segno, DIRTY);
 557        mutex_unlock(&dirty_i->seglist_lock);
 558
 559        reset_curseg(sbi, type, 1);
 560        curseg->alloc_type = SSR;
 561        __next_free_blkoff(sbi, curseg, 0);
 562
 563        if (reuse) {
 564                sum_page = get_sum_page(sbi, new_segno);
 565                sum_node = (struct f2fs_summary_block *)page_address(sum_page);
 566                memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
 567                f2fs_put_page(sum_page, 1);
 568        }
 569}
 570
 571static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
 572{
 573        struct curseg_info *curseg = CURSEG_I(sbi, type);
 574        const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
 575
 576        if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
 577                return v_ops->get_victim(sbi,
 578                                &(curseg)->next_segno, BG_GC, type, SSR);
 579
 580        /* For data segments, let's do SSR more intensively */
 581        for (; type >= CURSEG_HOT_DATA; type--)
 582                if (v_ops->get_victim(sbi, &(curseg)->next_segno,
 583                                                BG_GC, type, SSR))
 584                        return 1;
 585        return 0;
 586}
 587
 588/*
 589 * flush out current segment and replace it with new segment
 590 * This function should be returned with success, otherwise BUG
 591 */
 592static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
 593                                                int type, bool force)
 594{
 595        struct curseg_info *curseg = CURSEG_I(sbi, type);
 596
 597        if (force) {
 598                new_curseg(sbi, type, true);
 599                goto out;
 600        }
 601
 602        curseg->next_segno = check_prefree_segments(sbi, type);
 603
 604        if (curseg->next_segno != NULL_SEGNO)
 605                change_curseg(sbi, type, false);
 606        else if (type == CURSEG_WARM_NODE)
 607                new_curseg(sbi, type, false);
 608        else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
 609                new_curseg(sbi, type, false);
 610        else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
 611                change_curseg(sbi, type, true);
 612        else
 613                new_curseg(sbi, type, false);
 614out:
 615        sbi->segment_count[curseg->alloc_type]++;
 616}
 617
 618void allocate_new_segments(struct f2fs_sb_info *sbi)
 619{
 620        struct curseg_info *curseg;
 621        unsigned int old_curseg;
 622        int i;
 623
 624        for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
 625                curseg = CURSEG_I(sbi, i);
 626                old_curseg = curseg->segno;
 627                SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
 628                locate_dirty_segment(sbi, old_curseg);
 629        }
 630}
 631
 632static const struct segment_allocation default_salloc_ops = {
 633        .allocate_segment = allocate_segment_by_default,
 634};
 635
 636static void f2fs_end_io_write(struct bio *bio, int err)
 637{
 638        struct bio_private *p = bio->bi_private;
 639        struct bio_vec *bvec;
 640        int i;
 641
 642        bio_for_each_segment_all(bvec, bio, i) {
 643                struct page *page = bvec->bv_page;
 644
 645                if (err) {
 646                        SetPageError(page);
 647                        if (page->mapping)
 648                                set_bit(AS_EIO, &page->mapping->flags);
 649                        set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
 650                        p->sbi->sb->s_flags |= MS_RDONLY;
 651                }
 652                end_page_writeback(page);
 653                dec_page_count(p->sbi, F2FS_WRITEBACK);
 654        }
 655
 656        if (p->is_sync)
 657                complete(p->wait);
 658        kfree(p);
 659        bio_put(bio);
 660}
 661
 662struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
 663{
 664        struct bio *bio;
 665        struct bio_private *priv;
 666retry:
 667        priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
 668        if (!priv) {
 669                cond_resched();
 670                goto retry;
 671        }
 672
 673        /* No failure on bio allocation */
 674        bio = bio_alloc(GFP_NOIO, npages);
 675        bio->bi_bdev = bdev;
 676        bio->bi_private = priv;
 677        return bio;
 678}
 679
 680static void do_submit_bio(struct f2fs_sb_info *sbi,
 681                                enum page_type type, bool sync)
 682{
 683        int rw = sync ? WRITE_SYNC : WRITE;
 684        enum page_type btype = type > META ? META : type;
 685
 686        if (type >= META_FLUSH)
 687                rw = WRITE_FLUSH_FUA;
 688
 689        if (btype == META)
 690                rw |= REQ_META;
 691
 692        if (sbi->bio[btype]) {
 693                struct bio_private *p = sbi->bio[btype]->bi_private;
 694                p->sbi = sbi;
 695                sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
 696
 697                trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
 698
 699                if (type == META_FLUSH) {
 700                        DECLARE_COMPLETION_ONSTACK(wait);
 701                        p->is_sync = true;
 702                        p->wait = &wait;
 703                        submit_bio(rw, sbi->bio[btype]);
 704                        wait_for_completion(&wait);
 705                } else {
 706                        p->is_sync = false;
 707                        submit_bio(rw, sbi->bio[btype]);
 708                }
 709                sbi->bio[btype] = NULL;
 710        }
 711}
 712
 713void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
 714{
 715        down_write(&sbi->bio_sem);
 716        do_submit_bio(sbi, type, sync);
 717        up_write(&sbi->bio_sem);
 718}
 719
 720static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
 721                                block_t blk_addr, enum page_type type)
 722{
 723        struct block_device *bdev = sbi->sb->s_bdev;
 724
 725        verify_block_addr(sbi, blk_addr);
 726
 727        down_write(&sbi->bio_sem);
 728
 729        inc_page_count(sbi, F2FS_WRITEBACK);
 730
 731        if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
 732                do_submit_bio(sbi, type, false);
 733alloc_new:
 734        if (sbi->bio[type] == NULL) {
 735                sbi->bio[type] = f2fs_bio_alloc(bdev, max_hw_blocks(sbi));
 736                sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
 737                /*
 738                 * The end_io will be assigned at the sumbission phase.
 739                 * Until then, let bio_add_page() merge consecutive IOs as much
 740                 * as possible.
 741                 */
 742        }
 743
 744        if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
 745                                                        PAGE_CACHE_SIZE) {
 746                do_submit_bio(sbi, type, false);
 747                goto alloc_new;
 748        }
 749
 750        sbi->last_block_in_bio[type] = blk_addr;
 751
 752        up_write(&sbi->bio_sem);
 753        trace_f2fs_submit_write_page(page, blk_addr, type);
 754}
 755
 756static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
 757{
 758        struct curseg_info *curseg = CURSEG_I(sbi, type);
 759        if (curseg->next_blkoff < sbi->blocks_per_seg)
 760                return true;
 761        return false;
 762}
 763
 764static int __get_segment_type_2(struct page *page, enum page_type p_type)
 765{
 766        if (p_type == DATA)
 767                return CURSEG_HOT_DATA;
 768        else
 769                return CURSEG_HOT_NODE;
 770}
 771
 772static int __get_segment_type_4(struct page *page, enum page_type p_type)
 773{
 774        if (p_type == DATA) {
 775                struct inode *inode = page->mapping->host;
 776
 777                if (S_ISDIR(inode->i_mode))
 778                        return CURSEG_HOT_DATA;
 779                else
 780                        return CURSEG_COLD_DATA;
 781        } else {
 782                if (IS_DNODE(page) && !is_cold_node(page))
 783                        return CURSEG_HOT_NODE;
 784                else
 785                        return CURSEG_COLD_NODE;
 786        }
 787}
 788
 789static int __get_segment_type_6(struct page *page, enum page_type p_type)
 790{
 791        if (p_type == DATA) {
 792                struct inode *inode = page->mapping->host;
 793
 794                if (S_ISDIR(inode->i_mode))
 795                        return CURSEG_HOT_DATA;
 796                else if (is_cold_data(page) || is_cold_file(inode))
 797                        return CURSEG_COLD_DATA;
 798                else
 799                        return CURSEG_WARM_DATA;
 800        } else {
 801                if (IS_DNODE(page))
 802                        return is_cold_node(page) ? CURSEG_WARM_NODE :
 803                                                CURSEG_HOT_NODE;
 804                else
 805                        return CURSEG_COLD_NODE;
 806        }
 807}
 808
 809static int __get_segment_type(struct page *page, enum page_type p_type)
 810{
 811        struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
 812        switch (sbi->active_logs) {
 813        case 2:
 814                return __get_segment_type_2(page, p_type);
 815        case 4:
 816                return __get_segment_type_4(page, p_type);
 817        }
 818        /* NR_CURSEG_TYPE(6) logs by default */
 819        BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
 820        return __get_segment_type_6(page, p_type);
 821}
 822
 823static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
 824                        block_t old_blkaddr, block_t *new_blkaddr,
 825                        struct f2fs_summary *sum, enum page_type p_type)
 826{
 827        struct sit_info *sit_i = SIT_I(sbi);
 828        struct curseg_info *curseg;
 829        unsigned int old_cursegno;
 830        int type;
 831
 832        type = __get_segment_type(page, p_type);
 833        curseg = CURSEG_I(sbi, type);
 834
 835        mutex_lock(&curseg->curseg_mutex);
 836
 837        *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
 838        old_cursegno = curseg->segno;
 839
 840        /*
 841         * __add_sum_entry should be resided under the curseg_mutex
 842         * because, this function updates a summary entry in the
 843         * current summary block.
 844         */
 845        __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
 846
 847        mutex_lock(&sit_i->sentry_lock);
 848        __refresh_next_blkoff(sbi, curseg);
 849        sbi->block_count[curseg->alloc_type]++;
 850
 851        /*
 852         * SIT information should be updated before segment allocation,
 853         * since SSR needs latest valid block information.
 854         */
 855        refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
 856
 857        if (!__has_curseg_space(sbi, type))
 858                sit_i->s_ops->allocate_segment(sbi, type, false);
 859
 860        locate_dirty_segment(sbi, old_cursegno);
 861        locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
 862        mutex_unlock(&sit_i->sentry_lock);
 863
 864        if (p_type == NODE)
 865                fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
 866
 867        /* writeout dirty page into bdev */
 868        submit_write_page(sbi, page, *new_blkaddr, p_type);
 869
 870        mutex_unlock(&curseg->curseg_mutex);
 871}
 872
 873void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
 874{
 875        set_page_writeback(page);
 876        submit_write_page(sbi, page, page->index, META);
 877}
 878
 879void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
 880                unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
 881{
 882        struct f2fs_summary sum;
 883        set_summary(&sum, nid, 0, 0);
 884        do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
 885}
 886
 887void write_data_page(struct inode *inode, struct page *page,
 888                struct dnode_of_data *dn, block_t old_blkaddr,
 889                block_t *new_blkaddr)
 890{
 891        struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
 892        struct f2fs_summary sum;
 893        struct node_info ni;
 894
 895        BUG_ON(old_blkaddr == NULL_ADDR);
 896        get_node_info(sbi, dn->nid, &ni);
 897        set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
 898
 899        do_write_page(sbi, page, old_blkaddr,
 900                        new_blkaddr, &sum, DATA);
 901}
 902
 903void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
 904                                        block_t old_blk_addr)
 905{
 906        submit_write_page(sbi, page, old_blk_addr, DATA);
 907}
 908
 909void recover_data_page(struct f2fs_sb_info *sbi,
 910                        struct page *page, struct f2fs_summary *sum,
 911                        block_t old_blkaddr, block_t new_blkaddr)
 912{
 913        struct sit_info *sit_i = SIT_I(sbi);
 914        struct curseg_info *curseg;
 915        unsigned int segno, old_cursegno;
 916        struct seg_entry *se;
 917        int type;
 918
 919        segno = GET_SEGNO(sbi, new_blkaddr);
 920        se = get_seg_entry(sbi, segno);
 921        type = se->type;
 922
 923        if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
 924                if (old_blkaddr == NULL_ADDR)
 925                        type = CURSEG_COLD_DATA;
 926                else
 927                        type = CURSEG_WARM_DATA;
 928        }
 929        curseg = CURSEG_I(sbi, type);
 930
 931        mutex_lock(&curseg->curseg_mutex);
 932        mutex_lock(&sit_i->sentry_lock);
 933
 934        old_cursegno = curseg->segno;
 935
 936        /* change the current segment */
 937        if (segno != curseg->segno) {
 938                curseg->next_segno = segno;
 939                change_curseg(sbi, type, true);
 940        }
 941
 942        curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
 943                                        (sbi->blocks_per_seg - 1);
 944        __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
 945
 946        refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
 947
 948        locate_dirty_segment(sbi, old_cursegno);
 949        locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
 950
 951        mutex_unlock(&sit_i->sentry_lock);
 952        mutex_unlock(&curseg->curseg_mutex);
 953}
 954
 955void rewrite_node_page(struct f2fs_sb_info *sbi,
 956                        struct page *page, struct f2fs_summary *sum,
 957                        block_t old_blkaddr, block_t new_blkaddr)
 958{
 959        struct sit_info *sit_i = SIT_I(sbi);
 960        int type = CURSEG_WARM_NODE;
 961        struct curseg_info *curseg;
 962        unsigned int segno, old_cursegno;
 963        block_t next_blkaddr = next_blkaddr_of_node(page);
 964        unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
 965
 966        curseg = CURSEG_I(sbi, type);
 967
 968        mutex_lock(&curseg->curseg_mutex);
 969        mutex_lock(&sit_i->sentry_lock);
 970
 971        segno = GET_SEGNO(sbi, new_blkaddr);
 972        old_cursegno = curseg->segno;
 973
 974        /* change the current segment */
 975        if (segno != curseg->segno) {
 976                curseg->next_segno = segno;
 977                change_curseg(sbi, type, true);
 978        }
 979        curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
 980                                        (sbi->blocks_per_seg - 1);
 981        __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
 982
 983        /* change the current log to the next block addr in advance */
 984        if (next_segno != segno) {
 985                curseg->next_segno = next_segno;
 986                change_curseg(sbi, type, true);
 987        }
 988        curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
 989                                        (sbi->blocks_per_seg - 1);
 990
 991        /* rewrite node page */
 992        set_page_writeback(page);
 993        submit_write_page(sbi, page, new_blkaddr, NODE);
 994        f2fs_submit_bio(sbi, NODE, true);
 995        refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
 996
 997        locate_dirty_segment(sbi, old_cursegno);
 998        locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
 999
1000        mutex_unlock(&sit_i->sentry_lock);
1001        mutex_unlock(&curseg->curseg_mutex);
1002}
1003
1004static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1005{
1006        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1007        struct curseg_info *seg_i;
1008        unsigned char *kaddr;
1009        struct page *page;
1010        block_t start;
1011        int i, j, offset;
1012
1013        start = start_sum_block(sbi);
1014
1015        page = get_meta_page(sbi, start++);
1016        kaddr = (unsigned char *)page_address(page);
1017
1018        /* Step 1: restore nat cache */
1019        seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1020        memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1021
1022        /* Step 2: restore sit cache */
1023        seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1024        memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1025                                                SUM_JOURNAL_SIZE);
1026        offset = 2 * SUM_JOURNAL_SIZE;
1027
1028        /* Step 3: restore summary entries */
1029        for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1030                unsigned short blk_off;
1031                unsigned int segno;
1032
1033                seg_i = CURSEG_I(sbi, i);
1034                segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1035                blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1036                seg_i->next_segno = segno;
1037                reset_curseg(sbi, i, 0);
1038                seg_i->alloc_type = ckpt->alloc_type[i];
1039                seg_i->next_blkoff = blk_off;
1040
1041                if (seg_i->alloc_type == SSR)
1042                        blk_off = sbi->blocks_per_seg;
1043
1044                for (j = 0; j < blk_off; j++) {
1045                        struct f2fs_summary *s;
1046                        s = (struct f2fs_summary *)(kaddr + offset);
1047                        seg_i->sum_blk->entries[j] = *s;
1048                        offset += SUMMARY_SIZE;
1049                        if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1050                                                SUM_FOOTER_SIZE)
1051                                continue;
1052
1053                        f2fs_put_page(page, 1);
1054                        page = NULL;
1055
1056                        page = get_meta_page(sbi, start++);
1057                        kaddr = (unsigned char *)page_address(page);
1058                        offset = 0;
1059                }
1060        }
1061        f2fs_put_page(page, 1);
1062        return 0;
1063}
1064
1065static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1066{
1067        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1068        struct f2fs_summary_block *sum;
1069        struct curseg_info *curseg;
1070        struct page *new;
1071        unsigned short blk_off;
1072        unsigned int segno = 0;
1073        block_t blk_addr = 0;
1074
1075        /* get segment number and block addr */
1076        if (IS_DATASEG(type)) {
1077                segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1078                blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1079                                                        CURSEG_HOT_DATA]);
1080                if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1081                        blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1082                else
1083                        blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1084        } else {
1085                segno = le32_to_cpu(ckpt->cur_node_segno[type -
1086                                                        CURSEG_HOT_NODE]);
1087                blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1088                                                        CURSEG_HOT_NODE]);
1089                if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1090                        blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1091                                                        type - CURSEG_HOT_NODE);
1092                else
1093                        blk_addr = GET_SUM_BLOCK(sbi, segno);
1094        }
1095
1096        new = get_meta_page(sbi, blk_addr);
1097        sum = (struct f2fs_summary_block *)page_address(new);
1098
1099        if (IS_NODESEG(type)) {
1100                if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1101                        struct f2fs_summary *ns = &sum->entries[0];
1102                        int i;
1103                        for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1104                                ns->version = 0;
1105                                ns->ofs_in_node = 0;
1106                        }
1107                } else {
1108                        if (restore_node_summary(sbi, segno, sum)) {
1109                                f2fs_put_page(new, 1);
1110                                return -EINVAL;
1111                        }
1112                }
1113        }
1114
1115        /* set uncompleted segment to curseg */
1116        curseg = CURSEG_I(sbi, type);
1117        mutex_lock(&curseg->curseg_mutex);
1118        memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1119        curseg->next_segno = segno;
1120        reset_curseg(sbi, type, 0);
1121        curseg->alloc_type = ckpt->alloc_type[type];
1122        curseg->next_blkoff = blk_off;
1123        mutex_unlock(&curseg->curseg_mutex);
1124        f2fs_put_page(new, 1);
1125        return 0;
1126}
1127
1128static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1129{
1130        int type = CURSEG_HOT_DATA;
1131
1132        if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1133                /* restore for compacted data summary */
1134                if (read_compacted_summaries(sbi))
1135                        return -EINVAL;
1136                type = CURSEG_HOT_NODE;
1137        }
1138
1139        for (; type <= CURSEG_COLD_NODE; type++)
1140                if (read_normal_summaries(sbi, type))
1141                        return -EINVAL;
1142        return 0;
1143}
1144
1145static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1146{
1147        struct page *page;
1148        unsigned char *kaddr;
1149        struct f2fs_summary *summary;
1150        struct curseg_info *seg_i;
1151        int written_size = 0;
1152        int i, j;
1153
1154        page = grab_meta_page(sbi, blkaddr++);
1155        kaddr = (unsigned char *)page_address(page);
1156
1157        /* Step 1: write nat cache */
1158        seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1159        memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1160        written_size += SUM_JOURNAL_SIZE;
1161
1162        /* Step 2: write sit cache */
1163        seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1164        memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1165                                                SUM_JOURNAL_SIZE);
1166        written_size += SUM_JOURNAL_SIZE;
1167
1168        set_page_dirty(page);
1169
1170        /* Step 3: write summary entries */
1171        for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1172                unsigned short blkoff;
1173                seg_i = CURSEG_I(sbi, i);
1174                if (sbi->ckpt->alloc_type[i] == SSR)
1175                        blkoff = sbi->blocks_per_seg;
1176                else
1177                        blkoff = curseg_blkoff(sbi, i);
1178
1179                for (j = 0; j < blkoff; j++) {
1180                        if (!page) {
1181                                page = grab_meta_page(sbi, blkaddr++);
1182                                kaddr = (unsigned char *)page_address(page);
1183                                written_size = 0;
1184                        }
1185                        summary = (struct f2fs_summary *)(kaddr + written_size);
1186                        *summary = seg_i->sum_blk->entries[j];
1187                        written_size += SUMMARY_SIZE;
1188                        set_page_dirty(page);
1189
1190                        if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1191                                                        SUM_FOOTER_SIZE)
1192                                continue;
1193
1194                        f2fs_put_page(page, 1);
1195                        page = NULL;
1196                }
1197        }
1198        if (page)
1199                f2fs_put_page(page, 1);
1200}
1201
1202static void write_normal_summaries(struct f2fs_sb_info *sbi,
1203                                        block_t blkaddr, int type)
1204{
1205        int i, end;
1206        if (IS_DATASEG(type))
1207                end = type + NR_CURSEG_DATA_TYPE;
1208        else
1209                end = type + NR_CURSEG_NODE_TYPE;
1210
1211        for (i = type; i < end; i++) {
1212                struct curseg_info *sum = CURSEG_I(sbi, i);
1213                mutex_lock(&sum->curseg_mutex);
1214                write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1215                mutex_unlock(&sum->curseg_mutex);
1216        }
1217}
1218
1219void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1220{
1221        if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1222                write_compacted_summaries(sbi, start_blk);
1223        else
1224                write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1225}
1226
1227void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1228{
1229        if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1230                write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1231        return;
1232}
1233
1234int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1235                                        unsigned int val, int alloc)
1236{
1237        int i;
1238
1239        if (type == NAT_JOURNAL) {
1240                for (i = 0; i < nats_in_cursum(sum); i++) {
1241                        if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1242                                return i;
1243                }
1244                if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1245                        return update_nats_in_cursum(sum, 1);
1246        } else if (type == SIT_JOURNAL) {
1247                for (i = 0; i < sits_in_cursum(sum); i++)
1248                        if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1249                                return i;
1250                if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1251                        return update_sits_in_cursum(sum, 1);
1252        }
1253        return -1;
1254}
1255
1256static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1257                                        unsigned int segno)
1258{
1259        struct sit_info *sit_i = SIT_I(sbi);
1260        unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1261        block_t blk_addr = sit_i->sit_base_addr + offset;
1262
1263        check_seg_range(sbi, segno);
1264
1265        /* calculate sit block address */
1266        if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1267                blk_addr += sit_i->sit_blocks;
1268
1269        return get_meta_page(sbi, blk_addr);
1270}
1271
1272static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1273                                        unsigned int start)
1274{
1275        struct sit_info *sit_i = SIT_I(sbi);
1276        struct page *src_page, *dst_page;
1277        pgoff_t src_off, dst_off;
1278        void *src_addr, *dst_addr;
1279
1280        src_off = current_sit_addr(sbi, start);
1281        dst_off = next_sit_addr(sbi, src_off);
1282
1283        /* get current sit block page without lock */
1284        src_page = get_meta_page(sbi, src_off);
1285        dst_page = grab_meta_page(sbi, dst_off);
1286        BUG_ON(PageDirty(src_page));
1287
1288        src_addr = page_address(src_page);
1289        dst_addr = page_address(dst_page);
1290        memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1291
1292        set_page_dirty(dst_page);
1293        f2fs_put_page(src_page, 1);
1294
1295        set_to_next_sit(sit_i, start);
1296
1297        return dst_page;
1298}
1299
1300static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1301{
1302        struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1303        struct f2fs_summary_block *sum = curseg->sum_blk;
1304        int i;
1305
1306        /*
1307         * If the journal area in the current summary is full of sit entries,
1308         * all the sit entries will be flushed. Otherwise the sit entries
1309         * are not able to replace with newly hot sit entries.
1310         */
1311        if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1312                for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1313                        unsigned int segno;
1314                        segno = le32_to_cpu(segno_in_journal(sum, i));
1315                        __mark_sit_entry_dirty(sbi, segno);
1316                }
1317                update_sits_in_cursum(sum, -sits_in_cursum(sum));
1318                return 1;
1319        }
1320        return 0;
1321}
1322
1323/*
1324 * CP calls this function, which flushes SIT entries including sit_journal,
1325 * and moves prefree segs to free segs.
1326 */
1327void flush_sit_entries(struct f2fs_sb_info *sbi)
1328{
1329        struct sit_info *sit_i = SIT_I(sbi);
1330        unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1331        struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1332        struct f2fs_summary_block *sum = curseg->sum_blk;
1333        unsigned long nsegs = TOTAL_SEGS(sbi);
1334        struct page *page = NULL;
1335        struct f2fs_sit_block *raw_sit = NULL;
1336        unsigned int start = 0, end = 0;
1337        unsigned int segno = -1;
1338        bool flushed;
1339
1340        mutex_lock(&curseg->curseg_mutex);
1341        mutex_lock(&sit_i->sentry_lock);
1342
1343        /*
1344         * "flushed" indicates whether sit entries in journal are flushed
1345         * to the SIT area or not.
1346         */
1347        flushed = flush_sits_in_journal(sbi);
1348
1349        while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1350                struct seg_entry *se = get_seg_entry(sbi, segno);
1351                int sit_offset, offset;
1352
1353                sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1354
1355                if (flushed)
1356                        goto to_sit_page;
1357
1358                offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1359                if (offset >= 0) {
1360                        segno_in_journal(sum, offset) = cpu_to_le32(segno);
1361                        seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1362                        goto flush_done;
1363                }
1364to_sit_page:
1365                if (!page || (start > segno) || (segno > end)) {
1366                        if (page) {
1367                                f2fs_put_page(page, 1);
1368                                page = NULL;
1369                        }
1370
1371                        start = START_SEGNO(sit_i, segno);
1372                        end = start + SIT_ENTRY_PER_BLOCK - 1;
1373
1374                        /* read sit block that will be updated */
1375                        page = get_next_sit_page(sbi, start);
1376                        raw_sit = page_address(page);
1377                }
1378
1379                /* udpate entry in SIT block */
1380                seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1381flush_done:
1382                __clear_bit(segno, bitmap);
1383                sit_i->dirty_sentries--;
1384        }
1385        mutex_unlock(&sit_i->sentry_lock);
1386        mutex_unlock(&curseg->curseg_mutex);
1387
1388        /* writeout last modified SIT block */
1389        f2fs_put_page(page, 1);
1390
1391        set_prefree_as_free_segments(sbi);
1392}
1393
1394static int build_sit_info(struct f2fs_sb_info *sbi)
1395{
1396        struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1397        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1398        struct sit_info *sit_i;
1399        unsigned int sit_segs, start;
1400        char *src_bitmap, *dst_bitmap;
1401        unsigned int bitmap_size;
1402
1403        /* allocate memory for SIT information */
1404        sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1405        if (!sit_i)
1406                return -ENOMEM;
1407
1408        SM_I(sbi)->sit_info = sit_i;
1409
1410        sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1411        if (!sit_i->sentries)
1412                return -ENOMEM;
1413
1414        bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1415        sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1416        if (!sit_i->dirty_sentries_bitmap)
1417                return -ENOMEM;
1418
1419        for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1420                sit_i->sentries[start].cur_valid_map
1421                        = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1422                sit_i->sentries[start].ckpt_valid_map
1423                        = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1424                if (!sit_i->sentries[start].cur_valid_map
1425                                || !sit_i->sentries[start].ckpt_valid_map)
1426                        return -ENOMEM;
1427        }
1428
1429        if (sbi->segs_per_sec > 1) {
1430                sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1431                                        sizeof(struct sec_entry));
1432                if (!sit_i->sec_entries)
1433                        return -ENOMEM;
1434        }
1435
1436        /* get information related with SIT */
1437        sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1438
1439        /* setup SIT bitmap from ckeckpoint pack */
1440        bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1441        src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1442
1443        dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1444        if (!dst_bitmap)
1445                return -ENOMEM;
1446
1447        /* init SIT information */
1448        sit_i->s_ops = &default_salloc_ops;
1449
1450        sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1451        sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1452        sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1453        sit_i->sit_bitmap = dst_bitmap;
1454        sit_i->bitmap_size = bitmap_size;
1455        sit_i->dirty_sentries = 0;
1456        sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1457        sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1458        sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1459        mutex_init(&sit_i->sentry_lock);
1460        return 0;
1461}
1462
1463static int build_free_segmap(struct f2fs_sb_info *sbi)
1464{
1465        struct f2fs_sm_info *sm_info = SM_I(sbi);
1466        struct free_segmap_info *free_i;
1467        unsigned int bitmap_size, sec_bitmap_size;
1468
1469        /* allocate memory for free segmap information */
1470        free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1471        if (!free_i)
1472                return -ENOMEM;
1473
1474        SM_I(sbi)->free_info = free_i;
1475
1476        bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1477        free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1478        if (!free_i->free_segmap)
1479                return -ENOMEM;
1480
1481        sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1482        free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1483        if (!free_i->free_secmap)
1484                return -ENOMEM;
1485
1486        /* set all segments as dirty temporarily */
1487        memset(free_i->free_segmap, 0xff, bitmap_size);
1488        memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1489
1490        /* init free segmap information */
1491        free_i->start_segno =
1492                (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1493        free_i->free_segments = 0;
1494        free_i->free_sections = 0;
1495        rwlock_init(&free_i->segmap_lock);
1496        return 0;
1497}
1498
1499static int build_curseg(struct f2fs_sb_info *sbi)
1500{
1501        struct curseg_info *array;
1502        int i;
1503
1504        array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1505        if (!array)
1506                return -ENOMEM;
1507
1508        SM_I(sbi)->curseg_array = array;
1509
1510        for (i = 0; i < NR_CURSEG_TYPE; i++) {
1511                mutex_init(&array[i].curseg_mutex);
1512                array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1513                if (!array[i].sum_blk)
1514                        return -ENOMEM;
1515                array[i].segno = NULL_SEGNO;
1516                array[i].next_blkoff = 0;
1517        }
1518        return restore_curseg_summaries(sbi);
1519}
1520
1521static void build_sit_entries(struct f2fs_sb_info *sbi)
1522{
1523        struct sit_info *sit_i = SIT_I(sbi);
1524        struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1525        struct f2fs_summary_block *sum = curseg->sum_blk;
1526        unsigned int start;
1527
1528        for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1529                struct seg_entry *se = &sit_i->sentries[start];
1530                struct f2fs_sit_block *sit_blk;
1531                struct f2fs_sit_entry sit;
1532                struct page *page;
1533                int i;
1534
1535                mutex_lock(&curseg->curseg_mutex);
1536                for (i = 0; i < sits_in_cursum(sum); i++) {
1537                        if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1538                                sit = sit_in_journal(sum, i);
1539                                mutex_unlock(&curseg->curseg_mutex);
1540                                goto got_it;
1541                        }
1542                }
1543                mutex_unlock(&curseg->curseg_mutex);
1544                page = get_current_sit_page(sbi, start);
1545                sit_blk = (struct f2fs_sit_block *)page_address(page);
1546                sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1547                f2fs_put_page(page, 1);
1548got_it:
1549                check_block_count(sbi, start, &sit);
1550                seg_info_from_raw_sit(se, &sit);
1551                if (sbi->segs_per_sec > 1) {
1552                        struct sec_entry *e = get_sec_entry(sbi, start);
1553                        e->valid_blocks += se->valid_blocks;
1554                }
1555        }
1556}
1557
1558static void init_free_segmap(struct f2fs_sb_info *sbi)
1559{
1560        unsigned int start;
1561        int type;
1562
1563        for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1564                struct seg_entry *sentry = get_seg_entry(sbi, start);
1565                if (!sentry->valid_blocks)
1566                        __set_free(sbi, start);
1567        }
1568
1569        /* set use the current segments */
1570        for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1571                struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1572                __set_test_and_inuse(sbi, curseg_t->segno);
1573        }
1574}
1575
1576static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1577{
1578        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1579        struct free_segmap_info *free_i = FREE_I(sbi);
1580        unsigned int segno = 0, offset = 0;
1581        unsigned short valid_blocks;
1582
1583        while (segno < TOTAL_SEGS(sbi)) {
1584                /* find dirty segment based on free segmap */
1585                segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1586                if (segno >= TOTAL_SEGS(sbi))
1587                        break;
1588                offset = segno + 1;
1589                valid_blocks = get_valid_blocks(sbi, segno, 0);
1590                if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1591                        continue;
1592                mutex_lock(&dirty_i->seglist_lock);
1593                __locate_dirty_segment(sbi, segno, DIRTY);
1594                mutex_unlock(&dirty_i->seglist_lock);
1595        }
1596}
1597
1598static int init_victim_secmap(struct f2fs_sb_info *sbi)
1599{
1600        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1601        unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1602
1603        dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1604        if (!dirty_i->victim_secmap)
1605                return -ENOMEM;
1606        return 0;
1607}
1608
1609static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1610{
1611        struct dirty_seglist_info *dirty_i;
1612        unsigned int bitmap_size, i;
1613
1614        /* allocate memory for dirty segments list information */
1615        dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1616        if (!dirty_i)
1617                return -ENOMEM;
1618
1619        SM_I(sbi)->dirty_info = dirty_i;
1620        mutex_init(&dirty_i->seglist_lock);
1621
1622        bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1623
1624        for (i = 0; i < NR_DIRTY_TYPE; i++) {
1625                dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1626                if (!dirty_i->dirty_segmap[i])
1627                        return -ENOMEM;
1628        }
1629
1630        init_dirty_segmap(sbi);
1631        return init_victim_secmap(sbi);
1632}
1633
1634/*
1635 * Update min, max modified time for cost-benefit GC algorithm
1636 */
1637static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1638{
1639        struct sit_info *sit_i = SIT_I(sbi);
1640        unsigned int segno;
1641
1642        mutex_lock(&sit_i->sentry_lock);
1643
1644        sit_i->min_mtime = LLONG_MAX;
1645
1646        for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1647                unsigned int i;
1648                unsigned long long mtime = 0;
1649
1650                for (i = 0; i < sbi->segs_per_sec; i++)
1651                        mtime += get_seg_entry(sbi, segno + i)->mtime;
1652
1653                mtime = div_u64(mtime, sbi->segs_per_sec);
1654
1655                if (sit_i->min_mtime > mtime)
1656                        sit_i->min_mtime = mtime;
1657        }
1658        sit_i->max_mtime = get_mtime(sbi);
1659        mutex_unlock(&sit_i->sentry_lock);
1660}
1661
1662int build_segment_manager(struct f2fs_sb_info *sbi)
1663{
1664        struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1665        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1666        struct f2fs_sm_info *sm_info;
1667        int err;
1668
1669        sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1670        if (!sm_info)
1671                return -ENOMEM;
1672
1673        /* init sm info */
1674        sbi->sm_info = sm_info;
1675        INIT_LIST_HEAD(&sm_info->wblist_head);
1676        spin_lock_init(&sm_info->wblist_lock);
1677        sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1678        sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1679        sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1680        sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1681        sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1682        sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1683        sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1684
1685        err = build_sit_info(sbi);
1686        if (err)
1687                return err;
1688        err = build_free_segmap(sbi);
1689        if (err)
1690                return err;
1691        err = build_curseg(sbi);
1692        if (err)
1693                return err;
1694
1695        /* reinit free segmap based on SIT */
1696        build_sit_entries(sbi);
1697
1698        init_free_segmap(sbi);
1699        err = build_dirty_segmap(sbi);
1700        if (err)
1701                return err;
1702
1703        init_min_max_mtime(sbi);
1704        return 0;
1705}
1706
1707static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1708                enum dirty_type dirty_type)
1709{
1710        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1711
1712        mutex_lock(&dirty_i->seglist_lock);
1713        kfree(dirty_i->dirty_segmap[dirty_type]);
1714        dirty_i->nr_dirty[dirty_type] = 0;
1715        mutex_unlock(&dirty_i->seglist_lock);
1716}
1717
1718static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1719{
1720        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1721        kfree(dirty_i->victim_secmap);
1722}
1723
1724static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1725{
1726        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1727        int i;
1728
1729        if (!dirty_i)
1730                return;
1731
1732        /* discard pre-free/dirty segments list */
1733        for (i = 0; i < NR_DIRTY_TYPE; i++)
1734                discard_dirty_segmap(sbi, i);
1735
1736        destroy_victim_secmap(sbi);
1737        SM_I(sbi)->dirty_info = NULL;
1738        kfree(dirty_i);
1739}
1740
1741static void destroy_curseg(struct f2fs_sb_info *sbi)
1742{
1743        struct curseg_info *array = SM_I(sbi)->curseg_array;
1744        int i;
1745
1746        if (!array)
1747                return;
1748        SM_I(sbi)->curseg_array = NULL;
1749        for (i = 0; i < NR_CURSEG_TYPE; i++)
1750                kfree(array[i].sum_blk);
1751        kfree(array);
1752}
1753
1754static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1755{
1756        struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1757        if (!free_i)
1758                return;
1759        SM_I(sbi)->free_info = NULL;
1760        kfree(free_i->free_segmap);
1761        kfree(free_i->free_secmap);
1762        kfree(free_i);
1763}
1764
1765static void destroy_sit_info(struct f2fs_sb_info *sbi)
1766{
1767        struct sit_info *sit_i = SIT_I(sbi);
1768        unsigned int start;
1769
1770        if (!sit_i)
1771                return;
1772
1773        if (sit_i->sentries) {
1774                for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1775                        kfree(sit_i->sentries[start].cur_valid_map);
1776                        kfree(sit_i->sentries[start].ckpt_valid_map);
1777                }
1778        }
1779        vfree(sit_i->sentries);
1780        vfree(sit_i->sec_entries);
1781        kfree(sit_i->dirty_sentries_bitmap);
1782
1783        SM_I(sbi)->sit_info = NULL;
1784        kfree(sit_i->sit_bitmap);
1785        kfree(sit_i);
1786}
1787
1788void destroy_segment_manager(struct f2fs_sb_info *sbi)
1789{
1790        struct f2fs_sm_info *sm_info = SM_I(sbi);
1791        destroy_dirty_segmap(sbi);
1792        destroy_curseg(sbi);
1793        destroy_free_segmap(sbi);
1794        destroy_sit_info(sbi);
1795        sbi->sm_info = NULL;
1796        kfree(sm_info);
1797}
1798