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 /* 481da177e4SLinus Torvalds * This code generally works in units of "dio_blocks". A dio_block is 491da177e4SLinus Torvalds * somewhere between the hard sector size and the filesystem block size. it 501da177e4SLinus Torvalds * is determined on a per-invocation basis. When talking to the filesystem 511da177e4SLinus Torvalds * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity 521da177e4SLinus Torvalds * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted 531da177e4SLinus Torvalds * to bio_block quantities by shifting left by blkfactor. 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * If blkfactor is zero then the user's request was aligned to the filesystem's 561da177e4SLinus Torvalds * blocksize. 571da177e4SLinus Torvalds */ 581da177e4SLinus Torvalds 59eb28be2bSAndi Kleen /* dio_state only used in the submission path */ 60eb28be2bSAndi Kleen 61eb28be2bSAndi Kleen struct dio_submit { 621da177e4SLinus Torvalds struct bio *bio; /* bio under assembly */ 631da177e4SLinus Torvalds unsigned blkbits; /* doesn't change */ 641da177e4SLinus Torvalds unsigned blkfactor; /* When we're using an alignment which 651da177e4SLinus Torvalds is finer than the filesystem's soft 661da177e4SLinus Torvalds blocksize, this specifies how much 671da177e4SLinus Torvalds finer. blkfactor=2 means 1/4-block 681da177e4SLinus Torvalds alignment. Does not change */ 691da177e4SLinus Torvalds unsigned start_zero_done; /* flag: sub-blocksize zeroing has 701da177e4SLinus Torvalds been performed at the start of a 711da177e4SLinus Torvalds write */ 721da177e4SLinus Torvalds int pages_in_io; /* approximate total IO pages */ 731da177e4SLinus Torvalds sector_t block_in_file; /* Current offset into the underlying 741da177e4SLinus Torvalds file in dio_block units. */ 751da177e4SLinus Torvalds unsigned blocks_available; /* At block_in_file. changes */ 760dc2bc49SAndi Kleen int reap_counter; /* rate limit reaping */ 771da177e4SLinus Torvalds sector_t final_block_in_request;/* doesn't change */ 781da177e4SLinus Torvalds int boundary; /* prev block is at a boundary */ 791d8fa7a2SBadari Pulavarty get_block_t *get_block; /* block mapping function */ 80facd07b0SJosef Bacik dio_submit_t *submit_io; /* IO submition function */ 81eb28be2bSAndi Kleen 82facd07b0SJosef Bacik loff_t logical_offset_in_bio; /* current first logical block in bio */ 831da177e4SLinus Torvalds sector_t final_block_in_bio; /* current final block in bio + 1 */ 841da177e4SLinus Torvalds sector_t next_block_for_io; /* next block to be put under IO, 851da177e4SLinus Torvalds in dio_blocks units */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* 881da177e4SLinus Torvalds * Deferred addition of a page to the dio. These variables are 891da177e4SLinus Torvalds * private to dio_send_cur_page(), submit_page_section() and 901da177e4SLinus Torvalds * dio_bio_add_page(). 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds struct page *cur_page; /* The page */ 931da177e4SLinus Torvalds unsigned cur_page_offset; /* Offset into it, in bytes */ 941da177e4SLinus Torvalds unsigned cur_page_len; /* Nr of bytes at cur_page_offset */ 951da177e4SLinus Torvalds sector_t cur_page_block; /* Where it starts */ 96facd07b0SJosef Bacik loff_t cur_page_fs_offset; /* Offset in file */ 971da177e4SLinus Torvalds 987b2c99d1SAl Viro struct iov_iter *iter; 9923aee091SJeff Moyer /* 10023aee091SJeff Moyer * Page queue. These variables belong to dio_refill_pages() and 10123aee091SJeff Moyer * dio_get_page(). 10223aee091SJeff Moyer */ 10323aee091SJeff Moyer unsigned head; /* next page to process */ 10423aee091SJeff Moyer unsigned tail; /* last valid page + 1 */ 1057b2c99d1SAl Viro size_t from, to; 106eb28be2bSAndi Kleen }; 107eb28be2bSAndi Kleen 108eb28be2bSAndi Kleen /* dio_state communicated between submission path and end_io */ 109eb28be2bSAndi Kleen struct dio { 110eb28be2bSAndi Kleen int flags; /* doesn't change */ 111eb28be2bSAndi Kleen int rw; 1120dc2bc49SAndi Kleen struct inode *inode; 113eb28be2bSAndi Kleen loff_t i_size; /* i_size when submitted */ 114eb28be2bSAndi Kleen dio_iodone_t *end_io; /* IO completion function */ 115eb28be2bSAndi Kleen 11618772641SAndi Kleen void *private; /* copy from map_bh.b_private */ 117eb28be2bSAndi Kleen 118eb28be2bSAndi Kleen /* BIO completion state */ 119eb28be2bSAndi Kleen spinlock_t bio_lock; /* protects BIO fields below */ 1200dc2bc49SAndi Kleen int page_errors; /* errno from get_user_pages() */ 1210dc2bc49SAndi Kleen int is_async; /* is IO async ? */ 1227b7a8665SChristoph Hellwig bool defer_completion; /* defer AIO completion to workqueue? */ 123*53cbf3b1SMing Lei bool should_dirty; /* if pages should be dirtied */ 1240dc2bc49SAndi Kleen int io_error; /* IO error in completion path */ 125eb28be2bSAndi Kleen unsigned long refcount; /* direct_io_worker() and bios */ 126eb28be2bSAndi Kleen struct bio *bio_list; /* singly linked via bi_private */ 127eb28be2bSAndi Kleen struct task_struct *waiter; /* waiting task (NULL if none) */ 128eb28be2bSAndi Kleen 129eb28be2bSAndi Kleen /* AIO related stuff */ 130eb28be2bSAndi Kleen struct kiocb *iocb; /* kiocb */ 131eb28be2bSAndi Kleen ssize_t result; /* IO result */ 132eb28be2bSAndi Kleen 13323aee091SJeff Moyer /* 13423aee091SJeff Moyer * pages[] (and any fields placed after it) are not zeroed out at 13523aee091SJeff Moyer * allocation time. Don't add new fields after pages[] unless you 13623aee091SJeff Moyer * wish that they not be zeroed. 13723aee091SJeff Moyer */ 1387b7a8665SChristoph Hellwig union { 13923aee091SJeff Moyer struct page *pages[DIO_PAGES]; /* page buffer */ 1407b7a8665SChristoph Hellwig struct work_struct complete_work;/* deferred AIO completion */ 1417b7a8665SChristoph Hellwig }; 1426e8267f5SAndi Kleen } ____cacheline_aligned_in_smp; 1436e8267f5SAndi Kleen 1446e8267f5SAndi Kleen static struct kmem_cache *dio_cache __read_mostly; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 1471da177e4SLinus Torvalds * How many pages are in the queue? 1481da177e4SLinus Torvalds */ 149eb28be2bSAndi Kleen static inline unsigned dio_pages_present(struct dio_submit *sdio) 1501da177e4SLinus Torvalds { 151eb28be2bSAndi Kleen return sdio->tail - sdio->head; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 1551da177e4SLinus Torvalds * Go grab and pin some userspace pages. Typically we'll get 64 at a time. 1561da177e4SLinus Torvalds */ 157ba253fbfSAndi Kleen static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio) 1581da177e4SLinus Torvalds { 1597b2c99d1SAl Viro ssize_t ret; 1601da177e4SLinus Torvalds 1612c80929cSMiklos Szeredi ret = iov_iter_get_pages(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES, 1627b2c99d1SAl Viro &sdio->from); 1631da177e4SLinus Torvalds 164eb28be2bSAndi Kleen if (ret < 0 && sdio->blocks_available && (dio->rw & WRITE)) { 165557ed1faSNick Piggin struct page *page = ZERO_PAGE(0); 1661da177e4SLinus Torvalds /* 1671da177e4SLinus Torvalds * A memory fault, but the filesystem has some outstanding 1681da177e4SLinus Torvalds * mapped blocks. We need to use those blocks up to avoid 1691da177e4SLinus Torvalds * leaking stale data in the file. 1701da177e4SLinus Torvalds */ 1711da177e4SLinus Torvalds if (dio->page_errors == 0) 1721da177e4SLinus Torvalds dio->page_errors = ret; 173b5810039SNick Piggin page_cache_get(page); 174b5810039SNick Piggin dio->pages[0] = page; 175eb28be2bSAndi Kleen sdio->head = 0; 176eb28be2bSAndi Kleen sdio->tail = 1; 1777b2c99d1SAl Viro sdio->from = 0; 1787b2c99d1SAl Viro sdio->to = PAGE_SIZE; 1797b2c99d1SAl Viro return 0; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds if (ret >= 0) { 1837b2c99d1SAl Viro iov_iter_advance(sdio->iter, ret); 1847b2c99d1SAl Viro ret += sdio->from; 185eb28be2bSAndi Kleen sdio->head = 0; 1867b2c99d1SAl Viro sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE; 1877b2c99d1SAl Viro sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1; 1887b2c99d1SAl Viro return 0; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds return ret; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* 1941da177e4SLinus Torvalds * Get another userspace page. Returns an ERR_PTR on error. Pages are 1951da177e4SLinus Torvalds * buffered inside the dio so that we can call get_user_pages() against a 1961da177e4SLinus Torvalds * decent number of pages, less frequently. To provide nicer use of the 1971da177e4SLinus Torvalds * L1 cache. 1981da177e4SLinus Torvalds */ 199ba253fbfSAndi Kleen static inline struct page *dio_get_page(struct dio *dio, 2006fcc5420SBoaz Harrosh struct dio_submit *sdio) 2011da177e4SLinus Torvalds { 202eb28be2bSAndi Kleen if (dio_pages_present(sdio) == 0) { 2031da177e4SLinus Torvalds int ret; 2041da177e4SLinus Torvalds 205eb28be2bSAndi Kleen ret = dio_refill_pages(dio, sdio); 2061da177e4SLinus Torvalds if (ret) 2071da177e4SLinus Torvalds return ERR_PTR(ret); 208eb28be2bSAndi Kleen BUG_ON(dio_pages_present(sdio) == 0); 2091da177e4SLinus Torvalds } 2106fcc5420SBoaz Harrosh return dio->pages[sdio->head]; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2136d544bb4SZach Brown /** 2146d544bb4SZach Brown * dio_complete() - called when all DIO BIO I/O has been completed 2156d544bb4SZach Brown * @offset: the byte offset in the file of the completed operation 2166d544bb4SZach Brown * 2177b7a8665SChristoph Hellwig * This drops i_dio_count, lets interested parties know that a DIO operation 2187b7a8665SChristoph Hellwig * has completed, and calculates the resulting return code for the operation. 2196d544bb4SZach Brown * 2206d544bb4SZach Brown * It lets the filesystem know if it registered an interest earlier via 2216d544bb4SZach Brown * get_block. Pass the private field of the map buffer_head so that 2226d544bb4SZach Brown * filesystems can use it to hold additional state between get_block calls and 2236d544bb4SZach Brown * dio_complete. 2241da177e4SLinus Torvalds */ 2257b7a8665SChristoph Hellwig static ssize_t dio_complete(struct dio *dio, loff_t offset, ssize_t ret, 2267b7a8665SChristoph Hellwig bool is_async) 2271da177e4SLinus Torvalds { 2286d544bb4SZach Brown ssize_t transferred = 0; 2296d544bb4SZach Brown 2308459d86aSZach Brown /* 2318459d86aSZach Brown * AIO submission can race with bio completion to get here while 2328459d86aSZach Brown * expecting to have the last io completed by bio completion. 2338459d86aSZach Brown * In that case -EIOCBQUEUED is in fact not an error we want 2348459d86aSZach Brown * to preserve through this call. 2358459d86aSZach Brown */ 2368459d86aSZach Brown if (ret == -EIOCBQUEUED) 2378459d86aSZach Brown ret = 0; 2388459d86aSZach Brown 2396d544bb4SZach Brown if (dio->result) { 2406d544bb4SZach Brown transferred = dio->result; 2416d544bb4SZach Brown 2426d544bb4SZach Brown /* Check for short read case */ 2436d544bb4SZach Brown if ((dio->rw == READ) && ((offset + transferred) > dio->i_size)) 2446d544bb4SZach Brown transferred = dio->i_size - offset; 2456d544bb4SZach Brown } 2466d544bb4SZach Brown 2476d544bb4SZach Brown if (ret == 0) 2486d544bb4SZach Brown ret = dio->page_errors; 2496d544bb4SZach Brown if (ret == 0) 2506d544bb4SZach Brown ret = dio->io_error; 2516d544bb4SZach Brown if (ret == 0) 2526d544bb4SZach Brown ret = transferred; 2536d544bb4SZach Brown 2547b7a8665SChristoph Hellwig if (dio->end_io && dio->result) 2557b7a8665SChristoph Hellwig dio->end_io(dio->iocb, offset, transferred, dio->private); 2567b7a8665SChristoph Hellwig 257fe0f07d0SJens Axboe if (!(dio->flags & DIO_SKIP_DIO_COUNT)) 258fe0f07d0SJens Axboe inode_dio_end(dio->inode); 259fe0f07d0SJens Axboe 26002afc27fSChristoph Hellwig if (is_async) { 26102afc27fSChristoph Hellwig if (dio->rw & WRITE) { 26202afc27fSChristoph Hellwig int err; 26302afc27fSChristoph Hellwig 26402afc27fSChristoph Hellwig err = generic_write_sync(dio->iocb->ki_filp, offset, 26502afc27fSChristoph Hellwig transferred); 26602afc27fSChristoph Hellwig if (err < 0 && ret > 0) 26702afc27fSChristoph Hellwig ret = err; 26802afc27fSChristoph Hellwig } 26902afc27fSChristoph Hellwig 27004b2fa9fSChristoph Hellwig dio->iocb->ki_complete(dio->iocb, ret, 0); 27102afc27fSChristoph Hellwig } 27240e2e973SChristoph Hellwig 2737b7a8665SChristoph Hellwig kmem_cache_free(dio_cache, dio); 2746d544bb4SZach Brown return ret; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2777b7a8665SChristoph Hellwig static void dio_aio_complete_work(struct work_struct *work) 2787b7a8665SChristoph Hellwig { 2797b7a8665SChristoph Hellwig struct dio *dio = container_of(work, struct dio, complete_work); 2807b7a8665SChristoph Hellwig 2817b7a8665SChristoph Hellwig dio_complete(dio, dio->iocb->ki_pos, 0, true); 2827b7a8665SChristoph Hellwig } 2837b7a8665SChristoph Hellwig 2841da177e4SLinus Torvalds static int dio_bio_complete(struct dio *dio, struct bio *bio); 2857b7a8665SChristoph Hellwig 2861da177e4SLinus Torvalds /* 2871da177e4SLinus Torvalds * Asynchronous IO callback. 2881da177e4SLinus Torvalds */ 2894246a0b6SChristoph Hellwig static void dio_bio_end_aio(struct bio *bio) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 2925eb6c7a2SZach Brown unsigned long remaining; 2935eb6c7a2SZach Brown unsigned long flags; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* cleanup the bio */ 2961da177e4SLinus Torvalds dio_bio_complete(dio, bio); 2970273201eSZach Brown 2985eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 2995eb6c7a2SZach Brown remaining = --dio->refcount; 3005eb6c7a2SZach Brown if (remaining == 1 && dio->waiter) 30120258b2bSZach Brown wake_up_process(dio->waiter); 3025eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 30320258b2bSZach Brown 3048459d86aSZach Brown if (remaining == 0) { 3057b7a8665SChristoph Hellwig if (dio->result && dio->defer_completion) { 3067b7a8665SChristoph Hellwig INIT_WORK(&dio->complete_work, dio_aio_complete_work); 3077b7a8665SChristoph Hellwig queue_work(dio->inode->i_sb->s_dio_done_wq, 3087b7a8665SChristoph Hellwig &dio->complete_work); 3097b7a8665SChristoph Hellwig } else { 31040e2e973SChristoph Hellwig dio_complete(dio, dio->iocb->ki_pos, 0, true); 3117b7a8665SChristoph Hellwig } 3128459d86aSZach Brown } 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 3161da177e4SLinus Torvalds * The BIO completion handler simply queues the BIO up for the process-context 3171da177e4SLinus Torvalds * handler. 3181da177e4SLinus Torvalds * 3191da177e4SLinus Torvalds * During I/O bi_private points at the dio. After I/O, bi_private is used to 3201da177e4SLinus Torvalds * implement a singly-linked list of completed BIOs, at dio->bio_list. 3211da177e4SLinus Torvalds */ 3224246a0b6SChristoph Hellwig static void dio_bio_end_io(struct bio *bio) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 3251da177e4SLinus Torvalds unsigned long flags; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 3281da177e4SLinus Torvalds bio->bi_private = dio->bio_list; 3291da177e4SLinus Torvalds dio->bio_list = bio; 3305eb6c7a2SZach Brown if (--dio->refcount == 1 && dio->waiter) 3311da177e4SLinus Torvalds wake_up_process(dio->waiter); 3321da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 335facd07b0SJosef Bacik /** 336facd07b0SJosef Bacik * dio_end_io - handle the end io action for the given bio 337facd07b0SJosef Bacik * @bio: The direct io bio thats being completed 338facd07b0SJosef Bacik * @error: Error if there was one 339facd07b0SJosef Bacik * 340facd07b0SJosef Bacik * This is meant to be called by any filesystem that uses their own dio_submit_t 341facd07b0SJosef Bacik * so that the DIO specific endio actions are dealt with after the filesystem 342facd07b0SJosef Bacik * has done it's completion work. 343facd07b0SJosef Bacik */ 344facd07b0SJosef Bacik void dio_end_io(struct bio *bio, int error) 345facd07b0SJosef Bacik { 346facd07b0SJosef Bacik struct dio *dio = bio->bi_private; 347facd07b0SJosef Bacik 348facd07b0SJosef Bacik if (dio->is_async) 3494246a0b6SChristoph Hellwig dio_bio_end_aio(bio); 350facd07b0SJosef Bacik else 3514246a0b6SChristoph Hellwig dio_bio_end_io(bio); 352facd07b0SJosef Bacik } 353facd07b0SJosef Bacik EXPORT_SYMBOL_GPL(dio_end_io); 354facd07b0SJosef Bacik 355ba253fbfSAndi Kleen static inline void 356eb28be2bSAndi Kleen dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, 357eb28be2bSAndi Kleen struct block_device *bdev, 3581da177e4SLinus Torvalds sector_t first_sector, int nr_vecs) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds struct bio *bio; 3611da177e4SLinus Torvalds 36220d9600cSDavid Dillow /* 36320d9600cSDavid Dillow * bio_alloc() is guaranteed to return a bio when called with 36420d9600cSDavid Dillow * __GFP_WAIT and we request a valid number of vectors. 36520d9600cSDavid Dillow */ 3661da177e4SLinus Torvalds bio = bio_alloc(GFP_KERNEL, nr_vecs); 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds bio->bi_bdev = bdev; 3694f024f37SKent Overstreet bio->bi_iter.bi_sector = first_sector; 3701da177e4SLinus Torvalds if (dio->is_async) 3711da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_aio; 3721da177e4SLinus Torvalds else 3731da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_io; 3741da177e4SLinus Torvalds 375eb28be2bSAndi Kleen sdio->bio = bio; 376eb28be2bSAndi Kleen sdio->logical_offset_in_bio = sdio->cur_page_fs_offset; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds /* 3801da177e4SLinus Torvalds * In the AIO read case we speculatively dirty the pages before starting IO. 3811da177e4SLinus Torvalds * During IO completion, any of these pages which happen to have been written 3821da177e4SLinus Torvalds * back will be redirtied by bio_check_pages_dirty(). 3830273201eSZach Brown * 3840273201eSZach Brown * bios hold a dio reference between submit_bio and ->end_io. 3851da177e4SLinus Torvalds */ 386ba253fbfSAndi Kleen static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio) 3871da177e4SLinus Torvalds { 388eb28be2bSAndi Kleen struct bio *bio = sdio->bio; 3895eb6c7a2SZach Brown unsigned long flags; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds bio->bi_private = dio; 3925eb6c7a2SZach Brown 3935eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 3945eb6c7a2SZach Brown dio->refcount++; 3955eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 3965eb6c7a2SZach Brown 397*53cbf3b1SMing Lei if (dio->is_async && dio->rw == READ && dio->should_dirty) 3981da177e4SLinus Torvalds bio_set_pages_dirty(bio); 3995eb6c7a2SZach Brown 400eb28be2bSAndi Kleen if (sdio->submit_io) 401eb28be2bSAndi Kleen sdio->submit_io(dio->rw, bio, dio->inode, 402eb28be2bSAndi Kleen sdio->logical_offset_in_bio); 403facd07b0SJosef Bacik else 4041da177e4SLinus Torvalds submit_bio(dio->rw, bio); 4051da177e4SLinus Torvalds 406eb28be2bSAndi Kleen sdio->bio = NULL; 407eb28be2bSAndi Kleen sdio->boundary = 0; 408eb28be2bSAndi Kleen sdio->logical_offset_in_bio = 0; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* 4121da177e4SLinus Torvalds * Release any resources in case of a failure 4131da177e4SLinus Torvalds */ 414ba253fbfSAndi Kleen static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio) 4151da177e4SLinus Torvalds { 4167b2c99d1SAl Viro while (sdio->head < sdio->tail) 4177b2c99d1SAl Viro page_cache_release(dio->pages[sdio->head++]); 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds /* 4210273201eSZach Brown * Wait for the next BIO to complete. Remove it and return it. NULL is 4220273201eSZach Brown * returned once all BIOs have been completed. This must only be called once 4230273201eSZach Brown * all bios have been issued so that dio->refcount can only decrease. This 4240273201eSZach Brown * requires that that the caller hold a reference on the dio. 4251da177e4SLinus Torvalds */ 4261da177e4SLinus Torvalds static struct bio *dio_await_one(struct dio *dio) 4271da177e4SLinus Torvalds { 4281da177e4SLinus Torvalds unsigned long flags; 4290273201eSZach Brown struct bio *bio = NULL; 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 4325eb6c7a2SZach Brown 4335eb6c7a2SZach Brown /* 4345eb6c7a2SZach Brown * Wait as long as the list is empty and there are bios in flight. bio 4355eb6c7a2SZach Brown * completion drops the count, maybe adds to the list, and wakes while 4365eb6c7a2SZach Brown * holding the bio_lock so we don't need set_current_state()'s barrier 4375eb6c7a2SZach Brown * and can call it after testing our condition. 4385eb6c7a2SZach Brown */ 4395eb6c7a2SZach Brown while (dio->refcount > 1 && dio->bio_list == NULL) { 4405eb6c7a2SZach Brown __set_current_state(TASK_UNINTERRUPTIBLE); 4411da177e4SLinus Torvalds dio->waiter = current; 4421da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 4431da177e4SLinus Torvalds io_schedule(); 4445eb6c7a2SZach Brown /* wake up sets us TASK_RUNNING */ 4451da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 4461da177e4SLinus Torvalds dio->waiter = NULL; 4471da177e4SLinus Torvalds } 4480273201eSZach Brown if (dio->bio_list) { 4491da177e4SLinus Torvalds bio = dio->bio_list; 4501da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 4510273201eSZach Brown } 4521da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 4531da177e4SLinus Torvalds return bio; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* 4571da177e4SLinus Torvalds * Process one completed BIO. No locks are held. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds static int dio_bio_complete(struct dio *dio, struct bio *bio) 4601da177e4SLinus Torvalds { 461cb34e057SKent Overstreet struct bio_vec *bvec; 462cb34e057SKent Overstreet unsigned i; 4639b81c842SSasha Levin int err; 4641da177e4SLinus Torvalds 4654246a0b6SChristoph Hellwig if (bio->bi_error) 466174e27c6SChen, Kenneth W dio->io_error = -EIO; 4671da177e4SLinus Torvalds 468*53cbf3b1SMing Lei if (dio->is_async && dio->rw == READ && dio->should_dirty) { 4691da177e4SLinus Torvalds bio_check_pages_dirty(bio); /* transfers ownership */ 4709b81c842SSasha Levin err = bio->bi_error; 4711da177e4SLinus Torvalds } else { 472cb34e057SKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 473cb34e057SKent Overstreet struct page *page = bvec->bv_page; 4741da177e4SLinus Torvalds 475*53cbf3b1SMing Lei if (dio->rw == READ && !PageCompound(page) && 476*53cbf3b1SMing Lei dio->should_dirty) 4771da177e4SLinus Torvalds set_page_dirty_lock(page); 4781da177e4SLinus Torvalds page_cache_release(page); 4791da177e4SLinus Torvalds } 4809b81c842SSasha Levin err = bio->bi_error; 4811da177e4SLinus Torvalds bio_put(bio); 4821da177e4SLinus Torvalds } 4839b81c842SSasha Levin return err; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds /* 4870273201eSZach Brown * Wait on and process all in-flight BIOs. This must only be called once 4880273201eSZach Brown * all bios have been issued so that the refcount can only decrease. 4890273201eSZach Brown * This just waits for all bios to make it through dio_bio_complete. IO 490beb7dd86SRobert P. J. Day * errors are propagated through dio->io_error and should be propagated via 4910273201eSZach Brown * dio_complete(). 4921da177e4SLinus Torvalds */ 4936d544bb4SZach Brown static void dio_await_completion(struct dio *dio) 4941da177e4SLinus Torvalds { 4950273201eSZach Brown struct bio *bio; 4960273201eSZach Brown do { 4970273201eSZach Brown bio = dio_await_one(dio); 4980273201eSZach Brown if (bio) 4996d544bb4SZach Brown dio_bio_complete(dio, bio); 5000273201eSZach Brown } while (bio); 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds /* 5041da177e4SLinus Torvalds * A really large O_DIRECT read or write can generate a lot of BIOs. So 5051da177e4SLinus Torvalds * to keep the memory consumption sane we periodically reap any completed BIOs 5061da177e4SLinus Torvalds * during the BIO generation phase. 5071da177e4SLinus Torvalds * 5081da177e4SLinus Torvalds * This also helps to limit the peak amount of pinned userspace memory. 5091da177e4SLinus Torvalds */ 510ba253fbfSAndi Kleen static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio) 5111da177e4SLinus Torvalds { 5121da177e4SLinus Torvalds int ret = 0; 5131da177e4SLinus Torvalds 514eb28be2bSAndi Kleen if (sdio->reap_counter++ >= 64) { 5151da177e4SLinus Torvalds while (dio->bio_list) { 5161da177e4SLinus Torvalds unsigned long flags; 5171da177e4SLinus Torvalds struct bio *bio; 5181da177e4SLinus Torvalds int ret2; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 5211da177e4SLinus Torvalds bio = dio->bio_list; 5221da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 5231da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 5241da177e4SLinus Torvalds ret2 = dio_bio_complete(dio, bio); 5251da177e4SLinus Torvalds if (ret == 0) 5261da177e4SLinus Torvalds ret = ret2; 5271da177e4SLinus Torvalds } 528eb28be2bSAndi Kleen sdio->reap_counter = 0; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds return ret; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /* 5347b7a8665SChristoph Hellwig * Create workqueue for deferred direct IO completions. We allocate the 5357b7a8665SChristoph Hellwig * workqueue when it's first needed. This avoids creating workqueue for 5367b7a8665SChristoph Hellwig * filesystems that don't need it and also allows us to create the workqueue 5377b7a8665SChristoph Hellwig * late enough so the we can include s_id in the name of the workqueue. 5387b7a8665SChristoph Hellwig */ 5397b7a8665SChristoph Hellwig static int sb_init_dio_done_wq(struct super_block *sb) 5407b7a8665SChristoph Hellwig { 54145150c43SOlof Johansson struct workqueue_struct *old; 5427b7a8665SChristoph Hellwig struct workqueue_struct *wq = alloc_workqueue("dio/%s", 5437b7a8665SChristoph Hellwig WQ_MEM_RECLAIM, 0, 5447b7a8665SChristoph Hellwig sb->s_id); 5457b7a8665SChristoph Hellwig if (!wq) 5467b7a8665SChristoph Hellwig return -ENOMEM; 5477b7a8665SChristoph Hellwig /* 5487b7a8665SChristoph Hellwig * This has to be atomic as more DIOs can race to create the workqueue 5497b7a8665SChristoph Hellwig */ 55045150c43SOlof Johansson old = cmpxchg(&sb->s_dio_done_wq, NULL, wq); 5517b7a8665SChristoph Hellwig /* Someone created workqueue before us? Free ours... */ 55245150c43SOlof Johansson if (old) 5537b7a8665SChristoph Hellwig destroy_workqueue(wq); 5547b7a8665SChristoph Hellwig return 0; 5557b7a8665SChristoph Hellwig } 5567b7a8665SChristoph Hellwig 5577b7a8665SChristoph Hellwig static int dio_set_defer_completion(struct dio *dio) 5587b7a8665SChristoph Hellwig { 5597b7a8665SChristoph Hellwig struct super_block *sb = dio->inode->i_sb; 5607b7a8665SChristoph Hellwig 5617b7a8665SChristoph Hellwig if (dio->defer_completion) 5627b7a8665SChristoph Hellwig return 0; 5637b7a8665SChristoph Hellwig dio->defer_completion = true; 5647b7a8665SChristoph Hellwig if (!sb->s_dio_done_wq) 5657b7a8665SChristoph Hellwig return sb_init_dio_done_wq(sb); 5667b7a8665SChristoph Hellwig return 0; 5677b7a8665SChristoph Hellwig } 5687b7a8665SChristoph Hellwig 5697b7a8665SChristoph Hellwig /* 5701da177e4SLinus Torvalds * Call into the fs to map some more disk blocks. We record the current number 571eb28be2bSAndi Kleen * of available blocks at sdio->blocks_available. These are in units of the 5721da177e4SLinus Torvalds * fs blocksize, (1 << inode->i_blkbits). 5731da177e4SLinus Torvalds * 5741da177e4SLinus Torvalds * The fs is allowed to map lots of blocks at once. If it wants to do that, 5751da177e4SLinus Torvalds * it uses the passed inode-relative block number as the file offset, as usual. 5761da177e4SLinus Torvalds * 5771d8fa7a2SBadari Pulavarty * get_block() is passed the number of i_blkbits-sized blocks which direct_io 5781da177e4SLinus Torvalds * has remaining to do. The fs should not map more than this number of blocks. 5791da177e4SLinus Torvalds * 5801da177e4SLinus Torvalds * If the fs has mapped a lot of blocks, it should populate bh->b_size to 5811da177e4SLinus Torvalds * indicate how much contiguous disk space has been made available at 5821da177e4SLinus Torvalds * bh->b_blocknr. 5831da177e4SLinus Torvalds * 5841da177e4SLinus Torvalds * If *any* of the mapped blocks are new, then the fs must set buffer_new(). 5851da177e4SLinus Torvalds * This isn't very efficient... 5861da177e4SLinus Torvalds * 5871da177e4SLinus Torvalds * In the case of filesystem holes: the fs may return an arbitrarily-large 5881da177e4SLinus Torvalds * hole by returning an appropriate value in b_size and by clearing 5891da177e4SLinus Torvalds * buffer_mapped(). However the direct-io code will only process holes one 5901d8fa7a2SBadari Pulavarty * block at a time - it will repeatedly call get_block() as it walks the hole. 5911da177e4SLinus Torvalds */ 59218772641SAndi Kleen static int get_more_blocks(struct dio *dio, struct dio_submit *sdio, 59318772641SAndi Kleen struct buffer_head *map_bh) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds int ret; 5961da177e4SLinus Torvalds sector_t fs_startblk; /* Into file, in filesystem-sized blocks */ 597ae55e1aaSTao Ma sector_t fs_endblk; /* Into file, in filesystem-sized blocks */ 5981da177e4SLinus Torvalds unsigned long fs_count; /* Number of filesystem-sized blocks */ 5991da177e4SLinus Torvalds int create; 600ab73857eSLinus Torvalds unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds /* 6031da177e4SLinus Torvalds * If there was a memory error and we've overwritten all the 6041da177e4SLinus Torvalds * mapped blocks then we can now return that memory error 6051da177e4SLinus Torvalds */ 6061da177e4SLinus Torvalds ret = dio->page_errors; 6071da177e4SLinus Torvalds if (ret == 0) { 608eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file >= sdio->final_block_in_request); 609eb28be2bSAndi Kleen fs_startblk = sdio->block_in_file >> sdio->blkfactor; 610ae55e1aaSTao Ma fs_endblk = (sdio->final_block_in_request - 1) >> 611ae55e1aaSTao Ma sdio->blkfactor; 612ae55e1aaSTao Ma fs_count = fs_endblk - fs_startblk + 1; 6131da177e4SLinus Torvalds 6143c674e74SNathan Scott map_bh->b_state = 0; 615ab73857eSLinus Torvalds map_bh->b_size = fs_count << i_blkbits; 6163c674e74SNathan Scott 6175fe878aeSChristoph Hellwig /* 6185fe878aeSChristoph Hellwig * For writes inside i_size on a DIO_SKIP_HOLES filesystem we 6195fe878aeSChristoph Hellwig * forbid block creations: only overwrites are permitted. 6205fe878aeSChristoph Hellwig * We will return early to the caller once we see an 6215fe878aeSChristoph Hellwig * unmapped buffer head returned, and the caller will fall 6225fe878aeSChristoph Hellwig * back to buffered I/O. 6235fe878aeSChristoph Hellwig * 6245fe878aeSChristoph Hellwig * Otherwise the decision is left to the get_blocks method, 6255fe878aeSChristoph Hellwig * which may decide to handle it or also return an unmapped 6265fe878aeSChristoph Hellwig * buffer head. 6275fe878aeSChristoph Hellwig */ 628b31dc66aSJens Axboe create = dio->rw & WRITE; 6295fe878aeSChristoph Hellwig if (dio->flags & DIO_SKIP_HOLES) { 630eb28be2bSAndi Kleen if (sdio->block_in_file < (i_size_read(dio->inode) >> 631eb28be2bSAndi Kleen sdio->blkbits)) 6321da177e4SLinus Torvalds create = 0; 6331da177e4SLinus Torvalds } 6343c674e74SNathan Scott 635eb28be2bSAndi Kleen ret = (*sdio->get_block)(dio->inode, fs_startblk, 6361da177e4SLinus Torvalds map_bh, create); 63718772641SAndi Kleen 63818772641SAndi Kleen /* Store for completion */ 63918772641SAndi Kleen dio->private = map_bh->b_private; 6407b7a8665SChristoph Hellwig 6417b7a8665SChristoph Hellwig if (ret == 0 && buffer_defer_completion(map_bh)) 6427b7a8665SChristoph Hellwig ret = dio_set_defer_completion(dio); 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds return ret; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* 6481da177e4SLinus Torvalds * There is no bio. Make one now. 6491da177e4SLinus Torvalds */ 650ba253fbfSAndi Kleen static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio, 65118772641SAndi Kleen sector_t start_sector, struct buffer_head *map_bh) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds sector_t sector; 6541da177e4SLinus Torvalds int ret, nr_pages; 6551da177e4SLinus Torvalds 656eb28be2bSAndi Kleen ret = dio_bio_reap(dio, sdio); 6571da177e4SLinus Torvalds if (ret) 6581da177e4SLinus Torvalds goto out; 659eb28be2bSAndi Kleen sector = start_sector << (sdio->blkbits - 9); 660b54ffb73SKent Overstreet nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES); 6611da177e4SLinus Torvalds BUG_ON(nr_pages <= 0); 66218772641SAndi Kleen dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages); 663eb28be2bSAndi Kleen sdio->boundary = 0; 6641da177e4SLinus Torvalds out: 6651da177e4SLinus Torvalds return ret; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* 6691da177e4SLinus Torvalds * Attempt to put the current chunk of 'cur_page' into the current BIO. If 6701da177e4SLinus Torvalds * that was successful then update final_block_in_bio and take a ref against 6711da177e4SLinus Torvalds * the just-added page. 6721da177e4SLinus Torvalds * 6731da177e4SLinus Torvalds * Return zero on success. Non-zero means the caller needs to start a new BIO. 6741da177e4SLinus Torvalds */ 675ba253fbfSAndi Kleen static inline int dio_bio_add_page(struct dio_submit *sdio) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds int ret; 6781da177e4SLinus Torvalds 679eb28be2bSAndi Kleen ret = bio_add_page(sdio->bio, sdio->cur_page, 680eb28be2bSAndi Kleen sdio->cur_page_len, sdio->cur_page_offset); 681eb28be2bSAndi Kleen if (ret == sdio->cur_page_len) { 6821da177e4SLinus Torvalds /* 6831da177e4SLinus Torvalds * Decrement count only, if we are done with this page 6841da177e4SLinus Torvalds */ 685eb28be2bSAndi Kleen if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE) 686eb28be2bSAndi Kleen sdio->pages_in_io--; 687eb28be2bSAndi Kleen page_cache_get(sdio->cur_page); 688eb28be2bSAndi Kleen sdio->final_block_in_bio = sdio->cur_page_block + 689eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits); 6901da177e4SLinus Torvalds ret = 0; 6911da177e4SLinus Torvalds } else { 6921da177e4SLinus Torvalds ret = 1; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds return ret; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* 6981da177e4SLinus Torvalds * Put cur_page under IO. The section of cur_page which is described by 6991da177e4SLinus Torvalds * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page 7001da177e4SLinus Torvalds * starts on-disk at cur_page_block. 7011da177e4SLinus Torvalds * 7021da177e4SLinus Torvalds * We take a ref against the page here (on behalf of its presence in the bio). 7031da177e4SLinus Torvalds * 7041da177e4SLinus Torvalds * The caller of this function is responsible for removing cur_page from the 7051da177e4SLinus Torvalds * dio, and for dropping the refcount which came from that presence. 7061da177e4SLinus Torvalds */ 707ba253fbfSAndi Kleen static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio, 70818772641SAndi Kleen struct buffer_head *map_bh) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds int ret = 0; 7111da177e4SLinus Torvalds 712eb28be2bSAndi Kleen if (sdio->bio) { 713eb28be2bSAndi Kleen loff_t cur_offset = sdio->cur_page_fs_offset; 714eb28be2bSAndi Kleen loff_t bio_next_offset = sdio->logical_offset_in_bio + 7154f024f37SKent Overstreet sdio->bio->bi_iter.bi_size; 716c2c6ca41SJosef Bacik 7171da177e4SLinus Torvalds /* 718c2c6ca41SJosef Bacik * See whether this new request is contiguous with the old. 719c2c6ca41SJosef Bacik * 720f0940ceeSNamhyung Kim * Btrfs cannot handle having logically non-contiguous requests 721f0940ceeSNamhyung Kim * submitted. For example if you have 722c2c6ca41SJosef Bacik * 723c2c6ca41SJosef Bacik * Logical: [0-4095][HOLE][8192-12287] 724f0940ceeSNamhyung Kim * Physical: [0-4095] [4096-8191] 725c2c6ca41SJosef Bacik * 726c2c6ca41SJosef Bacik * We cannot submit those pages together as one BIO. So if our 727c2c6ca41SJosef Bacik * current logical offset in the file does not equal what would 728c2c6ca41SJosef Bacik * be the next logical offset in the bio, submit the bio we 729c2c6ca41SJosef Bacik * have. 7301da177e4SLinus Torvalds */ 731eb28be2bSAndi Kleen if (sdio->final_block_in_bio != sdio->cur_page_block || 732c2c6ca41SJosef Bacik cur_offset != bio_next_offset) 733eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 736eb28be2bSAndi Kleen if (sdio->bio == NULL) { 73718772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 7381da177e4SLinus Torvalds if (ret) 7391da177e4SLinus Torvalds goto out; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 742eb28be2bSAndi Kleen if (dio_bio_add_page(sdio) != 0) { 743eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 74418772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 7451da177e4SLinus Torvalds if (ret == 0) { 746eb28be2bSAndi Kleen ret = dio_bio_add_page(sdio); 7471da177e4SLinus Torvalds BUG_ON(ret != 0); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds out: 7511da177e4SLinus Torvalds return ret; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* 7551da177e4SLinus Torvalds * An autonomous function to put a chunk of a page under deferred IO. 7561da177e4SLinus Torvalds * 7571da177e4SLinus Torvalds * The caller doesn't actually know (or care) whether this piece of page is in 7581da177e4SLinus Torvalds * a BIO, or is under IO or whatever. We just take care of all possible 7591da177e4SLinus Torvalds * situations here. The separation between the logic of do_direct_IO() and 7601da177e4SLinus Torvalds * that of submit_page_section() is important for clarity. Please don't break. 7611da177e4SLinus Torvalds * 7621da177e4SLinus Torvalds * The chunk of page starts on-disk at blocknr. 7631da177e4SLinus Torvalds * 7641da177e4SLinus Torvalds * We perform deferred IO, by recording the last-submitted page inside our 7651da177e4SLinus Torvalds * private part of the dio structure. If possible, we just expand the IO 7661da177e4SLinus Torvalds * across that page here. 7671da177e4SLinus Torvalds * 7681da177e4SLinus Torvalds * If that doesn't work out then we put the old page into the bio and add this 7691da177e4SLinus Torvalds * page to the dio instead. 7701da177e4SLinus Torvalds */ 771ba253fbfSAndi Kleen static inline int 772eb28be2bSAndi Kleen submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page, 77318772641SAndi Kleen unsigned offset, unsigned len, sector_t blocknr, 77418772641SAndi Kleen struct buffer_head *map_bh) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds int ret = 0; 7771da177e4SLinus Torvalds 77898c4d57dSAndrew Morton if (dio->rw & WRITE) { 77998c4d57dSAndrew Morton /* 78098c4d57dSAndrew Morton * Read accounting is performed in submit_bio() 78198c4d57dSAndrew Morton */ 78298c4d57dSAndrew Morton task_io_account_write(len); 78398c4d57dSAndrew Morton } 78498c4d57dSAndrew Morton 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * Can we just grow the current page's presence in the dio? 7871da177e4SLinus Torvalds */ 788eb28be2bSAndi Kleen if (sdio->cur_page == page && 789eb28be2bSAndi Kleen sdio->cur_page_offset + sdio->cur_page_len == offset && 790eb28be2bSAndi Kleen sdio->cur_page_block + 791eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits) == blocknr) { 792eb28be2bSAndi Kleen sdio->cur_page_len += len; 7931da177e4SLinus Torvalds goto out; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * If there's a deferred page already there then send it. 7981da177e4SLinus Torvalds */ 799eb28be2bSAndi Kleen if (sdio->cur_page) { 80018772641SAndi Kleen ret = dio_send_cur_page(dio, sdio, map_bh); 801eb28be2bSAndi Kleen page_cache_release(sdio->cur_page); 802eb28be2bSAndi Kleen sdio->cur_page = NULL; 8031da177e4SLinus Torvalds if (ret) 804b1058b98SJan Kara return ret; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds page_cache_get(page); /* It is in dio */ 808eb28be2bSAndi Kleen sdio->cur_page = page; 809eb28be2bSAndi Kleen sdio->cur_page_offset = offset; 810eb28be2bSAndi Kleen sdio->cur_page_len = len; 811eb28be2bSAndi Kleen sdio->cur_page_block = blocknr; 812eb28be2bSAndi Kleen sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits; 8131da177e4SLinus Torvalds out: 814b1058b98SJan Kara /* 815b1058b98SJan Kara * If sdio->boundary then we want to schedule the IO now to 816b1058b98SJan Kara * avoid metadata seeks. 817b1058b98SJan Kara */ 818b1058b98SJan Kara if (sdio->boundary) { 819b1058b98SJan Kara ret = dio_send_cur_page(dio, sdio, map_bh); 820b1058b98SJan Kara dio_bio_submit(dio, sdio); 821b1058b98SJan Kara page_cache_release(sdio->cur_page); 822b1058b98SJan Kara sdio->cur_page = NULL; 823b1058b98SJan Kara } 8241da177e4SLinus Torvalds return ret; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * Clean any dirty buffers in the blockdev mapping which alias newly-created 8291da177e4SLinus Torvalds * file blocks. Only called for S_ISREG files - blockdevs do not set 8301da177e4SLinus Torvalds * buffer_new 8311da177e4SLinus Torvalds */ 83218772641SAndi Kleen static void clean_blockdev_aliases(struct dio *dio, struct buffer_head *map_bh) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds unsigned i; 8351da177e4SLinus Torvalds unsigned nblocks; 8361da177e4SLinus Torvalds 83718772641SAndi Kleen nblocks = map_bh->b_size >> dio->inode->i_blkbits; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds for (i = 0; i < nblocks; i++) { 84018772641SAndi Kleen unmap_underlying_metadata(map_bh->b_bdev, 84118772641SAndi Kleen map_bh->b_blocknr + i); 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds /* 8461da177e4SLinus Torvalds * If we are not writing the entire block and get_block() allocated 8471da177e4SLinus Torvalds * the block for us, we need to fill-in the unused portion of the 8481da177e4SLinus Torvalds * block with zeros. This happens only if user-buffer, fileoffset or 8491da177e4SLinus Torvalds * io length is not filesystem block-size multiple. 8501da177e4SLinus Torvalds * 8511da177e4SLinus Torvalds * `end' is zero if we're doing the start of the IO, 1 at the end of the 8521da177e4SLinus Torvalds * IO. 8531da177e4SLinus Torvalds */ 854ba253fbfSAndi Kleen static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio, 855ba253fbfSAndi Kleen int end, struct buffer_head *map_bh) 8561da177e4SLinus Torvalds { 8571da177e4SLinus Torvalds unsigned dio_blocks_per_fs_block; 8581da177e4SLinus Torvalds unsigned this_chunk_blocks; /* In dio_blocks */ 8591da177e4SLinus Torvalds unsigned this_chunk_bytes; 8601da177e4SLinus Torvalds struct page *page; 8611da177e4SLinus Torvalds 862eb28be2bSAndi Kleen sdio->start_zero_done = 1; 86318772641SAndi Kleen if (!sdio->blkfactor || !buffer_new(map_bh)) 8641da177e4SLinus Torvalds return; 8651da177e4SLinus Torvalds 866eb28be2bSAndi Kleen dio_blocks_per_fs_block = 1 << sdio->blkfactor; 867eb28be2bSAndi Kleen this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds if (!this_chunk_blocks) 8701da177e4SLinus Torvalds return; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds /* 8731da177e4SLinus Torvalds * We need to zero out part of an fs block. It is either at the 8741da177e4SLinus Torvalds * beginning or the end of the fs block. 8751da177e4SLinus Torvalds */ 8761da177e4SLinus Torvalds if (end) 8771da177e4SLinus Torvalds this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks; 8781da177e4SLinus Torvalds 879eb28be2bSAndi Kleen this_chunk_bytes = this_chunk_blocks << sdio->blkbits; 8801da177e4SLinus Torvalds 881557ed1faSNick Piggin page = ZERO_PAGE(0); 882eb28be2bSAndi Kleen if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes, 88318772641SAndi Kleen sdio->next_block_for_io, map_bh)) 8841da177e4SLinus Torvalds return; 8851da177e4SLinus Torvalds 886eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds /* 8901da177e4SLinus Torvalds * Walk the user pages, and the file, mapping blocks to disk and generating 8911da177e4SLinus Torvalds * a sequence of (page,offset,len,block) mappings. These mappings are injected 8921da177e4SLinus Torvalds * into submit_page_section(), which takes care of the next stage of submission 8931da177e4SLinus Torvalds * 8941da177e4SLinus Torvalds * Direct IO against a blockdev is different from a file. Because we can 8951da177e4SLinus Torvalds * happily perform page-sized but 512-byte aligned IOs. It is important that 8961da177e4SLinus Torvalds * blockdev IO be able to have fine alignment and large sizes. 8971da177e4SLinus Torvalds * 8981d8fa7a2SBadari Pulavarty * So what we do is to permit the ->get_block function to populate bh.b_size 8991da177e4SLinus Torvalds * with the size of IO which is permitted at this offset and this i_blkbits. 9001da177e4SLinus Torvalds * 9011da177e4SLinus Torvalds * For best results, the blockdev should be set up with 512-byte i_blkbits and 9021d8fa7a2SBadari Pulavarty * it should set b_size to PAGE_SIZE or more inside get_block(). This gives 9031da177e4SLinus Torvalds * fine alignment but still allows this function to work in PAGE_SIZE units. 9041da177e4SLinus Torvalds */ 90518772641SAndi Kleen static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, 90618772641SAndi Kleen struct buffer_head *map_bh) 9071da177e4SLinus Torvalds { 908eb28be2bSAndi Kleen const unsigned blkbits = sdio->blkbits; 9091da177e4SLinus Torvalds int ret = 0; 9101da177e4SLinus Torvalds 911eb28be2bSAndi Kleen while (sdio->block_in_file < sdio->final_block_in_request) { 9127b2c99d1SAl Viro struct page *page; 9137b2c99d1SAl Viro size_t from, to; 9146fcc5420SBoaz Harrosh 9156fcc5420SBoaz Harrosh page = dio_get_page(dio, sdio); 9161da177e4SLinus Torvalds if (IS_ERR(page)) { 9171da177e4SLinus Torvalds ret = PTR_ERR(page); 9181da177e4SLinus Torvalds goto out; 9191da177e4SLinus Torvalds } 9206fcc5420SBoaz Harrosh from = sdio->head ? 0 : sdio->from; 9216fcc5420SBoaz Harrosh to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE; 9226fcc5420SBoaz Harrosh sdio->head++; 9231da177e4SLinus Torvalds 9247b2c99d1SAl Viro while (from < to) { 9251da177e4SLinus Torvalds unsigned this_chunk_bytes; /* # of bytes mapped */ 9261da177e4SLinus Torvalds unsigned this_chunk_blocks; /* # of blocks */ 9271da177e4SLinus Torvalds unsigned u; 9281da177e4SLinus Torvalds 929eb28be2bSAndi Kleen if (sdio->blocks_available == 0) { 9301da177e4SLinus Torvalds /* 9311da177e4SLinus Torvalds * Need to go and map some more disk 9321da177e4SLinus Torvalds */ 9331da177e4SLinus Torvalds unsigned long blkmask; 9341da177e4SLinus Torvalds unsigned long dio_remainder; 9351da177e4SLinus Torvalds 93618772641SAndi Kleen ret = get_more_blocks(dio, sdio, map_bh); 9371da177e4SLinus Torvalds if (ret) { 9381da177e4SLinus Torvalds page_cache_release(page); 9391da177e4SLinus Torvalds goto out; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) 9421da177e4SLinus Torvalds goto do_holes; 9431da177e4SLinus Torvalds 944eb28be2bSAndi Kleen sdio->blocks_available = 945eb28be2bSAndi Kleen map_bh->b_size >> sdio->blkbits; 946eb28be2bSAndi Kleen sdio->next_block_for_io = 947eb28be2bSAndi Kleen map_bh->b_blocknr << sdio->blkfactor; 9481da177e4SLinus Torvalds if (buffer_new(map_bh)) 94918772641SAndi Kleen clean_blockdev_aliases(dio, map_bh); 9501da177e4SLinus Torvalds 951eb28be2bSAndi Kleen if (!sdio->blkfactor) 9521da177e4SLinus Torvalds goto do_holes; 9531da177e4SLinus Torvalds 954eb28be2bSAndi Kleen blkmask = (1 << sdio->blkfactor) - 1; 955eb28be2bSAndi Kleen dio_remainder = (sdio->block_in_file & blkmask); 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds /* 9581da177e4SLinus Torvalds * If we are at the start of IO and that IO 9591da177e4SLinus Torvalds * starts partway into a fs-block, 9601da177e4SLinus Torvalds * dio_remainder will be non-zero. If the IO 9611da177e4SLinus Torvalds * is a read then we can simply advance the IO 9621da177e4SLinus Torvalds * cursor to the first block which is to be 9631da177e4SLinus Torvalds * read. But if the IO is a write and the 9641da177e4SLinus Torvalds * block was newly allocated we cannot do that; 9651da177e4SLinus Torvalds * the start of the fs block must be zeroed out 9661da177e4SLinus Torvalds * on-disk 9671da177e4SLinus Torvalds */ 9681da177e4SLinus Torvalds if (!buffer_new(map_bh)) 969eb28be2bSAndi Kleen sdio->next_block_for_io += dio_remainder; 970eb28be2bSAndi Kleen sdio->blocks_available -= dio_remainder; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds do_holes: 9731da177e4SLinus Torvalds /* Handle holes */ 9741da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) { 97535dc8161SJeff Moyer loff_t i_size_aligned; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds /* AKPM: eargh, -ENOTBLK is a hack */ 978b31dc66aSJens Axboe if (dio->rw & WRITE) { 9791da177e4SLinus Torvalds page_cache_release(page); 9801da177e4SLinus Torvalds return -ENOTBLK; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 98335dc8161SJeff Moyer /* 98435dc8161SJeff Moyer * Be sure to account for a partial block as the 98535dc8161SJeff Moyer * last block in the file 98635dc8161SJeff Moyer */ 98735dc8161SJeff Moyer i_size_aligned = ALIGN(i_size_read(dio->inode), 98835dc8161SJeff Moyer 1 << blkbits); 989eb28be2bSAndi Kleen if (sdio->block_in_file >= 99035dc8161SJeff Moyer i_size_aligned >> blkbits) { 9911da177e4SLinus Torvalds /* We hit eof */ 9921da177e4SLinus Torvalds page_cache_release(page); 9931da177e4SLinus Torvalds goto out; 9941da177e4SLinus Torvalds } 9957b2c99d1SAl Viro zero_user(page, from, 1 << blkbits); 996eb28be2bSAndi Kleen sdio->block_in_file++; 9977b2c99d1SAl Viro from += 1 << blkbits; 9983320c60bSAl Viro dio->result += 1 << blkbits; 9991da177e4SLinus Torvalds goto next_block; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds /* 10031da177e4SLinus Torvalds * If we're performing IO which has an alignment which 10041da177e4SLinus Torvalds * is finer than the underlying fs, go check to see if 10051da177e4SLinus Torvalds * we must zero out the start of this block. 10061da177e4SLinus Torvalds */ 1007eb28be2bSAndi Kleen if (unlikely(sdio->blkfactor && !sdio->start_zero_done)) 100818772641SAndi Kleen dio_zero_block(dio, sdio, 0, map_bh); 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds /* 10111da177e4SLinus Torvalds * Work out, in this_chunk_blocks, how much disk we 10121da177e4SLinus Torvalds * can add to this page 10131da177e4SLinus Torvalds */ 1014eb28be2bSAndi Kleen this_chunk_blocks = sdio->blocks_available; 10157b2c99d1SAl Viro u = (to - from) >> blkbits; 10161da177e4SLinus Torvalds if (this_chunk_blocks > u) 10171da177e4SLinus Torvalds this_chunk_blocks = u; 1018eb28be2bSAndi Kleen u = sdio->final_block_in_request - sdio->block_in_file; 10191da177e4SLinus Torvalds if (this_chunk_blocks > u) 10201da177e4SLinus Torvalds this_chunk_blocks = u; 10211da177e4SLinus Torvalds this_chunk_bytes = this_chunk_blocks << blkbits; 10221da177e4SLinus Torvalds BUG_ON(this_chunk_bytes == 0); 10231da177e4SLinus Torvalds 1024092c8d46SJan Kara if (this_chunk_blocks == sdio->blocks_available) 1025eb28be2bSAndi Kleen sdio->boundary = buffer_boundary(map_bh); 1026eb28be2bSAndi Kleen ret = submit_page_section(dio, sdio, page, 10277b2c99d1SAl Viro from, 1028eb28be2bSAndi Kleen this_chunk_bytes, 102918772641SAndi Kleen sdio->next_block_for_io, 103018772641SAndi Kleen map_bh); 10311da177e4SLinus Torvalds if (ret) { 10321da177e4SLinus Torvalds page_cache_release(page); 10331da177e4SLinus Torvalds goto out; 10341da177e4SLinus Torvalds } 1035eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 10361da177e4SLinus Torvalds 1037eb28be2bSAndi Kleen sdio->block_in_file += this_chunk_blocks; 10387b2c99d1SAl Viro from += this_chunk_bytes; 10397b2c99d1SAl Viro dio->result += this_chunk_bytes; 1040eb28be2bSAndi Kleen sdio->blocks_available -= this_chunk_blocks; 10411da177e4SLinus Torvalds next_block: 1042eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file > sdio->final_block_in_request); 1043eb28be2bSAndi Kleen if (sdio->block_in_file == sdio->final_block_in_request) 10441da177e4SLinus Torvalds break; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* Drop the ref which was taken in get_user_pages() */ 10481da177e4SLinus Torvalds page_cache_release(page); 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds out: 10511da177e4SLinus Torvalds return ret; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 1054847cc637SAndi Kleen static inline int drop_refcount(struct dio *dio) 10551da177e4SLinus Torvalds { 1056847cc637SAndi Kleen int ret2; 10575eb6c7a2SZach Brown unsigned long flags; 105820258b2bSZach Brown 10591da177e4SLinus Torvalds /* 10608459d86aSZach Brown * Sync will always be dropping the final ref and completing the 10615eb6c7a2SZach Brown * operation. AIO can if it was a broken operation described above or 10625eb6c7a2SZach Brown * in fact if all the bios race to complete before we get here. In 10635eb6c7a2SZach Brown * that case dio_complete() translates the EIOCBQUEUED into the proper 106404b2fa9fSChristoph Hellwig * return code that the caller will hand to ->complete(). 10655eb6c7a2SZach Brown * 10665eb6c7a2SZach Brown * This is managed by the bio_lock instead of being an atomic_t so that 10675eb6c7a2SZach Brown * completion paths can drop their ref and use the remaining count to 10685eb6c7a2SZach Brown * decide to wake the submission path atomically. 10691da177e4SLinus Torvalds */ 10705eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 10715eb6c7a2SZach Brown ret2 = --dio->refcount; 10725eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 1073847cc637SAndi Kleen return ret2; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 1076eafdc7d1SChristoph Hellwig /* 1077eafdc7d1SChristoph Hellwig * This is a library function for use by filesystem drivers. 1078eafdc7d1SChristoph Hellwig * 1079eafdc7d1SChristoph Hellwig * The locking rules are governed by the flags parameter: 1080eafdc7d1SChristoph Hellwig * - if the flags value contains DIO_LOCKING we use a fancy locking 1081eafdc7d1SChristoph Hellwig * scheme for dumb filesystems. 1082eafdc7d1SChristoph Hellwig * For writes this function is called under i_mutex and returns with 1083eafdc7d1SChristoph Hellwig * i_mutex held, for reads, i_mutex is not held on entry, but it is 1084eafdc7d1SChristoph Hellwig * taken and dropped again before returning. 1085eafdc7d1SChristoph Hellwig * - if the flags value does NOT contain DIO_LOCKING we don't use any 1086eafdc7d1SChristoph Hellwig * internal locking but rather rely on the filesystem to synchronize 1087eafdc7d1SChristoph Hellwig * direct I/O reads/writes versus each other and truncate. 1088df2d6f26SChristoph Hellwig * 1089df2d6f26SChristoph Hellwig * To help with locking against truncate we incremented the i_dio_count 1090df2d6f26SChristoph Hellwig * counter before starting direct I/O, and decrement it once we are done. 1091df2d6f26SChristoph Hellwig * Truncate can wait for it to reach zero to provide exclusion. It is 1092df2d6f26SChristoph Hellwig * expected that filesystem provide exclusion between new direct I/O 1093df2d6f26SChristoph Hellwig * and truncates. For DIO_LOCKING filesystems this is done by i_mutex, 1094df2d6f26SChristoph Hellwig * but other filesystems need to take care of this on their own. 1095ba253fbfSAndi Kleen * 1096ba253fbfSAndi Kleen * NOTE: if you pass "sdio" to anything by pointer make sure that function 1097ba253fbfSAndi Kleen * is always inlined. Otherwise gcc is unable to split the structure into 1098ba253fbfSAndi Kleen * individual fields and will generate much worse code. This is important 1099ba253fbfSAndi Kleen * for the whole file. 1100eafdc7d1SChristoph Hellwig */ 110165dd2aa9SAndi Kleen static inline ssize_t 110217f8c842SOmar Sandoval do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, 110317f8c842SOmar Sandoval struct block_device *bdev, struct iov_iter *iter, 110417f8c842SOmar Sandoval loff_t offset, get_block_t get_block, dio_iodone_t end_io, 1105facd07b0SJosef Bacik dio_submit_t submit_io, int flags) 11061da177e4SLinus Torvalds { 1107ab73857eSLinus Torvalds unsigned i_blkbits = ACCESS_ONCE(inode->i_blkbits); 1108ab73857eSLinus Torvalds unsigned blkbits = i_blkbits; 11091da177e4SLinus Torvalds unsigned blocksize_mask = (1 << blkbits) - 1; 11101da177e4SLinus Torvalds ssize_t retval = -EINVAL; 1111af436472SChristoph Hellwig size_t count = iov_iter_count(iter); 1112af436472SChristoph Hellwig loff_t end = offset + count; 11131da177e4SLinus Torvalds struct dio *dio; 1114eb28be2bSAndi Kleen struct dio_submit sdio = { 0, }; 1115847cc637SAndi Kleen struct buffer_head map_bh = { 0, }; 1116647d1e4cSFengguang Wu struct blk_plug plug; 1117886a3911SAl Viro unsigned long align = offset | iov_iter_alignment(iter); 11181da177e4SLinus Torvalds 111965dd2aa9SAndi Kleen /* 112065dd2aa9SAndi Kleen * Avoid references to bdev if not absolutely needed to give 112165dd2aa9SAndi Kleen * the early prefetch in the caller enough time. 112265dd2aa9SAndi Kleen */ 11231da177e4SLinus Torvalds 1124886a3911SAl Viro if (align & blocksize_mask) { 11251da177e4SLinus Torvalds if (bdev) 112665dd2aa9SAndi Kleen blkbits = blksize_bits(bdev_logical_block_size(bdev)); 11271da177e4SLinus Torvalds blocksize_mask = (1 << blkbits) - 1; 1128886a3911SAl Viro if (align & blocksize_mask) 11291da177e4SLinus Torvalds goto out; 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 1132f9b5570dSChristoph Hellwig /* watch out for a 0 len io from a tricksy fs */ 113317f8c842SOmar Sandoval if (iov_iter_rw(iter) == READ && !iov_iter_count(iter)) 1134f9b5570dSChristoph Hellwig return 0; 1135f9b5570dSChristoph Hellwig 11366e8267f5SAndi Kleen dio = kmem_cache_alloc(dio_cache, GFP_KERNEL); 11371da177e4SLinus Torvalds retval = -ENOMEM; 11381da177e4SLinus Torvalds if (!dio) 11391da177e4SLinus Torvalds goto out; 114023aee091SJeff Moyer /* 114123aee091SJeff Moyer * Believe it or not, zeroing out the page array caused a .5% 114223aee091SJeff Moyer * performance regression in a database benchmark. So, we take 114323aee091SJeff Moyer * care to only zero out what's needed. 114423aee091SJeff Moyer */ 114523aee091SJeff Moyer memset(dio, 0, offsetof(struct dio, pages)); 11461da177e4SLinus Torvalds 11475fe878aeSChristoph Hellwig dio->flags = flags; 11485fe878aeSChristoph Hellwig if (dio->flags & DIO_LOCKING) { 114917f8c842SOmar Sandoval if (iov_iter_rw(iter) == READ) { 11505fe878aeSChristoph Hellwig struct address_space *mapping = 11515fe878aeSChristoph Hellwig iocb->ki_filp->f_mapping; 11521da177e4SLinus Torvalds 11535fe878aeSChristoph Hellwig /* will be released by direct_io_worker */ 11541b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds retval = filemap_write_and_wait_range(mapping, offset, 11571da177e4SLinus Torvalds end - 1); 11581da177e4SLinus Torvalds if (retval) { 11595fe878aeSChristoph Hellwig mutex_unlock(&inode->i_mutex); 11606e8267f5SAndi Kleen kmem_cache_free(dio_cache, dio); 11611da177e4SLinus Torvalds goto out; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds } 1164df2d6f26SChristoph Hellwig } 11651da177e4SLinus Torvalds 11665fe878aeSChristoph Hellwig /* 116760392573SChristoph Hellwig * For file extending writes updating i_size before data writeouts 116860392573SChristoph Hellwig * complete can expose uninitialized blocks in dumb filesystems. 116960392573SChristoph Hellwig * In that case we need to wait for I/O completion even if asked 117060392573SChristoph Hellwig * for an asynchronous write. 11711da177e4SLinus Torvalds */ 117260392573SChristoph Hellwig if (is_sync_kiocb(iocb)) 117360392573SChristoph Hellwig dio->is_async = false; 117460392573SChristoph Hellwig else if (!(dio->flags & DIO_ASYNC_EXTEND) && 117517f8c842SOmar Sandoval iov_iter_rw(iter) == WRITE && end > i_size_read(inode)) 117660392573SChristoph Hellwig dio->is_async = false; 117760392573SChristoph Hellwig else 117860392573SChristoph Hellwig dio->is_async = true; 117960392573SChristoph Hellwig 1180847cc637SAndi Kleen dio->inode = inode; 118117f8c842SOmar Sandoval dio->rw = iov_iter_rw(iter) == WRITE ? WRITE_ODIRECT : READ; 118202afc27fSChristoph Hellwig 118302afc27fSChristoph Hellwig /* 118402afc27fSChristoph Hellwig * For AIO O_(D)SYNC writes we need to defer completions to a workqueue 118502afc27fSChristoph Hellwig * so that we can call ->fsync. 118602afc27fSChristoph Hellwig */ 118717f8c842SOmar Sandoval if (dio->is_async && iov_iter_rw(iter) == WRITE && 118802afc27fSChristoph Hellwig ((iocb->ki_filp->f_flags & O_DSYNC) || 118902afc27fSChristoph Hellwig IS_SYNC(iocb->ki_filp->f_mapping->host))) { 119002afc27fSChristoph Hellwig retval = dio_set_defer_completion(dio); 119102afc27fSChristoph Hellwig if (retval) { 119202afc27fSChristoph Hellwig /* 119302afc27fSChristoph Hellwig * We grab i_mutex only for reads so we don't have 119402afc27fSChristoph Hellwig * to release it here 119502afc27fSChristoph Hellwig */ 119602afc27fSChristoph Hellwig kmem_cache_free(dio_cache, dio); 119702afc27fSChristoph Hellwig goto out; 119802afc27fSChristoph Hellwig } 119902afc27fSChristoph Hellwig } 120002afc27fSChristoph Hellwig 120102afc27fSChristoph Hellwig /* 120202afc27fSChristoph Hellwig * Will be decremented at I/O completion time. 120302afc27fSChristoph Hellwig */ 1204fe0f07d0SJens Axboe if (!(dio->flags & DIO_SKIP_DIO_COUNT)) 1205fe0f07d0SJens Axboe inode_dio_begin(inode); 120602afc27fSChristoph Hellwig 120702afc27fSChristoph Hellwig retval = 0; 1208847cc637SAndi Kleen sdio.blkbits = blkbits; 1209ab73857eSLinus Torvalds sdio.blkfactor = i_blkbits - blkbits; 1210847cc637SAndi Kleen sdio.block_in_file = offset >> blkbits; 1211847cc637SAndi Kleen 1212847cc637SAndi Kleen sdio.get_block = get_block; 1213847cc637SAndi Kleen dio->end_io = end_io; 1214847cc637SAndi Kleen sdio.submit_io = submit_io; 1215847cc637SAndi Kleen sdio.final_block_in_bio = -1; 1216847cc637SAndi Kleen sdio.next_block_for_io = -1; 1217847cc637SAndi Kleen 1218847cc637SAndi Kleen dio->iocb = iocb; 1219847cc637SAndi Kleen dio->i_size = i_size_read(inode); 1220847cc637SAndi Kleen 1221847cc637SAndi Kleen spin_lock_init(&dio->bio_lock); 1222847cc637SAndi Kleen dio->refcount = 1; 1223847cc637SAndi Kleen 1224*53cbf3b1SMing Lei dio->should_dirty = (iter->type == ITER_IOVEC); 12257b2c99d1SAl Viro sdio.iter = iter; 12267b2c99d1SAl Viro sdio.final_block_in_request = 12277b2c99d1SAl Viro (offset + iov_iter_count(iter)) >> blkbits; 12287b2c99d1SAl Viro 1229847cc637SAndi Kleen /* 1230847cc637SAndi Kleen * In case of non-aligned buffers, we may need 2 more 1231847cc637SAndi Kleen * pages since we need to zero out first and last block. 1232847cc637SAndi Kleen */ 1233847cc637SAndi Kleen if (unlikely(sdio.blkfactor)) 1234847cc637SAndi Kleen sdio.pages_in_io = 2; 1235847cc637SAndi Kleen 1236f67da30cSAl Viro sdio.pages_in_io += iov_iter_npages(iter, INT_MAX); 1237847cc637SAndi Kleen 1238647d1e4cSFengguang Wu blk_start_plug(&plug); 1239647d1e4cSFengguang Wu 1240847cc637SAndi Kleen retval = do_direct_IO(dio, &sdio, &map_bh); 12417b2c99d1SAl Viro if (retval) 1242847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1243847cc637SAndi Kleen 1244847cc637SAndi Kleen if (retval == -ENOTBLK) { 1245847cc637SAndi Kleen /* 1246847cc637SAndi Kleen * The remaining part of the request will be 1247847cc637SAndi Kleen * be handled by buffered I/O when we return 1248847cc637SAndi Kleen */ 1249847cc637SAndi Kleen retval = 0; 1250847cc637SAndi Kleen } 1251847cc637SAndi Kleen /* 1252847cc637SAndi Kleen * There may be some unwritten disk at the end of a part-written 1253847cc637SAndi Kleen * fs-block-sized block. Go zero that now. 1254847cc637SAndi Kleen */ 1255847cc637SAndi Kleen dio_zero_block(dio, &sdio, 1, &map_bh); 1256847cc637SAndi Kleen 1257847cc637SAndi Kleen if (sdio.cur_page) { 1258847cc637SAndi Kleen ssize_t ret2; 1259847cc637SAndi Kleen 1260847cc637SAndi Kleen ret2 = dio_send_cur_page(dio, &sdio, &map_bh); 1261847cc637SAndi Kleen if (retval == 0) 1262847cc637SAndi Kleen retval = ret2; 1263847cc637SAndi Kleen page_cache_release(sdio.cur_page); 1264847cc637SAndi Kleen sdio.cur_page = NULL; 1265847cc637SAndi Kleen } 1266847cc637SAndi Kleen if (sdio.bio) 1267847cc637SAndi Kleen dio_bio_submit(dio, &sdio); 1268847cc637SAndi Kleen 1269647d1e4cSFengguang Wu blk_finish_plug(&plug); 1270647d1e4cSFengguang Wu 1271847cc637SAndi Kleen /* 1272847cc637SAndi Kleen * It is possible that, we return short IO due to end of file. 1273847cc637SAndi Kleen * In that case, we need to release all the pages we got hold on. 1274847cc637SAndi Kleen */ 1275847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1276847cc637SAndi Kleen 1277847cc637SAndi Kleen /* 1278847cc637SAndi Kleen * All block lookups have been performed. For READ requests 1279847cc637SAndi Kleen * we can let i_mutex go now that its achieved its purpose 1280847cc637SAndi Kleen * of protecting us from looking up uninitialized blocks. 1281847cc637SAndi Kleen */ 128217f8c842SOmar Sandoval if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING)) 1283847cc637SAndi Kleen mutex_unlock(&dio->inode->i_mutex); 1284847cc637SAndi Kleen 1285847cc637SAndi Kleen /* 1286847cc637SAndi Kleen * The only time we want to leave bios in flight is when a successful 1287847cc637SAndi Kleen * partial aio read or full aio write have been setup. In that case 1288847cc637SAndi Kleen * bio completion will call aio_complete. The only time it's safe to 1289847cc637SAndi Kleen * call aio_complete is when we return -EIOCBQUEUED, so we key on that. 1290847cc637SAndi Kleen * This had *better* be the only place that raises -EIOCBQUEUED. 1291847cc637SAndi Kleen */ 1292847cc637SAndi Kleen BUG_ON(retval == -EIOCBQUEUED); 1293847cc637SAndi Kleen if (dio->is_async && retval == 0 && dio->result && 129417f8c842SOmar Sandoval (iov_iter_rw(iter) == READ || dio->result == count)) 1295847cc637SAndi Kleen retval = -EIOCBQUEUED; 1296af436472SChristoph Hellwig else 1297847cc637SAndi Kleen dio_await_completion(dio); 1298847cc637SAndi Kleen 1299847cc637SAndi Kleen if (drop_refcount(dio) == 0) { 1300847cc637SAndi Kleen retval = dio_complete(dio, offset, retval, false); 1301847cc637SAndi Kleen } else 1302847cc637SAndi Kleen BUG_ON(retval != -EIOCBQUEUED); 13031da177e4SLinus Torvalds 13047bb46a67Snpiggin@suse.de out: 13057bb46a67Snpiggin@suse.de return retval; 13067bb46a67Snpiggin@suse.de } 130765dd2aa9SAndi Kleen 130817f8c842SOmar Sandoval ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, 130917f8c842SOmar Sandoval struct block_device *bdev, struct iov_iter *iter, 131017f8c842SOmar Sandoval loff_t offset, get_block_t get_block, 131117f8c842SOmar Sandoval dio_iodone_t end_io, dio_submit_t submit_io, 131217f8c842SOmar Sandoval int flags) 131365dd2aa9SAndi Kleen { 131465dd2aa9SAndi Kleen /* 131565dd2aa9SAndi Kleen * The block device state is needed in the end to finally 131665dd2aa9SAndi Kleen * submit everything. Since it's likely to be cache cold 131765dd2aa9SAndi Kleen * prefetch it here as first thing to hide some of the 131865dd2aa9SAndi Kleen * latency. 131965dd2aa9SAndi Kleen * 132065dd2aa9SAndi Kleen * Attempt to prefetch the pieces we likely need later. 132165dd2aa9SAndi Kleen */ 132265dd2aa9SAndi Kleen prefetch(&bdev->bd_disk->part_tbl); 132365dd2aa9SAndi Kleen prefetch(bdev->bd_queue); 132465dd2aa9SAndi Kleen prefetch((char *)bdev->bd_queue + SMP_CACHE_BYTES); 132565dd2aa9SAndi Kleen 132617f8c842SOmar Sandoval return do_blockdev_direct_IO(iocb, inode, bdev, iter, offset, get_block, 132717f8c842SOmar Sandoval end_io, submit_io, flags); 132865dd2aa9SAndi Kleen } 132965dd2aa9SAndi Kleen 13301da177e4SLinus Torvalds EXPORT_SYMBOL(__blockdev_direct_IO); 13316e8267f5SAndi Kleen 13326e8267f5SAndi Kleen static __init int dio_init(void) 13336e8267f5SAndi Kleen { 13346e8267f5SAndi Kleen dio_cache = KMEM_CACHE(dio, SLAB_PANIC); 13356e8267f5SAndi Kleen return 0; 13366e8267f5SAndi Kleen } 13376e8267f5SAndi Kleen module_init(dio_init) 1338