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