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