blob: 4ecd255e0511ce8204fd6a4fee5ba08fa310c992 [file] [log] [blame]
Darrick J. Wongdb074432019-07-15 08:50:59 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07004 * Copyright (c) 2016-2021 Christoph Hellwig.
Darrick J. Wongdb074432019-07-15 08:50:59 -07005 */
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>
Christoph Hellwig60263d52020-07-23 22:45:59 -070013#include "trace.h"
Darrick J. Wongdb074432019-07-15 08:50:59 -070014
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
26struct iomap_dio {
27 struct kiocb *iocb;
Christoph Hellwig838c4f32019-09-19 15:32:45 -070028 const struct iomap_dio_ops *dops;
Darrick J. Wongdb074432019-07-15 08:50:59 -070029 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
52int 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}
60EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
61
Christoph Hellwiga6d3d492021-08-10 18:33:10 -070062static void iomap_dio_submit_bio(const struct iomap_iter *iter,
63 struct iomap_dio *dio, struct bio *bio, loff_t pos)
Darrick J. Wongdb074432019-07-15 08:50:59 -070064{
65 atomic_inc(&dio->ref);
66
67 if (dio->iocb->ki_flags & IOCB_HIPRI)
68 bio_set_polled(bio, dio->iocb);
69
Christoph Hellwiga6d3d492021-08-10 18:33:10 -070070 dio->submit.last_queue = bdev_get_queue(iter->iomap.bdev);
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -050071 if (dio->dops && dio->dops->submit_io)
Christoph Hellwiga6d3d492021-08-10 18:33:10 -070072 dio->submit.cookie = dio->dops->submit_io(iter, bio, pos);
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -050073 else
74 dio->submit.cookie = submit_bio(bio);
Darrick J. Wongdb074432019-07-15 08:50:59 -070075}
76
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -070077ssize_t iomap_dio_complete(struct iomap_dio *dio)
Darrick J. Wongdb074432019-07-15 08:50:59 -070078{
Christoph Hellwig838c4f32019-09-19 15:32:45 -070079 const struct iomap_dio_ops *dops = dio->dops;
Darrick J. Wongdb074432019-07-15 08:50:59 -070080 struct kiocb *iocb = dio->iocb;
81 struct inode *inode = file_inode(iocb->ki_filp);
82 loff_t offset = iocb->ki_pos;
Christoph Hellwig838c4f32019-09-19 15:32:45 -070083 ssize_t ret = dio->error;
Darrick J. Wongdb074432019-07-15 08:50:59 -070084
Christoph Hellwig838c4f32019-09-19 15:32:45 -070085 if (dops && dops->end_io)
86 ret = dops->end_io(iocb, dio->size, ret, dio->flags);
Darrick J. Wongdb074432019-07-15 08:50:59 -070087
88 if (likely(!ret)) {
89 ret = dio->size;
90 /* check for short read */
91 if (offset + ret > dio->i_size &&
92 !(dio->flags & IOMAP_DIO_WRITE))
93 ret = dio->i_size - offset;
94 iocb->ki_pos += ret;
95 }
96
97 /*
98 * Try again to invalidate clean pages which might have been cached by
99 * non-direct readahead, or faulted in by get_user_pages() if the source
100 * of the write was an mmap'ed region of the file we're writing. Either
101 * one is a pretty crazy thing to do, so we don't support it 100%. If
102 * this invalidation fails, tough, the write still worked...
103 *
Christoph Hellwig838c4f32019-09-19 15:32:45 -0700104 * And this page cache invalidation has to be after ->end_io(), as some
105 * filesystems convert unwritten extents to real allocations in
106 * ->end_io() when necessary, otherwise a racing buffer read would cache
Darrick J. Wongdb074432019-07-15 08:50:59 -0700107 * zeros from unwritten extents.
108 */
Andreas Gruenbacherc114bbc2020-09-10 08:26:16 -0700109 if (!dio->error && dio->size &&
Darrick J. Wongdb074432019-07-15 08:50:59 -0700110 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
111 int err;
112 err = invalidate_inode_pages2_range(inode->i_mapping,
113 offset >> PAGE_SHIFT,
114 (offset + dio->size - 1) >> PAGE_SHIFT);
115 if (err)
116 dio_warn_stale_pagecache(iocb->ki_filp);
117 }
118
Goldwyn Rodrigues1a311822020-09-28 08:51:08 -0700119 inode_dio_end(file_inode(iocb->ki_filp));
Darrick J. Wongdb074432019-07-15 08:50:59 -0700120 /*
121 * If this is a DSYNC write, make sure we push it to stable storage now
122 * that we've written data.
123 */
124 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
125 ret = generic_write_sync(iocb, ret);
126
Darrick J. Wongdb074432019-07-15 08:50:59 -0700127 kfree(dio);
128
129 return ret;
130}
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700131EXPORT_SYMBOL_GPL(iomap_dio_complete);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700132
133static void iomap_dio_complete_work(struct work_struct *work)
134{
135 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
136 struct kiocb *iocb = dio->iocb;
137
138 iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
139}
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 */
146static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
147{
148 cmpxchg(&dio->error, 0, ret);
149}
150
151static void iomap_dio_bio_end_io(struct bio *bio)
152{
153 struct iomap_dio *dio = bio->bi_private;
154 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
155
156 if (bio->bi_status)
157 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
158
159 if (atomic_dec_and_test(&dio->ref)) {
160 if (dio->wait_for_completion) {
161 struct task_struct *waiter = dio->submit.waiter;
162 WRITE_ONCE(dio->submit.waiter, NULL);
163 blk_wake_io_task(waiter);
164 } else if (dio->flags & IOMAP_DIO_WRITE) {
165 struct inode *inode = file_inode(dio->iocb->ki_filp);
166
167 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
168 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
169 } else {
170 iomap_dio_complete_work(&dio->aio.work);
171 }
172 }
173
174 if (should_dirty) {
175 bio_check_pages_dirty(bio);
176 } else {
177 bio_release_pages(bio, false);
178 bio_put(bio);
179 }
180}
181
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700182static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
183 loff_t pos, unsigned len)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700184{
185 struct page *page = ZERO_PAGE(0);
186 int flags = REQ_SYNC | REQ_IDLE;
187 struct bio *bio;
188
189 bio = bio_alloc(GFP_KERNEL, 1);
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700190 bio_set_dev(bio, iter->iomap.bdev);
191 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700192 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);
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700198 iomap_dio_submit_bio(iter, dio, bio, pos);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700199}
200
Naohiro Aotac3b0e882021-02-04 19:21:41 +0900201/*
202 * Figure out the bio's operation flags from the dio request, the
203 * mapping, and whether or not we want FUA. Note that we can end up
204 * clearing the WRITE_FUA flag in the dio request.
205 */
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700206static inline unsigned int iomap_dio_bio_opflags(struct iomap_dio *dio,
207 const struct iomap *iomap, bool use_fua)
Naohiro Aotac3b0e882021-02-04 19:21:41 +0900208{
209 unsigned int opflags = REQ_SYNC | REQ_IDLE;
210
211 if (!(dio->flags & IOMAP_DIO_WRITE)) {
212 WARN_ON_ONCE(iomap->flags & IOMAP_F_ZONE_APPEND);
213 return REQ_OP_READ;
214 }
215
216 if (iomap->flags & IOMAP_F_ZONE_APPEND)
217 opflags |= REQ_OP_ZONE_APPEND;
218 else
219 opflags |= REQ_OP_WRITE;
220
221 if (use_fua)
222 opflags |= REQ_FUA;
223 else
224 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
225
226 return opflags;
227}
228
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700229static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter,
230 struct iomap_dio *dio)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700231{
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700232 const struct iomap *iomap = &iter->iomap;
233 struct inode *inode = iter->inode;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700234 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
235 unsigned int fs_block_size = i_blocksize(inode), pad;
236 unsigned int align = iov_iter_alignment(dio->submit.iter);
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700237 loff_t length = iomap_length(iter);
238 loff_t pos = iter->pos;
Naohiro Aotac3b0e882021-02-04 19:21:41 +0900239 unsigned int bio_opf;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700240 struct bio *bio;
241 bool need_zeroout = false;
242 bool use_fua = false;
243 int nr_pages, ret = 0;
244 size_t copied = 0;
Jan Karaf550ee92019-11-26 09:28:47 -0800245 size_t orig_count;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700246
247 if ((pos | length | align) & ((1 << blkbits) - 1))
248 return -EINVAL;
249
250 if (iomap->type == IOMAP_UNWRITTEN) {
251 dio->flags |= IOMAP_DIO_UNWRITTEN;
252 need_zeroout = true;
253 }
254
255 if (iomap->flags & IOMAP_F_SHARED)
256 dio->flags |= IOMAP_DIO_COW;
257
258 if (iomap->flags & IOMAP_F_NEW) {
259 need_zeroout = true;
260 } else if (iomap->type == IOMAP_MAPPED) {
261 /*
262 * Use a FUA write if we need datasync semantics, this is a pure
263 * data IO that doesn't require any metadata updates (including
264 * after IO completion such as unwritten extent conversion) and
265 * the underlying device supports FUA. This allows us to avoid
266 * cache flushes on IO completion.
267 */
268 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
269 (dio->flags & IOMAP_DIO_WRITE_FUA) &&
270 blk_queue_fua(bdev_get_queue(iomap->bdev)))
271 use_fua = true;
272 }
273
274 /*
Jan Karaf550ee92019-11-26 09:28:47 -0800275 * Save the original count and trim the iter to just the extent we
276 * are operating on right now. The iter will be re-expanded once
277 * we are done.
Darrick J. Wongdb074432019-07-15 08:50:59 -0700278 */
Jan Karaf550ee92019-11-26 09:28:47 -0800279 orig_count = iov_iter_count(dio->submit.iter);
280 iov_iter_truncate(dio->submit.iter, length);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700281
Pavel Begunkov3e1a88e2021-01-09 16:03:02 +0000282 if (!iov_iter_count(dio->submit.iter))
Jan Karaf550ee92019-11-26 09:28:47 -0800283 goto out;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700284
285 if (need_zeroout) {
286 /* zero out from the start of the block to the write offset */
287 pad = pos & (fs_block_size - 1);
288 if (pad)
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700289 iomap_dio_zero(iter, dio, pos - pad, pad);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700290 }
291
Naohiro Aotac3b0e882021-02-04 19:21:41 +0900292 /*
293 * Set the operation flags early so that bio_iov_iter_get_pages
294 * can set up the page vector appropriately for a ZONE_APPEND
295 * operation.
296 */
297 bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua);
298
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100299 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700300 do {
301 size_t n;
302 if (dio->error) {
303 iov_iter_revert(dio->submit.iter, copied);
Jan Karaf550ee92019-11-26 09:28:47 -0800304 copied = ret = 0;
305 goto out;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700306 }
307
308 bio = bio_alloc(GFP_KERNEL, nr_pages);
309 bio_set_dev(bio, iomap->bdev);
310 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
311 bio->bi_write_hint = dio->iocb->ki_hint;
312 bio->bi_ioprio = dio->iocb->ki_ioprio;
313 bio->bi_private = dio;
314 bio->bi_end_io = iomap_dio_bio_end_io;
Naohiro Aotac3b0e882021-02-04 19:21:41 +0900315 bio->bi_opf = bio_opf;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700316
Jan Karaf550ee92019-11-26 09:28:47 -0800317 ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700318 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 bio_put(bio);
326 goto zero_tail;
327 }
328
329 n = bio->bi_iter.bi_size;
330 if (dio->flags & IOMAP_DIO_WRITE) {
Darrick J. Wongdb074432019-07-15 08:50:59 -0700331 task_io_account_write(n);
332 } else {
Darrick J. Wongdb074432019-07-15 08:50:59 -0700333 if (dio->flags & IOMAP_DIO_DIRTY)
334 bio_set_pages_dirty(bio);
335 }
336
Darrick J. Wongdb074432019-07-15 08:50:59 -0700337 dio->size += n;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700338 copied += n;
339
Pavel Begunkov3e1a88e2021-01-09 16:03:02 +0000340 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter,
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100341 BIO_MAX_VECS);
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700342 iomap_dio_submit_bio(iter, dio, bio, pos);
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -0500343 pos += n;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700344 } while (nr_pages);
345
346 /*
347 * We need to zeroout the tail of a sub-block write if the extent type
348 * requires zeroing or the write extends beyond EOF. If we don't zero
349 * the block tail in the latter case, we can expose stale data via mmap
350 * reads of the EOF block.
351 */
352zero_tail:
353 if (need_zeroout ||
354 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
355 /* zero out from the end of the write to the end of the block */
356 pad = pos & (fs_block_size - 1);
357 if (pad)
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700358 iomap_dio_zero(iter, dio, pos, fs_block_size - pad);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700359 }
Jan Karaf550ee92019-11-26 09:28:47 -0800360out:
361 /* Undo iter limitation to current extent */
362 iov_iter_reexpand(dio->submit.iter, orig_count - copied);
Jan Stanceke9f930a2019-11-11 12:58:24 -0800363 if (copied)
364 return copied;
365 return ret;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700366}
367
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700368static loff_t iomap_dio_hole_iter(const struct iomap_iter *iter,
369 struct iomap_dio *dio)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700370{
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700371 loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter);
372
Darrick J. Wongdb074432019-07-15 08:50:59 -0700373 dio->size += length;
374 return length;
375}
376
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700377static loff_t iomap_dio_inline_iter(const struct iomap_iter *iomi,
378 struct iomap_dio *dio)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700379{
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700380 const struct iomap *iomap = &iomi->iomap;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700381 struct iov_iter *iter = dio->submit.iter;
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700382 void *inline_data = iomap_inline_data(iomap, iomi->pos);
383 loff_t length = iomap_length(iomi);
384 loff_t pos = iomi->pos;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700385 size_t copied;
386
Gao Xiang69f4a262021-08-03 09:38:22 -0700387 if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
388 return -EIO;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700389
390 if (dio->flags & IOMAP_DIO_WRITE) {
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700391 loff_t size = iomi->inode->i_size;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700392
393 if (pos > size)
Gao Xiang69f4a262021-08-03 09:38:22 -0700394 memset(iomap_inline_data(iomap, size), 0, pos - size);
395 copied = copy_from_iter(inline_data, length, iter);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700396 if (copied) {
397 if (pos + copied > size)
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700398 i_size_write(iomi->inode, pos + copied);
399 mark_inode_dirty(iomi->inode);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700400 }
401 } else {
Gao Xiang69f4a262021-08-03 09:38:22 -0700402 copied = copy_to_iter(inline_data, length, iter);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700403 }
404 dio->size += copied;
405 return copied;
406}
407
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700408static loff_t iomap_dio_iter(const struct iomap_iter *iter,
409 struct iomap_dio *dio)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700410{
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700411 switch (iter->iomap.type) {
Darrick J. Wongdb074432019-07-15 08:50:59 -0700412 case IOMAP_HOLE:
413 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
414 return -EIO;
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700415 return iomap_dio_hole_iter(iter, dio);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700416 case IOMAP_UNWRITTEN:
417 if (!(dio->flags & IOMAP_DIO_WRITE))
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700418 return iomap_dio_hole_iter(iter, dio);
419 return iomap_dio_bio_iter(iter, dio);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700420 case IOMAP_MAPPED:
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700421 return iomap_dio_bio_iter(iter, dio);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700422 case IOMAP_INLINE:
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700423 return iomap_dio_inline_iter(iter, dio);
Qian Caia805c112020-09-10 08:26:15 -0700424 case IOMAP_DELALLOC:
425 /*
426 * DIO is not serialised against mmap() access at all, and so
427 * if the page_mkwrite occurs between the writeback and the
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700428 * iomap_iter() call in the DIO path, then it will see the
Qian Caia805c112020-09-10 08:26:15 -0700429 * DELALLOC block that the page-mkwrite allocated.
430 */
431 pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
432 dio->iocb->ki_filp, current->comm);
433 return -EIO;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700434 default:
435 WARN_ON_ONCE(1);
436 return -EIO;
437 }
438}
439
440/*
441 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
442 * is being issued as AIO or not. This allows us to optimise pure data writes
443 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
444 * REQ_FLUSH post write. This is slightly tricky because a single request here
445 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
446 * may be pure data writes. In that case, we still need to do a full data sync
447 * completion.
Christoph Hellwig60263d52020-07-23 22:45:59 -0700448 *
449 * Returns -ENOTBLK In case of a page invalidation invalidation failure for
450 * writes. The callers needs to fall back to buffered I/O in this case.
Darrick J. Wongdb074432019-07-15 08:50:59 -0700451 */
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700452struct iomap_dio *
453__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
Jan Kara13ef9542019-10-15 08:43:42 -0700454 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
Christoph Hellwig2f632962021-01-23 10:06:09 -0800455 unsigned int dio_flags)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700456{
457 struct address_space *mapping = iocb->ki_filp->f_mapping;
458 struct inode *inode = file_inode(iocb->ki_filp);
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700459 struct iomap_iter iomi = {
460 .inode = inode,
461 .pos = iocb->ki_pos,
462 .len = iov_iter_count(iter),
463 .flags = IOMAP_DIRECT,
464 };
465 loff_t end = iomi.pos + iomi.len - 1, ret = 0;
Christoph Hellwig2f632962021-01-23 10:06:09 -0800466 bool wait_for_completion =
467 is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700468 struct blk_plug plug;
469 struct iomap_dio *dio;
470
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700471 if (!iomi.len)
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700472 return NULL;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700473
474 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
475 if (!dio)
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700476 return ERR_PTR(-ENOMEM);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700477
478 dio->iocb = iocb;
479 atomic_set(&dio->ref, 1);
480 dio->size = 0;
481 dio->i_size = i_size_read(inode);
Christoph Hellwig838c4f32019-09-19 15:32:45 -0700482 dio->dops = dops;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700483 dio->error = 0;
484 dio->flags = 0;
485
486 dio->submit.iter = iter;
487 dio->submit.waiter = current;
488 dio->submit.cookie = BLK_QC_T_NONE;
489 dio->submit.last_queue = NULL;
490
491 if (iov_iter_rw(iter) == READ) {
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700492 if (iomi.pos >= dio->i_size)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700493 goto out_free_dio;
494
Jens Axboe985b71d2021-04-29 22:55:24 -0700495 if (iocb->ki_flags & IOCB_NOWAIT) {
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700496 if (filemap_range_needs_writeback(mapping, iomi.pos,
497 end)) {
Jens Axboe985b71d2021-04-29 22:55:24 -0700498 ret = -EAGAIN;
499 goto out_free_dio;
500 }
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700501 iomi.flags |= IOMAP_NOWAIT;
Jens Axboe985b71d2021-04-29 22:55:24 -0700502 }
503
Joseph Qia9010042019-10-29 09:51:24 -0700504 if (iter_is_iovec(iter))
Darrick J. Wongdb074432019-07-15 08:50:59 -0700505 dio->flags |= IOMAP_DIO_DIRTY;
506 } else {
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700507 iomi.flags |= IOMAP_WRITE;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700508 dio->flags |= IOMAP_DIO_WRITE;
509
Jens Axboe985b71d2021-04-29 22:55:24 -0700510 if (iocb->ki_flags & IOCB_NOWAIT) {
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700511 if (filemap_range_has_page(mapping, iomi.pos, end)) {
Jens Axboe985b71d2021-04-29 22:55:24 -0700512 ret = -EAGAIN;
513 goto out_free_dio;
514 }
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700515 iomi.flags |= IOMAP_NOWAIT;
Jens Axboe985b71d2021-04-29 22:55:24 -0700516 }
517
Darrick J. Wongdb074432019-07-15 08:50:59 -0700518 /* for data sync or sync, we need sync completion processing */
519 if (iocb->ki_flags & IOCB_DSYNC)
520 dio->flags |= IOMAP_DIO_NEED_SYNC;
521
522 /*
523 * For datasync only writes, we optimistically try using FUA for
524 * this IO. Any non-FUA write that occurs will clear this flag,
525 * hence we know before completion whether a cache flush is
526 * necessary.
527 */
528 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
529 dio->flags |= IOMAP_DIO_WRITE_FUA;
530 }
531
Christoph Hellwig213f6272021-01-23 10:06:10 -0800532 if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
533 ret = -EAGAIN;
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700534 if (iomi.pos >= dio->i_size ||
535 iomi.pos + iomi.len > dio->i_size)
Christoph Hellwig213f6272021-01-23 10:06:10 -0800536 goto out_free_dio;
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700537 iomi.flags |= IOMAP_OVERWRITE_ONLY;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700538 }
539
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700540 ret = filemap_write_and_wait_range(mapping, iomi.pos, end);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700541 if (ret)
542 goto out_free_dio;
543
Dave Chinner54752de2020-07-23 22:45:58 -0700544 if (iov_iter_rw(iter) == WRITE) {
545 /*
546 * Try to invalidate cache pages for the range we are writing.
Christoph Hellwig60263d52020-07-23 22:45:59 -0700547 * If this invalidation fails, let the caller fall back to
548 * buffered I/O.
Dave Chinner54752de2020-07-23 22:45:58 -0700549 */
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700550 if (invalidate_inode_pages2_range(mapping,
551 iomi.pos >> PAGE_SHIFT, end >> PAGE_SHIFT)) {
552 trace_iomap_dio_invalidate_fail(inode, iomi.pos,
553 iomi.len);
Christoph Hellwig60263d52020-07-23 22:45:59 -0700554 ret = -ENOTBLK;
555 goto out_free_dio;
556 }
Darrick J. Wongdb074432019-07-15 08:50:59 -0700557
Dave Chinner54752de2020-07-23 22:45:58 -0700558 if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
559 ret = sb_init_dio_done_wq(inode->i_sb);
560 if (ret < 0)
561 goto out_free_dio;
562 }
Darrick J. Wongdb074432019-07-15 08:50:59 -0700563 }
564
565 inode_dio_begin(inode);
566
567 blk_start_plug(&plug);
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700568 while ((ret = iomap_iter(&iomi, ops)) > 0)
569 iomi.processed = iomap_dio_iter(&iomi, dio);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700570 blk_finish_plug(&plug);
571
Christoph Hellwiga6d3d492021-08-10 18:33:10 -0700572 /*
573 * We only report that we've read data up to i_size.
574 * Revert iter to a state corresponding to that as some callers (such
575 * as the splice code) rely on it.
576 */
577 if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size)
578 iov_iter_revert(iter, iomi.pos - dio->i_size);
579
580 /* magic error code to fall back to buffered I/O */
581 if (ret == -ENOTBLK) {
582 wait_for_completion = true;
583 ret = 0;
584 }
Darrick J. Wongdb074432019-07-15 08:50:59 -0700585 if (ret < 0)
586 iomap_dio_set_error(dio, ret);
587
588 /*
589 * If all the writes we issued were FUA, we don't need to flush the
590 * cache on IO completion. Clear the sync flag for this case.
591 */
592 if (dio->flags & IOMAP_DIO_WRITE_FUA)
593 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
594
595 WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie);
596 WRITE_ONCE(iocb->private, dio->submit.last_queue);
597
598 /*
599 * We are about to drop our additional submission reference, which
yangerkund9973ce22020-03-18 08:04:36 -0700600 * might be the last reference to the dio. There are three different
601 * ways we can progress here:
Darrick J. Wongdb074432019-07-15 08:50:59 -0700602 *
603 * (a) If this is the last reference we will always complete and free
604 * the dio ourselves.
605 * (b) If this is not the last reference, and we serve an asynchronous
606 * iocb, we must never touch the dio after the decrement, the
607 * I/O completion handler will complete and free it.
608 * (c) If this is not the last reference, but we serve a synchronous
609 * iocb, the I/O completion handler will wake us up on the drop
610 * of the final reference, and we will complete and free it here
611 * after we got woken by the I/O completion handler.
612 */
613 dio->wait_for_completion = wait_for_completion;
614 if (!atomic_dec_and_test(&dio->ref)) {
615 if (!wait_for_completion)
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700616 return ERR_PTR(-EIOCBQUEUED);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700617
618 for (;;) {
619 set_current_state(TASK_UNINTERRUPTIBLE);
620 if (!READ_ONCE(dio->submit.waiter))
621 break;
622
623 if (!(iocb->ki_flags & IOCB_HIPRI) ||
624 !dio->submit.last_queue ||
625 !blk_poll(dio->submit.last_queue,
626 dio->submit.cookie, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800627 blk_io_schedule();
Darrick J. Wongdb074432019-07-15 08:50:59 -0700628 }
629 __set_current_state(TASK_RUNNING);
630 }
631
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700632 return dio;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700633
634out_free_dio:
635 kfree(dio);
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700636 if (ret)
637 return ERR_PTR(ret);
638 return NULL;
639}
640EXPORT_SYMBOL_GPL(__iomap_dio_rw);
641
642ssize_t
643iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
644 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
Christoph Hellwig2f632962021-01-23 10:06:09 -0800645 unsigned int dio_flags)
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700646{
647 struct iomap_dio *dio;
648
Christoph Hellwig2f632962021-01-23 10:06:09 -0800649 dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags);
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700650 if (IS_ERR_OR_NULL(dio))
651 return PTR_ERR_OR_ZERO(dio);
652 return iomap_dio_complete(dio);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700653}
654EXPORT_SYMBOL_GPL(iomap_dio_rw);