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