Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/direct-io.c |
| 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds. |
| 5 | * |
| 6 | * O_DIRECT |
| 7 | * |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 8 | * 04Jul2002 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Initial version |
| 10 | * 11Sep2002 janetinc@us.ibm.com |
| 11 | * added readv/writev support. |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 12 | * 29Oct2002 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * rewrote bio_add_page() support. |
| 14 | * 30Oct2002 pbadari@us.ibm.com |
| 15 | * added support for non-aligned IO. |
| 16 | * 06Nov2002 pbadari@us.ibm.com |
| 17 | * added asynchronous IO support. |
| 18 | * 21Jul2003 nathans@sgi.com |
| 19 | * added IO completion notifier. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/highmem.h> |
| 29 | #include <linux/pagemap.h> |
Andrew Morton | 98c4d57 | 2006-12-10 02:19:47 -0800 | [diff] [blame] | 30 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/bio.h> |
| 32 | #include <linux/wait.h> |
| 33 | #include <linux/err.h> |
| 34 | #include <linux/blkdev.h> |
| 35 | #include <linux/buffer_head.h> |
| 36 | #include <linux/rwsem.h> |
| 37 | #include <linux/uio.h> |
| 38 | #include <asm/atomic.h> |
| 39 | |
| 40 | /* |
| 41 | * How many user pages to map in one call to get_user_pages(). This determines |
| 42 | * the size of a structure on the stack. |
| 43 | */ |
| 44 | #define DIO_PAGES 64 |
| 45 | |
| 46 | /* |
| 47 | * This code generally works in units of "dio_blocks". A dio_block is |
| 48 | * somewhere between the hard sector size and the filesystem block size. it |
| 49 | * is determined on a per-invocation basis. When talking to the filesystem |
| 50 | * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity |
| 51 | * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted |
| 52 | * to bio_block quantities by shifting left by blkfactor. |
| 53 | * |
| 54 | * If blkfactor is zero then the user's request was aligned to the filesystem's |
| 55 | * blocksize. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
| 57 | |
| 58 | struct dio { |
| 59 | /* BIO submission state */ |
| 60 | struct bio *bio; /* bio under assembly */ |
| 61 | struct inode *inode; |
| 62 | int rw; |
Daniel McNeil | 29504ff | 2005-04-16 15:25:50 -0700 | [diff] [blame] | 63 | loff_t i_size; /* i_size when submitted */ |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 64 | int flags; /* doesn't change */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | unsigned blkbits; /* doesn't change */ |
| 66 | unsigned blkfactor; /* When we're using an alignment which |
| 67 | is finer than the filesystem's soft |
| 68 | blocksize, this specifies how much |
| 69 | finer. blkfactor=2 means 1/4-block |
| 70 | alignment. Does not change */ |
| 71 | unsigned start_zero_done; /* flag: sub-blocksize zeroing has |
| 72 | been performed at the start of a |
| 73 | write */ |
| 74 | int pages_in_io; /* approximate total IO pages */ |
| 75 | size_t size; /* total request size (doesn't change)*/ |
| 76 | sector_t block_in_file; /* Current offset into the underlying |
| 77 | file in dio_block units. */ |
| 78 | unsigned blocks_available; /* At block_in_file. changes */ |
| 79 | sector_t final_block_in_request;/* doesn't change */ |
| 80 | unsigned first_block_in_page; /* doesn't change, Used only once */ |
| 81 | int boundary; /* prev block is at a boundary */ |
| 82 | int reap_counter; /* rate limit reaping */ |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 83 | get_block_t *get_block; /* block mapping function */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | dio_iodone_t *end_io; /* IO completion function */ |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 85 | dio_submit_t *submit_io; /* IO submition function */ |
| 86 | loff_t logical_offset_in_bio; /* current first logical block in bio */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | sector_t final_block_in_bio; /* current final block in bio + 1 */ |
| 88 | sector_t next_block_for_io; /* next block to be put under IO, |
| 89 | in dio_blocks units */ |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 90 | struct buffer_head map_bh; /* last get_block() result */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Deferred addition of a page to the dio. These variables are |
| 94 | * private to dio_send_cur_page(), submit_page_section() and |
| 95 | * dio_bio_add_page(). |
| 96 | */ |
| 97 | struct page *cur_page; /* The page */ |
| 98 | unsigned cur_page_offset; /* Offset into it, in bytes */ |
| 99 | unsigned cur_page_len; /* Nr of bytes at cur_page_offset */ |
| 100 | sector_t cur_page_block; /* Where it starts */ |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 101 | loff_t cur_page_fs_offset; /* Offset in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* BIO completion state */ |
| 104 | spinlock_t bio_lock; /* protects BIO fields below */ |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 105 | unsigned long refcount; /* direct_io_worker() and bios */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | struct bio *bio_list; /* singly linked via bi_private */ |
| 107 | struct task_struct *waiter; /* waiting task (NULL if none) */ |
| 108 | |
| 109 | /* AIO related stuff */ |
| 110 | struct kiocb *iocb; /* kiocb */ |
| 111 | int is_async; /* is IO async ? */ |
Chen, Kenneth W | 174e27c | 2006-03-25 03:08:16 -0800 | [diff] [blame] | 112 | int io_error; /* IO error in completion path */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | ssize_t result; /* IO result */ |
Jeff Moyer | 23aee09 | 2009-12-15 16:47:49 -0800 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Page fetching state. These variables belong to dio_refill_pages(). |
| 117 | */ |
| 118 | int curr_page; /* changes */ |
| 119 | int total_pages; /* doesn't change */ |
| 120 | unsigned long curr_user_address;/* changes */ |
| 121 | |
| 122 | /* |
| 123 | * Page queue. These variables belong to dio_refill_pages() and |
| 124 | * dio_get_page(). |
| 125 | */ |
| 126 | unsigned head; /* next page to process */ |
| 127 | unsigned tail; /* last valid page + 1 */ |
| 128 | int page_errors; /* errno from get_user_pages() */ |
| 129 | |
| 130 | /* |
| 131 | * pages[] (and any fields placed after it) are not zeroed out at |
| 132 | * allocation time. Don't add new fields after pages[] unless you |
| 133 | * wish that they not be zeroed. |
| 134 | */ |
| 135 | struct page *pages[DIO_PAGES]; /* page buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /* |
| 139 | * How many pages are in the queue? |
| 140 | */ |
| 141 | static inline unsigned dio_pages_present(struct dio *dio) |
| 142 | { |
| 143 | return dio->tail - dio->head; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Go grab and pin some userspace pages. Typically we'll get 64 at a time. |
| 148 | */ |
| 149 | static int dio_refill_pages(struct dio *dio) |
| 150 | { |
| 151 | int ret; |
| 152 | int nr_pages; |
| 153 | |
| 154 | nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES); |
Nick Piggin | f5dd33c | 2008-07-25 19:45:25 -0700 | [diff] [blame] | 155 | ret = get_user_pages_fast( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | dio->curr_user_address, /* Where from? */ |
| 157 | nr_pages, /* How many pages? */ |
| 158 | dio->rw == READ, /* Write to memory? */ |
Nick Piggin | f5dd33c | 2008-07-25 19:45:25 -0700 | [diff] [blame] | 159 | &dio->pages[0]); /* Put results here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Jens Axboe | b31dc66 | 2006-06-13 08:26:10 +0200 | [diff] [blame] | 161 | if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) { |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 162 | struct page *page = ZERO_PAGE(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* |
| 164 | * A memory fault, but the filesystem has some outstanding |
| 165 | * mapped blocks. We need to use those blocks up to avoid |
| 166 | * leaking stale data in the file. |
| 167 | */ |
| 168 | if (dio->page_errors == 0) |
| 169 | dio->page_errors = ret; |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 170 | page_cache_get(page); |
| 171 | dio->pages[0] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | dio->head = 0; |
| 173 | dio->tail = 1; |
| 174 | ret = 0; |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | if (ret >= 0) { |
| 179 | dio->curr_user_address += ret * PAGE_SIZE; |
| 180 | dio->curr_page += ret; |
| 181 | dio->head = 0; |
| 182 | dio->tail = ret; |
| 183 | ret = 0; |
| 184 | } |
| 185 | out: |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Get another userspace page. Returns an ERR_PTR on error. Pages are |
| 191 | * buffered inside the dio so that we can call get_user_pages() against a |
| 192 | * decent number of pages, less frequently. To provide nicer use of the |
| 193 | * L1 cache. |
| 194 | */ |
| 195 | static struct page *dio_get_page(struct dio *dio) |
| 196 | { |
| 197 | if (dio_pages_present(dio) == 0) { |
| 198 | int ret; |
| 199 | |
| 200 | ret = dio_refill_pages(dio); |
| 201 | if (ret) |
| 202 | return ERR_PTR(ret); |
| 203 | BUG_ON(dio_pages_present(dio) == 0); |
| 204 | } |
| 205 | return dio->pages[dio->head++]; |
| 206 | } |
| 207 | |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 208 | /** |
| 209 | * dio_complete() - called when all DIO BIO I/O has been completed |
| 210 | * @offset: the byte offset in the file of the completed operation |
| 211 | * |
| 212 | * This releases locks as dictated by the locking type, lets interested parties |
| 213 | * know that a DIO operation has completed, and calculates the resulting return |
| 214 | * code for the operation. |
| 215 | * |
| 216 | * It lets the filesystem know if it registered an interest earlier via |
| 217 | * get_block. Pass the private field of the map buffer_head so that |
| 218 | * filesystems can use it to hold additional state between get_block calls and |
| 219 | * dio_complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | */ |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 221 | static int dio_complete(struct dio *dio, loff_t offset, int ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 223 | ssize_t transferred = 0; |
| 224 | |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 225 | /* |
| 226 | * AIO submission can race with bio completion to get here while |
| 227 | * expecting to have the last io completed by bio completion. |
| 228 | * In that case -EIOCBQUEUED is in fact not an error we want |
| 229 | * to preserve through this call. |
| 230 | */ |
| 231 | if (ret == -EIOCBQUEUED) |
| 232 | ret = 0; |
| 233 | |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 234 | if (dio->result) { |
| 235 | transferred = dio->result; |
| 236 | |
| 237 | /* Check for short read case */ |
| 238 | if ((dio->rw == READ) && ((offset + transferred) > dio->i_size)) |
| 239 | transferred = dio->i_size - offset; |
| 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (dio->end_io && dio->result) |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 243 | dio->end_io(dio->iocb, offset, transferred, |
| 244 | dio->map_bh.b_private); |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 245 | |
| 246 | if (dio->flags & DIO_LOCKING) |
Ingo Molnar | d8aa905 | 2006-07-03 00:25:02 -0700 | [diff] [blame] | 247 | /* lockdep: non-owner release */ |
| 248 | up_read_non_owner(&dio->inode->i_alloc_sem); |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 249 | |
| 250 | if (ret == 0) |
| 251 | ret = dio->page_errors; |
| 252 | if (ret == 0) |
| 253 | ret = dio->io_error; |
| 254 | if (ret == 0) |
| 255 | ret = transferred; |
| 256 | |
| 257 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | static int dio_bio_complete(struct dio *dio, struct bio *bio); |
| 261 | /* |
| 262 | * Asynchronous IO callback. |
| 263 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 264 | static void dio_bio_end_aio(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | struct dio *dio = bio->bi_private; |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 267 | unsigned long remaining; |
| 268 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* cleanup the bio */ |
| 271 | dio_bio_complete(dio, bio); |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 272 | |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 273 | spin_lock_irqsave(&dio->bio_lock, flags); |
| 274 | remaining = --dio->refcount; |
| 275 | if (remaining == 1 && dio->waiter) |
Zach Brown | 20258b2 | 2006-12-10 02:21:01 -0800 | [diff] [blame] | 276 | wake_up_process(dio->waiter); |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 277 | spin_unlock_irqrestore(&dio->bio_lock, flags); |
Zach Brown | 20258b2 | 2006-12-10 02:21:01 -0800 | [diff] [blame] | 278 | |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 279 | if (remaining == 0) { |
| 280 | int ret = dio_complete(dio, dio->iocb->ki_pos, 0); |
| 281 | aio_complete(dio->iocb, ret, 0); |
| 282 | kfree(dio); |
| 283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* |
| 287 | * The BIO completion handler simply queues the BIO up for the process-context |
| 288 | * handler. |
| 289 | * |
| 290 | * During I/O bi_private points at the dio. After I/O, bi_private is used to |
| 291 | * implement a singly-linked list of completed BIOs, at dio->bio_list. |
| 292 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 293 | static void dio_bio_end_io(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
| 295 | struct dio *dio = bio->bi_private; |
| 296 | unsigned long flags; |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | spin_lock_irqsave(&dio->bio_lock, flags); |
| 299 | bio->bi_private = dio->bio_list; |
| 300 | dio->bio_list = bio; |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 301 | if (--dio->refcount == 1 && dio->waiter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | wake_up_process(dio->waiter); |
| 303 | spin_unlock_irqrestore(&dio->bio_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 306 | /** |
| 307 | * dio_end_io - handle the end io action for the given bio |
| 308 | * @bio: The direct io bio thats being completed |
| 309 | * @error: Error if there was one |
| 310 | * |
| 311 | * This is meant to be called by any filesystem that uses their own dio_submit_t |
| 312 | * so that the DIO specific endio actions are dealt with after the filesystem |
| 313 | * has done it's completion work. |
| 314 | */ |
| 315 | void dio_end_io(struct bio *bio, int error) |
| 316 | { |
| 317 | struct dio *dio = bio->bi_private; |
| 318 | |
| 319 | if (dio->is_async) |
| 320 | dio_bio_end_aio(bio, error); |
| 321 | else |
| 322 | dio_bio_end_io(bio, error); |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(dio_end_io); |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | static int |
| 327 | dio_bio_alloc(struct dio *dio, struct block_device *bdev, |
| 328 | sector_t first_sector, int nr_vecs) |
| 329 | { |
| 330 | struct bio *bio; |
| 331 | |
| 332 | bio = bio_alloc(GFP_KERNEL, nr_vecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | bio->bi_bdev = bdev; |
| 335 | bio->bi_sector = first_sector; |
| 336 | if (dio->is_async) |
| 337 | bio->bi_end_io = dio_bio_end_aio; |
| 338 | else |
| 339 | bio->bi_end_io = dio_bio_end_io; |
| 340 | |
| 341 | dio->bio = bio; |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 342 | dio->logical_offset_in_bio = dio->cur_page_fs_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * In the AIO read case we speculatively dirty the pages before starting IO. |
| 348 | * During IO completion, any of these pages which happen to have been written |
| 349 | * back will be redirtied by bio_check_pages_dirty(). |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 350 | * |
| 351 | * bios hold a dio reference between submit_bio and ->end_io. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | */ |
| 353 | static void dio_bio_submit(struct dio *dio) |
| 354 | { |
| 355 | struct bio *bio = dio->bio; |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 356 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | bio->bi_private = dio; |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 359 | |
| 360 | spin_lock_irqsave(&dio->bio_lock, flags); |
| 361 | dio->refcount++; |
| 362 | spin_unlock_irqrestore(&dio->bio_lock, flags); |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (dio->is_async && dio->rw == READ) |
| 365 | bio_set_pages_dirty(bio); |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 366 | |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 367 | if (dio->submit_io) |
| 368 | dio->submit_io(dio->rw, bio, dio->inode, |
| 369 | dio->logical_offset_in_bio); |
| 370 | else |
| 371 | submit_bio(dio->rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | dio->bio = NULL; |
| 374 | dio->boundary = 0; |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 375 | dio->logical_offset_in_bio = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Release any resources in case of a failure |
| 380 | */ |
| 381 | static void dio_cleanup(struct dio *dio) |
| 382 | { |
| 383 | while (dio_pages_present(dio)) |
| 384 | page_cache_release(dio_get_page(dio)); |
| 385 | } |
| 386 | |
| 387 | /* |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 388 | * Wait for the next BIO to complete. Remove it and return it. NULL is |
| 389 | * returned once all BIOs have been completed. This must only be called once |
| 390 | * all bios have been issued so that dio->refcount can only decrease. This |
| 391 | * requires that that the caller hold a reference on the dio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | */ |
| 393 | static struct bio *dio_await_one(struct dio *dio) |
| 394 | { |
| 395 | unsigned long flags; |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 396 | struct bio *bio = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
| 398 | spin_lock_irqsave(&dio->bio_lock, flags); |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * Wait as long as the list is empty and there are bios in flight. bio |
| 402 | * completion drops the count, maybe adds to the list, and wakes while |
| 403 | * holding the bio_lock so we don't need set_current_state()'s barrier |
| 404 | * and can call it after testing our condition. |
| 405 | */ |
| 406 | while (dio->refcount > 1 && dio->bio_list == NULL) { |
| 407 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 408 | dio->waiter = current; |
| 409 | spin_unlock_irqrestore(&dio->bio_lock, flags); |
| 410 | io_schedule(); |
| 411 | /* wake up sets us TASK_RUNNING */ |
| 412 | spin_lock_irqsave(&dio->bio_lock, flags); |
| 413 | dio->waiter = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 415 | if (dio->bio_list) { |
| 416 | bio = dio->bio_list; |
| 417 | dio->bio_list = bio->bi_private; |
| 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | spin_unlock_irqrestore(&dio->bio_lock, flags); |
| 420 | return bio; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Process one completed BIO. No locks are held. |
| 425 | */ |
| 426 | static int dio_bio_complete(struct dio *dio, struct bio *bio) |
| 427 | { |
| 428 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 429 | struct bio_vec *bvec = bio->bi_io_vec; |
| 430 | int page_no; |
| 431 | |
| 432 | if (!uptodate) |
Chen, Kenneth W | 174e27c | 2006-03-25 03:08:16 -0800 | [diff] [blame] | 433 | dio->io_error = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | if (dio->is_async && dio->rw == READ) { |
| 436 | bio_check_pages_dirty(bio); /* transfers ownership */ |
| 437 | } else { |
| 438 | for (page_no = 0; page_no < bio->bi_vcnt; page_no++) { |
| 439 | struct page *page = bvec[page_no].bv_page; |
| 440 | |
| 441 | if (dio->rw == READ && !PageCompound(page)) |
| 442 | set_page_dirty_lock(page); |
| 443 | page_cache_release(page); |
| 444 | } |
| 445 | bio_put(bio); |
| 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return uptodate ? 0 : -EIO; |
| 448 | } |
| 449 | |
| 450 | /* |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 451 | * Wait on and process all in-flight BIOs. This must only be called once |
| 452 | * all bios have been issued so that the refcount can only decrease. |
| 453 | * This just waits for all bios to make it through dio_bio_complete. IO |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 454 | * errors are propagated through dio->io_error and should be propagated via |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 455 | * dio_complete(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | */ |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 457 | static void dio_await_completion(struct dio *dio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Zach Brown | 0273201 | 2006-12-10 02:20:59 -0800 | [diff] [blame] | 459 | struct bio *bio; |
| 460 | do { |
| 461 | bio = dio_await_one(dio); |
| 462 | if (bio) |
| 463 | dio_bio_complete(dio, bio); |
| 464 | } while (bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* |
| 468 | * A really large O_DIRECT read or write can generate a lot of BIOs. So |
| 469 | * to keep the memory consumption sane we periodically reap any completed BIOs |
| 470 | * during the BIO generation phase. |
| 471 | * |
| 472 | * This also helps to limit the peak amount of pinned userspace memory. |
| 473 | */ |
| 474 | static int dio_bio_reap(struct dio *dio) |
| 475 | { |
| 476 | int ret = 0; |
| 477 | |
| 478 | if (dio->reap_counter++ >= 64) { |
| 479 | while (dio->bio_list) { |
| 480 | unsigned long flags; |
| 481 | struct bio *bio; |
| 482 | int ret2; |
| 483 | |
| 484 | spin_lock_irqsave(&dio->bio_lock, flags); |
| 485 | bio = dio->bio_list; |
| 486 | dio->bio_list = bio->bi_private; |
| 487 | spin_unlock_irqrestore(&dio->bio_lock, flags); |
| 488 | ret2 = dio_bio_complete(dio, bio); |
| 489 | if (ret == 0) |
| 490 | ret = ret2; |
| 491 | } |
| 492 | dio->reap_counter = 0; |
| 493 | } |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Call into the fs to map some more disk blocks. We record the current number |
| 499 | * of available blocks at dio->blocks_available. These are in units of the |
| 500 | * fs blocksize, (1 << inode->i_blkbits). |
| 501 | * |
| 502 | * The fs is allowed to map lots of blocks at once. If it wants to do that, |
| 503 | * it uses the passed inode-relative block number as the file offset, as usual. |
| 504 | * |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 505 | * get_block() is passed the number of i_blkbits-sized blocks which direct_io |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | * has remaining to do. The fs should not map more than this number of blocks. |
| 507 | * |
| 508 | * If the fs has mapped a lot of blocks, it should populate bh->b_size to |
| 509 | * indicate how much contiguous disk space has been made available at |
| 510 | * bh->b_blocknr. |
| 511 | * |
| 512 | * If *any* of the mapped blocks are new, then the fs must set buffer_new(). |
| 513 | * This isn't very efficient... |
| 514 | * |
| 515 | * In the case of filesystem holes: the fs may return an arbitrarily-large |
| 516 | * hole by returning an appropriate value in b_size and by clearing |
| 517 | * buffer_mapped(). However the direct-io code will only process holes one |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 518 | * block at a time - it will repeatedly call get_block() as it walks the hole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | */ |
| 520 | static int get_more_blocks(struct dio *dio) |
| 521 | { |
| 522 | int ret; |
| 523 | struct buffer_head *map_bh = &dio->map_bh; |
| 524 | sector_t fs_startblk; /* Into file, in filesystem-sized blocks */ |
| 525 | unsigned long fs_count; /* Number of filesystem-sized blocks */ |
| 526 | unsigned long dio_count;/* Number of dio_block-sized blocks */ |
| 527 | unsigned long blkmask; |
| 528 | int create; |
| 529 | |
| 530 | /* |
| 531 | * If there was a memory error and we've overwritten all the |
| 532 | * mapped blocks then we can now return that memory error |
| 533 | */ |
| 534 | ret = dio->page_errors; |
| 535 | if (ret == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | BUG_ON(dio->block_in_file >= dio->final_block_in_request); |
| 537 | fs_startblk = dio->block_in_file >> dio->blkfactor; |
| 538 | dio_count = dio->final_block_in_request - dio->block_in_file; |
| 539 | fs_count = dio_count >> dio->blkfactor; |
| 540 | blkmask = (1 << dio->blkfactor) - 1; |
| 541 | if (dio_count & blkmask) |
| 542 | fs_count++; |
| 543 | |
Nathan Scott | 3c674e7 | 2006-03-29 09:26:15 +1000 | [diff] [blame] | 544 | map_bh->b_state = 0; |
| 545 | map_bh->b_size = fs_count << dio->inode->i_blkbits; |
| 546 | |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 547 | /* |
| 548 | * For writes inside i_size on a DIO_SKIP_HOLES filesystem we |
| 549 | * forbid block creations: only overwrites are permitted. |
| 550 | * We will return early to the caller once we see an |
| 551 | * unmapped buffer head returned, and the caller will fall |
| 552 | * back to buffered I/O. |
| 553 | * |
| 554 | * Otherwise the decision is left to the get_blocks method, |
| 555 | * which may decide to handle it or also return an unmapped |
| 556 | * buffer head. |
| 557 | */ |
Jens Axboe | b31dc66 | 2006-06-13 08:26:10 +0200 | [diff] [blame] | 558 | create = dio->rw & WRITE; |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 559 | if (dio->flags & DIO_SKIP_HOLES) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | if (dio->block_in_file < (i_size_read(dio->inode) >> |
| 561 | dio->blkbits)) |
| 562 | create = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
Nathan Scott | 3c674e7 | 2006-03-29 09:26:15 +1000 | [diff] [blame] | 564 | |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 565 | ret = (*dio->get_block)(dio->inode, fs_startblk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | map_bh, create); |
| 567 | } |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * There is no bio. Make one now. |
| 573 | */ |
| 574 | static int dio_new_bio(struct dio *dio, sector_t start_sector) |
| 575 | { |
| 576 | sector_t sector; |
| 577 | int ret, nr_pages; |
| 578 | |
| 579 | ret = dio_bio_reap(dio); |
| 580 | if (ret) |
| 581 | goto out; |
| 582 | sector = start_sector << (dio->blkbits - 9); |
| 583 | nr_pages = min(dio->pages_in_io, bio_get_nr_vecs(dio->map_bh.b_bdev)); |
| 584 | BUG_ON(nr_pages <= 0); |
| 585 | ret = dio_bio_alloc(dio, dio->map_bh.b_bdev, sector, nr_pages); |
| 586 | dio->boundary = 0; |
| 587 | out: |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Attempt to put the current chunk of 'cur_page' into the current BIO. If |
| 593 | * that was successful then update final_block_in_bio and take a ref against |
| 594 | * the just-added page. |
| 595 | * |
| 596 | * Return zero on success. Non-zero means the caller needs to start a new BIO. |
| 597 | */ |
| 598 | static int dio_bio_add_page(struct dio *dio) |
| 599 | { |
| 600 | int ret; |
| 601 | |
| 602 | ret = bio_add_page(dio->bio, dio->cur_page, |
| 603 | dio->cur_page_len, dio->cur_page_offset); |
| 604 | if (ret == dio->cur_page_len) { |
| 605 | /* |
| 606 | * Decrement count only, if we are done with this page |
| 607 | */ |
| 608 | if ((dio->cur_page_len + dio->cur_page_offset) == PAGE_SIZE) |
| 609 | dio->pages_in_io--; |
| 610 | page_cache_get(dio->cur_page); |
| 611 | dio->final_block_in_bio = dio->cur_page_block + |
| 612 | (dio->cur_page_len >> dio->blkbits); |
| 613 | ret = 0; |
| 614 | } else { |
| 615 | ret = 1; |
| 616 | } |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Put cur_page under IO. The section of cur_page which is described by |
| 622 | * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page |
| 623 | * starts on-disk at cur_page_block. |
| 624 | * |
| 625 | * We take a ref against the page here (on behalf of its presence in the bio). |
| 626 | * |
| 627 | * The caller of this function is responsible for removing cur_page from the |
| 628 | * dio, and for dropping the refcount which came from that presence. |
| 629 | */ |
| 630 | static int dio_send_cur_page(struct dio *dio) |
| 631 | { |
| 632 | int ret = 0; |
| 633 | |
| 634 | if (dio->bio) { |
Josef Bacik | c2c6ca4 | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 635 | loff_t cur_offset = dio->block_in_file << dio->blkbits; |
| 636 | loff_t bio_next_offset = dio->logical_offset_in_bio + |
| 637 | dio->bio->bi_size; |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /* |
Josef Bacik | c2c6ca4 | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 640 | * See whether this new request is contiguous with the old. |
| 641 | * |
| 642 | * Btrfs cannot handl having logically non-contiguous requests |
| 643 | * submitted. For exmple if you have |
| 644 | * |
| 645 | * Logical: [0-4095][HOLE][8192-12287] |
| 646 | * Phyiscal: [0-4095] [4096-8181] |
| 647 | * |
| 648 | * We cannot submit those pages together as one BIO. So if our |
| 649 | * current logical offset in the file does not equal what would |
| 650 | * be the next logical offset in the bio, submit the bio we |
| 651 | * have. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | */ |
Josef Bacik | c2c6ca4 | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 653 | if (dio->final_block_in_bio != dio->cur_page_block || |
| 654 | cur_offset != bio_next_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | dio_bio_submit(dio); |
| 656 | /* |
| 657 | * Submit now if the underlying fs is about to perform a |
| 658 | * metadata read |
| 659 | */ |
| 660 | if (dio->boundary) |
| 661 | dio_bio_submit(dio); |
| 662 | } |
| 663 | |
| 664 | if (dio->bio == NULL) { |
| 665 | ret = dio_new_bio(dio, dio->cur_page_block); |
| 666 | if (ret) |
| 667 | goto out; |
| 668 | } |
| 669 | |
| 670 | if (dio_bio_add_page(dio) != 0) { |
| 671 | dio_bio_submit(dio); |
| 672 | ret = dio_new_bio(dio, dio->cur_page_block); |
| 673 | if (ret == 0) { |
| 674 | ret = dio_bio_add_page(dio); |
| 675 | BUG_ON(ret != 0); |
| 676 | } |
| 677 | } |
| 678 | out: |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * An autonomous function to put a chunk of a page under deferred IO. |
| 684 | * |
| 685 | * The caller doesn't actually know (or care) whether this piece of page is in |
| 686 | * a BIO, or is under IO or whatever. We just take care of all possible |
| 687 | * situations here. The separation between the logic of do_direct_IO() and |
| 688 | * that of submit_page_section() is important for clarity. Please don't break. |
| 689 | * |
| 690 | * The chunk of page starts on-disk at blocknr. |
| 691 | * |
| 692 | * We perform deferred IO, by recording the last-submitted page inside our |
| 693 | * private part of the dio structure. If possible, we just expand the IO |
| 694 | * across that page here. |
| 695 | * |
| 696 | * If that doesn't work out then we put the old page into the bio and add this |
| 697 | * page to the dio instead. |
| 698 | */ |
| 699 | static int |
| 700 | submit_page_section(struct dio *dio, struct page *page, |
| 701 | unsigned offset, unsigned len, sector_t blocknr) |
| 702 | { |
| 703 | int ret = 0; |
| 704 | |
Andrew Morton | 98c4d57 | 2006-12-10 02:19:47 -0800 | [diff] [blame] | 705 | if (dio->rw & WRITE) { |
| 706 | /* |
| 707 | * Read accounting is performed in submit_bio() |
| 708 | */ |
| 709 | task_io_account_write(len); |
| 710 | } |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | /* |
| 713 | * Can we just grow the current page's presence in the dio? |
| 714 | */ |
| 715 | if ( (dio->cur_page == page) && |
| 716 | (dio->cur_page_offset + dio->cur_page_len == offset) && |
| 717 | (dio->cur_page_block + |
| 718 | (dio->cur_page_len >> dio->blkbits) == blocknr)) { |
| 719 | dio->cur_page_len += len; |
| 720 | |
| 721 | /* |
| 722 | * If dio->boundary then we want to schedule the IO now to |
| 723 | * avoid metadata seeks. |
| 724 | */ |
| 725 | if (dio->boundary) { |
| 726 | ret = dio_send_cur_page(dio); |
| 727 | page_cache_release(dio->cur_page); |
| 728 | dio->cur_page = NULL; |
| 729 | } |
| 730 | goto out; |
| 731 | } |
| 732 | |
| 733 | /* |
| 734 | * If there's a deferred page already there then send it. |
| 735 | */ |
| 736 | if (dio->cur_page) { |
| 737 | ret = dio_send_cur_page(dio); |
| 738 | page_cache_release(dio->cur_page); |
| 739 | dio->cur_page = NULL; |
| 740 | if (ret) |
| 741 | goto out; |
| 742 | } |
| 743 | |
| 744 | page_cache_get(page); /* It is in dio */ |
| 745 | dio->cur_page = page; |
| 746 | dio->cur_page_offset = offset; |
| 747 | dio->cur_page_len = len; |
| 748 | dio->cur_page_block = blocknr; |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 749 | dio->cur_page_fs_offset = dio->block_in_file << dio->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | out: |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Clean any dirty buffers in the blockdev mapping which alias newly-created |
| 756 | * file blocks. Only called for S_ISREG files - blockdevs do not set |
| 757 | * buffer_new |
| 758 | */ |
| 759 | static void clean_blockdev_aliases(struct dio *dio) |
| 760 | { |
| 761 | unsigned i; |
| 762 | unsigned nblocks; |
| 763 | |
| 764 | nblocks = dio->map_bh.b_size >> dio->inode->i_blkbits; |
| 765 | |
| 766 | for (i = 0; i < nblocks; i++) { |
| 767 | unmap_underlying_metadata(dio->map_bh.b_bdev, |
| 768 | dio->map_bh.b_blocknr + i); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * If we are not writing the entire block and get_block() allocated |
| 774 | * the block for us, we need to fill-in the unused portion of the |
| 775 | * block with zeros. This happens only if user-buffer, fileoffset or |
| 776 | * io length is not filesystem block-size multiple. |
| 777 | * |
| 778 | * `end' is zero if we're doing the start of the IO, 1 at the end of the |
| 779 | * IO. |
| 780 | */ |
| 781 | static void dio_zero_block(struct dio *dio, int end) |
| 782 | { |
| 783 | unsigned dio_blocks_per_fs_block; |
| 784 | unsigned this_chunk_blocks; /* In dio_blocks */ |
| 785 | unsigned this_chunk_bytes; |
| 786 | struct page *page; |
| 787 | |
| 788 | dio->start_zero_done = 1; |
| 789 | if (!dio->blkfactor || !buffer_new(&dio->map_bh)) |
| 790 | return; |
| 791 | |
| 792 | dio_blocks_per_fs_block = 1 << dio->blkfactor; |
| 793 | this_chunk_blocks = dio->block_in_file & (dio_blocks_per_fs_block - 1); |
| 794 | |
| 795 | if (!this_chunk_blocks) |
| 796 | return; |
| 797 | |
| 798 | /* |
| 799 | * We need to zero out part of an fs block. It is either at the |
| 800 | * beginning or the end of the fs block. |
| 801 | */ |
| 802 | if (end) |
| 803 | this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks; |
| 804 | |
| 805 | this_chunk_bytes = this_chunk_blocks << dio->blkbits; |
| 806 | |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 807 | page = ZERO_PAGE(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | if (submit_page_section(dio, page, 0, this_chunk_bytes, |
| 809 | dio->next_block_for_io)) |
| 810 | return; |
| 811 | |
| 812 | dio->next_block_for_io += this_chunk_blocks; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Walk the user pages, and the file, mapping blocks to disk and generating |
| 817 | * a sequence of (page,offset,len,block) mappings. These mappings are injected |
| 818 | * into submit_page_section(), which takes care of the next stage of submission |
| 819 | * |
| 820 | * Direct IO against a blockdev is different from a file. Because we can |
| 821 | * happily perform page-sized but 512-byte aligned IOs. It is important that |
| 822 | * blockdev IO be able to have fine alignment and large sizes. |
| 823 | * |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 824 | * So what we do is to permit the ->get_block function to populate bh.b_size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | * with the size of IO which is permitted at this offset and this i_blkbits. |
| 826 | * |
| 827 | * For best results, the blockdev should be set up with 512-byte i_blkbits and |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 828 | * it should set b_size to PAGE_SIZE or more inside get_block(). This gives |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | * fine alignment but still allows this function to work in PAGE_SIZE units. |
| 830 | */ |
| 831 | static int do_direct_IO(struct dio *dio) |
| 832 | { |
| 833 | const unsigned blkbits = dio->blkbits; |
| 834 | const unsigned blocks_per_page = PAGE_SIZE >> blkbits; |
| 835 | struct page *page; |
| 836 | unsigned block_in_page; |
| 837 | struct buffer_head *map_bh = &dio->map_bh; |
| 838 | int ret = 0; |
| 839 | |
| 840 | /* The I/O can start at any block offset within the first page */ |
| 841 | block_in_page = dio->first_block_in_page; |
| 842 | |
| 843 | while (dio->block_in_file < dio->final_block_in_request) { |
| 844 | page = dio_get_page(dio); |
| 845 | if (IS_ERR(page)) { |
| 846 | ret = PTR_ERR(page); |
| 847 | goto out; |
| 848 | } |
| 849 | |
| 850 | while (block_in_page < blocks_per_page) { |
| 851 | unsigned offset_in_page = block_in_page << blkbits; |
| 852 | unsigned this_chunk_bytes; /* # of bytes mapped */ |
| 853 | unsigned this_chunk_blocks; /* # of blocks */ |
| 854 | unsigned u; |
| 855 | |
| 856 | if (dio->blocks_available == 0) { |
| 857 | /* |
| 858 | * Need to go and map some more disk |
| 859 | */ |
| 860 | unsigned long blkmask; |
| 861 | unsigned long dio_remainder; |
| 862 | |
| 863 | ret = get_more_blocks(dio); |
| 864 | if (ret) { |
| 865 | page_cache_release(page); |
| 866 | goto out; |
| 867 | } |
| 868 | if (!buffer_mapped(map_bh)) |
| 869 | goto do_holes; |
| 870 | |
| 871 | dio->blocks_available = |
| 872 | map_bh->b_size >> dio->blkbits; |
| 873 | dio->next_block_for_io = |
| 874 | map_bh->b_blocknr << dio->blkfactor; |
| 875 | if (buffer_new(map_bh)) |
| 876 | clean_blockdev_aliases(dio); |
| 877 | |
| 878 | if (!dio->blkfactor) |
| 879 | goto do_holes; |
| 880 | |
| 881 | blkmask = (1 << dio->blkfactor) - 1; |
| 882 | dio_remainder = (dio->block_in_file & blkmask); |
| 883 | |
| 884 | /* |
| 885 | * If we are at the start of IO and that IO |
| 886 | * starts partway into a fs-block, |
| 887 | * dio_remainder will be non-zero. If the IO |
| 888 | * is a read then we can simply advance the IO |
| 889 | * cursor to the first block which is to be |
| 890 | * read. But if the IO is a write and the |
| 891 | * block was newly allocated we cannot do that; |
| 892 | * the start of the fs block must be zeroed out |
| 893 | * on-disk |
| 894 | */ |
| 895 | if (!buffer_new(map_bh)) |
| 896 | dio->next_block_for_io += dio_remainder; |
| 897 | dio->blocks_available -= dio_remainder; |
| 898 | } |
| 899 | do_holes: |
| 900 | /* Handle holes */ |
| 901 | if (!buffer_mapped(map_bh)) { |
Jeff Moyer | 35dc816 | 2006-02-03 03:04:27 -0800 | [diff] [blame] | 902 | loff_t i_size_aligned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | /* AKPM: eargh, -ENOTBLK is a hack */ |
Jens Axboe | b31dc66 | 2006-06-13 08:26:10 +0200 | [diff] [blame] | 905 | if (dio->rw & WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | page_cache_release(page); |
| 907 | return -ENOTBLK; |
| 908 | } |
| 909 | |
Jeff Moyer | 35dc816 | 2006-02-03 03:04:27 -0800 | [diff] [blame] | 910 | /* |
| 911 | * Be sure to account for a partial block as the |
| 912 | * last block in the file |
| 913 | */ |
| 914 | i_size_aligned = ALIGN(i_size_read(dio->inode), |
| 915 | 1 << blkbits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | if (dio->block_in_file >= |
Jeff Moyer | 35dc816 | 2006-02-03 03:04:27 -0800 | [diff] [blame] | 917 | i_size_aligned >> blkbits) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | /* We hit eof */ |
| 919 | page_cache_release(page); |
| 920 | goto out; |
| 921 | } |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 922 | zero_user(page, block_in_page << blkbits, |
| 923 | 1 << blkbits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | dio->block_in_file++; |
| 925 | block_in_page++; |
| 926 | goto next_block; |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * If we're performing IO which has an alignment which |
| 931 | * is finer than the underlying fs, go check to see if |
| 932 | * we must zero out the start of this block. |
| 933 | */ |
| 934 | if (unlikely(dio->blkfactor && !dio->start_zero_done)) |
| 935 | dio_zero_block(dio, 0); |
| 936 | |
| 937 | /* |
| 938 | * Work out, in this_chunk_blocks, how much disk we |
| 939 | * can add to this page |
| 940 | */ |
| 941 | this_chunk_blocks = dio->blocks_available; |
| 942 | u = (PAGE_SIZE - offset_in_page) >> blkbits; |
| 943 | if (this_chunk_blocks > u) |
| 944 | this_chunk_blocks = u; |
| 945 | u = dio->final_block_in_request - dio->block_in_file; |
| 946 | if (this_chunk_blocks > u) |
| 947 | this_chunk_blocks = u; |
| 948 | this_chunk_bytes = this_chunk_blocks << blkbits; |
| 949 | BUG_ON(this_chunk_bytes == 0); |
| 950 | |
| 951 | dio->boundary = buffer_boundary(map_bh); |
| 952 | ret = submit_page_section(dio, page, offset_in_page, |
| 953 | this_chunk_bytes, dio->next_block_for_io); |
| 954 | if (ret) { |
| 955 | page_cache_release(page); |
| 956 | goto out; |
| 957 | } |
| 958 | dio->next_block_for_io += this_chunk_blocks; |
| 959 | |
| 960 | dio->block_in_file += this_chunk_blocks; |
| 961 | block_in_page += this_chunk_blocks; |
| 962 | dio->blocks_available -= this_chunk_blocks; |
| 963 | next_block: |
Eric Sesterhenn | d4569d2 | 2006-04-01 01:10:13 +0200 | [diff] [blame] | 964 | BUG_ON(dio->block_in_file > dio->final_block_in_request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | if (dio->block_in_file == dio->final_block_in_request) |
| 966 | break; |
| 967 | } |
| 968 | |
| 969 | /* Drop the ref which was taken in get_user_pages() */ |
| 970 | page_cache_release(page); |
| 971 | block_in_page = 0; |
| 972 | } |
| 973 | out: |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 978 | * Releases both i_mutex and i_alloc_sem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | */ |
| 980 | static ssize_t |
| 981 | direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode, |
| 982 | const struct iovec *iov, loff_t offset, unsigned long nr_segs, |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 983 | unsigned blkbits, get_block_t get_block, dio_iodone_t end_io, |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 984 | dio_submit_t submit_io, struct dio *dio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | { |
| 986 | unsigned long user_addr; |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 987 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | int seg; |
| 989 | ssize_t ret = 0; |
| 990 | ssize_t ret2; |
| 991 | size_t bytes; |
| 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | dio->inode = inode; |
| 994 | dio->rw = rw; |
| 995 | dio->blkbits = blkbits; |
| 996 | dio->blkfactor = inode->i_blkbits - blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | dio->block_in_file = offset >> blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 999 | dio->get_block = get_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | dio->end_io = end_io; |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1001 | dio->submit_io = submit_io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | dio->final_block_in_bio = -1; |
| 1003 | dio->next_block_for_io = -1; |
| 1004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | dio->iocb = iocb; |
Daniel McNeil | 29504ff | 2005-04-16 15:25:50 -0700 | [diff] [blame] | 1006 | dio->i_size = i_size_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | spin_lock_init(&dio->bio_lock); |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 1009 | dio->refcount = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | /* |
| 1012 | * In case of non-aligned buffers, we may need 2 more |
| 1013 | * pages since we need to zero out first and last block. |
| 1014 | */ |
| 1015 | if (unlikely(dio->blkfactor)) |
| 1016 | dio->pages_in_io = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | for (seg = 0; seg < nr_segs; seg++) { |
| 1019 | user_addr = (unsigned long)iov[seg].iov_base; |
| 1020 | dio->pages_in_io += |
| 1021 | ((user_addr+iov[seg].iov_len +PAGE_SIZE-1)/PAGE_SIZE |
| 1022 | - user_addr/PAGE_SIZE); |
| 1023 | } |
| 1024 | |
| 1025 | for (seg = 0; seg < nr_segs; seg++) { |
| 1026 | user_addr = (unsigned long)iov[seg].iov_base; |
| 1027 | dio->size += bytes = iov[seg].iov_len; |
| 1028 | |
| 1029 | /* Index into the first page of the first block */ |
| 1030 | dio->first_block_in_page = (user_addr & ~PAGE_MASK) >> blkbits; |
| 1031 | dio->final_block_in_request = dio->block_in_file + |
| 1032 | (bytes >> blkbits); |
| 1033 | /* Page fetching state */ |
| 1034 | dio->head = 0; |
| 1035 | dio->tail = 0; |
| 1036 | dio->curr_page = 0; |
| 1037 | |
| 1038 | dio->total_pages = 0; |
| 1039 | if (user_addr & (PAGE_SIZE-1)) { |
| 1040 | dio->total_pages++; |
| 1041 | bytes -= PAGE_SIZE - (user_addr & (PAGE_SIZE - 1)); |
| 1042 | } |
| 1043 | dio->total_pages += (bytes + PAGE_SIZE - 1) / PAGE_SIZE; |
| 1044 | dio->curr_user_address = user_addr; |
| 1045 | |
| 1046 | ret = do_direct_IO(dio); |
| 1047 | |
| 1048 | dio->result += iov[seg].iov_len - |
| 1049 | ((dio->final_block_in_request - dio->block_in_file) << |
| 1050 | blkbits); |
| 1051 | |
| 1052 | if (ret) { |
| 1053 | dio_cleanup(dio); |
| 1054 | break; |
| 1055 | } |
| 1056 | } /* end iovec loop */ |
| 1057 | |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1058 | if (ret == -ENOTBLK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | /* |
| 1060 | * The remaining part of the request will be |
| 1061 | * be handled by buffered I/O when we return |
| 1062 | */ |
| 1063 | ret = 0; |
| 1064 | } |
| 1065 | /* |
| 1066 | * There may be some unwritten disk at the end of a part-written |
| 1067 | * fs-block-sized block. Go zero that now. |
| 1068 | */ |
| 1069 | dio_zero_block(dio, 1); |
| 1070 | |
| 1071 | if (dio->cur_page) { |
| 1072 | ret2 = dio_send_cur_page(dio); |
| 1073 | if (ret == 0) |
| 1074 | ret = ret2; |
| 1075 | page_cache_release(dio->cur_page); |
| 1076 | dio->cur_page = NULL; |
| 1077 | } |
| 1078 | if (dio->bio) |
| 1079 | dio_bio_submit(dio); |
| 1080 | |
| 1081 | /* |
| 1082 | * It is possible that, we return short IO due to end of file. |
| 1083 | * In that case, we need to release all the pages we got hold on. |
| 1084 | */ |
| 1085 | dio_cleanup(dio); |
| 1086 | |
| 1087 | /* |
| 1088 | * All block lookups have been performed. For READ requests |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1089 | * we can let i_mutex go now that its achieved its purpose |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | * of protecting us from looking up uninitialized blocks. |
| 1091 | */ |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1092 | if (rw == READ && (dio->flags & DIO_LOCKING)) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1093 | mutex_unlock(&dio->inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
| 1095 | /* |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1096 | * The only time we want to leave bios in flight is when a successful |
| 1097 | * partial aio read or full aio write have been setup. In that case |
| 1098 | * bio completion will call aio_complete. The only time it's safe to |
| 1099 | * call aio_complete is when we return -EIOCBQUEUED, so we key on that. |
| 1100 | * This had *better* be the only place that raises -EIOCBQUEUED. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | */ |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1102 | BUG_ON(ret == -EIOCBQUEUED); |
| 1103 | if (dio->is_async && ret == 0 && dio->result && |
| 1104 | ((rw & READ) || (dio->result == dio->size))) |
| 1105 | ret = -EIOCBQUEUED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
Jeff Moyer | cfb1e33 | 2009-10-02 18:57:36 -0400 | [diff] [blame] | 1107 | if (ret != -EIOCBQUEUED) { |
| 1108 | /* All IO is now issued, send it on its way */ |
| 1109 | blk_run_address_space(inode->i_mapping); |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 1110 | dio_await_completion(dio); |
Jeff Moyer | cfb1e33 | 2009-10-02 18:57:36 -0400 | [diff] [blame] | 1111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1113 | /* |
| 1114 | * Sync will always be dropping the final ref and completing the |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 1115 | * operation. AIO can if it was a broken operation described above or |
| 1116 | * in fact if all the bios race to complete before we get here. In |
| 1117 | * that case dio_complete() translates the EIOCBQUEUED into the proper |
| 1118 | * return code that the caller will hand to aio_complete(). |
| 1119 | * |
| 1120 | * This is managed by the bio_lock instead of being an atomic_t so that |
| 1121 | * completion paths can drop their ref and use the remaining count to |
| 1122 | * decide to wake the submission path atomically. |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1123 | */ |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 1124 | spin_lock_irqsave(&dio->bio_lock, flags); |
| 1125 | ret2 = --dio->refcount; |
| 1126 | spin_unlock_irqrestore(&dio->bio_lock, flags); |
Zach Brown | fcb82f8 | 2007-07-03 15:28:55 -0700 | [diff] [blame] | 1127 | |
Zach Brown | 5eb6c7a | 2006-12-10 02:21:07 -0800 | [diff] [blame] | 1128 | if (ret2 == 0) { |
Zach Brown | 6d544bb | 2006-12-10 02:20:54 -0800 | [diff] [blame] | 1129 | ret = dio_complete(dio, offset, ret); |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1130 | kfree(dio); |
| 1131 | } else |
| 1132 | BUG_ON(ret != -EIOCBQUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * This is a library function for use by filesystem drivers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | * |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1140 | * The locking rules are governed by the flags parameter: |
| 1141 | * - if the flags value contains DIO_LOCKING we use a fancy locking |
| 1142 | * scheme for dumb filesystems. |
| 1143 | * For writes this function is called under i_mutex and returns with |
| 1144 | * i_mutex held, for reads, i_mutex is not held on entry, but it is |
| 1145 | * taken and dropped again before returning. |
| 1146 | * For reads and writes i_alloc_sem is taken in shared mode and released |
| 1147 | * on I/O completion (which may happen asynchronously after returning to |
| 1148 | * the caller). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | * |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1150 | * - if the flags value does NOT contain DIO_LOCKING we don't use any |
| 1151 | * internal locking but rather rely on the filesystem to synchronize |
| 1152 | * direct I/O reads/writes versus each other and truncate. |
| 1153 | * For reads and writes both i_mutex and i_alloc_sem are not held on |
| 1154 | * entry and are never taken. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | */ |
| 1156 | ssize_t |
| 1157 | __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode, |
| 1158 | struct block_device *bdev, const struct iovec *iov, loff_t offset, |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 1159 | unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io, |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1160 | dio_submit_t submit_io, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | { |
| 1162 | int seg; |
| 1163 | size_t size; |
| 1164 | unsigned long addr; |
| 1165 | unsigned blkbits = inode->i_blkbits; |
| 1166 | unsigned bdev_blkbits = 0; |
| 1167 | unsigned blocksize_mask = (1 << blkbits) - 1; |
| 1168 | ssize_t retval = -EINVAL; |
| 1169 | loff_t end = offset; |
| 1170 | struct dio *dio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
| 1172 | if (rw & WRITE) |
Vivek Goyal | d9449ce | 2009-11-26 09:45:40 +0100 | [diff] [blame] | 1173 | rw = WRITE_ODIRECT_PLUG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
| 1175 | if (bdev) |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1176 | bdev_blkbits = blksize_bits(bdev_logical_block_size(bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
| 1178 | if (offset & blocksize_mask) { |
| 1179 | if (bdev) |
| 1180 | blkbits = bdev_blkbits; |
| 1181 | blocksize_mask = (1 << blkbits) - 1; |
| 1182 | if (offset & blocksize_mask) |
| 1183 | goto out; |
| 1184 | } |
| 1185 | |
| 1186 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 1187 | for (seg = 0; seg < nr_segs; seg++) { |
| 1188 | addr = (unsigned long)iov[seg].iov_base; |
| 1189 | size = iov[seg].iov_len; |
| 1190 | end += size; |
| 1191 | if ((addr & blocksize_mask) || (size & blocksize_mask)) { |
| 1192 | if (bdev) |
| 1193 | blkbits = bdev_blkbits; |
| 1194 | blocksize_mask = (1 << blkbits) - 1; |
| 1195 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
| 1196 | goto out; |
| 1197 | } |
| 1198 | } |
| 1199 | |
Jeff Moyer | 23aee09 | 2009-12-15 16:47:49 -0800 | [diff] [blame] | 1200 | dio = kmalloc(sizeof(*dio), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | retval = -ENOMEM; |
| 1202 | if (!dio) |
| 1203 | goto out; |
Jeff Moyer | 23aee09 | 2009-12-15 16:47:49 -0800 | [diff] [blame] | 1204 | /* |
| 1205 | * Believe it or not, zeroing out the page array caused a .5% |
| 1206 | * performance regression in a database benchmark. So, we take |
| 1207 | * care to only zero out what's needed. |
| 1208 | */ |
| 1209 | memset(dio, 0, offsetof(struct dio, pages)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1211 | dio->flags = flags; |
| 1212 | if (dio->flags & DIO_LOCKING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | /* watch out for a 0 len io from a tricksy fs */ |
| 1214 | if (rw == READ && end > offset) { |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1215 | struct address_space *mapping = |
| 1216 | iocb->ki_filp->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1218 | /* will be released by direct_io_worker */ |
| 1219 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
| 1221 | retval = filemap_write_and_wait_range(mapping, offset, |
| 1222 | end - 1); |
| 1223 | if (retval) { |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1224 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | kfree(dio); |
| 1226 | goto out; |
| 1227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1230 | /* |
| 1231 | * Will be released at I/O completion, possibly in a |
| 1232 | * different thread. |
| 1233 | */ |
| 1234 | down_read_non_owner(&inode->i_alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * For file extending writes updating i_size before data |
| 1239 | * writeouts complete can expose uninitialized blocks. So |
| 1240 | * even for AIO, we need to wait for i/o to complete before |
| 1241 | * returning in this case. |
| 1242 | */ |
Jens Axboe | b31dc66 | 2006-06-13 08:26:10 +0200 | [diff] [blame] | 1243 | dio->is_async = !is_sync_kiocb(iocb) && !((rw & WRITE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | (end > i_size_read(inode))); |
| 1245 | |
| 1246 | retval = direct_io_worker(rw, iocb, inode, iov, offset, |
Josef Bacik | facd07b | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1247 | nr_segs, blkbits, get_block, end_io, |
| 1248 | submit_io, dio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | |
Dmitri Monakhov | 0f64415 | 2009-01-06 14:40:04 -0800 | [diff] [blame] | 1250 | /* |
| 1251 | * In case of error extending write may have instantiated a few |
| 1252 | * blocks outside i_size. Trim these off again for DIO_LOCKING. |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1253 | * |
| 1254 | * NOTE: filesystems with their own locking have to handle this |
| 1255 | * on their own. |
Dmitri Monakhov | 0f64415 | 2009-01-06 14:40:04 -0800 | [diff] [blame] | 1256 | */ |
Al Viro | 06777d3 | 2009-12-17 04:52:13 -0500 | [diff] [blame] | 1257 | if (flags & DIO_LOCKING) { |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1258 | if (unlikely((rw & WRITE) && retval < 0)) { |
| 1259 | loff_t isize = i_size_read(inode); |
| 1260 | if (end > isize) |
| 1261 | vmtruncate(inode, isize); |
| 1262 | } |
Dmitri Monakhov | 0f64415 | 2009-01-06 14:40:04 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | return retval; |
| 1267 | } |
| 1268 | EXPORT_SYMBOL(__blockdev_direct_IO); |