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