1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * fs/direct-io.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * O_DIRECT 81da177e4SLinus Torvalds * 9e1f8e874SFrancois Cami * 04Jul2002 Andrew Morton 101da177e4SLinus Torvalds * Initial version 111da177e4SLinus Torvalds * 11Sep2002 janetinc@us.ibm.com 121da177e4SLinus Torvalds * added readv/writev support. 13e1f8e874SFrancois Cami * 29Oct2002 Andrew Morton 141da177e4SLinus Torvalds * rewrote bio_add_page() support. 151da177e4SLinus Torvalds * 30Oct2002 pbadari@us.ibm.com 161da177e4SLinus Torvalds * added support for non-aligned IO. 171da177e4SLinus Torvalds * 06Nov2002 pbadari@us.ibm.com 181da177e4SLinus Torvalds * added asynchronous IO support. 191da177e4SLinus Torvalds * 21Jul2003 nathans@sgi.com 201da177e4SLinus Torvalds * added IO completion notifier. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/kernel.h> 241da177e4SLinus Torvalds #include <linux/module.h> 251da177e4SLinus Torvalds #include <linux/types.h> 261da177e4SLinus Torvalds #include <linux/fs.h> 271da177e4SLinus Torvalds #include <linux/mm.h> 281da177e4SLinus Torvalds #include <linux/slab.h> 291da177e4SLinus Torvalds #include <linux/highmem.h> 301da177e4SLinus Torvalds #include <linux/pagemap.h> 3198c4d57dSAndrew Morton #include <linux/task_io_accounting_ops.h> 321da177e4SLinus Torvalds #include <linux/bio.h> 331da177e4SLinus Torvalds #include <linux/wait.h> 341da177e4SLinus Torvalds #include <linux/err.h> 351da177e4SLinus Torvalds #include <linux/blkdev.h> 361da177e4SLinus Torvalds #include <linux/buffer_head.h> 371da177e4SLinus Torvalds #include <linux/rwsem.h> 381da177e4SLinus Torvalds #include <linux/uio.h> 3960063497SArun Sharma #include <linux/atomic.h> 4065dd2aa9SAndi Kleen #include <linux/prefetch.h> 411da177e4SLinus Torvalds 42b16155a0SEric Biggers #include "internal.h" 43b16155a0SEric Biggers 441da177e4SLinus Torvalds /* 451da177e4SLinus Torvalds * How many user pages to map in one call to get_user_pages(). This determines 46cde1ecb3SAndi Kleen * the size of a structure in the slab cache 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds #define DIO_PAGES 64 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* 51ffe51f01SLukas Czerner * Flags for dio_complete() 52ffe51f01SLukas Czerner */ 53ffe51f01SLukas Czerner #define DIO_COMPLETE_ASYNC 0x01 /* This is async IO */ 54ffe51f01SLukas Czerner #define DIO_COMPLETE_INVALIDATE 0x02 /* Can invalidate pages */ 55ffe51f01SLukas Czerner 56ffe51f01SLukas Czerner /* 571da177e4SLinus Torvalds * This code generally works in units of "dio_blocks". A dio_block is 581da177e4SLinus Torvalds * somewhere between the hard sector size and the filesystem block size. it 591da177e4SLinus Torvalds * is determined on a per-invocation basis. When talking to the filesystem 601da177e4SLinus Torvalds * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity 611da177e4SLinus Torvalds * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted 621da177e4SLinus Torvalds * to bio_block quantities by shifting left by blkfactor. 631da177e4SLinus Torvalds * 641da177e4SLinus Torvalds * If blkfactor is zero then the user's request was aligned to the filesystem's 651da177e4SLinus Torvalds * blocksize. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 68eb28be2bSAndi Kleen /* dio_state only used in the submission path */ 69eb28be2bSAndi Kleen 70eb28be2bSAndi Kleen struct dio_submit { 711da177e4SLinus Torvalds struct bio *bio; /* bio under assembly */ 721da177e4SLinus Torvalds unsigned blkbits; /* doesn't change */ 731da177e4SLinus Torvalds unsigned blkfactor; /* When we're using an alignment which 741da177e4SLinus Torvalds is finer than the filesystem's soft 751da177e4SLinus Torvalds blocksize, this specifies how much 761da177e4SLinus Torvalds finer. blkfactor=2 means 1/4-block 771da177e4SLinus Torvalds alignment. Does not change */ 781da177e4SLinus Torvalds unsigned start_zero_done; /* flag: sub-blocksize zeroing has 791da177e4SLinus Torvalds been performed at the start of a 801da177e4SLinus Torvalds write */ 811da177e4SLinus Torvalds int pages_in_io; /* approximate total IO pages */ 821da177e4SLinus Torvalds sector_t block_in_file; /* Current offset into the underlying 831da177e4SLinus Torvalds file in dio_block units. */ 841da177e4SLinus Torvalds unsigned blocks_available; /* At block_in_file. changes */ 850dc2bc49SAndi Kleen int reap_counter; /* rate limit reaping */ 861da177e4SLinus Torvalds sector_t final_block_in_request;/* doesn't change */ 871da177e4SLinus Torvalds int boundary; /* prev block is at a boundary */ 881d8fa7a2SBadari Pulavarty get_block_t *get_block; /* block mapping function */ 89facd07b0SJosef Bacik dio_submit_t *submit_io; /* IO submition function */ 90eb28be2bSAndi Kleen 91facd07b0SJosef Bacik loff_t logical_offset_in_bio; /* current first logical block in bio */ 921da177e4SLinus Torvalds sector_t final_block_in_bio; /* current final block in bio + 1 */ 931da177e4SLinus Torvalds sector_t next_block_for_io; /* next block to be put under IO, 941da177e4SLinus Torvalds in dio_blocks units */ 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * Deferred addition of a page to the dio. These variables are 981da177e4SLinus Torvalds * private to dio_send_cur_page(), submit_page_section() and 991da177e4SLinus Torvalds * dio_bio_add_page(). 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds struct page *cur_page; /* The page */ 1021da177e4SLinus Torvalds unsigned cur_page_offset; /* Offset into it, in bytes */ 1031da177e4SLinus Torvalds unsigned cur_page_len; /* Nr of bytes at cur_page_offset */ 1041da177e4SLinus Torvalds sector_t cur_page_block; /* Where it starts */ 105facd07b0SJosef Bacik loff_t cur_page_fs_offset; /* Offset in file */ 1061da177e4SLinus Torvalds 1077b2c99d1SAl Viro struct iov_iter *iter; 10823aee091SJeff Moyer /* 10923aee091SJeff Moyer * Page queue. These variables belong to dio_refill_pages() and 11023aee091SJeff Moyer * dio_get_page(). 11123aee091SJeff Moyer */ 11223aee091SJeff Moyer unsigned head; /* next page to process */ 11323aee091SJeff Moyer unsigned tail; /* last valid page + 1 */ 1147b2c99d1SAl Viro size_t from, to; 115eb28be2bSAndi Kleen }; 116eb28be2bSAndi Kleen 117eb28be2bSAndi Kleen /* dio_state communicated between submission path and end_io */ 118eb28be2bSAndi Kleen struct dio { 119eb28be2bSAndi Kleen int flags; /* doesn't change */ 1208a4c1e42SMike Christie int op; 1218a4c1e42SMike Christie int op_flags; 12274d46992SChristoph Hellwig struct gendisk *bio_disk; 1230dc2bc49SAndi Kleen struct inode *inode; 124eb28be2bSAndi Kleen loff_t i_size; /* i_size when submitted */ 125eb28be2bSAndi Kleen dio_iodone_t *end_io; /* IO completion function */ 126eb28be2bSAndi Kleen 12718772641SAndi Kleen void *private; /* copy from map_bh.b_private */ 128eb28be2bSAndi Kleen 129eb28be2bSAndi Kleen /* BIO completion state */ 130eb28be2bSAndi Kleen spinlock_t bio_lock; /* protects BIO fields below */ 1310dc2bc49SAndi Kleen int page_errors; /* errno from get_user_pages() */ 1320dc2bc49SAndi Kleen int is_async; /* is IO async ? */ 1337b7a8665SChristoph Hellwig bool defer_completion; /* defer AIO completion to workqueue? */ 13453cbf3b1SMing Lei bool should_dirty; /* if pages should be dirtied */ 1350dc2bc49SAndi Kleen int io_error; /* IO error in completion path */ 136eb28be2bSAndi Kleen unsigned long refcount; /* direct_io_worker() and bios */ 137eb28be2bSAndi Kleen struct bio *bio_list; /* singly linked via bi_private */ 138eb28be2bSAndi Kleen struct task_struct *waiter; /* waiting task (NULL if none) */ 139eb28be2bSAndi Kleen 140eb28be2bSAndi Kleen /* AIO related stuff */ 141eb28be2bSAndi Kleen struct kiocb *iocb; /* kiocb */ 142eb28be2bSAndi Kleen ssize_t result; /* IO result */ 143eb28be2bSAndi Kleen 14423aee091SJeff Moyer /* 14523aee091SJeff Moyer * pages[] (and any fields placed after it) are not zeroed out at 14623aee091SJeff Moyer * allocation time. Don't add new fields after pages[] unless you 14723aee091SJeff Moyer * wish that they not be zeroed. 14823aee091SJeff Moyer */ 1497b7a8665SChristoph Hellwig union { 15023aee091SJeff Moyer struct page *pages[DIO_PAGES]; /* page buffer */ 1517b7a8665SChristoph Hellwig struct work_struct complete_work;/* deferred AIO completion */ 1527b7a8665SChristoph Hellwig }; 1536e8267f5SAndi Kleen } ____cacheline_aligned_in_smp; 1546e8267f5SAndi Kleen 1556e8267f5SAndi Kleen static struct kmem_cache *dio_cache __read_mostly; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* 1581da177e4SLinus Torvalds * How many pages are in the queue? 1591da177e4SLinus Torvalds */ 160eb28be2bSAndi Kleen static inline unsigned dio_pages_present(struct dio_submit *sdio) 1611da177e4SLinus Torvalds { 162eb28be2bSAndi Kleen return sdio->tail - sdio->head; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /* 1661da177e4SLinus Torvalds * Go grab and pin some userspace pages. Typically we'll get 64 at a time. 1671da177e4SLinus Torvalds */ 168ba253fbfSAndi Kleen static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio) 1691da177e4SLinus Torvalds { 1707b2c99d1SAl Viro ssize_t ret; 1711da177e4SLinus Torvalds 1722c80929cSMiklos Szeredi ret = iov_iter_get_pages(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES, 1737b2c99d1SAl Viro &sdio->from); 1741da177e4SLinus Torvalds 1758a4c1e42SMike Christie if (ret < 0 && sdio->blocks_available && (dio->op == REQ_OP_WRITE)) { 176557ed1faSNick Piggin struct page *page = ZERO_PAGE(0); 1771da177e4SLinus Torvalds /* 1781da177e4SLinus Torvalds * A memory fault, but the filesystem has some outstanding 1791da177e4SLinus Torvalds * mapped blocks. We need to use those blocks up to avoid 1801da177e4SLinus Torvalds * leaking stale data in the file. 1811da177e4SLinus Torvalds */ 1821da177e4SLinus Torvalds if (dio->page_errors == 0) 1831da177e4SLinus Torvalds dio->page_errors = ret; 18409cbfeafSKirill A. Shutemov get_page(page); 185b5810039SNick Piggin dio->pages[0] = page; 186eb28be2bSAndi Kleen sdio->head = 0; 187eb28be2bSAndi Kleen sdio->tail = 1; 1887b2c99d1SAl Viro sdio->from = 0; 1897b2c99d1SAl Viro sdio->to = PAGE_SIZE; 1907b2c99d1SAl Viro return 0; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds if (ret >= 0) { 1947b2c99d1SAl Viro iov_iter_advance(sdio->iter, ret); 1957b2c99d1SAl Viro ret += sdio->from; 196eb28be2bSAndi Kleen sdio->head = 0; 1977b2c99d1SAl Viro sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE; 1987b2c99d1SAl Viro sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1; 1997b2c99d1SAl Viro return 0; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds return ret; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds /* 2051da177e4SLinus Torvalds * Get another userspace page. Returns an ERR_PTR on error. Pages are 2061da177e4SLinus Torvalds * buffered inside the dio so that we can call get_user_pages() against a 2071da177e4SLinus Torvalds * decent number of pages, less frequently. To provide nicer use of the 2081da177e4SLinus Torvalds * L1 cache. 2091da177e4SLinus Torvalds */ 210ba253fbfSAndi Kleen static inline struct page *dio_get_page(struct dio *dio, 2116fcc5420SBoaz Harrosh struct dio_submit *sdio) 2121da177e4SLinus Torvalds { 213eb28be2bSAndi Kleen if (dio_pages_present(sdio) == 0) { 2141da177e4SLinus Torvalds int ret; 2151da177e4SLinus Torvalds 216eb28be2bSAndi Kleen ret = dio_refill_pages(dio, sdio); 2171da177e4SLinus Torvalds if (ret) 2181da177e4SLinus Torvalds return ERR_PTR(ret); 219eb28be2bSAndi Kleen BUG_ON(dio_pages_present(sdio) == 0); 2201da177e4SLinus Torvalds } 2216fcc5420SBoaz Harrosh return dio->pages[sdio->head]; 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2245a9d929dSDarrick J. Wong /* 2256d544bb4SZach Brown * dio_complete() - called when all DIO BIO I/O has been completed 2266d544bb4SZach Brown * 2277b7a8665SChristoph Hellwig * This drops i_dio_count, lets interested parties know that a DIO operation 2287b7a8665SChristoph Hellwig * has completed, and calculates the resulting return code for the operation. 2296d544bb4SZach Brown * 2306d544bb4SZach Brown * It lets the filesystem know if it registered an interest earlier via 2316d544bb4SZach Brown * get_block. Pass the private field of the map buffer_head so that 2326d544bb4SZach Brown * filesystems can use it to hold additional state between get_block calls and 2336d544bb4SZach Brown * dio_complete. 2341da177e4SLinus Torvalds */ 235ffe51f01SLukas Czerner static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags) 2361da177e4SLinus Torvalds { 237716b9bc0SChristoph Hellwig loff_t offset = dio->iocb->ki_pos; 2386d544bb4SZach Brown ssize_t transferred = 0; 239332391a9SLukas Czerner int err; 2406d544bb4SZach Brown 2418459d86aSZach Brown /* 2428459d86aSZach Brown * AIO submission can race with bio completion to get here while 2438459d86aSZach Brown * expecting to have the last io completed by bio completion. 2448459d86aSZach Brown * In that case -EIOCBQUEUED is in fact not an error we want 2458459d86aSZach Brown * to preserve through this call. 2468459d86aSZach Brown */ 2478459d86aSZach Brown if (ret == -EIOCBQUEUED) 2488459d86aSZach Brown ret = 0; 2498459d86aSZach Brown 2506d544bb4SZach Brown if (dio->result) { 2516d544bb4SZach Brown transferred = dio->result; 2526d544bb4SZach Brown 2536d544bb4SZach Brown /* Check for short read case */ 2548a4c1e42SMike Christie if ((dio->op == REQ_OP_READ) && 2558a4c1e42SMike Christie ((offset + transferred) > dio->i_size)) 2566d544bb4SZach Brown transferred = dio->i_size - offset; 2574038acdbSAl Viro /* ignore EFAULT if some IO has been done */ 2584038acdbSAl Viro if (unlikely(ret == -EFAULT) && transferred) 2594038acdbSAl Viro ret = 0; 2606d544bb4SZach Brown } 2616d544bb4SZach Brown 2626d544bb4SZach Brown if (ret == 0) 2636d544bb4SZach Brown ret = dio->page_errors; 2646d544bb4SZach Brown if (ret == 0) 2656d544bb4SZach Brown ret = dio->io_error; 2666d544bb4SZach Brown if (ret == 0) 2676d544bb4SZach Brown ret = transferred; 2686d544bb4SZach Brown 2695e25c269SEryu Guan if (dio->end_io) { 2705e25c269SEryu Guan // XXX: ki_pos?? 2715e25c269SEryu Guan err = dio->end_io(dio->iocb, offset, ret, dio->private); 2725e25c269SEryu Guan if (err) 2735e25c269SEryu Guan ret = err; 2745e25c269SEryu Guan } 2755e25c269SEryu Guan 276332391a9SLukas Czerner /* 277332391a9SLukas Czerner * Try again to invalidate clean pages which might have been cached by 278332391a9SLukas Czerner * non-direct readahead, or faulted in by get_user_pages() if the source 279332391a9SLukas Czerner * of the write was an mmap'ed region of the file we're writing. Either 280332391a9SLukas Czerner * one is a pretty crazy thing to do, so we don't support it 100%. If 281332391a9SLukas Czerner * this invalidation fails, tough, the write still worked... 2825e25c269SEryu Guan * 2835e25c269SEryu Guan * And this page cache invalidation has to be after dio->end_io(), as 2845e25c269SEryu Guan * some filesystems convert unwritten extents to real allocations in 2855e25c269SEryu Guan * end_io() when necessary, otherwise a racing buffer read would cache 2865e25c269SEryu Guan * zeros from unwritten extents. 287332391a9SLukas Czerner */ 288ffe51f01SLukas Czerner if (flags & DIO_COMPLETE_INVALIDATE && 289ffe51f01SLukas Czerner ret > 0 && dio->op == REQ_OP_WRITE && 290332391a9SLukas Czerner dio->inode->i_mapping->nrpages) { 291332391a9SLukas Czerner err = invalidate_inode_pages2_range(dio->inode->i_mapping, 292332391a9SLukas Czerner offset >> PAGE_SHIFT, 293332391a9SLukas Czerner (offset + ret - 1) >> PAGE_SHIFT); 2945a9d929dSDarrick J. Wong if (err) 2955a9d929dSDarrick J. Wong dio_warn_stale_pagecache(dio->iocb->ki_filp); 296332391a9SLukas Czerner } 297332391a9SLukas Czerner 298fe0f07d0SJens Axboe inode_dio_end(dio->inode); 299fe0f07d0SJens Axboe 300ffe51f01SLukas Czerner if (flags & DIO_COMPLETE_ASYNC) { 301e2592217SChristoph Hellwig /* 302e2592217SChristoph Hellwig * generic_write_sync expects ki_pos to have been updated 303e2592217SChristoph Hellwig * already, but the submission path only does this for 304e2592217SChristoph Hellwig * synchronous I/O. 305e2592217SChristoph Hellwig */ 306e2592217SChristoph Hellwig dio->iocb->ki_pos += transferred; 30702afc27fSChristoph Hellwig 30841e817bcSMaximilian Heyne if (ret > 0 && dio->op == REQ_OP_WRITE) 30941e817bcSMaximilian Heyne ret = generic_write_sync(dio->iocb, ret); 3106b19b766SJens Axboe dio->iocb->ki_complete(dio->iocb, ret); 31102afc27fSChristoph Hellwig } 31240e2e973SChristoph Hellwig 3137b7a8665SChristoph Hellwig kmem_cache_free(dio_cache, dio); 3146d544bb4SZach Brown return ret; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3177b7a8665SChristoph Hellwig static void dio_aio_complete_work(struct work_struct *work) 3187b7a8665SChristoph Hellwig { 3197b7a8665SChristoph Hellwig struct dio *dio = container_of(work, struct dio, complete_work); 3207b7a8665SChristoph Hellwig 321ffe51f01SLukas Czerner dio_complete(dio, 0, DIO_COMPLETE_ASYNC | DIO_COMPLETE_INVALIDATE); 3227b7a8665SChristoph Hellwig } 3237b7a8665SChristoph Hellwig 3244e4cbee9SChristoph Hellwig static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio); 3257b7a8665SChristoph Hellwig 3261da177e4SLinus Torvalds /* 3271da177e4SLinus Torvalds * Asynchronous IO callback. 3281da177e4SLinus Torvalds */ 3294246a0b6SChristoph Hellwig static void dio_bio_end_aio(struct bio *bio) 3301da177e4SLinus Torvalds { 3311da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 3325eb6c7a2SZach Brown unsigned long remaining; 3335eb6c7a2SZach Brown unsigned long flags; 334332391a9SLukas Czerner bool defer_completion = false; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* cleanup the bio */ 3371da177e4SLinus Torvalds dio_bio_complete(dio, bio); 3380273201eSZach Brown 3395eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 3405eb6c7a2SZach Brown remaining = --dio->refcount; 3415eb6c7a2SZach Brown if (remaining == 1 && dio->waiter) 34220258b2bSZach Brown wake_up_process(dio->waiter); 3435eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 34420258b2bSZach Brown 3458459d86aSZach Brown if (remaining == 0) { 346332391a9SLukas Czerner /* 347332391a9SLukas Czerner * Defer completion when defer_completion is set or 348332391a9SLukas Czerner * when the inode has pages mapped and this is AIO write. 349332391a9SLukas Czerner * We need to invalidate those pages because there is a 350332391a9SLukas Czerner * chance they contain stale data in the case buffered IO 351332391a9SLukas Czerner * went in between AIO submission and completion into the 352332391a9SLukas Czerner * same region. 353332391a9SLukas Czerner */ 354332391a9SLukas Czerner if (dio->result) 355332391a9SLukas Czerner defer_completion = dio->defer_completion || 356332391a9SLukas Czerner (dio->op == REQ_OP_WRITE && 357332391a9SLukas Czerner dio->inode->i_mapping->nrpages); 358332391a9SLukas Czerner if (defer_completion) { 3597b7a8665SChristoph Hellwig INIT_WORK(&dio->complete_work, dio_aio_complete_work); 3607b7a8665SChristoph Hellwig queue_work(dio->inode->i_sb->s_dio_done_wq, 3617b7a8665SChristoph Hellwig &dio->complete_work); 3627b7a8665SChristoph Hellwig } else { 363ffe51f01SLukas Czerner dio_complete(dio, 0, DIO_COMPLETE_ASYNC); 3647b7a8665SChristoph Hellwig } 3658459d86aSZach Brown } 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds /* 3691da177e4SLinus Torvalds * The BIO completion handler simply queues the BIO up for the process-context 3701da177e4SLinus Torvalds * handler. 3711da177e4SLinus Torvalds * 3721da177e4SLinus Torvalds * During I/O bi_private points at the dio. After I/O, bi_private is used to 3731da177e4SLinus Torvalds * implement a singly-linked list of completed BIOs, at dio->bio_list. 3741da177e4SLinus Torvalds */ 3754246a0b6SChristoph Hellwig static void dio_bio_end_io(struct bio *bio) 3761da177e4SLinus Torvalds { 3771da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 3781da177e4SLinus Torvalds unsigned long flags; 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 3811da177e4SLinus Torvalds bio->bi_private = dio->bio_list; 3821da177e4SLinus Torvalds dio->bio_list = bio; 3835eb6c7a2SZach Brown if (--dio->refcount == 1 && dio->waiter) 3841da177e4SLinus Torvalds wake_up_process(dio->waiter); 3851da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 388ba253fbfSAndi Kleen static inline void 389eb28be2bSAndi Kleen dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, 390eb28be2bSAndi Kleen struct block_device *bdev, 3911da177e4SLinus Torvalds sector_t first_sector, int nr_vecs) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds struct bio *bio; 3941da177e4SLinus Torvalds 39520d9600cSDavid Dillow /* 3960eb0b63cSChristoph Hellwig * bio_alloc() is guaranteed to return a bio when allowed to sleep and 3970eb0b63cSChristoph Hellwig * we request a valid number of vectors. 39820d9600cSDavid Dillow */ 39907888c66SChristoph Hellwig bio = bio_alloc(bdev, nr_vecs, dio->op | dio->op_flags, GFP_KERNEL); 4004f024f37SKent Overstreet bio->bi_iter.bi_sector = first_sector; 4011da177e4SLinus Torvalds if (dio->is_async) 4021da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_aio; 4031da177e4SLinus Torvalds else 4041da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_io; 405eb28be2bSAndi Kleen sdio->bio = bio; 406eb28be2bSAndi Kleen sdio->logical_offset_in_bio = sdio->cur_page_fs_offset; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* 4101da177e4SLinus Torvalds * In the AIO read case we speculatively dirty the pages before starting IO. 4111da177e4SLinus Torvalds * During IO completion, any of these pages which happen to have been written 4121da177e4SLinus Torvalds * back will be redirtied by bio_check_pages_dirty(). 4130273201eSZach Brown * 4140273201eSZach Brown * bios hold a dio reference between submit_bio and ->end_io. 4151da177e4SLinus Torvalds */ 416ba253fbfSAndi Kleen static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio) 4171da177e4SLinus Torvalds { 418eb28be2bSAndi Kleen struct bio *bio = sdio->bio; 4195eb6c7a2SZach Brown unsigned long flags; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds bio->bi_private = dio; 4220cf41e5eSPavel Begunkov /* don't account direct I/O as memory stall */ 4230cf41e5eSPavel Begunkov bio_clear_flag(bio, BIO_WORKINGSET); 4245eb6c7a2SZach Brown 4255eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 4265eb6c7a2SZach Brown dio->refcount++; 4275eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 4285eb6c7a2SZach Brown 4298a4c1e42SMike Christie if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty) 4301da177e4SLinus Torvalds bio_set_pages_dirty(bio); 4315eb6c7a2SZach Brown 432309dca30SChristoph Hellwig dio->bio_disk = bio->bi_bdev->bd_disk; 433c1c53460SJens Axboe 43494c2ed58SChristoph Hellwig if (sdio->submit_io) 4358a4c1e42SMike Christie sdio->submit_io(bio, dio->inode, sdio->logical_offset_in_bio); 43694c2ed58SChristoph Hellwig else 43794c2ed58SChristoph Hellwig submit_bio(bio); 4381da177e4SLinus Torvalds 439eb28be2bSAndi Kleen sdio->bio = NULL; 440eb28be2bSAndi Kleen sdio->boundary = 0; 441eb28be2bSAndi Kleen sdio->logical_offset_in_bio = 0; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* 4451da177e4SLinus Torvalds * Release any resources in case of a failure 4461da177e4SLinus Torvalds */ 447ba253fbfSAndi Kleen static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio) 4481da177e4SLinus Torvalds { 4497b2c99d1SAl Viro while (sdio->head < sdio->tail) 45009cbfeafSKirill A. Shutemov put_page(dio->pages[sdio->head++]); 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds /* 4540273201eSZach Brown * Wait for the next BIO to complete. Remove it and return it. NULL is 4550273201eSZach Brown * returned once all BIOs have been completed. This must only be called once 4560273201eSZach Brown * all bios have been issued so that dio->refcount can only decrease. This 4573d742d4bSRandy Dunlap * requires that the caller hold a reference on the dio. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds static struct bio *dio_await_one(struct dio *dio) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds unsigned long flags; 4620273201eSZach Brown struct bio *bio = NULL; 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 4655eb6c7a2SZach Brown 4665eb6c7a2SZach Brown /* 4675eb6c7a2SZach Brown * Wait as long as the list is empty and there are bios in flight. bio 4685eb6c7a2SZach Brown * completion drops the count, maybe adds to the list, and wakes while 4695eb6c7a2SZach Brown * holding the bio_lock so we don't need set_current_state()'s barrier 4705eb6c7a2SZach Brown * and can call it after testing our condition. 4715eb6c7a2SZach Brown */ 4725eb6c7a2SZach Brown while (dio->refcount > 1 && dio->bio_list == NULL) { 4735eb6c7a2SZach Brown __set_current_state(TASK_UNINTERRUPTIBLE); 4741da177e4SLinus Torvalds dio->waiter = current; 4751da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 476e6249cddSMing Lei blk_io_schedule(); 4775eb6c7a2SZach Brown /* wake up sets us TASK_RUNNING */ 4781da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 4791da177e4SLinus Torvalds dio->waiter = NULL; 4801da177e4SLinus Torvalds } 4810273201eSZach Brown if (dio->bio_list) { 4821da177e4SLinus Torvalds bio = dio->bio_list; 4831da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 4840273201eSZach Brown } 4851da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 4861da177e4SLinus Torvalds return bio; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* 4901da177e4SLinus Torvalds * Process one completed BIO. No locks are held. 4911da177e4SLinus Torvalds */ 4924e4cbee9SChristoph Hellwig static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio) 4931da177e4SLinus Torvalds { 4944e4cbee9SChristoph Hellwig blk_status_t err = bio->bi_status; 495d7c8aa85SChristoph Hellwig bool should_dirty = dio->op == REQ_OP_READ && dio->should_dirty; 4961da177e4SLinus Torvalds 49703a07c92SGoldwyn Rodrigues if (err) { 49803a07c92SGoldwyn Rodrigues if (err == BLK_STS_AGAIN && (bio->bi_opf & REQ_NOWAIT)) 49903a07c92SGoldwyn Rodrigues dio->io_error = -EAGAIN; 50003a07c92SGoldwyn Rodrigues else 501174e27c6SChen, Kenneth W dio->io_error = -EIO; 50203a07c92SGoldwyn Rodrigues } 5031da177e4SLinus Torvalds 504d7c8aa85SChristoph Hellwig if (dio->is_async && should_dirty) { 5057ddc971fSMike Krinkin bio_check_pages_dirty(bio); /* transfers ownership */ 5061da177e4SLinus Torvalds } else { 507d7c8aa85SChristoph Hellwig bio_release_pages(bio, should_dirty); 5081da177e4SLinus Torvalds bio_put(bio); 5091da177e4SLinus Torvalds } 5109b81c842SSasha Levin return err; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds /* 5140273201eSZach Brown * Wait on and process all in-flight BIOs. This must only be called once 5150273201eSZach Brown * all bios have been issued so that the refcount can only decrease. 5160273201eSZach Brown * This just waits for all bios to make it through dio_bio_complete. IO 517beb7dd86SRobert P. J. Day * errors are propagated through dio->io_error and should be propagated via 5180273201eSZach Brown * dio_complete(). 5191da177e4SLinus Torvalds */ 5206d544bb4SZach Brown static void dio_await_completion(struct dio *dio) 5211da177e4SLinus Torvalds { 5220273201eSZach Brown struct bio *bio; 5230273201eSZach Brown do { 5240273201eSZach Brown bio = dio_await_one(dio); 5250273201eSZach Brown if (bio) 5266d544bb4SZach Brown dio_bio_complete(dio, bio); 5270273201eSZach Brown } while (bio); 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * A really large O_DIRECT read or write can generate a lot of BIOs. So 5321da177e4SLinus Torvalds * to keep the memory consumption sane we periodically reap any completed BIOs 5331da177e4SLinus Torvalds * during the BIO generation phase. 5341da177e4SLinus Torvalds * 5351da177e4SLinus Torvalds * This also helps to limit the peak amount of pinned userspace memory. 5361da177e4SLinus Torvalds */ 537ba253fbfSAndi Kleen static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds int ret = 0; 5401da177e4SLinus Torvalds 541eb28be2bSAndi Kleen if (sdio->reap_counter++ >= 64) { 5421da177e4SLinus Torvalds while (dio->bio_list) { 5431da177e4SLinus Torvalds unsigned long flags; 5441da177e4SLinus Torvalds struct bio *bio; 5451da177e4SLinus Torvalds int ret2; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 5481da177e4SLinus Torvalds bio = dio->bio_list; 5491da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 5501da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 5514e4cbee9SChristoph Hellwig ret2 = blk_status_to_errno(dio_bio_complete(dio, bio)); 5521da177e4SLinus Torvalds if (ret == 0) 5531da177e4SLinus Torvalds ret = ret2; 5541da177e4SLinus Torvalds } 555eb28be2bSAndi Kleen sdio->reap_counter = 0; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds return ret; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds /* 5617b7a8665SChristoph Hellwig * Create workqueue for deferred direct IO completions. We allocate the 5627b7a8665SChristoph Hellwig * workqueue when it's first needed. This avoids creating workqueue for 5637b7a8665SChristoph Hellwig * filesystems that don't need it and also allows us to create the workqueue 5647b7a8665SChristoph Hellwig * late enough so the we can include s_id in the name of the workqueue. 5657b7a8665SChristoph Hellwig */ 566ec1b8260SChristoph Hellwig int sb_init_dio_done_wq(struct super_block *sb) 5677b7a8665SChristoph Hellwig { 56845150c43SOlof Johansson struct workqueue_struct *old; 5697b7a8665SChristoph Hellwig struct workqueue_struct *wq = alloc_workqueue("dio/%s", 5707b7a8665SChristoph Hellwig WQ_MEM_RECLAIM, 0, 5717b7a8665SChristoph Hellwig sb->s_id); 5727b7a8665SChristoph Hellwig if (!wq) 5737b7a8665SChristoph Hellwig return -ENOMEM; 5747b7a8665SChristoph Hellwig /* 5757b7a8665SChristoph Hellwig * This has to be atomic as more DIOs can race to create the workqueue 5767b7a8665SChristoph Hellwig */ 57745150c43SOlof Johansson old = cmpxchg(&sb->s_dio_done_wq, NULL, wq); 5787b7a8665SChristoph Hellwig /* Someone created workqueue before us? Free ours... */ 57945150c43SOlof Johansson if (old) 5807b7a8665SChristoph Hellwig destroy_workqueue(wq); 5817b7a8665SChristoph Hellwig return 0; 5827b7a8665SChristoph Hellwig } 5837b7a8665SChristoph Hellwig 5847b7a8665SChristoph Hellwig static int dio_set_defer_completion(struct dio *dio) 5857b7a8665SChristoph Hellwig { 5867b7a8665SChristoph Hellwig struct super_block *sb = dio->inode->i_sb; 5877b7a8665SChristoph Hellwig 5887b7a8665SChristoph Hellwig if (dio->defer_completion) 5897b7a8665SChristoph Hellwig return 0; 5907b7a8665SChristoph Hellwig dio->defer_completion = true; 5917b7a8665SChristoph Hellwig if (!sb->s_dio_done_wq) 5927b7a8665SChristoph Hellwig return sb_init_dio_done_wq(sb); 5937b7a8665SChristoph Hellwig return 0; 5947b7a8665SChristoph Hellwig } 5957b7a8665SChristoph Hellwig 5967b7a8665SChristoph Hellwig /* 5971da177e4SLinus Torvalds * Call into the fs to map some more disk blocks. We record the current number 598eb28be2bSAndi Kleen * of available blocks at sdio->blocks_available. These are in units of the 59993407472SFabian Frederick * fs blocksize, i_blocksize(inode). 6001da177e4SLinus Torvalds * 6011da177e4SLinus Torvalds * The fs is allowed to map lots of blocks at once. If it wants to do that, 6021da177e4SLinus Torvalds * it uses the passed inode-relative block number as the file offset, as usual. 6031da177e4SLinus Torvalds * 6041d8fa7a2SBadari Pulavarty * get_block() is passed the number of i_blkbits-sized blocks which direct_io 6051da177e4SLinus Torvalds * has remaining to do. The fs should not map more than this number of blocks. 6061da177e4SLinus Torvalds * 6071da177e4SLinus Torvalds * If the fs has mapped a lot of blocks, it should populate bh->b_size to 6081da177e4SLinus Torvalds * indicate how much contiguous disk space has been made available at 6091da177e4SLinus Torvalds * bh->b_blocknr. 6101da177e4SLinus Torvalds * 6111da177e4SLinus Torvalds * If *any* of the mapped blocks are new, then the fs must set buffer_new(). 6121da177e4SLinus Torvalds * This isn't very efficient... 6131da177e4SLinus Torvalds * 6141da177e4SLinus Torvalds * In the case of filesystem holes: the fs may return an arbitrarily-large 6151da177e4SLinus Torvalds * hole by returning an appropriate value in b_size and by clearing 6161da177e4SLinus Torvalds * buffer_mapped(). However the direct-io code will only process holes one 6171d8fa7a2SBadari Pulavarty * block at a time - it will repeatedly call get_block() as it walks the hole. 6181da177e4SLinus Torvalds */ 61918772641SAndi Kleen static int get_more_blocks(struct dio *dio, struct dio_submit *sdio, 62018772641SAndi Kleen struct buffer_head *map_bh) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds int ret; 6231da177e4SLinus Torvalds sector_t fs_startblk; /* Into file, in filesystem-sized blocks */ 624ae55e1aaSTao Ma sector_t fs_endblk; /* Into file, in filesystem-sized blocks */ 6251da177e4SLinus Torvalds unsigned long fs_count; /* Number of filesystem-sized blocks */ 6261da177e4SLinus Torvalds int create; 627ab73857eSLinus Torvalds unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor; 6288b9433ebSErnesto A. Fernández loff_t i_size; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * If there was a memory error and we've overwritten all the 6321da177e4SLinus Torvalds * mapped blocks then we can now return that memory error 6331da177e4SLinus Torvalds */ 6341da177e4SLinus Torvalds ret = dio->page_errors; 6351da177e4SLinus Torvalds if (ret == 0) { 636eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file >= sdio->final_block_in_request); 637eb28be2bSAndi Kleen fs_startblk = sdio->block_in_file >> sdio->blkfactor; 638ae55e1aaSTao Ma fs_endblk = (sdio->final_block_in_request - 1) >> 639ae55e1aaSTao Ma sdio->blkfactor; 640ae55e1aaSTao Ma fs_count = fs_endblk - fs_startblk + 1; 6411da177e4SLinus Torvalds 6423c674e74SNathan Scott map_bh->b_state = 0; 643ab73857eSLinus Torvalds map_bh->b_size = fs_count << i_blkbits; 6443c674e74SNathan Scott 6455fe878aeSChristoph Hellwig /* 6469ecd10b7SEryu Guan * For writes that could fill holes inside i_size on a 6479ecd10b7SEryu Guan * DIO_SKIP_HOLES filesystem we forbid block creations: only 6489ecd10b7SEryu Guan * overwrites are permitted. We will return early to the caller 6499ecd10b7SEryu Guan * once we see an unmapped buffer head returned, and the caller 6509ecd10b7SEryu Guan * will fall back to buffered I/O. 6515fe878aeSChristoph Hellwig * 6525fe878aeSChristoph Hellwig * Otherwise the decision is left to the get_blocks method, 6535fe878aeSChristoph Hellwig * which may decide to handle it or also return an unmapped 6545fe878aeSChristoph Hellwig * buffer head. 6555fe878aeSChristoph Hellwig */ 6568a4c1e42SMike Christie create = dio->op == REQ_OP_WRITE; 6575fe878aeSChristoph Hellwig if (dio->flags & DIO_SKIP_HOLES) { 6588b9433ebSErnesto A. Fernández i_size = i_size_read(dio->inode); 6598b9433ebSErnesto A. Fernández if (i_size && fs_startblk <= (i_size - 1) >> i_blkbits) 6601da177e4SLinus Torvalds create = 0; 6611da177e4SLinus Torvalds } 6623c674e74SNathan Scott 663eb28be2bSAndi Kleen ret = (*sdio->get_block)(dio->inode, fs_startblk, 6641da177e4SLinus Torvalds map_bh, create); 66518772641SAndi Kleen 66618772641SAndi Kleen /* Store for completion */ 66718772641SAndi Kleen dio->private = map_bh->b_private; 6687b7a8665SChristoph Hellwig 6697b7a8665SChristoph Hellwig if (ret == 0 && buffer_defer_completion(map_bh)) 6707b7a8665SChristoph Hellwig ret = dio_set_defer_completion(dio); 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds return ret; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* 6761da177e4SLinus Torvalds * There is no bio. Make one now. 6771da177e4SLinus Torvalds */ 678ba253fbfSAndi Kleen static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio, 67918772641SAndi Kleen sector_t start_sector, struct buffer_head *map_bh) 6801da177e4SLinus Torvalds { 6811da177e4SLinus Torvalds sector_t sector; 6821da177e4SLinus Torvalds int ret, nr_pages; 6831da177e4SLinus Torvalds 684eb28be2bSAndi Kleen ret = dio_bio_reap(dio, sdio); 6851da177e4SLinus Torvalds if (ret) 6861da177e4SLinus Torvalds goto out; 687eb28be2bSAndi Kleen sector = start_sector << (sdio->blkbits - 9); 6885f7136dbSMatthew Wilcox (Oracle) nr_pages = bio_max_segs(sdio->pages_in_io); 6891da177e4SLinus Torvalds BUG_ON(nr_pages <= 0); 69018772641SAndi Kleen dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages); 691eb28be2bSAndi Kleen sdio->boundary = 0; 6921da177e4SLinus Torvalds out: 6931da177e4SLinus Torvalds return ret; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* 6971da177e4SLinus Torvalds * Attempt to put the current chunk of 'cur_page' into the current BIO. If 6981da177e4SLinus Torvalds * that was successful then update final_block_in_bio and take a ref against 6991da177e4SLinus Torvalds * the just-added page. 7001da177e4SLinus Torvalds * 7011da177e4SLinus Torvalds * Return zero on success. Non-zero means the caller needs to start a new BIO. 7021da177e4SLinus Torvalds */ 703ba253fbfSAndi Kleen static inline int dio_bio_add_page(struct dio_submit *sdio) 7041da177e4SLinus Torvalds { 7051da177e4SLinus Torvalds int ret; 7061da177e4SLinus Torvalds 707eb28be2bSAndi Kleen ret = bio_add_page(sdio->bio, sdio->cur_page, 708eb28be2bSAndi Kleen sdio->cur_page_len, sdio->cur_page_offset); 709eb28be2bSAndi Kleen if (ret == sdio->cur_page_len) { 7101da177e4SLinus Torvalds /* 7111da177e4SLinus Torvalds * Decrement count only, if we are done with this page 7121da177e4SLinus Torvalds */ 713eb28be2bSAndi Kleen if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE) 714eb28be2bSAndi Kleen sdio->pages_in_io--; 71509cbfeafSKirill A. Shutemov get_page(sdio->cur_page); 716eb28be2bSAndi Kleen sdio->final_block_in_bio = sdio->cur_page_block + 717eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits); 7181da177e4SLinus Torvalds ret = 0; 7191da177e4SLinus Torvalds } else { 7201da177e4SLinus Torvalds ret = 1; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds return ret; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds /* 7261da177e4SLinus Torvalds * Put cur_page under IO. The section of cur_page which is described by 7271da177e4SLinus Torvalds * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page 7281da177e4SLinus Torvalds * starts on-disk at cur_page_block. 7291da177e4SLinus Torvalds * 7301da177e4SLinus Torvalds * We take a ref against the page here (on behalf of its presence in the bio). 7311da177e4SLinus Torvalds * 7321da177e4SLinus Torvalds * The caller of this function is responsible for removing cur_page from the 7331da177e4SLinus Torvalds * dio, and for dropping the refcount which came from that presence. 7341da177e4SLinus Torvalds */ 735ba253fbfSAndi Kleen static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio, 73618772641SAndi Kleen struct buffer_head *map_bh) 7371da177e4SLinus Torvalds { 7381da177e4SLinus Torvalds int ret = 0; 7391da177e4SLinus Torvalds 740eb28be2bSAndi Kleen if (sdio->bio) { 741eb28be2bSAndi Kleen loff_t cur_offset = sdio->cur_page_fs_offset; 742eb28be2bSAndi Kleen loff_t bio_next_offset = sdio->logical_offset_in_bio + 7434f024f37SKent Overstreet sdio->bio->bi_iter.bi_size; 744c2c6ca41SJosef Bacik 7451da177e4SLinus Torvalds /* 746c2c6ca41SJosef Bacik * See whether this new request is contiguous with the old. 747c2c6ca41SJosef Bacik * 748f0940ceeSNamhyung Kim * Btrfs cannot handle having logically non-contiguous requests 749f0940ceeSNamhyung Kim * submitted. For example if you have 750c2c6ca41SJosef Bacik * 751c2c6ca41SJosef Bacik * Logical: [0-4095][HOLE][8192-12287] 752f0940ceeSNamhyung Kim * Physical: [0-4095] [4096-8191] 753c2c6ca41SJosef Bacik * 754c2c6ca41SJosef Bacik * We cannot submit those pages together as one BIO. So if our 755c2c6ca41SJosef Bacik * current logical offset in the file does not equal what would 756c2c6ca41SJosef Bacik * be the next logical offset in the bio, submit the bio we 757c2c6ca41SJosef Bacik * have. 7581da177e4SLinus Torvalds */ 759eb28be2bSAndi Kleen if (sdio->final_block_in_bio != sdio->cur_page_block || 760c2c6ca41SJosef Bacik cur_offset != bio_next_offset) 761eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 764eb28be2bSAndi Kleen if (sdio->bio == NULL) { 76518772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 7661da177e4SLinus Torvalds if (ret) 7671da177e4SLinus Torvalds goto out; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 770eb28be2bSAndi Kleen if (dio_bio_add_page(sdio) != 0) { 771eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 77218772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 7731da177e4SLinus Torvalds if (ret == 0) { 774eb28be2bSAndi Kleen ret = dio_bio_add_page(sdio); 7751da177e4SLinus Torvalds BUG_ON(ret != 0); 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds out: 7791da177e4SLinus Torvalds return ret; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * An autonomous function to put a chunk of a page under deferred IO. 7841da177e4SLinus Torvalds * 7851da177e4SLinus Torvalds * The caller doesn't actually know (or care) whether this piece of page is in 7861da177e4SLinus Torvalds * a BIO, or is under IO or whatever. We just take care of all possible 7871da177e4SLinus Torvalds * situations here. The separation between the logic of do_direct_IO() and 7881da177e4SLinus Torvalds * that of submit_page_section() is important for clarity. Please don't break. 7891da177e4SLinus Torvalds * 7901da177e4SLinus Torvalds * The chunk of page starts on-disk at blocknr. 7911da177e4SLinus Torvalds * 7921da177e4SLinus Torvalds * We perform deferred IO, by recording the last-submitted page inside our 7931da177e4SLinus Torvalds * private part of the dio structure. If possible, we just expand the IO 7941da177e4SLinus Torvalds * across that page here. 7951da177e4SLinus Torvalds * 7961da177e4SLinus Torvalds * If that doesn't work out then we put the old page into the bio and add this 7971da177e4SLinus Torvalds * page to the dio instead. 7981da177e4SLinus Torvalds */ 799ba253fbfSAndi Kleen static inline int 800eb28be2bSAndi Kleen submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page, 80118772641SAndi Kleen unsigned offset, unsigned len, sector_t blocknr, 80218772641SAndi Kleen struct buffer_head *map_bh) 8031da177e4SLinus Torvalds { 8041da177e4SLinus Torvalds int ret = 0; 805df41872bSJack Qiu int boundary = sdio->boundary; /* dio_send_cur_page may clear it */ 8061da177e4SLinus Torvalds 8078a4c1e42SMike Christie if (dio->op == REQ_OP_WRITE) { 80898c4d57dSAndrew Morton /* 80998c4d57dSAndrew Morton * Read accounting is performed in submit_bio() 81098c4d57dSAndrew Morton */ 81198c4d57dSAndrew Morton task_io_account_write(len); 81298c4d57dSAndrew Morton } 81398c4d57dSAndrew Morton 8141da177e4SLinus Torvalds /* 8151da177e4SLinus Torvalds * Can we just grow the current page's presence in the dio? 8161da177e4SLinus Torvalds */ 817eb28be2bSAndi Kleen if (sdio->cur_page == page && 818eb28be2bSAndi Kleen sdio->cur_page_offset + sdio->cur_page_len == offset && 819eb28be2bSAndi Kleen sdio->cur_page_block + 820eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits) == blocknr) { 821eb28be2bSAndi Kleen sdio->cur_page_len += len; 8221da177e4SLinus Torvalds goto out; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* 8261da177e4SLinus Torvalds * If there's a deferred page already there then send it. 8271da177e4SLinus Torvalds */ 828eb28be2bSAndi Kleen if (sdio->cur_page) { 82918772641SAndi Kleen ret = dio_send_cur_page(dio, sdio, map_bh); 83009cbfeafSKirill A. Shutemov put_page(sdio->cur_page); 831eb28be2bSAndi Kleen sdio->cur_page = NULL; 8321da177e4SLinus Torvalds if (ret) 833b1058b98SJan Kara return ret; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 83609cbfeafSKirill A. Shutemov get_page(page); /* It is in dio */ 837eb28be2bSAndi Kleen sdio->cur_page = page; 838eb28be2bSAndi Kleen sdio->cur_page_offset = offset; 839eb28be2bSAndi Kleen sdio->cur_page_len = len; 840eb28be2bSAndi Kleen sdio->cur_page_block = blocknr; 841eb28be2bSAndi Kleen sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits; 8421da177e4SLinus Torvalds out: 843b1058b98SJan Kara /* 844df41872bSJack Qiu * If boundary then we want to schedule the IO now to 845b1058b98SJan Kara * avoid metadata seeks. 846b1058b98SJan Kara */ 847df41872bSJack Qiu if (boundary) { 848b1058b98SJan Kara ret = dio_send_cur_page(dio, sdio, map_bh); 849899f0429SAndreas Gruenbacher if (sdio->bio) 850b1058b98SJan Kara dio_bio_submit(dio, sdio); 85109cbfeafSKirill A. Shutemov put_page(sdio->cur_page); 852b1058b98SJan Kara sdio->cur_page = NULL; 853b1058b98SJan Kara } 8541da177e4SLinus Torvalds return ret; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds /* 8581da177e4SLinus Torvalds * If we are not writing the entire block and get_block() allocated 8591da177e4SLinus Torvalds * the block for us, we need to fill-in the unused portion of the 8601da177e4SLinus Torvalds * block with zeros. This happens only if user-buffer, fileoffset or 8611da177e4SLinus Torvalds * io length is not filesystem block-size multiple. 8621da177e4SLinus Torvalds * 8631da177e4SLinus Torvalds * `end' is zero if we're doing the start of the IO, 1 at the end of the 8641da177e4SLinus Torvalds * IO. 8651da177e4SLinus Torvalds */ 866ba253fbfSAndi Kleen static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio, 867ba253fbfSAndi Kleen int end, struct buffer_head *map_bh) 8681da177e4SLinus Torvalds { 8691da177e4SLinus Torvalds unsigned dio_blocks_per_fs_block; 8701da177e4SLinus Torvalds unsigned this_chunk_blocks; /* In dio_blocks */ 8711da177e4SLinus Torvalds unsigned this_chunk_bytes; 8721da177e4SLinus Torvalds struct page *page; 8731da177e4SLinus Torvalds 874eb28be2bSAndi Kleen sdio->start_zero_done = 1; 87518772641SAndi Kleen if (!sdio->blkfactor || !buffer_new(map_bh)) 8761da177e4SLinus Torvalds return; 8771da177e4SLinus Torvalds 878eb28be2bSAndi Kleen dio_blocks_per_fs_block = 1 << sdio->blkfactor; 879eb28be2bSAndi Kleen this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds if (!this_chunk_blocks) 8821da177e4SLinus Torvalds return; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds /* 8851da177e4SLinus Torvalds * We need to zero out part of an fs block. It is either at the 8861da177e4SLinus Torvalds * beginning or the end of the fs block. 8871da177e4SLinus Torvalds */ 8881da177e4SLinus Torvalds if (end) 8891da177e4SLinus Torvalds this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks; 8901da177e4SLinus Torvalds 891eb28be2bSAndi Kleen this_chunk_bytes = this_chunk_blocks << sdio->blkbits; 8921da177e4SLinus Torvalds 893557ed1faSNick Piggin page = ZERO_PAGE(0); 894eb28be2bSAndi Kleen if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes, 89518772641SAndi Kleen sdio->next_block_for_io, map_bh)) 8961da177e4SLinus Torvalds return; 8971da177e4SLinus Torvalds 898eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* 9021da177e4SLinus Torvalds * Walk the user pages, and the file, mapping blocks to disk and generating 9031da177e4SLinus Torvalds * a sequence of (page,offset,len,block) mappings. These mappings are injected 9041da177e4SLinus Torvalds * into submit_page_section(), which takes care of the next stage of submission 9051da177e4SLinus Torvalds * 9061da177e4SLinus Torvalds * Direct IO against a blockdev is different from a file. Because we can 9071da177e4SLinus Torvalds * happily perform page-sized but 512-byte aligned IOs. It is important that 9081da177e4SLinus Torvalds * blockdev IO be able to have fine alignment and large sizes. 9091da177e4SLinus Torvalds * 9101d8fa7a2SBadari Pulavarty * So what we do is to permit the ->get_block function to populate bh.b_size 9111da177e4SLinus Torvalds * with the size of IO which is permitted at this offset and this i_blkbits. 9121da177e4SLinus Torvalds * 9131da177e4SLinus Torvalds * For best results, the blockdev should be set up with 512-byte i_blkbits and 9141d8fa7a2SBadari Pulavarty * it should set b_size to PAGE_SIZE or more inside get_block(). This gives 9151da177e4SLinus Torvalds * fine alignment but still allows this function to work in PAGE_SIZE units. 9161da177e4SLinus Torvalds */ 91718772641SAndi Kleen static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, 91818772641SAndi Kleen struct buffer_head *map_bh) 9191da177e4SLinus Torvalds { 920eb28be2bSAndi Kleen const unsigned blkbits = sdio->blkbits; 921dd545b52SChandan Rajendra const unsigned i_blkbits = blkbits + sdio->blkfactor; 9221da177e4SLinus Torvalds int ret = 0; 9231da177e4SLinus Torvalds 924eb28be2bSAndi Kleen while (sdio->block_in_file < sdio->final_block_in_request) { 9257b2c99d1SAl Viro struct page *page; 9267b2c99d1SAl Viro size_t from, to; 9276fcc5420SBoaz Harrosh 9286fcc5420SBoaz Harrosh page = dio_get_page(dio, sdio); 9291da177e4SLinus Torvalds if (IS_ERR(page)) { 9301da177e4SLinus Torvalds ret = PTR_ERR(page); 9311da177e4SLinus Torvalds goto out; 9321da177e4SLinus Torvalds } 9336fcc5420SBoaz Harrosh from = sdio->head ? 0 : sdio->from; 9346fcc5420SBoaz Harrosh to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE; 9356fcc5420SBoaz Harrosh sdio->head++; 9361da177e4SLinus Torvalds 9377b2c99d1SAl Viro while (from < to) { 9381da177e4SLinus Torvalds unsigned this_chunk_bytes; /* # of bytes mapped */ 9391da177e4SLinus Torvalds unsigned this_chunk_blocks; /* # of blocks */ 9401da177e4SLinus Torvalds unsigned u; 9411da177e4SLinus Torvalds 942eb28be2bSAndi Kleen if (sdio->blocks_available == 0) { 9431da177e4SLinus Torvalds /* 9441da177e4SLinus Torvalds * Need to go and map some more disk 9451da177e4SLinus Torvalds */ 9461da177e4SLinus Torvalds unsigned long blkmask; 9471da177e4SLinus Torvalds unsigned long dio_remainder; 9481da177e4SLinus Torvalds 94918772641SAndi Kleen ret = get_more_blocks(dio, sdio, map_bh); 9501da177e4SLinus Torvalds if (ret) { 95109cbfeafSKirill A. Shutemov put_page(page); 9521da177e4SLinus Torvalds goto out; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) 9551da177e4SLinus Torvalds goto do_holes; 9561da177e4SLinus Torvalds 957eb28be2bSAndi Kleen sdio->blocks_available = 958f734c89cSJan Kara map_bh->b_size >> blkbits; 959eb28be2bSAndi Kleen sdio->next_block_for_io = 960eb28be2bSAndi Kleen map_bh->b_blocknr << sdio->blkfactor; 961f734c89cSJan Kara if (buffer_new(map_bh)) { 962f734c89cSJan Kara clean_bdev_aliases( 963f734c89cSJan Kara map_bh->b_bdev, 964f734c89cSJan Kara map_bh->b_blocknr, 965dd545b52SChandan Rajendra map_bh->b_size >> i_blkbits); 966f734c89cSJan Kara } 9671da177e4SLinus Torvalds 968eb28be2bSAndi Kleen if (!sdio->blkfactor) 9691da177e4SLinus Torvalds goto do_holes; 9701da177e4SLinus Torvalds 971eb28be2bSAndi Kleen blkmask = (1 << sdio->blkfactor) - 1; 972eb28be2bSAndi Kleen dio_remainder = (sdio->block_in_file & blkmask); 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds /* 9751da177e4SLinus Torvalds * If we are at the start of IO and that IO 9761da177e4SLinus Torvalds * starts partway into a fs-block, 9771da177e4SLinus Torvalds * dio_remainder will be non-zero. If the IO 9781da177e4SLinus Torvalds * is a read then we can simply advance the IO 9791da177e4SLinus Torvalds * cursor to the first block which is to be 9801da177e4SLinus Torvalds * read. But if the IO is a write and the 9811da177e4SLinus Torvalds * block was newly allocated we cannot do that; 9821da177e4SLinus Torvalds * the start of the fs block must be zeroed out 9831da177e4SLinus Torvalds * on-disk 9841da177e4SLinus Torvalds */ 9851da177e4SLinus Torvalds if (!buffer_new(map_bh)) 986eb28be2bSAndi Kleen sdio->next_block_for_io += dio_remainder; 987eb28be2bSAndi Kleen sdio->blocks_available -= dio_remainder; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds do_holes: 9901da177e4SLinus Torvalds /* Handle holes */ 9911da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) { 99235dc8161SJeff Moyer loff_t i_size_aligned; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* AKPM: eargh, -ENOTBLK is a hack */ 9958a4c1e42SMike Christie if (dio->op == REQ_OP_WRITE) { 99609cbfeafSKirill A. Shutemov put_page(page); 9971da177e4SLinus Torvalds return -ENOTBLK; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 100035dc8161SJeff Moyer /* 100135dc8161SJeff Moyer * Be sure to account for a partial block as the 100235dc8161SJeff Moyer * last block in the file 100335dc8161SJeff Moyer */ 100435dc8161SJeff Moyer i_size_aligned = ALIGN(i_size_read(dio->inode), 100535dc8161SJeff Moyer 1 << blkbits); 1006eb28be2bSAndi Kleen if (sdio->block_in_file >= 100735dc8161SJeff Moyer i_size_aligned >> blkbits) { 10081da177e4SLinus Torvalds /* We hit eof */ 100909cbfeafSKirill A. Shutemov put_page(page); 10101da177e4SLinus Torvalds goto out; 10111da177e4SLinus Torvalds } 10127b2c99d1SAl Viro zero_user(page, from, 1 << blkbits); 1013eb28be2bSAndi Kleen sdio->block_in_file++; 10147b2c99d1SAl Viro from += 1 << blkbits; 10153320c60bSAl Viro dio->result += 1 << blkbits; 10161da177e4SLinus Torvalds goto next_block; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds /* 10201da177e4SLinus Torvalds * If we're performing IO which has an alignment which 10211da177e4SLinus Torvalds * is finer than the underlying fs, go check to see if 10221da177e4SLinus Torvalds * we must zero out the start of this block. 10231da177e4SLinus Torvalds */ 1024eb28be2bSAndi Kleen if (unlikely(sdio->blkfactor && !sdio->start_zero_done)) 102518772641SAndi Kleen dio_zero_block(dio, sdio, 0, map_bh); 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds /* 10281da177e4SLinus Torvalds * Work out, in this_chunk_blocks, how much disk we 10291da177e4SLinus Torvalds * can add to this page 10301da177e4SLinus Torvalds */ 1031eb28be2bSAndi Kleen this_chunk_blocks = sdio->blocks_available; 10327b2c99d1SAl Viro u = (to - from) >> blkbits; 10331da177e4SLinus Torvalds if (this_chunk_blocks > u) 10341da177e4SLinus Torvalds this_chunk_blocks = u; 1035eb28be2bSAndi Kleen u = sdio->final_block_in_request - sdio->block_in_file; 10361da177e4SLinus Torvalds if (this_chunk_blocks > u) 10371da177e4SLinus Torvalds this_chunk_blocks = u; 10381da177e4SLinus Torvalds this_chunk_bytes = this_chunk_blocks << blkbits; 10391da177e4SLinus Torvalds BUG_ON(this_chunk_bytes == 0); 10401da177e4SLinus Torvalds 1041092c8d46SJan Kara if (this_chunk_blocks == sdio->blocks_available) 1042eb28be2bSAndi Kleen sdio->boundary = buffer_boundary(map_bh); 1043eb28be2bSAndi Kleen ret = submit_page_section(dio, sdio, page, 10447b2c99d1SAl Viro from, 1045eb28be2bSAndi Kleen this_chunk_bytes, 104618772641SAndi Kleen sdio->next_block_for_io, 104718772641SAndi Kleen map_bh); 10481da177e4SLinus Torvalds if (ret) { 104909cbfeafSKirill A. Shutemov put_page(page); 10501da177e4SLinus Torvalds goto out; 10511da177e4SLinus Torvalds } 1052eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 10531da177e4SLinus Torvalds 1054eb28be2bSAndi Kleen sdio->block_in_file += this_chunk_blocks; 10557b2c99d1SAl Viro from += this_chunk_bytes; 10567b2c99d1SAl Viro dio->result += this_chunk_bytes; 1057eb28be2bSAndi Kleen sdio->blocks_available -= this_chunk_blocks; 10581da177e4SLinus Torvalds next_block: 1059eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file > sdio->final_block_in_request); 1060eb28be2bSAndi Kleen if (sdio->block_in_file == sdio->final_block_in_request) 10611da177e4SLinus Torvalds break; 10621da177e4SLinus Torvalds } 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /* Drop the ref which was taken in get_user_pages() */ 106509cbfeafSKirill A. Shutemov put_page(page); 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds out: 10681da177e4SLinus Torvalds return ret; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 1071847cc637SAndi Kleen static inline int drop_refcount(struct dio *dio) 10721da177e4SLinus Torvalds { 1073847cc637SAndi Kleen int ret2; 10745eb6c7a2SZach Brown unsigned long flags; 107520258b2bSZach Brown 10761da177e4SLinus Torvalds /* 10778459d86aSZach Brown * Sync will always be dropping the final ref and completing the 10785eb6c7a2SZach Brown * operation. AIO can if it was a broken operation described above or 10795eb6c7a2SZach Brown * in fact if all the bios race to complete before we get here. In 10805eb6c7a2SZach Brown * that case dio_complete() translates the EIOCBQUEUED into the proper 108104b2fa9fSChristoph Hellwig * return code that the caller will hand to ->complete(). 10825eb6c7a2SZach Brown * 10835eb6c7a2SZach Brown * This is managed by the bio_lock instead of being an atomic_t so that 10845eb6c7a2SZach Brown * completion paths can drop their ref and use the remaining count to 10855eb6c7a2SZach Brown * decide to wake the submission path atomically. 10861da177e4SLinus Torvalds */ 10875eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 10885eb6c7a2SZach Brown ret2 = --dio->refcount; 10895eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 1090847cc637SAndi Kleen return ret2; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 1093eafdc7d1SChristoph Hellwig /* 1094eafdc7d1SChristoph Hellwig * This is a library function for use by filesystem drivers. 1095eafdc7d1SChristoph Hellwig * 1096eafdc7d1SChristoph Hellwig * The locking rules are governed by the flags parameter: 1097eafdc7d1SChristoph Hellwig * - if the flags value contains DIO_LOCKING we use a fancy locking 1098eafdc7d1SChristoph Hellwig * scheme for dumb filesystems. 1099eafdc7d1SChristoph Hellwig * For writes this function is called under i_mutex and returns with 1100eafdc7d1SChristoph Hellwig * i_mutex held, for reads, i_mutex is not held on entry, but it is 1101eafdc7d1SChristoph Hellwig * taken and dropped again before returning. 1102eafdc7d1SChristoph Hellwig * - if the flags value does NOT contain DIO_LOCKING we don't use any 1103eafdc7d1SChristoph Hellwig * internal locking but rather rely on the filesystem to synchronize 1104eafdc7d1SChristoph Hellwig * direct I/O reads/writes versus each other and truncate. 1105df2d6f26SChristoph Hellwig * 1106df2d6f26SChristoph Hellwig * To help with locking against truncate we incremented the i_dio_count 1107df2d6f26SChristoph Hellwig * counter before starting direct I/O, and decrement it once we are done. 1108df2d6f26SChristoph Hellwig * Truncate can wait for it to reach zero to provide exclusion. It is 1109df2d6f26SChristoph Hellwig * expected that filesystem provide exclusion between new direct I/O 1110df2d6f26SChristoph Hellwig * and truncates. For DIO_LOCKING filesystems this is done by i_mutex, 1111df2d6f26SChristoph Hellwig * but other filesystems need to take care of this on their own. 1112ba253fbfSAndi Kleen * 1113ba253fbfSAndi Kleen * NOTE: if you pass "sdio" to anything by pointer make sure that function 1114ba253fbfSAndi Kleen * is always inlined. Otherwise gcc is unable to split the structure into 1115ba253fbfSAndi Kleen * individual fields and will generate much worse code. This is important 1116ba253fbfSAndi Kleen * for the whole file. 1117eafdc7d1SChristoph Hellwig */ 1118c22198e7SChristoph Hellwig ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, 111917f8c842SOmar Sandoval struct block_device *bdev, struct iov_iter *iter, 1120c8b8e32dSChristoph Hellwig get_block_t get_block, dio_iodone_t end_io, 1121facd07b0SJosef Bacik dio_submit_t submit_io, int flags) 11221da177e4SLinus Torvalds { 11236aa7de05SMark Rutland unsigned i_blkbits = READ_ONCE(inode->i_blkbits); 1124ab73857eSLinus Torvalds unsigned blkbits = i_blkbits; 11251da177e4SLinus Torvalds unsigned blocksize_mask = (1 << blkbits) - 1; 11261da177e4SLinus Torvalds ssize_t retval = -EINVAL; 11271c0ff0f1SNikolay Borisov const size_t count = iov_iter_count(iter); 1128c8b8e32dSChristoph Hellwig loff_t offset = iocb->ki_pos; 11291c0ff0f1SNikolay Borisov const loff_t end = offset + count; 11301da177e4SLinus Torvalds struct dio *dio; 1131eb28be2bSAndi Kleen struct dio_submit sdio = { 0, }; 1132847cc637SAndi Kleen struct buffer_head map_bh = { 0, }; 1133647d1e4cSFengguang Wu struct blk_plug plug; 1134886a3911SAl Viro unsigned long align = offset | iov_iter_alignment(iter); 11351da177e4SLinus Torvalds 113665dd2aa9SAndi Kleen /* 113765dd2aa9SAndi Kleen * Avoid references to bdev if not absolutely needed to give 113865dd2aa9SAndi Kleen * the early prefetch in the caller enough time. 113965dd2aa9SAndi Kleen */ 11401da177e4SLinus Torvalds 1141f9b5570dSChristoph Hellwig /* watch out for a 0 len io from a tricksy fs */ 11421c0ff0f1SNikolay Borisov if (iov_iter_rw(iter) == READ && !count) 1143f9b5570dSChristoph Hellwig return 0; 1144f9b5570dSChristoph Hellwig 11456e8267f5SAndi Kleen dio = kmem_cache_alloc(dio_cache, GFP_KERNEL); 11461da177e4SLinus Torvalds if (!dio) 114746d71602SGabriel Krisman Bertazi return -ENOMEM; 114823aee091SJeff Moyer /* 114923aee091SJeff Moyer * Believe it or not, zeroing out the page array caused a .5% 115023aee091SJeff Moyer * performance regression in a database benchmark. So, we take 115123aee091SJeff Moyer * care to only zero out what's needed. 115223aee091SJeff Moyer */ 115323aee091SJeff Moyer memset(dio, 0, offsetof(struct dio, pages)); 11541da177e4SLinus Torvalds 11555fe878aeSChristoph Hellwig dio->flags = flags; 11560a9164cbSGabriel Krisman Bertazi if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) { 11575fe878aeSChristoph Hellwig /* will be released by direct_io_worker */ 11585955102cSAl Viro inode_lock(inode); 1159df2d6f26SChristoph Hellwig } 11601da177e4SLinus Torvalds 116174cedf9bSJan Kara /* Once we sampled i_size check for reads beyond EOF */ 116274cedf9bSJan Kara dio->i_size = i_size_read(inode); 116374cedf9bSJan Kara if (iov_iter_rw(iter) == READ && offset >= dio->i_size) { 11642d4594acSAl Viro retval = 0; 116546d71602SGabriel Krisman Bertazi goto fail_dio; 116674cedf9bSJan Kara } 116774cedf9bSJan Kara 116841b21af3SGabriel Krisman Bertazi if (align & blocksize_mask) { 116941b21af3SGabriel Krisman Bertazi if (bdev) 117041b21af3SGabriel Krisman Bertazi blkbits = blksize_bits(bdev_logical_block_size(bdev)); 117141b21af3SGabriel Krisman Bertazi blocksize_mask = (1 << blkbits) - 1; 117241b21af3SGabriel Krisman Bertazi if (align & blocksize_mask) 117341b21af3SGabriel Krisman Bertazi goto fail_dio; 117441b21af3SGabriel Krisman Bertazi } 117541b21af3SGabriel Krisman Bertazi 11760a9164cbSGabriel Krisman Bertazi if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) { 11770a9164cbSGabriel Krisman Bertazi struct address_space *mapping = iocb->ki_filp->f_mapping; 11780a9164cbSGabriel Krisman Bertazi 11790a9164cbSGabriel Krisman Bertazi retval = filemap_write_and_wait_range(mapping, offset, end - 1); 11800a9164cbSGabriel Krisman Bertazi if (retval) 11810a9164cbSGabriel Krisman Bertazi goto fail_dio; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 11845fe878aeSChristoph Hellwig /* 118560392573SChristoph Hellwig * For file extending writes updating i_size before data writeouts 118660392573SChristoph Hellwig * complete can expose uninitialized blocks in dumb filesystems. 118760392573SChristoph Hellwig * In that case we need to wait for I/O completion even if asked 118860392573SChristoph Hellwig * for an asynchronous write. 11891da177e4SLinus Torvalds */ 119060392573SChristoph Hellwig if (is_sync_kiocb(iocb)) 119160392573SChristoph Hellwig dio->is_async = false; 1192c8f4c36fSNikolay Borisov else if (iov_iter_rw(iter) == WRITE && end > i_size_read(inode)) 119360392573SChristoph Hellwig dio->is_async = false; 119460392573SChristoph Hellwig else 119560392573SChristoph Hellwig dio->is_async = true; 119660392573SChristoph Hellwig 1197847cc637SAndi Kleen dio->inode = inode; 11988a4c1e42SMike Christie if (iov_iter_rw(iter) == WRITE) { 11998a4c1e42SMike Christie dio->op = REQ_OP_WRITE; 120070fd7614SChristoph Hellwig dio->op_flags = REQ_SYNC | REQ_IDLE; 120103a07c92SGoldwyn Rodrigues if (iocb->ki_flags & IOCB_NOWAIT) 120203a07c92SGoldwyn Rodrigues dio->op_flags |= REQ_NOWAIT; 12038a4c1e42SMike Christie } else { 12048a4c1e42SMike Christie dio->op = REQ_OP_READ; 12058a4c1e42SMike Christie } 120602afc27fSChristoph Hellwig 120702afc27fSChristoph Hellwig /* 120802afc27fSChristoph Hellwig * For AIO O_(D)SYNC writes we need to defer completions to a workqueue 120902afc27fSChristoph Hellwig * so that we can call ->fsync. 121002afc27fSChristoph Hellwig */ 1211332391a9SLukas Czerner if (dio->is_async && iov_iter_rw(iter) == WRITE) { 1212332391a9SLukas Czerner retval = 0; 1213*91b94c5dSAl Viro if (iocb_is_dsync(iocb)) 121402afc27fSChristoph Hellwig retval = dio_set_defer_completion(dio); 1215332391a9SLukas Czerner else if (!dio->inode->i_sb->s_dio_done_wq) { 1216332391a9SLukas Czerner /* 1217332391a9SLukas Czerner * In case of AIO write racing with buffered read we 1218332391a9SLukas Czerner * need to defer completion. We can't decide this now, 1219332391a9SLukas Czerner * however the workqueue needs to be initialized here. 1220332391a9SLukas Czerner */ 1221332391a9SLukas Czerner retval = sb_init_dio_done_wq(dio->inode->i_sb); 1222332391a9SLukas Czerner } 122346d71602SGabriel Krisman Bertazi if (retval) 122446d71602SGabriel Krisman Bertazi goto fail_dio; 122502afc27fSChristoph Hellwig } 122602afc27fSChristoph Hellwig 122702afc27fSChristoph Hellwig /* 122802afc27fSChristoph Hellwig * Will be decremented at I/O completion time. 122902afc27fSChristoph Hellwig */ 1230fe0f07d0SJens Axboe inode_dio_begin(inode); 123102afc27fSChristoph Hellwig 123202afc27fSChristoph Hellwig retval = 0; 1233847cc637SAndi Kleen sdio.blkbits = blkbits; 1234ab73857eSLinus Torvalds sdio.blkfactor = i_blkbits - blkbits; 1235847cc637SAndi Kleen sdio.block_in_file = offset >> blkbits; 1236847cc637SAndi Kleen 1237847cc637SAndi Kleen sdio.get_block = get_block; 1238847cc637SAndi Kleen dio->end_io = end_io; 1239847cc637SAndi Kleen sdio.submit_io = submit_io; 1240847cc637SAndi Kleen sdio.final_block_in_bio = -1; 1241847cc637SAndi Kleen sdio.next_block_for_io = -1; 1242847cc637SAndi Kleen 1243847cc637SAndi Kleen dio->iocb = iocb; 1244847cc637SAndi Kleen 1245847cc637SAndi Kleen spin_lock_init(&dio->bio_lock); 1246847cc637SAndi Kleen dio->refcount = 1; 1247847cc637SAndi Kleen 124800e23707SDavid Howells dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ; 12497b2c99d1SAl Viro sdio.iter = iter; 12501c0ff0f1SNikolay Borisov sdio.final_block_in_request = end >> blkbits; 12517b2c99d1SAl Viro 1252847cc637SAndi Kleen /* 1253847cc637SAndi Kleen * In case of non-aligned buffers, we may need 2 more 1254847cc637SAndi Kleen * pages since we need to zero out first and last block. 1255847cc637SAndi Kleen */ 1256847cc637SAndi Kleen if (unlikely(sdio.blkfactor)) 1257847cc637SAndi Kleen sdio.pages_in_io = 2; 1258847cc637SAndi Kleen 1259f67da30cSAl Viro sdio.pages_in_io += iov_iter_npages(iter, INT_MAX); 1260847cc637SAndi Kleen 1261647d1e4cSFengguang Wu blk_start_plug(&plug); 1262647d1e4cSFengguang Wu 1263847cc637SAndi Kleen retval = do_direct_IO(dio, &sdio, &map_bh); 12647b2c99d1SAl Viro if (retval) 1265847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1266847cc637SAndi Kleen 1267847cc637SAndi Kleen if (retval == -ENOTBLK) { 1268847cc637SAndi Kleen /* 1269847cc637SAndi Kleen * The remaining part of the request will be 12703d742d4bSRandy Dunlap * handled by buffered I/O when we return 1271847cc637SAndi Kleen */ 1272847cc637SAndi Kleen retval = 0; 1273847cc637SAndi Kleen } 1274847cc637SAndi Kleen /* 1275847cc637SAndi Kleen * There may be some unwritten disk at the end of a part-written 1276847cc637SAndi Kleen * fs-block-sized block. Go zero that now. 1277847cc637SAndi Kleen */ 1278847cc637SAndi Kleen dio_zero_block(dio, &sdio, 1, &map_bh); 1279847cc637SAndi Kleen 1280847cc637SAndi Kleen if (sdio.cur_page) { 1281847cc637SAndi Kleen ssize_t ret2; 1282847cc637SAndi Kleen 1283847cc637SAndi Kleen ret2 = dio_send_cur_page(dio, &sdio, &map_bh); 1284847cc637SAndi Kleen if (retval == 0) 1285847cc637SAndi Kleen retval = ret2; 128609cbfeafSKirill A. Shutemov put_page(sdio.cur_page); 1287847cc637SAndi Kleen sdio.cur_page = NULL; 1288847cc637SAndi Kleen } 1289847cc637SAndi Kleen if (sdio.bio) 1290847cc637SAndi Kleen dio_bio_submit(dio, &sdio); 1291847cc637SAndi Kleen 1292647d1e4cSFengguang Wu blk_finish_plug(&plug); 1293647d1e4cSFengguang Wu 1294847cc637SAndi Kleen /* 1295847cc637SAndi Kleen * It is possible that, we return short IO due to end of file. 1296847cc637SAndi Kleen * In that case, we need to release all the pages we got hold on. 1297847cc637SAndi Kleen */ 1298847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1299847cc637SAndi Kleen 1300847cc637SAndi Kleen /* 1301847cc637SAndi Kleen * All block lookups have been performed. For READ requests 1302847cc637SAndi Kleen * we can let i_mutex go now that its achieved its purpose 1303847cc637SAndi Kleen * of protecting us from looking up uninitialized blocks. 1304847cc637SAndi Kleen */ 130517f8c842SOmar Sandoval if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING)) 13065955102cSAl Viro inode_unlock(dio->inode); 1307847cc637SAndi Kleen 1308847cc637SAndi Kleen /* 1309847cc637SAndi Kleen * The only time we want to leave bios in flight is when a successful 1310847cc637SAndi Kleen * partial aio read or full aio write have been setup. In that case 1311847cc637SAndi Kleen * bio completion will call aio_complete. The only time it's safe to 1312847cc637SAndi Kleen * call aio_complete is when we return -EIOCBQUEUED, so we key on that. 1313847cc637SAndi Kleen * This had *better* be the only place that raises -EIOCBQUEUED. 1314847cc637SAndi Kleen */ 1315847cc637SAndi Kleen BUG_ON(retval == -EIOCBQUEUED); 1316847cc637SAndi Kleen if (dio->is_async && retval == 0 && dio->result && 131717f8c842SOmar Sandoval (iov_iter_rw(iter) == READ || dio->result == count)) 1318847cc637SAndi Kleen retval = -EIOCBQUEUED; 1319af436472SChristoph Hellwig else 1320847cc637SAndi Kleen dio_await_completion(dio); 1321847cc637SAndi Kleen 1322847cc637SAndi Kleen if (drop_refcount(dio) == 0) { 1323ffe51f01SLukas Czerner retval = dio_complete(dio, retval, DIO_COMPLETE_INVALIDATE); 1324847cc637SAndi Kleen } else 1325847cc637SAndi Kleen BUG_ON(retval != -EIOCBQUEUED); 13261da177e4SLinus Torvalds 132746d71602SGabriel Krisman Bertazi return retval; 132846d71602SGabriel Krisman Bertazi 132946d71602SGabriel Krisman Bertazi fail_dio: 133046d71602SGabriel Krisman Bertazi if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) 133146d71602SGabriel Krisman Bertazi inode_unlock(inode); 133246d71602SGabriel Krisman Bertazi 133346d71602SGabriel Krisman Bertazi kmem_cache_free(dio_cache, dio); 13347bb46a67Snpiggin@suse.de return retval; 13357bb46a67Snpiggin@suse.de } 13361da177e4SLinus Torvalds EXPORT_SYMBOL(__blockdev_direct_IO); 13376e8267f5SAndi Kleen 13386e8267f5SAndi Kleen static __init int dio_init(void) 13396e8267f5SAndi Kleen { 13406e8267f5SAndi Kleen dio_cache = KMEM_CACHE(dio, SLAB_PANIC); 13416e8267f5SAndi Kleen return 0; 13426e8267f5SAndi Kleen } 13436e8267f5SAndi Kleen module_init(dio_init) 1344