diff options
Diffstat (limited to 'fs/ext4/file.c')
-rw-r--r-- | fs/ext4/file.c | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/fs/ext4/file.c b/fs/ext4/file.c index a10dc7742aec..1c81509f5bd9 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c | |||
@@ -93,9 +93,13 @@ static ssize_t | |||
93 | ext4_file_dio_write(struct kiocb *iocb, const struct iovec *iov, | 93 | ext4_file_dio_write(struct kiocb *iocb, const struct iovec *iov, |
94 | unsigned long nr_segs, loff_t pos) | 94 | unsigned long nr_segs, loff_t pos) |
95 | { | 95 | { |
96 | struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; | 96 | struct file *file = iocb->ki_filp; |
97 | struct inode *inode = file->f_mapping->host; | ||
98 | struct blk_plug plug; | ||
97 | int unaligned_aio = 0; | 99 | int unaligned_aio = 0; |
98 | ssize_t ret; | 100 | ssize_t ret; |
101 | int overwrite = 0; | ||
102 | size_t length = iov_length(iov, nr_segs); | ||
99 | 103 | ||
100 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) && | 104 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) && |
101 | !is_sync_kiocb(iocb)) | 105 | !is_sync_kiocb(iocb)) |
@@ -115,7 +119,50 @@ ext4_file_dio_write(struct kiocb *iocb, const struct iovec *iov, | |||
115 | ext4_aiodio_wait(inode); | 119 | ext4_aiodio_wait(inode); |
116 | } | 120 | } |
117 | 121 | ||
118 | ret = generic_file_aio_write(iocb, iov, nr_segs, pos); | 122 | BUG_ON(iocb->ki_pos != pos); |
123 | |||
124 | mutex_lock(&inode->i_mutex); | ||
125 | blk_start_plug(&plug); | ||
126 | |||
127 | iocb->private = &overwrite; | ||
128 | |||
129 | /* check whether we do a DIO overwrite or not */ | ||
130 | if (ext4_should_dioread_nolock(inode) && !unaligned_aio && | ||
131 | !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) { | ||
132 | struct ext4_map_blocks map; | ||
133 | unsigned int blkbits = inode->i_blkbits; | ||
134 | int err, len; | ||
135 | |||
136 | map.m_lblk = pos >> blkbits; | ||
137 | map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits) | ||
138 | - map.m_lblk; | ||
139 | len = map.m_len; | ||
140 | |||
141 | err = ext4_map_blocks(NULL, inode, &map, 0); | ||
142 | /* | ||
143 | * 'err==len' means that all of blocks has been preallocated no | ||
144 | * matter they are initialized or not. For excluding | ||
145 | * uninitialized extents, we need to check m_flags. There are | ||
146 | * two conditions that indicate for initialized extents. | ||
147 | * 1) If we hit extent cache, EXT4_MAP_MAPPED flag is returned; | ||
148 | * 2) If we do a real lookup, non-flags are returned. | ||
149 | * So we should check these two conditions. | ||
150 | */ | ||
151 | if (err == len && (map.m_flags & EXT4_MAP_MAPPED)) | ||
152 | overwrite = 1; | ||
153 | } | ||
154 | |||
155 | ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); | ||
156 | mutex_unlock(&inode->i_mutex); | ||
157 | |||
158 | if (ret > 0 || ret == -EIOCBQUEUED) { | ||
159 | ssize_t err; | ||
160 | |||
161 | err = generic_write_sync(file, pos, ret); | ||
162 | if (err < 0 && ret > 0) | ||
163 | ret = err; | ||
164 | } | ||
165 | blk_finish_plug(&plug); | ||
119 | 166 | ||
120 | if (unaligned_aio) | 167 | if (unaligned_aio) |
121 | mutex_unlock(ext4_aio_mutex(inode)); | 168 | mutex_unlock(ext4_aio_mutex(inode)); |