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