Commit | Line | Data |
---|---|---|
db074436 DW |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2010 Red Hat, Inc. | |
a6d3d495 | 4 | * Copyright (c) 2016-2021 Christoph Hellwig. |
db074436 DW |
5 | */ |
6 | #include <linux/module.h> | |
7 | #include <linux/compiler.h> | |
8 | #include <linux/fs.h> | |
9 | #include <linux/iomap.h> | |
10 | #include <linux/backing-dev.h> | |
11 | #include <linux/uio.h> | |
12 | #include <linux/task_io_accounting_ops.h> | |
60263d58 | 13 | #include "trace.h" |
db074436 DW |
14 | |
15 | #include "../internal.h" | |
16 | ||
17 | /* | |
18 | * Private flags for iomap_dio, must not overlap with the public ones in | |
19 | * iomap.h: | |
20 | */ | |
21 | #define IOMAP_DIO_WRITE_FUA (1 << 28) | |
22 | #define IOMAP_DIO_NEED_SYNC (1 << 29) | |
23 | #define IOMAP_DIO_WRITE (1 << 30) | |
24 | #define IOMAP_DIO_DIRTY (1 << 31) | |
25 | ||
26 | struct iomap_dio { | |
27 | struct kiocb *iocb; | |
838c4f3d | 28 | const struct iomap_dio_ops *dops; |
db074436 DW |
29 | loff_t i_size; |
30 | loff_t size; | |
31 | atomic_t ref; | |
32 | unsigned flags; | |
33 | int error; | |
34 | bool wait_for_completion; | |
35 | ||
36 | union { | |
37 | /* used during submission and for synchronous completion: */ | |
38 | struct { | |
39 | struct iov_iter *iter; | |
40 | struct task_struct *waiter; | |
3e08773c | 41 | struct bio *poll_bio; |
db074436 DW |
42 | } submit; |
43 | ||
44 | /* used for aio completion: */ | |
45 | struct { | |
46 | struct work_struct work; | |
47 | } aio; | |
48 | }; | |
49 | }; | |
50 | ||
a6d3d495 CH |
51 | static void iomap_dio_submit_bio(const struct iomap_iter *iter, |
52 | struct iomap_dio *dio, struct bio *bio, loff_t pos) | |
db074436 DW |
53 | { |
54 | atomic_inc(&dio->ref); | |
55 | ||
3e08773c | 56 | if (dio->iocb->ki_flags & IOCB_HIPRI) { |
db074436 | 57 | bio_set_polled(bio, dio->iocb); |
3e08773c CH |
58 | dio->submit.poll_bio = bio; |
59 | } | |
db074436 | 60 | |
8cecd0ba | 61 | if (dio->dops && dio->dops->submit_io) |
3e08773c | 62 | dio->dops->submit_io(iter, bio, pos); |
8cecd0ba | 63 | else |
3e08773c | 64 | submit_bio(bio); |
db074436 DW |
65 | } |
66 | ||
c3d4ed1a | 67 | ssize_t iomap_dio_complete(struct iomap_dio *dio) |
db074436 | 68 | { |
838c4f3d | 69 | const struct iomap_dio_ops *dops = dio->dops; |
db074436 DW |
70 | struct kiocb *iocb = dio->iocb; |
71 | struct inode *inode = file_inode(iocb->ki_filp); | |
72 | loff_t offset = iocb->ki_pos; | |
838c4f3d | 73 | ssize_t ret = dio->error; |
db074436 | 74 | |
838c4f3d CH |
75 | if (dops && dops->end_io) |
76 | ret = dops->end_io(iocb, dio->size, ret, dio->flags); | |
db074436 DW |
77 | |
78 | if (likely(!ret)) { | |
79 | ret = dio->size; | |
80 | /* check for short read */ | |
81 | if (offset + ret > dio->i_size && | |
82 | !(dio->flags & IOMAP_DIO_WRITE)) | |
83 | ret = dio->i_size - offset; | |
84 | iocb->ki_pos += ret; | |
85 | } | |
86 | ||
87 | /* | |
88 | * Try again to invalidate clean pages which might have been cached by | |
89 | * non-direct readahead, or faulted in by get_user_pages() if the source | |
90 | * of the write was an mmap'ed region of the file we're writing. Either | |
91 | * one is a pretty crazy thing to do, so we don't support it 100%. If | |
92 | * this invalidation fails, tough, the write still worked... | |
93 | * | |
838c4f3d CH |
94 | * And this page cache invalidation has to be after ->end_io(), as some |
95 | * filesystems convert unwritten extents to real allocations in | |
96 | * ->end_io() when necessary, otherwise a racing buffer read would cache | |
db074436 DW |
97 | * zeros from unwritten extents. |
98 | */ | |
c114bbc6 | 99 | if (!dio->error && dio->size && |
db074436 DW |
100 | (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) { |
101 | int err; | |
102 | err = invalidate_inode_pages2_range(inode->i_mapping, | |
103 | offset >> PAGE_SHIFT, | |
104 | (offset + dio->size - 1) >> PAGE_SHIFT); | |
105 | if (err) | |
106 | dio_warn_stale_pagecache(iocb->ki_filp); | |
107 | } | |
108 | ||
1a31182e | 109 | inode_dio_end(file_inode(iocb->ki_filp)); |
db074436 DW |
110 | /* |
111 | * If this is a DSYNC write, make sure we push it to stable storage now | |
112 | * that we've written data. | |
113 | */ | |
114 | if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC)) | |
115 | ret = generic_write_sync(iocb, ret); | |
116 | ||
db074436 DW |
117 | kfree(dio); |
118 | ||
119 | return ret; | |
120 | } | |
c3d4ed1a | 121 | EXPORT_SYMBOL_GPL(iomap_dio_complete); |
db074436 DW |
122 | |
123 | static void iomap_dio_complete_work(struct work_struct *work) | |
124 | { | |
125 | struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work); | |
126 | struct kiocb *iocb = dio->iocb; | |
127 | ||
128 | iocb->ki_complete(iocb, iomap_dio_complete(dio), 0); | |
129 | } | |
130 | ||
131 | /* | |
132 | * Set an error in the dio if none is set yet. We have to use cmpxchg | |
133 | * as the submission context and the completion context(s) can race to | |
134 | * update the error. | |
135 | */ | |
136 | static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret) | |
137 | { | |
138 | cmpxchg(&dio->error, 0, ret); | |
139 | } | |
140 | ||
141 | static void iomap_dio_bio_end_io(struct bio *bio) | |
142 | { | |
143 | struct iomap_dio *dio = bio->bi_private; | |
144 | bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY); | |
145 | ||
146 | if (bio->bi_status) | |
147 | iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status)); | |
148 | ||
149 | if (atomic_dec_and_test(&dio->ref)) { | |
150 | if (dio->wait_for_completion) { | |
151 | struct task_struct *waiter = dio->submit.waiter; | |
152 | WRITE_ONCE(dio->submit.waiter, NULL); | |
153 | blk_wake_io_task(waiter); | |
154 | } else if (dio->flags & IOMAP_DIO_WRITE) { | |
155 | struct inode *inode = file_inode(dio->iocb->ki_filp); | |
156 | ||
3e08773c | 157 | WRITE_ONCE(dio->iocb->private, NULL); |
db074436 DW |
158 | INIT_WORK(&dio->aio.work, iomap_dio_complete_work); |
159 | queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work); | |
160 | } else { | |
3e08773c | 161 | WRITE_ONCE(dio->iocb->private, NULL); |
db074436 DW |
162 | iomap_dio_complete_work(&dio->aio.work); |
163 | } | |
164 | } | |
165 | ||
166 | if (should_dirty) { | |
167 | bio_check_pages_dirty(bio); | |
168 | } else { | |
169 | bio_release_pages(bio, false); | |
170 | bio_put(bio); | |
171 | } | |
172 | } | |
173 | ||
a6d3d495 CH |
174 | static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio, |
175 | loff_t pos, unsigned len) | |
db074436 DW |
176 | { |
177 | struct page *page = ZERO_PAGE(0); | |
178 | int flags = REQ_SYNC | REQ_IDLE; | |
179 | struct bio *bio; | |
180 | ||
181 | bio = bio_alloc(GFP_KERNEL, 1); | |
a6d3d495 CH |
182 | bio_set_dev(bio, iter->iomap.bdev); |
183 | bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); | |
db074436 DW |
184 | bio->bi_private = dio; |
185 | bio->bi_end_io = iomap_dio_bio_end_io; | |
186 | ||
187 | get_page(page); | |
188 | __bio_add_page(bio, page, len, 0); | |
189 | bio_set_op_attrs(bio, REQ_OP_WRITE, flags); | |
a6d3d495 | 190 | iomap_dio_submit_bio(iter, dio, bio, pos); |
db074436 DW |
191 | } |
192 | ||
c3b0e880 NA |
193 | /* |
194 | * Figure out the bio's operation flags from the dio request, the | |
195 | * mapping, and whether or not we want FUA. Note that we can end up | |
196 | * clearing the WRITE_FUA flag in the dio request. | |
197 | */ | |
a6d3d495 CH |
198 | static inline unsigned int iomap_dio_bio_opflags(struct iomap_dio *dio, |
199 | const struct iomap *iomap, bool use_fua) | |
c3b0e880 NA |
200 | { |
201 | unsigned int opflags = REQ_SYNC | REQ_IDLE; | |
202 | ||
203 | if (!(dio->flags & IOMAP_DIO_WRITE)) { | |
204 | WARN_ON_ONCE(iomap->flags & IOMAP_F_ZONE_APPEND); | |
205 | return REQ_OP_READ; | |
206 | } | |
207 | ||
208 | if (iomap->flags & IOMAP_F_ZONE_APPEND) | |
209 | opflags |= REQ_OP_ZONE_APPEND; | |
210 | else | |
211 | opflags |= REQ_OP_WRITE; | |
212 | ||
213 | if (use_fua) | |
214 | opflags |= REQ_FUA; | |
215 | else | |
216 | dio->flags &= ~IOMAP_DIO_WRITE_FUA; | |
217 | ||
218 | return opflags; | |
219 | } | |
220 | ||
a6d3d495 CH |
221 | static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter, |
222 | struct iomap_dio *dio) | |
db074436 | 223 | { |
a6d3d495 CH |
224 | const struct iomap *iomap = &iter->iomap; |
225 | struct inode *inode = iter->inode; | |
db074436 DW |
226 | unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev)); |
227 | unsigned int fs_block_size = i_blocksize(inode), pad; | |
228 | unsigned int align = iov_iter_alignment(dio->submit.iter); | |
a6d3d495 CH |
229 | loff_t length = iomap_length(iter); |
230 | loff_t pos = iter->pos; | |
c3b0e880 | 231 | unsigned int bio_opf; |
db074436 DW |
232 | struct bio *bio; |
233 | bool need_zeroout = false; | |
234 | bool use_fua = false; | |
235 | int nr_pages, ret = 0; | |
236 | size_t copied = 0; | |
f550ee9b | 237 | size_t orig_count; |
db074436 DW |
238 | |
239 | if ((pos | length | align) & ((1 << blkbits) - 1)) | |
240 | return -EINVAL; | |
241 | ||
242 | if (iomap->type == IOMAP_UNWRITTEN) { | |
243 | dio->flags |= IOMAP_DIO_UNWRITTEN; | |
244 | need_zeroout = true; | |
245 | } | |
246 | ||
247 | if (iomap->flags & IOMAP_F_SHARED) | |
248 | dio->flags |= IOMAP_DIO_COW; | |
249 | ||
250 | if (iomap->flags & IOMAP_F_NEW) { | |
251 | need_zeroout = true; | |
252 | } else if (iomap->type == IOMAP_MAPPED) { | |
253 | /* | |
254 | * Use a FUA write if we need datasync semantics, this is a pure | |
255 | * data IO that doesn't require any metadata updates (including | |
256 | * after IO completion such as unwritten extent conversion) and | |
257 | * the underlying device supports FUA. This allows us to avoid | |
258 | * cache flushes on IO completion. | |
259 | */ | |
260 | if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) && | |
261 | (dio->flags & IOMAP_DIO_WRITE_FUA) && | |
262 | blk_queue_fua(bdev_get_queue(iomap->bdev))) | |
263 | use_fua = true; | |
264 | } | |
265 | ||
266 | /* | |
f550ee9b JK |
267 | * Save the original count and trim the iter to just the extent we |
268 | * are operating on right now. The iter will be re-expanded once | |
269 | * we are done. | |
db074436 | 270 | */ |
f550ee9b JK |
271 | orig_count = iov_iter_count(dio->submit.iter); |
272 | iov_iter_truncate(dio->submit.iter, length); | |
db074436 | 273 | |
3e1a88ec | 274 | if (!iov_iter_count(dio->submit.iter)) |
f550ee9b | 275 | goto out; |
db074436 | 276 | |
f79d4749 CH |
277 | /* |
278 | * We can only poll for single bio I/Os. | |
279 | */ | |
280 | if (need_zeroout || | |
281 | ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) | |
282 | dio->iocb->ki_flags &= ~IOCB_HIPRI; | |
283 | ||
db074436 DW |
284 | if (need_zeroout) { |
285 | /* zero out from the start of the block to the write offset */ | |
286 | pad = pos & (fs_block_size - 1); | |
287 | if (pad) | |
a6d3d495 | 288 | iomap_dio_zero(iter, dio, pos - pad, pad); |
db074436 DW |
289 | } |
290 | ||
c3b0e880 NA |
291 | /* |
292 | * Set the operation flags early so that bio_iov_iter_get_pages | |
293 | * can set up the page vector appropriately for a ZONE_APPEND | |
294 | * operation. | |
295 | */ | |
296 | bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua); | |
297 | ||
a8affc03 | 298 | nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS); |
db074436 DW |
299 | do { |
300 | size_t n; | |
301 | if (dio->error) { | |
302 | iov_iter_revert(dio->submit.iter, copied); | |
f550ee9b JK |
303 | copied = ret = 0; |
304 | goto out; | |
db074436 DW |
305 | } |
306 | ||
307 | bio = bio_alloc(GFP_KERNEL, nr_pages); | |
308 | bio_set_dev(bio, iomap->bdev); | |
309 | bio->bi_iter.bi_sector = iomap_sector(iomap, pos); | |
310 | bio->bi_write_hint = dio->iocb->ki_hint; | |
311 | bio->bi_ioprio = dio->iocb->ki_ioprio; | |
312 | bio->bi_private = dio; | |
313 | bio->bi_end_io = iomap_dio_bio_end_io; | |
c3b0e880 | 314 | bio->bi_opf = bio_opf; |
db074436 | 315 | |
f550ee9b | 316 | ret = bio_iov_iter_get_pages(bio, dio->submit.iter); |
db074436 DW |
317 | if (unlikely(ret)) { |
318 | /* | |
319 | * We have to stop part way through an IO. We must fall | |
320 | * through to the sub-block tail zeroing here, otherwise | |
321 | * this short IO may expose stale data in the tail of | |
322 | * the block we haven't written data to. | |
323 | */ | |
324 | bio_put(bio); | |
325 | goto zero_tail; | |
326 | } | |
327 | ||
328 | n = bio->bi_iter.bi_size; | |
329 | if (dio->flags & IOMAP_DIO_WRITE) { | |
db074436 DW |
330 | task_io_account_write(n); |
331 | } else { | |
db074436 DW |
332 | if (dio->flags & IOMAP_DIO_DIRTY) |
333 | bio_set_pages_dirty(bio); | |
334 | } | |
335 | ||
db074436 | 336 | dio->size += n; |
db074436 DW |
337 | copied += n; |
338 | ||
3e1a88ec | 339 | nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, |
a8affc03 | 340 | BIO_MAX_VECS); |
f79d4749 CH |
341 | /* |
342 | * We can only poll for single bio I/Os. | |
343 | */ | |
344 | if (nr_pages) | |
345 | dio->iocb->ki_flags &= ~IOCB_HIPRI; | |
a6d3d495 | 346 | iomap_dio_submit_bio(iter, dio, bio, pos); |
8cecd0ba | 347 | pos += n; |
db074436 DW |
348 | } while (nr_pages); |
349 | ||
350 | /* | |
351 | * We need to zeroout the tail of a sub-block write if the extent type | |
352 | * requires zeroing or the write extends beyond EOF. If we don't zero | |
353 | * the block tail in the latter case, we can expose stale data via mmap | |
354 | * reads of the EOF block. | |
355 | */ | |
356 | zero_tail: | |
357 | if (need_zeroout || | |
358 | ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) { | |
359 | /* zero out from the end of the write to the end of the block */ | |
360 | pad = pos & (fs_block_size - 1); | |
361 | if (pad) | |
a6d3d495 | 362 | iomap_dio_zero(iter, dio, pos, fs_block_size - pad); |
db074436 | 363 | } |
f550ee9b JK |
364 | out: |
365 | /* Undo iter limitation to current extent */ | |
366 | iov_iter_reexpand(dio->submit.iter, orig_count - copied); | |
e9f930ac JS |
367 | if (copied) |
368 | return copied; | |
369 | return ret; | |
db074436 DW |
370 | } |
371 | ||
a6d3d495 CH |
372 | static loff_t iomap_dio_hole_iter(const struct iomap_iter *iter, |
373 | struct iomap_dio *dio) | |
db074436 | 374 | { |
a6d3d495 CH |
375 | loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter); |
376 | ||
db074436 DW |
377 | dio->size += length; |
378 | return length; | |
379 | } | |
380 | ||
a6d3d495 CH |
381 | static loff_t iomap_dio_inline_iter(const struct iomap_iter *iomi, |
382 | struct iomap_dio *dio) | |
db074436 | 383 | { |
a6d3d495 | 384 | const struct iomap *iomap = &iomi->iomap; |
db074436 | 385 | struct iov_iter *iter = dio->submit.iter; |
a6d3d495 CH |
386 | void *inline_data = iomap_inline_data(iomap, iomi->pos); |
387 | loff_t length = iomap_length(iomi); | |
388 | loff_t pos = iomi->pos; | |
db074436 DW |
389 | size_t copied; |
390 | ||
69f4a26c GX |
391 | if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap))) |
392 | return -EIO; | |
db074436 DW |
393 | |
394 | if (dio->flags & IOMAP_DIO_WRITE) { | |
a6d3d495 | 395 | loff_t size = iomi->inode->i_size; |
db074436 DW |
396 | |
397 | if (pos > size) | |
69f4a26c GX |
398 | memset(iomap_inline_data(iomap, size), 0, pos - size); |
399 | copied = copy_from_iter(inline_data, length, iter); | |
db074436 DW |
400 | if (copied) { |
401 | if (pos + copied > size) | |
a6d3d495 CH |
402 | i_size_write(iomi->inode, pos + copied); |
403 | mark_inode_dirty(iomi->inode); | |
db074436 DW |
404 | } |
405 | } else { | |
69f4a26c | 406 | copied = copy_to_iter(inline_data, length, iter); |
db074436 DW |
407 | } |
408 | dio->size += copied; | |
409 | return copied; | |
410 | } | |
411 | ||
a6d3d495 CH |
412 | static loff_t iomap_dio_iter(const struct iomap_iter *iter, |
413 | struct iomap_dio *dio) | |
db074436 | 414 | { |
a6d3d495 | 415 | switch (iter->iomap.type) { |
db074436 DW |
416 | case IOMAP_HOLE: |
417 | if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE)) | |
418 | return -EIO; | |
a6d3d495 | 419 | return iomap_dio_hole_iter(iter, dio); |
db074436 DW |
420 | case IOMAP_UNWRITTEN: |
421 | if (!(dio->flags & IOMAP_DIO_WRITE)) | |
a6d3d495 CH |
422 | return iomap_dio_hole_iter(iter, dio); |
423 | return iomap_dio_bio_iter(iter, dio); | |
db074436 | 424 | case IOMAP_MAPPED: |
a6d3d495 | 425 | return iomap_dio_bio_iter(iter, dio); |
db074436 | 426 | case IOMAP_INLINE: |
a6d3d495 | 427 | return iomap_dio_inline_iter(iter, dio); |
a805c111 QC |
428 | case IOMAP_DELALLOC: |
429 | /* | |
430 | * DIO is not serialised against mmap() access at all, and so | |
431 | * if the page_mkwrite occurs between the writeback and the | |
a6d3d495 | 432 | * iomap_iter() call in the DIO path, then it will see the |
a805c111 QC |
433 | * DELALLOC block that the page-mkwrite allocated. |
434 | */ | |
435 | pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n", | |
436 | dio->iocb->ki_filp, current->comm); | |
437 | return -EIO; | |
db074436 DW |
438 | default: |
439 | WARN_ON_ONCE(1); | |
440 | return -EIO; | |
441 | } | |
442 | } | |
443 | ||
444 | /* | |
445 | * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO | |
446 | * is being issued as AIO or not. This allows us to optimise pure data writes | |
447 | * to use REQ_FUA rather than requiring generic_write_sync() to issue a | |
448 | * REQ_FLUSH post write. This is slightly tricky because a single request here | |
449 | * can be mapped into multiple disjoint IOs and only a subset of the IOs issued | |
450 | * may be pure data writes. In that case, we still need to do a full data sync | |
451 | * completion. | |
60263d58 CH |
452 | * |
453 | * Returns -ENOTBLK In case of a page invalidation invalidation failure for | |
454 | * writes. The callers needs to fall back to buffered I/O in this case. | |
db074436 | 455 | */ |
c3d4ed1a CH |
456 | struct iomap_dio * |
457 | __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, | |
13ef9544 | 458 | const struct iomap_ops *ops, const struct iomap_dio_ops *dops, |
2f632965 | 459 | unsigned int dio_flags) |
db074436 DW |
460 | { |
461 | struct address_space *mapping = iocb->ki_filp->f_mapping; | |
462 | struct inode *inode = file_inode(iocb->ki_filp); | |
a6d3d495 CH |
463 | struct iomap_iter iomi = { |
464 | .inode = inode, | |
465 | .pos = iocb->ki_pos, | |
466 | .len = iov_iter_count(iter), | |
467 | .flags = IOMAP_DIRECT, | |
468 | }; | |
469 | loff_t end = iomi.pos + iomi.len - 1, ret = 0; | |
2f632965 CH |
470 | bool wait_for_completion = |
471 | is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT); | |
db074436 DW |
472 | struct blk_plug plug; |
473 | struct iomap_dio *dio; | |
474 | ||
a6d3d495 | 475 | if (!iomi.len) |
c3d4ed1a | 476 | return NULL; |
db074436 DW |
477 | |
478 | dio = kmalloc(sizeof(*dio), GFP_KERNEL); | |
479 | if (!dio) | |
c3d4ed1a | 480 | return ERR_PTR(-ENOMEM); |
db074436 DW |
481 | |
482 | dio->iocb = iocb; | |
483 | atomic_set(&dio->ref, 1); | |
484 | dio->size = 0; | |
485 | dio->i_size = i_size_read(inode); | |
838c4f3d | 486 | dio->dops = dops; |
db074436 DW |
487 | dio->error = 0; |
488 | dio->flags = 0; | |
489 | ||
490 | dio->submit.iter = iter; | |
491 | dio->submit.waiter = current; | |
3e08773c | 492 | dio->submit.poll_bio = NULL; |
db074436 DW |
493 | |
494 | if (iov_iter_rw(iter) == READ) { | |
a6d3d495 | 495 | if (iomi.pos >= dio->i_size) |
db074436 DW |
496 | goto out_free_dio; |
497 | ||
985b71db | 498 | if (iocb->ki_flags & IOCB_NOWAIT) { |
a6d3d495 CH |
499 | if (filemap_range_needs_writeback(mapping, iomi.pos, |
500 | end)) { | |
985b71db JA |
501 | ret = -EAGAIN; |
502 | goto out_free_dio; | |
503 | } | |
a6d3d495 | 504 | iomi.flags |= IOMAP_NOWAIT; |
985b71db JA |
505 | } |
506 | ||
a9010042 | 507 | if (iter_is_iovec(iter)) |
db074436 DW |
508 | dio->flags |= IOMAP_DIO_DIRTY; |
509 | } else { | |
a6d3d495 | 510 | iomi.flags |= IOMAP_WRITE; |
db074436 DW |
511 | dio->flags |= IOMAP_DIO_WRITE; |
512 | ||
985b71db | 513 | if (iocb->ki_flags & IOCB_NOWAIT) { |
a6d3d495 | 514 | if (filemap_range_has_page(mapping, iomi.pos, end)) { |
985b71db JA |
515 | ret = -EAGAIN; |
516 | goto out_free_dio; | |
517 | } | |
a6d3d495 | 518 | iomi.flags |= IOMAP_NOWAIT; |
985b71db JA |
519 | } |
520 | ||
db074436 DW |
521 | /* for data sync or sync, we need sync completion processing */ |
522 | if (iocb->ki_flags & IOCB_DSYNC) | |
523 | dio->flags |= IOMAP_DIO_NEED_SYNC; | |
524 | ||
525 | /* | |
526 | * For datasync only writes, we optimistically try using FUA for | |
527 | * this IO. Any non-FUA write that occurs will clear this flag, | |
528 | * hence we know before completion whether a cache flush is | |
529 | * necessary. | |
530 | */ | |
531 | if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC) | |
532 | dio->flags |= IOMAP_DIO_WRITE_FUA; | |
533 | } | |
534 | ||
213f6271 CH |
535 | if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) { |
536 | ret = -EAGAIN; | |
a6d3d495 CH |
537 | if (iomi.pos >= dio->i_size || |
538 | iomi.pos + iomi.len > dio->i_size) | |
213f6271 | 539 | goto out_free_dio; |
a6d3d495 | 540 | iomi.flags |= IOMAP_OVERWRITE_ONLY; |
db074436 DW |
541 | } |
542 | ||
a6d3d495 | 543 | ret = filemap_write_and_wait_range(mapping, iomi.pos, end); |
db074436 DW |
544 | if (ret) |
545 | goto out_free_dio; | |
546 | ||
54752de9 DC |
547 | if (iov_iter_rw(iter) == WRITE) { |
548 | /* | |
549 | * Try to invalidate cache pages for the range we are writing. | |
60263d58 CH |
550 | * If this invalidation fails, let the caller fall back to |
551 | * buffered I/O. | |
54752de9 | 552 | */ |
a6d3d495 CH |
553 | if (invalidate_inode_pages2_range(mapping, |
554 | iomi.pos >> PAGE_SHIFT, end >> PAGE_SHIFT)) { | |
555 | trace_iomap_dio_invalidate_fail(inode, iomi.pos, | |
556 | iomi.len); | |
60263d58 CH |
557 | ret = -ENOTBLK; |
558 | goto out_free_dio; | |
559 | } | |
db074436 | 560 | |
54752de9 DC |
561 | if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) { |
562 | ret = sb_init_dio_done_wq(inode->i_sb); | |
563 | if (ret < 0) | |
564 | goto out_free_dio; | |
565 | } | |
db074436 DW |
566 | } |
567 | ||
568 | inode_dio_begin(inode); | |
569 | ||
570 | blk_start_plug(&plug); | |
f79d4749 | 571 | while ((ret = iomap_iter(&iomi, ops)) > 0) { |
a6d3d495 | 572 | iomi.processed = iomap_dio_iter(&iomi, dio); |
f79d4749 CH |
573 | |
574 | /* | |
575 | * We can only poll for single bio I/Os. | |
576 | */ | |
577 | iocb->ki_flags &= ~IOCB_HIPRI; | |
578 | } | |
579 | ||
db074436 DW |
580 | blk_finish_plug(&plug); |
581 | ||
a6d3d495 CH |
582 | /* |
583 | * We only report that we've read data up to i_size. | |
584 | * Revert iter to a state corresponding to that as some callers (such | |
585 | * as the splice code) rely on it. | |
586 | */ | |
587 | if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size) | |
588 | iov_iter_revert(iter, iomi.pos - dio->i_size); | |
589 | ||
590 | /* magic error code to fall back to buffered I/O */ | |
591 | if (ret == -ENOTBLK) { | |
592 | wait_for_completion = true; | |
593 | ret = 0; | |
594 | } | |
db074436 DW |
595 | if (ret < 0) |
596 | iomap_dio_set_error(dio, ret); | |
597 | ||
598 | /* | |
599 | * If all the writes we issued were FUA, we don't need to flush the | |
600 | * cache on IO completion. Clear the sync flag for this case. | |
601 | */ | |
602 | if (dio->flags & IOMAP_DIO_WRITE_FUA) | |
603 | dio->flags &= ~IOMAP_DIO_NEED_SYNC; | |
604 | ||
3e08773c | 605 | WRITE_ONCE(iocb->private, dio->submit.poll_bio); |
db074436 DW |
606 | |
607 | /* | |
608 | * We are about to drop our additional submission reference, which | |
d9973ce2 | 609 | * might be the last reference to the dio. There are three different |
610 | * ways we can progress here: | |
db074436 DW |
611 | * |
612 | * (a) If this is the last reference we will always complete and free | |
613 | * the dio ourselves. | |
614 | * (b) If this is not the last reference, and we serve an asynchronous | |
615 | * iocb, we must never touch the dio after the decrement, the | |
616 | * I/O completion handler will complete and free it. | |
617 | * (c) If this is not the last reference, but we serve a synchronous | |
618 | * iocb, the I/O completion handler will wake us up on the drop | |
619 | * of the final reference, and we will complete and free it here | |
620 | * after we got woken by the I/O completion handler. | |
621 | */ | |
622 | dio->wait_for_completion = wait_for_completion; | |
623 | if (!atomic_dec_and_test(&dio->ref)) { | |
624 | if (!wait_for_completion) | |
c3d4ed1a | 625 | return ERR_PTR(-EIOCBQUEUED); |
db074436 DW |
626 | |
627 | for (;;) { | |
628 | set_current_state(TASK_UNINTERRUPTIBLE); | |
629 | if (!READ_ONCE(dio->submit.waiter)) | |
630 | break; | |
631 | ||
3e08773c | 632 | if (!dio->submit.poll_bio || |
5a72e899 | 633 | !bio_poll(dio->submit.poll_bio, NULL, 0)) |
e6249cdd | 634 | blk_io_schedule(); |
db074436 DW |
635 | } |
636 | __set_current_state(TASK_RUNNING); | |
637 | } | |
638 | ||
c3d4ed1a | 639 | return dio; |
db074436 DW |
640 | |
641 | out_free_dio: | |
642 | kfree(dio); | |
c3d4ed1a CH |
643 | if (ret) |
644 | return ERR_PTR(ret); | |
645 | return NULL; | |
646 | } | |
647 | EXPORT_SYMBOL_GPL(__iomap_dio_rw); | |
648 | ||
649 | ssize_t | |
650 | iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, | |
651 | const struct iomap_ops *ops, const struct iomap_dio_ops *dops, | |
2f632965 | 652 | unsigned int dio_flags) |
c3d4ed1a CH |
653 | { |
654 | struct iomap_dio *dio; | |
655 | ||
2f632965 | 656 | dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags); |
c3d4ed1a CH |
657 | if (IS_ERR_OR_NULL(dio)) |
658 | return PTR_ERR_OR_ZERO(dio); | |
659 | return iomap_dio_complete(dio); | |
db074436 DW |
660 | } |
661 | EXPORT_SYMBOL_GPL(iomap_dio_rw); |