blob: d7823610da5c7580ca69a31dc773eefcf8bc0a53 [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
62static void
63iomap_page_release(struct page *page)
64{
Guoqing Jiang58aeb732020-06-01 21:47:54 -070065 struct iomap_page *iop = detach_page_private(page);
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070066 unsigned int nr_blocks = i_blocks_per_page(page->mapping->host, page);
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) !=
73 PageUptodate(page));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070074 kfree(iop);
75}
76
77/*
78 * Calculate the range inside the page that we actually need to read.
79 */
80static void
81iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
82 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
83{
84 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);
88 unsigned poff = offset_in_page(*pos);
89 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
90 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) {
127 unsigned end = offset_in_page(isize - 1) >> block_bits;
128
129 if (first <= end && last > end)
130 plen -= (last - end) * block_size;
131 }
132
133 *offp = poff;
134 *lenp = plen;
135}
136
137static void
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800138iomap_iop_set_range_uptodate(struct page *page, unsigned off, unsigned len)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700139{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -0400140 struct folio *folio = page_folio(page);
141 struct iomap_page *iop = to_iomap_page(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700142 struct inode *inode = page->mapping->host;
143 unsigned first = off >> inode->i_blkbits;
144 unsigned last = (off + len - 1) >> inode->i_blkbits;
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800145 unsigned long flags;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700146
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800147 spin_lock_irqsave(&iop->uptodate_lock, flags);
Matthew Wilcox (Oracle)b21866f2020-09-21 08:58:40 -0700148 bitmap_set(iop->uptodate, first, last - first + 1);
149 if (bitmap_full(iop->uptodate, i_blocks_per_page(inode, page)))
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800150 SetPageUptodate(page);
151 spin_unlock_irqrestore(&iop->uptodate_lock, flags);
152}
153
154static void
155iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
156{
157 if (PageError(page))
158 return;
159
160 if (page_has_private(page))
161 iomap_iop_set_range_uptodate(page, off, len);
162 else
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700163 SetPageUptodate(page);
164}
165
166static void
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700167iomap_read_page_end_io(struct bio_vec *bvec, int error)
168{
169 struct page *page = bvec->bv_page;
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -0400170 struct folio *folio = page_folio(page);
171 struct iomap_page *iop = to_iomap_page(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700172
173 if (unlikely(error)) {
174 ClearPageUptodate(page);
175 SetPageError(page);
176 } else {
177 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
178 }
179
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -0700180 if (!iop || atomic_sub_and_test(bvec->bv_len, &iop->read_bytes_pending))
181 unlock_page(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700182}
183
184static void
185iomap_read_end_io(struct bio *bio)
186{
187 int error = blk_status_to_errno(bio->bi_status);
188 struct bio_vec *bvec;
189 struct bvec_iter_all iter_all;
190
191 bio_for_each_segment_all(bvec, bio, iter_all)
192 iomap_read_page_end_io(bvec, error);
193 bio_put(bio);
194}
195
196struct iomap_readpage_ctx {
197 struct page *cur_page;
198 bool cur_page_in_bio;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700199 struct bio *bio;
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700200 struct readahead_control *rac;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700201};
202
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800203/**
204 * iomap_read_inline_data - copy inline data into the page cache
205 * @iter: iteration structure
206 * @page: page to copy to
207 *
208 * Copy the inline data in @iter into @page and zero out the rest of the page.
209 * Only a single IOMAP_INLINE extent is allowed at the end of each file.
210 * Returns zero for success to complete the read, or the usual negative errno.
211 */
212static int iomap_read_inline_data(const struct iomap_iter *iter,
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700213 struct page *page)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700214{
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400215 struct folio *folio = page_folio(page);
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700216 const struct iomap *iomap = iomap_iter_srcmap(iter);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700217 size_t size = i_size_read(iter->inode) - iomap->offset;
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700218 size_t poff = offset_in_page(iomap->offset);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700219 void *addr;
220
221 if (PageUptodate(page))
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800222 return 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700223
Matthew Wilcox (Oracle)ae44f9c2021-08-04 20:07:34 -0700224 if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
225 return -EIO;
Gao Xiang69f4a262021-08-03 09:38:22 -0700226 if (WARN_ON_ONCE(size > PAGE_SIZE -
227 offset_in_page(iomap->inline_data)))
228 return -EIO;
229 if (WARN_ON_ONCE(size > iomap->length))
230 return -EIO;
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700231 if (poff > 0)
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400232 iomap_page_create(iter->inode, folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700233
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700234 addr = kmap_local_page(page) + poff;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700235 memcpy(addr, iomap->inline_data, size);
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700236 memset(addr + size, 0, PAGE_SIZE - poff - size);
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700237 kunmap_local(addr);
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700238 iomap_set_range_uptodate(page, poff, PAGE_SIZE - poff);
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800239 return 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700240}
241
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700242static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700243 loff_t pos)
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700244{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700245 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700246
247 return srcmap->type != IOMAP_MAPPED ||
248 (srcmap->flags & IOMAP_F_NEW) ||
249 pos >= i_size_read(iter->inode);
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700250}
251
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700252static loff_t iomap_readpage_iter(const struct iomap_iter *iter,
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700253 struct iomap_readpage_ctx *ctx, loff_t offset)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700254{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700255 const struct iomap *iomap = &iter->iomap;
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700256 loff_t pos = iter->pos + offset;
257 loff_t length = iomap_length(iter) - offset;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700258 struct page *page = ctx->cur_page;
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400259 struct folio *folio = page_folio(page);
Andreas Gruenbacher637d3372021-07-15 09:58:05 -0700260 struct iomap_page *iop;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700261 loff_t orig_pos = pos;
262 unsigned poff, plen;
263 sector_t sector;
264
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800265 if (iomap->type == IOMAP_INLINE)
266 return iomap_read_inline_data(iter, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700267
268 /* zero post-eof blocks as the page may be mapped */
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400269 iop = iomap_page_create(iter->inode, folio);
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700270 iomap_adjust_read_range(iter->inode, iop, &pos, length, &poff, &plen);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700271 if (plen == 0)
272 goto done;
273
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700274 if (iomap_block_needs_zeroing(iter, pos)) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700275 zero_user(page, poff, plen);
276 iomap_set_range_uptodate(page, poff, plen);
277 goto done;
278 }
279
280 ctx->cur_page_in_bio = true;
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -0700281 if (iop)
282 atomic_add(plen, &iop->read_bytes_pending);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700283
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700284 sector = iomap_sector(iomap, pos);
Christoph Hellwigd0364f92021-08-02 14:43:43 -0700285 if (!ctx->bio ||
286 bio_end_sector(ctx->bio) != sector ||
287 bio_add_page(ctx->bio, page, plen, poff) != plen) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700288 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
Matthew Wilcox (Oracle)457df332020-04-02 09:08:53 -0700289 gfp_t orig_gfp = gfp;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000290 unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700291
292 if (ctx->bio)
293 submit_bio(ctx->bio);
294
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700295 if (ctx->rac) /* same as readahead_gfp_mask */
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700296 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000297 ctx->bio = bio_alloc(gfp, bio_max_segs(nr_vecs));
Matthew Wilcox (Oracle)457df332020-04-02 09:08:53 -0700298 /*
299 * If the bio_alloc fails, try it again for a single page to
300 * avoid having to deal with partial page reads. This emulates
301 * what do_mpage_readpage does.
302 */
303 if (!ctx->bio)
304 ctx->bio = bio_alloc(orig_gfp, 1);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700305 ctx->bio->bi_opf = REQ_OP_READ;
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700306 if (ctx->rac)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700307 ctx->bio->bi_opf |= REQ_RAHEAD;
308 ctx->bio->bi_iter.bi_sector = sector;
309 bio_set_dev(ctx->bio, iomap->bdev);
310 ctx->bio->bi_end_io = iomap_read_end_io;
Christoph Hellwigd0364f92021-08-02 14:43:43 -0700311 __bio_add_page(ctx->bio, page, plen, poff);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700312 }
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700313done:
314 /*
315 * Move the caller beyond our range so that it keeps making progress.
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700316 * For that, we have to include any leading non-uptodate ranges, but
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700317 * we can skip trailing ones as they will be handled in the next
318 * iteration.
319 */
320 return pos - orig_pos + plen;
321}
322
323int
324iomap_readpage(struct page *page, const struct iomap_ops *ops)
325{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700326 struct iomap_iter iter = {
327 .inode = page->mapping->host,
328 .pos = page_offset(page),
329 .len = PAGE_SIZE,
330 };
331 struct iomap_readpage_ctx ctx = {
332 .cur_page = page,
333 };
334 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700335
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700336 trace_iomap_readpage(page->mapping->host, 1);
337
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700338 while ((ret = iomap_iter(&iter, ops)) > 0)
339 iter.processed = iomap_readpage_iter(&iter, &ctx, 0);
340
341 if (ret < 0)
342 SetPageError(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700343
344 if (ctx.bio) {
345 submit_bio(ctx.bio);
346 WARN_ON_ONCE(!ctx.cur_page_in_bio);
347 } else {
348 WARN_ON_ONCE(ctx.cur_page_in_bio);
349 unlock_page(page);
350 }
351
352 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700353 * Just like mpage_readahead and block_read_full_page, we always
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700354 * return 0 and just mark the page as PageError on errors. This
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700355 * should be cleaned up throughout the stack eventually.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700356 */
357 return 0;
358}
359EXPORT_SYMBOL_GPL(iomap_readpage);
360
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700361static loff_t iomap_readahead_iter(const struct iomap_iter *iter,
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700362 struct iomap_readpage_ctx *ctx)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700363{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700364 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700365 loff_t done, ret;
366
367 for (done = 0; done < length; done += ret) {
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700368 if (ctx->cur_page && offset_in_page(iter->pos + done) == 0) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700369 if (!ctx->cur_page_in_bio)
370 unlock_page(ctx->cur_page);
371 put_page(ctx->cur_page);
372 ctx->cur_page = NULL;
373 }
374 if (!ctx->cur_page) {
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700375 ctx->cur_page = readahead_page(ctx->rac);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700376 ctx->cur_page_in_bio = false;
377 }
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700378 ret = iomap_readpage_iter(iter, ctx, done);
Andreas Gruenbacherd8af4042021-11-17 17:59:01 -0800379 if (ret <= 0)
380 return ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700381 }
382
383 return done;
384}
385
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700386/**
387 * iomap_readahead - Attempt to read pages from a file.
388 * @rac: Describes the pages to be read.
389 * @ops: The operations vector for the filesystem.
390 *
391 * This function is for filesystems to call to implement their readahead
392 * address_space operation.
393 *
394 * Context: The @ops callbacks may submit I/O (eg to read the addresses of
395 * blocks from disc), and may wait for it. The caller may be trying to
396 * access a different page, and so sleeping excessively should be avoided.
397 * It may allocate memory, but should avoid costly allocations. This
398 * function is called with memalloc_nofs set, so allocations will not cause
399 * the filesystem to be reentered.
400 */
401void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700402{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700403 struct iomap_iter iter = {
404 .inode = rac->mapping->host,
405 .pos = readahead_pos(rac),
406 .len = readahead_length(rac),
407 };
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700408 struct iomap_readpage_ctx ctx = {
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700409 .rac = rac,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700410 };
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700411
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700412 trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700413
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700414 while (iomap_iter(&iter, ops) > 0)
415 iter.processed = iomap_readahead_iter(&iter, &ctx);
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700416
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700417 if (ctx.bio)
418 submit_bio(ctx.bio);
419 if (ctx.cur_page) {
420 if (!ctx.cur_page_in_bio)
421 unlock_page(ctx.cur_page);
422 put_page(ctx.cur_page);
423 }
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700424}
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700425EXPORT_SYMBOL_GPL(iomap_readahead);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700426
427/*
428 * iomap_is_partially_uptodate checks whether blocks within a page are
429 * uptodate or not.
430 *
431 * Returns true if all blocks which correspond to a file portion
432 * we want to read within the page are uptodate.
433 */
434int
435iomap_is_partially_uptodate(struct page *page, unsigned long from,
436 unsigned long count)
437{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -0400438 struct folio *folio = page_folio(page);
439 struct iomap_page *iop = to_iomap_page(folio);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700440 struct inode *inode = page->mapping->host;
441 unsigned len, first, last;
442 unsigned i;
443
444 /* Limit range to one page */
445 len = min_t(unsigned, PAGE_SIZE - from, count);
446
447 /* First and last blocks in range within page */
448 first = from >> inode->i_blkbits;
449 last = (from + len - 1) >> inode->i_blkbits;
450
451 if (iop) {
452 for (i = first; i <= last; i++)
453 if (!test_bit(i, iop->uptodate))
454 return 0;
455 return 1;
456 }
457
458 return 0;
459}
460EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
461
462int
463iomap_releasepage(struct page *page, gfp_t gfp_mask)
464{
Matthew Wilcox (Oracle)1ac99452020-03-05 07:21:43 -0800465 trace_iomap_releasepage(page->mapping->host, page_offset(page),
466 PAGE_SIZE);
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700467
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700468 /*
469 * mm accommodates an old ext3 case where clean pages might not have had
470 * the dirty bit cleared. Thus, it can send actual dirty pages to
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700471 * ->releasepage() via shrink_active_list(); skip those here.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700472 */
473 if (PageDirty(page) || PageWriteback(page))
474 return 0;
475 iomap_page_release(page);
476 return 1;
477}
478EXPORT_SYMBOL_GPL(iomap_releasepage);
479
480void
481iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
482{
Matthew Wilcox (Oracle)1ac99452020-03-05 07:21:43 -0800483 trace_iomap_invalidatepage(page->mapping->host, offset, len);
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700484
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700485 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700486 * If we're invalidating the entire page, clear the dirty state from it
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700487 * and release it to avoid unnecessary buildup of the LRU.
488 */
489 if (offset == 0 && len == PAGE_SIZE) {
490 WARN_ON_ONCE(PageWriteback(page));
491 cancel_dirty_page(page);
492 iomap_page_release(page);
493 }
494}
495EXPORT_SYMBOL_GPL(iomap_invalidatepage);
496
497#ifdef CONFIG_MIGRATION
498int
499iomap_migrate_page(struct address_space *mapping, struct page *newpage,
500 struct page *page, enum migrate_mode mode)
501{
502 int ret;
503
Linus Torvalds26473f82019-07-19 11:38:12 -0700504 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700505 if (ret != MIGRATEPAGE_SUCCESS)
506 return ret;
507
Guoqing Jiang58aeb732020-06-01 21:47:54 -0700508 if (page_has_private(page))
509 attach_page_private(newpage, detach_page_private(page));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700510
511 if (mode != MIGRATE_SYNC_NO_COPY)
512 migrate_page_copy(newpage, page);
513 else
514 migrate_page_states(newpage, page);
515 return MIGRATEPAGE_SUCCESS;
516}
517EXPORT_SYMBOL_GPL(iomap_migrate_page);
518#endif /* CONFIG_MIGRATION */
519
520static void
521iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
522{
523 loff_t i_size = i_size_read(inode);
524
525 /*
526 * Only truncate newly allocated pages beyoned EOF, even if the
527 * write started inside the existing inode size.
528 */
529 if (pos + len > i_size)
530 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
531}
532
533static int
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700534iomap_read_page_sync(loff_t block_start, struct page *page, unsigned poff,
Christoph Hellwig1acd9e92021-08-10 18:33:06 -0700535 unsigned plen, const struct iomap *iomap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700536{
537 struct bio_vec bvec;
538 struct bio bio;
539
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700540 bio_init(&bio, &bvec, 1);
541 bio.bi_opf = REQ_OP_READ;
542 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
543 bio_set_dev(&bio, iomap->bdev);
544 __bio_add_page(&bio, page, plen, poff);
545 return submit_bio_wait(&bio);
546}
547
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700548static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700549 unsigned len, struct page *page)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700550{
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400551 struct folio *folio = page_folio(page);
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700552 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -0400553 struct iomap_page *iop = iomap_page_create(iter->inode, folio);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700554 loff_t block_size = i_blocksize(iter->inode);
Nikolay Borisov6cc19c52020-09-10 08:38:06 -0700555 loff_t block_start = round_down(pos, block_size);
556 loff_t block_end = round_up(pos + len, block_size);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700557 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700558
559 if (PageUptodate(page))
560 return 0;
Matthew Wilcox (Oracle)e6e7ca92020-09-10 08:26:17 -0700561 ClearPageError(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700562
563 do {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700564 iomap_adjust_read_range(iter->inode, iop, &block_start,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700565 block_end - block_start, &poff, &plen);
566 if (plen == 0)
567 break;
568
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700569 if (!(iter->flags & IOMAP_UNSHARE) &&
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700570 (from <= poff || from >= poff + plen) &&
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700571 (to <= poff || to >= poff + plen))
572 continue;
573
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700574 if (iomap_block_needs_zeroing(iter, block_start)) {
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700575 if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700576 return -EIO;
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700577 zero_user_segments(page, poff, from, to, poff + plen);
Matthew Wilcox (Oracle)14284fe2020-09-10 08:26:18 -0700578 } else {
579 int status = iomap_read_page_sync(block_start, page,
580 poff, plen, srcmap);
581 if (status)
582 return status;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700583 }
Matthew Wilcox (Oracle)14284fe2020-09-10 08:26:18 -0700584 iomap_set_range_uptodate(page, poff, plen);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700585 } while ((block_start += plen) < block_end);
586
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700587 return 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700588}
589
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700590static int iomap_write_begin_inline(const struct iomap_iter *iter,
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700591 struct page *page)
Gao Xiang69f4a262021-08-03 09:38:22 -0700592{
593 /* needs more work for the tailpacking case; disable for now */
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700594 if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
Gao Xiang69f4a262021-08-03 09:38:22 -0700595 return -EIO;
Andreas Gruenbacher5ad448c2021-11-24 10:15:47 -0800596 return iomap_read_inline_data(iter, page);
Gao Xiang69f4a262021-08-03 09:38:22 -0700597}
598
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700599static int iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
600 unsigned len, struct page **pagep)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700601{
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700602 const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700603 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700604 struct page *page;
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400605 struct folio *folio;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700606 int status = 0;
607
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700608 BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
609 if (srcmap != &iter->iomap)
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700610 BUG_ON(pos + len > srcmap->offset + srcmap->length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700611
612 if (fatal_signal_pending(current))
613 return -EINTR;
614
615 if (page_ops && page_ops->page_prepare) {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700616 status = page_ops->page_prepare(iter->inode, pos, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700617 if (status)
618 return status;
619 }
620
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700621 page = grab_cache_page_write_begin(iter->inode->i_mapping,
622 pos >> PAGE_SHIFT, AOP_FLAG_NOFS);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700623 if (!page) {
624 status = -ENOMEM;
625 goto out_no_page;
626 }
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400627 folio = page_folio(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700628
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700629 if (srcmap->type == IOMAP_INLINE)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700630 status = iomap_write_begin_inline(iter, page);
631 else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400632 status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700633 else
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700634 status = __iomap_write_begin(iter, pos, len, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700635
636 if (unlikely(status))
637 goto out_unlock;
638
639 *pagep = page;
640 return 0;
641
642out_unlock:
643 unlock_page(page);
644 put_page(page);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700645 iomap_write_failed(iter->inode, pos, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700646
647out_no_page:
648 if (page_ops && page_ops->page_done)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700649 page_ops->page_done(iter->inode, pos, 0, NULL);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700650 return status;
651}
652
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700653static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
654 size_t copied, struct page *page)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700655{
656 flush_dcache_page(page);
657
658 /*
659 * The blocks that were entirely written will now be uptodate, so we
660 * don't have to worry about a readpage reading them and overwriting a
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700661 * partial write. However, if we've encountered a short write and only
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700662 * partially written into a block, it will not be marked uptodate, so a
663 * readpage might come in and destroy our partial write.
664 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700665 * Do the simplest thing and just treat any short write to a
666 * non-uptodate page as a zero-length write, and force the caller to
667 * redo the whole thing.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700668 */
669 if (unlikely(copied < len && !PageUptodate(page)))
670 return 0;
671 iomap_set_range_uptodate(page, offset_in_page(pos), len);
Matthew Wilcox (Oracle)fd7353f2021-06-28 19:36:21 -0700672 __set_page_dirty_nobuffers(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700673 return copied;
674}
675
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700676static size_t iomap_write_end_inline(const struct iomap_iter *iter,
677 struct page *page, loff_t pos, size_t copied)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700678{
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700679 const struct iomap *iomap = &iter->iomap;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700680 void *addr;
681
682 WARN_ON_ONCE(!PageUptodate(page));
Gao Xiang69f4a262021-08-03 09:38:22 -0700683 BUG_ON(!iomap_inline_data_valid(iomap));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700684
Matthew Wilcox (Oracle)7ed3cd12020-09-21 08:58:38 -0700685 flush_dcache_page(page);
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700686 addr = kmap_local_page(page) + pos;
687 memcpy(iomap_inline_data(iomap, pos), addr, copied);
688 kunmap_local(addr);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700689
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700690 mark_inode_dirty(iter->inode);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700691 return copied;
692}
693
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700694/* Returns the number of bytes copied. May be 0. Cannot be an errno. */
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700695static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len,
696 size_t copied, struct page *page)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700697{
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700698 const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700699 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700700 loff_t old_size = iter->inode->i_size;
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700701 size_t ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700702
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700703 if (srcmap->type == IOMAP_INLINE) {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700704 ret = iomap_write_end_inline(iter, page, pos, copied);
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700705 } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700706 ret = block_write_end(NULL, iter->inode->i_mapping, pos, len,
707 copied, page, NULL);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700708 } else {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700709 ret = __iomap_write_end(iter->inode, pos, len, copied, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700710 }
711
712 /*
713 * Update the in-memory inode size after copying the data into the page
714 * cache. It's up to the file system to write the updated size to disk,
715 * preferably after I/O completion so that no stale data is exposed.
716 */
717 if (pos + ret > old_size) {
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700718 i_size_write(iter->inode, pos + ret);
719 iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700720 }
721 unlock_page(page);
722
723 if (old_size < pos)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700724 pagecache_isize_extended(iter->inode, old_size, pos);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700725 if (page_ops && page_ops->page_done)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700726 page_ops->page_done(iter->inode, pos, ret, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700727 put_page(page);
728
729 if (ret < len)
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700730 iomap_write_failed(iter->inode, pos, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700731 return ret;
732}
733
Christoph Hellwigce83a022021-08-10 18:33:08 -0700734static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700735{
Christoph Hellwigce83a022021-08-10 18:33:08 -0700736 loff_t length = iomap_length(iter);
737 loff_t pos = iter->pos;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700738 ssize_t written = 0;
Christoph Hellwigce83a022021-08-10 18:33:08 -0700739 long status = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700740
741 do {
742 struct page *page;
743 unsigned long offset; /* Offset into pagecache page */
744 unsigned long bytes; /* Bytes to write to page */
745 size_t copied; /* Bytes copied from user */
746
747 offset = offset_in_page(pos);
748 bytes = min_t(unsigned long, PAGE_SIZE - offset,
749 iov_iter_count(i));
750again:
751 if (bytes > length)
752 bytes = length;
753
754 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700755 * Bring in the user page that we'll copy from _first_.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700756 * Otherwise there's a nasty deadlock on copying from the
757 * same page as we're writing to, without it being marked
758 * up-to-date.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700759 */
Andreas Gruenbachera6294592021-08-02 14:54:16 +0200760 if (unlikely(fault_in_iov_iter_readable(i, bytes))) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700761 status = -EFAULT;
762 break;
763 }
764
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700765 status = iomap_write_begin(iter, pos, bytes, &page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700766 if (unlikely(status))
767 break;
768
Christoph Hellwigce83a022021-08-10 18:33:08 -0700769 if (mapping_writably_mapped(iter->inode->i_mapping))
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700770 flush_dcache_page(page);
771
Al Virof0b65f32021-04-30 10:26:41 -0400772 copied = copy_page_from_iter_atomic(page, offset, bytes, i);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700773
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700774 status = iomap_write_end(iter, pos, bytes, copied, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700775
Al Virof0b65f32021-04-30 10:26:41 -0400776 if (unlikely(copied != status))
777 iov_iter_revert(i, copied - status);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700778
Al Virof0b65f32021-04-30 10:26:41 -0400779 cond_resched();
Al Virobc1bb412021-05-31 00:32:44 -0400780 if (unlikely(status == 0)) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700781 /*
Al Virobc1bb412021-05-31 00:32:44 -0400782 * A short copy made iomap_write_end() reject the
783 * thing entirely. Might be memory poisoning
784 * halfway through, might be a race with munmap,
785 * might be severe memory pressure.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700786 */
Al Virobc1bb412021-05-31 00:32:44 -0400787 if (copied)
788 bytes = copied;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700789 goto again;
790 }
Al Virof0b65f32021-04-30 10:26:41 -0400791 pos += status;
792 written += status;
793 length -= status;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700794
Christoph Hellwigce83a022021-08-10 18:33:08 -0700795 balance_dirty_pages_ratelimited(iter->inode->i_mapping);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700796 } while (iov_iter_count(i) && length);
797
798 return written ? written : status;
799}
800
801ssize_t
Christoph Hellwigce83a022021-08-10 18:33:08 -0700802iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700803 const struct iomap_ops *ops)
804{
Christoph Hellwigce83a022021-08-10 18:33:08 -0700805 struct iomap_iter iter = {
806 .inode = iocb->ki_filp->f_mapping->host,
807 .pos = iocb->ki_pos,
808 .len = iov_iter_count(i),
809 .flags = IOMAP_WRITE,
810 };
811 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700812
Christoph Hellwigce83a022021-08-10 18:33:08 -0700813 while ((ret = iomap_iter(&iter, ops)) > 0)
814 iter.processed = iomap_write_iter(&iter, i);
815 if (iter.pos == iocb->ki_pos)
816 return ret;
817 return iter.pos - iocb->ki_pos;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700818}
819EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
820
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700821static loff_t iomap_unshare_iter(struct iomap_iter *iter)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700822{
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700823 struct iomap *iomap = &iter->iomap;
Christoph Hellwigfad0a1a2021-08-10 18:33:16 -0700824 const struct iomap *srcmap = iomap_iter_srcmap(iter);
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700825 loff_t pos = iter->pos;
826 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700827 long status = 0;
Matthew Wilcox (Oracle)d4ff3b22020-06-08 20:58:29 -0700828 loff_t written = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700829
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700830 /* don't bother with blocks that are not shared to start with */
831 if (!(iomap->flags & IOMAP_F_SHARED))
832 return length;
833 /* don't bother with holes or unwritten extents */
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700834 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700835 return length;
836
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700837 do {
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700838 unsigned long offset = offset_in_page(pos);
839 unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length);
840 struct page *page;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700841
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700842 status = iomap_write_begin(iter, pos, bytes, &page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700843 if (unlikely(status))
844 return status;
845
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700846 status = iomap_write_end(iter, pos, bytes, bytes, page);
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700847 if (WARN_ON_ONCE(status == 0))
848 return -EIO;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700849
850 cond_resched();
851
852 pos += status;
853 written += status;
854 length -= status;
855
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700856 balance_dirty_pages_ratelimited(iter->inode->i_mapping);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700857 } while (length);
858
859 return written;
860}
861
862int
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700863iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700864 const struct iomap_ops *ops)
865{
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700866 struct iomap_iter iter = {
867 .inode = inode,
868 .pos = pos,
869 .len = len,
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700870 .flags = IOMAP_WRITE | IOMAP_UNSHARE,
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700871 };
872 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700873
Christoph Hellwig8fc274d2021-08-10 18:33:09 -0700874 while ((ret = iomap_iter(&iter, ops)) > 0)
875 iter.processed = iomap_unshare_iter(&iter);
876 return ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700877}
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700878EXPORT_SYMBOL_GPL(iomap_file_unshare);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700879
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700880static s64 __iomap_zero_iter(struct iomap_iter *iter, loff_t pos, u64 length)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700881{
882 struct page *page;
883 int status;
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700884 unsigned offset = offset_in_page(pos);
885 unsigned bytes = min_t(u64, PAGE_SIZE - offset, length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700886
Christoph Hellwigb74b1292021-08-10 18:33:14 -0700887 status = iomap_write_begin(iter, pos, bytes, &page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700888 if (status)
889 return status;
890
891 zero_user(page, offset, bytes);
892 mark_page_accessed(page);
893
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700894 return iomap_write_end(iter, pos, bytes, bytes, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700895}
896
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700897static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700898{
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700899 struct iomap *iomap = &iter->iomap;
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)81ee8e52020-09-21 08:58:42 -0700910 s64 bytes;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700911
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700912 if (IS_DAX(iter->inode))
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700913 bytes = dax_iomap_zero(pos, length, iomap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700914 else
Christoph Hellwig1b5c1e32021-08-10 18:33:14 -0700915 bytes = __iomap_zero_iter(iter, pos, length);
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700916 if (bytes < 0)
917 return bytes;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700918
919 pos += bytes;
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700920 length -= bytes;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700921 written += bytes;
922 if (did_zero)
923 *did_zero = true;
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700924 } while (length > 0);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700925
926 return written;
927}
928
929int
930iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
931 const struct iomap_ops *ops)
932{
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700933 struct iomap_iter iter = {
934 .inode = inode,
935 .pos = pos,
936 .len = len,
937 .flags = IOMAP_ZERO,
938 };
939 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700940
Christoph Hellwig2aa30482021-08-10 18:33:09 -0700941 while ((ret = iomap_iter(&iter, ops)) > 0)
942 iter.processed = iomap_zero_iter(&iter, did_zero);
943 return ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700944}
945EXPORT_SYMBOL_GPL(iomap_zero_range);
946
947int
948iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
949 const struct iomap_ops *ops)
950{
951 unsigned int blocksize = i_blocksize(inode);
952 unsigned int off = pos & (blocksize - 1);
953
954 /* Block boundary? Nothing to do */
955 if (!off)
956 return 0;
957 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
958}
959EXPORT_SYMBOL_GPL(iomap_truncate_page);
960
Christoph Hellwig253564b2021-08-10 18:33:09 -0700961static loff_t iomap_page_mkwrite_iter(struct iomap_iter *iter,
962 struct page *page)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700963{
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400964 struct folio *folio = page_folio(page);
Christoph Hellwig253564b2021-08-10 18:33:09 -0700965 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700966 int ret;
967
Christoph Hellwig253564b2021-08-10 18:33:09 -0700968 if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
Matthew Wilcox (Oracle)d1bd0b42021-11-03 14:05:47 -0400969 ret = __block_write_begin_int(folio, iter->pos, length, NULL,
Christoph Hellwig253564b2021-08-10 18:33:09 -0700970 &iter->iomap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700971 if (ret)
972 return ret;
973 block_commit_write(page, 0, length);
974 } else {
975 WARN_ON_ONCE(!PageUptodate(page));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700976 set_page_dirty(page);
977 }
978
979 return length;
980}
981
982vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
983{
Christoph Hellwig253564b2021-08-10 18:33:09 -0700984 struct iomap_iter iter = {
985 .inode = file_inode(vmf->vma->vm_file),
986 .flags = IOMAP_WRITE | IOMAP_FAULT,
987 };
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700988 struct page *page = vmf->page;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700989 ssize_t ret;
990
991 lock_page(page);
Christoph Hellwig253564b2021-08-10 18:33:09 -0700992 ret = page_mkwrite_check_truncate(page, iter.inode);
Andreas Gruenbacher243145b2020-01-06 08:58:23 -0800993 if (ret < 0)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700994 goto out_unlock;
Christoph Hellwig253564b2021-08-10 18:33:09 -0700995 iter.pos = page_offset(page);
996 iter.len = ret;
997 while ((ret = iomap_iter(&iter, ops)) > 0)
998 iter.processed = iomap_page_mkwrite_iter(&iter, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700999
Christoph Hellwig253564b2021-08-10 18:33:09 -07001000 if (ret < 0)
1001 goto out_unlock;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07001002 wait_for_stable_page(page);
1003 return VM_FAULT_LOCKED;
1004out_unlock:
1005 unlock_page(page);
1006 return block_page_mkwrite_return(ret);
1007}
1008EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001009
1010static void
Christoph Hellwig48d64cd2019-10-17 13:12:22 -07001011iomap_finish_page_writeback(struct inode *inode, struct page *page,
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001012 int error, unsigned int len)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001013{
Matthew Wilcox (Oracle)95c4cd02021-04-27 22:56:17 -04001014 struct folio *folio = page_folio(page);
1015 struct iomap_page *iop = to_iomap_page(folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001016
1017 if (error) {
Christoph Hellwig48d64cd2019-10-17 13:12:22 -07001018 SetPageError(page);
Darrick J. Wongb69eea82021-08-10 18:32:55 -07001019 mapping_set_error(inode->i_mapping, error);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001020 }
1021
Matthew Wilcox (Oracle)24addd82020-09-21 08:58:39 -07001022 WARN_ON_ONCE(i_blocks_per_page(inode, page) > 1 && !iop);
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001023 WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001024
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001025 if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending))
Christoph Hellwig48d64cd2019-10-17 13:12:22 -07001026 end_page_writeback(page);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001027}
1028
1029/*
1030 * We're now finished for good with this ioend structure. Update the page
1031 * state, release holds on bios, and finally free up memory. Do not use the
1032 * ioend after this.
1033 */
1034static void
1035iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1036{
1037 struct inode *inode = ioend->io_inode;
1038 struct bio *bio = &ioend->io_inline_bio;
1039 struct bio *last = ioend->io_bio, *next;
1040 u64 start = bio->bi_iter.bi_sector;
Zorro Langc2757792019-12-04 22:59:02 -08001041 loff_t offset = ioend->io_offset;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001042 bool quiet = bio_flagged(bio, BIO_QUIET);
1043
1044 for (bio = &ioend->io_inline_bio; bio; bio = next) {
1045 struct bio_vec *bv;
1046 struct bvec_iter_all iter_all;
1047
1048 /*
1049 * For the last bio, bi_private points to the ioend, so we
1050 * need to explicitly end the iteration here.
1051 */
1052 if (bio == last)
1053 next = NULL;
1054 else
1055 next = bio->bi_private;
1056
1057 /* walk each page on bio, ending page IO on them */
1058 bio_for_each_segment_all(bv, bio, iter_all)
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001059 iomap_finish_page_writeback(inode, bv->bv_page, error,
1060 bv->bv_len);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001061 bio_put(bio);
1062 }
Zorro Langc2757792019-12-04 22:59:02 -08001063 /* The ioend has been freed by bio_put() */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001064
1065 if (unlikely(error && !quiet)) {
1066 printk_ratelimited(KERN_ERR
Darrick J. Wong9cd0ed62019-10-17 14:02:07 -07001067"%s: writeback error on inode %lu, offset %lld, sector %llu",
Zorro Langc2757792019-12-04 22:59:02 -08001068 inode->i_sb->s_id, inode->i_ino, offset, start);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001069 }
1070}
1071
1072void
1073iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1074{
1075 struct list_head tmp;
1076
1077 list_replace_init(&ioend->io_list, &tmp);
1078 iomap_finish_ioend(ioend, error);
1079
1080 while (!list_empty(&tmp)) {
1081 ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1082 list_del_init(&ioend->io_list);
1083 iomap_finish_ioend(ioend, error);
1084 }
1085}
1086EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1087
1088/*
1089 * We can merge two adjacent ioends if they have the same set of work to do.
1090 */
1091static bool
1092iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1093{
1094 if (ioend->io_bio->bi_status != next->io_bio->bi_status)
1095 return false;
1096 if ((ioend->io_flags & IOMAP_F_SHARED) ^
1097 (next->io_flags & IOMAP_F_SHARED))
1098 return false;
1099 if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1100 (next->io_type == IOMAP_UNWRITTEN))
1101 return false;
1102 if (ioend->io_offset + ioend->io_size != next->io_offset)
1103 return false;
1104 return true;
1105}
1106
1107void
Brian Foster6e552492021-05-04 08:54:29 -07001108iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001109{
1110 struct iomap_ioend *next;
1111
1112 INIT_LIST_HEAD(&ioend->io_list);
1113
1114 while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1115 io_list))) {
1116 if (!iomap_ioend_can_merge(ioend, next))
1117 break;
1118 list_move_tail(&next->io_list, &ioend->io_list);
1119 ioend->io_size += next->io_size;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001120 }
1121}
1122EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1123
1124static int
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07001125iomap_ioend_compare(void *priv, const struct list_head *a,
1126 const struct list_head *b)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001127{
Christoph Hellwigb3d423e2019-10-17 13:12:20 -07001128 struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1129 struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001130
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001131 if (ia->io_offset < ib->io_offset)
1132 return -1;
Christoph Hellwigb3d423e2019-10-17 13:12:20 -07001133 if (ia->io_offset > ib->io_offset)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001134 return 1;
1135 return 0;
1136}
1137
1138void
1139iomap_sort_ioends(struct list_head *ioend_list)
1140{
1141 list_sort(NULL, ioend_list, iomap_ioend_compare);
1142}
1143EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1144
1145static void iomap_writepage_end_bio(struct bio *bio)
1146{
1147 struct iomap_ioend *ioend = bio->bi_private;
1148
1149 iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
1150}
1151
1152/*
1153 * Submit the final bio for an ioend.
1154 *
1155 * If @error is non-zero, it means that we have a situation where some part of
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001156 * the submission process has failed after we've marked pages for writeback
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001157 * and unlocked them. In this situation, we need to fail the bio instead of
1158 * submitting it. This typically only happens on a filesystem shutdown.
1159 */
1160static int
1161iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
1162 int error)
1163{
1164 ioend->io_bio->bi_private = ioend;
1165 ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
1166
1167 if (wpc->ops->prepare_ioend)
1168 error = wpc->ops->prepare_ioend(ioend, error);
1169 if (error) {
1170 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001171 * If we're failing the IO now, just mark the ioend with an
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001172 * error and finish it. This will run IO completion immediately
1173 * as there is only one reference to the ioend at this point in
1174 * time.
1175 */
1176 ioend->io_bio->bi_status = errno_to_blk_status(error);
1177 bio_endio(ioend->io_bio);
1178 return error;
1179 }
1180
1181 submit_bio(ioend->io_bio);
1182 return 0;
1183}
1184
1185static struct iomap_ioend *
1186iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
1187 loff_t offset, sector_t sector, struct writeback_control *wbc)
1188{
1189 struct iomap_ioend *ioend;
1190 struct bio *bio;
1191
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001192 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &iomap_ioend_bioset);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001193 bio_set_dev(bio, wpc->iomap.bdev);
1194 bio->bi_iter.bi_sector = sector;
1195 bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
1196 bio->bi_write_hint = inode->i_write_hint;
1197 wbc_init_bio(wbc, bio);
1198
1199 ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
1200 INIT_LIST_HEAD(&ioend->io_list);
1201 ioend->io_type = wpc->iomap.type;
1202 ioend->io_flags = wpc->iomap.flags;
1203 ioend->io_inode = inode;
1204 ioend->io_size = 0;
1205 ioend->io_offset = offset;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001206 ioend->io_bio = bio;
1207 return ioend;
1208}
1209
1210/*
1211 * Allocate a new bio, and chain the old bio to the new one.
1212 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001213 * Note that we have to perform the chaining in this unintuitive order
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001214 * so that the bi_private linkage is set up in the right direction for the
1215 * traversal in iomap_finish_ioend().
1216 */
1217static struct bio *
1218iomap_chain_bio(struct bio *prev)
1219{
1220 struct bio *new;
1221
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001222 new = bio_alloc(GFP_NOFS, BIO_MAX_VECS);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001223 bio_copy_dev(new, prev);/* also copies over blkcg information */
1224 new->bi_iter.bi_sector = bio_end_sector(prev);
1225 new->bi_opf = prev->bi_opf;
1226 new->bi_write_hint = prev->bi_write_hint;
1227
1228 bio_chain(prev, new);
1229 bio_get(prev); /* for iomap_finish_ioend */
1230 submit_bio(prev);
1231 return new;
1232}
1233
1234static bool
1235iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
1236 sector_t sector)
1237{
1238 if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1239 (wpc->ioend->io_flags & IOMAP_F_SHARED))
1240 return false;
1241 if (wpc->iomap.type != wpc->ioend->io_type)
1242 return false;
1243 if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
1244 return false;
1245 if (sector != bio_end_sector(wpc->ioend->io_bio))
1246 return false;
1247 return true;
1248}
1249
1250/*
1251 * Test to see if we have an existing ioend structure that we could append to
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001252 * first; otherwise finish off the current ioend and start another.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001253 */
1254static void
1255iomap_add_to_ioend(struct inode *inode, loff_t offset, struct page *page,
1256 struct iomap_page *iop, struct iomap_writepage_ctx *wpc,
1257 struct writeback_control *wbc, struct list_head *iolist)
1258{
1259 sector_t sector = iomap_sector(&wpc->iomap, offset);
1260 unsigned len = i_blocksize(inode);
1261 unsigned poff = offset & (PAGE_SIZE - 1);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001262
1263 if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, offset, sector)) {
1264 if (wpc->ioend)
1265 list_add(&wpc->ioend->io_list, iolist);
1266 wpc->ioend = iomap_alloc_ioend(inode, wpc, offset, sector, wbc);
1267 }
1268
Christoph Hellwigc1b79f12021-08-02 14:43:43 -07001269 if (bio_add_page(wpc->ioend->io_bio, page, len, poff) != len) {
1270 wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio);
1271 __bio_add_page(wpc->ioend->io_bio, page, len, poff);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001272 }
1273
Christoph Hellwigc1b79f12021-08-02 14:43:43 -07001274 if (iop)
1275 atomic_add(len, &iop->write_bytes_pending);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001276 wpc->ioend->io_size += len;
1277 wbc_account_cgroup_owner(wbc, page, len);
1278}
1279
1280/*
1281 * We implement an immediate ioend submission policy here to avoid needing to
1282 * chain multiple ioends and hence nest mempool allocations which can violate
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001283 * the forward progress guarantees we need to provide. The current ioend we're
1284 * adding blocks to is cached in the writepage context, and if the new block
1285 * doesn't append to the cached ioend, it will create a new ioend and cache that
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001286 * instead.
1287 *
1288 * If a new ioend is created and cached, the old ioend is returned and queued
1289 * locally for submission once the entire page is processed or an error has been
1290 * detected. While ioends are submitted immediately after they are completed,
1291 * batching optimisations are provided by higher level block plugging.
1292 *
1293 * At the end of a writeback pass, there will be a cached ioend remaining on the
1294 * writepage context that the caller will need to submit.
1295 */
1296static int
1297iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1298 struct writeback_control *wbc, struct inode *inode,
1299 struct page *page, u64 end_offset)
1300{
Matthew Wilcox (Oracle)435d44b2021-04-27 23:12:52 -04001301 struct folio *folio = page_folio(page);
1302 struct iomap_page *iop = iomap_page_create(inode, folio);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001303 struct iomap_ioend *ioend, *next;
1304 unsigned len = i_blocksize(inode);
1305 u64 file_offset; /* file offset of page */
1306 int error = 0, count = 0, i;
1307 LIST_HEAD(submit_list);
1308
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001309 WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001310
1311 /*
1312 * Walk through the page to find areas to write back. If we run off the
1313 * end of the current map or find the current map invalid, grab a new
1314 * one.
1315 */
1316 for (i = 0, file_offset = page_offset(page);
1317 i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset;
1318 i++, file_offset += len) {
1319 if (iop && !test_bit(i, iop->uptodate))
1320 continue;
1321
1322 error = wpc->ops->map_blocks(wpc, inode, file_offset);
1323 if (error)
1324 break;
Christoph Hellwig3e19e6f2019-10-17 13:12:17 -07001325 if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
1326 continue;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001327 if (wpc->iomap.type == IOMAP_HOLE)
1328 continue;
1329 iomap_add_to_ioend(inode, file_offset, page, iop, wpc, wbc,
1330 &submit_list);
1331 count++;
1332 }
1333
1334 WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
1335 WARN_ON_ONCE(!PageLocked(page));
1336 WARN_ON_ONCE(PageWriteback(page));
Brian Foster50e7d6c2020-10-29 14:30:49 -07001337 WARN_ON_ONCE(PageDirty(page));
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001338
1339 /*
1340 * We cannot cancel the ioend directly here on error. We may have
1341 * already set other pages under writeback and hence we have to run I/O
1342 * completion to mark the error state of the pages under writeback
1343 * appropriately.
1344 */
1345 if (unlikely(error)) {
Brian Foster763e4cd2020-10-29 14:30:48 -07001346 /*
1347 * Let the filesystem know what portion of the current page
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001348 * failed to map. If the page hasn't been added to ioend, it
Brian Foster763e4cd2020-10-29 14:30:48 -07001349 * won't be affected by I/O completion and we must unlock it
1350 * now.
1351 */
1352 if (wpc->ops->discard_page)
1353 wpc->ops->discard_page(page, file_offset);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001354 if (!count) {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001355 ClearPageUptodate(page);
1356 unlock_page(page);
1357 goto done;
1358 }
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001359 }
1360
Brian Foster50e7d6c2020-10-29 14:30:49 -07001361 set_page_writeback(page);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001362 unlock_page(page);
1363
1364 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001365 * Preserve the original error if there was one; catch
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001366 * submission errors here and propagate into subsequent ioend
1367 * submissions.
1368 */
1369 list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
1370 int error2;
1371
1372 list_del_init(&ioend->io_list);
1373 error2 = iomap_submit_ioend(wpc, ioend, error);
1374 if (error2 && !error)
1375 error = error2;
1376 }
1377
1378 /*
1379 * We can end up here with no error and nothing to write only if we race
1380 * with a partial page truncate on a sub-page block sized filesystem.
1381 */
1382 if (!count)
1383 end_page_writeback(page);
1384done:
1385 mapping_set_error(page->mapping, error);
1386 return error;
1387}
1388
1389/*
1390 * Write out a dirty page.
1391 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001392 * For delalloc space on the page, we need to allocate space and flush it.
1393 * For unwritten space on the page, we need to start the conversion to
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001394 * regular allocated space.
1395 */
1396static int
1397iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)
1398{
1399 struct iomap_writepage_ctx *wpc = data;
1400 struct inode *inode = page->mapping->host;
1401 pgoff_t end_index;
1402 u64 end_offset;
1403 loff_t offset;
1404
Matthew Wilcox (Oracle)1ac99452020-03-05 07:21:43 -08001405 trace_iomap_writepage(inode, page_offset(page), PAGE_SIZE);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001406
1407 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001408 * Refuse to write the page out if we're called from reclaim context.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001409 *
1410 * This avoids stack overflows when called from deeply used stacks in
1411 * random callers for direct reclaim or memcg reclaim. We explicitly
1412 * allow reclaim from kswapd as the stack usage there is relatively low.
1413 *
1414 * This should never happen except in the case of a VM regression so
1415 * warn about it.
1416 */
1417 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1418 PF_MEMALLOC))
1419 goto redirty;
1420
1421 /*
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001422 * Is this page beyond the end of the file?
1423 *
1424 * The page index is less than the end_index, adjust the end_offset
1425 * to the highest offset that this page should represent.
1426 * -----------------------------------------------------
1427 * | file mapping | <EOF> |
1428 * -----------------------------------------------------
1429 * | Page ... | Page N-2 | Page N-1 | Page N | |
1430 * ^--------------------------------^----------|--------
1431 * | desired writeback range | see else |
1432 * ---------------------------------^------------------|
1433 */
1434 offset = i_size_read(inode);
1435 end_index = offset >> PAGE_SHIFT;
1436 if (page->index < end_index)
1437 end_offset = (loff_t)(page->index + 1) << PAGE_SHIFT;
1438 else {
1439 /*
1440 * Check whether the page to write out is beyond or straddles
1441 * i_size or not.
1442 * -------------------------------------------------------
1443 * | file mapping | <EOF> |
1444 * -------------------------------------------------------
1445 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
1446 * ^--------------------------------^-----------|---------
1447 * | | Straddles |
1448 * ---------------------------------^-----------|--------|
1449 */
1450 unsigned offset_into_page = offset & (PAGE_SIZE - 1);
1451
1452 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001453 * Skip the page if it's fully outside i_size, e.g. due to a
1454 * truncate operation that's in progress. We must redirty the
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001455 * page so that reclaim stops reclaiming it. Otherwise
1456 * iomap_vm_releasepage() is called on it and gets confused.
1457 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001458 * Note that the end_index is unsigned long. If the given
1459 * offset is greater than 16TB on a 32-bit system then if we
1460 * checked if the page is fully outside i_size with
1461 * "if (page->index >= end_index + 1)", "end_index + 1" would
1462 * overflow and evaluate to 0. Hence this page would be
1463 * redirtied and written out repeatedly, which would result in
1464 * an infinite loop; the user program performing this operation
1465 * would hang. Instead, we can detect this situation by
1466 * checking if the page is totally beyond i_size or if its
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001467 * offset is just equal to the EOF.
1468 */
1469 if (page->index > end_index ||
1470 (page->index == end_index && offset_into_page == 0))
1471 goto redirty;
1472
1473 /*
1474 * The page straddles i_size. It must be zeroed out on each
1475 * and every writepage invocation because it may be mmapped.
1476 * "A file is mapped in multiples of the page size. For a file
1477 * that is not a multiple of the page size, the remaining
1478 * memory is zeroed when mapped, and writes to that region are
1479 * not written out to the file."
1480 */
1481 zero_user_segment(page, offset_into_page, PAGE_SIZE);
1482
1483 /* Adjust the end_offset to the end of file */
1484 end_offset = offset;
1485 }
1486
1487 return iomap_writepage_map(wpc, wbc, inode, page, end_offset);
1488
1489redirty:
1490 redirty_page_for_writepage(wbc, page);
1491 unlock_page(page);
1492 return 0;
1493}
1494
1495int
1496iomap_writepage(struct page *page, struct writeback_control *wbc,
1497 struct iomap_writepage_ctx *wpc,
1498 const struct iomap_writeback_ops *ops)
1499{
1500 int ret;
1501
1502 wpc->ops = ops;
1503 ret = iomap_do_writepage(page, wbc, wpc);
1504 if (!wpc->ioend)
1505 return ret;
1506 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1507}
1508EXPORT_SYMBOL_GPL(iomap_writepage);
1509
1510int
1511iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1512 struct iomap_writepage_ctx *wpc,
1513 const struct iomap_writeback_ops *ops)
1514{
1515 int ret;
1516
1517 wpc->ops = ops;
1518 ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
1519 if (!wpc->ioend)
1520 return ret;
1521 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1522}
1523EXPORT_SYMBOL_GPL(iomap_writepages);
1524
1525static int __init iomap_init(void)
1526{
1527 return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
1528 offsetof(struct iomap_ioend, io_inline_bio),
1529 BIOSET_NEED_BVECS);
1530}
1531fs_initcall(iomap_init);