cd39414
From 69e4026a2d104ffcf1b935bc889f8abcbfbb29ec Mon Sep 17 00:00:00 2001
cd39414
From: Dmitry Monakhov <dmonakhov@openvz.org>
cd39414
Date: Sat, 29 Sep 2012 00:58:26 -0400
cd39414
Subject: [PATCH 08/13] ext4: serialize truncate with owerwrite DIO workers
cd39414
cd39414
Jan Kara have spotted interesting issue:
cd39414
There are  potential data corruption issue with  direct IO overwrites
cd39414
racing with truncate:
cd39414
 Like:
cd39414
  dio write                      truncate_task
cd39414
  ->ext4_ext_direct_IO
cd39414
   ->overwrite == 1
cd39414
    ->down_read(&EXT4_I(inode)->i_data_sem);
cd39414
    ->mutex_unlock(&inode->i_mutex);
cd39414
                               ->ext4_setattr()
cd39414
                                ->inode_dio_wait()
cd39414
                                ->truncate_setsize()
cd39414
                                ->ext4_truncate()
cd39414
                                 ->down_write(&EXT4_I(inode)->i_data_sem);
cd39414
    ->__blockdev_direct_IO
cd39414
     ->ext4_get_block
cd39414
     ->submit_io()
cd39414
    ->up_read(&EXT4_I(inode)->i_data_sem);
cd39414
                                 # truncate data blocks, allocate them to
cd39414
                                 # other inode - bad stuff happens because
cd39414
                                 # dio is still in flight.
cd39414
cd39414
In order to serialize with truncate dio worker should grab extra i_dio_count
cd39414
reference before drop i_mutex.
cd39414
cd39414
Reviewed-by: Jan Kara <jack@suse.cz>
cd39414
Signed-off-by: Dmitry Monakhov <dmonakhov@openvz.org>
cd39414
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
cd39414
(cherry picked from commit 1f555cfa29e8f787d675e8390f88ce517a37271a)
cd39414
---
cd39414
 fs/ext4/inode.c | 2 ++
cd39414
 1 file changed, 2 insertions(+)
cd39414
cd39414
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
cd39414
index 3b03dd6..484a327 100644
cd39414
--- a/fs/ext4/inode.c
cd39414
+++ b/fs/ext4/inode.c
cd39414
@@ -3008,6 +3008,7 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
cd39414
 		overwrite = *((int *)iocb->private);
cd39414
 
cd39414
 		if (overwrite) {
cd39414
+			atomic_inc(&inode->i_dio_count);
cd39414
 			down_read(&EXT4_I(inode)->i_data_sem);
cd39414
 			mutex_unlock(&inode->i_mutex);
cd39414
 		}
cd39414
@@ -3105,6 +3106,7 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
cd39414
 	retake_lock:
cd39414
 		/* take i_mutex locking again if we do a ovewrite dio */
cd39414
 		if (overwrite) {
cd39414
+			inode_dio_done(inode);
cd39414
 			up_read(&EXT4_I(inode)->i_data_sem);
cd39414
 			mutex_lock(&inode->i_mutex);
cd39414
 		}
cd39414
-- 
cd39414
1.7.12.rc0.22.gcdd159b
cd39414