Merge tag 'f2fs-for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk...
authorLinus Torvalds <torvalds@linux-foundation.org>
Sat, 13 Jul 2019 00:28:24 +0000 (17:28 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 13 Jul 2019 00:28:24 +0000 (17:28 -0700)
Pull f2fs updates from Jaegeuk Kim:
 "In this round, we've introduced native swap file support which can
  exploit DIO, enhanced existing checkpoint=disable feature with
  additional mount option to tune the triggering condition, and allowed
  user to preallocate physical blocks in a pinned file which will be
  useful to avoid f2fs fragmentation in append-only workloads. In
  addition, we've fixed subtle quota corruption issue.

  Enhancements:
   - add swap file support which uses DIO
   - allocate blocks for pinned file
   - allow SSR and mount option to enhance checkpoint=disable
   - enhance IPU IOs
   - add more sanity checks such as memory boundary access

  Bug fixes:
   - quota corruption in very corner case of error-injected SPO case
   - fix root_reserved on remount and some wrong counts
   - add missing fsck flag

  Some patches were also introduced to clean up ambiguous i_flags and
  debugging messages codes"

* tag 'f2fs-for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs: (33 commits)
  f2fs: improve print log in f2fs_sanity_check_ckpt()
  f2fs: avoid out-of-range memory access
  f2fs: fix to avoid long latency during umount
  f2fs: allow all the users to pin a file
  f2fs: support swap file w/ DIO
  f2fs: allocate blocks for pinned file
  f2fs: fix is_idle() check for discard type
  f2fs: add a rw_sem to cover quota flag changes
  f2fs: set SBI_NEED_FSCK for xattr corruption case
  f2fs: use generic EFSBADCRC/EFSCORRUPTED
  f2fs: Use DIV_ROUND_UP() instead of open-coding
  f2fs: print kernel message if filesystem is inconsistent
  f2fs: introduce f2fs_<level> macros to wrap f2fs_printk()
  f2fs: avoid get_valid_blocks() for cleanup
  f2fs: ioctl for removing a range from F2FS
  f2fs: only set project inherit bit for directory
  f2fs: separate f2fs i_flags from fs_flags and ext4 i_flags
  f2fs: replace ktype default_attrs with default_groups
  f2fs: Add option to limit required GC for checkpoint=disable
  f2fs: Fix accounting for unusable blocks
  ...

1  2 
fs/f2fs/data.c
include/trace/events/f2fs.h

diff --combined fs/f2fs/data.c
index a546ac8685ea64d5f787d9b8c84cfe1e9a009e48,6a8db4abdf5f32b9513c5bc823ea2ed37418942d..0ca530afc684ee622fbff18781e432420c1bc12a
@@@ -14,6 -14,7 +14,7 @@@
  #include <linux/pagevec.h>
  #include <linux/blkdev.h>
  #include <linux/bio.h>
+ #include <linux/swap.h>
  #include <linux/prefetch.h>
  #include <linux/uio.h>
  #include <linux/cleancache.h>
@@@ -54,7 -55,7 +55,7 @@@ static bool __is_cp_guaranteed(struct p
  
  static enum count_type __read_io_type(struct page *page)
  {
-       struct address_space *mapping = page->mapping;
+       struct address_space *mapping = page_file_mapping(page);
  
        if (mapping) {
                struct inode *inode = mapping->host;
@@@ -185,7 -186,7 +186,7 @@@ static void f2fs_write_end_io(struct bi
                        continue;
                }
  
 -              fscrypt_pullback_bio_page(&page, true);
 +              fscrypt_finalize_bounce_page(&page);
  
                if (unlikely(bio->bi_status)) {
                        mapping_set_error(page->mapping, -EIO);
@@@ -347,24 -348,25 +348,24 @@@ static void __submit_merged_bio(struct 
        io->bio = NULL;
  }
  
- static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
+ static bool __has_merged_page(struct bio *bio, struct inode *inode,
                                                struct page *page, nid_t ino)
  {
        struct bio_vec *bvec;
        struct page *target;
        struct bvec_iter_all iter_all;
  
-       if (!io->bio)
+       if (!bio)
                return false;
  
        if (!inode && !page && !ino)
                return true;
  
-       bio_for_each_segment_all(bvec, io->bio, iter_all) {
+       bio_for_each_segment_all(bvec, bio, iter_all) {
  
 -              if (bvec->bv_page->mapping)
 -                      target = bvec->bv_page;
 -              else
 -                      target = fscrypt_control_page(bvec->bv_page);
 +              target = bvec->bv_page;
 +              if (fscrypt_is_bounce_page(target))
 +                      target = fscrypt_pagecache_page(target);
  
                if (inode && inode == target->mapping->host)
                        return true;
@@@ -410,7 -412,7 +411,7 @@@ static void __submit_merged_write_cond(
                        struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
  
                        down_read(&io->io_rwsem);
-                       ret = __has_merged_page(io, inode, page, ino);
+                       ret = __has_merged_page(io->bio, inode, page, ino);
                        up_read(&io->io_rwsem);
                }
                if (ret)
@@@ -454,7 -456,7 +455,7 @@@ int f2fs_submit_page_bio(struct f2fs_io
        if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
                        fio->is_por ? META_POR : (__is_meta_io(fio) ?
                        META_GENERIC : DATA_GENERIC_ENHANCE)))
-               return -EFAULT;
+               return -EFSCORRUPTED;
  
        trace_f2fs_submit_page_bio(page, fio);
        f2fs_trace_ios(fio, 0);
        return 0;
  }
  
+ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
+ {
+       struct bio *bio = *fio->bio;
+       struct page *page = fio->encrypted_page ?
+                       fio->encrypted_page : fio->page;
+       if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
+                       __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
+               return -EFSCORRUPTED;
+       trace_f2fs_submit_page_bio(page, fio);
+       f2fs_trace_ios(fio, 0);
+       if (bio && (*fio->last_block + 1 != fio->new_blkaddr ||
+                       !__same_bdev(fio->sbi, fio->new_blkaddr, bio))) {
+               __submit_bio(fio->sbi, bio, fio->type);
+               bio = NULL;
+       }
+ alloc_new:
+       if (!bio) {
+               bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
+                               BIO_MAX_PAGES, false, fio->type, fio->temp);
+               bio_set_op_attrs(bio, fio->op, fio->op_flags);
+       }
+       if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
+               __submit_bio(fio->sbi, bio, fio->type);
+               bio = NULL;
+               goto alloc_new;
+       }
+       if (fio->io_wbc)
+               wbc_account_io(fio->io_wbc, page, PAGE_SIZE);
+       inc_page_count(fio->sbi, WB_DATA_TYPE(page));
+       *fio->last_block = fio->new_blkaddr;
+       *fio->bio = bio;
+       return 0;
+ }
+ static void f2fs_submit_ipu_bio(struct f2fs_sb_info *sbi, struct bio **bio,
+                                                       struct page *page)
+ {
+       if (!bio)
+               return;
+       if (!__has_merged_page(*bio, NULL, page, 0))
+               return;
+       __submit_bio(sbi, *bio, DATA);
+       *bio = NULL;
+ }
  void f2fs_submit_page_write(struct f2fs_io_info *fio)
  {
        struct f2fs_sb_info *sbi = fio->sbi;
@@@ -733,7 -790,7 +789,7 @@@ struct page *f2fs_get_read_data_page(st
                dn.data_blkaddr = ei.blk + index - ei.fofs;
                if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
                                                DATA_GENERIC_ENHANCE_READ)) {
-                       err = -EFAULT;
+                       err = -EFSCORRUPTED;
                        goto put_err;
                }
                goto got_it;
                        !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
                                                dn.data_blkaddr,
                                                DATA_GENERIC_ENHANCE)) {
-               err = -EFAULT;
+               err = -EFSCORRUPTED;
                goto put_err;
        }
  got_it:
@@@ -1099,7 -1156,7 +1155,7 @@@ next_block
  
        if (__is_valid_data_blkaddr(blkaddr) &&
                !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
-               err = -EFAULT;
+               err = -EFSCORRUPTED;
                goto sync_out;
        }
  
@@@ -1529,7 -1586,7 +1585,7 @@@ static int f2fs_read_single_page(struc
        sector_t block_nr;
        int ret = 0;
  
-       block_in_file = (sector_t)page->index;
+       block_in_file = (sector_t)page_index(page);
        last_block = block_in_file + nr_pages;
        last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
                                                        blkbits;
@@@ -1562,14 -1619,15 +1618,15 @@@ got_it
                block_nr = map->m_pblk + block_in_file - map->m_lblk;
                SetPageMappedToDisk(page);
  
-               if (!PageUptodate(page) && !cleancache_get_page(page)) {
+               if (!PageUptodate(page) && (!PageSwapCache(page) &&
+                                       !cleancache_get_page(page))) {
                        SetPageUptodate(page);
                        goto confused;
                }
  
                if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
                                                DATA_GENERIC_ENHANCE_READ)) {
-                       ret = -EFAULT;
+                       ret = -EFSCORRUPTED;
                        goto out;
                }
        } else {
@@@ -1660,7 -1718,7 +1717,7 @@@ static int f2fs_mpage_readpages(struct 
                        prefetchw(&page->flags);
                        list_del(&page->lru);
                        if (add_to_page_cache_lru(page, mapping,
-                                                 page->index,
+                                                 page_index(page),
                                                  readahead_gfp_mask(mapping)))
                                goto next_page;
                }
@@@ -1684,7 -1742,7 +1741,7 @@@ next_page
  
  static int f2fs_read_data_page(struct file *file, struct page *page)
  {
-       struct inode *inode = page->mapping->host;
+       struct inode *inode = page_file_mapping(page)->host;
        int ret = -EAGAIN;
  
        trace_f2fs_readpage(page, DATA);
        if (f2fs_has_inline_data(inode))
                ret = f2fs_read_inline_data(inode, page);
        if (ret == -EAGAIN)
-               ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
+               ret = f2fs_mpage_readpages(page_file_mapping(page),
+                                               NULL, page, 1, false);
        return ret;
  }
  
@@@ -1726,9 -1785,8 +1784,9 @@@ static int encrypt_one_page(struct f2fs
        f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
  
  retry_encrypt:
 -      fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
 -                      PAGE_SIZE, 0, fio->page->index, gfp_flags);
 +      fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(fio->page,
 +                                                             PAGE_SIZE, 0,
 +                                                             gfp_flags);
        if (IS_ERR(fio->encrypted_page)) {
                /* flush pending IOs and wait for a while in the ENOMEM case */
                if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
@@@ -1851,7 -1909,7 +1909,7 @@@ int f2fs_do_write_data_page(struct f2fs
  
                if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
                                                DATA_GENERIC_ENHANCE))
-                       return -EFAULT;
+                       return -EFSCORRUPTED;
  
                ipu_force = true;
                fio->need_lock = LOCK_DONE;
@@@ -1878,7 -1936,7 +1936,7 @@@ got_it
        if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
                !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
                                                DATA_GENERIC_ENHANCE)) {
-               err = -EFAULT;
+               err = -EFSCORRUPTED;
                goto out_writepage;
        }
        /*
                err = f2fs_inplace_write_data(fio);
                if (err) {
                        if (f2fs_encrypted_file(inode))
 -                              fscrypt_pullback_bio_page(&fio->encrypted_page,
 -                                                                      true);
 +                              fscrypt_finalize_bounce_page(&fio->encrypted_page);
                        if (PageWriteback(page))
                                end_page_writeback(page);
                } else {
@@@ -1946,6 -2005,8 +2004,8 @@@ out
  }
  
  static int __write_data_page(struct page *page, bool *submitted,
+                               struct bio **bio,
+                               sector_t *last_block,
                                struct writeback_control *wbc,
                                enum iostat_type io_type)
  {
                .need_lock = LOCK_RETRY,
                .io_type = io_type,
                .io_wbc = wbc,
+               .bio = bio,
+               .last_block = last_block,
        };
  
        trace_f2fs_writepage(page, DATA);
  
        unlock_page(page);
        if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
-                                       !F2FS_I(inode)->cp_task)
+                                       !F2FS_I(inode)->cp_task) {
+               f2fs_submit_ipu_bio(sbi, bio, page);
                f2fs_balance_fs(sbi, need_balance_fs);
+       }
  
        if (unlikely(f2fs_cp_error(sbi))) {
+               f2fs_submit_ipu_bio(sbi, bio, page);
                f2fs_submit_merged_write(sbi, DATA);
                submitted = NULL;
        }
@@@ -2099,7 -2165,7 +2164,7 @@@ redirty_out
  static int f2fs_write_data_page(struct page *page,
                                        struct writeback_control *wbc)
  {
-       return __write_data_page(page, NULL, wbc, FS_DATA_IO);
+       return __write_data_page(page, NULL, NULL, NULL, wbc, FS_DATA_IO);
  }
  
  /*
@@@ -2115,6 -2181,8 +2180,8 @@@ static int f2fs_write_cache_pages(struc
        int done = 0;
        struct pagevec pvec;
        struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
+       struct bio *bio = NULL;
+       sector_t last_block;
        int nr_pages;
        pgoff_t uninitialized_var(writeback_index);
        pgoff_t index;
@@@ -2191,17 -2259,20 +2258,20 @@@ continue_unlock
                        }
  
                        if (PageWriteback(page)) {
-                               if (wbc->sync_mode != WB_SYNC_NONE)
+                               if (wbc->sync_mode != WB_SYNC_NONE) {
                                        f2fs_wait_on_page_writeback(page,
                                                        DATA, true, true);
-                               else
+                                       f2fs_submit_ipu_bio(sbi, &bio, page);
+                               } else {
                                        goto continue_unlock;
+                               }
                        }
  
                        if (!clear_page_dirty_for_io(page))
                                goto continue_unlock;
  
-                       ret = __write_data_page(page, &submitted, wbc, io_type);
+                       ret = __write_data_page(page, &submitted, &bio,
+                                       &last_block, wbc, io_type);
                        if (unlikely(ret)) {
                                /*
                                 * keep nr_to_write, since vfs uses this to
        if (nwritten)
                f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
                                                                NULL, 0, DATA);
+       /* submit cached bio of IPU write */
+       if (bio)
+               __submit_bio(sbi, bio, DATA);
  
        return ret;
  }
@@@ -2261,6 -2335,9 +2334,9 @@@ static inline bool __should_serialize_i
                return false;
        if (IS_NOQUOTA(inode))
                return false;
+       /* to avoid deadlock in path of data flush */
+       if (F2FS_I(inode)->cp_task)
+               return false;
        if (wbc->sync_mode != WB_SYNC_ALL)
                return true;
        if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
@@@ -2532,7 -2609,7 +2608,7 @@@ repeat
        } else {
                if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
                                DATA_GENERIC_ENHANCE_READ)) {
-                       err = -EFAULT;
+                       err = -EFSCORRUPTED;
                        goto fail;
                }
                err = f2fs_submit_page_read(inode, page, blkaddr);
@@@ -2777,13 -2854,14 +2853,14 @@@ int f2fs_release_page(struct page *page
  
  static int f2fs_set_data_page_dirty(struct page *page)
  {
-       struct address_space *mapping = page->mapping;
-       struct inode *inode = mapping->host;
+       struct inode *inode = page_file_mapping(page)->host;
  
        trace_f2fs_set_page_dirty(page, DATA);
  
        if (!PageUptodate(page))
                SetPageUptodate(page);
+       if (PageSwapCache(page))
+               return __set_page_dirty_nobuffers(page);
  
        if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
                if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
@@@ -2875,6 -2953,126 +2952,126 @@@ int f2fs_migrate_page(struct address_sp
  }
  #endif
  
+ #ifdef CONFIG_SWAP
+ /* Copied from generic_swapfile_activate() to check any holes */
+ static int check_swap_activate(struct file *swap_file, unsigned int max)
+ {
+       struct address_space *mapping = swap_file->f_mapping;
+       struct inode *inode = mapping->host;
+       unsigned blocks_per_page;
+       unsigned long page_no;
+       unsigned blkbits;
+       sector_t probe_block;
+       sector_t last_block;
+       sector_t lowest_block = -1;
+       sector_t highest_block = 0;
+       blkbits = inode->i_blkbits;
+       blocks_per_page = PAGE_SIZE >> blkbits;
+       /*
+        * Map all the blocks into the extent list.  This code doesn't try
+        * to be very smart.
+        */
+       probe_block = 0;
+       page_no = 0;
+       last_block = i_size_read(inode) >> blkbits;
+       while ((probe_block + blocks_per_page) <= last_block && page_no < max) {
+               unsigned block_in_page;
+               sector_t first_block;
+               cond_resched();
+               first_block = bmap(inode, probe_block);
+               if (first_block == 0)
+                       goto bad_bmap;
+               /*
+                * It must be PAGE_SIZE aligned on-disk
+                */
+               if (first_block & (blocks_per_page - 1)) {
+                       probe_block++;
+                       goto reprobe;
+               }
+               for (block_in_page = 1; block_in_page < blocks_per_page;
+                                       block_in_page++) {
+                       sector_t block;
+                       block = bmap(inode, probe_block + block_in_page);
+                       if (block == 0)
+                               goto bad_bmap;
+                       if (block != first_block + block_in_page) {
+                               /* Discontiguity */
+                               probe_block++;
+                               goto reprobe;
+                       }
+               }
+               first_block >>= (PAGE_SHIFT - blkbits);
+               if (page_no) {  /* exclude the header page */
+                       if (first_block < lowest_block)
+                               lowest_block = first_block;
+                       if (first_block > highest_block)
+                               highest_block = first_block;
+               }
+               page_no++;
+               probe_block += blocks_per_page;
+ reprobe:
+               continue;
+       }
+       return 0;
+ bad_bmap:
+       pr_err("swapon: swapfile has holes\n");
+       return -EINVAL;
+ }
+ static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
+                               sector_t *span)
+ {
+       struct inode *inode = file_inode(file);
+       int ret;
+       if (!S_ISREG(inode->i_mode))
+               return -EINVAL;
+       if (f2fs_readonly(F2FS_I_SB(inode)->sb))
+               return -EROFS;
+       ret = f2fs_convert_inline_inode(inode);
+       if (ret)
+               return ret;
+       ret = check_swap_activate(file, sis->max);
+       if (ret)
+               return ret;
+       set_inode_flag(inode, FI_PIN_FILE);
+       f2fs_precache_extents(inode);
+       f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
+       return 0;
+ }
+ static void f2fs_swap_deactivate(struct file *file)
+ {
+       struct inode *inode = file_inode(file);
+       clear_inode_flag(inode, FI_PIN_FILE);
+ }
+ #else
+ static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
+                               sector_t *span)
+ {
+       return -EOPNOTSUPP;
+ }
+ static void f2fs_swap_deactivate(struct file *file)
+ {
+ }
+ #endif
  const struct address_space_operations f2fs_dblock_aops = {
        .readpage       = f2fs_read_data_page,
        .readpages      = f2fs_read_data_pages,
        .releasepage    = f2fs_release_page,
        .direct_IO      = f2fs_direct_IO,
        .bmap           = f2fs_bmap,
+       .swap_activate  = f2fs_swap_activate,
+       .swap_deactivate = f2fs_swap_deactivate,
  #ifdef CONFIG_MIGRATION
        .migratepage    = f2fs_migrate_page,
  #endif
index e3dc031af7f513027ac54227225c22570447180c,af79e0c72926667fca370fc5d597f0b8f9042f57..1796ff99c3e9c76d1b17d98cb2c28977244fc69c
@@@ -76,7 -76,16 +76,7 @@@ TRACE_DEFINE_ENUM(CP_TRIMMED)
  #define show_bio_type(op,op_flags)    show_bio_op(op),                \
                                                show_bio_op_flags(op_flags)
  
 -#define show_bio_op(op)                                                       \
 -      __print_symbolic(op,                                            \
 -              { REQ_OP_READ,                  "READ" },               \
 -              { REQ_OP_WRITE,                 "WRITE" },              \
 -              { REQ_OP_FLUSH,                 "FLUSH" },              \
 -              { REQ_OP_DISCARD,               "DISCARD" },            \
 -              { REQ_OP_SECURE_ERASE,          "SECURE_ERASE" },       \
 -              { REQ_OP_ZONE_RESET,            "ZONE_RESET" },         \
 -              { REQ_OP_WRITE_SAME,            "WRITE_SAME" },         \
 -              { REQ_OP_WRITE_ZEROES,          "WRITE_ZEROES" })
 +#define show_bio_op(op)               blk_op_str(op)
  
  #define show_bio_op_flags(flags)                                      \
        __print_flags(F2FS_BIO_FLAG_MASK(flags), "|",                   \
@@@ -1019,8 -1028,8 +1019,8 @@@ DECLARE_EVENT_CLASS(f2fs__submit_page_b
        ),
  
        TP_fast_assign(
-               __entry->dev            = page->mapping->host->i_sb->s_dev;
-               __entry->ino            = page->mapping->host->i_ino;
+               __entry->dev            = page_file_mapping(page)->host->i_sb->s_dev;
+               __entry->ino            = page_file_mapping(page)->host->i_ino;
                __entry->index          = page->index;
                __entry->old_blkaddr    = fio->old_blkaddr;
                __entry->new_blkaddr    = fio->new_blkaddr;
@@@ -1207,10 -1216,11 +1207,11 @@@ DECLARE_EVENT_CLASS(f2fs__page
        ),
  
        TP_fast_assign(
-               __entry->dev    = page->mapping->host->i_sb->s_dev;
-               __entry->ino    = page->mapping->host->i_ino;
+               __entry->dev    = page_file_mapping(page)->host->i_sb->s_dev;
+               __entry->ino    = page_file_mapping(page)->host->i_ino;
                __entry->type   = type;
-               __entry->dir    = S_ISDIR(page->mapping->host->i_mode);
+               __entry->dir    =
+                       S_ISDIR(page_file_mapping(page)->host->i_mode);
                __entry->index  = page->index;
                __entry->dirty  = PageDirty(page);
                __entry->uptodate = PageUptodate(page);