2019-07-15 23:50:59 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
|
|
|
* Copyright (c) 2016-2018 Christoph Hellwig.
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/compiler.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/iomap.h>
|
|
|
|
#include <linux/backing-dev.h>
|
|
|
|
#include <linux/uio.h>
|
|
|
|
#include <linux/task_io_accounting_ops.h>
|
2020-07-24 13:45:59 +08:00
|
|
|
#include "trace.h"
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
#include "../internal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private flags for iomap_dio, must not overlap with the public ones in
|
|
|
|
* iomap.h:
|
|
|
|
*/
|
|
|
|
#define IOMAP_DIO_WRITE_FUA (1 << 28)
|
|
|
|
#define IOMAP_DIO_NEED_SYNC (1 << 29)
|
|
|
|
#define IOMAP_DIO_WRITE (1 << 30)
|
|
|
|
#define IOMAP_DIO_DIRTY (1 << 31)
|
|
|
|
|
|
|
|
struct iomap_dio {
|
|
|
|
struct kiocb *iocb;
|
2019-09-20 06:32:45 +08:00
|
|
|
const struct iomap_dio_ops *dops;
|
2019-07-15 23:50:59 +08:00
|
|
|
loff_t i_size;
|
|
|
|
loff_t size;
|
|
|
|
atomic_t ref;
|
|
|
|
unsigned flags;
|
|
|
|
int error;
|
|
|
|
bool wait_for_completion;
|
|
|
|
|
|
|
|
union {
|
|
|
|
/* used during submission and for synchronous completion: */
|
|
|
|
struct {
|
|
|
|
struct iov_iter *iter;
|
|
|
|
struct task_struct *waiter;
|
|
|
|
struct request_queue *last_queue;
|
|
|
|
blk_qc_t cookie;
|
|
|
|
} submit;
|
|
|
|
|
|
|
|
/* used for aio completion: */
|
|
|
|
struct {
|
|
|
|
struct work_struct work;
|
|
|
|
} aio;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
int iomap_dio_iopoll(struct kiocb *kiocb, bool spin)
|
|
|
|
{
|
|
|
|
struct request_queue *q = READ_ONCE(kiocb->private);
|
|
|
|
|
|
|
|
if (!q)
|
|
|
|
return 0;
|
|
|
|
return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
|
|
|
|
|
|
|
|
static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,
|
2019-05-15 07:54:27 +08:00
|
|
|
struct bio *bio, loff_t pos)
|
2019-07-15 23:50:59 +08:00
|
|
|
{
|
|
|
|
atomic_inc(&dio->ref);
|
|
|
|
|
|
|
|
if (dio->iocb->ki_flags & IOCB_HIPRI)
|
|
|
|
bio_set_polled(bio, dio->iocb);
|
|
|
|
|
|
|
|
dio->submit.last_queue = bdev_get_queue(iomap->bdev);
|
2019-05-15 07:54:27 +08:00
|
|
|
if (dio->dops && dio->dops->submit_io)
|
|
|
|
dio->submit.cookie = dio->dops->submit_io(
|
|
|
|
file_inode(dio->iocb->ki_filp),
|
|
|
|
iomap, bio, pos);
|
|
|
|
else
|
|
|
|
dio->submit.cookie = submit_bio(bio);
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 23:51:08 +08:00
|
|
|
ssize_t iomap_dio_complete(struct iomap_dio *dio)
|
2019-07-15 23:50:59 +08:00
|
|
|
{
|
2019-09-20 06:32:45 +08:00
|
|
|
const struct iomap_dio_ops *dops = dio->dops;
|
2019-07-15 23:50:59 +08:00
|
|
|
struct kiocb *iocb = dio->iocb;
|
|
|
|
struct inode *inode = file_inode(iocb->ki_filp);
|
|
|
|
loff_t offset = iocb->ki_pos;
|
2019-09-20 06:32:45 +08:00
|
|
|
ssize_t ret = dio->error;
|
2019-07-15 23:50:59 +08:00
|
|
|
|
2019-09-20 06:32:45 +08:00
|
|
|
if (dops && dops->end_io)
|
|
|
|
ret = dops->end_io(iocb, dio->size, ret, dio->flags);
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
if (likely(!ret)) {
|
|
|
|
ret = dio->size;
|
|
|
|
/* check for short read */
|
|
|
|
if (offset + ret > dio->i_size &&
|
|
|
|
!(dio->flags & IOMAP_DIO_WRITE))
|
|
|
|
ret = dio->i_size - offset;
|
|
|
|
iocb->ki_pos += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try again to invalidate clean pages which might have been cached by
|
|
|
|
* non-direct readahead, or faulted in by get_user_pages() if the source
|
|
|
|
* of the write was an mmap'ed region of the file we're writing. Either
|
|
|
|
* one is a pretty crazy thing to do, so we don't support it 100%. If
|
|
|
|
* this invalidation fails, tough, the write still worked...
|
|
|
|
*
|
2019-09-20 06:32:45 +08:00
|
|
|
* And this page cache invalidation has to be after ->end_io(), as some
|
|
|
|
* filesystems convert unwritten extents to real allocations in
|
|
|
|
* ->end_io() when necessary, otherwise a racing buffer read would cache
|
2019-07-15 23:50:59 +08:00
|
|
|
* zeros from unwritten extents.
|
|
|
|
*/
|
2020-09-10 23:26:16 +08:00
|
|
|
if (!dio->error && dio->size &&
|
2019-07-15 23:50:59 +08:00
|
|
|
(dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
|
|
|
|
int err;
|
|
|
|
err = invalidate_inode_pages2_range(inode->i_mapping,
|
|
|
|
offset >> PAGE_SHIFT,
|
|
|
|
(offset + dio->size - 1) >> PAGE_SHIFT);
|
|
|
|
if (err)
|
|
|
|
dio_warn_stale_pagecache(iocb->ki_filp);
|
|
|
|
}
|
|
|
|
|
2020-09-28 23:51:08 +08:00
|
|
|
inode_dio_end(file_inode(iocb->ki_filp));
|
2019-07-15 23:50:59 +08:00
|
|
|
/*
|
|
|
|
* If this is a DSYNC write, make sure we push it to stable storage now
|
|
|
|
* that we've written data.
|
|
|
|
*/
|
|
|
|
if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
|
|
|
|
ret = generic_write_sync(iocb, ret);
|
|
|
|
|
|
|
|
kfree(dio);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-09-28 23:51:08 +08:00
|
|
|
EXPORT_SYMBOL_GPL(iomap_dio_complete);
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
static void iomap_dio_complete_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
|
|
|
|
struct kiocb *iocb = dio->iocb;
|
|
|
|
|
|
|
|
iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set an error in the dio if none is set yet. We have to use cmpxchg
|
|
|
|
* as the submission context and the completion context(s) can race to
|
|
|
|
* update the error.
|
|
|
|
*/
|
|
|
|
static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
|
|
|
|
{
|
|
|
|
cmpxchg(&dio->error, 0, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iomap_dio_bio_end_io(struct bio *bio)
|
|
|
|
{
|
|
|
|
struct iomap_dio *dio = bio->bi_private;
|
|
|
|
bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
|
|
|
|
|
|
|
|
if (bio->bi_status)
|
|
|
|
iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
|
|
|
|
|
|
|
|
if (atomic_dec_and_test(&dio->ref)) {
|
|
|
|
if (dio->wait_for_completion) {
|
|
|
|
struct task_struct *waiter = dio->submit.waiter;
|
|
|
|
WRITE_ONCE(dio->submit.waiter, NULL);
|
|
|
|
blk_wake_io_task(waiter);
|
|
|
|
} else if (dio->flags & IOMAP_DIO_WRITE) {
|
|
|
|
struct inode *inode = file_inode(dio->iocb->ki_filp);
|
|
|
|
|
|
|
|
INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
|
|
|
|
queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
|
|
|
|
} else {
|
|
|
|
iomap_dio_complete_work(&dio->aio.work);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_dirty) {
|
|
|
|
bio_check_pages_dirty(bio);
|
|
|
|
} else {
|
|
|
|
bio_release_pages(bio, false);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
|
|
|
|
unsigned len)
|
|
|
|
{
|
|
|
|
struct page *page = ZERO_PAGE(0);
|
|
|
|
int flags = REQ_SYNC | REQ_IDLE;
|
|
|
|
struct bio *bio;
|
|
|
|
|
|
|
|
bio = bio_alloc(GFP_KERNEL, 1);
|
|
|
|
bio_set_dev(bio, iomap->bdev);
|
|
|
|
bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
|
|
|
|
bio->bi_private = dio;
|
|
|
|
bio->bi_end_io = iomap_dio_bio_end_io;
|
|
|
|
|
|
|
|
get_page(page);
|
|
|
|
__bio_add_page(bio, page, len, 0);
|
|
|
|
bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
|
2019-05-15 07:54:27 +08:00
|
|
|
iomap_dio_submit_bio(dio, iomap, bio, pos);
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
|
2021-02-04 18:21:41 +08:00
|
|
|
/*
|
|
|
|
* Figure out the bio's operation flags from the dio request, the
|
|
|
|
* mapping, and whether or not we want FUA. Note that we can end up
|
|
|
|
* clearing the WRITE_FUA flag in the dio request.
|
|
|
|
*/
|
|
|
|
static inline unsigned int
|
|
|
|
iomap_dio_bio_opflags(struct iomap_dio *dio, struct iomap *iomap, bool use_fua)
|
|
|
|
{
|
|
|
|
unsigned int opflags = REQ_SYNC | REQ_IDLE;
|
|
|
|
|
|
|
|
if (!(dio->flags & IOMAP_DIO_WRITE)) {
|
|
|
|
WARN_ON_ONCE(iomap->flags & IOMAP_F_ZONE_APPEND);
|
|
|
|
return REQ_OP_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iomap->flags & IOMAP_F_ZONE_APPEND)
|
|
|
|
opflags |= REQ_OP_ZONE_APPEND;
|
|
|
|
else
|
|
|
|
opflags |= REQ_OP_WRITE;
|
|
|
|
|
|
|
|
if (use_fua)
|
|
|
|
opflags |= REQ_FUA;
|
|
|
|
else
|
|
|
|
dio->flags &= ~IOMAP_DIO_WRITE_FUA;
|
|
|
|
|
|
|
|
return opflags;
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:50:59 +08:00
|
|
|
static loff_t
|
|
|
|
iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
|
|
|
|
struct iomap_dio *dio, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
|
|
|
|
unsigned int fs_block_size = i_blocksize(inode), pad;
|
|
|
|
unsigned int align = iov_iter_alignment(dio->submit.iter);
|
2021-02-04 18:21:41 +08:00
|
|
|
unsigned int bio_opf;
|
2019-07-15 23:50:59 +08:00
|
|
|
struct bio *bio;
|
|
|
|
bool need_zeroout = false;
|
|
|
|
bool use_fua = false;
|
|
|
|
int nr_pages, ret = 0;
|
|
|
|
size_t copied = 0;
|
2019-11-27 01:28:47 +08:00
|
|
|
size_t orig_count;
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
if ((pos | length | align) & ((1 << blkbits) - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (iomap->type == IOMAP_UNWRITTEN) {
|
|
|
|
dio->flags |= IOMAP_DIO_UNWRITTEN;
|
|
|
|
need_zeroout = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iomap->flags & IOMAP_F_SHARED)
|
|
|
|
dio->flags |= IOMAP_DIO_COW;
|
|
|
|
|
|
|
|
if (iomap->flags & IOMAP_F_NEW) {
|
|
|
|
need_zeroout = true;
|
|
|
|
} else if (iomap->type == IOMAP_MAPPED) {
|
|
|
|
/*
|
|
|
|
* Use a FUA write if we need datasync semantics, this is a pure
|
|
|
|
* data IO that doesn't require any metadata updates (including
|
|
|
|
* after IO completion such as unwritten extent conversion) and
|
|
|
|
* the underlying device supports FUA. This allows us to avoid
|
|
|
|
* cache flushes on IO completion.
|
|
|
|
*/
|
|
|
|
if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
|
|
|
|
(dio->flags & IOMAP_DIO_WRITE_FUA) &&
|
|
|
|
blk_queue_fua(bdev_get_queue(iomap->bdev)))
|
|
|
|
use_fua = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-11-27 01:28:47 +08:00
|
|
|
* Save the original count and trim the iter to just the extent we
|
|
|
|
* are operating on right now. The iter will be re-expanded once
|
|
|
|
* we are done.
|
2019-07-15 23:50:59 +08:00
|
|
|
*/
|
2019-11-27 01:28:47 +08:00
|
|
|
orig_count = iov_iter_count(dio->submit.iter);
|
|
|
|
iov_iter_truncate(dio->submit.iter, length);
|
2019-07-15 23:50:59 +08:00
|
|
|
|
2021-01-10 00:03:02 +08:00
|
|
|
if (!iov_iter_count(dio->submit.iter))
|
2019-11-27 01:28:47 +08:00
|
|
|
goto out;
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
if (need_zeroout) {
|
|
|
|
/* zero out from the start of the block to the write offset */
|
|
|
|
pad = pos & (fs_block_size - 1);
|
|
|
|
if (pad)
|
|
|
|
iomap_dio_zero(dio, iomap, pos - pad, pad);
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:21:41 +08:00
|
|
|
/*
|
|
|
|
* Set the operation flags early so that bio_iov_iter_get_pages
|
|
|
|
* can set up the page vector appropriately for a ZONE_APPEND
|
|
|
|
* operation.
|
|
|
|
*/
|
|
|
|
bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua);
|
|
|
|
|
2021-03-11 19:01:37 +08:00
|
|
|
nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
|
2019-07-15 23:50:59 +08:00
|
|
|
do {
|
|
|
|
size_t n;
|
|
|
|
if (dio->error) {
|
|
|
|
iov_iter_revert(dio->submit.iter, copied);
|
2019-11-27 01:28:47 +08:00
|
|
|
copied = ret = 0;
|
|
|
|
goto out;
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bio = bio_alloc(GFP_KERNEL, nr_pages);
|
|
|
|
bio_set_dev(bio, iomap->bdev);
|
|
|
|
bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
|
|
|
|
bio->bi_write_hint = dio->iocb->ki_hint;
|
|
|
|
bio->bi_ioprio = dio->iocb->ki_ioprio;
|
|
|
|
bio->bi_private = dio;
|
|
|
|
bio->bi_end_io = iomap_dio_bio_end_io;
|
2021-02-04 18:21:41 +08:00
|
|
|
bio->bi_opf = bio_opf;
|
2019-07-15 23:50:59 +08:00
|
|
|
|
2019-11-27 01:28:47 +08:00
|
|
|
ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
|
2019-07-15 23:50:59 +08:00
|
|
|
if (unlikely(ret)) {
|
|
|
|
/*
|
|
|
|
* We have to stop part way through an IO. We must fall
|
|
|
|
* through to the sub-block tail zeroing here, otherwise
|
|
|
|
* this short IO may expose stale data in the tail of
|
|
|
|
* the block we haven't written data to.
|
|
|
|
*/
|
|
|
|
bio_put(bio);
|
|
|
|
goto zero_tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = bio->bi_iter.bi_size;
|
|
|
|
if (dio->flags & IOMAP_DIO_WRITE) {
|
|
|
|
task_io_account_write(n);
|
|
|
|
} else {
|
|
|
|
if (dio->flags & IOMAP_DIO_DIRTY)
|
|
|
|
bio_set_pages_dirty(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
dio->size += n;
|
|
|
|
copied += n;
|
|
|
|
|
2021-01-10 00:03:02 +08:00
|
|
|
nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter,
|
2021-03-11 19:01:37 +08:00
|
|
|
BIO_MAX_VECS);
|
2019-05-15 07:54:27 +08:00
|
|
|
iomap_dio_submit_bio(dio, iomap, bio, pos);
|
|
|
|
pos += n;
|
2019-07-15 23:50:59 +08:00
|
|
|
} while (nr_pages);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to zeroout the tail of a sub-block write if the extent type
|
|
|
|
* requires zeroing or the write extends beyond EOF. If we don't zero
|
|
|
|
* the block tail in the latter case, we can expose stale data via mmap
|
|
|
|
* reads of the EOF block.
|
|
|
|
*/
|
|
|
|
zero_tail:
|
|
|
|
if (need_zeroout ||
|
|
|
|
((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
|
|
|
|
/* zero out from the end of the write to the end of the block */
|
|
|
|
pad = pos & (fs_block_size - 1);
|
|
|
|
if (pad)
|
|
|
|
iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
|
|
|
|
}
|
2019-11-27 01:28:47 +08:00
|
|
|
out:
|
|
|
|
/* Undo iter limitation to current extent */
|
|
|
|
iov_iter_reexpand(dio->submit.iter, orig_count - copied);
|
2019-11-12 04:58:24 +08:00
|
|
|
if (copied)
|
|
|
|
return copied;
|
|
|
|
return ret;
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
|
|
|
|
{
|
|
|
|
length = iov_iter_zero(length, dio->submit.iter);
|
|
|
|
dio->size += length;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
|
|
|
|
struct iomap_dio *dio, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct iov_iter *iter = dio->submit.iter;
|
2021-08-04 00:38:22 +08:00
|
|
|
void *inline_data = iomap_inline_data(iomap, pos);
|
2019-07-15 23:50:59 +08:00
|
|
|
size_t copied;
|
|
|
|
|
2021-08-04 00:38:22 +08:00
|
|
|
if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
|
|
|
|
return -EIO;
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
if (dio->flags & IOMAP_DIO_WRITE) {
|
|
|
|
loff_t size = inode->i_size;
|
|
|
|
|
|
|
|
if (pos > size)
|
2021-08-04 00:38:22 +08:00
|
|
|
memset(iomap_inline_data(iomap, size), 0, pos - size);
|
|
|
|
copied = copy_from_iter(inline_data, length, iter);
|
2019-07-15 23:50:59 +08:00
|
|
|
if (copied) {
|
|
|
|
if (pos + copied > size)
|
|
|
|
i_size_write(inode, pos + copied);
|
|
|
|
mark_inode_dirty(inode);
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-04 00:38:22 +08:00
|
|
|
copied = copy_to_iter(inline_data, length, iter);
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
dio->size += copied;
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
|
2019-10-19 07:44:10 +08:00
|
|
|
void *data, struct iomap *iomap, struct iomap *srcmap)
|
2019-07-15 23:50:59 +08:00
|
|
|
{
|
|
|
|
struct iomap_dio *dio = data;
|
|
|
|
|
|
|
|
switch (iomap->type) {
|
|
|
|
case IOMAP_HOLE:
|
|
|
|
if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
|
|
|
|
return -EIO;
|
|
|
|
return iomap_dio_hole_actor(length, dio);
|
|
|
|
case IOMAP_UNWRITTEN:
|
|
|
|
if (!(dio->flags & IOMAP_DIO_WRITE))
|
|
|
|
return iomap_dio_hole_actor(length, dio);
|
|
|
|
return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
|
|
|
|
case IOMAP_MAPPED:
|
|
|
|
return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
|
|
|
|
case IOMAP_INLINE:
|
|
|
|
return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
|
2020-09-10 23:26:15 +08:00
|
|
|
case IOMAP_DELALLOC:
|
|
|
|
/*
|
|
|
|
* DIO is not serialised against mmap() access at all, and so
|
|
|
|
* if the page_mkwrite occurs between the writeback and the
|
|
|
|
* iomap_apply() call in the DIO path, then it will see the
|
|
|
|
* DELALLOC block that the page-mkwrite allocated.
|
|
|
|
*/
|
|
|
|
pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
|
|
|
|
dio->iocb->ki_filp, current->comm);
|
|
|
|
return -EIO;
|
2019-07-15 23:50:59 +08:00
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
|
|
|
|
* is being issued as AIO or not. This allows us to optimise pure data writes
|
|
|
|
* to use REQ_FUA rather than requiring generic_write_sync() to issue a
|
|
|
|
* REQ_FLUSH post write. This is slightly tricky because a single request here
|
|
|
|
* can be mapped into multiple disjoint IOs and only a subset of the IOs issued
|
|
|
|
* may be pure data writes. In that case, we still need to do a full data sync
|
|
|
|
* completion.
|
2020-07-24 13:45:59 +08:00
|
|
|
*
|
|
|
|
* Returns -ENOTBLK In case of a page invalidation invalidation failure for
|
|
|
|
* writes. The callers needs to fall back to buffered I/O in this case.
|
2019-07-15 23:50:59 +08:00
|
|
|
*/
|
2020-09-28 23:51:08 +08:00
|
|
|
struct iomap_dio *
|
|
|
|
__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
|
2019-10-15 23:43:42 +08:00
|
|
|
const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
|
2021-01-24 02:06:09 +08:00
|
|
|
unsigned int dio_flags)
|
2019-07-15 23:50:59 +08:00
|
|
|
{
|
|
|
|
struct address_space *mapping = iocb->ki_filp->f_mapping;
|
|
|
|
struct inode *inode = file_inode(iocb->ki_filp);
|
|
|
|
size_t count = iov_iter_count(iter);
|
2019-11-27 01:28:47 +08:00
|
|
|
loff_t pos = iocb->ki_pos;
|
2019-07-15 23:50:59 +08:00
|
|
|
loff_t end = iocb->ki_pos + count - 1, ret = 0;
|
2021-01-24 02:06:09 +08:00
|
|
|
bool wait_for_completion =
|
|
|
|
is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
|
2021-01-24 02:06:09 +08:00
|
|
|
unsigned int iomap_flags = IOMAP_DIRECT;
|
2019-07-15 23:50:59 +08:00
|
|
|
struct blk_plug plug;
|
|
|
|
struct iomap_dio *dio;
|
|
|
|
|
|
|
|
if (!count)
|
2020-09-28 23:51:08 +08:00
|
|
|
return NULL;
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
dio = kmalloc(sizeof(*dio), GFP_KERNEL);
|
|
|
|
if (!dio)
|
2020-09-28 23:51:08 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
dio->iocb = iocb;
|
|
|
|
atomic_set(&dio->ref, 1);
|
|
|
|
dio->size = 0;
|
|
|
|
dio->i_size = i_size_read(inode);
|
2019-09-20 06:32:45 +08:00
|
|
|
dio->dops = dops;
|
2019-07-15 23:50:59 +08:00
|
|
|
dio->error = 0;
|
|
|
|
dio->flags = 0;
|
|
|
|
|
|
|
|
dio->submit.iter = iter;
|
|
|
|
dio->submit.waiter = current;
|
|
|
|
dio->submit.cookie = BLK_QC_T_NONE;
|
|
|
|
dio->submit.last_queue = NULL;
|
|
|
|
|
|
|
|
if (iov_iter_rw(iter) == READ) {
|
|
|
|
if (pos >= dio->i_size)
|
|
|
|
goto out_free_dio;
|
|
|
|
|
2021-04-30 13:55:24 +08:00
|
|
|
if (iocb->ki_flags & IOCB_NOWAIT) {
|
|
|
|
if (filemap_range_needs_writeback(mapping, pos, end)) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto out_free_dio;
|
|
|
|
}
|
|
|
|
iomap_flags |= IOMAP_NOWAIT;
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:51:24 +08:00
|
|
|
if (iter_is_iovec(iter))
|
2019-07-15 23:50:59 +08:00
|
|
|
dio->flags |= IOMAP_DIO_DIRTY;
|
|
|
|
} else {
|
2021-01-24 02:06:09 +08:00
|
|
|
iomap_flags |= IOMAP_WRITE;
|
2019-07-15 23:50:59 +08:00
|
|
|
dio->flags |= IOMAP_DIO_WRITE;
|
|
|
|
|
2021-04-30 13:55:24 +08:00
|
|
|
if (iocb->ki_flags & IOCB_NOWAIT) {
|
|
|
|
if (filemap_range_has_page(mapping, pos, end)) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto out_free_dio;
|
|
|
|
}
|
|
|
|
iomap_flags |= IOMAP_NOWAIT;
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:50:59 +08:00
|
|
|
/* for data sync or sync, we need sync completion processing */
|
|
|
|
if (iocb->ki_flags & IOCB_DSYNC)
|
|
|
|
dio->flags |= IOMAP_DIO_NEED_SYNC;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For datasync only writes, we optimistically try using FUA for
|
|
|
|
* this IO. Any non-FUA write that occurs will clear this flag,
|
|
|
|
* hence we know before completion whether a cache flush is
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
|
|
|
|
dio->flags |= IOMAP_DIO_WRITE_FUA;
|
|
|
|
}
|
|
|
|
|
2021-01-24 02:06:10 +08:00
|
|
|
if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
if (pos >= dio->i_size || pos + count > dio->i_size)
|
|
|
|
goto out_free_dio;
|
|
|
|
iomap_flags |= IOMAP_OVERWRITE_ONLY;
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:28:47 +08:00
|
|
|
ret = filemap_write_and_wait_range(mapping, pos, end);
|
2019-07-15 23:50:59 +08:00
|
|
|
if (ret)
|
|
|
|
goto out_free_dio;
|
|
|
|
|
iomap: Only invalidate page cache pages on direct IO writes
The historic requirement for XFS to invalidate cached pages on
direct IO reads has been lost in the twisty pages of history - it was
inherited from Irix, which implemented page cache invalidation on
read as a method of working around problems synchronising page
cache state with uncached IO.
XFS has carried this ever since. In the initial linux ports it was
necessary to get mmap and DIO to play "ok" together and not
immediately corrupt data. This was the state of play until the linux
kernel had infrastructure to track unwritten extents and synchronise
page faults with allocations and unwritten extent conversions
(->page_mkwrite infrastructure). IOws, the page cache invalidation
on DIO read was necessary to prevent trivial data corruptions. This
didn't solve all the problems, though.
There were peformance problems if we didn't invalidate the entire
page cache over the file on read - we couldn't easily determine if
the cached pages were over the range of the IO, and invalidation
required taking a serialising lock (i_mutex) on the inode. This
serialising lock was an issue for XFS, as it was the only exclusive
lock in the direct Io read path.
Hence if there were any cached pages, we'd just invalidate the
entire file in one go so that subsequent IOs didn't need to take the
serialising lock. This was a problem that prevented ranged
invalidation from being particularly useful for avoiding the
remaining coherency issues. This was solved with the conversion of
i_mutex to i_rwsem and the conversion of the XFS inode IO lock to
use i_rwsem. Hence we could now just do ranged invalidation and the
performance problem went away.
However, page cache invalidation was still needed to serialise
sub-page/sub-block zeroing via direct IO against buffered IO because
bufferhead state attached to the cached page could get out of whack
when direct IOs were issued. We've removed bufferheads from the
XFS code, and we don't carry any extent state on the cached pages
anymore, and so this problem has gone away, too.
IOWs, it would appear that we don't have any good reason to be
invalidating the page cache on DIO reads anymore. Hence remove the
invalidation on read because it is unnecessary overhead,
not needed to maintain coherency between mmap/buffered access and
direct IO anymore, and prevents anyone from using direct IO reads
from intentionally invalidating the page cache of a file.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2020-07-24 13:45:58 +08:00
|
|
|
if (iov_iter_rw(iter) == WRITE) {
|
|
|
|
/*
|
|
|
|
* Try to invalidate cache pages for the range we are writing.
|
2020-07-24 13:45:59 +08:00
|
|
|
* If this invalidation fails, let the caller fall back to
|
|
|
|
* buffered I/O.
|
iomap: Only invalidate page cache pages on direct IO writes
The historic requirement for XFS to invalidate cached pages on
direct IO reads has been lost in the twisty pages of history - it was
inherited from Irix, which implemented page cache invalidation on
read as a method of working around problems synchronising page
cache state with uncached IO.
XFS has carried this ever since. In the initial linux ports it was
necessary to get mmap and DIO to play "ok" together and not
immediately corrupt data. This was the state of play until the linux
kernel had infrastructure to track unwritten extents and synchronise
page faults with allocations and unwritten extent conversions
(->page_mkwrite infrastructure). IOws, the page cache invalidation
on DIO read was necessary to prevent trivial data corruptions. This
didn't solve all the problems, though.
There were peformance problems if we didn't invalidate the entire
page cache over the file on read - we couldn't easily determine if
the cached pages were over the range of the IO, and invalidation
required taking a serialising lock (i_mutex) on the inode. This
serialising lock was an issue for XFS, as it was the only exclusive
lock in the direct Io read path.
Hence if there were any cached pages, we'd just invalidate the
entire file in one go so that subsequent IOs didn't need to take the
serialising lock. This was a problem that prevented ranged
invalidation from being particularly useful for avoiding the
remaining coherency issues. This was solved with the conversion of
i_mutex to i_rwsem and the conversion of the XFS inode IO lock to
use i_rwsem. Hence we could now just do ranged invalidation and the
performance problem went away.
However, page cache invalidation was still needed to serialise
sub-page/sub-block zeroing via direct IO against buffered IO because
bufferhead state attached to the cached page could get out of whack
when direct IOs were issued. We've removed bufferheads from the
XFS code, and we don't carry any extent state on the cached pages
anymore, and so this problem has gone away, too.
IOWs, it would appear that we don't have any good reason to be
invalidating the page cache on DIO reads anymore. Hence remove the
invalidation on read because it is unnecessary overhead,
not needed to maintain coherency between mmap/buffered access and
direct IO anymore, and prevents anyone from using direct IO reads
from intentionally invalidating the page cache of a file.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2020-07-24 13:45:58 +08:00
|
|
|
*/
|
|
|
|
if (invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
|
2020-07-24 13:45:59 +08:00
|
|
|
end >> PAGE_SHIFT)) {
|
|
|
|
trace_iomap_dio_invalidate_fail(inode, pos, count);
|
|
|
|
ret = -ENOTBLK;
|
|
|
|
goto out_free_dio;
|
|
|
|
}
|
2019-07-15 23:50:59 +08:00
|
|
|
|
iomap: Only invalidate page cache pages on direct IO writes
The historic requirement for XFS to invalidate cached pages on
direct IO reads has been lost in the twisty pages of history - it was
inherited from Irix, which implemented page cache invalidation on
read as a method of working around problems synchronising page
cache state with uncached IO.
XFS has carried this ever since. In the initial linux ports it was
necessary to get mmap and DIO to play "ok" together and not
immediately corrupt data. This was the state of play until the linux
kernel had infrastructure to track unwritten extents and synchronise
page faults with allocations and unwritten extent conversions
(->page_mkwrite infrastructure). IOws, the page cache invalidation
on DIO read was necessary to prevent trivial data corruptions. This
didn't solve all the problems, though.
There were peformance problems if we didn't invalidate the entire
page cache over the file on read - we couldn't easily determine if
the cached pages were over the range of the IO, and invalidation
required taking a serialising lock (i_mutex) on the inode. This
serialising lock was an issue for XFS, as it was the only exclusive
lock in the direct Io read path.
Hence if there were any cached pages, we'd just invalidate the
entire file in one go so that subsequent IOs didn't need to take the
serialising lock. This was a problem that prevented ranged
invalidation from being particularly useful for avoiding the
remaining coherency issues. This was solved with the conversion of
i_mutex to i_rwsem and the conversion of the XFS inode IO lock to
use i_rwsem. Hence we could now just do ranged invalidation and the
performance problem went away.
However, page cache invalidation was still needed to serialise
sub-page/sub-block zeroing via direct IO against buffered IO because
bufferhead state attached to the cached page could get out of whack
when direct IOs were issued. We've removed bufferheads from the
XFS code, and we don't carry any extent state on the cached pages
anymore, and so this problem has gone away, too.
IOWs, it would appear that we don't have any good reason to be
invalidating the page cache on DIO reads anymore. Hence remove the
invalidation on read because it is unnecessary overhead,
not needed to maintain coherency between mmap/buffered access and
direct IO anymore, and prevents anyone from using direct IO reads
from intentionally invalidating the page cache of a file.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2020-07-24 13:45:58 +08:00
|
|
|
if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
|
|
|
|
ret = sb_init_dio_done_wq(inode->i_sb);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_free_dio;
|
|
|
|
}
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inode_dio_begin(inode);
|
|
|
|
|
|
|
|
blk_start_plug(&plug);
|
|
|
|
do {
|
2021-01-24 02:06:09 +08:00
|
|
|
ret = iomap_apply(inode, pos, count, iomap_flags, ops, dio,
|
2019-07-15 23:50:59 +08:00
|
|
|
iomap_dio_actor);
|
|
|
|
if (ret <= 0) {
|
|
|
|
/* magic error code to fall back to buffered I/O */
|
|
|
|
if (ret == -ENOTBLK) {
|
|
|
|
wait_for_completion = true;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos += ret;
|
|
|
|
|
2019-11-22 08:14:38 +08:00
|
|
|
if (iov_iter_rw(iter) == READ && pos >= dio->i_size) {
|
|
|
|
/*
|
|
|
|
* We only report that we've read data up to i_size.
|
|
|
|
* Revert iter to a state corresponding to that as
|
|
|
|
* some callers (such as splice code) rely on it.
|
|
|
|
*/
|
|
|
|
iov_iter_revert(iter, pos - dio->i_size);
|
2019-07-15 23:50:59 +08:00
|
|
|
break;
|
2019-11-22 08:14:38 +08:00
|
|
|
}
|
2019-07-15 23:50:59 +08:00
|
|
|
} while ((count = iov_iter_count(iter)) > 0);
|
|
|
|
blk_finish_plug(&plug);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
iomap_dio_set_error(dio, ret);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If all the writes we issued were FUA, we don't need to flush the
|
|
|
|
* cache on IO completion. Clear the sync flag for this case.
|
|
|
|
*/
|
|
|
|
if (dio->flags & IOMAP_DIO_WRITE_FUA)
|
|
|
|
dio->flags &= ~IOMAP_DIO_NEED_SYNC;
|
|
|
|
|
|
|
|
WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie);
|
|
|
|
WRITE_ONCE(iocb->private, dio->submit.last_queue);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are about to drop our additional submission reference, which
|
2020-03-18 23:04:36 +08:00
|
|
|
* might be the last reference to the dio. There are three different
|
|
|
|
* ways we can progress here:
|
2019-07-15 23:50:59 +08:00
|
|
|
*
|
|
|
|
* (a) If this is the last reference we will always complete and free
|
|
|
|
* the dio ourselves.
|
|
|
|
* (b) If this is not the last reference, and we serve an asynchronous
|
|
|
|
* iocb, we must never touch the dio after the decrement, the
|
|
|
|
* I/O completion handler will complete and free it.
|
|
|
|
* (c) If this is not the last reference, but we serve a synchronous
|
|
|
|
* iocb, the I/O completion handler will wake us up on the drop
|
|
|
|
* of the final reference, and we will complete and free it here
|
|
|
|
* after we got woken by the I/O completion handler.
|
|
|
|
*/
|
|
|
|
dio->wait_for_completion = wait_for_completion;
|
|
|
|
if (!atomic_dec_and_test(&dio->ref)) {
|
|
|
|
if (!wait_for_completion)
|
2020-09-28 23:51:08 +08:00
|
|
|
return ERR_PTR(-EIOCBQUEUED);
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
|
|
|
if (!READ_ONCE(dio->submit.waiter))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!(iocb->ki_flags & IOCB_HIPRI) ||
|
|
|
|
!dio->submit.last_queue ||
|
|
|
|
!blk_poll(dio->submit.last_queue,
|
|
|
|
dio->submit.cookie, true))
|
2020-05-03 09:54:22 +08:00
|
|
|
blk_io_schedule();
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
}
|
|
|
|
|
2020-09-28 23:51:08 +08:00
|
|
|
return dio;
|
2019-07-15 23:50:59 +08:00
|
|
|
|
|
|
|
out_free_dio:
|
|
|
|
kfree(dio);
|
2020-09-28 23:51:08 +08:00
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__iomap_dio_rw);
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
|
|
|
|
const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
|
2021-01-24 02:06:09 +08:00
|
|
|
unsigned int dio_flags)
|
2020-09-28 23:51:08 +08:00
|
|
|
{
|
|
|
|
struct iomap_dio *dio;
|
|
|
|
|
2021-01-24 02:06:09 +08:00
|
|
|
dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags);
|
2020-09-28 23:51:08 +08:00
|
|
|
if (IS_ERR_OR_NULL(dio))
|
|
|
|
return PTR_ERR_OR_ZERO(dio);
|
|
|
|
return iomap_dio_complete(dio);
|
2019-07-15 23:50:59 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_dio_rw);
|