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