add patch add-mask-of-ext4-flags-to-swap
[ext4-patch-queue.git] / cleanup-pagecache-before-swap-i_data
blobee51e7f894f64c916c362311914356026261627d
1 ext4: cleanup pagecache before swap i_data
3 From: yangerkun <yangerkun@huawei.com>
5 While do swap, we should make sure there has no new dirty page since we
6 should swap i_data between two inode:
7 1.We should lock i_mmap_sem with write to avoid new pagecache from mmap
8 read/write;
9 2.Change filemap_flush to filemap_write_and_wait and move them to the
10 space protected by inode lock to avoid new pagecache from buffer read/write.
12 Signed-off-by: yangerkun <yangerkun@huawei.com>
13 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
14 Cc: stable@kernel.org
15 ---
16  fs/ext4/ioctl.c | 16 ++++++++++++----
17  1 file changed, 12 insertions(+), 4 deletions(-)
19 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
20 index 597e8b6..ea05e8d 100644
21 --- a/fs/ext4/ioctl.c
22 +++ b/fs/ext4/ioctl.c
23 @@ -121,9 +121,6 @@ static long swap_inode_boot_loader(struct super_block *sb,
24                 return PTR_ERR(inode_bl);
25         ei_bl = EXT4_I(inode_bl);
27 -       filemap_flush(inode->i_mapping);
28 -       filemap_flush(inode_bl->i_mapping);
30         /* Protect orig inodes against a truncate and make sure,
31          * that only 1 swap_inode_boot_loader is running. */
32         lock_two_nondirectories(inode, inode_bl);
33 @@ -141,6 +138,15 @@ static long swap_inode_boot_loader(struct super_block *sb,
34                 goto journal_err_out;
35         }
37 +       down_write(&EXT4_I(inode)->i_mmap_sem);
38 +       err = filemap_write_and_wait(inode->i_mapping);
39 +       if (err)
40 +               goto err_out;
42 +       err = filemap_write_and_wait(inode_bl->i_mapping);
43 +       if (err)
44 +               goto err_out;
46         /* Wait for all existing dio workers */
47         inode_dio_wait(inode);
48         inode_dio_wait(inode_bl);
49 @@ -151,7 +157,7 @@ static long swap_inode_boot_loader(struct super_block *sb,
50         handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
51         if (IS_ERR(handle)) {
52                 err = -EINVAL;
53 -               goto journal_err_out;
54 +               goto err_out;
55         }
57         /* Protect extent tree against block allocations via delalloc */
58 @@ -208,6 +214,8 @@ static long swap_inode_boot_loader(struct super_block *sb,
59         ext4_journal_stop(handle);
60         ext4_double_up_write_data_sem(inode, inode_bl);
62 +err_out:
63 +       up_write(&EXT4_I(inode)->i_mmap_sem);
64  journal_err_out:
65         unlock_two_nondirectories(inode, inode_bl);
66         iput(inode_bl);
67 -- 
68 2.9.5