1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (C) 2016-2023 Christoph Hellwig.
5 */
6 #include <linux/iomap.h>
7 #include <linux/buffer_head.h>
8 #include <linux/writeback.h>
9 #include <linux/swap.h>
10 #include <linux/migrate.h>
11 #include <linux/fserror.h>
12 #include <linux/fsverity.h>
13 #include "internal.h"
14 #include "trace.h"
15
16 #include "../internal.h"
17
18 /*
19 * Structure allocated for each folio to track per-block uptodate, dirty state
20 * and I/O completions.
21 */
22 struct iomap_folio_state {
23 spinlock_t state_lock;
24 unsigned int read_bytes_pending;
25 atomic_t write_bytes_pending;
26
27 /*
28 * Each block has two bits in this bitmap:
29 * Bits [0..blocks_per_folio) has the uptodate status.
30 * Bits [b_p_f...(2*b_p_f)) has the dirty status.
31 */
32 unsigned long state[];
33 };
34
ifs_is_fully_uptodate(struct folio * folio,struct iomap_folio_state * ifs)35 static inline bool ifs_is_fully_uptodate(struct folio *folio,
36 struct iomap_folio_state *ifs)
37 {
38 struct inode *inode = folio->mapping->host;
39
40 return bitmap_full(ifs->state, i_blocks_per_folio(inode, folio));
41 }
42
43 /*
44 * Find the next uptodate block in the folio. end_blk is inclusive.
45 * If no uptodate block is found, this will return end_blk + 1.
46 */
ifs_next_uptodate_block(struct folio * folio,unsigned start_blk,unsigned end_blk)47 static unsigned ifs_next_uptodate_block(struct folio *folio,
48 unsigned start_blk, unsigned end_blk)
49 {
50 struct iomap_folio_state *ifs = folio->private;
51
52 return find_next_bit(ifs->state, end_blk + 1, start_blk);
53 }
54
55 /*
56 * Find the next non-uptodate block in the folio. end_blk is inclusive.
57 * If no non-uptodate block is found, this will return end_blk + 1.
58 */
ifs_next_nonuptodate_block(struct folio * folio,unsigned start_blk,unsigned end_blk)59 static unsigned ifs_next_nonuptodate_block(struct folio *folio,
60 unsigned start_blk, unsigned end_blk)
61 {
62 struct iomap_folio_state *ifs = folio->private;
63
64 return find_next_zero_bit(ifs->state, end_blk + 1, start_blk);
65 }
66
ifs_set_range_uptodate(struct folio * folio,struct iomap_folio_state * ifs,size_t off,size_t len)67 static bool ifs_set_range_uptodate(struct folio *folio,
68 struct iomap_folio_state *ifs, size_t off, size_t len)
69 {
70 struct inode *inode = folio->mapping->host;
71 unsigned int first_blk, last_blk;
72
73 if (len) {
74 first_blk = off >> inode->i_blkbits;
75 last_blk = (off + len - 1) >> inode->i_blkbits;
76 bitmap_set(ifs->state, first_blk, last_blk - first_blk + 1);
77 }
78 return ifs_is_fully_uptodate(folio, ifs);
79 }
80
iomap_set_range_uptodate(struct folio * folio,size_t off,size_t len)81 static void iomap_set_range_uptodate(struct folio *folio, size_t off,
82 size_t len)
83 {
84 struct iomap_folio_state *ifs = folio->private;
85 unsigned long flags;
86 bool mark_uptodate = true;
87
88 if (folio_test_uptodate(folio))
89 return;
90
91 if (ifs) {
92 spin_lock_irqsave(&ifs->state_lock, flags);
93 /*
94 * If a read with bytes pending is in progress, we must not call
95 * folio_mark_uptodate(). The read completion path
96 * (iomap_read_end()) will call folio_end_read(), which uses XOR
97 * semantics to set the uptodate bit. If we set it here, the XOR
98 * in folio_end_read() will clear it, leaving the folio not
99 * uptodate.
100 */
101 mark_uptodate = ifs_set_range_uptodate(folio, ifs, off, len) &&
102 !ifs->read_bytes_pending;
103 spin_unlock_irqrestore(&ifs->state_lock, flags);
104 }
105
106 if (mark_uptodate)
107 folio_mark_uptodate(folio);
108 }
109
iomap_folio_mark_uptodate(struct folio * folio)110 void iomap_folio_mark_uptodate(struct folio *folio)
111 {
112 iomap_set_range_uptodate(folio, 0, folio_size(folio));
113 }
114 EXPORT_SYMBOL_GPL(iomap_folio_mark_uptodate);
115
116 /*
117 * Find the next dirty block in the folio. end_blk is inclusive.
118 * If no dirty block is found, this will return end_blk + 1.
119 */
ifs_next_dirty_block(struct folio * folio,unsigned start_blk,unsigned end_blk)120 static unsigned ifs_next_dirty_block(struct folio *folio,
121 unsigned start_blk, unsigned end_blk)
122 {
123 struct iomap_folio_state *ifs = folio->private;
124 struct inode *inode = folio->mapping->host;
125 unsigned int blks = i_blocks_per_folio(inode, folio);
126
127 return find_next_bit(ifs->state, blks + end_blk + 1,
128 blks + start_blk) - blks;
129 }
130
131 /*
132 * Find the next clean block in the folio. end_blk is inclusive.
133 * If no clean block is found, this will return end_blk + 1.
134 */
ifs_next_clean_block(struct folio * folio,unsigned start_blk,unsigned end_blk)135 static unsigned ifs_next_clean_block(struct folio *folio,
136 unsigned start_blk, unsigned end_blk)
137 {
138 struct iomap_folio_state *ifs = folio->private;
139 struct inode *inode = folio->mapping->host;
140 unsigned int blks = i_blocks_per_folio(inode, folio);
141
142 return find_next_zero_bit(ifs->state, blks + end_blk + 1,
143 blks + start_blk) - blks;
144 }
145
ifs_find_dirty_range(struct folio * folio,struct iomap_folio_state * ifs,u64 * range_start,u64 range_end)146 static unsigned ifs_find_dirty_range(struct folio *folio,
147 struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
148 {
149 struct inode *inode = folio->mapping->host;
150 unsigned start_blk =
151 offset_in_folio(folio, *range_start) >> inode->i_blkbits;
152 unsigned end_blk = min_not_zero(
153 offset_in_folio(folio, range_end) >> inode->i_blkbits,
154 i_blocks_per_folio(inode, folio)) - 1;
155 unsigned nblks;
156
157 start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
158 if (start_blk > end_blk)
159 return 0;
160 if (start_blk == end_blk)
161 nblks = 1;
162 else
163 nblks = ifs_next_clean_block(folio, start_blk + 1, end_blk) -
164 start_blk;
165
166 *range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
167 return nblks << inode->i_blkbits;
168 }
169
iomap_find_dirty_range(struct folio * folio,u64 * range_start,u64 range_end)170 static unsigned iomap_find_dirty_range(struct folio *folio, u64 *range_start,
171 u64 range_end)
172 {
173 struct iomap_folio_state *ifs = folio->private;
174
175 if (*range_start >= range_end)
176 return 0;
177
178 if (ifs)
179 return ifs_find_dirty_range(folio, ifs, range_start, range_end);
180 return range_end - *range_start;
181 }
182
183 /*
184 * Clear the per-block dirty bits for the range [@off, @off + @len) within a
185 * folio. The range is rounded inwards so that only blocks fully covered by
186 * the range are cleared. This is required for operations like folio
187 * invalidation, where we must ensure a block is fully clean before discarding
188 * it.
189 */
ifs_clear_range_dirty(struct folio * folio,struct iomap_folio_state * ifs,size_t off,size_t len)190 static void ifs_clear_range_dirty(struct folio *folio,
191 struct iomap_folio_state *ifs, size_t off, size_t len)
192 {
193 struct inode *inode = folio->mapping->host;
194 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
195 unsigned int first_blk = round_up(off, i_blocksize(inode)) >>
196 inode->i_blkbits;
197 unsigned int last_blk = (off + len) >> inode->i_blkbits;
198 unsigned long flags;
199
200 if (first_blk >= last_blk)
201 return;
202
203 spin_lock_irqsave(&ifs->state_lock, flags);
204 bitmap_clear(ifs->state, first_blk + blks_per_folio,
205 last_blk - first_blk);
206 spin_unlock_irqrestore(&ifs->state_lock, flags);
207 }
208
iomap_clear_range_dirty(struct folio * folio,size_t off,size_t len)209 static void iomap_clear_range_dirty(struct folio *folio, size_t off, size_t len)
210 {
211 struct iomap_folio_state *ifs = folio->private;
212
213 if (ifs)
214 ifs_clear_range_dirty(folio, ifs, off, len);
215 }
216
217 /*
218 * Set the per-block dirty bits for the range [@off, @off + @len) within a
219 * folio. The range is rounded outwards so that any block partially touched
220 * by the range is marked dirty. This ensures blocks containing even a
221 * single dirty byte will be included in subsequent writeback, preventing
222 * data loss when partial blocks are written.
223 */
ifs_set_range_dirty(struct folio * folio,struct iomap_folio_state * ifs,size_t off,size_t len)224 static void ifs_set_range_dirty(struct folio *folio,
225 struct iomap_folio_state *ifs, size_t off, size_t len)
226 {
227 struct inode *inode = folio->mapping->host;
228 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
229 unsigned int first_blk, last_blk;
230 unsigned long flags;
231
232 if (!len)
233 return;
234
235 first_blk = off >> inode->i_blkbits;
236 last_blk = (off + len - 1) >> inode->i_blkbits;
237 spin_lock_irqsave(&ifs->state_lock, flags);
238 bitmap_set(ifs->state, first_blk + blks_per_folio,
239 last_blk - first_blk + 1);
240 spin_unlock_irqrestore(&ifs->state_lock, flags);
241 }
242
iomap_set_range_dirty(struct folio * folio,size_t off,size_t len)243 static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
244 {
245 struct iomap_folio_state *ifs = folio->private;
246
247 if (ifs)
248 ifs_set_range_dirty(folio, ifs, off, len);
249 }
250
ifs_alloc(struct inode * inode,struct folio * folio,unsigned int flags)251 static struct iomap_folio_state *ifs_alloc(struct inode *inode,
252 struct folio *folio, unsigned int flags)
253 {
254 struct iomap_folio_state *ifs = folio->private;
255 unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
256 gfp_t gfp;
257
258 if (ifs || nr_blocks <= 1)
259 return ifs;
260
261 if (flags & IOMAP_NOWAIT)
262 gfp = GFP_NOWAIT;
263 else
264 gfp = GFP_NOFS | __GFP_NOFAIL;
265
266 /*
267 * ifs->state tracks two sets of state flags when the
268 * filesystem block size is smaller than the folio size.
269 * The first state tracks per-block uptodate and the
270 * second tracks per-block dirty state.
271 */
272 ifs = kzalloc_flex(*ifs, state, BITS_TO_LONGS(2 * nr_blocks), gfp);
273 if (!ifs)
274 return ifs;
275
276 spin_lock_init(&ifs->state_lock);
277 if (folio_test_uptodate(folio))
278 bitmap_set(ifs->state, 0, nr_blocks);
279 if (folio_test_dirty(folio))
280 bitmap_set(ifs->state, nr_blocks, nr_blocks);
281 folio_attach_private(folio, ifs);
282
283 return ifs;
284 }
285
ifs_free(struct folio * folio)286 static void ifs_free(struct folio *folio)
287 {
288 struct iomap_folio_state *ifs = folio_detach_private(folio);
289
290 if (!ifs)
291 return;
292 WARN_ON_ONCE(ifs->read_bytes_pending != 0);
293 WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending));
294 WARN_ON_ONCE(ifs_is_fully_uptodate(folio, ifs) !=
295 folio_test_uptodate(folio));
296 kfree(ifs);
297 }
298
299 /*
300 * Calculate how many bytes to truncate based off the number of blocks to
301 * truncate and the end position to start truncating from.
302 */
iomap_bytes_to_truncate(loff_t end_pos,unsigned block_bits,unsigned blocks_truncated)303 static size_t iomap_bytes_to_truncate(loff_t end_pos, unsigned block_bits,
304 unsigned blocks_truncated)
305 {
306 unsigned block_size = 1 << block_bits;
307 unsigned block_offset = end_pos & (block_size - 1);
308
309 if (!block_offset)
310 return blocks_truncated << block_bits;
311
312 return ((blocks_truncated - 1) << block_bits) + block_offset;
313 }
314
315 /*
316 * Calculate the range inside the folio that we actually need to read.
317 */
iomap_adjust_read_range(struct inode * inode,struct folio * folio,loff_t * pos,loff_t length,size_t * offp,size_t * lenp)318 static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
319 loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
320 {
321 struct iomap_folio_state *ifs = folio->private;
322 loff_t orig_pos = *pos;
323 loff_t isize = i_size_read(inode);
324 unsigned block_bits = inode->i_blkbits;
325 unsigned block_size = (1 << block_bits);
326 size_t poff = offset_in_folio(folio, *pos);
327 size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
328 size_t orig_plen = plen;
329 unsigned first = poff >> block_bits;
330 unsigned last = (poff + plen - 1) >> block_bits;
331
332 /*
333 * If the block size is smaller than the page size, we need to check the
334 * per-block uptodate status and adjust the offset and length if needed
335 * to avoid reading in already uptodate ranges.
336 */
337 if (ifs) {
338 unsigned int next, blocks_skipped;
339
340 next = ifs_next_nonuptodate_block(folio, first, last);
341 blocks_skipped = next - first;
342
343 if (blocks_skipped) {
344 unsigned long block_offset = *pos & (block_size - 1);
345 unsigned bytes_skipped =
346 (blocks_skipped << block_bits) - block_offset;
347
348 *pos += bytes_skipped;
349 poff += bytes_skipped;
350 plen -= bytes_skipped;
351 }
352 first = next;
353
354 /* truncate len if we find any trailing uptodate block(s) */
355 if (++next <= last) {
356 next = ifs_next_uptodate_block(folio, next, last);
357 if (next <= last) {
358 plen -= iomap_bytes_to_truncate(*pos + plen,
359 block_bits, last - next + 1);
360 last = next - 1;
361 }
362 }
363 }
364
365 /*
366 * If the extent spans the block that contains the i_size, we need to
367 * handle both halves separately so that we properly zero data in the
368 * page cache for blocks that are entirely outside of i_size.
369 */
370 if (orig_pos <= isize && orig_pos + orig_plen > isize) {
371 unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
372
373 if (first <= end && last > end)
374 plen -= iomap_bytes_to_truncate(*pos + plen, block_bits,
375 last - end);
376 }
377
378 *offp = poff;
379 *lenp = plen;
380 }
381
iomap_block_needs_zeroing(const struct iomap_iter * iter,loff_t pos)382 static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
383 loff_t pos)
384 {
385 const struct iomap *srcmap = iomap_iter_srcmap(iter);
386
387 /*
388 * If this block has not been written, there's nothing to read
389 */
390 if (srcmap->type != IOMAP_MAPPED)
391 return true;
392
393 /*
394 * Newly allocated blocks have not been written
395 */
396 if (srcmap->flags & IOMAP_F_NEW)
397 return true;
398
399 /*
400 * fsverity metadata is stored past i_size, we need to read it instead
401 * of zeroing
402 */
403 if (srcmap->flags & IOMAP_F_FSVERITY)
404 return false;
405
406 return pos >= i_size_read(iter->inode);
407 }
408
409 /**
410 * iomap_read_inline_data - copy inline data into the page cache
411 * @iter: iteration structure
412 * @folio: folio to copy to
413 *
414 * Copy the inline data in @iter into @folio and zero out the rest of the folio.
415 * Only a single IOMAP_INLINE extent is allowed at the end of each file.
416 * Returns zero for success to complete the read, or the usual negative errno.
417 */
iomap_read_inline_data(const struct iomap_iter * iter,struct folio * folio)418 static int iomap_read_inline_data(const struct iomap_iter *iter,
419 struct folio *folio)
420 {
421 const struct iomap *iomap = iomap_iter_srcmap(iter);
422 size_t size = i_size_read(iter->inode) - iomap->offset;
423 size_t offset = offset_in_folio(folio, iomap->offset);
424
425 if (WARN_ON_ONCE(!iomap->inline_data))
426 return -EIO;
427
428 if (folio_test_uptodate(folio))
429 return 0;
430
431 if (WARN_ON_ONCE(size > iomap->length)) {
432 fserror_report_io(iter->inode, FSERR_BUFFERED_READ,
433 iomap->offset, size, -EIO, GFP_NOFS);
434 return -EIO;
435 }
436 if (offset > 0)
437 ifs_alloc(iter->inode, folio, iter->flags);
438
439 folio_fill_tail(folio, offset, iomap->inline_data, size);
440 iomap_set_range_uptodate(folio, offset, folio_size(folio) - offset);
441 return 0;
442 }
443
iomap_finish_folio_read(struct folio * folio,size_t off,size_t len,int error)444 void iomap_finish_folio_read(struct folio *folio, size_t off, size_t len,
445 int error)
446 {
447 struct iomap_folio_state *ifs = folio->private;
448 bool uptodate = !error;
449 bool finished = true;
450
451 if (error)
452 fserror_report_io(folio->mapping->host, FSERR_BUFFERED_READ,
453 folio_pos(folio) + off, len, error,
454 GFP_ATOMIC);
455
456 if (ifs) {
457 unsigned long flags;
458
459 spin_lock_irqsave(&ifs->state_lock, flags);
460 if (!error)
461 uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
462 ifs->read_bytes_pending -= len;
463 finished = !ifs->read_bytes_pending;
464 spin_unlock_irqrestore(&ifs->state_lock, flags);
465 }
466
467 if (finished)
468 folio_end_read(folio, uptodate);
469 }
470 EXPORT_SYMBOL_GPL(iomap_finish_folio_read);
471
iomap_read_init(struct folio * folio)472 static void iomap_read_init(struct folio *folio)
473 {
474 struct iomap_folio_state *ifs = folio->private;
475
476 if (ifs) {
477 /*
478 * ifs->read_bytes_pending is used to track how many bytes are
479 * read in asynchronously by the IO helper. We need to track
480 * this so that we can know when the IO helper has finished
481 * reading in all the necessary ranges of the folio and can end
482 * the read.
483 *
484 * Increase ->read_bytes_pending by the folio size to start.
485 * We'll subtract any uptodate / zeroed ranges that did not
486 * require IO in iomap_read_end() after we're done processing
487 * the folio.
488 *
489 * We do this because otherwise, we would have to increment
490 * ifs->read_bytes_pending every time a range in the folio needs
491 * to be read in, which can get expensive since the spinlock
492 * needs to be held whenever modifying ifs->read_bytes_pending.
493 */
494 spin_lock_irq(&ifs->state_lock);
495 WARN_ON_ONCE(ifs->read_bytes_pending != 0);
496 ifs->read_bytes_pending = folio_size(folio);
497 spin_unlock_irq(&ifs->state_lock);
498 }
499 }
500
501 /*
502 * This ends IO if no bytes were submitted to an IO helper.
503 *
504 * Otherwise, this calibrates ifs->read_bytes_pending to represent only the
505 * submitted bytes (see comment in iomap_read_init()). If all bytes submitted
506 * have already been completed by the IO helper, then this will end the read.
507 * Else the IO helper will end the read after all submitted ranges have been
508 * read.
509 */
iomap_read_end(struct folio * folio,size_t bytes_submitted)510 static void iomap_read_end(struct folio *folio, size_t bytes_submitted)
511 {
512 struct iomap_folio_state *ifs = folio->private;
513
514 if (ifs) {
515 bool end_read, uptodate;
516
517 spin_lock_irq(&ifs->state_lock);
518 if (!ifs->read_bytes_pending) {
519 WARN_ON_ONCE(bytes_submitted);
520 spin_unlock_irq(&ifs->state_lock);
521 folio_unlock(folio);
522 return;
523 }
524
525 /*
526 * Subtract any bytes that were initially accounted to
527 * read_bytes_pending but skipped for IO.
528 */
529 ifs->read_bytes_pending -= folio_size(folio) - bytes_submitted;
530
531 /*
532 * If !ifs->read_bytes_pending, this means all pending reads by
533 * the IO helper have already completed, which means we need to
534 * end the folio read here. If ifs->read_bytes_pending != 0,
535 * the IO helper will end the folio read.
536 */
537 end_read = !ifs->read_bytes_pending;
538 if (end_read)
539 uptodate = ifs_is_fully_uptodate(folio, ifs);
540 spin_unlock_irq(&ifs->state_lock);
541 if (end_read)
542 folio_end_read(folio, uptodate);
543 } else {
544 /*
545 * If a folio without an ifs is submitted to the IO helper, the
546 * read must be on the entire folio and the IO helper takes
547 * ownership of the folio. This means we should only enter
548 * iomap_read_end() for the !ifs case if no bytes were submitted
549 * to the IO helper, in which case we are responsible for
550 * unlocking the folio here.
551 */
552 WARN_ON_ONCE(bytes_submitted);
553 folio_unlock(folio);
554 }
555 }
556
iomap_read_folio_iter(struct iomap_iter * iter,struct iomap_read_folio_ctx * ctx,size_t * bytes_submitted)557 static int iomap_read_folio_iter(struct iomap_iter *iter,
558 struct iomap_read_folio_ctx *ctx, size_t *bytes_submitted)
559 {
560 const struct iomap *iomap = &iter->iomap;
561 loff_t pos = iter->pos;
562 loff_t length = iomap_length(iter);
563 struct folio *folio = ctx->cur_folio;
564 size_t folio_len = folio_size(folio);
565 struct iomap_folio_state *ifs;
566 size_t poff, plen;
567 loff_t pos_diff;
568 int ret;
569
570 if (iomap->type == IOMAP_INLINE) {
571 ret = iomap_read_inline_data(iter, folio);
572 if (ret)
573 return ret;
574 return iomap_iter_advance(iter, length);
575 }
576
577 ifs = ifs_alloc(iter->inode, folio, iter->flags);
578
579 length = min_t(loff_t, length, folio_len - offset_in_folio(folio, pos));
580 while (length) {
581 iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff,
582 &plen);
583
584 pos_diff = pos - iter->pos;
585 if (WARN_ON_ONCE(pos_diff + plen > length))
586 return -EIO;
587
588 ret = iomap_iter_advance(iter, pos_diff);
589 if (ret)
590 return ret;
591
592 if (plen == 0)
593 return 0;
594
595 /*
596 * Handling of fsverity "holes". We hit this for two case:
597 * 1. No need to go further, the hole after fsverity
598 * descriptor is the end of the fsverity metadata.
599 *
600 * 2. This folio contains merkle tree blocks which need to be
601 * synthesized. If we already have fsverity info (ctx->vi)
602 * synthesize these blocks.
603 */
604 if ((iomap->flags & IOMAP_F_FSVERITY) &&
605 iomap->type == IOMAP_HOLE) {
606 if (ctx->vi)
607 fsverity_fill_zerohash(folio, poff, plen,
608 ctx->vi);
609 iomap_set_range_uptodate(folio, poff, plen);
610 } else if (iomap_block_needs_zeroing(iter, pos)) {
611 /* zero post-eof blocks as the page may be mapped */
612 folio_zero_range(folio, poff, plen);
613 if (ctx->vi &&
614 !fsverity_verify_blocks(ctx->vi, folio, plen, poff))
615 return -EIO;
616 iomap_set_range_uptodate(folio, poff, plen);
617 } else {
618 if (!*bytes_submitted)
619 iomap_read_init(folio);
620 ret = ctx->ops->read_folio_range(iter, ctx, plen);
621 if (ret < 0)
622 fserror_report_io(iter->inode,
623 FSERR_BUFFERED_READ, pos,
624 plen, ret, GFP_NOFS);
625 if (ret)
626 return ret;
627
628 *bytes_submitted += plen;
629 /*
630 * Hand off folio ownership to the IO helper when:
631 * 1) The entire folio has been submitted for IO, or
632 * 2) There is no ifs attached to the folio
633 *
634 * Case (2) occurs when 1 << i_blkbits matches the folio
635 * size but the underlying filesystem or block device
636 * uses a smaller granularity for IO.
637 */
638 if (*bytes_submitted == folio_len || !ifs)
639 ctx->cur_folio = NULL;
640 }
641
642 ret = iomap_iter_advance(iter, plen);
643 if (ret)
644 return ret;
645 length -= pos_diff + plen;
646 pos = iter->pos;
647 }
648 return 0;
649 }
650
iomap_read_folio(const struct iomap_ops * ops,struct iomap_read_folio_ctx * ctx,void * private)651 void iomap_read_folio(const struct iomap_ops *ops,
652 struct iomap_read_folio_ctx *ctx, void *private)
653 {
654 struct folio *folio = ctx->cur_folio;
655 struct iomap_iter iter = {
656 .inode = folio->mapping->host,
657 .pos = folio_pos(folio),
658 .len = folio_size(folio),
659 .private = private,
660 };
661 size_t bytes_submitted = 0;
662 int ret;
663
664 trace_iomap_readpage(iter.inode, 1);
665
666 /*
667 * Fetch fsverity_info for both data and fsverity metadata, as iomap
668 * needs zeroed hash for merkle tree block synthesis
669 */
670 ctx->vi = fsverity_get_info(iter.inode);
671 if (ctx->vi && iter.pos < i_size_read(iter.inode))
672 fsverity_readahead(ctx->vi, folio->index,
673 folio_nr_pages(folio));
674
675 while ((ret = iomap_iter(&iter, ops)) > 0) {
676 iter.status = iomap_read_folio_iter(&iter, ctx,
677 &bytes_submitted);
678 if (ctx->read_ctx && ctx->ops->submit_read)
679 ctx->ops->submit_read(&iter, ctx);
680 }
681
682 if (ctx->cur_folio)
683 iomap_read_end(ctx->cur_folio, bytes_submitted);
684 }
685 EXPORT_SYMBOL_GPL(iomap_read_folio);
686
iomap_readahead_iter(struct iomap_iter * iter,struct iomap_read_folio_ctx * ctx,size_t * cur_bytes_submitted)687 static int iomap_readahead_iter(struct iomap_iter *iter,
688 struct iomap_read_folio_ctx *ctx, size_t *cur_bytes_submitted)
689 {
690 int ret;
691
692 while (iomap_length(iter)) {
693 if (ctx->cur_folio &&
694 offset_in_folio(ctx->cur_folio, iter->pos) == 0) {
695 iomap_read_end(ctx->cur_folio, *cur_bytes_submitted);
696 ctx->cur_folio = NULL;
697 }
698 if (!ctx->cur_folio) {
699 ctx->cur_folio = readahead_folio(ctx->rac);
700 if (WARN_ON_ONCE(!ctx->cur_folio))
701 return -EINVAL;
702 *cur_bytes_submitted = 0;
703 }
704 ret = iomap_read_folio_iter(iter, ctx, cur_bytes_submitted);
705 if (ret)
706 return ret;
707 }
708
709 return 0;
710 }
711
712 /**
713 * iomap_readahead - Attempt to read pages from a file.
714 * @ops: The operations vector for the filesystem.
715 * @ctx: The ctx used for issuing readahead.
716 * @private: The filesystem-specific information for issuing iomap_iter.
717 *
718 * This function is for filesystems to call to implement their readahead
719 * address_space operation.
720 *
721 * Context: The @ops callbacks may submit I/O (eg to read the addresses of
722 * blocks from disc), and may wait for it. The caller may be trying to
723 * access a different page, and so sleeping excessively should be avoided.
724 * It may allocate memory, but should avoid costly allocations. This
725 * function is called with memalloc_nofs set, so allocations will not cause
726 * the filesystem to be reentered.
727 */
iomap_readahead(const struct iomap_ops * ops,struct iomap_read_folio_ctx * ctx,void * private)728 void iomap_readahead(const struct iomap_ops *ops,
729 struct iomap_read_folio_ctx *ctx, void *private)
730 {
731 struct readahead_control *rac = ctx->rac;
732 struct iomap_iter iter = {
733 .inode = rac->mapping->host,
734 .pos = readahead_pos(rac),
735 .len = readahead_length(rac),
736 .private = private,
737 };
738 size_t cur_bytes_submitted;
739
740 trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
741
742 /*
743 * Fetch fsverity_info for both data and fsverity metadata, as iomap
744 * needs zeroed hash for merkle tree block synthesis
745 */
746 ctx->vi = fsverity_get_info(iter.inode);
747 if (ctx->vi && iter.pos < i_size_read(iter.inode))
748 fsverity_readahead(ctx->vi, readahead_index(rac),
749 readahead_count(rac));
750
751 while (iomap_iter(&iter, ops) > 0) {
752 iter.status = iomap_readahead_iter(&iter, ctx,
753 &cur_bytes_submitted);
754 if (ctx->read_ctx && ctx->ops->submit_read)
755 ctx->ops->submit_read(&iter, ctx);
756 }
757
758 if (ctx->cur_folio)
759 iomap_read_end(ctx->cur_folio, cur_bytes_submitted);
760 }
761 EXPORT_SYMBOL_GPL(iomap_readahead);
762
763 /*
764 * iomap_is_partially_uptodate checks whether blocks within a folio are
765 * uptodate or not.
766 *
767 * Returns true if all blocks which correspond to the specified part
768 * of the folio are uptodate.
769 */
iomap_is_partially_uptodate(struct folio * folio,size_t from,size_t count)770 bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
771 {
772 struct iomap_folio_state *ifs = folio->private;
773 struct inode *inode = folio->mapping->host;
774 unsigned first, last;
775
776 if (!ifs)
777 return false;
778
779 /* Caller's range may extend past the end of this folio */
780 count = min(folio_size(folio) - from, count);
781
782 /* First and last blocks in range within folio */
783 first = from >> inode->i_blkbits;
784 last = (from + count - 1) >> inode->i_blkbits;
785
786 return ifs_next_nonuptodate_block(folio, first, last) > last;
787 }
788 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
789
790 /**
791 * iomap_get_folio - get a folio reference for writing
792 * @iter: iteration structure
793 * @pos: start offset of write
794 * @len: Suggested size of folio to create.
795 *
796 * Returns a locked reference to the folio at @pos, or an error pointer if the
797 * folio could not be obtained.
798 */
iomap_get_folio(struct iomap_iter * iter,loff_t pos,size_t len)799 struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len)
800 {
801 fgf_t fgp = FGP_WRITEBEGIN;
802
803 if (iter->flags & IOMAP_NOWAIT)
804 fgp |= FGP_NOWAIT;
805 if (iter->flags & IOMAP_DONTCACHE)
806 fgp |= FGP_DONTCACHE;
807 fgp |= fgf_set_order(len);
808
809 return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
810 fgp, mapping_gfp_mask(iter->inode->i_mapping));
811 }
812 EXPORT_SYMBOL_GPL(iomap_get_folio);
813
iomap_release_folio(struct folio * folio,gfp_t gfp_flags)814 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags)
815 {
816 trace_iomap_release_folio(folio->mapping->host, folio_pos(folio),
817 folio_size(folio));
818
819 /*
820 * If the folio is dirty, we refuse to release our metadata because
821 * it may be partially dirty. Once we track per-block dirty state,
822 * we can release the metadata if every block is dirty.
823 */
824 if (folio_test_dirty(folio))
825 return false;
826 ifs_free(folio);
827 return true;
828 }
829 EXPORT_SYMBOL_GPL(iomap_release_folio);
830
iomap_invalidate_folio(struct folio * folio,size_t offset,size_t len)831 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
832 {
833 trace_iomap_invalidate_folio(folio->mapping->host,
834 folio_pos(folio) + offset, len);
835
836 /*
837 * If we're invalidating the entire folio, clear the dirty state
838 * from it and release it to avoid unnecessary buildup of the LRU.
839 */
840 if (offset == 0 && len == folio_size(folio)) {
841 WARN_ON_ONCE(folio_test_writeback(folio));
842 folio_cancel_dirty(folio);
843 ifs_free(folio);
844 } else {
845 iomap_clear_range_dirty(folio, offset, len);
846 }
847 }
848 EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
849
iomap_dirty_folio(struct address_space * mapping,struct folio * folio)850 bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
851 {
852 struct inode *inode = mapping->host;
853 size_t len = folio_size(folio);
854
855 ifs_alloc(inode, folio, 0);
856 iomap_set_range_dirty(folio, 0, len);
857 return filemap_dirty_folio(mapping, folio);
858 }
859 EXPORT_SYMBOL_GPL(iomap_dirty_folio);
860
861 static void
iomap_write_failed(struct inode * inode,loff_t pos,unsigned len)862 iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
863 {
864 loff_t i_size = i_size_read(inode);
865
866 /*
867 * Only truncate newly allocated pages beyoned EOF, even if the
868 * write started inside the existing inode size.
869 */
870 if (pos + len > i_size)
871 truncate_pagecache_range(inode, max(pos, i_size),
872 pos + len - 1);
873 }
874
__iomap_write_begin(const struct iomap_iter * iter,const struct iomap_write_ops * write_ops,size_t len,struct folio * folio)875 static int __iomap_write_begin(const struct iomap_iter *iter,
876 const struct iomap_write_ops *write_ops, size_t len,
877 struct folio *folio)
878 {
879 struct iomap_folio_state *ifs;
880 loff_t pos = iter->pos;
881 loff_t block_size = i_blocksize(iter->inode);
882 loff_t block_start = round_down(pos, block_size);
883 loff_t block_end = round_up(pos + len, block_size);
884 unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio);
885 size_t from = offset_in_folio(folio, pos), to = from + len;
886 size_t poff, plen;
887
888 /*
889 * If the write or zeroing completely overlaps the current folio, then
890 * entire folio will be dirtied so there is no need for
891 * per-block state tracking structures to be attached to this folio.
892 * For the unshare case, we must read in the ondisk contents because we
893 * are not changing pagecache contents.
894 */
895 if (!(iter->flags & IOMAP_UNSHARE) && pos <= folio_pos(folio) &&
896 pos + len >= folio_next_pos(folio))
897 return 0;
898
899 ifs = ifs_alloc(iter->inode, folio, iter->flags);
900 if ((iter->flags & IOMAP_NOWAIT) && !ifs && nr_blocks > 1)
901 return -EAGAIN;
902
903 if (folio_test_uptodate(folio))
904 return 0;
905
906 do {
907 iomap_adjust_read_range(iter->inode, folio, &block_start,
908 block_end - block_start, &poff, &plen);
909 if (plen == 0)
910 break;
911
912 /*
913 * If the read range will be entirely overwritten by the write,
914 * we can skip having to zero/read it in.
915 */
916 if (!(iter->flags & IOMAP_UNSHARE) && from <= poff &&
917 to >= poff + plen)
918 continue;
919
920 if (iomap_block_needs_zeroing(iter, block_start)) {
921 if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
922 return -EIO;
923 folio_zero_segments(folio, poff, from, to, poff + plen);
924 } else {
925 const struct iomap *iomap = iomap_iter_srcmap(iter);
926 int status;
927
928 if (iter->flags & IOMAP_NOWAIT)
929 return -EAGAIN;
930
931 if (write_ops && write_ops->read_folio_range)
932 status = write_ops->read_folio_range(iter,
933 folio, block_start, plen);
934 else
935 status = iomap_bio_read_folio_range_sync(iter,
936 folio, block_start, plen);
937 if (status < 0)
938 fserror_report_io(iter->inode,
939 FSERR_BUFFERED_READ, pos,
940 plen, status, GFP_NOFS);
941 if (status)
942 return status;
943
944 if (iomap->flags & IOMAP_F_ZERO_TAIL)
945 folio_zero_segment(folio, to, poff + plen);
946 }
947 iomap_set_range_uptodate(folio, poff, plen);
948 } while ((block_start += plen) < block_end);
949
950 return 0;
951 }
952
__iomap_get_folio(struct iomap_iter * iter,const struct iomap_write_ops * write_ops,size_t len)953 static struct folio *__iomap_get_folio(struct iomap_iter *iter,
954 const struct iomap_write_ops *write_ops, size_t len)
955 {
956 loff_t pos = iter->pos;
957
958 if (!mapping_large_folio_support(iter->inode->i_mapping))
959 len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
960
961 if (iter->iomap.flags & IOMAP_F_FOLIO_BATCH) {
962 struct folio *folio = folio_batch_next(iter->fbatch);
963
964 if (!folio)
965 return NULL;
966
967 /*
968 * The folio mapping generally shouldn't have changed based on
969 * fs locks, but be consistent with filemap lookup and retry
970 * the iter if it does.
971 */
972 folio_lock(folio);
973 if (unlikely(folio->mapping != iter->inode->i_mapping)) {
974 iter->iomap.flags |= IOMAP_F_STALE;
975 folio_unlock(folio);
976 return NULL;
977 }
978
979 folio_get(folio);
980 folio_wait_stable(folio);
981 return folio;
982 }
983
984 if (write_ops && write_ops->get_folio)
985 return write_ops->get_folio(iter, pos, len);
986 return iomap_get_folio(iter, pos, len);
987 }
988
__iomap_put_folio(struct iomap_iter * iter,const struct iomap_write_ops * write_ops,size_t ret,struct folio * folio)989 static void __iomap_put_folio(struct iomap_iter *iter,
990 const struct iomap_write_ops *write_ops, size_t ret,
991 struct folio *folio)
992 {
993 loff_t pos = iter->pos;
994
995 if (write_ops && write_ops->put_folio) {
996 write_ops->put_folio(iter->inode, pos, ret, folio);
997 } else {
998 folio_unlock(folio);
999 folio_put(folio);
1000 }
1001 }
1002
1003 /* trim pos and bytes to within a given folio */
iomap_trim_folio_range(struct iomap_iter * iter,struct folio * folio,size_t * offset,u64 * bytes)1004 static loff_t iomap_trim_folio_range(struct iomap_iter *iter,
1005 struct folio *folio, size_t *offset, u64 *bytes)
1006 {
1007 loff_t pos = iter->pos;
1008 size_t fsize = folio_size(folio);
1009
1010 WARN_ON_ONCE(pos < folio_pos(folio));
1011 WARN_ON_ONCE(pos >= folio_pos(folio) + fsize);
1012
1013 *offset = offset_in_folio(folio, pos);
1014 *bytes = min(*bytes, fsize - *offset);
1015
1016 return pos;
1017 }
1018
iomap_write_begin_inline(const struct iomap_iter * iter,struct folio * folio)1019 static int iomap_write_begin_inline(const struct iomap_iter *iter,
1020 struct folio *folio)
1021 {
1022 /* needs more work for the tailpacking case; disable for now */
1023 if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
1024 return -EIO;
1025 return iomap_read_inline_data(iter, folio);
1026 }
1027
1028 /*
1029 * Grab and prepare a folio for write based on iter state. Returns the folio,
1030 * offset, and length. Callers can optionally pass a max length *plen,
1031 * otherwise init to zero.
1032 */
iomap_write_begin(struct iomap_iter * iter,const struct iomap_write_ops * write_ops,struct folio ** foliop,size_t * poffset,u64 * plen)1033 static int iomap_write_begin(struct iomap_iter *iter,
1034 const struct iomap_write_ops *write_ops, struct folio **foliop,
1035 size_t *poffset, u64 *plen)
1036 {
1037 const struct iomap *srcmap = iomap_iter_srcmap(iter);
1038 loff_t pos;
1039 u64 len = min_t(u64, SIZE_MAX, iomap_length(iter));
1040 struct folio *folio;
1041 int status = 0;
1042
1043 len = min_not_zero(len, *plen);
1044 *foliop = NULL;
1045 *plen = 0;
1046
1047 if (fatal_signal_pending(current))
1048 return -EINTR;
1049
1050 folio = __iomap_get_folio(iter, write_ops, len);
1051 if (IS_ERR(folio))
1052 return PTR_ERR(folio);
1053
1054 /*
1055 * No folio means we're done with a batch. We still have range to
1056 * process so return and let the caller iterate and refill the batch.
1057 */
1058 if (!folio) {
1059 WARN_ON_ONCE(!(iter->iomap.flags & IOMAP_F_FOLIO_BATCH));
1060 return 0;
1061 }
1062
1063 /*
1064 * Now we have a locked folio, before we do anything with it we need to
1065 * check that the iomap we have cached is not stale. The inode extent
1066 * mapping can change due to concurrent IO in flight (e.g.
1067 * IOMAP_UNWRITTEN state can change and memory reclaim could have
1068 * reclaimed a previously partially written page at this index after IO
1069 * completion before this write reaches this file offset) and hence we
1070 * could do the wrong thing here (zero a page range incorrectly or fail
1071 * to zero) and corrupt data.
1072 */
1073 if (write_ops && write_ops->iomap_valid) {
1074 bool iomap_valid = write_ops->iomap_valid(iter->inode,
1075 &iter->iomap);
1076 if (!iomap_valid) {
1077 iter->iomap.flags |= IOMAP_F_STALE;
1078 status = 0;
1079 goto out_unlock;
1080 }
1081 }
1082
1083 /*
1084 * The folios in a batch may not be contiguous. If we've skipped
1085 * forward, advance the iter to the pos of the current folio. If the
1086 * folio starts beyond the end of the mapping, it may have been trimmed
1087 * since the lookup for whatever reason. Return a NULL folio to
1088 * terminate the op.
1089 */
1090 if (folio_pos(folio) > iter->pos) {
1091 len = min_t(u64, folio_pos(folio) - iter->pos,
1092 iomap_length(iter));
1093 status = iomap_iter_advance(iter, len);
1094 len = iomap_length(iter);
1095 if (status || !len)
1096 goto out_unlock;
1097 }
1098
1099 pos = iomap_trim_folio_range(iter, folio, poffset, &len);
1100
1101 if (srcmap->type == IOMAP_INLINE)
1102 status = iomap_write_begin_inline(iter, folio);
1103 else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
1104 status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
1105 else
1106 status = __iomap_write_begin(iter, write_ops, len, folio);
1107
1108 if (unlikely(status))
1109 goto out_unlock;
1110
1111 *foliop = folio;
1112 *plen = len;
1113 return 0;
1114
1115 out_unlock:
1116 __iomap_put_folio(iter, write_ops, 0, folio);
1117 return status;
1118 }
1119
__iomap_write_end(struct inode * inode,loff_t pos,size_t len,size_t copied,struct folio * folio)1120 static bool __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
1121 size_t copied, struct folio *folio)
1122 {
1123 flush_dcache_folio(folio);
1124
1125 /*
1126 * The blocks that were entirely written will now be uptodate, so we
1127 * don't have to worry about a read_folio reading them and overwriting a
1128 * partial write. However, if we've encountered a short write and only
1129 * partially written into a block, it will not be marked uptodate, so a
1130 * read_folio might come in and destroy our partial write.
1131 *
1132 * Do the simplest thing and just treat any short write to a
1133 * non-uptodate page as a zero-length write, and force the caller to
1134 * redo the whole thing.
1135 */
1136 if (unlikely(copied < len && !folio_test_uptodate(folio)))
1137 return false;
1138 iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
1139 iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
1140 filemap_dirty_folio(inode->i_mapping, folio);
1141 return true;
1142 }
1143
iomap_write_end_inline(const struct iomap_iter * iter,struct folio * folio,loff_t pos,size_t copied)1144 static bool iomap_write_end_inline(const struct iomap_iter *iter,
1145 struct folio *folio, loff_t pos, size_t copied)
1146 {
1147 const struct iomap *iomap = &iter->iomap;
1148 void *addr;
1149
1150 WARN_ON_ONCE(!folio_test_uptodate(folio));
1151
1152 if (WARN_ON_ONCE(!iomap->inline_data))
1153 return false;
1154
1155 flush_dcache_folio(folio);
1156 addr = kmap_local_folio(folio, pos);
1157 memcpy(iomap_inline_data(iomap, pos), addr, copied);
1158 kunmap_local(addr);
1159
1160 mark_inode_dirty(iter->inode);
1161 return true;
1162 }
1163
1164 /*
1165 * Returns true if all copied bytes have been written to the pagecache,
1166 * otherwise return false.
1167 */
iomap_write_end(struct iomap_iter * iter,size_t len,size_t copied,struct folio * folio)1168 static bool iomap_write_end(struct iomap_iter *iter, size_t len, size_t copied,
1169 struct folio *folio)
1170 {
1171 const struct iomap *srcmap = iomap_iter_srcmap(iter);
1172 loff_t pos = iter->pos;
1173
1174 if (srcmap->type == IOMAP_INLINE)
1175 return iomap_write_end_inline(iter, folio, pos, copied);
1176
1177 if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
1178 size_t bh_written;
1179
1180 bh_written = block_write_end(pos, len, copied, folio);
1181 WARN_ON_ONCE(bh_written != copied && bh_written != 0);
1182 return bh_written == copied;
1183 }
1184
1185 return __iomap_write_end(iter->inode, pos, len, copied, folio);
1186 }
1187
iomap_write_iter(struct iomap_iter * iter,struct iov_iter * i,const struct iomap_write_ops * write_ops)1188 static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i,
1189 const struct iomap_write_ops *write_ops)
1190 {
1191 int status = 0;
1192 struct address_space *mapping = iter->inode->i_mapping;
1193 size_t chunk = mapping_max_folio_size(mapping);
1194 unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0;
1195
1196 do {
1197 struct folio *folio;
1198 loff_t old_size;
1199 size_t offset; /* Offset into folio */
1200 u64 bytes; /* Bytes to write to folio */
1201 size_t copied; /* Bytes copied from user */
1202 u64 written; /* Bytes have been written */
1203 loff_t pos;
1204
1205 bytes = iov_iter_count(i);
1206 retry:
1207 offset = iter->pos & (chunk - 1);
1208 bytes = min(chunk - offset, bytes);
1209 status = balance_dirty_pages_ratelimited_flags(mapping,
1210 bdp_flags);
1211 if (unlikely(status))
1212 break;
1213
1214 if (bytes > iomap_length(iter))
1215 bytes = iomap_length(iter);
1216
1217 /*
1218 * Bring in the user page that we'll copy from _first_.
1219 * Otherwise there's a nasty deadlock on copying from the
1220 * same page as we're writing to, without it being marked
1221 * up-to-date.
1222 *
1223 * For async buffered writes the assumption is that the user
1224 * page has already been faulted in. This can be optimized by
1225 * faulting the user page.
1226 */
1227 if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
1228 status = -EFAULT;
1229 break;
1230 }
1231
1232 status = iomap_write_begin(iter, write_ops, &folio, &offset,
1233 &bytes);
1234 if (unlikely(status)) {
1235 iomap_write_failed(iter->inode, iter->pos, bytes);
1236 break;
1237 }
1238 if (iter->iomap.flags & IOMAP_F_STALE)
1239 break;
1240
1241 pos = iter->pos;
1242
1243 if (mapping_writably_mapped(mapping))
1244 flush_dcache_folio(folio);
1245
1246 copied = copy_folio_from_iter_atomic(folio, offset, bytes, i);
1247 written = iomap_write_end(iter, bytes, copied, folio) ?
1248 copied : 0;
1249
1250 /*
1251 * Update the in-memory inode size after copying the data into
1252 * the page cache. It's up to the file system to write the
1253 * updated size to disk, preferably after I/O completion so that
1254 * no stale data is exposed. Only once that's done can we
1255 * unlock and release the folio.
1256 */
1257 old_size = iter->inode->i_size;
1258 if (pos + written > old_size &&
1259 !(iter->iomap.flags & IOMAP_F_FSVERITY)) {
1260 i_size_write(iter->inode, pos + written);
1261 iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
1262 }
1263 __iomap_put_folio(iter, write_ops, written, folio);
1264
1265 if (old_size < pos && !(iter->iomap.flags & IOMAP_F_FSVERITY))
1266 pagecache_isize_extended(iter->inode, old_size, pos);
1267
1268 cond_resched();
1269 if (unlikely(written == 0)) {
1270 /*
1271 * A short copy made iomap_write_end() reject the
1272 * thing entirely. Might be memory poisoning
1273 * halfway through, might be a race with munmap,
1274 * might be severe memory pressure.
1275 */
1276 iomap_write_failed(iter->inode, pos, bytes);
1277 iov_iter_revert(i, copied);
1278
1279 if (chunk > PAGE_SIZE)
1280 chunk /= 2;
1281 if (copied) {
1282 bytes = copied;
1283 goto retry;
1284 }
1285 } else {
1286 iomap_iter_advance(iter, written);
1287 }
1288 } while (iov_iter_count(i) && iomap_length(iter));
1289
1290 return status;
1291 }
1292
1293 ssize_t
iomap_file_buffered_write(struct kiocb * iocb,struct iov_iter * i,const struct iomap_ops * ops,const struct iomap_write_ops * write_ops,void * private)1294 iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
1295 const struct iomap_ops *ops,
1296 const struct iomap_write_ops *write_ops, void *private)
1297 {
1298 struct iomap_iter iter = {
1299 .inode = iocb->ki_filp->f_mapping->host,
1300 .pos = iocb->ki_pos,
1301 .len = iov_iter_count(i),
1302 .flags = IOMAP_WRITE,
1303 .private = private,
1304 };
1305 ssize_t ret;
1306
1307 if (iocb->ki_flags & IOCB_NOWAIT)
1308 iter.flags |= IOMAP_NOWAIT;
1309 if (iocb->ki_flags & IOCB_DONTCACHE)
1310 iter.flags |= IOMAP_DONTCACHE;
1311
1312 while ((ret = iomap_iter(&iter, ops)) > 0)
1313 iter.status = iomap_write_iter(&iter, i, write_ops);
1314
1315 if (unlikely(iter.pos == iocb->ki_pos))
1316 return ret;
1317 ret = iter.pos - iocb->ki_pos;
1318 iocb->ki_pos = iter.pos;
1319 return ret;
1320 }
1321 EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
1322
iomap_fsverity_write(struct file * file,loff_t pos,size_t length,const void * buf,const struct iomap_ops * ops,const struct iomap_write_ops * write_ops)1323 int iomap_fsverity_write(struct file *file, loff_t pos, size_t length,
1324 const void *buf, const struct iomap_ops *ops,
1325 const struct iomap_write_ops *write_ops)
1326 {
1327 int ret;
1328 struct iov_iter iiter;
1329 struct kvec kvec = {
1330 .iov_base = (void *)buf,
1331 .iov_len = length,
1332 };
1333 struct kiocb iocb = {
1334 .ki_filp = file,
1335 .ki_ioprio = get_current_ioprio(),
1336 .ki_pos = pos,
1337 };
1338
1339 iov_iter_kvec(&iiter, WRITE, &kvec, 1, length);
1340
1341 ret = iomap_file_buffered_write(&iocb, &iiter, ops, write_ops, NULL);
1342 if (ret < 0)
1343 return ret;
1344 return ret == length ? 0 : -EIO;
1345 }
1346 EXPORT_SYMBOL_GPL(iomap_fsverity_write);
1347
iomap_write_delalloc_ifs_punch(struct inode * inode,struct folio * folio,loff_t start_byte,loff_t end_byte,struct iomap * iomap,iomap_punch_t punch)1348 static void iomap_write_delalloc_ifs_punch(struct inode *inode,
1349 struct folio *folio, loff_t start_byte, loff_t end_byte,
1350 struct iomap *iomap, iomap_punch_t punch)
1351 {
1352 unsigned int first_blk, last_blk;
1353 loff_t last_byte;
1354 u8 blkbits = inode->i_blkbits;
1355 struct iomap_folio_state *ifs;
1356
1357 /*
1358 * When we have per-block dirty tracking, there can be
1359 * blocks within a folio which are marked uptodate
1360 * but not dirty. In that case it is necessary to punch
1361 * out such blocks to avoid leaking any delalloc blocks.
1362 */
1363 ifs = folio->private;
1364 if (!ifs)
1365 return;
1366
1367 last_byte = min_t(loff_t, end_byte - 1, folio_next_pos(folio) - 1);
1368 first_blk = offset_in_folio(folio, start_byte) >> blkbits;
1369 last_blk = offset_in_folio(folio, last_byte) >> blkbits;
1370 while ((first_blk = ifs_next_clean_block(folio, first_blk, last_blk))
1371 <= last_blk) {
1372 punch(inode, folio_pos(folio) + (first_blk << blkbits),
1373 1 << blkbits, iomap);
1374 first_blk++;
1375 }
1376 }
1377
iomap_write_delalloc_punch(struct inode * inode,struct folio * folio,loff_t * punch_start_byte,loff_t start_byte,loff_t end_byte,struct iomap * iomap,iomap_punch_t punch)1378 static void iomap_write_delalloc_punch(struct inode *inode, struct folio *folio,
1379 loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
1380 struct iomap *iomap, iomap_punch_t punch)
1381 {
1382 if (!folio_test_dirty(folio))
1383 return;
1384
1385 /* if dirty, punch up to offset */
1386 if (start_byte > *punch_start_byte) {
1387 punch(inode, *punch_start_byte, start_byte - *punch_start_byte,
1388 iomap);
1389 }
1390
1391 /* Punch non-dirty blocks within folio */
1392 iomap_write_delalloc_ifs_punch(inode, folio, start_byte, end_byte,
1393 iomap, punch);
1394
1395 /*
1396 * Make sure the next punch start is correctly bound to
1397 * the end of this data range, not the end of the folio.
1398 */
1399 *punch_start_byte = min_t(loff_t, end_byte, folio_next_pos(folio));
1400 }
1401
1402 /*
1403 * Scan the data range passed to us for dirty page cache folios. If we find a
1404 * dirty folio, punch out the preceding range and update the offset from which
1405 * the next punch will start from.
1406 *
1407 * We can punch out storage reservations under clean pages because they either
1408 * contain data that has been written back - in which case the delalloc punch
1409 * over that range is a no-op - or they have been read faults in which case they
1410 * contain zeroes and we can remove the delalloc backing range and any new
1411 * writes to those pages will do the normal hole filling operation...
1412 *
1413 * This makes the logic simple: we only need to keep the delalloc extents only
1414 * over the dirty ranges of the page cache.
1415 *
1416 * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
1417 * simplify range iterations.
1418 */
iomap_write_delalloc_scan(struct inode * inode,loff_t * punch_start_byte,loff_t start_byte,loff_t end_byte,struct iomap * iomap,iomap_punch_t punch)1419 static void iomap_write_delalloc_scan(struct inode *inode,
1420 loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
1421 struct iomap *iomap, iomap_punch_t punch)
1422 {
1423 while (start_byte < end_byte) {
1424 struct folio *folio;
1425
1426 /* grab locked page */
1427 folio = filemap_lock_folio(inode->i_mapping,
1428 start_byte >> PAGE_SHIFT);
1429 if (IS_ERR(folio)) {
1430 start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) +
1431 PAGE_SIZE;
1432 continue;
1433 }
1434
1435 iomap_write_delalloc_punch(inode, folio, punch_start_byte,
1436 start_byte, end_byte, iomap, punch);
1437
1438 /* move offset to start of next folio in range */
1439 start_byte = folio_next_pos(folio);
1440 folio_unlock(folio);
1441 folio_put(folio);
1442 }
1443 }
1444
1445 /*
1446 * When a short write occurs, the filesystem might need to use ->iomap_end
1447 * to remove space reservations created in ->iomap_begin.
1448 *
1449 * For filesystems that use delayed allocation, there can be dirty pages over
1450 * the delalloc extent outside the range of a short write but still within the
1451 * delalloc extent allocated for this iomap if the write raced with page
1452 * faults.
1453 *
1454 * Punch out all the delalloc blocks in the range given except for those that
1455 * have dirty data still pending in the page cache - those are going to be
1456 * written and so must still retain the delalloc backing for writeback.
1457 *
1458 * The punch() callback *must* only punch delalloc extents in the range passed
1459 * to it. It must skip over all other types of extents in the range and leave
1460 * them completely unchanged. It must do this punch atomically with respect to
1461 * other extent modifications.
1462 *
1463 * The punch() callback may be called with a folio locked to prevent writeback
1464 * extent allocation racing at the edge of the range we are currently punching.
1465 * The locked folio may or may not cover the range being punched, so it is not
1466 * safe for the punch() callback to lock folios itself.
1467 *
1468 * Lock order is:
1469 *
1470 * inode->i_rwsem (shared or exclusive)
1471 * inode->i_mapping->invalidate_lock (exclusive)
1472 * folio_lock()
1473 * ->punch
1474 * internal filesystem allocation lock
1475 *
1476 * As we are scanning the page cache for data, we don't need to reimplement the
1477 * wheel - mapping_seek_hole_data() does exactly what we need to identify the
1478 * start and end of data ranges correctly even for sub-folio block sizes. This
1479 * byte range based iteration is especially convenient because it means we
1480 * don't have to care about variable size folios, nor where the start or end of
1481 * the data range lies within a folio, if they lie within the same folio or even
1482 * if there are multiple discontiguous data ranges within the folio.
1483 *
1484 * It should be noted that mapping_seek_hole_data() is not aware of EOF, and so
1485 * can return data ranges that exist in the cache beyond EOF. e.g. a page fault
1486 * spanning EOF will initialise the post-EOF data to zeroes and mark it up to
1487 * date. A write page fault can then mark it dirty. If we then fail a write()
1488 * beyond EOF into that up to date cached range, we allocate a delalloc block
1489 * beyond EOF and then have to punch it out. Because the range is up to date,
1490 * mapping_seek_hole_data() will return it, and we will skip the punch because
1491 * the folio is dirty. THis is incorrect - we always need to punch out delalloc
1492 * beyond EOF in this case as writeback will never write back and covert that
1493 * delalloc block beyond EOF. Hence we limit the cached data scan range to EOF,
1494 * resulting in always punching out the range from the EOF to the end of the
1495 * range the iomap spans.
1496 *
1497 * Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it
1498 * matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA
1499 * returns the start of a data range (start_byte), and SEEK_HOLE(start_byte)
1500 * returns the end of the data range (data_end). Using closed intervals would
1501 * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose
1502 * the code to subtle off-by-one bugs....
1503 */
iomap_write_delalloc_release(struct inode * inode,loff_t start_byte,loff_t end_byte,unsigned flags,struct iomap * iomap,iomap_punch_t punch)1504 void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte,
1505 loff_t end_byte, unsigned flags, struct iomap *iomap,
1506 iomap_punch_t punch)
1507 {
1508 loff_t punch_start_byte = start_byte;
1509 loff_t scan_end_byte = min(i_size_read(inode), end_byte);
1510
1511 /*
1512 * The caller must hold invalidate_lock to avoid races with page faults
1513 * re-instantiating folios and dirtying them via ->page_mkwrite whilst
1514 * we walk the cache and perform delalloc extent removal. Failing to do
1515 * this can leave dirty pages with no space reservation in the cache.
1516 */
1517 lockdep_assert_held_write(&inode->i_mapping->invalidate_lock);
1518
1519 while (start_byte < scan_end_byte) {
1520 loff_t data_end;
1521
1522 start_byte = mapping_seek_hole_data(inode->i_mapping,
1523 start_byte, scan_end_byte, SEEK_DATA);
1524 /*
1525 * If there is no more data to scan, all that is left is to
1526 * punch out the remaining range.
1527 *
1528 * Note that mapping_seek_hole_data is only supposed to return
1529 * either an offset or -ENXIO, so WARN on any other error as
1530 * that would be an API change without updating the callers.
1531 */
1532 if (start_byte == -ENXIO || start_byte == scan_end_byte)
1533 break;
1534 if (WARN_ON_ONCE(start_byte < 0))
1535 return;
1536 WARN_ON_ONCE(start_byte < punch_start_byte);
1537 WARN_ON_ONCE(start_byte > scan_end_byte);
1538
1539 /*
1540 * We find the end of this contiguous cached data range by
1541 * seeking from start_byte to the beginning of the next hole.
1542 */
1543 data_end = mapping_seek_hole_data(inode->i_mapping, start_byte,
1544 scan_end_byte, SEEK_HOLE);
1545 if (WARN_ON_ONCE(data_end < 0))
1546 return;
1547
1548 /*
1549 * If we race with post-direct I/O invalidation of the page cache,
1550 * there might be no data left at start_byte.
1551 */
1552 if (data_end == start_byte)
1553 continue;
1554
1555 WARN_ON_ONCE(data_end < start_byte);
1556 WARN_ON_ONCE(data_end > scan_end_byte);
1557
1558 iomap_write_delalloc_scan(inode, &punch_start_byte, start_byte,
1559 data_end, iomap, punch);
1560
1561 /* The next data search starts at the end of this one. */
1562 start_byte = data_end;
1563 }
1564
1565 if (punch_start_byte < end_byte)
1566 punch(inode, punch_start_byte, end_byte - punch_start_byte,
1567 iomap);
1568 }
1569 EXPORT_SYMBOL_GPL(iomap_write_delalloc_release);
1570
iomap_unshare_iter(struct iomap_iter * iter,const struct iomap_write_ops * write_ops)1571 static int iomap_unshare_iter(struct iomap_iter *iter,
1572 const struct iomap_write_ops *write_ops)
1573 {
1574 struct iomap *iomap = &iter->iomap;
1575 u64 bytes = iomap_length(iter);
1576 int status;
1577
1578 if (!iomap_want_unshare_iter(iter))
1579 return iomap_iter_advance(iter, bytes);
1580
1581 do {
1582 struct folio *folio;
1583 size_t offset;
1584 bool ret;
1585
1586 bytes = min_t(u64, SIZE_MAX, bytes);
1587 status = iomap_write_begin(iter, write_ops, &folio, &offset,
1588 &bytes);
1589 if (unlikely(status))
1590 return status;
1591 if (iomap->flags & IOMAP_F_STALE)
1592 break;
1593
1594 ret = iomap_write_end(iter, bytes, bytes, folio);
1595 __iomap_put_folio(iter, write_ops, bytes, folio);
1596 if (WARN_ON_ONCE(!ret))
1597 return -EIO;
1598
1599 cond_resched();
1600
1601 balance_dirty_pages_ratelimited(iter->inode->i_mapping);
1602
1603 status = iomap_iter_advance(iter, bytes);
1604 if (status)
1605 break;
1606 } while ((bytes = iomap_length(iter)) > 0);
1607
1608 return status;
1609 }
1610
1611 int
iomap_file_unshare(struct inode * inode,loff_t pos,loff_t len,const struct iomap_ops * ops,const struct iomap_write_ops * write_ops)1612 iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
1613 const struct iomap_ops *ops,
1614 const struct iomap_write_ops *write_ops)
1615 {
1616 struct iomap_iter iter = {
1617 .inode = inode,
1618 .pos = pos,
1619 .flags = IOMAP_WRITE | IOMAP_UNSHARE,
1620 };
1621 loff_t size = i_size_read(inode);
1622 int ret;
1623
1624 if (pos < 0 || pos >= size)
1625 return 0;
1626
1627 iter.len = min(len, size - pos);
1628 while ((ret = iomap_iter(&iter, ops)) > 0)
1629 iter.status = iomap_unshare_iter(&iter, write_ops);
1630 return ret;
1631 }
1632 EXPORT_SYMBOL_GPL(iomap_file_unshare);
1633
1634 /*
1635 * Flush the remaining range of the iter and mark the current mapping stale.
1636 * This is used when zero range sees an unwritten mapping that may have had
1637 * dirty pagecache over it.
1638 */
iomap_zero_iter_flush_and_stale(struct iomap_iter * i)1639 static inline int iomap_zero_iter_flush_and_stale(struct iomap_iter *i)
1640 {
1641 struct address_space *mapping = i->inode->i_mapping;
1642 loff_t end = i->pos + i->len - 1;
1643
1644 i->iomap.flags |= IOMAP_F_STALE;
1645 return filemap_write_and_wait_range(mapping, i->pos, end);
1646 }
1647
iomap_zero_iter(struct iomap_iter * iter,bool * did_zero,const struct iomap_write_ops * write_ops)1648 static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero,
1649 const struct iomap_write_ops *write_ops)
1650 {
1651 u64 bytes = iomap_length(iter);
1652 bool zeroed = false;
1653 int status;
1654
1655 do {
1656 struct folio *folio;
1657 size_t offset;
1658 bool ret;
1659
1660 balance_dirty_pages_ratelimited(iter->inode->i_mapping);
1661
1662 bytes = min_t(u64, SIZE_MAX, bytes);
1663 status = iomap_write_begin(iter, write_ops, &folio, &offset,
1664 &bytes);
1665 if (status)
1666 return status;
1667 if (iter->iomap.flags & IOMAP_F_STALE)
1668 break;
1669
1670 /* a NULL folio means we're done with a folio batch */
1671 if (!folio) {
1672 status = iomap_iter_advance_full(iter);
1673 if (status)
1674 return status;
1675 break;
1676 }
1677
1678 /* warn about zeroing folios beyond eof that won't write back */
1679 WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);
1680
1681 trace_iomap_zero_iter(iter->inode, folio_pos(folio) + offset,
1682 bytes);
1683
1684 folio_zero_range(folio, offset, bytes);
1685 zeroed = true;
1686 folio_mark_accessed(folio);
1687
1688 ret = iomap_write_end(iter, bytes, bytes, folio);
1689 __iomap_put_folio(iter, write_ops, bytes, folio);
1690 if (WARN_ON_ONCE(!ret))
1691 return -EIO;
1692
1693 status = iomap_iter_advance(iter, bytes);
1694 if (status)
1695 return status;
1696 } while ((bytes = iomap_length(iter)) > 0);
1697
1698 if (did_zero && zeroed)
1699 *did_zero = true;
1700 return status;
1701 }
1702
1703 /**
1704 * iomap_fill_dirty_folios - fill a folio batch with dirty folios
1705 * @iter: Iteration structure
1706 * @start: Start offset of range. Updated based on lookup progress.
1707 * @end: End offset of range
1708 * @iomap_flags: Flags to set on the associated iomap to track the batch.
1709 *
1710 * Returns the folio count directly. Also returns the associated control flag if
1711 * the the batch lookup is performed and the expected offset of a subsequent
1712 * lookup via out params. The caller is responsible to set the flag on the
1713 * associated iomap.
1714 */
1715 unsigned int
iomap_fill_dirty_folios(struct iomap_iter * iter,loff_t * start,loff_t end,unsigned int * iomap_flags)1716 iomap_fill_dirty_folios(
1717 struct iomap_iter *iter,
1718 loff_t *start,
1719 loff_t end,
1720 unsigned int *iomap_flags)
1721 {
1722 struct address_space *mapping = iter->inode->i_mapping;
1723 pgoff_t pstart = *start >> PAGE_SHIFT;
1724 pgoff_t pend = (end - 1) >> PAGE_SHIFT;
1725 unsigned int count;
1726
1727 if (!iter->fbatch) {
1728 *start = end;
1729 return 0;
1730 }
1731
1732 count = filemap_get_folios_dirty(mapping, &pstart, pend, iter->fbatch);
1733 *start = (pstart << PAGE_SHIFT);
1734 *iomap_flags |= IOMAP_F_FOLIO_BATCH;
1735 return count;
1736 }
1737 EXPORT_SYMBOL_GPL(iomap_fill_dirty_folios);
1738
1739 int
iomap_zero_range(struct inode * inode,loff_t pos,loff_t len,bool * did_zero,const struct iomap_ops * ops,const struct iomap_write_ops * write_ops,void * private)1740 iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1741 const struct iomap_ops *ops,
1742 const struct iomap_write_ops *write_ops, void *private)
1743 {
1744 struct folio_batch fbatch;
1745 struct iomap_iter iter = {
1746 .inode = inode,
1747 .pos = pos,
1748 .len = len,
1749 .flags = IOMAP_ZERO,
1750 .private = private,
1751 .fbatch = &fbatch,
1752 };
1753 struct address_space *mapping = inode->i_mapping;
1754 int ret;
1755 bool range_dirty;
1756
1757 folio_batch_init(&fbatch);
1758
1759 /*
1760 * To avoid an unconditional flush, check pagecache state and only flush
1761 * if dirty and the fs returns a mapping that might convert on
1762 * writeback.
1763 */
1764 range_dirty = filemap_range_needs_writeback(mapping, iter.pos,
1765 iter.pos + iter.len - 1);
1766 while ((ret = iomap_iter(&iter, ops)) > 0) {
1767 const struct iomap *srcmap = iomap_iter_srcmap(&iter);
1768
1769 if (!(iter.iomap.flags & IOMAP_F_FOLIO_BATCH) &&
1770 (srcmap->type == IOMAP_HOLE ||
1771 srcmap->type == IOMAP_UNWRITTEN)) {
1772 s64 status;
1773
1774 if (range_dirty && srcmap->type == IOMAP_UNWRITTEN) {
1775 range_dirty = false;
1776 status = iomap_zero_iter_flush_and_stale(&iter);
1777 } else {
1778 status = iomap_iter_advance_full(&iter);
1779 }
1780 iter.status = status;
1781 continue;
1782 }
1783
1784 iter.status = iomap_zero_iter(&iter, did_zero, write_ops);
1785 }
1786 return ret;
1787 }
1788 EXPORT_SYMBOL_GPL(iomap_zero_range);
1789
1790 int
iomap_truncate_page(struct inode * inode,loff_t pos,bool * did_zero,const struct iomap_ops * ops,const struct iomap_write_ops * write_ops,void * private)1791 iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1792 const struct iomap_ops *ops,
1793 const struct iomap_write_ops *write_ops, void *private)
1794 {
1795 unsigned int blocksize = i_blocksize(inode);
1796 unsigned int off = pos & (blocksize - 1);
1797
1798 /* Block boundary? Nothing to do */
1799 if (!off)
1800 return 0;
1801 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops,
1802 write_ops, private);
1803 }
1804 EXPORT_SYMBOL_GPL(iomap_truncate_page);
1805
iomap_folio_mkwrite_iter(struct iomap_iter * iter,struct folio * folio)1806 static int iomap_folio_mkwrite_iter(struct iomap_iter *iter,
1807 struct folio *folio)
1808 {
1809 loff_t length = iomap_length(iter);
1810 int ret;
1811
1812 if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
1813 ret = __block_write_begin_int(folio, iter->pos, length, NULL,
1814 &iter->iomap);
1815 if (ret)
1816 return ret;
1817 block_commit_write(folio, 0, length);
1818 } else {
1819 WARN_ON_ONCE(!folio_test_uptodate(folio));
1820 folio_mark_dirty(folio);
1821 }
1822
1823 return iomap_iter_advance(iter, length);
1824 }
1825
iomap_page_mkwrite(struct vm_fault * vmf,const struct iomap_ops * ops,void * private)1826 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops,
1827 void *private)
1828 {
1829 struct iomap_iter iter = {
1830 .inode = file_inode(vmf->vma->vm_file),
1831 .flags = IOMAP_WRITE | IOMAP_FAULT,
1832 .private = private,
1833 };
1834 struct folio *folio = page_folio(vmf->page);
1835 ssize_t ret;
1836
1837 folio_lock(folio);
1838 ret = folio_mkwrite_check_truncate(folio, iter.inode);
1839 if (ret < 0)
1840 goto out_unlock;
1841 iter.pos = folio_pos(folio);
1842 iter.len = ret;
1843 while ((ret = iomap_iter(&iter, ops)) > 0)
1844 iter.status = iomap_folio_mkwrite_iter(&iter, folio);
1845
1846 if (ret < 0)
1847 goto out_unlock;
1848 folio_wait_stable(folio);
1849 return VM_FAULT_LOCKED;
1850 out_unlock:
1851 folio_unlock(folio);
1852 return vmf_fs_error(ret);
1853 }
1854 EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1855
iomap_writeback_init(struct inode * inode,struct folio * folio)1856 static void iomap_writeback_init(struct inode *inode, struct folio *folio)
1857 {
1858 struct iomap_folio_state *ifs = folio->private;
1859
1860 WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs);
1861 if (ifs) {
1862 WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending) != 0);
1863 /*
1864 * Set this to the folio size. After processing the folio for
1865 * writeback in iomap_writeback_folio(), we'll subtract any
1866 * ranges not written back.
1867 *
1868 * We do this because otherwise, we would have to atomically
1869 * increment ifs->write_bytes_pending every time a range in the
1870 * folio needs to be written back.
1871 */
1872 atomic_set(&ifs->write_bytes_pending, folio_size(folio));
1873 }
1874 }
1875
iomap_finish_folio_write(struct inode * inode,struct folio * folio,size_t len)1876 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
1877 size_t len)
1878 {
1879 struct iomap_folio_state *ifs = folio->private;
1880
1881 WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs);
1882 WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
1883
1884 if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
1885 folio_end_writeback(folio);
1886 }
1887 EXPORT_SYMBOL_GPL(iomap_finish_folio_write);
1888
iomap_writeback_range(struct iomap_writepage_ctx * wpc,struct folio * folio,u64 pos,u32 rlen,u64 end_pos,size_t * bytes_submitted)1889 static int iomap_writeback_range(struct iomap_writepage_ctx *wpc,
1890 struct folio *folio, u64 pos, u32 rlen, u64 end_pos,
1891 size_t *bytes_submitted)
1892 {
1893 do {
1894 ssize_t ret;
1895
1896 ret = wpc->ops->writeback_range(wpc, folio, pos, rlen, end_pos);
1897 if (WARN_ON_ONCE(ret == 0 || ret > rlen))
1898 return -EIO;
1899 if (ret < 0)
1900 return ret;
1901 rlen -= ret;
1902 pos += ret;
1903
1904 /*
1905 * Holes are not written back by ->writeback_range, so track
1906 * if we did handle anything that is not a hole here.
1907 */
1908 if (wpc->iomap.type != IOMAP_HOLE)
1909 *bytes_submitted += ret;
1910 } while (rlen);
1911
1912 return 0;
1913 }
1914
1915 /*
1916 * Check interaction of the folio with the file end.
1917 *
1918 * If the folio is entirely beyond i_size, return false. If it straddles
1919 * i_size, adjust end_pos and zero all data beyond i_size. Don't skip fsverity
1920 * folios as those are beyond i_size.
1921 */
iomap_writeback_handle_eof(struct folio * folio,struct iomap_writepage_ctx * wpc,u64 * end_pos)1922 static bool iomap_writeback_handle_eof(struct folio *folio,
1923 struct iomap_writepage_ctx *wpc, u64 *end_pos)
1924 {
1925 struct inode *inode = wpc->inode;
1926 u64 isize = i_size_read(inode);
1927
1928 if (wpc->iomap.flags & IOMAP_F_FSVERITY) {
1929 WARN_ON_ONCE(folio_pos(folio) < isize);
1930 return true;
1931 }
1932
1933 if (*end_pos > isize) {
1934 size_t poff = offset_in_folio(folio, isize);
1935 pgoff_t end_index = isize >> PAGE_SHIFT;
1936
1937 /*
1938 * If the folio is entirely ouside of i_size, skip it.
1939 *
1940 * This can happen due to a truncate operation that is in
1941 * progress and in that case truncate will finish it off once
1942 * we've dropped the folio lock.
1943 *
1944 * Note that the pgoff_t used for end_index is an unsigned long.
1945 * If the given offset is greater than 16TB on a 32-bit system,
1946 * then if we checked if the folio is fully outside i_size with
1947 * "if (folio->index >= end_index + 1)", "end_index + 1" would
1948 * overflow and evaluate to 0. Hence this folio would be
1949 * redirtied and written out repeatedly, which would result in
1950 * an infinite loop; the user program performing this operation
1951 * would hang. Instead, we can detect this situation by
1952 * checking if the folio is totally beyond i_size or if its
1953 * offset is just equal to the EOF.
1954 */
1955 if (folio->index > end_index ||
1956 (folio->index == end_index && poff == 0))
1957 return false;
1958
1959 /*
1960 * The folio straddles i_size.
1961 *
1962 * It must be zeroed out on each and every writepage invocation
1963 * because it may be mmapped:
1964 *
1965 * A file is mapped in multiples of the page size. For a
1966 * file that is not a multiple of the page size, the
1967 * remaining memory is zeroed when mapped, and writes to that
1968 * region are not written out to the file.
1969 *
1970 * Also adjust the end_pos to the end of file and skip writeback
1971 * for all blocks entirely beyond i_size.
1972 */
1973 folio_zero_segment(folio, poff, folio_size(folio));
1974 *end_pos = isize;
1975 }
1976
1977 return true;
1978 }
1979
iomap_writeback_folio(struct iomap_writepage_ctx * wpc,struct folio * folio)1980 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
1981 {
1982 struct iomap_folio_state *ifs = folio->private;
1983 struct inode *inode = wpc->inode;
1984 u64 pos = folio_pos(folio);
1985 u64 end_pos = pos + folio_size(folio);
1986 u64 end_aligned = 0;
1987 loff_t orig_pos = pos;
1988 size_t bytes_submitted = 0;
1989 int error = 0;
1990 u32 rlen;
1991
1992 WARN_ON_ONCE(!folio_test_locked(folio));
1993 WARN_ON_ONCE(folio_test_dirty(folio));
1994 WARN_ON_ONCE(folio_test_writeback(folio));
1995
1996 trace_iomap_writeback_folio(inode, pos, folio_size(folio));
1997
1998 if (!iomap_writeback_handle_eof(folio, wpc, &end_pos))
1999 return 0;
2000 WARN_ON_ONCE(end_pos <= pos);
2001
2002 if (i_blocks_per_folio(inode, folio) > 1) {
2003 if (!ifs) {
2004 ifs = ifs_alloc(inode, folio, 0);
2005 iomap_set_range_dirty(folio, 0, end_pos - pos);
2006 }
2007
2008 iomap_writeback_init(inode, folio);
2009 }
2010
2011 /*
2012 * Set the writeback bit ASAP, as the I/O completion for the single
2013 * block per folio case happen hit as soon as we're submitting the bio.
2014 */
2015 folio_start_writeback(folio);
2016
2017 /*
2018 * Walk through the folio to find dirty areas to write back.
2019 */
2020 end_aligned = round_up(end_pos, i_blocksize(inode));
2021 while ((rlen = iomap_find_dirty_range(folio, &pos, end_aligned))) {
2022 error = iomap_writeback_range(wpc, folio, pos, rlen, end_pos,
2023 &bytes_submitted);
2024 if (error)
2025 break;
2026 pos += rlen;
2027 }
2028
2029 if (bytes_submitted)
2030 wpc->nr_folios++;
2031 if (error && pos > orig_pos)
2032 fserror_report_io(inode, FSERR_BUFFERED_WRITE, orig_pos, 0,
2033 error, GFP_NOFS);
2034
2035 /*
2036 * We can have dirty bits set past end of file in page_mkwrite path
2037 * while mapping the last partial folio. Hence it's better to clear
2038 * all the dirty bits in the folio here.
2039 */
2040 iomap_clear_range_dirty(folio, 0, folio_size(folio));
2041
2042 /*
2043 * Usually the writeback bit is cleared by the I/O completion handler.
2044 * But we may end up either not actually writing any blocks, or (when
2045 * there are multiple blocks in a folio) all I/O might have finished
2046 * already at this point. In that case we need to clear the writeback
2047 * bit ourselves right after unlocking the page.
2048 */
2049 if (ifs) {
2050 /*
2051 * Subtract any bytes that were initially accounted to
2052 * write_bytes_pending but skipped for writeback.
2053 */
2054 size_t bytes_not_submitted = folio_size(folio) -
2055 bytes_submitted;
2056
2057 if (bytes_not_submitted)
2058 iomap_finish_folio_write(inode, folio,
2059 bytes_not_submitted);
2060 } else if (!bytes_submitted) {
2061 folio_end_writeback(folio);
2062 }
2063
2064 mapping_set_error(inode->i_mapping, error);
2065 return error;
2066 }
2067 EXPORT_SYMBOL_GPL(iomap_writeback_folio);
2068
2069 int
iomap_writepages(struct iomap_writepage_ctx * wpc)2070 iomap_writepages(struct iomap_writepage_ctx *wpc)
2071 {
2072 struct address_space *mapping = wpc->inode->i_mapping;
2073 struct folio *folio = NULL;
2074 int error;
2075
2076 /*
2077 * Writeback from reclaim context should never happen except in the case
2078 * of a VM regression so warn about it and refuse to write the data.
2079 */
2080 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC | PF_KSWAPD)) ==
2081 PF_MEMALLOC))
2082 return -EIO;
2083
2084 while ((folio = writeback_iter(mapping, wpc->wbc, folio, &error))) {
2085 error = iomap_writeback_folio(wpc, folio);
2086 folio_unlock(folio);
2087 }
2088
2089 /*
2090 * If @error is non-zero, it means that we have a situation where some
2091 * part of the submission process has failed after we've marked pages
2092 * for writeback.
2093 *
2094 * We cannot cancel the writeback directly in that case, so always call
2095 * ->writeback_submit to run the I/O completion handler to clear the
2096 * writeback bit and let the file system proess the errors.
2097 */
2098 if (wpc->wb_ctx)
2099 return wpc->ops->writeback_submit(wpc, error);
2100 return error;
2101 }
2102 EXPORT_SYMBOL_GPL(iomap_writepages);
2103