Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (c) 2016-2018 Christoph Hellwig.
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>
15#include <linux/swap.h>
16#include <linux/bio.h>
17#include <linux/sched/signal.h>
18#include <linux/migrate.h>
19
20#include "../internal.h"
21
22static struct iomap_page *
23iomap_page_create(struct inode *inode, struct page *page)
24{
25 struct iomap_page *iop = to_iomap_page(page);
26
27 if (iop || i_blocksize(inode) == PAGE_SIZE)
28 return iop;
29
30 iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL);
31 atomic_set(&iop->read_count, 0);
32 atomic_set(&iop->write_count, 0);
33 bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE);
34
35 /*
36 * migrate_page_move_mapping() assumes that pages with private data have
37 * their count elevated by 1.
38 */
39 get_page(page);
40 set_page_private(page, (unsigned long)iop);
41 SetPagePrivate(page);
42 return iop;
43}
44
45static void
46iomap_page_release(struct page *page)
47{
48 struct iomap_page *iop = to_iomap_page(page);
49
50 if (!iop)
51 return;
52 WARN_ON_ONCE(atomic_read(&iop->read_count));
53 WARN_ON_ONCE(atomic_read(&iop->write_count));
54 ClearPagePrivate(page);
55 set_page_private(page, 0);
56 put_page(page);
57 kfree(iop);
58}
59
60/*
61 * Calculate the range inside the page that we actually need to read.
62 */
63static void
64iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
65 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
66{
67 loff_t orig_pos = *pos;
68 loff_t isize = i_size_read(inode);
69 unsigned block_bits = inode->i_blkbits;
70 unsigned block_size = (1 << block_bits);
71 unsigned poff = offset_in_page(*pos);
72 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
73 unsigned first = poff >> block_bits;
74 unsigned last = (poff + plen - 1) >> block_bits;
75
76 /*
77 * If the block size is smaller than the page size we need to check the
78 * per-block uptodate status and adjust the offset and length if needed
79 * to avoid reading in already uptodate ranges.
80 */
81 if (iop) {
82 unsigned int i;
83
84 /* move forward for each leading block marked uptodate */
85 for (i = first; i <= last; i++) {
86 if (!test_bit(i, iop->uptodate))
87 break;
88 *pos += block_size;
89 poff += block_size;
90 plen -= block_size;
91 first++;
92 }
93
94 /* truncate len if we find any trailing uptodate block(s) */
95 for ( ; i <= last; i++) {
96 if (test_bit(i, iop->uptodate)) {
97 plen -= (last - i + 1) * block_size;
98 last = i - 1;
99 break;
100 }
101 }
102 }
103
104 /*
105 * If the extent spans the block that contains the i_size we need to
106 * handle both halves separately so that we properly zero data in the
107 * page cache for blocks that are entirely outside of i_size.
108 */
109 if (orig_pos <= isize && orig_pos + length > isize) {
110 unsigned end = offset_in_page(isize - 1) >> block_bits;
111
112 if (first <= end && last > end)
113 plen -= (last - end) * block_size;
114 }
115
116 *offp = poff;
117 *lenp = plen;
118}
119
120static void
121iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
122{
123 struct iomap_page *iop = to_iomap_page(page);
124 struct inode *inode = page->mapping->host;
125 unsigned first = off >> inode->i_blkbits;
126 unsigned last = (off + len - 1) >> inode->i_blkbits;
127 unsigned int i;
128 bool uptodate = true;
129
130 if (iop) {
131 for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
132 if (i >= first && i <= last)
133 set_bit(i, iop->uptodate);
134 else if (!test_bit(i, iop->uptodate))
135 uptodate = false;
136 }
137 }
138
139 if (uptodate && !PageError(page))
140 SetPageUptodate(page);
141}
142
143static void
144iomap_read_finish(struct iomap_page *iop, struct page *page)
145{
146 if (!iop || atomic_dec_and_test(&iop->read_count))
147 unlock_page(page);
148}
149
150static void
151iomap_read_page_end_io(struct bio_vec *bvec, int error)
152{
153 struct page *page = bvec->bv_page;
154 struct iomap_page *iop = to_iomap_page(page);
155
156 if (unlikely(error)) {
157 ClearPageUptodate(page);
158 SetPageError(page);
159 } else {
160 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
161 }
162
163 iomap_read_finish(iop, page);
164}
165
166static void
167iomap_read_end_io(struct bio *bio)
168{
169 int error = blk_status_to_errno(bio->bi_status);
170 struct bio_vec *bvec;
171 struct bvec_iter_all iter_all;
172
173 bio_for_each_segment_all(bvec, bio, iter_all)
174 iomap_read_page_end_io(bvec, error);
175 bio_put(bio);
176}
177
178struct iomap_readpage_ctx {
179 struct page *cur_page;
180 bool cur_page_in_bio;
181 bool is_readahead;
182 struct bio *bio;
183 struct list_head *pages;
184};
185
186static void
187iomap_read_inline_data(struct inode *inode, struct page *page,
188 struct iomap *iomap)
189{
190 size_t size = i_size_read(inode);
191 void *addr;
192
193 if (PageUptodate(page))
194 return;
195
196 BUG_ON(page->index);
197 BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
198
199 addr = kmap_atomic(page);
200 memcpy(addr, iomap->inline_data, size);
201 memset(addr + size, 0, PAGE_SIZE - size);
202 kunmap_atomic(addr);
203 SetPageUptodate(page);
204}
205
206static loff_t
207iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
208 struct iomap *iomap)
209{
210 struct iomap_readpage_ctx *ctx = data;
211 struct page *page = ctx->cur_page;
212 struct iomap_page *iop = iomap_page_create(inode, page);
213 bool same_page = false, is_contig = false;
214 loff_t orig_pos = pos;
215 unsigned poff, plen;
216 sector_t sector;
217
218 if (iomap->type == IOMAP_INLINE) {
219 WARN_ON_ONCE(pos);
220 iomap_read_inline_data(inode, page, iomap);
221 return PAGE_SIZE;
222 }
223
224 /* zero post-eof blocks as the page may be mapped */
225 iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
226 if (plen == 0)
227 goto done;
228
229 if (iomap->type != IOMAP_MAPPED || pos >= i_size_read(inode)) {
230 zero_user(page, poff, plen);
231 iomap_set_range_uptodate(page, poff, plen);
232 goto done;
233 }
234
235 ctx->cur_page_in_bio = true;
236
237 /*
238 * Try to merge into a previous segment if we can.
239 */
240 sector = iomap_sector(iomap, pos);
241 if (ctx->bio && bio_end_sector(ctx->bio) == sector)
242 is_contig = true;
243
244 if (is_contig &&
245 __bio_try_merge_page(ctx->bio, page, plen, poff, &same_page)) {
246 if (!same_page && iop)
247 atomic_inc(&iop->read_count);
248 goto done;
249 }
250
251 /*
252 * If we start a new segment we need to increase the read count, and we
253 * need to do so before submitting any previous full bio to make sure
254 * that we don't prematurely unlock the page.
255 */
256 if (iop)
257 atomic_inc(&iop->read_count);
258
259 if (!ctx->bio || !is_contig || bio_full(ctx->bio, plen)) {
260 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
261 int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
262
263 if (ctx->bio)
264 submit_bio(ctx->bio);
265
266 if (ctx->is_readahead) /* same as readahead_gfp_mask */
267 gfp |= __GFP_NORETRY | __GFP_NOWARN;
268 ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
269 ctx->bio->bi_opf = REQ_OP_READ;
270 if (ctx->is_readahead)
271 ctx->bio->bi_opf |= REQ_RAHEAD;
272 ctx->bio->bi_iter.bi_sector = sector;
273 bio_set_dev(ctx->bio, iomap->bdev);
274 ctx->bio->bi_end_io = iomap_read_end_io;
275 }
276
277 bio_add_page(ctx->bio, page, plen, poff);
278done:
279 /*
280 * Move the caller beyond our range so that it keeps making progress.
281 * For that we have to include any leading non-uptodate ranges, but
282 * we can skip trailing ones as they will be handled in the next
283 * iteration.
284 */
285 return pos - orig_pos + plen;
286}
287
288int
289iomap_readpage(struct page *page, const struct iomap_ops *ops)
290{
291 struct iomap_readpage_ctx ctx = { .cur_page = page };
292 struct inode *inode = page->mapping->host;
293 unsigned poff;
294 loff_t ret;
295
296 for (poff = 0; poff < PAGE_SIZE; poff += ret) {
297 ret = iomap_apply(inode, page_offset(page) + poff,
298 PAGE_SIZE - poff, 0, ops, &ctx,
299 iomap_readpage_actor);
300 if (ret <= 0) {
301 WARN_ON_ONCE(ret == 0);
302 SetPageError(page);
303 break;
304 }
305 }
306
307 if (ctx.bio) {
308 submit_bio(ctx.bio);
309 WARN_ON_ONCE(!ctx.cur_page_in_bio);
310 } else {
311 WARN_ON_ONCE(ctx.cur_page_in_bio);
312 unlock_page(page);
313 }
314
315 /*
316 * Just like mpage_readpages and block_read_full_page we always
317 * return 0 and just mark the page as PageError on errors. This
318 * should be cleaned up all through the stack eventually.
319 */
320 return 0;
321}
322EXPORT_SYMBOL_GPL(iomap_readpage);
323
324static struct page *
325iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
326 loff_t length, loff_t *done)
327{
328 while (!list_empty(pages)) {
329 struct page *page = lru_to_page(pages);
330
331 if (page_offset(page) >= (u64)pos + length)
332 break;
333
334 list_del(&page->lru);
335 if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
336 GFP_NOFS))
337 return page;
338
339 /*
340 * If we already have a page in the page cache at index we are
341 * done. Upper layers don't care if it is uptodate after the
342 * readpages call itself as every page gets checked again once
343 * actually needed.
344 */
345 *done += PAGE_SIZE;
346 put_page(page);
347 }
348
349 return NULL;
350}
351
352static loff_t
353iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
354 void *data, struct iomap *iomap)
355{
356 struct iomap_readpage_ctx *ctx = data;
357 loff_t done, ret;
358
359 for (done = 0; done < length; done += ret) {
360 if (ctx->cur_page && offset_in_page(pos + done) == 0) {
361 if (!ctx->cur_page_in_bio)
362 unlock_page(ctx->cur_page);
363 put_page(ctx->cur_page);
364 ctx->cur_page = NULL;
365 }
366 if (!ctx->cur_page) {
367 ctx->cur_page = iomap_next_page(inode, ctx->pages,
368 pos, length, &done);
369 if (!ctx->cur_page)
370 break;
371 ctx->cur_page_in_bio = false;
372 }
373 ret = iomap_readpage_actor(inode, pos + done, length - done,
374 ctx, iomap);
375 }
376
377 return done;
378}
379
380int
381iomap_readpages(struct address_space *mapping, struct list_head *pages,
382 unsigned nr_pages, const struct iomap_ops *ops)
383{
384 struct iomap_readpage_ctx ctx = {
385 .pages = pages,
386 .is_readahead = true,
387 };
388 loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
389 loff_t last = page_offset(list_entry(pages->next, struct page, lru));
390 loff_t length = last - pos + PAGE_SIZE, ret = 0;
391
392 while (length > 0) {
393 ret = iomap_apply(mapping->host, pos, length, 0, ops,
394 &ctx, iomap_readpages_actor);
395 if (ret <= 0) {
396 WARN_ON_ONCE(ret == 0);
397 goto done;
398 }
399 pos += ret;
400 length -= ret;
401 }
402 ret = 0;
403done:
404 if (ctx.bio)
405 submit_bio(ctx.bio);
406 if (ctx.cur_page) {
407 if (!ctx.cur_page_in_bio)
408 unlock_page(ctx.cur_page);
409 put_page(ctx.cur_page);
410 }
411
412 /*
413 * Check that we didn't lose a page due to the arcance calling
414 * conventions..
415 */
416 WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
417 return ret;
418}
419EXPORT_SYMBOL_GPL(iomap_readpages);
420
421/*
422 * iomap_is_partially_uptodate checks whether blocks within a page are
423 * uptodate or not.
424 *
425 * Returns true if all blocks which correspond to a file portion
426 * we want to read within the page are uptodate.
427 */
428int
429iomap_is_partially_uptodate(struct page *page, unsigned long from,
430 unsigned long count)
431{
432 struct iomap_page *iop = to_iomap_page(page);
433 struct inode *inode = page->mapping->host;
434 unsigned len, first, last;
435 unsigned i;
436
437 /* Limit range to one page */
438 len = min_t(unsigned, PAGE_SIZE - from, count);
439
440 /* First and last blocks in range within page */
441 first = from >> inode->i_blkbits;
442 last = (from + len - 1) >> inode->i_blkbits;
443
444 if (iop) {
445 for (i = first; i <= last; i++)
446 if (!test_bit(i, iop->uptodate))
447 return 0;
448 return 1;
449 }
450
451 return 0;
452}
453EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
454
455int
456iomap_releasepage(struct page *page, gfp_t gfp_mask)
457{
458 /*
459 * mm accommodates an old ext3 case where clean pages might not have had
460 * the dirty bit cleared. Thus, it can send actual dirty pages to
461 * ->releasepage() via shrink_active_list(), skip those here.
462 */
463 if (PageDirty(page) || PageWriteback(page))
464 return 0;
465 iomap_page_release(page);
466 return 1;
467}
468EXPORT_SYMBOL_GPL(iomap_releasepage);
469
470void
471iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
472{
473 /*
474 * If we are invalidating the entire page, clear the dirty state from it
475 * and release it to avoid unnecessary buildup of the LRU.
476 */
477 if (offset == 0 && len == PAGE_SIZE) {
478 WARN_ON_ONCE(PageWriteback(page));
479 cancel_dirty_page(page);
480 iomap_page_release(page);
481 }
482}
483EXPORT_SYMBOL_GPL(iomap_invalidatepage);
484
485#ifdef CONFIG_MIGRATION
486int
487iomap_migrate_page(struct address_space *mapping, struct page *newpage,
488 struct page *page, enum migrate_mode mode)
489{
490 int ret;
491
492 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
493 if (ret != MIGRATEPAGE_SUCCESS)
494 return ret;
495
496 if (page_has_private(page)) {
497 ClearPagePrivate(page);
498 get_page(newpage);
499 set_page_private(newpage, page_private(page));
500 set_page_private(page, 0);
501 put_page(page);
502 SetPagePrivate(newpage);
503 }
504
505 if (mode != MIGRATE_SYNC_NO_COPY)
506 migrate_page_copy(newpage, page);
507 else
508 migrate_page_states(newpage, page);
509 return MIGRATEPAGE_SUCCESS;
510}
511EXPORT_SYMBOL_GPL(iomap_migrate_page);
512#endif /* CONFIG_MIGRATION */
513
514static void
515iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
516{
517 loff_t i_size = i_size_read(inode);
518
519 /*
520 * Only truncate newly allocated pages beyoned EOF, even if the
521 * write started inside the existing inode size.
522 */
523 if (pos + len > i_size)
524 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
525}
526
527static int
528iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
529 unsigned poff, unsigned plen, unsigned from, unsigned to,
530 struct iomap *iomap)
531{
532 struct bio_vec bvec;
533 struct bio bio;
534
535 if (iomap->type != IOMAP_MAPPED || block_start >= i_size_read(inode)) {
536 zero_user_segments(page, poff, from, to, poff + plen);
537 iomap_set_range_uptodate(page, poff, plen);
538 return 0;
539 }
540
541 bio_init(&bio, &bvec, 1);
542 bio.bi_opf = REQ_OP_READ;
543 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
544 bio_set_dev(&bio, iomap->bdev);
545 __bio_add_page(&bio, page, plen, poff);
546 return submit_bio_wait(&bio);
547}
548
549static int
550__iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
551 struct page *page, struct iomap *iomap)
552{
553 struct iomap_page *iop = iomap_page_create(inode, page);
554 loff_t block_size = i_blocksize(inode);
555 loff_t block_start = pos & ~(block_size - 1);
556 loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
557 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
558 int status = 0;
559
560 if (PageUptodate(page))
561 return 0;
562
563 do {
564 iomap_adjust_read_range(inode, iop, &block_start,
565 block_end - block_start, &poff, &plen);
566 if (plen == 0)
567 break;
568
569 if ((from > poff && from < poff + plen) ||
570 (to > poff && to < poff + plen)) {
571 status = iomap_read_page_sync(inode, block_start, page,
572 poff, plen, from, to, iomap);
573 if (status)
574 break;
575 }
576
577 } while ((block_start += plen) < block_end);
578
579 return status;
580}
581
582static int
583iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
584 struct page **pagep, struct iomap *iomap)
585{
586 const struct iomap_page_ops *page_ops = iomap->page_ops;
587 pgoff_t index = pos >> PAGE_SHIFT;
588 struct page *page;
589 int status = 0;
590
591 BUG_ON(pos + len > iomap->offset + iomap->length);
592
593 if (fatal_signal_pending(current))
594 return -EINTR;
595
596 if (page_ops && page_ops->page_prepare) {
597 status = page_ops->page_prepare(inode, pos, len, iomap);
598 if (status)
599 return status;
600 }
601
602 page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
603 if (!page) {
604 status = -ENOMEM;
605 goto out_no_page;
606 }
607
608 if (iomap->type == IOMAP_INLINE)
609 iomap_read_inline_data(inode, page, iomap);
610 else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
611 status = __block_write_begin_int(page, pos, len, NULL, iomap);
612 else
613 status = __iomap_write_begin(inode, pos, len, page, iomap);
614
615 if (unlikely(status))
616 goto out_unlock;
617
618 *pagep = page;
619 return 0;
620
621out_unlock:
622 unlock_page(page);
623 put_page(page);
624 iomap_write_failed(inode, pos, len);
625
626out_no_page:
627 if (page_ops && page_ops->page_done)
628 page_ops->page_done(inode, pos, 0, NULL, iomap);
629 return status;
630}
631
632int
633iomap_set_page_dirty(struct page *page)
634{
635 struct address_space *mapping = page_mapping(page);
636 int newly_dirty;
637
638 if (unlikely(!mapping))
639 return !TestSetPageDirty(page);
640
641 /*
642 * Lock out page->mem_cgroup migration to keep PageDirty
643 * synchronized with per-memcg dirty page counters.
644 */
645 lock_page_memcg(page);
646 newly_dirty = !TestSetPageDirty(page);
647 if (newly_dirty)
648 __set_page_dirty(page, mapping, 0);
649 unlock_page_memcg(page);
650
651 if (newly_dirty)
652 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
653 return newly_dirty;
654}
655EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
656
657static int
658__iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
659 unsigned copied, struct page *page, struct iomap *iomap)
660{
661 flush_dcache_page(page);
662
663 /*
664 * The blocks that were entirely written will now be uptodate, so we
665 * don't have to worry about a readpage reading them and overwriting a
666 * partial write. However if we have encountered a short write and only
667 * partially written into a block, it will not be marked uptodate, so a
668 * readpage might come in and destroy our partial write.
669 *
670 * Do the simplest thing, and just treat any short write to a non
671 * uptodate page as a zero-length write, and force the caller to redo
672 * the whole thing.
673 */
674 if (unlikely(copied < len && !PageUptodate(page)))
675 return 0;
676 iomap_set_range_uptodate(page, offset_in_page(pos), len);
677 iomap_set_page_dirty(page);
678 return copied;
679}
680
681static int
682iomap_write_end_inline(struct inode *inode, struct page *page,
683 struct iomap *iomap, loff_t pos, unsigned copied)
684{
685 void *addr;
686
687 WARN_ON_ONCE(!PageUptodate(page));
688 BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
689
690 addr = kmap_atomic(page);
691 memcpy(iomap->inline_data + pos, addr + pos, copied);
692 kunmap_atomic(addr);
693
694 mark_inode_dirty(inode);
695 return copied;
696}
697
698static int
699iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
700 unsigned copied, struct page *page, struct iomap *iomap)
701{
702 const struct iomap_page_ops *page_ops = iomap->page_ops;
703 loff_t old_size = inode->i_size;
704 int ret;
705
706 if (iomap->type == IOMAP_INLINE) {
707 ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
708 } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
709 ret = block_write_end(NULL, inode->i_mapping, pos, len, copied,
710 page, NULL);
711 } else {
712 ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
713 }
714
715 /*
716 * Update the in-memory inode size after copying the data into the page
717 * cache. It's up to the file system to write the updated size to disk,
718 * preferably after I/O completion so that no stale data is exposed.
719 */
720 if (pos + ret > old_size) {
721 i_size_write(inode, pos + ret);
722 iomap->flags |= IOMAP_F_SIZE_CHANGED;
723 }
724 unlock_page(page);
725
726 if (old_size < pos)
727 pagecache_isize_extended(inode, old_size, pos);
728 if (page_ops && page_ops->page_done)
729 page_ops->page_done(inode, pos, ret, page, iomap);
730 put_page(page);
731
732 if (ret < len)
733 iomap_write_failed(inode, pos, len);
734 return ret;
735}
736
737static loff_t
738iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
739 struct iomap *iomap)
740{
741 struct iov_iter *i = data;
742 long status = 0;
743 ssize_t written = 0;
744 unsigned int flags = AOP_FLAG_NOFS;
745
746 do {
747 struct page *page;
748 unsigned long offset; /* Offset into pagecache page */
749 unsigned long bytes; /* Bytes to write to page */
750 size_t copied; /* Bytes copied from user */
751
752 offset = offset_in_page(pos);
753 bytes = min_t(unsigned long, PAGE_SIZE - offset,
754 iov_iter_count(i));
755again:
756 if (bytes > length)
757 bytes = length;
758
759 /*
760 * Bring in the user page that we will copy from _first_.
761 * Otherwise there's a nasty deadlock on copying from the
762 * same page as we're writing to, without it being marked
763 * up-to-date.
764 *
765 * Not only is this an optimisation, but it is also required
766 * to check that the address is actually valid, when atomic
767 * usercopies are used, below.
768 */
769 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
770 status = -EFAULT;
771 break;
772 }
773
774 status = iomap_write_begin(inode, pos, bytes, flags, &page,
775 iomap);
776 if (unlikely(status))
777 break;
778
779 if (mapping_writably_mapped(inode->i_mapping))
780 flush_dcache_page(page);
781
782 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
783
784 flush_dcache_page(page);
785
786 status = iomap_write_end(inode, pos, bytes, copied, page,
787 iomap);
788 if (unlikely(status < 0))
789 break;
790 copied = status;
791
792 cond_resched();
793
794 iov_iter_advance(i, copied);
795 if (unlikely(copied == 0)) {
796 /*
797 * If we were unable to copy any data at all, we must
798 * fall back to a single segment length write.
799 *
800 * If we didn't fallback here, we could livelock
801 * because not all segments in the iov can be copied at
802 * once without a pagefault.
803 */
804 bytes = min_t(unsigned long, PAGE_SIZE - offset,
805 iov_iter_single_seg_count(i));
806 goto again;
807 }
808 pos += copied;
809 written += copied;
810 length -= copied;
811
812 balance_dirty_pages_ratelimited(inode->i_mapping);
813 } while (iov_iter_count(i) && length);
814
815 return written ? written : status;
816}
817
818ssize_t
819iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
820 const struct iomap_ops *ops)
821{
822 struct inode *inode = iocb->ki_filp->f_mapping->host;
823 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
824
825 while (iov_iter_count(iter)) {
826 ret = iomap_apply(inode, pos, iov_iter_count(iter),
827 IOMAP_WRITE, ops, iter, iomap_write_actor);
828 if (ret <= 0)
829 break;
830 pos += ret;
831 written += ret;
832 }
833
834 return written ? written : ret;
835}
836EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
837
838static struct page *
839__iomap_read_page(struct inode *inode, loff_t offset)
840{
841 struct address_space *mapping = inode->i_mapping;
842 struct page *page;
843
844 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
845 if (IS_ERR(page))
846 return page;
847 if (!PageUptodate(page)) {
848 put_page(page);
849 return ERR_PTR(-EIO);
850 }
851 return page;
852}
853
854static loff_t
855iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
856 struct iomap *iomap)
857{
858 long status = 0;
859 ssize_t written = 0;
860
861 do {
862 struct page *page, *rpage;
863 unsigned long offset; /* Offset into pagecache page */
864 unsigned long bytes; /* Bytes to write to page */
865
866 offset = offset_in_page(pos);
867 bytes = min_t(loff_t, PAGE_SIZE - offset, length);
868
869 rpage = __iomap_read_page(inode, pos);
870 if (IS_ERR(rpage))
871 return PTR_ERR(rpage);
872
873 status = iomap_write_begin(inode, pos, bytes,
874 AOP_FLAG_NOFS, &page, iomap);
875 put_page(rpage);
876 if (unlikely(status))
877 return status;
878
879 WARN_ON_ONCE(!PageUptodate(page));
880
881 status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
882 if (unlikely(status <= 0)) {
883 if (WARN_ON_ONCE(status == 0))
884 return -EIO;
885 return status;
886 }
887
888 cond_resched();
889
890 pos += status;
891 written += status;
892 length -= status;
893
894 balance_dirty_pages_ratelimited(inode->i_mapping);
895 } while (length);
896
897 return written;
898}
899
900int
901iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
902 const struct iomap_ops *ops)
903{
904 loff_t ret;
905
906 while (len) {
907 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
908 iomap_dirty_actor);
909 if (ret <= 0)
910 return ret;
911 pos += ret;
912 len -= ret;
913 }
914
915 return 0;
916}
917EXPORT_SYMBOL_GPL(iomap_file_dirty);
918
919static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
920 unsigned bytes, struct iomap *iomap)
921{
922 struct page *page;
923 int status;
924
925 status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
926 iomap);
927 if (status)
928 return status;
929
930 zero_user(page, offset, bytes);
931 mark_page_accessed(page);
932
933 return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
934}
935
936static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
937 struct iomap *iomap)
938{
939 return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
940 iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
941}
942
943static loff_t
944iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
945 void *data, struct iomap *iomap)
946{
947 bool *did_zero = data;
948 loff_t written = 0;
949 int status;
950
951 /* already zeroed? we're done. */
952 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
953 return count;
954
955 do {
956 unsigned offset, bytes;
957
958 offset = offset_in_page(pos);
959 bytes = min_t(loff_t, PAGE_SIZE - offset, count);
960
961 if (IS_DAX(inode))
962 status = iomap_dax_zero(pos, offset, bytes, iomap);
963 else
964 status = iomap_zero(inode, pos, offset, bytes, iomap);
965 if (status < 0)
966 return status;
967
968 pos += bytes;
969 count -= bytes;
970 written += bytes;
971 if (did_zero)
972 *did_zero = true;
973 } while (count > 0);
974
975 return written;
976}
977
978int
979iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
980 const struct iomap_ops *ops)
981{
982 loff_t ret;
983
984 while (len > 0) {
985 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
986 ops, did_zero, iomap_zero_range_actor);
987 if (ret <= 0)
988 return ret;
989
990 pos += ret;
991 len -= ret;
992 }
993
994 return 0;
995}
996EXPORT_SYMBOL_GPL(iomap_zero_range);
997
998int
999iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1000 const struct iomap_ops *ops)
1001{
1002 unsigned int blocksize = i_blocksize(inode);
1003 unsigned int off = pos & (blocksize - 1);
1004
1005 /* Block boundary? Nothing to do */
1006 if (!off)
1007 return 0;
1008 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1009}
1010EXPORT_SYMBOL_GPL(iomap_truncate_page);
1011
1012static loff_t
1013iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
1014 void *data, struct iomap *iomap)
1015{
1016 struct page *page = data;
1017 int ret;
1018
1019 if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
1020 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
1021 if (ret)
1022 return ret;
1023 block_commit_write(page, 0, length);
1024 } else {
1025 WARN_ON_ONCE(!PageUptodate(page));
1026 iomap_page_create(inode, page);
1027 set_page_dirty(page);
1028 }
1029
1030 return length;
1031}
1032
1033vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1034{
1035 struct page *page = vmf->page;
1036 struct inode *inode = file_inode(vmf->vma->vm_file);
1037 unsigned long length;
1038 loff_t offset, size;
1039 ssize_t ret;
1040
1041 lock_page(page);
1042 size = i_size_read(inode);
1043 if ((page->mapping != inode->i_mapping) ||
1044 (page_offset(page) > size)) {
1045 /* We overload EFAULT to mean page got truncated */
1046 ret = -EFAULT;
1047 goto out_unlock;
1048 }
1049
1050 /* page is wholly or partially inside EOF */
1051 if (((page->index + 1) << PAGE_SHIFT) > size)
1052 length = offset_in_page(size);
1053 else
1054 length = PAGE_SIZE;
1055
1056 offset = page_offset(page);
1057 while (length > 0) {
1058 ret = iomap_apply(inode, offset, length,
1059 IOMAP_WRITE | IOMAP_FAULT, ops, page,
1060 iomap_page_mkwrite_actor);
1061 if (unlikely(ret <= 0))
1062 goto out_unlock;
1063 offset += ret;
1064 length -= ret;
1065 }
1066
1067 wait_for_stable_page(page);
1068 return VM_FAULT_LOCKED;
1069out_unlock:
1070 unlock_page(page);
1071 return block_page_mkwrite_return(ret);
1072}
1073EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (C) 2016-2023 Christoph Hellwig.
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>
15#include <linux/list_sort.h>
16#include <linux/swap.h>
17#include <linux/bio.h>
18#include <linux/sched/signal.h>
19#include <linux/migrate.h>
20#include "trace.h"
21
22#include "../internal.h"
23
24#define IOEND_BATCH_SIZE 4096
25
26typedef int (*iomap_punch_t)(struct inode *inode, loff_t offset, loff_t length);
27/*
28 * Structure allocated for each folio to track per-block uptodate, dirty state
29 * and I/O completions.
30 */
31struct iomap_folio_state {
32 spinlock_t state_lock;
33 unsigned int read_bytes_pending;
34 atomic_t write_bytes_pending;
35
36 /*
37 * Each block has two bits in this bitmap:
38 * Bits [0..blocks_per_folio) has the uptodate status.
39 * Bits [b_p_f...(2*b_p_f)) has the dirty status.
40 */
41 unsigned long state[];
42};
43
44static struct bio_set iomap_ioend_bioset;
45
46static inline bool ifs_is_fully_uptodate(struct folio *folio,
47 struct iomap_folio_state *ifs)
48{
49 struct inode *inode = folio->mapping->host;
50
51 return bitmap_full(ifs->state, i_blocks_per_folio(inode, folio));
52}
53
54static inline bool ifs_block_is_uptodate(struct iomap_folio_state *ifs,
55 unsigned int block)
56{
57 return test_bit(block, ifs->state);
58}
59
60static bool ifs_set_range_uptodate(struct folio *folio,
61 struct iomap_folio_state *ifs, size_t off, size_t len)
62{
63 struct inode *inode = folio->mapping->host;
64 unsigned int first_blk = off >> inode->i_blkbits;
65 unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
66 unsigned int nr_blks = last_blk - first_blk + 1;
67
68 bitmap_set(ifs->state, first_blk, nr_blks);
69 return ifs_is_fully_uptodate(folio, ifs);
70}
71
72static void iomap_set_range_uptodate(struct folio *folio, size_t off,
73 size_t len)
74{
75 struct iomap_folio_state *ifs = folio->private;
76 unsigned long flags;
77 bool uptodate = true;
78
79 if (ifs) {
80 spin_lock_irqsave(&ifs->state_lock, flags);
81 uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
82 spin_unlock_irqrestore(&ifs->state_lock, flags);
83 }
84
85 if (uptodate)
86 folio_mark_uptodate(folio);
87}
88
89static inline bool ifs_block_is_dirty(struct folio *folio,
90 struct iomap_folio_state *ifs, int block)
91{
92 struct inode *inode = folio->mapping->host;
93 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
94
95 return test_bit(block + blks_per_folio, ifs->state);
96}
97
98static unsigned ifs_find_dirty_range(struct folio *folio,
99 struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
100{
101 struct inode *inode = folio->mapping->host;
102 unsigned start_blk =
103 offset_in_folio(folio, *range_start) >> inode->i_blkbits;
104 unsigned end_blk = min_not_zero(
105 offset_in_folio(folio, range_end) >> inode->i_blkbits,
106 i_blocks_per_folio(inode, folio));
107 unsigned nblks = 1;
108
109 while (!ifs_block_is_dirty(folio, ifs, start_blk))
110 if (++start_blk == end_blk)
111 return 0;
112
113 while (start_blk + nblks < end_blk) {
114 if (!ifs_block_is_dirty(folio, ifs, start_blk + nblks))
115 break;
116 nblks++;
117 }
118
119 *range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
120 return nblks << inode->i_blkbits;
121}
122
123static unsigned iomap_find_dirty_range(struct folio *folio, u64 *range_start,
124 u64 range_end)
125{
126 struct iomap_folio_state *ifs = folio->private;
127
128 if (*range_start >= range_end)
129 return 0;
130
131 if (ifs)
132 return ifs_find_dirty_range(folio, ifs, range_start, range_end);
133 return range_end - *range_start;
134}
135
136static void ifs_clear_range_dirty(struct folio *folio,
137 struct iomap_folio_state *ifs, size_t off, size_t len)
138{
139 struct inode *inode = folio->mapping->host;
140 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
141 unsigned int first_blk = (off >> inode->i_blkbits);
142 unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
143 unsigned int nr_blks = last_blk - first_blk + 1;
144 unsigned long flags;
145
146 spin_lock_irqsave(&ifs->state_lock, flags);
147 bitmap_clear(ifs->state, first_blk + blks_per_folio, nr_blks);
148 spin_unlock_irqrestore(&ifs->state_lock, flags);
149}
150
151static void iomap_clear_range_dirty(struct folio *folio, size_t off, size_t len)
152{
153 struct iomap_folio_state *ifs = folio->private;
154
155 if (ifs)
156 ifs_clear_range_dirty(folio, ifs, off, len);
157}
158
159static void ifs_set_range_dirty(struct folio *folio,
160 struct iomap_folio_state *ifs, size_t off, size_t len)
161{
162 struct inode *inode = folio->mapping->host;
163 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
164 unsigned int first_blk = (off >> inode->i_blkbits);
165 unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
166 unsigned int nr_blks = last_blk - first_blk + 1;
167 unsigned long flags;
168
169 spin_lock_irqsave(&ifs->state_lock, flags);
170 bitmap_set(ifs->state, first_blk + blks_per_folio, nr_blks);
171 spin_unlock_irqrestore(&ifs->state_lock, flags);
172}
173
174static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
175{
176 struct iomap_folio_state *ifs = folio->private;
177
178 if (ifs)
179 ifs_set_range_dirty(folio, ifs, off, len);
180}
181
182static struct iomap_folio_state *ifs_alloc(struct inode *inode,
183 struct folio *folio, unsigned int flags)
184{
185 struct iomap_folio_state *ifs = folio->private;
186 unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
187 gfp_t gfp;
188
189 if (ifs || nr_blocks <= 1)
190 return ifs;
191
192 if (flags & IOMAP_NOWAIT)
193 gfp = GFP_NOWAIT;
194 else
195 gfp = GFP_NOFS | __GFP_NOFAIL;
196
197 /*
198 * ifs->state tracks two sets of state flags when the
199 * filesystem block size is smaller than the folio size.
200 * The first state tracks per-block uptodate and the
201 * second tracks per-block dirty state.
202 */
203 ifs = kzalloc(struct_size(ifs, state,
204 BITS_TO_LONGS(2 * nr_blocks)), gfp);
205 if (!ifs)
206 return ifs;
207
208 spin_lock_init(&ifs->state_lock);
209 if (folio_test_uptodate(folio))
210 bitmap_set(ifs->state, 0, nr_blocks);
211 if (folio_test_dirty(folio))
212 bitmap_set(ifs->state, nr_blocks, nr_blocks);
213 folio_attach_private(folio, ifs);
214
215 return ifs;
216}
217
218static void ifs_free(struct folio *folio)
219{
220 struct iomap_folio_state *ifs = folio_detach_private(folio);
221
222 if (!ifs)
223 return;
224 WARN_ON_ONCE(ifs->read_bytes_pending != 0);
225 WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending));
226 WARN_ON_ONCE(ifs_is_fully_uptodate(folio, ifs) !=
227 folio_test_uptodate(folio));
228 kfree(ifs);
229}
230
231/*
232 * Calculate the range inside the folio that we actually need to read.
233 */
234static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
235 loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
236{
237 struct iomap_folio_state *ifs = folio->private;
238 loff_t orig_pos = *pos;
239 loff_t isize = i_size_read(inode);
240 unsigned block_bits = inode->i_blkbits;
241 unsigned block_size = (1 << block_bits);
242 size_t poff = offset_in_folio(folio, *pos);
243 size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
244 unsigned first = poff >> block_bits;
245 unsigned last = (poff + plen - 1) >> block_bits;
246
247 /*
248 * If the block size is smaller than the page size, we need to check the
249 * per-block uptodate status and adjust the offset and length if needed
250 * to avoid reading in already uptodate ranges.
251 */
252 if (ifs) {
253 unsigned int i;
254
255 /* move forward for each leading block marked uptodate */
256 for (i = first; i <= last; i++) {
257 if (!ifs_block_is_uptodate(ifs, i))
258 break;
259 *pos += block_size;
260 poff += block_size;
261 plen -= block_size;
262 first++;
263 }
264
265 /* truncate len if we find any trailing uptodate block(s) */
266 for ( ; i <= last; i++) {
267 if (ifs_block_is_uptodate(ifs, i)) {
268 plen -= (last - i + 1) * block_size;
269 last = i - 1;
270 break;
271 }
272 }
273 }
274
275 /*
276 * If the extent spans the block that contains the i_size, we need to
277 * handle both halves separately so that we properly zero data in the
278 * page cache for blocks that are entirely outside of i_size.
279 */
280 if (orig_pos <= isize && orig_pos + length > isize) {
281 unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
282
283 if (first <= end && last > end)
284 plen -= (last - end) * block_size;
285 }
286
287 *offp = poff;
288 *lenp = plen;
289}
290
291static void iomap_finish_folio_read(struct folio *folio, size_t off,
292 size_t len, int error)
293{
294 struct iomap_folio_state *ifs = folio->private;
295 bool uptodate = !error;
296 bool finished = true;
297
298 if (ifs) {
299 unsigned long flags;
300
301 spin_lock_irqsave(&ifs->state_lock, flags);
302 if (!error)
303 uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
304 ifs->read_bytes_pending -= len;
305 finished = !ifs->read_bytes_pending;
306 spin_unlock_irqrestore(&ifs->state_lock, flags);
307 }
308
309 if (error)
310 folio_set_error(folio);
311 if (finished)
312 folio_end_read(folio, uptodate);
313}
314
315static void iomap_read_end_io(struct bio *bio)
316{
317 int error = blk_status_to_errno(bio->bi_status);
318 struct folio_iter fi;
319
320 bio_for_each_folio_all(fi, bio)
321 iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error);
322 bio_put(bio);
323}
324
325struct iomap_readpage_ctx {
326 struct folio *cur_folio;
327 bool cur_folio_in_bio;
328 struct bio *bio;
329 struct readahead_control *rac;
330};
331
332/**
333 * iomap_read_inline_data - copy inline data into the page cache
334 * @iter: iteration structure
335 * @folio: folio to copy to
336 *
337 * Copy the inline data in @iter into @folio and zero out the rest of the folio.
338 * Only a single IOMAP_INLINE extent is allowed at the end of each file.
339 * Returns zero for success to complete the read, or the usual negative errno.
340 */
341static int iomap_read_inline_data(const struct iomap_iter *iter,
342 struct folio *folio)
343{
344 const struct iomap *iomap = iomap_iter_srcmap(iter);
345 size_t size = i_size_read(iter->inode) - iomap->offset;
346 size_t offset = offset_in_folio(folio, iomap->offset);
347
348 if (folio_test_uptodate(folio))
349 return 0;
350
351 if (WARN_ON_ONCE(size > iomap->length))
352 return -EIO;
353 if (offset > 0)
354 ifs_alloc(iter->inode, folio, iter->flags);
355
356 folio_fill_tail(folio, offset, iomap->inline_data, size);
357 iomap_set_range_uptodate(folio, offset, folio_size(folio) - offset);
358 return 0;
359}
360
361static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
362 loff_t pos)
363{
364 const struct iomap *srcmap = iomap_iter_srcmap(iter);
365
366 return srcmap->type != IOMAP_MAPPED ||
367 (srcmap->flags & IOMAP_F_NEW) ||
368 pos >= i_size_read(iter->inode);
369}
370
371static loff_t iomap_readpage_iter(const struct iomap_iter *iter,
372 struct iomap_readpage_ctx *ctx, loff_t offset)
373{
374 const struct iomap *iomap = &iter->iomap;
375 loff_t pos = iter->pos + offset;
376 loff_t length = iomap_length(iter) - offset;
377 struct folio *folio = ctx->cur_folio;
378 struct iomap_folio_state *ifs;
379 loff_t orig_pos = pos;
380 size_t poff, plen;
381 sector_t sector;
382
383 if (iomap->type == IOMAP_INLINE)
384 return iomap_read_inline_data(iter, folio);
385
386 /* zero post-eof blocks as the page may be mapped */
387 ifs = ifs_alloc(iter->inode, folio, iter->flags);
388 iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen);
389 if (plen == 0)
390 goto done;
391
392 if (iomap_block_needs_zeroing(iter, pos)) {
393 folio_zero_range(folio, poff, plen);
394 iomap_set_range_uptodate(folio, poff, plen);
395 goto done;
396 }
397
398 ctx->cur_folio_in_bio = true;
399 if (ifs) {
400 spin_lock_irq(&ifs->state_lock);
401 ifs->read_bytes_pending += plen;
402 spin_unlock_irq(&ifs->state_lock);
403 }
404
405 sector = iomap_sector(iomap, pos);
406 if (!ctx->bio ||
407 bio_end_sector(ctx->bio) != sector ||
408 !bio_add_folio(ctx->bio, folio, plen, poff)) {
409 gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
410 gfp_t orig_gfp = gfp;
411 unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
412
413 if (ctx->bio)
414 submit_bio(ctx->bio);
415
416 if (ctx->rac) /* same as readahead_gfp_mask */
417 gfp |= __GFP_NORETRY | __GFP_NOWARN;
418 ctx->bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs),
419 REQ_OP_READ, gfp);
420 /*
421 * If the bio_alloc fails, try it again for a single page to
422 * avoid having to deal with partial page reads. This emulates
423 * what do_mpage_read_folio does.
424 */
425 if (!ctx->bio) {
426 ctx->bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ,
427 orig_gfp);
428 }
429 if (ctx->rac)
430 ctx->bio->bi_opf |= REQ_RAHEAD;
431 ctx->bio->bi_iter.bi_sector = sector;
432 ctx->bio->bi_end_io = iomap_read_end_io;
433 bio_add_folio_nofail(ctx->bio, folio, plen, poff);
434 }
435
436done:
437 /*
438 * Move the caller beyond our range so that it keeps making progress.
439 * For that, we have to include any leading non-uptodate ranges, but
440 * we can skip trailing ones as they will be handled in the next
441 * iteration.
442 */
443 return pos - orig_pos + plen;
444}
445
446int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops)
447{
448 struct iomap_iter iter = {
449 .inode = folio->mapping->host,
450 .pos = folio_pos(folio),
451 .len = folio_size(folio),
452 };
453 struct iomap_readpage_ctx ctx = {
454 .cur_folio = folio,
455 };
456 int ret;
457
458 trace_iomap_readpage(iter.inode, 1);
459
460 while ((ret = iomap_iter(&iter, ops)) > 0)
461 iter.processed = iomap_readpage_iter(&iter, &ctx, 0);
462
463 if (ret < 0)
464 folio_set_error(folio);
465
466 if (ctx.bio) {
467 submit_bio(ctx.bio);
468 WARN_ON_ONCE(!ctx.cur_folio_in_bio);
469 } else {
470 WARN_ON_ONCE(ctx.cur_folio_in_bio);
471 folio_unlock(folio);
472 }
473
474 /*
475 * Just like mpage_readahead and block_read_full_folio, we always
476 * return 0 and just set the folio error flag on errors. This
477 * should be cleaned up throughout the stack eventually.
478 */
479 return 0;
480}
481EXPORT_SYMBOL_GPL(iomap_read_folio);
482
483static loff_t iomap_readahead_iter(const struct iomap_iter *iter,
484 struct iomap_readpage_ctx *ctx)
485{
486 loff_t length = iomap_length(iter);
487 loff_t done, ret;
488
489 for (done = 0; done < length; done += ret) {
490 if (ctx->cur_folio &&
491 offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) {
492 if (!ctx->cur_folio_in_bio)
493 folio_unlock(ctx->cur_folio);
494 ctx->cur_folio = NULL;
495 }
496 if (!ctx->cur_folio) {
497 ctx->cur_folio = readahead_folio(ctx->rac);
498 ctx->cur_folio_in_bio = false;
499 }
500 ret = iomap_readpage_iter(iter, ctx, done);
501 if (ret <= 0)
502 return ret;
503 }
504
505 return done;
506}
507
508/**
509 * iomap_readahead - Attempt to read pages from a file.
510 * @rac: Describes the pages to be read.
511 * @ops: The operations vector for the filesystem.
512 *
513 * This function is for filesystems to call to implement their readahead
514 * address_space operation.
515 *
516 * Context: The @ops callbacks may submit I/O (eg to read the addresses of
517 * blocks from disc), and may wait for it. The caller may be trying to
518 * access a different page, and so sleeping excessively should be avoided.
519 * It may allocate memory, but should avoid costly allocations. This
520 * function is called with memalloc_nofs set, so allocations will not cause
521 * the filesystem to be reentered.
522 */
523void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
524{
525 struct iomap_iter iter = {
526 .inode = rac->mapping->host,
527 .pos = readahead_pos(rac),
528 .len = readahead_length(rac),
529 };
530 struct iomap_readpage_ctx ctx = {
531 .rac = rac,
532 };
533
534 trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
535
536 while (iomap_iter(&iter, ops) > 0)
537 iter.processed = iomap_readahead_iter(&iter, &ctx);
538
539 if (ctx.bio)
540 submit_bio(ctx.bio);
541 if (ctx.cur_folio) {
542 if (!ctx.cur_folio_in_bio)
543 folio_unlock(ctx.cur_folio);
544 }
545}
546EXPORT_SYMBOL_GPL(iomap_readahead);
547
548/*
549 * iomap_is_partially_uptodate checks whether blocks within a folio are
550 * uptodate or not.
551 *
552 * Returns true if all blocks which correspond to the specified part
553 * of the folio are uptodate.
554 */
555bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
556{
557 struct iomap_folio_state *ifs = folio->private;
558 struct inode *inode = folio->mapping->host;
559 unsigned first, last, i;
560
561 if (!ifs)
562 return false;
563
564 /* Caller's range may extend past the end of this folio */
565 count = min(folio_size(folio) - from, count);
566
567 /* First and last blocks in range within folio */
568 first = from >> inode->i_blkbits;
569 last = (from + count - 1) >> inode->i_blkbits;
570
571 for (i = first; i <= last; i++)
572 if (!ifs_block_is_uptodate(ifs, i))
573 return false;
574 return true;
575}
576EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
577
578/**
579 * iomap_get_folio - get a folio reference for writing
580 * @iter: iteration structure
581 * @pos: start offset of write
582 * @len: Suggested size of folio to create.
583 *
584 * Returns a locked reference to the folio at @pos, or an error pointer if the
585 * folio could not be obtained.
586 */
587struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len)
588{
589 fgf_t fgp = FGP_WRITEBEGIN | FGP_NOFS;
590
591 if (iter->flags & IOMAP_NOWAIT)
592 fgp |= FGP_NOWAIT;
593 fgp |= fgf_set_order(len);
594
595 return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
596 fgp, mapping_gfp_mask(iter->inode->i_mapping));
597}
598EXPORT_SYMBOL_GPL(iomap_get_folio);
599
600bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags)
601{
602 trace_iomap_release_folio(folio->mapping->host, folio_pos(folio),
603 folio_size(folio));
604
605 /*
606 * If the folio is dirty, we refuse to release our metadata because
607 * it may be partially dirty. Once we track per-block dirty state,
608 * we can release the metadata if every block is dirty.
609 */
610 if (folio_test_dirty(folio))
611 return false;
612 ifs_free(folio);
613 return true;
614}
615EXPORT_SYMBOL_GPL(iomap_release_folio);
616
617void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
618{
619 trace_iomap_invalidate_folio(folio->mapping->host,
620 folio_pos(folio) + offset, len);
621
622 /*
623 * If we're invalidating the entire folio, clear the dirty state
624 * from it and release it to avoid unnecessary buildup of the LRU.
625 */
626 if (offset == 0 && len == folio_size(folio)) {
627 WARN_ON_ONCE(folio_test_writeback(folio));
628 folio_cancel_dirty(folio);
629 ifs_free(folio);
630 }
631}
632EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
633
634bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
635{
636 struct inode *inode = mapping->host;
637 size_t len = folio_size(folio);
638
639 ifs_alloc(inode, folio, 0);
640 iomap_set_range_dirty(folio, 0, len);
641 return filemap_dirty_folio(mapping, folio);
642}
643EXPORT_SYMBOL_GPL(iomap_dirty_folio);
644
645static void
646iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
647{
648 loff_t i_size = i_size_read(inode);
649
650 /*
651 * Only truncate newly allocated pages beyoned EOF, even if the
652 * write started inside the existing inode size.
653 */
654 if (pos + len > i_size)
655 truncate_pagecache_range(inode, max(pos, i_size),
656 pos + len - 1);
657}
658
659static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
660 size_t poff, size_t plen, const struct iomap *iomap)
661{
662 struct bio_vec bvec;
663 struct bio bio;
664
665 bio_init(&bio, iomap->bdev, &bvec, 1, REQ_OP_READ);
666 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
667 bio_add_folio_nofail(&bio, folio, plen, poff);
668 return submit_bio_wait(&bio);
669}
670
671static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
672 size_t len, struct folio *folio)
673{
674 const struct iomap *srcmap = iomap_iter_srcmap(iter);
675 struct iomap_folio_state *ifs;
676 loff_t block_size = i_blocksize(iter->inode);
677 loff_t block_start = round_down(pos, block_size);
678 loff_t block_end = round_up(pos + len, block_size);
679 unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio);
680 size_t from = offset_in_folio(folio, pos), to = from + len;
681 size_t poff, plen;
682
683 /*
684 * If the write or zeroing completely overlaps the current folio, then
685 * entire folio will be dirtied so there is no need for
686 * per-block state tracking structures to be attached to this folio.
687 * For the unshare case, we must read in the ondisk contents because we
688 * are not changing pagecache contents.
689 */
690 if (!(iter->flags & IOMAP_UNSHARE) && pos <= folio_pos(folio) &&
691 pos + len >= folio_pos(folio) + folio_size(folio))
692 return 0;
693
694 ifs = ifs_alloc(iter->inode, folio, iter->flags);
695 if ((iter->flags & IOMAP_NOWAIT) && !ifs && nr_blocks > 1)
696 return -EAGAIN;
697
698 if (folio_test_uptodate(folio))
699 return 0;
700 folio_clear_error(folio);
701
702 do {
703 iomap_adjust_read_range(iter->inode, folio, &block_start,
704 block_end - block_start, &poff, &plen);
705 if (plen == 0)
706 break;
707
708 if (!(iter->flags & IOMAP_UNSHARE) &&
709 (from <= poff || from >= poff + plen) &&
710 (to <= poff || to >= poff + plen))
711 continue;
712
713 if (iomap_block_needs_zeroing(iter, block_start)) {
714 if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
715 return -EIO;
716 folio_zero_segments(folio, poff, from, to, poff + plen);
717 } else {
718 int status;
719
720 if (iter->flags & IOMAP_NOWAIT)
721 return -EAGAIN;
722
723 status = iomap_read_folio_sync(block_start, folio,
724 poff, plen, srcmap);
725 if (status)
726 return status;
727 }
728 iomap_set_range_uptodate(folio, poff, plen);
729 } while ((block_start += plen) < block_end);
730
731 return 0;
732}
733
734static struct folio *__iomap_get_folio(struct iomap_iter *iter, loff_t pos,
735 size_t len)
736{
737 const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
738
739 if (folio_ops && folio_ops->get_folio)
740 return folio_ops->get_folio(iter, pos, len);
741 else
742 return iomap_get_folio(iter, pos, len);
743}
744
745static void __iomap_put_folio(struct iomap_iter *iter, loff_t pos, size_t ret,
746 struct folio *folio)
747{
748 const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
749
750 if (folio_ops && folio_ops->put_folio) {
751 folio_ops->put_folio(iter->inode, pos, ret, folio);
752 } else {
753 folio_unlock(folio);
754 folio_put(folio);
755 }
756}
757
758static int iomap_write_begin_inline(const struct iomap_iter *iter,
759 struct folio *folio)
760{
761 /* needs more work for the tailpacking case; disable for now */
762 if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
763 return -EIO;
764 return iomap_read_inline_data(iter, folio);
765}
766
767static int iomap_write_begin(struct iomap_iter *iter, loff_t pos,
768 size_t len, struct folio **foliop)
769{
770 const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
771 const struct iomap *srcmap = iomap_iter_srcmap(iter);
772 struct folio *folio;
773 int status = 0;
774
775 BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
776 if (srcmap != &iter->iomap)
777 BUG_ON(pos + len > srcmap->offset + srcmap->length);
778
779 if (fatal_signal_pending(current))
780 return -EINTR;
781
782 if (!mapping_large_folio_support(iter->inode->i_mapping))
783 len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
784
785 folio = __iomap_get_folio(iter, pos, len);
786 if (IS_ERR(folio))
787 return PTR_ERR(folio);
788
789 /*
790 * Now we have a locked folio, before we do anything with it we need to
791 * check that the iomap we have cached is not stale. The inode extent
792 * mapping can change due to concurrent IO in flight (e.g.
793 * IOMAP_UNWRITTEN state can change and memory reclaim could have
794 * reclaimed a previously partially written page at this index after IO
795 * completion before this write reaches this file offset) and hence we
796 * could do the wrong thing here (zero a page range incorrectly or fail
797 * to zero) and corrupt data.
798 */
799 if (folio_ops && folio_ops->iomap_valid) {
800 bool iomap_valid = folio_ops->iomap_valid(iter->inode,
801 &iter->iomap);
802 if (!iomap_valid) {
803 iter->iomap.flags |= IOMAP_F_STALE;
804 status = 0;
805 goto out_unlock;
806 }
807 }
808
809 if (pos + len > folio_pos(folio) + folio_size(folio))
810 len = folio_pos(folio) + folio_size(folio) - pos;
811
812 if (srcmap->type == IOMAP_INLINE)
813 status = iomap_write_begin_inline(iter, folio);
814 else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
815 status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
816 else
817 status = __iomap_write_begin(iter, pos, len, folio);
818
819 if (unlikely(status))
820 goto out_unlock;
821
822 *foliop = folio;
823 return 0;
824
825out_unlock:
826 __iomap_put_folio(iter, pos, 0, folio);
827 iomap_write_failed(iter->inode, pos, len);
828
829 return status;
830}
831
832static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
833 size_t copied, struct folio *folio)
834{
835 flush_dcache_folio(folio);
836
837 /*
838 * The blocks that were entirely written will now be uptodate, so we
839 * don't have to worry about a read_folio reading them and overwriting a
840 * partial write. However, if we've encountered a short write and only
841 * partially written into a block, it will not be marked uptodate, so a
842 * read_folio might come in and destroy our partial write.
843 *
844 * Do the simplest thing and just treat any short write to a
845 * non-uptodate page as a zero-length write, and force the caller to
846 * redo the whole thing.
847 */
848 if (unlikely(copied < len && !folio_test_uptodate(folio)))
849 return 0;
850 iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
851 iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
852 filemap_dirty_folio(inode->i_mapping, folio);
853 return copied;
854}
855
856static size_t iomap_write_end_inline(const struct iomap_iter *iter,
857 struct folio *folio, loff_t pos, size_t copied)
858{
859 const struct iomap *iomap = &iter->iomap;
860 void *addr;
861
862 WARN_ON_ONCE(!folio_test_uptodate(folio));
863 BUG_ON(!iomap_inline_data_valid(iomap));
864
865 flush_dcache_folio(folio);
866 addr = kmap_local_folio(folio, pos);
867 memcpy(iomap_inline_data(iomap, pos), addr, copied);
868 kunmap_local(addr);
869
870 mark_inode_dirty(iter->inode);
871 return copied;
872}
873
874/* Returns the number of bytes copied. May be 0. Cannot be an errno. */
875static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len,
876 size_t copied, struct folio *folio)
877{
878 const struct iomap *srcmap = iomap_iter_srcmap(iter);
879 loff_t old_size = iter->inode->i_size;
880 size_t ret;
881
882 if (srcmap->type == IOMAP_INLINE) {
883 ret = iomap_write_end_inline(iter, folio, pos, copied);
884 } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
885 ret = block_write_end(NULL, iter->inode->i_mapping, pos, len,
886 copied, &folio->page, NULL);
887 } else {
888 ret = __iomap_write_end(iter->inode, pos, len, copied, folio);
889 }
890
891 /*
892 * Update the in-memory inode size after copying the data into the page
893 * cache. It's up to the file system to write the updated size to disk,
894 * preferably after I/O completion so that no stale data is exposed.
895 */
896 if (pos + ret > old_size) {
897 i_size_write(iter->inode, pos + ret);
898 iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
899 }
900 __iomap_put_folio(iter, pos, ret, folio);
901
902 if (old_size < pos)
903 pagecache_isize_extended(iter->inode, old_size, pos);
904 if (ret < len)
905 iomap_write_failed(iter->inode, pos + ret, len - ret);
906 return ret;
907}
908
909static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
910{
911 loff_t length = iomap_length(iter);
912 size_t chunk = PAGE_SIZE << MAX_PAGECACHE_ORDER;
913 loff_t pos = iter->pos;
914 ssize_t written = 0;
915 long status = 0;
916 struct address_space *mapping = iter->inode->i_mapping;
917 unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0;
918
919 do {
920 struct folio *folio;
921 size_t offset; /* Offset into folio */
922 size_t bytes; /* Bytes to write to folio */
923 size_t copied; /* Bytes copied from user */
924
925 bytes = iov_iter_count(i);
926retry:
927 offset = pos & (chunk - 1);
928 bytes = min(chunk - offset, bytes);
929 status = balance_dirty_pages_ratelimited_flags(mapping,
930 bdp_flags);
931 if (unlikely(status))
932 break;
933
934 if (bytes > length)
935 bytes = length;
936
937 /*
938 * Bring in the user page that we'll copy from _first_.
939 * Otherwise there's a nasty deadlock on copying from the
940 * same page as we're writing to, without it being marked
941 * up-to-date.
942 *
943 * For async buffered writes the assumption is that the user
944 * page has already been faulted in. This can be optimized by
945 * faulting the user page.
946 */
947 if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
948 status = -EFAULT;
949 break;
950 }
951
952 status = iomap_write_begin(iter, pos, bytes, &folio);
953 if (unlikely(status))
954 break;
955 if (iter->iomap.flags & IOMAP_F_STALE)
956 break;
957
958 offset = offset_in_folio(folio, pos);
959 if (bytes > folio_size(folio) - offset)
960 bytes = folio_size(folio) - offset;
961
962 if (mapping_writably_mapped(mapping))
963 flush_dcache_folio(folio);
964
965 copied = copy_folio_from_iter_atomic(folio, offset, bytes, i);
966 status = iomap_write_end(iter, pos, bytes, copied, folio);
967
968 if (unlikely(copied != status))
969 iov_iter_revert(i, copied - status);
970
971 cond_resched();
972 if (unlikely(status == 0)) {
973 /*
974 * A short copy made iomap_write_end() reject the
975 * thing entirely. Might be memory poisoning
976 * halfway through, might be a race with munmap,
977 * might be severe memory pressure.
978 */
979 if (chunk > PAGE_SIZE)
980 chunk /= 2;
981 if (copied) {
982 bytes = copied;
983 goto retry;
984 }
985 } else {
986 pos += status;
987 written += status;
988 length -= status;
989 }
990 } while (iov_iter_count(i) && length);
991
992 if (status == -EAGAIN) {
993 iov_iter_revert(i, written);
994 return -EAGAIN;
995 }
996 return written ? written : status;
997}
998
999ssize_t
1000iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
1001 const struct iomap_ops *ops)
1002{
1003 struct iomap_iter iter = {
1004 .inode = iocb->ki_filp->f_mapping->host,
1005 .pos = iocb->ki_pos,
1006 .len = iov_iter_count(i),
1007 .flags = IOMAP_WRITE,
1008 };
1009 ssize_t ret;
1010
1011 if (iocb->ki_flags & IOCB_NOWAIT)
1012 iter.flags |= IOMAP_NOWAIT;
1013
1014 while ((ret = iomap_iter(&iter, ops)) > 0)
1015 iter.processed = iomap_write_iter(&iter, i);
1016
1017 if (unlikely(iter.pos == iocb->ki_pos))
1018 return ret;
1019 ret = iter.pos - iocb->ki_pos;
1020 iocb->ki_pos = iter.pos;
1021 return ret;
1022}
1023EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
1024
1025static int iomap_write_delalloc_ifs_punch(struct inode *inode,
1026 struct folio *folio, loff_t start_byte, loff_t end_byte,
1027 iomap_punch_t punch)
1028{
1029 unsigned int first_blk, last_blk, i;
1030 loff_t last_byte;
1031 u8 blkbits = inode->i_blkbits;
1032 struct iomap_folio_state *ifs;
1033 int ret = 0;
1034
1035 /*
1036 * When we have per-block dirty tracking, there can be
1037 * blocks within a folio which are marked uptodate
1038 * but not dirty. In that case it is necessary to punch
1039 * out such blocks to avoid leaking any delalloc blocks.
1040 */
1041 ifs = folio->private;
1042 if (!ifs)
1043 return ret;
1044
1045 last_byte = min_t(loff_t, end_byte - 1,
1046 folio_pos(folio) + folio_size(folio) - 1);
1047 first_blk = offset_in_folio(folio, start_byte) >> blkbits;
1048 last_blk = offset_in_folio(folio, last_byte) >> blkbits;
1049 for (i = first_blk; i <= last_blk; i++) {
1050 if (!ifs_block_is_dirty(folio, ifs, i)) {
1051 ret = punch(inode, folio_pos(folio) + (i << blkbits),
1052 1 << blkbits);
1053 if (ret)
1054 return ret;
1055 }
1056 }
1057
1058 return ret;
1059}
1060
1061
1062static int iomap_write_delalloc_punch(struct inode *inode, struct folio *folio,
1063 loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
1064 iomap_punch_t punch)
1065{
1066 int ret = 0;
1067
1068 if (!folio_test_dirty(folio))
1069 return ret;
1070
1071 /* if dirty, punch up to offset */
1072 if (start_byte > *punch_start_byte) {
1073 ret = punch(inode, *punch_start_byte,
1074 start_byte - *punch_start_byte);
1075 if (ret)
1076 return ret;
1077 }
1078
1079 /* Punch non-dirty blocks within folio */
1080 ret = iomap_write_delalloc_ifs_punch(inode, folio, start_byte,
1081 end_byte, punch);
1082 if (ret)
1083 return ret;
1084
1085 /*
1086 * Make sure the next punch start is correctly bound to
1087 * the end of this data range, not the end of the folio.
1088 */
1089 *punch_start_byte = min_t(loff_t, end_byte,
1090 folio_pos(folio) + folio_size(folio));
1091
1092 return ret;
1093}
1094
1095/*
1096 * Scan the data range passed to us for dirty page cache folios. If we find a
1097 * dirty folio, punch out the preceding range and update the offset from which
1098 * the next punch will start from.
1099 *
1100 * We can punch out storage reservations under clean pages because they either
1101 * contain data that has been written back - in which case the delalloc punch
1102 * over that range is a no-op - or they have been read faults in which case they
1103 * contain zeroes and we can remove the delalloc backing range and any new
1104 * writes to those pages will do the normal hole filling operation...
1105 *
1106 * This makes the logic simple: we only need to keep the delalloc extents only
1107 * over the dirty ranges of the page cache.
1108 *
1109 * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
1110 * simplify range iterations.
1111 */
1112static int iomap_write_delalloc_scan(struct inode *inode,
1113 loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
1114 iomap_punch_t punch)
1115{
1116 while (start_byte < end_byte) {
1117 struct folio *folio;
1118 int ret;
1119
1120 /* grab locked page */
1121 folio = filemap_lock_folio(inode->i_mapping,
1122 start_byte >> PAGE_SHIFT);
1123 if (IS_ERR(folio)) {
1124 start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) +
1125 PAGE_SIZE;
1126 continue;
1127 }
1128
1129 ret = iomap_write_delalloc_punch(inode, folio, punch_start_byte,
1130 start_byte, end_byte, punch);
1131 if (ret) {
1132 folio_unlock(folio);
1133 folio_put(folio);
1134 return ret;
1135 }
1136
1137 /* move offset to start of next folio in range */
1138 start_byte = folio_next_index(folio) << PAGE_SHIFT;
1139 folio_unlock(folio);
1140 folio_put(folio);
1141 }
1142 return 0;
1143}
1144
1145/*
1146 * Punch out all the delalloc blocks in the range given except for those that
1147 * have dirty data still pending in the page cache - those are going to be
1148 * written and so must still retain the delalloc backing for writeback.
1149 *
1150 * As we are scanning the page cache for data, we don't need to reimplement the
1151 * wheel - mapping_seek_hole_data() does exactly what we need to identify the
1152 * start and end of data ranges correctly even for sub-folio block sizes. This
1153 * byte range based iteration is especially convenient because it means we
1154 * don't have to care about variable size folios, nor where the start or end of
1155 * the data range lies within a folio, if they lie within the same folio or even
1156 * if there are multiple discontiguous data ranges within the folio.
1157 *
1158 * It should be noted that mapping_seek_hole_data() is not aware of EOF, and so
1159 * can return data ranges that exist in the cache beyond EOF. e.g. a page fault
1160 * spanning EOF will initialise the post-EOF data to zeroes and mark it up to
1161 * date. A write page fault can then mark it dirty. If we then fail a write()
1162 * beyond EOF into that up to date cached range, we allocate a delalloc block
1163 * beyond EOF and then have to punch it out. Because the range is up to date,
1164 * mapping_seek_hole_data() will return it, and we will skip the punch because
1165 * the folio is dirty. THis is incorrect - we always need to punch out delalloc
1166 * beyond EOF in this case as writeback will never write back and covert that
1167 * delalloc block beyond EOF. Hence we limit the cached data scan range to EOF,
1168 * resulting in always punching out the range from the EOF to the end of the
1169 * range the iomap spans.
1170 *
1171 * Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it
1172 * matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA
1173 * returns the start of a data range (start_byte), and SEEK_HOLE(start_byte)
1174 * returns the end of the data range (data_end). Using closed intervals would
1175 * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose
1176 * the code to subtle off-by-one bugs....
1177 */
1178static int iomap_write_delalloc_release(struct inode *inode,
1179 loff_t start_byte, loff_t end_byte, iomap_punch_t punch)
1180{
1181 loff_t punch_start_byte = start_byte;
1182 loff_t scan_end_byte = min(i_size_read(inode), end_byte);
1183 int error = 0;
1184
1185 /*
1186 * Lock the mapping to avoid races with page faults re-instantiating
1187 * folios and dirtying them via ->page_mkwrite whilst we walk the
1188 * cache and perform delalloc extent removal. Failing to do this can
1189 * leave dirty pages with no space reservation in the cache.
1190 */
1191 filemap_invalidate_lock(inode->i_mapping);
1192 while (start_byte < scan_end_byte) {
1193 loff_t data_end;
1194
1195 start_byte = mapping_seek_hole_data(inode->i_mapping,
1196 start_byte, scan_end_byte, SEEK_DATA);
1197 /*
1198 * If there is no more data to scan, all that is left is to
1199 * punch out the remaining range.
1200 */
1201 if (start_byte == -ENXIO || start_byte == scan_end_byte)
1202 break;
1203 if (start_byte < 0) {
1204 error = start_byte;
1205 goto out_unlock;
1206 }
1207 WARN_ON_ONCE(start_byte < punch_start_byte);
1208 WARN_ON_ONCE(start_byte > scan_end_byte);
1209
1210 /*
1211 * We find the end of this contiguous cached data range by
1212 * seeking from start_byte to the beginning of the next hole.
1213 */
1214 data_end = mapping_seek_hole_data(inode->i_mapping, start_byte,
1215 scan_end_byte, SEEK_HOLE);
1216 if (data_end < 0) {
1217 error = data_end;
1218 goto out_unlock;
1219 }
1220 WARN_ON_ONCE(data_end <= start_byte);
1221 WARN_ON_ONCE(data_end > scan_end_byte);
1222
1223 error = iomap_write_delalloc_scan(inode, &punch_start_byte,
1224 start_byte, data_end, punch);
1225 if (error)
1226 goto out_unlock;
1227
1228 /* The next data search starts at the end of this one. */
1229 start_byte = data_end;
1230 }
1231
1232 if (punch_start_byte < end_byte)
1233 error = punch(inode, punch_start_byte,
1234 end_byte - punch_start_byte);
1235out_unlock:
1236 filemap_invalidate_unlock(inode->i_mapping);
1237 return error;
1238}
1239
1240/*
1241 * When a short write occurs, the filesystem may need to remove reserved space
1242 * that was allocated in ->iomap_begin from it's ->iomap_end method. For
1243 * filesystems that use delayed allocation, we need to punch out delalloc
1244 * extents from the range that are not dirty in the page cache. As the write can
1245 * race with page faults, there can be dirty pages over the delalloc extent
1246 * outside the range of a short write but still within the delalloc extent
1247 * allocated for this iomap.
1248 *
1249 * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
1250 * simplify range iterations.
1251 *
1252 * The punch() callback *must* only punch delalloc extents in the range passed
1253 * to it. It must skip over all other types of extents in the range and leave
1254 * them completely unchanged. It must do this punch atomically with respect to
1255 * other extent modifications.
1256 *
1257 * The punch() callback may be called with a folio locked to prevent writeback
1258 * extent allocation racing at the edge of the range we are currently punching.
1259 * The locked folio may or may not cover the range being punched, so it is not
1260 * safe for the punch() callback to lock folios itself.
1261 *
1262 * Lock order is:
1263 *
1264 * inode->i_rwsem (shared or exclusive)
1265 * inode->i_mapping->invalidate_lock (exclusive)
1266 * folio_lock()
1267 * ->punch
1268 * internal filesystem allocation lock
1269 */
1270int iomap_file_buffered_write_punch_delalloc(struct inode *inode,
1271 struct iomap *iomap, loff_t pos, loff_t length,
1272 ssize_t written, iomap_punch_t punch)
1273{
1274 loff_t start_byte;
1275 loff_t end_byte;
1276 unsigned int blocksize = i_blocksize(inode);
1277
1278 if (iomap->type != IOMAP_DELALLOC)
1279 return 0;
1280
1281 /* If we didn't reserve the blocks, we're not allowed to punch them. */
1282 if (!(iomap->flags & IOMAP_F_NEW))
1283 return 0;
1284
1285 /*
1286 * start_byte refers to the first unused block after a short write. If
1287 * nothing was written, round offset down to point at the first block in
1288 * the range.
1289 */
1290 if (unlikely(!written))
1291 start_byte = round_down(pos, blocksize);
1292 else
1293 start_byte = round_up(pos + written, blocksize);
1294 end_byte = round_up(pos + length, blocksize);
1295
1296 /* Nothing to do if we've written the entire delalloc extent */
1297 if (start_byte >= end_byte)
1298 return 0;
1299
1300 return iomap_write_delalloc_release(inode, start_byte, end_byte,
1301 punch);
1302}
1303EXPORT_SYMBOL_GPL(iomap_file_buffered_write_punch_delalloc);
1304
1305static loff_t iomap_unshare_iter(struct iomap_iter *iter)
1306{
1307 struct iomap *iomap = &iter->iomap;
1308 const struct iomap *srcmap = iomap_iter_srcmap(iter);
1309 loff_t pos = iter->pos;
1310 loff_t length = iomap_length(iter);
1311 loff_t written = 0;
1312
1313 /* don't bother with blocks that are not shared to start with */
1314 if (!(iomap->flags & IOMAP_F_SHARED))
1315 return length;
1316 /* don't bother with holes or unwritten extents */
1317 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
1318 return length;
1319
1320 do {
1321 struct folio *folio;
1322 int status;
1323 size_t offset;
1324 size_t bytes = min_t(u64, SIZE_MAX, length);
1325
1326 status = iomap_write_begin(iter, pos, bytes, &folio);
1327 if (unlikely(status))
1328 return status;
1329 if (iomap->flags & IOMAP_F_STALE)
1330 break;
1331
1332 offset = offset_in_folio(folio, pos);
1333 if (bytes > folio_size(folio) - offset)
1334 bytes = folio_size(folio) - offset;
1335
1336 bytes = iomap_write_end(iter, pos, bytes, bytes, folio);
1337 if (WARN_ON_ONCE(bytes == 0))
1338 return -EIO;
1339
1340 cond_resched();
1341
1342 pos += bytes;
1343 written += bytes;
1344 length -= bytes;
1345
1346 balance_dirty_pages_ratelimited(iter->inode->i_mapping);
1347 } while (length > 0);
1348
1349 return written;
1350}
1351
1352int
1353iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
1354 const struct iomap_ops *ops)
1355{
1356 struct iomap_iter iter = {
1357 .inode = inode,
1358 .pos = pos,
1359 .len = len,
1360 .flags = IOMAP_WRITE | IOMAP_UNSHARE,
1361 };
1362 int ret;
1363
1364 while ((ret = iomap_iter(&iter, ops)) > 0)
1365 iter.processed = iomap_unshare_iter(&iter);
1366 return ret;
1367}
1368EXPORT_SYMBOL_GPL(iomap_file_unshare);
1369
1370static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
1371{
1372 const struct iomap *srcmap = iomap_iter_srcmap(iter);
1373 loff_t pos = iter->pos;
1374 loff_t length = iomap_length(iter);
1375 loff_t written = 0;
1376
1377 /* already zeroed? we're done. */
1378 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
1379 return length;
1380
1381 do {
1382 struct folio *folio;
1383 int status;
1384 size_t offset;
1385 size_t bytes = min_t(u64, SIZE_MAX, length);
1386
1387 status = iomap_write_begin(iter, pos, bytes, &folio);
1388 if (status)
1389 return status;
1390 if (iter->iomap.flags & IOMAP_F_STALE)
1391 break;
1392
1393 offset = offset_in_folio(folio, pos);
1394 if (bytes > folio_size(folio) - offset)
1395 bytes = folio_size(folio) - offset;
1396
1397 folio_zero_range(folio, offset, bytes);
1398 folio_mark_accessed(folio);
1399
1400 bytes = iomap_write_end(iter, pos, bytes, bytes, folio);
1401 if (WARN_ON_ONCE(bytes == 0))
1402 return -EIO;
1403
1404 pos += bytes;
1405 length -= bytes;
1406 written += bytes;
1407 } while (length > 0);
1408
1409 if (did_zero)
1410 *did_zero = true;
1411 return written;
1412}
1413
1414int
1415iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1416 const struct iomap_ops *ops)
1417{
1418 struct iomap_iter iter = {
1419 .inode = inode,
1420 .pos = pos,
1421 .len = len,
1422 .flags = IOMAP_ZERO,
1423 };
1424 int ret;
1425
1426 while ((ret = iomap_iter(&iter, ops)) > 0)
1427 iter.processed = iomap_zero_iter(&iter, did_zero);
1428 return ret;
1429}
1430EXPORT_SYMBOL_GPL(iomap_zero_range);
1431
1432int
1433iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1434 const struct iomap_ops *ops)
1435{
1436 unsigned int blocksize = i_blocksize(inode);
1437 unsigned int off = pos & (blocksize - 1);
1438
1439 /* Block boundary? Nothing to do */
1440 if (!off)
1441 return 0;
1442 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1443}
1444EXPORT_SYMBOL_GPL(iomap_truncate_page);
1445
1446static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter,
1447 struct folio *folio)
1448{
1449 loff_t length = iomap_length(iter);
1450 int ret;
1451
1452 if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
1453 ret = __block_write_begin_int(folio, iter->pos, length, NULL,
1454 &iter->iomap);
1455 if (ret)
1456 return ret;
1457 block_commit_write(&folio->page, 0, length);
1458 } else {
1459 WARN_ON_ONCE(!folio_test_uptodate(folio));
1460 folio_mark_dirty(folio);
1461 }
1462
1463 return length;
1464}
1465
1466vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1467{
1468 struct iomap_iter iter = {
1469 .inode = file_inode(vmf->vma->vm_file),
1470 .flags = IOMAP_WRITE | IOMAP_FAULT,
1471 };
1472 struct folio *folio = page_folio(vmf->page);
1473 ssize_t ret;
1474
1475 folio_lock(folio);
1476 ret = folio_mkwrite_check_truncate(folio, iter.inode);
1477 if (ret < 0)
1478 goto out_unlock;
1479 iter.pos = folio_pos(folio);
1480 iter.len = ret;
1481 while ((ret = iomap_iter(&iter, ops)) > 0)
1482 iter.processed = iomap_folio_mkwrite_iter(&iter, folio);
1483
1484 if (ret < 0)
1485 goto out_unlock;
1486 folio_wait_stable(folio);
1487 return VM_FAULT_LOCKED;
1488out_unlock:
1489 folio_unlock(folio);
1490 return vmf_fs_error(ret);
1491}
1492EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1493
1494static void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
1495 size_t len)
1496{
1497 struct iomap_folio_state *ifs = folio->private;
1498
1499 WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs);
1500 WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
1501
1502 if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
1503 folio_end_writeback(folio);
1504}
1505
1506/*
1507 * We're now finished for good with this ioend structure. Update the page
1508 * state, release holds on bios, and finally free up memory. Do not use the
1509 * ioend after this.
1510 */
1511static u32
1512iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1513{
1514 struct inode *inode = ioend->io_inode;
1515 struct bio *bio = &ioend->io_bio;
1516 struct folio_iter fi;
1517 u32 folio_count = 0;
1518
1519 if (error) {
1520 mapping_set_error(inode->i_mapping, error);
1521 if (!bio_flagged(bio, BIO_QUIET)) {
1522 pr_err_ratelimited(
1523"%s: writeback error on inode %lu, offset %lld, sector %llu",
1524 inode->i_sb->s_id, inode->i_ino,
1525 ioend->io_offset, ioend->io_sector);
1526 }
1527 }
1528
1529 /* walk all folios in bio, ending page IO on them */
1530 bio_for_each_folio_all(fi, bio) {
1531 if (error)
1532 folio_set_error(fi.folio);
1533 iomap_finish_folio_write(inode, fi.folio, fi.length);
1534 folio_count++;
1535 }
1536
1537 bio_put(bio); /* frees the ioend */
1538 return folio_count;
1539}
1540
1541/*
1542 * Ioend completion routine for merged bios. This can only be called from task
1543 * contexts as merged ioends can be of unbound length. Hence we have to break up
1544 * the writeback completions into manageable chunks to avoid long scheduler
1545 * holdoffs. We aim to keep scheduler holdoffs down below 10ms so that we get
1546 * good batch processing throughput without creating adverse scheduler latency
1547 * conditions.
1548 */
1549void
1550iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1551{
1552 struct list_head tmp;
1553 u32 completions;
1554
1555 might_sleep();
1556
1557 list_replace_init(&ioend->io_list, &tmp);
1558 completions = iomap_finish_ioend(ioend, error);
1559
1560 while (!list_empty(&tmp)) {
1561 if (completions > IOEND_BATCH_SIZE * 8) {
1562 cond_resched();
1563 completions = 0;
1564 }
1565 ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1566 list_del_init(&ioend->io_list);
1567 completions += iomap_finish_ioend(ioend, error);
1568 }
1569}
1570EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1571
1572/*
1573 * We can merge two adjacent ioends if they have the same set of work to do.
1574 */
1575static bool
1576iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1577{
1578 if (ioend->io_bio.bi_status != next->io_bio.bi_status)
1579 return false;
1580 if ((ioend->io_flags & IOMAP_F_SHARED) ^
1581 (next->io_flags & IOMAP_F_SHARED))
1582 return false;
1583 if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1584 (next->io_type == IOMAP_UNWRITTEN))
1585 return false;
1586 if (ioend->io_offset + ioend->io_size != next->io_offset)
1587 return false;
1588 /*
1589 * Do not merge physically discontiguous ioends. The filesystem
1590 * completion functions will have to iterate the physical
1591 * discontiguities even if we merge the ioends at a logical level, so
1592 * we don't gain anything by merging physical discontiguities here.
1593 *
1594 * We cannot use bio->bi_iter.bi_sector here as it is modified during
1595 * submission so does not point to the start sector of the bio at
1596 * completion.
1597 */
1598 if (ioend->io_sector + (ioend->io_size >> 9) != next->io_sector)
1599 return false;
1600 return true;
1601}
1602
1603void
1604iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
1605{
1606 struct iomap_ioend *next;
1607
1608 INIT_LIST_HEAD(&ioend->io_list);
1609
1610 while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1611 io_list))) {
1612 if (!iomap_ioend_can_merge(ioend, next))
1613 break;
1614 list_move_tail(&next->io_list, &ioend->io_list);
1615 ioend->io_size += next->io_size;
1616 }
1617}
1618EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1619
1620static int
1621iomap_ioend_compare(void *priv, const struct list_head *a,
1622 const struct list_head *b)
1623{
1624 struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1625 struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
1626
1627 if (ia->io_offset < ib->io_offset)
1628 return -1;
1629 if (ia->io_offset > ib->io_offset)
1630 return 1;
1631 return 0;
1632}
1633
1634void
1635iomap_sort_ioends(struct list_head *ioend_list)
1636{
1637 list_sort(NULL, ioend_list, iomap_ioend_compare);
1638}
1639EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1640
1641static void iomap_writepage_end_bio(struct bio *bio)
1642{
1643 iomap_finish_ioend(iomap_ioend_from_bio(bio),
1644 blk_status_to_errno(bio->bi_status));
1645}
1646
1647/*
1648 * Submit the final bio for an ioend.
1649 *
1650 * If @error is non-zero, it means that we have a situation where some part of
1651 * the submission process has failed after we've marked pages for writeback.
1652 * We cannot cancel ioend directly in that case, so call the bio end I/O handler
1653 * with the error status here to run the normal I/O completion handler to clear
1654 * the writeback bit and let the file system proess the errors.
1655 */
1656static int iomap_submit_ioend(struct iomap_writepage_ctx *wpc, int error)
1657{
1658 if (!wpc->ioend)
1659 return error;
1660
1661 /*
1662 * Let the file systems prepare the I/O submission and hook in an I/O
1663 * comletion handler. This also needs to happen in case after a
1664 * failure happened so that the file system end I/O handler gets called
1665 * to clean up.
1666 */
1667 if (wpc->ops->prepare_ioend)
1668 error = wpc->ops->prepare_ioend(wpc->ioend, error);
1669
1670 if (error) {
1671 wpc->ioend->io_bio.bi_status = errno_to_blk_status(error);
1672 bio_endio(&wpc->ioend->io_bio);
1673 } else {
1674 submit_bio(&wpc->ioend->io_bio);
1675 }
1676
1677 wpc->ioend = NULL;
1678 return error;
1679}
1680
1681static struct iomap_ioend *iomap_alloc_ioend(struct iomap_writepage_ctx *wpc,
1682 struct writeback_control *wbc, struct inode *inode, loff_t pos)
1683{
1684 struct iomap_ioend *ioend;
1685 struct bio *bio;
1686
1687 bio = bio_alloc_bioset(wpc->iomap.bdev, BIO_MAX_VECS,
1688 REQ_OP_WRITE | wbc_to_write_flags(wbc),
1689 GFP_NOFS, &iomap_ioend_bioset);
1690 bio->bi_iter.bi_sector = iomap_sector(&wpc->iomap, pos);
1691 bio->bi_end_io = iomap_writepage_end_bio;
1692 wbc_init_bio(wbc, bio);
1693 bio->bi_write_hint = inode->i_write_hint;
1694
1695 ioend = iomap_ioend_from_bio(bio);
1696 INIT_LIST_HEAD(&ioend->io_list);
1697 ioend->io_type = wpc->iomap.type;
1698 ioend->io_flags = wpc->iomap.flags;
1699 ioend->io_inode = inode;
1700 ioend->io_size = 0;
1701 ioend->io_offset = pos;
1702 ioend->io_sector = bio->bi_iter.bi_sector;
1703
1704 wpc->nr_folios = 0;
1705 return ioend;
1706}
1707
1708static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos)
1709{
1710 if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1711 (wpc->ioend->io_flags & IOMAP_F_SHARED))
1712 return false;
1713 if (wpc->iomap.type != wpc->ioend->io_type)
1714 return false;
1715 if (pos != wpc->ioend->io_offset + wpc->ioend->io_size)
1716 return false;
1717 if (iomap_sector(&wpc->iomap, pos) !=
1718 bio_end_sector(&wpc->ioend->io_bio))
1719 return false;
1720 /*
1721 * Limit ioend bio chain lengths to minimise IO completion latency. This
1722 * also prevents long tight loops ending page writeback on all the
1723 * folios in the ioend.
1724 */
1725 if (wpc->nr_folios >= IOEND_BATCH_SIZE)
1726 return false;
1727 return true;
1728}
1729
1730/*
1731 * Test to see if we have an existing ioend structure that we could append to
1732 * first; otherwise finish off the current ioend and start another.
1733 *
1734 * If a new ioend is created and cached, the old ioend is submitted to the block
1735 * layer instantly. Batching optimisations are provided by higher level block
1736 * plugging.
1737 *
1738 * At the end of a writeback pass, there will be a cached ioend remaining on the
1739 * writepage context that the caller will need to submit.
1740 */
1741static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,
1742 struct writeback_control *wbc, struct folio *folio,
1743 struct inode *inode, loff_t pos, unsigned len)
1744{
1745 struct iomap_folio_state *ifs = folio->private;
1746 size_t poff = offset_in_folio(folio, pos);
1747 int error;
1748
1749 if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, pos)) {
1750new_ioend:
1751 error = iomap_submit_ioend(wpc, 0);
1752 if (error)
1753 return error;
1754 wpc->ioend = iomap_alloc_ioend(wpc, wbc, inode, pos);
1755 }
1756
1757 if (!bio_add_folio(&wpc->ioend->io_bio, folio, len, poff))
1758 goto new_ioend;
1759
1760 if (ifs)
1761 atomic_add(len, &ifs->write_bytes_pending);
1762 wpc->ioend->io_size += len;
1763 wbc_account_cgroup_owner(wbc, &folio->page, len);
1764 return 0;
1765}
1766
1767static int iomap_writepage_map_blocks(struct iomap_writepage_ctx *wpc,
1768 struct writeback_control *wbc, struct folio *folio,
1769 struct inode *inode, u64 pos, unsigned dirty_len,
1770 unsigned *count)
1771{
1772 int error;
1773
1774 do {
1775 unsigned map_len;
1776
1777 error = wpc->ops->map_blocks(wpc, inode, pos, dirty_len);
1778 if (error)
1779 break;
1780 trace_iomap_writepage_map(inode, pos, dirty_len, &wpc->iomap);
1781
1782 map_len = min_t(u64, dirty_len,
1783 wpc->iomap.offset + wpc->iomap.length - pos);
1784 WARN_ON_ONCE(!folio->private && map_len < dirty_len);
1785
1786 switch (wpc->iomap.type) {
1787 case IOMAP_INLINE:
1788 WARN_ON_ONCE(1);
1789 error = -EIO;
1790 break;
1791 case IOMAP_HOLE:
1792 break;
1793 default:
1794 error = iomap_add_to_ioend(wpc, wbc, folio, inode, pos,
1795 map_len);
1796 if (!error)
1797 (*count)++;
1798 break;
1799 }
1800 dirty_len -= map_len;
1801 pos += map_len;
1802 } while (dirty_len && !error);
1803
1804 /*
1805 * We cannot cancel the ioend directly here on error. We may have
1806 * already set other pages under writeback and hence we have to run I/O
1807 * completion to mark the error state of the pages under writeback
1808 * appropriately.
1809 *
1810 * Just let the file system know what portion of the folio failed to
1811 * map.
1812 */
1813 if (error && wpc->ops->discard_folio)
1814 wpc->ops->discard_folio(folio, pos);
1815 return error;
1816}
1817
1818/*
1819 * Check interaction of the folio with the file end.
1820 *
1821 * If the folio is entirely beyond i_size, return false. If it straddles
1822 * i_size, adjust end_pos and zero all data beyond i_size.
1823 */
1824static bool iomap_writepage_handle_eof(struct folio *folio, struct inode *inode,
1825 u64 *end_pos)
1826{
1827 u64 isize = i_size_read(inode);
1828
1829 if (*end_pos > isize) {
1830 size_t poff = offset_in_folio(folio, isize);
1831 pgoff_t end_index = isize >> PAGE_SHIFT;
1832
1833 /*
1834 * If the folio is entirely ouside of i_size, skip it.
1835 *
1836 * This can happen due to a truncate operation that is in
1837 * progress and in that case truncate will finish it off once
1838 * we've dropped the folio lock.
1839 *
1840 * Note that the pgoff_t used for end_index is an unsigned long.
1841 * If the given offset is greater than 16TB on a 32-bit system,
1842 * then if we checked if the folio is fully outside i_size with
1843 * "if (folio->index >= end_index + 1)", "end_index + 1" would
1844 * overflow and evaluate to 0. Hence this folio would be
1845 * redirtied and written out repeatedly, which would result in
1846 * an infinite loop; the user program performing this operation
1847 * would hang. Instead, we can detect this situation by
1848 * checking if the folio is totally beyond i_size or if its
1849 * offset is just equal to the EOF.
1850 */
1851 if (folio->index > end_index ||
1852 (folio->index == end_index && poff == 0))
1853 return false;
1854
1855 /*
1856 * The folio straddles i_size.
1857 *
1858 * It must be zeroed out on each and every writepage invocation
1859 * because it may be mmapped:
1860 *
1861 * A file is mapped in multiples of the page size. For a
1862 * file that is not a multiple of the page size, the
1863 * remaining memory is zeroed when mapped, and writes to that
1864 * region are not written out to the file.
1865 *
1866 * Also adjust the writeback range to skip all blocks entirely
1867 * beyond i_size.
1868 */
1869 folio_zero_segment(folio, poff, folio_size(folio));
1870 *end_pos = round_up(isize, i_blocksize(inode));
1871 }
1872
1873 return true;
1874}
1875
1876static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1877 struct writeback_control *wbc, struct folio *folio)
1878{
1879 struct iomap_folio_state *ifs = folio->private;
1880 struct inode *inode = folio->mapping->host;
1881 u64 pos = folio_pos(folio);
1882 u64 end_pos = pos + folio_size(folio);
1883 unsigned count = 0;
1884 int error = 0;
1885 u32 rlen;
1886
1887 WARN_ON_ONCE(!folio_test_locked(folio));
1888 WARN_ON_ONCE(folio_test_dirty(folio));
1889 WARN_ON_ONCE(folio_test_writeback(folio));
1890
1891 trace_iomap_writepage(inode, pos, folio_size(folio));
1892
1893 if (!iomap_writepage_handle_eof(folio, inode, &end_pos)) {
1894 folio_unlock(folio);
1895 return 0;
1896 }
1897 WARN_ON_ONCE(end_pos <= pos);
1898
1899 if (i_blocks_per_folio(inode, folio) > 1) {
1900 if (!ifs) {
1901 ifs = ifs_alloc(inode, folio, 0);
1902 iomap_set_range_dirty(folio, 0, end_pos - pos);
1903 }
1904
1905 /*
1906 * Keep the I/O completion handler from clearing the writeback
1907 * bit until we have submitted all blocks by adding a bias to
1908 * ifs->write_bytes_pending, which is dropped after submitting
1909 * all blocks.
1910 */
1911 WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending) != 0);
1912 atomic_inc(&ifs->write_bytes_pending);
1913 }
1914
1915 /*
1916 * Set the writeback bit ASAP, as the I/O completion for the single
1917 * block per folio case happen hit as soon as we're submitting the bio.
1918 */
1919 folio_start_writeback(folio);
1920
1921 /*
1922 * Walk through the folio to find dirty areas to write back.
1923 */
1924 while ((rlen = iomap_find_dirty_range(folio, &pos, end_pos))) {
1925 error = iomap_writepage_map_blocks(wpc, wbc, folio, inode,
1926 pos, rlen, &count);
1927 if (error)
1928 break;
1929 pos += rlen;
1930 }
1931
1932 if (count)
1933 wpc->nr_folios++;
1934
1935 /*
1936 * We can have dirty bits set past end of file in page_mkwrite path
1937 * while mapping the last partial folio. Hence it's better to clear
1938 * all the dirty bits in the folio here.
1939 */
1940 iomap_clear_range_dirty(folio, 0, folio_size(folio));
1941
1942 /*
1943 * Usually the writeback bit is cleared by the I/O completion handler.
1944 * But we may end up either not actually writing any blocks, or (when
1945 * there are multiple blocks in a folio) all I/O might have finished
1946 * already at this point. In that case we need to clear the writeback
1947 * bit ourselves right after unlocking the page.
1948 */
1949 folio_unlock(folio);
1950 if (ifs) {
1951 if (atomic_dec_and_test(&ifs->write_bytes_pending))
1952 folio_end_writeback(folio);
1953 } else {
1954 if (!count)
1955 folio_end_writeback(folio);
1956 }
1957 mapping_set_error(inode->i_mapping, error);
1958 return error;
1959}
1960
1961static int iomap_do_writepage(struct folio *folio,
1962 struct writeback_control *wbc, void *data)
1963{
1964 return iomap_writepage_map(data, wbc, folio);
1965}
1966
1967int
1968iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1969 struct iomap_writepage_ctx *wpc,
1970 const struct iomap_writeback_ops *ops)
1971{
1972 int ret;
1973
1974 /*
1975 * Writeback from reclaim context should never happen except in the case
1976 * of a VM regression so warn about it and refuse to write the data.
1977 */
1978 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC | PF_KSWAPD)) ==
1979 PF_MEMALLOC))
1980 return -EIO;
1981
1982 wpc->ops = ops;
1983 ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
1984 return iomap_submit_ioend(wpc, ret);
1985}
1986EXPORT_SYMBOL_GPL(iomap_writepages);
1987
1988static int __init iomap_init(void)
1989{
1990 return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
1991 offsetof(struct iomap_ioend, io_bio),
1992 BIOSET_NEED_BVECS);
1993}
1994fs_initcall(iomap_init);