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