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