]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/iomap/buffered-io.c
iomap: warn on inline maps in iomap_writepage_map
[mirror_ubuntu-hirsute-kernel.git] / fs / iomap / buffered-io.c
CommitLineData
afc51aaa
DW
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
598ecfba 4 * Copyright (C) 2016-2019 Christoph Hellwig.
afc51aaa
DW
5 */
6#include <linux/module.h>
7#include <linux/compiler.h>
8#include <linux/fs.h>
9#include <linux/iomap.h>
10#include <linux/pagemap.h>
11#include <linux/uio.h>
12#include <linux/buffer_head.h>
13#include <linux/dax.h>
14#include <linux/writeback.h>
598ecfba 15#include <linux/list_sort.h>
afc51aaa
DW
16#include <linux/swap.h>
17#include <linux/bio.h>
18#include <linux/sched/signal.h>
19#include <linux/migrate.h>
9e91c572 20#include "trace.h"
afc51aaa
DW
21
22#include "../internal.h"
23
598ecfba
CH
24static struct bio_set iomap_ioend_bioset;
25
afc51aaa
DW
26static struct iomap_page *
27iomap_page_create(struct inode *inode, struct page *page)
28{
29 struct iomap_page *iop = to_iomap_page(page);
30
31 if (iop || i_blocksize(inode) == PAGE_SIZE)
32 return iop;
33
34 iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL);
35 atomic_set(&iop->read_count, 0);
36 atomic_set(&iop->write_count, 0);
37 bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE);
38
39 /*
40 * migrate_page_move_mapping() assumes that pages with private data have
41 * their count elevated by 1.
42 */
43 get_page(page);
44 set_page_private(page, (unsigned long)iop);
45 SetPagePrivate(page);
46 return iop;
47}
48
49static void
50iomap_page_release(struct page *page)
51{
52 struct iomap_page *iop = to_iomap_page(page);
53
54 if (!iop)
55 return;
56 WARN_ON_ONCE(atomic_read(&iop->read_count));
57 WARN_ON_ONCE(atomic_read(&iop->write_count));
58 ClearPagePrivate(page);
59 set_page_private(page, 0);
60 put_page(page);
61 kfree(iop);
62}
63
64/*
65 * Calculate the range inside the page that we actually need to read.
66 */
67static void
68iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
69 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
70{
71 loff_t orig_pos = *pos;
72 loff_t isize = i_size_read(inode);
73 unsigned block_bits = inode->i_blkbits;
74 unsigned block_size = (1 << block_bits);
75 unsigned poff = offset_in_page(*pos);
76 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
77 unsigned first = poff >> block_bits;
78 unsigned last = (poff + plen - 1) >> block_bits;
79
80 /*
81 * If the block size is smaller than the page size we need to check the
82 * per-block uptodate status and adjust the offset and length if needed
83 * to avoid reading in already uptodate ranges.
84 */
85 if (iop) {
86 unsigned int i;
87
88 /* move forward for each leading block marked uptodate */
89 for (i = first; i <= last; i++) {
90 if (!test_bit(i, iop->uptodate))
91 break;
92 *pos += block_size;
93 poff += block_size;
94 plen -= block_size;
95 first++;
96 }
97
98 /* truncate len if we find any trailing uptodate block(s) */
99 for ( ; i <= last; i++) {
100 if (test_bit(i, iop->uptodate)) {
101 plen -= (last - i + 1) * block_size;
102 last = i - 1;
103 break;
104 }
105 }
106 }
107
108 /*
109 * If the extent spans the block that contains the i_size we need to
110 * handle both halves separately so that we properly zero data in the
111 * page cache for blocks that are entirely outside of i_size.
112 */
113 if (orig_pos <= isize && orig_pos + length > isize) {
114 unsigned end = offset_in_page(isize - 1) >> block_bits;
115
116 if (first <= end && last > end)
117 plen -= (last - end) * block_size;
118 }
119
120 *offp = poff;
121 *lenp = plen;
122}
123
124static void
125iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
126{
127 struct iomap_page *iop = to_iomap_page(page);
128 struct inode *inode = page->mapping->host;
129 unsigned first = off >> inode->i_blkbits;
130 unsigned last = (off + len - 1) >> inode->i_blkbits;
131 unsigned int i;
132 bool uptodate = true;
133
134 if (iop) {
135 for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
136 if (i >= first && i <= last)
137 set_bit(i, iop->uptodate);
138 else if (!test_bit(i, iop->uptodate))
139 uptodate = false;
140 }
141 }
142
143 if (uptodate && !PageError(page))
144 SetPageUptodate(page);
145}
146
147static void
148iomap_read_finish(struct iomap_page *iop, struct page *page)
149{
150 if (!iop || atomic_dec_and_test(&iop->read_count))
151 unlock_page(page);
152}
153
154static void
155iomap_read_page_end_io(struct bio_vec *bvec, int error)
156{
157 struct page *page = bvec->bv_page;
158 struct iomap_page *iop = to_iomap_page(page);
159
160 if (unlikely(error)) {
161 ClearPageUptodate(page);
162 SetPageError(page);
163 } else {
164 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
165 }
166
167 iomap_read_finish(iop, page);
168}
169
170static void
171iomap_read_end_io(struct bio *bio)
172{
173 int error = blk_status_to_errno(bio->bi_status);
174 struct bio_vec *bvec;
175 struct bvec_iter_all iter_all;
176
177 bio_for_each_segment_all(bvec, bio, iter_all)
178 iomap_read_page_end_io(bvec, error);
179 bio_put(bio);
180}
181
182struct iomap_readpage_ctx {
183 struct page *cur_page;
184 bool cur_page_in_bio;
185 bool is_readahead;
186 struct bio *bio;
187 struct list_head *pages;
188};
189
190static void
191iomap_read_inline_data(struct inode *inode, struct page *page,
192 struct iomap *iomap)
193{
194 size_t size = i_size_read(inode);
195 void *addr;
196
197 if (PageUptodate(page))
198 return;
199
200 BUG_ON(page->index);
201 BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
202
203 addr = kmap_atomic(page);
204 memcpy(addr, iomap->inline_data, size);
205 memset(addr + size, 0, PAGE_SIZE - size);
206 kunmap_atomic(addr);
207 SetPageUptodate(page);
208}
209
009d8d84
CH
210static inline bool iomap_block_needs_zeroing(struct inode *inode,
211 struct iomap *iomap, loff_t pos)
212{
213 return iomap->type != IOMAP_MAPPED ||
214 (iomap->flags & IOMAP_F_NEW) ||
215 pos >= i_size_read(inode);
216}
217
afc51aaa
DW
218static loff_t
219iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
220 struct iomap *iomap)
221{
222 struct iomap_readpage_ctx *ctx = data;
223 struct page *page = ctx->cur_page;
224 struct iomap_page *iop = iomap_page_create(inode, page);
225 bool same_page = false, is_contig = false;
226 loff_t orig_pos = pos;
227 unsigned poff, plen;
228 sector_t sector;
229
230 if (iomap->type == IOMAP_INLINE) {
231 WARN_ON_ONCE(pos);
232 iomap_read_inline_data(inode, page, iomap);
233 return PAGE_SIZE;
234 }
235
236 /* zero post-eof blocks as the page may be mapped */
237 iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
238 if (plen == 0)
239 goto done;
240
009d8d84 241 if (iomap_block_needs_zeroing(inode, iomap, pos)) {
afc51aaa
DW
242 zero_user(page, poff, plen);
243 iomap_set_range_uptodate(page, poff, plen);
244 goto done;
245 }
246
247 ctx->cur_page_in_bio = true;
248
249 /*
250 * Try to merge into a previous segment if we can.
251 */
252 sector = iomap_sector(iomap, pos);
253 if (ctx->bio && bio_end_sector(ctx->bio) == sector)
254 is_contig = true;
255
256 if (is_contig &&
257 __bio_try_merge_page(ctx->bio, page, plen, poff, &same_page)) {
258 if (!same_page && iop)
259 atomic_inc(&iop->read_count);
260 goto done;
261 }
262
263 /*
264 * If we start a new segment we need to increase the read count, and we
265 * need to do so before submitting any previous full bio to make sure
266 * that we don't prematurely unlock the page.
267 */
268 if (iop)
269 atomic_inc(&iop->read_count);
270
271 if (!ctx->bio || !is_contig || bio_full(ctx->bio, plen)) {
272 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
273 int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
274
275 if (ctx->bio)
276 submit_bio(ctx->bio);
277
278 if (ctx->is_readahead) /* same as readahead_gfp_mask */
279 gfp |= __GFP_NORETRY | __GFP_NOWARN;
280 ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
281 ctx->bio->bi_opf = REQ_OP_READ;
282 if (ctx->is_readahead)
283 ctx->bio->bi_opf |= REQ_RAHEAD;
284 ctx->bio->bi_iter.bi_sector = sector;
285 bio_set_dev(ctx->bio, iomap->bdev);
286 ctx->bio->bi_end_io = iomap_read_end_io;
287 }
288
289 bio_add_page(ctx->bio, page, plen, poff);
290done:
291 /*
292 * Move the caller beyond our range so that it keeps making progress.
293 * For that we have to include any leading non-uptodate ranges, but
294 * we can skip trailing ones as they will be handled in the next
295 * iteration.
296 */
297 return pos - orig_pos + plen;
298}
299
300int
301iomap_readpage(struct page *page, const struct iomap_ops *ops)
302{
303 struct iomap_readpage_ctx ctx = { .cur_page = page };
304 struct inode *inode = page->mapping->host;
305 unsigned poff;
306 loff_t ret;
307
9e91c572
CH
308 trace_iomap_readpage(page->mapping->host, 1);
309
afc51aaa
DW
310 for (poff = 0; poff < PAGE_SIZE; poff += ret) {
311 ret = iomap_apply(inode, page_offset(page) + poff,
312 PAGE_SIZE - poff, 0, ops, &ctx,
313 iomap_readpage_actor);
314 if (ret <= 0) {
315 WARN_ON_ONCE(ret == 0);
316 SetPageError(page);
317 break;
318 }
319 }
320
321 if (ctx.bio) {
322 submit_bio(ctx.bio);
323 WARN_ON_ONCE(!ctx.cur_page_in_bio);
324 } else {
325 WARN_ON_ONCE(ctx.cur_page_in_bio);
326 unlock_page(page);
327 }
328
329 /*
330 * Just like mpage_readpages and block_read_full_page we always
331 * return 0 and just mark the page as PageError on errors. This
332 * should be cleaned up all through the stack eventually.
333 */
334 return 0;
335}
336EXPORT_SYMBOL_GPL(iomap_readpage);
337
338static struct page *
339iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
340 loff_t length, loff_t *done)
341{
342 while (!list_empty(pages)) {
343 struct page *page = lru_to_page(pages);
344
345 if (page_offset(page) >= (u64)pos + length)
346 break;
347
348 list_del(&page->lru);
349 if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
350 GFP_NOFS))
351 return page;
352
353 /*
354 * If we already have a page in the page cache at index we are
355 * done. Upper layers don't care if it is uptodate after the
356 * readpages call itself as every page gets checked again once
357 * actually needed.
358 */
359 *done += PAGE_SIZE;
360 put_page(page);
361 }
362
363 return NULL;
364}
365
366static loff_t
367iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
368 void *data, struct iomap *iomap)
369{
370 struct iomap_readpage_ctx *ctx = data;
371 loff_t done, ret;
372
373 for (done = 0; done < length; done += ret) {
374 if (ctx->cur_page && offset_in_page(pos + done) == 0) {
375 if (!ctx->cur_page_in_bio)
376 unlock_page(ctx->cur_page);
377 put_page(ctx->cur_page);
378 ctx->cur_page = NULL;
379 }
380 if (!ctx->cur_page) {
381 ctx->cur_page = iomap_next_page(inode, ctx->pages,
382 pos, length, &done);
383 if (!ctx->cur_page)
384 break;
385 ctx->cur_page_in_bio = false;
386 }
387 ret = iomap_readpage_actor(inode, pos + done, length - done,
388 ctx, iomap);
389 }
390
391 return done;
392}
393
394int
395iomap_readpages(struct address_space *mapping, struct list_head *pages,
396 unsigned nr_pages, const struct iomap_ops *ops)
397{
398 struct iomap_readpage_ctx ctx = {
399 .pages = pages,
400 .is_readahead = true,
401 };
402 loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
403 loff_t last = page_offset(list_entry(pages->next, struct page, lru));
404 loff_t length = last - pos + PAGE_SIZE, ret = 0;
405
9e91c572
CH
406 trace_iomap_readpages(mapping->host, nr_pages);
407
afc51aaa
DW
408 while (length > 0) {
409 ret = iomap_apply(mapping->host, pos, length, 0, ops,
410 &ctx, iomap_readpages_actor);
411 if (ret <= 0) {
412 WARN_ON_ONCE(ret == 0);
413 goto done;
414 }
415 pos += ret;
416 length -= ret;
417 }
418 ret = 0;
419done:
420 if (ctx.bio)
421 submit_bio(ctx.bio);
422 if (ctx.cur_page) {
423 if (!ctx.cur_page_in_bio)
424 unlock_page(ctx.cur_page);
425 put_page(ctx.cur_page);
426 }
427
428 /*
429 * Check that we didn't lose a page due to the arcance calling
430 * conventions..
431 */
432 WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
433 return ret;
434}
435EXPORT_SYMBOL_GPL(iomap_readpages);
436
437/*
438 * iomap_is_partially_uptodate checks whether blocks within a page are
439 * uptodate or not.
440 *
441 * Returns true if all blocks which correspond to a file portion
442 * we want to read within the page are uptodate.
443 */
444int
445iomap_is_partially_uptodate(struct page *page, unsigned long from,
446 unsigned long count)
447{
448 struct iomap_page *iop = to_iomap_page(page);
449 struct inode *inode = page->mapping->host;
450 unsigned len, first, last;
451 unsigned i;
452
453 /* Limit range to one page */
454 len = min_t(unsigned, PAGE_SIZE - from, count);
455
456 /* First and last blocks in range within page */
457 first = from >> inode->i_blkbits;
458 last = (from + len - 1) >> inode->i_blkbits;
459
460 if (iop) {
461 for (i = first; i <= last; i++)
462 if (!test_bit(i, iop->uptodate))
463 return 0;
464 return 1;
465 }
466
467 return 0;
468}
469EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
470
471int
472iomap_releasepage(struct page *page, gfp_t gfp_mask)
473{
9e91c572
CH
474 trace_iomap_releasepage(page->mapping->host, page, 0, 0);
475
afc51aaa
DW
476 /*
477 * mm accommodates an old ext3 case where clean pages might not have had
478 * the dirty bit cleared. Thus, it can send actual dirty pages to
479 * ->releasepage() via shrink_active_list(), skip those here.
480 */
481 if (PageDirty(page) || PageWriteback(page))
482 return 0;
483 iomap_page_release(page);
484 return 1;
485}
486EXPORT_SYMBOL_GPL(iomap_releasepage);
487
488void
489iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
490{
9e91c572
CH
491 trace_iomap_invalidatepage(page->mapping->host, page, offset, len);
492
afc51aaa
DW
493 /*
494 * If we are invalidating the entire page, clear the dirty state from it
495 * and release it to avoid unnecessary buildup of the LRU.
496 */
497 if (offset == 0 && len == PAGE_SIZE) {
498 WARN_ON_ONCE(PageWriteback(page));
499 cancel_dirty_page(page);
500 iomap_page_release(page);
501 }
502}
503EXPORT_SYMBOL_GPL(iomap_invalidatepage);
504
505#ifdef CONFIG_MIGRATION
506int
507iomap_migrate_page(struct address_space *mapping, struct page *newpage,
508 struct page *page, enum migrate_mode mode)
509{
510 int ret;
511
26473f83 512 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
afc51aaa
DW
513 if (ret != MIGRATEPAGE_SUCCESS)
514 return ret;
515
516 if (page_has_private(page)) {
517 ClearPagePrivate(page);
518 get_page(newpage);
519 set_page_private(newpage, page_private(page));
520 set_page_private(page, 0);
521 put_page(page);
522 SetPagePrivate(newpage);
523 }
524
525 if (mode != MIGRATE_SYNC_NO_COPY)
526 migrate_page_copy(newpage, page);
527 else
528 migrate_page_states(newpage, page);
529 return MIGRATEPAGE_SUCCESS;
530}
531EXPORT_SYMBOL_GPL(iomap_migrate_page);
532#endif /* CONFIG_MIGRATION */
533
534static void
535iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
536{
537 loff_t i_size = i_size_read(inode);
538
539 /*
540 * Only truncate newly allocated pages beyoned EOF, even if the
541 * write started inside the existing inode size.
542 */
543 if (pos + len > i_size)
544 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
545}
546
547static int
548iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
549 unsigned poff, unsigned plen, unsigned from, unsigned to,
550 struct iomap *iomap)
551{
552 struct bio_vec bvec;
553 struct bio bio;
554
009d8d84 555 if (iomap_block_needs_zeroing(inode, iomap, block_start)) {
afc51aaa
DW
556 zero_user_segments(page, poff, from, to, poff + plen);
557 iomap_set_range_uptodate(page, poff, plen);
558 return 0;
559 }
560
561 bio_init(&bio, &bvec, 1);
562 bio.bi_opf = REQ_OP_READ;
563 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
564 bio_set_dev(&bio, iomap->bdev);
565 __bio_add_page(&bio, page, plen, poff);
566 return submit_bio_wait(&bio);
567}
568
569static int
570__iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
571 struct page *page, struct iomap *iomap)
572{
573 struct iomap_page *iop = iomap_page_create(inode, page);
574 loff_t block_size = i_blocksize(inode);
575 loff_t block_start = pos & ~(block_size - 1);
576 loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
577 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
578 int status = 0;
579
580 if (PageUptodate(page))
581 return 0;
582
583 do {
584 iomap_adjust_read_range(inode, iop, &block_start,
585 block_end - block_start, &poff, &plen);
586 if (plen == 0)
587 break;
588
589 if ((from > poff && from < poff + plen) ||
590 (to > poff && to < poff + plen)) {
591 status = iomap_read_page_sync(inode, block_start, page,
592 poff, plen, from, to, iomap);
593 if (status)
594 break;
595 }
596
597 } while ((block_start += plen) < block_end);
598
599 return status;
600}
601
602static int
603iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
604 struct page **pagep, struct iomap *iomap)
605{
606 const struct iomap_page_ops *page_ops = iomap->page_ops;
607 pgoff_t index = pos >> PAGE_SHIFT;
608 struct page *page;
609 int status = 0;
610
611 BUG_ON(pos + len > iomap->offset + iomap->length);
612
613 if (fatal_signal_pending(current))
614 return -EINTR;
615
616 if (page_ops && page_ops->page_prepare) {
617 status = page_ops->page_prepare(inode, pos, len, iomap);
618 if (status)
619 return status;
620 }
621
622 page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
623 if (!page) {
624 status = -ENOMEM;
625 goto out_no_page;
626 }
627
628 if (iomap->type == IOMAP_INLINE)
629 iomap_read_inline_data(inode, page, iomap);
630 else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
631 status = __block_write_begin_int(page, pos, len, NULL, iomap);
632 else
633 status = __iomap_write_begin(inode, pos, len, page, iomap);
634
635 if (unlikely(status))
636 goto out_unlock;
637
638 *pagep = page;
639 return 0;
640
641out_unlock:
642 unlock_page(page);
643 put_page(page);
644 iomap_write_failed(inode, pos, len);
645
646out_no_page:
647 if (page_ops && page_ops->page_done)
648 page_ops->page_done(inode, pos, 0, NULL, iomap);
649 return status;
650}
651
652int
653iomap_set_page_dirty(struct page *page)
654{
655 struct address_space *mapping = page_mapping(page);
656 int newly_dirty;
657
658 if (unlikely(!mapping))
659 return !TestSetPageDirty(page);
660
661 /*
662 * Lock out page->mem_cgroup migration to keep PageDirty
663 * synchronized with per-memcg dirty page counters.
664 */
665 lock_page_memcg(page);
666 newly_dirty = !TestSetPageDirty(page);
667 if (newly_dirty)
668 __set_page_dirty(page, mapping, 0);
669 unlock_page_memcg(page);
670
671 if (newly_dirty)
672 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
673 return newly_dirty;
674}
675EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
676
677static int
678__iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
679 unsigned copied, struct page *page, struct iomap *iomap)
680{
681 flush_dcache_page(page);
682
683 /*
684 * The blocks that were entirely written will now be uptodate, so we
685 * don't have to worry about a readpage reading them and overwriting a
686 * partial write. However if we have encountered a short write and only
687 * partially written into a block, it will not be marked uptodate, so a
688 * readpage might come in and destroy our partial write.
689 *
690 * Do the simplest thing, and just treat any short write to a non
691 * uptodate page as a zero-length write, and force the caller to redo
692 * the whole thing.
693 */
694 if (unlikely(copied < len && !PageUptodate(page)))
695 return 0;
696 iomap_set_range_uptodate(page, offset_in_page(pos), len);
697 iomap_set_page_dirty(page);
698 return copied;
699}
700
701static int
702iomap_write_end_inline(struct inode *inode, struct page *page,
703 struct iomap *iomap, loff_t pos, unsigned copied)
704{
705 void *addr;
706
707 WARN_ON_ONCE(!PageUptodate(page));
708 BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
709
710 addr = kmap_atomic(page);
711 memcpy(iomap->inline_data + pos, addr + pos, copied);
712 kunmap_atomic(addr);
713
714 mark_inode_dirty(inode);
715 return copied;
716}
717
718static int
719iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
720 unsigned copied, struct page *page, struct iomap *iomap)
721{
722 const struct iomap_page_ops *page_ops = iomap->page_ops;
723 loff_t old_size = inode->i_size;
724 int ret;
725
726 if (iomap->type == IOMAP_INLINE) {
727 ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
728 } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
729 ret = block_write_end(NULL, inode->i_mapping, pos, len, copied,
730 page, NULL);
731 } else {
732 ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
733 }
734
735 /*
736 * Update the in-memory inode size after copying the data into the page
737 * cache. It's up to the file system to write the updated size to disk,
738 * preferably after I/O completion so that no stale data is exposed.
739 */
740 if (pos + ret > old_size) {
741 i_size_write(inode, pos + ret);
742 iomap->flags |= IOMAP_F_SIZE_CHANGED;
743 }
744 unlock_page(page);
745
746 if (old_size < pos)
747 pagecache_isize_extended(inode, old_size, pos);
748 if (page_ops && page_ops->page_done)
749 page_ops->page_done(inode, pos, ret, page, iomap);
750 put_page(page);
751
752 if (ret < len)
753 iomap_write_failed(inode, pos, len);
754 return ret;
755}
756
757static loff_t
758iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
759 struct iomap *iomap)
760{
761 struct iov_iter *i = data;
762 long status = 0;
763 ssize_t written = 0;
764 unsigned int flags = AOP_FLAG_NOFS;
765
766 do {
767 struct page *page;
768 unsigned long offset; /* Offset into pagecache page */
769 unsigned long bytes; /* Bytes to write to page */
770 size_t copied; /* Bytes copied from user */
771
772 offset = offset_in_page(pos);
773 bytes = min_t(unsigned long, PAGE_SIZE - offset,
774 iov_iter_count(i));
775again:
776 if (bytes > length)
777 bytes = length;
778
779 /*
780 * Bring in the user page that we will copy from _first_.
781 * Otherwise there's a nasty deadlock on copying from the
782 * same page as we're writing to, without it being marked
783 * up-to-date.
784 *
785 * Not only is this an optimisation, but it is also required
786 * to check that the address is actually valid, when atomic
787 * usercopies are used, below.
788 */
789 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
790 status = -EFAULT;
791 break;
792 }
793
794 status = iomap_write_begin(inode, pos, bytes, flags, &page,
795 iomap);
796 if (unlikely(status))
797 break;
798
799 if (mapping_writably_mapped(inode->i_mapping))
800 flush_dcache_page(page);
801
802 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
803
804 flush_dcache_page(page);
805
806 status = iomap_write_end(inode, pos, bytes, copied, page,
807 iomap);
808 if (unlikely(status < 0))
809 break;
810 copied = status;
811
812 cond_resched();
813
814 iov_iter_advance(i, copied);
815 if (unlikely(copied == 0)) {
816 /*
817 * If we were unable to copy any data at all, we must
818 * fall back to a single segment length write.
819 *
820 * If we didn't fallback here, we could livelock
821 * because not all segments in the iov can be copied at
822 * once without a pagefault.
823 */
824 bytes = min_t(unsigned long, PAGE_SIZE - offset,
825 iov_iter_single_seg_count(i));
826 goto again;
827 }
828 pos += copied;
829 written += copied;
830 length -= copied;
831
832 balance_dirty_pages_ratelimited(inode->i_mapping);
833 } while (iov_iter_count(i) && length);
834
835 return written ? written : status;
836}
837
838ssize_t
839iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
840 const struct iomap_ops *ops)
841{
842 struct inode *inode = iocb->ki_filp->f_mapping->host;
843 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
844
845 while (iov_iter_count(iter)) {
846 ret = iomap_apply(inode, pos, iov_iter_count(iter),
847 IOMAP_WRITE, ops, iter, iomap_write_actor);
848 if (ret <= 0)
849 break;
850 pos += ret;
851 written += ret;
852 }
853
854 return written ? written : ret;
855}
856EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
857
858static struct page *
859__iomap_read_page(struct inode *inode, loff_t offset)
860{
861 struct address_space *mapping = inode->i_mapping;
862 struct page *page;
863
864 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
865 if (IS_ERR(page))
866 return page;
867 if (!PageUptodate(page)) {
868 put_page(page);
869 return ERR_PTR(-EIO);
870 }
871 return page;
872}
873
874static loff_t
875iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
876 struct iomap *iomap)
877{
878 long status = 0;
879 ssize_t written = 0;
880
881 do {
882 struct page *page, *rpage;
883 unsigned long offset; /* Offset into pagecache page */
884 unsigned long bytes; /* Bytes to write to page */
885
886 offset = offset_in_page(pos);
887 bytes = min_t(loff_t, PAGE_SIZE - offset, length);
888
889 rpage = __iomap_read_page(inode, pos);
890 if (IS_ERR(rpage))
891 return PTR_ERR(rpage);
892
893 status = iomap_write_begin(inode, pos, bytes,
894 AOP_FLAG_NOFS, &page, iomap);
895 put_page(rpage);
896 if (unlikely(status))
897 return status;
898
899 WARN_ON_ONCE(!PageUptodate(page));
900
901 status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
902 if (unlikely(status <= 0)) {
903 if (WARN_ON_ONCE(status == 0))
904 return -EIO;
905 return status;
906 }
907
908 cond_resched();
909
910 pos += status;
911 written += status;
912 length -= status;
913
914 balance_dirty_pages_ratelimited(inode->i_mapping);
915 } while (length);
916
917 return written;
918}
919
920int
921iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
922 const struct iomap_ops *ops)
923{
924 loff_t ret;
925
926 while (len) {
927 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
928 iomap_dirty_actor);
929 if (ret <= 0)
930 return ret;
931 pos += ret;
932 len -= ret;
933 }
934
935 return 0;
936}
937EXPORT_SYMBOL_GPL(iomap_file_dirty);
938
939static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
940 unsigned bytes, struct iomap *iomap)
941{
942 struct page *page;
943 int status;
944
945 status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
946 iomap);
947 if (status)
948 return status;
949
950 zero_user(page, offset, bytes);
951 mark_page_accessed(page);
952
953 return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
954}
955
956static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
957 struct iomap *iomap)
958{
959 return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
960 iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
961}
962
963static loff_t
964iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
965 void *data, struct iomap *iomap)
966{
967 bool *did_zero = data;
968 loff_t written = 0;
969 int status;
970
971 /* already zeroed? we're done. */
972 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
973 return count;
974
975 do {
976 unsigned offset, bytes;
977
978 offset = offset_in_page(pos);
979 bytes = min_t(loff_t, PAGE_SIZE - offset, count);
980
981 if (IS_DAX(inode))
982 status = iomap_dax_zero(pos, offset, bytes, iomap);
983 else
984 status = iomap_zero(inode, pos, offset, bytes, iomap);
985 if (status < 0)
986 return status;
987
988 pos += bytes;
989 count -= bytes;
990 written += bytes;
991 if (did_zero)
992 *did_zero = true;
993 } while (count > 0);
994
995 return written;
996}
997
998int
999iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1000 const struct iomap_ops *ops)
1001{
1002 loff_t ret;
1003
1004 while (len > 0) {
1005 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
1006 ops, did_zero, iomap_zero_range_actor);
1007 if (ret <= 0)
1008 return ret;
1009
1010 pos += ret;
1011 len -= ret;
1012 }
1013
1014 return 0;
1015}
1016EXPORT_SYMBOL_GPL(iomap_zero_range);
1017
1018int
1019iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1020 const struct iomap_ops *ops)
1021{
1022 unsigned int blocksize = i_blocksize(inode);
1023 unsigned int off = pos & (blocksize - 1);
1024
1025 /* Block boundary? Nothing to do */
1026 if (!off)
1027 return 0;
1028 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1029}
1030EXPORT_SYMBOL_GPL(iomap_truncate_page);
1031
1032static loff_t
1033iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
1034 void *data, struct iomap *iomap)
1035{
1036 struct page *page = data;
1037 int ret;
1038
1039 if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
1040 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
1041 if (ret)
1042 return ret;
1043 block_commit_write(page, 0, length);
1044 } else {
1045 WARN_ON_ONCE(!PageUptodate(page));
1046 iomap_page_create(inode, page);
1047 set_page_dirty(page);
1048 }
1049
1050 return length;
1051}
1052
1053vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1054{
1055 struct page *page = vmf->page;
1056 struct inode *inode = file_inode(vmf->vma->vm_file);
1057 unsigned long length;
1058 loff_t offset, size;
1059 ssize_t ret;
1060
1061 lock_page(page);
1062 size = i_size_read(inode);
1063 if ((page->mapping != inode->i_mapping) ||
1064 (page_offset(page) > size)) {
1065 /* We overload EFAULT to mean page got truncated */
1066 ret = -EFAULT;
1067 goto out_unlock;
1068 }
1069
1070 /* page is wholly or partially inside EOF */
1071 if (((page->index + 1) << PAGE_SHIFT) > size)
1072 length = offset_in_page(size);
1073 else
1074 length = PAGE_SIZE;
1075
1076 offset = page_offset(page);
1077 while (length > 0) {
1078 ret = iomap_apply(inode, offset, length,
1079 IOMAP_WRITE | IOMAP_FAULT, ops, page,
1080 iomap_page_mkwrite_actor);
1081 if (unlikely(ret <= 0))
1082 goto out_unlock;
1083 offset += ret;
1084 length -= ret;
1085 }
1086
1087 wait_for_stable_page(page);
1088 return VM_FAULT_LOCKED;
1089out_unlock:
1090 unlock_page(page);
1091 return block_page_mkwrite_return(ret);
1092}
1093EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
598ecfba
CH
1094
1095static void
1096iomap_finish_page_writeback(struct inode *inode, struct bio_vec *bvec,
1097 int error)
1098{
1099 struct iomap_page *iop = to_iomap_page(bvec->bv_page);
1100
1101 if (error) {
1102 SetPageError(bvec->bv_page);
1103 mapping_set_error(inode->i_mapping, -EIO);
1104 }
1105
1106 WARN_ON_ONCE(i_blocksize(inode) < PAGE_SIZE && !iop);
1107 WARN_ON_ONCE(iop && atomic_read(&iop->write_count) <= 0);
1108
1109 if (!iop || atomic_dec_and_test(&iop->write_count))
1110 end_page_writeback(bvec->bv_page);
1111}
1112
1113/*
1114 * We're now finished for good with this ioend structure. Update the page
1115 * state, release holds on bios, and finally free up memory. Do not use the
1116 * ioend after this.
1117 */
1118static void
1119iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1120{
1121 struct inode *inode = ioend->io_inode;
1122 struct bio *bio = &ioend->io_inline_bio;
1123 struct bio *last = ioend->io_bio, *next;
1124 u64 start = bio->bi_iter.bi_sector;
1125 bool quiet = bio_flagged(bio, BIO_QUIET);
1126
1127 for (bio = &ioend->io_inline_bio; bio; bio = next) {
1128 struct bio_vec *bv;
1129 struct bvec_iter_all iter_all;
1130
1131 /*
1132 * For the last bio, bi_private points to the ioend, so we
1133 * need to explicitly end the iteration here.
1134 */
1135 if (bio == last)
1136 next = NULL;
1137 else
1138 next = bio->bi_private;
1139
1140 /* walk each page on bio, ending page IO on them */
1141 bio_for_each_segment_all(bv, bio, iter_all)
1142 iomap_finish_page_writeback(inode, bv, error);
1143 bio_put(bio);
1144 }
1145
1146 if (unlikely(error && !quiet)) {
1147 printk_ratelimited(KERN_ERR
1148 "%s: writeback error on sector %llu",
1149 inode->i_sb->s_id, start);
1150 }
1151}
1152
1153void
1154iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1155{
1156 struct list_head tmp;
1157
1158 list_replace_init(&ioend->io_list, &tmp);
1159 iomap_finish_ioend(ioend, error);
1160
1161 while (!list_empty(&tmp)) {
1162 ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1163 list_del_init(&ioend->io_list);
1164 iomap_finish_ioend(ioend, error);
1165 }
1166}
1167EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1168
1169/*
1170 * We can merge two adjacent ioends if they have the same set of work to do.
1171 */
1172static bool
1173iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1174{
1175 if (ioend->io_bio->bi_status != next->io_bio->bi_status)
1176 return false;
1177 if ((ioend->io_flags & IOMAP_F_SHARED) ^
1178 (next->io_flags & IOMAP_F_SHARED))
1179 return false;
1180 if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1181 (next->io_type == IOMAP_UNWRITTEN))
1182 return false;
1183 if (ioend->io_offset + ioend->io_size != next->io_offset)
1184 return false;
1185 return true;
1186}
1187
1188void
1189iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends,
1190 void (*merge_private)(struct iomap_ioend *ioend,
1191 struct iomap_ioend *next))
1192{
1193 struct iomap_ioend *next;
1194
1195 INIT_LIST_HEAD(&ioend->io_list);
1196
1197 while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1198 io_list))) {
1199 if (!iomap_ioend_can_merge(ioend, next))
1200 break;
1201 list_move_tail(&next->io_list, &ioend->io_list);
1202 ioend->io_size += next->io_size;
1203 if (next->io_private && merge_private)
1204 merge_private(ioend, next);
1205 }
1206}
1207EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1208
1209static int
1210iomap_ioend_compare(void *priv, struct list_head *a, struct list_head *b)
1211{
1212 struct iomap_ioend *ia, *ib;
1213
1214 ia = container_of(a, struct iomap_ioend, io_list);
1215 ib = container_of(b, struct iomap_ioend, io_list);
1216 if (ia->io_offset < ib->io_offset)
1217 return -1;
1218 else if (ia->io_offset > ib->io_offset)
1219 return 1;
1220 return 0;
1221}
1222
1223void
1224iomap_sort_ioends(struct list_head *ioend_list)
1225{
1226 list_sort(NULL, ioend_list, iomap_ioend_compare);
1227}
1228EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1229
1230static void iomap_writepage_end_bio(struct bio *bio)
1231{
1232 struct iomap_ioend *ioend = bio->bi_private;
1233
1234 iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
1235}
1236
1237/*
1238 * Submit the final bio for an ioend.
1239 *
1240 * If @error is non-zero, it means that we have a situation where some part of
1241 * the submission process has failed after we have marked paged for writeback
1242 * and unlocked them. In this situation, we need to fail the bio instead of
1243 * submitting it. This typically only happens on a filesystem shutdown.
1244 */
1245static int
1246iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
1247 int error)
1248{
1249 ioend->io_bio->bi_private = ioend;
1250 ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
1251
1252 if (wpc->ops->prepare_ioend)
1253 error = wpc->ops->prepare_ioend(ioend, error);
1254 if (error) {
1255 /*
1256 * If we are failing the IO now, just mark the ioend with an
1257 * error and finish it. This will run IO completion immediately
1258 * as there is only one reference to the ioend at this point in
1259 * time.
1260 */
1261 ioend->io_bio->bi_status = errno_to_blk_status(error);
1262 bio_endio(ioend->io_bio);
1263 return error;
1264 }
1265
1266 submit_bio(ioend->io_bio);
1267 return 0;
1268}
1269
1270static struct iomap_ioend *
1271iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
1272 loff_t offset, sector_t sector, struct writeback_control *wbc)
1273{
1274 struct iomap_ioend *ioend;
1275 struct bio *bio;
1276
1277 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &iomap_ioend_bioset);
1278 bio_set_dev(bio, wpc->iomap.bdev);
1279 bio->bi_iter.bi_sector = sector;
1280 bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
1281 bio->bi_write_hint = inode->i_write_hint;
1282 wbc_init_bio(wbc, bio);
1283
1284 ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
1285 INIT_LIST_HEAD(&ioend->io_list);
1286 ioend->io_type = wpc->iomap.type;
1287 ioend->io_flags = wpc->iomap.flags;
1288 ioend->io_inode = inode;
1289 ioend->io_size = 0;
1290 ioend->io_offset = offset;
1291 ioend->io_private = NULL;
1292 ioend->io_bio = bio;
1293 return ioend;
1294}
1295
1296/*
1297 * Allocate a new bio, and chain the old bio to the new one.
1298 *
1299 * Note that we have to do perform the chaining in this unintuitive order
1300 * so that the bi_private linkage is set up in the right direction for the
1301 * traversal in iomap_finish_ioend().
1302 */
1303static struct bio *
1304iomap_chain_bio(struct bio *prev)
1305{
1306 struct bio *new;
1307
1308 new = bio_alloc(GFP_NOFS, BIO_MAX_PAGES);
1309 bio_copy_dev(new, prev);/* also copies over blkcg information */
1310 new->bi_iter.bi_sector = bio_end_sector(prev);
1311 new->bi_opf = prev->bi_opf;
1312 new->bi_write_hint = prev->bi_write_hint;
1313
1314 bio_chain(prev, new);
1315 bio_get(prev); /* for iomap_finish_ioend */
1316 submit_bio(prev);
1317 return new;
1318}
1319
1320static bool
1321iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
1322 sector_t sector)
1323{
1324 if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1325 (wpc->ioend->io_flags & IOMAP_F_SHARED))
1326 return false;
1327 if (wpc->iomap.type != wpc->ioend->io_type)
1328 return false;
1329 if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
1330 return false;
1331 if (sector != bio_end_sector(wpc->ioend->io_bio))
1332 return false;
1333 return true;
1334}
1335
1336/*
1337 * Test to see if we have an existing ioend structure that we could append to
1338 * first, otherwise finish off the current ioend and start another.
1339 */
1340static void
1341iomap_add_to_ioend(struct inode *inode, loff_t offset, struct page *page,
1342 struct iomap_page *iop, struct iomap_writepage_ctx *wpc,
1343 struct writeback_control *wbc, struct list_head *iolist)
1344{
1345 sector_t sector = iomap_sector(&wpc->iomap, offset);
1346 unsigned len = i_blocksize(inode);
1347 unsigned poff = offset & (PAGE_SIZE - 1);
1348 bool merged, same_page = false;
1349
1350 if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, offset, sector)) {
1351 if (wpc->ioend)
1352 list_add(&wpc->ioend->io_list, iolist);
1353 wpc->ioend = iomap_alloc_ioend(inode, wpc, offset, sector, wbc);
1354 }
1355
1356 merged = __bio_try_merge_page(wpc->ioend->io_bio, page, len, poff,
1357 &same_page);
1358 if (iop && !same_page)
1359 atomic_inc(&iop->write_count);
1360
1361 if (!merged) {
1362 if (bio_full(wpc->ioend->io_bio, len)) {
1363 wpc->ioend->io_bio =
1364 iomap_chain_bio(wpc->ioend->io_bio);
1365 }
1366 bio_add_page(wpc->ioend->io_bio, page, len, poff);
1367 }
1368
1369 wpc->ioend->io_size += len;
1370 wbc_account_cgroup_owner(wbc, page, len);
1371}
1372
1373/*
1374 * We implement an immediate ioend submission policy here to avoid needing to
1375 * chain multiple ioends and hence nest mempool allocations which can violate
1376 * forward progress guarantees we need to provide. The current ioend we are
1377 * adding blocks to is cached on the writepage context, and if the new block
1378 * does not append to the cached ioend it will create a new ioend and cache that
1379 * instead.
1380 *
1381 * If a new ioend is created and cached, the old ioend is returned and queued
1382 * locally for submission once the entire page is processed or an error has been
1383 * detected. While ioends are submitted immediately after they are completed,
1384 * batching optimisations are provided by higher level block plugging.
1385 *
1386 * At the end of a writeback pass, there will be a cached ioend remaining on the
1387 * writepage context that the caller will need to submit.
1388 */
1389static int
1390iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1391 struct writeback_control *wbc, struct inode *inode,
1392 struct page *page, u64 end_offset)
1393{
1394 struct iomap_page *iop = to_iomap_page(page);
1395 struct iomap_ioend *ioend, *next;
1396 unsigned len = i_blocksize(inode);
1397 u64 file_offset; /* file offset of page */
1398 int error = 0, count = 0, i;
1399 LIST_HEAD(submit_list);
1400
1401 WARN_ON_ONCE(i_blocksize(inode) < PAGE_SIZE && !iop);
1402 WARN_ON_ONCE(iop && atomic_read(&iop->write_count) != 0);
1403
1404 /*
1405 * Walk through the page to find areas to write back. If we run off the
1406 * end of the current map or find the current map invalid, grab a new
1407 * one.
1408 */
1409 for (i = 0, file_offset = page_offset(page);
1410 i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset;
1411 i++, file_offset += len) {
1412 if (iop && !test_bit(i, iop->uptodate))
1413 continue;
1414
1415 error = wpc->ops->map_blocks(wpc, inode, file_offset);
1416 if (error)
1417 break;
3e19e6f3
CH
1418 if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
1419 continue;
598ecfba
CH
1420 if (wpc->iomap.type == IOMAP_HOLE)
1421 continue;
1422 iomap_add_to_ioend(inode, file_offset, page, iop, wpc, wbc,
1423 &submit_list);
1424 count++;
1425 }
1426
1427 WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
1428 WARN_ON_ONCE(!PageLocked(page));
1429 WARN_ON_ONCE(PageWriteback(page));
1430
1431 /*
1432 * We cannot cancel the ioend directly here on error. We may have
1433 * already set other pages under writeback and hence we have to run I/O
1434 * completion to mark the error state of the pages under writeback
1435 * appropriately.
1436 */
1437 if (unlikely(error)) {
1438 if (!count) {
1439 /*
1440 * If the current page hasn't been added to ioend, it
1441 * won't be affected by I/O completions and we must
1442 * discard and unlock it right here.
1443 */
1444 if (wpc->ops->discard_page)
1445 wpc->ops->discard_page(page);
1446 ClearPageUptodate(page);
1447 unlock_page(page);
1448 goto done;
1449 }
1450
1451 /*
1452 * If the page was not fully cleaned, we need to ensure that the
1453 * higher layers come back to it correctly. That means we need
1454 * to keep the page dirty, and for WB_SYNC_ALL writeback we need
1455 * to ensure the PAGECACHE_TAG_TOWRITE index mark is not removed
1456 * so another attempt to write this page in this writeback sweep
1457 * will be made.
1458 */
1459 set_page_writeback_keepwrite(page);
1460 } else {
1461 clear_page_dirty_for_io(page);
1462 set_page_writeback(page);
1463 }
1464
1465 unlock_page(page);
1466
1467 /*
1468 * Preserve the original error if there was one, otherwise catch
1469 * submission errors here and propagate into subsequent ioend
1470 * submissions.
1471 */
1472 list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
1473 int error2;
1474
1475 list_del_init(&ioend->io_list);
1476 error2 = iomap_submit_ioend(wpc, ioend, error);
1477 if (error2 && !error)
1478 error = error2;
1479 }
1480
1481 /*
1482 * We can end up here with no error and nothing to write only if we race
1483 * with a partial page truncate on a sub-page block sized filesystem.
1484 */
1485 if (!count)
1486 end_page_writeback(page);
1487done:
1488 mapping_set_error(page->mapping, error);
1489 return error;
1490}
1491
1492/*
1493 * Write out a dirty page.
1494 *
1495 * For delalloc space on the page we need to allocate space and flush it.
1496 * For unwritten space on the page we need to start the conversion to
1497 * regular allocated space.
1498 */
1499static int
1500iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)
1501{
1502 struct iomap_writepage_ctx *wpc = data;
1503 struct inode *inode = page->mapping->host;
1504 pgoff_t end_index;
1505 u64 end_offset;
1506 loff_t offset;
1507
1508 trace_iomap_writepage(inode, page, 0, 0);
1509
1510 /*
1511 * Refuse to write the page out if we are called from reclaim context.
1512 *
1513 * This avoids stack overflows when called from deeply used stacks in
1514 * random callers for direct reclaim or memcg reclaim. We explicitly
1515 * allow reclaim from kswapd as the stack usage there is relatively low.
1516 *
1517 * This should never happen except in the case of a VM regression so
1518 * warn about it.
1519 */
1520 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1521 PF_MEMALLOC))
1522 goto redirty;
1523
1524 /*
1525 * Given that we do not allow direct reclaim to call us, we should
1526 * never be called in a recursive filesystem reclaim context.
1527 */
1528 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS))
1529 goto redirty;
1530
1531 /*
1532 * Is this page beyond the end of the file?
1533 *
1534 * The page index is less than the end_index, adjust the end_offset
1535 * to the highest offset that this page should represent.
1536 * -----------------------------------------------------
1537 * | file mapping | <EOF> |
1538 * -----------------------------------------------------
1539 * | Page ... | Page N-2 | Page N-1 | Page N | |
1540 * ^--------------------------------^----------|--------
1541 * | desired writeback range | see else |
1542 * ---------------------------------^------------------|
1543 */
1544 offset = i_size_read(inode);
1545 end_index = offset >> PAGE_SHIFT;
1546 if (page->index < end_index)
1547 end_offset = (loff_t)(page->index + 1) << PAGE_SHIFT;
1548 else {
1549 /*
1550 * Check whether the page to write out is beyond or straddles
1551 * i_size or not.
1552 * -------------------------------------------------------
1553 * | file mapping | <EOF> |
1554 * -------------------------------------------------------
1555 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
1556 * ^--------------------------------^-----------|---------
1557 * | | Straddles |
1558 * ---------------------------------^-----------|--------|
1559 */
1560 unsigned offset_into_page = offset & (PAGE_SIZE - 1);
1561
1562 /*
1563 * Skip the page if it is fully outside i_size, e.g. due to a
1564 * truncate operation that is in progress. We must redirty the
1565 * page so that reclaim stops reclaiming it. Otherwise
1566 * iomap_vm_releasepage() is called on it and gets confused.
1567 *
1568 * Note that the end_index is unsigned long, it would overflow
1569 * if the given offset is greater than 16TB on 32-bit system
1570 * and if we do check the page is fully outside i_size or not
1571 * via "if (page->index >= end_index + 1)" as "end_index + 1"
1572 * will be evaluated to 0. Hence this page will be redirtied
1573 * and be written out repeatedly which would result in an
1574 * infinite loop, the user program that perform this operation
1575 * will hang. Instead, we can verify this situation by checking
1576 * if the page to write is totally beyond the i_size or if it's
1577 * offset is just equal to the EOF.
1578 */
1579 if (page->index > end_index ||
1580 (page->index == end_index && offset_into_page == 0))
1581 goto redirty;
1582
1583 /*
1584 * The page straddles i_size. It must be zeroed out on each
1585 * and every writepage invocation because it may be mmapped.
1586 * "A file is mapped in multiples of the page size. For a file
1587 * that is not a multiple of the page size, the remaining
1588 * memory is zeroed when mapped, and writes to that region are
1589 * not written out to the file."
1590 */
1591 zero_user_segment(page, offset_into_page, PAGE_SIZE);
1592
1593 /* Adjust the end_offset to the end of file */
1594 end_offset = offset;
1595 }
1596
1597 return iomap_writepage_map(wpc, wbc, inode, page, end_offset);
1598
1599redirty:
1600 redirty_page_for_writepage(wbc, page);
1601 unlock_page(page);
1602 return 0;
1603}
1604
1605int
1606iomap_writepage(struct page *page, struct writeback_control *wbc,
1607 struct iomap_writepage_ctx *wpc,
1608 const struct iomap_writeback_ops *ops)
1609{
1610 int ret;
1611
1612 wpc->ops = ops;
1613 ret = iomap_do_writepage(page, wbc, wpc);
1614 if (!wpc->ioend)
1615 return ret;
1616 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1617}
1618EXPORT_SYMBOL_GPL(iomap_writepage);
1619
1620int
1621iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1622 struct iomap_writepage_ctx *wpc,
1623 const struct iomap_writeback_ops *ops)
1624{
1625 int ret;
1626
1627 wpc->ops = ops;
1628 ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
1629 if (!wpc->ioend)
1630 return ret;
1631 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1632}
1633EXPORT_SYMBOL_GPL(iomap_writepages);
1634
1635static int __init iomap_init(void)
1636{
1637 return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
1638 offsetof(struct iomap_ioend, io_inline_bio),
1639 BIOSET_NEED_BVECS);
1640}
1641fs_initcall(iomap_init);