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