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