Add ext4_file_write() rework series
[ext4-patch-queue.git] / drop-aio_mutex-after-grabbing-i_mutex
blobad029f9fd06d8fac31b5424a3b84118b1f1f018f
1 ext4: drop aio_mutex after grabbing i_mutex in ext4_file_write()
3 Once we have grabbed i_mutex, this will prevent any other writes from
4 proceeding, so we don't need to worry about any writes to unwritten
5 regions from taking place.  So drop the aio_mutex so that if there are
6 any other parallel writes happen to some other inode that happens to
7 hash to the same hash table entry, we won't end up blocking that work.
9 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
10 ---
11  fs/ext4/file.c | 20 +++++++++-----------
12  1 file changed, 9 insertions(+), 11 deletions(-)
14 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
15 index 64c3de7..5005391 100644
16 --- a/fs/ext4/file.c
17 +++ b/fs/ext4/file.c
18 @@ -98,7 +98,6 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
19         struct file *file = iocb->ki_filp;
20         struct inode *inode = file_inode(iocb->ki_filp);
21         struct blk_plug plug;
22 -       int unaligned_aio = 0;
23         int overwrite = 0;
24         size_t length = iov_length(iov, nr_segs);
25         ssize_t ret;
26 @@ -124,24 +123,26 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
27         }
29         if (unlikely(iocb->ki_filp->f_flags & O_DIRECT)) {
30 -               if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
31 -                   !is_sync_kiocb(iocb))
32 -                       unaligned_aio = ext4_unaligned_aio(inode, iov,
33 -                                                          nr_segs, pos);
34 +               struct mutex *aio_mutex = NULL;
36                 /* Unaligned direct AIO must be serialized; see comment above */
37 -               if (unaligned_aio) {
38 -                       mutex_lock(ext4_aio_mutex(inode));
39 +               if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
40 +                   !is_sync_kiocb(iocb) &&
41 +                   ext4_unaligned_aio(inode, iov, nr_segs, pos)) {
42 +                       aio_mutex = ext4_aio_mutex(inode);
43 +                       mutex_lock(aio_mutex);
44                         ext4_unwritten_wait(inode);
45                 }
47                 mutex_lock(&inode->i_mutex);
48 +               if (aio_mutex)
49 +                       mutex_unlock(aio_mutex);
50                 blk_start_plug(&plug);
52                 iocb->private = &overwrite;
54                 /* check whether we do a DIO overwrite or not */
55 -               if (ext4_should_dioread_nolock(inode) && !unaligned_aio &&
56 +               if (ext4_should_dioread_nolock(inode) && !aio_mutex &&
57                     !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
58                         struct ext4_map_blocks map;
59                         unsigned int blkbits = inode->i_blkbits;
60 @@ -181,9 +182,6 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
61                                 ret = err;
62                 }
63                 blk_finish_plug(&plug);
65 -               if (unaligned_aio)
66 -                       mutex_unlock(ext4_aio_mutex(inode));
67         } else {
68                 mutex_lock(&inode->i_mutex);
69                 ret = __generic_file_aio_write(iocb, iov, nr_segs,