Christoph Hellwig | 73ce6ab | 2019-04-28 08:34:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010 Red Hat, Inc. |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 4 | * Copyright (c) 2016-2018 Christoph Hellwig. |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [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/uaccess.h> |
| 11 | #include <linux/gfp.h> |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 12 | #include <linux/migrate.h> |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 13 | #include <linux/mm.h> |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 14 | #include <linux/mm_inline.h> |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 15 | #include <linux/swap.h> |
| 16 | #include <linux/pagemap.h> |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 17 | #include <linux/pagevec.h> |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 18 | #include <linux/file.h> |
| 19 | #include <linux/uio.h> |
| 20 | #include <linux/backing-dev.h> |
| 21 | #include <linux/buffer_head.h> |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 22 | #include <linux/task_io_accounting_ops.h> |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 23 | #include <linux/dax.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 24 | #include <linux/sched/signal.h> |
| 25 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 26 | #include "internal.h" |
| 27 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 28 | /* |
| 29 | * Execute a iomap write on a segment of the mapping that spans a |
| 30 | * contiguous range of pages that have identical block mapping state. |
| 31 | * |
| 32 | * This avoids the need to map pages individually, do individual allocations |
| 33 | * for each page and most importantly avoid the need for filesystem specific |
| 34 | * locking per page. Instead, all the operations are amortised over the entire |
| 35 | * range of pages. It is assumed that the filesystems will lock whatever |
| 36 | * resources they require in the iomap_begin call, and release them in the |
| 37 | * iomap_end call. |
| 38 | */ |
Christoph Hellwig | befb503 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 39 | loff_t |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 40 | iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 41 | const struct iomap_ops *ops, void *data, iomap_actor_t actor) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 42 | { |
| 43 | struct iomap iomap = { 0 }; |
| 44 | loff_t written = 0, ret; |
| 45 | |
| 46 | /* |
| 47 | * Need to map a range from start position for length bytes. This can |
| 48 | * span multiple pages - it is only guaranteed to return a range of a |
| 49 | * single type of pages (e.g. all into a hole, all mapped or all |
| 50 | * unwritten). Failure at this point has nothing to undo. |
| 51 | * |
| 52 | * If allocation is required for this range, reserve the space now so |
| 53 | * that the allocation is guaranteed to succeed later on. Once we copy |
| 54 | * the data into the page cache pages, then we cannot fail otherwise we |
| 55 | * expose transient stale data. If the reserve fails, we can safely |
| 56 | * back out at this point as there is nothing to undo. |
| 57 | */ |
| 58 | ret = ops->iomap_begin(inode, pos, length, flags, &iomap); |
| 59 | if (ret) |
| 60 | return ret; |
| 61 | if (WARN_ON(iomap.offset > pos)) |
| 62 | return -EIO; |
Darrick J. Wong | 0c6dda7 | 2018-01-26 11:11:20 -0800 | [diff] [blame] | 63 | if (WARN_ON(iomap.length == 0)) |
| 64 | return -EIO; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Cut down the length to the one actually provided by the filesystem, |
| 68 | * as it might not be able to give us the whole size that we requested. |
| 69 | */ |
| 70 | if (iomap.offset + iomap.length < pos + length) |
| 71 | length = iomap.offset + iomap.length - pos; |
| 72 | |
| 73 | /* |
| 74 | * Now that we have guaranteed that the space allocation will succeed. |
| 75 | * we can do the copy-in page by page without having to worry about |
| 76 | * failures exposing transient data. |
| 77 | */ |
| 78 | written = actor(inode, pos, length, data, &iomap); |
| 79 | |
| 80 | /* |
| 81 | * Now the data has been copied, commit the range we've copied. This |
| 82 | * should not fail unless the filesystem has had a fatal error. |
| 83 | */ |
Christoph Hellwig | f20ac7a | 2016-08-17 08:42:34 +1000 | [diff] [blame] | 84 | if (ops->iomap_end) { |
| 85 | ret = ops->iomap_end(inode, pos, length, |
| 86 | written > 0 ? written : 0, |
| 87 | flags, &iomap); |
| 88 | } |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 89 | |
| 90 | return written ? written : ret; |
| 91 | } |
| 92 | |
Christoph Hellwig | 57fc505 | 2018-06-01 09:03:08 -0700 | [diff] [blame] | 93 | static sector_t |
| 94 | iomap_sector(struct iomap *iomap, loff_t pos) |
| 95 | { |
| 96 | return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT; |
| 97 | } |
| 98 | |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 99 | static struct iomap_page * |
| 100 | iomap_page_create(struct inode *inode, struct page *page) |
| 101 | { |
| 102 | struct iomap_page *iop = to_iomap_page(page); |
| 103 | |
| 104 | if (iop || i_blocksize(inode) == PAGE_SIZE) |
| 105 | return iop; |
| 106 | |
| 107 | iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL); |
| 108 | atomic_set(&iop->read_count, 0); |
| 109 | atomic_set(&iop->write_count, 0); |
| 110 | bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE); |
Piotr Jaroszynski | 8e47a45 | 2019-01-27 08:46:45 -0800 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * migrate_page_move_mapping() assumes that pages with private data have |
| 114 | * their count elevated by 1. |
| 115 | */ |
| 116 | get_page(page); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 117 | set_page_private(page, (unsigned long)iop); |
| 118 | SetPagePrivate(page); |
| 119 | return iop; |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | iomap_page_release(struct page *page) |
| 124 | { |
| 125 | struct iomap_page *iop = to_iomap_page(page); |
| 126 | |
| 127 | if (!iop) |
| 128 | return; |
| 129 | WARN_ON_ONCE(atomic_read(&iop->read_count)); |
| 130 | WARN_ON_ONCE(atomic_read(&iop->write_count)); |
| 131 | ClearPagePrivate(page); |
| 132 | set_page_private(page, 0); |
Piotr Jaroszynski | 8e47a45 | 2019-01-27 08:46:45 -0800 | [diff] [blame] | 133 | put_page(page); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 134 | kfree(iop); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Calculate the range inside the page that we actually need to read. |
| 139 | */ |
| 140 | static void |
| 141 | iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop, |
| 142 | loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp) |
| 143 | { |
Dave Chinner | 8c110d4 | 2018-11-21 08:06:37 -0800 | [diff] [blame] | 144 | loff_t orig_pos = *pos; |
| 145 | loff_t isize = i_size_read(inode); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 146 | unsigned block_bits = inode->i_blkbits; |
| 147 | unsigned block_size = (1 << block_bits); |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 148 | unsigned poff = offset_in_page(*pos); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 149 | unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length); |
| 150 | unsigned first = poff >> block_bits; |
| 151 | unsigned last = (poff + plen - 1) >> block_bits; |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * If the block size is smaller than the page size we need to check the |
| 155 | * per-block uptodate status and adjust the offset and length if needed |
| 156 | * to avoid reading in already uptodate ranges. |
| 157 | */ |
| 158 | if (iop) { |
| 159 | unsigned int i; |
| 160 | |
| 161 | /* move forward for each leading block marked uptodate */ |
| 162 | for (i = first; i <= last; i++) { |
| 163 | if (!test_bit(i, iop->uptodate)) |
| 164 | break; |
| 165 | *pos += block_size; |
| 166 | poff += block_size; |
| 167 | plen -= block_size; |
| 168 | first++; |
| 169 | } |
| 170 | |
| 171 | /* truncate len if we find any trailing uptodate block(s) */ |
| 172 | for ( ; i <= last; i++) { |
| 173 | if (test_bit(i, iop->uptodate)) { |
| 174 | plen -= (last - i + 1) * block_size; |
| 175 | last = i - 1; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * If the extent spans the block that contains the i_size we need to |
| 183 | * handle both halves separately so that we properly zero data in the |
| 184 | * page cache for blocks that are entirely outside of i_size. |
| 185 | */ |
Dave Chinner | 8c110d4 | 2018-11-21 08:06:37 -0800 | [diff] [blame] | 186 | if (orig_pos <= isize && orig_pos + length > isize) { |
| 187 | unsigned end = offset_in_page(isize - 1) >> block_bits; |
| 188 | |
| 189 | if (first <= end && last > end) |
| 190 | plen -= (last - end) * block_size; |
| 191 | } |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 192 | |
| 193 | *offp = poff; |
| 194 | *lenp = plen; |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len) |
| 199 | { |
| 200 | struct iomap_page *iop = to_iomap_page(page); |
| 201 | struct inode *inode = page->mapping->host; |
| 202 | unsigned first = off >> inode->i_blkbits; |
| 203 | unsigned last = (off + len - 1) >> inode->i_blkbits; |
| 204 | unsigned int i; |
| 205 | bool uptodate = true; |
| 206 | |
| 207 | if (iop) { |
| 208 | for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) { |
| 209 | if (i >= first && i <= last) |
| 210 | set_bit(i, iop->uptodate); |
| 211 | else if (!test_bit(i, iop->uptodate)) |
| 212 | uptodate = false; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (uptodate && !PageError(page)) |
| 217 | SetPageUptodate(page); |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | iomap_read_finish(struct iomap_page *iop, struct page *page) |
| 222 | { |
| 223 | if (!iop || atomic_dec_and_test(&iop->read_count)) |
| 224 | unlock_page(page); |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | iomap_read_page_end_io(struct bio_vec *bvec, int error) |
| 229 | { |
| 230 | struct page *page = bvec->bv_page; |
| 231 | struct iomap_page *iop = to_iomap_page(page); |
| 232 | |
| 233 | if (unlikely(error)) { |
| 234 | ClearPageUptodate(page); |
| 235 | SetPageError(page); |
| 236 | } else { |
| 237 | iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len); |
| 238 | } |
| 239 | |
| 240 | iomap_read_finish(iop, page); |
| 241 | } |
| 242 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 243 | static void |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 244 | iomap_read_end_io(struct bio *bio) |
| 245 | { |
| 246 | int error = blk_status_to_errno(bio->bi_status); |
| 247 | struct bio_vec *bvec; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 248 | struct bvec_iter_all iter_all; |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 249 | |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 250 | bio_for_each_segment_all(bvec, bio, iter_all) |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 251 | iomap_read_page_end_io(bvec, error); |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 252 | bio_put(bio); |
| 253 | } |
| 254 | |
| 255 | struct iomap_readpage_ctx { |
| 256 | struct page *cur_page; |
| 257 | bool cur_page_in_bio; |
| 258 | bool is_readahead; |
| 259 | struct bio *bio; |
| 260 | struct list_head *pages; |
| 261 | }; |
| 262 | |
Christoph Hellwig | cbbf4c0 | 2019-05-01 20:16:40 -0700 | [diff] [blame] | 263 | static void |
| 264 | iomap_read_inline_data(struct inode *inode, struct page *page, |
| 265 | struct iomap *iomap) |
| 266 | { |
| 267 | size_t size = i_size_read(inode); |
| 268 | void *addr; |
| 269 | |
| 270 | if (PageUptodate(page)) |
| 271 | return; |
| 272 | |
| 273 | BUG_ON(page->index); |
| 274 | BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data)); |
| 275 | |
| 276 | addr = kmap_atomic(page); |
| 277 | memcpy(addr, iomap->inline_data, size); |
| 278 | memset(addr + size, 0, PAGE_SIZE - size); |
| 279 | kunmap_atomic(addr); |
| 280 | SetPageUptodate(page); |
| 281 | } |
| 282 | |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 283 | static loff_t |
| 284 | iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
| 285 | struct iomap *iomap) |
| 286 | { |
| 287 | struct iomap_readpage_ctx *ctx = data; |
| 288 | struct page *page = ctx->cur_page; |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 289 | struct iomap_page *iop = iomap_page_create(inode, page); |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 290 | bool is_contig = false; |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 291 | loff_t orig_pos = pos; |
| 292 | unsigned poff, plen; |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 293 | sector_t sector; |
| 294 | |
Andreas Gruenbacher | 806a147 | 2018-07-03 09:07:47 -0700 | [diff] [blame] | 295 | if (iomap->type == IOMAP_INLINE) { |
Darrick J. Wong | 7d5e049 | 2018-08-10 17:55:57 -0700 | [diff] [blame] | 296 | WARN_ON_ONCE(pos); |
Andreas Gruenbacher | 806a147 | 2018-07-03 09:07:47 -0700 | [diff] [blame] | 297 | iomap_read_inline_data(inode, page, iomap); |
| 298 | return PAGE_SIZE; |
| 299 | } |
| 300 | |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 301 | /* zero post-eof blocks as the page may be mapped */ |
| 302 | iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen); |
| 303 | if (plen == 0) |
| 304 | goto done; |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 305 | |
| 306 | if (iomap->type != IOMAP_MAPPED || pos >= i_size_read(inode)) { |
| 307 | zero_user(page, poff, plen); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 308 | iomap_set_range_uptodate(page, poff, plen); |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 309 | goto done; |
| 310 | } |
| 311 | |
| 312 | ctx->cur_page_in_bio = true; |
| 313 | |
| 314 | /* |
| 315 | * Try to merge into a previous segment if we can. |
| 316 | */ |
| 317 | sector = iomap_sector(iomap, pos); |
| 318 | if (ctx->bio && bio_end_sector(ctx->bio) == sector) { |
Ming Lei | 07173c3 | 2019-02-15 19:13:20 +0800 | [diff] [blame] | 319 | if (__bio_try_merge_page(ctx->bio, page, plen, poff, true)) |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 320 | goto done; |
| 321 | is_contig = true; |
| 322 | } |
| 323 | |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 324 | /* |
| 325 | * If we start a new segment we need to increase the read count, and we |
| 326 | * need to do so before submitting any previous full bio to make sure |
| 327 | * that we don't prematurely unlock the page. |
| 328 | */ |
| 329 | if (iop) |
| 330 | atomic_inc(&iop->read_count); |
| 331 | |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 332 | if (!ctx->bio || !is_contig || bio_full(ctx->bio)) { |
| 333 | gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL); |
| 334 | int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 335 | |
| 336 | if (ctx->bio) |
| 337 | submit_bio(ctx->bio); |
| 338 | |
| 339 | if (ctx->is_readahead) /* same as readahead_gfp_mask */ |
| 340 | gfp |= __GFP_NORETRY | __GFP_NOWARN; |
| 341 | ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs)); |
| 342 | ctx->bio->bi_opf = REQ_OP_READ; |
| 343 | if (ctx->is_readahead) |
| 344 | ctx->bio->bi_opf |= REQ_RAHEAD; |
| 345 | ctx->bio->bi_iter.bi_sector = sector; |
| 346 | bio_set_dev(ctx->bio, iomap->bdev); |
| 347 | ctx->bio->bi_end_io = iomap_read_end_io; |
| 348 | } |
| 349 | |
Ming Lei | 07173c3 | 2019-02-15 19:13:20 +0800 | [diff] [blame] | 350 | bio_add_page(ctx->bio, page, plen, poff); |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 351 | done: |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 352 | /* |
| 353 | * Move the caller beyond our range so that it keeps making progress. |
| 354 | * For that we have to include any leading non-uptodate ranges, but |
| 355 | * we can skip trailing ones as they will be handled in the next |
| 356 | * iteration. |
| 357 | */ |
| 358 | return pos - orig_pos + plen; |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | int |
| 362 | iomap_readpage(struct page *page, const struct iomap_ops *ops) |
| 363 | { |
| 364 | struct iomap_readpage_ctx ctx = { .cur_page = page }; |
| 365 | struct inode *inode = page->mapping->host; |
| 366 | unsigned poff; |
| 367 | loff_t ret; |
| 368 | |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 369 | for (poff = 0; poff < PAGE_SIZE; poff += ret) { |
| 370 | ret = iomap_apply(inode, page_offset(page) + poff, |
| 371 | PAGE_SIZE - poff, 0, ops, &ctx, |
| 372 | iomap_readpage_actor); |
| 373 | if (ret <= 0) { |
| 374 | WARN_ON_ONCE(ret == 0); |
| 375 | SetPageError(page); |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (ctx.bio) { |
| 381 | submit_bio(ctx.bio); |
| 382 | WARN_ON_ONCE(!ctx.cur_page_in_bio); |
| 383 | } else { |
| 384 | WARN_ON_ONCE(ctx.cur_page_in_bio); |
| 385 | unlock_page(page); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Just like mpage_readpages and block_read_full_page we always |
| 390 | * return 0 and just mark the page as PageError on errors. This |
| 391 | * should be cleaned up all through the stack eventually. |
| 392 | */ |
| 393 | return 0; |
| 394 | } |
| 395 | EXPORT_SYMBOL_GPL(iomap_readpage); |
| 396 | |
| 397 | static struct page * |
| 398 | iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos, |
| 399 | loff_t length, loff_t *done) |
| 400 | { |
| 401 | while (!list_empty(pages)) { |
| 402 | struct page *page = lru_to_page(pages); |
| 403 | |
| 404 | if (page_offset(page) >= (u64)pos + length) |
| 405 | break; |
| 406 | |
| 407 | list_del(&page->lru); |
| 408 | if (!add_to_page_cache_lru(page, inode->i_mapping, page->index, |
| 409 | GFP_NOFS)) |
| 410 | return page; |
| 411 | |
| 412 | /* |
| 413 | * If we already have a page in the page cache at index we are |
| 414 | * done. Upper layers don't care if it is uptodate after the |
| 415 | * readpages call itself as every page gets checked again once |
| 416 | * actually needed. |
| 417 | */ |
| 418 | *done += PAGE_SIZE; |
| 419 | put_page(page); |
| 420 | } |
| 421 | |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | static loff_t |
| 426 | iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length, |
| 427 | void *data, struct iomap *iomap) |
| 428 | { |
| 429 | struct iomap_readpage_ctx *ctx = data; |
| 430 | loff_t done, ret; |
| 431 | |
| 432 | for (done = 0; done < length; done += ret) { |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 433 | if (ctx->cur_page && offset_in_page(pos + done) == 0) { |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 434 | if (!ctx->cur_page_in_bio) |
| 435 | unlock_page(ctx->cur_page); |
| 436 | put_page(ctx->cur_page); |
| 437 | ctx->cur_page = NULL; |
| 438 | } |
| 439 | if (!ctx->cur_page) { |
| 440 | ctx->cur_page = iomap_next_page(inode, ctx->pages, |
| 441 | pos, length, &done); |
| 442 | if (!ctx->cur_page) |
| 443 | break; |
| 444 | ctx->cur_page_in_bio = false; |
| 445 | } |
| 446 | ret = iomap_readpage_actor(inode, pos + done, length - done, |
| 447 | ctx, iomap); |
| 448 | } |
| 449 | |
| 450 | return done; |
| 451 | } |
| 452 | |
| 453 | int |
| 454 | iomap_readpages(struct address_space *mapping, struct list_head *pages, |
| 455 | unsigned nr_pages, const struct iomap_ops *ops) |
| 456 | { |
| 457 | struct iomap_readpage_ctx ctx = { |
| 458 | .pages = pages, |
| 459 | .is_readahead = true, |
| 460 | }; |
| 461 | loff_t pos = page_offset(list_entry(pages->prev, struct page, lru)); |
| 462 | loff_t last = page_offset(list_entry(pages->next, struct page, lru)); |
| 463 | loff_t length = last - pos + PAGE_SIZE, ret = 0; |
| 464 | |
| 465 | while (length > 0) { |
| 466 | ret = iomap_apply(mapping->host, pos, length, 0, ops, |
| 467 | &ctx, iomap_readpages_actor); |
| 468 | if (ret <= 0) { |
| 469 | WARN_ON_ONCE(ret == 0); |
| 470 | goto done; |
| 471 | } |
| 472 | pos += ret; |
| 473 | length -= ret; |
| 474 | } |
| 475 | ret = 0; |
| 476 | done: |
| 477 | if (ctx.bio) |
| 478 | submit_bio(ctx.bio); |
| 479 | if (ctx.cur_page) { |
| 480 | if (!ctx.cur_page_in_bio) |
| 481 | unlock_page(ctx.cur_page); |
| 482 | put_page(ctx.cur_page); |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Check that we didn't lose a page due to the arcance calling |
| 487 | * conventions.. |
| 488 | */ |
| 489 | WARN_ON_ONCE(!ret && !list_empty(ctx.pages)); |
| 490 | return ret; |
| 491 | } |
| 492 | EXPORT_SYMBOL_GPL(iomap_readpages); |
| 493 | |
Eric Sandeen | 3cc31fa | 2018-12-21 08:42:50 -0800 | [diff] [blame] | 494 | /* |
| 495 | * iomap_is_partially_uptodate checks whether blocks within a page are |
| 496 | * uptodate or not. |
| 497 | * |
| 498 | * Returns true if all blocks which correspond to a file portion |
| 499 | * we want to read within the page are uptodate. |
| 500 | */ |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 501 | int |
| 502 | iomap_is_partially_uptodate(struct page *page, unsigned long from, |
| 503 | unsigned long count) |
| 504 | { |
| 505 | struct iomap_page *iop = to_iomap_page(page); |
| 506 | struct inode *inode = page->mapping->host; |
Eric Sandeen | 3cc31fa | 2018-12-21 08:42:50 -0800 | [diff] [blame] | 507 | unsigned len, first, last; |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 508 | unsigned i; |
| 509 | |
Eric Sandeen | 3cc31fa | 2018-12-21 08:42:50 -0800 | [diff] [blame] | 510 | /* Limit range to one page */ |
| 511 | len = min_t(unsigned, PAGE_SIZE - from, count); |
| 512 | |
| 513 | /* First and last blocks in range within page */ |
| 514 | first = from >> inode->i_blkbits; |
| 515 | last = (from + len - 1) >> inode->i_blkbits; |
| 516 | |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 517 | if (iop) { |
| 518 | for (i = first; i <= last; i++) |
| 519 | if (!test_bit(i, iop->uptodate)) |
| 520 | return 0; |
| 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate); |
| 527 | |
| 528 | int |
| 529 | iomap_releasepage(struct page *page, gfp_t gfp_mask) |
| 530 | { |
| 531 | /* |
| 532 | * mm accommodates an old ext3 case where clean pages might not have had |
| 533 | * the dirty bit cleared. Thus, it can send actual dirty pages to |
| 534 | * ->releasepage() via shrink_active_list(), skip those here. |
| 535 | */ |
| 536 | if (PageDirty(page) || PageWriteback(page)) |
| 537 | return 0; |
| 538 | iomap_page_release(page); |
| 539 | return 1; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(iomap_releasepage); |
| 542 | |
| 543 | void |
| 544 | iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len) |
| 545 | { |
| 546 | /* |
| 547 | * If we are invalidating the entire page, clear the dirty state from it |
| 548 | * and release it to avoid unnecessary buildup of the LRU. |
| 549 | */ |
| 550 | if (offset == 0 && len == PAGE_SIZE) { |
| 551 | WARN_ON_ONCE(PageWriteback(page)); |
| 552 | cancel_dirty_page(page); |
| 553 | iomap_page_release(page); |
| 554 | } |
| 555 | } |
| 556 | EXPORT_SYMBOL_GPL(iomap_invalidatepage); |
| 557 | |
| 558 | #ifdef CONFIG_MIGRATION |
| 559 | int |
| 560 | iomap_migrate_page(struct address_space *mapping, struct page *newpage, |
| 561 | struct page *page, enum migrate_mode mode) |
| 562 | { |
| 563 | int ret; |
| 564 | |
Jan Kara | ab41ee6 | 2018-12-28 00:39:20 -0800 | [diff] [blame] | 565 | ret = migrate_page_move_mapping(mapping, newpage, page, mode, 0); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 566 | if (ret != MIGRATEPAGE_SUCCESS) |
| 567 | return ret; |
| 568 | |
| 569 | if (page_has_private(page)) { |
| 570 | ClearPagePrivate(page); |
Piotr Jaroszynski | 8e47a45 | 2019-01-27 08:46:45 -0800 | [diff] [blame] | 571 | get_page(newpage); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 572 | set_page_private(newpage, page_private(page)); |
| 573 | set_page_private(page, 0); |
Piotr Jaroszynski | 8e47a45 | 2019-01-27 08:46:45 -0800 | [diff] [blame] | 574 | put_page(page); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 575 | SetPagePrivate(newpage); |
| 576 | } |
| 577 | |
| 578 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 579 | migrate_page_copy(newpage, page); |
| 580 | else |
| 581 | migrate_page_states(newpage, page); |
| 582 | return MIGRATEPAGE_SUCCESS; |
| 583 | } |
| 584 | EXPORT_SYMBOL_GPL(iomap_migrate_page); |
| 585 | #endif /* CONFIG_MIGRATION */ |
| 586 | |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 587 | static void |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 588 | iomap_write_failed(struct inode *inode, loff_t pos, unsigned len) |
| 589 | { |
| 590 | loff_t i_size = i_size_read(inode); |
| 591 | |
| 592 | /* |
| 593 | * Only truncate newly allocated pages beyoned EOF, even if the |
| 594 | * write started inside the existing inode size. |
| 595 | */ |
| 596 | if (pos + len > i_size) |
| 597 | truncate_pagecache_range(inode, max(pos, i_size), pos + len); |
| 598 | } |
| 599 | |
| 600 | static int |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 601 | iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page, |
| 602 | unsigned poff, unsigned plen, unsigned from, unsigned to, |
| 603 | struct iomap *iomap) |
| 604 | { |
| 605 | struct bio_vec bvec; |
| 606 | struct bio bio; |
| 607 | |
| 608 | if (iomap->type != IOMAP_MAPPED || block_start >= i_size_read(inode)) { |
| 609 | zero_user_segments(page, poff, from, to, poff + plen); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 610 | iomap_set_range_uptodate(page, poff, plen); |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | bio_init(&bio, &bvec, 1); |
| 615 | bio.bi_opf = REQ_OP_READ; |
| 616 | bio.bi_iter.bi_sector = iomap_sector(iomap, block_start); |
| 617 | bio_set_dev(&bio, iomap->bdev); |
| 618 | __bio_add_page(&bio, page, plen, poff); |
| 619 | return submit_bio_wait(&bio); |
| 620 | } |
| 621 | |
| 622 | static int |
| 623 | __iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, |
| 624 | struct page *page, struct iomap *iomap) |
| 625 | { |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 626 | struct iomap_page *iop = iomap_page_create(inode, page); |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 627 | loff_t block_size = i_blocksize(inode); |
| 628 | loff_t block_start = pos & ~(block_size - 1); |
| 629 | loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1); |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 630 | unsigned from = offset_in_page(pos), to = from + len, poff, plen; |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 631 | int status = 0; |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 632 | |
| 633 | if (PageUptodate(page)) |
| 634 | return 0; |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 635 | |
| 636 | do { |
| 637 | iomap_adjust_read_range(inode, iop, &block_start, |
| 638 | block_end - block_start, &poff, &plen); |
| 639 | if (plen == 0) |
| 640 | break; |
| 641 | |
| 642 | if ((from > poff && from < poff + plen) || |
| 643 | (to > poff && to < poff + plen)) { |
| 644 | status = iomap_read_page_sync(inode, block_start, page, |
| 645 | poff, plen, from, to, iomap); |
| 646 | if (status) |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | } while ((block_start += plen) < block_end); |
| 651 | |
| 652 | return status; |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static int |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 656 | iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags, |
| 657 | struct page **pagep, struct iomap *iomap) |
| 658 | { |
Andreas Gruenbacher | df0db3e | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 659 | const struct iomap_page_ops *page_ops = iomap->page_ops; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 660 | pgoff_t index = pos >> PAGE_SHIFT; |
| 661 | struct page *page; |
| 662 | int status = 0; |
| 663 | |
| 664 | BUG_ON(pos + len > iomap->offset + iomap->length); |
| 665 | |
Michal Hocko | d1908f5 | 2017-02-03 13:13:26 -0800 | [diff] [blame] | 666 | if (fatal_signal_pending(current)) |
| 667 | return -EINTR; |
| 668 | |
Andreas Gruenbacher | df0db3e | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 669 | if (page_ops && page_ops->page_prepare) { |
| 670 | status = page_ops->page_prepare(inode, pos, len, iomap); |
| 671 | if (status) |
| 672 | return status; |
| 673 | } |
| 674 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 675 | page = grab_cache_page_write_begin(inode->i_mapping, index, flags); |
Andreas Gruenbacher | df0db3e | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 676 | if (!page) { |
| 677 | status = -ENOMEM; |
| 678 | goto out_no_page; |
| 679 | } |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 680 | |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 681 | if (iomap->type == IOMAP_INLINE) |
| 682 | iomap_read_inline_data(inode, page, iomap); |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 683 | else if (iomap->flags & IOMAP_F_BUFFER_HEAD) |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 684 | status = __block_write_begin_int(page, pos, len, NULL, iomap); |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 685 | else |
| 686 | status = __iomap_write_begin(inode, pos, len, page, iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 687 | |
Andreas Gruenbacher | df0db3e | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 688 | if (unlikely(status)) |
| 689 | goto out_unlock; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 690 | |
| 691 | *pagep = page; |
Andreas Gruenbacher | df0db3e | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 692 | return 0; |
| 693 | |
| 694 | out_unlock: |
| 695 | unlock_page(page); |
| 696 | put_page(page); |
| 697 | iomap_write_failed(inode, pos, len); |
| 698 | |
| 699 | out_no_page: |
| 700 | if (page_ops && page_ops->page_done) |
| 701 | page_ops->page_done(inode, pos, 0, NULL, iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 702 | return status; |
| 703 | } |
| 704 | |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 705 | int |
| 706 | iomap_set_page_dirty(struct page *page) |
| 707 | { |
| 708 | struct address_space *mapping = page_mapping(page); |
| 709 | int newly_dirty; |
| 710 | |
| 711 | if (unlikely(!mapping)) |
| 712 | return !TestSetPageDirty(page); |
| 713 | |
| 714 | /* |
| 715 | * Lock out page->mem_cgroup migration to keep PageDirty |
| 716 | * synchronized with per-memcg dirty page counters. |
| 717 | */ |
| 718 | lock_page_memcg(page); |
| 719 | newly_dirty = !TestSetPageDirty(page); |
| 720 | if (newly_dirty) |
| 721 | __set_page_dirty(page, mapping, 0); |
| 722 | unlock_page_memcg(page); |
| 723 | |
| 724 | if (newly_dirty) |
| 725 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
| 726 | return newly_dirty; |
| 727 | } |
| 728 | EXPORT_SYMBOL_GPL(iomap_set_page_dirty); |
| 729 | |
| 730 | static int |
| 731 | __iomap_write_end(struct inode *inode, loff_t pos, unsigned len, |
| 732 | unsigned copied, struct page *page, struct iomap *iomap) |
| 733 | { |
| 734 | flush_dcache_page(page); |
| 735 | |
| 736 | /* |
| 737 | * The blocks that were entirely written will now be uptodate, so we |
| 738 | * don't have to worry about a readpage reading them and overwriting a |
| 739 | * partial write. However if we have encountered a short write and only |
| 740 | * partially written into a block, it will not be marked uptodate, so a |
| 741 | * readpage might come in and destroy our partial write. |
| 742 | * |
| 743 | * Do the simplest thing, and just treat any short write to a non |
| 744 | * uptodate page as a zero-length write, and force the caller to redo |
| 745 | * the whole thing. |
| 746 | */ |
Christoph Hellwig | dbc582b | 2019-04-30 08:45:33 -0700 | [diff] [blame] | 747 | if (unlikely(copied < len && !PageUptodate(page))) |
| 748 | return 0; |
| 749 | iomap_set_range_uptodate(page, offset_in_page(pos), len); |
| 750 | iomap_set_page_dirty(page); |
| 751 | return copied; |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 754 | static int |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 755 | iomap_write_end_inline(struct inode *inode, struct page *page, |
| 756 | struct iomap *iomap, loff_t pos, unsigned copied) |
| 757 | { |
| 758 | void *addr; |
| 759 | |
| 760 | WARN_ON_ONCE(!PageUptodate(page)); |
| 761 | BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data)); |
| 762 | |
| 763 | addr = kmap_atomic(page); |
| 764 | memcpy(iomap->inline_data + pos, addr + pos, copied); |
| 765 | kunmap_atomic(addr); |
| 766 | |
| 767 | mark_inode_dirty(inode); |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 768 | return copied; |
| 769 | } |
| 770 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 771 | static int |
| 772 | iomap_write_end(struct inode *inode, loff_t pos, unsigned len, |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 773 | unsigned copied, struct page *page, struct iomap *iomap) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 774 | { |
Andreas Gruenbacher | df0db3e | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 775 | const struct iomap_page_ops *page_ops = iomap->page_ops; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 776 | int ret; |
| 777 | |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 778 | if (iomap->type == IOMAP_INLINE) { |
| 779 | ret = iomap_write_end_inline(inode, page, iomap, pos, copied); |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 780 | } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) { |
Christoph Hellwig | dbc582b | 2019-04-30 08:45:33 -0700 | [diff] [blame] | 781 | ret = block_write_end(NULL, inode->i_mapping, pos, len, copied, |
| 782 | page, NULL); |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 783 | } else { |
| 784 | ret = __iomap_write_end(inode, pos, len, copied, page, iomap); |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Andreas Gruenbacher | 26ddb1f | 2019-04-30 08:45:33 -0700 | [diff] [blame] | 787 | __generic_write_end(inode, pos, ret, page); |
Andreas Gruenbacher | df0db3e | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 788 | if (page_ops && page_ops->page_done) |
| 789 | page_ops->page_done(inode, pos, copied, page, iomap); |
Andreas Gruenbacher | 7a77dad | 2019-04-30 08:45:34 -0700 | [diff] [blame] | 790 | put_page(page); |
Christoph Hellwig | 63899c6 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 791 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 792 | if (ret < len) |
| 793 | iomap_write_failed(inode, pos, len); |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | static loff_t |
| 798 | iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
| 799 | struct iomap *iomap) |
| 800 | { |
| 801 | struct iov_iter *i = data; |
| 802 | long status = 0; |
| 803 | ssize_t written = 0; |
| 804 | unsigned int flags = AOP_FLAG_NOFS; |
| 805 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 806 | do { |
| 807 | struct page *page; |
| 808 | unsigned long offset; /* Offset into pagecache page */ |
| 809 | unsigned long bytes; /* Bytes to write to page */ |
| 810 | size_t copied; /* Bytes copied from user */ |
| 811 | |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 812 | offset = offset_in_page(pos); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 813 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
| 814 | iov_iter_count(i)); |
| 815 | again: |
| 816 | if (bytes > length) |
| 817 | bytes = length; |
| 818 | |
| 819 | /* |
| 820 | * Bring in the user page that we will copy from _first_. |
| 821 | * Otherwise there's a nasty deadlock on copying from the |
| 822 | * same page as we're writing to, without it being marked |
| 823 | * up-to-date. |
| 824 | * |
| 825 | * Not only is this an optimisation, but it is also required |
| 826 | * to check that the address is actually valid, when atomic |
| 827 | * usercopies are used, below. |
| 828 | */ |
| 829 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { |
| 830 | status = -EFAULT; |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | status = iomap_write_begin(inode, pos, bytes, flags, &page, |
| 835 | iomap); |
| 836 | if (unlikely(status)) |
| 837 | break; |
| 838 | |
| 839 | if (mapping_writably_mapped(inode->i_mapping)) |
| 840 | flush_dcache_page(page); |
| 841 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 842 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 843 | |
| 844 | flush_dcache_page(page); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 845 | |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 846 | status = iomap_write_end(inode, pos, bytes, copied, page, |
| 847 | iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 848 | if (unlikely(status < 0)) |
| 849 | break; |
| 850 | copied = status; |
| 851 | |
| 852 | cond_resched(); |
| 853 | |
| 854 | iov_iter_advance(i, copied); |
| 855 | if (unlikely(copied == 0)) { |
| 856 | /* |
| 857 | * If we were unable to copy any data at all, we must |
| 858 | * fall back to a single segment length write. |
| 859 | * |
| 860 | * If we didn't fallback here, we could livelock |
| 861 | * because not all segments in the iov can be copied at |
| 862 | * once without a pagefault. |
| 863 | */ |
| 864 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
| 865 | iov_iter_single_seg_count(i)); |
| 866 | goto again; |
| 867 | } |
| 868 | pos += copied; |
| 869 | written += copied; |
| 870 | length -= copied; |
| 871 | |
| 872 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 873 | } while (iov_iter_count(i) && length); |
| 874 | |
| 875 | return written ? written : status; |
| 876 | } |
| 877 | |
| 878 | ssize_t |
| 879 | iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 880 | const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 881 | { |
| 882 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 883 | loff_t pos = iocb->ki_pos, ret = 0, written = 0; |
| 884 | |
| 885 | while (iov_iter_count(iter)) { |
| 886 | ret = iomap_apply(inode, pos, iov_iter_count(iter), |
| 887 | IOMAP_WRITE, ops, iter, iomap_write_actor); |
| 888 | if (ret <= 0) |
| 889 | break; |
| 890 | pos += ret; |
| 891 | written += ret; |
| 892 | } |
| 893 | |
| 894 | return written ? written : ret; |
| 895 | } |
| 896 | EXPORT_SYMBOL_GPL(iomap_file_buffered_write); |
| 897 | |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 898 | static struct page * |
| 899 | __iomap_read_page(struct inode *inode, loff_t offset) |
| 900 | { |
| 901 | struct address_space *mapping = inode->i_mapping; |
| 902 | struct page *page; |
| 903 | |
| 904 | page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL); |
| 905 | if (IS_ERR(page)) |
| 906 | return page; |
| 907 | if (!PageUptodate(page)) { |
| 908 | put_page(page); |
| 909 | return ERR_PTR(-EIO); |
| 910 | } |
| 911 | return page; |
| 912 | } |
| 913 | |
| 914 | static loff_t |
| 915 | iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
| 916 | struct iomap *iomap) |
| 917 | { |
| 918 | long status = 0; |
| 919 | ssize_t written = 0; |
| 920 | |
| 921 | do { |
| 922 | struct page *page, *rpage; |
| 923 | unsigned long offset; /* Offset into pagecache page */ |
| 924 | unsigned long bytes; /* Bytes to write to page */ |
| 925 | |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 926 | offset = offset_in_page(pos); |
Christoph Hellwig | e28ae8e | 2017-08-11 12:45:35 -0700 | [diff] [blame] | 927 | bytes = min_t(loff_t, PAGE_SIZE - offset, length); |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 928 | |
| 929 | rpage = __iomap_read_page(inode, pos); |
| 930 | if (IS_ERR(rpage)) |
| 931 | return PTR_ERR(rpage); |
| 932 | |
| 933 | status = iomap_write_begin(inode, pos, bytes, |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 934 | AOP_FLAG_NOFS, &page, iomap); |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 935 | put_page(rpage); |
| 936 | if (unlikely(status)) |
| 937 | return status; |
| 938 | |
| 939 | WARN_ON_ONCE(!PageUptodate(page)); |
| 940 | |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 941 | status = iomap_write_end(inode, pos, bytes, bytes, page, iomap); |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 942 | if (unlikely(status <= 0)) { |
| 943 | if (WARN_ON_ONCE(status == 0)) |
| 944 | return -EIO; |
| 945 | return status; |
| 946 | } |
| 947 | |
| 948 | cond_resched(); |
| 949 | |
| 950 | pos += status; |
| 951 | written += status; |
| 952 | length -= status; |
| 953 | |
| 954 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 955 | } while (length); |
| 956 | |
| 957 | return written; |
| 958 | } |
| 959 | |
| 960 | int |
| 961 | iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 962 | const struct iomap_ops *ops) |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 963 | { |
| 964 | loff_t ret; |
| 965 | |
| 966 | while (len) { |
| 967 | ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL, |
| 968 | iomap_dirty_actor); |
| 969 | if (ret <= 0) |
| 970 | return ret; |
| 971 | pos += ret; |
| 972 | len -= ret; |
| 973 | } |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | EXPORT_SYMBOL_GPL(iomap_file_dirty); |
| 978 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 979 | static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset, |
| 980 | unsigned bytes, struct iomap *iomap) |
| 981 | { |
| 982 | struct page *page; |
| 983 | int status; |
| 984 | |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 985 | status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page, |
| 986 | iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 987 | if (status) |
| 988 | return status; |
| 989 | |
| 990 | zero_user(page, offset, bytes); |
| 991 | mark_page_accessed(page); |
| 992 | |
Andreas Gruenbacher | 19e0c58 | 2018-06-19 15:10:56 -0700 | [diff] [blame] | 993 | return iomap_write_end(inode, pos, bytes, bytes, page, iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 994 | } |
| 995 | |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 996 | static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes, |
| 997 | struct iomap *iomap) |
| 998 | { |
Christoph Hellwig | 57fc505 | 2018-06-01 09:03:08 -0700 | [diff] [blame] | 999 | return __dax_zero_page_range(iomap->bdev, iomap->dax_dev, |
| 1000 | iomap_sector(iomap, pos & PAGE_MASK), offset, bytes); |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 1001 | } |
| 1002 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1003 | static loff_t |
| 1004 | iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count, |
| 1005 | void *data, struct iomap *iomap) |
| 1006 | { |
| 1007 | bool *did_zero = data; |
| 1008 | loff_t written = 0; |
| 1009 | int status; |
| 1010 | |
| 1011 | /* already zeroed? we're done. */ |
| 1012 | if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) |
| 1013 | return count; |
| 1014 | |
| 1015 | do { |
| 1016 | unsigned offset, bytes; |
| 1017 | |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 1018 | offset = offset_in_page(pos); |
Christoph Hellwig | e28ae8e | 2017-08-11 12:45:35 -0700 | [diff] [blame] | 1019 | bytes = min_t(loff_t, PAGE_SIZE - offset, count); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1020 | |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 1021 | if (IS_DAX(inode)) |
| 1022 | status = iomap_dax_zero(pos, offset, bytes, iomap); |
| 1023 | else |
| 1024 | status = iomap_zero(inode, pos, offset, bytes, iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1025 | if (status < 0) |
| 1026 | return status; |
| 1027 | |
| 1028 | pos += bytes; |
| 1029 | count -= bytes; |
| 1030 | written += bytes; |
| 1031 | if (did_zero) |
| 1032 | *did_zero = true; |
| 1033 | } while (count > 0); |
| 1034 | |
| 1035 | return written; |
| 1036 | } |
| 1037 | |
| 1038 | int |
| 1039 | iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1040 | const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1041 | { |
| 1042 | loff_t ret; |
| 1043 | |
| 1044 | while (len > 0) { |
| 1045 | ret = iomap_apply(inode, pos, len, IOMAP_ZERO, |
| 1046 | ops, did_zero, iomap_zero_range_actor); |
| 1047 | if (ret <= 0) |
| 1048 | return ret; |
| 1049 | |
| 1050 | pos += ret; |
| 1051 | len -= ret; |
| 1052 | } |
| 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | EXPORT_SYMBOL_GPL(iomap_zero_range); |
| 1057 | |
| 1058 | int |
| 1059 | iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1060 | const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1061 | { |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 1062 | unsigned int blocksize = i_blocksize(inode); |
| 1063 | unsigned int off = pos & (blocksize - 1); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1064 | |
| 1065 | /* Block boundary? Nothing to do */ |
| 1066 | if (!off) |
| 1067 | return 0; |
| 1068 | return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops); |
| 1069 | } |
| 1070 | EXPORT_SYMBOL_GPL(iomap_truncate_page); |
| 1071 | |
| 1072 | static loff_t |
| 1073 | iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length, |
| 1074 | void *data, struct iomap *iomap) |
| 1075 | { |
| 1076 | struct page *page = data; |
| 1077 | int ret; |
| 1078 | |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 1079 | if (iomap->flags & IOMAP_F_BUFFER_HEAD) { |
| 1080 | ret = __block_write_begin_int(page, pos, length, NULL, iomap); |
| 1081 | if (ret) |
| 1082 | return ret; |
| 1083 | block_commit_write(page, 0, length); |
| 1084 | } else { |
| 1085 | WARN_ON_ONCE(!PageUptodate(page)); |
Christoph Hellwig | 9dc55f1 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 1086 | iomap_page_create(inode, page); |
Brian Foster | 561295a | 2018-09-29 13:51:01 +1000 | [diff] [blame] | 1087 | set_page_dirty(page); |
Christoph Hellwig | c03cea4 | 2018-06-19 15:10:58 -0700 | [diff] [blame] | 1088 | } |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1089 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1090 | return length; |
| 1091 | } |
| 1092 | |
Souptick Joarder | 5780a02 | 2018-10-26 15:02:59 -0700 | [diff] [blame] | 1093 | vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1094 | { |
| 1095 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1096 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1097 | unsigned long length; |
| 1098 | loff_t offset, size; |
| 1099 | ssize_t ret; |
| 1100 | |
| 1101 | lock_page(page); |
| 1102 | size = i_size_read(inode); |
| 1103 | if ((page->mapping != inode->i_mapping) || |
| 1104 | (page_offset(page) > size)) { |
| 1105 | /* We overload EFAULT to mean page got truncated */ |
| 1106 | ret = -EFAULT; |
| 1107 | goto out_unlock; |
| 1108 | } |
| 1109 | |
| 1110 | /* page is wholly or partially inside EOF */ |
| 1111 | if (((page->index + 1) << PAGE_SHIFT) > size) |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 1112 | length = offset_in_page(size); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1113 | else |
| 1114 | length = PAGE_SIZE; |
| 1115 | |
| 1116 | offset = page_offset(page); |
| 1117 | while (length > 0) { |
Jan Kara | 9484ab1 | 2016-11-10 10:26:50 +1100 | [diff] [blame] | 1118 | ret = iomap_apply(inode, offset, length, |
| 1119 | IOMAP_WRITE | IOMAP_FAULT, ops, page, |
| 1120 | iomap_page_mkwrite_actor); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1121 | if (unlikely(ret <= 0)) |
| 1122 | goto out_unlock; |
| 1123 | offset += ret; |
| 1124 | length -= ret; |
| 1125 | } |
| 1126 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1127 | wait_for_stable_page(page); |
Christoph Hellwig | e7647fb | 2017-08-29 10:08:41 -0700 | [diff] [blame] | 1128 | return VM_FAULT_LOCKED; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1129 | out_unlock: |
| 1130 | unlock_page(page); |
Christoph Hellwig | e7647fb | 2017-08-29 10:08:41 -0700 | [diff] [blame] | 1131 | return block_page_mkwrite_return(ret); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 1132 | } |
| 1133 | EXPORT_SYMBOL_GPL(iomap_page_mkwrite); |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1134 | |
| 1135 | struct fiemap_ctx { |
| 1136 | struct fiemap_extent_info *fi; |
| 1137 | struct iomap prev; |
| 1138 | }; |
| 1139 | |
| 1140 | static int iomap_to_fiemap(struct fiemap_extent_info *fi, |
| 1141 | struct iomap *iomap, u32 flags) |
| 1142 | { |
| 1143 | switch (iomap->type) { |
| 1144 | case IOMAP_HOLE: |
| 1145 | /* skip holes */ |
| 1146 | return 0; |
| 1147 | case IOMAP_DELALLOC: |
| 1148 | flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN; |
| 1149 | break; |
Christoph Hellwig | 19319b5 | 2018-06-01 09:03:06 -0700 | [diff] [blame] | 1150 | case IOMAP_MAPPED: |
| 1151 | break; |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1152 | case IOMAP_UNWRITTEN: |
| 1153 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 1154 | break; |
Christoph Hellwig | 19319b5 | 2018-06-01 09:03:06 -0700 | [diff] [blame] | 1155 | case IOMAP_INLINE: |
| 1156 | flags |= FIEMAP_EXTENT_DATA_INLINE; |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1157 | break; |
| 1158 | } |
| 1159 | |
Christoph Hellwig | 17de0a9 | 2016-08-29 11:33:58 +1000 | [diff] [blame] | 1160 | if (iomap->flags & IOMAP_F_MERGED) |
| 1161 | flags |= FIEMAP_EXTENT_MERGED; |
Darrick J. Wong | e43c460 | 2016-09-19 10:13:02 +1000 | [diff] [blame] | 1162 | if (iomap->flags & IOMAP_F_SHARED) |
| 1163 | flags |= FIEMAP_EXTENT_SHARED; |
Christoph Hellwig | 17de0a9 | 2016-08-29 11:33:58 +1000 | [diff] [blame] | 1164 | |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1165 | return fiemap_fill_next_extent(fi, iomap->offset, |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 1166 | iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0, |
Christoph Hellwig | 17de0a9 | 2016-08-29 11:33:58 +1000 | [diff] [blame] | 1167 | iomap->length, flags); |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | static loff_t |
| 1171 | iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
| 1172 | struct iomap *iomap) |
| 1173 | { |
| 1174 | struct fiemap_ctx *ctx = data; |
| 1175 | loff_t ret = length; |
| 1176 | |
| 1177 | if (iomap->type == IOMAP_HOLE) |
| 1178 | return length; |
| 1179 | |
| 1180 | ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0); |
| 1181 | ctx->prev = *iomap; |
| 1182 | switch (ret) { |
| 1183 | case 0: /* success */ |
| 1184 | return length; |
| 1185 | case 1: /* extent array full */ |
| 1186 | return 0; |
| 1187 | default: |
| 1188 | return ret; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1193 | loff_t start, loff_t len, const struct iomap_ops *ops) |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1194 | { |
| 1195 | struct fiemap_ctx ctx; |
| 1196 | loff_t ret; |
| 1197 | |
| 1198 | memset(&ctx, 0, sizeof(ctx)); |
| 1199 | ctx.fi = fi; |
| 1200 | ctx.prev.type = IOMAP_HOLE; |
| 1201 | |
| 1202 | ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC); |
| 1203 | if (ret) |
| 1204 | return ret; |
| 1205 | |
Dave Chinner | 8896b8f | 2016-08-17 08:41:10 +1000 | [diff] [blame] | 1206 | if (fi->fi_flags & FIEMAP_FLAG_SYNC) { |
| 1207 | ret = filemap_write_and_wait(inode->i_mapping); |
| 1208 | if (ret) |
| 1209 | return ret; |
| 1210 | } |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1211 | |
| 1212 | while (len > 0) { |
Christoph Hellwig | d33fd77 | 2016-10-20 15:51:28 +1100 | [diff] [blame] | 1213 | ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx, |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1214 | iomap_fiemap_actor); |
Dave Chinner | ac2dc05 | 2016-08-17 08:41:34 +1000 | [diff] [blame] | 1215 | /* inode with no (attribute) mapping will give ENOENT */ |
| 1216 | if (ret == -ENOENT) |
| 1217 | break; |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 1218 | if (ret < 0) |
| 1219 | return ret; |
| 1220 | if (ret == 0) |
| 1221 | break; |
| 1222 | |
| 1223 | start += ret; |
| 1224 | len -= ret; |
| 1225 | } |
| 1226 | |
| 1227 | if (ctx.prev.type != IOMAP_HOLE) { |
| 1228 | ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST); |
| 1229 | if (ret < 0) |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | EXPORT_SYMBOL_GPL(iomap_fiemap); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1236 | |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1237 | /* |
| 1238 | * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff. |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1239 | * Returns true if found and updates @lastoff to the offset in file. |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1240 | */ |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1241 | static bool |
| 1242 | page_seek_hole_data(struct inode *inode, struct page *page, loff_t *lastoff, |
| 1243 | int whence) |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1244 | { |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1245 | const struct address_space_operations *ops = inode->i_mapping->a_ops; |
| 1246 | unsigned int bsize = i_blocksize(inode), off; |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1247 | bool seek_data = whence == SEEK_DATA; |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1248 | loff_t poff = page_offset(page); |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1249 | |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1250 | if (WARN_ON_ONCE(*lastoff >= poff + PAGE_SIZE)) |
| 1251 | return false; |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1252 | |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1253 | if (*lastoff < poff) { |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1254 | /* |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1255 | * Last offset smaller than the start of the page means we found |
| 1256 | * a hole: |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1257 | */ |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1258 | if (whence == SEEK_HOLE) |
| 1259 | return true; |
| 1260 | *lastoff = poff; |
| 1261 | } |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1262 | |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1263 | /* |
| 1264 | * Just check the page unless we can and should check block ranges: |
| 1265 | */ |
| 1266 | if (bsize == PAGE_SIZE || !ops->is_partially_uptodate) |
| 1267 | return PageUptodate(page) == seek_data; |
| 1268 | |
| 1269 | lock_page(page); |
| 1270 | if (unlikely(page->mapping != inode->i_mapping)) |
| 1271 | goto out_unlock_not_found; |
| 1272 | |
| 1273 | for (off = 0; off < PAGE_SIZE; off += bsize) { |
Andreas Gruenbacher | 10259de | 2018-08-10 11:46:14 -0700 | [diff] [blame] | 1274 | if (offset_in_page(*lastoff) >= off + bsize) |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1275 | continue; |
| 1276 | if (ops->is_partially_uptodate(page, off, bsize) == seek_data) { |
| 1277 | unlock_page(page); |
| 1278 | return true; |
| 1279 | } |
| 1280 | *lastoff = poff + off + bsize; |
| 1281 | } |
| 1282 | |
| 1283 | out_unlock_not_found: |
| 1284 | unlock_page(page); |
| 1285 | return false; |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Seek for SEEK_DATA / SEEK_HOLE in the page cache. |
| 1290 | * |
| 1291 | * Within unwritten extents, the page cache determines which parts are holes |
Christoph Hellwig | bd56b3e | 2018-06-01 09:05:14 -0700 | [diff] [blame] | 1292 | * and which are data: uptodate buffer heads count as data; everything else |
| 1293 | * counts as a hole. |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1294 | * |
| 1295 | * Returns the resulting offset on successs, and -ENOENT otherwise. |
| 1296 | */ |
| 1297 | static loff_t |
| 1298 | page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length, |
| 1299 | int whence) |
| 1300 | { |
| 1301 | pgoff_t index = offset >> PAGE_SHIFT; |
| 1302 | pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE); |
| 1303 | loff_t lastoff = offset; |
| 1304 | struct pagevec pvec; |
| 1305 | |
| 1306 | if (length <= 0) |
| 1307 | return -ENOENT; |
| 1308 | |
| 1309 | pagevec_init(&pvec); |
| 1310 | |
| 1311 | do { |
| 1312 | unsigned nr_pages, i; |
| 1313 | |
| 1314 | nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index, |
| 1315 | end - 1); |
| 1316 | if (nr_pages == 0) |
| 1317 | break; |
| 1318 | |
| 1319 | for (i = 0; i < nr_pages; i++) { |
| 1320 | struct page *page = pvec.pages[i]; |
| 1321 | |
Christoph Hellwig | afd9d6a | 2018-06-01 09:05:15 -0700 | [diff] [blame] | 1322 | if (page_seek_hole_data(inode, page, &lastoff, whence)) |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1323 | goto check_range; |
Christoph Hellwig | 8a78cb1 | 2018-06-01 09:04:40 -0700 | [diff] [blame] | 1324 | lastoff = page_offset(page) + PAGE_SIZE; |
| 1325 | } |
| 1326 | pagevec_release(&pvec); |
| 1327 | } while (index < end); |
| 1328 | |
| 1329 | /* When no page at lastoff and we are not done, we found a hole. */ |
| 1330 | if (whence != SEEK_HOLE) |
| 1331 | goto not_found; |
| 1332 | |
| 1333 | check_range: |
| 1334 | if (lastoff < offset + length) |
| 1335 | goto out; |
| 1336 | not_found: |
| 1337 | lastoff = -ENOENT; |
| 1338 | out: |
| 1339 | pagevec_release(&pvec); |
| 1340 | return lastoff; |
| 1341 | } |
| 1342 | |
| 1343 | |
Andreas Gruenbacher | 0ed3b0d | 2017-06-29 11:43:21 -0700 | [diff] [blame] | 1344 | static loff_t |
| 1345 | iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length, |
| 1346 | void *data, struct iomap *iomap) |
| 1347 | { |
| 1348 | switch (iomap->type) { |
| 1349 | case IOMAP_UNWRITTEN: |
| 1350 | offset = page_cache_seek_hole_data(inode, offset, length, |
| 1351 | SEEK_HOLE); |
| 1352 | if (offset < 0) |
| 1353 | return length; |
| 1354 | /* fall through */ |
| 1355 | case IOMAP_HOLE: |
| 1356 | *(loff_t *)data = offset; |
| 1357 | return 0; |
| 1358 | default: |
| 1359 | return length; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | loff_t |
| 1364 | iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops) |
| 1365 | { |
| 1366 | loff_t size = i_size_read(inode); |
| 1367 | loff_t length = size - offset; |
| 1368 | loff_t ret; |
| 1369 | |
Darrick J. Wong | d6ab17f | 2017-07-12 10:26:47 -0700 | [diff] [blame] | 1370 | /* Nothing to be found before or beyond the end of the file. */ |
| 1371 | if (offset < 0 || offset >= size) |
Andreas Gruenbacher | 0ed3b0d | 2017-06-29 11:43:21 -0700 | [diff] [blame] | 1372 | return -ENXIO; |
| 1373 | |
| 1374 | while (length > 0) { |
| 1375 | ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops, |
| 1376 | &offset, iomap_seek_hole_actor); |
| 1377 | if (ret < 0) |
| 1378 | return ret; |
| 1379 | if (ret == 0) |
| 1380 | break; |
| 1381 | |
| 1382 | offset += ret; |
| 1383 | length -= ret; |
| 1384 | } |
| 1385 | |
| 1386 | return offset; |
| 1387 | } |
| 1388 | EXPORT_SYMBOL_GPL(iomap_seek_hole); |
| 1389 | |
| 1390 | static loff_t |
| 1391 | iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length, |
| 1392 | void *data, struct iomap *iomap) |
| 1393 | { |
| 1394 | switch (iomap->type) { |
| 1395 | case IOMAP_HOLE: |
| 1396 | return length; |
| 1397 | case IOMAP_UNWRITTEN: |
| 1398 | offset = page_cache_seek_hole_data(inode, offset, length, |
| 1399 | SEEK_DATA); |
| 1400 | if (offset < 0) |
| 1401 | return length; |
| 1402 | /*FALLTHRU*/ |
| 1403 | default: |
| 1404 | *(loff_t *)data = offset; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | loff_t |
| 1410 | iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops) |
| 1411 | { |
| 1412 | loff_t size = i_size_read(inode); |
| 1413 | loff_t length = size - offset; |
| 1414 | loff_t ret; |
| 1415 | |
Darrick J. Wong | d6ab17f | 2017-07-12 10:26:47 -0700 | [diff] [blame] | 1416 | /* Nothing to be found before or beyond the end of the file. */ |
| 1417 | if (offset < 0 || offset >= size) |
Andreas Gruenbacher | 0ed3b0d | 2017-06-29 11:43:21 -0700 | [diff] [blame] | 1418 | return -ENXIO; |
| 1419 | |
| 1420 | while (length > 0) { |
| 1421 | ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops, |
| 1422 | &offset, iomap_seek_data_actor); |
| 1423 | if (ret < 0) |
| 1424 | return ret; |
| 1425 | if (ret == 0) |
| 1426 | break; |
| 1427 | |
| 1428 | offset += ret; |
| 1429 | length -= ret; |
| 1430 | } |
| 1431 | |
| 1432 | if (length <= 0) |
| 1433 | return -ENXIO; |
| 1434 | return offset; |
| 1435 | } |
| 1436 | EXPORT_SYMBOL_GPL(iomap_seek_data); |
| 1437 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1438 | /* |
| 1439 | * Private flags for iomap_dio, must not overlap with the public ones in |
| 1440 | * iomap.h: |
| 1441 | */ |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1442 | #define IOMAP_DIO_WRITE_FUA (1 << 28) |
Dave Chinner | 4f8ff44 | 2018-05-02 12:54:52 -0700 | [diff] [blame] | 1443 | #define IOMAP_DIO_NEED_SYNC (1 << 29) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1444 | #define IOMAP_DIO_WRITE (1 << 30) |
| 1445 | #define IOMAP_DIO_DIRTY (1 << 31) |
| 1446 | |
| 1447 | struct iomap_dio { |
| 1448 | struct kiocb *iocb; |
| 1449 | iomap_dio_end_io_t *end_io; |
| 1450 | loff_t i_size; |
| 1451 | loff_t size; |
| 1452 | atomic_t ref; |
| 1453 | unsigned flags; |
| 1454 | int error; |
Andreas Gruenbacher | ebf00be | 2018-06-19 15:10:55 -0700 | [diff] [blame] | 1455 | bool wait_for_completion; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1456 | |
| 1457 | union { |
| 1458 | /* used during submission and for synchronous completion: */ |
| 1459 | struct { |
| 1460 | struct iov_iter *iter; |
| 1461 | struct task_struct *waiter; |
| 1462 | struct request_queue *last_queue; |
| 1463 | blk_qc_t cookie; |
| 1464 | } submit; |
| 1465 | |
| 1466 | /* used for aio completion: */ |
| 1467 | struct { |
| 1468 | struct work_struct work; |
| 1469 | } aio; |
| 1470 | }; |
| 1471 | }; |
| 1472 | |
Christoph Hellwig | 81214ba | 2018-12-04 11:12:08 -0700 | [diff] [blame] | 1473 | int iomap_dio_iopoll(struct kiocb *kiocb, bool spin) |
| 1474 | { |
| 1475 | struct request_queue *q = READ_ONCE(kiocb->private); |
| 1476 | |
| 1477 | if (!q) |
| 1478 | return 0; |
| 1479 | return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin); |
| 1480 | } |
| 1481 | EXPORT_SYMBOL_GPL(iomap_dio_iopoll); |
| 1482 | |
| 1483 | static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap, |
| 1484 | struct bio *bio) |
| 1485 | { |
| 1486 | atomic_inc(&dio->ref); |
| 1487 | |
| 1488 | if (dio->iocb->ki_flags & IOCB_HIPRI) |
| 1489 | bio_set_polled(bio, dio->iocb); |
| 1490 | |
| 1491 | dio->submit.last_queue = bdev_get_queue(iomap->bdev); |
| 1492 | dio->submit.cookie = submit_bio(bio); |
| 1493 | } |
| 1494 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1495 | static ssize_t iomap_dio_complete(struct iomap_dio *dio) |
| 1496 | { |
| 1497 | struct kiocb *iocb = dio->iocb; |
Lukas Czerner | 332391a | 2017-09-21 08:16:29 -0600 | [diff] [blame] | 1498 | struct inode *inode = file_inode(iocb->ki_filp); |
Eryu Guan | 5e25c26 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 1499 | loff_t offset = iocb->ki_pos; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1500 | ssize_t ret; |
| 1501 | |
| 1502 | if (dio->end_io) { |
| 1503 | ret = dio->end_io(iocb, |
| 1504 | dio->error ? dio->error : dio->size, |
| 1505 | dio->flags); |
| 1506 | } else { |
| 1507 | ret = dio->error; |
| 1508 | } |
| 1509 | |
| 1510 | if (likely(!ret)) { |
| 1511 | ret = dio->size; |
| 1512 | /* check for short read */ |
Eryu Guan | 5e25c26 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 1513 | if (offset + ret > dio->i_size && |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1514 | !(dio->flags & IOMAP_DIO_WRITE)) |
Eryu Guan | 5e25c26 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 1515 | ret = dio->i_size - offset; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1516 | iocb->ki_pos += ret; |
| 1517 | } |
| 1518 | |
Eryu Guan | 5e25c26 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 1519 | /* |
| 1520 | * Try again to invalidate clean pages which might have been cached by |
| 1521 | * non-direct readahead, or faulted in by get_user_pages() if the source |
| 1522 | * of the write was an mmap'ed region of the file we're writing. Either |
| 1523 | * one is a pretty crazy thing to do, so we don't support it 100%. If |
| 1524 | * this invalidation fails, tough, the write still worked... |
| 1525 | * |
| 1526 | * And this page cache invalidation has to be after dio->end_io(), as |
| 1527 | * some filesystems convert unwritten extents to real allocations in |
| 1528 | * end_io() when necessary, otherwise a racing buffer read would cache |
| 1529 | * zeros from unwritten extents. |
| 1530 | */ |
| 1531 | if (!dio->error && |
| 1532 | (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) { |
| 1533 | int err; |
| 1534 | err = invalidate_inode_pages2_range(inode->i_mapping, |
| 1535 | offset >> PAGE_SHIFT, |
| 1536 | (offset + dio->size - 1) >> PAGE_SHIFT); |
Darrick J. Wong | 5a9d929 | 2018-01-08 10:41:39 -0800 | [diff] [blame] | 1537 | if (err) |
| 1538 | dio_warn_stale_pagecache(iocb->ki_filp); |
Eryu Guan | 5e25c26 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Dave Chinner | 4f8ff44 | 2018-05-02 12:54:52 -0700 | [diff] [blame] | 1541 | /* |
| 1542 | * If this is a DSYNC write, make sure we push it to stable storage now |
| 1543 | * that we've written data. |
| 1544 | */ |
| 1545 | if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC)) |
| 1546 | ret = generic_write_sync(iocb, ret); |
| 1547 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1548 | inode_dio_end(file_inode(iocb->ki_filp)); |
| 1549 | kfree(dio); |
| 1550 | |
| 1551 | return ret; |
| 1552 | } |
| 1553 | |
| 1554 | static void iomap_dio_complete_work(struct work_struct *work) |
| 1555 | { |
| 1556 | struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work); |
| 1557 | struct kiocb *iocb = dio->iocb; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1558 | |
Dave Chinner | 4f8ff44 | 2018-05-02 12:54:52 -0700 | [diff] [blame] | 1559 | iocb->ki_complete(iocb, iomap_dio_complete(dio), 0); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * Set an error in the dio if none is set yet. We have to use cmpxchg |
| 1564 | * as the submission context and the completion context(s) can race to |
| 1565 | * update the error. |
| 1566 | */ |
| 1567 | static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret) |
| 1568 | { |
| 1569 | cmpxchg(&dio->error, 0, ret); |
| 1570 | } |
| 1571 | |
| 1572 | static void iomap_dio_bio_end_io(struct bio *bio) |
| 1573 | { |
| 1574 | struct iomap_dio *dio = bio->bi_private; |
| 1575 | bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY); |
| 1576 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1577 | if (bio->bi_status) |
| 1578 | iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status)); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1579 | |
| 1580 | if (atomic_dec_and_test(&dio->ref)) { |
Andreas Gruenbacher | ebf00be | 2018-06-19 15:10:55 -0700 | [diff] [blame] | 1581 | if (dio->wait_for_completion) { |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1582 | struct task_struct *waiter = dio->submit.waiter; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1583 | WRITE_ONCE(dio->submit.waiter, NULL); |
Jens Axboe | 0619317 | 2018-11-13 21:16:54 -0700 | [diff] [blame] | 1584 | blk_wake_io_task(waiter); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1585 | } else if (dio->flags & IOMAP_DIO_WRITE) { |
| 1586 | struct inode *inode = file_inode(dio->iocb->ki_filp); |
| 1587 | |
| 1588 | INIT_WORK(&dio->aio.work, iomap_dio_complete_work); |
| 1589 | queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work); |
| 1590 | } else { |
| 1591 | iomap_dio_complete_work(&dio->aio.work); |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | if (should_dirty) { |
| 1596 | bio_check_pages_dirty(bio); |
| 1597 | } else { |
Jens Axboe | 399254a | 2019-02-27 13:13:23 -0700 | [diff] [blame] | 1598 | if (!bio_flagged(bio, BIO_NO_PAGE_REF)) { |
| 1599 | struct bvec_iter_all iter_all; |
| 1600 | struct bio_vec *bvec; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1601 | |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 1602 | bio_for_each_segment_all(bvec, bio, iter_all) |
Jens Axboe | 399254a | 2019-02-27 13:13:23 -0700 | [diff] [blame] | 1603 | put_page(bvec->bv_page); |
| 1604 | } |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1605 | bio_put(bio); |
| 1606 | } |
| 1607 | } |
| 1608 | |
Christoph Hellwig | 81214ba | 2018-12-04 11:12:08 -0700 | [diff] [blame] | 1609 | static void |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1610 | iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos, |
| 1611 | unsigned len) |
| 1612 | { |
| 1613 | struct page *page = ZERO_PAGE(0); |
Jens Axboe | d1e3628 | 2018-08-29 10:36:56 -0600 | [diff] [blame] | 1614 | int flags = REQ_SYNC | REQ_IDLE; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1615 | struct bio *bio; |
| 1616 | |
| 1617 | bio = bio_alloc(GFP_KERNEL, 1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1618 | bio_set_dev(bio, iomap->bdev); |
Christoph Hellwig | 57fc505 | 2018-06-01 09:03:08 -0700 | [diff] [blame] | 1619 | bio->bi_iter.bi_sector = iomap_sector(iomap, pos); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1620 | bio->bi_private = dio; |
| 1621 | bio->bi_end_io = iomap_dio_bio_end_io; |
| 1622 | |
| 1623 | get_page(page); |
Christoph Hellwig | 6533b4e | 2018-06-01 09:03:07 -0700 | [diff] [blame] | 1624 | __bio_add_page(bio, page, len, 0); |
Jens Axboe | d1e3628 | 2018-08-29 10:36:56 -0600 | [diff] [blame] | 1625 | bio_set_op_attrs(bio, REQ_OP_WRITE, flags); |
Christoph Hellwig | 81214ba | 2018-12-04 11:12:08 -0700 | [diff] [blame] | 1626 | iomap_dio_submit_bio(dio, iomap, bio); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | static loff_t |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1630 | iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length, |
| 1631 | struct iomap_dio *dio, struct iomap *iomap) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1632 | { |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 1633 | unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev)); |
| 1634 | unsigned int fs_block_size = i_blocksize(inode), pad; |
| 1635 | unsigned int align = iov_iter_alignment(dio->submit.iter); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1636 | struct iov_iter iter; |
| 1637 | struct bio *bio; |
| 1638 | bool need_zeroout = false; |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1639 | bool use_fua = false; |
Dave Chinner | 4721a60 | 2018-11-19 13:31:11 -0800 | [diff] [blame] | 1640 | int nr_pages, ret = 0; |
Al Viro | cfe057f | 2017-09-11 21:17:09 +0100 | [diff] [blame] | 1641 | size_t copied = 0; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1642 | |
| 1643 | if ((pos | length | align) & ((1 << blkbits) - 1)) |
| 1644 | return -EINVAL; |
| 1645 | |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1646 | if (iomap->type == IOMAP_UNWRITTEN) { |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1647 | dio->flags |= IOMAP_DIO_UNWRITTEN; |
| 1648 | need_zeroout = true; |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | if (iomap->flags & IOMAP_F_SHARED) |
| 1652 | dio->flags |= IOMAP_DIO_COW; |
| 1653 | |
| 1654 | if (iomap->flags & IOMAP_F_NEW) { |
| 1655 | need_zeroout = true; |
Dave Chinner | 0929d85 | 2018-11-19 13:31:10 -0800 | [diff] [blame] | 1656 | } else if (iomap->type == IOMAP_MAPPED) { |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1657 | /* |
Dave Chinner | 0929d85 | 2018-11-19 13:31:10 -0800 | [diff] [blame] | 1658 | * Use a FUA write if we need datasync semantics, this is a pure |
| 1659 | * data IO that doesn't require any metadata updates (including |
| 1660 | * after IO completion such as unwritten extent conversion) and |
| 1661 | * the underlying device supports FUA. This allows us to avoid |
| 1662 | * cache flushes on IO completion. |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1663 | */ |
| 1664 | if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) && |
| 1665 | (dio->flags & IOMAP_DIO_WRITE_FUA) && |
| 1666 | blk_queue_fua(bdev_get_queue(iomap->bdev))) |
| 1667 | use_fua = true; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | * Operate on a partial iter trimmed to the extent we were called for. |
| 1672 | * We'll update the iter in the dio once we're done with this extent. |
| 1673 | */ |
| 1674 | iter = *dio->submit.iter; |
| 1675 | iov_iter_truncate(&iter, length); |
| 1676 | |
| 1677 | nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES); |
| 1678 | if (nr_pages <= 0) |
| 1679 | return nr_pages; |
| 1680 | |
| 1681 | if (need_zeroout) { |
| 1682 | /* zero out from the start of the block to the write offset */ |
| 1683 | pad = pos & (fs_block_size - 1); |
| 1684 | if (pad) |
| 1685 | iomap_dio_zero(dio, iomap, pos - pad, pad); |
| 1686 | } |
| 1687 | |
| 1688 | do { |
Al Viro | cfe057f | 2017-09-11 21:17:09 +0100 | [diff] [blame] | 1689 | size_t n; |
| 1690 | if (dio->error) { |
| 1691 | iov_iter_revert(dio->submit.iter, copied); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1692 | return 0; |
Al Viro | cfe057f | 2017-09-11 21:17:09 +0100 | [diff] [blame] | 1693 | } |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1694 | |
| 1695 | bio = bio_alloc(GFP_KERNEL, nr_pages); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1696 | bio_set_dev(bio, iomap->bdev); |
Christoph Hellwig | 57fc505 | 2018-06-01 09:03:08 -0700 | [diff] [blame] | 1697 | bio->bi_iter.bi_sector = iomap_sector(iomap, pos); |
Jens Axboe | 45d06cf | 2017-06-27 11:01:22 -0600 | [diff] [blame] | 1698 | bio->bi_write_hint = dio->iocb->ki_hint; |
Adam Manzanares | 087e566 | 2018-05-22 10:52:21 -0700 | [diff] [blame] | 1699 | bio->bi_ioprio = dio->iocb->ki_ioprio; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1700 | bio->bi_private = dio; |
| 1701 | bio->bi_end_io = iomap_dio_bio_end_io; |
| 1702 | |
| 1703 | ret = bio_iov_iter_get_pages(bio, &iter); |
| 1704 | if (unlikely(ret)) { |
Dave Chinner | 4721a60 | 2018-11-19 13:31:11 -0800 | [diff] [blame] | 1705 | /* |
| 1706 | * We have to stop part way through an IO. We must fall |
| 1707 | * through to the sub-block tail zeroing here, otherwise |
| 1708 | * this short IO may expose stale data in the tail of |
| 1709 | * the block we haven't written data to. |
| 1710 | */ |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1711 | bio_put(bio); |
Dave Chinner | 4721a60 | 2018-11-19 13:31:11 -0800 | [diff] [blame] | 1712 | goto zero_tail; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1713 | } |
| 1714 | |
Al Viro | cfe057f | 2017-09-11 21:17:09 +0100 | [diff] [blame] | 1715 | n = bio->bi_iter.bi_size; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1716 | if (dio->flags & IOMAP_DIO_WRITE) { |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1717 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE; |
| 1718 | if (use_fua) |
| 1719 | bio->bi_opf |= REQ_FUA; |
| 1720 | else |
| 1721 | dio->flags &= ~IOMAP_DIO_WRITE_FUA; |
Al Viro | cfe057f | 2017-09-11 21:17:09 +0100 | [diff] [blame] | 1722 | task_io_account_write(n); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1723 | } else { |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1724 | bio->bi_opf = REQ_OP_READ; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1725 | if (dio->flags & IOMAP_DIO_DIRTY) |
| 1726 | bio_set_pages_dirty(bio); |
| 1727 | } |
| 1728 | |
Al Viro | cfe057f | 2017-09-11 21:17:09 +0100 | [diff] [blame] | 1729 | iov_iter_advance(dio->submit.iter, n); |
| 1730 | |
| 1731 | dio->size += n; |
| 1732 | pos += n; |
| 1733 | copied += n; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1734 | |
| 1735 | nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES); |
Christoph Hellwig | 81214ba | 2018-12-04 11:12:08 -0700 | [diff] [blame] | 1736 | iomap_dio_submit_bio(dio, iomap, bio); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1737 | } while (nr_pages); |
| 1738 | |
Dave Chinner | b450672 | 2018-11-19 13:31:10 -0800 | [diff] [blame] | 1739 | /* |
| 1740 | * We need to zeroout the tail of a sub-block write if the extent type |
| 1741 | * requires zeroing or the write extends beyond EOF. If we don't zero |
| 1742 | * the block tail in the latter case, we can expose stale data via mmap |
| 1743 | * reads of the EOF block. |
| 1744 | */ |
Dave Chinner | 4721a60 | 2018-11-19 13:31:11 -0800 | [diff] [blame] | 1745 | zero_tail: |
Dave Chinner | b450672 | 2018-11-19 13:31:10 -0800 | [diff] [blame] | 1746 | if (need_zeroout || |
| 1747 | ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) { |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1748 | /* zero out from the end of the write to the end of the block */ |
| 1749 | pad = pos & (fs_block_size - 1); |
| 1750 | if (pad) |
| 1751 | iomap_dio_zero(dio, iomap, pos, fs_block_size - pad); |
| 1752 | } |
Dave Chinner | 4721a60 | 2018-11-19 13:31:11 -0800 | [diff] [blame] | 1753 | return copied ? copied : ret; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1754 | } |
| 1755 | |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1756 | static loff_t |
| 1757 | iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio) |
| 1758 | { |
| 1759 | length = iov_iter_zero(length, dio->submit.iter); |
| 1760 | dio->size += length; |
| 1761 | return length; |
| 1762 | } |
| 1763 | |
| 1764 | static loff_t |
Andreas Gruenbacher | ec181f6 | 2018-07-03 09:07:47 -0700 | [diff] [blame] | 1765 | iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length, |
| 1766 | struct iomap_dio *dio, struct iomap *iomap) |
| 1767 | { |
| 1768 | struct iov_iter *iter = dio->submit.iter; |
| 1769 | size_t copied; |
| 1770 | |
| 1771 | BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data)); |
| 1772 | |
| 1773 | if (dio->flags & IOMAP_DIO_WRITE) { |
| 1774 | loff_t size = inode->i_size; |
| 1775 | |
| 1776 | if (pos > size) |
| 1777 | memset(iomap->inline_data + size, 0, pos - size); |
| 1778 | copied = copy_from_iter(iomap->inline_data + pos, length, iter); |
| 1779 | if (copied) { |
| 1780 | if (pos + copied > size) |
| 1781 | i_size_write(inode, pos + copied); |
| 1782 | mark_inode_dirty(inode); |
| 1783 | } |
| 1784 | } else { |
| 1785 | copied = copy_to_iter(iomap->inline_data + pos, length, iter); |
| 1786 | } |
| 1787 | dio->size += copied; |
| 1788 | return copied; |
| 1789 | } |
| 1790 | |
| 1791 | static loff_t |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1792 | iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length, |
| 1793 | void *data, struct iomap *iomap) |
| 1794 | { |
| 1795 | struct iomap_dio *dio = data; |
| 1796 | |
| 1797 | switch (iomap->type) { |
| 1798 | case IOMAP_HOLE: |
| 1799 | if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE)) |
| 1800 | return -EIO; |
| 1801 | return iomap_dio_hole_actor(length, dio); |
| 1802 | case IOMAP_UNWRITTEN: |
| 1803 | if (!(dio->flags & IOMAP_DIO_WRITE)) |
| 1804 | return iomap_dio_hole_actor(length, dio); |
| 1805 | return iomap_dio_bio_actor(inode, pos, length, dio, iomap); |
| 1806 | case IOMAP_MAPPED: |
| 1807 | return iomap_dio_bio_actor(inode, pos, length, dio, iomap); |
Andreas Gruenbacher | ec181f6 | 2018-07-03 09:07:47 -0700 | [diff] [blame] | 1808 | case IOMAP_INLINE: |
| 1809 | return iomap_dio_inline_actor(inode, pos, length, dio, iomap); |
Christoph Hellwig | 0923043 | 2018-07-03 09:07:46 -0700 | [diff] [blame] | 1810 | default: |
| 1811 | WARN_ON_ONCE(1); |
| 1812 | return -EIO; |
| 1813 | } |
| 1814 | } |
| 1815 | |
Dave Chinner | 4f8ff44 | 2018-05-02 12:54:52 -0700 | [diff] [blame] | 1816 | /* |
| 1817 | * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1818 | * is being issued as AIO or not. This allows us to optimise pure data writes |
| 1819 | * to use REQ_FUA rather than requiring generic_write_sync() to issue a |
| 1820 | * REQ_FLUSH post write. This is slightly tricky because a single request here |
| 1821 | * can be mapped into multiple disjoint IOs and only a subset of the IOs issued |
| 1822 | * may be pure data writes. In that case, we still need to do a full data sync |
| 1823 | * completion. |
Dave Chinner | 4f8ff44 | 2018-05-02 12:54:52 -0700 | [diff] [blame] | 1824 | */ |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1825 | ssize_t |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1826 | iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, |
| 1827 | const struct iomap_ops *ops, iomap_dio_end_io_t end_io) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1828 | { |
| 1829 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
| 1830 | struct inode *inode = file_inode(iocb->ki_filp); |
| 1831 | size_t count = iov_iter_count(iter); |
Eryu Guan | c771c14 | 2017-03-02 15:02:06 -0800 | [diff] [blame] | 1832 | loff_t pos = iocb->ki_pos, start = pos; |
| 1833 | loff_t end = iocb->ki_pos + count - 1, ret = 0; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1834 | unsigned int flags = IOMAP_DIRECT; |
Christoph Hellwig | 4ea899e | 2019-01-17 08:58:58 -0800 | [diff] [blame] | 1835 | bool wait_for_completion = is_sync_kiocb(iocb); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1836 | struct blk_plug plug; |
| 1837 | struct iomap_dio *dio; |
| 1838 | |
| 1839 | lockdep_assert_held(&inode->i_rwsem); |
| 1840 | |
| 1841 | if (!count) |
| 1842 | return 0; |
| 1843 | |
| 1844 | dio = kmalloc(sizeof(*dio), GFP_KERNEL); |
| 1845 | if (!dio) |
| 1846 | return -ENOMEM; |
| 1847 | |
| 1848 | dio->iocb = iocb; |
| 1849 | atomic_set(&dio->ref, 1); |
| 1850 | dio->size = 0; |
| 1851 | dio->i_size = i_size_read(inode); |
| 1852 | dio->end_io = end_io; |
| 1853 | dio->error = 0; |
| 1854 | dio->flags = 0; |
| 1855 | |
| 1856 | dio->submit.iter = iter; |
Andreas Gruenbacher | ebf00be | 2018-06-19 15:10:55 -0700 | [diff] [blame] | 1857 | dio->submit.waiter = current; |
| 1858 | dio->submit.cookie = BLK_QC_T_NONE; |
| 1859 | dio->submit.last_queue = NULL; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1860 | |
| 1861 | if (iov_iter_rw(iter) == READ) { |
| 1862 | if (pos >= dio->i_size) |
| 1863 | goto out_free_dio; |
| 1864 | |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 1865 | if (iter_is_iovec(iter) && iov_iter_rw(iter) == READ) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1866 | dio->flags |= IOMAP_DIO_DIRTY; |
| 1867 | } else { |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1868 | flags |= IOMAP_WRITE; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1869 | dio->flags |= IOMAP_DIO_WRITE; |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1870 | |
| 1871 | /* for data sync or sync, we need sync completion processing */ |
Dave Chinner | 4f8ff44 | 2018-05-02 12:54:52 -0700 | [diff] [blame] | 1872 | if (iocb->ki_flags & IOCB_DSYNC) |
| 1873 | dio->flags |= IOMAP_DIO_NEED_SYNC; |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1874 | |
| 1875 | /* |
| 1876 | * For datasync only writes, we optimistically try using FUA for |
| 1877 | * this IO. Any non-FUA write that occurs will clear this flag, |
| 1878 | * hence we know before completion whether a cache flush is |
| 1879 | * necessary. |
| 1880 | */ |
| 1881 | if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC) |
| 1882 | dio->flags |= IOMAP_DIO_WRITE_FUA; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1883 | } |
| 1884 | |
Goldwyn Rodrigues | a38d124 | 2017-06-20 07:05:45 -0500 | [diff] [blame] | 1885 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 1886 | if (filemap_range_has_page(mapping, start, end)) { |
| 1887 | ret = -EAGAIN; |
| 1888 | goto out_free_dio; |
| 1889 | } |
| 1890 | flags |= IOMAP_NOWAIT; |
| 1891 | } |
| 1892 | |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 1893 | ret = filemap_write_and_wait_range(mapping, start, end); |
| 1894 | if (ret) |
| 1895 | goto out_free_dio; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1896 | |
Darrick J. Wong | 5a9d929 | 2018-01-08 10:41:39 -0800 | [diff] [blame] | 1897 | /* |
| 1898 | * Try to invalidate cache pages for the range we're direct |
| 1899 | * writing. If this invalidation fails, tough, the write will |
| 1900 | * still work, but racing two incompatible write paths is a |
| 1901 | * pretty crazy thing to do, so we don't support it 100%. |
| 1902 | */ |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 1903 | ret = invalidate_inode_pages2_range(mapping, |
| 1904 | start >> PAGE_SHIFT, end >> PAGE_SHIFT); |
Darrick J. Wong | 5a9d929 | 2018-01-08 10:41:39 -0800 | [diff] [blame] | 1905 | if (ret) |
| 1906 | dio_warn_stale_pagecache(iocb->ki_filp); |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 1907 | ret = 0; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1908 | |
Christoph Hellwig | 4ea899e | 2019-01-17 08:58:58 -0800 | [diff] [blame] | 1909 | if (iov_iter_rw(iter) == WRITE && !wait_for_completion && |
Chandan Rajendra | 546e7be | 2017-09-22 11:47:33 -0700 | [diff] [blame] | 1910 | !inode->i_sb->s_dio_done_wq) { |
| 1911 | ret = sb_init_dio_done_wq(inode->i_sb); |
| 1912 | if (ret < 0) |
| 1913 | goto out_free_dio; |
| 1914 | } |
| 1915 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1916 | inode_dio_begin(inode); |
| 1917 | |
| 1918 | blk_start_plug(&plug); |
| 1919 | do { |
| 1920 | ret = iomap_apply(inode, pos, count, flags, ops, dio, |
| 1921 | iomap_dio_actor); |
| 1922 | if (ret <= 0) { |
| 1923 | /* magic error code to fall back to buffered I/O */ |
Andreas Gruenbacher | ebf00be | 2018-06-19 15:10:55 -0700 | [diff] [blame] | 1924 | if (ret == -ENOTBLK) { |
Christoph Hellwig | 4ea899e | 2019-01-17 08:58:58 -0800 | [diff] [blame] | 1925 | wait_for_completion = true; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1926 | ret = 0; |
Andreas Gruenbacher | ebf00be | 2018-06-19 15:10:55 -0700 | [diff] [blame] | 1927 | } |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1928 | break; |
| 1929 | } |
| 1930 | pos += ret; |
Chandan Rajendra | a008c31 | 2017-04-12 11:03:20 -0700 | [diff] [blame] | 1931 | |
| 1932 | if (iov_iter_rw(iter) == READ && pos >= dio->i_size) |
| 1933 | break; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1934 | } while ((count = iov_iter_count(iter)) > 0); |
| 1935 | blk_finish_plug(&plug); |
| 1936 | |
| 1937 | if (ret < 0) |
| 1938 | iomap_dio_set_error(dio, ret); |
| 1939 | |
Dave Chinner | 3460cac | 2018-05-02 12:54:53 -0700 | [diff] [blame] | 1940 | /* |
| 1941 | * If all the writes we issued were FUA, we don't need to flush the |
| 1942 | * cache on IO completion. Clear the sync flag for this case. |
| 1943 | */ |
| 1944 | if (dio->flags & IOMAP_DIO_WRITE_FUA) |
| 1945 | dio->flags &= ~IOMAP_DIO_NEED_SYNC; |
| 1946 | |
Christoph Hellwig | 81214ba | 2018-12-04 11:12:08 -0700 | [diff] [blame] | 1947 | WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie); |
| 1948 | WRITE_ONCE(iocb->private, dio->submit.last_queue); |
| 1949 | |
Christoph Hellwig | 4ea899e | 2019-01-17 08:58:58 -0800 | [diff] [blame] | 1950 | /* |
| 1951 | * We are about to drop our additional submission reference, which |
| 1952 | * might be the last reference to the dio. There are three three |
| 1953 | * different ways we can progress here: |
| 1954 | * |
| 1955 | * (a) If this is the last reference we will always complete and free |
| 1956 | * the dio ourselves. |
| 1957 | * (b) If this is not the last reference, and we serve an asynchronous |
| 1958 | * iocb, we must never touch the dio after the decrement, the |
| 1959 | * I/O completion handler will complete and free it. |
| 1960 | * (c) If this is not the last reference, but we serve a synchronous |
| 1961 | * iocb, the I/O completion handler will wake us up on the drop |
| 1962 | * of the final reference, and we will complete and free it here |
| 1963 | * after we got woken by the I/O completion handler. |
| 1964 | */ |
| 1965 | dio->wait_for_completion = wait_for_completion; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1966 | if (!atomic_dec_and_test(&dio->ref)) { |
Christoph Hellwig | 4ea899e | 2019-01-17 08:58:58 -0800 | [diff] [blame] | 1967 | if (!wait_for_completion) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1968 | return -EIOCBQUEUED; |
| 1969 | |
| 1970 | for (;;) { |
Linus Torvalds | 1ac5cd4 | 2019-01-02 10:46:03 -0800 | [diff] [blame] | 1971 | set_current_state(TASK_UNINTERRUPTIBLE); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1972 | if (!READ_ONCE(dio->submit.waiter)) |
| 1973 | break; |
| 1974 | |
| 1975 | if (!(iocb->ki_flags & IOCB_HIPRI) || |
| 1976 | !dio->submit.last_queue || |
Christoph Hellwig | ea435e1 | 2017-11-02 21:29:54 +0300 | [diff] [blame] | 1977 | !blk_poll(dio->submit.last_queue, |
Jens Axboe | 0a1b8b8 | 2018-11-26 08:24:43 -0700 | [diff] [blame] | 1978 | dio->submit.cookie, true)) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1979 | io_schedule(); |
| 1980 | } |
| 1981 | __set_current_state(TASK_RUNNING); |
| 1982 | } |
| 1983 | |
Christoph Hellwig | 4ea899e | 2019-01-17 08:58:58 -0800 | [diff] [blame] | 1984 | return iomap_dio_complete(dio); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1985 | |
| 1986 | out_free_dio: |
| 1987 | kfree(dio); |
| 1988 | return ret; |
| 1989 | } |
| 1990 | EXPORT_SYMBOL_GPL(iomap_dio_rw); |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 1991 | |
| 1992 | /* Swapfile activation */ |
| 1993 | |
| 1994 | #ifdef CONFIG_SWAP |
| 1995 | struct iomap_swapfile_info { |
| 1996 | struct iomap iomap; /* accumulated iomap */ |
| 1997 | struct swap_info_struct *sis; |
| 1998 | uint64_t lowest_ppage; /* lowest physical addr seen (pages) */ |
| 1999 | uint64_t highest_ppage; /* highest physical addr seen (pages) */ |
| 2000 | unsigned long nr_pages; /* number of pages collected */ |
| 2001 | int nr_extents; /* extent count */ |
| 2002 | }; |
| 2003 | |
| 2004 | /* |
| 2005 | * Collect physical extents for this swap file. Physical extents reported to |
| 2006 | * the swap code must be trimmed to align to a page boundary. The logical |
| 2007 | * offset within the file is irrelevant since the swapfile code maps logical |
| 2008 | * page numbers of the swap device to the physical page-aligned extents. |
| 2009 | */ |
| 2010 | static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi) |
| 2011 | { |
| 2012 | struct iomap *iomap = &isi->iomap; |
| 2013 | unsigned long nr_pages; |
| 2014 | uint64_t first_ppage; |
| 2015 | uint64_t first_ppage_reported; |
| 2016 | uint64_t next_ppage; |
| 2017 | int error; |
| 2018 | |
| 2019 | /* |
| 2020 | * Round the start up and the end down so that the physical |
| 2021 | * extent aligns to a page boundary. |
| 2022 | */ |
| 2023 | first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT; |
| 2024 | next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >> |
| 2025 | PAGE_SHIFT; |
| 2026 | |
| 2027 | /* Skip too-short physical extents. */ |
| 2028 | if (first_ppage >= next_ppage) |
| 2029 | return 0; |
| 2030 | nr_pages = next_ppage - first_ppage; |
| 2031 | |
| 2032 | /* |
| 2033 | * Calculate how much swap space we're adding; the first page contains |
| 2034 | * the swap header and doesn't count. The mm still wants that first |
| 2035 | * page fed to add_swap_extent, however. |
| 2036 | */ |
| 2037 | first_ppage_reported = first_ppage; |
| 2038 | if (iomap->offset == 0) |
| 2039 | first_ppage_reported++; |
| 2040 | if (isi->lowest_ppage > first_ppage_reported) |
| 2041 | isi->lowest_ppage = first_ppage_reported; |
| 2042 | if (isi->highest_ppage < (next_ppage - 1)) |
| 2043 | isi->highest_ppage = next_ppage - 1; |
| 2044 | |
| 2045 | /* Add extent, set up for the next call. */ |
| 2046 | error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage); |
| 2047 | if (error < 0) |
| 2048 | return error; |
| 2049 | isi->nr_extents += error; |
| 2050 | isi->nr_pages += nr_pages; |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
| 2054 | /* |
| 2055 | * Accumulate iomaps for this swap file. We have to accumulate iomaps because |
| 2056 | * swap only cares about contiguous page-aligned physical extents and makes no |
| 2057 | * distinction between written and unwritten extents. |
| 2058 | */ |
| 2059 | static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos, |
| 2060 | loff_t count, void *data, struct iomap *iomap) |
| 2061 | { |
| 2062 | struct iomap_swapfile_info *isi = data; |
| 2063 | int error; |
| 2064 | |
Christoph Hellwig | 19319b5 | 2018-06-01 09:03:06 -0700 | [diff] [blame] | 2065 | switch (iomap->type) { |
| 2066 | case IOMAP_MAPPED: |
| 2067 | case IOMAP_UNWRITTEN: |
| 2068 | /* Only real or unwritten extents. */ |
| 2069 | break; |
| 2070 | case IOMAP_INLINE: |
| 2071 | /* No inline data. */ |
Omar Sandoval | ec60192 | 2018-05-16 11:13:34 -0700 | [diff] [blame] | 2072 | pr_err("swapon: file is inline\n"); |
| 2073 | return -EINVAL; |
Christoph Hellwig | 19319b5 | 2018-06-01 09:03:06 -0700 | [diff] [blame] | 2074 | default: |
Omar Sandoval | ec60192 | 2018-05-16 11:13:34 -0700 | [diff] [blame] | 2075 | pr_err("swapon: file has unallocated extents\n"); |
| 2076 | return -EINVAL; |
| 2077 | } |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 2078 | |
Omar Sandoval | ec60192 | 2018-05-16 11:13:34 -0700 | [diff] [blame] | 2079 | /* No uncommitted metadata or shared blocks. */ |
| 2080 | if (iomap->flags & IOMAP_F_DIRTY) { |
| 2081 | pr_err("swapon: file is not committed\n"); |
| 2082 | return -EINVAL; |
| 2083 | } |
| 2084 | if (iomap->flags & IOMAP_F_SHARED) { |
| 2085 | pr_err("swapon: file has shared extents\n"); |
| 2086 | return -EINVAL; |
| 2087 | } |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 2088 | |
Omar Sandoval | ec60192 | 2018-05-16 11:13:34 -0700 | [diff] [blame] | 2089 | /* Only one bdev per swap file. */ |
| 2090 | if (iomap->bdev != isi->sis->bdev) { |
| 2091 | pr_err("swapon: file is on multiple devices\n"); |
| 2092 | return -EINVAL; |
| 2093 | } |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 2094 | |
| 2095 | if (isi->iomap.length == 0) { |
| 2096 | /* No accumulated extent, so just store it. */ |
| 2097 | memcpy(&isi->iomap, iomap, sizeof(isi->iomap)); |
| 2098 | } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) { |
| 2099 | /* Append this to the accumulated extent. */ |
| 2100 | isi->iomap.length += iomap->length; |
| 2101 | } else { |
| 2102 | /* Otherwise, add the retained iomap and store this one. */ |
| 2103 | error = iomap_swapfile_add_extent(isi); |
| 2104 | if (error) |
| 2105 | return error; |
| 2106 | memcpy(&isi->iomap, iomap, sizeof(isi->iomap)); |
| 2107 | } |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 2108 | return count; |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * Iterate a swap file's iomaps to construct physical extents that can be |
| 2113 | * passed to the swapfile subsystem. |
| 2114 | */ |
| 2115 | int iomap_swapfile_activate(struct swap_info_struct *sis, |
| 2116 | struct file *swap_file, sector_t *pagespan, |
| 2117 | const struct iomap_ops *ops) |
| 2118 | { |
| 2119 | struct iomap_swapfile_info isi = { |
| 2120 | .sis = sis, |
| 2121 | .lowest_ppage = (sector_t)-1ULL, |
| 2122 | }; |
| 2123 | struct address_space *mapping = swap_file->f_mapping; |
| 2124 | struct inode *inode = mapping->host; |
| 2125 | loff_t pos = 0; |
| 2126 | loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE); |
| 2127 | loff_t ret; |
| 2128 | |
Darrick J. Wong | 117a148 | 2018-06-05 09:53:05 -0700 | [diff] [blame] | 2129 | /* |
| 2130 | * Persist all file mapping metadata so that we won't have any |
| 2131 | * IOMAP_F_DIRTY iomaps. |
| 2132 | */ |
| 2133 | ret = vfs_fsync(swap_file, 1); |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 2134 | if (ret) |
| 2135 | return ret; |
| 2136 | |
| 2137 | while (len > 0) { |
| 2138 | ret = iomap_apply(inode, pos, len, IOMAP_REPORT, |
| 2139 | ops, &isi, iomap_swapfile_activate_actor); |
| 2140 | if (ret <= 0) |
| 2141 | return ret; |
| 2142 | |
| 2143 | pos += ret; |
| 2144 | len -= ret; |
| 2145 | } |
| 2146 | |
| 2147 | if (isi.iomap.length) { |
| 2148 | ret = iomap_swapfile_add_extent(&isi); |
| 2149 | if (ret) |
| 2150 | return ret; |
| 2151 | } |
| 2152 | |
| 2153 | *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage; |
| 2154 | sis->max = isi.nr_pages; |
| 2155 | sis->pages = isi.nr_pages - 1; |
| 2156 | sis->highest_bit = isi.nr_pages - 1; |
| 2157 | return isi.nr_extents; |
| 2158 | } |
| 2159 | EXPORT_SYMBOL_GPL(iomap_swapfile_activate); |
| 2160 | #endif /* CONFIG_SWAP */ |
Christoph Hellwig | 89eb190 | 2018-06-01 09:03:08 -0700 | [diff] [blame] | 2161 | |
| 2162 | static loff_t |
| 2163 | iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length, |
| 2164 | void *data, struct iomap *iomap) |
| 2165 | { |
| 2166 | sector_t *bno = data, addr; |
| 2167 | |
| 2168 | if (iomap->type == IOMAP_MAPPED) { |
| 2169 | addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits; |
| 2170 | if (addr > INT_MAX) |
| 2171 | WARN(1, "would truncate bmap result\n"); |
| 2172 | else |
| 2173 | *bno = addr; |
| 2174 | } |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
| 2178 | /* legacy ->bmap interface. 0 is the error return (!) */ |
| 2179 | sector_t |
| 2180 | iomap_bmap(struct address_space *mapping, sector_t bno, |
| 2181 | const struct iomap_ops *ops) |
| 2182 | { |
| 2183 | struct inode *inode = mapping->host; |
Eric Sandeen | 79b3dbe | 2018-08-02 13:09:27 -0700 | [diff] [blame] | 2184 | loff_t pos = bno << inode->i_blkbits; |
Christoph Hellwig | 89eb190 | 2018-06-01 09:03:08 -0700 | [diff] [blame] | 2185 | unsigned blocksize = i_blocksize(inode); |
| 2186 | |
| 2187 | if (filemap_write_and_wait(mapping)) |
| 2188 | return 0; |
| 2189 | |
| 2190 | bno = 0; |
| 2191 | iomap_apply(inode, pos, blocksize, 0, ops, &bno, iomap_bmap_actor); |
| 2192 | return bno; |
| 2193 | } |
| 2194 | EXPORT_SYMBOL_GPL(iomap_bmap); |