Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2010 Red Hat, Inc. |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 4 | * Copyright (C) 2016-2019 Christoph Hellwig. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/compiler.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/iomap.h> |
| 10 | #include <linux/pagemap.h> |
| 11 | #include <linux/uio.h> |
| 12 | #include <linux/buffer_head.h> |
| 13 | #include <linux/dax.h> |
| 14 | #include <linux/writeback.h> |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 15 | #include <linux/list_sort.h> |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 16 | #include <linux/swap.h> |
| 17 | #include <linux/bio.h> |
| 18 | #include <linux/sched/signal.h> |
| 19 | #include <linux/migrate.h> |
Christoph Hellwig | 9e91c57 | 2019-10-17 13:12:13 -0700 | [diff] [blame] | 20 | #include "trace.h" |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 21 | |
| 22 | #include "../internal.h" |
| 23 | |
Christoph Hellwig | ab08b01 | 2019-10-17 13:12:19 -0700 | [diff] [blame] | 24 | /* |
Matthew Wilcox (Oracle) | 95c4cd0 | 2021-04-27 22:56:17 -0400 | [diff] [blame] | 25 | * Structure allocated for each folio when block size < folio size |
| 26 | * to track sub-folio uptodate status and I/O completions. |
Christoph Hellwig | ab08b01 | 2019-10-17 13:12:19 -0700 | [diff] [blame] | 27 | */ |
| 28 | struct iomap_page { |
Matthew Wilcox (Oracle) | 7d63667 | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 29 | atomic_t read_bytes_pending; |
Matthew Wilcox (Oracle) | 0fb2d72 | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 30 | atomic_t write_bytes_pending; |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 31 | spinlock_t uptodate_lock; |
Matthew Wilcox (Oracle) | 0a195b9 | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 32 | unsigned long uptodate[]; |
Christoph Hellwig | ab08b01 | 2019-10-17 13:12:19 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Matthew Wilcox (Oracle) | 95c4cd0 | 2021-04-27 22:56:17 -0400 | [diff] [blame] | 35 | static inline struct iomap_page *to_iomap_page(struct folio *folio) |
Christoph Hellwig | ab08b01 | 2019-10-17 13:12:19 -0700 | [diff] [blame] | 36 | { |
Matthew Wilcox (Oracle) | 95c4cd0 | 2021-04-27 22:56:17 -0400 | [diff] [blame] | 37 | if (folio_test_private(folio)) |
| 38 | return folio_get_private(folio); |
Christoph Hellwig | ab08b01 | 2019-10-17 13:12:19 -0700 | [diff] [blame] | 39 | return NULL; |
| 40 | } |
| 41 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 42 | static struct bio_set iomap_ioend_bioset; |
| 43 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 44 | static struct iomap_page * |
Matthew Wilcox (Oracle) | 435d44b | 2021-04-27 23:12:52 -0400 | [diff] [blame] | 45 | iomap_page_create(struct inode *inode, struct folio *folio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 46 | { |
Matthew Wilcox (Oracle) | 95c4cd0 | 2021-04-27 22:56:17 -0400 | [diff] [blame] | 47 | struct iomap_page *iop = to_iomap_page(folio); |
Matthew Wilcox (Oracle) | 435d44b | 2021-04-27 23:12:52 -0400 | [diff] [blame] | 48 | unsigned int nr_blocks = i_blocks_per_folio(inode, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 49 | |
Matthew Wilcox (Oracle) | 0a195b9 | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 50 | if (iop || nr_blocks <= 1) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 51 | return iop; |
| 52 | |
Matthew Wilcox (Oracle) | 0a195b9 | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 53 | iop = kzalloc(struct_size(iop, uptodate, BITS_TO_LONGS(nr_blocks)), |
| 54 | GFP_NOFS | __GFP_NOFAIL); |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 55 | spin_lock_init(&iop->uptodate_lock); |
Matthew Wilcox (Oracle) | 435d44b | 2021-04-27 23:12:52 -0400 | [diff] [blame] | 56 | if (folio_test_uptodate(folio)) |
Matthew Wilcox (Oracle) | 4595a29 | 2020-09-25 11:16:53 -0700 | [diff] [blame] | 57 | bitmap_fill(iop->uptodate, nr_blocks); |
Matthew Wilcox (Oracle) | 435d44b | 2021-04-27 23:12:52 -0400 | [diff] [blame] | 58 | folio_attach_private(folio, iop); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 59 | return iop; |
| 60 | } |
| 61 | |
Matthew Wilcox (Oracle) | c46e832 | 2021-04-27 23:22:22 -0400 | [diff] [blame] | 62 | static void iomap_page_release(struct folio *folio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 63 | { |
Matthew Wilcox (Oracle) | c46e832 | 2021-04-27 23:22:22 -0400 | [diff] [blame] | 64 | struct iomap_page *iop = folio_detach_private(folio); |
| 65 | struct inode *inode = folio->mapping->host; |
| 66 | unsigned int nr_blocks = i_blocks_per_folio(inode, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 67 | |
| 68 | if (!iop) |
| 69 | return; |
Matthew Wilcox (Oracle) | 7d63667 | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 70 | WARN_ON_ONCE(atomic_read(&iop->read_bytes_pending)); |
Matthew Wilcox (Oracle) | 0fb2d72 | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 71 | WARN_ON_ONCE(atomic_read(&iop->write_bytes_pending)); |
Matthew Wilcox (Oracle) | 0a195b9 | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 72 | WARN_ON_ONCE(bitmap_full(iop->uptodate, nr_blocks) != |
Matthew Wilcox (Oracle) | c46e832 | 2021-04-27 23:22:22 -0400 | [diff] [blame] | 73 | folio_test_uptodate(folio)); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 74 | kfree(iop); |
| 75 | } |
| 76 | |
| 77 | /* |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 78 | * Calculate the range inside the folio that we actually need to read. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 79 | */ |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 80 | static void iomap_adjust_read_range(struct inode *inode, struct folio *folio, |
| 81 | loff_t *pos, loff_t length, size_t *offp, size_t *lenp) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 82 | { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 83 | struct iomap_page *iop = to_iomap_page(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 84 | loff_t orig_pos = *pos; |
| 85 | loff_t isize = i_size_read(inode); |
| 86 | unsigned block_bits = inode->i_blkbits; |
| 87 | unsigned block_size = (1 << block_bits); |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 88 | size_t poff = offset_in_folio(folio, *pos); |
| 89 | size_t plen = min_t(loff_t, folio_size(folio) - poff, length); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 90 | unsigned first = poff >> block_bits; |
| 91 | unsigned last = (poff + plen - 1) >> block_bits; |
| 92 | |
| 93 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 94 | * If the block size is smaller than the page size, we need to check the |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 95 | * per-block uptodate status and adjust the offset and length if needed |
| 96 | * to avoid reading in already uptodate ranges. |
| 97 | */ |
| 98 | if (iop) { |
| 99 | unsigned int i; |
| 100 | |
| 101 | /* move forward for each leading block marked uptodate */ |
| 102 | for (i = first; i <= last; i++) { |
| 103 | if (!test_bit(i, iop->uptodate)) |
| 104 | break; |
| 105 | *pos += block_size; |
| 106 | poff += block_size; |
| 107 | plen -= block_size; |
| 108 | first++; |
| 109 | } |
| 110 | |
| 111 | /* truncate len if we find any trailing uptodate block(s) */ |
| 112 | for ( ; i <= last; i++) { |
| 113 | if (test_bit(i, iop->uptodate)) { |
| 114 | plen -= (last - i + 1) * block_size; |
| 115 | last = i - 1; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 122 | * If the extent spans the block that contains the i_size, we need to |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 123 | * handle both halves separately so that we properly zero data in the |
| 124 | * page cache for blocks that are entirely outside of i_size. |
| 125 | */ |
| 126 | if (orig_pos <= isize && orig_pos + length > isize) { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 127 | unsigned end = offset_in_folio(folio, isize - 1) >> block_bits; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 128 | |
| 129 | if (first <= end && last > end) |
| 130 | plen -= (last - end) * block_size; |
| 131 | } |
| 132 | |
| 133 | *offp = poff; |
| 134 | *lenp = plen; |
| 135 | } |
| 136 | |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 137 | static void iomap_iop_set_range_uptodate(struct folio *folio, |
| 138 | struct iomap_page *iop, size_t off, size_t len) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 139 | { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 140 | struct inode *inode = folio->mapping->host; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 141 | unsigned first = off >> inode->i_blkbits; |
| 142 | unsigned last = (off + len - 1) >> inode->i_blkbits; |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 143 | unsigned long flags; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 144 | |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 145 | spin_lock_irqsave(&iop->uptodate_lock, flags); |
Matthew Wilcox (Oracle) | b21866f | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 146 | bitmap_set(iop->uptodate, first, last - first + 1); |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 147 | if (bitmap_full(iop->uptodate, i_blocks_per_folio(inode, folio))) |
| 148 | folio_mark_uptodate(folio); |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 149 | spin_unlock_irqrestore(&iop->uptodate_lock, flags); |
| 150 | } |
| 151 | |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 152 | static void iomap_set_range_uptodate(struct folio *folio, |
| 153 | struct iomap_page *iop, size_t off, size_t len) |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 154 | { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 155 | if (folio_test_error(folio)) |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 156 | return; |
| 157 | |
Matthew Wilcox (Oracle) | cd1e5af | 2021-04-28 08:16:50 -0400 | [diff] [blame] | 158 | if (iop) |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 159 | iomap_iop_set_range_uptodate(folio, iop, off, len); |
Christoph Hellwig | 1cea335 | 2019-12-04 09:33:52 -0800 | [diff] [blame] | 160 | else |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 161 | folio_mark_uptodate(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 164 | static void iomap_finish_folio_read(struct folio *folio, size_t offset, |
| 165 | size_t len, int error) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 166 | { |
Matthew Wilcox (Oracle) | 95c4cd0 | 2021-04-27 22:56:17 -0400 | [diff] [blame] | 167 | struct iomap_page *iop = to_iomap_page(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 168 | |
| 169 | if (unlikely(error)) { |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 170 | folio_clear_uptodate(folio); |
| 171 | folio_set_error(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 172 | } else { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 173 | iomap_set_range_uptodate(folio, iop, offset, len); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 176 | if (!iop || atomic_sub_and_test(len, &iop->read_bytes_pending)) |
| 177 | folio_unlock(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 180 | static void iomap_read_end_io(struct bio *bio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 181 | { |
| 182 | int error = blk_status_to_errno(bio->bi_status); |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 183 | struct folio_iter fi; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 184 | |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 185 | bio_for_each_folio_all(fi, bio) |
| 186 | iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 187 | bio_put(bio); |
| 188 | } |
| 189 | |
| 190 | struct iomap_readpage_ctx { |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 191 | struct folio *cur_folio; |
| 192 | bool cur_folio_in_bio; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 193 | struct bio *bio; |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 194 | struct readahead_control *rac; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Andreas Gruenbacher | 5ad448c | 2021-11-24 10:15:47 -0800 | [diff] [blame] | 197 | /** |
| 198 | * iomap_read_inline_data - copy inline data into the page cache |
| 199 | * @iter: iteration structure |
Matthew Wilcox (Oracle) | 874628a | 2021-07-23 23:24:50 -0400 | [diff] [blame] | 200 | * @folio: folio to copy to |
Andreas Gruenbacher | 5ad448c | 2021-11-24 10:15:47 -0800 | [diff] [blame] | 201 | * |
Matthew Wilcox (Oracle) | 874628a | 2021-07-23 23:24:50 -0400 | [diff] [blame] | 202 | * Copy the inline data in @iter into @folio and zero out the rest of the folio. |
Andreas Gruenbacher | 5ad448c | 2021-11-24 10:15:47 -0800 | [diff] [blame] | 203 | * Only a single IOMAP_INLINE extent is allowed at the end of each file. |
| 204 | * Returns zero for success to complete the read, or the usual negative errno. |
| 205 | */ |
| 206 | static int iomap_read_inline_data(const struct iomap_iter *iter, |
Matthew Wilcox (Oracle) | 874628a | 2021-07-23 23:24:50 -0400 | [diff] [blame] | 207 | struct folio *folio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 208 | { |
Matthew Wilcox (Oracle) | cd1e5af | 2021-04-28 08:16:50 -0400 | [diff] [blame] | 209 | struct iomap_page *iop; |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 210 | const struct iomap *iomap = iomap_iter_srcmap(iter); |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 211 | size_t size = i_size_read(iter->inode) - iomap->offset; |
Matthew Wilcox (Oracle) | b405435 | 2021-08-02 14:45:57 -0700 | [diff] [blame] | 212 | size_t poff = offset_in_page(iomap->offset); |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 213 | size_t offset = offset_in_folio(folio, iomap->offset); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 214 | void *addr; |
| 215 | |
Matthew Wilcox (Oracle) | 874628a | 2021-07-23 23:24:50 -0400 | [diff] [blame] | 216 | if (folio_test_uptodate(folio)) |
Andreas Gruenbacher | 5ad448c | 2021-11-24 10:15:47 -0800 | [diff] [blame] | 217 | return 0; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 218 | |
Matthew Wilcox (Oracle) | ae44f9c | 2021-08-04 20:07:34 -0700 | [diff] [blame] | 219 | if (WARN_ON_ONCE(size > PAGE_SIZE - poff)) |
| 220 | return -EIO; |
Gao Xiang | 69f4a26 | 2021-08-03 09:38:22 -0700 | [diff] [blame] | 221 | if (WARN_ON_ONCE(size > PAGE_SIZE - |
| 222 | offset_in_page(iomap->inline_data))) |
| 223 | return -EIO; |
| 224 | if (WARN_ON_ONCE(size > iomap->length)) |
| 225 | return -EIO; |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 226 | if (offset > 0) |
Matthew Wilcox (Oracle) | cd1e5af | 2021-04-28 08:16:50 -0400 | [diff] [blame] | 227 | iop = iomap_page_create(iter->inode, folio); |
| 228 | else |
| 229 | iop = to_iomap_page(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 230 | |
Matthew Wilcox (Oracle) | 874628a | 2021-07-23 23:24:50 -0400 | [diff] [blame] | 231 | addr = kmap_local_folio(folio, offset); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 232 | memcpy(addr, iomap->inline_data, size); |
Matthew Wilcox (Oracle) | b405435 | 2021-08-02 14:45:57 -0700 | [diff] [blame] | 233 | memset(addr + size, 0, PAGE_SIZE - poff - size); |
Matthew Wilcox (Oracle) | ab069d5 | 2021-08-04 20:07:33 -0700 | [diff] [blame] | 234 | kunmap_local(addr); |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 235 | iomap_set_range_uptodate(folio, iop, offset, PAGE_SIZE - poff); |
Andreas Gruenbacher | 5ad448c | 2021-11-24 10:15:47 -0800 | [diff] [blame] | 236 | return 0; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 239 | static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter, |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 240 | loff_t pos) |
Christoph Hellwig | 009d8d8 | 2019-10-17 13:12:12 -0700 | [diff] [blame] | 241 | { |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 242 | const struct iomap *srcmap = iomap_iter_srcmap(iter); |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 243 | |
| 244 | return srcmap->type != IOMAP_MAPPED || |
| 245 | (srcmap->flags & IOMAP_F_NEW) || |
| 246 | pos >= i_size_read(iter->inode); |
Christoph Hellwig | 009d8d8 | 2019-10-17 13:12:12 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 249 | static loff_t iomap_readpage_iter(const struct iomap_iter *iter, |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 250 | struct iomap_readpage_ctx *ctx, loff_t offset) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 251 | { |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 252 | const struct iomap *iomap = &iter->iomap; |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 253 | loff_t pos = iter->pos + offset; |
| 254 | loff_t length = iomap_length(iter) - offset; |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 255 | struct folio *folio = ctx->cur_folio; |
Andreas Gruenbacher | 637d337 | 2021-07-15 09:58:05 -0700 | [diff] [blame] | 256 | struct iomap_page *iop; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 257 | loff_t orig_pos = pos; |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 258 | size_t poff, plen; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 259 | sector_t sector; |
| 260 | |
Andreas Gruenbacher | 5ad448c | 2021-11-24 10:15:47 -0800 | [diff] [blame] | 261 | if (iomap->type == IOMAP_INLINE) |
Matthew Wilcox (Oracle) | 874628a | 2021-07-23 23:24:50 -0400 | [diff] [blame] | 262 | return iomap_read_inline_data(iter, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 263 | |
| 264 | /* zero post-eof blocks as the page may be mapped */ |
Matthew Wilcox (Oracle) | 435d44b | 2021-04-27 23:12:52 -0400 | [diff] [blame] | 265 | iop = iomap_page_create(iter->inode, folio); |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 266 | iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 267 | if (plen == 0) |
| 268 | goto done; |
| 269 | |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 270 | if (iomap_block_needs_zeroing(iter, pos)) { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 271 | folio_zero_range(folio, poff, plen); |
| 272 | iomap_set_range_uptodate(folio, iop, poff, plen); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 273 | goto done; |
| 274 | } |
| 275 | |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 276 | ctx->cur_folio_in_bio = true; |
Matthew Wilcox (Oracle) | 7d63667 | 2020-09-21 08:58:40 -0700 | [diff] [blame] | 277 | if (iop) |
| 278 | atomic_add(plen, &iop->read_bytes_pending); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 279 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 280 | sector = iomap_sector(iomap, pos); |
Christoph Hellwig | d0364f9 | 2021-08-02 14:43:43 -0700 | [diff] [blame] | 281 | if (!ctx->bio || |
| 282 | bio_end_sector(ctx->bio) != sector || |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 283 | !bio_add_folio(ctx->bio, folio, plen, poff)) { |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 284 | gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL); |
Matthew Wilcox (Oracle) | 457df33 | 2020-04-02 09:08:53 -0700 | [diff] [blame] | 285 | gfp_t orig_gfp = gfp; |
Matthew Wilcox (Oracle) | 5f7136d | 2021-01-29 04:38:57 +0000 | [diff] [blame] | 286 | unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 287 | |
| 288 | if (ctx->bio) |
| 289 | submit_bio(ctx->bio); |
| 290 | |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 291 | if (ctx->rac) /* same as readahead_gfp_mask */ |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 292 | gfp |= __GFP_NORETRY | __GFP_NOWARN; |
Matthew Wilcox (Oracle) | 5f7136d | 2021-01-29 04:38:57 +0000 | [diff] [blame] | 293 | ctx->bio = bio_alloc(gfp, bio_max_segs(nr_vecs)); |
Matthew Wilcox (Oracle) | 457df33 | 2020-04-02 09:08:53 -0700 | [diff] [blame] | 294 | /* |
| 295 | * If the bio_alloc fails, try it again for a single page to |
| 296 | * avoid having to deal with partial page reads. This emulates |
| 297 | * what do_mpage_readpage does. |
| 298 | */ |
| 299 | if (!ctx->bio) |
| 300 | ctx->bio = bio_alloc(orig_gfp, 1); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 301 | ctx->bio->bi_opf = REQ_OP_READ; |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 302 | if (ctx->rac) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 303 | ctx->bio->bi_opf |= REQ_RAHEAD; |
| 304 | ctx->bio->bi_iter.bi_sector = sector; |
| 305 | bio_set_dev(ctx->bio, iomap->bdev); |
| 306 | ctx->bio->bi_end_io = iomap_read_end_io; |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 307 | bio_add_folio(ctx->bio, folio, plen, poff); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 308 | } |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 309 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 310 | done: |
| 311 | /* |
| 312 | * Move the caller beyond our range so that it keeps making progress. |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 313 | * For that, we have to include any leading non-uptodate ranges, but |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 314 | * we can skip trailing ones as they will be handled in the next |
| 315 | * iteration. |
| 316 | */ |
| 317 | return pos - orig_pos + plen; |
| 318 | } |
| 319 | |
| 320 | int |
| 321 | iomap_readpage(struct page *page, const struct iomap_ops *ops) |
| 322 | { |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 323 | struct folio *folio = page_folio(page); |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 324 | struct iomap_iter iter = { |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 325 | .inode = folio->mapping->host, |
| 326 | .pos = folio_pos(folio), |
| 327 | .len = folio_size(folio), |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 328 | }; |
| 329 | struct iomap_readpage_ctx ctx = { |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 330 | .cur_folio = folio, |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 331 | }; |
| 332 | int ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 333 | |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 334 | trace_iomap_readpage(iter.inode, 1); |
Christoph Hellwig | 9e91c57 | 2019-10-17 13:12:13 -0700 | [diff] [blame] | 335 | |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 336 | while ((ret = iomap_iter(&iter, ops)) > 0) |
| 337 | iter.processed = iomap_readpage_iter(&iter, &ctx, 0); |
| 338 | |
| 339 | if (ret < 0) |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 340 | folio_set_error(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 341 | |
| 342 | if (ctx.bio) { |
| 343 | submit_bio(ctx.bio); |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 344 | WARN_ON_ONCE(!ctx.cur_folio_in_bio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 345 | } else { |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 346 | WARN_ON_ONCE(ctx.cur_folio_in_bio); |
| 347 | folio_unlock(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 351 | * Just like mpage_readahead and block_read_full_page, we always |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 352 | * return 0 and just mark the page as PageError on errors. This |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 353 | * should be cleaned up throughout the stack eventually. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 354 | */ |
| 355 | return 0; |
| 356 | } |
| 357 | EXPORT_SYMBOL_GPL(iomap_readpage); |
| 358 | |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 359 | static loff_t iomap_readahead_iter(const struct iomap_iter *iter, |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 360 | struct iomap_readpage_ctx *ctx) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 361 | { |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 362 | loff_t length = iomap_length(iter); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 363 | loff_t done, ret; |
| 364 | |
| 365 | for (done = 0; done < length; done += ret) { |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 366 | if (ctx->cur_folio && |
| 367 | offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) { |
| 368 | if (!ctx->cur_folio_in_bio) |
| 369 | folio_unlock(ctx->cur_folio); |
| 370 | ctx->cur_folio = NULL; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 371 | } |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 372 | if (!ctx->cur_folio) { |
| 373 | ctx->cur_folio = readahead_folio(ctx->rac); |
| 374 | ctx->cur_folio_in_bio = false; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 375 | } |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 376 | ret = iomap_readpage_iter(iter, ctx, done); |
Andreas Gruenbacher | d8af404 | 2021-11-17 17:59:01 -0800 | [diff] [blame] | 377 | if (ret <= 0) |
| 378 | return ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | return done; |
| 382 | } |
| 383 | |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 384 | /** |
| 385 | * iomap_readahead - Attempt to read pages from a file. |
| 386 | * @rac: Describes the pages to be read. |
| 387 | * @ops: The operations vector for the filesystem. |
| 388 | * |
| 389 | * This function is for filesystems to call to implement their readahead |
| 390 | * address_space operation. |
| 391 | * |
| 392 | * Context: The @ops callbacks may submit I/O (eg to read the addresses of |
| 393 | * blocks from disc), and may wait for it. The caller may be trying to |
| 394 | * access a different page, and so sleeping excessively should be avoided. |
| 395 | * It may allocate memory, but should avoid costly allocations. This |
| 396 | * function is called with memalloc_nofs set, so allocations will not cause |
| 397 | * the filesystem to be reentered. |
| 398 | */ |
| 399 | void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 400 | { |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 401 | struct iomap_iter iter = { |
| 402 | .inode = rac->mapping->host, |
| 403 | .pos = readahead_pos(rac), |
| 404 | .len = readahead_length(rac), |
| 405 | }; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 406 | struct iomap_readpage_ctx ctx = { |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 407 | .rac = rac, |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 408 | }; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 409 | |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 410 | trace_iomap_readahead(rac->mapping->host, readahead_count(rac)); |
Christoph Hellwig | 9e91c57 | 2019-10-17 13:12:13 -0700 | [diff] [blame] | 411 | |
Christoph Hellwig | f6d48000 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 412 | while (iomap_iter(&iter, ops) > 0) |
| 413 | iter.processed = iomap_readahead_iter(&iter, &ctx); |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 414 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 415 | if (ctx.bio) |
| 416 | submit_bio(ctx.bio); |
Matthew Wilcox (Oracle) | 3aa9c65 | 2021-04-28 09:39:51 -0400 | [diff] [blame] | 417 | if (ctx.cur_folio) { |
| 418 | if (!ctx.cur_folio_in_bio) |
| 419 | folio_unlock(ctx.cur_folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 420 | } |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 421 | } |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 422 | EXPORT_SYMBOL_GPL(iomap_readahead); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * iomap_is_partially_uptodate checks whether blocks within a page are |
| 426 | * uptodate or not. |
| 427 | * |
| 428 | * Returns true if all blocks which correspond to a file portion |
| 429 | * we want to read within the page are uptodate. |
| 430 | */ |
| 431 | int |
| 432 | iomap_is_partially_uptodate(struct page *page, unsigned long from, |
| 433 | unsigned long count) |
| 434 | { |
Matthew Wilcox (Oracle) | 95c4cd0 | 2021-04-27 22:56:17 -0400 | [diff] [blame] | 435 | struct folio *folio = page_folio(page); |
| 436 | struct iomap_page *iop = to_iomap_page(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 437 | struct inode *inode = page->mapping->host; |
| 438 | unsigned len, first, last; |
| 439 | unsigned i; |
| 440 | |
| 441 | /* Limit range to one page */ |
| 442 | len = min_t(unsigned, PAGE_SIZE - from, count); |
| 443 | |
| 444 | /* First and last blocks in range within page */ |
| 445 | first = from >> inode->i_blkbits; |
| 446 | last = (from + len - 1) >> inode->i_blkbits; |
| 447 | |
| 448 | if (iop) { |
| 449 | for (i = first; i <= last; i++) |
| 450 | if (!test_bit(i, iop->uptodate)) |
| 451 | return 0; |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate); |
| 458 | |
| 459 | int |
| 460 | iomap_releasepage(struct page *page, gfp_t gfp_mask) |
| 461 | { |
Matthew Wilcox (Oracle) | c46e832 | 2021-04-27 23:22:22 -0400 | [diff] [blame] | 462 | struct folio *folio = page_folio(page); |
| 463 | |
Matthew Wilcox (Oracle) | 39f16c8 | 2021-04-28 07:51:36 -0400 | [diff] [blame] | 464 | trace_iomap_releasepage(folio->mapping->host, folio_pos(folio), |
| 465 | folio_size(folio)); |
Christoph Hellwig | 9e91c57 | 2019-10-17 13:12:13 -0700 | [diff] [blame] | 466 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 467 | /* |
| 468 | * mm accommodates an old ext3 case where clean pages might not have had |
| 469 | * the dirty bit cleared. Thus, it can send actual dirty pages to |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 470 | * ->releasepage() via shrink_active_list(); skip those here. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 471 | */ |
Matthew Wilcox (Oracle) | 39f16c8 | 2021-04-28 07:51:36 -0400 | [diff] [blame] | 472 | if (folio_test_dirty(folio) || folio_test_writeback(folio)) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 473 | return 0; |
Matthew Wilcox (Oracle) | c46e832 | 2021-04-27 23:22:22 -0400 | [diff] [blame] | 474 | iomap_page_release(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 475 | return 1; |
| 476 | } |
| 477 | EXPORT_SYMBOL_GPL(iomap_releasepage); |
| 478 | |
Matthew Wilcox (Oracle) | 8306a5f | 2021-04-28 07:51:36 -0400 | [diff] [blame] | 479 | void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 480 | { |
Matthew Wilcox (Oracle) | 8306a5f | 2021-04-28 07:51:36 -0400 | [diff] [blame] | 481 | trace_iomap_invalidatepage(folio->mapping->host, offset, len); |
Christoph Hellwig | 9e91c57 | 2019-10-17 13:12:13 -0700 | [diff] [blame] | 482 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 483 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 484 | * If we're invalidating the entire page, clear the dirty state from it |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 485 | * and release it to avoid unnecessary buildup of the LRU. |
| 486 | */ |
Matthew Wilcox (Oracle) | 8306a5f | 2021-04-28 07:51:36 -0400 | [diff] [blame] | 487 | if (offset == 0 && len == folio_size(folio)) { |
| 488 | WARN_ON_ONCE(folio_test_writeback(folio)); |
| 489 | folio_cancel_dirty(folio); |
Matthew Wilcox (Oracle) | c46e832 | 2021-04-27 23:22:22 -0400 | [diff] [blame] | 490 | iomap_page_release(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
Matthew Wilcox (Oracle) | 8306a5f | 2021-04-28 07:51:36 -0400 | [diff] [blame] | 493 | EXPORT_SYMBOL_GPL(iomap_invalidate_folio); |
| 494 | |
| 495 | void iomap_invalidatepage(struct page *page, unsigned int offset, |
| 496 | unsigned int len) |
| 497 | { |
| 498 | iomap_invalidate_folio(page_folio(page), offset, len); |
| 499 | } |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 500 | EXPORT_SYMBOL_GPL(iomap_invalidatepage); |
| 501 | |
| 502 | #ifdef CONFIG_MIGRATION |
| 503 | int |
| 504 | iomap_migrate_page(struct address_space *mapping, struct page *newpage, |
| 505 | struct page *page, enum migrate_mode mode) |
| 506 | { |
| 507 | int ret; |
| 508 | |
Linus Torvalds | 26473f8 | 2019-07-19 11:38:12 -0700 | [diff] [blame] | 509 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 510 | if (ret != MIGRATEPAGE_SUCCESS) |
| 511 | return ret; |
| 512 | |
Guoqing Jiang | 58aeb73 | 2020-06-01 21:47:54 -0700 | [diff] [blame] | 513 | if (page_has_private(page)) |
| 514 | attach_page_private(newpage, detach_page_private(page)); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 515 | |
| 516 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 517 | migrate_page_copy(newpage, page); |
| 518 | else |
| 519 | migrate_page_states(newpage, page); |
| 520 | return MIGRATEPAGE_SUCCESS; |
| 521 | } |
| 522 | EXPORT_SYMBOL_GPL(iomap_migrate_page); |
| 523 | #endif /* CONFIG_MIGRATION */ |
| 524 | |
| 525 | static void |
| 526 | iomap_write_failed(struct inode *inode, loff_t pos, unsigned len) |
| 527 | { |
| 528 | loff_t i_size = i_size_read(inode); |
| 529 | |
| 530 | /* |
| 531 | * Only truncate newly allocated pages beyoned EOF, even if the |
| 532 | * write started inside the existing inode size. |
| 533 | */ |
| 534 | if (pos + len > i_size) |
| 535 | truncate_pagecache_range(inode, max(pos, i_size), pos + len); |
| 536 | } |
| 537 | |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 538 | static int iomap_read_folio_sync(loff_t block_start, struct folio *folio, |
| 539 | size_t poff, size_t plen, const struct iomap *iomap) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 540 | { |
| 541 | struct bio_vec bvec; |
| 542 | struct bio bio; |
| 543 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 544 | bio_init(&bio, &bvec, 1); |
| 545 | bio.bi_opf = REQ_OP_READ; |
| 546 | bio.bi_iter.bi_sector = iomap_sector(iomap, block_start); |
| 547 | bio_set_dev(&bio, iomap->bdev); |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 548 | bio_add_folio(&bio, folio, plen, poff); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 549 | return submit_bio_wait(&bio); |
| 550 | } |
| 551 | |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 552 | static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos, |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 553 | size_t len, struct folio *folio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 554 | { |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 555 | const struct iomap *srcmap = iomap_iter_srcmap(iter); |
Matthew Wilcox (Oracle) | 435d44b | 2021-04-27 23:12:52 -0400 | [diff] [blame] | 556 | struct iomap_page *iop = iomap_page_create(iter->inode, folio); |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 557 | loff_t block_size = i_blocksize(iter->inode); |
Nikolay Borisov | 6cc19c5 | 2020-09-10 08:38:06 -0700 | [diff] [blame] | 558 | loff_t block_start = round_down(pos, block_size); |
| 559 | loff_t block_end = round_up(pos + len, block_size); |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 560 | size_t from = offset_in_folio(folio, pos), to = from + len; |
| 561 | size_t poff, plen; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 562 | |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 563 | if (folio_test_uptodate(folio)) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 564 | return 0; |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 565 | folio_clear_error(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 566 | |
| 567 | do { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 568 | iomap_adjust_read_range(iter->inode, folio, &block_start, |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 569 | block_end - block_start, &poff, &plen); |
| 570 | if (plen == 0) |
| 571 | break; |
| 572 | |
Christoph Hellwig | b74b129 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 573 | if (!(iter->flags & IOMAP_UNSHARE) && |
Christoph Hellwig | 32a38a4 | 2019-10-18 16:42:50 -0700 | [diff] [blame] | 574 | (from <= poff || from >= poff + plen) && |
Christoph Hellwig | d3b4043 | 2019-10-18 16:42:24 -0700 | [diff] [blame] | 575 | (to <= poff || to >= poff + plen)) |
| 576 | continue; |
| 577 | |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 578 | if (iomap_block_needs_zeroing(iter, block_start)) { |
Christoph Hellwig | b74b129 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 579 | if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE)) |
Christoph Hellwig | 32a38a4 | 2019-10-18 16:42:50 -0700 | [diff] [blame] | 580 | return -EIO; |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 581 | folio_zero_segments(folio, poff, from, to, poff + plen); |
Matthew Wilcox (Oracle) | 14284fe | 2020-09-10 08:26:18 -0700 | [diff] [blame] | 582 | } else { |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 583 | int status = iomap_read_folio_sync(block_start, folio, |
Matthew Wilcox (Oracle) | 14284fe | 2020-09-10 08:26:18 -0700 | [diff] [blame] | 584 | poff, plen, srcmap); |
| 585 | if (status) |
| 586 | return status; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 587 | } |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 588 | iomap_set_range_uptodate(folio, iop, poff, plen); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 589 | } while ((block_start += plen) < block_end); |
| 590 | |
Christoph Hellwig | d3b4043 | 2019-10-18 16:42:24 -0700 | [diff] [blame] | 591 | return 0; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 594 | static int iomap_write_begin_inline(const struct iomap_iter *iter, |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 595 | struct folio *folio) |
Gao Xiang | 69f4a26 | 2021-08-03 09:38:22 -0700 | [diff] [blame] | 596 | { |
| 597 | /* needs more work for the tailpacking case; disable for now */ |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 598 | if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0)) |
Gao Xiang | 69f4a26 | 2021-08-03 09:38:22 -0700 | [diff] [blame] | 599 | return -EIO; |
Matthew Wilcox (Oracle) | 874628a | 2021-07-23 23:24:50 -0400 | [diff] [blame] | 600 | return iomap_read_inline_data(iter, folio); |
Gao Xiang | 69f4a26 | 2021-08-03 09:38:22 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 603 | static int iomap_write_begin(const struct iomap_iter *iter, loff_t pos, |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 604 | size_t len, struct folio **foliop) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 605 | { |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 606 | const struct iomap_page_ops *page_ops = iter->iomap.page_ops; |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 607 | const struct iomap *srcmap = iomap_iter_srcmap(iter); |
Matthew Wilcox (Oracle) | d1bd0b4 | 2021-11-03 14:05:47 -0400 | [diff] [blame] | 608 | struct folio *folio; |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 609 | unsigned fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE | FGP_NOFS; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 610 | int status = 0; |
| 611 | |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 612 | BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length); |
| 613 | if (srcmap != &iter->iomap) |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 614 | BUG_ON(pos + len > srcmap->offset + srcmap->length); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 615 | |
| 616 | if (fatal_signal_pending(current)) |
| 617 | return -EINTR; |
| 618 | |
Matthew Wilcox (Oracle) | d454ab8 | 2021-12-09 15:47:44 -0500 | [diff] [blame] | 619 | if (!mapping_large_folio_support(iter->inode->i_mapping)) |
| 620 | len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos)); |
| 621 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 622 | if (page_ops && page_ops->page_prepare) { |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 623 | status = page_ops->page_prepare(iter->inode, pos, len); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 624 | if (status) |
| 625 | return status; |
| 626 | } |
| 627 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 628 | folio = __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT, |
| 629 | fgp, mapping_gfp_mask(iter->inode->i_mapping)); |
| 630 | if (!folio) { |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 631 | status = -ENOMEM; |
| 632 | goto out_no_page; |
| 633 | } |
Matthew Wilcox (Oracle) | d454ab8 | 2021-12-09 15:47:44 -0500 | [diff] [blame] | 634 | if (pos + len > folio_pos(folio) + folio_size(folio)) |
| 635 | len = folio_pos(folio) + folio_size(folio) - pos; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 636 | |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 637 | if (srcmap->type == IOMAP_INLINE) |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 638 | status = iomap_write_begin_inline(iter, folio); |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 639 | else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) |
Matthew Wilcox (Oracle) | d1bd0b4 | 2021-11-03 14:05:47 -0400 | [diff] [blame] | 640 | status = __block_write_begin_int(folio, pos, len, NULL, srcmap); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 641 | else |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 642 | status = __iomap_write_begin(iter, pos, len, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 643 | |
| 644 | if (unlikely(status)) |
| 645 | goto out_unlock; |
| 646 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 647 | *foliop = folio; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | |
| 650 | out_unlock: |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 651 | folio_unlock(folio); |
| 652 | folio_put(folio); |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 653 | iomap_write_failed(iter->inode, pos, len); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 654 | |
| 655 | out_no_page: |
| 656 | if (page_ops && page_ops->page_done) |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 657 | page_ops->page_done(iter->inode, pos, 0, NULL); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 658 | return status; |
| 659 | } |
| 660 | |
Matthew Wilcox (Oracle) | e25ba8c | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 661 | static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len, |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 662 | size_t copied, struct folio *folio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 663 | { |
Matthew Wilcox (Oracle) | cd1e5af | 2021-04-28 08:16:50 -0400 | [diff] [blame] | 664 | struct iomap_page *iop = to_iomap_page(folio); |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 665 | flush_dcache_folio(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 666 | |
| 667 | /* |
| 668 | * The blocks that were entirely written will now be uptodate, so we |
| 669 | * don't have to worry about a readpage reading them and overwriting a |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 670 | * partial write. However, if we've encountered a short write and only |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 671 | * partially written into a block, it will not be marked uptodate, so a |
| 672 | * readpage might come in and destroy our partial write. |
| 673 | * |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 674 | * Do the simplest thing and just treat any short write to a |
| 675 | * non-uptodate page as a zero-length write, and force the caller to |
| 676 | * redo the whole thing. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 677 | */ |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 678 | if (unlikely(copied < len && !folio_test_uptodate(folio))) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 679 | return 0; |
Matthew Wilcox (Oracle) | 431c056 | 2021-04-28 08:20:48 -0400 | [diff] [blame] | 680 | iomap_set_range_uptodate(folio, iop, offset_in_folio(folio, pos), len); |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 681 | filemap_dirty_folio(inode->i_mapping, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 682 | return copied; |
| 683 | } |
| 684 | |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 685 | static size_t iomap_write_end_inline(const struct iomap_iter *iter, |
| 686 | struct page *page, loff_t pos, size_t copied) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 687 | { |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 688 | const struct iomap *iomap = &iter->iomap; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 689 | void *addr; |
| 690 | |
| 691 | WARN_ON_ONCE(!PageUptodate(page)); |
Gao Xiang | 69f4a26 | 2021-08-03 09:38:22 -0700 | [diff] [blame] | 692 | BUG_ON(!iomap_inline_data_valid(iomap)); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 693 | |
Matthew Wilcox (Oracle) | 7ed3cd1 | 2020-09-21 08:58:38 -0700 | [diff] [blame] | 694 | flush_dcache_page(page); |
Matthew Wilcox (Oracle) | ab069d5 | 2021-08-04 20:07:33 -0700 | [diff] [blame] | 695 | addr = kmap_local_page(page) + pos; |
| 696 | memcpy(iomap_inline_data(iomap, pos), addr, copied); |
| 697 | kunmap_local(addr); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 698 | |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 699 | mark_inode_dirty(iter->inode); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 700 | return copied; |
| 701 | } |
| 702 | |
Matthew Wilcox (Oracle) | e25ba8c | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 703 | /* Returns the number of bytes copied. May be 0. Cannot be an errno. */ |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 704 | static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len, |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 705 | size_t copied, struct folio *folio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 706 | { |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 707 | const struct iomap_page_ops *page_ops = iter->iomap.page_ops; |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 708 | const struct iomap *srcmap = iomap_iter_srcmap(iter); |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 709 | loff_t old_size = iter->inode->i_size; |
Matthew Wilcox (Oracle) | e25ba8c | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 710 | size_t ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 711 | |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 712 | if (srcmap->type == IOMAP_INLINE) { |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 713 | ret = iomap_write_end_inline(iter, &folio->page, pos, copied); |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 714 | } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) { |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 715 | ret = block_write_end(NULL, iter->inode->i_mapping, pos, len, |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 716 | copied, &folio->page, NULL); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 717 | } else { |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 718 | ret = __iomap_write_end(iter->inode, pos, len, copied, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Update the in-memory inode size after copying the data into the page |
| 723 | * cache. It's up to the file system to write the updated size to disk, |
| 724 | * preferably after I/O completion so that no stale data is exposed. |
| 725 | */ |
| 726 | if (pos + ret > old_size) { |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 727 | i_size_write(iter->inode, pos + ret); |
| 728 | iter->iomap.flags |= IOMAP_F_SIZE_CHANGED; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 729 | } |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 730 | folio_unlock(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 731 | |
| 732 | if (old_size < pos) |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 733 | pagecache_isize_extended(iter->inode, old_size, pos); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 734 | if (page_ops && page_ops->page_done) |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 735 | page_ops->page_done(iter->inode, pos, ret, &folio->page); |
| 736 | folio_put(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 737 | |
| 738 | if (ret < len) |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 739 | iomap_write_failed(iter->inode, pos, len); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 740 | return ret; |
| 741 | } |
| 742 | |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 743 | static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 744 | { |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 745 | loff_t length = iomap_length(iter); |
| 746 | loff_t pos = iter->pos; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 747 | ssize_t written = 0; |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 748 | long status = 0; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 749 | |
| 750 | do { |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 751 | struct folio *folio; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 752 | struct page *page; |
| 753 | unsigned long offset; /* Offset into pagecache page */ |
| 754 | unsigned long bytes; /* Bytes to write to page */ |
| 755 | size_t copied; /* Bytes copied from user */ |
| 756 | |
| 757 | offset = offset_in_page(pos); |
| 758 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
| 759 | iov_iter_count(i)); |
| 760 | again: |
| 761 | if (bytes > length) |
| 762 | bytes = length; |
| 763 | |
| 764 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 765 | * Bring in the user page that we'll copy from _first_. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 766 | * Otherwise there's a nasty deadlock on copying from the |
| 767 | * same page as we're writing to, without it being marked |
| 768 | * up-to-date. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 769 | */ |
Andreas Gruenbacher | a629459 | 2021-08-02 14:54:16 +0200 | [diff] [blame] | 770 | if (unlikely(fault_in_iov_iter_readable(i, bytes))) { |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 771 | status = -EFAULT; |
| 772 | break; |
| 773 | } |
| 774 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 775 | status = iomap_write_begin(iter, pos, bytes, &folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 776 | if (unlikely(status)) |
| 777 | break; |
| 778 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 779 | page = folio_file_page(folio, pos >> PAGE_SHIFT); |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 780 | if (mapping_writably_mapped(iter->inode->i_mapping)) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 781 | flush_dcache_page(page); |
| 782 | |
Al Viro | f0b65f3 | 2021-04-30 10:26:41 -0400 | [diff] [blame] | 783 | copied = copy_page_from_iter_atomic(page, offset, bytes, i); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 784 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 785 | status = iomap_write_end(iter, pos, bytes, copied, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 786 | |
Al Viro | f0b65f3 | 2021-04-30 10:26:41 -0400 | [diff] [blame] | 787 | if (unlikely(copied != status)) |
| 788 | iov_iter_revert(i, copied - status); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 789 | |
Al Viro | f0b65f3 | 2021-04-30 10:26:41 -0400 | [diff] [blame] | 790 | cond_resched(); |
Al Viro | bc1bb41 | 2021-05-31 00:32:44 -0400 | [diff] [blame] | 791 | if (unlikely(status == 0)) { |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 792 | /* |
Al Viro | bc1bb41 | 2021-05-31 00:32:44 -0400 | [diff] [blame] | 793 | * A short copy made iomap_write_end() reject the |
| 794 | * thing entirely. Might be memory poisoning |
| 795 | * halfway through, might be a race with munmap, |
| 796 | * might be severe memory pressure. |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 797 | */ |
Al Viro | bc1bb41 | 2021-05-31 00:32:44 -0400 | [diff] [blame] | 798 | if (copied) |
| 799 | bytes = copied; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 800 | goto again; |
| 801 | } |
Al Viro | f0b65f3 | 2021-04-30 10:26:41 -0400 | [diff] [blame] | 802 | pos += status; |
| 803 | written += status; |
| 804 | length -= status; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 805 | |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 806 | balance_dirty_pages_ratelimited(iter->inode->i_mapping); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 807 | } while (iov_iter_count(i) && length); |
| 808 | |
| 809 | return written ? written : status; |
| 810 | } |
| 811 | |
| 812 | ssize_t |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 813 | iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i, |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 814 | const struct iomap_ops *ops) |
| 815 | { |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 816 | struct iomap_iter iter = { |
| 817 | .inode = iocb->ki_filp->f_mapping->host, |
| 818 | .pos = iocb->ki_pos, |
| 819 | .len = iov_iter_count(i), |
| 820 | .flags = IOMAP_WRITE, |
| 821 | }; |
| 822 | int ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 823 | |
Christoph Hellwig | ce83a02 | 2021-08-10 18:33:08 -0700 | [diff] [blame] | 824 | while ((ret = iomap_iter(&iter, ops)) > 0) |
| 825 | iter.processed = iomap_write_iter(&iter, i); |
| 826 | if (iter.pos == iocb->ki_pos) |
| 827 | return ret; |
| 828 | return iter.pos - iocb->ki_pos; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 829 | } |
| 830 | EXPORT_SYMBOL_GPL(iomap_file_buffered_write); |
| 831 | |
Christoph Hellwig | 8fc274d | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 832 | static loff_t iomap_unshare_iter(struct iomap_iter *iter) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 833 | { |
Christoph Hellwig | 8fc274d | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 834 | struct iomap *iomap = &iter->iomap; |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 835 | const struct iomap *srcmap = iomap_iter_srcmap(iter); |
Christoph Hellwig | 8fc274d | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 836 | loff_t pos = iter->pos; |
| 837 | loff_t length = iomap_length(iter); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 838 | long status = 0; |
Matthew Wilcox (Oracle) | d4ff3b2 | 2020-06-08 20:58:29 -0700 | [diff] [blame] | 839 | loff_t written = 0; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 840 | |
Christoph Hellwig | 3590c4d | 2019-10-18 16:41:34 -0700 | [diff] [blame] | 841 | /* don't bother with blocks that are not shared to start with */ |
| 842 | if (!(iomap->flags & IOMAP_F_SHARED)) |
| 843 | return length; |
| 844 | /* don't bother with holes or unwritten extents */ |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 845 | if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN) |
Christoph Hellwig | 3590c4d | 2019-10-18 16:41:34 -0700 | [diff] [blame] | 846 | return length; |
| 847 | |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 848 | do { |
Christoph Hellwig | 32a38a4 | 2019-10-18 16:42:50 -0700 | [diff] [blame] | 849 | unsigned long offset = offset_in_page(pos); |
| 850 | unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length); |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 851 | struct folio *folio; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 852 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 853 | status = iomap_write_begin(iter, pos, bytes, &folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 854 | if (unlikely(status)) |
| 855 | return status; |
| 856 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 857 | status = iomap_write_end(iter, pos, bytes, bytes, folio); |
Matthew Wilcox (Oracle) | e25ba8c | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 858 | if (WARN_ON_ONCE(status == 0)) |
| 859 | return -EIO; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 860 | |
| 861 | cond_resched(); |
| 862 | |
| 863 | pos += status; |
| 864 | written += status; |
| 865 | length -= status; |
| 866 | |
Christoph Hellwig | 8fc274d | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 867 | balance_dirty_pages_ratelimited(iter->inode->i_mapping); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 868 | } while (length); |
| 869 | |
| 870 | return written; |
| 871 | } |
| 872 | |
| 873 | int |
Christoph Hellwig | 3590c4d | 2019-10-18 16:41:34 -0700 | [diff] [blame] | 874 | iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len, |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 875 | const struct iomap_ops *ops) |
| 876 | { |
Christoph Hellwig | 8fc274d | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 877 | struct iomap_iter iter = { |
| 878 | .inode = inode, |
| 879 | .pos = pos, |
| 880 | .len = len, |
Christoph Hellwig | b74b129 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 881 | .flags = IOMAP_WRITE | IOMAP_UNSHARE, |
Christoph Hellwig | 8fc274d | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 882 | }; |
| 883 | int ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 884 | |
Christoph Hellwig | 8fc274d | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 885 | while ((ret = iomap_iter(&iter, ops)) > 0) |
| 886 | iter.processed = iomap_unshare_iter(&iter); |
| 887 | return ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 888 | } |
Christoph Hellwig | 3590c4d | 2019-10-18 16:41:34 -0700 | [diff] [blame] | 889 | EXPORT_SYMBOL_GPL(iomap_file_unshare); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 890 | |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 891 | static s64 __iomap_zero_iter(struct iomap_iter *iter, loff_t pos, u64 length) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 892 | { |
Matthew Wilcox (Oracle) | a25def1 | 2021-11-05 14:24:09 -0400 | [diff] [blame] | 893 | struct folio *folio; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 894 | int status; |
Matthew Wilcox (Oracle) | a25def1 | 2021-11-05 14:24:09 -0400 | [diff] [blame] | 895 | size_t offset; |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 896 | size_t bytes = min_t(u64, SIZE_MAX, length); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 897 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 898 | status = iomap_write_begin(iter, pos, bytes, &folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 899 | if (status) |
| 900 | return status; |
| 901 | |
Matthew Wilcox (Oracle) | a25def1 | 2021-11-05 14:24:09 -0400 | [diff] [blame] | 902 | offset = offset_in_folio(folio, pos); |
| 903 | if (bytes > folio_size(folio) - offset) |
| 904 | bytes = folio_size(folio) - offset; |
| 905 | |
| 906 | folio_zero_range(folio, offset, bytes); |
| 907 | folio_mark_accessed(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 908 | |
Matthew Wilcox (Oracle) | bc6123a | 2021-05-02 11:33:08 -0400 | [diff] [blame^] | 909 | return iomap_write_end(iter, pos, bytes, bytes, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Christoph Hellwig | 2aa3048 | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 912 | static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 913 | { |
Christoph Hellwig | 2aa3048 | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 914 | struct iomap *iomap = &iter->iomap; |
Christoph Hellwig | fad0a1a | 2021-08-10 18:33:16 -0700 | [diff] [blame] | 915 | const struct iomap *srcmap = iomap_iter_srcmap(iter); |
Christoph Hellwig | 2aa3048 | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 916 | loff_t pos = iter->pos; |
| 917 | loff_t length = iomap_length(iter); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 918 | loff_t written = 0; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 919 | |
| 920 | /* already zeroed? we're done. */ |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 921 | if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN) |
Matthew Wilcox (Oracle) | 81ee8e5 | 2020-09-21 08:58:42 -0700 | [diff] [blame] | 922 | return length; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 923 | |
| 924 | do { |
Matthew Wilcox (Oracle) | 81ee8e5 | 2020-09-21 08:58:42 -0700 | [diff] [blame] | 925 | s64 bytes; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 926 | |
Christoph Hellwig | 2aa3048 | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 927 | if (IS_DAX(iter->inode)) |
Matthew Wilcox (Oracle) | 81ee8e5 | 2020-09-21 08:58:42 -0700 | [diff] [blame] | 928 | bytes = dax_iomap_zero(pos, length, iomap); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 929 | else |
Christoph Hellwig | 1b5c1e3 | 2021-08-10 18:33:14 -0700 | [diff] [blame] | 930 | bytes = __iomap_zero_iter(iter, pos, length); |
Matthew Wilcox (Oracle) | 81ee8e5 | 2020-09-21 08:58:42 -0700 | [diff] [blame] | 931 | if (bytes < 0) |
| 932 | return bytes; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 933 | |
| 934 | pos += bytes; |
Matthew Wilcox (Oracle) | 81ee8e5 | 2020-09-21 08:58:42 -0700 | [diff] [blame] | 935 | length -= bytes; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 936 | written += bytes; |
| 937 | if (did_zero) |
| 938 | *did_zero = true; |
Matthew Wilcox (Oracle) | 81ee8e5 | 2020-09-21 08:58:42 -0700 | [diff] [blame] | 939 | } while (length > 0); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 940 | |
| 941 | return written; |
| 942 | } |
| 943 | |
| 944 | int |
| 945 | iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, |
| 946 | const struct iomap_ops *ops) |
| 947 | { |
Christoph Hellwig | 2aa3048 | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 948 | struct iomap_iter iter = { |
| 949 | .inode = inode, |
| 950 | .pos = pos, |
| 951 | .len = len, |
| 952 | .flags = IOMAP_ZERO, |
| 953 | }; |
| 954 | int ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 955 | |
Christoph Hellwig | 2aa3048 | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 956 | while ((ret = iomap_iter(&iter, ops)) > 0) |
| 957 | iter.processed = iomap_zero_iter(&iter, did_zero); |
| 958 | return ret; |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 959 | } |
| 960 | EXPORT_SYMBOL_GPL(iomap_zero_range); |
| 961 | |
| 962 | int |
| 963 | iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, |
| 964 | const struct iomap_ops *ops) |
| 965 | { |
| 966 | unsigned int blocksize = i_blocksize(inode); |
| 967 | unsigned int off = pos & (blocksize - 1); |
| 968 | |
| 969 | /* Block boundary? Nothing to do */ |
| 970 | if (!off) |
| 971 | return 0; |
| 972 | return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops); |
| 973 | } |
| 974 | EXPORT_SYMBOL_GPL(iomap_truncate_page); |
| 975 | |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 976 | static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter, |
| 977 | struct folio *folio) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 978 | { |
Christoph Hellwig | 253564b | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 979 | loff_t length = iomap_length(iter); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 980 | int ret; |
| 981 | |
Christoph Hellwig | 253564b | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 982 | if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) { |
Matthew Wilcox (Oracle) | d1bd0b4 | 2021-11-03 14:05:47 -0400 | [diff] [blame] | 983 | ret = __block_write_begin_int(folio, iter->pos, length, NULL, |
Christoph Hellwig | 253564b | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 984 | &iter->iomap); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 985 | if (ret) |
| 986 | return ret; |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 987 | block_commit_write(&folio->page, 0, length); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 988 | } else { |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 989 | WARN_ON_ONCE(!folio_test_uptodate(folio)); |
| 990 | folio_mark_dirty(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return length; |
| 994 | } |
| 995 | |
| 996 | vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops) |
| 997 | { |
Christoph Hellwig | 253564b | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 998 | struct iomap_iter iter = { |
| 999 | .inode = file_inode(vmf->vma->vm_file), |
| 1000 | .flags = IOMAP_WRITE | IOMAP_FAULT, |
| 1001 | }; |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 1002 | struct folio *folio = page_folio(vmf->page); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 1003 | ssize_t ret; |
| 1004 | |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 1005 | folio_lock(folio); |
| 1006 | ret = folio_mkwrite_check_truncate(folio, iter.inode); |
Andreas Gruenbacher | 243145b | 2020-01-06 08:58:23 -0800 | [diff] [blame] | 1007 | if (ret < 0) |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 1008 | goto out_unlock; |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 1009 | iter.pos = folio_pos(folio); |
Christoph Hellwig | 253564b | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 1010 | iter.len = ret; |
| 1011 | while ((ret = iomap_iter(&iter, ops)) > 0) |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 1012 | iter.processed = iomap_folio_mkwrite_iter(&iter, folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 1013 | |
Christoph Hellwig | 253564b | 2021-08-10 18:33:09 -0700 | [diff] [blame] | 1014 | if (ret < 0) |
| 1015 | goto out_unlock; |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 1016 | folio_wait_stable(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 1017 | return VM_FAULT_LOCKED; |
| 1018 | out_unlock: |
Matthew Wilcox (Oracle) | ea0f843 | 2021-04-28 22:32:02 -0400 | [diff] [blame] | 1019 | folio_unlock(folio); |
Darrick J. Wong | afc51aa | 2019-07-15 08:50:59 -0700 | [diff] [blame] | 1020 | return block_page_mkwrite_return(ret); |
| 1021 | } |
| 1022 | EXPORT_SYMBOL_GPL(iomap_page_mkwrite); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1023 | |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 1024 | static void iomap_finish_folio_write(struct inode *inode, struct folio *folio, |
| 1025 | size_t len, int error) |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1026 | { |
Matthew Wilcox (Oracle) | 95c4cd0 | 2021-04-27 22:56:17 -0400 | [diff] [blame] | 1027 | struct iomap_page *iop = to_iomap_page(folio); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1028 | |
| 1029 | if (error) { |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 1030 | folio_set_error(folio); |
Darrick J. Wong | b69eea8 | 2021-08-10 18:32:55 -0700 | [diff] [blame] | 1031 | mapping_set_error(inode->i_mapping, error); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 1034 | WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !iop); |
Matthew Wilcox (Oracle) | 0fb2d72 | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 1035 | WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1036 | |
Matthew Wilcox (Oracle) | 0fb2d72 | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 1037 | if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending)) |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 1038 | folio_end_writeback(folio); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * We're now finished for good with this ioend structure. Update the page |
| 1043 | * state, release holds on bios, and finally free up memory. Do not use the |
| 1044 | * ioend after this. |
| 1045 | */ |
| 1046 | static void |
| 1047 | iomap_finish_ioend(struct iomap_ioend *ioend, int error) |
| 1048 | { |
| 1049 | struct inode *inode = ioend->io_inode; |
| 1050 | struct bio *bio = &ioend->io_inline_bio; |
| 1051 | struct bio *last = ioend->io_bio, *next; |
| 1052 | u64 start = bio->bi_iter.bi_sector; |
Zorro Lang | c275779 | 2019-12-04 22:59:02 -0800 | [diff] [blame] | 1053 | loff_t offset = ioend->io_offset; |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1054 | bool quiet = bio_flagged(bio, BIO_QUIET); |
| 1055 | |
| 1056 | for (bio = &ioend->io_inline_bio; bio; bio = next) { |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 1057 | struct folio_iter fi; |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1058 | |
| 1059 | /* |
| 1060 | * For the last bio, bi_private points to the ioend, so we |
| 1061 | * need to explicitly end the iteration here. |
| 1062 | */ |
| 1063 | if (bio == last) |
| 1064 | next = NULL; |
| 1065 | else |
| 1066 | next = bio->bi_private; |
| 1067 | |
Matthew Wilcox (Oracle) | 8ffd74e | 2021-01-01 16:53:26 -0500 | [diff] [blame] | 1068 | /* walk all folios in bio, ending page IO on them */ |
| 1069 | bio_for_each_folio_all(fi, bio) |
| 1070 | iomap_finish_folio_write(inode, fi.folio, fi.length, |
| 1071 | error); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1072 | bio_put(bio); |
| 1073 | } |
Zorro Lang | c275779 | 2019-12-04 22:59:02 -0800 | [diff] [blame] | 1074 | /* The ioend has been freed by bio_put() */ |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1075 | |
| 1076 | if (unlikely(error && !quiet)) { |
| 1077 | printk_ratelimited(KERN_ERR |
Darrick J. Wong | 9cd0ed6 | 2019-10-17 14:02:07 -0700 | [diff] [blame] | 1078 | "%s: writeback error on inode %lu, offset %lld, sector %llu", |
Zorro Lang | c275779 | 2019-12-04 22:59:02 -0800 | [diff] [blame] | 1079 | inode->i_sb->s_id, inode->i_ino, offset, start); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | void |
| 1084 | iomap_finish_ioends(struct iomap_ioend *ioend, int error) |
| 1085 | { |
| 1086 | struct list_head tmp; |
| 1087 | |
| 1088 | list_replace_init(&ioend->io_list, &tmp); |
| 1089 | iomap_finish_ioend(ioend, error); |
| 1090 | |
| 1091 | while (!list_empty(&tmp)) { |
| 1092 | ioend = list_first_entry(&tmp, struct iomap_ioend, io_list); |
| 1093 | list_del_init(&ioend->io_list); |
| 1094 | iomap_finish_ioend(ioend, error); |
| 1095 | } |
| 1096 | } |
| 1097 | EXPORT_SYMBOL_GPL(iomap_finish_ioends); |
| 1098 | |
| 1099 | /* |
| 1100 | * We can merge two adjacent ioends if they have the same set of work to do. |
| 1101 | */ |
| 1102 | static bool |
| 1103 | iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next) |
| 1104 | { |
| 1105 | if (ioend->io_bio->bi_status != next->io_bio->bi_status) |
| 1106 | return false; |
| 1107 | if ((ioend->io_flags & IOMAP_F_SHARED) ^ |
| 1108 | (next->io_flags & IOMAP_F_SHARED)) |
| 1109 | return false; |
| 1110 | if ((ioend->io_type == IOMAP_UNWRITTEN) ^ |
| 1111 | (next->io_type == IOMAP_UNWRITTEN)) |
| 1112 | return false; |
| 1113 | if (ioend->io_offset + ioend->io_size != next->io_offset) |
| 1114 | return false; |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
| 1118 | void |
Brian Foster | 6e55249 | 2021-05-04 08:54:29 -0700 | [diff] [blame] | 1119 | iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends) |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1120 | { |
| 1121 | struct iomap_ioend *next; |
| 1122 | |
| 1123 | INIT_LIST_HEAD(&ioend->io_list); |
| 1124 | |
| 1125 | while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend, |
| 1126 | io_list))) { |
| 1127 | if (!iomap_ioend_can_merge(ioend, next)) |
| 1128 | break; |
| 1129 | list_move_tail(&next->io_list, &ioend->io_list); |
| 1130 | ioend->io_size += next->io_size; |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | EXPORT_SYMBOL_GPL(iomap_ioend_try_merge); |
| 1134 | |
| 1135 | static int |
Sami Tolvanen | 4f0f586 | 2021-04-08 11:28:34 -0700 | [diff] [blame] | 1136 | iomap_ioend_compare(void *priv, const struct list_head *a, |
| 1137 | const struct list_head *b) |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1138 | { |
Christoph Hellwig | b3d423e | 2019-10-17 13:12:20 -0700 | [diff] [blame] | 1139 | struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list); |
| 1140 | struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1141 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1142 | if (ia->io_offset < ib->io_offset) |
| 1143 | return -1; |
Christoph Hellwig | b3d423e | 2019-10-17 13:12:20 -0700 | [diff] [blame] | 1144 | if (ia->io_offset > ib->io_offset) |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1145 | return 1; |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | void |
| 1150 | iomap_sort_ioends(struct list_head *ioend_list) |
| 1151 | { |
| 1152 | list_sort(NULL, ioend_list, iomap_ioend_compare); |
| 1153 | } |
| 1154 | EXPORT_SYMBOL_GPL(iomap_sort_ioends); |
| 1155 | |
| 1156 | static void iomap_writepage_end_bio(struct bio *bio) |
| 1157 | { |
| 1158 | struct iomap_ioend *ioend = bio->bi_private; |
| 1159 | |
| 1160 | iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status)); |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * Submit the final bio for an ioend. |
| 1165 | * |
| 1166 | * If @error is non-zero, it means that we have a situation where some part of |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1167 | * the submission process has failed after we've marked pages for writeback |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1168 | * and unlocked them. In this situation, we need to fail the bio instead of |
| 1169 | * submitting it. This typically only happens on a filesystem shutdown. |
| 1170 | */ |
| 1171 | static int |
| 1172 | iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend, |
| 1173 | int error) |
| 1174 | { |
| 1175 | ioend->io_bio->bi_private = ioend; |
| 1176 | ioend->io_bio->bi_end_io = iomap_writepage_end_bio; |
| 1177 | |
| 1178 | if (wpc->ops->prepare_ioend) |
| 1179 | error = wpc->ops->prepare_ioend(ioend, error); |
| 1180 | if (error) { |
| 1181 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1182 | * If we're failing the IO now, just mark the ioend with an |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1183 | * error and finish it. This will run IO completion immediately |
| 1184 | * as there is only one reference to the ioend at this point in |
| 1185 | * time. |
| 1186 | */ |
| 1187 | ioend->io_bio->bi_status = errno_to_blk_status(error); |
| 1188 | bio_endio(ioend->io_bio); |
| 1189 | return error; |
| 1190 | } |
| 1191 | |
| 1192 | submit_bio(ioend->io_bio); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static struct iomap_ioend * |
| 1197 | iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc, |
| 1198 | loff_t offset, sector_t sector, struct writeback_control *wbc) |
| 1199 | { |
| 1200 | struct iomap_ioend *ioend; |
| 1201 | struct bio *bio; |
| 1202 | |
Christoph Hellwig | a8affc0 | 2021-03-11 12:01:37 +0100 | [diff] [blame] | 1203 | bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &iomap_ioend_bioset); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1204 | bio_set_dev(bio, wpc->iomap.bdev); |
| 1205 | bio->bi_iter.bi_sector = sector; |
| 1206 | bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc); |
| 1207 | bio->bi_write_hint = inode->i_write_hint; |
| 1208 | wbc_init_bio(wbc, bio); |
| 1209 | |
| 1210 | ioend = container_of(bio, struct iomap_ioend, io_inline_bio); |
| 1211 | INIT_LIST_HEAD(&ioend->io_list); |
| 1212 | ioend->io_type = wpc->iomap.type; |
| 1213 | ioend->io_flags = wpc->iomap.flags; |
| 1214 | ioend->io_inode = inode; |
| 1215 | ioend->io_size = 0; |
| 1216 | ioend->io_offset = offset; |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1217 | ioend->io_bio = bio; |
| 1218 | return ioend; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Allocate a new bio, and chain the old bio to the new one. |
| 1223 | * |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1224 | * Note that we have to perform the chaining in this unintuitive order |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1225 | * so that the bi_private linkage is set up in the right direction for the |
| 1226 | * traversal in iomap_finish_ioend(). |
| 1227 | */ |
| 1228 | static struct bio * |
| 1229 | iomap_chain_bio(struct bio *prev) |
| 1230 | { |
| 1231 | struct bio *new; |
| 1232 | |
Christoph Hellwig | a8affc0 | 2021-03-11 12:01:37 +0100 | [diff] [blame] | 1233 | new = bio_alloc(GFP_NOFS, BIO_MAX_VECS); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1234 | bio_copy_dev(new, prev);/* also copies over blkcg information */ |
| 1235 | new->bi_iter.bi_sector = bio_end_sector(prev); |
| 1236 | new->bi_opf = prev->bi_opf; |
| 1237 | new->bi_write_hint = prev->bi_write_hint; |
| 1238 | |
| 1239 | bio_chain(prev, new); |
| 1240 | bio_get(prev); /* for iomap_finish_ioend */ |
| 1241 | submit_bio(prev); |
| 1242 | return new; |
| 1243 | } |
| 1244 | |
| 1245 | static bool |
| 1246 | iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset, |
| 1247 | sector_t sector) |
| 1248 | { |
| 1249 | if ((wpc->iomap.flags & IOMAP_F_SHARED) != |
| 1250 | (wpc->ioend->io_flags & IOMAP_F_SHARED)) |
| 1251 | return false; |
| 1252 | if (wpc->iomap.type != wpc->ioend->io_type) |
| 1253 | return false; |
| 1254 | if (offset != wpc->ioend->io_offset + wpc->ioend->io_size) |
| 1255 | return false; |
| 1256 | if (sector != bio_end_sector(wpc->ioend->io_bio)) |
| 1257 | return false; |
| 1258 | return true; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Test to see if we have an existing ioend structure that we could append to |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1263 | * first; otherwise finish off the current ioend and start another. |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1264 | */ |
| 1265 | static void |
| 1266 | iomap_add_to_ioend(struct inode *inode, loff_t offset, struct page *page, |
| 1267 | struct iomap_page *iop, struct iomap_writepage_ctx *wpc, |
| 1268 | struct writeback_control *wbc, struct list_head *iolist) |
| 1269 | { |
| 1270 | sector_t sector = iomap_sector(&wpc->iomap, offset); |
| 1271 | unsigned len = i_blocksize(inode); |
| 1272 | unsigned poff = offset & (PAGE_SIZE - 1); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1273 | |
| 1274 | if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, offset, sector)) { |
| 1275 | if (wpc->ioend) |
| 1276 | list_add(&wpc->ioend->io_list, iolist); |
| 1277 | wpc->ioend = iomap_alloc_ioend(inode, wpc, offset, sector, wbc); |
| 1278 | } |
| 1279 | |
Christoph Hellwig | c1b79f1 | 2021-08-02 14:43:43 -0700 | [diff] [blame] | 1280 | if (bio_add_page(wpc->ioend->io_bio, page, len, poff) != len) { |
| 1281 | wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio); |
| 1282 | __bio_add_page(wpc->ioend->io_bio, page, len, poff); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Christoph Hellwig | c1b79f1 | 2021-08-02 14:43:43 -0700 | [diff] [blame] | 1285 | if (iop) |
| 1286 | atomic_add(len, &iop->write_bytes_pending); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1287 | wpc->ioend->io_size += len; |
| 1288 | wbc_account_cgroup_owner(wbc, page, len); |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * We implement an immediate ioend submission policy here to avoid needing to |
| 1293 | * chain multiple ioends and hence nest mempool allocations which can violate |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1294 | * the forward progress guarantees we need to provide. The current ioend we're |
| 1295 | * adding blocks to is cached in the writepage context, and if the new block |
| 1296 | * doesn't append to the cached ioend, it will create a new ioend and cache that |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1297 | * instead. |
| 1298 | * |
| 1299 | * If a new ioend is created and cached, the old ioend is returned and queued |
| 1300 | * locally for submission once the entire page is processed or an error has been |
| 1301 | * detected. While ioends are submitted immediately after they are completed, |
| 1302 | * batching optimisations are provided by higher level block plugging. |
| 1303 | * |
| 1304 | * At the end of a writeback pass, there will be a cached ioend remaining on the |
| 1305 | * writepage context that the caller will need to submit. |
| 1306 | */ |
| 1307 | static int |
| 1308 | iomap_writepage_map(struct iomap_writepage_ctx *wpc, |
| 1309 | struct writeback_control *wbc, struct inode *inode, |
| 1310 | struct page *page, u64 end_offset) |
| 1311 | { |
Matthew Wilcox (Oracle) | 435d44b | 2021-04-27 23:12:52 -0400 | [diff] [blame] | 1312 | struct folio *folio = page_folio(page); |
| 1313 | struct iomap_page *iop = iomap_page_create(inode, folio); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1314 | struct iomap_ioend *ioend, *next; |
| 1315 | unsigned len = i_blocksize(inode); |
| 1316 | u64 file_offset; /* file offset of page */ |
| 1317 | int error = 0, count = 0, i; |
| 1318 | LIST_HEAD(submit_list); |
| 1319 | |
Matthew Wilcox (Oracle) | 0fb2d72 | 2020-09-21 08:58:41 -0700 | [diff] [blame] | 1320 | WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * Walk through the page to find areas to write back. If we run off the |
| 1324 | * end of the current map or find the current map invalid, grab a new |
| 1325 | * one. |
| 1326 | */ |
| 1327 | for (i = 0, file_offset = page_offset(page); |
| 1328 | i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset; |
| 1329 | i++, file_offset += len) { |
| 1330 | if (iop && !test_bit(i, iop->uptodate)) |
| 1331 | continue; |
| 1332 | |
| 1333 | error = wpc->ops->map_blocks(wpc, inode, file_offset); |
| 1334 | if (error) |
| 1335 | break; |
Christoph Hellwig | 3e19e6f | 2019-10-17 13:12:17 -0700 | [diff] [blame] | 1336 | if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE)) |
| 1337 | continue; |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1338 | if (wpc->iomap.type == IOMAP_HOLE) |
| 1339 | continue; |
| 1340 | iomap_add_to_ioend(inode, file_offset, page, iop, wpc, wbc, |
| 1341 | &submit_list); |
| 1342 | count++; |
| 1343 | } |
| 1344 | |
| 1345 | WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list)); |
| 1346 | WARN_ON_ONCE(!PageLocked(page)); |
| 1347 | WARN_ON_ONCE(PageWriteback(page)); |
Brian Foster | 50e7d6c | 2020-10-29 14:30:49 -0700 | [diff] [blame] | 1348 | WARN_ON_ONCE(PageDirty(page)); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1349 | |
| 1350 | /* |
| 1351 | * We cannot cancel the ioend directly here on error. We may have |
| 1352 | * already set other pages under writeback and hence we have to run I/O |
| 1353 | * completion to mark the error state of the pages under writeback |
| 1354 | * appropriately. |
| 1355 | */ |
| 1356 | if (unlikely(error)) { |
Brian Foster | 763e4cd | 2020-10-29 14:30:48 -0700 | [diff] [blame] | 1357 | /* |
| 1358 | * Let the filesystem know what portion of the current page |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1359 | * failed to map. If the page hasn't been added to ioend, it |
Brian Foster | 763e4cd | 2020-10-29 14:30:48 -0700 | [diff] [blame] | 1360 | * won't be affected by I/O completion and we must unlock it |
| 1361 | * now. |
| 1362 | */ |
| 1363 | if (wpc->ops->discard_page) |
| 1364 | wpc->ops->discard_page(page, file_offset); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1365 | if (!count) { |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1366 | ClearPageUptodate(page); |
| 1367 | unlock_page(page); |
| 1368 | goto done; |
| 1369 | } |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
Brian Foster | 50e7d6c | 2020-10-29 14:30:49 -0700 | [diff] [blame] | 1372 | set_page_writeback(page); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1373 | unlock_page(page); |
| 1374 | |
| 1375 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1376 | * Preserve the original error if there was one; catch |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1377 | * submission errors here and propagate into subsequent ioend |
| 1378 | * submissions. |
| 1379 | */ |
| 1380 | list_for_each_entry_safe(ioend, next, &submit_list, io_list) { |
| 1381 | int error2; |
| 1382 | |
| 1383 | list_del_init(&ioend->io_list); |
| 1384 | error2 = iomap_submit_ioend(wpc, ioend, error); |
| 1385 | if (error2 && !error) |
| 1386 | error = error2; |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * We can end up here with no error and nothing to write only if we race |
| 1391 | * with a partial page truncate on a sub-page block sized filesystem. |
| 1392 | */ |
| 1393 | if (!count) |
| 1394 | end_page_writeback(page); |
| 1395 | done: |
| 1396 | mapping_set_error(page->mapping, error); |
| 1397 | return error; |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * Write out a dirty page. |
| 1402 | * |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1403 | * For delalloc space on the page, we need to allocate space and flush it. |
| 1404 | * For unwritten space on the page, we need to start the conversion to |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1405 | * regular allocated space. |
| 1406 | */ |
| 1407 | static int |
| 1408 | iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data) |
| 1409 | { |
| 1410 | struct iomap_writepage_ctx *wpc = data; |
| 1411 | struct inode *inode = page->mapping->host; |
| 1412 | pgoff_t end_index; |
| 1413 | u64 end_offset; |
| 1414 | loff_t offset; |
| 1415 | |
Matthew Wilcox (Oracle) | 1ac9945 | 2020-03-05 07:21:43 -0800 | [diff] [blame] | 1416 | trace_iomap_writepage(inode, page_offset(page), PAGE_SIZE); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1417 | |
| 1418 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1419 | * Refuse to write the page out if we're called from reclaim context. |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1420 | * |
| 1421 | * This avoids stack overflows when called from deeply used stacks in |
| 1422 | * random callers for direct reclaim or memcg reclaim. We explicitly |
| 1423 | * allow reclaim from kswapd as the stack usage there is relatively low. |
| 1424 | * |
| 1425 | * This should never happen except in the case of a VM regression so |
| 1426 | * warn about it. |
| 1427 | */ |
| 1428 | if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == |
| 1429 | PF_MEMALLOC)) |
| 1430 | goto redirty; |
| 1431 | |
| 1432 | /* |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1433 | * Is this page beyond the end of the file? |
| 1434 | * |
| 1435 | * The page index is less than the end_index, adjust the end_offset |
| 1436 | * to the highest offset that this page should represent. |
| 1437 | * ----------------------------------------------------- |
| 1438 | * | file mapping | <EOF> | |
| 1439 | * ----------------------------------------------------- |
| 1440 | * | Page ... | Page N-2 | Page N-1 | Page N | | |
| 1441 | * ^--------------------------------^----------|-------- |
| 1442 | * | desired writeback range | see else | |
| 1443 | * ---------------------------------^------------------| |
| 1444 | */ |
| 1445 | offset = i_size_read(inode); |
| 1446 | end_index = offset >> PAGE_SHIFT; |
| 1447 | if (page->index < end_index) |
| 1448 | end_offset = (loff_t)(page->index + 1) << PAGE_SHIFT; |
| 1449 | else { |
| 1450 | /* |
| 1451 | * Check whether the page to write out is beyond or straddles |
| 1452 | * i_size or not. |
| 1453 | * ------------------------------------------------------- |
| 1454 | * | file mapping | <EOF> | |
| 1455 | * ------------------------------------------------------- |
| 1456 | * | Page ... | Page N-2 | Page N-1 | Page N | Beyond | |
| 1457 | * ^--------------------------------^-----------|--------- |
| 1458 | * | | Straddles | |
| 1459 | * ---------------------------------^-----------|--------| |
| 1460 | */ |
| 1461 | unsigned offset_into_page = offset & (PAGE_SIZE - 1); |
| 1462 | |
| 1463 | /* |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1464 | * Skip the page if it's fully outside i_size, e.g. due to a |
| 1465 | * truncate operation that's in progress. We must redirty the |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1466 | * page so that reclaim stops reclaiming it. Otherwise |
| 1467 | * iomap_vm_releasepage() is called on it and gets confused. |
| 1468 | * |
Andreas Gruenbacher | f1f264b | 2021-08-02 14:46:31 -0700 | [diff] [blame] | 1469 | * Note that the end_index is unsigned long. If the given |
| 1470 | * offset is greater than 16TB on a 32-bit system then if we |
| 1471 | * checked if the page is fully outside i_size with |
| 1472 | * "if (page->index >= end_index + 1)", "end_index + 1" would |
| 1473 | * overflow and evaluate to 0. Hence this page would be |
| 1474 | * redirtied and written out repeatedly, which would result in |
| 1475 | * an infinite loop; the user program performing this operation |
| 1476 | * would hang. Instead, we can detect this situation by |
| 1477 | * checking if the page is totally beyond i_size or if its |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 1478 | * offset is just equal to the EOF. |
| 1479 | */ |
| 1480 | if (page->index > end_index || |
| 1481 | (page->index == end_index && offset_into_page == 0)) |
| 1482 | goto redirty; |
| 1483 | |
| 1484 | /* |
| 1485 | * The page straddles i_size. It must be zeroed out on each |
| 1486 | * and every writepage invocation because it may be mmapped. |
| 1487 | * "A file is mapped in multiples of the page size. For a file |
| 1488 | * that is not a multiple of the page size, the remaining |
| 1489 | * memory is zeroed when mapped, and writes to that region are |
| 1490 | * not written out to the file." |
| 1491 | */ |
| 1492 | zero_user_segment(page, offset_into_page, PAGE_SIZE); |
| 1493 | |
| 1494 | /* Adjust the end_offset to the end of file */ |
| 1495 | end_offset = offset; |
| 1496 | } |
| 1497 | |
| 1498 | return iomap_writepage_map(wpc, wbc, inode, page, end_offset); |
| 1499 | |
| 1500 | redirty: |
| 1501 | redirty_page_for_writepage(wbc, page); |
| 1502 | unlock_page(page); |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | int |
| 1507 | iomap_writepage(struct page *page, struct writeback_control *wbc, |
| 1508 | struct iomap_writepage_ctx *wpc, |
| 1509 | const struct iomap_writeback_ops *ops) |
| 1510 | { |
| 1511 | int ret; |
| 1512 | |
| 1513 | wpc->ops = ops; |
| 1514 | ret = iomap_do_writepage(page, wbc, wpc); |
| 1515 | if (!wpc->ioend) |
| 1516 | return ret; |
| 1517 | return iomap_submit_ioend(wpc, wpc->ioend, ret); |
| 1518 | } |
| 1519 | EXPORT_SYMBOL_GPL(iomap_writepage); |
| 1520 | |
| 1521 | int |
| 1522 | iomap_writepages(struct address_space *mapping, struct writeback_control *wbc, |
| 1523 | struct iomap_writepage_ctx *wpc, |
| 1524 | const struct iomap_writeback_ops *ops) |
| 1525 | { |
| 1526 | int ret; |
| 1527 | |
| 1528 | wpc->ops = ops; |
| 1529 | ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc); |
| 1530 | if (!wpc->ioend) |
| 1531 | return ret; |
| 1532 | return iomap_submit_ioend(wpc, wpc->ioend, ret); |
| 1533 | } |
| 1534 | EXPORT_SYMBOL_GPL(iomap_writepages); |
| 1535 | |
| 1536 | static int __init iomap_init(void) |
| 1537 | { |
| 1538 | return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE), |
| 1539 | offsetof(struct iomap_ioend, io_inline_bio), |
| 1540 | BIOSET_NEED_BVECS); |
| 1541 | } |
| 1542 | fs_initcall(iomap_init); |