Blame view

fs/iomap/direct-io.c 17 KB
db074436f   Darrick J. Wong   iomap: move the d...
1
2
3
4
5
6
7
8
  // SPDX-License-Identifier: GPL-2.0
  /*
   * Copyright (C) 2010 Red Hat, Inc.
   * Copyright (c) 2016-2018 Christoph Hellwig.
   */
  #include <linux/module.h>
  #include <linux/compiler.h>
  #include <linux/fs.h>
141f59b91   Eric Biggers   ANDROID: ext4, f2...
9
  #include <linux/fscrypt.h>
db074436f   Darrick J. Wong   iomap: move the d...
10
11
12
13
  #include <linux/iomap.h>
  #include <linux/backing-dev.h>
  #include <linux/uio.h>
  #include <linux/task_io_accounting_ops.h>
60263d588   Christoph Hellwig   iomap: fall back ...
14
  #include "trace.h"
db074436f   Darrick J. Wong   iomap: move the d...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  
  #include "../internal.h"
  
  /*
   * Private flags for iomap_dio, must not overlap with the public ones in
   * iomap.h:
   */
  #define IOMAP_DIO_WRITE_FUA	(1 << 28)
  #define IOMAP_DIO_NEED_SYNC	(1 << 29)
  #define IOMAP_DIO_WRITE		(1 << 30)
  #define IOMAP_DIO_DIRTY		(1 << 31)
  
  struct iomap_dio {
  	struct kiocb		*iocb;
838c4f3d7   Christoph Hellwig   iomap: move the i...
29
  	const struct iomap_dio_ops *dops;
db074436f   Darrick J. Wong   iomap: move the d...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  	loff_t			i_size;
  	loff_t			size;
  	atomic_t		ref;
  	unsigned		flags;
  	int			error;
  	bool			wait_for_completion;
  
  	union {
  		/* used during submission and for synchronous completion: */
  		struct {
  			struct iov_iter		*iter;
  			struct task_struct	*waiter;
  			struct request_queue	*last_queue;
  			blk_qc_t		cookie;
  		} submit;
  
  		/* used for aio completion: */
  		struct {
  			struct work_struct	work;
  		} aio;
  	};
  };
  
  int iomap_dio_iopoll(struct kiocb *kiocb, bool spin)
  {
  	struct request_queue *q = READ_ONCE(kiocb->private);
  
  	if (!q)
  		return 0;
  	return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin);
  }
  EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
  
  static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,
8cecd0ba8   Goldwyn Rodrigues   iomap: add a file...
64
  		struct bio *bio, loff_t pos)
db074436f   Darrick J. Wong   iomap: move the d...
65
66
67
68
69
70
71
  {
  	atomic_inc(&dio->ref);
  
  	if (dio->iocb->ki_flags & IOCB_HIPRI)
  		bio_set_polled(bio, dio->iocb);
  
  	dio->submit.last_queue = bdev_get_queue(iomap->bdev);
8cecd0ba8   Goldwyn Rodrigues   iomap: add a file...
72
73
74
75
76
77
  	if (dio->dops && dio->dops->submit_io)
  		dio->submit.cookie = dio->dops->submit_io(
  				file_inode(dio->iocb->ki_filp),
  				iomap, bio, pos);
  	else
  		dio->submit.cookie = submit_bio(bio);
db074436f   Darrick J. Wong   iomap: move the d...
78
  }
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
79
  ssize_t iomap_dio_complete(struct iomap_dio *dio)
db074436f   Darrick J. Wong   iomap: move the d...
80
  {
838c4f3d7   Christoph Hellwig   iomap: move the i...
81
  	const struct iomap_dio_ops *dops = dio->dops;
db074436f   Darrick J. Wong   iomap: move the d...
82
83
84
  	struct kiocb *iocb = dio->iocb;
  	struct inode *inode = file_inode(iocb->ki_filp);
  	loff_t offset = iocb->ki_pos;
838c4f3d7   Christoph Hellwig   iomap: move the i...
85
  	ssize_t ret = dio->error;
db074436f   Darrick J. Wong   iomap: move the d...
86

838c4f3d7   Christoph Hellwig   iomap: move the i...
87
88
  	if (dops && dops->end_io)
  		ret = dops->end_io(iocb, dio->size, ret, dio->flags);
db074436f   Darrick J. Wong   iomap: move the d...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  
  	if (likely(!ret)) {
  		ret = dio->size;
  		/* check for short read */
  		if (offset + ret > dio->i_size &&
  		    !(dio->flags & IOMAP_DIO_WRITE))
  			ret = dio->i_size - offset;
  		iocb->ki_pos += ret;
  	}
  
  	/*
  	 * Try again to invalidate clean pages which might have been cached by
  	 * non-direct readahead, or faulted in by get_user_pages() if the source
  	 * of the write was an mmap'ed region of the file we're writing.  Either
  	 * one is a pretty crazy thing to do, so we don't support it 100%.  If
  	 * this invalidation fails, tough, the write still worked...
  	 *
838c4f3d7   Christoph Hellwig   iomap: move the i...
106
107
108
  	 * And this page cache invalidation has to be after ->end_io(), as some
  	 * filesystems convert unwritten extents to real allocations in
  	 * ->end_io() when necessary, otherwise a racing buffer read would cache
db074436f   Darrick J. Wong   iomap: move the d...
109
110
  	 * zeros from unwritten extents.
  	 */
c114bbc6c   Andreas Gruenbacher   iomap: Fix direct...
111
  	if (!dio->error && dio->size &&
db074436f   Darrick J. Wong   iomap: move the d...
112
113
114
115
116
117
118
119
  	    (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
  		int err;
  		err = invalidate_inode_pages2_range(inode->i_mapping,
  				offset >> PAGE_SHIFT,
  				(offset + dio->size - 1) >> PAGE_SHIFT);
  		if (err)
  			dio_warn_stale_pagecache(iocb->ki_filp);
  	}
1a31182ed   Goldwyn Rodrigues   iomap: Call inode...
120
  	inode_dio_end(file_inode(iocb->ki_filp));
db074436f   Darrick J. Wong   iomap: move the d...
121
122
123
124
125
126
  	/*
  	 * If this is a DSYNC write, make sure we push it to stable storage now
  	 * that we've written data.
  	 */
  	if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
  		ret = generic_write_sync(iocb, ret);
db074436f   Darrick J. Wong   iomap: move the d...
127
128
129
130
  	kfree(dio);
  
  	return ret;
  }
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
131
  EXPORT_SYMBOL_GPL(iomap_dio_complete);
db074436f   Darrick J. Wong   iomap: move the d...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  
  static void iomap_dio_complete_work(struct work_struct *work)
  {
  	struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
  	struct kiocb *iocb = dio->iocb;
  
  	iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
  }
  
  /*
   * Set an error in the dio if none is set yet.  We have to use cmpxchg
   * as the submission context and the completion context(s) can race to
   * update the error.
   */
  static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
  {
  	cmpxchg(&dio->error, 0, ret);
  }
  
  static void iomap_dio_bio_end_io(struct bio *bio)
  {
  	struct iomap_dio *dio = bio->bi_private;
  	bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
  
  	if (bio->bi_status)
  		iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
  
  	if (atomic_dec_and_test(&dio->ref)) {
  		if (dio->wait_for_completion) {
  			struct task_struct *waiter = dio->submit.waiter;
  			WRITE_ONCE(dio->submit.waiter, NULL);
  			blk_wake_io_task(waiter);
  		} else if (dio->flags & IOMAP_DIO_WRITE) {
  			struct inode *inode = file_inode(dio->iocb->ki_filp);
  
  			INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
  			queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
  		} else {
  			iomap_dio_complete_work(&dio->aio.work);
  		}
  	}
  
  	if (should_dirty) {
  		bio_check_pages_dirty(bio);
  	} else {
  		bio_release_pages(bio, false);
  		bio_put(bio);
  	}
  }
  
  static void
  iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
  		unsigned len)
  {
141f59b91   Eric Biggers   ANDROID: ext4, f2...
186
  	struct inode *inode = file_inode(dio->iocb->ki_filp);
db074436f   Darrick J. Wong   iomap: move the d...
187
188
189
190
191
  	struct page *page = ZERO_PAGE(0);
  	int flags = REQ_SYNC | REQ_IDLE;
  	struct bio *bio;
  
  	bio = bio_alloc(GFP_KERNEL, 1);
141f59b91   Eric Biggers   ANDROID: ext4, f2...
192
193
  	fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
  				  GFP_KERNEL);
db074436f   Darrick J. Wong   iomap: move the d...
194
195
196
197
198
199
200
201
  	bio_set_dev(bio, iomap->bdev);
  	bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
  	bio->bi_private = dio;
  	bio->bi_end_io = iomap_dio_bio_end_io;
  
  	get_page(page);
  	__bio_add_page(bio, page, len, 0);
  	bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
8cecd0ba8   Goldwyn Rodrigues   iomap: add a file...
202
  	iomap_dio_submit_bio(dio, iomap, bio, pos);
db074436f   Darrick J. Wong   iomap: move the d...
203
204
205
206
207
208
209
210
211
  }
  
  static loff_t
  iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
  		struct iomap_dio *dio, struct iomap *iomap)
  {
  	unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
  	unsigned int fs_block_size = i_blocksize(inode), pad;
  	unsigned int align = iov_iter_alignment(dio->submit.iter);
db074436f   Darrick J. Wong   iomap: move the d...
212
213
214
215
216
  	struct bio *bio;
  	bool need_zeroout = false;
  	bool use_fua = false;
  	int nr_pages, ret = 0;
  	size_t copied = 0;
f550ee9b8   Jan Kara   iomap: Do not cre...
217
  	size_t orig_count;
db074436f   Darrick J. Wong   iomap: move the d...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  
  	if ((pos | length | align) & ((1 << blkbits) - 1))
  		return -EINVAL;
  
  	if (iomap->type == IOMAP_UNWRITTEN) {
  		dio->flags |= IOMAP_DIO_UNWRITTEN;
  		need_zeroout = true;
  	}
  
  	if (iomap->flags & IOMAP_F_SHARED)
  		dio->flags |= IOMAP_DIO_COW;
  
  	if (iomap->flags & IOMAP_F_NEW) {
  		need_zeroout = true;
  	} else if (iomap->type == IOMAP_MAPPED) {
  		/*
  		 * Use a FUA write if we need datasync semantics, this is a pure
  		 * data IO that doesn't require any metadata updates (including
  		 * after IO completion such as unwritten extent conversion) and
  		 * the underlying device supports FUA. This allows us to avoid
  		 * cache flushes on IO completion.
  		 */
  		if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
  		    (dio->flags & IOMAP_DIO_WRITE_FUA) &&
  		    blk_queue_fua(bdev_get_queue(iomap->bdev)))
  			use_fua = true;
  	}
  
  	/*
f550ee9b8   Jan Kara   iomap: Do not cre...
247
248
249
  	 * Save the original count and trim the iter to just the extent we
  	 * are operating on right now.  The iter will be re-expanded once
  	 * we are done.
db074436f   Darrick J. Wong   iomap: move the d...
250
  	 */
f550ee9b8   Jan Kara   iomap: Do not cre...
251
252
  	orig_count = iov_iter_count(dio->submit.iter);
  	iov_iter_truncate(dio->submit.iter, length);
db074436f   Darrick J. Wong   iomap: move the d...
253

f550ee9b8   Jan Kara   iomap: Do not cre...
254
255
256
257
258
  	nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
  	if (nr_pages <= 0) {
  		ret = nr_pages;
  		goto out;
  	}
8d6c90c9d   Eric Biggers   ANDROID: fscrypt:...
259
  	nr_pages = fscrypt_limit_dio_pages(inode, pos, nr_pages);
db074436f   Darrick J. Wong   iomap: move the d...
260
261
262
263
264
265
266
267
268
269
270
271
  
  	if (need_zeroout) {
  		/* zero out from the start of the block to the write offset */
  		pad = pos & (fs_block_size - 1);
  		if (pad)
  			iomap_dio_zero(dio, iomap, pos - pad, pad);
  	}
  
  	do {
  		size_t n;
  		if (dio->error) {
  			iov_iter_revert(dio->submit.iter, copied);
f550ee9b8   Jan Kara   iomap: Do not cre...
272
273
  			copied = ret = 0;
  			goto out;
db074436f   Darrick J. Wong   iomap: move the d...
274
275
276
  		}
  
  		bio = bio_alloc(GFP_KERNEL, nr_pages);
141f59b91   Eric Biggers   ANDROID: ext4, f2...
277
278
  		fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
  					  GFP_KERNEL);
db074436f   Darrick J. Wong   iomap: move the d...
279
280
281
282
283
284
  		bio_set_dev(bio, iomap->bdev);
  		bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
  		bio->bi_write_hint = dio->iocb->ki_hint;
  		bio->bi_ioprio = dio->iocb->ki_ioprio;
  		bio->bi_private = dio;
  		bio->bi_end_io = iomap_dio_bio_end_io;
f550ee9b8   Jan Kara   iomap: Do not cre...
285
  		ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
db074436f   Darrick J. Wong   iomap: move the d...
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  		if (unlikely(ret)) {
  			/*
  			 * We have to stop part way through an IO. We must fall
  			 * through to the sub-block tail zeroing here, otherwise
  			 * this short IO may expose stale data in the tail of
  			 * the block we haven't written data to.
  			 */
  			bio_put(bio);
  			goto zero_tail;
  		}
  
  		n = bio->bi_iter.bi_size;
  		if (dio->flags & IOMAP_DIO_WRITE) {
  			bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
  			if (use_fua)
  				bio->bi_opf |= REQ_FUA;
  			else
  				dio->flags &= ~IOMAP_DIO_WRITE_FUA;
  			task_io_account_write(n);
  		} else {
  			bio->bi_opf = REQ_OP_READ;
  			if (dio->flags & IOMAP_DIO_DIRTY)
  				bio_set_pages_dirty(bio);
  		}
db074436f   Darrick J. Wong   iomap: move the d...
310
  		dio->size += n;
db074436f   Darrick J. Wong   iomap: move the d...
311
  		copied += n;
f550ee9b8   Jan Kara   iomap: Do not cre...
312
  		nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
8d6c90c9d   Eric Biggers   ANDROID: fscrypt:...
313
  		nr_pages = fscrypt_limit_dio_pages(inode, pos, nr_pages);
8cecd0ba8   Goldwyn Rodrigues   iomap: add a file...
314
315
  		iomap_dio_submit_bio(dio, iomap, bio, pos);
  		pos += n;
db074436f   Darrick J. Wong   iomap: move the d...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  	} while (nr_pages);
  
  	/*
  	 * We need to zeroout the tail of a sub-block write if the extent type
  	 * requires zeroing or the write extends beyond EOF. If we don't zero
  	 * the block tail in the latter case, we can expose stale data via mmap
  	 * reads of the EOF block.
  	 */
  zero_tail:
  	if (need_zeroout ||
  	    ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
  		/* zero out from the end of the write to the end of the block */
  		pad = pos & (fs_block_size - 1);
  		if (pad)
  			iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
  	}
f550ee9b8   Jan Kara   iomap: Do not cre...
332
333
334
  out:
  	/* Undo iter limitation to current extent */
  	iov_iter_reexpand(dio->submit.iter, orig_count - copied);
e9f930ac8   Jan Stancek   iomap: fix return...
335
336
337
  	if (copied)
  		return copied;
  	return ret;
db074436f   Darrick J. Wong   iomap: move the d...
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  }
  
  static loff_t
  iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
  {
  	length = iov_iter_zero(length, dio->submit.iter);
  	dio->size += length;
  	return length;
  }
  
  static loff_t
  iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
  		struct iomap_dio *dio, struct iomap *iomap)
  {
  	struct iov_iter *iter = dio->submit.iter;
  	size_t copied;
  
  	BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
  
  	if (dio->flags & IOMAP_DIO_WRITE) {
  		loff_t size = inode->i_size;
  
  		if (pos > size)
  			memset(iomap->inline_data + size, 0, pos - size);
  		copied = copy_from_iter(iomap->inline_data + pos, length, iter);
  		if (copied) {
  			if (pos + copied > size)
  				i_size_write(inode, pos + copied);
  			mark_inode_dirty(inode);
  		}
  	} else {
  		copied = copy_to_iter(iomap->inline_data + pos, length, iter);
  	}
  	dio->size += copied;
  	return copied;
  }
  
  static loff_t
  iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
c039b9979   Goldwyn Rodrigues   iomap: use a srcm...
377
  		void *data, struct iomap *iomap, struct iomap *srcmap)
db074436f   Darrick J. Wong   iomap: move the d...
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  {
  	struct iomap_dio *dio = data;
  
  	switch (iomap->type) {
  	case IOMAP_HOLE:
  		if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
  			return -EIO;
  		return iomap_dio_hole_actor(length, dio);
  	case IOMAP_UNWRITTEN:
  		if (!(dio->flags & IOMAP_DIO_WRITE))
  			return iomap_dio_hole_actor(length, dio);
  		return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
  	case IOMAP_MAPPED:
  		return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
  	case IOMAP_INLINE:
  		return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
a805c1116   Qian Cai   iomap: fix WARN_O...
394
395
396
397
398
399
400
401
402
403
404
  	case IOMAP_DELALLOC:
  		/*
  		 * DIO is not serialised against mmap() access at all, and so
  		 * if the page_mkwrite occurs between the writeback and the
  		 * iomap_apply() call in the DIO path, then it will see the
  		 * DELALLOC block that the page-mkwrite allocated.
  		 */
  		pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s
  ",
  				    dio->iocb->ki_filp, current->comm);
  		return -EIO;
db074436f   Darrick J. Wong   iomap: move the d...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  	default:
  		WARN_ON_ONCE(1);
  		return -EIO;
  	}
  }
  
  /*
   * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
   * is being issued as AIO or not.  This allows us to optimise pure data writes
   * to use REQ_FUA rather than requiring generic_write_sync() to issue a
   * REQ_FLUSH post write. This is slightly tricky because a single request here
   * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
   * may be pure data writes. In that case, we still need to do a full data sync
   * completion.
60263d588   Christoph Hellwig   iomap: fall back ...
419
420
421
   *
   * Returns -ENOTBLK In case of a page invalidation invalidation failure for
   * writes.  The callers needs to fall back to buffered I/O in this case.
db074436f   Darrick J. Wong   iomap: move the d...
422
   */
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
423
424
  struct iomap_dio *
  __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
13ef95444   Jan Kara   iomap: Allow forc...
425
426
  		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
  		bool wait_for_completion)
db074436f   Darrick J. Wong   iomap: move the d...
427
428
429
430
  {
  	struct address_space *mapping = iocb->ki_filp->f_mapping;
  	struct inode *inode = file_inode(iocb->ki_filp);
  	size_t count = iov_iter_count(iter);
88cfd30e1   Johannes Thumshirn   iomap: remove unn...
431
  	loff_t pos = iocb->ki_pos;
db074436f   Darrick J. Wong   iomap: move the d...
432
433
  	loff_t end = iocb->ki_pos + count - 1, ret = 0;
  	unsigned int flags = IOMAP_DIRECT;
db074436f   Darrick J. Wong   iomap: move the d...
434
435
  	struct blk_plug plug;
  	struct iomap_dio *dio;
db074436f   Darrick J. Wong   iomap: move the d...
436
  	if (!count)
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
437
  		return NULL;
db074436f   Darrick J. Wong   iomap: move the d...
438

13ef95444   Jan Kara   iomap: Allow forc...
439
  	if (WARN_ON(is_sync_kiocb(iocb) && !wait_for_completion))
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
440
  		return ERR_PTR(-EIO);
13ef95444   Jan Kara   iomap: Allow forc...
441

db074436f   Darrick J. Wong   iomap: move the d...
442
443
  	dio = kmalloc(sizeof(*dio), GFP_KERNEL);
  	if (!dio)
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
444
  		return ERR_PTR(-ENOMEM);
db074436f   Darrick J. Wong   iomap: move the d...
445
446
447
448
449
  
  	dio->iocb = iocb;
  	atomic_set(&dio->ref, 1);
  	dio->size = 0;
  	dio->i_size = i_size_read(inode);
838c4f3d7   Christoph Hellwig   iomap: move the i...
450
  	dio->dops = dops;
db074436f   Darrick J. Wong   iomap: move the d...
451
452
453
454
455
456
457
458
459
460
461
  	dio->error = 0;
  	dio->flags = 0;
  
  	dio->submit.iter = iter;
  	dio->submit.waiter = current;
  	dio->submit.cookie = BLK_QC_T_NONE;
  	dio->submit.last_queue = NULL;
  
  	if (iov_iter_rw(iter) == READ) {
  		if (pos >= dio->i_size)
  			goto out_free_dio;
a90100421   Joseph Qi   fs/iomap: remove ...
462
  		if (iter_is_iovec(iter))
db074436f   Darrick J. Wong   iomap: move the d...
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  			dio->flags |= IOMAP_DIO_DIRTY;
  	} else {
  		flags |= IOMAP_WRITE;
  		dio->flags |= IOMAP_DIO_WRITE;
  
  		/* for data sync or sync, we need sync completion processing */
  		if (iocb->ki_flags & IOCB_DSYNC)
  			dio->flags |= IOMAP_DIO_NEED_SYNC;
  
  		/*
  		 * For datasync only writes, we optimistically try using FUA for
  		 * this IO.  Any non-FUA write that occurs will clear this flag,
  		 * hence we know before completion whether a cache flush is
  		 * necessary.
  		 */
  		if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
  			dio->flags |= IOMAP_DIO_WRITE_FUA;
  	}
  
  	if (iocb->ki_flags & IOCB_NOWAIT) {
88cfd30e1   Johannes Thumshirn   iomap: remove unn...
483
  		if (filemap_range_has_page(mapping, pos, end)) {
db074436f   Darrick J. Wong   iomap: move the d...
484
485
486
487
488
  			ret = -EAGAIN;
  			goto out_free_dio;
  		}
  		flags |= IOMAP_NOWAIT;
  	}
88cfd30e1   Johannes Thumshirn   iomap: remove unn...
489
  	ret = filemap_write_and_wait_range(mapping, pos, end);
db074436f   Darrick J. Wong   iomap: move the d...
490
491
  	if (ret)
  		goto out_free_dio;
54752de92   Dave Chinner   iomap: Only inval...
492
493
494
  	if (iov_iter_rw(iter) == WRITE) {
  		/*
  		 * Try to invalidate cache pages for the range we are writing.
60263d588   Christoph Hellwig   iomap: fall back ...
495
496
  		 * If this invalidation fails, let the caller fall back to
  		 * buffered I/O.
54752de92   Dave Chinner   iomap: Only inval...
497
498
  		 */
  		if (invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
60263d588   Christoph Hellwig   iomap: fall back ...
499
500
501
  				end >> PAGE_SHIFT)) {
  			trace_iomap_dio_invalidate_fail(inode, pos, count);
  			ret = -ENOTBLK;
db074436f   Darrick J. Wong   iomap: move the d...
502
  			goto out_free_dio;
60263d588   Christoph Hellwig   iomap: fall back ...
503
  		}
db074436f   Darrick J. Wong   iomap: move the d...
504

54752de92   Dave Chinner   iomap: Only inval...
505
506
507
508
509
  		if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
  			ret = sb_init_dio_done_wq(inode->i_sb);
  			if (ret < 0)
  				goto out_free_dio;
  		}
db074436f   Darrick J. Wong   iomap: move the d...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  	}
  
  	inode_dio_begin(inode);
  
  	blk_start_plug(&plug);
  	do {
  		ret = iomap_apply(inode, pos, count, flags, ops, dio,
  				iomap_dio_actor);
  		if (ret <= 0) {
  			/* magic error code to fall back to buffered I/O */
  			if (ret == -ENOTBLK) {
  				wait_for_completion = true;
  				ret = 0;
  			}
  			break;
  		}
  		pos += ret;
419e9c38a   Jan Kara   iomap: Fix pipe p...
527
528
529
530
531
532
533
  		if (iov_iter_rw(iter) == READ && pos >= dio->i_size) {
  			/*
  			 * We only report that we've read data up to i_size.
  			 * Revert iter to a state corresponding to that as
  			 * some callers (such as splice code) rely on it.
  			 */
  			iov_iter_revert(iter, pos - dio->i_size);
db074436f   Darrick J. Wong   iomap: move the d...
534
  			break;
419e9c38a   Jan Kara   iomap: Fix pipe p...
535
  		}
db074436f   Darrick J. Wong   iomap: move the d...
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  	} while ((count = iov_iter_count(iter)) > 0);
  	blk_finish_plug(&plug);
  
  	if (ret < 0)
  		iomap_dio_set_error(dio, ret);
  
  	/*
  	 * If all the writes we issued were FUA, we don't need to flush the
  	 * cache on IO completion. Clear the sync flag for this case.
  	 */
  	if (dio->flags & IOMAP_DIO_WRITE_FUA)
  		dio->flags &= ~IOMAP_DIO_NEED_SYNC;
  
  	WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie);
  	WRITE_ONCE(iocb->private, dio->submit.last_queue);
  
  	/*
  	 * We are about to drop our additional submission reference, which
d9973ce2f   yangerkun   iomap: fix commen...
554
555
  	 * might be the last reference to the dio.  There are three different
  	 * ways we can progress here:
db074436f   Darrick J. Wong   iomap: move the d...
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  	 *
  	 *  (a) If this is the last reference we will always complete and free
  	 *	the dio ourselves.
  	 *  (b) If this is not the last reference, and we serve an asynchronous
  	 *	iocb, we must never touch the dio after the decrement, the
  	 *	I/O completion handler will complete and free it.
  	 *  (c) If this is not the last reference, but we serve a synchronous
  	 *	iocb, the I/O completion handler will wake us up on the drop
  	 *	of the final reference, and we will complete and free it here
  	 *	after we got woken by the I/O completion handler.
  	 */
  	dio->wait_for_completion = wait_for_completion;
  	if (!atomic_dec_and_test(&dio->ref)) {
  		if (!wait_for_completion)
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
570
  			return ERR_PTR(-EIOCBQUEUED);
db074436f   Darrick J. Wong   iomap: move the d...
571
572
573
574
575
576
577
578
579
580
  
  		for (;;) {
  			set_current_state(TASK_UNINTERRUPTIBLE);
  			if (!READ_ONCE(dio->submit.waiter))
  				break;
  
  			if (!(iocb->ki_flags & IOCB_HIPRI) ||
  			    !dio->submit.last_queue ||
  			    !blk_poll(dio->submit.last_queue,
  					 dio->submit.cookie, true))
e6249cdd4   Ming Lei   block: add blk_io...
581
  				blk_io_schedule();
db074436f   Darrick J. Wong   iomap: move the d...
582
583
584
  		}
  		__set_current_state(TASK_RUNNING);
  	}
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
585
  	return dio;
db074436f   Darrick J. Wong   iomap: move the d...
586
587
588
  
  out_free_dio:
  	kfree(dio);
c3d4ed1ab   Christoph Hellwig   iomap: Allow file...
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  	if (ret)
  		return ERR_PTR(ret);
  	return NULL;
  }
  EXPORT_SYMBOL_GPL(__iomap_dio_rw);
  
  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)
  {
  	struct iomap_dio *dio;
  
  	dio = __iomap_dio_rw(iocb, iter, ops, dops, wait_for_completion);
  	if (IS_ERR_OR_NULL(dio))
  		return PTR_ERR_OR_ZERO(dio);
  	return iomap_dio_complete(dio);
db074436f   Darrick J. Wong   iomap: move the d...
606
607
  }
  EXPORT_SYMBOL_GPL(iomap_dio_rw);