blob: c938bbad075e1ff72631ff4e93728d395f237126 [file] [log] [blame]
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07004 * Copyright (C) 2016-2019 Christoph Hellwig.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07005 */
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>
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070015#include <linux/list_sort.h>
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070016#include <linux/swap.h>
17#include <linux/bio.h>
18#include <linux/sched/signal.h>
19#include <linux/migrate.h>
Christoph Hellwig9e91c572019-10-17 13:12:13 -070020#include "trace.h"
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070021
22#include "../internal.h"
23
Christoph Hellwigab08b012019-10-17 13:12:19 -070024/*
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -040025 * Structure allocated for each folio when block size < folio size
26 * to track sub-folio uptodate status and I/O completions.
Christoph Hellwigab08b012019-10-17 13:12:19 -070027 */
28struct iomap_page {
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -070029 atomic_t read_bytes_pending;
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -070030 atomic_t write_bytes_pending;
Christoph Hellwig1cea3352019-12-04 09:33:52 -080031 spinlock_t uptodate_lock;
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070032 unsigned long uptodate[];
Christoph Hellwigab08b012019-10-17 13:12:19 -070033};
34
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -040035static inline struct iomap_page *to_iomap_page(struct folio *folio)
Christoph Hellwigab08b012019-10-17 13:12:19 -070036{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -040037 if (folio_test_private(folio))
38 return folio_get_private(folio);
Christoph Hellwigab08b012019-10-17 13:12:19 -070039 return NULL;
40}
41
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070042static struct bio_set iomap_ioend_bioset;
43
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070044static struct iomap_page *
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -040045iomap_page_create(struct inode *inode, struct folio *folio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070046{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -040047 struct iomap_page *iop = to_iomap_page(folio);
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -040048 unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070049
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070050 if (iop || nr_blocks <= 1)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070051 return iop;
52
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070053 iop = kzalloc(struct_size(iop, uptodate, BITS_TO_LONGS(nr_blocks)),
54 GFP_NOFS | __GFP_NOFAIL);
Christoph Hellwig1cea3352019-12-04 09:33:52 -080055 spin_lock_init(&iop->uptodate_lock);
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -040056 if (folio_test_uptodate(folio))
Matthew Wilcox (Oracle)4595a292020-09-25 11:16:53 -070057 bitmap_fill(iop->uptodate, nr_blocks);
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -040058 folio_attach_private(folio, iop);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070059 return iop;
60}
61
Matthew Wilcox (Oracle)c46e8322021-04-27 23:22:22 -040062static void iomap_page_release(struct folio *folio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070063{
Matthew Wilcox (Oracle)c46e8322021-04-27 23:22:22 -040064 struct iomap_page *iop = folio_detach_private(folio);
65 struct inode *inode = folio->mapping->host;
66 unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070067
68 if (!iop)
69 return;
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -070070 WARN_ON_ONCE(atomic_read(&iop->read_bytes_pending));
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -070071 WARN_ON_ONCE(atomic_read(&iop->write_bytes_pending));
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070072 WARN_ON_ONCE(bitmap_full(iop->uptodate, nr_blocks) !=
Matthew Wilcox (Oracle)c46e8322021-04-27 23:22:22 -040073 folio_test_uptodate(folio));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070074 kfree(iop);
75}
76
77/*
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -040078 * Calculate the range inside the folio that we actually need to read.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070079 */
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -040080static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
81 loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070082{
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -040083 struct iomap_page *iop = to_iomap_page(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070084 loff_t orig_pos = *pos;
85 loff_t isize = i_size_read(inode);
86 unsigned block_bits = inode->i_blkbits;
87 unsigned block_size = (1 << block_bits);
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -040088 size_t poff = offset_in_folio(folio, *pos);
89 size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070090 unsigned first = poff >> block_bits;
91 unsigned last = (poff + plen - 1) >> block_bits;
92
93 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -070094 * If the block size is smaller than the page size, we need to check the
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070095 * per-block uptodate status and adjust the offset and length if needed
96 * to avoid reading in already uptodate ranges.
97 */
98 if (iop) {
99 unsigned int i;
100
101 /* move forward for each leading block marked uptodate */
102 for (i = first; i <= last; i++) {
103 if (!test_bit(i, iop->uptodate))
104 break;
105 *pos += block_size;
106 poff += block_size;
107 plen -= block_size;
108 first++;
109 }
110
111 /* truncate len if we find any trailing uptodate block(s) */
112 for ( ; i <= last; i++) {
113 if (test_bit(i, iop->uptodate)) {
114 plen -= (last - i + 1) * block_size;
115 last = i - 1;
116 break;
117 }
118 }
119 }
120
121 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700122 * If the extent spans the block that contains the i_size, we need to
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700123 * handle both halves separately so that we properly zero data in the
124 * page cache for blocks that are entirely outside of i_size.
125 */
126 if (orig_pos <= isize && orig_pos + length > isize) {
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400127 unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700128
129 if (first <= end && last > end)
130 plen -= (last - end) * block_size;
131 }
132
133 *offp = poff;
134 *lenp = plen;
135}
136
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400137static void iomap_iop_set_range_uptodate(struct folio *folio,
138 struct iomap_page *iop, size_t off, size_t len)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700139{
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400140 struct inode *inode = folio->mapping->host;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700141 unsigned first = off >> inode->i_blkbits;
142 unsigned last = (off + len - 1) >> inode->i_blkbits;
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800143 unsigned long flags;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700144
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800145 spin_lock_irqsave(&iop->uptodate_lock, flags);
Matthew Wilcox (Oracle)b21866f2020-09-21 08:58:40 -0700146 bitmap_set(iop->uptodate, first, last - first + 1);
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400147 if (bitmap_full(iop->uptodate, i_blocks_per_folio(inode, folio)))
148 folio_mark_uptodate(folio);
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800149 spin_unlock_irqrestore(&iop->uptodate_lock, flags);
150}
151
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400152static void iomap_set_range_uptodate(struct folio *folio,
153 struct iomap_page *iop, size_t off, size_t len)
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800154{
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400155 if (folio_test_error(folio))
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800156 return;
157
Matthew Wilcox (Oracle)cd1e5af2021-04-28 08:16:50 -0400158 if (iop)
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400159 iomap_iop_set_range_uptodate(folio, iop, off, len);
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800160 else
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400161 folio_mark_uptodate(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700162}
163
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -0500164static void iomap_finish_folio_read(struct folio *folio, size_t offset,
165 size_t len, int error)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700166{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -0400167 struct iomap_page *iop = to_iomap_page(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700168
169 if (unlikely(error)) {
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -0500170 folio_clear_uptodate(folio);
171 folio_set_error(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700172 } else {
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400173 iomap_set_range_uptodate(folio, iop, offset, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700174 }
175
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -0500176 if (!iop || atomic_sub_and_test(len, &iop->read_bytes_pending))
177 folio_unlock(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700178}
179
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -0500180static void iomap_read_end_io(struct bio *bio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700181{
182 int error = blk_status_to_errno(bio->bi_status);
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -0500183 struct folio_iter fi;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700184
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -0500185 bio_for_each_folio_all(fi, bio)
186 iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700187 bio_put(bio);
188}
189
190struct iomap_readpage_ctx {
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400191 struct folio *cur_folio;
192 bool cur_folio_in_bio;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700193 struct bio *bio;
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700194 struct readahead_control *rac;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700195};
196
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800197/**
198 * iomap_read_inline_data - copy inline data into the page cache
199 * @iter: iteration structure
Matthew Wilcox (Oracle)874628a2021-07-23 23:24:50 -0400200 * @folio: folio to copy to
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800201 *
Matthew Wilcox (Oracle)874628a2021-07-23 23:24:50 -0400202 * Copy the inline data in @iter into @folio and zero out the rest of the folio.
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800203 * Only a single IOMAP_INLINE extent is allowed at the end of each file.
204 * Returns zero for success to complete the read, or the usual negative errno.
205 */
206static int iomap_read_inline_data(const struct iomap_iter *iter,
Matthew Wilcox (Oracle)874628a2021-07-23 23:24:50 -0400207 struct folio *folio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700208{
Matthew Wilcox (Oracle)cd1e5af2021-04-28 08:16:50 -0400209 struct iomap_page *iop;
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700210 const struct iomap *iomap = iomap_iter_srcmap(iter);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700211 size_t size = i_size_read(iter->inode) - iomap->offset;
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700212 size_t poff = offset_in_page(iomap->offset);
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400213 size_t offset = offset_in_folio(folio, iomap->offset);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700214 void *addr;
215
Matthew Wilcox (Oracle)874628a2021-07-23 23:24:50 -0400216 if (folio_test_uptodate(folio))
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800217 return 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700218
Matthew Wilcox (Oracle)ae44f9c2021-08-04 20:07:34 -0700219 if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
220 return -EIO;
Gao Xiang69f4a262021-08-03 09:38:22 -0700221 if (WARN_ON_ONCE(size > PAGE_SIZE -
222 offset_in_page(iomap->inline_data)))
223 return -EIO;
224 if (WARN_ON_ONCE(size > iomap->length))
225 return -EIO;
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400226 if (offset > 0)
Matthew Wilcox (Oracle)cd1e5af2021-04-28 08:16:50 -0400227 iop = iomap_page_create(iter->inode, folio);
228 else
229 iop = to_iomap_page(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700230
Matthew Wilcox (Oracle)874628a2021-07-23 23:24:50 -0400231 addr = kmap_local_folio(folio, offset);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700232 memcpy(addr, iomap->inline_data, size);
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700233 memset(addr + size, 0, PAGE_SIZE - poff - size);
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700234 kunmap_local(addr);
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400235 iomap_set_range_uptodate(folio, iop, offset, PAGE_SIZE - poff);
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800236 return 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700237}
238
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700239static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700240 loff_t pos)
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700241{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700242 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700243
244 return srcmap->type != IOMAP_MAPPED ||
245 (srcmap->flags & IOMAP_F_NEW) ||
246 pos >= i_size_read(iter->inode);
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700247}
248
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700249static loff_t iomap_readpage_iter(const struct iomap_iter *iter,
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700250 struct iomap_readpage_ctx *ctx, loff_t offset)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700251{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700252 const struct iomap *iomap = &iter->iomap;
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700253 loff_t pos = iter->pos + offset;
254 loff_t length = iomap_length(iter) - offset;
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400255 struct folio *folio = ctx->cur_folio;
Andreas Gruenbacher637d3372021-07-15 09:58:05 -0700256 struct iomap_page *iop;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700257 loff_t orig_pos = pos;
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400258 size_t poff, plen;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700259 sector_t sector;
260
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800261 if (iomap->type == IOMAP_INLINE)
Matthew Wilcox (Oracle)874628a2021-07-23 23:24:50 -0400262 return iomap_read_inline_data(iter, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700263
264 /* zero post-eof blocks as the page may be mapped */
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400265 iop = iomap_page_create(iter->inode, folio);
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400266 iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700267 if (plen == 0)
268 goto done;
269
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700270 if (iomap_block_needs_zeroing(iter, pos)) {
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400271 folio_zero_range(folio, poff, plen);
272 iomap_set_range_uptodate(folio, iop, poff, plen);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700273 goto done;
274 }
275
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400276 ctx->cur_folio_in_bio = true;
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -0700277 if (iop)
278 atomic_add(plen, &iop->read_bytes_pending);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700279
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700280 sector = iomap_sector(iomap, pos);
Christoph Hellwigd0364f92021-08-02 14:43:43 -0700281 if (!ctx->bio ||
282 bio_end_sector(ctx->bio) != sector ||
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400283 !bio_add_folio(ctx->bio, folio, plen, poff)) {
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400284 gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
Matthew Wilcox (Oracle)457df332020-04-02 09:08:53 -0700285 gfp_t orig_gfp = gfp;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000286 unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700287
288 if (ctx->bio)
289 submit_bio(ctx->bio);
290
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700291 if (ctx->rac) /* same as readahead_gfp_mask */
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700292 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000293 ctx->bio = bio_alloc(gfp, bio_max_segs(nr_vecs));
Matthew Wilcox (Oracle)457df332020-04-02 09:08:53 -0700294 /*
295 * If the bio_alloc fails, try it again for a single page to
296 * avoid having to deal with partial page reads. This emulates
297 * what do_mpage_readpage does.
298 */
299 if (!ctx->bio)
300 ctx->bio = bio_alloc(orig_gfp, 1);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700301 ctx->bio->bi_opf = REQ_OP_READ;
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700302 if (ctx->rac)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700303 ctx->bio->bi_opf |= REQ_RAHEAD;
304 ctx->bio->bi_iter.bi_sector = sector;
305 bio_set_dev(ctx->bio, iomap->bdev);
306 ctx->bio->bi_end_io = iomap_read_end_io;
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400307 bio_add_folio(ctx->bio, folio, plen, poff);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700308 }
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400309
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700310done:
311 /*
312 * Move the caller beyond our range so that it keeps making progress.
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700313 * For that, we have to include any leading non-uptodate ranges, but
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700314 * we can skip trailing ones as they will be handled in the next
315 * iteration.
316 */
317 return pos - orig_pos + plen;
318}
319
320int
321iomap_readpage(struct page *page, const struct iomap_ops *ops)
322{
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400323 struct folio *folio = page_folio(page);
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700324 struct iomap_iter iter = {
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400325 .inode = folio->mapping->host,
326 .pos = folio_pos(folio),
327 .len = folio_size(folio),
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700328 };
329 struct iomap_readpage_ctx ctx = {
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400330 .cur_folio = folio,
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700331 };
332 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700333
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400334 trace_iomap_readpage(iter.inode, 1);
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700335
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700336 while ((ret = iomap_iter(&iter, ops)) > 0)
337 iter.processed = iomap_readpage_iter(&iter, &ctx, 0);
338
339 if (ret < 0)
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400340 folio_set_error(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700341
342 if (ctx.bio) {
343 submit_bio(ctx.bio);
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400344 WARN_ON_ONCE(!ctx.cur_folio_in_bio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700345 } else {
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400346 WARN_ON_ONCE(ctx.cur_folio_in_bio);
347 folio_unlock(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700348 }
349
350 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700351 * Just like mpage_readahead and block_read_full_page, we always
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700352 * return 0 and just mark the page as PageError on errors. This
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700353 * should be cleaned up throughout the stack eventually.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700354 */
355 return 0;
356}
357EXPORT_SYMBOL_GPL(iomap_readpage);
358
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700359static loff_t iomap_readahead_iter(const struct iomap_iter *iter,
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700360 struct iomap_readpage_ctx *ctx)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700361{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700362 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700363 loff_t done, ret;
364
365 for (done = 0; done < length; done += ret) {
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400366 if (ctx->cur_folio &&
367 offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) {
368 if (!ctx->cur_folio_in_bio)
369 folio_unlock(ctx->cur_folio);
370 ctx->cur_folio = NULL;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700371 }
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400372 if (!ctx->cur_folio) {
373 ctx->cur_folio = readahead_folio(ctx->rac);
374 ctx->cur_folio_in_bio = false;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700375 }
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700376 ret = iomap_readpage_iter(iter, ctx, done);
Andreas Gruenbacherd8af4042021-11-17 17:59:01 -0800377 if (ret <= 0)
378 return ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700379 }
380
381 return done;
382}
383
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700384/**
385 * iomap_readahead - Attempt to read pages from a file.
386 * @rac: Describes the pages to be read.
387 * @ops: The operations vector for the filesystem.
388 *
389 * This function is for filesystems to call to implement their readahead
390 * address_space operation.
391 *
392 * Context: The @ops callbacks may submit I/O (eg to read the addresses of
393 * blocks from disc), and may wait for it. The caller may be trying to
394 * access a different page, and so sleeping excessively should be avoided.
395 * It may allocate memory, but should avoid costly allocations. This
396 * function is called with memalloc_nofs set, so allocations will not cause
397 * the filesystem to be reentered.
398 */
399void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700400{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700401 struct iomap_iter iter = {
402 .inode = rac->mapping->host,
403 .pos = readahead_pos(rac),
404 .len = readahead_length(rac),
405 };
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700406 struct iomap_readpage_ctx ctx = {
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700407 .rac = rac,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700408 };
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700409
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700410 trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700411
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700412 while (iomap_iter(&iter, ops) > 0)
413 iter.processed = iomap_readahead_iter(&iter, &ctx);
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700414
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700415 if (ctx.bio)
416 submit_bio(ctx.bio);
Matthew Wilcox (Oracle)3aa9c652021-04-28 09:39:51 -0400417 if (ctx.cur_folio) {
418 if (!ctx.cur_folio_in_bio)
419 folio_unlock(ctx.cur_folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700420 }
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700421}
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700422EXPORT_SYMBOL_GPL(iomap_readahead);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700423
424/*
425 * iomap_is_partially_uptodate checks whether blocks within a page are
426 * uptodate or not.
427 *
428 * Returns true if all blocks which correspond to a file portion
429 * we want to read within the page are uptodate.
430 */
431int
432iomap_is_partially_uptodate(struct page *page, unsigned long from,
433 unsigned long count)
434{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -0400435 struct folio *folio = page_folio(page);
436 struct iomap_page *iop = to_iomap_page(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700437 struct inode *inode = page->mapping->host;
438 unsigned len, first, last;
439 unsigned i;
440
441 /* Limit range to one page */
442 len = min_t(unsigned, PAGE_SIZE - from, count);
443
444 /* First and last blocks in range within page */
445 first = from >> inode->i_blkbits;
446 last = (from + len - 1) >> inode->i_blkbits;
447
448 if (iop) {
449 for (i = first; i <= last; i++)
450 if (!test_bit(i, iop->uptodate))
451 return 0;
452 return 1;
453 }
454
455 return 0;
456}
457EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
458
459int
460iomap_releasepage(struct page *page, gfp_t gfp_mask)
461{
Matthew Wilcox (Oracle)c46e8322021-04-27 23:22:22 -0400462 struct folio *folio = page_folio(page);
463
Matthew Wilcox (Oracle)39f16c82021-04-28 07:51:36 -0400464 trace_iomap_releasepage(folio->mapping->host, folio_pos(folio),
465 folio_size(folio));
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700466
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700467 /*
468 * mm accommodates an old ext3 case where clean pages might not have had
469 * the dirty bit cleared. Thus, it can send actual dirty pages to
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700470 * ->releasepage() via shrink_active_list(); skip those here.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700471 */
Matthew Wilcox (Oracle)39f16c82021-04-28 07:51:36 -0400472 if (folio_test_dirty(folio) || folio_test_writeback(folio))
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700473 return 0;
Matthew Wilcox (Oracle)c46e8322021-04-27 23:22:22 -0400474 iomap_page_release(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700475 return 1;
476}
477EXPORT_SYMBOL_GPL(iomap_releasepage);
478
Matthew Wilcox (Oracle)8306a5f2021-04-28 07:51:36 -0400479void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700480{
Matthew Wilcox (Oracle)8306a5f2021-04-28 07:51:36 -0400481 trace_iomap_invalidatepage(folio->mapping->host, offset, len);
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700482
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700483 /*
Matthew Wilcox (Oracle)60d82312021-01-13 10:48:49 -0500484 * If we're invalidating the entire folio, clear the dirty state
485 * from it and release it to avoid unnecessary buildup of the LRU.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700486 */
Matthew Wilcox (Oracle)8306a5f2021-04-28 07:51:36 -0400487 if (offset == 0 && len == folio_size(folio)) {
488 WARN_ON_ONCE(folio_test_writeback(folio));
489 folio_cancel_dirty(folio);
Matthew Wilcox (Oracle)c46e8322021-04-27 23:22:22 -0400490 iomap_page_release(folio);
Matthew Wilcox (Oracle)60d82312021-01-13 10:48:49 -0500491 } else if (folio_test_large(folio)) {
492 /* Must release the iop so the page can be split */
493 WARN_ON_ONCE(!folio_test_uptodate(folio) &&
494 folio_test_dirty(folio));
495 iomap_page_release(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700496 }
497}
Matthew Wilcox (Oracle)8306a5f2021-04-28 07:51:36 -0400498EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
499
500void iomap_invalidatepage(struct page *page, unsigned int offset,
501 unsigned int len)
502{
503 iomap_invalidate_folio(page_folio(page), offset, len);
504}
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700505EXPORT_SYMBOL_GPL(iomap_invalidatepage);
506
507#ifdef CONFIG_MIGRATION
508int
509iomap_migrate_page(struct address_space *mapping, struct page *newpage,
510 struct page *page, enum migrate_mode mode)
511{
Matthew Wilcox (Oracle)589110e2021-05-07 15:08:09 -0400512 struct folio *folio = page_folio(page);
513 struct folio *newfolio = page_folio(newpage);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700514 int ret;
515
Matthew Wilcox (Oracle)589110e2021-05-07 15:08:09 -0400516 ret = folio_migrate_mapping(mapping, newfolio, folio, 0);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700517 if (ret != MIGRATEPAGE_SUCCESS)
518 return ret;
519
Matthew Wilcox (Oracle)589110e2021-05-07 15:08:09 -0400520 if (folio_test_private(folio))
521 folio_attach_private(newfolio, folio_detach_private(folio));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700522
523 if (mode != MIGRATE_SYNC_NO_COPY)
Matthew Wilcox (Oracle)589110e2021-05-07 15:08:09 -0400524 folio_migrate_copy(newfolio, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700525 else
Matthew Wilcox (Oracle)589110e2021-05-07 15:08:09 -0400526 folio_migrate_flags(newfolio, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700527 return MIGRATEPAGE_SUCCESS;
528}
529EXPORT_SYMBOL_GPL(iomap_migrate_page);
530#endif /* CONFIG_MIGRATION */
531
532static void
533iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
534{
535 loff_t i_size = i_size_read(inode);
536
537 /*
538 * Only truncate newly allocated pages beyoned EOF, even if the
539 * write started inside the existing inode size.
540 */
541 if (pos + len > i_size)
542 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
543}
544
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400545static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
546 size_t poff, size_t plen, const struct iomap *iomap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700547{
548 struct bio_vec bvec;
549 struct bio bio;
550
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700551 bio_init(&bio, &bvec, 1);
552 bio.bi_opf = REQ_OP_READ;
553 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
554 bio_set_dev(&bio, iomap->bdev);
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400555 bio_add_folio(&bio, folio, plen, poff);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700556 return submit_bio_wait(&bio);
557}
558
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700559static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400560 size_t len, struct folio *folio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700561{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700562 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400563 struct iomap_page *iop = iomap_page_create(iter->inode, folio);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700564 loff_t block_size = i_blocksize(iter->inode);
Nikolay Borisov6cc19c52020-09-10 08:38:06 -0700565 loff_t block_start = round_down(pos, block_size);
566 loff_t block_end = round_up(pos + len, block_size);
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400567 size_t from = offset_in_folio(folio, pos), to = from + len;
568 size_t poff, plen;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700569
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400570 if (folio_test_uptodate(folio))
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700571 return 0;
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400572 folio_clear_error(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700573
574 do {
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400575 iomap_adjust_read_range(iter->inode, folio, &block_start,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700576 block_end - block_start, &poff, &plen);
577 if (plen == 0)
578 break;
579
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700580 if (!(iter->flags & IOMAP_UNSHARE) &&
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700581 (from <= poff || from >= poff + plen) &&
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700582 (to <= poff || to >= poff + plen))
583 continue;
584
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700585 if (iomap_block_needs_zeroing(iter, block_start)) {
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700586 if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700587 return -EIO;
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400588 folio_zero_segments(folio, poff, from, to, poff + plen);
Matthew Wilcox (Oracle)14284fe2020-09-10 08:26:18 -0700589 } else {
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400590 int status = iomap_read_folio_sync(block_start, folio,
Matthew Wilcox (Oracle)14284fe2020-09-10 08:26:18 -0700591 poff, plen, srcmap);
592 if (status)
593 return status;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700594 }
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400595 iomap_set_range_uptodate(folio, iop, poff, plen);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700596 } while ((block_start += plen) < block_end);
597
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700598 return 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700599}
600
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700601static int iomap_write_begin_inline(const struct iomap_iter *iter,
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400602 struct folio *folio)
Gao Xiang69f4a262021-08-03 09:38:22 -0700603{
604 /* needs more work for the tailpacking case; disable for now */
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700605 if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
Gao Xiang69f4a262021-08-03 09:38:22 -0700606 return -EIO;
Matthew Wilcox (Oracle)874628a2021-07-23 23:24:50 -0400607 return iomap_read_inline_data(iter, folio);
Gao Xiang69f4a262021-08-03 09:38:22 -0700608}
609
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700610static int iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400611 size_t len, struct folio **foliop)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700612{
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700613 const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700614 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400615 struct folio *folio;
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400616 unsigned fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE | FGP_NOFS;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700617 int status = 0;
618
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700619 BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
620 if (srcmap != &iter->iomap)
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700621 BUG_ON(pos + len > srcmap->offset + srcmap->length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700622
623 if (fatal_signal_pending(current))
624 return -EINTR;
625
Matthew Wilcox (Oracle)d454ab82021-12-09 15:47:44 -0500626 if (!mapping_large_folio_support(iter->inode->i_mapping))
627 len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
628
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700629 if (page_ops && page_ops->page_prepare) {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700630 status = page_ops->page_prepare(iter->inode, pos, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700631 if (status)
632 return status;
633 }
634
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400635 folio = __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
636 fgp, mapping_gfp_mask(iter->inode->i_mapping));
637 if (!folio) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700638 status = -ENOMEM;
639 goto out_no_page;
640 }
Matthew Wilcox (Oracle)d454ab82021-12-09 15:47:44 -0500641 if (pos + len > folio_pos(folio) + folio_size(folio))
642 len = folio_pos(folio) + folio_size(folio) - pos;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700643
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700644 if (srcmap->type == IOMAP_INLINE)
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400645 status = iomap_write_begin_inline(iter, folio);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700646 else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400647 status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700648 else
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400649 status = __iomap_write_begin(iter, pos, len, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700650
651 if (unlikely(status))
652 goto out_unlock;
653
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400654 *foliop = folio;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700655 return 0;
656
657out_unlock:
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400658 folio_unlock(folio);
659 folio_put(folio);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700660 iomap_write_failed(iter->inode, pos, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700661
662out_no_page:
663 if (page_ops && page_ops->page_done)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700664 page_ops->page_done(iter->inode, pos, 0, NULL);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700665 return status;
666}
667
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700668static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400669 size_t copied, struct folio *folio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700670{
Matthew Wilcox (Oracle)cd1e5af2021-04-28 08:16:50 -0400671 struct iomap_page *iop = to_iomap_page(folio);
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400672 flush_dcache_folio(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700673
674 /*
675 * The blocks that were entirely written will now be uptodate, so we
676 * don't have to worry about a readpage reading them and overwriting a
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700677 * partial write. However, if we've encountered a short write and only
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700678 * partially written into a block, it will not be marked uptodate, so a
679 * readpage might come in and destroy our partial write.
680 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700681 * Do the simplest thing and just treat any short write to a
682 * non-uptodate page as a zero-length write, and force the caller to
683 * redo the whole thing.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700684 */
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400685 if (unlikely(copied < len && !folio_test_uptodate(folio)))
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700686 return 0;
Matthew Wilcox (Oracle)431c0562021-04-28 08:20:48 -0400687 iomap_set_range_uptodate(folio, iop, offset_in_folio(folio, pos), len);
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400688 filemap_dirty_folio(inode->i_mapping, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700689 return copied;
690}
691
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700692static size_t iomap_write_end_inline(const struct iomap_iter *iter,
Matthew Wilcox (Oracle)9c4ce08d2021-05-02 11:44:44 -0400693 struct folio *folio, loff_t pos, size_t copied)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700694{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700695 const struct iomap *iomap = &iter->iomap;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700696 void *addr;
697
Matthew Wilcox (Oracle)9c4ce08d2021-05-02 11:44:44 -0400698 WARN_ON_ONCE(!folio_test_uptodate(folio));
Gao Xiang69f4a262021-08-03 09:38:22 -0700699 BUG_ON(!iomap_inline_data_valid(iomap));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700700
Matthew Wilcox (Oracle)9c4ce08d2021-05-02 11:44:44 -0400701 flush_dcache_folio(folio);
702 addr = kmap_local_folio(folio, pos);
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700703 memcpy(iomap_inline_data(iomap, pos), addr, copied);
704 kunmap_local(addr);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700705
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700706 mark_inode_dirty(iter->inode);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700707 return copied;
708}
709
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700710/* Returns the number of bytes copied. May be 0. Cannot be an errno. */
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700711static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len,
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400712 size_t copied, struct folio *folio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700713{
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700714 const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700715 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700716 loff_t old_size = iter->inode->i_size;
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700717 size_t ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700718
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700719 if (srcmap->type == IOMAP_INLINE) {
Matthew Wilcox (Oracle)9c4ce08d2021-05-02 11:44:44 -0400720 ret = iomap_write_end_inline(iter, folio, pos, copied);
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700721 } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700722 ret = block_write_end(NULL, iter->inode->i_mapping, pos, len,
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400723 copied, &folio->page, NULL);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700724 } else {
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400725 ret = __iomap_write_end(iter->inode, pos, len, copied, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700726 }
727
728 /*
729 * Update the in-memory inode size after copying the data into the page
730 * cache. It's up to the file system to write the updated size to disk,
731 * preferably after I/O completion so that no stale data is exposed.
732 */
733 if (pos + ret > old_size) {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700734 i_size_write(iter->inode, pos + ret);
735 iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700736 }
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400737 folio_unlock(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700738
739 if (old_size < pos)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700740 pagecache_isize_extended(iter->inode, old_size, pos);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700741 if (page_ops && page_ops->page_done)
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400742 page_ops->page_done(iter->inode, pos, ret, &folio->page);
743 folio_put(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700744
745 if (ret < len)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700746 iomap_write_failed(iter->inode, pos, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700747 return ret;
748}
749
Christoph Hellwigce83a022021-08-10 18:33:08 -0700750static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700751{
Christoph Hellwigce83a022021-08-10 18:33:08 -0700752 loff_t length = iomap_length(iter);
753 loff_t pos = iter->pos;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700754 ssize_t written = 0;
Christoph Hellwigce83a022021-08-10 18:33:08 -0700755 long status = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700756
757 do {
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400758 struct folio *folio;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700759 struct page *page;
760 unsigned long offset; /* Offset into pagecache page */
761 unsigned long bytes; /* Bytes to write to page */
762 size_t copied; /* Bytes copied from user */
763
764 offset = offset_in_page(pos);
765 bytes = min_t(unsigned long, PAGE_SIZE - offset,
766 iov_iter_count(i));
767again:
768 if (bytes > length)
769 bytes = length;
770
771 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700772 * Bring in the user page that we'll copy from _first_.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700773 * Otherwise there's a nasty deadlock on copying from the
774 * same page as we're writing to, without it being marked
775 * up-to-date.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700776 */
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200777 if (unlikely(fault_in_iov_iter_readable(i, bytes))) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700778 status = -EFAULT;
779 break;
780 }
781
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400782 status = iomap_write_begin(iter, pos, bytes, &folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700783 if (unlikely(status))
784 break;
785
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400786 page = folio_file_page(folio, pos >> PAGE_SHIFT);
Christoph Hellwigce83a022021-08-10 18:33:08 -0700787 if (mapping_writably_mapped(iter->inode->i_mapping))
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700788 flush_dcache_page(page);
789
Al Virof0b65f32021-04-30 10:26:41 -0400790 copied = copy_page_from_iter_atomic(page, offset, bytes, i);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700791
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400792 status = iomap_write_end(iter, pos, bytes, copied, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700793
Al Virof0b65f32021-04-30 10:26:41 -0400794 if (unlikely(copied != status))
795 iov_iter_revert(i, copied - status);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700796
Al Virof0b65f32021-04-30 10:26:41 -0400797 cond_resched();
Al Virobc1bb412021-05-31 00:32:44 -0400798 if (unlikely(status == 0)) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700799 /*
Al Virobc1bb412021-05-31 00:32:44 -0400800 * A short copy made iomap_write_end() reject the
801 * thing entirely. Might be memory poisoning
802 * halfway through, might be a race with munmap,
803 * might be severe memory pressure.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700804 */
Al Virobc1bb412021-05-31 00:32:44 -0400805 if (copied)
806 bytes = copied;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700807 goto again;
808 }
Al Virof0b65f32021-04-30 10:26:41 -0400809 pos += status;
810 written += status;
811 length -= status;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700812
Christoph Hellwigce83a022021-08-10 18:33:08 -0700813 balance_dirty_pages_ratelimited(iter->inode->i_mapping);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700814 } while (iov_iter_count(i) && length);
815
816 return written ? written : status;
817}
818
819ssize_t
Christoph Hellwigce83a022021-08-10 18:33:08 -0700820iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700821 const struct iomap_ops *ops)
822{
Christoph Hellwigce83a022021-08-10 18:33:08 -0700823 struct iomap_iter iter = {
824 .inode = iocb->ki_filp->f_mapping->host,
825 .pos = iocb->ki_pos,
826 .len = iov_iter_count(i),
827 .flags = IOMAP_WRITE,
828 };
829 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700830
Christoph Hellwigce83a022021-08-10 18:33:08 -0700831 while ((ret = iomap_iter(&iter, ops)) > 0)
832 iter.processed = iomap_write_iter(&iter, i);
833 if (iter.pos == iocb->ki_pos)
834 return ret;
835 return iter.pos - iocb->ki_pos;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700836}
837EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
838
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700839static loff_t iomap_unshare_iter(struct iomap_iter *iter)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700840{
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700841 struct iomap *iomap = &iter->iomap;
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700842 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700843 loff_t pos = iter->pos;
844 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700845 long status = 0;
Matthew Wilcox (Oracle)d4ff3b22020-06-08 20:58:29 -0700846 loff_t written = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700847
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700848 /* don't bother with blocks that are not shared to start with */
849 if (!(iomap->flags & IOMAP_F_SHARED))
850 return length;
851 /* don't bother with holes or unwritten extents */
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700852 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700853 return length;
854
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700855 do {
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700856 unsigned long offset = offset_in_page(pos);
857 unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length);
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400858 struct folio *folio;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700859
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400860 status = iomap_write_begin(iter, pos, bytes, &folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700861 if (unlikely(status))
862 return status;
863
Matthew Wilcox (Oracle)bc6123a2021-05-02 11:33:08 -0400864 status = iomap_write_end(iter, pos, bytes, bytes, folio);
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700865 if (WARN_ON_ONCE(status == 0))
866 return -EIO;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700867
868 cond_resched();
869
870 pos += status;
871 written += status;
872 length -= status;
873
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700874 balance_dirty_pages_ratelimited(iter->inode->i_mapping);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700875 } while (length);
876
877 return written;
878}
879
880int
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700881iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700882 const struct iomap_ops *ops)
883{
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700884 struct iomap_iter iter = {
885 .inode = inode,
886 .pos = pos,
887 .len = len,
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700888 .flags = IOMAP_WRITE | IOMAP_UNSHARE,
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700889 };
890 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700891
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700892 while ((ret = iomap_iter(&iter, ops)) > 0)
893 iter.processed = iomap_unshare_iter(&iter);
894 return ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700895}
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700896EXPORT_SYMBOL_GPL(iomap_file_unshare);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700897
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700898static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700899{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700900 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700901 loff_t pos = iter->pos;
902 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700903 loff_t written = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700904
905 /* already zeroed? we're done. */
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700906 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700907 return length;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700908
909 do {
Matthew Wilcox (Oracle)4d7bd0e2021-12-20 19:03:46 -0500910 struct folio *folio;
911 int status;
912 size_t offset;
913 size_t bytes = min_t(u64, SIZE_MAX, length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700914
Matthew Wilcox (Oracle)4d7bd0e2021-12-20 19:03:46 -0500915 status = iomap_write_begin(iter, pos, bytes, &folio);
916 if (status)
917 return status;
918
919 offset = offset_in_folio(folio, pos);
920 if (bytes > folio_size(folio) - offset)
921 bytes = folio_size(folio) - offset;
922
923 folio_zero_range(folio, offset, bytes);
924 folio_mark_accessed(folio);
925
926 bytes = iomap_write_end(iter, pos, bytes, bytes, folio);
Matthew Wilcox (Oracle)4d7bd0e2021-12-20 19:03:46 -0500927 if (WARN_ON_ONCE(bytes == 0))
928 return -EIO;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700929
930 pos += bytes;
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700931 length -= bytes;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700932 written += bytes;
933 if (did_zero)
934 *did_zero = true;
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700935 } while (length > 0);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700936
937 return written;
938}
939
940int
941iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
942 const struct iomap_ops *ops)
943{
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700944 struct iomap_iter iter = {
945 .inode = inode,
946 .pos = pos,
947 .len = len,
948 .flags = IOMAP_ZERO,
949 };
950 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700951
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700952 while ((ret = iomap_iter(&iter, ops)) > 0)
953 iter.processed = iomap_zero_iter(&iter, did_zero);
954 return ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700955}
956EXPORT_SYMBOL_GPL(iomap_zero_range);
957
958int
959iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
960 const struct iomap_ops *ops)
961{
962 unsigned int blocksize = i_blocksize(inode);
963 unsigned int off = pos & (blocksize - 1);
964
965 /* Block boundary? Nothing to do */
966 if (!off)
967 return 0;
968 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
969}
970EXPORT_SYMBOL_GPL(iomap_truncate_page);
971
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -0400972static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter,
973 struct folio *folio)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700974{
Christoph Hellwig253564b2021-08-10 18:33:09 -0700975 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700976 int ret;
977
Christoph Hellwig253564b2021-08-10 18:33:09 -0700978 if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400979 ret = __block_write_begin_int(folio, iter->pos, length, NULL,
Christoph Hellwig253564b2021-08-10 18:33:09 -0700980 &iter->iomap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700981 if (ret)
982 return ret;
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -0400983 block_commit_write(&folio->page, 0, length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700984 } else {
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -0400985 WARN_ON_ONCE(!folio_test_uptodate(folio));
986 folio_mark_dirty(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700987 }
988
989 return length;
990}
991
992vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
993{
Christoph Hellwig253564b2021-08-10 18:33:09 -0700994 struct iomap_iter iter = {
995 .inode = file_inode(vmf->vma->vm_file),
996 .flags = IOMAP_WRITE | IOMAP_FAULT,
997 };
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -0400998 struct folio *folio = page_folio(vmf->page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700999 ssize_t ret;
1000
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -04001001 folio_lock(folio);
1002 ret = folio_mkwrite_check_truncate(folio, iter.inode);
Andreas Gruenbacher243145b2020-01-06 08:58:23 -08001003 if (ret < 0)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07001004 goto out_unlock;
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -04001005 iter.pos = folio_pos(folio);
Christoph Hellwig253564b2021-08-10 18:33:09 -07001006 iter.len = ret;
1007 while ((ret = iomap_iter(&iter, ops)) > 0)
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -04001008 iter.processed = iomap_folio_mkwrite_iter(&iter, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07001009
Christoph Hellwig253564b2021-08-10 18:33:09 -07001010 if (ret < 0)
1011 goto out_unlock;
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -04001012 folio_wait_stable(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07001013 return VM_FAULT_LOCKED;
1014out_unlock:
Matthew Wilcox (Oracle)ea0f8432021-04-28 22:32:02 -04001015 folio_unlock(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07001016 return block_page_mkwrite_return(ret);
1017}
1018EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001019
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -05001020static void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
1021 size_t len, int error)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001022{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -04001023 struct iomap_page *iop = to_iomap_page(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001024
1025 if (error) {
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -05001026 folio_set_error(folio);
Darrick J. Wongb69eea82021-08-10 18:32:55 -07001027 mapping_set_error(inode->i_mapping, error);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001028 }
1029
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -05001030 WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !iop);
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001031 WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001032
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001033 if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending))
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -05001034 folio_end_writeback(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001035}
1036
1037/*
1038 * We're now finished for good with this ioend structure. Update the page
1039 * state, release holds on bios, and finally free up memory. Do not use the
1040 * ioend after this.
1041 */
1042static void
1043iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1044{
1045 struct inode *inode = ioend->io_inode;
1046 struct bio *bio = &ioend->io_inline_bio;
1047 struct bio *last = ioend->io_bio, *next;
1048 u64 start = bio->bi_iter.bi_sector;
Zorro Langc2757792019-12-04 22:59:02 -08001049 loff_t offset = ioend->io_offset;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001050 bool quiet = bio_flagged(bio, BIO_QUIET);
1051
1052 for (bio = &ioend->io_inline_bio; bio; bio = next) {
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -05001053 struct folio_iter fi;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001054
1055 /*
1056 * For the last bio, bi_private points to the ioend, so we
1057 * need to explicitly end the iteration here.
1058 */
1059 if (bio == last)
1060 next = NULL;
1061 else
1062 next = bio->bi_private;
1063
Matthew Wilcox (Oracle)8ffd74e2021-01-01 16:53:26 -05001064 /* walk all folios in bio, ending page IO on them */
1065 bio_for_each_folio_all(fi, bio)
1066 iomap_finish_folio_write(inode, fi.folio, fi.length,
1067 error);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001068 bio_put(bio);
1069 }
Zorro Langc2757792019-12-04 22:59:02 -08001070 /* The ioend has been freed by bio_put() */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001071
1072 if (unlikely(error && !quiet)) {
1073 printk_ratelimited(KERN_ERR
Darrick J. Wong9cd0ed62019-10-17 14:02:07 -07001074"%s: writeback error on inode %lu, offset %lld, sector %llu",
Zorro Langc2757792019-12-04 22:59:02 -08001075 inode->i_sb->s_id, inode->i_ino, offset, start);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001076 }
1077}
1078
1079void
1080iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1081{
1082 struct list_head tmp;
1083
1084 list_replace_init(&ioend->io_list, &tmp);
1085 iomap_finish_ioend(ioend, error);
1086
1087 while (!list_empty(&tmp)) {
1088 ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1089 list_del_init(&ioend->io_list);
1090 iomap_finish_ioend(ioend, error);
1091 }
1092}
1093EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1094
1095/*
1096 * We can merge two adjacent ioends if they have the same set of work to do.
1097 */
1098static bool
1099iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1100{
1101 if (ioend->io_bio->bi_status != next->io_bio->bi_status)
1102 return false;
1103 if ((ioend->io_flags & IOMAP_F_SHARED) ^
1104 (next->io_flags & IOMAP_F_SHARED))
1105 return false;
1106 if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1107 (next->io_type == IOMAP_UNWRITTEN))
1108 return false;
1109 if (ioend->io_offset + ioend->io_size != next->io_offset)
1110 return false;
1111 return true;
1112}
1113
1114void
Brian Foster6e552492021-05-04 08:54:29 -07001115iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001116{
1117 struct iomap_ioend *next;
1118
1119 INIT_LIST_HEAD(&ioend->io_list);
1120
1121 while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1122 io_list))) {
1123 if (!iomap_ioend_can_merge(ioend, next))
1124 break;
1125 list_move_tail(&next->io_list, &ioend->io_list);
1126 ioend->io_size += next->io_size;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001127 }
1128}
1129EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1130
1131static int
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07001132iomap_ioend_compare(void *priv, const struct list_head *a,
1133 const struct list_head *b)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001134{
Christoph Hellwigb3d423e2019-10-17 13:12:20 -07001135 struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1136 struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001137
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001138 if (ia->io_offset < ib->io_offset)
1139 return -1;
Christoph Hellwigb3d423e2019-10-17 13:12:20 -07001140 if (ia->io_offset > ib->io_offset)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001141 return 1;
1142 return 0;
1143}
1144
1145void
1146iomap_sort_ioends(struct list_head *ioend_list)
1147{
1148 list_sort(NULL, ioend_list, iomap_ioend_compare);
1149}
1150EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1151
1152static void iomap_writepage_end_bio(struct bio *bio)
1153{
1154 struct iomap_ioend *ioend = bio->bi_private;
1155
1156 iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
1157}
1158
1159/*
1160 * Submit the final bio for an ioend.
1161 *
1162 * If @error is non-zero, it means that we have a situation where some part of
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001163 * the submission process has failed after we've marked pages for writeback
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001164 * and unlocked them. In this situation, we need to fail the bio instead of
1165 * submitting it. This typically only happens on a filesystem shutdown.
1166 */
1167static int
1168iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
1169 int error)
1170{
1171 ioend->io_bio->bi_private = ioend;
1172 ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
1173
1174 if (wpc->ops->prepare_ioend)
1175 error = wpc->ops->prepare_ioend(ioend, error);
1176 if (error) {
1177 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001178 * If we're failing the IO now, just mark the ioend with an
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001179 * error and finish it. This will run IO completion immediately
1180 * as there is only one reference to the ioend at this point in
1181 * time.
1182 */
1183 ioend->io_bio->bi_status = errno_to_blk_status(error);
1184 bio_endio(ioend->io_bio);
1185 return error;
1186 }
1187
1188 submit_bio(ioend->io_bio);
1189 return 0;
1190}
1191
1192static struct iomap_ioend *
1193iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
1194 loff_t offset, sector_t sector, struct writeback_control *wbc)
1195{
1196 struct iomap_ioend *ioend;
1197 struct bio *bio;
1198
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001199 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &iomap_ioend_bioset);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001200 bio_set_dev(bio, wpc->iomap.bdev);
1201 bio->bi_iter.bi_sector = sector;
1202 bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
1203 bio->bi_write_hint = inode->i_write_hint;
1204 wbc_init_bio(wbc, bio);
1205
1206 ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
1207 INIT_LIST_HEAD(&ioend->io_list);
1208 ioend->io_type = wpc->iomap.type;
1209 ioend->io_flags = wpc->iomap.flags;
1210 ioend->io_inode = inode;
1211 ioend->io_size = 0;
1212 ioend->io_offset = offset;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001213 ioend->io_bio = bio;
1214 return ioend;
1215}
1216
1217/*
1218 * Allocate a new bio, and chain the old bio to the new one.
1219 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001220 * Note that we have to perform the chaining in this unintuitive order
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001221 * so that the bi_private linkage is set up in the right direction for the
1222 * traversal in iomap_finish_ioend().
1223 */
1224static struct bio *
1225iomap_chain_bio(struct bio *prev)
1226{
1227 struct bio *new;
1228
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001229 new = bio_alloc(GFP_NOFS, BIO_MAX_VECS);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001230 bio_copy_dev(new, prev);/* also copies over blkcg information */
1231 new->bi_iter.bi_sector = bio_end_sector(prev);
1232 new->bi_opf = prev->bi_opf;
1233 new->bi_write_hint = prev->bi_write_hint;
1234
1235 bio_chain(prev, new);
1236 bio_get(prev); /* for iomap_finish_ioend */
1237 submit_bio(prev);
1238 return new;
1239}
1240
1241static bool
1242iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
1243 sector_t sector)
1244{
1245 if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1246 (wpc->ioend->io_flags & IOMAP_F_SHARED))
1247 return false;
1248 if (wpc->iomap.type != wpc->ioend->io_type)
1249 return false;
1250 if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
1251 return false;
1252 if (sector != bio_end_sector(wpc->ioend->io_bio))
1253 return false;
1254 return true;
1255}
1256
1257/*
1258 * Test to see if we have an existing ioend structure that we could append to
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001259 * first; otherwise finish off the current ioend and start another.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001260 */
1261static void
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001262iomap_add_to_ioend(struct inode *inode, loff_t pos, struct folio *folio,
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001263 struct iomap_page *iop, struct iomap_writepage_ctx *wpc,
1264 struct writeback_control *wbc, struct list_head *iolist)
1265{
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001266 sector_t sector = iomap_sector(&wpc->iomap, pos);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001267 unsigned len = i_blocksize(inode);
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001268 size_t poff = offset_in_folio(folio, pos);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001269
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001270 if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, pos, sector)) {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001271 if (wpc->ioend)
1272 list_add(&wpc->ioend->io_list, iolist);
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001273 wpc->ioend = iomap_alloc_ioend(inode, wpc, pos, sector, wbc);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001274 }
1275
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001276 if (!bio_add_folio(wpc->ioend->io_bio, folio, len, poff)) {
Christoph Hellwigc1b79f12021-08-02 14:43:43 -07001277 wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio);
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001278 bio_add_folio(wpc->ioend->io_bio, folio, len, poff);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001279 }
1280
Christoph Hellwigc1b79f12021-08-02 14:43:43 -07001281 if (iop)
1282 atomic_add(len, &iop->write_bytes_pending);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001283 wpc->ioend->io_size += len;
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001284 wbc_account_cgroup_owner(wbc, &folio->page, len);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001285}
1286
1287/*
1288 * We implement an immediate ioend submission policy here to avoid needing to
1289 * chain multiple ioends and hence nest mempool allocations which can violate
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001290 * the forward progress guarantees we need to provide. The current ioend we're
1291 * adding blocks to is cached in the writepage context, and if the new block
1292 * doesn't append to the cached ioend, it will create a new ioend and cache that
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001293 * instead.
1294 *
1295 * If a new ioend is created and cached, the old ioend is returned and queued
1296 * locally for submission once the entire page is processed or an error has been
1297 * detected. While ioends are submitted immediately after they are completed,
1298 * batching optimisations are provided by higher level block plugging.
1299 *
1300 * At the end of a writeback pass, there will be a cached ioend remaining on the
1301 * writepage context that the caller will need to submit.
1302 */
1303static int
1304iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1305 struct writeback_control *wbc, struct inode *inode,
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001306 struct folio *folio, u64 end_pos)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001307{
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -04001308 struct iomap_page *iop = iomap_page_create(inode, folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001309 struct iomap_ioend *ioend, *next;
1310 unsigned len = i_blocksize(inode);
Matthew Wilcox (Oracle)92655032021-11-02 10:51:55 -04001311 unsigned nblocks = i_blocks_per_folio(inode, folio);
1312 u64 pos = folio_pos(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001313 int error = 0, count = 0, i;
1314 LIST_HEAD(submit_list);
1315
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001316 WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001317
1318 /*
Matthew Wilcox (Oracle)92655032021-11-02 10:51:55 -04001319 * Walk through the folio to find areas to write back. If we
1320 * run off the end of the current map or find the current map
1321 * invalid, grab a new one.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001322 */
Matthew Wilcox (Oracle)92655032021-11-02 10:51:55 -04001323 for (i = 0; i < nblocks && pos < end_pos; i++, pos += len) {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001324 if (iop && !test_bit(i, iop->uptodate))
1325 continue;
1326
Matthew Wilcox (Oracle)92655032021-11-02 10:51:55 -04001327 error = wpc->ops->map_blocks(wpc, inode, pos);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001328 if (error)
1329 break;
Christoph Hellwig3e19e6f2019-10-17 13:12:17 -07001330 if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
1331 continue;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001332 if (wpc->iomap.type == IOMAP_HOLE)
1333 continue;
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001334 iomap_add_to_ioend(inode, pos, folio, iop, wpc, wbc,
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001335 &submit_list);
1336 count++;
1337 }
1338
1339 WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001340 WARN_ON_ONCE(!folio_test_locked(folio));
1341 WARN_ON_ONCE(folio_test_writeback(folio));
1342 WARN_ON_ONCE(folio_test_dirty(folio));
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001343
1344 /*
1345 * We cannot cancel the ioend directly here on error. We may have
1346 * already set other pages under writeback and hence we have to run I/O
1347 * completion to mark the error state of the pages under writeback
1348 * appropriately.
1349 */
1350 if (unlikely(error)) {
Brian Foster763e4cd2020-10-29 14:30:48 -07001351 /*
1352 * Let the filesystem know what portion of the current page
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001353 * failed to map. If the page hasn't been added to ioend, it
Brian Foster763e4cd2020-10-29 14:30:48 -07001354 * won't be affected by I/O completion and we must unlock it
1355 * now.
1356 */
Matthew Wilcox (Oracle)6e478522021-07-30 09:56:05 -04001357 if (wpc->ops->discard_folio)
Matthew Wilcox (Oracle)92655032021-11-02 10:51:55 -04001358 wpc->ops->discard_folio(folio, pos);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001359 if (!count) {
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001360 folio_clear_uptodate(folio);
1361 folio_unlock(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001362 goto done;
1363 }
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001364 }
1365
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001366 folio_start_writeback(folio);
1367 folio_unlock(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001368
1369 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001370 * Preserve the original error if there was one; catch
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001371 * submission errors here and propagate into subsequent ioend
1372 * submissions.
1373 */
1374 list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
1375 int error2;
1376
1377 list_del_init(&ioend->io_list);
1378 error2 = iomap_submit_ioend(wpc, ioend, error);
1379 if (error2 && !error)
1380 error = error2;
1381 }
1382
1383 /*
1384 * We can end up here with no error and nothing to write only if we race
1385 * with a partial page truncate on a sub-page block sized filesystem.
1386 */
1387 if (!count)
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001388 folio_end_writeback(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001389done:
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001390 mapping_set_error(folio->mapping, error);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001391 return error;
1392}
1393
1394/*
1395 * Write out a dirty page.
1396 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001397 * For delalloc space on the page, we need to allocate space and flush it.
1398 * For unwritten space on the page, we need to start the conversion to
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001399 * regular allocated space.
1400 */
1401static int
1402iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)
1403{
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001404 struct folio *folio = page_folio(page);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001405 struct iomap_writepage_ctx *wpc = data;
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001406 struct inode *inode = folio->mapping->host;
Matthew Wilcox (Oracle)81d47822021-11-02 11:41:16 -04001407 u64 end_pos, isize;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001408
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001409 trace_iomap_writepage(inode, folio_pos(folio), folio_size(folio));
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001410
1411 /*
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001412 * Refuse to write the folio out if we're called from reclaim context.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001413 *
1414 * This avoids stack overflows when called from deeply used stacks in
1415 * random callers for direct reclaim or memcg reclaim. We explicitly
1416 * allow reclaim from kswapd as the stack usage there is relatively low.
1417 *
1418 * This should never happen except in the case of a VM regression so
1419 * warn about it.
1420 */
1421 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1422 PF_MEMALLOC))
1423 goto redirty;
1424
1425 /*
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001426 * Is this folio beyond the end of the file?
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001427 *
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001428 * The folio index is less than the end_index, adjust the end_pos
1429 * to the highest offset that this folio should represent.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001430 * -----------------------------------------------------
1431 * | file mapping | <EOF> |
1432 * -----------------------------------------------------
1433 * | Page ... | Page N-2 | Page N-1 | Page N | |
1434 * ^--------------------------------^----------|--------
1435 * | desired writeback range | see else |
1436 * ---------------------------------^------------------|
1437 */
Matthew Wilcox (Oracle)81d47822021-11-02 11:41:16 -04001438 isize = i_size_read(inode);
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001439 end_pos = folio_pos(folio) + folio_size(folio);
Matthew Wilcox (Oracle)81d47822021-11-02 11:41:16 -04001440 if (end_pos > isize) {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001441 /*
1442 * Check whether the page to write out is beyond or straddles
1443 * i_size or not.
1444 * -------------------------------------------------------
1445 * | file mapping | <EOF> |
1446 * -------------------------------------------------------
1447 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
1448 * ^--------------------------------^-----------|---------
1449 * | | Straddles |
1450 * ---------------------------------^-----------|--------|
1451 */
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001452 size_t poff = offset_in_folio(folio, isize);
Matthew Wilcox (Oracle)81d47822021-11-02 11:41:16 -04001453 pgoff_t end_index = isize >> PAGE_SHIFT;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001454
1455 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001456 * Skip the page if it's fully outside i_size, e.g. due to a
1457 * truncate operation that's in progress. We must redirty the
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001458 * page so that reclaim stops reclaiming it. Otherwise
1459 * iomap_vm_releasepage() is called on it and gets confused.
1460 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001461 * Note that the end_index is unsigned long. If the given
1462 * offset is greater than 16TB on a 32-bit system then if we
1463 * checked if the page is fully outside i_size with
1464 * "if (page->index >= end_index + 1)", "end_index + 1" would
1465 * overflow and evaluate to 0. Hence this page would be
1466 * redirtied and written out repeatedly, which would result in
1467 * an infinite loop; the user program performing this operation
1468 * would hang. Instead, we can detect this situation by
1469 * checking if the page is totally beyond i_size or if its
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001470 * offset is just equal to the EOF.
1471 */
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001472 if (folio->index > end_index ||
1473 (folio->index == end_index && poff == 0))
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001474 goto redirty;
1475
1476 /*
1477 * The page straddles i_size. It must be zeroed out on each
1478 * and every writepage invocation because it may be mmapped.
1479 * "A file is mapped in multiples of the page size. For a file
1480 * that is not a multiple of the page size, the remaining
1481 * memory is zeroed when mapped, and writes to that region are
1482 * not written out to the file."
1483 */
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001484 folio_zero_segment(folio, poff, folio_size(folio));
Matthew Wilcox (Oracle)81d47822021-11-02 11:41:16 -04001485 end_pos = isize;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001486 }
1487
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001488 return iomap_writepage_map(wpc, wbc, inode, folio, end_pos);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001489
1490redirty:
Matthew Wilcox (Oracle)e735c002021-11-02 12:45:12 -04001491 folio_redirty_for_writepage(wbc, folio);
1492 folio_unlock(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001493 return 0;
1494}
1495
1496int
1497iomap_writepage(struct page *page, struct writeback_control *wbc,
1498 struct iomap_writepage_ctx *wpc,
1499 const struct iomap_writeback_ops *ops)
1500{
1501 int ret;
1502
1503 wpc->ops = ops;
1504 ret = iomap_do_writepage(page, wbc, wpc);
1505 if (!wpc->ioend)
1506 return ret;
1507 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1508}
1509EXPORT_SYMBOL_GPL(iomap_writepage);
1510
1511int
1512iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1513 struct iomap_writepage_ctx *wpc,
1514 const struct iomap_writeback_ops *ops)
1515{
1516 int ret;
1517
1518 wpc->ops = ops;
1519 ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
1520 if (!wpc->ioend)
1521 return ret;
1522 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1523}
1524EXPORT_SYMBOL_GPL(iomap_writepages);
1525
1526static int __init iomap_init(void)
1527{
1528 return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
1529 offsetof(struct iomap_ioend, io_inline_bio),
1530 BIOSET_NEED_BVECS);
1531}
1532fs_initcall(iomap_init);