blob: 55e514899b8caca4b13e2938db126aed28e9f34d [file] [log] [blame]
Darrick J. Wongafc51aa2019-07-15 08:50:59 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (c) 2016-2018 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/swap.h>
16#include <linux/bio.h>
17#include <linux/sched/signal.h>
18#include <linux/migrate.h>
Christoph Hellwig9e91c572019-10-17 13:12:13 -070019#include "trace.h"
Darrick J. Wongafc51aa2019-07-15 08:50:59 -070020
21#include "../internal.h"
22
23static struct iomap_page *
24iomap_page_create(struct inode *inode, struct page *page)
25{
26 struct iomap_page *iop = to_iomap_page(page);
27
28 if (iop || i_blocksize(inode) == PAGE_SIZE)
29 return iop;
30
31 iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL);
32 atomic_set(&iop->read_count, 0);
33 atomic_set(&iop->write_count, 0);
34 bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE);
35
36 /*
37 * migrate_page_move_mapping() assumes that pages with private data have
38 * their count elevated by 1.
39 */
40 get_page(page);
41 set_page_private(page, (unsigned long)iop);
42 SetPagePrivate(page);
43 return iop;
44}
45
46static void
47iomap_page_release(struct page *page)
48{
49 struct iomap_page *iop = to_iomap_page(page);
50
51 if (!iop)
52 return;
53 WARN_ON_ONCE(atomic_read(&iop->read_count));
54 WARN_ON_ONCE(atomic_read(&iop->write_count));
55 ClearPagePrivate(page);
56 set_page_private(page, 0);
57 put_page(page);
58 kfree(iop);
59}
60
61/*
62 * Calculate the range inside the page that we actually need to read.
63 */
64static void
65iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
66 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
67{
68 loff_t orig_pos = *pos;
69 loff_t isize = i_size_read(inode);
70 unsigned block_bits = inode->i_blkbits;
71 unsigned block_size = (1 << block_bits);
72 unsigned poff = offset_in_page(*pos);
73 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
74 unsigned first = poff >> block_bits;
75 unsigned last = (poff + plen - 1) >> block_bits;
76
77 /*
78 * If the block size is smaller than the page size we need to check the
79 * per-block uptodate status and adjust the offset and length if needed
80 * to avoid reading in already uptodate ranges.
81 */
82 if (iop) {
83 unsigned int i;
84
85 /* move forward for each leading block marked uptodate */
86 for (i = first; i <= last; i++) {
87 if (!test_bit(i, iop->uptodate))
88 break;
89 *pos += block_size;
90 poff += block_size;
91 plen -= block_size;
92 first++;
93 }
94
95 /* truncate len if we find any trailing uptodate block(s) */
96 for ( ; i <= last; i++) {
97 if (test_bit(i, iop->uptodate)) {
98 plen -= (last - i + 1) * block_size;
99 last = i - 1;
100 break;
101 }
102 }
103 }
104
105 /*
106 * If the extent spans the block that contains the i_size we need to
107 * handle both halves separately so that we properly zero data in the
108 * page cache for blocks that are entirely outside of i_size.
109 */
110 if (orig_pos <= isize && orig_pos + length > isize) {
111 unsigned end = offset_in_page(isize - 1) >> block_bits;
112
113 if (first <= end && last > end)
114 plen -= (last - end) * block_size;
115 }
116
117 *offp = poff;
118 *lenp = plen;
119}
120
121static void
122iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
123{
124 struct iomap_page *iop = to_iomap_page(page);
125 struct inode *inode = page->mapping->host;
126 unsigned first = off >> inode->i_blkbits;
127 unsigned last = (off + len - 1) >> inode->i_blkbits;
128 unsigned int i;
129 bool uptodate = true;
130
131 if (iop) {
132 for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
133 if (i >= first && i <= last)
134 set_bit(i, iop->uptodate);
135 else if (!test_bit(i, iop->uptodate))
136 uptodate = false;
137 }
138 }
139
140 if (uptodate && !PageError(page))
141 SetPageUptodate(page);
142}
143
144static void
145iomap_read_finish(struct iomap_page *iop, struct page *page)
146{
147 if (!iop || atomic_dec_and_test(&iop->read_count))
148 unlock_page(page);
149}
150
151static void
152iomap_read_page_end_io(struct bio_vec *bvec, int error)
153{
154 struct page *page = bvec->bv_page;
155 struct iomap_page *iop = to_iomap_page(page);
156
157 if (unlikely(error)) {
158 ClearPageUptodate(page);
159 SetPageError(page);
160 } else {
161 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
162 }
163
164 iomap_read_finish(iop, page);
165}
166
167static void
168iomap_read_end_io(struct bio *bio)
169{
170 int error = blk_status_to_errno(bio->bi_status);
171 struct bio_vec *bvec;
172 struct bvec_iter_all iter_all;
173
174 bio_for_each_segment_all(bvec, bio, iter_all)
175 iomap_read_page_end_io(bvec, error);
176 bio_put(bio);
177}
178
179struct iomap_readpage_ctx {
180 struct page *cur_page;
181 bool cur_page_in_bio;
182 bool is_readahead;
183 struct bio *bio;
184 struct list_head *pages;
185};
186
187static void
188iomap_read_inline_data(struct inode *inode, struct page *page,
189 struct iomap *iomap)
190{
191 size_t size = i_size_read(inode);
192 void *addr;
193
194 if (PageUptodate(page))
195 return;
196
197 BUG_ON(page->index);
198 BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
199
200 addr = kmap_atomic(page);
201 memcpy(addr, iomap->inline_data, size);
202 memset(addr + size, 0, PAGE_SIZE - size);
203 kunmap_atomic(addr);
204 SetPageUptodate(page);
205}
206
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700207static inline bool iomap_block_needs_zeroing(struct inode *inode,
208 struct iomap *iomap, loff_t pos)
209{
210 return iomap->type != IOMAP_MAPPED ||
211 (iomap->flags & IOMAP_F_NEW) ||
212 pos >= i_size_read(inode);
213}
214
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700215static loff_t
216iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
217 struct iomap *iomap)
218{
219 struct iomap_readpage_ctx *ctx = data;
220 struct page *page = ctx->cur_page;
221 struct iomap_page *iop = iomap_page_create(inode, page);
222 bool same_page = false, is_contig = false;
223 loff_t orig_pos = pos;
224 unsigned poff, plen;
225 sector_t sector;
226
227 if (iomap->type == IOMAP_INLINE) {
228 WARN_ON_ONCE(pos);
229 iomap_read_inline_data(inode, page, iomap);
230 return PAGE_SIZE;
231 }
232
233 /* zero post-eof blocks as the page may be mapped */
234 iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
235 if (plen == 0)
236 goto done;
237
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700238 if (iomap_block_needs_zeroing(inode, iomap, pos)) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700239 zero_user(page, poff, plen);
240 iomap_set_range_uptodate(page, poff, plen);
241 goto done;
242 }
243
244 ctx->cur_page_in_bio = true;
245
246 /*
247 * Try to merge into a previous segment if we can.
248 */
249 sector = iomap_sector(iomap, pos);
250 if (ctx->bio && bio_end_sector(ctx->bio) == sector)
251 is_contig = true;
252
253 if (is_contig &&
254 __bio_try_merge_page(ctx->bio, page, plen, poff, &same_page)) {
255 if (!same_page && iop)
256 atomic_inc(&iop->read_count);
257 goto done;
258 }
259
260 /*
261 * If we start a new segment we need to increase the read count, and we
262 * need to do so before submitting any previous full bio to make sure
263 * that we don't prematurely unlock the page.
264 */
265 if (iop)
266 atomic_inc(&iop->read_count);
267
268 if (!ctx->bio || !is_contig || bio_full(ctx->bio, plen)) {
269 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
270 int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
271
272 if (ctx->bio)
273 submit_bio(ctx->bio);
274
275 if (ctx->is_readahead) /* same as readahead_gfp_mask */
276 gfp |= __GFP_NORETRY | __GFP_NOWARN;
277 ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
278 ctx->bio->bi_opf = REQ_OP_READ;
279 if (ctx->is_readahead)
280 ctx->bio->bi_opf |= REQ_RAHEAD;
281 ctx->bio->bi_iter.bi_sector = sector;
282 bio_set_dev(ctx->bio, iomap->bdev);
283 ctx->bio->bi_end_io = iomap_read_end_io;
284 }
285
286 bio_add_page(ctx->bio, page, plen, poff);
287done:
288 /*
289 * Move the caller beyond our range so that it keeps making progress.
290 * For that we have to include any leading non-uptodate ranges, but
291 * we can skip trailing ones as they will be handled in the next
292 * iteration.
293 */
294 return pos - orig_pos + plen;
295}
296
297int
298iomap_readpage(struct page *page, const struct iomap_ops *ops)
299{
300 struct iomap_readpage_ctx ctx = { .cur_page = page };
301 struct inode *inode = page->mapping->host;
302 unsigned poff;
303 loff_t ret;
304
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700305 trace_iomap_readpage(page->mapping->host, 1);
306
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700307 for (poff = 0; poff < PAGE_SIZE; poff += ret) {
308 ret = iomap_apply(inode, page_offset(page) + poff,
309 PAGE_SIZE - poff, 0, ops, &ctx,
310 iomap_readpage_actor);
311 if (ret <= 0) {
312 WARN_ON_ONCE(ret == 0);
313 SetPageError(page);
314 break;
315 }
316 }
317
318 if (ctx.bio) {
319 submit_bio(ctx.bio);
320 WARN_ON_ONCE(!ctx.cur_page_in_bio);
321 } else {
322 WARN_ON_ONCE(ctx.cur_page_in_bio);
323 unlock_page(page);
324 }
325
326 /*
327 * Just like mpage_readpages and block_read_full_page we always
328 * return 0 and just mark the page as PageError on errors. This
329 * should be cleaned up all through the stack eventually.
330 */
331 return 0;
332}
333EXPORT_SYMBOL_GPL(iomap_readpage);
334
335static struct page *
336iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
337 loff_t length, loff_t *done)
338{
339 while (!list_empty(pages)) {
340 struct page *page = lru_to_page(pages);
341
342 if (page_offset(page) >= (u64)pos + length)
343 break;
344
345 list_del(&page->lru);
346 if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
347 GFP_NOFS))
348 return page;
349
350 /*
351 * If we already have a page in the page cache at index we are
352 * done. Upper layers don't care if it is uptodate after the
353 * readpages call itself as every page gets checked again once
354 * actually needed.
355 */
356 *done += PAGE_SIZE;
357 put_page(page);
358 }
359
360 return NULL;
361}
362
363static loff_t
364iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
365 void *data, struct iomap *iomap)
366{
367 struct iomap_readpage_ctx *ctx = data;
368 loff_t done, ret;
369
370 for (done = 0; done < length; done += ret) {
371 if (ctx->cur_page && offset_in_page(pos + done) == 0) {
372 if (!ctx->cur_page_in_bio)
373 unlock_page(ctx->cur_page);
374 put_page(ctx->cur_page);
375 ctx->cur_page = NULL;
376 }
377 if (!ctx->cur_page) {
378 ctx->cur_page = iomap_next_page(inode, ctx->pages,
379 pos, length, &done);
380 if (!ctx->cur_page)
381 break;
382 ctx->cur_page_in_bio = false;
383 }
384 ret = iomap_readpage_actor(inode, pos + done, length - done,
385 ctx, iomap);
386 }
387
388 return done;
389}
390
391int
392iomap_readpages(struct address_space *mapping, struct list_head *pages,
393 unsigned nr_pages, const struct iomap_ops *ops)
394{
395 struct iomap_readpage_ctx ctx = {
396 .pages = pages,
397 .is_readahead = true,
398 };
399 loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
400 loff_t last = page_offset(list_entry(pages->next, struct page, lru));
401 loff_t length = last - pos + PAGE_SIZE, ret = 0;
402
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700403 trace_iomap_readpages(mapping->host, nr_pages);
404
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700405 while (length > 0) {
406 ret = iomap_apply(mapping->host, pos, length, 0, ops,
407 &ctx, iomap_readpages_actor);
408 if (ret <= 0) {
409 WARN_ON_ONCE(ret == 0);
410 goto done;
411 }
412 pos += ret;
413 length -= ret;
414 }
415 ret = 0;
416done:
417 if (ctx.bio)
418 submit_bio(ctx.bio);
419 if (ctx.cur_page) {
420 if (!ctx.cur_page_in_bio)
421 unlock_page(ctx.cur_page);
422 put_page(ctx.cur_page);
423 }
424
425 /*
426 * Check that we didn't lose a page due to the arcance calling
427 * conventions..
428 */
429 WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
430 return ret;
431}
432EXPORT_SYMBOL_GPL(iomap_readpages);
433
434/*
435 * iomap_is_partially_uptodate checks whether blocks within a page are
436 * uptodate or not.
437 *
438 * Returns true if all blocks which correspond to a file portion
439 * we want to read within the page are uptodate.
440 */
441int
442iomap_is_partially_uptodate(struct page *page, unsigned long from,
443 unsigned long count)
444{
445 struct iomap_page *iop = to_iomap_page(page);
446 struct inode *inode = page->mapping->host;
447 unsigned len, first, last;
448 unsigned i;
449
450 /* Limit range to one page */
451 len = min_t(unsigned, PAGE_SIZE - from, count);
452
453 /* First and last blocks in range within page */
454 first = from >> inode->i_blkbits;
455 last = (from + len - 1) >> inode->i_blkbits;
456
457 if (iop) {
458 for (i = first; i <= last; i++)
459 if (!test_bit(i, iop->uptodate))
460 return 0;
461 return 1;
462 }
463
464 return 0;
465}
466EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
467
468int
469iomap_releasepage(struct page *page, gfp_t gfp_mask)
470{
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700471 trace_iomap_releasepage(page->mapping->host, page, 0, 0);
472
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700473 /*
474 * mm accommodates an old ext3 case where clean pages might not have had
475 * the dirty bit cleared. Thus, it can send actual dirty pages to
476 * ->releasepage() via shrink_active_list(), skip those here.
477 */
478 if (PageDirty(page) || PageWriteback(page))
479 return 0;
480 iomap_page_release(page);
481 return 1;
482}
483EXPORT_SYMBOL_GPL(iomap_releasepage);
484
485void
486iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
487{
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700488 trace_iomap_invalidatepage(page->mapping->host, page, offset, len);
489
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700490 /*
491 * If we are invalidating the entire page, clear the dirty state from it
492 * and release it to avoid unnecessary buildup of the LRU.
493 */
494 if (offset == 0 && len == PAGE_SIZE) {
495 WARN_ON_ONCE(PageWriteback(page));
496 cancel_dirty_page(page);
497 iomap_page_release(page);
498 }
499}
500EXPORT_SYMBOL_GPL(iomap_invalidatepage);
501
502#ifdef CONFIG_MIGRATION
503int
504iomap_migrate_page(struct address_space *mapping, struct page *newpage,
505 struct page *page, enum migrate_mode mode)
506{
507 int ret;
508
Linus Torvalds26473f82019-07-19 11:38:12 -0700509 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700510 if (ret != MIGRATEPAGE_SUCCESS)
511 return ret;
512
513 if (page_has_private(page)) {
514 ClearPagePrivate(page);
515 get_page(newpage);
516 set_page_private(newpage, page_private(page));
517 set_page_private(page, 0);
518 put_page(page);
519 SetPagePrivate(newpage);
520 }
521
522 if (mode != MIGRATE_SYNC_NO_COPY)
523 migrate_page_copy(newpage, page);
524 else
525 migrate_page_states(newpage, page);
526 return MIGRATEPAGE_SUCCESS;
527}
528EXPORT_SYMBOL_GPL(iomap_migrate_page);
529#endif /* CONFIG_MIGRATION */
530
531static void
532iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
533{
534 loff_t i_size = i_size_read(inode);
535
536 /*
537 * Only truncate newly allocated pages beyoned EOF, even if the
538 * write started inside the existing inode size.
539 */
540 if (pos + len > i_size)
541 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
542}
543
544static int
545iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
546 unsigned poff, unsigned plen, unsigned from, unsigned to,
547 struct iomap *iomap)
548{
549 struct bio_vec bvec;
550 struct bio bio;
551
Christoph Hellwig009d8d82019-10-17 13:12:12 -0700552 if (iomap_block_needs_zeroing(inode, iomap, block_start)) {
Darrick J. Wongafc51aa2019-07-15 08:50:59 -0700553 zero_user_segments(page, poff, from, to, poff + plen);
554 iomap_set_range_uptodate(page, poff, plen);
555 return 0;
556 }
557
558 bio_init(&bio, &bvec, 1);
559 bio.bi_opf = REQ_OP_READ;
560 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
561 bio_set_dev(&bio, iomap->bdev);
562 __bio_add_page(&bio, page, plen, poff);
563 return submit_bio_wait(&bio);
564}
565
566static int
567__iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
568 struct page *page, struct iomap *iomap)
569{
570 struct iomap_page *iop = iomap_page_create(inode, page);
571 loff_t block_size = i_blocksize(inode);
572 loff_t block_start = pos & ~(block_size - 1);
573 loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
574 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
575 int status = 0;
576
577 if (PageUptodate(page))
578 return 0;
579
580 do {
581 iomap_adjust_read_range(inode, iop, &block_start,
582 block_end - block_start, &poff, &plen);
583 if (plen == 0)
584 break;
585
586 if ((from > poff && from < poff + plen) ||
587 (to > poff && to < poff + plen)) {
588 status = iomap_read_page_sync(inode, block_start, page,
589 poff, plen, from, to, iomap);
590 if (status)
591 break;
592 }
593
594 } while ((block_start += plen) < block_end);
595
596 return status;
597}
598
599static int
600iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
601 struct page **pagep, struct iomap *iomap)
602{
603 const struct iomap_page_ops *page_ops = iomap->page_ops;
604 pgoff_t index = pos >> PAGE_SHIFT;
605 struct page *page;
606 int status = 0;
607
608 BUG_ON(pos + len > iomap->offset + iomap->length);
609
610 if (fatal_signal_pending(current))
611 return -EINTR;
612
613 if (page_ops && page_ops->page_prepare) {
614 status = page_ops->page_prepare(inode, pos, len, iomap);
615 if (status)
616 return status;
617 }
618
619 page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
620 if (!page) {
621 status = -ENOMEM;
622 goto out_no_page;
623 }
624
625 if (iomap->type == IOMAP_INLINE)
626 iomap_read_inline_data(inode, page, iomap);
627 else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
628 status = __block_write_begin_int(page, pos, len, NULL, iomap);
629 else
630 status = __iomap_write_begin(inode, pos, len, page, iomap);
631
632 if (unlikely(status))
633 goto out_unlock;
634
635 *pagep = page;
636 return 0;
637
638out_unlock:
639 unlock_page(page);
640 put_page(page);
641 iomap_write_failed(inode, pos, len);
642
643out_no_page:
644 if (page_ops && page_ops->page_done)
645 page_ops->page_done(inode, pos, 0, NULL, iomap);
646 return status;
647}
648
649int
650iomap_set_page_dirty(struct page *page)
651{
652 struct address_space *mapping = page_mapping(page);
653 int newly_dirty;
654
655 if (unlikely(!mapping))
656 return !TestSetPageDirty(page);
657
658 /*
659 * Lock out page->mem_cgroup migration to keep PageDirty
660 * synchronized with per-memcg dirty page counters.
661 */
662 lock_page_memcg(page);
663 newly_dirty = !TestSetPageDirty(page);
664 if (newly_dirty)
665 __set_page_dirty(page, mapping, 0);
666 unlock_page_memcg(page);
667
668 if (newly_dirty)
669 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
670 return newly_dirty;
671}
672EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
673
674static int
675__iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
676 unsigned copied, struct page *page, struct iomap *iomap)
677{
678 flush_dcache_page(page);
679
680 /*
681 * The blocks that were entirely written will now be uptodate, so we
682 * don't have to worry about a readpage reading them and overwriting a
683 * partial write. However if we have encountered a short write and only
684 * partially written into a block, it will not be marked uptodate, so a
685 * readpage might come in and destroy our partial write.
686 *
687 * Do the simplest thing, and just treat any short write to a non
688 * uptodate page as a zero-length write, and force the caller to redo
689 * the whole thing.
690 */
691 if (unlikely(copied < len && !PageUptodate(page)))
692 return 0;
693 iomap_set_range_uptodate(page, offset_in_page(pos), len);
694 iomap_set_page_dirty(page);
695 return copied;
696}
697
698static int
699iomap_write_end_inline(struct inode *inode, struct page *page,
700 struct iomap *iomap, loff_t pos, unsigned copied)
701{
702 void *addr;
703
704 WARN_ON_ONCE(!PageUptodate(page));
705 BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
706
707 addr = kmap_atomic(page);
708 memcpy(iomap->inline_data + pos, addr + pos, copied);
709 kunmap_atomic(addr);
710
711 mark_inode_dirty(inode);
712 return copied;
713}
714
715static int
716iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
717 unsigned copied, struct page *page, struct iomap *iomap)
718{
719 const struct iomap_page_ops *page_ops = iomap->page_ops;
720 loff_t old_size = inode->i_size;
721 int ret;
722
723 if (iomap->type == IOMAP_INLINE) {
724 ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
725 } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
726 ret = block_write_end(NULL, inode->i_mapping, pos, len, copied,
727 page, NULL);
728 } else {
729 ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
730 }
731
732 /*
733 * Update the in-memory inode size after copying the data into the page
734 * cache. It's up to the file system to write the updated size to disk,
735 * preferably after I/O completion so that no stale data is exposed.
736 */
737 if (pos + ret > old_size) {
738 i_size_write(inode, pos + ret);
739 iomap->flags |= IOMAP_F_SIZE_CHANGED;
740 }
741 unlock_page(page);
742
743 if (old_size < pos)
744 pagecache_isize_extended(inode, old_size, pos);
745 if (page_ops && page_ops->page_done)
746 page_ops->page_done(inode, pos, ret, page, iomap);
747 put_page(page);
748
749 if (ret < len)
750 iomap_write_failed(inode, pos, len);
751 return ret;
752}
753
754static loff_t
755iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
756 struct iomap *iomap)
757{
758 struct iov_iter *i = data;
759 long status = 0;
760 ssize_t written = 0;
761 unsigned int flags = AOP_FLAG_NOFS;
762
763 do {
764 struct page *page;
765 unsigned long offset; /* Offset into pagecache page */
766 unsigned long bytes; /* Bytes to write to page */
767 size_t copied; /* Bytes copied from user */
768
769 offset = offset_in_page(pos);
770 bytes = min_t(unsigned long, PAGE_SIZE - offset,
771 iov_iter_count(i));
772again:
773 if (bytes > length)
774 bytes = length;
775
776 /*
777 * Bring in the user page that we will copy from _first_.
778 * Otherwise there's a nasty deadlock on copying from the
779 * same page as we're writing to, without it being marked
780 * up-to-date.
781 *
782 * Not only is this an optimisation, but it is also required
783 * to check that the address is actually valid, when atomic
784 * usercopies are used, below.
785 */
786 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
787 status = -EFAULT;
788 break;
789 }
790
791 status = iomap_write_begin(inode, pos, bytes, flags, &page,
792 iomap);
793 if (unlikely(status))
794 break;
795
796 if (mapping_writably_mapped(inode->i_mapping))
797 flush_dcache_page(page);
798
799 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
800
801 flush_dcache_page(page);
802
803 status = iomap_write_end(inode, pos, bytes, copied, page,
804 iomap);
805 if (unlikely(status < 0))
806 break;
807 copied = status;
808
809 cond_resched();
810
811 iov_iter_advance(i, copied);
812 if (unlikely(copied == 0)) {
813 /*
814 * If we were unable to copy any data at all, we must
815 * fall back to a single segment length write.
816 *
817 * If we didn't fallback here, we could livelock
818 * because not all segments in the iov can be copied at
819 * once without a pagefault.
820 */
821 bytes = min_t(unsigned long, PAGE_SIZE - offset,
822 iov_iter_single_seg_count(i));
823 goto again;
824 }
825 pos += copied;
826 written += copied;
827 length -= copied;
828
829 balance_dirty_pages_ratelimited(inode->i_mapping);
830 } while (iov_iter_count(i) && length);
831
832 return written ? written : status;
833}
834
835ssize_t
836iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
837 const struct iomap_ops *ops)
838{
839 struct inode *inode = iocb->ki_filp->f_mapping->host;
840 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
841
842 while (iov_iter_count(iter)) {
843 ret = iomap_apply(inode, pos, iov_iter_count(iter),
844 IOMAP_WRITE, ops, iter, iomap_write_actor);
845 if (ret <= 0)
846 break;
847 pos += ret;
848 written += ret;
849 }
850
851 return written ? written : ret;
852}
853EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
854
855static struct page *
856__iomap_read_page(struct inode *inode, loff_t offset)
857{
858 struct address_space *mapping = inode->i_mapping;
859 struct page *page;
860
861 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
862 if (IS_ERR(page))
863 return page;
864 if (!PageUptodate(page)) {
865 put_page(page);
866 return ERR_PTR(-EIO);
867 }
868 return page;
869}
870
871static loff_t
872iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
873 struct iomap *iomap)
874{
875 long status = 0;
876 ssize_t written = 0;
877
878 do {
879 struct page *page, *rpage;
880 unsigned long offset; /* Offset into pagecache page */
881 unsigned long bytes; /* Bytes to write to page */
882
883 offset = offset_in_page(pos);
884 bytes = min_t(loff_t, PAGE_SIZE - offset, length);
885
886 rpage = __iomap_read_page(inode, pos);
887 if (IS_ERR(rpage))
888 return PTR_ERR(rpage);
889
890 status = iomap_write_begin(inode, pos, bytes,
891 AOP_FLAG_NOFS, &page, iomap);
892 put_page(rpage);
893 if (unlikely(status))
894 return status;
895
896 WARN_ON_ONCE(!PageUptodate(page));
897
898 status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
899 if (unlikely(status <= 0)) {
900 if (WARN_ON_ONCE(status == 0))
901 return -EIO;
902 return status;
903 }
904
905 cond_resched();
906
907 pos += status;
908 written += status;
909 length -= status;
910
911 balance_dirty_pages_ratelimited(inode->i_mapping);
912 } while (length);
913
914 return written;
915}
916
917int
918iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
919 const struct iomap_ops *ops)
920{
921 loff_t ret;
922
923 while (len) {
924 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
925 iomap_dirty_actor);
926 if (ret <= 0)
927 return ret;
928 pos += ret;
929 len -= ret;
930 }
931
932 return 0;
933}
934EXPORT_SYMBOL_GPL(iomap_file_dirty);
935
936static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
937 unsigned bytes, struct iomap *iomap)
938{
939 struct page *page;
940 int status;
941
942 status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
943 iomap);
944 if (status)
945 return status;
946
947 zero_user(page, offset, bytes);
948 mark_page_accessed(page);
949
950 return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
951}
952
953static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
954 struct iomap *iomap)
955{
956 return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
957 iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
958}
959
960static loff_t
961iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
962 void *data, struct iomap *iomap)
963{
964 bool *did_zero = data;
965 loff_t written = 0;
966 int status;
967
968 /* already zeroed? we're done. */
969 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
970 return count;
971
972 do {
973 unsigned offset, bytes;
974
975 offset = offset_in_page(pos);
976 bytes = min_t(loff_t, PAGE_SIZE - offset, count);
977
978 if (IS_DAX(inode))
979 status = iomap_dax_zero(pos, offset, bytes, iomap);
980 else
981 status = iomap_zero(inode, pos, offset, bytes, iomap);
982 if (status < 0)
983 return status;
984
985 pos += bytes;
986 count -= bytes;
987 written += bytes;
988 if (did_zero)
989 *did_zero = true;
990 } while (count > 0);
991
992 return written;
993}
994
995int
996iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
997 const struct iomap_ops *ops)
998{
999 loff_t ret;
1000
1001 while (len > 0) {
1002 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
1003 ops, did_zero, iomap_zero_range_actor);
1004 if (ret <= 0)
1005 return ret;
1006
1007 pos += ret;
1008 len -= ret;
1009 }
1010
1011 return 0;
1012}
1013EXPORT_SYMBOL_GPL(iomap_zero_range);
1014
1015int
1016iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1017 const struct iomap_ops *ops)
1018{
1019 unsigned int blocksize = i_blocksize(inode);
1020 unsigned int off = pos & (blocksize - 1);
1021
1022 /* Block boundary? Nothing to do */
1023 if (!off)
1024 return 0;
1025 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1026}
1027EXPORT_SYMBOL_GPL(iomap_truncate_page);
1028
1029static loff_t
1030iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
1031 void *data, struct iomap *iomap)
1032{
1033 struct page *page = data;
1034 int ret;
1035
1036 if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
1037 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
1038 if (ret)
1039 return ret;
1040 block_commit_write(page, 0, length);
1041 } else {
1042 WARN_ON_ONCE(!PageUptodate(page));
1043 iomap_page_create(inode, page);
1044 set_page_dirty(page);
1045 }
1046
1047 return length;
1048}
1049
1050vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1051{
1052 struct page *page = vmf->page;
1053 struct inode *inode = file_inode(vmf->vma->vm_file);
1054 unsigned long length;
1055 loff_t offset, size;
1056 ssize_t ret;
1057
1058 lock_page(page);
1059 size = i_size_read(inode);
1060 if ((page->mapping != inode->i_mapping) ||
1061 (page_offset(page) > size)) {
1062 /* We overload EFAULT to mean page got truncated */
1063 ret = -EFAULT;
1064 goto out_unlock;
1065 }
1066
1067 /* page is wholly or partially inside EOF */
1068 if (((page->index + 1) << PAGE_SHIFT) > size)
1069 length = offset_in_page(size);
1070 else
1071 length = PAGE_SIZE;
1072
1073 offset = page_offset(page);
1074 while (length > 0) {
1075 ret = iomap_apply(inode, offset, length,
1076 IOMAP_WRITE | IOMAP_FAULT, ops, page,
1077 iomap_page_mkwrite_actor);
1078 if (unlikely(ret <= 0))
1079 goto out_unlock;
1080 offset += ret;
1081 length -= ret;
1082 }
1083
1084 wait_for_stable_page(page);
1085 return VM_FAULT_LOCKED;
1086out_unlock:
1087 unlock_page(page);
1088 return block_page_mkwrite_return(ret);
1089}
1090EXPORT_SYMBOL_GPL(iomap_page_mkwrite);