2019-01-10 03:33:22

by yangerkun

[permalink] [raw]
Subject: [PATCH 0/4] fix bugs for ioctl EXT4_IOC_SWAP_BOOT

yangerkun (4):
ext4: fix check of inode in swap_inode_boot_loader
ext4: cleanup pagecache before swap i_data
ext4: update quota information while swapping boot loader inode
ext4: add mask of ext4 flags to swap

fs/ext4/ext4.h | 4 ++++
fs/ext4/ioctl.c | 73 ++++++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 61 insertions(+), 16 deletions(-)

--
2.9.5


2019-01-10 03:33:23

by yangerkun

[permalink] [raw]
Subject: [PATCH 4/4] ext4: add mask of ext4 flags to swap

Not all flags should be swap, add a mask to do this.

Signed-off-by: yangerkun <[email protected]>
---
fs/ext4/ext4.h | 4 ++++
fs/ext4/ioctl.c | 6 +++++-
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 3f89d0a..b438a7c 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -426,6 +426,10 @@ struct flex_groups {
/* Flags that are appropriate for non-directories/regular files. */
#define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL)

+/* Flags that should be swap */
+#define EXT4_FL_SHOULD_SWAP (EXT4_COMPR_FL | EXT4_COMPRBLK_FL | EXT4_NOCOMPR_FL |\
+ EXT4_HUGE_FILE_FL | EXT4_EXTENTS_FL)
+
/* Mask out flags that are inappropriate for the given type of inode. */
static inline __u32 ext4_mask_flags(umode_t mode, __u32 flags)
{
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 60d406f..53e26a6 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -66,6 +66,7 @@ static void swap_inode_data(struct inode *inode1, struct inode *inode2)
struct inode *big_inode;
struct inode *small_inode;
qsize_t size1, size2, diff;
+ unsigned long tmp;

ei1 = EXT4_I(inode1);
ei2 = EXT4_I(inode2);
@@ -75,7 +76,10 @@ static void swap_inode_data(struct inode *inode1, struct inode *inode2)
swap(inode1->i_mtime, inode2->i_mtime);

memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
- swap(ei1->i_flags, ei2->i_flags);
+ tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP;
+ ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) |
+ (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP);
+ ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP);
swap(ei1->i_disksize, ei2->i_disksize);
ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
--
2.9.5

2019-01-10 03:33:22

by yangerkun

[permalink] [raw]
Subject: [PATCH 1/4] ext4: fix check of inode in swap_inode_boot_loader

What there need to be checked should be protected by inode lock.

Signed-off-by: yangerkun <[email protected]>
---
fs/ext4/ioctl.c | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 0edee31..579ebe5 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -116,15 +116,6 @@ static long swap_inode_boot_loader(struct super_block *sb,
struct inode *inode_bl;
struct ext4_inode_info *ei_bl;

- if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
- IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
- ext4_has_inline_data(inode))
- return -EINVAL;
-
- if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
- !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
- return -EPERM;
-
inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
if (IS_ERR(inode_bl))
return PTR_ERR(inode_bl);
@@ -137,6 +128,19 @@ static long swap_inode_boot_loader(struct super_block *sb,
* that only 1 swap_inode_boot_loader is running. */
lock_two_nondirectories(inode, inode_bl);

+ if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
+ IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
+ ext4_has_inline_data(inode)) {
+ err = -EINVAL;
+ goto journal_err_out;
+ }
+
+ if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
+ !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
+ err = -EPERM;
+ goto journal_err_out;
+ }
+
/* Wait for all existing dio workers */
inode_dio_wait(inode);
inode_dio_wait(inode_bl);
--
2.9.5

2019-01-10 03:33:22

by yangerkun

[permalink] [raw]
Subject: [PATCH 3/4] ext4: update quota information while swapping boot loader inode

The quota information should also be update.

Signed-off-by: yangerkun <[email protected]>
---
fs/ext4/ioctl.c | 29 +++++++++++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index fdfada3..60d406f 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -63,13 +63,14 @@ static void swap_inode_data(struct inode *inode1, struct inode *inode2)
loff_t isize;
struct ext4_inode_info *ei1;
struct ext4_inode_info *ei2;
+ struct inode *big_inode;
+ struct inode *small_inode;
+ qsize_t size1, size2, diff;

ei1 = EXT4_I(inode1);
ei2 = EXT4_I(inode2);

swap(inode1->i_version, inode2->i_version);
- swap(inode1->i_blocks, inode2->i_blocks);
- swap(inode1->i_bytes, inode2->i_bytes);
swap(inode1->i_atime, inode2->i_atime);
swap(inode1->i_mtime, inode2->i_mtime);

@@ -79,6 +80,21 @@ static void swap_inode_data(struct inode *inode1, struct inode *inode2)
ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);

+ size1 = (qsize_t)(inode1->i_blocks) * (1 << 9) + inode1->i_bytes;
+ size2 = (qsize_t)(inode2->i_blocks) * (1 << 9) + inode2->i_bytes;
+ if (size1 > size2) {
+ big_inode = inode1;
+ small_inode = inode2;
+ diff = size1 - size2;
+ } else {
+ big_inode = inode2;
+ small_inode = inode1;
+ diff = size2 - size1;
+ }
+
+ dquot_alloc_space_nofail(small_inode, diff);
+ dquot_free_space(big_inode, diff);
+
isize = i_size_read(inode1);
i_size_write(inode1, i_size_read(inode2));
i_size_write(inode2, isize);
@@ -180,6 +196,14 @@ static long swap_inode_boot_loader(struct super_block *sb,
memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
}

+ err = dquot_initialize(inode);
+ if (err)
+ goto err_out1;
+
+ err = dquot_initialize(inode_bl);
+ if (err)
+ goto err_out1;
+
swap_inode_data(inode, inode_bl);

inode->i_ctime = inode_bl->i_ctime = current_time(inode);
@@ -211,6 +235,7 @@ static long swap_inode_boot_loader(struct super_block *sb,
ext4_mark_inode_dirty(handle, inode_bl);
}
}
+err_out1:
ext4_journal_stop(handle);
ext4_double_up_write_data_sem(inode, inode_bl);

--
2.9.5

2019-01-10 03:33:22

by yangerkun

[permalink] [raw]
Subject: [PATCH 2/4] ext4: cleanup pagecache before swap i_data

Make sure new dirty page will not appear.

Signed-off-by: yangerkun <[email protected]>
---
fs/ext4/ioctl.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 579ebe5..fdfada3 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -121,9 +121,6 @@ static long swap_inode_boot_loader(struct super_block *sb,
return PTR_ERR(inode_bl);
ei_bl = EXT4_I(inode_bl);

- filemap_flush(inode->i_mapping);
- filemap_flush(inode_bl->i_mapping);
-
/* Protect orig inodes against a truncate and make sure,
* that only 1 swap_inode_boot_loader is running. */
lock_two_nondirectories(inode, inode_bl);
@@ -141,6 +138,15 @@ static long swap_inode_boot_loader(struct super_block *sb,
goto journal_err_out;
}

+ down_write(&EXT4_I(inode)->i_mmap_sem);
+ err = filemap_write_and_wait(inode->i_mapping);
+ if (err)
+ goto err_out;
+
+ err = filemap_write_and_wait(inode_bl->i_mapping);
+ if (err)
+ goto err_out;
+
/* Wait for all existing dio workers */
inode_dio_wait(inode);
inode_dio_wait(inode_bl);
@@ -151,7 +157,7 @@ static long swap_inode_boot_loader(struct super_block *sb,
handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
if (IS_ERR(handle)) {
err = -EINVAL;
- goto journal_err_out;
+ goto err_out;
}

/* Protect extent tree against block allocations via delalloc */
@@ -208,6 +214,8 @@ static long swap_inode_boot_loader(struct super_block *sb,
ext4_journal_stop(handle);
ext4_double_up_write_data_sem(inode, inode_bl);

+err_out:
+ up_write(&EXT4_I(inode)->i_mmap_sem);
journal_err_out:
unlock_two_nondirectories(inode, inode_bl);
iput(inode_bl);
--
2.9.5