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