Linux Audio

Check our new training course

Embedded Linux training

Mar 10-20, 2025, special US time zones
Register
Loading...
v5.9
  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/iomap.h>
 10#include <linux/backing-dev.h>
 11#include <linux/uio.h>
 12#include <linux/task_io_accounting_ops.h>
 13#include "trace.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
 26struct 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
 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
 62static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,
 63		struct bio *bio, loff_t pos)
 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	if (dio->dops && dio->dops->submit_io)
 72		dio->submit.cookie = dio->dops->submit_io(
 73				file_inode(dio->iocb->ki_filp),
 74				iomap, bio, pos);
 75	else
 76		dio->submit.cookie = submit_bio(bio);
 77}
 78
 79static ssize_t iomap_dio_complete(struct iomap_dio *dio)
 80{
 81	const struct iomap_dio_ops *dops = dio->dops;
 82	struct kiocb *iocb = dio->iocb;
 83	struct inode *inode = file_inode(iocb->ki_filp);
 84	loff_t offset = iocb->ki_pos;
 85	ssize_t ret = dio->error;
 86
 87	if (dops && dops->end_io)
 88		ret = dops->end_io(iocb, dio->size, ret, dio->flags);
 89
 90	if (likely(!ret)) {
 91		ret = dio->size;
 92		/* check for short read */
 93		if (offset + ret > dio->i_size &&
 94		    !(dio->flags & IOMAP_DIO_WRITE))
 95			ret = dio->i_size - offset;
 96		iocb->ki_pos += ret;
 97	}
 98
 99	/*
100	 * Try again to invalidate clean pages which might have been cached by
101	 * non-direct readahead, or faulted in by get_user_pages() if the source
102	 * of the write was an mmap'ed region of the file we're writing.  Either
103	 * one is a pretty crazy thing to do, so we don't support it 100%.  If
104	 * this invalidation fails, tough, the write still worked...
105	 *
106	 * And this page cache invalidation has to be after ->end_io(), as some
107	 * filesystems convert unwritten extents to real allocations in
108	 * ->end_io() when necessary, otherwise a racing buffer read would cache
109	 * zeros from unwritten extents.
110	 */
111	if (!dio->error &&
112	    (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
113		int err;
114		err = invalidate_inode_pages2_range(inode->i_mapping,
115				offset >> PAGE_SHIFT,
116				(offset + dio->size - 1) >> PAGE_SHIFT);
117		if (err)
118			dio_warn_stale_pagecache(iocb->ki_filp);
119	}
120
121	/*
122	 * If this is a DSYNC write, make sure we push it to stable storage now
123	 * that we've written data.
124	 */
125	if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
126		ret = generic_write_sync(iocb, ret);
127
128	inode_dio_end(file_inode(iocb->ki_filp));
129	kfree(dio);
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131	return ret;
132}
 
 
 
 
 
 
133
134static void iomap_dio_complete_work(struct work_struct *work)
135{
136	struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
137	struct kiocb *iocb = dio->iocb;
138
139	iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
140}
141
142/*
143 * Set an error in the dio if none is set yet.  We have to use cmpxchg
144 * as the submission context and the completion context(s) can race to
145 * update the error.
146 */
147static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
148{
149	cmpxchg(&dio->error, 0, ret);
150}
151
152static void iomap_dio_bio_end_io(struct bio *bio)
153{
154	struct iomap_dio *dio = bio->bi_private;
155	bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
 
156
157	if (bio->bi_status)
158		iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
 
 
 
 
 
 
 
 
 
159
160	if (atomic_dec_and_test(&dio->ref)) {
161		if (dio->wait_for_completion) {
162			struct task_struct *waiter = dio->submit.waiter;
163			WRITE_ONCE(dio->submit.waiter, NULL);
164			blk_wake_io_task(waiter);
165		} else if (dio->flags & IOMAP_DIO_WRITE) {
166			struct inode *inode = file_inode(dio->iocb->ki_filp);
167
168			INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
169			queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
170		} else {
171			iomap_dio_complete_work(&dio->aio.work);
172		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173	}
174
 
 
 
 
 
 
 
 
 
 
175	if (should_dirty) {
176		bio_check_pages_dirty(bio);
177	} else {
178		bio_release_pages(bio, false);
179		bio_put(bio);
180	}
181}
 
182
183static void
184iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
185		unsigned len)
186{
187	struct page *page = ZERO_PAGE(0);
188	int flags = REQ_SYNC | REQ_IDLE;
189	struct bio *bio;
190
191	bio = bio_alloc(GFP_KERNEL, 1);
192	bio_set_dev(bio, iomap->bdev);
193	bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
 
 
 
 
 
 
 
 
 
194	bio->bi_private = dio;
195	bio->bi_end_io = iomap_dio_bio_end_io;
196
197	get_page(page);
198	__bio_add_page(bio, page, len, 0);
199	bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
200	iomap_dio_submit_bio(dio, iomap, bio, pos);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201}
202
203static loff_t
204iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
205		struct iomap_dio *dio, struct iomap *iomap)
206{
207	unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
 
208	unsigned int fs_block_size = i_blocksize(inode), pad;
209	unsigned int align = iov_iter_alignment(dio->submit.iter);
 
 
 
210	struct bio *bio;
211	bool need_zeroout = false;
212	bool use_fua = false;
213	int nr_pages, ret = 0;
214	size_t copied = 0;
215	size_t orig_count;
216
217	if ((pos | length | align) & ((1 << blkbits) - 1))
 
 
 
 
218		return -EINVAL;
219
220	if (iomap->type == IOMAP_UNWRITTEN) {
221		dio->flags |= IOMAP_DIO_UNWRITTEN;
222		need_zeroout = true;
223	}
224
225	if (iomap->flags & IOMAP_F_SHARED)
226		dio->flags |= IOMAP_DIO_COW;
227
228	if (iomap->flags & IOMAP_F_NEW) {
229		need_zeroout = true;
230	} else if (iomap->type == IOMAP_MAPPED) {
231		/*
232		 * Use a FUA write if we need datasync semantics, this is a pure
233		 * data IO that doesn't require any metadata updates (including
234		 * after IO completion such as unwritten extent conversion) and
235		 * the underlying device supports FUA. This allows us to avoid
236		 * cache flushes on IO completion.
 
 
 
 
237		 */
238		if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
239		    (dio->flags & IOMAP_DIO_WRITE_FUA) &&
240		    blk_queue_fua(bdev_get_queue(iomap->bdev)))
241			use_fua = true;
 
 
242	}
243
244	/*
245	 * Save the original count and trim the iter to just the extent we
246	 * are operating on right now.  The iter will be re-expanded once
247	 * we are done.
248	 */
249	orig_count = iov_iter_count(dio->submit.iter);
250	iov_iter_truncate(dio->submit.iter, length);
251
252	nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
253	if (nr_pages <= 0) {
254		ret = nr_pages;
255		goto out;
256	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
258	if (need_zeroout) {
259		/* zero out from the start of the block to the write offset */
260		pad = pos & (fs_block_size - 1);
261		if (pad)
262			iomap_dio_zero(dio, iomap, pos - pad, pad);
 
 
263	}
264
 
 
 
265	do {
266		size_t n;
267		if (dio->error) {
268			iov_iter_revert(dio->submit.iter, copied);
269			copied = ret = 0;
270			goto out;
271		}
272
273		bio = bio_alloc(GFP_KERNEL, nr_pages);
274		bio_set_dev(bio, iomap->bdev);
 
275		bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
276		bio->bi_write_hint = dio->iocb->ki_hint;
277		bio->bi_ioprio = dio->iocb->ki_ioprio;
278		bio->bi_private = dio;
279		bio->bi_end_io = iomap_dio_bio_end_io;
280
281		ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
282		if (unlikely(ret)) {
283			/*
284			 * We have to stop part way through an IO. We must fall
285			 * through to the sub-block tail zeroing here, otherwise
286			 * this short IO may expose stale data in the tail of
287			 * the block we haven't written data to.
288			 */
289			bio_put(bio);
290			goto zero_tail;
291		}
292
293		n = bio->bi_iter.bi_size;
 
 
 
 
 
 
 
 
 
 
 
294		if (dio->flags & IOMAP_DIO_WRITE) {
295			bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
296			if (use_fua)
297				bio->bi_opf |= REQ_FUA;
298			else
299				dio->flags &= ~IOMAP_DIO_WRITE_FUA;
300			task_io_account_write(n);
301		} else {
302			bio->bi_opf = REQ_OP_READ;
303			if (dio->flags & IOMAP_DIO_DIRTY)
304				bio_set_pages_dirty(bio);
305		}
306
307		dio->size += n;
308		copied += n;
309
310		nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
311		iomap_dio_submit_bio(dio, iomap, bio, pos);
 
 
 
 
 
 
312		pos += n;
313	} while (nr_pages);
314
315	/*
316	 * We need to zeroout the tail of a sub-block write if the extent type
317	 * requires zeroing or the write extends beyond EOF. If we don't zero
318	 * the block tail in the latter case, we can expose stale data via mmap
319	 * reads of the EOF block.
320	 */
321zero_tail:
322	if (need_zeroout ||
323	    ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
324		/* zero out from the end of the write to the end of the block */
325		pad = pos & (fs_block_size - 1);
326		if (pad)
327			iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
 
328	}
329out:
330	/* Undo iter limitation to current extent */
331	iov_iter_reexpand(dio->submit.iter, orig_count - copied);
332	if (copied)
333		return copied;
334	return ret;
335}
336
337static loff_t
338iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
339{
340	length = iov_iter_zero(length, dio->submit.iter);
 
341	dio->size += length;
 
 
342	return length;
343}
344
345static loff_t
346iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
347		struct iomap_dio *dio, struct iomap *iomap)
348{
 
349	struct iov_iter *iter = dio->submit.iter;
 
 
 
350	size_t copied;
351
352	BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
 
353
354	if (dio->flags & IOMAP_DIO_WRITE) {
355		loff_t size = inode->i_size;
356
357		if (pos > size)
358			memset(iomap->inline_data + size, 0, pos - size);
359		copied = copy_from_iter(iomap->inline_data + pos, length, iter);
360		if (copied) {
361			if (pos + copied > size)
362				i_size_write(inode, pos + copied);
363			mark_inode_dirty(inode);
364		}
365	} else {
366		copied = copy_to_iter(iomap->inline_data + pos, length, iter);
367	}
368	dio->size += copied;
 
 
369	return copied;
370}
371
372static loff_t
373iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
374		void *data, struct iomap *iomap, struct iomap *srcmap)
375{
376	struct iomap_dio *dio = data;
377
378	switch (iomap->type) {
379	case IOMAP_HOLE:
380		if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
381			return -EIO;
382		return iomap_dio_hole_actor(length, dio);
383	case IOMAP_UNWRITTEN:
384		if (!(dio->flags & IOMAP_DIO_WRITE))
385			return iomap_dio_hole_actor(length, dio);
386		return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
387	case IOMAP_MAPPED:
388		return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
389	case IOMAP_INLINE:
390		return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
 
 
 
 
 
 
 
 
 
 
391	default:
392		WARN_ON_ONCE(1);
393		return -EIO;
394	}
395}
396
397/*
398 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
399 * is being issued as AIO or not.  This allows us to optimise pure data writes
400 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
401 * REQ_FLUSH post write. This is slightly tricky because a single request here
402 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
403 * may be pure data writes. In that case, we still need to do a full data sync
404 * completion.
405 *
 
 
 
 
 
 
 
 
406 * Returns -ENOTBLK In case of a page invalidation invalidation failure for
407 * writes.  The callers needs to fall back to buffered I/O in this case.
408 */
409ssize_t
410iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
411		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
412		bool wait_for_completion)
413{
414	struct address_space *mapping = iocb->ki_filp->f_mapping;
415	struct inode *inode = file_inode(iocb->ki_filp);
416	size_t count = iov_iter_count(iter);
417	loff_t pos = iocb->ki_pos;
418	loff_t end = iocb->ki_pos + count - 1, ret = 0;
419	unsigned int flags = IOMAP_DIRECT;
 
 
 
 
 
420	struct blk_plug plug;
421	struct iomap_dio *dio;
 
422
423	if (!count)
424		return 0;
425
426	if (WARN_ON(is_sync_kiocb(iocb) && !wait_for_completion))
427		return -EIO;
428
429	dio = kmalloc(sizeof(*dio), GFP_KERNEL);
430	if (!dio)
431		return -ENOMEM;
432
433	dio->iocb = iocb;
434	atomic_set(&dio->ref, 1);
435	dio->size = 0;
436	dio->i_size = i_size_read(inode);
437	dio->dops = dops;
438	dio->error = 0;
439	dio->flags = 0;
 
440
441	dio->submit.iter = iter;
442	dio->submit.waiter = current;
443	dio->submit.cookie = BLK_QC_T_NONE;
444	dio->submit.last_queue = NULL;
 
 
 
 
445
446	if (iov_iter_rw(iter) == READ) {
447		if (pos >= dio->i_size)
 
 
 
448			goto out_free_dio;
449
450		if (iter_is_iovec(iter))
451			dio->flags |= IOMAP_DIO_DIRTY;
 
 
 
 
452	} else {
453		flags |= IOMAP_WRITE;
454		dio->flags |= IOMAP_DIO_WRITE;
455
456		/* for data sync or sync, we need sync completion processing */
457		if (iocb->ki_flags & IOCB_DSYNC)
458			dio->flags |= IOMAP_DIO_NEED_SYNC;
459
460		/*
461		 * For datasync only writes, we optimistically try using FUA for
462		 * this IO.  Any non-FUA write that occurs will clear this flag,
463		 * hence we know before completion whether a cache flush is
464		 * necessary.
465		 */
466		if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
467			dio->flags |= IOMAP_DIO_WRITE_FUA;
468	}
469
470	if (iocb->ki_flags & IOCB_NOWAIT) {
471		if (filemap_range_has_page(mapping, pos, end)) {
472			ret = -EAGAIN;
473			goto out_free_dio;
 
 
 
474		}
475		flags |= IOMAP_NOWAIT;
476	}
477
478	ret = filemap_write_and_wait_range(mapping, pos, end);
479	if (ret)
480		goto out_free_dio;
 
 
 
 
 
 
 
 
 
 
 
 
 
481
482	if (iov_iter_rw(iter) == WRITE) {
483		/*
484		 * Try to invalidate cache pages for the range we are writing.
485		 * If this invalidation fails, let the caller fall back to
486		 * buffered I/O.
487		 */
488		if (invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
489				end >> PAGE_SHIFT)) {
490			trace_iomap_dio_invalidate_fail(inode, pos, count);
491			ret = -ENOTBLK;
 
 
 
 
 
 
 
 
 
 
 
 
 
492			goto out_free_dio;
493		}
494
495		if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
496			ret = sb_init_dio_done_wq(inode->i_sb);
497			if (ret < 0)
498				goto out_free_dio;
499		}
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 different
546	 * 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				blk_io_schedule();
573		}
574		__set_current_state(TASK_RUNNING);
575	}
576
577	return iomap_dio_complete(dio);
578
579out_free_dio:
580	kfree(dio);
581	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582}
583EXPORT_SYMBOL_GPL(iomap_dio_rw);
v6.13.7
  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_CALLER_COMP	(1U << 26)
 24#define IOMAP_DIO_INLINE_COMP	(1U << 27)
 25#define IOMAP_DIO_WRITE_THROUGH	(1U << 28)
 26#define IOMAP_DIO_NEED_SYNC	(1U << 29)
 27#define IOMAP_DIO_WRITE		(1U << 30)
 28#define IOMAP_DIO_DIRTY		(1U << 31)
 29
 30/*
 31 * Used for sub block zeroing in iomap_dio_zero()
 32 */
 33#define IOMAP_ZERO_PAGE_SIZE (SZ_64K)
 34#define IOMAP_ZERO_PAGE_ORDER (get_order(IOMAP_ZERO_PAGE_SIZE))
 35static struct page *zero_page;
 36
 37struct iomap_dio {
 38	struct kiocb		*iocb;
 39	const struct iomap_dio_ops *dops;
 40	loff_t			i_size;
 41	loff_t			size;
 42	atomic_t		ref;
 43	unsigned		flags;
 44	int			error;
 45	size_t			done_before;
 46	bool			wait_for_completion;
 47
 48	union {
 49		/* used during submission and for synchronous completion: */
 50		struct {
 51			struct iov_iter		*iter;
 52			struct task_struct	*waiter;
 
 
 53		} submit;
 54
 55		/* used for aio completion: */
 56		struct {
 57			struct work_struct	work;
 58		} aio;
 59	};
 60};
 61
 62static struct bio *iomap_dio_alloc_bio(const struct iomap_iter *iter,
 63		struct iomap_dio *dio, unsigned short nr_vecs, blk_opf_t opf)
 64{
 65	if (dio->dops && dio->dops->bio_set)
 66		return bio_alloc_bioset(iter->iomap.bdev, nr_vecs, opf,
 67					GFP_KERNEL, dio->dops->bio_set);
 68	return bio_alloc(iter->iomap.bdev, nr_vecs, opf, GFP_KERNEL);
 
 69}
 
 70
 71static void iomap_dio_submit_bio(const struct iomap_iter *iter,
 72		struct iomap_dio *dio, struct bio *bio, loff_t pos)
 73{
 74	struct kiocb *iocb = dio->iocb;
 75
 76	atomic_inc(&dio->ref);
 77
 78	/* Sync dio can't be polled reliably */
 79	if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) {
 80		bio_set_polled(bio, iocb);
 81		WRITE_ONCE(iocb->private, bio);
 82	}
 83
 
 84	if (dio->dops && dio->dops->submit_io)
 85		dio->dops->submit_io(iter, bio, pos);
 
 
 86	else
 87		submit_bio(bio);
 88}
 89
 90ssize_t iomap_dio_complete(struct iomap_dio *dio)
 91{
 92	const struct iomap_dio_ops *dops = dio->dops;
 93	struct kiocb *iocb = dio->iocb;
 
 94	loff_t offset = iocb->ki_pos;
 95	ssize_t ret = dio->error;
 96
 97	if (dops && dops->end_io)
 98		ret = dops->end_io(iocb, dio->size, ret, dio->flags);
 99
100	if (likely(!ret)) {
101		ret = dio->size;
102		/* check for short read */
103		if (offset + ret > dio->i_size &&
104		    !(dio->flags & IOMAP_DIO_WRITE))
105			ret = dio->i_size - offset;
 
106	}
107
108	/*
109	 * Try again to invalidate clean pages which might have been cached by
110	 * non-direct readahead, or faulted in by get_user_pages() if the source
111	 * of the write was an mmap'ed region of the file we're writing.  Either
112	 * one is a pretty crazy thing to do, so we don't support it 100%.  If
113	 * this invalidation fails, tough, the write still worked...
114	 *
115	 * And this page cache invalidation has to be after ->end_io(), as some
116	 * filesystems convert unwritten extents to real allocations in
117	 * ->end_io() when necessary, otherwise a racing buffer read would cache
118	 * zeros from unwritten extents.
119	 */
120	if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE))
121		kiocb_invalidate_post_direct_write(iocb, dio->size);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
123	inode_dio_end(file_inode(iocb->ki_filp));
 
124
125	if (ret > 0) {
126		iocb->ki_pos += ret;
127
128		/*
129		 * If this is a DSYNC write, make sure we push it to stable
130		 * storage now that we've written data.
131		 */
132		if (dio->flags & IOMAP_DIO_NEED_SYNC)
133			ret = generic_write_sync(iocb, ret);
134		if (ret > 0)
135			ret += dio->done_before;
136	}
137	trace_iomap_dio_complete(iocb, dio->error, ret);
138	kfree(dio);
139	return ret;
140}
141EXPORT_SYMBOL_GPL(iomap_dio_complete);
142
143static ssize_t iomap_dio_deferred_complete(void *data)
144{
145	return iomap_dio_complete(data);
146}
147
148static void iomap_dio_complete_work(struct work_struct *work)
149{
150	struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
151	struct kiocb *iocb = dio->iocb;
152
153	iocb->ki_complete(iocb, iomap_dio_complete(dio));
154}
155
156/*
157 * Set an error in the dio if none is set yet.  We have to use cmpxchg
158 * as the submission context and the completion context(s) can race to
159 * update the error.
160 */
161static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
162{
163	cmpxchg(&dio->error, 0, ret);
164}
165
166void iomap_dio_bio_end_io(struct bio *bio)
167{
168	struct iomap_dio *dio = bio->bi_private;
169	bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
170	struct kiocb *iocb = dio->iocb;
171
172	if (bio->bi_status)
173		iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
174	if (!atomic_dec_and_test(&dio->ref))
175		goto release_bio;
176
177	/*
178	 * Synchronous dio, task itself will handle any completion work
179	 * that needs after IO. All we need to do is wake the task.
180	 */
181	if (dio->wait_for_completion) {
182		struct task_struct *waiter = dio->submit.waiter;
183
184		WRITE_ONCE(dio->submit.waiter, NULL);
185		blk_wake_io_task(waiter);
186		goto release_bio;
187	}
 
 
 
188
189	/*
190	 * Flagged with IOMAP_DIO_INLINE_COMP, we can complete it inline
191	 */
192	if (dio->flags & IOMAP_DIO_INLINE_COMP) {
193		WRITE_ONCE(iocb->private, NULL);
194		iomap_dio_complete_work(&dio->aio.work);
195		goto release_bio;
196	}
197
198	/*
199	 * If this dio is flagged with IOMAP_DIO_CALLER_COMP, then schedule
200	 * our completion that way to avoid an async punt to a workqueue.
201	 */
202	if (dio->flags & IOMAP_DIO_CALLER_COMP) {
203		/* only polled IO cares about private cleared */
204		iocb->private = dio;
205		iocb->dio_complete = iomap_dio_deferred_complete;
206
207		/*
208		 * Invoke ->ki_complete() directly. We've assigned our
209		 * dio_complete callback handler, and since the issuer set
210		 * IOCB_DIO_CALLER_COMP, we know their ki_complete handler will
211		 * notice ->dio_complete being set and will defer calling that
212		 * handler until it can be done from a safe task context.
213		 *
214		 * Note that the 'res' being passed in here is not important
215		 * for this case. The actual completion value of the request
216		 * will be gotten from dio_complete when that is run by the
217		 * issuer.
218		 */
219		iocb->ki_complete(iocb, 0);
220		goto release_bio;
221	}
222
223	/*
224	 * Async DIO completion that requires filesystem level completion work
225	 * gets punted to a work queue to complete as the operation may require
226	 * more IO to be issued to finalise filesystem metadata changes or
227	 * guarantee data integrity.
228	 */
229	INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
230	queue_work(file_inode(iocb->ki_filp)->i_sb->s_dio_done_wq,
231			&dio->aio.work);
232release_bio:
233	if (should_dirty) {
234		bio_check_pages_dirty(bio);
235	} else {
236		bio_release_pages(bio, false);
237		bio_put(bio);
238	}
239}
240EXPORT_SYMBOL_GPL(iomap_dio_bio_end_io);
241
242static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
243		loff_t pos, unsigned len)
 
244{
245	struct inode *inode = file_inode(dio->iocb->ki_filp);
 
246	struct bio *bio;
247
248	if (!len)
249		return 0;
250	/*
251	 * Max block size supported is 64k
252	 */
253	if (WARN_ON_ONCE(len > IOMAP_ZERO_PAGE_SIZE))
254		return -EINVAL;
255
256	bio = iomap_dio_alloc_bio(iter, dio, 1, REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
257	fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
258				  GFP_KERNEL);
259	bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
260	bio->bi_private = dio;
261	bio->bi_end_io = iomap_dio_bio_end_io;
262
263	__bio_add_page(bio, zero_page, len, 0);
264	iomap_dio_submit_bio(iter, dio, bio, pos);
265	return 0;
266}
267
268/*
269 * Figure out the bio's operation flags from the dio request, the
270 * mapping, and whether or not we want FUA.  Note that we can end up
271 * clearing the WRITE_THROUGH flag in the dio request.
272 */
273static inline blk_opf_t iomap_dio_bio_opflags(struct iomap_dio *dio,
274		const struct iomap *iomap, bool use_fua, bool atomic)
275{
276	blk_opf_t opflags = REQ_SYNC | REQ_IDLE;
277
278	if (!(dio->flags & IOMAP_DIO_WRITE))
279		return REQ_OP_READ;
280
281	opflags |= REQ_OP_WRITE;
282	if (use_fua)
283		opflags |= REQ_FUA;
284	else
285		dio->flags &= ~IOMAP_DIO_WRITE_THROUGH;
286	if (atomic)
287		opflags |= REQ_ATOMIC;
288
289	return opflags;
290}
291
292static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter,
293		struct iomap_dio *dio)
 
294{
295	const struct iomap *iomap = &iter->iomap;
296	struct inode *inode = iter->inode;
297	unsigned int fs_block_size = i_blocksize(inode), pad;
298	const loff_t length = iomap_length(iter);
299	bool atomic = iter->flags & IOMAP_ATOMIC;
300	loff_t pos = iter->pos;
301	blk_opf_t bio_opf;
302	struct bio *bio;
303	bool need_zeroout = false;
304	bool use_fua = false;
305	int nr_pages, ret = 0;
306	size_t copied = 0;
307	size_t orig_count;
308
309	if (atomic && length != fs_block_size)
310		return -EINVAL;
311
312	if ((pos | length) & (bdev_logical_block_size(iomap->bdev) - 1) ||
313	    !bdev_iter_is_aligned(iomap->bdev, dio->submit.iter))
314		return -EINVAL;
315
316	if (iomap->type == IOMAP_UNWRITTEN) {
317		dio->flags |= IOMAP_DIO_UNWRITTEN;
318		need_zeroout = true;
319	}
320
321	if (iomap->flags & IOMAP_F_SHARED)
322		dio->flags |= IOMAP_DIO_COW;
323
324	if (iomap->flags & IOMAP_F_NEW) {
325		need_zeroout = true;
326	} else if (iomap->type == IOMAP_MAPPED) {
327		/*
328		 * Use a FUA write if we need datasync semantics, this is a pure
329		 * data IO that doesn't require any metadata updates (including
330		 * after IO completion such as unwritten extent conversion) and
331		 * the underlying device either supports FUA or doesn't have
332		 * a volatile write cache. This allows us to avoid cache flushes
333		 * on IO completion. If we can't use writethrough and need to
334		 * sync, disable in-task completions as dio completion will
335		 * need to call generic_write_sync() which will do a blocking
336		 * fsync / cache flush call.
337		 */
338		if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
339		    (dio->flags & IOMAP_DIO_WRITE_THROUGH) &&
340		    (bdev_fua(iomap->bdev) || !bdev_write_cache(iomap->bdev)))
341			use_fua = true;
342		else if (dio->flags & IOMAP_DIO_NEED_SYNC)
343			dio->flags &= ~IOMAP_DIO_CALLER_COMP;
344	}
345
346	/*
347	 * Save the original count and trim the iter to just the extent we
348	 * are operating on right now.  The iter will be re-expanded once
349	 * we are done.
350	 */
351	orig_count = iov_iter_count(dio->submit.iter);
352	iov_iter_truncate(dio->submit.iter, length);
353
354	if (!iov_iter_count(dio->submit.iter))
 
 
355		goto out;
356
357	/*
358	 * We can only do deferred completion for pure overwrites that
359	 * don't require additional IO at completion. This rules out
360	 * writes that need zeroing or extent conversion, extend
361	 * the file size, or issue journal IO or cache flushes
362	 * during completion processing.
363	 */
364	if (need_zeroout ||
365	    ((dio->flags & IOMAP_DIO_NEED_SYNC) && !use_fua) ||
366	    ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode)))
367		dio->flags &= ~IOMAP_DIO_CALLER_COMP;
368
369	/*
370	 * The rules for polled IO completions follow the guidelines as the
371	 * ones we set for inline and deferred completions. If none of those
372	 * are available for this IO, clear the polled flag.
373	 */
374	if (!(dio->flags & (IOMAP_DIO_INLINE_COMP|IOMAP_DIO_CALLER_COMP)))
375		dio->iocb->ki_flags &= ~IOCB_HIPRI;
376
377	if (need_zeroout) {
378		/* zero out from the start of the block to the write offset */
379		pad = pos & (fs_block_size - 1);
380
381		ret = iomap_dio_zero(iter, dio, pos - pad, pad);
382		if (ret)
383			goto out;
384	}
385
386	bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua, atomic);
387
388	nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
389	do {
390		size_t n;
391		if (dio->error) {
392			iov_iter_revert(dio->submit.iter, copied);
393			copied = ret = 0;
394			goto out;
395		}
396
397		bio = iomap_dio_alloc_bio(iter, dio, nr_pages, bio_opf);
398		fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
399					  GFP_KERNEL);
400		bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
401		bio->bi_write_hint = inode->i_write_hint;
402		bio->bi_ioprio = dio->iocb->ki_ioprio;
403		bio->bi_private = dio;
404		bio->bi_end_io = iomap_dio_bio_end_io;
405
406		ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
407		if (unlikely(ret)) {
408			/*
409			 * We have to stop part way through an IO. We must fall
410			 * through to the sub-block tail zeroing here, otherwise
411			 * this short IO may expose stale data in the tail of
412			 * the block we haven't written data to.
413			 */
414			bio_put(bio);
415			goto zero_tail;
416		}
417
418		n = bio->bi_iter.bi_size;
419		if (WARN_ON_ONCE(atomic && n != length)) {
420			/*
421			 * This bio should have covered the complete length,
422			 * which it doesn't, so error. We may need to zero out
423			 * the tail (complete FS block), similar to when
424			 * bio_iov_iter_get_pages() returns an error, above.
425			 */
426			ret = -EINVAL;
427			bio_put(bio);
428			goto zero_tail;
429		}
430		if (dio->flags & IOMAP_DIO_WRITE) {
 
 
 
 
 
431			task_io_account_write(n);
432		} else {
 
433			if (dio->flags & IOMAP_DIO_DIRTY)
434				bio_set_pages_dirty(bio);
435		}
436
437		dio->size += n;
438		copied += n;
439
440		nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter,
441						 BIO_MAX_VECS);
442		/*
443		 * We can only poll for single bio I/Os.
444		 */
445		if (nr_pages)
446			dio->iocb->ki_flags &= ~IOCB_HIPRI;
447		iomap_dio_submit_bio(iter, dio, bio, pos);
448		pos += n;
449	} while (nr_pages);
450
451	/*
452	 * We need to zeroout the tail of a sub-block write if the extent type
453	 * requires zeroing or the write extends beyond EOF. If we don't zero
454	 * the block tail in the latter case, we can expose stale data via mmap
455	 * reads of the EOF block.
456	 */
457zero_tail:
458	if (need_zeroout ||
459	    ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
460		/* zero out from the end of the write to the end of the block */
461		pad = pos & (fs_block_size - 1);
462		if (pad)
463			ret = iomap_dio_zero(iter, dio, pos,
464					     fs_block_size - pad);
465	}
466out:
467	/* Undo iter limitation to current extent */
468	iov_iter_reexpand(dio->submit.iter, orig_count - copied);
469	if (copied)
470		return copied;
471	return ret;
472}
473
474static loff_t iomap_dio_hole_iter(const struct iomap_iter *iter,
475		struct iomap_dio *dio)
476{
477	loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter);
478
479	dio->size += length;
480	if (!length)
481		return -EFAULT;
482	return length;
483}
484
485static loff_t iomap_dio_inline_iter(const struct iomap_iter *iomi,
486		struct iomap_dio *dio)
 
487{
488	const struct iomap *iomap = &iomi->iomap;
489	struct iov_iter *iter = dio->submit.iter;
490	void *inline_data = iomap_inline_data(iomap, iomi->pos);
491	loff_t length = iomap_length(iomi);
492	loff_t pos = iomi->pos;
493	size_t copied;
494
495	if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
496		return -EIO;
497
498	if (dio->flags & IOMAP_DIO_WRITE) {
499		loff_t size = iomi->inode->i_size;
500
501		if (pos > size)
502			memset(iomap_inline_data(iomap, size), 0, pos - size);
503		copied = copy_from_iter(inline_data, length, iter);
504		if (copied) {
505			if (pos + copied > size)
506				i_size_write(iomi->inode, pos + copied);
507			mark_inode_dirty(iomi->inode);
508		}
509	} else {
510		copied = copy_to_iter(inline_data, length, iter);
511	}
512	dio->size += copied;
513	if (!copied)
514		return -EFAULT;
515	return copied;
516}
517
518static loff_t iomap_dio_iter(const struct iomap_iter *iter,
519		struct iomap_dio *dio)
 
520{
521	switch (iter->iomap.type) {
 
 
522	case IOMAP_HOLE:
523		if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
524			return -EIO;
525		return iomap_dio_hole_iter(iter, dio);
526	case IOMAP_UNWRITTEN:
527		if (!(dio->flags & IOMAP_DIO_WRITE))
528			return iomap_dio_hole_iter(iter, dio);
529		return iomap_dio_bio_iter(iter, dio);
530	case IOMAP_MAPPED:
531		return iomap_dio_bio_iter(iter, dio);
532	case IOMAP_INLINE:
533		return iomap_dio_inline_iter(iter, dio);
534	case IOMAP_DELALLOC:
535		/*
536		 * DIO is not serialised against mmap() access at all, and so
537		 * if the page_mkwrite occurs between the writeback and the
538		 * iomap_iter() call in the DIO path, then it will see the
539		 * DELALLOC block that the page-mkwrite allocated.
540		 */
541		pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
542				    dio->iocb->ki_filp, current->comm);
543		return -EIO;
544	default:
545		WARN_ON_ONCE(1);
546		return -EIO;
547	}
548}
549
550/*
551 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
552 * is being issued as AIO or not.  This allows us to optimise pure data writes
553 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
554 * REQ_FLUSH post write. This is slightly tricky because a single request here
555 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
556 * may be pure data writes. In that case, we still need to do a full data sync
557 * completion.
558 *
559 * When page faults are disabled and @dio_flags includes IOMAP_DIO_PARTIAL,
560 * __iomap_dio_rw can return a partial result if it encounters a non-resident
561 * page in @iter after preparing a transfer.  In that case, the non-resident
562 * pages can be faulted in and the request resumed with @done_before set to the
563 * number of bytes previously transferred.  The request will then complete with
564 * the correct total number of bytes transferred; this is essential for
565 * completing partial requests asynchronously.
566 *
567 * Returns -ENOTBLK In case of a page invalidation invalidation failure for
568 * writes.  The callers needs to fall back to buffered I/O in this case.
569 */
570struct iomap_dio *
571__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
572		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
573		unsigned int dio_flags, void *private, size_t done_before)
574{
 
575	struct inode *inode = file_inode(iocb->ki_filp);
576	struct iomap_iter iomi = {
577		.inode		= inode,
578		.pos		= iocb->ki_pos,
579		.len		= iov_iter_count(iter),
580		.flags		= IOMAP_DIRECT,
581		.private	= private,
582	};
583	bool wait_for_completion =
584		is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
585	struct blk_plug plug;
586	struct iomap_dio *dio;
587	loff_t ret = 0;
588
589	trace_iomap_dio_rw_begin(iocb, iter, dio_flags, done_before);
 
590
591	if (!iomi.len)
592		return NULL;
593
594	dio = kmalloc(sizeof(*dio), GFP_KERNEL);
595	if (!dio)
596		return ERR_PTR(-ENOMEM);
597
598	dio->iocb = iocb;
599	atomic_set(&dio->ref, 1);
600	dio->size = 0;
601	dio->i_size = i_size_read(inode);
602	dio->dops = dops;
603	dio->error = 0;
604	dio->flags = 0;
605	dio->done_before = done_before;
606
607	dio->submit.iter = iter;
608	dio->submit.waiter = current;
609
610	if (iocb->ki_flags & IOCB_NOWAIT)
611		iomi.flags |= IOMAP_NOWAIT;
612
613	if (iocb->ki_flags & IOCB_ATOMIC)
614		iomi.flags |= IOMAP_ATOMIC;
615
616	if (iov_iter_rw(iter) == READ) {
617		/* reads can always complete inline */
618		dio->flags |= IOMAP_DIO_INLINE_COMP;
619
620		if (iomi.pos >= dio->i_size)
621			goto out_free_dio;
622
623		if (user_backed_iter(iter))
624			dio->flags |= IOMAP_DIO_DIRTY;
625
626		ret = kiocb_write_and_wait(iocb, iomi.len);
627		if (ret)
628			goto out_free_dio;
629	} else {
630		iomi.flags |= IOMAP_WRITE;
631		dio->flags |= IOMAP_DIO_WRITE;
632
 
 
 
 
633		/*
634		 * Flag as supporting deferred completions, if the issuer
635		 * groks it. This can avoid a workqueue punt for writes.
636		 * We may later clear this flag if we need to do other IO
637		 * as part of this IO completion.
638		 */
639		if (iocb->ki_flags & IOCB_DIO_CALLER_COMP)
640			dio->flags |= IOMAP_DIO_CALLER_COMP;
 
641
642		if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
 
643			ret = -EAGAIN;
644			if (iomi.pos >= dio->i_size ||
645			    iomi.pos + iomi.len > dio->i_size)
646				goto out_free_dio;
647			iomi.flags |= IOMAP_OVERWRITE_ONLY;
648		}
 
 
649
650		/* for data sync or sync, we need sync completion processing */
651		if (iocb_is_dsync(iocb)) {
652			dio->flags |= IOMAP_DIO_NEED_SYNC;
653
654		       /*
655			* For datasync only writes, we optimistically try using
656			* WRITE_THROUGH for this IO. This flag requires either
657			* FUA writes through the device's write cache, or a
658			* normal write to a device without a volatile write
659			* cache. For the former, Any non-FUA write that occurs
660			* will clear this flag, hence we know before completion
661			* whether a cache flush is necessary.
662			*/
663			if (!(iocb->ki_flags & IOCB_SYNC))
664				dio->flags |= IOMAP_DIO_WRITE_THROUGH;
665		}
666
 
667		/*
668		 * Try to invalidate cache pages for the range we are writing.
669		 * If this invalidation fails, let the caller fall back to
670		 * buffered I/O.
671		 */
672		ret = kiocb_invalidate_pages(iocb, iomi.len);
673		if (ret) {
674			if (ret != -EAGAIN) {
675				trace_iomap_dio_invalidate_fail(inode, iomi.pos,
676								iomi.len);
677				if (iocb->ki_flags & IOCB_ATOMIC) {
678					/*
679					 * folio invalidation failed, maybe
680					 * this is transient, unlock and see if
681					 * the caller tries again.
682					 */
683					ret = -EAGAIN;
684				} else {
685					/* fall back to buffered write */
686					ret = -ENOTBLK;
687				}
688			}
689			goto out_free_dio;
690		}
691
692		if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
693			ret = sb_init_dio_done_wq(inode->i_sb);
694			if (ret < 0)
695				goto out_free_dio;
696		}
697	}
698
699	inode_dio_begin(inode);
700
701	blk_start_plug(&plug);
702	while ((ret = iomap_iter(&iomi, ops)) > 0) {
703		iomi.processed = iomap_dio_iter(&iomi, dio);
704
705		/*
706		 * We can only poll for single bio I/Os.
707		 */
708		iocb->ki_flags &= ~IOCB_HIPRI;
709	}
 
 
 
 
710
 
 
 
 
 
 
 
 
 
 
711	blk_finish_plug(&plug);
712
713	/*
714	 * We only report that we've read data up to i_size.
715	 * Revert iter to a state corresponding to that as some callers (such
716	 * as the splice code) rely on it.
717	 */
718	if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size)
719		iov_iter_revert(iter, iomi.pos - dio->i_size);
720
721	if (ret == -EFAULT && dio->size && (dio_flags & IOMAP_DIO_PARTIAL)) {
722		if (!(iocb->ki_flags & IOCB_NOWAIT))
723			wait_for_completion = true;
724		ret = 0;
725	}
726
727	/* magic error code to fall back to buffered I/O */
728	if (ret == -ENOTBLK) {
729		wait_for_completion = true;
730		ret = 0;
731	}
732	if (ret < 0)
733		iomap_dio_set_error(dio, ret);
734
735	/*
736	 * If all the writes we issued were already written through to the
737	 * media, we don't need to flush the cache on IO completion. Clear the
738	 * sync flag for this case.
739	 */
740	if (dio->flags & IOMAP_DIO_WRITE_THROUGH)
741		dio->flags &= ~IOMAP_DIO_NEED_SYNC;
742
 
 
 
743	/*
744	 * We are about to drop our additional submission reference, which
745	 * might be the last reference to the dio.  There are three different
746	 * ways we can progress here:
747	 *
748	 *  (a) If this is the last reference we will always complete and free
749	 *	the dio ourselves.
750	 *  (b) If this is not the last reference, and we serve an asynchronous
751	 *	iocb, we must never touch the dio after the decrement, the
752	 *	I/O completion handler will complete and free it.
753	 *  (c) If this is not the last reference, but we serve a synchronous
754	 *	iocb, the I/O completion handler will wake us up on the drop
755	 *	of the final reference, and we will complete and free it here
756	 *	after we got woken by the I/O completion handler.
757	 */
758	dio->wait_for_completion = wait_for_completion;
759	if (!atomic_dec_and_test(&dio->ref)) {
760		if (!wait_for_completion) {
761			trace_iomap_dio_rw_queued(inode, iomi.pos, iomi.len);
762			return ERR_PTR(-EIOCBQUEUED);
763		}
764
765		for (;;) {
766			set_current_state(TASK_UNINTERRUPTIBLE);
767			if (!READ_ONCE(dio->submit.waiter))
768				break;
769
770			blk_io_schedule();
 
 
 
 
771		}
772		__set_current_state(TASK_RUNNING);
773	}
774
775	return dio;
776
777out_free_dio:
778	kfree(dio);
779	if (ret)
780		return ERR_PTR(ret);
781	return NULL;
782}
783EXPORT_SYMBOL_GPL(__iomap_dio_rw);
784
785ssize_t
786iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
787		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
788		unsigned int dio_flags, void *private, size_t done_before)
789{
790	struct iomap_dio *dio;
791
792	dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags, private,
793			     done_before);
794	if (IS_ERR_OR_NULL(dio))
795		return PTR_ERR_OR_ZERO(dio);
796	return iomap_dio_complete(dio);
797}
798EXPORT_SYMBOL_GPL(iomap_dio_rw);
799
800static int __init iomap_dio_init(void)
801{
802	zero_page = alloc_pages(GFP_KERNEL | __GFP_ZERO,
803				IOMAP_ZERO_PAGE_ORDER);
804
805	if (!zero_page)
806		return -ENOMEM;
807
808	return 0;
809}
810fs_initcall(iomap_dio_init);