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