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