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