f2fs: remove the own bi_private allocation
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / f2fs / data.c
index 941f9b9ca3a5b41fa9208f1d929b20212e08dfcb..c9a76f8c102827e596ebb51a1ca715455eb94ec4 100644 (file)
@@ -64,25 +64,40 @@ int reserve_new_block(struct dnode_of_data *dn)
        return 0;
 }
 
+int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
+{
+       bool need_put = dn->inode_page ? false : true;
+       int err;
+
+       err = get_dnode_of_data(dn, index, ALLOC_NODE);
+       if (err)
+               return err;
+       if (dn->data_blkaddr == NULL_ADDR)
+               err = reserve_new_block(dn);
+
+       if (need_put)
+               f2fs_put_dnode(dn);
+       return err;
+}
+
 static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
                                        struct buffer_head *bh_result)
 {
        struct f2fs_inode_info *fi = F2FS_I(inode);
-#ifdef CONFIG_F2FS_STAT_FS
-       struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
-#endif
        pgoff_t start_fofs, end_fofs;
        block_t start_blkaddr;
 
+       if (is_inode_flag_set(fi, FI_NO_EXTENT))
+               return 0;
+
        read_lock(&fi->ext.ext_lock);
        if (fi->ext.len == 0) {
                read_unlock(&fi->ext.ext_lock);
                return 0;
        }
 
-#ifdef CONFIG_F2FS_STAT_FS
-       sbi->total_hit_ext++;
-#endif
+       stat_inc_total_hit(inode->i_sb);
+
        start_fofs = fi->ext.fofs;
        end_fofs = fi->ext.fofs + fi->ext.len - 1;
        start_blkaddr = fi->ext.blk_addr;
@@ -100,9 +115,7 @@ static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
                else
                        bh_result->b_size = UINT_MAX;
 
-#ifdef CONFIG_F2FS_STAT_FS
-               sbi->read_hit_ext++;
-#endif
+               stat_inc_read_hit(inode->i_sb);
                read_unlock(&fi->ext.ext_lock);
                return 1;
        }
@@ -115,14 +128,18 @@ void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
        struct f2fs_inode_info *fi = F2FS_I(dn->inode);
        pgoff_t fofs, start_fofs, end_fofs;
        block_t start_blkaddr, end_blkaddr;
+       int need_update = true;
 
-       BUG_ON(blk_addr == NEW_ADDR);
+       f2fs_bug_on(blk_addr == NEW_ADDR);
        fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
                                                        dn->ofs_in_node;
 
        /* Update the page address in the parent node */
        __set_data_blkaddr(dn, blk_addr);
 
+       if (is_inode_flag_set(fi, FI_NO_EXTENT))
+               return;
+
        write_lock(&fi->ext.ext_lock);
 
        start_fofs = fi->ext.fofs;
@@ -169,14 +186,21 @@ void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
                                        fofs - start_fofs + 1;
                        fi->ext.len -= fofs - start_fofs + 1;
                }
-               goto end_update;
+       } else {
+               need_update = false;
        }
-       write_unlock(&fi->ext.ext_lock);
-       return;
 
+       /* Finally, if the extent is very fragmented, let's drop the cache. */
+       if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
+               fi->ext.len = 0;
+               set_inode_flag(fi, FI_NO_EXTENT);
+               need_update = true;
+       }
 end_update:
        write_unlock(&fi->ext.ext_lock);
-       sync_inode_page(dn);
+       if (need_update)
+               sync_inode_page(dn);
+       return;
 }
 
 struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
@@ -216,6 +240,9 @@ struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
 
        err = f2fs_readpage(sbi, page, dn.data_blkaddr,
                                        sync ? READ_SYNC : READA);
+       if (err)
+               return ERR_PTR(err);
+
        if (sync) {
                wait_on_page_locked(page);
                if (!PageUptodate(page)) {
@@ -306,19 +333,10 @@ struct page *get_new_data_page(struct inode *inode,
        int err;
 
        set_new_dnode(&dn, inode, npage, npage, 0);
-       err = get_dnode_of_data(&dn, index, ALLOC_NODE);
+       err = f2fs_reserve_block(&dn, index);
        if (err)
                return ERR_PTR(err);
 
-       if (dn.data_blkaddr == NULL_ADDR) {
-               if (reserve_new_block(&dn)) {
-                       if (!npage)
-                               f2fs_put_dnode(&dn);
-                       return ERR_PTR(-ENOSPC);
-               }
-       }
-       if (!npage)
-               f2fs_put_dnode(&dn);
 repeat:
        page = grab_cache_page(mapping, index);
        if (!page)
@@ -389,8 +407,6 @@ int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
 
        trace_f2fs_readpage(page, blk_addr, type);
 
-       down_read(&sbi->bio_sem);
-
        /* Allocate a new bio */
        bio = f2fs_bio_alloc(bdev, 1);
 
@@ -400,16 +416,67 @@ int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
 
        if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
                bio_put(bio);
-               up_read(&sbi->bio_sem);
                f2fs_put_page(page, 1);
                return -EFAULT;
        }
 
        submit_bio(type, bio);
-       up_read(&sbi->bio_sem);
        return 0;
 }
 
+void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, int rw)
+{
+       struct f2fs_bio_info *io = &sbi->read_io;
+
+       if (!io->bio)
+               return;
+
+       trace_f2fs_submit_read_bio(sbi->sb, rw, META, io->bio);
+
+       mutex_lock(&io->io_mutex);
+       if (io->bio) {
+               submit_bio(rw, io->bio);
+               io->bio = NULL;
+       }
+       mutex_unlock(&io->io_mutex);
+}
+
+void submit_read_page(struct f2fs_sb_info *sbi, struct page *page,
+                                       block_t blk_addr, int rw)
+{
+       struct block_device *bdev = sbi->sb->s_bdev;
+       struct f2fs_bio_info *io = &sbi->read_io;
+       int bio_blocks;
+
+       verify_block_addr(sbi, blk_addr);
+
+       mutex_lock(&io->io_mutex);
+
+       if (io->bio && io->last_block_in_bio != blk_addr - 1) {
+               submit_bio(rw, io->bio);
+               io->bio = NULL;
+       }
+alloc_new:
+       if (io->bio == NULL) {
+               bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
+               io->bio = f2fs_bio_alloc(bdev, bio_blocks);
+               io->bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
+               io->bio->bi_end_io = read_end_io;
+       }
+
+       if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
+                                                       PAGE_CACHE_SIZE) {
+               submit_bio(rw, io->bio);
+               io->bio = NULL;
+               goto alloc_new;
+       }
+
+       io->last_block_in_bio = blk_addr;
+
+       mutex_unlock(&io->io_mutex);
+       trace_f2fs_submit_read_page(page, rw, META, blk_addr);
+}
+
 /*
  * This function should be used by the data read flow only where it
  * does not check the "create" flag that indicates block allocation.
@@ -442,7 +509,7 @@ static int get_data_block_ro(struct inode *inode, sector_t iblock,
        }
 
        /* It does not support data allocation */
-       BUG_ON(create);
+       f2fs_bug_on(create);
 
        if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
                int i;
@@ -461,7 +528,7 @@ static int get_data_block_ro(struct inode *inode, sector_t iblock,
                                != (dn.data_blkaddr + i)) || maxblocks == i)
                                break;
                map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
-               bh_result->b_size = (i << blkbits);
+               bh_result->b_size = (((size_t)i) << blkbits);
        }
        f2fs_put_dnode(&dn);
        trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
@@ -560,9 +627,9 @@ write:
                inode_dec_dirty_dents(inode);
                err = do_write_data_page(page);
        } else {
-               int ilock = mutex_lock_op(sbi);
+               f2fs_lock_op(sbi);
                err = do_write_data_page(page);
-               mutex_unlock_op(sbi, ilock);
+               f2fs_unlock_op(sbi);
                need_balance_fs = true;
        }
        if (err == -ENOENT)
@@ -641,7 +708,6 @@ static int f2fs_write_begin(struct file *file, struct address_space *mapping,
        pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
        struct dnode_of_data dn;
        int err = 0;
-       int ilock;
 
        f2fs_balance_fs(sbi);
 repeat:
@@ -650,21 +716,15 @@ repeat:
                return -ENOMEM;
        *pagep = page;
 
-       ilock = mutex_lock_op(sbi);
-
+       f2fs_lock_op(sbi);
        set_new_dnode(&dn, inode, NULL, NULL, 0);
-       err = get_dnode_of_data(&dn, index, ALLOC_NODE);
-       if (err)
-               goto err;
-
-       if (dn.data_blkaddr == NULL_ADDR)
-               err = reserve_new_block(&dn);
-
-       f2fs_put_dnode(&dn);
-       if (err)
-               goto err;
+       err = f2fs_reserve_block(&dn, index);
+       f2fs_unlock_op(sbi);
 
-       mutex_unlock_op(sbi, ilock);
+       if (err) {
+               f2fs_put_page(page, 1);
+               return err;
+       }
 
        if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
                return 0;
@@ -698,11 +758,6 @@ out:
        SetPageUptodate(page);
        clear_cold_data(page);
        return 0;
-
-err:
-       mutex_unlock_op(sbi, ilock);
-       f2fs_put_page(page, 1);
-       return err;
 }
 
 static int f2fs_write_end(struct file *file,
@@ -721,8 +776,7 @@ static int f2fs_write_end(struct file *file,
                update_inode_page(inode);
        }
 
-       unlock_page(page);
-       page_cache_release(page);
+       f2fs_put_page(page, 1);
        return copied;
 }
 
@@ -763,6 +817,8 @@ static int f2fs_set_data_page_dirty(struct page *page)
        struct address_space *mapping = page->mapping;
        struct inode *inode = mapping->host;
 
+       trace_f2fs_set_page_dirty(page, DATA);
+
        SetPageUptodate(page);
        if (!PageDirty(page)) {
                __set_page_dirty_nobuffers(page);