btrfs: set EXTENT_NORESERVE bits side btrfs_dirty_pages()
authorGoldwyn Rodrigues <rgoldwyn@suse.com>
Wed, 14 Oct 2020 14:55:45 +0000 (09:55 -0500)
committerDavid Sterba <dsterba@suse.com>
Tue, 8 Dec 2020 14:53:38 +0000 (15:53 +0100)
Set the extent bits EXTENT_NORESERVE inside btrfs_dirty_pages() as
opposed to calling set_extent_bits again later.

Fold check for written length within the function.

Note: EXTENT_NORESERVE is set before unlocking extents.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.h
fs/btrfs/file.c
fs/btrfs/free-space-cache.c

index 0b29bdb251050c60c555c4e1de6c22fd6061838e..0738ec94d8064984ef6c4d2244cd2cb54515287d 100644 (file)
@@ -3112,7 +3112,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
 int btrfs_release_file(struct inode *inode, struct file *file);
 int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
                      size_t num_pages, loff_t pos, size_t write_bytes,
-                     struct extent_state **cached);
+                     struct extent_state **cached, bool noreserve);
 int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
 int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
                           size_t *write_bytes);
index c7ed30edf1cd4009c087bc25a176ee10218625ab..5e2b0592abeaf9797cb5e248e03d50b50fe85f9e 100644 (file)
@@ -462,7 +462,7 @@ static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  */
 int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
                      size_t num_pages, loff_t pos, size_t write_bytes,
-                     struct extent_state **cached)
+                     struct extent_state **cached, bool noreserve)
 {
        struct btrfs_fs_info *fs_info = inode->root->fs_info;
        int err = 0;
@@ -474,6 +474,12 @@ int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
        loff_t isize = i_size_read(&inode->vfs_inode);
        unsigned int extra_bits = 0;
 
+       if (write_bytes == 0)
+               return 0;
+
+       if (noreserve)
+               extra_bits |= EXTENT_NORESERVE;
+
        start_pos = round_down(pos, fs_info->sectorsize);
        num_bytes = round_up(write_bytes + pos - start_pos,
                             fs_info->sectorsize);
@@ -1720,10 +1726,9 @@ again:
                release_bytes = round_up(copied + sector_offset,
                                        fs_info->sectorsize);
 
-               if (copied > 0)
-                       ret = btrfs_dirty_pages(BTRFS_I(inode), pages,
-                                               dirty_pages, pos, copied,
-                                               &cached_state);
+               ret = btrfs_dirty_pages(BTRFS_I(inode), pages,
+                                       dirty_pages, pos, copied,
+                                       &cached_state, only_release_metadata);
 
                /*
                 * If we have not locked the extent range, because the range's
@@ -1748,17 +1753,6 @@ again:
                if (only_release_metadata)
                        btrfs_check_nocow_unlock(BTRFS_I(inode));
 
-               if (only_release_metadata && copied > 0) {
-                       lockstart = round_down(pos,
-                                              fs_info->sectorsize);
-                       lockend = round_up(pos + copied,
-                                          fs_info->sectorsize) - 1;
-
-                       set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
-                                      lockend, EXTENT_NORESERVE, NULL,
-                                      NULL, GFP_NOFS);
-               }
-
                btrfs_drop_pages(pages, num_pages);
 
                cond_resched();
index af0013d3df63fdf052ee3f6f8872dea914733996..5ea36a06e5147b65f0f77ca81e22aec90e109352 100644 (file)
@@ -1332,7 +1332,7 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
        /* Everything is written out, now we dirty the pages in the file. */
        ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages,
                                io_ctl->num_pages, 0, i_size_read(inode),
-                               &cached_state);
+                               &cached_state, false);
        if (ret)
                goto out_nospc;