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