blob: 32d80350bb558901410d48a5cc8ee44ddd918f22 [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)0a195b92020-09-21 08:58:40 -070025 * Structure allocated for each page or THP when block size < page size
26 * to track sub-page 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
35static inline struct iomap_page *to_iomap_page(struct page *page)
36{
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070037 /*
38 * per-block data is stored in the head page. Callers should
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -070039 * not be dealing with tail pages, and if they are, they can
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070040 * call thp_head() first.
41 */
42 VM_BUG_ON_PGFLAGS(PageTail(page), page);
43
Christoph Hellwigab08b012019-10-17 13:12:19 -070044 if (page_has_private(page))
45 return (struct iomap_page *)page_private(page);
46 return NULL;
47}
48
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070049static struct bio_set iomap_ioend_bioset;
50
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070051static struct iomap_page *
52iomap_page_create(struct inode *inode, struct page *page)
53{
54 struct iomap_page *iop = to_iomap_page(page);
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070055 unsigned int nr_blocks = i_blocks_per_page(inode, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070056
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070057 if (iop || nr_blocks <= 1)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070058 return iop;
59
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070060 iop = kzalloc(struct_size(iop, uptodate, BITS_TO_LONGS(nr_blocks)),
61 GFP_NOFS | __GFP_NOFAIL);
Christoph Hellwig1cea3352019-12-04 09:33:52 -080062 spin_lock_init(&iop->uptodate_lock);
Matthew Wilcox (Oracle)4595a292020-09-25 11:16:53 -070063 if (PageUptodate(page))
64 bitmap_fill(iop->uptodate, nr_blocks);
Guoqing Jiang58aeb732020-06-01 21:47:54 -070065 attach_page_private(page, iop);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070066 return iop;
67}
68
69static void
70iomap_page_release(struct page *page)
71{
Guoqing Jiang58aeb732020-06-01 21:47:54 -070072 struct iomap_page *iop = detach_page_private(page);
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070073 unsigned int nr_blocks = i_blocks_per_page(page->mapping->host, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070074
75 if (!iop)
76 return;
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -070077 WARN_ON_ONCE(atomic_read(&iop->read_bytes_pending));
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -070078 WARN_ON_ONCE(atomic_read(&iop->write_bytes_pending));
Matthew Wilcox (Oracle)0a195b92020-09-21 08:58:40 -070079 WARN_ON_ONCE(bitmap_full(iop->uptodate, nr_blocks) !=
80 PageUptodate(page));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070081 kfree(iop);
82}
83
84/*
85 * Calculate the range inside the page that we actually need to read.
86 */
87static void
88iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
89 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
90{
91 loff_t orig_pos = *pos;
92 loff_t isize = i_size_read(inode);
93 unsigned block_bits = inode->i_blkbits;
94 unsigned block_size = (1 << block_bits);
95 unsigned poff = offset_in_page(*pos);
96 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
97 unsigned first = poff >> block_bits;
98 unsigned last = (poff + plen - 1) >> block_bits;
99
100 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700101 * If the block size is smaller than the page size, we need to check the
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700102 * per-block uptodate status and adjust the offset and length if needed
103 * to avoid reading in already uptodate ranges.
104 */
105 if (iop) {
106 unsigned int i;
107
108 /* move forward for each leading block marked uptodate */
109 for (i = first; i <= last; i++) {
110 if (!test_bit(i, iop->uptodate))
111 break;
112 *pos += block_size;
113 poff += block_size;
114 plen -= block_size;
115 first++;
116 }
117
118 /* truncate len if we find any trailing uptodate block(s) */
119 for ( ; i <= last; i++) {
120 if (test_bit(i, iop->uptodate)) {
121 plen -= (last - i + 1) * block_size;
122 last = i - 1;
123 break;
124 }
125 }
126 }
127
128 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700129 * If the extent spans the block that contains the i_size, we need to
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700130 * handle both halves separately so that we properly zero data in the
131 * page cache for blocks that are entirely outside of i_size.
132 */
133 if (orig_pos <= isize && orig_pos + length > isize) {
134 unsigned end = offset_in_page(isize - 1) >> block_bits;
135
136 if (first <= end && last > end)
137 plen -= (last - end) * block_size;
138 }
139
140 *offp = poff;
141 *lenp = plen;
142}
143
144static void
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800145iomap_iop_set_range_uptodate(struct page *page, unsigned off, unsigned len)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700146{
147 struct iomap_page *iop = to_iomap_page(page);
148 struct inode *inode = page->mapping->host;
149 unsigned first = off >> inode->i_blkbits;
150 unsigned last = (off + len - 1) >> inode->i_blkbits;
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800151 unsigned long flags;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700152
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800153 spin_lock_irqsave(&iop->uptodate_lock, flags);
Matthew Wilcox (Oracle)b21866f2020-09-21 08:58:40 -0700154 bitmap_set(iop->uptodate, first, last - first + 1);
155 if (bitmap_full(iop->uptodate, i_blocks_per_page(inode, page)))
Christoph Hellwig1cea3352019-12-04 09:33:52 -0800156 SetPageUptodate(page);
157 spin_unlock_irqrestore(&iop->uptodate_lock, flags);
158}
159
160static void
161iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
162{
163 if (PageError(page))
164 return;
165
166 if (page_has_private(page))
167 iomap_iop_set_range_uptodate(page, off, len);
168 else
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700169 SetPageUptodate(page);
170}
171
172static void
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700173iomap_read_page_end_io(struct bio_vec *bvec, int error)
174{
175 struct page *page = bvec->bv_page;
176 struct iomap_page *iop = to_iomap_page(page);
177
178 if (unlikely(error)) {
179 ClearPageUptodate(page);
180 SetPageError(page);
181 } else {
182 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
183 }
184
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -0700185 if (!iop || atomic_sub_and_test(bvec->bv_len, &iop->read_bytes_pending))
186 unlock_page(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700187}
188
189static void
190iomap_read_end_io(struct bio *bio)
191{
192 int error = blk_status_to_errno(bio->bi_status);
193 struct bio_vec *bvec;
194 struct bvec_iter_all iter_all;
195
196 bio_for_each_segment_all(bvec, bio, iter_all)
197 iomap_read_page_end_io(bvec, error);
198 bio_put(bio);
199}
200
201struct iomap_readpage_ctx {
202 struct page *cur_page;
203 bool cur_page_in_bio;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700204 struct bio *bio;
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700205 struct readahead_control *rac;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700206};
207
Christoph Hellwig740499c2021-08-10 18:33:07 -0700208static loff_t iomap_read_inline_data(struct inode *inode, struct page *page,
Christoph Hellwig78c64b02021-08-10 18:33:06 -0700209 const struct iomap *iomap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700210{
Gao Xiang69f4a262021-08-03 09:38:22 -0700211 size_t size = i_size_read(inode) - iomap->offset;
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700212 size_t poff = offset_in_page(iomap->offset);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700213 void *addr;
214
215 if (PageUptodate(page))
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700216 return PAGE_SIZE - poff;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700217
Matthew Wilcox (Oracle)ae44f9c2021-08-04 20:07:34 -0700218 if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
219 return -EIO;
Gao Xiang69f4a262021-08-03 09:38:22 -0700220 if (WARN_ON_ONCE(size > PAGE_SIZE -
221 offset_in_page(iomap->inline_data)))
222 return -EIO;
223 if (WARN_ON_ONCE(size > iomap->length))
224 return -EIO;
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700225 if (poff > 0)
226 iomap_page_create(inode, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700227
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700228 addr = kmap_local_page(page) + poff;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700229 memcpy(addr, iomap->inline_data, size);
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700230 memset(addr + size, 0, PAGE_SIZE - poff - size);
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700231 kunmap_local(addr);
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700232 iomap_set_range_uptodate(page, poff, PAGE_SIZE - poff);
233 return PAGE_SIZE - poff;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700234}
235
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700236static inline bool iomap_block_needs_zeroing(struct inode *inode,
237 struct iomap *iomap, loff_t pos)
238{
239 return iomap->type != IOMAP_MAPPED ||
240 (iomap->flags & IOMAP_F_NEW) ||
241 pos >= i_size_read(inode);
242}
243
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700244static loff_t iomap_readpage_iter(struct iomap_iter *iter,
245 struct iomap_readpage_ctx *ctx, loff_t offset)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700246{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700247 struct iomap *iomap = &iter->iomap;
248 loff_t pos = iter->pos + offset;
249 loff_t length = iomap_length(iter) - offset;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700250 struct page *page = ctx->cur_page;
Andreas Gruenbacher637d3372021-07-15 09:58:05 -0700251 struct iomap_page *iop;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700252 loff_t orig_pos = pos;
253 unsigned poff, plen;
254 sector_t sector;
255
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700256 if (iomap->type == IOMAP_INLINE)
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700257 return min(iomap_read_inline_data(iter->inode, page, iomap),
258 length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700259
260 /* zero post-eof blocks as the page may be mapped */
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700261 iop = iomap_page_create(iter->inode, page);
262 iomap_adjust_read_range(iter->inode, iop, &pos, length, &poff, &plen);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700263 if (plen == 0)
264 goto done;
265
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700266 if (iomap_block_needs_zeroing(iter->inode, iomap, pos)) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700267 zero_user(page, poff, plen);
268 iomap_set_range_uptodate(page, poff, plen);
269 goto done;
270 }
271
272 ctx->cur_page_in_bio = true;
Matthew Wilcox (Oracle)7d636672020-09-21 08:58:40 -0700273 if (iop)
274 atomic_add(plen, &iop->read_bytes_pending);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700275
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700276 sector = iomap_sector(iomap, pos);
Christoph Hellwigd0364f92021-08-02 14:43:43 -0700277 if (!ctx->bio ||
278 bio_end_sector(ctx->bio) != sector ||
279 bio_add_page(ctx->bio, page, plen, poff) != plen) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700280 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
Matthew Wilcox (Oracle)457df332020-04-02 09:08:53 -0700281 gfp_t orig_gfp = gfp;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000282 unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700283
284 if (ctx->bio)
285 submit_bio(ctx->bio);
286
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700287 if (ctx->rac) /* same as readahead_gfp_mask */
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700288 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000289 ctx->bio = bio_alloc(gfp, bio_max_segs(nr_vecs));
Matthew Wilcox (Oracle)457df332020-04-02 09:08:53 -0700290 /*
291 * If the bio_alloc fails, try it again for a single page to
292 * avoid having to deal with partial page reads. This emulates
293 * what do_mpage_readpage does.
294 */
295 if (!ctx->bio)
296 ctx->bio = bio_alloc(orig_gfp, 1);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700297 ctx->bio->bi_opf = REQ_OP_READ;
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700298 if (ctx->rac)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700299 ctx->bio->bi_opf |= REQ_RAHEAD;
300 ctx->bio->bi_iter.bi_sector = sector;
301 bio_set_dev(ctx->bio, iomap->bdev);
302 ctx->bio->bi_end_io = iomap_read_end_io;
Christoph Hellwigd0364f92021-08-02 14:43:43 -0700303 __bio_add_page(ctx->bio, page, plen, poff);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700304 }
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700305done:
306 /*
307 * Move the caller beyond our range so that it keeps making progress.
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700308 * For that, we have to include any leading non-uptodate ranges, but
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700309 * we can skip trailing ones as they will be handled in the next
310 * iteration.
311 */
312 return pos - orig_pos + plen;
313}
314
315int
316iomap_readpage(struct page *page, const struct iomap_ops *ops)
317{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700318 struct iomap_iter iter = {
319 .inode = page->mapping->host,
320 .pos = page_offset(page),
321 .len = PAGE_SIZE,
322 };
323 struct iomap_readpage_ctx ctx = {
324 .cur_page = page,
325 };
326 int ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700327
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700328 trace_iomap_readpage(page->mapping->host, 1);
329
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700330 while ((ret = iomap_iter(&iter, ops)) > 0)
331 iter.processed = iomap_readpage_iter(&iter, &ctx, 0);
332
333 if (ret < 0)
334 SetPageError(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700335
336 if (ctx.bio) {
337 submit_bio(ctx.bio);
338 WARN_ON_ONCE(!ctx.cur_page_in_bio);
339 } else {
340 WARN_ON_ONCE(ctx.cur_page_in_bio);
341 unlock_page(page);
342 }
343
344 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700345 * Just like mpage_readahead and block_read_full_page, we always
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700346 * return 0 and just mark the page as PageError on errors. This
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700347 * should be cleaned up throughout the stack eventually.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700348 */
349 return 0;
350}
351EXPORT_SYMBOL_GPL(iomap_readpage);
352
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700353static loff_t iomap_readahead_iter(struct iomap_iter *iter,
354 struct iomap_readpage_ctx *ctx)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700355{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700356 loff_t length = iomap_length(iter);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700357 loff_t done, ret;
358
359 for (done = 0; done < length; done += ret) {
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700360 if (ctx->cur_page && offset_in_page(iter->pos + done) == 0) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700361 if (!ctx->cur_page_in_bio)
362 unlock_page(ctx->cur_page);
363 put_page(ctx->cur_page);
364 ctx->cur_page = NULL;
365 }
366 if (!ctx->cur_page) {
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700367 ctx->cur_page = readahead_page(ctx->rac);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700368 ctx->cur_page_in_bio = false;
369 }
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700370 ret = iomap_readpage_iter(iter, ctx, done);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700371 }
372
373 return done;
374}
375
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700376/**
377 * iomap_readahead - Attempt to read pages from a file.
378 * @rac: Describes the pages to be read.
379 * @ops: The operations vector for the filesystem.
380 *
381 * This function is for filesystems to call to implement their readahead
382 * address_space operation.
383 *
384 * Context: The @ops callbacks may submit I/O (eg to read the addresses of
385 * blocks from disc), and may wait for it. The caller may be trying to
386 * access a different page, and so sleeping excessively should be avoided.
387 * It may allocate memory, but should avoid costly allocations. This
388 * function is called with memalloc_nofs set, so allocations will not cause
389 * the filesystem to be reentered.
390 */
391void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700392{
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700393 struct iomap_iter iter = {
394 .inode = rac->mapping->host,
395 .pos = readahead_pos(rac),
396 .len = readahead_length(rac),
397 };
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700398 struct iomap_readpage_ctx ctx = {
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700399 .rac = rac,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700400 };
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700401
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700402 trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700403
Christoph Hellwigf6d480002021-08-10 18:33:08 -0700404 while (iomap_iter(&iter, ops) > 0)
405 iter.processed = iomap_readahead_iter(&iter, &ctx);
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700406
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700407 if (ctx.bio)
408 submit_bio(ctx.bio);
409 if (ctx.cur_page) {
410 if (!ctx.cur_page_in_bio)
411 unlock_page(ctx.cur_page);
412 put_page(ctx.cur_page);
413 }
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700414}
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700415EXPORT_SYMBOL_GPL(iomap_readahead);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700416
417/*
418 * iomap_is_partially_uptodate checks whether blocks within a page are
419 * uptodate or not.
420 *
421 * Returns true if all blocks which correspond to a file portion
422 * we want to read within the page are uptodate.
423 */
424int
425iomap_is_partially_uptodate(struct page *page, unsigned long from,
426 unsigned long count)
427{
428 struct iomap_page *iop = to_iomap_page(page);
429 struct inode *inode = page->mapping->host;
430 unsigned len, first, last;
431 unsigned i;
432
433 /* Limit range to one page */
434 len = min_t(unsigned, PAGE_SIZE - from, count);
435
436 /* First and last blocks in range within page */
437 first = from >> inode->i_blkbits;
438 last = (from + len - 1) >> inode->i_blkbits;
439
440 if (iop) {
441 for (i = first; i <= last; i++)
442 if (!test_bit(i, iop->uptodate))
443 return 0;
444 return 1;
445 }
446
447 return 0;
448}
449EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
450
451int
452iomap_releasepage(struct page *page, gfp_t gfp_mask)
453{
Matthew Wilcox (Oracle)1ac99452020-03-05 07:21:43 -0800454 trace_iomap_releasepage(page->mapping->host, page_offset(page),
455 PAGE_SIZE);
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700456
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700457 /*
458 * mm accommodates an old ext3 case where clean pages might not have had
459 * the dirty bit cleared. Thus, it can send actual dirty pages to
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700460 * ->releasepage() via shrink_active_list(); skip those here.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700461 */
462 if (PageDirty(page) || PageWriteback(page))
463 return 0;
464 iomap_page_release(page);
465 return 1;
466}
467EXPORT_SYMBOL_GPL(iomap_releasepage);
468
469void
470iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
471{
Matthew Wilcox (Oracle)1ac99452020-03-05 07:21:43 -0800472 trace_iomap_invalidatepage(page->mapping->host, offset, len);
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700473
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700474 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700475 * If we're invalidating the entire page, clear the dirty state from it
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700476 * and release it to avoid unnecessary buildup of the LRU.
477 */
478 if (offset == 0 && len == PAGE_SIZE) {
479 WARN_ON_ONCE(PageWriteback(page));
480 cancel_dirty_page(page);
481 iomap_page_release(page);
482 }
483}
484EXPORT_SYMBOL_GPL(iomap_invalidatepage);
485
486#ifdef CONFIG_MIGRATION
487int
488iomap_migrate_page(struct address_space *mapping, struct page *newpage,
489 struct page *page, enum migrate_mode mode)
490{
491 int ret;
492
Linus Torvalds26473f82019-07-19 11:38:12 -0700493 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700494 if (ret != MIGRATEPAGE_SUCCESS)
495 return ret;
496
Guoqing Jiang58aeb732020-06-01 21:47:54 -0700497 if (page_has_private(page))
498 attach_page_private(newpage, detach_page_private(page));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700499
500 if (mode != MIGRATE_SYNC_NO_COPY)
501 migrate_page_copy(newpage, page);
502 else
503 migrate_page_states(newpage, page);
504 return MIGRATEPAGE_SUCCESS;
505}
506EXPORT_SYMBOL_GPL(iomap_migrate_page);
507#endif /* CONFIG_MIGRATION */
508
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700509enum {
510 IOMAP_WRITE_F_UNSHARE = (1 << 0),
511};
512
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700513static void
514iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
515{
516 loff_t i_size = i_size_read(inode);
517
518 /*
519 * Only truncate newly allocated pages beyoned EOF, even if the
520 * write started inside the existing inode size.
521 */
522 if (pos + len > i_size)
523 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
524}
525
526static int
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700527iomap_read_page_sync(loff_t block_start, struct page *page, unsigned poff,
Christoph Hellwig1acd9e92021-08-10 18:33:06 -0700528 unsigned plen, const struct iomap *iomap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700529{
530 struct bio_vec bvec;
531 struct bio bio;
532
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700533 bio_init(&bio, &bvec, 1);
534 bio.bi_opf = REQ_OP_READ;
535 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
536 bio_set_dev(&bio, iomap->bdev);
537 __bio_add_page(&bio, page, plen, poff);
538 return submit_bio_wait(&bio);
539}
540
541static int
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700542__iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, int flags,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700543 struct page *page, struct iomap *srcmap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700544{
545 struct iomap_page *iop = iomap_page_create(inode, page);
546 loff_t block_size = i_blocksize(inode);
Nikolay Borisov6cc19c52020-09-10 08:38:06 -0700547 loff_t block_start = round_down(pos, block_size);
548 loff_t block_end = round_up(pos + len, block_size);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700549 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700550
551 if (PageUptodate(page))
552 return 0;
Matthew Wilcox (Oracle)e6e7ca92020-09-10 08:26:17 -0700553 ClearPageError(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700554
555 do {
556 iomap_adjust_read_range(inode, iop, &block_start,
557 block_end - block_start, &poff, &plen);
558 if (plen == 0)
559 break;
560
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700561 if (!(flags & IOMAP_WRITE_F_UNSHARE) &&
562 (from <= poff || from >= poff + plen) &&
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700563 (to <= poff || to >= poff + plen))
564 continue;
565
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700566 if (iomap_block_needs_zeroing(inode, srcmap, block_start)) {
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700567 if (WARN_ON_ONCE(flags & IOMAP_WRITE_F_UNSHARE))
568 return -EIO;
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700569 zero_user_segments(page, poff, from, to, poff + plen);
Matthew Wilcox (Oracle)14284fe2020-09-10 08:26:18 -0700570 } else {
571 int status = iomap_read_page_sync(block_start, page,
572 poff, plen, srcmap);
573 if (status)
574 return status;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700575 }
Matthew Wilcox (Oracle)14284fe2020-09-10 08:26:18 -0700576 iomap_set_range_uptodate(page, poff, plen);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700577 } while ((block_start += plen) < block_end);
578
Christoph Hellwigd3b40432019-10-18 16:42:24 -0700579 return 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700580}
581
Gao Xiang69f4a262021-08-03 09:38:22 -0700582static int iomap_write_begin_inline(struct inode *inode,
583 struct page *page, struct iomap *srcmap)
584{
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700585 int ret;
586
Gao Xiang69f4a262021-08-03 09:38:22 -0700587 /* needs more work for the tailpacking case; disable for now */
588 if (WARN_ON_ONCE(srcmap->offset != 0))
589 return -EIO;
Matthew Wilcox (Oracle)b4054352021-08-02 14:45:57 -0700590 ret = iomap_read_inline_data(inode, page, srcmap);
591 if (ret < 0)
592 return ret;
593 return 0;
Gao Xiang69f4a262021-08-03 09:38:22 -0700594}
595
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700596static int
597iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700598 struct page **pagep, struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700599{
600 const struct iomap_page_ops *page_ops = iomap->page_ops;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700601 struct page *page;
602 int status = 0;
603
604 BUG_ON(pos + len > iomap->offset + iomap->length);
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700605 if (srcmap != iomap)
606 BUG_ON(pos + len > srcmap->offset + srcmap->length);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700607
608 if (fatal_signal_pending(current))
609 return -EINTR;
610
611 if (page_ops && page_ops->page_prepare) {
Christoph Hellwig1d25d0a2021-08-10 18:33:03 -0700612 status = page_ops->page_prepare(inode, pos, len);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700613 if (status)
614 return status;
615 }
616
Christoph Hellwigdcd61582019-10-18 16:41:12 -0700617 page = grab_cache_page_write_begin(inode->i_mapping, pos >> PAGE_SHIFT,
618 AOP_FLAG_NOFS);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700619 if (!page) {
620 status = -ENOMEM;
621 goto out_no_page;
622 }
623
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700624 if (srcmap->type == IOMAP_INLINE)
Gao Xiang69f4a262021-08-03 09:38:22 -0700625 status = iomap_write_begin_inline(inode, page, srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700626 else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700627 status = __block_write_begin_int(page, pos, len, NULL, srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700628 else
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700629 status = __iomap_write_begin(inode, pos, len, flags, page,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700630 srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700631
632 if (unlikely(status))
633 goto out_unlock;
634
635 *pagep = page;
636 return 0;
637
638out_unlock:
639 unlock_page(page);
640 put_page(page);
641 iomap_write_failed(inode, pos, len);
642
643out_no_page:
644 if (page_ops && page_ops->page_done)
Christoph Hellwig1d25d0a2021-08-10 18:33:03 -0700645 page_ops->page_done(inode, pos, 0, NULL);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700646 return status;
647}
648
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700649static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
650 size_t copied, struct page *page)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700651{
652 flush_dcache_page(page);
653
654 /*
655 * The blocks that were entirely written will now be uptodate, so we
656 * don't have to worry about a readpage reading them and overwriting a
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700657 * partial write. However, if we've encountered a short write and only
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700658 * partially written into a block, it will not be marked uptodate, so a
659 * readpage might come in and destroy our partial write.
660 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700661 * Do the simplest thing and just treat any short write to a
662 * non-uptodate page as a zero-length write, and force the caller to
663 * redo the whole thing.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700664 */
665 if (unlikely(copied < len && !PageUptodate(page)))
666 return 0;
667 iomap_set_range_uptodate(page, offset_in_page(pos), len);
Matthew Wilcox (Oracle)fd7353f2021-06-28 19:36:21 -0700668 __set_page_dirty_nobuffers(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700669 return copied;
670}
671
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700672static size_t iomap_write_end_inline(struct inode *inode, struct page *page,
673 struct iomap *iomap, loff_t pos, size_t copied)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700674{
675 void *addr;
676
677 WARN_ON_ONCE(!PageUptodate(page));
Gao Xiang69f4a262021-08-03 09:38:22 -0700678 BUG_ON(!iomap_inline_data_valid(iomap));
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700679
Matthew Wilcox (Oracle)7ed3cd12020-09-21 08:58:38 -0700680 flush_dcache_page(page);
Matthew Wilcox (Oracle)ab069d52021-08-04 20:07:33 -0700681 addr = kmap_local_page(page) + pos;
682 memcpy(iomap_inline_data(iomap, pos), addr, copied);
683 kunmap_local(addr);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700684
685 mark_inode_dirty(inode);
686 return copied;
687}
688
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700689/* Returns the number of bytes copied. May be 0. Cannot be an errno. */
690static size_t iomap_write_end(struct inode *inode, loff_t pos, size_t len,
691 size_t copied, struct page *page, struct iomap *iomap,
692 struct iomap *srcmap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700693{
694 const struct iomap_page_ops *page_ops = iomap->page_ops;
695 loff_t old_size = inode->i_size;
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700696 size_t ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700697
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700698 if (srcmap->type == IOMAP_INLINE) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700699 ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700700 } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700701 ret = block_write_end(NULL, inode->i_mapping, pos, len, copied,
702 page, NULL);
703 } else {
Christoph Hellwigc12d6fa2019-10-18 16:40:57 -0700704 ret = __iomap_write_end(inode, pos, len, copied, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700705 }
706
707 /*
708 * Update the in-memory inode size after copying the data into the page
709 * cache. It's up to the file system to write the updated size to disk,
710 * preferably after I/O completion so that no stale data is exposed.
711 */
712 if (pos + ret > old_size) {
713 i_size_write(inode, pos + ret);
714 iomap->flags |= IOMAP_F_SIZE_CHANGED;
715 }
716 unlock_page(page);
717
718 if (old_size < pos)
719 pagecache_isize_extended(inode, old_size, pos);
720 if (page_ops && page_ops->page_done)
Christoph Hellwig1d25d0a2021-08-10 18:33:03 -0700721 page_ops->page_done(inode, pos, ret, page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700722 put_page(page);
723
724 if (ret < len)
725 iomap_write_failed(inode, pos, len);
726 return ret;
727}
728
729static loff_t
730iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700731 struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700732{
733 struct iov_iter *i = data;
734 long status = 0;
735 ssize_t written = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700736
737 do {
738 struct page *page;
739 unsigned long offset; /* Offset into pagecache page */
740 unsigned long bytes; /* Bytes to write to page */
741 size_t copied; /* Bytes copied from user */
742
743 offset = offset_in_page(pos);
744 bytes = min_t(unsigned long, PAGE_SIZE - offset,
745 iov_iter_count(i));
746again:
747 if (bytes > length)
748 bytes = length;
749
750 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -0700751 * Bring in the user page that we'll copy from _first_.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700752 * Otherwise there's a nasty deadlock on copying from the
753 * same page as we're writing to, without it being marked
754 * up-to-date.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700755 */
756 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
757 status = -EFAULT;
758 break;
759 }
760
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700761 status = iomap_write_begin(inode, pos, bytes, 0, &page, iomap,
762 srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700763 if (unlikely(status))
764 break;
765
766 if (mapping_writably_mapped(inode->i_mapping))
767 flush_dcache_page(page);
768
Al Virof0b65f32021-04-30 10:26:41 -0400769 copied = copy_page_from_iter_atomic(page, offset, bytes, i);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700770
Al Virobc1bb412021-05-31 00:32:44 -0400771 status = iomap_write_end(inode, pos, bytes, copied, page, iomap,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700772 srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700773
Al Virof0b65f32021-04-30 10:26:41 -0400774 if (unlikely(copied != status))
775 iov_iter_revert(i, copied - status);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700776
Al Virof0b65f32021-04-30 10:26:41 -0400777 cond_resched();
Al Virobc1bb412021-05-31 00:32:44 -0400778 if (unlikely(status == 0)) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700779 /*
Al Virobc1bb412021-05-31 00:32:44 -0400780 * A short copy made iomap_write_end() reject the
781 * thing entirely. Might be memory poisoning
782 * halfway through, might be a race with munmap,
783 * might be severe memory pressure.
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700784 */
Al Virobc1bb412021-05-31 00:32:44 -0400785 if (copied)
786 bytes = copied;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700787 goto again;
788 }
Al Virof0b65f32021-04-30 10:26:41 -0400789 pos += status;
790 written += status;
791 length -= status;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700792
793 balance_dirty_pages_ratelimited(inode->i_mapping);
794 } while (iov_iter_count(i) && length);
795
796 return written ? written : status;
797}
798
799ssize_t
800iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
801 const struct iomap_ops *ops)
802{
803 struct inode *inode = iocb->ki_filp->f_mapping->host;
804 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
805
806 while (iov_iter_count(iter)) {
807 ret = iomap_apply(inode, pos, iov_iter_count(iter),
808 IOMAP_WRITE, ops, iter, iomap_write_actor);
809 if (ret <= 0)
810 break;
811 pos += ret;
812 written += ret;
813 }
814
815 return written ? written : ret;
816}
817EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
818
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700819static loff_t
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700820iomap_unshare_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700821 struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700822{
823 long status = 0;
Matthew Wilcox (Oracle)d4ff3b22020-06-08 20:58:29 -0700824 loff_t written = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700825
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700826 /* don't bother with blocks that are not shared to start with */
827 if (!(iomap->flags & IOMAP_F_SHARED))
828 return length;
829 /* don't bother with holes or unwritten extents */
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700830 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700831 return length;
832
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700833 do {
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700834 unsigned long offset = offset_in_page(pos);
835 unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length);
836 struct page *page;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700837
Christoph Hellwig32a38a42019-10-18 16:42:50 -0700838 status = iomap_write_begin(inode, pos, bytes,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700839 IOMAP_WRITE_F_UNSHARE, &page, iomap, srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700840 if (unlikely(status))
841 return status;
842
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700843 status = iomap_write_end(inode, pos, bytes, bytes, page, iomap,
844 srcmap);
Matthew Wilcox (Oracle)e25ba8c2020-09-21 08:58:41 -0700845 if (WARN_ON_ONCE(status == 0))
846 return -EIO;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700847
848 cond_resched();
849
850 pos += status;
851 written += status;
852 length -= status;
853
854 balance_dirty_pages_ratelimited(inode->i_mapping);
855 } while (length);
856
857 return written;
858}
859
860int
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700861iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700862 const struct iomap_ops *ops)
863{
864 loff_t ret;
865
866 while (len) {
867 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700868 iomap_unshare_actor);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700869 if (ret <= 0)
870 return ret;
871 pos += ret;
872 len -= ret;
873 }
874
875 return 0;
876}
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700877EXPORT_SYMBOL_GPL(iomap_file_unshare);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700878
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700879static s64 iomap_zero(struct inode *inode, loff_t pos, u64 length,
880 struct iomap *iomap, struct iomap *srcmap)
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
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700887 status = iomap_write_begin(inode, pos, bytes, 0, &page, iomap, srcmap);
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
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700894 return iomap_write_end(inode, pos, bytes, bytes, page, iomap, srcmap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700895}
896
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700897static loff_t iomap_zero_range_actor(struct inode *inode, loff_t pos,
898 loff_t length, void *data, struct iomap *iomap,
899 struct iomap *srcmap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700900{
901 bool *did_zero = data;
902 loff_t written = 0;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700903
904 /* already zeroed? we're done. */
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700905 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700906 return length;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700907
908 do {
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700909 s64 bytes;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700910
911 if (IS_DAX(inode))
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700912 bytes = dax_iomap_zero(pos, length, iomap);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700913 else
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700914 bytes = iomap_zero(inode, pos, length, iomap, srcmap);
915 if (bytes < 0)
916 return bytes;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700917
918 pos += bytes;
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700919 length -= bytes;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700920 written += bytes;
921 if (did_zero)
922 *did_zero = true;
Matthew Wilcox (Oracle)81ee8e52020-09-21 08:58:42 -0700923 } while (length > 0);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700924
925 return written;
926}
927
928int
929iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
930 const struct iomap_ops *ops)
931{
932 loff_t ret;
933
934 while (len > 0) {
935 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
936 ops, did_zero, iomap_zero_range_actor);
937 if (ret <= 0)
938 return ret;
939
940 pos += ret;
941 len -= ret;
942 }
943
944 return 0;
945}
946EXPORT_SYMBOL_GPL(iomap_zero_range);
947
948int
949iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
950 const struct iomap_ops *ops)
951{
952 unsigned int blocksize = i_blocksize(inode);
953 unsigned int off = pos & (blocksize - 1);
954
955 /* Block boundary? Nothing to do */
956 if (!off)
957 return 0;
958 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
959}
960EXPORT_SYMBOL_GPL(iomap_truncate_page);
961
962static loff_t
963iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700964 void *data, struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700965{
966 struct page *page = data;
967 int ret;
968
969 if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
970 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
971 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{
984 struct page *page = vmf->page;
985 struct inode *inode = file_inode(vmf->vma->vm_file);
986 unsigned long length;
Andreas Gruenbacher243145b2020-01-06 08:58:23 -0800987 loff_t offset;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700988 ssize_t ret;
989
990 lock_page(page);
Andreas Gruenbacher243145b2020-01-06 08:58:23 -0800991 ret = page_mkwrite_check_truncate(page, inode);
992 if (ret < 0)
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700993 goto out_unlock;
Andreas Gruenbacher243145b2020-01-06 08:58:23 -0800994 length = ret;
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700995
Andreas Gruenbacher243145b2020-01-06 08:58:23 -0800996 offset = page_offset(page);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700997 while (length > 0) {
998 ret = iomap_apply(inode, offset, length,
999 IOMAP_WRITE | IOMAP_FAULT, ops, page,
1000 iomap_page_mkwrite_actor);
1001 if (unlikely(ret <= 0))
1002 goto out_unlock;
1003 offset += ret;
1004 length -= ret;
1005 }
1006
1007 wait_for_stable_page(page);
1008 return VM_FAULT_LOCKED;
1009out_unlock:
1010 unlock_page(page);
1011 return block_page_mkwrite_return(ret);
1012}
1013EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001014
1015static void
Christoph Hellwig48d64cd2019-10-17 13:12:22 -07001016iomap_finish_page_writeback(struct inode *inode, struct page *page,
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001017 int error, unsigned int len)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001018{
Christoph Hellwig48d64cd2019-10-17 13:12:22 -07001019 struct iomap_page *iop = to_iomap_page(page);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001020
1021 if (error) {
Christoph Hellwig48d64cd2019-10-17 13:12:22 -07001022 SetPageError(page);
Darrick J. Wongb69eea82021-08-10 18:32:55 -07001023 mapping_set_error(inode->i_mapping, error);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001024 }
1025
Matthew Wilcox (Oracle)24addd82020-09-21 08:58:39 -07001026 WARN_ON_ONCE(i_blocks_per_page(inode, page) > 1 && !iop);
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001027 WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001028
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001029 if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending))
Christoph Hellwig48d64cd2019-10-17 13:12:22 -07001030 end_page_writeback(page);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001031}
1032
1033/*
1034 * We're now finished for good with this ioend structure. Update the page
1035 * state, release holds on bios, and finally free up memory. Do not use the
1036 * ioend after this.
1037 */
1038static void
1039iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1040{
1041 struct inode *inode = ioend->io_inode;
1042 struct bio *bio = &ioend->io_inline_bio;
1043 struct bio *last = ioend->io_bio, *next;
1044 u64 start = bio->bi_iter.bi_sector;
Zorro Langc2757792019-12-04 22:59:02 -08001045 loff_t offset = ioend->io_offset;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001046 bool quiet = bio_flagged(bio, BIO_QUIET);
1047
1048 for (bio = &ioend->io_inline_bio; bio; bio = next) {
1049 struct bio_vec *bv;
1050 struct bvec_iter_all iter_all;
1051
1052 /*
1053 * For the last bio, bi_private points to the ioend, so we
1054 * need to explicitly end the iteration here.
1055 */
1056 if (bio == last)
1057 next = NULL;
1058 else
1059 next = bio->bi_private;
1060
1061 /* walk each page on bio, ending page IO on them */
1062 bio_for_each_segment_all(bv, bio, iter_all)
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001063 iomap_finish_page_writeback(inode, bv->bv_page, error,
1064 bv->bv_len);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001065 bio_put(bio);
1066 }
Zorro Langc2757792019-12-04 22:59:02 -08001067 /* The ioend has been freed by bio_put() */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001068
1069 if (unlikely(error && !quiet)) {
1070 printk_ratelimited(KERN_ERR
Darrick J. Wong9cd0ed62019-10-17 14:02:07 -07001071"%s: writeback error on inode %lu, offset %lld, sector %llu",
Zorro Langc2757792019-12-04 22:59:02 -08001072 inode->i_sb->s_id, inode->i_ino, offset, start);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001073 }
1074}
1075
1076void
1077iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1078{
1079 struct list_head tmp;
1080
1081 list_replace_init(&ioend->io_list, &tmp);
1082 iomap_finish_ioend(ioend, error);
1083
1084 while (!list_empty(&tmp)) {
1085 ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1086 list_del_init(&ioend->io_list);
1087 iomap_finish_ioend(ioend, error);
1088 }
1089}
1090EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1091
1092/*
1093 * We can merge two adjacent ioends if they have the same set of work to do.
1094 */
1095static bool
1096iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1097{
1098 if (ioend->io_bio->bi_status != next->io_bio->bi_status)
1099 return false;
1100 if ((ioend->io_flags & IOMAP_F_SHARED) ^
1101 (next->io_flags & IOMAP_F_SHARED))
1102 return false;
1103 if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1104 (next->io_type == IOMAP_UNWRITTEN))
1105 return false;
1106 if (ioend->io_offset + ioend->io_size != next->io_offset)
1107 return false;
1108 return true;
1109}
1110
1111void
Brian Foster6e552492021-05-04 08:54:29 -07001112iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001113{
1114 struct iomap_ioend *next;
1115
1116 INIT_LIST_HEAD(&ioend->io_list);
1117
1118 while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1119 io_list))) {
1120 if (!iomap_ioend_can_merge(ioend, next))
1121 break;
1122 list_move_tail(&next->io_list, &ioend->io_list);
1123 ioend->io_size += next->io_size;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001124 }
1125}
1126EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1127
1128static int
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07001129iomap_ioend_compare(void *priv, const struct list_head *a,
1130 const struct list_head *b)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001131{
Christoph Hellwigb3d423e2019-10-17 13:12:20 -07001132 struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1133 struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001134
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001135 if (ia->io_offset < ib->io_offset)
1136 return -1;
Christoph Hellwigb3d423e2019-10-17 13:12:20 -07001137 if (ia->io_offset > ib->io_offset)
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001138 return 1;
1139 return 0;
1140}
1141
1142void
1143iomap_sort_ioends(struct list_head *ioend_list)
1144{
1145 list_sort(NULL, ioend_list, iomap_ioend_compare);
1146}
1147EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1148
1149static void iomap_writepage_end_bio(struct bio *bio)
1150{
1151 struct iomap_ioend *ioend = bio->bi_private;
1152
1153 iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
1154}
1155
1156/*
1157 * Submit the final bio for an ioend.
1158 *
1159 * If @error is non-zero, it means that we have a situation where some part of
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001160 * the submission process has failed after we've marked pages for writeback
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001161 * and unlocked them. In this situation, we need to fail the bio instead of
1162 * submitting it. This typically only happens on a filesystem shutdown.
1163 */
1164static int
1165iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
1166 int error)
1167{
1168 ioend->io_bio->bi_private = ioend;
1169 ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
1170
1171 if (wpc->ops->prepare_ioend)
1172 error = wpc->ops->prepare_ioend(ioend, error);
1173 if (error) {
1174 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001175 * If we're failing the IO now, just mark the ioend with an
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001176 * error and finish it. This will run IO completion immediately
1177 * as there is only one reference to the ioend at this point in
1178 * time.
1179 */
1180 ioend->io_bio->bi_status = errno_to_blk_status(error);
1181 bio_endio(ioend->io_bio);
1182 return error;
1183 }
1184
1185 submit_bio(ioend->io_bio);
1186 return 0;
1187}
1188
1189static struct iomap_ioend *
1190iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
1191 loff_t offset, sector_t sector, struct writeback_control *wbc)
1192{
1193 struct iomap_ioend *ioend;
1194 struct bio *bio;
1195
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001196 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &iomap_ioend_bioset);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001197 bio_set_dev(bio, wpc->iomap.bdev);
1198 bio->bi_iter.bi_sector = sector;
1199 bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
1200 bio->bi_write_hint = inode->i_write_hint;
1201 wbc_init_bio(wbc, bio);
1202
1203 ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
1204 INIT_LIST_HEAD(&ioend->io_list);
1205 ioend->io_type = wpc->iomap.type;
1206 ioend->io_flags = wpc->iomap.flags;
1207 ioend->io_inode = inode;
1208 ioend->io_size = 0;
1209 ioend->io_offset = offset;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001210 ioend->io_bio = bio;
1211 return ioend;
1212}
1213
1214/*
1215 * Allocate a new bio, and chain the old bio to the new one.
1216 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001217 * Note that we have to perform the chaining in this unintuitive order
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001218 * so that the bi_private linkage is set up in the right direction for the
1219 * traversal in iomap_finish_ioend().
1220 */
1221static struct bio *
1222iomap_chain_bio(struct bio *prev)
1223{
1224 struct bio *new;
1225
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001226 new = bio_alloc(GFP_NOFS, BIO_MAX_VECS);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001227 bio_copy_dev(new, prev);/* also copies over blkcg information */
1228 new->bi_iter.bi_sector = bio_end_sector(prev);
1229 new->bi_opf = prev->bi_opf;
1230 new->bi_write_hint = prev->bi_write_hint;
1231
1232 bio_chain(prev, new);
1233 bio_get(prev); /* for iomap_finish_ioend */
1234 submit_bio(prev);
1235 return new;
1236}
1237
1238static bool
1239iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
1240 sector_t sector)
1241{
1242 if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1243 (wpc->ioend->io_flags & IOMAP_F_SHARED))
1244 return false;
1245 if (wpc->iomap.type != wpc->ioend->io_type)
1246 return false;
1247 if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
1248 return false;
1249 if (sector != bio_end_sector(wpc->ioend->io_bio))
1250 return false;
1251 return true;
1252}
1253
1254/*
1255 * Test to see if we have an existing ioend structure that we could append to
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001256 * first; otherwise finish off the current ioend and start another.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001257 */
1258static void
1259iomap_add_to_ioend(struct inode *inode, loff_t offset, struct page *page,
1260 struct iomap_page *iop, struct iomap_writepage_ctx *wpc,
1261 struct writeback_control *wbc, struct list_head *iolist)
1262{
1263 sector_t sector = iomap_sector(&wpc->iomap, offset);
1264 unsigned len = i_blocksize(inode);
1265 unsigned poff = offset & (PAGE_SIZE - 1);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001266
1267 if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, offset, sector)) {
1268 if (wpc->ioend)
1269 list_add(&wpc->ioend->io_list, iolist);
1270 wpc->ioend = iomap_alloc_ioend(inode, wpc, offset, sector, wbc);
1271 }
1272
Christoph Hellwigc1b79f12021-08-02 14:43:43 -07001273 if (bio_add_page(wpc->ioend->io_bio, page, len, poff) != len) {
1274 wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio);
1275 __bio_add_page(wpc->ioend->io_bio, page, len, poff);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001276 }
1277
Christoph Hellwigc1b79f12021-08-02 14:43:43 -07001278 if (iop)
1279 atomic_add(len, &iop->write_bytes_pending);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001280 wpc->ioend->io_size += len;
1281 wbc_account_cgroup_owner(wbc, page, len);
1282}
1283
1284/*
1285 * We implement an immediate ioend submission policy here to avoid needing to
1286 * chain multiple ioends and hence nest mempool allocations which can violate
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001287 * the forward progress guarantees we need to provide. The current ioend we're
1288 * adding blocks to is cached in the writepage context, and if the new block
1289 * doesn't append to the cached ioend, it will create a new ioend and cache that
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001290 * instead.
1291 *
1292 * If a new ioend is created and cached, the old ioend is returned and queued
1293 * locally for submission once the entire page is processed or an error has been
1294 * detected. While ioends are submitted immediately after they are completed,
1295 * batching optimisations are provided by higher level block plugging.
1296 *
1297 * At the end of a writeback pass, there will be a cached ioend remaining on the
1298 * writepage context that the caller will need to submit.
1299 */
1300static int
1301iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1302 struct writeback_control *wbc, struct inode *inode,
1303 struct page *page, u64 end_offset)
1304{
Andreas Gruenbacher8e1bcef2021-07-15 09:58:05 -07001305 struct iomap_page *iop = iomap_page_create(inode, page);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001306 struct iomap_ioend *ioend, *next;
1307 unsigned len = i_blocksize(inode);
1308 u64 file_offset; /* file offset of page */
1309 int error = 0, count = 0, i;
1310 LIST_HEAD(submit_list);
1311
Matthew Wilcox (Oracle)0fb2d722020-09-21 08:58:41 -07001312 WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001313
1314 /*
1315 * Walk through the page to find areas to write back. If we run off the
1316 * end of the current map or find the current map invalid, grab a new
1317 * one.
1318 */
1319 for (i = 0, file_offset = page_offset(page);
1320 i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset;
1321 i++, file_offset += len) {
1322 if (iop && !test_bit(i, iop->uptodate))
1323 continue;
1324
1325 error = wpc->ops->map_blocks(wpc, inode, file_offset);
1326 if (error)
1327 break;
Christoph Hellwig3e19e6f2019-10-17 13:12:17 -07001328 if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
1329 continue;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001330 if (wpc->iomap.type == IOMAP_HOLE)
1331 continue;
1332 iomap_add_to_ioend(inode, file_offset, page, iop, wpc, wbc,
1333 &submit_list);
1334 count++;
1335 }
1336
1337 WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
1338 WARN_ON_ONCE(!PageLocked(page));
1339 WARN_ON_ONCE(PageWriteback(page));
Brian Foster50e7d6c2020-10-29 14:30:49 -07001340 WARN_ON_ONCE(PageDirty(page));
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001341
1342 /*
1343 * We cannot cancel the ioend directly here on error. We may have
1344 * already set other pages under writeback and hence we have to run I/O
1345 * completion to mark the error state of the pages under writeback
1346 * appropriately.
1347 */
1348 if (unlikely(error)) {
Brian Foster763e4cd2020-10-29 14:30:48 -07001349 /*
1350 * Let the filesystem know what portion of the current page
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001351 * failed to map. If the page hasn't been added to ioend, it
Brian Foster763e4cd2020-10-29 14:30:48 -07001352 * won't be affected by I/O completion and we must unlock it
1353 * now.
1354 */
1355 if (wpc->ops->discard_page)
1356 wpc->ops->discard_page(page, file_offset);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001357 if (!count) {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001358 ClearPageUptodate(page);
1359 unlock_page(page);
1360 goto done;
1361 }
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001362 }
1363
Brian Foster50e7d6c2020-10-29 14:30:49 -07001364 set_page_writeback(page);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001365 unlock_page(page);
1366
1367 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001368 * Preserve the original error if there was one; catch
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001369 * submission errors here and propagate into subsequent ioend
1370 * submissions.
1371 */
1372 list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
1373 int error2;
1374
1375 list_del_init(&ioend->io_list);
1376 error2 = iomap_submit_ioend(wpc, ioend, error);
1377 if (error2 && !error)
1378 error = error2;
1379 }
1380
1381 /*
1382 * We can end up here with no error and nothing to write only if we race
1383 * with a partial page truncate on a sub-page block sized filesystem.
1384 */
1385 if (!count)
1386 end_page_writeback(page);
1387done:
1388 mapping_set_error(page->mapping, error);
1389 return error;
1390}
1391
1392/*
1393 * Write out a dirty page.
1394 *
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001395 * For delalloc space on the page, we need to allocate space and flush it.
1396 * For unwritten space on the page, we need to start the conversion to
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001397 * regular allocated space.
1398 */
1399static int
1400iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)
1401{
1402 struct iomap_writepage_ctx *wpc = data;
1403 struct inode *inode = page->mapping->host;
1404 pgoff_t end_index;
1405 u64 end_offset;
1406 loff_t offset;
1407
Matthew Wilcox (Oracle)1ac99452020-03-05 07:21:43 -08001408 trace_iomap_writepage(inode, page_offset(page), PAGE_SIZE);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001409
1410 /*
Andreas Gruenbacherf1f264b2021-08-02 14:46:31 -07001411 * Refuse to write the page out if we're called from reclaim context.
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001412 *
1413 * This avoids stack overflows when called from deeply used stacks in
1414 * random callers for direct reclaim or memcg reclaim. We explicitly
1415 * allow reclaim from kswapd as the stack usage there is relatively low.
1416 *
1417 * This should never happen except in the case of a VM regression so
1418 * warn about it.
1419 */
1420 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1421 PF_MEMALLOC))
1422 goto redirty;
1423
1424 /*
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07001425 * Is this page beyond the end of the file?
1426 *
1427 * The page index is less than the end_index, adjust the end_offset
1428 * to the highest offset that this page should represent.
1429 * -----------------------------------------------------
1430 * | file mapping | <EOF> |
1431 * -----------------------------------------------------
1432 * | Page ... | Page N-2 | Page N-1 | Page N | |
1433 * ^--------------------------------^----------|--------
1434 * | desired writeback range | see else |
1435 * ---------------------------------^------------------|
1436 */
1437 offset = i_size_read(inode);
1438 end_index = offset >> PAGE_SHIFT;
1439 if (page->index < end_index)
1440 end_offset = (loff_t)(page->index + 1) << PAGE_SHIFT;
1441 else {
1442 /*
1443 * Check whether the page to write out is beyond or straddles
1444 * i_size or not.
1445 * -------------------------------------------------------
1446 * | file mapping | <EOF> |
1447 * -------------------------------------------------------
1448 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
1449 * ^--------------------------------^-----------|---------
1450 * | | Straddles |
1451 * ---------------------------------^-----------|--------|
1452 */
1453 unsigned offset_into_page = offset & (PAGE_SIZE - 1);
1454
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 */
1472 if (page->index > end_index ||
1473 (page->index == end_index && offset_into_page == 0))
1474 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 */
1484 zero_user_segment(page, offset_into_page, PAGE_SIZE);
1485
1486 /* Adjust the end_offset to the end of file */
1487 end_offset = offset;
1488 }
1489
1490 return iomap_writepage_map(wpc, wbc, inode, page, end_offset);
1491
1492redirty:
1493 redirty_page_for_writepage(wbc, page);
1494 unlock_page(page);
1495 return 0;
1496}
1497
1498int
1499iomap_writepage(struct page *page, struct writeback_control *wbc,
1500 struct iomap_writepage_ctx *wpc,
1501 const struct iomap_writeback_ops *ops)
1502{
1503 int ret;
1504
1505 wpc->ops = ops;
1506 ret = iomap_do_writepage(page, wbc, wpc);
1507 if (!wpc->ioend)
1508 return ret;
1509 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1510}
1511EXPORT_SYMBOL_GPL(iomap_writepage);
1512
1513int
1514iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1515 struct iomap_writepage_ctx *wpc,
1516 const struct iomap_writeback_ops *ops)
1517{
1518 int ret;
1519
1520 wpc->ops = ops;
1521 ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
1522 if (!wpc->ioend)
1523 return ret;
1524 return iomap_submit_ioend(wpc, wpc->ioend, ret);
1525}
1526EXPORT_SYMBOL_GPL(iomap_writepages);
1527
1528static int __init iomap_init(void)
1529{
1530 return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
1531 offsetof(struct iomap_ioend, io_inline_bio),
1532 BIOSET_NEED_BVECS);
1533}
1534fs_initcall(iomap_init);