11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/direct-io.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * O_DIRECT 71da177e4SLinus Torvalds * 8e1f8e874SFrancois Cami * 04Jul2002 Andrew Morton 91da177e4SLinus Torvalds * Initial version 101da177e4SLinus Torvalds * 11Sep2002 janetinc@us.ibm.com 111da177e4SLinus Torvalds * added readv/writev support. 12e1f8e874SFrancois Cami * 29Oct2002 Andrew Morton 131da177e4SLinus Torvalds * rewrote bio_add_page() support. 141da177e4SLinus Torvalds * 30Oct2002 pbadari@us.ibm.com 151da177e4SLinus Torvalds * added support for non-aligned IO. 161da177e4SLinus Torvalds * 06Nov2002 pbadari@us.ibm.com 171da177e4SLinus Torvalds * added asynchronous IO support. 181da177e4SLinus Torvalds * 21Jul2003 nathans@sgi.com 191da177e4SLinus Torvalds * added IO completion notifier. 201da177e4SLinus Torvalds */ 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include <linux/kernel.h> 231da177e4SLinus Torvalds #include <linux/module.h> 241da177e4SLinus Torvalds #include <linux/types.h> 251da177e4SLinus Torvalds #include <linux/fs.h> 261da177e4SLinus Torvalds #include <linux/mm.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/highmem.h> 291da177e4SLinus Torvalds #include <linux/pagemap.h> 3098c4d57dSAndrew Morton #include <linux/task_io_accounting_ops.h> 311da177e4SLinus Torvalds #include <linux/bio.h> 321da177e4SLinus Torvalds #include <linux/wait.h> 331da177e4SLinus Torvalds #include <linux/err.h> 341da177e4SLinus Torvalds #include <linux/blkdev.h> 351da177e4SLinus Torvalds #include <linux/buffer_head.h> 361da177e4SLinus Torvalds #include <linux/rwsem.h> 371da177e4SLinus Torvalds #include <linux/uio.h> 3860063497SArun Sharma #include <linux/atomic.h> 3965dd2aa9SAndi Kleen #include <linux/prefetch.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds /* 421da177e4SLinus Torvalds * How many user pages to map in one call to get_user_pages(). This determines 43cde1ecb3SAndi Kleen * the size of a structure in the slab cache 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds #define DIO_PAGES 64 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds /* 48ffe51f01SLukas Czerner * Flags for dio_complete() 49ffe51f01SLukas Czerner */ 50ffe51f01SLukas Czerner #define DIO_COMPLETE_ASYNC 0x01 /* This is async IO */ 51ffe51f01SLukas Czerner #define DIO_COMPLETE_INVALIDATE 0x02 /* Can invalidate pages */ 52ffe51f01SLukas Czerner 53ffe51f01SLukas Czerner /* 541da177e4SLinus Torvalds * This code generally works in units of "dio_blocks". A dio_block is 551da177e4SLinus Torvalds * somewhere between the hard sector size and the filesystem block size. it 561da177e4SLinus Torvalds * is determined on a per-invocation basis. When talking to the filesystem 571da177e4SLinus Torvalds * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity 581da177e4SLinus Torvalds * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted 591da177e4SLinus Torvalds * to bio_block quantities by shifting left by blkfactor. 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * If blkfactor is zero then the user's request was aligned to the filesystem's 621da177e4SLinus Torvalds * blocksize. 631da177e4SLinus Torvalds */ 641da177e4SLinus Torvalds 65eb28be2bSAndi Kleen /* dio_state only used in the submission path */ 66eb28be2bSAndi Kleen 67eb28be2bSAndi Kleen struct dio_submit { 681da177e4SLinus Torvalds struct bio *bio; /* bio under assembly */ 691da177e4SLinus Torvalds unsigned blkbits; /* doesn't change */ 701da177e4SLinus Torvalds unsigned blkfactor; /* When we're using an alignment which 711da177e4SLinus Torvalds is finer than the filesystem's soft 721da177e4SLinus Torvalds blocksize, this specifies how much 731da177e4SLinus Torvalds finer. blkfactor=2 means 1/4-block 741da177e4SLinus Torvalds alignment. Does not change */ 751da177e4SLinus Torvalds unsigned start_zero_done; /* flag: sub-blocksize zeroing has 761da177e4SLinus Torvalds been performed at the start of a 771da177e4SLinus Torvalds write */ 781da177e4SLinus Torvalds int pages_in_io; /* approximate total IO pages */ 791da177e4SLinus Torvalds sector_t block_in_file; /* Current offset into the underlying 801da177e4SLinus Torvalds file in dio_block units. */ 811da177e4SLinus Torvalds unsigned blocks_available; /* At block_in_file. changes */ 820dc2bc49SAndi Kleen int reap_counter; /* rate limit reaping */ 831da177e4SLinus Torvalds sector_t final_block_in_request;/* doesn't change */ 841da177e4SLinus Torvalds int boundary; /* prev block is at a boundary */ 851d8fa7a2SBadari Pulavarty get_block_t *get_block; /* block mapping function */ 86facd07b0SJosef Bacik dio_submit_t *submit_io; /* IO submition function */ 87eb28be2bSAndi Kleen 88facd07b0SJosef Bacik loff_t logical_offset_in_bio; /* current first logical block in bio */ 891da177e4SLinus Torvalds sector_t final_block_in_bio; /* current final block in bio + 1 */ 901da177e4SLinus Torvalds sector_t next_block_for_io; /* next block to be put under IO, 911da177e4SLinus Torvalds in dio_blocks units */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* 941da177e4SLinus Torvalds * Deferred addition of a page to the dio. These variables are 951da177e4SLinus Torvalds * private to dio_send_cur_page(), submit_page_section() and 961da177e4SLinus Torvalds * dio_bio_add_page(). 971da177e4SLinus Torvalds */ 981da177e4SLinus Torvalds struct page *cur_page; /* The page */ 991da177e4SLinus Torvalds unsigned cur_page_offset; /* Offset into it, in bytes */ 1001da177e4SLinus Torvalds unsigned cur_page_len; /* Nr of bytes at cur_page_offset */ 1011da177e4SLinus Torvalds sector_t cur_page_block; /* Where it starts */ 102facd07b0SJosef Bacik loff_t cur_page_fs_offset; /* Offset in file */ 1031da177e4SLinus Torvalds 1047b2c99d1SAl Viro struct iov_iter *iter; 10523aee091SJeff Moyer /* 10623aee091SJeff Moyer * Page queue. These variables belong to dio_refill_pages() and 10723aee091SJeff Moyer * dio_get_page(). 10823aee091SJeff Moyer */ 10923aee091SJeff Moyer unsigned head; /* next page to process */ 11023aee091SJeff Moyer unsigned tail; /* last valid page + 1 */ 1117b2c99d1SAl Viro size_t from, to; 112eb28be2bSAndi Kleen }; 113eb28be2bSAndi Kleen 114eb28be2bSAndi Kleen /* dio_state communicated between submission path and end_io */ 115eb28be2bSAndi Kleen struct dio { 116eb28be2bSAndi Kleen int flags; /* doesn't change */ 1178a4c1e42SMike Christie int op; 1188a4c1e42SMike Christie int op_flags; 11915c4f638SJens Axboe blk_qc_t bio_cookie; 12074d46992SChristoph Hellwig struct gendisk *bio_disk; 1210dc2bc49SAndi Kleen struct inode *inode; 122eb28be2bSAndi Kleen loff_t i_size; /* i_size when submitted */ 123eb28be2bSAndi Kleen dio_iodone_t *end_io; /* IO completion function */ 124eb28be2bSAndi Kleen 12518772641SAndi Kleen void *private; /* copy from map_bh.b_private */ 126eb28be2bSAndi Kleen 127eb28be2bSAndi Kleen /* BIO completion state */ 128eb28be2bSAndi Kleen spinlock_t bio_lock; /* protects BIO fields below */ 1290dc2bc49SAndi Kleen int page_errors; /* errno from get_user_pages() */ 1300dc2bc49SAndi Kleen int is_async; /* is IO async ? */ 1317b7a8665SChristoph Hellwig bool defer_completion; /* defer AIO completion to workqueue? */ 13253cbf3b1SMing Lei bool should_dirty; /* if pages should be dirtied */ 1330dc2bc49SAndi Kleen int io_error; /* IO error in completion path */ 134eb28be2bSAndi Kleen unsigned long refcount; /* direct_io_worker() and bios */ 135eb28be2bSAndi Kleen struct bio *bio_list; /* singly linked via bi_private */ 136eb28be2bSAndi Kleen struct task_struct *waiter; /* waiting task (NULL if none) */ 137eb28be2bSAndi Kleen 138eb28be2bSAndi Kleen /* AIO related stuff */ 139eb28be2bSAndi Kleen struct kiocb *iocb; /* kiocb */ 140eb28be2bSAndi Kleen ssize_t result; /* IO result */ 141eb28be2bSAndi Kleen 14223aee091SJeff Moyer /* 14323aee091SJeff Moyer * pages[] (and any fields placed after it) are not zeroed out at 14423aee091SJeff Moyer * allocation time. Don't add new fields after pages[] unless you 14523aee091SJeff Moyer * wish that they not be zeroed. 14623aee091SJeff Moyer */ 1477b7a8665SChristoph Hellwig union { 14823aee091SJeff Moyer struct page *pages[DIO_PAGES]; /* page buffer */ 1497b7a8665SChristoph Hellwig struct work_struct complete_work;/* deferred AIO completion */ 1507b7a8665SChristoph Hellwig }; 1516e8267f5SAndi Kleen } ____cacheline_aligned_in_smp; 1526e8267f5SAndi Kleen 1536e8267f5SAndi Kleen static struct kmem_cache *dio_cache __read_mostly; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* 1561da177e4SLinus Torvalds * How many pages are in the queue? 1571da177e4SLinus Torvalds */ 158eb28be2bSAndi Kleen static inline unsigned dio_pages_present(struct dio_submit *sdio) 1591da177e4SLinus Torvalds { 160eb28be2bSAndi Kleen return sdio->tail - sdio->head; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * Go grab and pin some userspace pages. Typically we'll get 64 at a time. 1651da177e4SLinus Torvalds */ 166ba253fbfSAndi Kleen static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio) 1671da177e4SLinus Torvalds { 1687b2c99d1SAl Viro ssize_t ret; 1691da177e4SLinus Torvalds 1702c80929cSMiklos Szeredi ret = iov_iter_get_pages(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES, 1717b2c99d1SAl Viro &sdio->from); 1721da177e4SLinus Torvalds 1738a4c1e42SMike Christie if (ret < 0 && sdio->blocks_available && (dio->op == REQ_OP_WRITE)) { 174557ed1faSNick Piggin struct page *page = ZERO_PAGE(0); 1751da177e4SLinus Torvalds /* 1761da177e4SLinus Torvalds * A memory fault, but the filesystem has some outstanding 1771da177e4SLinus Torvalds * mapped blocks. We need to use those blocks up to avoid 1781da177e4SLinus Torvalds * leaking stale data in the file. 1791da177e4SLinus Torvalds */ 1801da177e4SLinus Torvalds if (dio->page_errors == 0) 1811da177e4SLinus Torvalds dio->page_errors = ret; 18209cbfeafSKirill A. Shutemov get_page(page); 183b5810039SNick Piggin dio->pages[0] = page; 184eb28be2bSAndi Kleen sdio->head = 0; 185eb28be2bSAndi Kleen sdio->tail = 1; 1867b2c99d1SAl Viro sdio->from = 0; 1877b2c99d1SAl Viro sdio->to = PAGE_SIZE; 1887b2c99d1SAl Viro return 0; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds if (ret >= 0) { 1927b2c99d1SAl Viro iov_iter_advance(sdio->iter, ret); 1937b2c99d1SAl Viro ret += sdio->from; 194eb28be2bSAndi Kleen sdio->head = 0; 1957b2c99d1SAl Viro sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE; 1967b2c99d1SAl Viro sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1; 1977b2c99d1SAl Viro return 0; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds return ret; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* 2031da177e4SLinus Torvalds * Get another userspace page. Returns an ERR_PTR on error. Pages are 2041da177e4SLinus Torvalds * buffered inside the dio so that we can call get_user_pages() against a 2051da177e4SLinus Torvalds * decent number of pages, less frequently. To provide nicer use of the 2061da177e4SLinus Torvalds * L1 cache. 2071da177e4SLinus Torvalds */ 208ba253fbfSAndi Kleen static inline struct page *dio_get_page(struct dio *dio, 2096fcc5420SBoaz Harrosh struct dio_submit *sdio) 2101da177e4SLinus Torvalds { 211eb28be2bSAndi Kleen if (dio_pages_present(sdio) == 0) { 2121da177e4SLinus Torvalds int ret; 2131da177e4SLinus Torvalds 214eb28be2bSAndi Kleen ret = dio_refill_pages(dio, sdio); 2151da177e4SLinus Torvalds if (ret) 2161da177e4SLinus Torvalds return ERR_PTR(ret); 217eb28be2bSAndi Kleen BUG_ON(dio_pages_present(sdio) == 0); 2181da177e4SLinus Torvalds } 2196fcc5420SBoaz Harrosh return dio->pages[sdio->head]; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2225a9d929dSDarrick J. Wong /* 2235a9d929dSDarrick J. Wong * Warn about a page cache invalidation failure during a direct io write. 2245a9d929dSDarrick J. Wong */ 2255a9d929dSDarrick J. Wong void dio_warn_stale_pagecache(struct file *filp) 2265a9d929dSDarrick J. Wong { 2275a9d929dSDarrick J. Wong static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST); 2285a9d929dSDarrick J. Wong char pathname[128]; 2295a9d929dSDarrick J. Wong struct inode *inode = file_inode(filp); 2305a9d929dSDarrick J. Wong char *path; 2315a9d929dSDarrick J. Wong 2325a9d929dSDarrick J. Wong errseq_set(&inode->i_mapping->wb_err, -EIO); 2335a9d929dSDarrick J. Wong if (__ratelimit(&_rs)) { 2345a9d929dSDarrick J. Wong path = file_path(filp, pathname, sizeof(pathname)); 2355a9d929dSDarrick J. Wong if (IS_ERR(path)) 2365a9d929dSDarrick J. Wong path = "(unknown)"; 2375a9d929dSDarrick J. Wong pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n"); 2385a9d929dSDarrick J. Wong pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid, 2395a9d929dSDarrick J. Wong current->comm); 2405a9d929dSDarrick J. Wong } 2415a9d929dSDarrick J. Wong } 2425a9d929dSDarrick J. Wong 2436d544bb4SZach Brown /** 2446d544bb4SZach Brown * dio_complete() - called when all DIO BIO I/O has been completed 2456d544bb4SZach Brown * @offset: the byte offset in the file of the completed operation 2466d544bb4SZach Brown * 2477b7a8665SChristoph Hellwig * This drops i_dio_count, lets interested parties know that a DIO operation 2487b7a8665SChristoph Hellwig * has completed, and calculates the resulting return code for the operation. 2496d544bb4SZach Brown * 2506d544bb4SZach Brown * It lets the filesystem know if it registered an interest earlier via 2516d544bb4SZach Brown * get_block. Pass the private field of the map buffer_head so that 2526d544bb4SZach Brown * filesystems can use it to hold additional state between get_block calls and 2536d544bb4SZach Brown * dio_complete. 2541da177e4SLinus Torvalds */ 255ffe51f01SLukas Czerner static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags) 2561da177e4SLinus Torvalds { 257716b9bc0SChristoph Hellwig loff_t offset = dio->iocb->ki_pos; 2586d544bb4SZach Brown ssize_t transferred = 0; 259332391a9SLukas Czerner int err; 2606d544bb4SZach Brown 2618459d86aSZach Brown /* 2628459d86aSZach Brown * AIO submission can race with bio completion to get here while 2638459d86aSZach Brown * expecting to have the last io completed by bio completion. 2648459d86aSZach Brown * In that case -EIOCBQUEUED is in fact not an error we want 2658459d86aSZach Brown * to preserve through this call. 2668459d86aSZach Brown */ 2678459d86aSZach Brown if (ret == -EIOCBQUEUED) 2688459d86aSZach Brown ret = 0; 2698459d86aSZach Brown 2706d544bb4SZach Brown if (dio->result) { 2716d544bb4SZach Brown transferred = dio->result; 2726d544bb4SZach Brown 2736d544bb4SZach Brown /* Check for short read case */ 2748a4c1e42SMike Christie if ((dio->op == REQ_OP_READ) && 2758a4c1e42SMike Christie ((offset + transferred) > dio->i_size)) 2766d544bb4SZach Brown transferred = dio->i_size - offset; 2774038acdbSAl Viro /* ignore EFAULT if some IO has been done */ 2784038acdbSAl Viro if (unlikely(ret == -EFAULT) && transferred) 2794038acdbSAl Viro ret = 0; 2806d544bb4SZach Brown } 2816d544bb4SZach Brown 2826d544bb4SZach Brown if (ret == 0) 2836d544bb4SZach Brown ret = dio->page_errors; 2846d544bb4SZach Brown if (ret == 0) 2856d544bb4SZach Brown ret = dio->io_error; 2866d544bb4SZach Brown if (ret == 0) 2876d544bb4SZach Brown ret = transferred; 2886d544bb4SZach Brown 2895e25c269SEryu Guan if (dio->end_io) { 2905e25c269SEryu Guan // XXX: ki_pos?? 2915e25c269SEryu Guan err = dio->end_io(dio->iocb, offset, ret, dio->private); 2925e25c269SEryu Guan if (err) 2935e25c269SEryu Guan ret = err; 2945e25c269SEryu Guan } 2955e25c269SEryu Guan 296332391a9SLukas Czerner /* 297332391a9SLukas Czerner * Try again to invalidate clean pages which might have been cached by 298332391a9SLukas Czerner * non-direct readahead, or faulted in by get_user_pages() if the source 299332391a9SLukas Czerner * of the write was an mmap'ed region of the file we're writing. Either 300332391a9SLukas Czerner * one is a pretty crazy thing to do, so we don't support it 100%. If 301332391a9SLukas Czerner * this invalidation fails, tough, the write still worked... 3025e25c269SEryu Guan * 3035e25c269SEryu Guan * And this page cache invalidation has to be after dio->end_io(), as 3045e25c269SEryu Guan * some filesystems convert unwritten extents to real allocations in 3055e25c269SEryu Guan * end_io() when necessary, otherwise a racing buffer read would cache 3065e25c269SEryu Guan * zeros from unwritten extents. 307332391a9SLukas Czerner */ 308ffe51f01SLukas Czerner if (flags & DIO_COMPLETE_INVALIDATE && 309ffe51f01SLukas Czerner ret > 0 && dio->op == REQ_OP_WRITE && 310332391a9SLukas Czerner dio->inode->i_mapping->nrpages) { 311332391a9SLukas Czerner err = invalidate_inode_pages2_range(dio->inode->i_mapping, 312332391a9SLukas Czerner offset >> PAGE_SHIFT, 313332391a9SLukas Czerner (offset + ret - 1) >> PAGE_SHIFT); 3145a9d929dSDarrick J. Wong if (err) 3155a9d929dSDarrick J. Wong dio_warn_stale_pagecache(dio->iocb->ki_filp); 316332391a9SLukas Czerner } 317332391a9SLukas Czerner 318fe0f07d0SJens Axboe inode_dio_end(dio->inode); 319fe0f07d0SJens Axboe 320ffe51f01SLukas Czerner if (flags & DIO_COMPLETE_ASYNC) { 321e2592217SChristoph Hellwig /* 322e2592217SChristoph Hellwig * generic_write_sync expects ki_pos to have been updated 323e2592217SChristoph Hellwig * already, but the submission path only does this for 324e2592217SChristoph Hellwig * synchronous I/O. 325e2592217SChristoph Hellwig */ 326e2592217SChristoph Hellwig dio->iocb->ki_pos += transferred; 32702afc27fSChristoph Hellwig 328*41e817bcSMaximilian Heyne if (ret > 0 && dio->op == REQ_OP_WRITE) 329*41e817bcSMaximilian Heyne ret = generic_write_sync(dio->iocb, ret); 33004b2fa9fSChristoph Hellwig dio->iocb->ki_complete(dio->iocb, ret, 0); 33102afc27fSChristoph Hellwig } 33240e2e973SChristoph Hellwig 3337b7a8665SChristoph Hellwig kmem_cache_free(dio_cache, dio); 3346d544bb4SZach Brown return ret; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3377b7a8665SChristoph Hellwig static void dio_aio_complete_work(struct work_struct *work) 3387b7a8665SChristoph Hellwig { 3397b7a8665SChristoph Hellwig struct dio *dio = container_of(work, struct dio, complete_work); 3407b7a8665SChristoph Hellwig 341ffe51f01SLukas Czerner dio_complete(dio, 0, DIO_COMPLETE_ASYNC | DIO_COMPLETE_INVALIDATE); 3427b7a8665SChristoph Hellwig } 3437b7a8665SChristoph Hellwig 3444e4cbee9SChristoph Hellwig static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio); 3457b7a8665SChristoph Hellwig 3461da177e4SLinus Torvalds /* 3471da177e4SLinus Torvalds * Asynchronous IO callback. 3481da177e4SLinus Torvalds */ 3494246a0b6SChristoph Hellwig static void dio_bio_end_aio(struct bio *bio) 3501da177e4SLinus Torvalds { 3511da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 3525eb6c7a2SZach Brown unsigned long remaining; 3535eb6c7a2SZach Brown unsigned long flags; 354332391a9SLukas Czerner bool defer_completion = false; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds /* cleanup the bio */ 3571da177e4SLinus Torvalds dio_bio_complete(dio, bio); 3580273201eSZach Brown 3595eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 3605eb6c7a2SZach Brown remaining = --dio->refcount; 3615eb6c7a2SZach Brown if (remaining == 1 && dio->waiter) 36220258b2bSZach Brown wake_up_process(dio->waiter); 3635eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 36420258b2bSZach Brown 3658459d86aSZach Brown if (remaining == 0) { 366332391a9SLukas Czerner /* 367332391a9SLukas Czerner * Defer completion when defer_completion is set or 368332391a9SLukas Czerner * when the inode has pages mapped and this is AIO write. 369332391a9SLukas Czerner * We need to invalidate those pages because there is a 370332391a9SLukas Czerner * chance they contain stale data in the case buffered IO 371332391a9SLukas Czerner * went in between AIO submission and completion into the 372332391a9SLukas Czerner * same region. 373332391a9SLukas Czerner */ 374332391a9SLukas Czerner if (dio->result) 375332391a9SLukas Czerner defer_completion = dio->defer_completion || 376332391a9SLukas Czerner (dio->op == REQ_OP_WRITE && 377332391a9SLukas Czerner dio->inode->i_mapping->nrpages); 378332391a9SLukas Czerner if (defer_completion) { 3797b7a8665SChristoph Hellwig INIT_WORK(&dio->complete_work, dio_aio_complete_work); 3807b7a8665SChristoph Hellwig queue_work(dio->inode->i_sb->s_dio_done_wq, 3817b7a8665SChristoph Hellwig &dio->complete_work); 3827b7a8665SChristoph Hellwig } else { 383ffe51f01SLukas Czerner dio_complete(dio, 0, DIO_COMPLETE_ASYNC); 3847b7a8665SChristoph Hellwig } 3858459d86aSZach Brown } 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* 3891da177e4SLinus Torvalds * The BIO completion handler simply queues the BIO up for the process-context 3901da177e4SLinus Torvalds * handler. 3911da177e4SLinus Torvalds * 3921da177e4SLinus Torvalds * During I/O bi_private points at the dio. After I/O, bi_private is used to 3931da177e4SLinus Torvalds * implement a singly-linked list of completed BIOs, at dio->bio_list. 3941da177e4SLinus Torvalds */ 3954246a0b6SChristoph Hellwig static void dio_bio_end_io(struct bio *bio) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 3981da177e4SLinus Torvalds unsigned long flags; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 4011da177e4SLinus Torvalds bio->bi_private = dio->bio_list; 4021da177e4SLinus Torvalds dio->bio_list = bio; 4035eb6c7a2SZach Brown if (--dio->refcount == 1 && dio->waiter) 4041da177e4SLinus Torvalds wake_up_process(dio->waiter); 4051da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 408facd07b0SJosef Bacik /** 409facd07b0SJosef Bacik * dio_end_io - handle the end io action for the given bio 410facd07b0SJosef Bacik * @bio: The direct io bio thats being completed 411facd07b0SJosef Bacik * 412facd07b0SJosef Bacik * This is meant to be called by any filesystem that uses their own dio_submit_t 413facd07b0SJosef Bacik * so that the DIO specific endio actions are dealt with after the filesystem 414facd07b0SJosef Bacik * has done it's completion work. 415facd07b0SJosef Bacik */ 4164055351cSChristoph Hellwig void dio_end_io(struct bio *bio) 417facd07b0SJosef Bacik { 418facd07b0SJosef Bacik struct dio *dio = bio->bi_private; 419facd07b0SJosef Bacik 420facd07b0SJosef Bacik if (dio->is_async) 4214246a0b6SChristoph Hellwig dio_bio_end_aio(bio); 422facd07b0SJosef Bacik else 4234246a0b6SChristoph Hellwig dio_bio_end_io(bio); 424facd07b0SJosef Bacik } 425facd07b0SJosef Bacik EXPORT_SYMBOL_GPL(dio_end_io); 426facd07b0SJosef Bacik 427ba253fbfSAndi Kleen static inline void 428eb28be2bSAndi Kleen dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, 429eb28be2bSAndi Kleen struct block_device *bdev, 4301da177e4SLinus Torvalds sector_t first_sector, int nr_vecs) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds struct bio *bio; 4331da177e4SLinus Torvalds 43420d9600cSDavid Dillow /* 4350eb0b63cSChristoph Hellwig * bio_alloc() is guaranteed to return a bio when allowed to sleep and 4360eb0b63cSChristoph Hellwig * we request a valid number of vectors. 43720d9600cSDavid Dillow */ 4381da177e4SLinus Torvalds bio = bio_alloc(GFP_KERNEL, nr_vecs); 4391da177e4SLinus Torvalds 44074d46992SChristoph Hellwig bio_set_dev(bio, bdev); 4414f024f37SKent Overstreet bio->bi_iter.bi_sector = first_sector; 4428a4c1e42SMike Christie bio_set_op_attrs(bio, dio->op, dio->op_flags); 4431da177e4SLinus Torvalds if (dio->is_async) 4441da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_aio; 4451da177e4SLinus Torvalds else 4461da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_io; 4471da177e4SLinus Torvalds 44845d06cf7SJens Axboe bio->bi_write_hint = dio->iocb->ki_hint; 44945d06cf7SJens Axboe 450eb28be2bSAndi Kleen sdio->bio = bio; 451eb28be2bSAndi Kleen sdio->logical_offset_in_bio = sdio->cur_page_fs_offset; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* 4551da177e4SLinus Torvalds * In the AIO read case we speculatively dirty the pages before starting IO. 4561da177e4SLinus Torvalds * During IO completion, any of these pages which happen to have been written 4571da177e4SLinus Torvalds * back will be redirtied by bio_check_pages_dirty(). 4580273201eSZach Brown * 4590273201eSZach Brown * bios hold a dio reference between submit_bio and ->end_io. 4601da177e4SLinus Torvalds */ 461ba253fbfSAndi Kleen static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio) 4621da177e4SLinus Torvalds { 463eb28be2bSAndi Kleen struct bio *bio = sdio->bio; 4645eb6c7a2SZach Brown unsigned long flags; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds bio->bi_private = dio; 4675eb6c7a2SZach Brown 4685eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 4695eb6c7a2SZach Brown dio->refcount++; 4705eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 4715eb6c7a2SZach Brown 4728a4c1e42SMike Christie if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty) 4731da177e4SLinus Torvalds bio_set_pages_dirty(bio); 4745eb6c7a2SZach Brown 47574d46992SChristoph Hellwig dio->bio_disk = bio->bi_disk; 476c1c53460SJens Axboe 47715c4f638SJens Axboe if (sdio->submit_io) { 4788a4c1e42SMike Christie sdio->submit_io(bio, dio->inode, sdio->logical_offset_in_bio); 47915c4f638SJens Axboe dio->bio_cookie = BLK_QC_T_NONE; 480c1c53460SJens Axboe } else 4814e49ea4aSMike Christie dio->bio_cookie = submit_bio(bio); 4821da177e4SLinus Torvalds 483eb28be2bSAndi Kleen sdio->bio = NULL; 484eb28be2bSAndi Kleen sdio->boundary = 0; 485eb28be2bSAndi Kleen sdio->logical_offset_in_bio = 0; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* 4891da177e4SLinus Torvalds * Release any resources in case of a failure 4901da177e4SLinus Torvalds */ 491ba253fbfSAndi Kleen static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio) 4921da177e4SLinus Torvalds { 4937b2c99d1SAl Viro while (sdio->head < sdio->tail) 49409cbfeafSKirill A. Shutemov put_page(dio->pages[sdio->head++]); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* 4980273201eSZach Brown * Wait for the next BIO to complete. Remove it and return it. NULL is 4990273201eSZach Brown * returned once all BIOs have been completed. This must only be called once 5000273201eSZach Brown * all bios have been issued so that dio->refcount can only decrease. This 5010273201eSZach Brown * requires that that the caller hold a reference on the dio. 5021da177e4SLinus Torvalds */ 5031da177e4SLinus Torvalds static struct bio *dio_await_one(struct dio *dio) 5041da177e4SLinus Torvalds { 5051da177e4SLinus Torvalds unsigned long flags; 5060273201eSZach Brown struct bio *bio = NULL; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 5095eb6c7a2SZach Brown 5105eb6c7a2SZach Brown /* 5115eb6c7a2SZach Brown * Wait as long as the list is empty and there are bios in flight. bio 5125eb6c7a2SZach Brown * completion drops the count, maybe adds to the list, and wakes while 5135eb6c7a2SZach Brown * holding the bio_lock so we don't need set_current_state()'s barrier 5145eb6c7a2SZach Brown * and can call it after testing our condition. 5155eb6c7a2SZach Brown */ 5165eb6c7a2SZach Brown while (dio->refcount > 1 && dio->bio_list == NULL) { 5175eb6c7a2SZach Brown __set_current_state(TASK_UNINTERRUPTIBLE); 5181da177e4SLinus Torvalds dio->waiter = current; 5191da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 520c43c83a2SChristoph Hellwig if (!(dio->iocb->ki_flags & IOCB_HIPRI) || 521ea435e1bSChristoph Hellwig !blk_poll(dio->bio_disk->queue, dio->bio_cookie)) 5221da177e4SLinus Torvalds io_schedule(); 5235eb6c7a2SZach Brown /* wake up sets us TASK_RUNNING */ 5241da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 5251da177e4SLinus Torvalds dio->waiter = NULL; 5261da177e4SLinus Torvalds } 5270273201eSZach Brown if (dio->bio_list) { 5281da177e4SLinus Torvalds bio = dio->bio_list; 5291da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 5300273201eSZach Brown } 5311da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 5321da177e4SLinus Torvalds return bio; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /* 5361da177e4SLinus Torvalds * Process one completed BIO. No locks are held. 5371da177e4SLinus Torvalds */ 5384e4cbee9SChristoph Hellwig static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio) 5391da177e4SLinus Torvalds { 540cb34e057SKent Overstreet struct bio_vec *bvec; 541cb34e057SKent Overstreet unsigned i; 5424e4cbee9SChristoph Hellwig blk_status_t err = bio->bi_status; 5431da177e4SLinus Torvalds 54403a07c92SGoldwyn Rodrigues if (err) { 54503a07c92SGoldwyn Rodrigues if (err == BLK_STS_AGAIN && (bio->bi_opf & REQ_NOWAIT)) 54603a07c92SGoldwyn Rodrigues dio->io_error = -EAGAIN; 54703a07c92SGoldwyn Rodrigues else 548174e27c6SChen, Kenneth W dio->io_error = -EIO; 54903a07c92SGoldwyn Rodrigues } 5501da177e4SLinus Torvalds 5518a4c1e42SMike Christie if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty) { 5527ddc971fSMike Krinkin bio_check_pages_dirty(bio); /* transfers ownership */ 5531da177e4SLinus Torvalds } else { 554cb34e057SKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 555cb34e057SKent Overstreet struct page *page = bvec->bv_page; 5561da177e4SLinus Torvalds 5578a4c1e42SMike Christie if (dio->op == REQ_OP_READ && !PageCompound(page) && 55853cbf3b1SMing Lei dio->should_dirty) 5591da177e4SLinus Torvalds set_page_dirty_lock(page); 56009cbfeafSKirill A. Shutemov put_page(page); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds bio_put(bio); 5631da177e4SLinus Torvalds } 5649b81c842SSasha Levin return err; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* 5680273201eSZach Brown * Wait on and process all in-flight BIOs. This must only be called once 5690273201eSZach Brown * all bios have been issued so that the refcount can only decrease. 5700273201eSZach Brown * This just waits for all bios to make it through dio_bio_complete. IO 571beb7dd86SRobert P. J. Day * errors are propagated through dio->io_error and should be propagated via 5720273201eSZach Brown * dio_complete(). 5731da177e4SLinus Torvalds */ 5746d544bb4SZach Brown static void dio_await_completion(struct dio *dio) 5751da177e4SLinus Torvalds { 5760273201eSZach Brown struct bio *bio; 5770273201eSZach Brown do { 5780273201eSZach Brown bio = dio_await_one(dio); 5790273201eSZach Brown if (bio) 5806d544bb4SZach Brown dio_bio_complete(dio, bio); 5810273201eSZach Brown } while (bio); 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds /* 5851da177e4SLinus Torvalds * A really large O_DIRECT read or write can generate a lot of BIOs. So 5861da177e4SLinus Torvalds * to keep the memory consumption sane we periodically reap any completed BIOs 5871da177e4SLinus Torvalds * during the BIO generation phase. 5881da177e4SLinus Torvalds * 5891da177e4SLinus Torvalds * This also helps to limit the peak amount of pinned userspace memory. 5901da177e4SLinus Torvalds */ 591ba253fbfSAndi Kleen static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio) 5921da177e4SLinus Torvalds { 5931da177e4SLinus Torvalds int ret = 0; 5941da177e4SLinus Torvalds 595eb28be2bSAndi Kleen if (sdio->reap_counter++ >= 64) { 5961da177e4SLinus Torvalds while (dio->bio_list) { 5971da177e4SLinus Torvalds unsigned long flags; 5981da177e4SLinus Torvalds struct bio *bio; 5991da177e4SLinus Torvalds int ret2; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 6021da177e4SLinus Torvalds bio = dio->bio_list; 6031da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 6041da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 6054e4cbee9SChristoph Hellwig ret2 = blk_status_to_errno(dio_bio_complete(dio, bio)); 6061da177e4SLinus Torvalds if (ret == 0) 6071da177e4SLinus Torvalds ret = ret2; 6081da177e4SLinus Torvalds } 609eb28be2bSAndi Kleen sdio->reap_counter = 0; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds return ret; 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds /* 6157b7a8665SChristoph Hellwig * Create workqueue for deferred direct IO completions. We allocate the 6167b7a8665SChristoph Hellwig * workqueue when it's first needed. This avoids creating workqueue for 6177b7a8665SChristoph Hellwig * filesystems that don't need it and also allows us to create the workqueue 6187b7a8665SChristoph Hellwig * late enough so the we can include s_id in the name of the workqueue. 6197b7a8665SChristoph Hellwig */ 620ec1b8260SChristoph Hellwig int sb_init_dio_done_wq(struct super_block *sb) 6217b7a8665SChristoph Hellwig { 62245150c43SOlof Johansson struct workqueue_struct *old; 6237b7a8665SChristoph Hellwig struct workqueue_struct *wq = alloc_workqueue("dio/%s", 6247b7a8665SChristoph Hellwig WQ_MEM_RECLAIM, 0, 6257b7a8665SChristoph Hellwig sb->s_id); 6267b7a8665SChristoph Hellwig if (!wq) 6277b7a8665SChristoph Hellwig return -ENOMEM; 6287b7a8665SChristoph Hellwig /* 6297b7a8665SChristoph Hellwig * This has to be atomic as more DIOs can race to create the workqueue 6307b7a8665SChristoph Hellwig */ 63145150c43SOlof Johansson old = cmpxchg(&sb->s_dio_done_wq, NULL, wq); 6327b7a8665SChristoph Hellwig /* Someone created workqueue before us? Free ours... */ 63345150c43SOlof Johansson if (old) 6347b7a8665SChristoph Hellwig destroy_workqueue(wq); 6357b7a8665SChristoph Hellwig return 0; 6367b7a8665SChristoph Hellwig } 6377b7a8665SChristoph Hellwig 6387b7a8665SChristoph Hellwig static int dio_set_defer_completion(struct dio *dio) 6397b7a8665SChristoph Hellwig { 6407b7a8665SChristoph Hellwig struct super_block *sb = dio->inode->i_sb; 6417b7a8665SChristoph Hellwig 6427b7a8665SChristoph Hellwig if (dio->defer_completion) 6437b7a8665SChristoph Hellwig return 0; 6447b7a8665SChristoph Hellwig dio->defer_completion = true; 6457b7a8665SChristoph Hellwig if (!sb->s_dio_done_wq) 6467b7a8665SChristoph Hellwig return sb_init_dio_done_wq(sb); 6477b7a8665SChristoph Hellwig return 0; 6487b7a8665SChristoph Hellwig } 6497b7a8665SChristoph Hellwig 6507b7a8665SChristoph Hellwig /* 6511da177e4SLinus Torvalds * Call into the fs to map some more disk blocks. We record the current number 652eb28be2bSAndi Kleen * of available blocks at sdio->blocks_available. These are in units of the 65393407472SFabian Frederick * fs blocksize, i_blocksize(inode). 6541da177e4SLinus Torvalds * 6551da177e4SLinus Torvalds * The fs is allowed to map lots of blocks at once. If it wants to do that, 6561da177e4SLinus Torvalds * it uses the passed inode-relative block number as the file offset, as usual. 6571da177e4SLinus Torvalds * 6581d8fa7a2SBadari Pulavarty * get_block() is passed the number of i_blkbits-sized blocks which direct_io 6591da177e4SLinus Torvalds * has remaining to do. The fs should not map more than this number of blocks. 6601da177e4SLinus Torvalds * 6611da177e4SLinus Torvalds * If the fs has mapped a lot of blocks, it should populate bh->b_size to 6621da177e4SLinus Torvalds * indicate how much contiguous disk space has been made available at 6631da177e4SLinus Torvalds * bh->b_blocknr. 6641da177e4SLinus Torvalds * 6651da177e4SLinus Torvalds * If *any* of the mapped blocks are new, then the fs must set buffer_new(). 6661da177e4SLinus Torvalds * This isn't very efficient... 6671da177e4SLinus Torvalds * 6681da177e4SLinus Torvalds * In the case of filesystem holes: the fs may return an arbitrarily-large 6691da177e4SLinus Torvalds * hole by returning an appropriate value in b_size and by clearing 6701da177e4SLinus Torvalds * buffer_mapped(). However the direct-io code will only process holes one 6711d8fa7a2SBadari Pulavarty * block at a time - it will repeatedly call get_block() as it walks the hole. 6721da177e4SLinus Torvalds */ 67318772641SAndi Kleen static int get_more_blocks(struct dio *dio, struct dio_submit *sdio, 67418772641SAndi Kleen struct buffer_head *map_bh) 6751da177e4SLinus Torvalds { 6761da177e4SLinus Torvalds int ret; 6771da177e4SLinus Torvalds sector_t fs_startblk; /* Into file, in filesystem-sized blocks */ 678ae55e1aaSTao Ma sector_t fs_endblk; /* Into file, in filesystem-sized blocks */ 6791da177e4SLinus Torvalds unsigned long fs_count; /* Number of filesystem-sized blocks */ 6801da177e4SLinus Torvalds int create; 681ab73857eSLinus Torvalds unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds /* 6841da177e4SLinus Torvalds * If there was a memory error and we've overwritten all the 6851da177e4SLinus Torvalds * mapped blocks then we can now return that memory error 6861da177e4SLinus Torvalds */ 6871da177e4SLinus Torvalds ret = dio->page_errors; 6881da177e4SLinus Torvalds if (ret == 0) { 689eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file >= sdio->final_block_in_request); 690eb28be2bSAndi Kleen fs_startblk = sdio->block_in_file >> sdio->blkfactor; 691ae55e1aaSTao Ma fs_endblk = (sdio->final_block_in_request - 1) >> 692ae55e1aaSTao Ma sdio->blkfactor; 693ae55e1aaSTao Ma fs_count = fs_endblk - fs_startblk + 1; 6941da177e4SLinus Torvalds 6953c674e74SNathan Scott map_bh->b_state = 0; 696ab73857eSLinus Torvalds map_bh->b_size = fs_count << i_blkbits; 6973c674e74SNathan Scott 6985fe878aeSChristoph Hellwig /* 6999ecd10b7SEryu Guan * For writes that could fill holes inside i_size on a 7009ecd10b7SEryu Guan * DIO_SKIP_HOLES filesystem we forbid block creations: only 7019ecd10b7SEryu Guan * overwrites are permitted. We will return early to the caller 7029ecd10b7SEryu Guan * once we see an unmapped buffer head returned, and the caller 7039ecd10b7SEryu Guan * will fall back to buffered I/O. 7045fe878aeSChristoph Hellwig * 7055fe878aeSChristoph Hellwig * Otherwise the decision is left to the get_blocks method, 7065fe878aeSChristoph Hellwig * which may decide to handle it or also return an unmapped 7075fe878aeSChristoph Hellwig * buffer head. 7085fe878aeSChristoph Hellwig */ 7098a4c1e42SMike Christie create = dio->op == REQ_OP_WRITE; 7105fe878aeSChristoph Hellwig if (dio->flags & DIO_SKIP_HOLES) { 7119ecd10b7SEryu Guan if (fs_startblk <= ((i_size_read(dio->inode) - 1) >> 7129ecd10b7SEryu Guan i_blkbits)) 7131da177e4SLinus Torvalds create = 0; 7141da177e4SLinus Torvalds } 7153c674e74SNathan Scott 716eb28be2bSAndi Kleen ret = (*sdio->get_block)(dio->inode, fs_startblk, 7171da177e4SLinus Torvalds map_bh, create); 71818772641SAndi Kleen 71918772641SAndi Kleen /* Store for completion */ 72018772641SAndi Kleen dio->private = map_bh->b_private; 7217b7a8665SChristoph Hellwig 7227b7a8665SChristoph Hellwig if (ret == 0 && buffer_defer_completion(map_bh)) 7237b7a8665SChristoph Hellwig ret = dio_set_defer_completion(dio); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds return ret; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* 7291da177e4SLinus Torvalds * There is no bio. Make one now. 7301da177e4SLinus Torvalds */ 731ba253fbfSAndi Kleen static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio, 73218772641SAndi Kleen sector_t start_sector, struct buffer_head *map_bh) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds sector_t sector; 7351da177e4SLinus Torvalds int ret, nr_pages; 7361da177e4SLinus Torvalds 737eb28be2bSAndi Kleen ret = dio_bio_reap(dio, sdio); 7381da177e4SLinus Torvalds if (ret) 7391da177e4SLinus Torvalds goto out; 740eb28be2bSAndi Kleen sector = start_sector << (sdio->blkbits - 9); 741b54ffb73SKent Overstreet nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES); 7421da177e4SLinus Torvalds BUG_ON(nr_pages <= 0); 74318772641SAndi Kleen dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages); 744eb28be2bSAndi Kleen sdio->boundary = 0; 7451da177e4SLinus Torvalds out: 7461da177e4SLinus Torvalds return ret; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* 7501da177e4SLinus Torvalds * Attempt to put the current chunk of 'cur_page' into the current BIO. If 7511da177e4SLinus Torvalds * that was successful then update final_block_in_bio and take a ref against 7521da177e4SLinus Torvalds * the just-added page. 7531da177e4SLinus Torvalds * 7541da177e4SLinus Torvalds * Return zero on success. Non-zero means the caller needs to start a new BIO. 7551da177e4SLinus Torvalds */ 756ba253fbfSAndi Kleen static inline int dio_bio_add_page(struct dio_submit *sdio) 7571da177e4SLinus Torvalds { 7581da177e4SLinus Torvalds int ret; 7591da177e4SLinus Torvalds 760eb28be2bSAndi Kleen ret = bio_add_page(sdio->bio, sdio->cur_page, 761eb28be2bSAndi Kleen sdio->cur_page_len, sdio->cur_page_offset); 762eb28be2bSAndi Kleen if (ret == sdio->cur_page_len) { 7631da177e4SLinus Torvalds /* 7641da177e4SLinus Torvalds * Decrement count only, if we are done with this page 7651da177e4SLinus Torvalds */ 766eb28be2bSAndi Kleen if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE) 767eb28be2bSAndi Kleen sdio->pages_in_io--; 76809cbfeafSKirill A. Shutemov get_page(sdio->cur_page); 769eb28be2bSAndi Kleen sdio->final_block_in_bio = sdio->cur_page_block + 770eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits); 7711da177e4SLinus Torvalds ret = 0; 7721da177e4SLinus Torvalds } else { 7731da177e4SLinus Torvalds ret = 1; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds return ret; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds /* 7791da177e4SLinus Torvalds * Put cur_page under IO. The section of cur_page which is described by 7801da177e4SLinus Torvalds * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page 7811da177e4SLinus Torvalds * starts on-disk at cur_page_block. 7821da177e4SLinus Torvalds * 7831da177e4SLinus Torvalds * We take a ref against the page here (on behalf of its presence in the bio). 7841da177e4SLinus Torvalds * 7851da177e4SLinus Torvalds * The caller of this function is responsible for removing cur_page from the 7861da177e4SLinus Torvalds * dio, and for dropping the refcount which came from that presence. 7871da177e4SLinus Torvalds */ 788ba253fbfSAndi Kleen static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio, 78918772641SAndi Kleen struct buffer_head *map_bh) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds int ret = 0; 7921da177e4SLinus Torvalds 793eb28be2bSAndi Kleen if (sdio->bio) { 794eb28be2bSAndi Kleen loff_t cur_offset = sdio->cur_page_fs_offset; 795eb28be2bSAndi Kleen loff_t bio_next_offset = sdio->logical_offset_in_bio + 7964f024f37SKent Overstreet sdio->bio->bi_iter.bi_size; 797c2c6ca41SJosef Bacik 7981da177e4SLinus Torvalds /* 799c2c6ca41SJosef Bacik * See whether this new request is contiguous with the old. 800c2c6ca41SJosef Bacik * 801f0940ceeSNamhyung Kim * Btrfs cannot handle having logically non-contiguous requests 802f0940ceeSNamhyung Kim * submitted. For example if you have 803c2c6ca41SJosef Bacik * 804c2c6ca41SJosef Bacik * Logical: [0-4095][HOLE][8192-12287] 805f0940ceeSNamhyung Kim * Physical: [0-4095] [4096-8191] 806c2c6ca41SJosef Bacik * 807c2c6ca41SJosef Bacik * We cannot submit those pages together as one BIO. So if our 808c2c6ca41SJosef Bacik * current logical offset in the file does not equal what would 809c2c6ca41SJosef Bacik * be the next logical offset in the bio, submit the bio we 810c2c6ca41SJosef Bacik * have. 8111da177e4SLinus Torvalds */ 812eb28be2bSAndi Kleen if (sdio->final_block_in_bio != sdio->cur_page_block || 813c2c6ca41SJosef Bacik cur_offset != bio_next_offset) 814eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 817eb28be2bSAndi Kleen if (sdio->bio == NULL) { 81818772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 8191da177e4SLinus Torvalds if (ret) 8201da177e4SLinus Torvalds goto out; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 823eb28be2bSAndi Kleen if (dio_bio_add_page(sdio) != 0) { 824eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 82518772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 8261da177e4SLinus Torvalds if (ret == 0) { 827eb28be2bSAndi Kleen ret = dio_bio_add_page(sdio); 8281da177e4SLinus Torvalds BUG_ON(ret != 0); 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds out: 8321da177e4SLinus Torvalds return ret; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* 8361da177e4SLinus Torvalds * An autonomous function to put a chunk of a page under deferred IO. 8371da177e4SLinus Torvalds * 8381da177e4SLinus Torvalds * The caller doesn't actually know (or care) whether this piece of page is in 8391da177e4SLinus Torvalds * a BIO, or is under IO or whatever. We just take care of all possible 8401da177e4SLinus Torvalds * situations here. The separation between the logic of do_direct_IO() and 8411da177e4SLinus Torvalds * that of submit_page_section() is important for clarity. Please don't break. 8421da177e4SLinus Torvalds * 8431da177e4SLinus Torvalds * The chunk of page starts on-disk at blocknr. 8441da177e4SLinus Torvalds * 8451da177e4SLinus Torvalds * We perform deferred IO, by recording the last-submitted page inside our 8461da177e4SLinus Torvalds * private part of the dio structure. If possible, we just expand the IO 8471da177e4SLinus Torvalds * across that page here. 8481da177e4SLinus Torvalds * 8491da177e4SLinus Torvalds * If that doesn't work out then we put the old page into the bio and add this 8501da177e4SLinus Torvalds * page to the dio instead. 8511da177e4SLinus Torvalds */ 852ba253fbfSAndi Kleen static inline int 853eb28be2bSAndi Kleen submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page, 85418772641SAndi Kleen unsigned offset, unsigned len, sector_t blocknr, 85518772641SAndi Kleen struct buffer_head *map_bh) 8561da177e4SLinus Torvalds { 8571da177e4SLinus Torvalds int ret = 0; 8581da177e4SLinus Torvalds 8598a4c1e42SMike Christie if (dio->op == REQ_OP_WRITE) { 86098c4d57dSAndrew Morton /* 86198c4d57dSAndrew Morton * Read accounting is performed in submit_bio() 86298c4d57dSAndrew Morton */ 86398c4d57dSAndrew Morton task_io_account_write(len); 86498c4d57dSAndrew Morton } 86598c4d57dSAndrew Morton 8661da177e4SLinus Torvalds /* 8671da177e4SLinus Torvalds * Can we just grow the current page's presence in the dio? 8681da177e4SLinus Torvalds */ 869eb28be2bSAndi Kleen if (sdio->cur_page == page && 870eb28be2bSAndi Kleen sdio->cur_page_offset + sdio->cur_page_len == offset && 871eb28be2bSAndi Kleen sdio->cur_page_block + 872eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits) == blocknr) { 873eb28be2bSAndi Kleen sdio->cur_page_len += len; 8741da177e4SLinus Torvalds goto out; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 8781da177e4SLinus Torvalds * If there's a deferred page already there then send it. 8791da177e4SLinus Torvalds */ 880eb28be2bSAndi Kleen if (sdio->cur_page) { 88118772641SAndi Kleen ret = dio_send_cur_page(dio, sdio, map_bh); 88209cbfeafSKirill A. Shutemov put_page(sdio->cur_page); 883eb28be2bSAndi Kleen sdio->cur_page = NULL; 8841da177e4SLinus Torvalds if (ret) 885b1058b98SJan Kara return ret; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 88809cbfeafSKirill A. Shutemov get_page(page); /* It is in dio */ 889eb28be2bSAndi Kleen sdio->cur_page = page; 890eb28be2bSAndi Kleen sdio->cur_page_offset = offset; 891eb28be2bSAndi Kleen sdio->cur_page_len = len; 892eb28be2bSAndi Kleen sdio->cur_page_block = blocknr; 893eb28be2bSAndi Kleen sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits; 8941da177e4SLinus Torvalds out: 895b1058b98SJan Kara /* 896b1058b98SJan Kara * If sdio->boundary then we want to schedule the IO now to 897b1058b98SJan Kara * avoid metadata seeks. 898b1058b98SJan Kara */ 899b1058b98SJan Kara if (sdio->boundary) { 900b1058b98SJan Kara ret = dio_send_cur_page(dio, sdio, map_bh); 901899f0429SAndreas Gruenbacher if (sdio->bio) 902b1058b98SJan Kara dio_bio_submit(dio, sdio); 90309cbfeafSKirill A. Shutemov put_page(sdio->cur_page); 904b1058b98SJan Kara sdio->cur_page = NULL; 905b1058b98SJan Kara } 9061da177e4SLinus Torvalds return ret; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds /* 9101da177e4SLinus Torvalds * If we are not writing the entire block and get_block() allocated 9111da177e4SLinus Torvalds * the block for us, we need to fill-in the unused portion of the 9121da177e4SLinus Torvalds * block with zeros. This happens only if user-buffer, fileoffset or 9131da177e4SLinus Torvalds * io length is not filesystem block-size multiple. 9141da177e4SLinus Torvalds * 9151da177e4SLinus Torvalds * `end' is zero if we're doing the start of the IO, 1 at the end of the 9161da177e4SLinus Torvalds * IO. 9171da177e4SLinus Torvalds */ 918ba253fbfSAndi Kleen static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio, 919ba253fbfSAndi Kleen int end, struct buffer_head *map_bh) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds unsigned dio_blocks_per_fs_block; 9221da177e4SLinus Torvalds unsigned this_chunk_blocks; /* In dio_blocks */ 9231da177e4SLinus Torvalds unsigned this_chunk_bytes; 9241da177e4SLinus Torvalds struct page *page; 9251da177e4SLinus Torvalds 926eb28be2bSAndi Kleen sdio->start_zero_done = 1; 92718772641SAndi Kleen if (!sdio->blkfactor || !buffer_new(map_bh)) 9281da177e4SLinus Torvalds return; 9291da177e4SLinus Torvalds 930eb28be2bSAndi Kleen dio_blocks_per_fs_block = 1 << sdio->blkfactor; 931eb28be2bSAndi Kleen this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1); 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds if (!this_chunk_blocks) 9341da177e4SLinus Torvalds return; 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds /* 9371da177e4SLinus Torvalds * We need to zero out part of an fs block. It is either at the 9381da177e4SLinus Torvalds * beginning or the end of the fs block. 9391da177e4SLinus Torvalds */ 9401da177e4SLinus Torvalds if (end) 9411da177e4SLinus Torvalds this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks; 9421da177e4SLinus Torvalds 943eb28be2bSAndi Kleen this_chunk_bytes = this_chunk_blocks << sdio->blkbits; 9441da177e4SLinus Torvalds 945557ed1faSNick Piggin page = ZERO_PAGE(0); 946eb28be2bSAndi Kleen if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes, 94718772641SAndi Kleen sdio->next_block_for_io, map_bh)) 9481da177e4SLinus Torvalds return; 9491da177e4SLinus Torvalds 950eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* 9541da177e4SLinus Torvalds * Walk the user pages, and the file, mapping blocks to disk and generating 9551da177e4SLinus Torvalds * a sequence of (page,offset,len,block) mappings. These mappings are injected 9561da177e4SLinus Torvalds * into submit_page_section(), which takes care of the next stage of submission 9571da177e4SLinus Torvalds * 9581da177e4SLinus Torvalds * Direct IO against a blockdev is different from a file. Because we can 9591da177e4SLinus Torvalds * happily perform page-sized but 512-byte aligned IOs. It is important that 9601da177e4SLinus Torvalds * blockdev IO be able to have fine alignment and large sizes. 9611da177e4SLinus Torvalds * 9621d8fa7a2SBadari Pulavarty * So what we do is to permit the ->get_block function to populate bh.b_size 9631da177e4SLinus Torvalds * with the size of IO which is permitted at this offset and this i_blkbits. 9641da177e4SLinus Torvalds * 9651da177e4SLinus Torvalds * For best results, the blockdev should be set up with 512-byte i_blkbits and 9661d8fa7a2SBadari Pulavarty * it should set b_size to PAGE_SIZE or more inside get_block(). This gives 9671da177e4SLinus Torvalds * fine alignment but still allows this function to work in PAGE_SIZE units. 9681da177e4SLinus Torvalds */ 96918772641SAndi Kleen static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, 97018772641SAndi Kleen struct buffer_head *map_bh) 9711da177e4SLinus Torvalds { 972eb28be2bSAndi Kleen const unsigned blkbits = sdio->blkbits; 973dd545b52SChandan Rajendra const unsigned i_blkbits = blkbits + sdio->blkfactor; 9741da177e4SLinus Torvalds int ret = 0; 9751da177e4SLinus Torvalds 976eb28be2bSAndi Kleen while (sdio->block_in_file < sdio->final_block_in_request) { 9777b2c99d1SAl Viro struct page *page; 9787b2c99d1SAl Viro size_t from, to; 9796fcc5420SBoaz Harrosh 9806fcc5420SBoaz Harrosh page = dio_get_page(dio, sdio); 9811da177e4SLinus Torvalds if (IS_ERR(page)) { 9821da177e4SLinus Torvalds ret = PTR_ERR(page); 9831da177e4SLinus Torvalds goto out; 9841da177e4SLinus Torvalds } 9856fcc5420SBoaz Harrosh from = sdio->head ? 0 : sdio->from; 9866fcc5420SBoaz Harrosh to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE; 9876fcc5420SBoaz Harrosh sdio->head++; 9881da177e4SLinus Torvalds 9897b2c99d1SAl Viro while (from < to) { 9901da177e4SLinus Torvalds unsigned this_chunk_bytes; /* # of bytes mapped */ 9911da177e4SLinus Torvalds unsigned this_chunk_blocks; /* # of blocks */ 9921da177e4SLinus Torvalds unsigned u; 9931da177e4SLinus Torvalds 994eb28be2bSAndi Kleen if (sdio->blocks_available == 0) { 9951da177e4SLinus Torvalds /* 9961da177e4SLinus Torvalds * Need to go and map some more disk 9971da177e4SLinus Torvalds */ 9981da177e4SLinus Torvalds unsigned long blkmask; 9991da177e4SLinus Torvalds unsigned long dio_remainder; 10001da177e4SLinus Torvalds 100118772641SAndi Kleen ret = get_more_blocks(dio, sdio, map_bh); 10021da177e4SLinus Torvalds if (ret) { 100309cbfeafSKirill A. Shutemov put_page(page); 10041da177e4SLinus Torvalds goto out; 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) 10071da177e4SLinus Torvalds goto do_holes; 10081da177e4SLinus Torvalds 1009eb28be2bSAndi Kleen sdio->blocks_available = 1010f734c89cSJan Kara map_bh->b_size >> blkbits; 1011eb28be2bSAndi Kleen sdio->next_block_for_io = 1012eb28be2bSAndi Kleen map_bh->b_blocknr << sdio->blkfactor; 1013f734c89cSJan Kara if (buffer_new(map_bh)) { 1014f734c89cSJan Kara clean_bdev_aliases( 1015f734c89cSJan Kara map_bh->b_bdev, 1016f734c89cSJan Kara map_bh->b_blocknr, 1017dd545b52SChandan Rajendra map_bh->b_size >> i_blkbits); 1018f734c89cSJan Kara } 10191da177e4SLinus Torvalds 1020eb28be2bSAndi Kleen if (!sdio->blkfactor) 10211da177e4SLinus Torvalds goto do_holes; 10221da177e4SLinus Torvalds 1023eb28be2bSAndi Kleen blkmask = (1 << sdio->blkfactor) - 1; 1024eb28be2bSAndi Kleen dio_remainder = (sdio->block_in_file & blkmask); 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* 10271da177e4SLinus Torvalds * If we are at the start of IO and that IO 10281da177e4SLinus Torvalds * starts partway into a fs-block, 10291da177e4SLinus Torvalds * dio_remainder will be non-zero. If the IO 10301da177e4SLinus Torvalds * is a read then we can simply advance the IO 10311da177e4SLinus Torvalds * cursor to the first block which is to be 10321da177e4SLinus Torvalds * read. But if the IO is a write and the 10331da177e4SLinus Torvalds * block was newly allocated we cannot do that; 10341da177e4SLinus Torvalds * the start of the fs block must be zeroed out 10351da177e4SLinus Torvalds * on-disk 10361da177e4SLinus Torvalds */ 10371da177e4SLinus Torvalds if (!buffer_new(map_bh)) 1038eb28be2bSAndi Kleen sdio->next_block_for_io += dio_remainder; 1039eb28be2bSAndi Kleen sdio->blocks_available -= dio_remainder; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds do_holes: 10421da177e4SLinus Torvalds /* Handle holes */ 10431da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) { 104435dc8161SJeff Moyer loff_t i_size_aligned; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds /* AKPM: eargh, -ENOTBLK is a hack */ 10478a4c1e42SMike Christie if (dio->op == REQ_OP_WRITE) { 104809cbfeafSKirill A. Shutemov put_page(page); 10491da177e4SLinus Torvalds return -ENOTBLK; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 105235dc8161SJeff Moyer /* 105335dc8161SJeff Moyer * Be sure to account for a partial block as the 105435dc8161SJeff Moyer * last block in the file 105535dc8161SJeff Moyer */ 105635dc8161SJeff Moyer i_size_aligned = ALIGN(i_size_read(dio->inode), 105735dc8161SJeff Moyer 1 << blkbits); 1058eb28be2bSAndi Kleen if (sdio->block_in_file >= 105935dc8161SJeff Moyer i_size_aligned >> blkbits) { 10601da177e4SLinus Torvalds /* We hit eof */ 106109cbfeafSKirill A. Shutemov put_page(page); 10621da177e4SLinus Torvalds goto out; 10631da177e4SLinus Torvalds } 10647b2c99d1SAl Viro zero_user(page, from, 1 << blkbits); 1065eb28be2bSAndi Kleen sdio->block_in_file++; 10667b2c99d1SAl Viro from += 1 << blkbits; 10673320c60bSAl Viro dio->result += 1 << blkbits; 10681da177e4SLinus Torvalds goto next_block; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds /* 10721da177e4SLinus Torvalds * If we're performing IO which has an alignment which 10731da177e4SLinus Torvalds * is finer than the underlying fs, go check to see if 10741da177e4SLinus Torvalds * we must zero out the start of this block. 10751da177e4SLinus Torvalds */ 1076eb28be2bSAndi Kleen if (unlikely(sdio->blkfactor && !sdio->start_zero_done)) 107718772641SAndi Kleen dio_zero_block(dio, sdio, 0, map_bh); 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds /* 10801da177e4SLinus Torvalds * Work out, in this_chunk_blocks, how much disk we 10811da177e4SLinus Torvalds * can add to this page 10821da177e4SLinus Torvalds */ 1083eb28be2bSAndi Kleen this_chunk_blocks = sdio->blocks_available; 10847b2c99d1SAl Viro u = (to - from) >> blkbits; 10851da177e4SLinus Torvalds if (this_chunk_blocks > u) 10861da177e4SLinus Torvalds this_chunk_blocks = u; 1087eb28be2bSAndi Kleen u = sdio->final_block_in_request - sdio->block_in_file; 10881da177e4SLinus Torvalds if (this_chunk_blocks > u) 10891da177e4SLinus Torvalds this_chunk_blocks = u; 10901da177e4SLinus Torvalds this_chunk_bytes = this_chunk_blocks << blkbits; 10911da177e4SLinus Torvalds BUG_ON(this_chunk_bytes == 0); 10921da177e4SLinus Torvalds 1093092c8d46SJan Kara if (this_chunk_blocks == sdio->blocks_available) 1094eb28be2bSAndi Kleen sdio->boundary = buffer_boundary(map_bh); 1095eb28be2bSAndi Kleen ret = submit_page_section(dio, sdio, page, 10967b2c99d1SAl Viro from, 1097eb28be2bSAndi Kleen this_chunk_bytes, 109818772641SAndi Kleen sdio->next_block_for_io, 109918772641SAndi Kleen map_bh); 11001da177e4SLinus Torvalds if (ret) { 110109cbfeafSKirill A. Shutemov put_page(page); 11021da177e4SLinus Torvalds goto out; 11031da177e4SLinus Torvalds } 1104eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 11051da177e4SLinus Torvalds 1106eb28be2bSAndi Kleen sdio->block_in_file += this_chunk_blocks; 11077b2c99d1SAl Viro from += this_chunk_bytes; 11087b2c99d1SAl Viro dio->result += this_chunk_bytes; 1109eb28be2bSAndi Kleen sdio->blocks_available -= this_chunk_blocks; 11101da177e4SLinus Torvalds next_block: 1111eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file > sdio->final_block_in_request); 1112eb28be2bSAndi Kleen if (sdio->block_in_file == sdio->final_block_in_request) 11131da177e4SLinus Torvalds break; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds /* Drop the ref which was taken in get_user_pages() */ 111709cbfeafSKirill A. Shutemov put_page(page); 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds out: 11201da177e4SLinus Torvalds return ret; 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 1123847cc637SAndi Kleen static inline int drop_refcount(struct dio *dio) 11241da177e4SLinus Torvalds { 1125847cc637SAndi Kleen int ret2; 11265eb6c7a2SZach Brown unsigned long flags; 112720258b2bSZach Brown 11281da177e4SLinus Torvalds /* 11298459d86aSZach Brown * Sync will always be dropping the final ref and completing the 11305eb6c7a2SZach Brown * operation. AIO can if it was a broken operation described above or 11315eb6c7a2SZach Brown * in fact if all the bios race to complete before we get here. In 11325eb6c7a2SZach Brown * that case dio_complete() translates the EIOCBQUEUED into the proper 113304b2fa9fSChristoph Hellwig * return code that the caller will hand to ->complete(). 11345eb6c7a2SZach Brown * 11355eb6c7a2SZach Brown * This is managed by the bio_lock instead of being an atomic_t so that 11365eb6c7a2SZach Brown * completion paths can drop their ref and use the remaining count to 11375eb6c7a2SZach Brown * decide to wake the submission path atomically. 11381da177e4SLinus Torvalds */ 11395eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 11405eb6c7a2SZach Brown ret2 = --dio->refcount; 11415eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 1142847cc637SAndi Kleen return ret2; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 1145eafdc7d1SChristoph Hellwig /* 1146eafdc7d1SChristoph Hellwig * This is a library function for use by filesystem drivers. 1147eafdc7d1SChristoph Hellwig * 1148eafdc7d1SChristoph Hellwig * The locking rules are governed by the flags parameter: 1149eafdc7d1SChristoph Hellwig * - if the flags value contains DIO_LOCKING we use a fancy locking 1150eafdc7d1SChristoph Hellwig * scheme for dumb filesystems. 1151eafdc7d1SChristoph Hellwig * For writes this function is called under i_mutex and returns with 1152eafdc7d1SChristoph Hellwig * i_mutex held, for reads, i_mutex is not held on entry, but it is 1153eafdc7d1SChristoph Hellwig * taken and dropped again before returning. 1154eafdc7d1SChristoph Hellwig * - if the flags value does NOT contain DIO_LOCKING we don't use any 1155eafdc7d1SChristoph Hellwig * internal locking but rather rely on the filesystem to synchronize 1156eafdc7d1SChristoph Hellwig * direct I/O reads/writes versus each other and truncate. 1157df2d6f26SChristoph Hellwig * 1158df2d6f26SChristoph Hellwig * To help with locking against truncate we incremented the i_dio_count 1159df2d6f26SChristoph Hellwig * counter before starting direct I/O, and decrement it once we are done. 1160df2d6f26SChristoph Hellwig * Truncate can wait for it to reach zero to provide exclusion. It is 1161df2d6f26SChristoph Hellwig * expected that filesystem provide exclusion between new direct I/O 1162df2d6f26SChristoph Hellwig * and truncates. For DIO_LOCKING filesystems this is done by i_mutex, 1163df2d6f26SChristoph Hellwig * but other filesystems need to take care of this on their own. 1164ba253fbfSAndi Kleen * 1165ba253fbfSAndi Kleen * NOTE: if you pass "sdio" to anything by pointer make sure that function 1166ba253fbfSAndi Kleen * is always inlined. Otherwise gcc is unable to split the structure into 1167ba253fbfSAndi Kleen * individual fields and will generate much worse code. This is important 1168ba253fbfSAndi Kleen * for the whole file. 1169eafdc7d1SChristoph Hellwig */ 117065dd2aa9SAndi Kleen static inline ssize_t 117117f8c842SOmar Sandoval do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, 117217f8c842SOmar Sandoval struct block_device *bdev, struct iov_iter *iter, 1173c8b8e32dSChristoph Hellwig get_block_t get_block, dio_iodone_t end_io, 1174facd07b0SJosef Bacik dio_submit_t submit_io, int flags) 11751da177e4SLinus Torvalds { 11766aa7de05SMark Rutland unsigned i_blkbits = READ_ONCE(inode->i_blkbits); 1177ab73857eSLinus Torvalds unsigned blkbits = i_blkbits; 11781da177e4SLinus Torvalds unsigned blocksize_mask = (1 << blkbits) - 1; 11791da177e4SLinus Torvalds ssize_t retval = -EINVAL; 11801c0ff0f1SNikolay Borisov const size_t count = iov_iter_count(iter); 1181c8b8e32dSChristoph Hellwig loff_t offset = iocb->ki_pos; 11821c0ff0f1SNikolay Borisov const loff_t end = offset + count; 11831da177e4SLinus Torvalds struct dio *dio; 1184eb28be2bSAndi Kleen struct dio_submit sdio = { 0, }; 1185847cc637SAndi Kleen struct buffer_head map_bh = { 0, }; 1186647d1e4cSFengguang Wu struct blk_plug plug; 1187886a3911SAl Viro unsigned long align = offset | iov_iter_alignment(iter); 11881da177e4SLinus Torvalds 118965dd2aa9SAndi Kleen /* 119065dd2aa9SAndi Kleen * Avoid references to bdev if not absolutely needed to give 119165dd2aa9SAndi Kleen * the early prefetch in the caller enough time. 119265dd2aa9SAndi Kleen */ 11931da177e4SLinus Torvalds 1194886a3911SAl Viro if (align & blocksize_mask) { 11951da177e4SLinus Torvalds if (bdev) 119665dd2aa9SAndi Kleen blkbits = blksize_bits(bdev_logical_block_size(bdev)); 11971da177e4SLinus Torvalds blocksize_mask = (1 << blkbits) - 1; 1198886a3911SAl Viro if (align & blocksize_mask) 11991da177e4SLinus Torvalds goto out; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 1202f9b5570dSChristoph Hellwig /* watch out for a 0 len io from a tricksy fs */ 12031c0ff0f1SNikolay Borisov if (iov_iter_rw(iter) == READ && !count) 1204f9b5570dSChristoph Hellwig return 0; 1205f9b5570dSChristoph Hellwig 12066e8267f5SAndi Kleen dio = kmem_cache_alloc(dio_cache, GFP_KERNEL); 12071da177e4SLinus Torvalds retval = -ENOMEM; 12081da177e4SLinus Torvalds if (!dio) 12091da177e4SLinus Torvalds goto out; 121023aee091SJeff Moyer /* 121123aee091SJeff Moyer * Believe it or not, zeroing out the page array caused a .5% 121223aee091SJeff Moyer * performance regression in a database benchmark. So, we take 121323aee091SJeff Moyer * care to only zero out what's needed. 121423aee091SJeff Moyer */ 121523aee091SJeff Moyer memset(dio, 0, offsetof(struct dio, pages)); 12161da177e4SLinus Torvalds 12175fe878aeSChristoph Hellwig dio->flags = flags; 12185fe878aeSChristoph Hellwig if (dio->flags & DIO_LOCKING) { 121917f8c842SOmar Sandoval if (iov_iter_rw(iter) == READ) { 12205fe878aeSChristoph Hellwig struct address_space *mapping = 12215fe878aeSChristoph Hellwig iocb->ki_filp->f_mapping; 12221da177e4SLinus Torvalds 12235fe878aeSChristoph Hellwig /* will be released by direct_io_worker */ 12245955102cSAl Viro inode_lock(inode); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds retval = filemap_write_and_wait_range(mapping, offset, 12271da177e4SLinus Torvalds end - 1); 12281da177e4SLinus Torvalds if (retval) { 12295955102cSAl Viro inode_unlock(inode); 12306e8267f5SAndi Kleen kmem_cache_free(dio_cache, dio); 12311da177e4SLinus Torvalds goto out; 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds } 1234df2d6f26SChristoph Hellwig } 12351da177e4SLinus Torvalds 123674cedf9bSJan Kara /* Once we sampled i_size check for reads beyond EOF */ 123774cedf9bSJan Kara dio->i_size = i_size_read(inode); 123874cedf9bSJan Kara if (iov_iter_rw(iter) == READ && offset >= dio->i_size) { 123974cedf9bSJan Kara if (dio->flags & DIO_LOCKING) 12405955102cSAl Viro inode_unlock(inode); 124174cedf9bSJan Kara kmem_cache_free(dio_cache, dio); 12422d4594acSAl Viro retval = 0; 124374cedf9bSJan Kara goto out; 124474cedf9bSJan Kara } 124574cedf9bSJan Kara 12465fe878aeSChristoph Hellwig /* 124760392573SChristoph Hellwig * For file extending writes updating i_size before data writeouts 124860392573SChristoph Hellwig * complete can expose uninitialized blocks in dumb filesystems. 124960392573SChristoph Hellwig * In that case we need to wait for I/O completion even if asked 125060392573SChristoph Hellwig * for an asynchronous write. 12511da177e4SLinus Torvalds */ 125260392573SChristoph Hellwig if (is_sync_kiocb(iocb)) 125360392573SChristoph Hellwig dio->is_async = false; 1254c8f4c36fSNikolay Borisov else if (iov_iter_rw(iter) == WRITE && end > i_size_read(inode)) 125560392573SChristoph Hellwig dio->is_async = false; 125660392573SChristoph Hellwig else 125760392573SChristoph Hellwig dio->is_async = true; 125860392573SChristoph Hellwig 1259847cc637SAndi Kleen dio->inode = inode; 12608a4c1e42SMike Christie if (iov_iter_rw(iter) == WRITE) { 12618a4c1e42SMike Christie dio->op = REQ_OP_WRITE; 126270fd7614SChristoph Hellwig dio->op_flags = REQ_SYNC | REQ_IDLE; 126303a07c92SGoldwyn Rodrigues if (iocb->ki_flags & IOCB_NOWAIT) 126403a07c92SGoldwyn Rodrigues dio->op_flags |= REQ_NOWAIT; 12658a4c1e42SMike Christie } else { 12668a4c1e42SMike Christie dio->op = REQ_OP_READ; 12678a4c1e42SMike Christie } 126802afc27fSChristoph Hellwig 126902afc27fSChristoph Hellwig /* 127002afc27fSChristoph Hellwig * For AIO O_(D)SYNC writes we need to defer completions to a workqueue 127102afc27fSChristoph Hellwig * so that we can call ->fsync. 127202afc27fSChristoph Hellwig */ 1273332391a9SLukas Czerner if (dio->is_async && iov_iter_rw(iter) == WRITE) { 1274332391a9SLukas Czerner retval = 0; 1275d9c10e5bSJan Kara if (iocb->ki_flags & IOCB_DSYNC) 127602afc27fSChristoph Hellwig retval = dio_set_defer_completion(dio); 1277332391a9SLukas Czerner else if (!dio->inode->i_sb->s_dio_done_wq) { 1278332391a9SLukas Czerner /* 1279332391a9SLukas Czerner * In case of AIO write racing with buffered read we 1280332391a9SLukas Czerner * need to defer completion. We can't decide this now, 1281332391a9SLukas Czerner * however the workqueue needs to be initialized here. 1282332391a9SLukas Czerner */ 1283332391a9SLukas Czerner retval = sb_init_dio_done_wq(dio->inode->i_sb); 1284332391a9SLukas Czerner } 128502afc27fSChristoph Hellwig if (retval) { 128602afc27fSChristoph Hellwig /* 128702afc27fSChristoph Hellwig * We grab i_mutex only for reads so we don't have 128802afc27fSChristoph Hellwig * to release it here 128902afc27fSChristoph Hellwig */ 129002afc27fSChristoph Hellwig kmem_cache_free(dio_cache, dio); 129102afc27fSChristoph Hellwig goto out; 129202afc27fSChristoph Hellwig } 129302afc27fSChristoph Hellwig } 129402afc27fSChristoph Hellwig 129502afc27fSChristoph Hellwig /* 129602afc27fSChristoph Hellwig * Will be decremented at I/O completion time. 129702afc27fSChristoph Hellwig */ 1298fe0f07d0SJens Axboe inode_dio_begin(inode); 129902afc27fSChristoph Hellwig 130002afc27fSChristoph Hellwig retval = 0; 1301847cc637SAndi Kleen sdio.blkbits = blkbits; 1302ab73857eSLinus Torvalds sdio.blkfactor = i_blkbits - blkbits; 1303847cc637SAndi Kleen sdio.block_in_file = offset >> blkbits; 1304847cc637SAndi Kleen 1305847cc637SAndi Kleen sdio.get_block = get_block; 1306847cc637SAndi Kleen dio->end_io = end_io; 1307847cc637SAndi Kleen sdio.submit_io = submit_io; 1308847cc637SAndi Kleen sdio.final_block_in_bio = -1; 1309847cc637SAndi Kleen sdio.next_block_for_io = -1; 1310847cc637SAndi Kleen 1311847cc637SAndi Kleen dio->iocb = iocb; 1312847cc637SAndi Kleen 1313847cc637SAndi Kleen spin_lock_init(&dio->bio_lock); 1314847cc637SAndi Kleen dio->refcount = 1; 1315847cc637SAndi Kleen 131600e23707SDavid Howells dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ; 13177b2c99d1SAl Viro sdio.iter = iter; 13181c0ff0f1SNikolay Borisov sdio.final_block_in_request = end >> blkbits; 13197b2c99d1SAl Viro 1320847cc637SAndi Kleen /* 1321847cc637SAndi Kleen * In case of non-aligned buffers, we may need 2 more 1322847cc637SAndi Kleen * pages since we need to zero out first and last block. 1323847cc637SAndi Kleen */ 1324847cc637SAndi Kleen if (unlikely(sdio.blkfactor)) 1325847cc637SAndi Kleen sdio.pages_in_io = 2; 1326847cc637SAndi Kleen 1327f67da30cSAl Viro sdio.pages_in_io += iov_iter_npages(iter, INT_MAX); 1328847cc637SAndi Kleen 1329647d1e4cSFengguang Wu blk_start_plug(&plug); 1330647d1e4cSFengguang Wu 1331847cc637SAndi Kleen retval = do_direct_IO(dio, &sdio, &map_bh); 13327b2c99d1SAl Viro if (retval) 1333847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1334847cc637SAndi Kleen 1335847cc637SAndi Kleen if (retval == -ENOTBLK) { 1336847cc637SAndi Kleen /* 1337847cc637SAndi Kleen * The remaining part of the request will be 1338847cc637SAndi Kleen * be handled by buffered I/O when we return 1339847cc637SAndi Kleen */ 1340847cc637SAndi Kleen retval = 0; 1341847cc637SAndi Kleen } 1342847cc637SAndi Kleen /* 1343847cc637SAndi Kleen * There may be some unwritten disk at the end of a part-written 1344847cc637SAndi Kleen * fs-block-sized block. Go zero that now. 1345847cc637SAndi Kleen */ 1346847cc637SAndi Kleen dio_zero_block(dio, &sdio, 1, &map_bh); 1347847cc637SAndi Kleen 1348847cc637SAndi Kleen if (sdio.cur_page) { 1349847cc637SAndi Kleen ssize_t ret2; 1350847cc637SAndi Kleen 1351847cc637SAndi Kleen ret2 = dio_send_cur_page(dio, &sdio, &map_bh); 1352847cc637SAndi Kleen if (retval == 0) 1353847cc637SAndi Kleen retval = ret2; 135409cbfeafSKirill A. Shutemov put_page(sdio.cur_page); 1355847cc637SAndi Kleen sdio.cur_page = NULL; 1356847cc637SAndi Kleen } 1357847cc637SAndi Kleen if (sdio.bio) 1358847cc637SAndi Kleen dio_bio_submit(dio, &sdio); 1359847cc637SAndi Kleen 1360647d1e4cSFengguang Wu blk_finish_plug(&plug); 1361647d1e4cSFengguang Wu 1362847cc637SAndi Kleen /* 1363847cc637SAndi Kleen * It is possible that, we return short IO due to end of file. 1364847cc637SAndi Kleen * In that case, we need to release all the pages we got hold on. 1365847cc637SAndi Kleen */ 1366847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1367847cc637SAndi Kleen 1368847cc637SAndi Kleen /* 1369847cc637SAndi Kleen * All block lookups have been performed. For READ requests 1370847cc637SAndi Kleen * we can let i_mutex go now that its achieved its purpose 1371847cc637SAndi Kleen * of protecting us from looking up uninitialized blocks. 1372847cc637SAndi Kleen */ 137317f8c842SOmar Sandoval if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING)) 13745955102cSAl Viro inode_unlock(dio->inode); 1375847cc637SAndi Kleen 1376847cc637SAndi Kleen /* 1377847cc637SAndi Kleen * The only time we want to leave bios in flight is when a successful 1378847cc637SAndi Kleen * partial aio read or full aio write have been setup. In that case 1379847cc637SAndi Kleen * bio completion will call aio_complete. The only time it's safe to 1380847cc637SAndi Kleen * call aio_complete is when we return -EIOCBQUEUED, so we key on that. 1381847cc637SAndi Kleen * This had *better* be the only place that raises -EIOCBQUEUED. 1382847cc637SAndi Kleen */ 1383847cc637SAndi Kleen BUG_ON(retval == -EIOCBQUEUED); 1384847cc637SAndi Kleen if (dio->is_async && retval == 0 && dio->result && 138517f8c842SOmar Sandoval (iov_iter_rw(iter) == READ || dio->result == count)) 1386847cc637SAndi Kleen retval = -EIOCBQUEUED; 1387af436472SChristoph Hellwig else 1388847cc637SAndi Kleen dio_await_completion(dio); 1389847cc637SAndi Kleen 1390847cc637SAndi Kleen if (drop_refcount(dio) == 0) { 1391ffe51f01SLukas Czerner retval = dio_complete(dio, retval, DIO_COMPLETE_INVALIDATE); 1392847cc637SAndi Kleen } else 1393847cc637SAndi Kleen BUG_ON(retval != -EIOCBQUEUED); 13941da177e4SLinus Torvalds 13957bb46a67Snpiggin@suse.de out: 13967bb46a67Snpiggin@suse.de return retval; 13977bb46a67Snpiggin@suse.de } 139865dd2aa9SAndi Kleen 139917f8c842SOmar Sandoval ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, 140017f8c842SOmar Sandoval struct block_device *bdev, struct iov_iter *iter, 1401c8b8e32dSChristoph Hellwig get_block_t get_block, 140217f8c842SOmar Sandoval dio_iodone_t end_io, dio_submit_t submit_io, 140317f8c842SOmar Sandoval int flags) 140465dd2aa9SAndi Kleen { 140565dd2aa9SAndi Kleen /* 140665dd2aa9SAndi Kleen * The block device state is needed in the end to finally 140765dd2aa9SAndi Kleen * submit everything. Since it's likely to be cache cold 140865dd2aa9SAndi Kleen * prefetch it here as first thing to hide some of the 140965dd2aa9SAndi Kleen * latency. 141065dd2aa9SAndi Kleen * 141165dd2aa9SAndi Kleen * Attempt to prefetch the pieces we likely need later. 141265dd2aa9SAndi Kleen */ 141365dd2aa9SAndi Kleen prefetch(&bdev->bd_disk->part_tbl); 141465dd2aa9SAndi Kleen prefetch(bdev->bd_queue); 141565dd2aa9SAndi Kleen prefetch((char *)bdev->bd_queue + SMP_CACHE_BYTES); 141665dd2aa9SAndi Kleen 1417c8b8e32dSChristoph Hellwig return do_blockdev_direct_IO(iocb, inode, bdev, iter, get_block, 141817f8c842SOmar Sandoval end_io, submit_io, flags); 141965dd2aa9SAndi Kleen } 142065dd2aa9SAndi Kleen 14211da177e4SLinus Torvalds EXPORT_SYMBOL(__blockdev_direct_IO); 14226e8267f5SAndi Kleen 14236e8267f5SAndi Kleen static __init int dio_init(void) 14246e8267f5SAndi Kleen { 14256e8267f5SAndi Kleen dio_cache = KMEM_CACHE(dio, SLAB_PANIC); 14266e8267f5SAndi Kleen return 0; 14276e8267f5SAndi Kleen } 14286e8267f5SAndi Kleen module_init(dio_init) 1429