Skip to content

Commit 267e4db

Browse files
kvaneeshtytso
authored andcommitted
ext4: Fix race between migration and mmap write
Fail migrate if we allocated new blocks via mmap write. If we write to holes in the file via mmap, we end up allocating new blocks. This block allocation happens without taking inode->i_mutex. Since migrate is protected by i_mutex and migrate expects that no new blocks get allocated during migrate, fail migrate if new blocks get allocated. We can't take inode->i_mutex in the mmap write path because that would result in a locking order violation between i_mutex and mmap_sem. Also adding a separate rw_sempahore for protection is really high overhead for a rare operation such as migrate. Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> Acked-by: Jan Kara <jack@suse.cz> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 3977c96 commit 267e4db

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

‎fs/ext4/inode.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,16 @@ int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
985985
} else {
986986
retval = ext4_get_blocks_handle(handle, inode, block,
987987
max_blocks, bh, create, extend_disksize);
988+
989+
if (retval > 0 && buffer_new(bh)) {
990+
/*
991+
* We allocated new blocks which will result in
992+
* i_data's format changing. Force the migrate
993+
* to fail by clearing migrate flags
994+
*/
995+
EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
996+
~EXT4_EXT_MIGRATE;
997+
}
988998
}
989999
up_write((&EXT4_I(inode)->i_data_sem));
9901000
return retval;
@@ -2976,7 +2986,8 @@ static int ext4_do_update_inode(handle_t *handle,
29762986
if (ext4_inode_blocks_set(handle, raw_inode, ei))
29772987
goto out_brelse;
29782988
raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
2979-
raw_inode->i_flags = cpu_to_le32(ei->i_flags);
2989+
/* clear the migrate flag in the raw_inode */
2990+
raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
29802991
if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
29812992
cpu_to_le32(EXT4_OS_HURD))
29822993
raw_inode->i_file_acl_high =

‎fs/ext4/migrate.c‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
327327
}
328328

329329
static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
330-
struct inode *tmp_inode)
330+
struct inode *tmp_inode)
331331
{
332332
int retval;
333333
__le32 i_data[3];
@@ -339,7 +339,7 @@ static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
339339
* i_data field of the original inode
340340
*/
341341
retval = ext4_journal_extend(handle, 1);
342-
if (retval != 0) {
342+
if (retval) {
343343
retval = ext4_journal_restart(handle, 1);
344344
if (retval)
345345
goto err_out;
@@ -350,6 +350,18 @@ static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
350350
i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
351351

352352
down_write(&EXT4_I(inode)->i_data_sem);
353+
/*
354+
* if EXT4_EXT_MIGRATE is cleared a block allocation
355+
* happened after we started the migrate. We need to
356+
* fail the migrate
357+
*/
358+
if (!(EXT4_I(inode)->i_flags & EXT4_EXT_MIGRATE)) {
359+
retval = -EAGAIN;
360+
up_write(&EXT4_I(inode)->i_data_sem);
361+
goto err_out;
362+
} else
363+
EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
364+
~EXT4_EXT_MIGRATE;
353365
/*
354366
* We have the extent map build with the tmp inode.
355367
* Now copy the i_data across
@@ -508,6 +520,17 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
508520
* switch the inode format to prevent read.
509521
*/
510522
mutex_lock(&(inode->i_mutex));
523+
/*
524+
* Even though we take i_mutex we can still cause block allocation
525+
* via mmap write to holes. If we have allocated new blocks we fail
526+
* migrate. New block allocation will clear EXT4_EXT_MIGRATE flag.
527+
* The flag is updated with i_data_sem held to prevent racing with
528+
* block allocation.
529+
*/
530+
down_read((&EXT4_I(inode)->i_data_sem));
531+
EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags | EXT4_EXT_MIGRATE;
532+
up_read((&EXT4_I(inode)->i_data_sem));
533+
511534
handle = ext4_journal_start(inode, 1);
512535

513536
ei = EXT4_I(inode);
@@ -559,9 +582,15 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
559582
* tmp_inode
560583
*/
561584
free_ext_block(handle, tmp_inode);
562-
else
563-
retval = ext4_ext_swap_inode_data(handle, inode,
564-
tmp_inode);
585+
else {
586+
retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode);
587+
if (retval)
588+
/*
589+
* if we fail to swap inode data free the extent
590+
* details of the tmp inode
591+
*/
592+
free_ext_block(handle, tmp_inode);
593+
}
565594

566595
/* We mark the tmp_inode dirty via ext4_ext_tree_init. */
567596
if (ext4_journal_extend(handle, 1) != 0)

‎include/linux/ext4_fs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ struct ext4_group_desc
231231
#define EXT4_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
232232
#define EXT4_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
233233
#define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
234+
#define EXT4_EXT_MIGRATE 0x00100000 /* Inode is migrating */
234235
#define EXT4_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
235236

236237
#define EXT4_FL_USER_VISIBLE 0x000BDFFF /* User visible flags */

0 commit comments

Comments
 (0)