2019-05-19 12:08:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* fs/direct-io.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002, Linus Torvalds.
|
|
|
|
*
|
|
|
|
* O_DIRECT
|
|
|
|
*
|
2008-10-16 05:01:59 +00:00
|
|
|
* 04Jul2002 Andrew Morton
|
2005-04-16 22:20:36 +00:00
|
|
|
* Initial version
|
|
|
|
* 11Sep2002 janetinc@us.ibm.com
|
|
|
|
* added readv/writev support.
|
2008-10-16 05:01:59 +00:00
|
|
|
* 29Oct2002 Andrew Morton
|
2005-04-16 22:20:36 +00:00
|
|
|
* rewrote bio_add_page() support.
|
|
|
|
* 30Oct2002 pbadari@us.ibm.com
|
|
|
|
* added support for non-aligned IO.
|
|
|
|
* 06Nov2002 pbadari@us.ibm.com
|
|
|
|
* added asynchronous IO support.
|
|
|
|
* 21Jul2003 nathans@sgi.com
|
|
|
|
* added IO completion notifier.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/pagemap.h>
|
2006-12-10 10:19:47 +00:00
|
|
|
#include <linux/task_io_accounting_ops.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/buffer_head.h>
|
|
|
|
#include <linux/rwsem.h>
|
|
|
|
#include <linux/uio.h>
|
2011-07-26 23:09:06 +00:00
|
|
|
#include <linux/atomic.h>
|
2012-01-13 01:20:35 +00:00
|
|
|
#include <linux/prefetch.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2020-01-04 20:59:49 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2023-05-26 21:41:42 +00:00
|
|
|
* How many user pages to map in one call to iov_iter_extract_pages(). This
|
|
|
|
* determines the size of a structure in the slab cache
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
#define DIO_PAGES 64
|
|
|
|
|
2017-10-17 14:43:09 +00:00
|
|
|
/*
|
|
|
|
* Flags for dio_complete()
|
|
|
|
*/
|
|
|
|
#define DIO_COMPLETE_ASYNC 0x01 /* This is async IO */
|
|
|
|
#define DIO_COMPLETE_INVALIDATE 0x02 /* Can invalidate pages */
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* This code generally works in units of "dio_blocks". A dio_block is
|
|
|
|
* somewhere between the hard sector size and the filesystem block size. it
|
|
|
|
* is determined on a per-invocation basis. When talking to the filesystem
|
|
|
|
* we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
|
|
|
|
* down by dio->blkfactor. Similarly, fs-blocksize quantities are converted
|
|
|
|
* to bio_block quantities by shifting left by blkfactor.
|
|
|
|
*
|
|
|
|
* If blkfactor is zero then the user's request was aligned to the filesystem's
|
|
|
|
* blocksize.
|
|
|
|
*/
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
/* dio_state only used in the submission path */
|
|
|
|
|
|
|
|
struct dio_submit {
|
2005-04-16 22:20:36 +00:00
|
|
|
struct bio *bio; /* bio under assembly */
|
|
|
|
unsigned blkbits; /* doesn't change */
|
|
|
|
unsigned blkfactor; /* When we're using an alignment which
|
|
|
|
is finer than the filesystem's soft
|
|
|
|
blocksize, this specifies how much
|
|
|
|
finer. blkfactor=2 means 1/4-block
|
|
|
|
alignment. Does not change */
|
|
|
|
unsigned start_zero_done; /* flag: sub-blocksize zeroing has
|
|
|
|
been performed at the start of a
|
|
|
|
write */
|
|
|
|
int pages_in_io; /* approximate total IO pages */
|
|
|
|
sector_t block_in_file; /* Current offset into the underlying
|
|
|
|
file in dio_block units. */
|
|
|
|
unsigned blocks_available; /* At block_in_file. changes */
|
2011-08-02 04:38:05 +00:00
|
|
|
int reap_counter; /* rate limit reaping */
|
2005-04-16 22:20:36 +00:00
|
|
|
sector_t final_block_in_request;/* doesn't change */
|
|
|
|
int boundary; /* prev block is at a boundary */
|
2006-03-26 09:38:02 +00:00
|
|
|
get_block_t *get_block; /* block mapping function */
|
2011-08-02 04:38:03 +00:00
|
|
|
|
2010-05-23 15:00:55 +00:00
|
|
|
loff_t logical_offset_in_bio; /* current first logical block in bio */
|
2005-04-16 22:20:36 +00:00
|
|
|
sector_t final_block_in_bio; /* current final block in bio + 1 */
|
|
|
|
sector_t next_block_for_io; /* next block to be put under IO,
|
|
|
|
in dio_blocks units */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deferred addition of a page to the dio. These variables are
|
|
|
|
* private to dio_send_cur_page(), submit_page_section() and
|
|
|
|
* dio_bio_add_page().
|
|
|
|
*/
|
|
|
|
struct page *cur_page; /* The page */
|
|
|
|
unsigned cur_page_offset; /* Offset into it, in bytes */
|
|
|
|
unsigned cur_page_len; /* Nr of bytes at cur_page_offset */
|
|
|
|
sector_t cur_page_block; /* Where it starts */
|
2010-05-23 15:00:55 +00:00
|
|
|
loff_t cur_page_fs_offset; /* Offset in file */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2014-03-15 08:05:57 +00:00
|
|
|
struct iov_iter *iter;
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Page queue. These variables belong to dio_refill_pages() and
|
|
|
|
* dio_get_page().
|
|
|
|
*/
|
|
|
|
unsigned head; /* next page to process */
|
|
|
|
unsigned tail; /* last valid page + 1 */
|
2014-03-15 08:05:57 +00:00
|
|
|
size_t from, to;
|
2011-08-02 04:38:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* dio_state communicated between submission path and end_io */
|
|
|
|
struct dio {
|
|
|
|
int flags; /* doesn't change */
|
2022-07-14 18:07:14 +00:00
|
|
|
blk_opf_t opf; /* request operation type and flags */
|
2017-08-23 17:10:32 +00:00
|
|
|
struct gendisk *bio_disk;
|
2011-08-02 04:38:05 +00:00
|
|
|
struct inode *inode;
|
2011-08-02 04:38:03 +00:00
|
|
|
loff_t i_size; /* i_size when submitted */
|
|
|
|
dio_iodone_t *end_io; /* IO completion function */
|
2023-05-26 21:41:42 +00:00
|
|
|
bool is_pinned; /* T if we have pins on the pages */
|
2011-08-02 04:38:03 +00:00
|
|
|
|
2011-08-02 04:38:07 +00:00
|
|
|
void *private; /* copy from map_bh.b_private */
|
2011-08-02 04:38:03 +00:00
|
|
|
|
|
|
|
/* BIO completion state */
|
|
|
|
spinlock_t bio_lock; /* protects BIO fields below */
|
2023-05-26 21:41:42 +00:00
|
|
|
int page_errors; /* err from iov_iter_extract_pages() */
|
2011-08-02 04:38:05 +00:00
|
|
|
int is_async; /* is IO async ? */
|
2013-09-04 13:04:39 +00:00
|
|
|
bool defer_completion; /* defer AIO completion to workqueue? */
|
2015-08-17 02:31:46 +00:00
|
|
|
bool should_dirty; /* if pages should be dirtied */
|
2011-08-02 04:38:05 +00:00
|
|
|
int io_error; /* IO error in completion path */
|
2011-08-02 04:38:03 +00:00
|
|
|
unsigned long refcount; /* direct_io_worker() and bios */
|
|
|
|
struct bio *bio_list; /* singly linked via bi_private */
|
|
|
|
struct task_struct *waiter; /* waiting task (NULL if none) */
|
|
|
|
|
|
|
|
/* AIO related stuff */
|
|
|
|
struct kiocb *iocb; /* kiocb */
|
|
|
|
ssize_t result; /* IO result */
|
|
|
|
|
2009-12-16 00:47:49 +00:00
|
|
|
/*
|
|
|
|
* pages[] (and any fields placed after it) are not zeroed out at
|
|
|
|
* allocation time. Don't add new fields after pages[] unless you
|
|
|
|
* wish that they not be zeroed.
|
|
|
|
*/
|
2013-09-04 13:04:39 +00:00
|
|
|
union {
|
|
|
|
struct page *pages[DIO_PAGES]; /* page buffer */
|
|
|
|
struct work_struct complete_work;/* deferred AIO completion */
|
|
|
|
};
|
2011-08-02 04:38:06 +00:00
|
|
|
} ____cacheline_aligned_in_smp;
|
|
|
|
|
2023-10-11 16:55:00 +00:00
|
|
|
static struct kmem_cache *dio_cache __ro_after_init;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* How many pages are in the queue?
|
|
|
|
*/
|
2011-08-02 04:38:03 +00:00
|
|
|
static inline unsigned dio_pages_present(struct dio_submit *sdio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2011-08-02 04:38:03 +00:00
|
|
|
return sdio->tail - sdio->head;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go grab and pin some userspace pages. Typically we'll get 64 at a time.
|
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2023-05-26 21:41:42 +00:00
|
|
|
struct page **pages = dio->pages;
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
2014-03-15 08:05:57 +00:00
|
|
|
ssize_t ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2023-05-26 21:41:42 +00:00
|
|
|
ret = iov_iter_extract_pages(sdio->iter, &pages, LONG_MAX,
|
|
|
|
DIO_PAGES, 0, &sdio->from);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2022-07-14 18:07:14 +00:00
|
|
|
if (ret < 0 && sdio->blocks_available && dio_op == REQ_OP_WRITE) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* A memory fault, but the filesystem has some outstanding
|
|
|
|
* mapped blocks. We need to use those blocks up to avoid
|
|
|
|
* leaking stale data in the file.
|
|
|
|
*/
|
|
|
|
if (dio->page_errors == 0)
|
|
|
|
dio->page_errors = ret;
|
2023-05-26 21:41:42 +00:00
|
|
|
dio->pages[0] = ZERO_PAGE(0);
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->head = 0;
|
|
|
|
sdio->tail = 1;
|
2014-03-15 08:05:57 +00:00
|
|
|
sdio->from = 0;
|
|
|
|
sdio->to = PAGE_SIZE;
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0) {
|
2014-03-15 08:05:57 +00:00
|
|
|
ret += sdio->from;
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->head = 0;
|
2014-03-15 08:05:57 +00:00
|
|
|
sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
|
|
|
|
sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get another userspace page. Returns an ERR_PTR on error. Pages are
|
2023-05-26 21:41:42 +00:00
|
|
|
* buffered inside the dio so that we can call iov_iter_extract_pages()
|
|
|
|
* against a decent number of pages, less frequently. To provide nicer use of
|
|
|
|
* the L1 cache.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline struct page *dio_get_page(struct dio *dio,
|
2014-07-20 09:09:04 +00:00
|
|
|
struct dio_submit *sdio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2011-08-02 04:38:03 +00:00
|
|
|
if (dio_pages_present(sdio) == 0) {
|
2005-04-16 22:20:36 +00:00
|
|
|
int ret;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
ret = dio_refill_pages(dio, sdio);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
2011-08-02 04:38:03 +00:00
|
|
|
BUG_ON(dio_pages_present(sdio) == 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2014-07-20 09:09:04 +00:00
|
|
|
return dio->pages[sdio->head];
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:41:42 +00:00
|
|
|
static void dio_pin_page(struct dio *dio, struct page *page)
|
|
|
|
{
|
|
|
|
if (dio->is_pinned)
|
|
|
|
folio_add_pin(page_folio(page));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dio_unpin_page(struct dio *dio, struct page *page)
|
|
|
|
{
|
|
|
|
if (dio->is_pinned)
|
|
|
|
unpin_user_page(page);
|
|
|
|
}
|
|
|
|
|
2019-10-14 21:12:11 +00:00
|
|
|
/*
|
2006-12-10 10:20:54 +00:00
|
|
|
* dio_complete() - called when all DIO BIO I/O has been completed
|
|
|
|
*
|
2013-09-04 13:04:39 +00:00
|
|
|
* This drops i_dio_count, lets interested parties know that a DIO operation
|
|
|
|
* has completed, and calculates the resulting return code for the operation.
|
2006-12-10 10:20:54 +00:00
|
|
|
*
|
|
|
|
* It lets the filesystem know if it registered an interest earlier via
|
|
|
|
* get_block. Pass the private field of the map buffer_head so that
|
|
|
|
* filesystems can use it to hold additional state between get_block calls and
|
|
|
|
* dio_complete.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2017-10-17 14:43:09 +00:00
|
|
|
static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
2016-04-07 15:51:59 +00:00
|
|
|
loff_t offset = dio->iocb->ki_pos;
|
2006-12-10 10:20:54 +00:00
|
|
|
ssize_t transferred = 0;
|
2017-09-21 14:16:29 +00:00
|
|
|
int err;
|
2006-12-10 10:20:54 +00:00
|
|
|
|
2006-12-10 10:21:05 +00:00
|
|
|
/*
|
|
|
|
* AIO submission can race with bio completion to get here while
|
|
|
|
* expecting to have the last io completed by bio completion.
|
|
|
|
* In that case -EIOCBQUEUED is in fact not an error we want
|
|
|
|
* to preserve through this call.
|
|
|
|
*/
|
|
|
|
if (ret == -EIOCBQUEUED)
|
|
|
|
ret = 0;
|
|
|
|
|
2006-12-10 10:20:54 +00:00
|
|
|
if (dio->result) {
|
|
|
|
transferred = dio->result;
|
|
|
|
|
|
|
|
/* Check for short read case */
|
2022-07-14 18:07:14 +00:00
|
|
|
if (dio_op == REQ_OP_READ &&
|
2016-06-05 19:31:50 +00:00
|
|
|
((offset + transferred) > dio->i_size))
|
2006-12-10 10:20:54 +00:00
|
|
|
transferred = dio->i_size - offset;
|
2016-10-04 00:38:55 +00:00
|
|
|
/* ignore EFAULT if some IO has been done */
|
|
|
|
if (unlikely(ret == -EFAULT) && transferred)
|
|
|
|
ret = 0;
|
2006-12-10 10:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
ret = dio->page_errors;
|
|
|
|
if (ret == 0)
|
|
|
|
ret = dio->io_error;
|
|
|
|
if (ret == 0)
|
|
|
|
ret = transferred;
|
|
|
|
|
2017-10-13 16:47:46 +00:00
|
|
|
if (dio->end_io) {
|
|
|
|
// XXX: ki_pos??
|
|
|
|
err = dio->end_io(dio->iocb, offset, ret, dio->private);
|
|
|
|
if (err)
|
|
|
|
ret = err;
|
|
|
|
}
|
|
|
|
|
2017-09-21 14:16:29 +00: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...
|
2017-10-13 16:47:46 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2017-09-21 14:16:29 +00:00
|
|
|
*/
|
2017-10-17 14:43:09 +00:00
|
|
|
if (flags & DIO_COMPLETE_INVALIDATE &&
|
2023-06-01 14:58:58 +00:00
|
|
|
ret > 0 && dio_op == REQ_OP_WRITE)
|
|
|
|
kiocb_invalidate_post_direct_write(dio->iocb, ret);
|
2017-09-21 14:16:29 +00:00
|
|
|
|
2018-02-23 11:45:29 +00:00
|
|
|
inode_dio_end(dio->inode);
|
direct-io: only inc/dec inode->i_dio_count for file systems
do_blockdev_direct_IO() increments and decrements the inode
->i_dio_count for each IO operation. It does this to protect against
truncate of a file. Block devices don't need this sort of protection.
For a capable multiqueue setup, this atomic int is the only shared
state between applications accessing the device for O_DIRECT, and it
presents a scaling wall for that. In my testing, as much as 30% of
system time is spent incrementing and decrementing this value. A mixed
read/write workload improved from ~2.5M IOPS to ~9.6M IOPS, with
better latencies too. Before:
clat percentiles (usec):
| 1.00th=[ 33], 5.00th=[ 34], 10.00th=[ 34], 20.00th=[ 34],
| 30.00th=[ 34], 40.00th=[ 34], 50.00th=[ 35], 60.00th=[ 35],
| 70.00th=[ 35], 80.00th=[ 35], 90.00th=[ 37], 95.00th=[ 80],
| 99.00th=[ 98], 99.50th=[ 151], 99.90th=[ 155], 99.95th=[ 155],
| 99.99th=[ 165]
After:
clat percentiles (usec):
| 1.00th=[ 95], 5.00th=[ 108], 10.00th=[ 129], 20.00th=[ 149],
| 30.00th=[ 155], 40.00th=[ 161], 50.00th=[ 167], 60.00th=[ 171],
| 70.00th=[ 177], 80.00th=[ 185], 90.00th=[ 201], 95.00th=[ 270],
| 99.00th=[ 390], 99.50th=[ 398], 99.90th=[ 418], 99.95th=[ 422],
| 99.99th=[ 438]
In other setups, Robert Elliott reported seeing good performance
improvements:
https://lkml.org/lkml/2015/4/3/557
The more applications accessing the device, the worse it gets.
Add a new direct-io flags, DIO_SKIP_DIO_COUNT, which tells
do_blockdev_direct_IO() that it need not worry about incrementing
or decrementing the inode i_dio_count for this caller.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Elliott, Robert (Server Storage) <elliott@hp.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Jens Axboe <axboe@fb.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2015-04-15 23:05:48 +00:00
|
|
|
|
2017-10-17 14:43:09 +00:00
|
|
|
if (flags & DIO_COMPLETE_ASYNC) {
|
2016-04-07 15:52:01 +00:00
|
|
|
/*
|
|
|
|
* generic_write_sync expects ki_pos to have been updated
|
|
|
|
* already, but the submission path only does this for
|
|
|
|
* synchronous I/O.
|
|
|
|
*/
|
|
|
|
dio->iocb->ki_pos += transferred;
|
2013-09-04 13:04:40 +00:00
|
|
|
|
2022-07-14 18:07:14 +00:00
|
|
|
if (ret > 0 && dio_op == REQ_OP_WRITE)
|
fs: fix lost error code in dio_complete
commit e259221763a40403d5bb232209998e8c45804ab8 ("fs: simplify the
generic_write_sync prototype") reworked callers of generic_write_sync(),
and ended up dropping the error return for the directio path. Prior to
that commit, in dio_complete(), an error would be bubbled up the stack,
but after that commit, errors passed on to dio_complete were eaten up.
This was reported on the list earlier, and a fix was proposed in
https://lore.kernel.org/lkml/20160921141539.GA17898@infradead.org/, but
never followed up with. We recently hit this bug in our testing where
fencing io errors, which were previously erroring out with EIO, were
being returned as success operations after this commit.
The fix proposed on the list earlier was a little short -- it would have
still called generic_write_sync() in case `ret` already contained an
error. This fix ensures generic_write_sync() is only called when there's
no pending error in the write. Additionally, transferred is replaced
with ret to bring this code in line with other callers.
Fixes: e259221763a4 ("fs: simplify the generic_write_sync prototype")
Reported-by: Ravi Nankani <rnankani@amazon.com>
Signed-off-by: Maximilian Heyne <mheyne@amazon.de>
Reviewed-by: Christoph Hellwig <hch@lst.de>
CC: Torsten Mehlan <tomeh@amazon.de>
CC: Uwe Dannowski <uwed@amazon.de>
CC: Amit Shah <aams@amazon.de>
CC: David Woodhouse <dwmw@amazon.co.uk>
CC: stable@vger.kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-11-30 15:35:14 +00:00
|
|
|
ret = generic_write_sync(dio->iocb, ret);
|
2021-10-21 15:22:35 +00:00
|
|
|
dio->iocb->ki_complete(dio->iocb, ret);
|
2013-09-04 13:04:40 +00:00
|
|
|
}
|
2010-07-18 21:17:09 +00:00
|
|
|
|
2013-09-04 13:04:39 +00:00
|
|
|
kmem_cache_free(dio_cache, dio);
|
2006-12-10 10:20:54 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 13:04:39 +00:00
|
|
|
static void dio_aio_complete_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct dio *dio = container_of(work, struct dio, complete_work);
|
|
|
|
|
2017-10-17 14:43:09 +00:00
|
|
|
dio_complete(dio, 0, DIO_COMPLETE_ASYNC | DIO_COMPLETE_INVALIDATE);
|
2013-09-04 13:04:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 07:38:06 +00:00
|
|
|
static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio);
|
2013-09-04 13:04:39 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Asynchronous IO callback.
|
|
|
|
*/
|
2015-07-20 13:29:37 +00:00
|
|
|
static void dio_bio_end_aio(struct bio *bio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct dio *dio = bio->bi_private;
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
2006-12-10 10:21:07 +00:00
|
|
|
unsigned long remaining;
|
|
|
|
unsigned long flags;
|
2017-09-21 14:16:29 +00:00
|
|
|
bool defer_completion = false;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* cleanup the bio */
|
|
|
|
dio_bio_complete(dio, bio);
|
2006-12-10 10:20:59 +00:00
|
|
|
|
2006-12-10 10:21:07 +00:00
|
|
|
spin_lock_irqsave(&dio->bio_lock, flags);
|
|
|
|
remaining = --dio->refcount;
|
|
|
|
if (remaining == 1 && dio->waiter)
|
2006-12-10 10:21:01 +00:00
|
|
|
wake_up_process(dio->waiter);
|
2006-12-10 10:21:07 +00:00
|
|
|
spin_unlock_irqrestore(&dio->bio_lock, flags);
|
2006-12-10 10:21:01 +00:00
|
|
|
|
2006-12-10 10:21:05 +00:00
|
|
|
if (remaining == 0) {
|
2017-09-21 14:16:29 +00:00
|
|
|
/*
|
|
|
|
* Defer completion when defer_completion is set or
|
|
|
|
* when the inode has pages mapped and this is AIO write.
|
|
|
|
* We need to invalidate those pages because there is a
|
|
|
|
* chance they contain stale data in the case buffered IO
|
|
|
|
* went in between AIO submission and completion into the
|
|
|
|
* same region.
|
|
|
|
*/
|
|
|
|
if (dio->result)
|
|
|
|
defer_completion = dio->defer_completion ||
|
2022-07-14 18:07:14 +00:00
|
|
|
(dio_op == REQ_OP_WRITE &&
|
2017-09-21 14:16:29 +00:00
|
|
|
dio->inode->i_mapping->nrpages);
|
|
|
|
if (defer_completion) {
|
2013-09-04 13:04:39 +00:00
|
|
|
INIT_WORK(&dio->complete_work, dio_aio_complete_work);
|
|
|
|
queue_work(dio->inode->i_sb->s_dio_done_wq,
|
|
|
|
&dio->complete_work);
|
|
|
|
} else {
|
2017-10-17 14:43:09 +00:00
|
|
|
dio_complete(dio, 0, DIO_COMPLETE_ASYNC);
|
2013-09-04 13:04:39 +00:00
|
|
|
}
|
2006-12-10 10:21:05 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The BIO completion handler simply queues the BIO up for the process-context
|
|
|
|
* handler.
|
|
|
|
*
|
|
|
|
* During I/O bi_private points at the dio. After I/O, bi_private is used to
|
|
|
|
* implement a singly-linked list of completed BIOs, at dio->bio_list.
|
|
|
|
*/
|
2015-07-20 13:29:37 +00:00
|
|
|
static void dio_bio_end_io(struct bio *bio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct dio *dio = bio->bi_private;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dio->bio_lock, flags);
|
|
|
|
bio->bi_private = dio->bio_list;
|
|
|
|
dio->bio_list = bio;
|
2006-12-10 10:21:07 +00:00
|
|
|
if (--dio->refcount == 1 && dio->waiter)
|
2005-04-16 22:20:36 +00:00
|
|
|
wake_up_process(dio->waiter);
|
|
|
|
spin_unlock_irqrestore(&dio->bio_lock, flags);
|
|
|
|
}
|
|
|
|
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline void
|
2011-08-02 04:38:03 +00:00
|
|
|
dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
|
|
|
|
struct block_device *bdev,
|
|
|
|
sector_t first_sector, int nr_vecs)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct bio *bio;
|
|
|
|
|
2011-01-20 22:44:22 +00:00
|
|
|
/*
|
2018-05-09 07:54:08 +00:00
|
|
|
* bio_alloc() is guaranteed to return a bio when allowed to sleep and
|
|
|
|
* we request a valid number of vectors.
|
2011-01-20 22:44:22 +00:00
|
|
|
*/
|
2022-07-14 18:07:14 +00:00
|
|
|
bio = bio_alloc(bdev, nr_vecs, dio->opf, GFP_KERNEL);
|
2013-10-11 22:44:27 +00:00
|
|
|
bio->bi_iter.bi_sector = first_sector;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (dio->is_async)
|
|
|
|
bio->bi_end_io = dio_bio_end_aio;
|
|
|
|
else
|
|
|
|
bio->bi_end_io = dio_bio_end_io;
|
2023-05-26 21:41:42 +00:00
|
|
|
if (dio->is_pinned)
|
|
|
|
bio_set_flag(bio, BIO_PAGE_PINNED);
|
2024-02-02 20:39:25 +00:00
|
|
|
bio->bi_write_hint = file_inode(dio->iocb->ki_filp)->i_write_hint;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->bio = bio;
|
|
|
|
sdio->logical_offset_in_bio = sdio->cur_page_fs_offset;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the AIO read case we speculatively dirty the pages before starting IO.
|
|
|
|
* During IO completion, any of these pages which happen to have been written
|
|
|
|
* back will be redirtied by bio_check_pages_dirty().
|
2006-12-10 10:20:59 +00:00
|
|
|
*
|
|
|
|
* bios hold a dio reference between submit_bio and ->end_io.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
2011-08-02 04:38:03 +00:00
|
|
|
struct bio *bio = sdio->bio;
|
2006-12-10 10:21:07 +00:00
|
|
|
unsigned long flags;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
bio->bi_private = dio;
|
2006-12-10 10:21:07 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&dio->bio_lock, flags);
|
|
|
|
dio->refcount++;
|
|
|
|
spin_unlock_irqrestore(&dio->bio_lock, flags);
|
|
|
|
|
2022-07-14 18:07:14 +00:00
|
|
|
if (dio->is_async && dio_op == REQ_OP_READ && dio->should_dirty)
|
2005-04-16 22:20:36 +00:00
|
|
|
bio_set_pages_dirty(bio);
|
2006-12-10 10:21:07 +00:00
|
|
|
|
2021-01-24 10:02:34 +00:00
|
|
|
dio->bio_disk = bio->bi_bdev->bd_disk;
|
2015-11-10 17:14:38 +00:00
|
|
|
|
2023-01-20 00:22:22 +00:00
|
|
|
submit_bio(bio);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->bio = NULL;
|
|
|
|
sdio->boundary = 0;
|
|
|
|
sdio->logical_offset_in_bio = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release any resources in case of a failure
|
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2023-05-26 21:41:42 +00:00
|
|
|
if (dio->is_pinned)
|
|
|
|
unpin_user_pages(dio->pages + sdio->head,
|
|
|
|
sdio->tail - sdio->head);
|
2023-06-13 21:54:39 +00:00
|
|
|
sdio->head = sdio->tail;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-12-10 10:20:59 +00:00
|
|
|
* Wait for the next BIO to complete. Remove it and return it. NULL is
|
|
|
|
* returned once all BIOs have been completed. This must only be called once
|
|
|
|
* all bios have been issued so that dio->refcount can only decrease. This
|
2021-02-24 20:00:48 +00:00
|
|
|
* requires that the caller hold a reference on the dio.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
static struct bio *dio_await_one(struct dio *dio)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2006-12-10 10:20:59 +00:00
|
|
|
struct bio *bio = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&dio->bio_lock, flags);
|
2006-12-10 10:21:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait as long as the list is empty and there are bios in flight. bio
|
|
|
|
* completion drops the count, maybe adds to the list, and wakes while
|
|
|
|
* holding the bio_lock so we don't need set_current_state()'s barrier
|
|
|
|
* and can call it after testing our condition.
|
|
|
|
*/
|
|
|
|
while (dio->refcount > 1 && dio->bio_list == NULL) {
|
|
|
|
__set_current_state(TASK_UNINTERRUPTIBLE);
|
|
|
|
dio->waiter = current;
|
|
|
|
spin_unlock_irqrestore(&dio->bio_lock, flags);
|
2021-10-12 11:12:11 +00:00
|
|
|
blk_io_schedule();
|
2006-12-10 10:21:07 +00:00
|
|
|
/* wake up sets us TASK_RUNNING */
|
|
|
|
spin_lock_irqsave(&dio->bio_lock, flags);
|
|
|
|
dio->waiter = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2006-12-10 10:20:59 +00:00
|
|
|
if (dio->bio_list) {
|
|
|
|
bio = dio->bio_list;
|
|
|
|
dio->bio_list = bio->bi_private;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
spin_unlock_irqrestore(&dio->bio_lock, flags);
|
|
|
|
return bio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process one completed BIO. No locks are held.
|
|
|
|
*/
|
2017-06-03 07:38:06 +00:00
|
|
|
static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2017-06-03 07:38:06 +00:00
|
|
|
blk_status_t err = bio->bi_status;
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
|
|
|
bool should_dirty = dio_op == REQ_OP_READ && dio->should_dirty;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-06-20 12:05:46 +00:00
|
|
|
if (err) {
|
|
|
|
if (err == BLK_STS_AGAIN && (bio->bi_opf & REQ_NOWAIT))
|
|
|
|
dio->io_error = -EAGAIN;
|
|
|
|
else
|
|
|
|
dio->io_error = -EIO;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2019-06-26 13:49:27 +00:00
|
|
|
if (dio->is_async && should_dirty) {
|
2016-01-30 16:09:59 +00:00
|
|
|
bio_check_pages_dirty(bio); /* transfers ownership */
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
2019-06-26 13:49:27 +00:00
|
|
|
bio_release_pages(bio, should_dirty);
|
2005-04-16 22:20:36 +00:00
|
|
|
bio_put(bio);
|
|
|
|
}
|
2015-08-10 23:05:18 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-12-10 10:20:59 +00:00
|
|
|
* Wait on and process all in-flight BIOs. This must only be called once
|
|
|
|
* all bios have been issued so that the refcount can only decrease.
|
|
|
|
* This just waits for all bios to make it through dio_bio_complete. IO
|
2007-05-09 05:14:03 +00:00
|
|
|
* errors are propagated through dio->io_error and should be propagated via
|
2006-12-10 10:20:59 +00:00
|
|
|
* dio_complete().
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2006-12-10 10:20:54 +00:00
|
|
|
static void dio_await_completion(struct dio *dio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-12-10 10:20:59 +00:00
|
|
|
struct bio *bio;
|
|
|
|
do {
|
|
|
|
bio = dio_await_one(dio);
|
|
|
|
if (bio)
|
|
|
|
dio_bio_complete(dio, bio);
|
|
|
|
} while (bio);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A really large O_DIRECT read or write can generate a lot of BIOs. So
|
|
|
|
* to keep the memory consumption sane we periodically reap any completed BIOs
|
|
|
|
* during the BIO generation phase.
|
|
|
|
*
|
|
|
|
* This also helps to limit the peak amount of pinned userspace memory.
|
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->reap_counter++ >= 64) {
|
2005-04-16 22:20:36 +00:00
|
|
|
while (dio->bio_list) {
|
|
|
|
unsigned long flags;
|
|
|
|
struct bio *bio;
|
|
|
|
int ret2;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dio->bio_lock, flags);
|
|
|
|
bio = dio->bio_list;
|
|
|
|
dio->bio_list = bio->bi_private;
|
|
|
|
spin_unlock_irqrestore(&dio->bio_lock, flags);
|
2017-06-03 07:38:06 +00:00
|
|
|
ret2 = blk_status_to_errno(dio_bio_complete(dio, bio));
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret == 0)
|
|
|
|
ret = ret2;
|
|
|
|
}
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->reap_counter = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-04 13:04:39 +00:00
|
|
|
static int dio_set_defer_completion(struct dio *dio)
|
|
|
|
{
|
|
|
|
struct super_block *sb = dio->inode->i_sb;
|
|
|
|
|
|
|
|
if (dio->defer_completion)
|
|
|
|
return 0;
|
|
|
|
dio->defer_completion = true;
|
|
|
|
if (!sb->s_dio_done_wq)
|
|
|
|
return sb_init_dio_done_wq(sb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Call into the fs to map some more disk blocks. We record the current number
|
2011-08-02 04:38:03 +00:00
|
|
|
* of available blocks at sdio->blocks_available. These are in units of the
|
2017-02-27 22:28:32 +00:00
|
|
|
* fs blocksize, i_blocksize(inode).
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* The fs is allowed to map lots of blocks at once. If it wants to do that,
|
|
|
|
* it uses the passed inode-relative block number as the file offset, as usual.
|
|
|
|
*
|
2006-03-26 09:38:02 +00:00
|
|
|
* get_block() is passed the number of i_blkbits-sized blocks which direct_io
|
2005-04-16 22:20:36 +00:00
|
|
|
* has remaining to do. The fs should not map more than this number of blocks.
|
|
|
|
*
|
|
|
|
* If the fs has mapped a lot of blocks, it should populate bh->b_size to
|
|
|
|
* indicate how much contiguous disk space has been made available at
|
|
|
|
* bh->b_blocknr.
|
|
|
|
*
|
|
|
|
* If *any* of the mapped blocks are new, then the fs must set buffer_new().
|
|
|
|
* This isn't very efficient...
|
|
|
|
*
|
|
|
|
* In the case of filesystem holes: the fs may return an arbitrarily-large
|
|
|
|
* hole by returning an appropriate value in b_size and by clearing
|
|
|
|
* buffer_mapped(). However the direct-io code will only process holes one
|
2006-03-26 09:38:02 +00:00
|
|
|
* block at a time - it will repeatedly call get_block() as it walks the hole.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2011-08-02 04:38:07 +00:00
|
|
|
static int get_more_blocks(struct dio *dio, struct dio_submit *sdio,
|
|
|
|
struct buffer_head *map_bh)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
2005-04-16 22:20:36 +00:00
|
|
|
int ret;
|
|
|
|
sector_t fs_startblk; /* Into file, in filesystem-sized blocks */
|
2012-01-13 01:20:33 +00:00
|
|
|
sector_t fs_endblk; /* Into file, in filesystem-sized blocks */
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long fs_count; /* Number of filesystem-sized blocks */
|
|
|
|
int create;
|
2012-11-29 20:27:00 +00:00
|
|
|
unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor;
|
2018-10-08 23:58:23 +00:00
|
|
|
loff_t i_size;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there was a memory error and we've overwritten all the
|
|
|
|
* mapped blocks then we can now return that memory error
|
|
|
|
*/
|
|
|
|
ret = dio->page_errors;
|
|
|
|
if (ret == 0) {
|
2011-08-02 04:38:03 +00:00
|
|
|
BUG_ON(sdio->block_in_file >= sdio->final_block_in_request);
|
|
|
|
fs_startblk = sdio->block_in_file >> sdio->blkfactor;
|
2012-01-13 01:20:33 +00:00
|
|
|
fs_endblk = (sdio->final_block_in_request - 1) >>
|
|
|
|
sdio->blkfactor;
|
|
|
|
fs_count = fs_endblk - fs_startblk + 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-03-28 23:26:15 +00:00
|
|
|
map_bh->b_state = 0;
|
2012-11-29 20:27:00 +00:00
|
|
|
map_bh->b_size = fs_count << i_blkbits;
|
2006-03-28 23:26:15 +00:00
|
|
|
|
direct-io: cleanup blockdev_direct_IO locking
Currently the locking in blockdev_direct_IO is a mess, we have three
different locking types and very confusing checks for some of them. The
most complicated one is DIO_OWN_LOCKING for reads, which happens to not
actually be used.
This patch gets rid of the DIO_OWN_LOCKING - as mentioned above the read
case is unused anyway, and the write side is almost identical to
DIO_NO_LOCKING. The difference is that DIO_NO_LOCKING always sets the
create argument for the get_blocks callback to zero, but we can easily
move that to the actual get_blocks callbacks. There are four users of the
DIO_NO_LOCKING mode: gfs already ignores the create argument and thus is
fine with the new version, ocfs2 only errors out if create were ever set,
and we can remove this dead code now, the block device code only ever uses
create for an error message if we are fully beyond the device which can
never happen, and last but not least XFS will need the new behavour for
writes.
Now we can replace the lock_type variable with a flags one, where no flag
means the DIO_NO_LOCKING behaviour and DIO_LOCKING is kept as the first
flag. Separate out the check for not allowing to fill holes into a
separate flag, although for now both flags always get set at the same
time.
Also revamp the documentation of the locking scheme to actually make
sense.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Christoph Hellwig <hch@lst.de>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Jeff Moyer <jmoyer@redhat.com>
Cc: Jens Axboe <jens.axboe@oracle.com>
Cc: Zach Brown <zach.brown@oracle.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Alex Elder <aelder@sgi.com>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2009-12-16 00:47:50 +00:00
|
|
|
/*
|
direct-io: fix direct write stale data exposure from concurrent buffered read
Currently direct writes inside i_size on a DIO_SKIP_HOLES filesystem are
not allowed to allocate blocks(get_more_blocks() sets 'create' to 0
before calling get_block() callback), if it's a sparse file, direct
writes fall back to buffered writes to avoid stale data exposure from
concurrent buffered read. But there're two cases that can result in
stale data exposure are not correctly detected.
1. The detection for "writing inside i_size" is not sufficient,
writes can be treated as "extending writes" wrongly. For example,
direct write 1FSB (file system block) to a 1FSB sparse file on
ext2/3/4, starting from offset 0, in this case it's writing inside
i_size, but 'create' is non-zero, because 'block_in_file' and
'(i_size_read(inode) >> blkbits' are both zero.
2. Direct writes starting from or beyong i_size (not inside i_size)
also could trigger block allocation and expose stale data. For
example, consider a sparse file with i_size of 2k, and a write to
offset 2k or 3k into the file, with a filesystem block size of 4k.
(Thanks to Jeff Moyer for pointing this case out in his review.)
The first problem can be demostrated by running ltp-aiodio test ADSP045
many times. When testing on extN filesystems, I see test failures
occasionally, buffered read could read non-zero (stale) data.
ADSP045: dio_sparse -a 4k -w 4k -s 2k -n 1
dio_sparse 0 TINFO : Dirtying free blocks
dio_sparse 0 TINFO : Starting I/O tests
non zero buffer at buf[0] => 0xffffffaa,ffffffaa,ffffffaa,ffffffaa
non-zero read at offset 0
dio_sparse 0 TINFO : Killing childrens(s)
dio_sparse 1 TFAIL : dio_sparse.c:191: 1 children(s) exited abnormally
The second problem can also be reproduced easily by a hacked dio_sparse
program, which accepts an option to specify the write offset.
What we should really do is to disable block allocation for writes that
could result in filling holes inside i_size.
Link: http://lkml.kernel.org/r/1463156728-13357-1-git-send-email-guaneryu@gmail.com
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-05-27 21:27:18 +00:00
|
|
|
* For writes that could fill holes inside i_size on a
|
|
|
|
* DIO_SKIP_HOLES filesystem we forbid block creations: only
|
|
|
|
* overwrites are permitted. We will return early to the caller
|
|
|
|
* once we see an unmapped buffer head returned, and the caller
|
|
|
|
* will fall back to buffered I/O.
|
direct-io: cleanup blockdev_direct_IO locking
Currently the locking in blockdev_direct_IO is a mess, we have three
different locking types and very confusing checks for some of them. The
most complicated one is DIO_OWN_LOCKING for reads, which happens to not
actually be used.
This patch gets rid of the DIO_OWN_LOCKING - as mentioned above the read
case is unused anyway, and the write side is almost identical to
DIO_NO_LOCKING. The difference is that DIO_NO_LOCKING always sets the
create argument for the get_blocks callback to zero, but we can easily
move that to the actual get_blocks callbacks. There are four users of the
DIO_NO_LOCKING mode: gfs already ignores the create argument and thus is
fine with the new version, ocfs2 only errors out if create were ever set,
and we can remove this dead code now, the block device code only ever uses
create for an error message if we are fully beyond the device which can
never happen, and last but not least XFS will need the new behavour for
writes.
Now we can replace the lock_type variable with a flags one, where no flag
means the DIO_NO_LOCKING behaviour and DIO_LOCKING is kept as the first
flag. Separate out the check for not allowing to fill holes into a
separate flag, although for now both flags always get set at the same
time.
Also revamp the documentation of the locking scheme to actually make
sense.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Christoph Hellwig <hch@lst.de>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Jeff Moyer <jmoyer@redhat.com>
Cc: Jens Axboe <jens.axboe@oracle.com>
Cc: Zach Brown <zach.brown@oracle.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Alex Elder <aelder@sgi.com>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2009-12-16 00:47:50 +00:00
|
|
|
*
|
|
|
|
* Otherwise the decision is left to the get_blocks method,
|
|
|
|
* which may decide to handle it or also return an unmapped
|
|
|
|
* buffer head.
|
|
|
|
*/
|
2022-07-14 18:07:14 +00:00
|
|
|
create = dio_op == REQ_OP_WRITE;
|
direct-io: cleanup blockdev_direct_IO locking
Currently the locking in blockdev_direct_IO is a mess, we have three
different locking types and very confusing checks for some of them. The
most complicated one is DIO_OWN_LOCKING for reads, which happens to not
actually be used.
This patch gets rid of the DIO_OWN_LOCKING - as mentioned above the read
case is unused anyway, and the write side is almost identical to
DIO_NO_LOCKING. The difference is that DIO_NO_LOCKING always sets the
create argument for the get_blocks callback to zero, but we can easily
move that to the actual get_blocks callbacks. There are four users of the
DIO_NO_LOCKING mode: gfs already ignores the create argument and thus is
fine with the new version, ocfs2 only errors out if create were ever set,
and we can remove this dead code now, the block device code only ever uses
create for an error message if we are fully beyond the device which can
never happen, and last but not least XFS will need the new behavour for
writes.
Now we can replace the lock_type variable with a flags one, where no flag
means the DIO_NO_LOCKING behaviour and DIO_LOCKING is kept as the first
flag. Separate out the check for not allowing to fill holes into a
separate flag, although for now both flags always get set at the same
time.
Also revamp the documentation of the locking scheme to actually make
sense.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Christoph Hellwig <hch@lst.de>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Jeff Moyer <jmoyer@redhat.com>
Cc: Jens Axboe <jens.axboe@oracle.com>
Cc: Zach Brown <zach.brown@oracle.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Alex Elder <aelder@sgi.com>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2009-12-16 00:47:50 +00:00
|
|
|
if (dio->flags & DIO_SKIP_HOLES) {
|
2018-10-08 23:58:23 +00:00
|
|
|
i_size = i_size_read(dio->inode);
|
|
|
|
if (i_size && fs_startblk <= (i_size - 1) >> i_blkbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
create = 0;
|
|
|
|
}
|
2006-03-28 23:26:15 +00:00
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
ret = (*sdio->get_block)(dio->inode, fs_startblk,
|
2005-04-16 22:20:36 +00:00
|
|
|
map_bh, create);
|
2011-08-02 04:38:07 +00:00
|
|
|
|
|
|
|
/* Store for completion */
|
|
|
|
dio->private = map_bh->b_private;
|
2013-09-04 13:04:39 +00:00
|
|
|
|
|
|
|
if (ret == 0 && buffer_defer_completion(map_bh))
|
|
|
|
ret = dio_set_defer_completion(dio);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is no bio. Make one now.
|
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
|
|
|
|
sector_t start_sector, struct buffer_head *map_bh)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
sector_t sector;
|
|
|
|
int ret, nr_pages;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
ret = dio_bio_reap(dio, sdio);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
2011-08-02 04:38:03 +00:00
|
|
|
sector = start_sector << (sdio->blkbits - 9);
|
2021-01-29 04:38:57 +00:00
|
|
|
nr_pages = bio_max_segs(sdio->pages_in_io);
|
2005-04-16 22:20:36 +00:00
|
|
|
BUG_ON(nr_pages <= 0);
|
2011-08-02 04:38:07 +00:00
|
|
|
dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->boundary = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to put the current chunk of 'cur_page' into the current BIO. If
|
|
|
|
* that was successful then update final_block_in_bio and take a ref against
|
|
|
|
* the just-added page.
|
|
|
|
*
|
|
|
|
* Return zero on success. Non-zero means the caller needs to start a new BIO.
|
|
|
|
*/
|
2023-05-26 21:41:42 +00:00
|
|
|
static inline int dio_bio_add_page(struct dio *dio, struct dio_submit *sdio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
ret = bio_add_page(sdio->bio, sdio->cur_page,
|
|
|
|
sdio->cur_page_len, sdio->cur_page_offset);
|
|
|
|
if (ret == sdio->cur_page_len) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Decrement count only, if we are done with this page
|
|
|
|
*/
|
2011-08-02 04:38:03 +00:00
|
|
|
if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE)
|
|
|
|
sdio->pages_in_io--;
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_pin_page(dio, sdio->cur_page);
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->final_block_in_bio = sdio->cur_page_block +
|
|
|
|
(sdio->cur_page_len >> sdio->blkbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put cur_page under IO. The section of cur_page which is described by
|
|
|
|
* cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
|
|
|
|
* starts on-disk at cur_page_block.
|
|
|
|
*
|
|
|
|
* We take a ref against the page here (on behalf of its presence in the bio).
|
|
|
|
*
|
|
|
|
* The caller of this function is responsible for removing cur_page from the
|
|
|
|
* dio, and for dropping the refcount which came from that presence.
|
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio,
|
|
|
|
struct buffer_head *map_bh)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->bio) {
|
|
|
|
loff_t cur_offset = sdio->cur_page_fs_offset;
|
|
|
|
loff_t bio_next_offset = sdio->logical_offset_in_bio +
|
2013-10-11 22:44:27 +00:00
|
|
|
sdio->bio->bi_iter.bi_size;
|
2010-05-23 15:00:55 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2010-05-23 15:00:55 +00:00
|
|
|
* See whether this new request is contiguous with the old.
|
|
|
|
*
|
2011-01-11 12:15:03 +00:00
|
|
|
* Btrfs cannot handle having logically non-contiguous requests
|
|
|
|
* submitted. For example if you have
|
2010-05-23 15:00:55 +00:00
|
|
|
*
|
|
|
|
* Logical: [0-4095][HOLE][8192-12287]
|
2011-01-11 12:15:03 +00:00
|
|
|
* Physical: [0-4095] [4096-8191]
|
2010-05-23 15:00:55 +00:00
|
|
|
*
|
|
|
|
* We cannot submit those pages together as one BIO. So if our
|
|
|
|
* current logical offset in the file does not equal what would
|
|
|
|
* be the next logical offset in the bio, submit the bio we
|
|
|
|
* have.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->final_block_in_bio != sdio->cur_page_block ||
|
2010-05-23 15:00:55 +00:00
|
|
|
cur_offset != bio_next_offset)
|
2011-08-02 04:38:03 +00:00
|
|
|
dio_bio_submit(dio, sdio);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->bio == NULL) {
|
2011-08-02 04:38:07 +00:00
|
|
|
ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:41:42 +00:00
|
|
|
if (dio_bio_add_page(dio, sdio) != 0) {
|
2011-08-02 04:38:03 +00:00
|
|
|
dio_bio_submit(dio, sdio);
|
2011-08-02 04:38:07 +00:00
|
|
|
ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret == 0) {
|
2023-05-26 21:41:42 +00:00
|
|
|
ret = dio_bio_add_page(dio, sdio);
|
2005-04-16 22:20:36 +00:00
|
|
|
BUG_ON(ret != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An autonomous function to put a chunk of a page under deferred IO.
|
|
|
|
*
|
|
|
|
* The caller doesn't actually know (or care) whether this piece of page is in
|
|
|
|
* a BIO, or is under IO or whatever. We just take care of all possible
|
|
|
|
* situations here. The separation between the logic of do_direct_IO() and
|
|
|
|
* that of submit_page_section() is important for clarity. Please don't break.
|
|
|
|
*
|
|
|
|
* The chunk of page starts on-disk at blocknr.
|
|
|
|
*
|
|
|
|
* We perform deferred IO, by recording the last-submitted page inside our
|
|
|
|
* private part of the dio structure. If possible, we just expand the IO
|
|
|
|
* across that page here.
|
|
|
|
*
|
|
|
|
* If that doesn't work out then we put the old page into the bio and add this
|
|
|
|
* page to the dio instead.
|
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline int
|
2011-08-02 04:38:03 +00:00
|
|
|
submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page,
|
2011-08-02 04:38:07 +00:00
|
|
|
unsigned offset, unsigned len, sector_t blocknr,
|
|
|
|
struct buffer_head *map_bh)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
2005-04-16 22:20:36 +00:00
|
|
|
int ret = 0;
|
2021-04-09 20:27:35 +00:00
|
|
|
int boundary = sdio->boundary; /* dio_send_cur_page may clear it */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2022-07-14 18:07:14 +00:00
|
|
|
if (dio_op == REQ_OP_WRITE) {
|
2006-12-10 10:19:47 +00:00
|
|
|
/*
|
|
|
|
* Read accounting is performed in submit_bio()
|
|
|
|
*/
|
|
|
|
task_io_account_write(len);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Can we just grow the current page's presence in the dio?
|
|
|
|
*/
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->cur_page == page &&
|
|
|
|
sdio->cur_page_offset + sdio->cur_page_len == offset &&
|
|
|
|
sdio->cur_page_block +
|
|
|
|
(sdio->cur_page_len >> sdio->blkbits) == blocknr) {
|
|
|
|
sdio->cur_page_len += len;
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's a deferred page already there then send it.
|
|
|
|
*/
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->cur_page) {
|
2011-08-02 04:38:07 +00:00
|
|
|
ret = dio_send_cur_page(dio, sdio, map_bh);
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_unpin_page(dio, sdio->cur_page);
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->cur_page = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret)
|
2013-04-29 22:06:18 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_pin_page(dio, page); /* It is in dio */
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->cur_page = page;
|
|
|
|
sdio->cur_page_offset = offset;
|
|
|
|
sdio->cur_page_len = len;
|
|
|
|
sdio->cur_page_block = blocknr;
|
|
|
|
sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits;
|
2005-04-16 22:20:36 +00:00
|
|
|
out:
|
2013-04-29 22:06:18 +00:00
|
|
|
/*
|
2021-04-09 20:27:35 +00:00
|
|
|
* If boundary then we want to schedule the IO now to
|
2013-04-29 22:06:18 +00:00
|
|
|
* avoid metadata seeks.
|
|
|
|
*/
|
2021-04-09 20:27:35 +00:00
|
|
|
if (boundary) {
|
2013-04-29 22:06:18 +00:00
|
|
|
ret = dio_send_cur_page(dio, sdio, map_bh);
|
2017-10-09 09:13:18 +00:00
|
|
|
if (sdio->bio)
|
|
|
|
dio_bio_submit(dio, sdio);
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_unpin_page(dio, sdio->cur_page);
|
2013-04-29 22:06:18 +00:00
|
|
|
sdio->cur_page = NULL;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are not writing the entire block and get_block() allocated
|
|
|
|
* the block for us, we need to fill-in the unused portion of the
|
|
|
|
* block with zeros. This happens only if user-buffer, fileoffset or
|
|
|
|
* io length is not filesystem block-size multiple.
|
|
|
|
*
|
|
|
|
* `end' is zero if we're doing the start of the IO, 1 at the end of the
|
|
|
|
* IO.
|
|
|
|
*/
|
2011-08-02 04:38:08 +00:00
|
|
|
static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio,
|
|
|
|
int end, struct buffer_head *map_bh)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
unsigned dio_blocks_per_fs_block;
|
|
|
|
unsigned this_chunk_blocks; /* In dio_blocks */
|
|
|
|
unsigned this_chunk_bytes;
|
|
|
|
struct page *page;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->start_zero_done = 1;
|
2011-08-02 04:38:07 +00:00
|
|
|
if (!sdio->blkfactor || !buffer_new(map_bh))
|
2005-04-16 22:20:36 +00:00
|
|
|
return;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
dio_blocks_per_fs_block = 1 << sdio->blkfactor;
|
|
|
|
this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (!this_chunk_blocks)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to zero out part of an fs block. It is either at the
|
|
|
|
* beginning or the end of the fs block.
|
|
|
|
*/
|
|
|
|
if (end)
|
|
|
|
this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
this_chunk_bytes = this_chunk_blocks << sdio->blkbits;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
remove ZERO_PAGE
The commit b5810039a54e5babf428e9a1e89fc1940fabff11 contains the note
A last caveat: the ZERO_PAGE is now refcounted and managed with rmap
(and thus mapcounted and count towards shared rss). These writes to
the struct page could cause excessive cacheline bouncing on big
systems. There are a number of ways this could be addressed if it is
an issue.
And indeed this cacheline bouncing has shown up on large SGI systems.
There was a situation where an Altix system was essentially livelocked
tearing down ZERO_PAGE pagetables when an HPC app aborted during startup.
This situation can be avoided in userspace, but it does highlight the
potential scalability problem with refcounting ZERO_PAGE, and corner
cases where it can really hurt (we don't want the system to livelock!).
There are several broad ways to fix this problem:
1. add back some special casing to avoid refcounting ZERO_PAGE
2. per-node or per-cpu ZERO_PAGES
3. remove the ZERO_PAGE completely
I will argue for 3. The others should also fix the problem, but they
result in more complex code than does 3, with little or no real benefit
that I can see.
Why? Inserting a ZERO_PAGE for anonymous read faults appears to be a
false optimisation: if an application is performance critical, it would
not be doing many read faults of new memory, or at least it could be
expected to write to that memory soon afterwards. If cache or memory use
is critical, it should not be working with a significant number of
ZERO_PAGEs anyway (a more compact representation of zeroes should be
used).
As a sanity check -- mesuring on my desktop system, there are never many
mappings to the ZERO_PAGE (eg. 2 or 3), thus memory usage here should not
increase much without it.
When running a make -j4 kernel compile on my dual core system, there are
about 1,000 mappings to the ZERO_PAGE created per second, but about 1,000
ZERO_PAGE COW faults per second (less than 1 ZERO_PAGE mapping per second
is torn down without being COWed). So removing ZERO_PAGE will save 1,000
page faults per second when running kbuild, while keeping it only saves
less than 1 page clearing operation per second. 1 page clear is cheaper
than a thousand faults, presumably, so there isn't an obvious loss.
Neither the logical argument nor these basic tests give a guarantee of no
regressions. However, this is a reasonable opportunity to try to remove
the ZERO_PAGE from the pagefault path. If it is found to cause regressions,
we can reintroduce it and just avoid refcounting it.
The /dev/zero ZERO_PAGE usage and TLB tricks also get nuked. I don't see
much use to them except on benchmarks. All other users of ZERO_PAGE are
converted just to use ZERO_PAGE(0) for simplicity. We can look at
replacing them all and maybe ripping out ZERO_PAGE completely when we are
more satisfied with this solution.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus "snif" Torvalds <torvalds@linux-foundation.org>
2007-10-16 08:24:40 +00:00
|
|
|
page = ZERO_PAGE(0);
|
2011-08-02 04:38:03 +00:00
|
|
|
if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes,
|
2011-08-02 04:38:07 +00:00
|
|
|
sdio->next_block_for_io, map_bh))
|
2005-04-16 22:20:36 +00:00
|
|
|
return;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->next_block_for_io += this_chunk_blocks;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the user pages, and the file, mapping blocks to disk and generating
|
|
|
|
* a sequence of (page,offset,len,block) mappings. These mappings are injected
|
|
|
|
* into submit_page_section(), which takes care of the next stage of submission
|
|
|
|
*
|
|
|
|
* Direct IO against a blockdev is different from a file. Because we can
|
|
|
|
* happily perform page-sized but 512-byte aligned IOs. It is important that
|
|
|
|
* blockdev IO be able to have fine alignment and large sizes.
|
|
|
|
*
|
2006-03-26 09:38:02 +00:00
|
|
|
* So what we do is to permit the ->get_block function to populate bh.b_size
|
2005-04-16 22:20:36 +00:00
|
|
|
* with the size of IO which is permitted at this offset and this i_blkbits.
|
|
|
|
*
|
|
|
|
* For best results, the blockdev should be set up with 512-byte i_blkbits and
|
2006-03-26 09:38:02 +00:00
|
|
|
* it should set b_size to PAGE_SIZE or more inside get_block(). This gives
|
2005-04-16 22:20:36 +00:00
|
|
|
* fine alignment but still allows this function to work in PAGE_SIZE units.
|
|
|
|
*/
|
2011-08-02 04:38:07 +00:00
|
|
|
static int do_direct_IO(struct dio *dio, struct dio_submit *sdio,
|
|
|
|
struct buffer_head *map_bh)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2022-07-14 18:07:14 +00:00
|
|
|
const enum req_op dio_op = dio->opf & REQ_OP_MASK;
|
2011-08-02 04:38:03 +00:00
|
|
|
const unsigned blkbits = sdio->blkbits;
|
2017-01-10 20:29:54 +00:00
|
|
|
const unsigned i_blkbits = blkbits + sdio->blkfactor;
|
2005-04-16 22:20:36 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
while (sdio->block_in_file < sdio->final_block_in_request) {
|
2014-03-15 08:05:57 +00:00
|
|
|
struct page *page;
|
|
|
|
size_t from, to;
|
2014-07-20 09:09:04 +00:00
|
|
|
|
|
|
|
page = dio_get_page(dio, sdio);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (IS_ERR(page)) {
|
|
|
|
ret = PTR_ERR(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-20 09:09:04 +00:00
|
|
|
from = sdio->head ? 0 : sdio->from;
|
|
|
|
to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE;
|
|
|
|
sdio->head++;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2014-03-15 08:05:57 +00:00
|
|
|
while (from < to) {
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned this_chunk_bytes; /* # of bytes mapped */
|
|
|
|
unsigned this_chunk_blocks; /* # of blocks */
|
|
|
|
unsigned u;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->blocks_available == 0) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Need to go and map some more disk
|
|
|
|
*/
|
|
|
|
unsigned long blkmask;
|
|
|
|
unsigned long dio_remainder;
|
|
|
|
|
2011-08-02 04:38:07 +00:00
|
|
|
ret = get_more_blocks(dio, sdio, map_bh);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret) {
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_unpin_page(dio, page);
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!buffer_mapped(map_bh))
|
|
|
|
goto do_holes;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->blocks_available =
|
2016-11-04 17:08:12 +00:00
|
|
|
map_bh->b_size >> blkbits;
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->next_block_for_io =
|
|
|
|
map_bh->b_blocknr << sdio->blkfactor;
|
2016-11-04 17:08:12 +00:00
|
|
|
if (buffer_new(map_bh)) {
|
|
|
|
clean_bdev_aliases(
|
|
|
|
map_bh->b_bdev,
|
|
|
|
map_bh->b_blocknr,
|
2017-01-10 20:29:54 +00:00
|
|
|
map_bh->b_size >> i_blkbits);
|
2016-11-04 17:08:12 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
if (!sdio->blkfactor)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto do_holes;
|
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
blkmask = (1 << sdio->blkfactor) - 1;
|
|
|
|
dio_remainder = (sdio->block_in_file & blkmask);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are at the start of IO and that IO
|
|
|
|
* starts partway into a fs-block,
|
|
|
|
* dio_remainder will be non-zero. If the IO
|
|
|
|
* is a read then we can simply advance the IO
|
|
|
|
* cursor to the first block which is to be
|
|
|
|
* read. But if the IO is a write and the
|
|
|
|
* block was newly allocated we cannot do that;
|
|
|
|
* the start of the fs block must be zeroed out
|
|
|
|
* on-disk
|
|
|
|
*/
|
|
|
|
if (!buffer_new(map_bh))
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->next_block_for_io += dio_remainder;
|
|
|
|
sdio->blocks_available -= dio_remainder;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
do_holes:
|
|
|
|
/* Handle holes */
|
|
|
|
if (!buffer_mapped(map_bh)) {
|
[PATCH] fix O_DIRECT read of last block in a sparse file
Currently, if you open a file O_DIRECT, truncate it to a size that is not a
multiple of the disk block size, and then try to read the last block in the
file, the read will return 0. The problem is in do_direct_IO, here:
/* Handle holes */
if (!buffer_mapped(map_bh)) {
char *kaddr;
...
if (dio->block_in_file >=
i_size_read(dio->inode)>>blkbits) {
/* We hit eof */
page_cache_release(page);
goto out;
}
We shift off any remaining bytes in the final block of the I/O, resulting
in a 0-sized read. I've attached a patch that fixes this. I'm not happy
about how ugly the math is getting, so suggestions are more than welcome.
I've tested this with a simple program that performs the steps outlined for
reproducing the problem above. Without the patch, we get a 0-sized result
from read. With the patch, we get the correct return value from the short
read.
Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Suparna Bhattacharya <suparna@in.ibm.com>
Cc: Mingming Cao <cmm@us.ibm.com>
Cc: Joel Becker <Joel.Becker@oracle.com>
Cc: "Chen, Kenneth W" <kenneth.w.chen@intel.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-02-03 11:04:27 +00:00
|
|
|
loff_t i_size_aligned;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* AKPM: eargh, -ENOTBLK is a hack */
|
2022-07-14 18:07:14 +00:00
|
|
|
if (dio_op == REQ_OP_WRITE) {
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_unpin_page(dio, page);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOTBLK;
|
|
|
|
}
|
|
|
|
|
[PATCH] fix O_DIRECT read of last block in a sparse file
Currently, if you open a file O_DIRECT, truncate it to a size that is not a
multiple of the disk block size, and then try to read the last block in the
file, the read will return 0. The problem is in do_direct_IO, here:
/* Handle holes */
if (!buffer_mapped(map_bh)) {
char *kaddr;
...
if (dio->block_in_file >=
i_size_read(dio->inode)>>blkbits) {
/* We hit eof */
page_cache_release(page);
goto out;
}
We shift off any remaining bytes in the final block of the I/O, resulting
in a 0-sized read. I've attached a patch that fixes this. I'm not happy
about how ugly the math is getting, so suggestions are more than welcome.
I've tested this with a simple program that performs the steps outlined for
reproducing the problem above. Without the patch, we get a 0-sized result
from read. With the patch, we get the correct return value from the short
read.
Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Suparna Bhattacharya <suparna@in.ibm.com>
Cc: Mingming Cao <cmm@us.ibm.com>
Cc: Joel Becker <Joel.Becker@oracle.com>
Cc: "Chen, Kenneth W" <kenneth.w.chen@intel.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-02-03 11:04:27 +00:00
|
|
|
/*
|
|
|
|
* Be sure to account for a partial block as the
|
|
|
|
* last block in the file
|
|
|
|
*/
|
|
|
|
i_size_aligned = ALIGN(i_size_read(dio->inode),
|
|
|
|
1 << blkbits);
|
2011-08-02 04:38:03 +00:00
|
|
|
if (sdio->block_in_file >=
|
[PATCH] fix O_DIRECT read of last block in a sparse file
Currently, if you open a file O_DIRECT, truncate it to a size that is not a
multiple of the disk block size, and then try to read the last block in the
file, the read will return 0. The problem is in do_direct_IO, here:
/* Handle holes */
if (!buffer_mapped(map_bh)) {
char *kaddr;
...
if (dio->block_in_file >=
i_size_read(dio->inode)>>blkbits) {
/* We hit eof */
page_cache_release(page);
goto out;
}
We shift off any remaining bytes in the final block of the I/O, resulting
in a 0-sized read. I've attached a patch that fixes this. I'm not happy
about how ugly the math is getting, so suggestions are more than welcome.
I've tested this with a simple program that performs the steps outlined for
reproducing the problem above. Without the patch, we get a 0-sized result
from read. With the patch, we get the correct return value from the short
read.
Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Suparna Bhattacharya <suparna@in.ibm.com>
Cc: Mingming Cao <cmm@us.ibm.com>
Cc: Joel Becker <Joel.Becker@oracle.com>
Cc: "Chen, Kenneth W" <kenneth.w.chen@intel.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-02-03 11:04:27 +00:00
|
|
|
i_size_aligned >> blkbits) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/* We hit eof */
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_unpin_page(dio, page);
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-03-15 08:05:57 +00:00
|
|
|
zero_user(page, from, 1 << blkbits);
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->block_in_file++;
|
2014-03-15 08:05:57 +00:00
|
|
|
from += 1 << blkbits;
|
2014-03-10 06:30:55 +00:00
|
|
|
dio->result += 1 << blkbits;
|
2005-04-16 22:20:36 +00:00
|
|
|
goto next_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're performing IO which has an alignment which
|
|
|
|
* is finer than the underlying fs, go check to see if
|
|
|
|
* we must zero out the start of this block.
|
|
|
|
*/
|
2011-08-02 04:38:03 +00:00
|
|
|
if (unlikely(sdio->blkfactor && !sdio->start_zero_done))
|
2011-08-02 04:38:07 +00:00
|
|
|
dio_zero_block(dio, sdio, 0, map_bh);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Work out, in this_chunk_blocks, how much disk we
|
|
|
|
* can add to this page
|
|
|
|
*/
|
2011-08-02 04:38:03 +00:00
|
|
|
this_chunk_blocks = sdio->blocks_available;
|
2014-03-15 08:05:57 +00:00
|
|
|
u = (to - from) >> blkbits;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (this_chunk_blocks > u)
|
|
|
|
this_chunk_blocks = u;
|
2011-08-02 04:38:03 +00:00
|
|
|
u = sdio->final_block_in_request - sdio->block_in_file;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (this_chunk_blocks > u)
|
|
|
|
this_chunk_blocks = u;
|
|
|
|
this_chunk_bytes = this_chunk_blocks << blkbits;
|
|
|
|
BUG_ON(this_chunk_bytes == 0);
|
|
|
|
|
2013-04-29 22:06:17 +00:00
|
|
|
if (this_chunk_blocks == sdio->blocks_available)
|
|
|
|
sdio->boundary = buffer_boundary(map_bh);
|
2011-08-02 04:38:03 +00:00
|
|
|
ret = submit_page_section(dio, sdio, page,
|
2014-03-15 08:05:57 +00:00
|
|
|
from,
|
2011-08-02 04:38:03 +00:00
|
|
|
this_chunk_bytes,
|
2011-08-02 04:38:07 +00:00
|
|
|
sdio->next_block_for_io,
|
|
|
|
map_bh);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ret) {
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_unpin_page(dio, page);
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->next_block_for_io += this_chunk_blocks;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->block_in_file += this_chunk_blocks;
|
2014-03-15 08:05:57 +00:00
|
|
|
from += this_chunk_bytes;
|
|
|
|
dio->result += this_chunk_bytes;
|
2011-08-02 04:38:03 +00:00
|
|
|
sdio->blocks_available -= this_chunk_blocks;
|
2005-04-16 22:20:36 +00:00
|
|
|
next_block:
|
2011-08-02 04:38:03 +00:00
|
|
|
BUG_ON(sdio->block_in_file > sdio->final_block_in_request);
|
|
|
|
if (sdio->block_in_file == sdio->final_block_in_request)
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:41:42 +00:00
|
|
|
/* Drop the pin which was taken in get_user_pages() */
|
|
|
|
dio_unpin_page(dio, page);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-02 04:38:09 +00:00
|
|
|
static inline int drop_refcount(struct dio *dio)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2011-08-02 04:38:09 +00:00
|
|
|
int ret2;
|
2006-12-10 10:21:07 +00:00
|
|
|
unsigned long flags;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-12-10 10:21:05 +00:00
|
|
|
/*
|
|
|
|
* Sync will always be dropping the final ref and completing the
|
2006-12-10 10:21:07 +00:00
|
|
|
* operation. AIO can if it was a broken operation described above or
|
|
|
|
* in fact if all the bios race to complete before we get here. In
|
|
|
|
* that case dio_complete() translates the EIOCBQUEUED into the proper
|
2015-02-02 13:49:06 +00:00
|
|
|
* return code that the caller will hand to ->complete().
|
2006-12-10 10:21:07 +00:00
|
|
|
*
|
|
|
|
* This is managed by the bio_lock instead of being an atomic_t so that
|
|
|
|
* completion paths can drop their ref and use the remaining count to
|
|
|
|
* decide to wake the submission path atomically.
|
2006-12-10 10:21:05 +00:00
|
|
|
*/
|
2006-12-10 10:21:07 +00:00
|
|
|
spin_lock_irqsave(&dio->bio_lock, flags);
|
|
|
|
ret2 = --dio->refcount;
|
|
|
|
spin_unlock_irqrestore(&dio->bio_lock, flags);
|
2011-08-02 04:38:09 +00:00
|
|
|
return ret2;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2010-06-04 09:29:53 +00:00
|
|
|
/*
|
|
|
|
* This is a library function for use by filesystem drivers.
|
|
|
|
*
|
|
|
|
* The locking rules are governed by the flags parameter:
|
|
|
|
* - if the flags value contains DIO_LOCKING we use a fancy locking
|
|
|
|
* scheme for dumb filesystems.
|
|
|
|
* For writes this function is called under i_mutex and returns with
|
|
|
|
* i_mutex held, for reads, i_mutex is not held on entry, but it is
|
|
|
|
* taken and dropped again before returning.
|
|
|
|
* - if the flags value does NOT contain DIO_LOCKING we don't use any
|
|
|
|
* internal locking but rather rely on the filesystem to synchronize
|
|
|
|
* direct I/O reads/writes versus each other and truncate.
|
2011-06-24 18:29:46 +00:00
|
|
|
*
|
|
|
|
* To help with locking against truncate we incremented the i_dio_count
|
|
|
|
* counter before starting direct I/O, and decrement it once we are done.
|
|
|
|
* Truncate can wait for it to reach zero to provide exclusion. It is
|
|
|
|
* expected that filesystem provide exclusion between new direct I/O
|
|
|
|
* and truncates. For DIO_LOCKING filesystems this is done by i_mutex,
|
|
|
|
* but other filesystems need to take care of this on their own.
|
2011-08-02 04:38:08 +00:00
|
|
|
*
|
|
|
|
* NOTE: if you pass "sdio" to anything by pointer make sure that function
|
|
|
|
* is always inlined. Otherwise gcc is unable to split the structure into
|
|
|
|
* individual fields and will generate much worse code. This is important
|
|
|
|
* for the whole file.
|
2010-06-04 09:29:53 +00:00
|
|
|
*/
|
2022-04-15 04:52:58 +00:00
|
|
|
ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
|
|
|
|
struct block_device *bdev, struct iov_iter *iter,
|
|
|
|
get_block_t get_block, dio_iodone_t end_io,
|
2023-01-20 00:22:22 +00:00
|
|
|
int flags)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()
Please do not apply this to mainline directly, instead please re-run the
coccinelle script shown below and apply its output.
For several reasons, it is desirable to use {READ,WRITE}_ONCE() in
preference to ACCESS_ONCE(), and new code is expected to use one of the
former. So far, there's been no reason to change most existing uses of
ACCESS_ONCE(), as these aren't harmful, and changing them results in
churn.
However, for some features, the read/write distinction is critical to
correct operation. To distinguish these cases, separate read/write
accessors must be used. This patch migrates (most) remaining
ACCESS_ONCE() instances to {READ,WRITE}_ONCE(), using the following
coccinelle script:
----
// Convert trivial ACCESS_ONCE() uses to equivalent READ_ONCE() and
// WRITE_ONCE()
// $ make coccicheck COCCI=/home/mark/once.cocci SPFLAGS="--include-headers" MODE=patch
virtual patch
@ depends on patch @
expression E1, E2;
@@
- ACCESS_ONCE(E1) = E2
+ WRITE_ONCE(E1, E2)
@ depends on patch @
expression E;
@@
- ACCESS_ONCE(E)
+ READ_ONCE(E)
----
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: davem@davemloft.net
Cc: linux-arch@vger.kernel.org
Cc: mpe@ellerman.id.au
Cc: shuah@kernel.org
Cc: snitzer@redhat.com
Cc: thor.thayer@linux.intel.com
Cc: tj@kernel.org
Cc: viro@zeniv.linux.org.uk
Cc: will.deacon@arm.com
Link: http://lkml.kernel.org/r/1508792849-3115-19-git-send-email-paulmck@linux.vnet.ibm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-10-23 21:07:29 +00:00
|
|
|
unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
|
2012-11-29 20:27:00 +00:00
|
|
|
unsigned blkbits = i_blkbits;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned blocksize_mask = (1 << blkbits) - 1;
|
|
|
|
ssize_t retval = -EINVAL;
|
2018-04-05 23:24:36 +00:00
|
|
|
const size_t count = iov_iter_count(iter);
|
2016-04-07 15:51:58 +00:00
|
|
|
loff_t offset = iocb->ki_pos;
|
2018-04-05 23:24:36 +00:00
|
|
|
const loff_t end = offset + count;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct dio *dio;
|
2023-11-08 04:45:50 +00:00
|
|
|
struct dio_submit sdio = { NULL, };
|
2011-08-02 04:38:09 +00:00
|
|
|
struct buffer_head map_bh = { 0, };
|
2012-08-09 13:23:09 +00:00
|
|
|
struct blk_plug plug;
|
2014-03-05 18:50:45 +00:00
|
|
|
unsigned long align = offset | iov_iter_alignment(iter);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-01-13 01:20:35 +00:00
|
|
|
/*
|
|
|
|
* Avoid references to bdev if not absolutely needed to give
|
|
|
|
* the early prefetch in the caller enough time.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-06-24 18:29:42 +00:00
|
|
|
/* watch out for a 0 len io from a tricksy fs */
|
2018-04-05 23:24:36 +00:00
|
|
|
if (iov_iter_rw(iter) == READ && !count)
|
2011-06-24 18:29:42 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-08-02 04:38:06 +00:00
|
|
|
dio = kmem_cache_alloc(dio_cache, GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!dio)
|
2020-10-08 06:26:18 +00:00
|
|
|
return -ENOMEM;
|
2009-12-16 00:47:49 +00:00
|
|
|
/*
|
|
|
|
* Believe it or not, zeroing out the page array caused a .5%
|
|
|
|
* performance regression in a database benchmark. So, we take
|
|
|
|
* care to only zero out what's needed.
|
|
|
|
*/
|
|
|
|
memset(dio, 0, offsetof(struct dio, pages));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
direct-io: cleanup blockdev_direct_IO locking
Currently the locking in blockdev_direct_IO is a mess, we have three
different locking types and very confusing checks for some of them. The
most complicated one is DIO_OWN_LOCKING for reads, which happens to not
actually be used.
This patch gets rid of the DIO_OWN_LOCKING - as mentioned above the read
case is unused anyway, and the write side is almost identical to
DIO_NO_LOCKING. The difference is that DIO_NO_LOCKING always sets the
create argument for the get_blocks callback to zero, but we can easily
move that to the actual get_blocks callbacks. There are four users of the
DIO_NO_LOCKING mode: gfs already ignores the create argument and thus is
fine with the new version, ocfs2 only errors out if create were ever set,
and we can remove this dead code now, the block device code only ever uses
create for an error message if we are fully beyond the device which can
never happen, and last but not least XFS will need the new behavour for
writes.
Now we can replace the lock_type variable with a flags one, where no flag
means the DIO_NO_LOCKING behaviour and DIO_LOCKING is kept as the first
flag. Separate out the check for not allowing to fill holes into a
separate flag, although for now both flags always get set at the same
time.
Also revamp the documentation of the locking scheme to actually make
sense.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Christoph Hellwig <hch@lst.de>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Jeff Moyer <jmoyer@redhat.com>
Cc: Jens Axboe <jens.axboe@oracle.com>
Cc: Zach Brown <zach.brown@oracle.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Alex Elder <aelder@sgi.com>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2009-12-16 00:47:50 +00:00
|
|
|
dio->flags = flags;
|
2020-10-08 06:26:19 +00:00
|
|
|
if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) {
|
|
|
|
/* will be released by direct_io_worker */
|
|
|
|
inode_lock(inode);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2023-05-26 21:41:42 +00:00
|
|
|
dio->is_pinned = iov_iter_extract_will_pin(iter);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-11-30 17:15:42 +00:00
|
|
|
/* Once we sampled i_size check for reads beyond EOF */
|
|
|
|
dio->i_size = i_size_read(inode);
|
|
|
|
if (iov_iter_rw(iter) == READ && offset >= dio->i_size) {
|
2015-12-08 17:22:47 +00:00
|
|
|
retval = 0;
|
2020-10-08 06:26:18 +00:00
|
|
|
goto fail_dio;
|
2015-11-30 17:15:42 +00:00
|
|
|
}
|
|
|
|
|
direct-io: defer alignment check until after the EOF check
Prior to commit 9fe55eea7e4b ("Fix race when checking i_size on direct
i/o read"), an unaligned direct read past end of file would trigger EOF,
since generic_file_aio_read detected this read-at-EOF condition and
skipped the direct IO read entirely, returning 0. After that change, the
read now reaches dio_generic, which detects the misalignment and returns
EINVAL.
This consolidates the generic direct-io to follow the same behavior of
filesystems. Apparently, this fix will only affect ocfs2 since other
filesystems do this verification before calling do_blockdev_direct_IO,
with the exception of f2fs, which has the same bug, but is fixed in the
next patch.
it can be verified by a read loop on a file that does a partial read
before EOF (On file that doesn't end at an aligned address). The
following code fails on an unaligned file on filesystems without
prior validation without this patch, but not on btrfs, ext4, and xfs.
while (done < total) {
ssize_t delta = pread(fd, buf + done, total - done, off + done);
if (!delta)
break;
...
}
Fix this regression by moving the misalignment check to after the EOF
check added by commit 74cedf9b6c60 ("direct-io: Fix negative return from
dio read beyond eof").
Based on a patch by Jamie Liu.
Link: https://lore.kernel.org/r/20201008062620.2928326-4-krisman@collabora.com
Reported-by: Jamie Liu <jamieliu@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Jan Kara <jack@suse.cz>
2020-10-08 06:26:20 +00:00
|
|
|
if (align & blocksize_mask) {
|
|
|
|
if (bdev)
|
|
|
|
blkbits = blksize_bits(bdev_logical_block_size(bdev));
|
|
|
|
blocksize_mask = (1 << blkbits) - 1;
|
|
|
|
if (align & blocksize_mask)
|
|
|
|
goto fail_dio;
|
|
|
|
}
|
|
|
|
|
2020-10-08 06:26:19 +00:00
|
|
|
if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) {
|
|
|
|
struct address_space *mapping = iocb->ki_filp->f_mapping;
|
|
|
|
|
|
|
|
retval = filemap_write_and_wait_range(mapping, offset, end - 1);
|
|
|
|
if (retval)
|
|
|
|
goto fail_dio;
|
2015-11-30 17:15:42 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2014-02-09 23:27:11 +00:00
|
|
|
* For file extending writes updating i_size before data writeouts
|
|
|
|
* complete can expose uninitialized blocks in dumb filesystems.
|
|
|
|
* In that case we need to wait for I/O completion even if asked
|
|
|
|
* for an asynchronous write.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2014-02-09 23:27:11 +00:00
|
|
|
if (is_sync_kiocb(iocb))
|
|
|
|
dio->is_async = false;
|
2018-02-23 11:45:28 +00:00
|
|
|
else if (iov_iter_rw(iter) == WRITE && end > i_size_read(inode))
|
2014-02-09 23:27:11 +00:00
|
|
|
dio->is_async = false;
|
|
|
|
else
|
|
|
|
dio->is_async = true;
|
|
|
|
|
2011-08-02 04:38:09 +00:00
|
|
|
dio->inode = inode;
|
2016-06-05 19:31:50 +00:00
|
|
|
if (iov_iter_rw(iter) == WRITE) {
|
2022-07-14 18:07:14 +00:00
|
|
|
dio->opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
|
2017-06-20 12:05:46 +00:00
|
|
|
if (iocb->ki_flags & IOCB_NOWAIT)
|
2022-07-14 18:07:14 +00:00
|
|
|
dio->opf |= REQ_NOWAIT;
|
2016-06-05 19:31:50 +00:00
|
|
|
} else {
|
2022-07-14 18:07:14 +00:00
|
|
|
dio->opf = REQ_OP_READ;
|
2016-06-05 19:31:50 +00:00
|
|
|
}
|
2013-09-04 13:04:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For AIO O_(D)SYNC writes we need to defer completions to a workqueue
|
|
|
|
* so that we can call ->fsync.
|
|
|
|
*/
|
2017-09-21 14:16:29 +00:00
|
|
|
if (dio->is_async && iov_iter_rw(iter) == WRITE) {
|
|
|
|
retval = 0;
|
2022-05-22 13:39:27 +00:00
|
|
|
if (iocb_is_dsync(iocb))
|
2017-09-21 14:16:29 +00:00
|
|
|
retval = dio_set_defer_completion(dio);
|
|
|
|
else if (!dio->inode->i_sb->s_dio_done_wq) {
|
|
|
|
/*
|
|
|
|
* In case of AIO write racing with buffered read we
|
|
|
|
* need to defer completion. We can't decide this now,
|
|
|
|
* however the workqueue needs to be initialized here.
|
|
|
|
*/
|
|
|
|
retval = sb_init_dio_done_wq(dio->inode->i_sb);
|
|
|
|
}
|
2020-10-08 06:26:18 +00:00
|
|
|
if (retval)
|
|
|
|
goto fail_dio;
|
2013-09-04 13:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Will be decremented at I/O completion time.
|
|
|
|
*/
|
2018-02-23 11:45:29 +00:00
|
|
|
inode_dio_begin(inode);
|
2013-09-04 13:04:40 +00:00
|
|
|
|
2011-08-02 04:38:09 +00:00
|
|
|
sdio.blkbits = blkbits;
|
2012-11-29 20:27:00 +00:00
|
|
|
sdio.blkfactor = i_blkbits - blkbits;
|
2011-08-02 04:38:09 +00:00
|
|
|
sdio.block_in_file = offset >> blkbits;
|
|
|
|
|
|
|
|
sdio.get_block = get_block;
|
|
|
|
dio->end_io = end_io;
|
|
|
|
sdio.final_block_in_bio = -1;
|
|
|
|
sdio.next_block_for_io = -1;
|
|
|
|
|
|
|
|
dio->iocb = iocb;
|
|
|
|
|
|
|
|
spin_lock_init(&dio->bio_lock);
|
|
|
|
dio->refcount = 1;
|
|
|
|
|
2022-05-22 18:59:25 +00:00
|
|
|
dio->should_dirty = user_backed_iter(iter) && iov_iter_rw(iter) == READ;
|
2014-03-15 08:05:57 +00:00
|
|
|
sdio.iter = iter;
|
2018-04-05 23:24:36 +00:00
|
|
|
sdio.final_block_in_request = end >> blkbits;
|
2014-03-15 08:05:57 +00:00
|
|
|
|
2011-08-02 04:38:09 +00:00
|
|
|
/*
|
|
|
|
* In case of non-aligned buffers, we may need 2 more
|
|
|
|
* pages since we need to zero out first and last block.
|
|
|
|
*/
|
|
|
|
if (unlikely(sdio.blkfactor))
|
|
|
|
sdio.pages_in_io = 2;
|
|
|
|
|
2014-03-19 05:16:16 +00:00
|
|
|
sdio.pages_in_io += iov_iter_npages(iter, INT_MAX);
|
2011-08-02 04:38:09 +00:00
|
|
|
|
2012-08-09 13:23:09 +00:00
|
|
|
blk_start_plug(&plug);
|
|
|
|
|
2014-03-15 08:05:57 +00:00
|
|
|
retval = do_direct_IO(dio, &sdio, &map_bh);
|
|
|
|
if (retval)
|
|
|
|
dio_cleanup(dio, &sdio);
|
2011-08-02 04:38:09 +00:00
|
|
|
|
|
|
|
if (retval == -ENOTBLK) {
|
|
|
|
/*
|
|
|
|
* The remaining part of the request will be
|
2021-02-24 20:00:48 +00:00
|
|
|
* handled by buffered I/O when we return
|
2011-08-02 04:38:09 +00:00
|
|
|
*/
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* There may be some unwritten disk at the end of a part-written
|
|
|
|
* fs-block-sized block. Go zero that now.
|
|
|
|
*/
|
|
|
|
dio_zero_block(dio, &sdio, 1, &map_bh);
|
|
|
|
|
|
|
|
if (sdio.cur_page) {
|
|
|
|
ssize_t ret2;
|
|
|
|
|
|
|
|
ret2 = dio_send_cur_page(dio, &sdio, &map_bh);
|
|
|
|
if (retval == 0)
|
|
|
|
retval = ret2;
|
2023-05-26 21:41:42 +00:00
|
|
|
dio_unpin_page(dio, sdio.cur_page);
|
2011-08-02 04:38:09 +00:00
|
|
|
sdio.cur_page = NULL;
|
|
|
|
}
|
|
|
|
if (sdio.bio)
|
|
|
|
dio_bio_submit(dio, &sdio);
|
|
|
|
|
2012-08-09 13:23:09 +00:00
|
|
|
blk_finish_plug(&plug);
|
|
|
|
|
2011-08-02 04:38:09 +00:00
|
|
|
/*
|
|
|
|
* It is possible that, we return short IO due to end of file.
|
|
|
|
* In that case, we need to release all the pages we got hold on.
|
|
|
|
*/
|
|
|
|
dio_cleanup(dio, &sdio);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All block lookups have been performed. For READ requests
|
|
|
|
* we can let i_mutex go now that its achieved its purpose
|
|
|
|
* of protecting us from looking up uninitialized blocks.
|
|
|
|
*/
|
2015-03-16 11:33:50 +00:00
|
|
|
if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING))
|
2016-01-22 20:40:57 +00:00
|
|
|
inode_unlock(dio->inode);
|
2011-08-02 04:38:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The only time we want to leave bios in flight is when a successful
|
|
|
|
* partial aio read or full aio write have been setup. In that case
|
|
|
|
* bio completion will call aio_complete. The only time it's safe to
|
|
|
|
* call aio_complete is when we return -EIOCBQUEUED, so we key on that.
|
|
|
|
* This had *better* be the only place that raises -EIOCBQUEUED.
|
|
|
|
*/
|
|
|
|
BUG_ON(retval == -EIOCBQUEUED);
|
|
|
|
if (dio->is_async && retval == 0 && dio->result &&
|
2015-03-16 11:33:50 +00:00
|
|
|
(iov_iter_rw(iter) == READ || dio->result == count))
|
2011-08-02 04:38:09 +00:00
|
|
|
retval = -EIOCBQUEUED;
|
2014-07-30 11:18:48 +00:00
|
|
|
else
|
2011-08-02 04:38:09 +00:00
|
|
|
dio_await_completion(dio);
|
|
|
|
|
|
|
|
if (drop_refcount(dio) == 0) {
|
2017-10-17 14:43:09 +00:00
|
|
|
retval = dio_complete(dio, retval, DIO_COMPLETE_INVALIDATE);
|
2011-08-02 04:38:09 +00:00
|
|
|
} else
|
|
|
|
BUG_ON(retval != -EIOCBQUEUED);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2020-10-08 06:26:18 +00:00
|
|
|
return retval;
|
|
|
|
|
|
|
|
fail_dio:
|
|
|
|
if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ)
|
|
|
|
inode_unlock(inode);
|
|
|
|
|
|
|
|
kmem_cache_free(dio_cache, dio);
|
fs: introduce new truncate sequence
Introduce a new truncate calling sequence into fs/mm subsystems. Rather than
setattr > vmtruncate > truncate, have filesystems call their truncate sequence
from ->setattr if filesystem specific operations are required. vmtruncate is
deprecated, and truncate_pagecache and inode_newsize_ok helpers introduced
previously should be used.
simple_setattr is introduced for simple in-ram filesystems to implement
the new truncate sequence. Eventually all filesystems should be converted
to implement a setattr, and the default code in notify_change should go
away.
simple_setsize is also introduced to perform just the ATTR_SIZE portion
of simple_setattr (ie. changing i_size and trimming pagecache).
To implement the new truncate sequence:
- filesystem specific manipulations (eg freeing blocks) must be done in
the setattr method rather than ->truncate.
- vmtruncate can not be used by core code to trim blocks past i_size in
the event of write failure after allocation, so this must be performed
in the fs code.
- convert usage of helpers block_write_begin, nobh_write_begin,
cont_write_begin, and *blockdev_direct_IO* to use _newtrunc postfixed
variants. These avoid calling vmtruncate to trim blocks (see previous).
- inode_setattr should not be used. generic_setattr is a new function
to be used to copy simple attributes into the generic inode.
- make use of the better opportunity to handle errors with the new sequence.
Big problem with the previous calling sequence: the filesystem is not called
until i_size has already changed. This means it is not allowed to fail the
call, and also it does not know what the previous i_size was. Also, generic
code calling vmtruncate to truncate allocated blocks in case of error had
no good way to return a meaningful error (or, for example, atomically handle
block deallocation).
Cc: Christoph Hellwig <hch@lst.de>
Acked-by: Jan Kara <jack@suse.cz>
Signed-off-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2010-05-26 15:05:33 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
EXPORT_SYMBOL(__blockdev_direct_IO);
|
2011-08-02 04:38:06 +00:00
|
|
|
|
|
|
|
static __init int dio_init(void)
|
|
|
|
{
|
|
|
|
dio_cache = KMEM_CACHE(dio, SLAB_PANIC);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(dio_init)
|