add patch add-fallocate-mode-blocking-for-debugging
[ext4-patch-queue.git] / add-locking-for-O_APPEND-writes
blobf0a3c823ea3e8d29e9a68864b69684b12acfb439
1 ext4: add locking for O_APPEND writes
3 Al Viro pointed out that we need to make sure we only allow one
4 O_APPEND write to proceed at a time so that the the s_bitmap_maxbytes
5 check can be properly checked.
7 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
8 ---
9  fs/ext4/file.c | 46 +++++++++++++++++++++++++++++-----------------
10  1 file changed, 29 insertions(+), 17 deletions(-)
12 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
13 index 6ac33da..16eef81 100644
14 --- a/fs/ext4/file.c
15 +++ b/fs/ext4/file.c
16 @@ -98,24 +98,32 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
17         struct file *file = iocb->ki_filp;
18         struct inode *inode = file_inode(iocb->ki_filp);
19         struct blk_plug plug;
20 +       struct mutex *aio_mutex = NULL;
21         int o_direct = file->f_flags & O_DIRECT;
22 -       int overwrite = 0;
23 +       int overwrite = 0, i_mutex_grabbed = 0;
24         size_t length = iov_length(iov, nr_segs);
25         ssize_t ret;
27         BUG_ON(iocb->ki_pos != pos);
29 +       if (file->f_flags & O_APPEND) {
30 +               mutex_lock(&inode->i_mutex);
31 +               i_mutex_grabbed = 1;
32 +               iocb->ki_pos = pos = i_size_read(inode);
33 +       }
35         /*
36          * If we have encountered a bitmap-format file, the size limit
37          * is smaller than s_maxbytes, which is for extent-mapped files.
38          */
40         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
41                 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
43                 if ((pos > sbi->s_bitmap_maxbytes ||
44 -                   (pos == sbi->s_bitmap_maxbytes && length > 0)))
45 -                       return -EFBIG;
46 +                    (pos == sbi->s_bitmap_maxbytes && length > 0))) {
47 +                       ret = -EFBIG;
48 +                       goto errout;
49 +               }
51                 if (pos + length > sbi->s_bitmap_maxbytes) {
52                         nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
53 @@ -123,19 +131,20 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
54                 }
55         }
57 -       if (o_direct) {
58 -               struct mutex *aio_mutex = NULL;
60 -               /* Unaligned direct AIO must be serialized; see comment above */
61 -               if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
62 -                   !is_sync_kiocb(iocb) &&
63 -                   ext4_unaligned_aio(inode, iov, nr_segs, pos)) {
64 -                       aio_mutex = ext4_aio_mutex(inode);
65 -                       mutex_lock(aio_mutex);
66 -                       ext4_unwritten_wait(inode);
67 -               }
68 +       /* Unaligned direct AIO must be serialized; see comment above */
69 +       if (o_direct && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
70 +           !is_sync_kiocb(iocb) &&
71 +           ext4_unaligned_aio(inode, iov, nr_segs, pos)) {
72 +               aio_mutex = ext4_aio_mutex(inode);
73 +               mutex_lock(aio_mutex);
74 +               ext4_unwritten_wait(inode);
75 +       }
77 +       if (!i_mutex_grabbed)
78                 mutex_lock(&inode->i_mutex);
79 +       i_mutex_grabbed = 1;
81 +       if (o_direct) {
82                 if (aio_mutex)
83                         mutex_unlock(aio_mutex);
84                 blk_start_plug(&plug);
85 @@ -170,11 +179,11 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
86                         if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
87                                 overwrite = 1;
88                 }
89 -       } else
90 -               mutex_lock(&inode->i_mutex);
91 +       }
93         ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
94         mutex_unlock(&inode->i_mutex);
95 +       i_mutex_grabbed = 0;
97         if (ret > 0) {
98                 ssize_t err;
99 @@ -186,6 +195,9 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
100         if (o_direct)
101                 blk_finish_plug(&plug);
103 +errout:
104 +       if (i_mutex_grabbed)
105 +               mutex_unlock(&inode->i_mutex);
106         return ret;