Merge tag 'f2fs-for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk...
[linux-block.git] / fs / f2fs / data.c
index a546ac8685ea64d5f787d9b8c84cfe1e9a009e48..0ca530afc684ee622fbff18781e432420c1bc12a 100644 (file)
@@ -14,6 +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 @@ static bool __is_cp_guaranteed(struct page *page)
 
 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;
@@ -347,20 +348,20 @@ static void __submit_merged_bio(struct f2fs_bio_info *io)
        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) {
 
                target = bvec->bv_page;
                if (fscrypt_is_bounce_page(target))
@@ -410,7 +411,7 @@ static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
                        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 +455,7 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
        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);
@@ -480,6 +481,61 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
        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 +789,7 @@ struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
                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;
@@ -753,7 +809,7 @@ struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
                        !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 +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 +1585,7 @@ static int f2fs_read_single_page(struct inode *inode, struct page *page,
        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 +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 +1717,7 @@ static int f2fs_mpage_readpages(struct address_space *mapping,
                        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 +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);
@@ -1693,7 +1750,8 @@ static int f2fs_read_data_page(struct file *file, struct page *page)
        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;
 }
 
@@ -1851,7 +1909,7 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
 
                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 @@ 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;
        }
        /*
@@ -1946,6 +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)
 {
@@ -1971,6 +2031,8 @@ static int __write_data_page(struct page *page, bool *submitted,
                .need_lock = LOCK_RETRY,
                .io_type = io_type,
                .io_wbc = wbc,
+               .bio = bio,
+               .last_block = last_block,
        };
 
        trace_f2fs_writepage(page, DATA);
@@ -2069,10 +2131,13 @@ out:
 
        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 +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 +2180,8 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
        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 +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
@@ -2250,6 +2320,9 @@ continue_unlock:
        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 +2334,9 @@ static inline bool __should_serialize_io(struct inode *inode,
                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 +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 +2853,14 @@ int f2fs_release_page(struct page *page, gfp_t wait)
 
 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 +2952,126 @@ int f2fs_migrate_page(struct address_space *mapping,
 }
 #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,
@@ -2887,6 +3084,8 @@ const struct address_space_operations f2fs_dblock_aops = {
        .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