linux/fs/f2fs/checkpoint.c
<<
>>
Prefs
   1/*
   2 * fs/f2fs/checkpoint.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/bio.h>
  13#include <linux/mpage.h>
  14#include <linux/writeback.h>
  15#include <linux/blkdev.h>
  16#include <linux/f2fs_fs.h>
  17#include <linux/pagevec.h>
  18#include <linux/swap.h>
  19
  20#include "f2fs.h"
  21#include "node.h"
  22#include "segment.h"
  23#include "trace.h"
  24#include <trace/events/f2fs.h>
  25
  26static struct kmem_cache *ino_entry_slab;
  27struct kmem_cache *f2fs_inode_entry_slab;
  28
  29void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
  30{
  31        set_ckpt_flags(sbi, CP_ERROR_FLAG);
  32        if (!end_io)
  33                f2fs_flush_merged_writes(sbi);
  34}
  35
  36/*
  37 * We guarantee no failure on the returned page.
  38 */
  39struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
  40{
  41        struct address_space *mapping = META_MAPPING(sbi);
  42        struct page *page = NULL;
  43repeat:
  44        page = f2fs_grab_cache_page(mapping, index, false);
  45        if (!page) {
  46                cond_resched();
  47                goto repeat;
  48        }
  49        f2fs_wait_on_page_writeback(page, META, true);
  50        if (!PageUptodate(page))
  51                SetPageUptodate(page);
  52        return page;
  53}
  54
  55/*
  56 * We guarantee no failure on the returned page.
  57 */
  58static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
  59                                                        bool is_meta)
  60{
  61        struct address_space *mapping = META_MAPPING(sbi);
  62        struct page *page;
  63        struct f2fs_io_info fio = {
  64                .sbi = sbi,
  65                .type = META,
  66                .op = REQ_OP_READ,
  67                .op_flags = REQ_META | REQ_PRIO,
  68                .old_blkaddr = index,
  69                .new_blkaddr = index,
  70                .encrypted_page = NULL,
  71                .is_meta = is_meta,
  72        };
  73
  74        if (unlikely(!is_meta))
  75                fio.op_flags &= ~REQ_META;
  76repeat:
  77        page = f2fs_grab_cache_page(mapping, index, false);
  78        if (!page) {
  79                cond_resched();
  80                goto repeat;
  81        }
  82        if (PageUptodate(page))
  83                goto out;
  84
  85        fio.page = page;
  86
  87        if (f2fs_submit_page_bio(&fio)) {
  88                f2fs_put_page(page, 1);
  89                goto repeat;
  90        }
  91
  92        lock_page(page);
  93        if (unlikely(page->mapping != mapping)) {
  94                f2fs_put_page(page, 1);
  95                goto repeat;
  96        }
  97
  98        /*
  99         * if there is any IO error when accessing device, make our filesystem
 100         * readonly and make sure do not write checkpoint with non-uptodate
 101         * meta page.
 102         */
 103        if (unlikely(!PageUptodate(page))) {
 104                memset(page_address(page), 0, PAGE_SIZE);
 105                f2fs_stop_checkpoint(sbi, false);
 106        }
 107out:
 108        return page;
 109}
 110
 111struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
 112{
 113        return __get_meta_page(sbi, index, true);
 114}
 115
 116/* for POR only */
 117struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
 118{
 119        return __get_meta_page(sbi, index, false);
 120}
 121
 122bool f2fs_is_valid_meta_blkaddr(struct f2fs_sb_info *sbi,
 123                                        block_t blkaddr, int type)
 124{
 125        switch (type) {
 126        case META_NAT:
 127                break;
 128        case META_SIT:
 129                if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
 130                        return false;
 131                break;
 132        case META_SSA:
 133                if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
 134                        blkaddr < SM_I(sbi)->ssa_blkaddr))
 135                        return false;
 136                break;
 137        case META_CP:
 138                if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
 139                        blkaddr < __start_cp_addr(sbi)))
 140                        return false;
 141                break;
 142        case META_POR:
 143                if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
 144                        blkaddr < MAIN_BLKADDR(sbi)))
 145                        return false;
 146                break;
 147        default:
 148                BUG();
 149        }
 150
 151        return true;
 152}
 153
 154/*
 155 * Readahead CP/NAT/SIT/SSA pages
 156 */
 157int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
 158                                                        int type, bool sync)
 159{
 160        struct page *page;
 161        block_t blkno = start;
 162        struct f2fs_io_info fio = {
 163                .sbi = sbi,
 164                .type = META,
 165                .op = REQ_OP_READ,
 166                .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
 167                .encrypted_page = NULL,
 168                .in_list = false,
 169                .is_meta = (type != META_POR),
 170        };
 171        struct blk_plug plug;
 172
 173        if (unlikely(type == META_POR))
 174                fio.op_flags &= ~REQ_META;
 175
 176        blk_start_plug(&plug);
 177        for (; nrpages-- > 0; blkno++) {
 178
 179                if (!f2fs_is_valid_meta_blkaddr(sbi, blkno, type))
 180                        goto out;
 181
 182                switch (type) {
 183                case META_NAT:
 184                        if (unlikely(blkno >=
 185                                        NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
 186                                blkno = 0;
 187                        /* get nat block addr */
 188                        fio.new_blkaddr = current_nat_addr(sbi,
 189                                        blkno * NAT_ENTRY_PER_BLOCK);
 190                        break;
 191                case META_SIT:
 192                        /* get sit block addr */
 193                        fio.new_blkaddr = current_sit_addr(sbi,
 194                                        blkno * SIT_ENTRY_PER_BLOCK);
 195                        break;
 196                case META_SSA:
 197                case META_CP:
 198                case META_POR:
 199                        fio.new_blkaddr = blkno;
 200                        break;
 201                default:
 202                        BUG();
 203                }
 204
 205                page = f2fs_grab_cache_page(META_MAPPING(sbi),
 206                                                fio.new_blkaddr, false);
 207                if (!page)
 208                        continue;
 209                if (PageUptodate(page)) {
 210                        f2fs_put_page(page, 1);
 211                        continue;
 212                }
 213
 214                fio.page = page;
 215                f2fs_submit_page_bio(&fio);
 216                f2fs_put_page(page, 0);
 217        }
 218out:
 219        blk_finish_plug(&plug);
 220        return blkno - start;
 221}
 222
 223void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
 224{
 225        struct page *page;
 226        bool readahead = false;
 227
 228        page = find_get_page(META_MAPPING(sbi), index);
 229        if (!page || !PageUptodate(page))
 230                readahead = true;
 231        f2fs_put_page(page, 0);
 232
 233        if (readahead)
 234                f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
 235}
 236
 237static int __f2fs_write_meta_page(struct page *page,
 238                                struct writeback_control *wbc,
 239                                enum iostat_type io_type)
 240{
 241        struct f2fs_sb_info *sbi = F2FS_P_SB(page);
 242
 243        trace_f2fs_writepage(page, META);
 244
 245        if (unlikely(f2fs_cp_error(sbi))) {
 246                dec_page_count(sbi, F2FS_DIRTY_META);
 247                unlock_page(page);
 248                return 0;
 249        }
 250        if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
 251                goto redirty_out;
 252        if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
 253                goto redirty_out;
 254
 255        f2fs_do_write_meta_page(sbi, page, io_type);
 256        dec_page_count(sbi, F2FS_DIRTY_META);
 257
 258        if (wbc->for_reclaim)
 259                f2fs_submit_merged_write_cond(sbi, page->mapping->host,
 260                                                0, page->index, META);
 261
 262        unlock_page(page);
 263
 264        if (unlikely(f2fs_cp_error(sbi)))
 265                f2fs_submit_merged_write(sbi, META);
 266
 267        return 0;
 268
 269redirty_out:
 270        redirty_page_for_writepage(wbc, page);
 271        return AOP_WRITEPAGE_ACTIVATE;
 272}
 273
 274static int f2fs_write_meta_page(struct page *page,
 275                                struct writeback_control *wbc)
 276{
 277        return __f2fs_write_meta_page(page, wbc, FS_META_IO);
 278}
 279
 280static int f2fs_write_meta_pages(struct address_space *mapping,
 281                                struct writeback_control *wbc)
 282{
 283        struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
 284        long diff, written;
 285
 286        if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
 287                goto skip_write;
 288
 289        /* collect a number of dirty meta pages and write together */
 290        if (wbc->for_kupdate ||
 291                get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
 292                goto skip_write;
 293
 294        /* if locked failed, cp will flush dirty pages instead */
 295        if (!mutex_trylock(&sbi->cp_mutex))
 296                goto skip_write;
 297
 298        trace_f2fs_writepages(mapping->host, wbc, META);
 299        diff = nr_pages_to_write(sbi, META, wbc);
 300        written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
 301        mutex_unlock(&sbi->cp_mutex);
 302        wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
 303        return 0;
 304
 305skip_write:
 306        wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
 307        trace_f2fs_writepages(mapping->host, wbc, META);
 308        return 0;
 309}
 310
 311long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
 312                                long nr_to_write, enum iostat_type io_type)
 313{
 314        struct address_space *mapping = META_MAPPING(sbi);
 315        pgoff_t index = 0, prev = ULONG_MAX;
 316        struct pagevec pvec;
 317        long nwritten = 0;
 318        int nr_pages;
 319        struct writeback_control wbc = {
 320                .for_reclaim = 0,
 321        };
 322        struct blk_plug plug;
 323
 324        pagevec_init(&pvec);
 325
 326        blk_start_plug(&plug);
 327
 328        while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
 329                                PAGECACHE_TAG_DIRTY))) {
 330                int i;
 331
 332                for (i = 0; i < nr_pages; i++) {
 333                        struct page *page = pvec.pages[i];
 334
 335                        if (prev == ULONG_MAX)
 336                                prev = page->index - 1;
 337                        if (nr_to_write != LONG_MAX && page->index != prev + 1) {
 338                                pagevec_release(&pvec);
 339                                goto stop;
 340                        }
 341
 342                        lock_page(page);
 343
 344                        if (unlikely(page->mapping != mapping)) {
 345continue_unlock:
 346                                unlock_page(page);
 347                                continue;
 348                        }
 349                        if (!PageDirty(page)) {
 350                                /* someone wrote it for us */
 351                                goto continue_unlock;
 352                        }
 353
 354                        f2fs_wait_on_page_writeback(page, META, true);
 355
 356                        BUG_ON(PageWriteback(page));
 357                        if (!clear_page_dirty_for_io(page))
 358                                goto continue_unlock;
 359
 360                        if (__f2fs_write_meta_page(page, &wbc, io_type)) {
 361                                unlock_page(page);
 362                                break;
 363                        }
 364                        nwritten++;
 365                        prev = page->index;
 366                        if (unlikely(nwritten >= nr_to_write))
 367                                break;
 368                }
 369                pagevec_release(&pvec);
 370                cond_resched();
 371        }
 372stop:
 373        if (nwritten)
 374                f2fs_submit_merged_write(sbi, type);
 375
 376        blk_finish_plug(&plug);
 377
 378        return nwritten;
 379}
 380
 381static int f2fs_set_meta_page_dirty(struct page *page)
 382{
 383        trace_f2fs_set_page_dirty(page, META);
 384
 385        if (!PageUptodate(page))
 386                SetPageUptodate(page);
 387        if (!PageDirty(page)) {
 388                __set_page_dirty_nobuffers(page);
 389                inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
 390                SetPagePrivate(page);
 391                f2fs_trace_pid(page);
 392                return 1;
 393        }
 394        return 0;
 395}
 396
 397const struct address_space_operations f2fs_meta_aops = {
 398        .writepage      = f2fs_write_meta_page,
 399        .writepages     = f2fs_write_meta_pages,
 400        .set_page_dirty = f2fs_set_meta_page_dirty,
 401        .invalidatepage = f2fs_invalidate_page,
 402        .releasepage    = f2fs_release_page,
 403#ifdef CONFIG_MIGRATION
 404        .migratepage    = f2fs_migrate_page,
 405#endif
 406};
 407
 408static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
 409                                                unsigned int devidx, int type)
 410{
 411        struct inode_management *im = &sbi->im[type];
 412        struct ino_entry *e, *tmp;
 413
 414        tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
 415
 416        radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
 417
 418        spin_lock(&im->ino_lock);
 419        e = radix_tree_lookup(&im->ino_root, ino);
 420        if (!e) {
 421                e = tmp;
 422                if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
 423                        f2fs_bug_on(sbi, 1);
 424
 425                memset(e, 0, sizeof(struct ino_entry));
 426                e->ino = ino;
 427
 428                list_add_tail(&e->list, &im->ino_list);
 429                if (type != ORPHAN_INO)
 430                        im->ino_num++;
 431        }
 432
 433        if (type == FLUSH_INO)
 434                f2fs_set_bit(devidx, (char *)&e->dirty_device);
 435
 436        spin_unlock(&im->ino_lock);
 437        radix_tree_preload_end();
 438
 439        if (e != tmp)
 440                kmem_cache_free(ino_entry_slab, tmp);
 441}
 442
 443static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
 444{
 445        struct inode_management *im = &sbi->im[type];
 446        struct ino_entry *e;
 447
 448        spin_lock(&im->ino_lock);
 449        e = radix_tree_lookup(&im->ino_root, ino);
 450        if (e) {
 451                list_del(&e->list);
 452                radix_tree_delete(&im->ino_root, ino);
 453                im->ino_num--;
 454                spin_unlock(&im->ino_lock);
 455                kmem_cache_free(ino_entry_slab, e);
 456                return;
 457        }
 458        spin_unlock(&im->ino_lock);
 459}
 460
 461void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
 462{
 463        /* add new dirty ino entry into list */
 464        __add_ino_entry(sbi, ino, 0, type);
 465}
 466
 467void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
 468{
 469        /* remove dirty ino entry from list */
 470        __remove_ino_entry(sbi, ino, type);
 471}
 472
 473/* mode should be APPEND_INO or UPDATE_INO */
 474bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
 475{
 476        struct inode_management *im = &sbi->im[mode];
 477        struct ino_entry *e;
 478
 479        spin_lock(&im->ino_lock);
 480        e = radix_tree_lookup(&im->ino_root, ino);
 481        spin_unlock(&im->ino_lock);
 482        return e ? true : false;
 483}
 484
 485void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
 486{
 487        struct ino_entry *e, *tmp;
 488        int i;
 489
 490        for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
 491                struct inode_management *im = &sbi->im[i];
 492
 493                spin_lock(&im->ino_lock);
 494                list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
 495                        list_del(&e->list);
 496                        radix_tree_delete(&im->ino_root, e->ino);
 497                        kmem_cache_free(ino_entry_slab, e);
 498                        im->ino_num--;
 499                }
 500                spin_unlock(&im->ino_lock);
 501        }
 502}
 503
 504void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
 505                                        unsigned int devidx, int type)
 506{
 507        __add_ino_entry(sbi, ino, devidx, type);
 508}
 509
 510bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
 511                                        unsigned int devidx, int type)
 512{
 513        struct inode_management *im = &sbi->im[type];
 514        struct ino_entry *e;
 515        bool is_dirty = false;
 516
 517        spin_lock(&im->ino_lock);
 518        e = radix_tree_lookup(&im->ino_root, ino);
 519        if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
 520                is_dirty = true;
 521        spin_unlock(&im->ino_lock);
 522        return is_dirty;
 523}
 524
 525int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
 526{
 527        struct inode_management *im = &sbi->im[ORPHAN_INO];
 528        int err = 0;
 529
 530        spin_lock(&im->ino_lock);
 531
 532#ifdef CONFIG_F2FS_FAULT_INJECTION
 533        if (time_to_inject(sbi, FAULT_ORPHAN)) {
 534                spin_unlock(&im->ino_lock);
 535                f2fs_show_injection_info(FAULT_ORPHAN);
 536                return -ENOSPC;
 537        }
 538#endif
 539        if (unlikely(im->ino_num >= sbi->max_orphans))
 540                err = -ENOSPC;
 541        else
 542                im->ino_num++;
 543        spin_unlock(&im->ino_lock);
 544
 545        return err;
 546}
 547
 548void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
 549{
 550        struct inode_management *im = &sbi->im[ORPHAN_INO];
 551
 552        spin_lock(&im->ino_lock);
 553        f2fs_bug_on(sbi, im->ino_num == 0);
 554        im->ino_num--;
 555        spin_unlock(&im->ino_lock);
 556}
 557
 558void f2fs_add_orphan_inode(struct inode *inode)
 559{
 560        /* add new orphan ino entry into list */
 561        __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
 562        f2fs_update_inode_page(inode);
 563}
 564
 565void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
 566{
 567        /* remove orphan entry from orphan list */
 568        __remove_ino_entry(sbi, ino, ORPHAN_INO);
 569}
 570
 571static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
 572{
 573        struct inode *inode;
 574        struct node_info ni;
 575        int err = f2fs_acquire_orphan_inode(sbi);
 576
 577        if (err)
 578                goto err_out;
 579
 580        __add_ino_entry(sbi, ino, 0, ORPHAN_INO);
 581
 582        inode = f2fs_iget_retry(sbi->sb, ino);
 583        if (IS_ERR(inode)) {
 584                /*
 585                 * there should be a bug that we can't find the entry
 586                 * to orphan inode.
 587                 */
 588                f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
 589                return PTR_ERR(inode);
 590        }
 591
 592        err = dquot_initialize(inode);
 593        if (err) {
 594                iput(inode);
 595                goto err_out;
 596        }
 597
 598        clear_nlink(inode);
 599
 600        /* truncate all the data during iput */
 601        iput(inode);
 602
 603        f2fs_get_node_info(sbi, ino, &ni);
 604
 605        /* ENOMEM was fully retried in f2fs_evict_inode. */
 606        if (ni.blk_addr != NULL_ADDR) {
 607                err = -EIO;
 608                goto err_out;
 609        }
 610        __remove_ino_entry(sbi, ino, ORPHAN_INO);
 611        return 0;
 612
 613err_out:
 614        set_sbi_flag(sbi, SBI_NEED_FSCK);
 615        f2fs_msg(sbi->sb, KERN_WARNING,
 616                        "%s: orphan failed (ino=%x), run fsck to fix.",
 617                        __func__, ino);
 618        return err;
 619}
 620
 621int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 622{
 623        block_t start_blk, orphan_blocks, i, j;
 624        unsigned int s_flags = sbi->sb->s_flags;
 625        int err = 0;
 626#ifdef CONFIG_QUOTA
 627        int quota_enabled;
 628#endif
 629
 630        if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
 631                return 0;
 632
 633        if (s_flags & SB_RDONLY) {
 634                f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
 635                sbi->sb->s_flags &= ~SB_RDONLY;
 636        }
 637
 638#ifdef CONFIG_QUOTA
 639        /* Needed for iput() to work correctly and not trash data */
 640        sbi->sb->s_flags |= SB_ACTIVE;
 641
 642        /* Turn on quotas so that they are updated correctly */
 643        quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
 644#endif
 645
 646        start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
 647        orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 648
 649        f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
 650
 651        for (i = 0; i < orphan_blocks; i++) {
 652                struct page *page = f2fs_get_meta_page(sbi, start_blk + i);
 653                struct f2fs_orphan_block *orphan_blk;
 654
 655                orphan_blk = (struct f2fs_orphan_block *)page_address(page);
 656                for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
 657                        nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
 658                        err = recover_orphan_inode(sbi, ino);
 659                        if (err) {
 660                                f2fs_put_page(page, 1);
 661                                goto out;
 662                        }
 663                }
 664                f2fs_put_page(page, 1);
 665        }
 666        /* clear Orphan Flag */
 667        clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 668out:
 669#ifdef CONFIG_QUOTA
 670        /* Turn quotas off */
 671        if (quota_enabled)
 672                f2fs_quota_off_umount(sbi->sb);
 673#endif
 674        sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
 675
 676        return err;
 677}
 678
 679static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
 680{
 681        struct list_head *head;
 682        struct f2fs_orphan_block *orphan_blk = NULL;
 683        unsigned int nentries = 0;
 684        unsigned short index = 1;
 685        unsigned short orphan_blocks;
 686        struct page *page = NULL;
 687        struct ino_entry *orphan = NULL;
 688        struct inode_management *im = &sbi->im[ORPHAN_INO];
 689
 690        orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
 691
 692        /*
 693         * we don't need to do spin_lock(&im->ino_lock) here, since all the
 694         * orphan inode operations are covered under f2fs_lock_op().
 695         * And, spin_lock should be avoided due to page operations below.
 696         */
 697        head = &im->ino_list;
 698
 699        /* loop for each orphan inode entry and write them in Jornal block */
 700        list_for_each_entry(orphan, head, list) {
 701                if (!page) {
 702                        page = f2fs_grab_meta_page(sbi, start_blk++);
 703                        orphan_blk =
 704                                (struct f2fs_orphan_block *)page_address(page);
 705                        memset(orphan_blk, 0, sizeof(*orphan_blk));
 706                }
 707
 708                orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
 709
 710                if (nentries == F2FS_ORPHANS_PER_BLOCK) {
 711                        /*
 712                         * an orphan block is full of 1020 entries,
 713                         * then we need to flush current orphan blocks
 714                         * and bring another one in memory
 715                         */
 716                        orphan_blk->blk_addr = cpu_to_le16(index);
 717                        orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
 718                        orphan_blk->entry_count = cpu_to_le32(nentries);
 719                        set_page_dirty(page);
 720                        f2fs_put_page(page, 1);
 721                        index++;
 722                        nentries = 0;
 723                        page = NULL;
 724                }
 725        }
 726
 727        if (page) {
 728                orphan_blk->blk_addr = cpu_to_le16(index);
 729                orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
 730                orphan_blk->entry_count = cpu_to_le32(nentries);
 731                set_page_dirty(page);
 732                f2fs_put_page(page, 1);
 733        }
 734}
 735
 736static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
 737                struct f2fs_checkpoint **cp_block, struct page **cp_page,
 738                unsigned long long *version)
 739{
 740        unsigned long blk_size = sbi->blocksize;
 741        size_t crc_offset = 0;
 742        __u32 crc = 0;
 743
 744        *cp_page = f2fs_get_meta_page(sbi, cp_addr);
 745        *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
 746
 747        crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
 748        if (crc_offset > (blk_size - sizeof(__le32))) {
 749                f2fs_msg(sbi->sb, KERN_WARNING,
 750                        "invalid crc_offset: %zu", crc_offset);
 751                return -EINVAL;
 752        }
 753
 754        crc = cur_cp_crc(*cp_block);
 755        if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
 756                f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
 757                return -EINVAL;
 758        }
 759
 760        *version = cur_cp_version(*cp_block);
 761        return 0;
 762}
 763
 764static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
 765                                block_t cp_addr, unsigned long long *version)
 766{
 767        struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
 768        struct f2fs_checkpoint *cp_block = NULL;
 769        unsigned long long cur_version = 0, pre_version = 0;
 770        int err;
 771
 772        err = get_checkpoint_version(sbi, cp_addr, &cp_block,
 773                                        &cp_page_1, version);
 774        if (err)
 775                goto invalid_cp1;
 776        pre_version = *version;
 777
 778        cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
 779        err = get_checkpoint_version(sbi, cp_addr, &cp_block,
 780                                        &cp_page_2, version);
 781        if (err)
 782                goto invalid_cp2;
 783        cur_version = *version;
 784
 785        if (cur_version == pre_version) {
 786                *version = cur_version;
 787                f2fs_put_page(cp_page_2, 1);
 788                return cp_page_1;
 789        }
 790invalid_cp2:
 791        f2fs_put_page(cp_page_2, 1);
 792invalid_cp1:
 793        f2fs_put_page(cp_page_1, 1);
 794        return NULL;
 795}
 796
 797int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
 798{
 799        struct f2fs_checkpoint *cp_block;
 800        struct f2fs_super_block *fsb = sbi->raw_super;
 801        struct page *cp1, *cp2, *cur_page;
 802        unsigned long blk_size = sbi->blocksize;
 803        unsigned long long cp1_version = 0, cp2_version = 0;
 804        unsigned long long cp_start_blk_no;
 805        unsigned int cp_blks = 1 + __cp_payload(sbi);
 806        block_t cp_blk_no;
 807        int i;
 808
 809        sbi->ckpt = f2fs_kzalloc(sbi, array_size(blk_size, cp_blks),
 810                                 GFP_KERNEL);
 811        if (!sbi->ckpt)
 812                return -ENOMEM;
 813        /*
 814         * Finding out valid cp block involves read both
 815         * sets( cp pack1 and cp pack 2)
 816         */
 817        cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
 818        cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
 819
 820        /* The second checkpoint pack should start at the next segment */
 821        cp_start_blk_no += ((unsigned long long)1) <<
 822                                le32_to_cpu(fsb->log_blocks_per_seg);
 823        cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
 824
 825        if (cp1 && cp2) {
 826                if (ver_after(cp2_version, cp1_version))
 827                        cur_page = cp2;
 828                else
 829                        cur_page = cp1;
 830        } else if (cp1) {
 831                cur_page = cp1;
 832        } else if (cp2) {
 833                cur_page = cp2;
 834        } else {
 835                goto fail_no_cp;
 836        }
 837
 838        cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
 839        memcpy(sbi->ckpt, cp_block, blk_size);
 840
 841        /* Sanity checking of checkpoint */
 842        if (f2fs_sanity_check_ckpt(sbi))
 843                goto free_fail_no_cp;
 844
 845        if (cur_page == cp1)
 846                sbi->cur_cp_pack = 1;
 847        else
 848                sbi->cur_cp_pack = 2;
 849
 850        if (cp_blks <= 1)
 851                goto done;
 852
 853        cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
 854        if (cur_page == cp2)
 855                cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
 856
 857        for (i = 1; i < cp_blks; i++) {
 858                void *sit_bitmap_ptr;
 859                unsigned char *ckpt = (unsigned char *)sbi->ckpt;
 860
 861                cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
 862                sit_bitmap_ptr = page_address(cur_page);
 863                memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
 864                f2fs_put_page(cur_page, 1);
 865        }
 866done:
 867        f2fs_put_page(cp1, 1);
 868        f2fs_put_page(cp2, 1);
 869        return 0;
 870
 871free_fail_no_cp:
 872        f2fs_put_page(cp1, 1);
 873        f2fs_put_page(cp2, 1);
 874fail_no_cp:
 875        kfree(sbi->ckpt);
 876        return -EINVAL;
 877}
 878
 879static void __add_dirty_inode(struct inode *inode, enum inode_type type)
 880{
 881        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 882        int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
 883
 884        if (is_inode_flag_set(inode, flag))
 885                return;
 886
 887        set_inode_flag(inode, flag);
 888        if (!f2fs_is_volatile_file(inode))
 889                list_add_tail(&F2FS_I(inode)->dirty_list,
 890                                                &sbi->inode_list[type]);
 891        stat_inc_dirty_inode(sbi, type);
 892}
 893
 894static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
 895{
 896        int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
 897
 898        if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
 899                return;
 900
 901        list_del_init(&F2FS_I(inode)->dirty_list);
 902        clear_inode_flag(inode, flag);
 903        stat_dec_dirty_inode(F2FS_I_SB(inode), type);
 904}
 905
 906void f2fs_update_dirty_page(struct inode *inode, struct page *page)
 907{
 908        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 909        enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
 910
 911        if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
 912                        !S_ISLNK(inode->i_mode))
 913                return;
 914
 915        spin_lock(&sbi->inode_lock[type]);
 916        if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
 917                __add_dirty_inode(inode, type);
 918        inode_inc_dirty_pages(inode);
 919        spin_unlock(&sbi->inode_lock[type]);
 920
 921        SetPagePrivate(page);
 922        f2fs_trace_pid(page);
 923}
 924
 925void f2fs_remove_dirty_inode(struct inode *inode)
 926{
 927        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 928        enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
 929
 930        if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
 931                        !S_ISLNK(inode->i_mode))
 932                return;
 933
 934        if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
 935                return;
 936
 937        spin_lock(&sbi->inode_lock[type]);
 938        __remove_dirty_inode(inode, type);
 939        spin_unlock(&sbi->inode_lock[type]);
 940}
 941
 942int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
 943{
 944        struct list_head *head;
 945        struct inode *inode;
 946        struct f2fs_inode_info *fi;
 947        bool is_dir = (type == DIR_INODE);
 948        unsigned long ino = 0;
 949
 950        trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
 951                                get_pages(sbi, is_dir ?
 952                                F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
 953retry:
 954        if (unlikely(f2fs_cp_error(sbi)))
 955                return -EIO;
 956
 957        spin_lock(&sbi->inode_lock[type]);
 958
 959        head = &sbi->inode_list[type];
 960        if (list_empty(head)) {
 961                spin_unlock(&sbi->inode_lock[type]);
 962                trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
 963                                get_pages(sbi, is_dir ?
 964                                F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
 965                return 0;
 966        }
 967        fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
 968        inode = igrab(&fi->vfs_inode);
 969        spin_unlock(&sbi->inode_lock[type]);
 970        if (inode) {
 971                unsigned long cur_ino = inode->i_ino;
 972
 973                if (is_dir)
 974                        F2FS_I(inode)->cp_task = current;
 975
 976                filemap_fdatawrite(inode->i_mapping);
 977
 978                if (is_dir)
 979                        F2FS_I(inode)->cp_task = NULL;
 980
 981                iput(inode);
 982                /* We need to give cpu to another writers. */
 983                if (ino == cur_ino) {
 984                        congestion_wait(BLK_RW_ASYNC, HZ/50);
 985                        cond_resched();
 986                } else {
 987                        ino = cur_ino;
 988                }
 989        } else {
 990                /*
 991                 * We should submit bio, since it exists several
 992                 * wribacking dentry pages in the freeing inode.
 993                 */
 994                f2fs_submit_merged_write(sbi, DATA);
 995                cond_resched();
 996        }
 997        goto retry;
 998}
 999
1000int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
1001{
1002        struct list_head *head = &sbi->inode_list[DIRTY_META];
1003        struct inode *inode;
1004        struct f2fs_inode_info *fi;
1005        s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
1006
1007        while (total--) {
1008                if (unlikely(f2fs_cp_error(sbi)))
1009                        return -EIO;
1010
1011                spin_lock(&sbi->inode_lock[DIRTY_META]);
1012                if (list_empty(head)) {
1013                        spin_unlock(&sbi->inode_lock[DIRTY_META]);
1014                        return 0;
1015                }
1016                fi = list_first_entry(head, struct f2fs_inode_info,
1017                                                        gdirty_list);
1018                inode = igrab(&fi->vfs_inode);
1019                spin_unlock(&sbi->inode_lock[DIRTY_META]);
1020                if (inode) {
1021                        sync_inode_metadata(inode, 0);
1022
1023                        /* it's on eviction */
1024                        if (is_inode_flag_set(inode, FI_DIRTY_INODE))
1025                                f2fs_update_inode_page(inode);
1026                        iput(inode);
1027                }
1028        }
1029        return 0;
1030}
1031
1032static void __prepare_cp_block(struct f2fs_sb_info *sbi)
1033{
1034        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1035        struct f2fs_nm_info *nm_i = NM_I(sbi);
1036        nid_t last_nid = nm_i->next_scan_nid;
1037
1038        next_free_nid(sbi, &last_nid);
1039        ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1040        ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1041        ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1042        ckpt->next_free_nid = cpu_to_le32(last_nid);
1043}
1044
1045/*
1046 * Freeze all the FS-operations for checkpoint.
1047 */
1048static int block_operations(struct f2fs_sb_info *sbi)
1049{
1050        struct writeback_control wbc = {
1051                .sync_mode = WB_SYNC_ALL,
1052                .nr_to_write = LONG_MAX,
1053                .for_reclaim = 0,
1054        };
1055        struct blk_plug plug;
1056        int err = 0;
1057
1058        blk_start_plug(&plug);
1059
1060retry_flush_dents:
1061        f2fs_lock_all(sbi);
1062        /* write all the dirty dentry pages */
1063        if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1064                f2fs_unlock_all(sbi);
1065                err = f2fs_sync_dirty_inodes(sbi, DIR_INODE);
1066                if (err)
1067                        goto out;
1068                cond_resched();
1069                goto retry_flush_dents;
1070        }
1071
1072        /*
1073         * POR: we should ensure that there are no dirty node pages
1074         * until finishing nat/sit flush. inode->i_blocks can be updated.
1075         */
1076        down_write(&sbi->node_change);
1077
1078        if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1079                up_write(&sbi->node_change);
1080                f2fs_unlock_all(sbi);
1081                err = f2fs_sync_inode_meta(sbi);
1082                if (err)
1083                        goto out;
1084                cond_resched();
1085                goto retry_flush_dents;
1086        }
1087
1088retry_flush_nodes:
1089        down_write(&sbi->node_write);
1090
1091        if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1092                up_write(&sbi->node_write);
1093                atomic_inc(&sbi->wb_sync_req[NODE]);
1094                err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1095                atomic_dec(&sbi->wb_sync_req[NODE]);
1096                if (err) {
1097                        up_write(&sbi->node_change);
1098                        f2fs_unlock_all(sbi);
1099                        goto out;
1100                }
1101                cond_resched();
1102                goto retry_flush_nodes;
1103        }
1104
1105        /*
1106         * sbi->node_change is used only for AIO write_begin path which produces
1107         * dirty node blocks and some checkpoint values by block allocation.
1108         */
1109        __prepare_cp_block(sbi);
1110        up_write(&sbi->node_change);
1111out:
1112        blk_finish_plug(&plug);
1113        return err;
1114}
1115
1116static void unblock_operations(struct f2fs_sb_info *sbi)
1117{
1118        up_write(&sbi->node_write);
1119        f2fs_unlock_all(sbi);
1120}
1121
1122static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1123{
1124        DEFINE_WAIT(wait);
1125
1126        for (;;) {
1127                prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1128
1129                if (!get_pages(sbi, F2FS_WB_CP_DATA))
1130                        break;
1131
1132                io_schedule_timeout(5*HZ);
1133        }
1134        finish_wait(&sbi->cp_wait, &wait);
1135}
1136
1137static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1138{
1139        unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1140        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1141        unsigned long flags;
1142
1143        spin_lock_irqsave(&sbi->cp_lock, flags);
1144
1145        if ((cpc->reason & CP_UMOUNT) &&
1146                        le32_to_cpu(ckpt->cp_pack_total_block_count) >
1147                        sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1148                disable_nat_bits(sbi, false);
1149
1150        if (cpc->reason & CP_TRIMMED)
1151                __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1152        else
1153                __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1154
1155        if (cpc->reason & CP_UMOUNT)
1156                __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1157        else
1158                __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1159
1160        if (cpc->reason & CP_FASTBOOT)
1161                __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1162        else
1163                __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1164
1165        if (orphan_num)
1166                __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1167        else
1168                __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1169
1170        if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1171                __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1172
1173        /* set this flag to activate crc|cp_ver for recovery */
1174        __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1175        __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1176
1177        spin_unlock_irqrestore(&sbi->cp_lock, flags);
1178}
1179
1180static void commit_checkpoint(struct f2fs_sb_info *sbi,
1181        void *src, block_t blk_addr)
1182{
1183        struct writeback_control wbc = {
1184                .for_reclaim = 0,
1185        };
1186
1187        /*
1188         * pagevec_lookup_tag and lock_page again will take
1189         * some extra time. Therefore, f2fs_update_meta_pages and
1190         * f2fs_sync_meta_pages are combined in this function.
1191         */
1192        struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
1193        int err;
1194
1195        memcpy(page_address(page), src, PAGE_SIZE);
1196        set_page_dirty(page);
1197
1198        f2fs_wait_on_page_writeback(page, META, true);
1199        f2fs_bug_on(sbi, PageWriteback(page));
1200        if (unlikely(!clear_page_dirty_for_io(page)))
1201                f2fs_bug_on(sbi, 1);
1202
1203        /* writeout cp pack 2 page */
1204        err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1205        f2fs_bug_on(sbi, err);
1206
1207        f2fs_put_page(page, 0);
1208
1209        /* submit checkpoint (with barrier if NOBARRIER is not set) */
1210        f2fs_submit_merged_write(sbi, META_FLUSH);
1211}
1212
1213static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1214{
1215        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1216        struct f2fs_nm_info *nm_i = NM_I(sbi);
1217        unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1218        block_t start_blk;
1219        unsigned int data_sum_blocks, orphan_blocks;
1220        __u32 crc32 = 0;
1221        int i;
1222        int cp_payload_blks = __cp_payload(sbi);
1223        struct super_block *sb = sbi->sb;
1224        struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1225        u64 kbytes_written;
1226        int err;
1227
1228        /* Flush all the NAT/SIT pages */
1229        while (get_pages(sbi, F2FS_DIRTY_META)) {
1230                f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1231                if (unlikely(f2fs_cp_error(sbi)))
1232                        return -EIO;
1233        }
1234
1235        /*
1236         * modify checkpoint
1237         * version number is already updated
1238         */
1239        ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
1240        ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1241        for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1242                ckpt->cur_node_segno[i] =
1243                        cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1244                ckpt->cur_node_blkoff[i] =
1245                        cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1246                ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1247                                curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1248        }
1249        for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1250                ckpt->cur_data_segno[i] =
1251                        cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1252                ckpt->cur_data_blkoff[i] =
1253                        cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1254                ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1255                                curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1256        }
1257
1258        /* 2 cp  + n data seg summary + orphan inode blocks */
1259        data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1260        spin_lock_irqsave(&sbi->cp_lock, flags);
1261        if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1262                __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1263        else
1264                __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1265        spin_unlock_irqrestore(&sbi->cp_lock, flags);
1266
1267        orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1268        ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1269                        orphan_blocks);
1270
1271        if (__remain_node_summaries(cpc->reason))
1272                ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1273                                cp_payload_blks + data_sum_blocks +
1274                                orphan_blocks + NR_CURSEG_NODE_TYPE);
1275        else
1276                ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1277                                cp_payload_blks + data_sum_blocks +
1278                                orphan_blocks);
1279
1280        /* update ckpt flag for checkpoint */
1281        update_ckpt_flags(sbi, cpc);
1282
1283        /* update SIT/NAT bitmap */
1284        get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1285        get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1286
1287        crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
1288        *((__le32 *)((unsigned char *)ckpt +
1289                                le32_to_cpu(ckpt->checksum_offset)))
1290                                = cpu_to_le32(crc32);
1291
1292        start_blk = __start_cp_next_addr(sbi);
1293
1294        /* write nat bits */
1295        if (enabled_nat_bits(sbi, cpc)) {
1296                __u64 cp_ver = cur_cp_version(ckpt);
1297                block_t blk;
1298
1299                cp_ver |= ((__u64)crc32 << 32);
1300                *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
1301
1302                blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
1303                for (i = 0; i < nm_i->nat_bits_blocks; i++)
1304                        f2fs_update_meta_page(sbi, nm_i->nat_bits +
1305                                        (i << F2FS_BLKSIZE_BITS), blk + i);
1306
1307                /* Flush all the NAT BITS pages */
1308                while (get_pages(sbi, F2FS_DIRTY_META)) {
1309                        f2fs_sync_meta_pages(sbi, META, LONG_MAX,
1310                                                        FS_CP_META_IO);
1311                        if (unlikely(f2fs_cp_error(sbi)))
1312                                return -EIO;
1313                }
1314        }
1315
1316        /* write out checkpoint buffer at block 0 */
1317        f2fs_update_meta_page(sbi, ckpt, start_blk++);
1318
1319        for (i = 1; i < 1 + cp_payload_blks; i++)
1320                f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1321                                                        start_blk++);
1322
1323        if (orphan_num) {
1324                write_orphan_inodes(sbi, start_blk);
1325                start_blk += orphan_blocks;
1326        }
1327
1328        f2fs_write_data_summaries(sbi, start_blk);
1329        start_blk += data_sum_blocks;
1330
1331        /* Record write statistics in the hot node summary */
1332        kbytes_written = sbi->kbytes_written;
1333        if (sb->s_bdev->bd_part)
1334                kbytes_written += BD_PART_WRITTEN(sbi);
1335
1336        seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1337
1338        if (__remain_node_summaries(cpc->reason)) {
1339                f2fs_write_node_summaries(sbi, start_blk);
1340                start_blk += NR_CURSEG_NODE_TYPE;
1341        }
1342
1343        /* update user_block_counts */
1344        sbi->last_valid_block_count = sbi->total_valid_block_count;
1345        percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1346
1347        /* Here, we have one bio having CP pack except cp pack 2 page */
1348        f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1349
1350        /* wait for previous submitted meta pages writeback */
1351        wait_on_all_pages_writeback(sbi);
1352
1353        if (unlikely(f2fs_cp_error(sbi)))
1354                return -EIO;
1355
1356        /* flush all device cache */
1357        err = f2fs_flush_device_cache(sbi);
1358        if (err)
1359                return err;
1360
1361        /* barrier and flush checkpoint cp pack 2 page if it can */
1362        commit_checkpoint(sbi, ckpt, start_blk);
1363        wait_on_all_pages_writeback(sbi);
1364
1365        f2fs_release_ino_entry(sbi, false);
1366
1367        if (unlikely(f2fs_cp_error(sbi)))
1368                return -EIO;
1369
1370        clear_sbi_flag(sbi, SBI_IS_DIRTY);
1371        clear_sbi_flag(sbi, SBI_NEED_CP);
1372        __set_cp_next_pack(sbi);
1373
1374        /*
1375         * redirty superblock if metadata like node page or inode cache is
1376         * updated during writing checkpoint.
1377         */
1378        if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1379                        get_pages(sbi, F2FS_DIRTY_IMETA))
1380                set_sbi_flag(sbi, SBI_IS_DIRTY);
1381
1382        f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1383
1384        return 0;
1385}
1386
1387/*
1388 * We guarantee that this checkpoint procedure will not fail.
1389 */
1390int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1391{
1392        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1393        unsigned long long ckpt_ver;
1394        int err = 0;
1395
1396        mutex_lock(&sbi->cp_mutex);
1397
1398        if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1399                ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1400                ((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
1401                goto out;
1402        if (unlikely(f2fs_cp_error(sbi))) {
1403                err = -EIO;
1404                goto out;
1405        }
1406        if (f2fs_readonly(sbi->sb)) {
1407                err = -EROFS;
1408                goto out;
1409        }
1410
1411        trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1412
1413        err = block_operations(sbi);
1414        if (err)
1415                goto out;
1416
1417        trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1418
1419        f2fs_flush_merged_writes(sbi);
1420
1421        /* this is the case of multiple fstrims without any changes */
1422        if (cpc->reason & CP_DISCARD) {
1423                if (!f2fs_exist_trim_candidates(sbi, cpc)) {
1424                        unblock_operations(sbi);
1425                        goto out;
1426                }
1427
1428                if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1429                                SIT_I(sbi)->dirty_sentries == 0 &&
1430                                prefree_segments(sbi) == 0) {
1431                        f2fs_flush_sit_entries(sbi, cpc);
1432                        f2fs_clear_prefree_segments(sbi, cpc);
1433                        unblock_operations(sbi);
1434                        goto out;
1435                }
1436        }
1437
1438        /*
1439         * update checkpoint pack index
1440         * Increase the version number so that
1441         * SIT entries and seg summaries are written at correct place
1442         */
1443        ckpt_ver = cur_cp_version(ckpt);
1444        ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1445
1446        /* write cached NAT/SIT entries to NAT/SIT area */
1447        f2fs_flush_nat_entries(sbi, cpc);
1448        f2fs_flush_sit_entries(sbi, cpc);
1449
1450        /* unlock all the fs_lock[] in do_checkpoint() */
1451        err = do_checkpoint(sbi, cpc);
1452        if (err)
1453                f2fs_release_discard_addrs(sbi);
1454        else
1455                f2fs_clear_prefree_segments(sbi, cpc);
1456
1457        unblock_operations(sbi);
1458        stat_inc_cp_count(sbi->stat_info);
1459
1460        if (cpc->reason & CP_RECOVERY)
1461                f2fs_msg(sbi->sb, KERN_NOTICE,
1462                        "checkpoint: version = %llx", ckpt_ver);
1463
1464        /* do checkpoint periodically */
1465        f2fs_update_time(sbi, CP_TIME);
1466        trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1467out:
1468        mutex_unlock(&sbi->cp_mutex);
1469        return err;
1470}
1471
1472void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1473{
1474        int i;
1475
1476        for (i = 0; i < MAX_INO_ENTRY; i++) {
1477                struct inode_management *im = &sbi->im[i];
1478
1479                INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1480                spin_lock_init(&im->ino_lock);
1481                INIT_LIST_HEAD(&im->ino_list);
1482                im->ino_num = 0;
1483        }
1484
1485        sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1486                        NR_CURSEG_TYPE - __cp_payload(sbi)) *
1487                                F2FS_ORPHANS_PER_BLOCK;
1488}
1489
1490int __init f2fs_create_checkpoint_caches(void)
1491{
1492        ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1493                        sizeof(struct ino_entry));
1494        if (!ino_entry_slab)
1495                return -ENOMEM;
1496        f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1497                        sizeof(struct inode_entry));
1498        if (!f2fs_inode_entry_slab) {
1499                kmem_cache_destroy(ino_entry_slab);
1500                return -ENOMEM;
1501        }
1502        return 0;
1503}
1504
1505void f2fs_destroy_checkpoint_caches(void)
1506{
1507        kmem_cache_destroy(ino_entry_slab);
1508        kmem_cache_destroy(f2fs_inode_entry_slab);
1509}
1510