]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ext4/page-io.c
ext4: Fix data exposure after failed AIO DIO
[mirror_ubuntu-bionic-kernel.git] / fs / ext4 / page-io.c
CommitLineData
bd2d0210
TT
1/*
2 * linux/fs/ext4/page-io.c
3 *
4 * This contains the new page_io functions for ext4
5 *
6 * Written by Theodore Ts'o, 2010.
7 */
8
bd2d0210
TT
9#include <linux/fs.h>
10#include <linux/time.h>
bd2d0210
TT
11#include <linux/highuid.h>
12#include <linux/pagemap.h>
13#include <linux/quotaops.h>
14#include <linux/string.h>
15#include <linux/buffer_head.h>
16#include <linux/writeback.h>
17#include <linux/pagevec.h>
18#include <linux/mpage.h>
19#include <linux/namei.h>
20#include <linux/uio.h>
21#include <linux/bio.h>
22#include <linux/workqueue.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
1ae48a63 25#include <linux/mm.h>
bd2d0210
TT
26
27#include "ext4_jbd2.h"
28#include "xattr.h"
29#include "acl.h"
bd2d0210 30
0058f965 31static struct kmem_cache *io_end_cachep;
bd2d0210 32
5dabfc78 33int __init ext4_init_pageio(void)
bd2d0210 34{
bd2d0210 35 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
0058f965 36 if (io_end_cachep == NULL)
bd2d0210 37 return -ENOMEM;
bd2d0210
TT
38 return 0;
39}
40
5dabfc78 41void ext4_exit_pageio(void)
bd2d0210
TT
42{
43 kmem_cache_destroy(io_end_cachep);
bd2d0210
TT
44}
45
b0857d30
JK
46/*
47 * Print an buffer I/O error compatible with the fs/buffer.c. This
48 * provides compatibility with dmesg scrapers that look for a specific
49 * buffer I/O error message. We really need a unified error reporting
50 * structure to userspace ala Digital Unix's uerf system, but it's
51 * probably not going to happen in my lifetime, due to LKML politics...
52 */
53static void buffer_io_error(struct buffer_head *bh)
54{
a1c6f057
DM
55 printk_ratelimited(KERN_ERR "Buffer I/O error on device %pg, logical block %llu\n",
56 bh->b_bdev,
b0857d30
JK
57 (unsigned long long)bh->b_blocknr);
58}
59
60static void ext4_finish_bio(struct bio *bio)
61{
62 int i;
2c30c71b 63 struct bio_vec *bvec;
b0857d30 64
2c30c71b 65 bio_for_each_segment_all(bvec, bio, i) {
b0857d30 66 struct page *page = bvec->bv_page;
2058f83a
MH
67#ifdef CONFIG_EXT4_FS_ENCRYPTION
68 struct page *data_page = NULL;
69 struct ext4_crypto_ctx *ctx = NULL;
70#endif
b0857d30
JK
71 struct buffer_head *bh, *head;
72 unsigned bio_start = bvec->bv_offset;
73 unsigned bio_end = bio_start + bvec->bv_len;
74 unsigned under_io = 0;
75 unsigned long flags;
76
77 if (!page)
78 continue;
79
2058f83a
MH
80#ifdef CONFIG_EXT4_FS_ENCRYPTION
81 if (!page->mapping) {
82 /* The bounce data pages are unmapped. */
83 data_page = page;
84 ctx = (struct ext4_crypto_ctx *)page_private(data_page);
614def70 85 page = ctx->w.control_page;
2058f83a
MH
86 }
87#endif
88
4246a0b6 89 if (bio->bi_error) {
b0857d30
JK
90 SetPageError(page);
91 set_bit(AS_EIO, &page->mapping->flags);
92 }
93 bh = head = page_buffers(page);
94 /*
95 * We check all buffers in the page under BH_Uptodate_Lock
96 * to avoid races with other end io clearing async_write flags
97 */
98 local_irq_save(flags);
99 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
100 do {
101 if (bh_offset(bh) < bio_start ||
102 bh_offset(bh) + bh->b_size > bio_end) {
103 if (buffer_async_write(bh))
104 under_io++;
105 continue;
106 }
107 clear_buffer_async_write(bh);
4246a0b6 108 if (bio->bi_error)
b0857d30
JK
109 buffer_io_error(bh);
110 } while ((bh = bh->b_this_page) != head);
111 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
112 local_irq_restore(flags);
2058f83a
MH
113 if (!under_io) {
114#ifdef CONFIG_EXT4_FS_ENCRYPTION
115 if (ctx)
116 ext4_restore_control_page(data_page);
117#endif
b0857d30 118 end_page_writeback(page);
2058f83a 119 }
b0857d30
JK
120 }
121}
122
97a851ed 123static void ext4_release_io_end(ext4_io_end_t *io_end)
bd2d0210 124{
b0857d30
JK
125 struct bio *bio, *next_bio;
126
97a851ed
JK
127 BUG_ON(!list_empty(&io_end->list));
128 BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
6b523df4 129 WARN_ON(io_end->handle);
97a851ed
JK
130
131 if (atomic_dec_and_test(&EXT4_I(io_end->inode)->i_ioend_count))
132 wake_up_all(ext4_ioend_wq(io_end->inode));
b0857d30
JK
133
134 for (bio = io_end->bio; bio; bio = next_bio) {
135 next_bio = bio->bi_private;
136 ext4_finish_bio(bio);
137 bio_put(bio);
138 }
97a851ed
JK
139 kmem_cache_free(io_end_cachep, io_end);
140}
141
a115f749
JK
142/*
143 * Check a range of space and convert unwritten extents to written. Note that
144 * we are protected from truncate touching same part of extent tree by the
145 * fact that truncate code waits for all DIO to finish (thus exclusion from
146 * direct IO is achieved) and also waits for PageWriteback bits. Thus we
147 * cannot get to ext4_ext_truncate() before all IOs overlapping that range are
148 * completed (happens from ext4_free_ioend()).
149 */
28a535f9 150static int ext4_end_io(ext4_io_end_t *io)
bd2d0210
TT
151{
152 struct inode *inode = io->inode;
153 loff_t offset = io->offset;
154 ssize_t size = io->size;
6b523df4 155 handle_t *handle = io->handle;
bd2d0210
TT
156 int ret = 0;
157
158 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
159 "list->prev 0x%p\n",
160 io, inode->i_ino, io->list.next, io->list.prev);
161
6b523df4
JK
162 io->handle = NULL; /* Following call will use up the handle */
163 ret = ext4_convert_unwritten_extents(handle, inode, offset, size);
bd2d0210 164 if (ret < 0) {
b82e384c
TT
165 ext4_msg(inode->i_sb, KERN_EMERG,
166 "failed to convert unwritten extents to written "
167 "extents -- potential data loss! "
168 "(inode %lu, offset %llu, size %zd, error %d)",
169 inode->i_ino, offset, size, ret);
bd2d0210 170 }
97a851ed
JK
171 ext4_clear_io_unwritten_flag(io);
172 ext4_release_io_end(io);
bd2d0210
TT
173 return ret;
174}
175
2e8fa54e 176static void dump_completed_IO(struct inode *inode, struct list_head *head)
28a535f9
DM
177{
178#ifdef EXT4FS_DEBUG
179 struct list_head *cur, *before, *after;
180 ext4_io_end_t *io, *io0, *io1;
28a535f9 181
2e8fa54e 182 if (list_empty(head))
28a535f9 183 return;
28a535f9 184
2e8fa54e
JK
185 ext4_debug("Dump inode %lu completed io list\n", inode->i_ino);
186 list_for_each_entry(io, head, list) {
28a535f9
DM
187 cur = &io->list;
188 before = cur->prev;
189 io0 = container_of(before, ext4_io_end_t, list);
190 after = cur->next;
191 io1 = container_of(after, ext4_io_end_t, list);
192
193 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
194 io, inode->i_ino, io0, io1);
195 }
196#endif
197}
198
199/* Add the io_end to per-inode completed end_io list. */
97a851ed 200static void ext4_add_complete_io(ext4_io_end_t *io_end)
bd2d0210 201{
28a535f9 202 struct ext4_inode_info *ei = EXT4_I(io_end->inode);
78371a45 203 struct ext4_sb_info *sbi = EXT4_SB(io_end->inode->i_sb);
28a535f9
DM
204 struct workqueue_struct *wq;
205 unsigned long flags;
206
7b7a8665
CH
207 /* Only reserved conversions from writeback should enter here */
208 WARN_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
78371a45 209 WARN_ON(!io_end->handle && sbi->s_journal);
d73d5046 210 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
78371a45 211 wq = sbi->rsv_conversion_wq;
7b7a8665
CH
212 if (list_empty(&ei->i_rsv_conversion_list))
213 queue_work(wq, &ei->i_rsv_conversion_work);
214 list_add_tail(&io_end->list, &ei->i_rsv_conversion_list);
28a535f9
DM
215 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
216}
d73d5046 217
2e8fa54e
JK
218static int ext4_do_flush_completed_IO(struct inode *inode,
219 struct list_head *head)
28a535f9
DM
220{
221 ext4_io_end_t *io;
002bd7fa 222 struct list_head unwritten;
28a535f9
DM
223 unsigned long flags;
224 struct ext4_inode_info *ei = EXT4_I(inode);
225 int err, ret = 0;
226
28a535f9 227 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
2e8fa54e
JK
228 dump_completed_IO(inode, head);
229 list_replace_init(head, &unwritten);
28a535f9
DM
230 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
231
232 while (!list_empty(&unwritten)) {
233 io = list_entry(unwritten.next, ext4_io_end_t, list);
234 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
235 list_del_init(&io->list);
236
237 err = ext4_end_io(io);
238 if (unlikely(!ret && err))
239 ret = err;
28a535f9
DM
240 }
241 return ret;
242}
243
244/*
2e8fa54e 245 * work on completed IO, to convert unwritten extents to extents
28a535f9 246 */
2e8fa54e
JK
247void ext4_end_io_rsv_work(struct work_struct *work)
248{
249 struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
250 i_rsv_conversion_work);
251 ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_rsv_conversion_list);
252}
253
bd2d0210
TT
254ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
255{
b17b35ec 256 ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
bd2d0210 257 if (io) {
f7ad6d2e
TT
258 atomic_inc(&EXT4_I(inode)->i_ioend_count);
259 io->inode = inode;
bd2d0210 260 INIT_LIST_HEAD(&io->list);
97a851ed 261 atomic_set(&io->count, 1);
bd2d0210
TT
262 }
263 return io;
264}
265
97a851ed
JK
266void ext4_put_io_end_defer(ext4_io_end_t *io_end)
267{
268 if (atomic_dec_and_test(&io_end->count)) {
269 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
270 ext4_release_io_end(io_end);
271 return;
272 }
273 ext4_add_complete_io(io_end);
274 }
275}
276
277int ext4_put_io_end(ext4_io_end_t *io_end)
278{
279 int err = 0;
280
281 if (atomic_dec_and_test(&io_end->count)) {
282 if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
6b523df4
JK
283 err = ext4_convert_unwritten_extents(io_end->handle,
284 io_end->inode, io_end->offset,
285 io_end->size);
286 io_end->handle = NULL;
97a851ed
JK
287 ext4_clear_io_unwritten_flag(io_end);
288 }
289 ext4_release_io_end(io_end);
290 }
291 return err;
292}
293
294ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
295{
296 atomic_inc(&io_end->count);
297 return io_end;
298}
299
822dbba3 300/* BIO completion function for page writeback */
4246a0b6 301static void ext4_end_bio(struct bio *bio)
bd2d0210
TT
302{
303 ext4_io_end_t *io_end = bio->bi_private;
4f024f37 304 sector_t bi_sector = bio->bi_iter.bi_sector;
bd2d0210
TT
305
306 BUG_ON(!io_end);
bd2d0210 307 bio->bi_end_io = NULL;
0058f965 308
4246a0b6 309 if (bio->bi_error) {
b0857d30
JK
310 struct inode *inode = io_end->inode;
311
9503c67c 312 ext4_warning(inode->i_sb, "I/O error %d writing to inode %lu "
f7ad6d2e 313 "(offset %llu size %ld starting block %llu)",
4246a0b6 314 bio->bi_error, inode->i_ino,
f7ad6d2e
TT
315 (unsigned long long) io_end->offset,
316 (long) io_end->size,
317 (unsigned long long)
d50bdd5a 318 bi_sector >> (inode->i_blkbits - 9));
4246a0b6 319 mapping_set_error(inode->i_mapping, bio->bi_error);
f7ad6d2e 320 }
822dbba3
JK
321
322 if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
323 /*
324 * Link bio into list hanging from io_end. We have to do it
325 * atomically as bio completions can be racing against each
326 * other.
327 */
328 bio->bi_private = xchg(&io_end->bio, bio);
329 ext4_put_io_end_defer(io_end);
330 } else {
331 /*
332 * Drop io_end reference early. Inode can get freed once
333 * we finish the bio.
334 */
335 ext4_put_io_end_defer(io_end);
336 ext4_finish_bio(bio);
337 bio_put(bio);
338 }
bd2d0210
TT
339}
340
341void ext4_io_submit(struct ext4_io_submit *io)
342{
343 struct bio *bio = io->io_bio;
344
345 if (bio) {
5a33911f
TH
346 int io_op = io->io_wbc->sync_mode == WB_SYNC_ALL ?
347 WRITE_SYNC : WRITE;
bd2d0210 348 bio_get(io->io_bio);
5a33911f 349 submit_bio(io_op, io->io_bio);
bd2d0210
TT
350 bio_put(io->io_bio);
351 }
7dc57615 352 io->io_bio = NULL;
97a851ed
JK
353}
354
355void ext4_io_submit_init(struct ext4_io_submit *io,
356 struct writeback_control *wbc)
357{
5a33911f 358 io->io_wbc = wbc;
97a851ed 359 io->io_bio = NULL;
7dc57615 360 io->io_end = NULL;
bd2d0210
TT
361}
362
97a851ed
JK
363static int io_submit_init_bio(struct ext4_io_submit *io,
364 struct buffer_head *bh)
bd2d0210 365{
bd2d0210
TT
366 struct bio *bio;
367
b54ffb73 368 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
a1d8d9a7
TT
369 if (!bio)
370 return -ENOMEM;
001e4a87 371 wbc_init_bio(io->io_wbc, bio);
4f024f37 372 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
bd2d0210 373 bio->bi_bdev = bh->b_bdev;
bd2d0210 374 bio->bi_end_io = ext4_end_bio;
97a851ed 375 bio->bi_private = ext4_get_io_end(io->io_end);
bd2d0210 376 io->io_bio = bio;
bd2d0210
TT
377 io->io_next_block = bh->b_blocknr;
378 return 0;
379}
380
381static int io_submit_add_bh(struct ext4_io_submit *io,
bd2d0210 382 struct inode *inode,
2058f83a 383 struct page *page,
bd2d0210
TT
384 struct buffer_head *bh)
385{
bd2d0210
TT
386 int ret;
387
bd2d0210
TT
388 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
389submit_and_retry:
390 ext4_io_submit(io);
391 }
392 if (io->io_bio == NULL) {
97a851ed 393 ret = io_submit_init_bio(io, bh);
bd2d0210
TT
394 if (ret)
395 return ret;
396 }
2058f83a 397 ret = bio_add_page(io->io_bio, page, bh->b_size, bh_offset(bh));
97a851ed
JK
398 if (ret != bh->b_size)
399 goto submit_and_retry;
001e4a87 400 wbc_account_io(io->io_wbc, page, bh->b_size);
bd2d0210 401 io->io_next_block++;
bd2d0210
TT
402 return 0;
403}
404
405int ext4_bio_write_page(struct ext4_io_submit *io,
406 struct page *page,
407 int len,
1c8349a1
NJ
408 struct writeback_control *wbc,
409 bool keep_towrite)
bd2d0210 410{
2058f83a 411 struct page *data_page = NULL;
bd2d0210 412 struct inode *inode = page->mapping->host;
0058f965 413 unsigned block_start, blocksize;
bd2d0210
TT
414 struct buffer_head *bh, *head;
415 int ret = 0;
0058f965 416 int nr_submitted = 0;
937d7b84 417 int nr_to_submit = 0;
bd2d0210
TT
418
419 blocksize = 1 << inode->i_blkbits;
420
d50bdd5a 421 BUG_ON(!PageLocked(page));
bd2d0210 422 BUG_ON(PageWriteback(page));
bd2d0210 423
1c8349a1
NJ
424 if (keep_towrite)
425 set_page_writeback_keepwrite(page);
426 else
427 set_page_writeback(page);
a54aa761 428 ClearPageError(page);
bd2d0210 429
eeece469 430 /*
f8409abd 431 * Comments copied from block_write_full_page:
eeece469
JK
432 *
433 * The page straddles i_size. It must be zeroed out on each and every
434 * writepage invocation because it may be mmapped. "A file is mapped
435 * in multiples of the page size. For a file that is not a multiple of
436 * the page size, the remaining memory is zeroed when mapped, and
437 * writes to that region are not written out to the file."
438 */
439 if (len < PAGE_CACHE_SIZE)
440 zero_user_segment(page, len, PAGE_CACHE_SIZE);
0058f965
JK
441 /*
442 * In the first loop we prepare and mark buffers to submit. We have to
443 * mark all buffers in the page before submitting so that
444 * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
445 * on the first buffer finishes and we are still working on submitting
446 * the second buffer.
447 */
448 bh = head = page_buffers(page);
449 do {
450 block_start = bh_offset(bh);
bd2d0210
TT
451 if (block_start >= len) {
452 clear_buffer_dirty(bh);
453 set_buffer_uptodate(bh);
454 continue;
455 }
8a850c3f
JK
456 if (!buffer_dirty(bh) || buffer_delay(bh) ||
457 !buffer_mapped(bh) || buffer_unwritten(bh)) {
458 /* A hole? We can safely clear the dirty bit */
459 if (!buffer_mapped(bh))
460 clear_buffer_dirty(bh);
461 if (io->io_bio)
462 ext4_io_submit(io);
463 continue;
464 }
0058f965
JK
465 if (buffer_new(bh)) {
466 clear_buffer_new(bh);
467 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
468 }
469 set_buffer_async_write(bh);
937d7b84 470 nr_to_submit++;
0058f965
JK
471 } while ((bh = bh->b_this_page) != head);
472
0058f965 473 bh = head = page_buffers(page);
2058f83a 474
937d7b84
TT
475 if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode) &&
476 nr_to_submit) {
2058f83a
MH
477 data_page = ext4_encrypt(inode, page);
478 if (IS_ERR(data_page)) {
479 ret = PTR_ERR(data_page);
480 data_page = NULL;
481 goto out;
482 }
483 }
484
485 /* Now submit buffers to write */
0058f965
JK
486 do {
487 if (!buffer_async_write(bh))
488 continue;
2058f83a
MH
489 ret = io_submit_add_bh(io, inode,
490 data_page ? data_page : page, bh);
bd2d0210
TT
491 if (ret) {
492 /*
493 * We only get here on ENOMEM. Not much else
494 * we can do but mark the page as dirty, and
495 * better luck next time.
496 */
bd2d0210
TT
497 break;
498 }
0058f965 499 nr_submitted++;
1ae48a63 500 clear_buffer_dirty(bh);
0058f965
JK
501 } while ((bh = bh->b_this_page) != head);
502
503 /* Error stopped previous loop? Clean up buffers... */
504 if (ret) {
2058f83a
MH
505 out:
506 if (data_page)
507 ext4_restore_control_page(data_page);
508 printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
509 redirty_page_for_writepage(wbc, page);
0058f965
JK
510 do {
511 clear_buffer_async_write(bh);
512 bh = bh->b_this_page;
513 } while (bh != head);
bd2d0210
TT
514 }
515 unlock_page(page);
0058f965
JK
516 /* Nothing submitted - we have to end page writeback */
517 if (!nr_submitted)
518 end_page_writeback(page);
bd2d0210
TT
519 return ret;
520}