2016-06-21 06:23:11 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
2018-06-20 05:10:57 +07:00
|
|
|
* Copyright (c) 2016-2018 Christoph Hellwig.
|
2016-06-21 06:23:11 +07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/compiler.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/iomap.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/gfp.h>
|
2018-07-12 12:26:05 +07:00
|
|
|
#include <linux/migrate.h>
|
2016-06-21 06:23:11 +07:00
|
|
|
#include <linux/mm.h>
|
2018-06-20 05:10:57 +07:00
|
|
|
#include <linux/mm_inline.h>
|
2016-06-21 06:23:11 +07:00
|
|
|
#include <linux/swap.h>
|
|
|
|
#include <linux/pagemap.h>
|
2018-06-01 23:04:40 +07:00
|
|
|
#include <linux/pagevec.h>
|
2016-06-21 06:23:11 +07:00
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/uio.h>
|
|
|
|
#include <linux/backing-dev.h>
|
|
|
|
#include <linux/buffer_head.h>
|
2016-11-30 10:36:01 +07:00
|
|
|
#include <linux/task_io_accounting_ops.h>
|
2016-06-21 06:31:39 +07:00
|
|
|
#include <linux/dax.h>
|
2017-02-04 05:47:37 +07:00
|
|
|
#include <linux/sched/signal.h>
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a iomap write on a segment of the mapping that spans a
|
|
|
|
* contiguous range of pages that have identical block mapping state.
|
|
|
|
*
|
|
|
|
* This avoids the need to map pages individually, do individual allocations
|
|
|
|
* for each page and most importantly avoid the need for filesystem specific
|
|
|
|
* locking per page. Instead, all the operations are amortised over the entire
|
|
|
|
* range of pages. It is assumed that the filesystems will lock whatever
|
|
|
|
* resources they require in the iomap_begin call, and release them in the
|
|
|
|
* iomap_end call.
|
|
|
|
*/
|
2016-09-19 08:24:49 +07:00
|
|
|
loff_t
|
2016-06-21 06:23:11 +07:00
|
|
|
iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,
|
2017-01-28 14:20:26 +07:00
|
|
|
const struct iomap_ops *ops, void *data, iomap_actor_t actor)
|
2016-06-21 06:23:11 +07:00
|
|
|
{
|
|
|
|
struct iomap iomap = { 0 };
|
|
|
|
loff_t written = 0, ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to map a range from start position for length bytes. This can
|
|
|
|
* span multiple pages - it is only guaranteed to return a range of a
|
|
|
|
* single type of pages (e.g. all into a hole, all mapped or all
|
|
|
|
* unwritten). Failure at this point has nothing to undo.
|
|
|
|
*
|
|
|
|
* If allocation is required for this range, reserve the space now so
|
|
|
|
* that the allocation is guaranteed to succeed later on. Once we copy
|
|
|
|
* the data into the page cache pages, then we cannot fail otherwise we
|
|
|
|
* expose transient stale data. If the reserve fails, we can safely
|
|
|
|
* back out at this point as there is nothing to undo.
|
|
|
|
*/
|
|
|
|
ret = ops->iomap_begin(inode, pos, length, flags, &iomap);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (WARN_ON(iomap.offset > pos))
|
|
|
|
return -EIO;
|
2018-01-27 02:11:20 +07:00
|
|
|
if (WARN_ON(iomap.length == 0))
|
|
|
|
return -EIO;
|
2016-06-21 06:23:11 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cut down the length to the one actually provided by the filesystem,
|
|
|
|
* as it might not be able to give us the whole size that we requested.
|
|
|
|
*/
|
|
|
|
if (iomap.offset + iomap.length < pos + length)
|
|
|
|
length = iomap.offset + iomap.length - pos;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we have guaranteed that the space allocation will succeed.
|
|
|
|
* we can do the copy-in page by page without having to worry about
|
|
|
|
* failures exposing transient data.
|
|
|
|
*/
|
|
|
|
written = actor(inode, pos, length, data, &iomap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now the data has been copied, commit the range we've copied. This
|
|
|
|
* should not fail unless the filesystem has had a fatal error.
|
|
|
|
*/
|
2016-08-17 05:42:34 +07:00
|
|
|
if (ops->iomap_end) {
|
|
|
|
ret = ops->iomap_end(inode, pos, length,
|
|
|
|
written > 0 ? written : 0,
|
|
|
|
flags, &iomap);
|
|
|
|
}
|
2016-06-21 06:23:11 +07:00
|
|
|
|
|
|
|
return written ? written : ret;
|
|
|
|
}
|
|
|
|
|
2018-06-01 23:03:08 +07:00
|
|
|
static sector_t
|
|
|
|
iomap_sector(struct iomap *iomap, loff_t pos)
|
|
|
|
{
|
|
|
|
return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:26:05 +07:00
|
|
|
static struct iomap_page *
|
|
|
|
iomap_page_create(struct inode *inode, struct page *page)
|
|
|
|
{
|
|
|
|
struct iomap_page *iop = to_iomap_page(page);
|
|
|
|
|
|
|
|
if (iop || i_blocksize(inode) == PAGE_SIZE)
|
|
|
|
return iop;
|
|
|
|
|
|
|
|
iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL);
|
|
|
|
atomic_set(&iop->read_count, 0);
|
|
|
|
atomic_set(&iop->write_count, 0);
|
|
|
|
bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE);
|
|
|
|
set_page_private(page, (unsigned long)iop);
|
|
|
|
SetPagePrivate(page);
|
|
|
|
return iop;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iomap_page_release(struct page *page)
|
|
|
|
{
|
|
|
|
struct iomap_page *iop = to_iomap_page(page);
|
|
|
|
|
|
|
|
if (!iop)
|
|
|
|
return;
|
|
|
|
WARN_ON_ONCE(atomic_read(&iop->read_count));
|
|
|
|
WARN_ON_ONCE(atomic_read(&iop->write_count));
|
|
|
|
ClearPagePrivate(page);
|
|
|
|
set_page_private(page, 0);
|
|
|
|
kfree(iop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the range inside the page that we actually need to read.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
|
|
|
|
loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
|
|
|
|
{
|
|
|
|
unsigned block_bits = inode->i_blkbits;
|
|
|
|
unsigned block_size = (1 << block_bits);
|
2018-08-11 01:46:14 +07:00
|
|
|
unsigned poff = offset_in_page(*pos);
|
2018-07-12 12:26:05 +07:00
|
|
|
unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
|
|
|
|
unsigned first = poff >> block_bits;
|
|
|
|
unsigned last = (poff + plen - 1) >> block_bits;
|
2018-08-11 01:46:14 +07:00
|
|
|
unsigned end = offset_in_page(i_size_read(inode)) >> block_bits;
|
2018-07-12 12:26:05 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the block size is smaller than the page size we need to check the
|
|
|
|
* per-block uptodate status and adjust the offset and length if needed
|
|
|
|
* to avoid reading in already uptodate ranges.
|
|
|
|
*/
|
|
|
|
if (iop) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* move forward for each leading block marked uptodate */
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
if (!test_bit(i, iop->uptodate))
|
|
|
|
break;
|
|
|
|
*pos += block_size;
|
|
|
|
poff += block_size;
|
|
|
|
plen -= block_size;
|
|
|
|
first++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* truncate len if we find any trailing uptodate block(s) */
|
|
|
|
for ( ; i <= last; i++) {
|
|
|
|
if (test_bit(i, iop->uptodate)) {
|
|
|
|
plen -= (last - i + 1) * block_size;
|
|
|
|
last = i - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the extent spans the block that contains the i_size we need to
|
|
|
|
* handle both halves separately so that we properly zero data in the
|
|
|
|
* page cache for blocks that are entirely outside of i_size.
|
|
|
|
*/
|
|
|
|
if (first <= end && last > end)
|
|
|
|
plen -= (last - end) * block_size;
|
|
|
|
|
|
|
|
*offp = poff;
|
|
|
|
*lenp = plen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
|
|
|
|
{
|
|
|
|
struct iomap_page *iop = to_iomap_page(page);
|
|
|
|
struct inode *inode = page->mapping->host;
|
|
|
|
unsigned first = off >> inode->i_blkbits;
|
|
|
|
unsigned last = (off + len - 1) >> inode->i_blkbits;
|
|
|
|
unsigned int i;
|
|
|
|
bool uptodate = true;
|
|
|
|
|
|
|
|
if (iop) {
|
|
|
|
for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
|
|
|
|
if (i >= first && i <= last)
|
|
|
|
set_bit(i, iop->uptodate);
|
|
|
|
else if (!test_bit(i, iop->uptodate))
|
|
|
|
uptodate = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uptodate && !PageError(page))
|
|
|
|
SetPageUptodate(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iomap_read_finish(struct iomap_page *iop, struct page *page)
|
|
|
|
{
|
|
|
|
if (!iop || atomic_dec_and_test(&iop->read_count))
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iomap_read_page_end_io(struct bio_vec *bvec, int error)
|
|
|
|
{
|
|
|
|
struct page *page = bvec->bv_page;
|
|
|
|
struct iomap_page *iop = to_iomap_page(page);
|
|
|
|
|
|
|
|
if (unlikely(error)) {
|
|
|
|
ClearPageUptodate(page);
|
|
|
|
SetPageError(page);
|
|
|
|
} else {
|
|
|
|
iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
iomap_read_finish(iop, page);
|
|
|
|
}
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
static void
|
|
|
|
iomap_read_inline_data(struct inode *inode, struct page *page,
|
|
|
|
struct iomap *iomap)
|
|
|
|
{
|
|
|
|
size_t size = i_size_read(inode);
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
if (PageUptodate(page))
|
|
|
|
return;
|
|
|
|
|
|
|
|
BUG_ON(page->index);
|
|
|
|
BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
|
|
|
|
|
|
|
|
addr = kmap_atomic(page);
|
|
|
|
memcpy(addr, iomap->inline_data, size);
|
|
|
|
memset(addr + size, 0, PAGE_SIZE - size);
|
|
|
|
kunmap_atomic(addr);
|
|
|
|
SetPageUptodate(page);
|
|
|
|
}
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
static void
|
2018-06-20 05:10:57 +07:00
|
|
|
iomap_read_end_io(struct bio *bio)
|
|
|
|
{
|
|
|
|
int error = blk_status_to_errno(bio->bi_status);
|
|
|
|
struct bio_vec *bvec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bio_for_each_segment_all(bvec, bio, i)
|
2018-07-12 12:26:05 +07:00
|
|
|
iomap_read_page_end_io(bvec, error);
|
2018-06-20 05:10:57 +07:00
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iomap_readpage_ctx {
|
|
|
|
struct page *cur_page;
|
|
|
|
bool cur_page_in_bio;
|
|
|
|
bool is_readahead;
|
|
|
|
struct bio *bio;
|
|
|
|
struct list_head *pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
|
|
|
|
struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct iomap_readpage_ctx *ctx = data;
|
|
|
|
struct page *page = ctx->cur_page;
|
2018-07-12 12:26:05 +07:00
|
|
|
struct iomap_page *iop = iomap_page_create(inode, page);
|
2018-06-20 05:10:57 +07:00
|
|
|
bool is_contig = false;
|
2018-07-12 12:26:05 +07:00
|
|
|
loff_t orig_pos = pos;
|
|
|
|
unsigned poff, plen;
|
2018-06-20 05:10:57 +07:00
|
|
|
sector_t sector;
|
|
|
|
|
2018-07-03 23:07:47 +07:00
|
|
|
if (iomap->type == IOMAP_INLINE) {
|
2018-08-11 07:55:57 +07:00
|
|
|
WARN_ON_ONCE(pos);
|
2018-07-03 23:07:47 +07:00
|
|
|
iomap_read_inline_data(inode, page, iomap);
|
|
|
|
return PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:26:05 +07:00
|
|
|
/* zero post-eof blocks as the page may be mapped */
|
|
|
|
iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
|
|
|
|
if (plen == 0)
|
|
|
|
goto done;
|
2018-06-20 05:10:57 +07:00
|
|
|
|
|
|
|
if (iomap->type != IOMAP_MAPPED || pos >= i_size_read(inode)) {
|
|
|
|
zero_user(page, poff, plen);
|
2018-07-12 12:26:05 +07:00
|
|
|
iomap_set_range_uptodate(page, poff, plen);
|
2018-06-20 05:10:57 +07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->cur_page_in_bio = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to merge into a previous segment if we can.
|
|
|
|
*/
|
|
|
|
sector = iomap_sector(iomap, pos);
|
|
|
|
if (ctx->bio && bio_end_sector(ctx->bio) == sector) {
|
|
|
|
if (__bio_try_merge_page(ctx->bio, page, plen, poff))
|
|
|
|
goto done;
|
|
|
|
is_contig = true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:26:05 +07:00
|
|
|
/*
|
|
|
|
* If we start a new segment we need to increase the read count, and we
|
|
|
|
* need to do so before submitting any previous full bio to make sure
|
|
|
|
* that we don't prematurely unlock the page.
|
|
|
|
*/
|
|
|
|
if (iop)
|
|
|
|
atomic_inc(&iop->read_count);
|
|
|
|
|
2018-06-20 05:10:57 +07:00
|
|
|
if (!ctx->bio || !is_contig || bio_full(ctx->bio)) {
|
|
|
|
gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
|
|
|
|
int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
|
|
|
|
|
|
if (ctx->bio)
|
|
|
|
submit_bio(ctx->bio);
|
|
|
|
|
|
|
|
if (ctx->is_readahead) /* same as readahead_gfp_mask */
|
|
|
|
gfp |= __GFP_NORETRY | __GFP_NOWARN;
|
|
|
|
ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
|
|
|
|
ctx->bio->bi_opf = REQ_OP_READ;
|
|
|
|
if (ctx->is_readahead)
|
|
|
|
ctx->bio->bi_opf |= REQ_RAHEAD;
|
|
|
|
ctx->bio->bi_iter.bi_sector = sector;
|
|
|
|
bio_set_dev(ctx->bio, iomap->bdev);
|
|
|
|
ctx->bio->bi_end_io = iomap_read_end_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bio_add_page(ctx->bio, page, plen, poff);
|
|
|
|
done:
|
2018-07-12 12:26:05 +07:00
|
|
|
/*
|
|
|
|
* Move the caller beyond our range so that it keeps making progress.
|
|
|
|
* For that we have to include any leading non-uptodate ranges, but
|
|
|
|
* we can skip trailing ones as they will be handled in the next
|
|
|
|
* iteration.
|
|
|
|
*/
|
|
|
|
return pos - orig_pos + plen;
|
2018-06-20 05:10:57 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iomap_readpage(struct page *page, const struct iomap_ops *ops)
|
|
|
|
{
|
|
|
|
struct iomap_readpage_ctx ctx = { .cur_page = page };
|
|
|
|
struct inode *inode = page->mapping->host;
|
|
|
|
unsigned poff;
|
|
|
|
loff_t ret;
|
|
|
|
|
|
|
|
for (poff = 0; poff < PAGE_SIZE; poff += ret) {
|
|
|
|
ret = iomap_apply(inode, page_offset(page) + poff,
|
|
|
|
PAGE_SIZE - poff, 0, ops, &ctx,
|
|
|
|
iomap_readpage_actor);
|
|
|
|
if (ret <= 0) {
|
|
|
|
WARN_ON_ONCE(ret == 0);
|
|
|
|
SetPageError(page);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.bio) {
|
|
|
|
submit_bio(ctx.bio);
|
|
|
|
WARN_ON_ONCE(!ctx.cur_page_in_bio);
|
|
|
|
} else {
|
|
|
|
WARN_ON_ONCE(ctx.cur_page_in_bio);
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just like mpage_readpages and block_read_full_page we always
|
|
|
|
* return 0 and just mark the page as PageError on errors. This
|
|
|
|
* should be cleaned up all through the stack eventually.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_readpage);
|
|
|
|
|
|
|
|
static struct page *
|
|
|
|
iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
|
|
|
|
loff_t length, loff_t *done)
|
|
|
|
{
|
|
|
|
while (!list_empty(pages)) {
|
|
|
|
struct page *page = lru_to_page(pages);
|
|
|
|
|
|
|
|
if (page_offset(page) >= (u64)pos + length)
|
|
|
|
break;
|
|
|
|
|
|
|
|
list_del(&page->lru);
|
|
|
|
if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
|
|
|
|
GFP_NOFS))
|
|
|
|
return page;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we already have a page in the page cache at index we are
|
|
|
|
* done. Upper layers don't care if it is uptodate after the
|
|
|
|
* readpages call itself as every page gets checked again once
|
|
|
|
* actually needed.
|
|
|
|
*/
|
|
|
|
*done += PAGE_SIZE;
|
|
|
|
put_page(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
|
|
|
|
void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct iomap_readpage_ctx *ctx = data;
|
|
|
|
loff_t done, ret;
|
|
|
|
|
|
|
|
for (done = 0; done < length; done += ret) {
|
2018-08-11 01:46:14 +07:00
|
|
|
if (ctx->cur_page && offset_in_page(pos + done) == 0) {
|
2018-06-20 05:10:57 +07:00
|
|
|
if (!ctx->cur_page_in_bio)
|
|
|
|
unlock_page(ctx->cur_page);
|
|
|
|
put_page(ctx->cur_page);
|
|
|
|
ctx->cur_page = NULL;
|
|
|
|
}
|
|
|
|
if (!ctx->cur_page) {
|
|
|
|
ctx->cur_page = iomap_next_page(inode, ctx->pages,
|
|
|
|
pos, length, &done);
|
|
|
|
if (!ctx->cur_page)
|
|
|
|
break;
|
|
|
|
ctx->cur_page_in_bio = false;
|
|
|
|
}
|
|
|
|
ret = iomap_readpage_actor(inode, pos + done, length - done,
|
|
|
|
ctx, iomap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iomap_readpages(struct address_space *mapping, struct list_head *pages,
|
|
|
|
unsigned nr_pages, const struct iomap_ops *ops)
|
|
|
|
{
|
|
|
|
struct iomap_readpage_ctx ctx = {
|
|
|
|
.pages = pages,
|
|
|
|
.is_readahead = true,
|
|
|
|
};
|
|
|
|
loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
|
|
|
|
loff_t last = page_offset(list_entry(pages->next, struct page, lru));
|
|
|
|
loff_t length = last - pos + PAGE_SIZE, ret = 0;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
ret = iomap_apply(mapping->host, pos, length, 0, ops,
|
|
|
|
&ctx, iomap_readpages_actor);
|
|
|
|
if (ret <= 0) {
|
|
|
|
WARN_ON_ONCE(ret == 0);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
pos += ret;
|
|
|
|
length -= ret;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
done:
|
|
|
|
if (ctx.bio)
|
|
|
|
submit_bio(ctx.bio);
|
|
|
|
if (ctx.cur_page) {
|
|
|
|
if (!ctx.cur_page_in_bio)
|
|
|
|
unlock_page(ctx.cur_page);
|
|
|
|
put_page(ctx.cur_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we didn't lose a page due to the arcance calling
|
|
|
|
* conventions..
|
|
|
|
*/
|
|
|
|
WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_readpages);
|
|
|
|
|
2018-07-12 12:26:05 +07:00
|
|
|
int
|
|
|
|
iomap_is_partially_uptodate(struct page *page, unsigned long from,
|
|
|
|
unsigned long count)
|
|
|
|
{
|
|
|
|
struct iomap_page *iop = to_iomap_page(page);
|
|
|
|
struct inode *inode = page->mapping->host;
|
|
|
|
unsigned first = from >> inode->i_blkbits;
|
|
|
|
unsigned last = (from + count - 1) >> inode->i_blkbits;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (iop) {
|
|
|
|
for (i = first; i <= last; i++)
|
|
|
|
if (!test_bit(i, iop->uptodate))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
|
|
|
|
|
|
|
|
int
|
|
|
|
iomap_releasepage(struct page *page, gfp_t gfp_mask)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* mm accommodates an old ext3 case where clean pages might not have had
|
|
|
|
* the dirty bit cleared. Thus, it can send actual dirty pages to
|
|
|
|
* ->releasepage() via shrink_active_list(), skip those here.
|
|
|
|
*/
|
|
|
|
if (PageDirty(page) || PageWriteback(page))
|
|
|
|
return 0;
|
|
|
|
iomap_page_release(page);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_releasepage);
|
|
|
|
|
|
|
|
void
|
|
|
|
iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we are invalidating the entire page, clear the dirty state from it
|
|
|
|
* and release it to avoid unnecessary buildup of the LRU.
|
|
|
|
*/
|
|
|
|
if (offset == 0 && len == PAGE_SIZE) {
|
|
|
|
WARN_ON_ONCE(PageWriteback(page));
|
|
|
|
cancel_dirty_page(page);
|
|
|
|
iomap_page_release(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_invalidatepage);
|
|
|
|
|
|
|
|
#ifdef CONFIG_MIGRATION
|
|
|
|
int
|
|
|
|
iomap_migrate_page(struct address_space *mapping, struct page *newpage,
|
|
|
|
struct page *page, enum migrate_mode mode)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
|
|
|
|
if (ret != MIGRATEPAGE_SUCCESS)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (page_has_private(page)) {
|
|
|
|
ClearPagePrivate(page);
|
|
|
|
set_page_private(newpage, page_private(page));
|
|
|
|
set_page_private(page, 0);
|
|
|
|
SetPagePrivate(newpage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != MIGRATE_SYNC_NO_COPY)
|
|
|
|
migrate_page_copy(newpage, page);
|
|
|
|
else
|
|
|
|
migrate_page_states(newpage, page);
|
|
|
|
return MIGRATEPAGE_SUCCESS;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_migrate_page);
|
|
|
|
#endif /* CONFIG_MIGRATION */
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
static void
|
|
|
|
iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
|
|
|
|
{
|
|
|
|
loff_t i_size = i_size_read(inode);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only truncate newly allocated pages beyoned EOF, even if the
|
|
|
|
* write started inside the existing inode size.
|
|
|
|
*/
|
|
|
|
if (pos + len > i_size)
|
|
|
|
truncate_pagecache_range(inode, max(pos, i_size), pos + len);
|
|
|
|
}
|
|
|
|
|
2018-06-20 05:10:58 +07:00
|
|
|
static int
|
|
|
|
iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
|
|
|
|
unsigned poff, unsigned plen, unsigned from, unsigned to,
|
|
|
|
struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct bio_vec bvec;
|
|
|
|
struct bio bio;
|
|
|
|
|
|
|
|
if (iomap->type != IOMAP_MAPPED || block_start >= i_size_read(inode)) {
|
|
|
|
zero_user_segments(page, poff, from, to, poff + plen);
|
2018-07-12 12:26:05 +07:00
|
|
|
iomap_set_range_uptodate(page, poff, plen);
|
2018-06-20 05:10:58 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio_init(&bio, &bvec, 1);
|
|
|
|
bio.bi_opf = REQ_OP_READ;
|
|
|
|
bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
|
|
|
|
bio_set_dev(&bio, iomap->bdev);
|
|
|
|
__bio_add_page(&bio, page, plen, poff);
|
|
|
|
return submit_bio_wait(&bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
__iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
|
|
|
|
struct page *page, struct iomap *iomap)
|
|
|
|
{
|
2018-07-12 12:26:05 +07:00
|
|
|
struct iomap_page *iop = iomap_page_create(inode, page);
|
2018-06-20 05:10:58 +07:00
|
|
|
loff_t block_size = i_blocksize(inode);
|
|
|
|
loff_t block_start = pos & ~(block_size - 1);
|
|
|
|
loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
|
2018-08-11 01:46:14 +07:00
|
|
|
unsigned from = offset_in_page(pos), to = from + len, poff, plen;
|
2018-07-12 12:26:05 +07:00
|
|
|
int status = 0;
|
2018-06-20 05:10:58 +07:00
|
|
|
|
|
|
|
if (PageUptodate(page))
|
|
|
|
return 0;
|
2018-07-12 12:26:05 +07:00
|
|
|
|
|
|
|
do {
|
|
|
|
iomap_adjust_read_range(inode, iop, &block_start,
|
|
|
|
block_end - block_start, &poff, &plen);
|
|
|
|
if (plen == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((from > poff && from < poff + plen) ||
|
|
|
|
(to > poff && to < poff + plen)) {
|
|
|
|
status = iomap_read_page_sync(inode, block_start, page,
|
|
|
|
poff, plen, from, to, iomap);
|
|
|
|
if (status)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while ((block_start += plen) < block_end);
|
|
|
|
|
|
|
|
return status;
|
2018-06-20 05:10:58 +07:00
|
|
|
}
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
static int
|
|
|
|
iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
|
|
|
|
struct page **pagep, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
pgoff_t index = pos >> PAGE_SHIFT;
|
|
|
|
struct page *page;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
BUG_ON(pos + len > iomap->offset + iomap->length);
|
|
|
|
|
2017-02-04 04:13:26 +07:00
|
|
|
if (fatal_signal_pending(current))
|
|
|
|
return -EINTR;
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
if (iomap->type == IOMAP_INLINE)
|
|
|
|
iomap_read_inline_data(inode, page, iomap);
|
2018-06-20 05:10:58 +07:00
|
|
|
else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
|
2018-06-20 05:10:56 +07:00
|
|
|
status = __block_write_begin_int(page, pos, len, NULL, iomap);
|
2018-06-20 05:10:58 +07:00
|
|
|
else
|
|
|
|
status = __iomap_write_begin(inode, pos, len, page, iomap);
|
2016-06-21 06:23:11 +07:00
|
|
|
if (unlikely(status)) {
|
|
|
|
unlock_page(page);
|
|
|
|
put_page(page);
|
|
|
|
page = NULL;
|
|
|
|
|
|
|
|
iomap_write_failed(inode, pos, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pagep = page;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-06-20 05:10:58 +07:00
|
|
|
int
|
|
|
|
iomap_set_page_dirty(struct page *page)
|
|
|
|
{
|
|
|
|
struct address_space *mapping = page_mapping(page);
|
|
|
|
int newly_dirty;
|
|
|
|
|
|
|
|
if (unlikely(!mapping))
|
|
|
|
return !TestSetPageDirty(page);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock out page->mem_cgroup migration to keep PageDirty
|
|
|
|
* synchronized with per-memcg dirty page counters.
|
|
|
|
*/
|
|
|
|
lock_page_memcg(page);
|
|
|
|
newly_dirty = !TestSetPageDirty(page);
|
|
|
|
if (newly_dirty)
|
|
|
|
__set_page_dirty(page, mapping, 0);
|
|
|
|
unlock_page_memcg(page);
|
|
|
|
|
|
|
|
if (newly_dirty)
|
|
|
|
__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
|
|
|
|
return newly_dirty;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
|
|
|
|
|
|
|
|
static int
|
|
|
|
__iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
|
|
|
|
unsigned copied, struct page *page, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
flush_dcache_page(page);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The blocks that were entirely written will now be uptodate, so we
|
|
|
|
* don't have to worry about a readpage reading them and overwriting a
|
|
|
|
* partial write. However if we have encountered a short write and only
|
|
|
|
* partially written into a block, it will not be marked uptodate, so a
|
|
|
|
* readpage might come in and destroy our partial write.
|
|
|
|
*
|
|
|
|
* Do the simplest thing, and just treat any short write to a non
|
|
|
|
* uptodate page as a zero-length write, and force the caller to redo
|
|
|
|
* the whole thing.
|
|
|
|
*/
|
|
|
|
if (unlikely(copied < len && !PageUptodate(page))) {
|
|
|
|
copied = 0;
|
|
|
|
} else {
|
2018-08-11 01:46:14 +07:00
|
|
|
iomap_set_range_uptodate(page, offset_in_page(pos), len);
|
2018-06-20 05:10:58 +07:00
|
|
|
iomap_set_page_dirty(page);
|
|
|
|
}
|
|
|
|
return __generic_write_end(inode, pos, copied, page);
|
|
|
|
}
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
static int
|
|
|
|
iomap_write_end_inline(struct inode *inode, struct page *page,
|
|
|
|
struct iomap *iomap, loff_t pos, unsigned copied)
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
WARN_ON_ONCE(!PageUptodate(page));
|
|
|
|
BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
|
|
|
|
|
|
|
|
addr = kmap_atomic(page);
|
|
|
|
memcpy(iomap->inline_data + pos, addr + pos, copied);
|
|
|
|
kunmap_atomic(addr);
|
|
|
|
|
|
|
|
mark_inode_dirty(inode);
|
|
|
|
__generic_write_end(inode, pos, copied, page);
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
static int
|
|
|
|
iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
|
2018-06-20 05:10:56 +07:00
|
|
|
unsigned copied, struct page *page, struct iomap *iomap)
|
2016-06-21 06:23:11 +07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
if (iomap->type == IOMAP_INLINE) {
|
|
|
|
ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
|
2018-06-20 05:10:58 +07:00
|
|
|
} else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
|
2018-06-20 05:10:56 +07:00
|
|
|
ret = generic_write_end(NULL, inode->i_mapping, pos, len,
|
|
|
|
copied, page, NULL);
|
2018-06-20 05:10:58 +07:00
|
|
|
} else {
|
|
|
|
ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
|
2018-06-20 05:10:56 +07:00
|
|
|
}
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
if (iomap->page_done)
|
|
|
|
iomap->page_done(inode, pos, copied, page, iomap);
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
if (ret < len)
|
|
|
|
iomap_write_failed(inode, pos, len);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
|
|
|
|
struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct iov_iter *i = data;
|
|
|
|
long status = 0;
|
|
|
|
ssize_t written = 0;
|
|
|
|
unsigned int flags = AOP_FLAG_NOFS;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct page *page;
|
|
|
|
unsigned long offset; /* Offset into pagecache page */
|
|
|
|
unsigned long bytes; /* Bytes to write to page */
|
|
|
|
size_t copied; /* Bytes copied from user */
|
|
|
|
|
2018-08-11 01:46:14 +07:00
|
|
|
offset = offset_in_page(pos);
|
2016-06-21 06:23:11 +07:00
|
|
|
bytes = min_t(unsigned long, PAGE_SIZE - offset,
|
|
|
|
iov_iter_count(i));
|
|
|
|
again:
|
|
|
|
if (bytes > length)
|
|
|
|
bytes = length;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bring in the user page that we will copy from _first_.
|
|
|
|
* Otherwise there's a nasty deadlock on copying from the
|
|
|
|
* same page as we're writing to, without it being marked
|
|
|
|
* up-to-date.
|
|
|
|
*
|
|
|
|
* Not only is this an optimisation, but it is also required
|
|
|
|
* to check that the address is actually valid, when atomic
|
|
|
|
* usercopies are used, below.
|
|
|
|
*/
|
|
|
|
if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
|
|
|
|
status = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = iomap_write_begin(inode, pos, bytes, flags, &page,
|
|
|
|
iomap);
|
|
|
|
if (unlikely(status))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (mapping_writably_mapped(inode->i_mapping))
|
|
|
|
flush_dcache_page(page);
|
|
|
|
|
|
|
|
copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
|
|
|
|
|
|
|
|
flush_dcache_page(page);
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
status = iomap_write_end(inode, pos, bytes, copied, page,
|
|
|
|
iomap);
|
2016-06-21 06:23:11 +07:00
|
|
|
if (unlikely(status < 0))
|
|
|
|
break;
|
|
|
|
copied = status;
|
|
|
|
|
|
|
|
cond_resched();
|
|
|
|
|
|
|
|
iov_iter_advance(i, copied);
|
|
|
|
if (unlikely(copied == 0)) {
|
|
|
|
/*
|
|
|
|
* If we were unable to copy any data at all, we must
|
|
|
|
* fall back to a single segment length write.
|
|
|
|
*
|
|
|
|
* If we didn't fallback here, we could livelock
|
|
|
|
* because not all segments in the iov can be copied at
|
|
|
|
* once without a pagefault.
|
|
|
|
*/
|
|
|
|
bytes = min_t(unsigned long, PAGE_SIZE - offset,
|
|
|
|
iov_iter_single_seg_count(i));
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
pos += copied;
|
|
|
|
written += copied;
|
|
|
|
length -= copied;
|
|
|
|
|
|
|
|
balance_dirty_pages_ratelimited(inode->i_mapping);
|
|
|
|
} while (iov_iter_count(i) && length);
|
|
|
|
|
|
|
|
return written ? written : status;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
|
2017-01-28 14:20:26 +07:00
|
|
|
const struct iomap_ops *ops)
|
2016-06-21 06:23:11 +07:00
|
|
|
{
|
|
|
|
struct inode *inode = iocb->ki_filp->f_mapping->host;
|
|
|
|
loff_t pos = iocb->ki_pos, ret = 0, written = 0;
|
|
|
|
|
|
|
|
while (iov_iter_count(iter)) {
|
|
|
|
ret = iomap_apply(inode, pos, iov_iter_count(iter),
|
|
|
|
IOMAP_WRITE, ops, iter, iomap_write_actor);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
pos += ret;
|
|
|
|
written += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return written ? written : ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
|
|
|
|
|
2016-09-19 07:12:45 +07:00
|
|
|
static struct page *
|
|
|
|
__iomap_read_page(struct inode *inode, loff_t offset)
|
|
|
|
{
|
|
|
|
struct address_space *mapping = inode->i_mapping;
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
|
|
|
|
if (IS_ERR(page))
|
|
|
|
return page;
|
|
|
|
if (!PageUptodate(page)) {
|
|
|
|
put_page(page);
|
|
|
|
return ERR_PTR(-EIO);
|
|
|
|
}
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
|
|
|
|
struct iomap *iomap)
|
|
|
|
{
|
|
|
|
long status = 0;
|
|
|
|
ssize_t written = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct page *page, *rpage;
|
|
|
|
unsigned long offset; /* Offset into pagecache page */
|
|
|
|
unsigned long bytes; /* Bytes to write to page */
|
|
|
|
|
2018-08-11 01:46:14 +07:00
|
|
|
offset = offset_in_page(pos);
|
2017-08-12 02:45:35 +07:00
|
|
|
bytes = min_t(loff_t, PAGE_SIZE - offset, length);
|
2016-09-19 07:12:45 +07:00
|
|
|
|
|
|
|
rpage = __iomap_read_page(inode, pos);
|
|
|
|
if (IS_ERR(rpage))
|
|
|
|
return PTR_ERR(rpage);
|
|
|
|
|
|
|
|
status = iomap_write_begin(inode, pos, bytes,
|
2017-05-09 05:58:59 +07:00
|
|
|
AOP_FLAG_NOFS, &page, iomap);
|
2016-09-19 07:12:45 +07:00
|
|
|
put_page(rpage);
|
|
|
|
if (unlikely(status))
|
|
|
|
return status;
|
|
|
|
|
|
|
|
WARN_ON_ONCE(!PageUptodate(page));
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
|
2016-09-19 07:12:45 +07:00
|
|
|
if (unlikely(status <= 0)) {
|
|
|
|
if (WARN_ON_ONCE(status == 0))
|
|
|
|
return -EIO;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
cond_resched();
|
|
|
|
|
|
|
|
pos += status;
|
|
|
|
written += status;
|
|
|
|
length -= status;
|
|
|
|
|
|
|
|
balance_dirty_pages_ratelimited(inode->i_mapping);
|
|
|
|
} while (length);
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
|
2017-01-28 14:20:26 +07:00
|
|
|
const struct iomap_ops *ops)
|
2016-09-19 07:12:45 +07:00
|
|
|
{
|
|
|
|
loff_t ret;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
|
|
|
|
iomap_dirty_actor);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
pos += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_file_dirty);
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
|
|
|
|
unsigned bytes, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct page *page;
|
|
|
|
int status;
|
|
|
|
|
2017-05-09 05:58:59 +07:00
|
|
|
status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
|
|
|
|
iomap);
|
2016-06-21 06:23:11 +07:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
zero_user(page, offset, bytes);
|
|
|
|
mark_page_accessed(page);
|
|
|
|
|
2018-06-20 05:10:56 +07:00
|
|
|
return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
|
2016-06-21 06:23:11 +07:00
|
|
|
}
|
|
|
|
|
2016-06-21 06:31:39 +07:00
|
|
|
static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
|
|
|
|
struct iomap *iomap)
|
|
|
|
{
|
2018-06-01 23:03:08 +07:00
|
|
|
return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
|
|
|
|
iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
|
2016-06-21 06:31:39 +07:00
|
|
|
}
|
|
|
|
|
2016-06-21 06:23:11 +07:00
|
|
|
static loff_t
|
|
|
|
iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
|
|
|
|
void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
bool *did_zero = data;
|
|
|
|
loff_t written = 0;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* already zeroed? we're done. */
|
|
|
|
if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
|
|
|
|
return count;
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned offset, bytes;
|
|
|
|
|
2018-08-11 01:46:14 +07:00
|
|
|
offset = offset_in_page(pos);
|
2017-08-12 02:45:35 +07:00
|
|
|
bytes = min_t(loff_t, PAGE_SIZE - offset, count);
|
2016-06-21 06:23:11 +07:00
|
|
|
|
2016-06-21 06:31:39 +07:00
|
|
|
if (IS_DAX(inode))
|
|
|
|
status = iomap_dax_zero(pos, offset, bytes, iomap);
|
|
|
|
else
|
|
|
|
status = iomap_zero(inode, pos, offset, bytes, iomap);
|
2016-06-21 06:23:11 +07:00
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
pos += bytes;
|
|
|
|
count -= bytes;
|
|
|
|
written += bytes;
|
|
|
|
if (did_zero)
|
|
|
|
*did_zero = true;
|
|
|
|
} while (count > 0);
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
|
2017-01-28 14:20:26 +07:00
|
|
|
const struct iomap_ops *ops)
|
2016-06-21 06:23:11 +07:00
|
|
|
{
|
|
|
|
loff_t ret;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
|
|
|
|
ops, did_zero, iomap_zero_range_actor);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pos += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_zero_range);
|
|
|
|
|
|
|
|
int
|
|
|
|
iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
|
2017-01-28 14:20:26 +07:00
|
|
|
const struct iomap_ops *ops)
|
2016-06-21 06:23:11 +07:00
|
|
|
{
|
2017-02-28 05:28:32 +07:00
|
|
|
unsigned int blocksize = i_blocksize(inode);
|
|
|
|
unsigned int off = pos & (blocksize - 1);
|
2016-06-21 06:23:11 +07:00
|
|
|
|
|
|
|
/* Block boundary? Nothing to do */
|
|
|
|
if (!off)
|
|
|
|
return 0;
|
|
|
|
return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_truncate_page);
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
|
|
|
|
void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct page *page = data;
|
|
|
|
int ret;
|
|
|
|
|
2018-06-20 05:10:58 +07:00
|
|
|
if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
|
|
|
|
ret = __block_write_begin_int(page, pos, length, NULL, iomap);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
block_commit_write(page, 0, length);
|
|
|
|
} else {
|
|
|
|
WARN_ON_ONCE(!PageUptodate(page));
|
2018-07-12 12:26:05 +07:00
|
|
|
iomap_page_create(inode, page);
|
2018-09-29 10:51:01 +07:00
|
|
|
set_page_dirty(page);
|
2018-06-20 05:10:58 +07:00
|
|
|
}
|
2016-06-21 06:23:11 +07:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-10-27 05:02:59 +07:00
|
|
|
vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
|
2016-06-21 06:23:11 +07:00
|
|
|
{
|
|
|
|
struct page *page = vmf->page;
|
2017-02-25 05:56:41 +07:00
|
|
|
struct inode *inode = file_inode(vmf->vma->vm_file);
|
2016-06-21 06:23:11 +07:00
|
|
|
unsigned long length;
|
|
|
|
loff_t offset, size;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
lock_page(page);
|
|
|
|
size = i_size_read(inode);
|
|
|
|
if ((page->mapping != inode->i_mapping) ||
|
|
|
|
(page_offset(page) > size)) {
|
|
|
|
/* We overload EFAULT to mean page got truncated */
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* page is wholly or partially inside EOF */
|
|
|
|
if (((page->index + 1) << PAGE_SHIFT) > size)
|
2018-08-11 01:46:14 +07:00
|
|
|
length = offset_in_page(size);
|
2016-06-21 06:23:11 +07:00
|
|
|
else
|
|
|
|
length = PAGE_SIZE;
|
|
|
|
|
|
|
|
offset = page_offset(page);
|
|
|
|
while (length > 0) {
|
2016-11-10 06:26:50 +07:00
|
|
|
ret = iomap_apply(inode, offset, length,
|
|
|
|
IOMAP_WRITE | IOMAP_FAULT, ops, page,
|
|
|
|
iomap_page_mkwrite_actor);
|
2016-06-21 06:23:11 +07:00
|
|
|
if (unlikely(ret <= 0))
|
|
|
|
goto out_unlock;
|
|
|
|
offset += ret;
|
|
|
|
length -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_stable_page(page);
|
2017-08-30 00:08:41 +07:00
|
|
|
return VM_FAULT_LOCKED;
|
2016-06-21 06:23:11 +07:00
|
|
|
out_unlock:
|
|
|
|
unlock_page(page);
|
2017-08-30 00:08:41 +07:00
|
|
|
return block_page_mkwrite_return(ret);
|
2016-06-21 06:23:11 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
|
2016-06-21 06:38:45 +07:00
|
|
|
|
|
|
|
struct fiemap_ctx {
|
|
|
|
struct fiemap_extent_info *fi;
|
|
|
|
struct iomap prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int iomap_to_fiemap(struct fiemap_extent_info *fi,
|
|
|
|
struct iomap *iomap, u32 flags)
|
|
|
|
{
|
|
|
|
switch (iomap->type) {
|
|
|
|
case IOMAP_HOLE:
|
|
|
|
/* skip holes */
|
|
|
|
return 0;
|
|
|
|
case IOMAP_DELALLOC:
|
|
|
|
flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
|
|
|
|
break;
|
2018-06-01 23:03:06 +07:00
|
|
|
case IOMAP_MAPPED:
|
|
|
|
break;
|
2016-06-21 06:38:45 +07:00
|
|
|
case IOMAP_UNWRITTEN:
|
|
|
|
flags |= FIEMAP_EXTENT_UNWRITTEN;
|
|
|
|
break;
|
2018-06-01 23:03:06 +07:00
|
|
|
case IOMAP_INLINE:
|
|
|
|
flags |= FIEMAP_EXTENT_DATA_INLINE;
|
2016-06-21 06:38:45 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:33:58 +07:00
|
|
|
if (iomap->flags & IOMAP_F_MERGED)
|
|
|
|
flags |= FIEMAP_EXTENT_MERGED;
|
2016-09-19 07:13:02 +07:00
|
|
|
if (iomap->flags & IOMAP_F_SHARED)
|
|
|
|
flags |= FIEMAP_EXTENT_SHARED;
|
2016-08-29 08:33:58 +07:00
|
|
|
|
2016-06-21 06:38:45 +07:00
|
|
|
return fiemap_fill_next_extent(fi, iomap->offset,
|
2017-10-02 04:55:54 +07:00
|
|
|
iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
|
2016-08-29 08:33:58 +07:00
|
|
|
iomap->length, flags);
|
2016-06-21 06:38:45 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
|
|
|
|
struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct fiemap_ctx *ctx = data;
|
|
|
|
loff_t ret = length;
|
|
|
|
|
|
|
|
if (iomap->type == IOMAP_HOLE)
|
|
|
|
return length;
|
|
|
|
|
|
|
|
ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
|
|
|
|
ctx->prev = *iomap;
|
|
|
|
switch (ret) {
|
|
|
|
case 0: /* success */
|
|
|
|
return length;
|
|
|
|
case 1: /* extent array full */
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
|
2017-01-28 14:20:26 +07:00
|
|
|
loff_t start, loff_t len, const struct iomap_ops *ops)
|
2016-06-21 06:38:45 +07:00
|
|
|
{
|
|
|
|
struct fiemap_ctx ctx;
|
|
|
|
loff_t ret;
|
|
|
|
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
ctx.fi = fi;
|
|
|
|
ctx.prev.type = IOMAP_HOLE;
|
|
|
|
|
|
|
|
ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2016-08-17 05:41:10 +07:00
|
|
|
if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
|
|
|
|
ret = filemap_write_and_wait(inode->i_mapping);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-21 06:38:45 +07:00
|
|
|
|
|
|
|
while (len > 0) {
|
2016-10-20 11:51:28 +07:00
|
|
|
ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
|
2016-06-21 06:38:45 +07:00
|
|
|
iomap_fiemap_actor);
|
2016-08-17 05:41:34 +07:00
|
|
|
/* inode with no (attribute) mapping will give ENOENT */
|
|
|
|
if (ret == -ENOENT)
|
|
|
|
break;
|
2016-06-21 06:38:45 +07:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
start += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.prev.type != IOMAP_HOLE) {
|
|
|
|
ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_fiemap);
|
2016-11-30 10:36:01 +07:00
|
|
|
|
2018-06-01 23:04:40 +07:00
|
|
|
/*
|
|
|
|
* Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
|
2018-06-01 23:05:15 +07:00
|
|
|
* Returns true if found and updates @lastoff to the offset in file.
|
2018-06-01 23:04:40 +07:00
|
|
|
*/
|
2018-06-01 23:05:15 +07:00
|
|
|
static bool
|
|
|
|
page_seek_hole_data(struct inode *inode, struct page *page, loff_t *lastoff,
|
|
|
|
int whence)
|
2018-06-01 23:04:40 +07:00
|
|
|
{
|
2018-06-01 23:05:15 +07:00
|
|
|
const struct address_space_operations *ops = inode->i_mapping->a_ops;
|
|
|
|
unsigned int bsize = i_blocksize(inode), off;
|
2018-06-01 23:04:40 +07:00
|
|
|
bool seek_data = whence == SEEK_DATA;
|
2018-06-01 23:05:15 +07:00
|
|
|
loff_t poff = page_offset(page);
|
2018-06-01 23:04:40 +07:00
|
|
|
|
2018-06-01 23:05:15 +07:00
|
|
|
if (WARN_ON_ONCE(*lastoff >= poff + PAGE_SIZE))
|
|
|
|
return false;
|
2018-06-01 23:04:40 +07:00
|
|
|
|
2018-06-01 23:05:15 +07:00
|
|
|
if (*lastoff < poff) {
|
2018-06-01 23:04:40 +07:00
|
|
|
/*
|
2018-06-01 23:05:15 +07:00
|
|
|
* Last offset smaller than the start of the page means we found
|
|
|
|
* a hole:
|
2018-06-01 23:04:40 +07:00
|
|
|
*/
|
2018-06-01 23:05:15 +07:00
|
|
|
if (whence == SEEK_HOLE)
|
|
|
|
return true;
|
|
|
|
*lastoff = poff;
|
|
|
|
}
|
2018-06-01 23:04:40 +07:00
|
|
|
|
2018-06-01 23:05:15 +07:00
|
|
|
/*
|
|
|
|
* Just check the page unless we can and should check block ranges:
|
|
|
|
*/
|
|
|
|
if (bsize == PAGE_SIZE || !ops->is_partially_uptodate)
|
|
|
|
return PageUptodate(page) == seek_data;
|
|
|
|
|
|
|
|
lock_page(page);
|
|
|
|
if (unlikely(page->mapping != inode->i_mapping))
|
|
|
|
goto out_unlock_not_found;
|
|
|
|
|
|
|
|
for (off = 0; off < PAGE_SIZE; off += bsize) {
|
2018-08-11 01:46:14 +07:00
|
|
|
if (offset_in_page(*lastoff) >= off + bsize)
|
2018-06-01 23:05:15 +07:00
|
|
|
continue;
|
|
|
|
if (ops->is_partially_uptodate(page, off, bsize) == seek_data) {
|
|
|
|
unlock_page(page);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
*lastoff = poff + off + bsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_unlock_not_found:
|
|
|
|
unlock_page(page);
|
|
|
|
return false;
|
2018-06-01 23:04:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Seek for SEEK_DATA / SEEK_HOLE in the page cache.
|
|
|
|
*
|
|
|
|
* Within unwritten extents, the page cache determines which parts are holes
|
2018-06-01 23:05:14 +07:00
|
|
|
* and which are data: uptodate buffer heads count as data; everything else
|
|
|
|
* counts as a hole.
|
2018-06-01 23:04:40 +07:00
|
|
|
*
|
|
|
|
* Returns the resulting offset on successs, and -ENOENT otherwise.
|
|
|
|
*/
|
|
|
|
static loff_t
|
|
|
|
page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,
|
|
|
|
int whence)
|
|
|
|
{
|
|
|
|
pgoff_t index = offset >> PAGE_SHIFT;
|
|
|
|
pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE);
|
|
|
|
loff_t lastoff = offset;
|
|
|
|
struct pagevec pvec;
|
|
|
|
|
|
|
|
if (length <= 0)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
pagevec_init(&pvec);
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned nr_pages, i;
|
|
|
|
|
|
|
|
nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index,
|
|
|
|
end - 1);
|
|
|
|
if (nr_pages == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
struct page *page = pvec.pages[i];
|
|
|
|
|
2018-06-01 23:05:15 +07:00
|
|
|
if (page_seek_hole_data(inode, page, &lastoff, whence))
|
2018-06-01 23:04:40 +07:00
|
|
|
goto check_range;
|
|
|
|
lastoff = page_offset(page) + PAGE_SIZE;
|
|
|
|
}
|
|
|
|
pagevec_release(&pvec);
|
|
|
|
} while (index < end);
|
|
|
|
|
|
|
|
/* When no page at lastoff and we are not done, we found a hole. */
|
|
|
|
if (whence != SEEK_HOLE)
|
|
|
|
goto not_found;
|
|
|
|
|
|
|
|
check_range:
|
|
|
|
if (lastoff < offset + length)
|
|
|
|
goto out;
|
|
|
|
not_found:
|
|
|
|
lastoff = -ENOENT;
|
|
|
|
out:
|
|
|
|
pagevec_release(&pvec);
|
|
|
|
return lastoff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-30 01:43:21 +07:00
|
|
|
static loff_t
|
|
|
|
iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length,
|
|
|
|
void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
switch (iomap->type) {
|
|
|
|
case IOMAP_UNWRITTEN:
|
|
|
|
offset = page_cache_seek_hole_data(inode, offset, length,
|
|
|
|
SEEK_HOLE);
|
|
|
|
if (offset < 0)
|
|
|
|
return length;
|
|
|
|
/* fall through */
|
|
|
|
case IOMAP_HOLE:
|
|
|
|
*(loff_t *)data = offset;
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loff_t
|
|
|
|
iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
|
|
|
|
{
|
|
|
|
loff_t size = i_size_read(inode);
|
|
|
|
loff_t length = size - offset;
|
|
|
|
loff_t ret;
|
|
|
|
|
2017-07-13 00:26:47 +07:00
|
|
|
/* Nothing to be found before or beyond the end of the file. */
|
|
|
|
if (offset < 0 || offset >= size)
|
2017-06-30 01:43:21 +07:00
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
|
|
|
|
&offset, iomap_seek_hole_actor);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset += ret;
|
|
|
|
length -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_seek_hole);
|
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length,
|
|
|
|
void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
switch (iomap->type) {
|
|
|
|
case IOMAP_HOLE:
|
|
|
|
return length;
|
|
|
|
case IOMAP_UNWRITTEN:
|
|
|
|
offset = page_cache_seek_hole_data(inode, offset, length,
|
|
|
|
SEEK_DATA);
|
|
|
|
if (offset < 0)
|
|
|
|
return length;
|
|
|
|
/*FALLTHRU*/
|
|
|
|
default:
|
|
|
|
*(loff_t *)data = offset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loff_t
|
|
|
|
iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
|
|
|
|
{
|
|
|
|
loff_t size = i_size_read(inode);
|
|
|
|
loff_t length = size - offset;
|
|
|
|
loff_t ret;
|
|
|
|
|
2017-07-13 00:26:47 +07:00
|
|
|
/* Nothing to be found before or beyond the end of the file. */
|
|
|
|
if (offset < 0 || offset >= size)
|
2017-06-30 01:43:21 +07:00
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
|
|
|
|
&offset, iomap_seek_data_actor);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset += ret;
|
|
|
|
length -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length <= 0)
|
|
|
|
return -ENXIO;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_seek_data);
|
|
|
|
|
2016-11-30 10:36:01 +07:00
|
|
|
/*
|
|
|
|
* Private flags for iomap_dio, must not overlap with the public ones in
|
|
|
|
* iomap.h:
|
|
|
|
*/
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
#define IOMAP_DIO_WRITE_FUA (1 << 28)
|
2018-05-03 02:54:52 +07:00
|
|
|
#define IOMAP_DIO_NEED_SYNC (1 << 29)
|
2016-11-30 10:36:01 +07:00
|
|
|
#define IOMAP_DIO_WRITE (1 << 30)
|
|
|
|
#define IOMAP_DIO_DIRTY (1 << 31)
|
|
|
|
|
|
|
|
struct iomap_dio {
|
|
|
|
struct kiocb *iocb;
|
|
|
|
iomap_dio_end_io_t *end_io;
|
|
|
|
loff_t i_size;
|
|
|
|
loff_t size;
|
|
|
|
atomic_t ref;
|
|
|
|
unsigned flags;
|
|
|
|
int error;
|
2018-06-20 05:10:55 +07:00
|
|
|
bool wait_for_completion;
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t iomap_dio_complete(struct iomap_dio *dio)
|
|
|
|
{
|
|
|
|
struct kiocb *iocb = dio->iocb;
|
2017-09-21 21:16:29 +07:00
|
|
|
struct inode *inode = file_inode(iocb->ki_filp);
|
2017-10-13 23:47:46 +07:00
|
|
|
loff_t offset = iocb->ki_pos;
|
2016-11-30 10:36:01 +07:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (dio->end_io) {
|
|
|
|
ret = dio->end_io(iocb,
|
|
|
|
dio->error ? dio->error : dio->size,
|
|
|
|
dio->flags);
|
|
|
|
} else {
|
|
|
|
ret = dio->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (likely(!ret)) {
|
|
|
|
ret = dio->size;
|
|
|
|
/* check for short read */
|
2017-10-13 23:47:46 +07:00
|
|
|
if (offset + ret > dio->i_size &&
|
2016-11-30 10:36:01 +07:00
|
|
|
!(dio->flags & IOMAP_DIO_WRITE))
|
2017-10-13 23:47:46 +07:00
|
|
|
ret = dio->i_size - offset;
|
2016-11-30 10:36:01 +07:00
|
|
|
iocb->ki_pos += ret;
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:47:46 +07:00
|
|
|
/*
|
|
|
|
* 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...
|
|
|
|
*
|
|
|
|
* And this page cache invalidation has to be after dio->end_io(), as
|
|
|
|
* some filesystems convert unwritten extents to real allocations in
|
|
|
|
* end_io() when necessary, otherwise a racing buffer read would cache
|
|
|
|
* zeros from unwritten extents.
|
|
|
|
*/
|
|
|
|
if (!dio->error &&
|
|
|
|
(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);
|
2018-01-09 01:41:39 +07:00
|
|
|
if (err)
|
|
|
|
dio_warn_stale_pagecache(iocb->ki_filp);
|
2017-10-13 23:47:46 +07:00
|
|
|
}
|
|
|
|
|
2018-05-03 02:54:52 +07: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);
|
|
|
|
|
2016-11-30 10:36:01 +07:00
|
|
|
inode_dio_end(file_inode(iocb->ki_filp));
|
|
|
|
kfree(dio);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-05-03 02:54:52 +07:00
|
|
|
iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
|
2016-11-30 10:36:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2017-06-03 14:38:06 +07:00
|
|
|
if (bio->bi_status)
|
|
|
|
iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
if (atomic_dec_and_test(&dio->ref)) {
|
2018-06-20 05:10:55 +07:00
|
|
|
if (dio->wait_for_completion) {
|
2016-11-30 10:36:01 +07:00
|
|
|
struct task_struct *waiter = dio->submit.waiter;
|
|
|
|
WRITE_ONCE(dio->submit.waiter, NULL);
|
2018-11-14 11:16:54 +07:00
|
|
|
blk_wake_io_task(waiter);
|
2016-11-30 10:36:01 +07:00
|
|
|
} 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 {
|
|
|
|
struct bio_vec *bvec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bio_for_each_segment_all(bvec, bio, i)
|
|
|
|
put_page(bvec->bv_page);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static blk_qc_t
|
|
|
|
iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
|
|
|
|
unsigned len)
|
|
|
|
{
|
|
|
|
struct page *page = ZERO_PAGE(0);
|
2018-08-29 23:36:56 +07:00
|
|
|
int flags = REQ_SYNC | REQ_IDLE;
|
2016-11-30 10:36:01 +07:00
|
|
|
struct bio *bio;
|
|
|
|
|
|
|
|
bio = bio_alloc(GFP_KERNEL, 1);
|
2017-08-24 00:10:32 +07:00
|
|
|
bio_set_dev(bio, iomap->bdev);
|
2018-06-01 23:03:08 +07:00
|
|
|
bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
|
2016-11-30 10:36:01 +07:00
|
|
|
bio->bi_private = dio;
|
|
|
|
bio->bi_end_io = iomap_dio_bio_end_io;
|
|
|
|
|
2018-08-29 23:36:56 +07:00
|
|
|
if (dio->iocb->ki_flags & IOCB_HIPRI)
|
|
|
|
flags |= REQ_HIPRI;
|
|
|
|
|
2016-11-30 10:36:01 +07:00
|
|
|
get_page(page);
|
2018-06-01 23:03:07 +07:00
|
|
|
__bio_add_page(bio, page, len, 0);
|
2018-08-29 23:36:56 +07:00
|
|
|
bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
atomic_inc(&dio->ref);
|
|
|
|
return submit_bio(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t
|
2018-07-03 23:07:46 +07:00
|
|
|
iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
|
|
|
|
struct iomap_dio *dio, struct iomap *iomap)
|
2016-11-30 10:36:01 +07:00
|
|
|
{
|
2017-02-28 05:28:32 +07:00
|
|
|
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);
|
2016-11-30 10:36:01 +07:00
|
|
|
struct iov_iter iter;
|
|
|
|
struct bio *bio;
|
|
|
|
bool need_zeroout = false;
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
bool use_fua = false;
|
2016-11-30 10:36:01 +07:00
|
|
|
int nr_pages, ret;
|
2017-09-12 03:17:09 +07:00
|
|
|
size_t copied = 0;
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
if ((pos | length | align) & ((1 << blkbits) - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-07-03 23:07:46 +07:00
|
|
|
if (iomap->type == IOMAP_UNWRITTEN) {
|
2016-11-30 10:36:01 +07:00
|
|
|
dio->flags |= IOMAP_DIO_UNWRITTEN;
|
|
|
|
need_zeroout = true;
|
2018-07-03 23:07:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iomap->flags & IOMAP_F_SHARED)
|
|
|
|
dio->flags |= IOMAP_DIO_COW;
|
|
|
|
|
|
|
|
if (iomap->flags & IOMAP_F_NEW) {
|
|
|
|
need_zeroout = true;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Use a FUA write if we need datasync semantics, this
|
|
|
|
* is a pure data IO that doesn't require any metadata
|
|
|
|
* updates 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;
|
2016-11-30 10:36:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Operate on a partial iter trimmed to the extent we were called for.
|
|
|
|
* We'll update the iter in the dio once we're done with this extent.
|
|
|
|
*/
|
|
|
|
iter = *dio->submit.iter;
|
|
|
|
iov_iter_truncate(&iter, length);
|
|
|
|
|
|
|
|
nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
|
|
|
|
if (nr_pages <= 0)
|
|
|
|
return nr_pages;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2017-09-12 03:17:09 +07:00
|
|
|
size_t n;
|
|
|
|
if (dio->error) {
|
|
|
|
iov_iter_revert(dio->submit.iter, copied);
|
2016-11-30 10:36:01 +07:00
|
|
|
return 0;
|
2017-09-12 03:17:09 +07:00
|
|
|
}
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
bio = bio_alloc(GFP_KERNEL, nr_pages);
|
2017-08-24 00:10:32 +07:00
|
|
|
bio_set_dev(bio, iomap->bdev);
|
2018-06-01 23:03:08 +07:00
|
|
|
bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
|
2017-06-28 00:01:22 +07:00
|
|
|
bio->bi_write_hint = dio->iocb->ki_hint;
|
2018-05-23 00:52:21 +07:00
|
|
|
bio->bi_ioprio = dio->iocb->ki_ioprio;
|
2016-11-30 10:36:01 +07:00
|
|
|
bio->bi_private = dio;
|
|
|
|
bio->bi_end_io = iomap_dio_bio_end_io;
|
|
|
|
|
|
|
|
ret = bio_iov_iter_get_pages(bio, &iter);
|
|
|
|
if (unlikely(ret)) {
|
|
|
|
bio_put(bio);
|
2017-09-12 03:17:09 +07:00
|
|
|
return copied ? copied : ret;
|
2016-11-30 10:36:01 +07:00
|
|
|
}
|
|
|
|
|
2017-09-12 03:17:09 +07:00
|
|
|
n = bio->bi_iter.bi_size;
|
2016-11-30 10:36:01 +07:00
|
|
|
if (dio->flags & IOMAP_DIO_WRITE) {
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
|
|
|
|
if (use_fua)
|
|
|
|
bio->bi_opf |= REQ_FUA;
|
|
|
|
else
|
|
|
|
dio->flags &= ~IOMAP_DIO_WRITE_FUA;
|
2017-09-12 03:17:09 +07:00
|
|
|
task_io_account_write(n);
|
2016-11-30 10:36:01 +07:00
|
|
|
} else {
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
bio->bi_opf = REQ_OP_READ;
|
2016-11-30 10:36:01 +07:00
|
|
|
if (dio->flags & IOMAP_DIO_DIRTY)
|
|
|
|
bio_set_pages_dirty(bio);
|
|
|
|
}
|
|
|
|
|
2018-08-29 23:36:56 +07:00
|
|
|
if (dio->iocb->ki_flags & IOCB_HIPRI)
|
|
|
|
bio->bi_opf |= REQ_HIPRI;
|
|
|
|
|
2017-09-12 03:17:09 +07:00
|
|
|
iov_iter_advance(dio->submit.iter, n);
|
|
|
|
|
|
|
|
dio->size += n;
|
|
|
|
pos += n;
|
|
|
|
copied += n;
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
|
|
|
|
|
|
|
|
atomic_inc(&dio->ref);
|
|
|
|
|
|
|
|
dio->submit.last_queue = bdev_get_queue(iomap->bdev);
|
|
|
|
dio->submit.cookie = submit_bio(bio);
|
|
|
|
} while (nr_pages);
|
|
|
|
|
|
|
|
if (need_zeroout) {
|
|
|
|
/* 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);
|
|
|
|
}
|
2017-09-12 03:17:09 +07:00
|
|
|
return copied;
|
2016-11-30 10:36:01 +07:00
|
|
|
}
|
|
|
|
|
2018-07-03 23:07:46 +07: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;
|
|
|
|
}
|
|
|
|
|
2018-07-03 23:07:47 +07:00
|
|
|
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;
|
|
|
|
size_t copied;
|
|
|
|
|
|
|
|
BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
|
|
|
|
|
|
|
|
if (dio->flags & IOMAP_DIO_WRITE) {
|
|
|
|
loff_t size = inode->i_size;
|
|
|
|
|
|
|
|
if (pos > size)
|
|
|
|
memset(iomap->inline_data + size, 0, pos - size);
|
|
|
|
copied = copy_from_iter(iomap->inline_data + pos, length, iter);
|
|
|
|
if (copied) {
|
|
|
|
if (pos + copied > size)
|
|
|
|
i_size_write(inode, pos + copied);
|
|
|
|
mark_inode_dirty(inode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
copied = copy_to_iter(iomap->inline_data + pos, length, iter);
|
|
|
|
}
|
|
|
|
dio->size += copied;
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
2018-07-03 23:07:46 +07:00
|
|
|
static loff_t
|
|
|
|
iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
|
|
|
|
void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
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);
|
2018-07-03 23:07:47 +07:00
|
|
|
case IOMAP_INLINE:
|
|
|
|
return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
|
2018-07-03 23:07:46 +07:00
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 02:54:52 +07:00
|
|
|
/*
|
|
|
|
* iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
* 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.
|
2018-05-03 02:54:52 +07:00
|
|
|
*/
|
2016-11-30 10:36:01 +07:00
|
|
|
ssize_t
|
2017-01-28 14:20:26 +07:00
|
|
|
iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
|
|
|
|
const struct iomap_ops *ops, iomap_dio_end_io_t end_io)
|
2016-11-30 10:36:01 +07: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);
|
iomap: invalidate page caches should be after iomap_dio_complete() in direct write
After XFS switching to iomap based DIO (commit acdda3aae146 ("xfs:
use iomap_dio_rw")), I started to notice dio29/dio30 tests failures
from LTP run on ppc64 hosts, and they can be reproduced on x86_64
hosts with 512B/1k block size XFS too.
dio29 diotest3 -b 65536 -n 100 -i 1000 -o 1024000
dio30 diotest6 -b 65536 -n 100 -i 1000 -o 1024000
The failure message is like:
bufcmp: offset 0: Expected: 0x62, got 0x0
diotest03 1 TPASS : Read with Direct IO, Write without
diotest03 2 TFAIL : diotest3.c:142: comparsion failed; child=98 offset=1425408
diotest03 3 TFAIL : diotest3.c:194: Write Direct-child 98 failed
Direct write wrote 0x62 but buffer read got zero. This is because,
when doing direct write to a hole or preallocated file, we
invalidate the page caches before converting the extent from
unwritten state to normal state, which is done by
iomap_dio_complete(), thus leave a window for other buffer reader to
cache the unwritten state extent.
Consider this case, with sub-page blocksize XFS, two processes are
direct writing to different blocksize-aligned regions (say 512B) of
the same preallocated file, and reading the region back via buffered
I/O to compare contents.
process A, region [0,512] process B, region [512,1024]
xfs_file_write_iter
xfs_file_aio_dio_write
iomap_dio_rw
iomap_apply
invalidate_inode_pages2_range
xfs_file_write_iter
xfs_file_aio_dio_write
iomap_dio_rw
iomap_apply
invalidate_inode_pages2_range
iomap_dio_complete
xfs_file_read_iter
xfs_file_buffered_aio_read
generic_file_read_iter
do_generic_file_read
<readahead fills pagecache with 0>
iomap_dio_complete
xfs_file_read_iter
<read gets 0 from pagecache>
Process A first invalidates page caches, at this point the
underlying extent is still in unwritten state (iomap_dio_complete
not called yet), and process B finishs direct write and populates
page caches via readahead, which caches zeros in page for region A,
then process A reads zeros from page cache, instead of the actual
data.
Fix it by invalidating page caches after converting unwritten extent
to make sure we read content from disk after extent state changed,
as what we did before switching to iomap based dio.
Also introduce a new 'start' variable to save the original write
offset (iomap_dio_complete() updates iocb->ki_pos), and a 'err'
variable for invalidating caches result, cause we can't reuse 'ret'
anymore.
Signed-off-by: Eryu Guan <eguan@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2017-03-03 06:02:06 +07:00
|
|
|
loff_t pos = iocb->ki_pos, start = pos;
|
|
|
|
loff_t end = iocb->ki_pos + count - 1, ret = 0;
|
2016-11-30 10:36:01 +07:00
|
|
|
unsigned int flags = IOMAP_DIRECT;
|
|
|
|
struct blk_plug plug;
|
|
|
|
struct iomap_dio *dio;
|
|
|
|
|
|
|
|
lockdep_assert_held(&inode->i_rwsem);
|
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dio = kmalloc(sizeof(*dio), GFP_KERNEL);
|
|
|
|
if (!dio)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
dio->iocb = iocb;
|
|
|
|
atomic_set(&dio->ref, 1);
|
|
|
|
dio->size = 0;
|
|
|
|
dio->i_size = i_size_read(inode);
|
|
|
|
dio->end_io = end_io;
|
|
|
|
dio->error = 0;
|
|
|
|
dio->flags = 0;
|
2018-06-20 05:10:55 +07:00
|
|
|
dio->wait_for_completion = is_sync_kiocb(iocb);
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
dio->submit.iter = iter;
|
2018-06-20 05:10:55 +07:00
|
|
|
dio->submit.waiter = current;
|
|
|
|
dio->submit.cookie = BLK_QC_T_NONE;
|
|
|
|
dio->submit.last_queue = NULL;
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
if (iov_iter_rw(iter) == READ) {
|
|
|
|
if (pos >= dio->i_size)
|
|
|
|
goto out_free_dio;
|
|
|
|
|
2018-10-22 19:07:28 +07:00
|
|
|
if (iter_is_iovec(iter) && iov_iter_rw(iter) == READ)
|
2016-11-30 10:36:01 +07:00
|
|
|
dio->flags |= IOMAP_DIO_DIRTY;
|
|
|
|
} else {
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
flags |= IOMAP_WRITE;
|
2016-11-30 10:36:01 +07:00
|
|
|
dio->flags |= IOMAP_DIO_WRITE;
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
|
|
|
|
/* for data sync or sync, we need sync completion processing */
|
2018-05-03 02:54:52 +07:00
|
|
|
if (iocb->ki_flags & IOCB_DSYNC)
|
|
|
|
dio->flags |= IOMAP_DIO_NEED_SYNC;
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2016-11-30 10:36:01 +07:00
|
|
|
}
|
|
|
|
|
2017-06-20 19:05:45 +07:00
|
|
|
if (iocb->ki_flags & IOCB_NOWAIT) {
|
|
|
|
if (filemap_range_has_page(mapping, start, end)) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto out_free_dio;
|
|
|
|
}
|
|
|
|
flags |= IOMAP_NOWAIT;
|
|
|
|
}
|
|
|
|
|
fs: fix data invalidation in the cleancache during direct IO
Patch series "Properly invalidate data in the cleancache", v2.
We've noticed that after direct IO write, buffered read sometimes gets
stale data which is coming from the cleancache. The reason for this is
that some direct write hooks call call invalidate_inode_pages2[_range]()
conditionally iff mapping->nrpages is not zero, so we may not invalidate
data in the cleancache.
Another odd thing is that we check only for ->nrpages and don't check
for ->nrexceptional, but invalidate_inode_pages2[_range] also
invalidates exceptional entries as well. So we invalidate exceptional
entries only if ->nrpages != 0? This doesn't feel right.
- Patch 1 fixes direct IO writes by removing ->nrpages check.
- Patch 2 fixes similar case in invalidate_bdev().
Note: I only fixed conditional cleancache_invalidate_inode() here.
Do we also need to add ->nrexceptional check in into invalidate_bdev()?
- Patches 3-4: some optimizations.
This patch (of 4):
Some direct IO write fs hooks call invalidate_inode_pages2[_range]()
conditionally iff mapping->nrpages is not zero. This can't be right,
because invalidate_inode_pages2[_range]() also invalidate data in the
cleancache via cleancache_invalidate_inode() call. So if page cache is
empty but there is some data in the cleancache, buffered read after
direct IO write would get stale data from the cleancache.
Also it doesn't feel right to check only for ->nrpages because
invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of
nrpages state.
Note: nfs,cifs,9p doesn't need similar fix because the never call
cleancache_get_page() (nor directly, nor via mpage_readpage[s]()), so
they are not affected by this bug.
Fixes: c515e1fd361c ("mm/fs: add hooks to support cleancache")
Link: http://lkml.kernel.org/r/20170424164135.22350-2-aryabinin@virtuozzo.com
Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Alexey Kuznetsov <kuznet@virtuozzo.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Nikolay Borisov <n.borisov.lkml@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-05-04 04:55:59 +07:00
|
|
|
ret = filemap_write_and_wait_range(mapping, start, end);
|
|
|
|
if (ret)
|
|
|
|
goto out_free_dio;
|
2016-11-30 10:36:01 +07:00
|
|
|
|
2018-01-09 01:41:39 +07:00
|
|
|
/*
|
|
|
|
* Try to invalidate cache pages for the range we're direct
|
|
|
|
* writing. If this invalidation fails, tough, the write will
|
|
|
|
* still work, but racing two incompatible write paths is a
|
|
|
|
* pretty crazy thing to do, so we don't support it 100%.
|
|
|
|
*/
|
fs: fix data invalidation in the cleancache during direct IO
Patch series "Properly invalidate data in the cleancache", v2.
We've noticed that after direct IO write, buffered read sometimes gets
stale data which is coming from the cleancache. The reason for this is
that some direct write hooks call call invalidate_inode_pages2[_range]()
conditionally iff mapping->nrpages is not zero, so we may not invalidate
data in the cleancache.
Another odd thing is that we check only for ->nrpages and don't check
for ->nrexceptional, but invalidate_inode_pages2[_range] also
invalidates exceptional entries as well. So we invalidate exceptional
entries only if ->nrpages != 0? This doesn't feel right.
- Patch 1 fixes direct IO writes by removing ->nrpages check.
- Patch 2 fixes similar case in invalidate_bdev().
Note: I only fixed conditional cleancache_invalidate_inode() here.
Do we also need to add ->nrexceptional check in into invalidate_bdev()?
- Patches 3-4: some optimizations.
This patch (of 4):
Some direct IO write fs hooks call invalidate_inode_pages2[_range]()
conditionally iff mapping->nrpages is not zero. This can't be right,
because invalidate_inode_pages2[_range]() also invalidate data in the
cleancache via cleancache_invalidate_inode() call. So if page cache is
empty but there is some data in the cleancache, buffered read after
direct IO write would get stale data from the cleancache.
Also it doesn't feel right to check only for ->nrpages because
invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of
nrpages state.
Note: nfs,cifs,9p doesn't need similar fix because the never call
cleancache_get_page() (nor directly, nor via mpage_readpage[s]()), so
they are not affected by this bug.
Fixes: c515e1fd361c ("mm/fs: add hooks to support cleancache")
Link: http://lkml.kernel.org/r/20170424164135.22350-2-aryabinin@virtuozzo.com
Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Alexey Kuznetsov <kuznet@virtuozzo.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Nikolay Borisov <n.borisov.lkml@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-05-04 04:55:59 +07:00
|
|
|
ret = invalidate_inode_pages2_range(mapping,
|
|
|
|
start >> PAGE_SHIFT, end >> PAGE_SHIFT);
|
2018-01-09 01:41:39 +07:00
|
|
|
if (ret)
|
|
|
|
dio_warn_stale_pagecache(iocb->ki_filp);
|
fs: fix data invalidation in the cleancache during direct IO
Patch series "Properly invalidate data in the cleancache", v2.
We've noticed that after direct IO write, buffered read sometimes gets
stale data which is coming from the cleancache. The reason for this is
that some direct write hooks call call invalidate_inode_pages2[_range]()
conditionally iff mapping->nrpages is not zero, so we may not invalidate
data in the cleancache.
Another odd thing is that we check only for ->nrpages and don't check
for ->nrexceptional, but invalidate_inode_pages2[_range] also
invalidates exceptional entries as well. So we invalidate exceptional
entries only if ->nrpages != 0? This doesn't feel right.
- Patch 1 fixes direct IO writes by removing ->nrpages check.
- Patch 2 fixes similar case in invalidate_bdev().
Note: I only fixed conditional cleancache_invalidate_inode() here.
Do we also need to add ->nrexceptional check in into invalidate_bdev()?
- Patches 3-4: some optimizations.
This patch (of 4):
Some direct IO write fs hooks call invalidate_inode_pages2[_range]()
conditionally iff mapping->nrpages is not zero. This can't be right,
because invalidate_inode_pages2[_range]() also invalidate data in the
cleancache via cleancache_invalidate_inode() call. So if page cache is
empty but there is some data in the cleancache, buffered read after
direct IO write would get stale data from the cleancache.
Also it doesn't feel right to check only for ->nrpages because
invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of
nrpages state.
Note: nfs,cifs,9p doesn't need similar fix because the never call
cleancache_get_page() (nor directly, nor via mpage_readpage[s]()), so
they are not affected by this bug.
Fixes: c515e1fd361c ("mm/fs: add hooks to support cleancache")
Link: http://lkml.kernel.org/r/20170424164135.22350-2-aryabinin@virtuozzo.com
Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Alexey Kuznetsov <kuznet@virtuozzo.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Nikolay Borisov <n.borisov.lkml@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-05-04 04:55:59 +07:00
|
|
|
ret = 0;
|
2016-11-30 10:36:01 +07:00
|
|
|
|
2018-06-20 05:10:55 +07:00
|
|
|
if (iov_iter_rw(iter) == WRITE && !dio->wait_for_completion &&
|
2017-09-23 01:47:33 +07:00
|
|
|
!inode->i_sb->s_dio_done_wq) {
|
|
|
|
ret = sb_init_dio_done_wq(inode->i_sb);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_free_dio;
|
|
|
|
}
|
|
|
|
|
2016-11-30 10:36:01 +07:00
|
|
|
inode_dio_begin(inode);
|
|
|
|
|
|
|
|
blk_start_plug(&plug);
|
|
|
|
do {
|
|
|
|
ret = iomap_apply(inode, pos, count, flags, ops, dio,
|
|
|
|
iomap_dio_actor);
|
|
|
|
if (ret <= 0) {
|
|
|
|
/* magic error code to fall back to buffered I/O */
|
2018-06-20 05:10:55 +07:00
|
|
|
if (ret == -ENOTBLK) {
|
|
|
|
dio->wait_for_completion = true;
|
2016-11-30 10:36:01 +07:00
|
|
|
ret = 0;
|
2018-06-20 05:10:55 +07:00
|
|
|
}
|
2016-11-30 10:36:01 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos += ret;
|
iomap_dio_rw: Prevent reading file data beyond iomap_dio->i_size
On a ppc64 machine executing overlayfs/019 with xfs as the lower and
upper filesystem causes the following call trace,
WARNING: CPU: 2 PID: 8034 at /root/repos/linux/fs/iomap.c:765 .iomap_dio_actor+0xcc/0x420
Modules linked in:
CPU: 2 PID: 8034 Comm: fsstress Tainted: G L 4.11.0-rc5-next-20170405 #100
task: c000000631314880 task.stack: c0000003915d4000
NIP: c00000000035a72c LR: c00000000035a6f4 CTR: c00000000035a660
REGS: c0000003915d7570 TRAP: 0700 Tainted: G L (4.11.0-rc5-next-20170405)
MSR: 800000000282b032 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI>
CR: 24004284 XER: 00000000
CFAR: c0000000006f7190 SOFTE: 1
GPR00: c00000000035a6f4 c0000003915d77f0 c0000000015a3f00 000000007c22f600
GPR04: 000000000022d000 0000000000002600 c0000003b2d56360 c0000003915d7960
GPR08: c0000003915d7cd0 0000000000000002 0000000000002600 c000000000521cc0
GPR12: 0000000024004284 c00000000fd80a00 000000004b04ae64 ffffffffffffffff
GPR16: 000000001000ca70 0000000000000000 c0000003b2d56380 c00000000153d2b8
GPR20: 0000000000000010 c0000003bc87bac8 0000000000223000 000000000022f5ff
GPR24: c0000003b2d56360 000000000000000c 0000000000002600 000000000022d000
GPR28: 0000000000000000 c0000003915d7960 c0000003b2d56360 00000000000001ff
NIP [c00000000035a72c] .iomap_dio_actor+0xcc/0x420
LR [c00000000035a6f4] .iomap_dio_actor+0x94/0x420
Call Trace:
[c0000003915d77f0] [c00000000035a6f4] .iomap_dio_actor+0x94/0x420 (unreliable)
[c0000003915d78f0] [c00000000035b9f4] .iomap_apply+0xf4/0x1f0
[c0000003915d79d0] [c00000000035c320] .iomap_dio_rw+0x230/0x420
[c0000003915d7ae0] [c000000000512a14] .xfs_file_dio_aio_read+0x84/0x160
[c0000003915d7b80] [c000000000512d24] .xfs_file_read_iter+0x104/0x130
[c0000003915d7c10] [c0000000002d6234] .__vfs_read+0x114/0x1a0
[c0000003915d7cf0] [c0000000002d7a8c] .vfs_read+0xac/0x1a0
[c0000003915d7d90] [c0000000002d96b8] .SyS_read+0x58/0x100
[c0000003915d7e30] [c00000000000b8e0] system_call+0x38/0xfc
Instruction dump:
78630020 7f831b78 7ffc07b4 7c7ce039 40820360 a13d0018 2f890003 419e0288
2f890004 419e00a0 2f890001 419e02a8 <0fe00000> 3b80fffb 38210100 7f83e378
The above problem can also be recreated on a regular xfs filesystem
using the command,
$ fsstress -d /mnt -l 1000 -n 1000 -p 1000
The reason for the call trace is,
1. When 'reserving' blocks for delayed allocation , XFS reserves more
blocks (i.e. past file's current EOF) than required. This is done
because XFS assumes that userspace might write more data and hence
'reserving' more blocks might lead to the file's new data being
stored contiguously on disk.
2. The in-memory 'struct xfs_bmbt_irec' mapping the file's last extent would
then cover the prealloc-ed EOF blocks in addition to the regular blocks.
3. When flushing the dirty blocks to disk, we only flush data till the
file's EOF. But before writing out the dirty data, we allocate blocks
on the disk for holding the file's new data. This allocation includes
the blocks that are part of the 'prealloc EOF blocks'.
4. Later, when the last reference to the inode is being closed, XFS frees the
unused 'prealloc EOF blocks' in xfs_inactive().
In step 3 above, When allocating space on disk for the delayed allocation
range, the space allocator might sometimes allocate less blocks than
required. If such an allocation ends right at the current EOF of the
file, We will not be able to clear the "delayed allocation" flag for the
'prealloc EOF blocks', since we won't have dirty buffer heads associated
with that range of the file.
In such a situation if a Direct I/O read operation is performed on file
range [X, Y] (where X < EOF and Y > EOF), we flush dirty data in the
range [X, Y] and invalidate page cache for that range (Refer to
iomap_dio_rw()). Later for performing the Direct I/O read, XFS obtains
the extent items (which are still cached in memory) for the file
range. When doing so we are not supposed to get an extent item with
IOMAP_DELALLOC flag set, since the previous "flush" operation should
have converted any delayed allocation data in the range [X, Y]. Hence we
end up hitting a WARN_ON_ONCE(1) statement in iomap_dio_actor().
This commit fixes the bug by preventing the read operation from going
beyond iomap_dio->i_size.
Reported-by: Santhosh G <santhog4@linux.vnet.ibm.com>
Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2017-04-13 01:03:20 +07:00
|
|
|
|
|
|
|
if (iov_iter_rw(iter) == READ && pos >= dio->i_size)
|
|
|
|
break;
|
2016-11-30 10:36:01 +07:00
|
|
|
} while ((count = iov_iter_count(iter)) > 0);
|
|
|
|
blk_finish_plug(&plug);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
iomap_dio_set_error(dio, ret);
|
|
|
|
|
iomap: Use FUA for pure data O_DSYNC DIO writes
If we are doing direct IO writes with datasync semantics, we often
have to flush metadata changes along with the data write. However,
if we are overwriting existing data, there are no metadata changes
that we need to flush. In this case, optimising the IO by using
FUA write makes sense.
We know from the IOMAP_F_DIRTY flag as to whether a specific inode
requires a metadata flush - this is currently used by DAX to ensure
extent modification as stable in page fault operations. For direct
IO writes, we can use it to determine if we need to flush metadata
or not once the data is on disk.
Hence if we have been returned a mapped extent that is not new and
the IO mapping is not dirty, then we can use a FUA write to provide
datasync semantics. This allows us to short-cut the
generic_write_sync() call in IO completion and hence avoid
unnecessary operations. This makes pure direct IO data write
behaviour identical to the way block devices use REQ_FUA to provide
datasync semantics.
On a FUA enabled device, a synchronous direct IO write workload
(sequential 4k overwrites in 32MB file) had the following results:
# xfs_io -fd -c "pwrite -V 1 -D 0 32m" /mnt/scratch/boo
kernel time write()s write iops Write b/w
------ ---- -------- ---------- ---------
(no dsync) 4s 2173/s 2173 8.5MB/s
vanilla 22s 370/s 750 1.4MB/s
patched 19s 420/s 420 1.6MB/s
The patched code clearly doesn't send cache flushes anymore, but
instead uses FUA (confirmed via blktrace), and performance improves
a bit as a result. However, the benefits will be higher on workloads
that mix O_DSYNC overwrites with other write IO as we won't be
flushing the entire device cache on every DSYNC overwrite IO
anymore.
Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2018-05-03 02:54:53 +07:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2016-11-30 10:36:01 +07:00
|
|
|
if (!atomic_dec_and_test(&dio->ref)) {
|
2018-06-20 05:10:55 +07:00
|
|
|
if (!dio->wait_for_completion)
|
2016-11-30 10:36:01 +07:00
|
|
|
return -EIOCBQUEUED;
|
|
|
|
|
|
|
|
for (;;) {
|
2018-11-16 22:37:34 +07:00
|
|
|
__set_current_state(TASK_UNINTERRUPTIBLE);
|
|
|
|
|
2016-11-30 10:36:01 +07:00
|
|
|
if (!READ_ONCE(dio->submit.waiter))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!(iocb->ki_flags & IOCB_HIPRI) ||
|
|
|
|
!dio->submit.last_queue ||
|
2017-11-03 01:29:54 +07:00
|
|
|
!blk_poll(dio->submit.last_queue,
|
2016-12-15 12:35:31 +07:00
|
|
|
dio->submit.cookie))
|
2016-11-30 10:36:01 +07:00
|
|
|
io_schedule();
|
|
|
|
}
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
}
|
|
|
|
|
iomap: invalidate page caches should be after iomap_dio_complete() in direct write
After XFS switching to iomap based DIO (commit acdda3aae146 ("xfs:
use iomap_dio_rw")), I started to notice dio29/dio30 tests failures
from LTP run on ppc64 hosts, and they can be reproduced on x86_64
hosts with 512B/1k block size XFS too.
dio29 diotest3 -b 65536 -n 100 -i 1000 -o 1024000
dio30 diotest6 -b 65536 -n 100 -i 1000 -o 1024000
The failure message is like:
bufcmp: offset 0: Expected: 0x62, got 0x0
diotest03 1 TPASS : Read with Direct IO, Write without
diotest03 2 TFAIL : diotest3.c:142: comparsion failed; child=98 offset=1425408
diotest03 3 TFAIL : diotest3.c:194: Write Direct-child 98 failed
Direct write wrote 0x62 but buffer read got zero. This is because,
when doing direct write to a hole or preallocated file, we
invalidate the page caches before converting the extent from
unwritten state to normal state, which is done by
iomap_dio_complete(), thus leave a window for other buffer reader to
cache the unwritten state extent.
Consider this case, with sub-page blocksize XFS, two processes are
direct writing to different blocksize-aligned regions (say 512B) of
the same preallocated file, and reading the region back via buffered
I/O to compare contents.
process A, region [0,512] process B, region [512,1024]
xfs_file_write_iter
xfs_file_aio_dio_write
iomap_dio_rw
iomap_apply
invalidate_inode_pages2_range
xfs_file_write_iter
xfs_file_aio_dio_write
iomap_dio_rw
iomap_apply
invalidate_inode_pages2_range
iomap_dio_complete
xfs_file_read_iter
xfs_file_buffered_aio_read
generic_file_read_iter
do_generic_file_read
<readahead fills pagecache with 0>
iomap_dio_complete
xfs_file_read_iter
<read gets 0 from pagecache>
Process A first invalidates page caches, at this point the
underlying extent is still in unwritten state (iomap_dio_complete
not called yet), and process B finishs direct write and populates
page caches via readahead, which caches zeros in page for region A,
then process A reads zeros from page cache, instead of the actual
data.
Fix it by invalidating page caches after converting unwritten extent
to make sure we read content from disk after extent state changed,
as what we did before switching to iomap based dio.
Also introduce a new 'start' variable to save the original write
offset (iomap_dio_complete() updates iocb->ki_pos), and a 'err'
variable for invalidating caches result, cause we can't reuse 'ret'
anymore.
Signed-off-by: Eryu Guan <eguan@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2017-03-03 06:02:06 +07:00
|
|
|
ret = iomap_dio_complete(dio);
|
|
|
|
|
|
|
|
return ret;
|
2016-11-30 10:36:01 +07:00
|
|
|
|
|
|
|
out_free_dio:
|
|
|
|
kfree(dio);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_dio_rw);
|
2018-05-10 22:38:15 +07:00
|
|
|
|
|
|
|
/* Swapfile activation */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SWAP
|
|
|
|
struct iomap_swapfile_info {
|
|
|
|
struct iomap iomap; /* accumulated iomap */
|
|
|
|
struct swap_info_struct *sis;
|
|
|
|
uint64_t lowest_ppage; /* lowest physical addr seen (pages) */
|
|
|
|
uint64_t highest_ppage; /* highest physical addr seen (pages) */
|
|
|
|
unsigned long nr_pages; /* number of pages collected */
|
|
|
|
int nr_extents; /* extent count */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect physical extents for this swap file. Physical extents reported to
|
|
|
|
* the swap code must be trimmed to align to a page boundary. The logical
|
|
|
|
* offset within the file is irrelevant since the swapfile code maps logical
|
|
|
|
* page numbers of the swap device to the physical page-aligned extents.
|
|
|
|
*/
|
|
|
|
static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi)
|
|
|
|
{
|
|
|
|
struct iomap *iomap = &isi->iomap;
|
|
|
|
unsigned long nr_pages;
|
|
|
|
uint64_t first_ppage;
|
|
|
|
uint64_t first_ppage_reported;
|
|
|
|
uint64_t next_ppage;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Round the start up and the end down so that the physical
|
|
|
|
* extent aligns to a page boundary.
|
|
|
|
*/
|
|
|
|
first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT;
|
|
|
|
next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >>
|
|
|
|
PAGE_SHIFT;
|
|
|
|
|
|
|
|
/* Skip too-short physical extents. */
|
|
|
|
if (first_ppage >= next_ppage)
|
|
|
|
return 0;
|
|
|
|
nr_pages = next_ppage - first_ppage;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate how much swap space we're adding; the first page contains
|
|
|
|
* the swap header and doesn't count. The mm still wants that first
|
|
|
|
* page fed to add_swap_extent, however.
|
|
|
|
*/
|
|
|
|
first_ppage_reported = first_ppage;
|
|
|
|
if (iomap->offset == 0)
|
|
|
|
first_ppage_reported++;
|
|
|
|
if (isi->lowest_ppage > first_ppage_reported)
|
|
|
|
isi->lowest_ppage = first_ppage_reported;
|
|
|
|
if (isi->highest_ppage < (next_ppage - 1))
|
|
|
|
isi->highest_ppage = next_ppage - 1;
|
|
|
|
|
|
|
|
/* Add extent, set up for the next call. */
|
|
|
|
error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage);
|
|
|
|
if (error < 0)
|
|
|
|
return error;
|
|
|
|
isi->nr_extents += error;
|
|
|
|
isi->nr_pages += nr_pages;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accumulate iomaps for this swap file. We have to accumulate iomaps because
|
|
|
|
* swap only cares about contiguous page-aligned physical extents and makes no
|
|
|
|
* distinction between written and unwritten extents.
|
|
|
|
*/
|
|
|
|
static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
|
|
|
|
loff_t count, void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
struct iomap_swapfile_info *isi = data;
|
|
|
|
int error;
|
|
|
|
|
2018-06-01 23:03:06 +07:00
|
|
|
switch (iomap->type) {
|
|
|
|
case IOMAP_MAPPED:
|
|
|
|
case IOMAP_UNWRITTEN:
|
|
|
|
/* Only real or unwritten extents. */
|
|
|
|
break;
|
|
|
|
case IOMAP_INLINE:
|
|
|
|
/* No inline data. */
|
2018-05-17 01:13:34 +07:00
|
|
|
pr_err("swapon: file is inline\n");
|
|
|
|
return -EINVAL;
|
2018-06-01 23:03:06 +07:00
|
|
|
default:
|
2018-05-17 01:13:34 +07:00
|
|
|
pr_err("swapon: file has unallocated extents\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-05-10 22:38:15 +07:00
|
|
|
|
2018-05-17 01:13:34 +07:00
|
|
|
/* No uncommitted metadata or shared blocks. */
|
|
|
|
if (iomap->flags & IOMAP_F_DIRTY) {
|
|
|
|
pr_err("swapon: file is not committed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (iomap->flags & IOMAP_F_SHARED) {
|
|
|
|
pr_err("swapon: file has shared extents\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-05-10 22:38:15 +07:00
|
|
|
|
2018-05-17 01:13:34 +07:00
|
|
|
/* Only one bdev per swap file. */
|
|
|
|
if (iomap->bdev != isi->sis->bdev) {
|
|
|
|
pr_err("swapon: file is on multiple devices\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-05-10 22:38:15 +07:00
|
|
|
|
|
|
|
if (isi->iomap.length == 0) {
|
|
|
|
/* No accumulated extent, so just store it. */
|
|
|
|
memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
|
|
|
|
} else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
|
|
|
|
/* Append this to the accumulated extent. */
|
|
|
|
isi->iomap.length += iomap->length;
|
|
|
|
} else {
|
|
|
|
/* Otherwise, add the retained iomap and store this one. */
|
|
|
|
error = iomap_swapfile_add_extent(isi);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate a swap file's iomaps to construct physical extents that can be
|
|
|
|
* passed to the swapfile subsystem.
|
|
|
|
*/
|
|
|
|
int iomap_swapfile_activate(struct swap_info_struct *sis,
|
|
|
|
struct file *swap_file, sector_t *pagespan,
|
|
|
|
const struct iomap_ops *ops)
|
|
|
|
{
|
|
|
|
struct iomap_swapfile_info isi = {
|
|
|
|
.sis = sis,
|
|
|
|
.lowest_ppage = (sector_t)-1ULL,
|
|
|
|
};
|
|
|
|
struct address_space *mapping = swap_file->f_mapping;
|
|
|
|
struct inode *inode = mapping->host;
|
|
|
|
loff_t pos = 0;
|
|
|
|
loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE);
|
|
|
|
loff_t ret;
|
|
|
|
|
2018-06-05 23:53:05 +07:00
|
|
|
/*
|
|
|
|
* Persist all file mapping metadata so that we won't have any
|
|
|
|
* IOMAP_F_DIRTY iomaps.
|
|
|
|
*/
|
|
|
|
ret = vfs_fsync(swap_file, 1);
|
2018-05-10 22:38:15 +07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
ret = iomap_apply(inode, pos, len, IOMAP_REPORT,
|
|
|
|
ops, &isi, iomap_swapfile_activate_actor);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pos += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isi.iomap.length) {
|
|
|
|
ret = iomap_swapfile_add_extent(&isi);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pagespan = 1 + isi.highest_ppage - isi.lowest_ppage;
|
|
|
|
sis->max = isi.nr_pages;
|
|
|
|
sis->pages = isi.nr_pages - 1;
|
|
|
|
sis->highest_bit = isi.nr_pages - 1;
|
|
|
|
return isi.nr_extents;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_swapfile_activate);
|
|
|
|
#endif /* CONFIG_SWAP */
|
2018-06-01 23:03:08 +07:00
|
|
|
|
|
|
|
static loff_t
|
|
|
|
iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
|
|
|
|
void *data, struct iomap *iomap)
|
|
|
|
{
|
|
|
|
sector_t *bno = data, addr;
|
|
|
|
|
|
|
|
if (iomap->type == IOMAP_MAPPED) {
|
|
|
|
addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
|
|
|
|
if (addr > INT_MAX)
|
|
|
|
WARN(1, "would truncate bmap result\n");
|
|
|
|
else
|
|
|
|
*bno = addr;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* legacy ->bmap interface. 0 is the error return (!) */
|
|
|
|
sector_t
|
|
|
|
iomap_bmap(struct address_space *mapping, sector_t bno,
|
|
|
|
const struct iomap_ops *ops)
|
|
|
|
{
|
|
|
|
struct inode *inode = mapping->host;
|
2018-08-03 03:09:27 +07:00
|
|
|
loff_t pos = bno << inode->i_blkbits;
|
2018-06-01 23:03:08 +07:00
|
|
|
unsigned blocksize = i_blocksize(inode);
|
|
|
|
|
|
|
|
if (filemap_write_and_wait(mapping))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bno = 0;
|
|
|
|
iomap_apply(inode, pos, blocksize, 0, ops, &bno, iomap_bmap_actor);
|
|
|
|
return bno;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iomap_bmap);
|