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