add patch avoid-unneeded-lookup-when-xattr-name-is-invalid
[ext4-patch-queue.git] / move-ext4_file_dio_write-into-ext4_file_write
bloba1f49750a506ba9916727eb758214532a10b2589
1 ext4: move ext4_file_dio_write() into ext4_file_write()
3 This commit doesn't actually change anything; it just moves code
4 around in preparation for some code simplification work.
6 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
7 Reviewed-by: Jan Kara <jack@suse.cz>
8 ---
9  fs/ext4/file.c | 138 ++++++++++++++++++++++++++-------------------------------
10  1 file changed, 64 insertions(+), 74 deletions(-)
12 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
13 index 79b77a5..20f1c03 100644
14 --- a/fs/ext4/file.c
15 +++ b/fs/ext4/file.c
16 @@ -92,82 +92,15 @@ ext4_unaligned_aio(struct inode *inode, const struct iovec *iov,
17  }
19  static ssize_t
20 -ext4_file_dio_write(struct kiocb *iocb, const struct iovec *iov,
21 -                   unsigned long nr_segs, loff_t pos)
22 +ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
23 +               unsigned long nr_segs, loff_t pos)
24  {
25         struct file *file = iocb->ki_filp;
26 -       struct inode *inode = file->f_mapping->host;
27 +       struct inode *inode = file_inode(iocb->ki_filp);
28         struct blk_plug plug;
29         int unaligned_aio = 0;
30 -       ssize_t ret;
31         int overwrite = 0;
32         size_t length = iov_length(iov, nr_segs);
34 -       if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
35 -           !is_sync_kiocb(iocb))
36 -               unaligned_aio = ext4_unaligned_aio(inode, iov, nr_segs, pos);
38 -       /* Unaligned direct AIO must be serialized; see comment above */
39 -       if (unaligned_aio) {
40 -               mutex_lock(ext4_aio_mutex(inode));
41 -               ext4_unwritten_wait(inode);
42 -       }
44 -       mutex_lock(&inode->i_mutex);
45 -       blk_start_plug(&plug);
47 -       iocb->private = &overwrite;
49 -       /* check whether we do a DIO overwrite or not */
50 -       if (ext4_should_dioread_nolock(inode) && !unaligned_aio &&
51 -           !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
52 -               struct ext4_map_blocks map;
53 -               unsigned int blkbits = inode->i_blkbits;
54 -               int err, len;
56 -               map.m_lblk = pos >> blkbits;
57 -               map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
58 -                       - map.m_lblk;
59 -               len = map.m_len;
61 -               err = ext4_map_blocks(NULL, inode, &map, 0);
62 -               /*
63 -                * 'err==len' means that all of blocks has been preallocated no
64 -                * matter they are initialized or not.  For excluding
65 -                * unwritten extents, we need to check m_flags.  There are
66 -                * two conditions that indicate for initialized extents.
67 -                * 1) If we hit extent cache, EXT4_MAP_MAPPED flag is returned;
68 -                * 2) If we do a real lookup, non-flags are returned.
69 -                * So we should check these two conditions.
70 -                */
71 -               if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
72 -                       overwrite = 1;
73 -       }
75 -       ret = __generic_file_aio_write(iocb, iov, nr_segs);
76 -       mutex_unlock(&inode->i_mutex);
78 -       if (ret > 0) {
79 -               ssize_t err;
81 -               err = generic_write_sync(file, iocb->ki_pos - ret, ret);
82 -               if (err < 0)
83 -                       ret = err;
84 -       }
85 -       blk_finish_plug(&plug);
87 -       if (unaligned_aio)
88 -               mutex_unlock(ext4_aio_mutex(inode));
90 -       return ret;
93 -static ssize_t
94 -ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
95 -               unsigned long nr_segs, loff_t pos)
97 -       struct file *file = iocb->ki_filp;
98 -       struct inode *inode = file_inode(iocb->ki_filp);
99         ssize_t ret;
101         BUG_ON(iocb->ki_pos != pos);
102 @@ -179,7 +112,6 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
104         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
105                 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
106 -               size_t length = iov_length(iov, nr_segs);
108                 if ((pos > sbi->s_bitmap_maxbytes ||
109                     (pos == sbi->s_bitmap_maxbytes && length > 0)))
110 @@ -191,9 +123,67 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
111                 }
112         }
114 -       if (unlikely(iocb->ki_filp->f_flags & O_DIRECT))
115 -               ret = ext4_file_dio_write(iocb, iov, nr_segs, pos);
116 -       else {
117 +       if (unlikely(iocb->ki_filp->f_flags & O_DIRECT)) {
118 +               if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
119 +                   !is_sync_kiocb(iocb))
120 +                       unaligned_aio = ext4_unaligned_aio(inode, iov,
121 +                                                          nr_segs, pos);
123 +               /* Unaligned direct AIO must be serialized; see comment above */
124 +               if (unaligned_aio) {
125 +                       mutex_lock(ext4_aio_mutex(inode));
126 +                       ext4_unwritten_wait(inode);
127 +               }
129 +               mutex_lock(&inode->i_mutex);
130 +               blk_start_plug(&plug);
132 +               iocb->private = &overwrite;
134 +               /* check whether we do a DIO overwrite or not */
135 +               if (ext4_should_dioread_nolock(inode) && !unaligned_aio &&
136 +                   !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
137 +                       struct ext4_map_blocks map;
138 +                       unsigned int blkbits = inode->i_blkbits;
139 +                       int err, len;
141 +                       map.m_lblk = pos >> blkbits;
142 +                       map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
143 +                               - map.m_lblk;
144 +                       len = map.m_len;
146 +                       err = ext4_map_blocks(NULL, inode, &map, 0);
147 +                       /*
148 +                        * 'err==len' means that all of blocks has
149 +                        * been preallocated no matter they are
150 +                        * initialized or not.  For excluding
151 +                        * unwritten extents, we need to check
152 +                        * m_flags.  There are two conditions that
153 +                        * indicate for initialized extents.  1) If we
154 +                        * hit extent cache, EXT4_MAP_MAPPED flag is
155 +                        * returned; 2) If we do a real lookup,
156 +                        * non-flags are returned.  So we should check
157 +                        * these two conditions.
158 +                        */
159 +                       if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
160 +                               overwrite = 1;
161 +               }
163 +               ret = __generic_file_aio_write(iocb, iov, nr_segs);
164 +               mutex_unlock(&inode->i_mutex);
166 +               if (ret > 0) {
167 +                       ssize_t err;
169 +                       err = generic_write_sync(file, iocb->ki_pos - ret, ret);
170 +                       if (err < 0)
171 +                               ret = err;
172 +               }
173 +               blk_finish_plug(&plug);
175 +               if (unaligned_aio)
176 +                       mutex_unlock(ext4_aio_mutex(inode));
177 +       } else {
178                 mutex_lock(&inode->i_mutex);
179                 ret = __generic_file_aio_write(iocb, iov, nr_segs);
180                 mutex_unlock(&inode->i_mutex);