iomap: Switch from blkno to disk offset
[linux-2.6-block.git] / fs / iomap.c
CommitLineData
ae259a9c
CH
1/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 * Copyright (c) 2016 Christoph Hellwig.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14#include <linux/module.h>
15#include <linux/compiler.h>
16#include <linux/fs.h>
17#include <linux/iomap.h>
18#include <linux/uaccess.h>
19#include <linux/gfp.h>
20#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/pagemap.h>
23#include <linux/file.h>
24#include <linux/uio.h>
25#include <linux/backing-dev.h>
26#include <linux/buffer_head.h>
ff6a9292 27#include <linux/task_io_accounting_ops.h>
9a286f0e 28#include <linux/dax.h>
f361bf4a
IM
29#include <linux/sched/signal.h>
30
ae259a9c
CH
31#include "internal.h"
32
ae259a9c
CH
33/*
34 * Execute a iomap write on a segment of the mapping that spans a
35 * contiguous range of pages that have identical block mapping state.
36 *
37 * This avoids the need to map pages individually, do individual allocations
38 * for each page and most importantly avoid the need for filesystem specific
39 * locking per page. Instead, all the operations are amortised over the entire
40 * range of pages. It is assumed that the filesystems will lock whatever
41 * resources they require in the iomap_begin call, and release them in the
42 * iomap_end call.
43 */
befb503c 44loff_t
ae259a9c 45iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,
8ff6daa1 46 const struct iomap_ops *ops, void *data, iomap_actor_t actor)
ae259a9c
CH
47{
48 struct iomap iomap = { 0 };
49 loff_t written = 0, ret;
50
51 /*
52 * Need to map a range from start position for length bytes. This can
53 * span multiple pages - it is only guaranteed to return a range of a
54 * single type of pages (e.g. all into a hole, all mapped or all
55 * unwritten). Failure at this point has nothing to undo.
56 *
57 * If allocation is required for this range, reserve the space now so
58 * that the allocation is guaranteed to succeed later on. Once we copy
59 * the data into the page cache pages, then we cannot fail otherwise we
60 * expose transient stale data. If the reserve fails, we can safely
61 * back out at this point as there is nothing to undo.
62 */
63 ret = ops->iomap_begin(inode, pos, length, flags, &iomap);
64 if (ret)
65 return ret;
66 if (WARN_ON(iomap.offset > pos))
67 return -EIO;
68
69 /*
70 * Cut down the length to the one actually provided by the filesystem,
71 * as it might not be able to give us the whole size that we requested.
72 */
73 if (iomap.offset + iomap.length < pos + length)
74 length = iomap.offset + iomap.length - pos;
75
76 /*
77 * Now that we have guaranteed that the space allocation will succeed.
78 * we can do the copy-in page by page without having to worry about
79 * failures exposing transient data.
80 */
81 written = actor(inode, pos, length, data, &iomap);
82
83 /*
84 * Now the data has been copied, commit the range we've copied. This
85 * should not fail unless the filesystem has had a fatal error.
86 */
f20ac7ab
CH
87 if (ops->iomap_end) {
88 ret = ops->iomap_end(inode, pos, length,
89 written > 0 ? written : 0,
90 flags, &iomap);
91 }
ae259a9c
CH
92
93 return written ? written : ret;
94}
95
96static void
97iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
98{
99 loff_t i_size = i_size_read(inode);
100
101 /*
102 * Only truncate newly allocated pages beyoned EOF, even if the
103 * write started inside the existing inode size.
104 */
105 if (pos + len > i_size)
106 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
107}
108
109static int
110iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
111 struct page **pagep, struct iomap *iomap)
112{
113 pgoff_t index = pos >> PAGE_SHIFT;
114 struct page *page;
115 int status = 0;
116
117 BUG_ON(pos + len > iomap->offset + iomap->length);
118
d1908f52
MH
119 if (fatal_signal_pending(current))
120 return -EINTR;
121
ae259a9c
CH
122 page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
123 if (!page)
124 return -ENOMEM;
125
126 status = __block_write_begin_int(page, pos, len, NULL, iomap);
127 if (unlikely(status)) {
128 unlock_page(page);
129 put_page(page);
130 page = NULL;
131
132 iomap_write_failed(inode, pos, len);
133 }
134
135 *pagep = page;
136 return status;
137}
138
139static int
140iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
141 unsigned copied, struct page *page)
142{
143 int ret;
144
145 ret = generic_write_end(NULL, inode->i_mapping, pos, len,
146 copied, page, NULL);
147 if (ret < len)
148 iomap_write_failed(inode, pos, len);
149 return ret;
150}
151
152static loff_t
153iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
154 struct iomap *iomap)
155{
156 struct iov_iter *i = data;
157 long status = 0;
158 ssize_t written = 0;
159 unsigned int flags = AOP_FLAG_NOFS;
160
ae259a9c
CH
161 do {
162 struct page *page;
163 unsigned long offset; /* Offset into pagecache page */
164 unsigned long bytes; /* Bytes to write to page */
165 size_t copied; /* Bytes copied from user */
166
167 offset = (pos & (PAGE_SIZE - 1));
168 bytes = min_t(unsigned long, PAGE_SIZE - offset,
169 iov_iter_count(i));
170again:
171 if (bytes > length)
172 bytes = length;
173
174 /*
175 * Bring in the user page that we will copy from _first_.
176 * Otherwise there's a nasty deadlock on copying from the
177 * same page as we're writing to, without it being marked
178 * up-to-date.
179 *
180 * Not only is this an optimisation, but it is also required
181 * to check that the address is actually valid, when atomic
182 * usercopies are used, below.
183 */
184 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
185 status = -EFAULT;
186 break;
187 }
188
189 status = iomap_write_begin(inode, pos, bytes, flags, &page,
190 iomap);
191 if (unlikely(status))
192 break;
193
194 if (mapping_writably_mapped(inode->i_mapping))
195 flush_dcache_page(page);
196
ae259a9c 197 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
ae259a9c
CH
198
199 flush_dcache_page(page);
ae259a9c
CH
200
201 status = iomap_write_end(inode, pos, bytes, copied, page);
202 if (unlikely(status < 0))
203 break;
204 copied = status;
205
206 cond_resched();
207
208 iov_iter_advance(i, copied);
209 if (unlikely(copied == 0)) {
210 /*
211 * If we were unable to copy any data at all, we must
212 * fall back to a single segment length write.
213 *
214 * If we didn't fallback here, we could livelock
215 * because not all segments in the iov can be copied at
216 * once without a pagefault.
217 */
218 bytes = min_t(unsigned long, PAGE_SIZE - offset,
219 iov_iter_single_seg_count(i));
220 goto again;
221 }
222 pos += copied;
223 written += copied;
224 length -= copied;
225
226 balance_dirty_pages_ratelimited(inode->i_mapping);
227 } while (iov_iter_count(i) && length);
228
229 return written ? written : status;
230}
231
232ssize_t
233iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
8ff6daa1 234 const struct iomap_ops *ops)
ae259a9c
CH
235{
236 struct inode *inode = iocb->ki_filp->f_mapping->host;
237 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
238
239 while (iov_iter_count(iter)) {
240 ret = iomap_apply(inode, pos, iov_iter_count(iter),
241 IOMAP_WRITE, ops, iter, iomap_write_actor);
242 if (ret <= 0)
243 break;
244 pos += ret;
245 written += ret;
246 }
247
248 return written ? written : ret;
249}
250EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
251
5f4e5752
CH
252static struct page *
253__iomap_read_page(struct inode *inode, loff_t offset)
254{
255 struct address_space *mapping = inode->i_mapping;
256 struct page *page;
257
258 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
259 if (IS_ERR(page))
260 return page;
261 if (!PageUptodate(page)) {
262 put_page(page);
263 return ERR_PTR(-EIO);
264 }
265 return page;
266}
267
268static loff_t
269iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
270 struct iomap *iomap)
271{
272 long status = 0;
273 ssize_t written = 0;
274
275 do {
276 struct page *page, *rpage;
277 unsigned long offset; /* Offset into pagecache page */
278 unsigned long bytes; /* Bytes to write to page */
279
280 offset = (pos & (PAGE_SIZE - 1));
e28ae8e4 281 bytes = min_t(loff_t, PAGE_SIZE - offset, length);
5f4e5752
CH
282
283 rpage = __iomap_read_page(inode, pos);
284 if (IS_ERR(rpage))
285 return PTR_ERR(rpage);
286
287 status = iomap_write_begin(inode, pos, bytes,
c718a975 288 AOP_FLAG_NOFS, &page, iomap);
5f4e5752
CH
289 put_page(rpage);
290 if (unlikely(status))
291 return status;
292
293 WARN_ON_ONCE(!PageUptodate(page));
294
295 status = iomap_write_end(inode, pos, bytes, bytes, page);
296 if (unlikely(status <= 0)) {
297 if (WARN_ON_ONCE(status == 0))
298 return -EIO;
299 return status;
300 }
301
302 cond_resched();
303
304 pos += status;
305 written += status;
306 length -= status;
307
308 balance_dirty_pages_ratelimited(inode->i_mapping);
309 } while (length);
310
311 return written;
312}
313
314int
315iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
8ff6daa1 316 const struct iomap_ops *ops)
5f4e5752
CH
317{
318 loff_t ret;
319
320 while (len) {
321 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
322 iomap_dirty_actor);
323 if (ret <= 0)
324 return ret;
325 pos += ret;
326 len -= ret;
327 }
328
329 return 0;
330}
331EXPORT_SYMBOL_GPL(iomap_file_dirty);
332
ae259a9c
CH
333static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
334 unsigned bytes, struct iomap *iomap)
335{
336 struct page *page;
337 int status;
338
c718a975
TH
339 status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
340 iomap);
ae259a9c
CH
341 if (status)
342 return status;
343
344 zero_user(page, offset, bytes);
345 mark_page_accessed(page);
346
347 return iomap_write_end(inode, pos, bytes, bytes, page);
348}
349
9a286f0e
CH
350static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
351 struct iomap *iomap)
352{
19fe5f64
AG
353 sector_t sector = (iomap->addr +
354 (pos & PAGE_MASK) - iomap->offset) >> 9;
9a286f0e 355
cccbce67
DW
356 return __dax_zero_page_range(iomap->bdev, iomap->dax_dev, sector,
357 offset, bytes);
9a286f0e
CH
358}
359
ae259a9c
CH
360static loff_t
361iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
362 void *data, struct iomap *iomap)
363{
364 bool *did_zero = data;
365 loff_t written = 0;
366 int status;
367
368 /* already zeroed? we're done. */
369 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
370 return count;
371
372 do {
373 unsigned offset, bytes;
374
375 offset = pos & (PAGE_SIZE - 1); /* Within page */
e28ae8e4 376 bytes = min_t(loff_t, PAGE_SIZE - offset, count);
ae259a9c 377
9a286f0e
CH
378 if (IS_DAX(inode))
379 status = iomap_dax_zero(pos, offset, bytes, iomap);
380 else
381 status = iomap_zero(inode, pos, offset, bytes, iomap);
ae259a9c
CH
382 if (status < 0)
383 return status;
384
385 pos += bytes;
386 count -= bytes;
387 written += bytes;
388 if (did_zero)
389 *did_zero = true;
390 } while (count > 0);
391
392 return written;
393}
394
395int
396iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
8ff6daa1 397 const struct iomap_ops *ops)
ae259a9c
CH
398{
399 loff_t ret;
400
401 while (len > 0) {
402 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
403 ops, did_zero, iomap_zero_range_actor);
404 if (ret <= 0)
405 return ret;
406
407 pos += ret;
408 len -= ret;
409 }
410
411 return 0;
412}
413EXPORT_SYMBOL_GPL(iomap_zero_range);
414
415int
416iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
8ff6daa1 417 const struct iomap_ops *ops)
ae259a9c 418{
93407472
FF
419 unsigned int blocksize = i_blocksize(inode);
420 unsigned int off = pos & (blocksize - 1);
ae259a9c
CH
421
422 /* Block boundary? Nothing to do */
423 if (!off)
424 return 0;
425 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
426}
427EXPORT_SYMBOL_GPL(iomap_truncate_page);
428
429static loff_t
430iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
431 void *data, struct iomap *iomap)
432{
433 struct page *page = data;
434 int ret;
435
c663e29f 436 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
ae259a9c
CH
437 if (ret)
438 return ret;
439
440 block_commit_write(page, 0, length);
441 return length;
442}
443
11bac800 444int iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
ae259a9c
CH
445{
446 struct page *page = vmf->page;
11bac800 447 struct inode *inode = file_inode(vmf->vma->vm_file);
ae259a9c
CH
448 unsigned long length;
449 loff_t offset, size;
450 ssize_t ret;
451
452 lock_page(page);
453 size = i_size_read(inode);
454 if ((page->mapping != inode->i_mapping) ||
455 (page_offset(page) > size)) {
456 /* We overload EFAULT to mean page got truncated */
457 ret = -EFAULT;
458 goto out_unlock;
459 }
460
461 /* page is wholly or partially inside EOF */
462 if (((page->index + 1) << PAGE_SHIFT) > size)
463 length = size & ~PAGE_MASK;
464 else
465 length = PAGE_SIZE;
466
467 offset = page_offset(page);
468 while (length > 0) {
9484ab1b
JK
469 ret = iomap_apply(inode, offset, length,
470 IOMAP_WRITE | IOMAP_FAULT, ops, page,
471 iomap_page_mkwrite_actor);
ae259a9c
CH
472 if (unlikely(ret <= 0))
473 goto out_unlock;
474 offset += ret;
475 length -= ret;
476 }
477
478 set_page_dirty(page);
479 wait_for_stable_page(page);
e7647fb4 480 return VM_FAULT_LOCKED;
ae259a9c
CH
481out_unlock:
482 unlock_page(page);
e7647fb4 483 return block_page_mkwrite_return(ret);
ae259a9c
CH
484}
485EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
8be9f564
CH
486
487struct fiemap_ctx {
488 struct fiemap_extent_info *fi;
489 struct iomap prev;
490};
491
492static int iomap_to_fiemap(struct fiemap_extent_info *fi,
493 struct iomap *iomap, u32 flags)
494{
495 switch (iomap->type) {
496 case IOMAP_HOLE:
497 /* skip holes */
498 return 0;
499 case IOMAP_DELALLOC:
500 flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
501 break;
502 case IOMAP_UNWRITTEN:
503 flags |= FIEMAP_EXTENT_UNWRITTEN;
504 break;
505 case IOMAP_MAPPED:
506 break;
507 }
508
17de0a9f
CH
509 if (iomap->flags & IOMAP_F_MERGED)
510 flags |= FIEMAP_EXTENT_MERGED;
e43c460d
DW
511 if (iomap->flags & IOMAP_F_SHARED)
512 flags |= FIEMAP_EXTENT_SHARED;
17de0a9f 513
8be9f564 514 return fiemap_fill_next_extent(fi, iomap->offset,
19fe5f64 515 iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
17de0a9f 516 iomap->length, flags);
8be9f564
CH
517}
518
519static loff_t
520iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
521 struct iomap *iomap)
522{
523 struct fiemap_ctx *ctx = data;
524 loff_t ret = length;
525
526 if (iomap->type == IOMAP_HOLE)
527 return length;
528
529 ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
530 ctx->prev = *iomap;
531 switch (ret) {
532 case 0: /* success */
533 return length;
534 case 1: /* extent array full */
535 return 0;
536 default:
537 return ret;
538 }
539}
540
541int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
8ff6daa1 542 loff_t start, loff_t len, const struct iomap_ops *ops)
8be9f564
CH
543{
544 struct fiemap_ctx ctx;
545 loff_t ret;
546
547 memset(&ctx, 0, sizeof(ctx));
548 ctx.fi = fi;
549 ctx.prev.type = IOMAP_HOLE;
550
551 ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
552 if (ret)
553 return ret;
554
8896b8f6
DC
555 if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
556 ret = filemap_write_and_wait(inode->i_mapping);
557 if (ret)
558 return ret;
559 }
8be9f564
CH
560
561 while (len > 0) {
d33fd776 562 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
8be9f564 563 iomap_fiemap_actor);
ac2dc058
DC
564 /* inode with no (attribute) mapping will give ENOENT */
565 if (ret == -ENOENT)
566 break;
8be9f564
CH
567 if (ret < 0)
568 return ret;
569 if (ret == 0)
570 break;
571
572 start += ret;
573 len -= ret;
574 }
575
576 if (ctx.prev.type != IOMAP_HOLE) {
577 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
578 if (ret < 0)
579 return ret;
580 }
581
582 return 0;
583}
584EXPORT_SYMBOL_GPL(iomap_fiemap);
ff6a9292 585
0ed3b0d4
AG
586static loff_t
587iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length,
588 void *data, struct iomap *iomap)
589{
590 switch (iomap->type) {
591 case IOMAP_UNWRITTEN:
592 offset = page_cache_seek_hole_data(inode, offset, length,
593 SEEK_HOLE);
594 if (offset < 0)
595 return length;
596 /* fall through */
597 case IOMAP_HOLE:
598 *(loff_t *)data = offset;
599 return 0;
600 default:
601 return length;
602 }
603}
604
605loff_t
606iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
607{
608 loff_t size = i_size_read(inode);
609 loff_t length = size - offset;
610 loff_t ret;
611
d6ab17f2
DW
612 /* Nothing to be found before or beyond the end of the file. */
613 if (offset < 0 || offset >= size)
0ed3b0d4
AG
614 return -ENXIO;
615
616 while (length > 0) {
617 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
618 &offset, iomap_seek_hole_actor);
619 if (ret < 0)
620 return ret;
621 if (ret == 0)
622 break;
623
624 offset += ret;
625 length -= ret;
626 }
627
628 return offset;
629}
630EXPORT_SYMBOL_GPL(iomap_seek_hole);
631
632static loff_t
633iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length,
634 void *data, struct iomap *iomap)
635{
636 switch (iomap->type) {
637 case IOMAP_HOLE:
638 return length;
639 case IOMAP_UNWRITTEN:
640 offset = page_cache_seek_hole_data(inode, offset, length,
641 SEEK_DATA);
642 if (offset < 0)
643 return length;
644 /*FALLTHRU*/
645 default:
646 *(loff_t *)data = offset;
647 return 0;
648 }
649}
650
651loff_t
652iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
653{
654 loff_t size = i_size_read(inode);
655 loff_t length = size - offset;
656 loff_t ret;
657
d6ab17f2
DW
658 /* Nothing to be found before or beyond the end of the file. */
659 if (offset < 0 || offset >= size)
0ed3b0d4
AG
660 return -ENXIO;
661
662 while (length > 0) {
663 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
664 &offset, iomap_seek_data_actor);
665 if (ret < 0)
666 return ret;
667 if (ret == 0)
668 break;
669
670 offset += ret;
671 length -= ret;
672 }
673
674 if (length <= 0)
675 return -ENXIO;
676 return offset;
677}
678EXPORT_SYMBOL_GPL(iomap_seek_data);
679
ff6a9292
CH
680/*
681 * Private flags for iomap_dio, must not overlap with the public ones in
682 * iomap.h:
683 */
684#define IOMAP_DIO_WRITE (1 << 30)
685#define IOMAP_DIO_DIRTY (1 << 31)
686
687struct iomap_dio {
688 struct kiocb *iocb;
689 iomap_dio_end_io_t *end_io;
690 loff_t i_size;
691 loff_t size;
692 atomic_t ref;
693 unsigned flags;
694 int error;
695
696 union {
697 /* used during submission and for synchronous completion: */
698 struct {
699 struct iov_iter *iter;
700 struct task_struct *waiter;
701 struct request_queue *last_queue;
702 blk_qc_t cookie;
703 } submit;
704
705 /* used for aio completion: */
706 struct {
707 struct work_struct work;
708 } aio;
709 };
710};
711
712static ssize_t iomap_dio_complete(struct iomap_dio *dio)
713{
714 struct kiocb *iocb = dio->iocb;
332391a9 715 struct inode *inode = file_inode(iocb->ki_filp);
ff6a9292
CH
716 ssize_t ret;
717
332391a9
LC
718 /*
719 * Try again to invalidate clean pages which might have been cached by
720 * non-direct readahead, or faulted in by get_user_pages() if the source
721 * of the write was an mmap'ed region of the file we're writing. Either
722 * one is a pretty crazy thing to do, so we don't support it 100%. If
723 * this invalidation fails, tough, the write still worked...
724 */
725 if (!dio->error &&
726 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
727 ret = invalidate_inode_pages2_range(inode->i_mapping,
728 iocb->ki_pos >> PAGE_SHIFT,
729 (iocb->ki_pos + dio->size - 1) >> PAGE_SHIFT);
730 WARN_ON_ONCE(ret);
731 }
732
ff6a9292
CH
733 if (dio->end_io) {
734 ret = dio->end_io(iocb,
735 dio->error ? dio->error : dio->size,
736 dio->flags);
737 } else {
738 ret = dio->error;
739 }
740
741 if (likely(!ret)) {
742 ret = dio->size;
743 /* check for short read */
744 if (iocb->ki_pos + ret > dio->i_size &&
745 !(dio->flags & IOMAP_DIO_WRITE))
746 ret = dio->i_size - iocb->ki_pos;
747 iocb->ki_pos += ret;
748 }
749
750 inode_dio_end(file_inode(iocb->ki_filp));
751 kfree(dio);
752
753 return ret;
754}
755
756static void iomap_dio_complete_work(struct work_struct *work)
757{
758 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
759 struct kiocb *iocb = dio->iocb;
760 bool is_write = (dio->flags & IOMAP_DIO_WRITE);
761 ssize_t ret;
762
763 ret = iomap_dio_complete(dio);
764 if (is_write && ret > 0)
765 ret = generic_write_sync(iocb, ret);
766 iocb->ki_complete(iocb, ret, 0);
767}
768
769/*
770 * Set an error in the dio if none is set yet. We have to use cmpxchg
771 * as the submission context and the completion context(s) can race to
772 * update the error.
773 */
774static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
775{
776 cmpxchg(&dio->error, 0, ret);
777}
778
779static void iomap_dio_bio_end_io(struct bio *bio)
780{
781 struct iomap_dio *dio = bio->bi_private;
782 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
783
4e4cbee9
CH
784 if (bio->bi_status)
785 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
ff6a9292
CH
786
787 if (atomic_dec_and_test(&dio->ref)) {
788 if (is_sync_kiocb(dio->iocb)) {
789 struct task_struct *waiter = dio->submit.waiter;
790
791 WRITE_ONCE(dio->submit.waiter, NULL);
792 wake_up_process(waiter);
793 } else if (dio->flags & IOMAP_DIO_WRITE) {
794 struct inode *inode = file_inode(dio->iocb->ki_filp);
795
796 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
797 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
798 } else {
799 iomap_dio_complete_work(&dio->aio.work);
800 }
801 }
802
803 if (should_dirty) {
804 bio_check_pages_dirty(bio);
805 } else {
806 struct bio_vec *bvec;
807 int i;
808
809 bio_for_each_segment_all(bvec, bio, i)
810 put_page(bvec->bv_page);
811 bio_put(bio);
812 }
813}
814
815static blk_qc_t
816iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
817 unsigned len)
818{
819 struct page *page = ZERO_PAGE(0);
820 struct bio *bio;
821
822 bio = bio_alloc(GFP_KERNEL, 1);
74d46992 823 bio_set_dev(bio, iomap->bdev);
ff6a9292 824 bio->bi_iter.bi_sector =
19fe5f64 825 (iomap->addr + pos - iomap->offset) >> 9;
ff6a9292
CH
826 bio->bi_private = dio;
827 bio->bi_end_io = iomap_dio_bio_end_io;
828
829 get_page(page);
830 if (bio_add_page(bio, page, len, 0) != len)
831 BUG();
5cc60aee 832 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE);
ff6a9292
CH
833
834 atomic_inc(&dio->ref);
835 return submit_bio(bio);
836}
837
838static loff_t
839iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
840 void *data, struct iomap *iomap)
841{
842 struct iomap_dio *dio = data;
93407472
FF
843 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
844 unsigned int fs_block_size = i_blocksize(inode), pad;
845 unsigned int align = iov_iter_alignment(dio->submit.iter);
ff6a9292
CH
846 struct iov_iter iter;
847 struct bio *bio;
848 bool need_zeroout = false;
849 int nr_pages, ret;
850
851 if ((pos | length | align) & ((1 << blkbits) - 1))
852 return -EINVAL;
853
854 switch (iomap->type) {
855 case IOMAP_HOLE:
856 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
857 return -EIO;
858 /*FALLTHRU*/
859 case IOMAP_UNWRITTEN:
860 if (!(dio->flags & IOMAP_DIO_WRITE)) {
861 iov_iter_zero(length, dio->submit.iter);
862 dio->size += length;
863 return length;
864 }
865 dio->flags |= IOMAP_DIO_UNWRITTEN;
866 need_zeroout = true;
867 break;
868 case IOMAP_MAPPED:
869 if (iomap->flags & IOMAP_F_SHARED)
870 dio->flags |= IOMAP_DIO_COW;
871 if (iomap->flags & IOMAP_F_NEW)
872 need_zeroout = true;
873 break;
874 default:
875 WARN_ON_ONCE(1);
876 return -EIO;
877 }
878
879 /*
880 * Operate on a partial iter trimmed to the extent we were called for.
881 * We'll update the iter in the dio once we're done with this extent.
882 */
883 iter = *dio->submit.iter;
884 iov_iter_truncate(&iter, length);
885
886 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
887 if (nr_pages <= 0)
888 return nr_pages;
889
890 if (need_zeroout) {
891 /* zero out from the start of the block to the write offset */
892 pad = pos & (fs_block_size - 1);
893 if (pad)
894 iomap_dio_zero(dio, iomap, pos - pad, pad);
895 }
896
897 do {
898 if (dio->error)
899 return 0;
900
901 bio = bio_alloc(GFP_KERNEL, nr_pages);
74d46992 902 bio_set_dev(bio, iomap->bdev);
ff6a9292 903 bio->bi_iter.bi_sector =
19fe5f64 904 (iomap->addr + pos - iomap->offset) >> 9;
45d06cf7 905 bio->bi_write_hint = dio->iocb->ki_hint;
ff6a9292
CH
906 bio->bi_private = dio;
907 bio->bi_end_io = iomap_dio_bio_end_io;
908
909 ret = bio_iov_iter_get_pages(bio, &iter);
910 if (unlikely(ret)) {
911 bio_put(bio);
912 return ret;
913 }
914
915 if (dio->flags & IOMAP_DIO_WRITE) {
5cc60aee 916 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE);
ff6a9292
CH
917 task_io_account_write(bio->bi_iter.bi_size);
918 } else {
919 bio_set_op_attrs(bio, REQ_OP_READ, 0);
920 if (dio->flags & IOMAP_DIO_DIRTY)
921 bio_set_pages_dirty(bio);
922 }
923
924 dio->size += bio->bi_iter.bi_size;
925 pos += bio->bi_iter.bi_size;
926
927 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
928
929 atomic_inc(&dio->ref);
930
931 dio->submit.last_queue = bdev_get_queue(iomap->bdev);
932 dio->submit.cookie = submit_bio(bio);
933 } while (nr_pages);
934
935 if (need_zeroout) {
936 /* zero out from the end of the write to the end of the block */
937 pad = pos & (fs_block_size - 1);
938 if (pad)
939 iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
940 }
941
942 iov_iter_advance(dio->submit.iter, length);
943 return length;
944}
945
946ssize_t
8ff6daa1
CH
947iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
948 const struct iomap_ops *ops, iomap_dio_end_io_t end_io)
ff6a9292
CH
949{
950 struct address_space *mapping = iocb->ki_filp->f_mapping;
951 struct inode *inode = file_inode(iocb->ki_filp);
952 size_t count = iov_iter_count(iter);
c771c14b
EG
953 loff_t pos = iocb->ki_pos, start = pos;
954 loff_t end = iocb->ki_pos + count - 1, ret = 0;
ff6a9292
CH
955 unsigned int flags = IOMAP_DIRECT;
956 struct blk_plug plug;
957 struct iomap_dio *dio;
958
959 lockdep_assert_held(&inode->i_rwsem);
960
961 if (!count)
962 return 0;
963
964 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
965 if (!dio)
966 return -ENOMEM;
967
968 dio->iocb = iocb;
969 atomic_set(&dio->ref, 1);
970 dio->size = 0;
971 dio->i_size = i_size_read(inode);
972 dio->end_io = end_io;
973 dio->error = 0;
974 dio->flags = 0;
975
976 dio->submit.iter = iter;
977 if (is_sync_kiocb(iocb)) {
978 dio->submit.waiter = current;
979 dio->submit.cookie = BLK_QC_T_NONE;
980 dio->submit.last_queue = NULL;
981 }
982
983 if (iov_iter_rw(iter) == READ) {
984 if (pos >= dio->i_size)
985 goto out_free_dio;
986
987 if (iter->type == ITER_IOVEC)
988 dio->flags |= IOMAP_DIO_DIRTY;
989 } else {
990 dio->flags |= IOMAP_DIO_WRITE;
991 flags |= IOMAP_WRITE;
992 }
993
a38d1243
GR
994 if (iocb->ki_flags & IOCB_NOWAIT) {
995 if (filemap_range_has_page(mapping, start, end)) {
996 ret = -EAGAIN;
997 goto out_free_dio;
998 }
999 flags |= IOMAP_NOWAIT;
1000 }
1001
55635ba7
AR
1002 ret = filemap_write_and_wait_range(mapping, start, end);
1003 if (ret)
1004 goto out_free_dio;
ff6a9292 1005
55635ba7
AR
1006 ret = invalidate_inode_pages2_range(mapping,
1007 start >> PAGE_SHIFT, end >> PAGE_SHIFT);
1008 WARN_ON_ONCE(ret);
1009 ret = 0;
ff6a9292 1010
546e7be8
CR
1011 if (iov_iter_rw(iter) == WRITE && !is_sync_kiocb(iocb) &&
1012 !inode->i_sb->s_dio_done_wq) {
1013 ret = sb_init_dio_done_wq(inode->i_sb);
1014 if (ret < 0)
1015 goto out_free_dio;
1016 }
1017
ff6a9292
CH
1018 inode_dio_begin(inode);
1019
1020 blk_start_plug(&plug);
1021 do {
1022 ret = iomap_apply(inode, pos, count, flags, ops, dio,
1023 iomap_dio_actor);
1024 if (ret <= 0) {
1025 /* magic error code to fall back to buffered I/O */
1026 if (ret == -ENOTBLK)
1027 ret = 0;
1028 break;
1029 }
1030 pos += ret;
a008c31c
CR
1031
1032 if (iov_iter_rw(iter) == READ && pos >= dio->i_size)
1033 break;
ff6a9292
CH
1034 } while ((count = iov_iter_count(iter)) > 0);
1035 blk_finish_plug(&plug);
1036
1037 if (ret < 0)
1038 iomap_dio_set_error(dio, ret);
1039
ff6a9292
CH
1040 if (!atomic_dec_and_test(&dio->ref)) {
1041 if (!is_sync_kiocb(iocb))
1042 return -EIOCBQUEUED;
1043
1044 for (;;) {
1045 set_current_state(TASK_UNINTERRUPTIBLE);
1046 if (!READ_ONCE(dio->submit.waiter))
1047 break;
1048
1049 if (!(iocb->ki_flags & IOCB_HIPRI) ||
1050 !dio->submit.last_queue ||
5cc60aee
LT
1051 !blk_mq_poll(dio->submit.last_queue,
1052 dio->submit.cookie))
ff6a9292
CH
1053 io_schedule();
1054 }
1055 __set_current_state(TASK_RUNNING);
1056 }
1057
c771c14b
EG
1058 ret = iomap_dio_complete(dio);
1059
c771c14b 1060 return ret;
ff6a9292
CH
1061
1062out_free_dio:
1063 kfree(dio);
1064 return ret;
1065}
1066EXPORT_SYMBOL_GPL(iomap_dio_rw);