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