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> 39*65dd2aa9SAndi 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 size_t size; /* total request size (doesn't change)*/ 741da177e4SLinus Torvalds sector_t block_in_file; /* Current offset into the underlying 751da177e4SLinus Torvalds file in dio_block units. */ 761da177e4SLinus Torvalds unsigned blocks_available; /* At block_in_file. changes */ 770dc2bc49SAndi Kleen int reap_counter; /* rate limit reaping */ 781da177e4SLinus Torvalds sector_t final_block_in_request;/* doesn't change */ 791da177e4SLinus Torvalds unsigned first_block_in_page; /* doesn't change, Used only once */ 801da177e4SLinus Torvalds int boundary; /* prev block is at a boundary */ 811d8fa7a2SBadari Pulavarty get_block_t *get_block; /* block mapping function */ 82facd07b0SJosef Bacik dio_submit_t *submit_io; /* IO submition function */ 83eb28be2bSAndi Kleen 84facd07b0SJosef Bacik loff_t logical_offset_in_bio; /* current first logical block in bio */ 851da177e4SLinus Torvalds sector_t final_block_in_bio; /* current final block in bio + 1 */ 861da177e4SLinus Torvalds sector_t next_block_for_io; /* next block to be put under IO, 871da177e4SLinus Torvalds in dio_blocks units */ 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * Deferred addition of a page to the dio. These variables are 911da177e4SLinus Torvalds * private to dio_send_cur_page(), submit_page_section() and 921da177e4SLinus Torvalds * dio_bio_add_page(). 931da177e4SLinus Torvalds */ 941da177e4SLinus Torvalds struct page *cur_page; /* The page */ 951da177e4SLinus Torvalds unsigned cur_page_offset; /* Offset into it, in bytes */ 961da177e4SLinus Torvalds unsigned cur_page_len; /* Nr of bytes at cur_page_offset */ 971da177e4SLinus Torvalds sector_t cur_page_block; /* Where it starts */ 98facd07b0SJosef Bacik loff_t cur_page_fs_offset; /* Offset in file */ 991da177e4SLinus Torvalds 10023aee091SJeff Moyer /* 10123aee091SJeff Moyer * Page fetching state. These variables belong to dio_refill_pages(). 10223aee091SJeff Moyer */ 10323aee091SJeff Moyer int curr_page; /* changes */ 10423aee091SJeff Moyer int total_pages; /* doesn't change */ 10523aee091SJeff Moyer unsigned long curr_user_address;/* changes */ 10623aee091SJeff Moyer 10723aee091SJeff Moyer /* 10823aee091SJeff Moyer * Page queue. These variables belong to dio_refill_pages() and 10923aee091SJeff Moyer * dio_get_page(). 11023aee091SJeff Moyer */ 11123aee091SJeff Moyer unsigned head; /* next page to process */ 11223aee091SJeff Moyer unsigned tail; /* last valid page + 1 */ 113eb28be2bSAndi Kleen }; 114eb28be2bSAndi Kleen 115eb28be2bSAndi Kleen /* dio_state communicated between submission path and end_io */ 116eb28be2bSAndi Kleen struct dio { 117eb28be2bSAndi Kleen int flags; /* doesn't change */ 118eb28be2bSAndi Kleen int rw; 1190dc2bc49SAndi Kleen struct inode *inode; 120eb28be2bSAndi Kleen loff_t i_size; /* i_size when submitted */ 121eb28be2bSAndi Kleen dio_iodone_t *end_io; /* IO completion function */ 122eb28be2bSAndi Kleen 12318772641SAndi Kleen void *private; /* copy from map_bh.b_private */ 124eb28be2bSAndi Kleen 125eb28be2bSAndi Kleen /* BIO completion state */ 126eb28be2bSAndi Kleen spinlock_t bio_lock; /* protects BIO fields below */ 1270dc2bc49SAndi Kleen int page_errors; /* errno from get_user_pages() */ 1280dc2bc49SAndi Kleen int is_async; /* is IO async ? */ 1290dc2bc49SAndi Kleen int io_error; /* IO error in completion path */ 130eb28be2bSAndi Kleen unsigned long refcount; /* direct_io_worker() and bios */ 131eb28be2bSAndi Kleen struct bio *bio_list; /* singly linked via bi_private */ 132eb28be2bSAndi Kleen struct task_struct *waiter; /* waiting task (NULL if none) */ 133eb28be2bSAndi Kleen 134eb28be2bSAndi Kleen /* AIO related stuff */ 135eb28be2bSAndi Kleen struct kiocb *iocb; /* kiocb */ 136eb28be2bSAndi Kleen ssize_t result; /* IO result */ 137eb28be2bSAndi Kleen 13823aee091SJeff Moyer /* 13923aee091SJeff Moyer * pages[] (and any fields placed after it) are not zeroed out at 14023aee091SJeff Moyer * allocation time. Don't add new fields after pages[] unless you 14123aee091SJeff Moyer * wish that they not be zeroed. 14223aee091SJeff Moyer */ 14323aee091SJeff Moyer struct page *pages[DIO_PAGES]; /* page buffer */ 1446e8267f5SAndi Kleen } ____cacheline_aligned_in_smp; 1456e8267f5SAndi Kleen 1466e8267f5SAndi Kleen static struct kmem_cache *dio_cache __read_mostly; 1471da177e4SLinus Torvalds 148bd5fe6c5SChristoph Hellwig static void __inode_dio_wait(struct inode *inode) 149bd5fe6c5SChristoph Hellwig { 150bd5fe6c5SChristoph Hellwig wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP); 151bd5fe6c5SChristoph Hellwig DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP); 152bd5fe6c5SChristoph Hellwig 153bd5fe6c5SChristoph Hellwig do { 154bd5fe6c5SChristoph Hellwig prepare_to_wait(wq, &q.wait, TASK_UNINTERRUPTIBLE); 155bd5fe6c5SChristoph Hellwig if (atomic_read(&inode->i_dio_count)) 156bd5fe6c5SChristoph Hellwig schedule(); 157bd5fe6c5SChristoph Hellwig } while (atomic_read(&inode->i_dio_count)); 158bd5fe6c5SChristoph Hellwig finish_wait(wq, &q.wait); 159bd5fe6c5SChristoph Hellwig } 160bd5fe6c5SChristoph Hellwig 161bd5fe6c5SChristoph Hellwig /** 162bd5fe6c5SChristoph Hellwig * inode_dio_wait - wait for outstanding DIO requests to finish 163bd5fe6c5SChristoph Hellwig * @inode: inode to wait for 164bd5fe6c5SChristoph Hellwig * 165bd5fe6c5SChristoph Hellwig * Waits for all pending direct I/O requests to finish so that we can 166bd5fe6c5SChristoph Hellwig * proceed with a truncate or equivalent operation. 167bd5fe6c5SChristoph Hellwig * 168bd5fe6c5SChristoph Hellwig * Must be called under a lock that serializes taking new references 169bd5fe6c5SChristoph Hellwig * to i_dio_count, usually by inode->i_mutex. 170bd5fe6c5SChristoph Hellwig */ 171bd5fe6c5SChristoph Hellwig void inode_dio_wait(struct inode *inode) 172bd5fe6c5SChristoph Hellwig { 173bd5fe6c5SChristoph Hellwig if (atomic_read(&inode->i_dio_count)) 174bd5fe6c5SChristoph Hellwig __inode_dio_wait(inode); 175bd5fe6c5SChristoph Hellwig } 176bd5fe6c5SChristoph Hellwig EXPORT_SYMBOL_GPL(inode_dio_wait); 177bd5fe6c5SChristoph Hellwig 178bd5fe6c5SChristoph Hellwig /* 179bd5fe6c5SChristoph Hellwig * inode_dio_done - signal finish of a direct I/O requests 180bd5fe6c5SChristoph Hellwig * @inode: inode the direct I/O happens on 181bd5fe6c5SChristoph Hellwig * 182bd5fe6c5SChristoph Hellwig * This is called once we've finished processing a direct I/O request, 183bd5fe6c5SChristoph Hellwig * and is used to wake up callers waiting for direct I/O to be quiesced. 184bd5fe6c5SChristoph Hellwig */ 185bd5fe6c5SChristoph Hellwig void inode_dio_done(struct inode *inode) 186bd5fe6c5SChristoph Hellwig { 187bd5fe6c5SChristoph Hellwig if (atomic_dec_and_test(&inode->i_dio_count)) 188bd5fe6c5SChristoph Hellwig wake_up_bit(&inode->i_state, __I_DIO_WAKEUP); 189bd5fe6c5SChristoph Hellwig } 190bd5fe6c5SChristoph Hellwig EXPORT_SYMBOL_GPL(inode_dio_done); 191bd5fe6c5SChristoph Hellwig 1921da177e4SLinus Torvalds /* 1931da177e4SLinus Torvalds * How many pages are in the queue? 1941da177e4SLinus Torvalds */ 195eb28be2bSAndi Kleen static inline unsigned dio_pages_present(struct dio_submit *sdio) 1961da177e4SLinus Torvalds { 197eb28be2bSAndi Kleen return sdio->tail - sdio->head; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* 2011da177e4SLinus Torvalds * Go grab and pin some userspace pages. Typically we'll get 64 at a time. 2021da177e4SLinus Torvalds */ 203ba253fbfSAndi Kleen static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds int ret; 2061da177e4SLinus Torvalds int nr_pages; 2071da177e4SLinus Torvalds 208eb28be2bSAndi Kleen nr_pages = min(sdio->total_pages - sdio->curr_page, DIO_PAGES); 209f5dd33c4SNick Piggin ret = get_user_pages_fast( 210eb28be2bSAndi Kleen sdio->curr_user_address, /* Where from? */ 2111da177e4SLinus Torvalds nr_pages, /* How many pages? */ 2121da177e4SLinus Torvalds dio->rw == READ, /* Write to memory? */ 213f5dd33c4SNick Piggin &dio->pages[0]); /* Put results here */ 2141da177e4SLinus Torvalds 215eb28be2bSAndi Kleen if (ret < 0 && sdio->blocks_available && (dio->rw & WRITE)) { 216557ed1faSNick Piggin struct page *page = ZERO_PAGE(0); 2171da177e4SLinus Torvalds /* 2181da177e4SLinus Torvalds * A memory fault, but the filesystem has some outstanding 2191da177e4SLinus Torvalds * mapped blocks. We need to use those blocks up to avoid 2201da177e4SLinus Torvalds * leaking stale data in the file. 2211da177e4SLinus Torvalds */ 2221da177e4SLinus Torvalds if (dio->page_errors == 0) 2231da177e4SLinus Torvalds dio->page_errors = ret; 224b5810039SNick Piggin page_cache_get(page); 225b5810039SNick Piggin dio->pages[0] = page; 226eb28be2bSAndi Kleen sdio->head = 0; 227eb28be2bSAndi Kleen sdio->tail = 1; 2281da177e4SLinus Torvalds ret = 0; 2291da177e4SLinus Torvalds goto out; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds if (ret >= 0) { 233eb28be2bSAndi Kleen sdio->curr_user_address += ret * PAGE_SIZE; 234eb28be2bSAndi Kleen sdio->curr_page += ret; 235eb28be2bSAndi Kleen sdio->head = 0; 236eb28be2bSAndi Kleen sdio->tail = ret; 2371da177e4SLinus Torvalds ret = 0; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds out: 2401da177e4SLinus Torvalds return ret; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* 2441da177e4SLinus Torvalds * Get another userspace page. Returns an ERR_PTR on error. Pages are 2451da177e4SLinus Torvalds * buffered inside the dio so that we can call get_user_pages() against a 2461da177e4SLinus Torvalds * decent number of pages, less frequently. To provide nicer use of the 2471da177e4SLinus Torvalds * L1 cache. 2481da177e4SLinus Torvalds */ 249ba253fbfSAndi Kleen static inline struct page *dio_get_page(struct dio *dio, 250ba253fbfSAndi Kleen struct dio_submit *sdio) 2511da177e4SLinus Torvalds { 252eb28be2bSAndi Kleen if (dio_pages_present(sdio) == 0) { 2531da177e4SLinus Torvalds int ret; 2541da177e4SLinus Torvalds 255eb28be2bSAndi Kleen ret = dio_refill_pages(dio, sdio); 2561da177e4SLinus Torvalds if (ret) 2571da177e4SLinus Torvalds return ERR_PTR(ret); 258eb28be2bSAndi Kleen BUG_ON(dio_pages_present(sdio) == 0); 2591da177e4SLinus Torvalds } 260eb28be2bSAndi Kleen return dio->pages[sdio->head++]; 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2636d544bb4SZach Brown /** 2646d544bb4SZach Brown * dio_complete() - called when all DIO BIO I/O has been completed 2656d544bb4SZach Brown * @offset: the byte offset in the file of the completed operation 2666d544bb4SZach Brown * 2676d544bb4SZach Brown * This releases locks as dictated by the locking type, lets interested parties 2686d544bb4SZach Brown * know that a DIO operation has completed, and calculates the resulting return 2696d544bb4SZach Brown * code for the operation. 2706d544bb4SZach Brown * 2716d544bb4SZach Brown * It lets the filesystem know if it registered an interest earlier via 2726d544bb4SZach Brown * get_block. Pass the private field of the map buffer_head so that 2736d544bb4SZach Brown * filesystems can use it to hold additional state between get_block calls and 2746d544bb4SZach Brown * dio_complete. 2751da177e4SLinus Torvalds */ 276cd1c584fSEdward Shishkin static ssize_t dio_complete(struct dio *dio, loff_t offset, ssize_t ret, bool is_async) 2771da177e4SLinus Torvalds { 2786d544bb4SZach Brown ssize_t transferred = 0; 2796d544bb4SZach Brown 2808459d86aSZach Brown /* 2818459d86aSZach Brown * AIO submission can race with bio completion to get here while 2828459d86aSZach Brown * expecting to have the last io completed by bio completion. 2838459d86aSZach Brown * In that case -EIOCBQUEUED is in fact not an error we want 2848459d86aSZach Brown * to preserve through this call. 2858459d86aSZach Brown */ 2868459d86aSZach Brown if (ret == -EIOCBQUEUED) 2878459d86aSZach Brown ret = 0; 2888459d86aSZach Brown 2896d544bb4SZach Brown if (dio->result) { 2906d544bb4SZach Brown transferred = dio->result; 2916d544bb4SZach Brown 2926d544bb4SZach Brown /* Check for short read case */ 2936d544bb4SZach Brown if ((dio->rw == READ) && ((offset + transferred) > dio->i_size)) 2946d544bb4SZach Brown transferred = dio->i_size - offset; 2956d544bb4SZach Brown } 2966d544bb4SZach Brown 2976d544bb4SZach Brown if (ret == 0) 2986d544bb4SZach Brown ret = dio->page_errors; 2996d544bb4SZach Brown if (ret == 0) 3006d544bb4SZach Brown ret = dio->io_error; 3016d544bb4SZach Brown if (ret == 0) 3026d544bb4SZach Brown ret = transferred; 3036d544bb4SZach Brown 30440e2e973SChristoph Hellwig if (dio->end_io && dio->result) { 30540e2e973SChristoph Hellwig dio->end_io(dio->iocb, offset, transferred, 30618772641SAndi Kleen dio->private, ret, is_async); 30772c5052dSChristoph Hellwig } else { 30872c5052dSChristoph Hellwig if (is_async) 30940e2e973SChristoph Hellwig aio_complete(dio->iocb, ret, 0); 31072c5052dSChristoph Hellwig inode_dio_done(dio->inode); 31140e2e973SChristoph Hellwig } 31240e2e973SChristoph Hellwig 3136d544bb4SZach Brown return ret; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds static int dio_bio_complete(struct dio *dio, struct bio *bio); 3171da177e4SLinus Torvalds /* 3181da177e4SLinus Torvalds * Asynchronous IO callback. 3191da177e4SLinus Torvalds */ 3206712ecf8SNeilBrown static void dio_bio_end_aio(struct bio *bio, int error) 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 3235eb6c7a2SZach Brown unsigned long remaining; 3245eb6c7a2SZach Brown unsigned long flags; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* cleanup the bio */ 3271da177e4SLinus Torvalds dio_bio_complete(dio, bio); 3280273201eSZach Brown 3295eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 3305eb6c7a2SZach Brown remaining = --dio->refcount; 3315eb6c7a2SZach Brown if (remaining == 1 && dio->waiter) 33220258b2bSZach Brown wake_up_process(dio->waiter); 3335eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 33420258b2bSZach Brown 3358459d86aSZach Brown if (remaining == 0) { 33640e2e973SChristoph Hellwig dio_complete(dio, dio->iocb->ki_pos, 0, true); 3376e8267f5SAndi Kleen kmem_cache_free(dio_cache, dio); 3388459d86aSZach Brown } 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* 3421da177e4SLinus Torvalds * The BIO completion handler simply queues the BIO up for the process-context 3431da177e4SLinus Torvalds * handler. 3441da177e4SLinus Torvalds * 3451da177e4SLinus Torvalds * During I/O bi_private points at the dio. After I/O, bi_private is used to 3461da177e4SLinus Torvalds * implement a singly-linked list of completed BIOs, at dio->bio_list. 3471da177e4SLinus Torvalds */ 3486712ecf8SNeilBrown static void dio_bio_end_io(struct bio *bio, int error) 3491da177e4SLinus Torvalds { 3501da177e4SLinus Torvalds struct dio *dio = bio->bi_private; 3511da177e4SLinus Torvalds unsigned long flags; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 3541da177e4SLinus Torvalds bio->bi_private = dio->bio_list; 3551da177e4SLinus Torvalds dio->bio_list = bio; 3565eb6c7a2SZach Brown if (--dio->refcount == 1 && dio->waiter) 3571da177e4SLinus Torvalds wake_up_process(dio->waiter); 3581da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 361facd07b0SJosef Bacik /** 362facd07b0SJosef Bacik * dio_end_io - handle the end io action for the given bio 363facd07b0SJosef Bacik * @bio: The direct io bio thats being completed 364facd07b0SJosef Bacik * @error: Error if there was one 365facd07b0SJosef Bacik * 366facd07b0SJosef Bacik * This is meant to be called by any filesystem that uses their own dio_submit_t 367facd07b0SJosef Bacik * so that the DIO specific endio actions are dealt with after the filesystem 368facd07b0SJosef Bacik * has done it's completion work. 369facd07b0SJosef Bacik */ 370facd07b0SJosef Bacik void dio_end_io(struct bio *bio, int error) 371facd07b0SJosef Bacik { 372facd07b0SJosef Bacik struct dio *dio = bio->bi_private; 373facd07b0SJosef Bacik 374facd07b0SJosef Bacik if (dio->is_async) 375facd07b0SJosef Bacik dio_bio_end_aio(bio, error); 376facd07b0SJosef Bacik else 377facd07b0SJosef Bacik dio_bio_end_io(bio, error); 378facd07b0SJosef Bacik } 379facd07b0SJosef Bacik EXPORT_SYMBOL_GPL(dio_end_io); 380facd07b0SJosef Bacik 381ba253fbfSAndi Kleen static inline void 382eb28be2bSAndi Kleen dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, 383eb28be2bSAndi Kleen struct block_device *bdev, 3841da177e4SLinus Torvalds sector_t first_sector, int nr_vecs) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds struct bio *bio; 3871da177e4SLinus Torvalds 38820d9600cSDavid Dillow /* 38920d9600cSDavid Dillow * bio_alloc() is guaranteed to return a bio when called with 39020d9600cSDavid Dillow * __GFP_WAIT and we request a valid number of vectors. 39120d9600cSDavid Dillow */ 3921da177e4SLinus Torvalds bio = bio_alloc(GFP_KERNEL, nr_vecs); 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds bio->bi_bdev = bdev; 3951da177e4SLinus Torvalds bio->bi_sector = first_sector; 3961da177e4SLinus Torvalds if (dio->is_async) 3971da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_aio; 3981da177e4SLinus Torvalds else 3991da177e4SLinus Torvalds bio->bi_end_io = dio_bio_end_io; 4001da177e4SLinus Torvalds 401eb28be2bSAndi Kleen sdio->bio = bio; 402eb28be2bSAndi Kleen sdio->logical_offset_in_bio = sdio->cur_page_fs_offset; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * In the AIO read case we speculatively dirty the pages before starting IO. 4071da177e4SLinus Torvalds * During IO completion, any of these pages which happen to have been written 4081da177e4SLinus Torvalds * back will be redirtied by bio_check_pages_dirty(). 4090273201eSZach Brown * 4100273201eSZach Brown * bios hold a dio reference between submit_bio and ->end_io. 4111da177e4SLinus Torvalds */ 412ba253fbfSAndi Kleen static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio) 4131da177e4SLinus Torvalds { 414eb28be2bSAndi Kleen struct bio *bio = sdio->bio; 4155eb6c7a2SZach Brown unsigned long flags; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds bio->bi_private = dio; 4185eb6c7a2SZach Brown 4195eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 4205eb6c7a2SZach Brown dio->refcount++; 4215eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 4225eb6c7a2SZach Brown 4231da177e4SLinus Torvalds if (dio->is_async && dio->rw == READ) 4241da177e4SLinus Torvalds bio_set_pages_dirty(bio); 4255eb6c7a2SZach Brown 426eb28be2bSAndi Kleen if (sdio->submit_io) 427eb28be2bSAndi Kleen sdio->submit_io(dio->rw, bio, dio->inode, 428eb28be2bSAndi Kleen sdio->logical_offset_in_bio); 429facd07b0SJosef Bacik else 4301da177e4SLinus Torvalds submit_bio(dio->rw, bio); 4311da177e4SLinus Torvalds 432eb28be2bSAndi Kleen sdio->bio = NULL; 433eb28be2bSAndi Kleen sdio->boundary = 0; 434eb28be2bSAndi Kleen sdio->logical_offset_in_bio = 0; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* 4381da177e4SLinus Torvalds * Release any resources in case of a failure 4391da177e4SLinus Torvalds */ 440ba253fbfSAndi Kleen static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio) 4411da177e4SLinus Torvalds { 442eb28be2bSAndi Kleen while (dio_pages_present(sdio)) 443eb28be2bSAndi Kleen page_cache_release(dio_get_page(dio, sdio)); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* 4470273201eSZach Brown * Wait for the next BIO to complete. Remove it and return it. NULL is 4480273201eSZach Brown * returned once all BIOs have been completed. This must only be called once 4490273201eSZach Brown * all bios have been issued so that dio->refcount can only decrease. This 4500273201eSZach Brown * requires that that the caller hold a reference on the dio. 4511da177e4SLinus Torvalds */ 4521da177e4SLinus Torvalds static struct bio *dio_await_one(struct dio *dio) 4531da177e4SLinus Torvalds { 4541da177e4SLinus Torvalds unsigned long flags; 4550273201eSZach Brown struct bio *bio = NULL; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 4585eb6c7a2SZach Brown 4595eb6c7a2SZach Brown /* 4605eb6c7a2SZach Brown * Wait as long as the list is empty and there are bios in flight. bio 4615eb6c7a2SZach Brown * completion drops the count, maybe adds to the list, and wakes while 4625eb6c7a2SZach Brown * holding the bio_lock so we don't need set_current_state()'s barrier 4635eb6c7a2SZach Brown * and can call it after testing our condition. 4645eb6c7a2SZach Brown */ 4655eb6c7a2SZach Brown while (dio->refcount > 1 && dio->bio_list == NULL) { 4665eb6c7a2SZach Brown __set_current_state(TASK_UNINTERRUPTIBLE); 4671da177e4SLinus Torvalds dio->waiter = current; 4681da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 4691da177e4SLinus Torvalds io_schedule(); 4705eb6c7a2SZach Brown /* wake up sets us TASK_RUNNING */ 4711da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 4721da177e4SLinus Torvalds dio->waiter = NULL; 4731da177e4SLinus Torvalds } 4740273201eSZach Brown if (dio->bio_list) { 4751da177e4SLinus Torvalds bio = dio->bio_list; 4761da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 4770273201eSZach Brown } 4781da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 4791da177e4SLinus Torvalds return bio; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* 4831da177e4SLinus Torvalds * Process one completed BIO. No locks are held. 4841da177e4SLinus Torvalds */ 4851da177e4SLinus Torvalds static int dio_bio_complete(struct dio *dio, struct bio *bio) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 4881da177e4SLinus Torvalds struct bio_vec *bvec = bio->bi_io_vec; 4891da177e4SLinus Torvalds int page_no; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds if (!uptodate) 492174e27c6SChen, Kenneth W dio->io_error = -EIO; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (dio->is_async && dio->rw == READ) { 4951da177e4SLinus Torvalds bio_check_pages_dirty(bio); /* transfers ownership */ 4961da177e4SLinus Torvalds } else { 4971da177e4SLinus Torvalds for (page_no = 0; page_no < bio->bi_vcnt; page_no++) { 4981da177e4SLinus Torvalds struct page *page = bvec[page_no].bv_page; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (dio->rw == READ && !PageCompound(page)) 5011da177e4SLinus Torvalds set_page_dirty_lock(page); 5021da177e4SLinus Torvalds page_cache_release(page); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds bio_put(bio); 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds return uptodate ? 0 : -EIO; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* 5100273201eSZach Brown * Wait on and process all in-flight BIOs. This must only be called once 5110273201eSZach Brown * all bios have been issued so that the refcount can only decrease. 5120273201eSZach Brown * This just waits for all bios to make it through dio_bio_complete. IO 513beb7dd86SRobert P. J. Day * errors are propagated through dio->io_error and should be propagated via 5140273201eSZach Brown * dio_complete(). 5151da177e4SLinus Torvalds */ 5166d544bb4SZach Brown static void dio_await_completion(struct dio *dio) 5171da177e4SLinus Torvalds { 5180273201eSZach Brown struct bio *bio; 5190273201eSZach Brown do { 5200273201eSZach Brown bio = dio_await_one(dio); 5210273201eSZach Brown if (bio) 5226d544bb4SZach Brown dio_bio_complete(dio, bio); 5230273201eSZach Brown } while (bio); 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds /* 5271da177e4SLinus Torvalds * A really large O_DIRECT read or write can generate a lot of BIOs. So 5281da177e4SLinus Torvalds * to keep the memory consumption sane we periodically reap any completed BIOs 5291da177e4SLinus Torvalds * during the BIO generation phase. 5301da177e4SLinus Torvalds * 5311da177e4SLinus Torvalds * This also helps to limit the peak amount of pinned userspace memory. 5321da177e4SLinus Torvalds */ 533ba253fbfSAndi Kleen static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds int ret = 0; 5361da177e4SLinus Torvalds 537eb28be2bSAndi Kleen if (sdio->reap_counter++ >= 64) { 5381da177e4SLinus Torvalds while (dio->bio_list) { 5391da177e4SLinus Torvalds unsigned long flags; 5401da177e4SLinus Torvalds struct bio *bio; 5411da177e4SLinus Torvalds int ret2; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds spin_lock_irqsave(&dio->bio_lock, flags); 5441da177e4SLinus Torvalds bio = dio->bio_list; 5451da177e4SLinus Torvalds dio->bio_list = bio->bi_private; 5461da177e4SLinus Torvalds spin_unlock_irqrestore(&dio->bio_lock, flags); 5471da177e4SLinus Torvalds ret2 = dio_bio_complete(dio, bio); 5481da177e4SLinus Torvalds if (ret == 0) 5491da177e4SLinus Torvalds ret = ret2; 5501da177e4SLinus Torvalds } 551eb28be2bSAndi Kleen sdio->reap_counter = 0; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds return ret; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds /* 5571da177e4SLinus Torvalds * Call into the fs to map some more disk blocks. We record the current number 558eb28be2bSAndi Kleen * of available blocks at sdio->blocks_available. These are in units of the 5591da177e4SLinus Torvalds * fs blocksize, (1 << inode->i_blkbits). 5601da177e4SLinus Torvalds * 5611da177e4SLinus Torvalds * The fs is allowed to map lots of blocks at once. If it wants to do that, 5621da177e4SLinus Torvalds * it uses the passed inode-relative block number as the file offset, as usual. 5631da177e4SLinus Torvalds * 5641d8fa7a2SBadari Pulavarty * get_block() is passed the number of i_blkbits-sized blocks which direct_io 5651da177e4SLinus Torvalds * has remaining to do. The fs should not map more than this number of blocks. 5661da177e4SLinus Torvalds * 5671da177e4SLinus Torvalds * If the fs has mapped a lot of blocks, it should populate bh->b_size to 5681da177e4SLinus Torvalds * indicate how much contiguous disk space has been made available at 5691da177e4SLinus Torvalds * bh->b_blocknr. 5701da177e4SLinus Torvalds * 5711da177e4SLinus Torvalds * If *any* of the mapped blocks are new, then the fs must set buffer_new(). 5721da177e4SLinus Torvalds * This isn't very efficient... 5731da177e4SLinus Torvalds * 5741da177e4SLinus Torvalds * In the case of filesystem holes: the fs may return an arbitrarily-large 5751da177e4SLinus Torvalds * hole by returning an appropriate value in b_size and by clearing 5761da177e4SLinus Torvalds * buffer_mapped(). However the direct-io code will only process holes one 5771d8fa7a2SBadari Pulavarty * block at a time - it will repeatedly call get_block() as it walks the hole. 5781da177e4SLinus Torvalds */ 57918772641SAndi Kleen static int get_more_blocks(struct dio *dio, struct dio_submit *sdio, 58018772641SAndi Kleen struct buffer_head *map_bh) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds int ret; 5831da177e4SLinus Torvalds sector_t fs_startblk; /* Into file, in filesystem-sized blocks */ 584ae55e1aaSTao Ma sector_t fs_endblk; /* Into file, in filesystem-sized blocks */ 5851da177e4SLinus Torvalds unsigned long fs_count; /* Number of filesystem-sized blocks */ 5861da177e4SLinus Torvalds int create; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds /* 5891da177e4SLinus Torvalds * If there was a memory error and we've overwritten all the 5901da177e4SLinus Torvalds * mapped blocks then we can now return that memory error 5911da177e4SLinus Torvalds */ 5921da177e4SLinus Torvalds ret = dio->page_errors; 5931da177e4SLinus Torvalds if (ret == 0) { 594eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file >= sdio->final_block_in_request); 595eb28be2bSAndi Kleen fs_startblk = sdio->block_in_file >> sdio->blkfactor; 596ae55e1aaSTao Ma fs_endblk = (sdio->final_block_in_request - 1) >> 597ae55e1aaSTao Ma sdio->blkfactor; 598ae55e1aaSTao Ma fs_count = fs_endblk - fs_startblk + 1; 5991da177e4SLinus Torvalds 6003c674e74SNathan Scott map_bh->b_state = 0; 6013c674e74SNathan Scott map_bh->b_size = fs_count << dio->inode->i_blkbits; 6023c674e74SNathan Scott 6035fe878aeSChristoph Hellwig /* 6045fe878aeSChristoph Hellwig * For writes inside i_size on a DIO_SKIP_HOLES filesystem we 6055fe878aeSChristoph Hellwig * forbid block creations: only overwrites are permitted. 6065fe878aeSChristoph Hellwig * We will return early to the caller once we see an 6075fe878aeSChristoph Hellwig * unmapped buffer head returned, and the caller will fall 6085fe878aeSChristoph Hellwig * back to buffered I/O. 6095fe878aeSChristoph Hellwig * 6105fe878aeSChristoph Hellwig * Otherwise the decision is left to the get_blocks method, 6115fe878aeSChristoph Hellwig * which may decide to handle it or also return an unmapped 6125fe878aeSChristoph Hellwig * buffer head. 6135fe878aeSChristoph Hellwig */ 614b31dc66aSJens Axboe create = dio->rw & WRITE; 6155fe878aeSChristoph Hellwig if (dio->flags & DIO_SKIP_HOLES) { 616eb28be2bSAndi Kleen if (sdio->block_in_file < (i_size_read(dio->inode) >> 617eb28be2bSAndi Kleen sdio->blkbits)) 6181da177e4SLinus Torvalds create = 0; 6191da177e4SLinus Torvalds } 6203c674e74SNathan Scott 621eb28be2bSAndi Kleen ret = (*sdio->get_block)(dio->inode, fs_startblk, 6221da177e4SLinus Torvalds map_bh, create); 62318772641SAndi Kleen 62418772641SAndi Kleen /* Store for completion */ 62518772641SAndi Kleen dio->private = map_bh->b_private; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds return ret; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * There is no bio. Make one now. 6321da177e4SLinus Torvalds */ 633ba253fbfSAndi Kleen static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio, 63418772641SAndi Kleen sector_t start_sector, struct buffer_head *map_bh) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds sector_t sector; 6371da177e4SLinus Torvalds int ret, nr_pages; 6381da177e4SLinus Torvalds 639eb28be2bSAndi Kleen ret = dio_bio_reap(dio, sdio); 6401da177e4SLinus Torvalds if (ret) 6411da177e4SLinus Torvalds goto out; 642eb28be2bSAndi Kleen sector = start_sector << (sdio->blkbits - 9); 64318772641SAndi Kleen nr_pages = min(sdio->pages_in_io, bio_get_nr_vecs(map_bh->b_bdev)); 64420d9600cSDavid Dillow nr_pages = min(nr_pages, BIO_MAX_PAGES); 6451da177e4SLinus Torvalds BUG_ON(nr_pages <= 0); 64618772641SAndi Kleen dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages); 647eb28be2bSAndi Kleen sdio->boundary = 0; 6481da177e4SLinus Torvalds out: 6491da177e4SLinus Torvalds return ret; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* 6531da177e4SLinus Torvalds * Attempt to put the current chunk of 'cur_page' into the current BIO. If 6541da177e4SLinus Torvalds * that was successful then update final_block_in_bio and take a ref against 6551da177e4SLinus Torvalds * the just-added page. 6561da177e4SLinus Torvalds * 6571da177e4SLinus Torvalds * Return zero on success. Non-zero means the caller needs to start a new BIO. 6581da177e4SLinus Torvalds */ 659ba253fbfSAndi Kleen static inline int dio_bio_add_page(struct dio_submit *sdio) 6601da177e4SLinus Torvalds { 6611da177e4SLinus Torvalds int ret; 6621da177e4SLinus Torvalds 663eb28be2bSAndi Kleen ret = bio_add_page(sdio->bio, sdio->cur_page, 664eb28be2bSAndi Kleen sdio->cur_page_len, sdio->cur_page_offset); 665eb28be2bSAndi Kleen if (ret == sdio->cur_page_len) { 6661da177e4SLinus Torvalds /* 6671da177e4SLinus Torvalds * Decrement count only, if we are done with this page 6681da177e4SLinus Torvalds */ 669eb28be2bSAndi Kleen if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE) 670eb28be2bSAndi Kleen sdio->pages_in_io--; 671eb28be2bSAndi Kleen page_cache_get(sdio->cur_page); 672eb28be2bSAndi Kleen sdio->final_block_in_bio = sdio->cur_page_block + 673eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits); 6741da177e4SLinus Torvalds ret = 0; 6751da177e4SLinus Torvalds } else { 6761da177e4SLinus Torvalds ret = 1; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds return ret; 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* 6821da177e4SLinus Torvalds * Put cur_page under IO. The section of cur_page which is described by 6831da177e4SLinus Torvalds * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page 6841da177e4SLinus Torvalds * starts on-disk at cur_page_block. 6851da177e4SLinus Torvalds * 6861da177e4SLinus Torvalds * We take a ref against the page here (on behalf of its presence in the bio). 6871da177e4SLinus Torvalds * 6881da177e4SLinus Torvalds * The caller of this function is responsible for removing cur_page from the 6891da177e4SLinus Torvalds * dio, and for dropping the refcount which came from that presence. 6901da177e4SLinus Torvalds */ 691ba253fbfSAndi Kleen static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio, 69218772641SAndi Kleen struct buffer_head *map_bh) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds int ret = 0; 6951da177e4SLinus Torvalds 696eb28be2bSAndi Kleen if (sdio->bio) { 697eb28be2bSAndi Kleen loff_t cur_offset = sdio->cur_page_fs_offset; 698eb28be2bSAndi Kleen loff_t bio_next_offset = sdio->logical_offset_in_bio + 699eb28be2bSAndi Kleen sdio->bio->bi_size; 700c2c6ca41SJosef Bacik 7011da177e4SLinus Torvalds /* 702c2c6ca41SJosef Bacik * See whether this new request is contiguous with the old. 703c2c6ca41SJosef Bacik * 704f0940ceeSNamhyung Kim * Btrfs cannot handle having logically non-contiguous requests 705f0940ceeSNamhyung Kim * submitted. For example if you have 706c2c6ca41SJosef Bacik * 707c2c6ca41SJosef Bacik * Logical: [0-4095][HOLE][8192-12287] 708f0940ceeSNamhyung Kim * Physical: [0-4095] [4096-8191] 709c2c6ca41SJosef Bacik * 710c2c6ca41SJosef Bacik * We cannot submit those pages together as one BIO. So if our 711c2c6ca41SJosef Bacik * current logical offset in the file does not equal what would 712c2c6ca41SJosef Bacik * be the next logical offset in the bio, submit the bio we 713c2c6ca41SJosef Bacik * have. 7141da177e4SLinus Torvalds */ 715eb28be2bSAndi Kleen if (sdio->final_block_in_bio != sdio->cur_page_block || 716c2c6ca41SJosef Bacik cur_offset != bio_next_offset) 717eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * Submit now if the underlying fs is about to perform a 7201da177e4SLinus Torvalds * metadata read 7211da177e4SLinus Torvalds */ 722eb28be2bSAndi Kleen else if (sdio->boundary) 723eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 726eb28be2bSAndi Kleen if (sdio->bio == NULL) { 72718772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 7281da177e4SLinus Torvalds if (ret) 7291da177e4SLinus Torvalds goto out; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 732eb28be2bSAndi Kleen if (dio_bio_add_page(sdio) != 0) { 733eb28be2bSAndi Kleen dio_bio_submit(dio, sdio); 73418772641SAndi Kleen ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); 7351da177e4SLinus Torvalds if (ret == 0) { 736eb28be2bSAndi Kleen ret = dio_bio_add_page(sdio); 7371da177e4SLinus Torvalds BUG_ON(ret != 0); 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds out: 7411da177e4SLinus Torvalds return ret; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* 7451da177e4SLinus Torvalds * An autonomous function to put a chunk of a page under deferred IO. 7461da177e4SLinus Torvalds * 7471da177e4SLinus Torvalds * The caller doesn't actually know (or care) whether this piece of page is in 7481da177e4SLinus Torvalds * a BIO, or is under IO or whatever. We just take care of all possible 7491da177e4SLinus Torvalds * situations here. The separation between the logic of do_direct_IO() and 7501da177e4SLinus Torvalds * that of submit_page_section() is important for clarity. Please don't break. 7511da177e4SLinus Torvalds * 7521da177e4SLinus Torvalds * The chunk of page starts on-disk at blocknr. 7531da177e4SLinus Torvalds * 7541da177e4SLinus Torvalds * We perform deferred IO, by recording the last-submitted page inside our 7551da177e4SLinus Torvalds * private part of the dio structure. If possible, we just expand the IO 7561da177e4SLinus Torvalds * across that page here. 7571da177e4SLinus Torvalds * 7581da177e4SLinus Torvalds * If that doesn't work out then we put the old page into the bio and add this 7591da177e4SLinus Torvalds * page to the dio instead. 7601da177e4SLinus Torvalds */ 761ba253fbfSAndi Kleen static inline int 762eb28be2bSAndi Kleen submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page, 76318772641SAndi Kleen unsigned offset, unsigned len, sector_t blocknr, 76418772641SAndi Kleen struct buffer_head *map_bh) 7651da177e4SLinus Torvalds { 7661da177e4SLinus Torvalds int ret = 0; 7671da177e4SLinus Torvalds 76898c4d57dSAndrew Morton if (dio->rw & WRITE) { 76998c4d57dSAndrew Morton /* 77098c4d57dSAndrew Morton * Read accounting is performed in submit_bio() 77198c4d57dSAndrew Morton */ 77298c4d57dSAndrew Morton task_io_account_write(len); 77398c4d57dSAndrew Morton } 77498c4d57dSAndrew Morton 7751da177e4SLinus Torvalds /* 7761da177e4SLinus Torvalds * Can we just grow the current page's presence in the dio? 7771da177e4SLinus Torvalds */ 778eb28be2bSAndi Kleen if (sdio->cur_page == page && 779eb28be2bSAndi Kleen sdio->cur_page_offset + sdio->cur_page_len == offset && 780eb28be2bSAndi Kleen sdio->cur_page_block + 781eb28be2bSAndi Kleen (sdio->cur_page_len >> sdio->blkbits) == blocknr) { 782eb28be2bSAndi Kleen sdio->cur_page_len += len; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds /* 785eb28be2bSAndi Kleen * If sdio->boundary then we want to schedule the IO now to 7861da177e4SLinus Torvalds * avoid metadata seeks. 7871da177e4SLinus Torvalds */ 788eb28be2bSAndi Kleen if (sdio->boundary) { 78918772641SAndi Kleen ret = dio_send_cur_page(dio, sdio, map_bh); 790eb28be2bSAndi Kleen page_cache_release(sdio->cur_page); 791eb28be2bSAndi Kleen sdio->cur_page = NULL; 7921da177e4SLinus Torvalds } 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) 8041da177e4SLinus Torvalds goto out; 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: 8141da177e4SLinus Torvalds return ret; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds /* 8181da177e4SLinus Torvalds * Clean any dirty buffers in the blockdev mapping which alias newly-created 8191da177e4SLinus Torvalds * file blocks. Only called for S_ISREG files - blockdevs do not set 8201da177e4SLinus Torvalds * buffer_new 8211da177e4SLinus Torvalds */ 82218772641SAndi Kleen static void clean_blockdev_aliases(struct dio *dio, struct buffer_head *map_bh) 8231da177e4SLinus Torvalds { 8241da177e4SLinus Torvalds unsigned i; 8251da177e4SLinus Torvalds unsigned nblocks; 8261da177e4SLinus Torvalds 82718772641SAndi Kleen nblocks = map_bh->b_size >> dio->inode->i_blkbits; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds for (i = 0; i < nblocks; i++) { 83018772641SAndi Kleen unmap_underlying_metadata(map_bh->b_bdev, 83118772641SAndi Kleen map_bh->b_blocknr + i); 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* 8361da177e4SLinus Torvalds * If we are not writing the entire block and get_block() allocated 8371da177e4SLinus Torvalds * the block for us, we need to fill-in the unused portion of the 8381da177e4SLinus Torvalds * block with zeros. This happens only if user-buffer, fileoffset or 8391da177e4SLinus Torvalds * io length is not filesystem block-size multiple. 8401da177e4SLinus Torvalds * 8411da177e4SLinus Torvalds * `end' is zero if we're doing the start of the IO, 1 at the end of the 8421da177e4SLinus Torvalds * IO. 8431da177e4SLinus Torvalds */ 844ba253fbfSAndi Kleen static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio, 845ba253fbfSAndi Kleen int end, struct buffer_head *map_bh) 8461da177e4SLinus Torvalds { 8471da177e4SLinus Torvalds unsigned dio_blocks_per_fs_block; 8481da177e4SLinus Torvalds unsigned this_chunk_blocks; /* In dio_blocks */ 8491da177e4SLinus Torvalds unsigned this_chunk_bytes; 8501da177e4SLinus Torvalds struct page *page; 8511da177e4SLinus Torvalds 852eb28be2bSAndi Kleen sdio->start_zero_done = 1; 85318772641SAndi Kleen if (!sdio->blkfactor || !buffer_new(map_bh)) 8541da177e4SLinus Torvalds return; 8551da177e4SLinus Torvalds 856eb28be2bSAndi Kleen dio_blocks_per_fs_block = 1 << sdio->blkfactor; 857eb28be2bSAndi Kleen this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds if (!this_chunk_blocks) 8601da177e4SLinus Torvalds return; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds /* 8631da177e4SLinus Torvalds * We need to zero out part of an fs block. It is either at the 8641da177e4SLinus Torvalds * beginning or the end of the fs block. 8651da177e4SLinus Torvalds */ 8661da177e4SLinus Torvalds if (end) 8671da177e4SLinus Torvalds this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks; 8681da177e4SLinus Torvalds 869eb28be2bSAndi Kleen this_chunk_bytes = this_chunk_blocks << sdio->blkbits; 8701da177e4SLinus Torvalds 871557ed1faSNick Piggin page = ZERO_PAGE(0); 872eb28be2bSAndi Kleen if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes, 87318772641SAndi Kleen sdio->next_block_for_io, map_bh)) 8741da177e4SLinus Torvalds return; 8751da177e4SLinus Torvalds 876eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* 8801da177e4SLinus Torvalds * Walk the user pages, and the file, mapping blocks to disk and generating 8811da177e4SLinus Torvalds * a sequence of (page,offset,len,block) mappings. These mappings are injected 8821da177e4SLinus Torvalds * into submit_page_section(), which takes care of the next stage of submission 8831da177e4SLinus Torvalds * 8841da177e4SLinus Torvalds * Direct IO against a blockdev is different from a file. Because we can 8851da177e4SLinus Torvalds * happily perform page-sized but 512-byte aligned IOs. It is important that 8861da177e4SLinus Torvalds * blockdev IO be able to have fine alignment and large sizes. 8871da177e4SLinus Torvalds * 8881d8fa7a2SBadari Pulavarty * So what we do is to permit the ->get_block function to populate bh.b_size 8891da177e4SLinus Torvalds * with the size of IO which is permitted at this offset and this i_blkbits. 8901da177e4SLinus Torvalds * 8911da177e4SLinus Torvalds * For best results, the blockdev should be set up with 512-byte i_blkbits and 8921d8fa7a2SBadari Pulavarty * it should set b_size to PAGE_SIZE or more inside get_block(). This gives 8931da177e4SLinus Torvalds * fine alignment but still allows this function to work in PAGE_SIZE units. 8941da177e4SLinus Torvalds */ 89518772641SAndi Kleen static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, 89618772641SAndi Kleen struct buffer_head *map_bh) 8971da177e4SLinus Torvalds { 898eb28be2bSAndi Kleen const unsigned blkbits = sdio->blkbits; 8991da177e4SLinus Torvalds const unsigned blocks_per_page = PAGE_SIZE >> blkbits; 9001da177e4SLinus Torvalds struct page *page; 9011da177e4SLinus Torvalds unsigned block_in_page; 9021da177e4SLinus Torvalds int ret = 0; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds /* The I/O can start at any block offset within the first page */ 905eb28be2bSAndi Kleen block_in_page = sdio->first_block_in_page; 9061da177e4SLinus Torvalds 907eb28be2bSAndi Kleen while (sdio->block_in_file < sdio->final_block_in_request) { 908eb28be2bSAndi Kleen page = dio_get_page(dio, sdio); 9091da177e4SLinus Torvalds if (IS_ERR(page)) { 9101da177e4SLinus Torvalds ret = PTR_ERR(page); 9111da177e4SLinus Torvalds goto out; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds while (block_in_page < blocks_per_page) { 9151da177e4SLinus Torvalds unsigned offset_in_page = block_in_page << blkbits; 9161da177e4SLinus Torvalds unsigned this_chunk_bytes; /* # of bytes mapped */ 9171da177e4SLinus Torvalds unsigned this_chunk_blocks; /* # of blocks */ 9181da177e4SLinus Torvalds unsigned u; 9191da177e4SLinus Torvalds 920eb28be2bSAndi Kleen if (sdio->blocks_available == 0) { 9211da177e4SLinus Torvalds /* 9221da177e4SLinus Torvalds * Need to go and map some more disk 9231da177e4SLinus Torvalds */ 9241da177e4SLinus Torvalds unsigned long blkmask; 9251da177e4SLinus Torvalds unsigned long dio_remainder; 9261da177e4SLinus Torvalds 92718772641SAndi Kleen ret = get_more_blocks(dio, sdio, map_bh); 9281da177e4SLinus Torvalds if (ret) { 9291da177e4SLinus Torvalds page_cache_release(page); 9301da177e4SLinus Torvalds goto out; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) 9331da177e4SLinus Torvalds goto do_holes; 9341da177e4SLinus Torvalds 935eb28be2bSAndi Kleen sdio->blocks_available = 936eb28be2bSAndi Kleen map_bh->b_size >> sdio->blkbits; 937eb28be2bSAndi Kleen sdio->next_block_for_io = 938eb28be2bSAndi Kleen map_bh->b_blocknr << sdio->blkfactor; 9391da177e4SLinus Torvalds if (buffer_new(map_bh)) 94018772641SAndi Kleen clean_blockdev_aliases(dio, map_bh); 9411da177e4SLinus Torvalds 942eb28be2bSAndi Kleen if (!sdio->blkfactor) 9431da177e4SLinus Torvalds goto do_holes; 9441da177e4SLinus Torvalds 945eb28be2bSAndi Kleen blkmask = (1 << sdio->blkfactor) - 1; 946eb28be2bSAndi Kleen dio_remainder = (sdio->block_in_file & blkmask); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds /* 9491da177e4SLinus Torvalds * If we are at the start of IO and that IO 9501da177e4SLinus Torvalds * starts partway into a fs-block, 9511da177e4SLinus Torvalds * dio_remainder will be non-zero. If the IO 9521da177e4SLinus Torvalds * is a read then we can simply advance the IO 9531da177e4SLinus Torvalds * cursor to the first block which is to be 9541da177e4SLinus Torvalds * read. But if the IO is a write and the 9551da177e4SLinus Torvalds * block was newly allocated we cannot do that; 9561da177e4SLinus Torvalds * the start of the fs block must be zeroed out 9571da177e4SLinus Torvalds * on-disk 9581da177e4SLinus Torvalds */ 9591da177e4SLinus Torvalds if (!buffer_new(map_bh)) 960eb28be2bSAndi Kleen sdio->next_block_for_io += dio_remainder; 961eb28be2bSAndi Kleen sdio->blocks_available -= dio_remainder; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds do_holes: 9641da177e4SLinus Torvalds /* Handle holes */ 9651da177e4SLinus Torvalds if (!buffer_mapped(map_bh)) { 96635dc8161SJeff Moyer loff_t i_size_aligned; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds /* AKPM: eargh, -ENOTBLK is a hack */ 969b31dc66aSJens Axboe if (dio->rw & WRITE) { 9701da177e4SLinus Torvalds page_cache_release(page); 9711da177e4SLinus Torvalds return -ENOTBLK; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 97435dc8161SJeff Moyer /* 97535dc8161SJeff Moyer * Be sure to account for a partial block as the 97635dc8161SJeff Moyer * last block in the file 97735dc8161SJeff Moyer */ 97835dc8161SJeff Moyer i_size_aligned = ALIGN(i_size_read(dio->inode), 97935dc8161SJeff Moyer 1 << blkbits); 980eb28be2bSAndi Kleen if (sdio->block_in_file >= 98135dc8161SJeff Moyer i_size_aligned >> blkbits) { 9821da177e4SLinus Torvalds /* We hit eof */ 9831da177e4SLinus Torvalds page_cache_release(page); 9841da177e4SLinus Torvalds goto out; 9851da177e4SLinus Torvalds } 986eebd2aa3SChristoph Lameter zero_user(page, block_in_page << blkbits, 987eebd2aa3SChristoph Lameter 1 << blkbits); 988eb28be2bSAndi Kleen sdio->block_in_file++; 9891da177e4SLinus Torvalds block_in_page++; 9901da177e4SLinus Torvalds goto next_block; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* 9941da177e4SLinus Torvalds * If we're performing IO which has an alignment which 9951da177e4SLinus Torvalds * is finer than the underlying fs, go check to see if 9961da177e4SLinus Torvalds * we must zero out the start of this block. 9971da177e4SLinus Torvalds */ 998eb28be2bSAndi Kleen if (unlikely(sdio->blkfactor && !sdio->start_zero_done)) 99918772641SAndi Kleen dio_zero_block(dio, sdio, 0, map_bh); 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* 10021da177e4SLinus Torvalds * Work out, in this_chunk_blocks, how much disk we 10031da177e4SLinus Torvalds * can add to this page 10041da177e4SLinus Torvalds */ 1005eb28be2bSAndi Kleen this_chunk_blocks = sdio->blocks_available; 10061da177e4SLinus Torvalds u = (PAGE_SIZE - offset_in_page) >> blkbits; 10071da177e4SLinus Torvalds if (this_chunk_blocks > u) 10081da177e4SLinus Torvalds this_chunk_blocks = u; 1009eb28be2bSAndi Kleen u = sdio->final_block_in_request - sdio->block_in_file; 10101da177e4SLinus Torvalds if (this_chunk_blocks > u) 10111da177e4SLinus Torvalds this_chunk_blocks = u; 10121da177e4SLinus Torvalds this_chunk_bytes = this_chunk_blocks << blkbits; 10131da177e4SLinus Torvalds BUG_ON(this_chunk_bytes == 0); 10141da177e4SLinus Torvalds 1015eb28be2bSAndi Kleen sdio->boundary = buffer_boundary(map_bh); 1016eb28be2bSAndi Kleen ret = submit_page_section(dio, sdio, page, 1017eb28be2bSAndi Kleen offset_in_page, 1018eb28be2bSAndi Kleen this_chunk_bytes, 101918772641SAndi Kleen sdio->next_block_for_io, 102018772641SAndi Kleen map_bh); 10211da177e4SLinus Torvalds if (ret) { 10221da177e4SLinus Torvalds page_cache_release(page); 10231da177e4SLinus Torvalds goto out; 10241da177e4SLinus Torvalds } 1025eb28be2bSAndi Kleen sdio->next_block_for_io += this_chunk_blocks; 10261da177e4SLinus Torvalds 1027eb28be2bSAndi Kleen sdio->block_in_file += this_chunk_blocks; 10281da177e4SLinus Torvalds block_in_page += this_chunk_blocks; 1029eb28be2bSAndi Kleen sdio->blocks_available -= this_chunk_blocks; 10301da177e4SLinus Torvalds next_block: 1031eb28be2bSAndi Kleen BUG_ON(sdio->block_in_file > sdio->final_block_in_request); 1032eb28be2bSAndi Kleen if (sdio->block_in_file == sdio->final_block_in_request) 10331da177e4SLinus Torvalds break; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds /* Drop the ref which was taken in get_user_pages() */ 10371da177e4SLinus Torvalds page_cache_release(page); 10381da177e4SLinus Torvalds block_in_page = 0; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds out: 10411da177e4SLinus Torvalds return ret; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 1044847cc637SAndi Kleen static inline int drop_refcount(struct dio *dio) 10451da177e4SLinus Torvalds { 1046847cc637SAndi Kleen int ret2; 10475eb6c7a2SZach Brown unsigned long flags; 104820258b2bSZach Brown 10491da177e4SLinus Torvalds /* 10508459d86aSZach Brown * Sync will always be dropping the final ref and completing the 10515eb6c7a2SZach Brown * operation. AIO can if it was a broken operation described above or 10525eb6c7a2SZach Brown * in fact if all the bios race to complete before we get here. In 10535eb6c7a2SZach Brown * that case dio_complete() translates the EIOCBQUEUED into the proper 10545eb6c7a2SZach Brown * return code that the caller will hand to aio_complete(). 10555eb6c7a2SZach Brown * 10565eb6c7a2SZach Brown * This is managed by the bio_lock instead of being an atomic_t so that 10575eb6c7a2SZach Brown * completion paths can drop their ref and use the remaining count to 10585eb6c7a2SZach Brown * decide to wake the submission path atomically. 10591da177e4SLinus Torvalds */ 10605eb6c7a2SZach Brown spin_lock_irqsave(&dio->bio_lock, flags); 10615eb6c7a2SZach Brown ret2 = --dio->refcount; 10625eb6c7a2SZach Brown spin_unlock_irqrestore(&dio->bio_lock, flags); 1063847cc637SAndi Kleen return ret2; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 1066eafdc7d1SChristoph Hellwig /* 1067eafdc7d1SChristoph Hellwig * This is a library function for use by filesystem drivers. 1068eafdc7d1SChristoph Hellwig * 1069eafdc7d1SChristoph Hellwig * The locking rules are governed by the flags parameter: 1070eafdc7d1SChristoph Hellwig * - if the flags value contains DIO_LOCKING we use a fancy locking 1071eafdc7d1SChristoph Hellwig * scheme for dumb filesystems. 1072eafdc7d1SChristoph Hellwig * For writes this function is called under i_mutex and returns with 1073eafdc7d1SChristoph Hellwig * i_mutex held, for reads, i_mutex is not held on entry, but it is 1074eafdc7d1SChristoph Hellwig * taken and dropped again before returning. 1075eafdc7d1SChristoph Hellwig * - if the flags value does NOT contain DIO_LOCKING we don't use any 1076eafdc7d1SChristoph Hellwig * internal locking but rather rely on the filesystem to synchronize 1077eafdc7d1SChristoph Hellwig * direct I/O reads/writes versus each other and truncate. 1078df2d6f26SChristoph Hellwig * 1079df2d6f26SChristoph Hellwig * To help with locking against truncate we incremented the i_dio_count 1080df2d6f26SChristoph Hellwig * counter before starting direct I/O, and decrement it once we are done. 1081df2d6f26SChristoph Hellwig * Truncate can wait for it to reach zero to provide exclusion. It is 1082df2d6f26SChristoph Hellwig * expected that filesystem provide exclusion between new direct I/O 1083df2d6f26SChristoph Hellwig * and truncates. For DIO_LOCKING filesystems this is done by i_mutex, 1084df2d6f26SChristoph Hellwig * but other filesystems need to take care of this on their own. 1085ba253fbfSAndi Kleen * 1086ba253fbfSAndi Kleen * NOTE: if you pass "sdio" to anything by pointer make sure that function 1087ba253fbfSAndi Kleen * is always inlined. Otherwise gcc is unable to split the structure into 1088ba253fbfSAndi Kleen * individual fields and will generate much worse code. This is important 1089ba253fbfSAndi Kleen * for the whole file. 1090eafdc7d1SChristoph Hellwig */ 1091*65dd2aa9SAndi Kleen static inline ssize_t 1092*65dd2aa9SAndi Kleen do_blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode, 10931da177e4SLinus Torvalds struct block_device *bdev, const struct iovec *iov, loff_t offset, 10941d8fa7a2SBadari Pulavarty unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io, 1095facd07b0SJosef Bacik dio_submit_t submit_io, int flags) 10961da177e4SLinus Torvalds { 10971da177e4SLinus Torvalds int seg; 10981da177e4SLinus Torvalds size_t size; 10991da177e4SLinus Torvalds unsigned long addr; 11001da177e4SLinus Torvalds unsigned blkbits = inode->i_blkbits; 11011da177e4SLinus Torvalds unsigned blocksize_mask = (1 << blkbits) - 1; 11021da177e4SLinus Torvalds ssize_t retval = -EINVAL; 11031da177e4SLinus Torvalds loff_t end = offset; 11041da177e4SLinus Torvalds struct dio *dio; 1105eb28be2bSAndi Kleen struct dio_submit sdio = { 0, }; 1106847cc637SAndi Kleen unsigned long user_addr; 1107847cc637SAndi Kleen size_t bytes; 1108847cc637SAndi Kleen struct buffer_head map_bh = { 0, }; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds if (rw & WRITE) 1111721a9602SJens Axboe rw = WRITE_ODIRECT; 11121da177e4SLinus Torvalds 1113*65dd2aa9SAndi Kleen /* 1114*65dd2aa9SAndi Kleen * Avoid references to bdev if not absolutely needed to give 1115*65dd2aa9SAndi Kleen * the early prefetch in the caller enough time. 1116*65dd2aa9SAndi Kleen */ 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds if (offset & blocksize_mask) { 11191da177e4SLinus Torvalds if (bdev) 1120*65dd2aa9SAndi Kleen blkbits = blksize_bits(bdev_logical_block_size(bdev)); 11211da177e4SLinus Torvalds blocksize_mask = (1 << blkbits) - 1; 11221da177e4SLinus Torvalds if (offset & blocksize_mask) 11231da177e4SLinus Torvalds goto out; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds /* Check the memory alignment. Blocks cannot straddle pages */ 11271da177e4SLinus Torvalds for (seg = 0; seg < nr_segs; seg++) { 11281da177e4SLinus Torvalds addr = (unsigned long)iov[seg].iov_base; 11291da177e4SLinus Torvalds size = iov[seg].iov_len; 11301da177e4SLinus Torvalds end += size; 1131*65dd2aa9SAndi Kleen if (unlikely((addr & blocksize_mask) || 1132*65dd2aa9SAndi Kleen (size & blocksize_mask))) { 11331da177e4SLinus Torvalds if (bdev) 1134*65dd2aa9SAndi Kleen blkbits = blksize_bits( 1135*65dd2aa9SAndi Kleen bdev_logical_block_size(bdev)); 11361da177e4SLinus Torvalds blocksize_mask = (1 << blkbits) - 1; 11371da177e4SLinus Torvalds if ((addr & blocksize_mask) || (size & blocksize_mask)) 11381da177e4SLinus Torvalds goto out; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 1142f9b5570dSChristoph Hellwig /* watch out for a 0 len io from a tricksy fs */ 1143f9b5570dSChristoph Hellwig if (rw == READ && end == offset) 1144f9b5570dSChristoph Hellwig return 0; 1145f9b5570dSChristoph Hellwig 11466e8267f5SAndi Kleen dio = kmem_cache_alloc(dio_cache, GFP_KERNEL); 11471da177e4SLinus Torvalds retval = -ENOMEM; 11481da177e4SLinus Torvalds if (!dio) 11491da177e4SLinus Torvalds goto out; 115023aee091SJeff Moyer /* 115123aee091SJeff Moyer * Believe it or not, zeroing out the page array caused a .5% 115223aee091SJeff Moyer * performance regression in a database benchmark. So, we take 115323aee091SJeff Moyer * care to only zero out what's needed. 115423aee091SJeff Moyer */ 115523aee091SJeff Moyer memset(dio, 0, offsetof(struct dio, pages)); 11561da177e4SLinus Torvalds 11575fe878aeSChristoph Hellwig dio->flags = flags; 11585fe878aeSChristoph Hellwig if (dio->flags & DIO_LOCKING) { 1159f9b5570dSChristoph Hellwig if (rw == READ) { 11605fe878aeSChristoph Hellwig struct address_space *mapping = 11615fe878aeSChristoph Hellwig iocb->ki_filp->f_mapping; 11621da177e4SLinus Torvalds 11635fe878aeSChristoph Hellwig /* will be released by direct_io_worker */ 11641b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds retval = filemap_write_and_wait_range(mapping, offset, 11671da177e4SLinus Torvalds end - 1); 11681da177e4SLinus Torvalds if (retval) { 11695fe878aeSChristoph Hellwig mutex_unlock(&inode->i_mutex); 11706e8267f5SAndi Kleen kmem_cache_free(dio_cache, dio); 11711da177e4SLinus Torvalds goto out; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds } 1174df2d6f26SChristoph Hellwig } 11751da177e4SLinus Torvalds 11765fe878aeSChristoph Hellwig /* 1177bd5fe6c5SChristoph Hellwig * Will be decremented at I/O completion time. 11785fe878aeSChristoph Hellwig */ 1179bd5fe6c5SChristoph Hellwig atomic_inc(&inode->i_dio_count); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* 11821da177e4SLinus Torvalds * For file extending writes updating i_size before data 11831da177e4SLinus Torvalds * writeouts complete can expose uninitialized blocks. So 11841da177e4SLinus Torvalds * even for AIO, we need to wait for i/o to complete before 11851da177e4SLinus Torvalds * returning in this case. 11861da177e4SLinus Torvalds */ 1187b31dc66aSJens Axboe dio->is_async = !is_sync_kiocb(iocb) && !((rw & WRITE) && 11881da177e4SLinus Torvalds (end > i_size_read(inode))); 11891da177e4SLinus Torvalds 1190847cc637SAndi Kleen retval = 0; 1191847cc637SAndi Kleen 1192847cc637SAndi Kleen dio->inode = inode; 1193847cc637SAndi Kleen dio->rw = rw; 1194847cc637SAndi Kleen sdio.blkbits = blkbits; 1195847cc637SAndi Kleen sdio.blkfactor = inode->i_blkbits - blkbits; 1196847cc637SAndi Kleen sdio.block_in_file = offset >> blkbits; 1197847cc637SAndi Kleen 1198847cc637SAndi Kleen sdio.get_block = get_block; 1199847cc637SAndi Kleen dio->end_io = end_io; 1200847cc637SAndi Kleen sdio.submit_io = submit_io; 1201847cc637SAndi Kleen sdio.final_block_in_bio = -1; 1202847cc637SAndi Kleen sdio.next_block_for_io = -1; 1203847cc637SAndi Kleen 1204847cc637SAndi Kleen dio->iocb = iocb; 1205847cc637SAndi Kleen dio->i_size = i_size_read(inode); 1206847cc637SAndi Kleen 1207847cc637SAndi Kleen spin_lock_init(&dio->bio_lock); 1208847cc637SAndi Kleen dio->refcount = 1; 1209847cc637SAndi Kleen 1210847cc637SAndi Kleen /* 1211847cc637SAndi Kleen * In case of non-aligned buffers, we may need 2 more 1212847cc637SAndi Kleen * pages since we need to zero out first and last block. 1213847cc637SAndi Kleen */ 1214847cc637SAndi Kleen if (unlikely(sdio.blkfactor)) 1215847cc637SAndi Kleen sdio.pages_in_io = 2; 1216847cc637SAndi Kleen 1217847cc637SAndi Kleen for (seg = 0; seg < nr_segs; seg++) { 1218847cc637SAndi Kleen user_addr = (unsigned long)iov[seg].iov_base; 1219847cc637SAndi Kleen sdio.pages_in_io += 1220847cc637SAndi Kleen ((user_addr + iov[seg].iov_len + PAGE_SIZE-1) / 1221847cc637SAndi Kleen PAGE_SIZE - user_addr / PAGE_SIZE); 1222847cc637SAndi Kleen } 1223847cc637SAndi Kleen 1224847cc637SAndi Kleen for (seg = 0; seg < nr_segs; seg++) { 1225847cc637SAndi Kleen user_addr = (unsigned long)iov[seg].iov_base; 1226847cc637SAndi Kleen sdio.size += bytes = iov[seg].iov_len; 1227847cc637SAndi Kleen 1228847cc637SAndi Kleen /* Index into the first page of the first block */ 1229847cc637SAndi Kleen sdio.first_block_in_page = (user_addr & ~PAGE_MASK) >> blkbits; 1230847cc637SAndi Kleen sdio.final_block_in_request = sdio.block_in_file + 1231847cc637SAndi Kleen (bytes >> blkbits); 1232847cc637SAndi Kleen /* Page fetching state */ 1233847cc637SAndi Kleen sdio.head = 0; 1234847cc637SAndi Kleen sdio.tail = 0; 1235847cc637SAndi Kleen sdio.curr_page = 0; 1236847cc637SAndi Kleen 1237847cc637SAndi Kleen sdio.total_pages = 0; 1238847cc637SAndi Kleen if (user_addr & (PAGE_SIZE-1)) { 1239847cc637SAndi Kleen sdio.total_pages++; 1240847cc637SAndi Kleen bytes -= PAGE_SIZE - (user_addr & (PAGE_SIZE - 1)); 1241847cc637SAndi Kleen } 1242847cc637SAndi Kleen sdio.total_pages += (bytes + PAGE_SIZE - 1) / PAGE_SIZE; 1243847cc637SAndi Kleen sdio.curr_user_address = user_addr; 1244847cc637SAndi Kleen 1245847cc637SAndi Kleen retval = do_direct_IO(dio, &sdio, &map_bh); 1246847cc637SAndi Kleen 1247847cc637SAndi Kleen dio->result += iov[seg].iov_len - 1248847cc637SAndi Kleen ((sdio.final_block_in_request - sdio.block_in_file) << 1249847cc637SAndi Kleen blkbits); 1250847cc637SAndi Kleen 1251847cc637SAndi Kleen if (retval) { 1252847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1253847cc637SAndi Kleen break; 1254847cc637SAndi Kleen } 1255847cc637SAndi Kleen } /* end iovec loop */ 1256847cc637SAndi Kleen 1257847cc637SAndi Kleen if (retval == -ENOTBLK) { 1258847cc637SAndi Kleen /* 1259847cc637SAndi Kleen * The remaining part of the request will be 1260847cc637SAndi Kleen * be handled by buffered I/O when we return 1261847cc637SAndi Kleen */ 1262847cc637SAndi Kleen retval = 0; 1263847cc637SAndi Kleen } 1264847cc637SAndi Kleen /* 1265847cc637SAndi Kleen * There may be some unwritten disk at the end of a part-written 1266847cc637SAndi Kleen * fs-block-sized block. Go zero that now. 1267847cc637SAndi Kleen */ 1268847cc637SAndi Kleen dio_zero_block(dio, &sdio, 1, &map_bh); 1269847cc637SAndi Kleen 1270847cc637SAndi Kleen if (sdio.cur_page) { 1271847cc637SAndi Kleen ssize_t ret2; 1272847cc637SAndi Kleen 1273847cc637SAndi Kleen ret2 = dio_send_cur_page(dio, &sdio, &map_bh); 1274847cc637SAndi Kleen if (retval == 0) 1275847cc637SAndi Kleen retval = ret2; 1276847cc637SAndi Kleen page_cache_release(sdio.cur_page); 1277847cc637SAndi Kleen sdio.cur_page = NULL; 1278847cc637SAndi Kleen } 1279847cc637SAndi Kleen if (sdio.bio) 1280847cc637SAndi Kleen dio_bio_submit(dio, &sdio); 1281847cc637SAndi Kleen 1282847cc637SAndi Kleen /* 1283847cc637SAndi Kleen * It is possible that, we return short IO due to end of file. 1284847cc637SAndi Kleen * In that case, we need to release all the pages we got hold on. 1285847cc637SAndi Kleen */ 1286847cc637SAndi Kleen dio_cleanup(dio, &sdio); 1287847cc637SAndi Kleen 1288847cc637SAndi Kleen /* 1289847cc637SAndi Kleen * All block lookups have been performed. For READ requests 1290847cc637SAndi Kleen * we can let i_mutex go now that its achieved its purpose 1291847cc637SAndi Kleen * of protecting us from looking up uninitialized blocks. 1292847cc637SAndi Kleen */ 1293847cc637SAndi Kleen if (rw == READ && (dio->flags & DIO_LOCKING)) 1294847cc637SAndi Kleen mutex_unlock(&dio->inode->i_mutex); 1295847cc637SAndi Kleen 1296847cc637SAndi Kleen /* 1297847cc637SAndi Kleen * The only time we want to leave bios in flight is when a successful 1298847cc637SAndi Kleen * partial aio read or full aio write have been setup. In that case 1299847cc637SAndi Kleen * bio completion will call aio_complete. The only time it's safe to 1300847cc637SAndi Kleen * call aio_complete is when we return -EIOCBQUEUED, so we key on that. 1301847cc637SAndi Kleen * This had *better* be the only place that raises -EIOCBQUEUED. 1302847cc637SAndi Kleen */ 1303847cc637SAndi Kleen BUG_ON(retval == -EIOCBQUEUED); 1304847cc637SAndi Kleen if (dio->is_async && retval == 0 && dio->result && 1305847cc637SAndi Kleen ((rw & READ) || (dio->result == sdio.size))) 1306847cc637SAndi Kleen retval = -EIOCBQUEUED; 1307847cc637SAndi Kleen 1308847cc637SAndi Kleen if (retval != -EIOCBQUEUED) 1309847cc637SAndi Kleen dio_await_completion(dio); 1310847cc637SAndi Kleen 1311847cc637SAndi Kleen if (drop_refcount(dio) == 0) { 1312847cc637SAndi Kleen retval = dio_complete(dio, offset, retval, false); 1313847cc637SAndi Kleen kmem_cache_free(dio_cache, dio); 1314847cc637SAndi Kleen } else 1315847cc637SAndi Kleen BUG_ON(retval != -EIOCBQUEUED); 13161da177e4SLinus Torvalds 13177bb46a67Snpiggin@suse.de out: 13187bb46a67Snpiggin@suse.de return retval; 13197bb46a67Snpiggin@suse.de } 1320*65dd2aa9SAndi Kleen 1321*65dd2aa9SAndi Kleen ssize_t 1322*65dd2aa9SAndi Kleen __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode, 1323*65dd2aa9SAndi Kleen struct block_device *bdev, const struct iovec *iov, loff_t offset, 1324*65dd2aa9SAndi Kleen unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io, 1325*65dd2aa9SAndi Kleen dio_submit_t submit_io, int flags) 1326*65dd2aa9SAndi Kleen { 1327*65dd2aa9SAndi Kleen /* 1328*65dd2aa9SAndi Kleen * The block device state is needed in the end to finally 1329*65dd2aa9SAndi Kleen * submit everything. Since it's likely to be cache cold 1330*65dd2aa9SAndi Kleen * prefetch it here as first thing to hide some of the 1331*65dd2aa9SAndi Kleen * latency. 1332*65dd2aa9SAndi Kleen * 1333*65dd2aa9SAndi Kleen * Attempt to prefetch the pieces we likely need later. 1334*65dd2aa9SAndi Kleen */ 1335*65dd2aa9SAndi Kleen prefetch(&bdev->bd_disk->part_tbl); 1336*65dd2aa9SAndi Kleen prefetch(bdev->bd_queue); 1337*65dd2aa9SAndi Kleen prefetch((char *)bdev->bd_queue + SMP_CACHE_BYTES); 1338*65dd2aa9SAndi Kleen 1339*65dd2aa9SAndi Kleen return do_blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset, 1340*65dd2aa9SAndi Kleen nr_segs, get_block, end_io, 1341*65dd2aa9SAndi Kleen submit_io, flags); 1342*65dd2aa9SAndi Kleen } 1343*65dd2aa9SAndi Kleen 13441da177e4SLinus Torvalds EXPORT_SYMBOL(__blockdev_direct_IO); 13456e8267f5SAndi Kleen 13466e8267f5SAndi Kleen static __init int dio_init(void) 13476e8267f5SAndi Kleen { 13486e8267f5SAndi Kleen dio_cache = KMEM_CACHE(dio, SLAB_PANIC); 13496e8267f5SAndi Kleen return 0; 13506e8267f5SAndi Kleen } 13516e8267f5SAndi Kleen module_init(dio_init) 1352