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