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