blob: 0a073c7125a677705872c5381699b810477b68b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/direct-io.c
3 *
4 * Copyright (C) 2002, Linus Torvalds.
5 *
6 * O_DIRECT
7 *
Francois Camie1f8e872008-10-15 22:01:59 -07008 * 04Jul2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Initial version
10 * 11Sep2002 janetinc@us.ibm.com
11 * added readv/writev support.
Francois Camie1f8e872008-10-15 22:01:59 -070012 * 29Oct2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Morton98c4d572006-12-10 02:19:47 -080030#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/bio.h>
32#include <linux/wait.h>
33#include <linux/err.h>
34#include <linux/blkdev.h>
35#include <linux/buffer_head.h>
36#include <linux/rwsem.h>
37#include <linux/uio.h>
38#include <asm/atomic.h>
39
40/*
41 * How many user pages to map in one call to get_user_pages(). This determines
42 * the size of a structure on the stack.
43 */
44#define DIO_PAGES 64
45
46/*
47 * This code generally works in units of "dio_blocks". A dio_block is
48 * somewhere between the hard sector size and the filesystem block size. it
49 * is determined on a per-invocation basis. When talking to the filesystem
50 * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
51 * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted
52 * to bio_block quantities by shifting left by blkfactor.
53 *
54 * If blkfactor is zero then the user's request was aligned to the filesystem's
55 * blocksize.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
57
58struct dio {
59 /* BIO submission state */
60 struct bio *bio; /* bio under assembly */
61 struct inode *inode;
62 int rw;
Daniel McNeil29504ff2005-04-16 15:25:50 -070063 loff_t i_size; /* i_size when submitted */
Christoph Hellwig5fe878a2009-12-15 16:47:50 -080064 int flags; /* doesn't change */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned blkbits; /* doesn't change */
66 unsigned blkfactor; /* When we're using an alignment which
67 is finer than the filesystem's soft
68 blocksize, this specifies how much
69 finer. blkfactor=2 means 1/4-block
70 alignment. Does not change */
71 unsigned start_zero_done; /* flag: sub-blocksize zeroing has
72 been performed at the start of a
73 write */
74 int pages_in_io; /* approximate total IO pages */
75 size_t size; /* total request size (doesn't change)*/
76 sector_t block_in_file; /* Current offset into the underlying
77 file in dio_block units. */
78 unsigned blocks_available; /* At block_in_file. changes */
79 sector_t final_block_in_request;/* doesn't change */
80 unsigned first_block_in_page; /* doesn't change, Used only once */
81 int boundary; /* prev block is at a boundary */
82 int reap_counter; /* rate limit reaping */
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -080083 get_block_t *get_block; /* block mapping function */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 dio_iodone_t *end_io; /* IO completion function */
Josef Bacikfacd07b2010-05-23 11:00:55 -040085 dio_submit_t *submit_io; /* IO submition function */
86 loff_t logical_offset_in_bio; /* current first logical block in bio */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 sector_t final_block_in_bio; /* current final block in bio + 1 */
88 sector_t next_block_for_io; /* next block to be put under IO,
89 in dio_blocks units */
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -080090 struct buffer_head map_bh; /* last get_block() result */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /*
93 * Deferred addition of a page to the dio. These variables are
94 * private to dio_send_cur_page(), submit_page_section() and
95 * dio_bio_add_page().
96 */
97 struct page *cur_page; /* The page */
98 unsigned cur_page_offset; /* Offset into it, in bytes */
99 unsigned cur_page_len; /* Nr of bytes at cur_page_offset */
100 sector_t cur_page_block; /* Where it starts */
Josef Bacikfacd07b2010-05-23 11:00:55 -0400101 loff_t cur_page_fs_offset; /* Offset in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* BIO completion state */
104 spinlock_t bio_lock; /* protects BIO fields below */
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800105 unsigned long refcount; /* direct_io_worker() and bios */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct bio *bio_list; /* singly linked via bi_private */
107 struct task_struct *waiter; /* waiting task (NULL if none) */
108
109 /* AIO related stuff */
110 struct kiocb *iocb; /* kiocb */
111 int is_async; /* is IO async ? */
Chen, Kenneth W174e27c2006-03-25 03:08:16 -0800112 int io_error; /* IO error in completion path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 ssize_t result; /* IO result */
Jeff Moyer23aee092009-12-15 16:47:49 -0800114
115 /*
116 * Page fetching state. These variables belong to dio_refill_pages().
117 */
118 int curr_page; /* changes */
119 int total_pages; /* doesn't change */
120 unsigned long curr_user_address;/* changes */
121
122 /*
123 * Page queue. These variables belong to dio_refill_pages() and
124 * dio_get_page().
125 */
126 unsigned head; /* next page to process */
127 unsigned tail; /* last valid page + 1 */
128 int page_errors; /* errno from get_user_pages() */
129
130 /*
131 * pages[] (and any fields placed after it) are not zeroed out at
132 * allocation time. Don't add new fields after pages[] unless you
133 * wish that they not be zeroed.
134 */
135 struct page *pages[DIO_PAGES]; /* page buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Christoph Hellwigbd5fe6c2011-06-24 14:29:43 -0400138static void __inode_dio_wait(struct inode *inode)
139{
140 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
141 DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
142
143 do {
144 prepare_to_wait(wq, &q.wait, TASK_UNINTERRUPTIBLE);
145 if (atomic_read(&inode->i_dio_count))
146 schedule();
147 } while (atomic_read(&inode->i_dio_count));
148 finish_wait(wq, &q.wait);
149}
150
151/**
152 * inode_dio_wait - wait for outstanding DIO requests to finish
153 * @inode: inode to wait for
154 *
155 * Waits for all pending direct I/O requests to finish so that we can
156 * proceed with a truncate or equivalent operation.
157 *
158 * Must be called under a lock that serializes taking new references
159 * to i_dio_count, usually by inode->i_mutex.
160 */
161void inode_dio_wait(struct inode *inode)
162{
163 if (atomic_read(&inode->i_dio_count))
164 __inode_dio_wait(inode);
165}
166EXPORT_SYMBOL_GPL(inode_dio_wait);
167
168/*
169 * inode_dio_done - signal finish of a direct I/O requests
170 * @inode: inode the direct I/O happens on
171 *
172 * This is called once we've finished processing a direct I/O request,
173 * and is used to wake up callers waiting for direct I/O to be quiesced.
174 */
175void inode_dio_done(struct inode *inode)
176{
177 if (atomic_dec_and_test(&inode->i_dio_count))
178 wake_up_bit(&inode->i_state, __I_DIO_WAKEUP);
179}
180EXPORT_SYMBOL_GPL(inode_dio_done);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
183 * How many pages are in the queue?
184 */
185static inline unsigned dio_pages_present(struct dio *dio)
186{
187 return dio->tail - dio->head;
188}
189
190/*
191 * Go grab and pin some userspace pages. Typically we'll get 64 at a time.
192 */
193static int dio_refill_pages(struct dio *dio)
194{
195 int ret;
196 int nr_pages;
197
198 nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
Nick Pigginf5dd33c2008-07-25 19:45:25 -0700199 ret = get_user_pages_fast(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dio->curr_user_address, /* Where from? */
201 nr_pages, /* How many pages? */
202 dio->rw == READ, /* Write to memory? */
Nick Pigginf5dd33c2008-07-25 19:45:25 -0700203 &dio->pages[0]); /* Put results here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Jens Axboeb31dc662006-06-13 08:26:10 +0200205 if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) {
Nick Piggin557ed1f2007-10-16 01:24:40 -0700206 struct page *page = ZERO_PAGE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /*
208 * A memory fault, but the filesystem has some outstanding
209 * mapped blocks. We need to use those blocks up to avoid
210 * leaking stale data in the file.
211 */
212 if (dio->page_errors == 0)
213 dio->page_errors = ret;
Nick Pigginb5810032005-10-29 18:16:12 -0700214 page_cache_get(page);
215 dio->pages[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 dio->head = 0;
217 dio->tail = 1;
218 ret = 0;
219 goto out;
220 }
221
222 if (ret >= 0) {
223 dio->curr_user_address += ret * PAGE_SIZE;
224 dio->curr_page += ret;
225 dio->head = 0;
226 dio->tail = ret;
227 ret = 0;
228 }
229out:
230 return ret;
231}
232
233/*
234 * Get another userspace page. Returns an ERR_PTR on error. Pages are
235 * buffered inside the dio so that we can call get_user_pages() against a
236 * decent number of pages, less frequently. To provide nicer use of the
237 * L1 cache.
238 */
239static struct page *dio_get_page(struct dio *dio)
240{
241 if (dio_pages_present(dio) == 0) {
242 int ret;
243
244 ret = dio_refill_pages(dio);
245 if (ret)
246 return ERR_PTR(ret);
247 BUG_ON(dio_pages_present(dio) == 0);
248 }
249 return dio->pages[dio->head++];
250}
251
Zach Brown6d544bb2006-12-10 02:20:54 -0800252/**
253 * dio_complete() - called when all DIO BIO I/O has been completed
254 * @offset: the byte offset in the file of the completed operation
255 *
256 * This releases locks as dictated by the locking type, lets interested parties
257 * know that a DIO operation has completed, and calculates the resulting return
258 * code for the operation.
259 *
260 * It lets the filesystem know if it registered an interest earlier via
261 * get_block. Pass the private field of the map buffer_head so that
262 * filesystems can use it to hold additional state between get_block calls and
263 * dio_complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Edward Shishkincd1c5842010-10-26 14:22:28 -0700265static ssize_t dio_complete(struct dio *dio, loff_t offset, ssize_t ret, bool is_async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Zach Brown6d544bb2006-12-10 02:20:54 -0800267 ssize_t transferred = 0;
268
Zach Brown8459d862006-12-10 02:21:05 -0800269 /*
270 * AIO submission can race with bio completion to get here while
271 * expecting to have the last io completed by bio completion.
272 * In that case -EIOCBQUEUED is in fact not an error we want
273 * to preserve through this call.
274 */
275 if (ret == -EIOCBQUEUED)
276 ret = 0;
277
Zach Brown6d544bb2006-12-10 02:20:54 -0800278 if (dio->result) {
279 transferred = dio->result;
280
281 /* Check for short read case */
282 if ((dio->rw == READ) && ((offset + transferred) > dio->i_size))
283 transferred = dio->i_size - offset;
284 }
285
Zach Brown6d544bb2006-12-10 02:20:54 -0800286 if (ret == 0)
287 ret = dio->page_errors;
288 if (ret == 0)
289 ret = dio->io_error;
290 if (ret == 0)
291 ret = transferred;
292
Christoph Hellwig40e2e972010-07-18 21:17:09 +0000293 if (dio->end_io && dio->result) {
294 dio->end_io(dio->iocb, offset, transferred,
295 dio->map_bh.b_private, ret, is_async);
296 } else if (is_async) {
297 aio_complete(dio->iocb, ret, 0);
298 }
299
Christoph Hellwigdf2d6f22011-06-24 14:29:46 -0400300 inode_dio_done(dio->inode);
Zach Brown6d544bb2006-12-10 02:20:54 -0800301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static int dio_bio_complete(struct dio *dio, struct bio *bio);
305/*
306 * Asynchronous IO callback.
307 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200308static void dio_bio_end_aio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct dio *dio = bio->bi_private;
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800311 unsigned long remaining;
312 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* cleanup the bio */
315 dio_bio_complete(dio, bio);
Zach Brown02732012006-12-10 02:20:59 -0800316
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800317 spin_lock_irqsave(&dio->bio_lock, flags);
318 remaining = --dio->refcount;
319 if (remaining == 1 && dio->waiter)
Zach Brown20258b22006-12-10 02:21:01 -0800320 wake_up_process(dio->waiter);
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800321 spin_unlock_irqrestore(&dio->bio_lock, flags);
Zach Brown20258b22006-12-10 02:21:01 -0800322
Zach Brown8459d862006-12-10 02:21:05 -0800323 if (remaining == 0) {
Christoph Hellwig40e2e972010-07-18 21:17:09 +0000324 dio_complete(dio, dio->iocb->ki_pos, 0, true);
Zach Brown8459d862006-12-10 02:21:05 -0800325 kfree(dio);
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/*
330 * The BIO completion handler simply queues the BIO up for the process-context
331 * handler.
332 *
333 * During I/O bi_private points at the dio. After I/O, bi_private is used to
334 * implement a singly-linked list of completed BIOs, at dio->bio_list.
335 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200336static void dio_bio_end_io(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct dio *dio = bio->bi_private;
339 unsigned long flags;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_lock_irqsave(&dio->bio_lock, flags);
342 bio->bi_private = dio->bio_list;
343 dio->bio_list = bio;
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800344 if (--dio->refcount == 1 && dio->waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 wake_up_process(dio->waiter);
346 spin_unlock_irqrestore(&dio->bio_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Josef Bacikfacd07b2010-05-23 11:00:55 -0400349/**
350 * dio_end_io - handle the end io action for the given bio
351 * @bio: The direct io bio thats being completed
352 * @error: Error if there was one
353 *
354 * This is meant to be called by any filesystem that uses their own dio_submit_t
355 * so that the DIO specific endio actions are dealt with after the filesystem
356 * has done it's completion work.
357 */
358void dio_end_io(struct bio *bio, int error)
359{
360 struct dio *dio = bio->bi_private;
361
362 if (dio->is_async)
363 dio_bio_end_aio(bio, error);
364 else
365 dio_bio_end_io(bio, error);
366}
367EXPORT_SYMBOL_GPL(dio_end_io);
368
David Dillow20d96002011-01-20 14:44:22 -0800369static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370dio_bio_alloc(struct dio *dio, struct block_device *bdev,
371 sector_t first_sector, int nr_vecs)
372{
373 struct bio *bio;
374
David Dillow20d96002011-01-20 14:44:22 -0800375 /*
376 * bio_alloc() is guaranteed to return a bio when called with
377 * __GFP_WAIT and we request a valid number of vectors.
378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 bio = bio_alloc(GFP_KERNEL, nr_vecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 bio->bi_bdev = bdev;
382 bio->bi_sector = first_sector;
383 if (dio->is_async)
384 bio->bi_end_io = dio_bio_end_aio;
385 else
386 bio->bi_end_io = dio_bio_end_io;
387
388 dio->bio = bio;
Josef Bacikfacd07b2010-05-23 11:00:55 -0400389 dio->logical_offset_in_bio = dio->cur_page_fs_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/*
393 * In the AIO read case we speculatively dirty the pages before starting IO.
394 * During IO completion, any of these pages which happen to have been written
395 * back will be redirtied by bio_check_pages_dirty().
Zach Brown02732012006-12-10 02:20:59 -0800396 *
397 * bios hold a dio reference between submit_bio and ->end_io.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
399static void dio_bio_submit(struct dio *dio)
400{
401 struct bio *bio = dio->bio;
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800402 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 bio->bi_private = dio;
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800405
406 spin_lock_irqsave(&dio->bio_lock, flags);
407 dio->refcount++;
408 spin_unlock_irqrestore(&dio->bio_lock, flags);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (dio->is_async && dio->rw == READ)
411 bio_set_pages_dirty(bio);
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800412
Josef Bacikfacd07b2010-05-23 11:00:55 -0400413 if (dio->submit_io)
414 dio->submit_io(dio->rw, bio, dio->inode,
415 dio->logical_offset_in_bio);
416 else
417 submit_bio(dio->rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 dio->bio = NULL;
420 dio->boundary = 0;
Josef Bacikfacd07b2010-05-23 11:00:55 -0400421 dio->logical_offset_in_bio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
425 * Release any resources in case of a failure
426 */
427static void dio_cleanup(struct dio *dio)
428{
429 while (dio_pages_present(dio))
430 page_cache_release(dio_get_page(dio));
431}
432
433/*
Zach Brown02732012006-12-10 02:20:59 -0800434 * Wait for the next BIO to complete. Remove it and return it. NULL is
435 * returned once all BIOs have been completed. This must only be called once
436 * all bios have been issued so that dio->refcount can only decrease. This
437 * requires that that the caller hold a reference on the dio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
439static struct bio *dio_await_one(struct dio *dio)
440{
441 unsigned long flags;
Zach Brown02732012006-12-10 02:20:59 -0800442 struct bio *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 spin_lock_irqsave(&dio->bio_lock, flags);
Zach Brown5eb6c7a2006-12-10 02:21:07 -0800445
446 /*
447 * Wait as long as the list is empty and there are bios in flight. bio
448 * completion drops the count, maybe adds to the list, and wakes while
449 * holding the bio_lock so we don't need set_current_state()'s barrier
450 * and can call it after testing our condition.
451 */
452 while (dio->refcount > 1 && dio->bio_list == NULL) {
453 __set_current_state(TASK_UNINTERRUPTIBLE);
454 dio->waiter = current;
455 spin_unlock_irqrestore(&dio->bio_lock, flags);
456 io_schedule();
457 /* wake up sets us TASK_RUNNING */
458 spin_lock_irqsave(&dio->bio_lock, flags);
459 dio->waiter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Zach Brown02732012006-12-10 02:20:59 -0800461 if (dio->bio_list) {
462 bio = dio->bio_list;
463 dio->bio_list = bio->bi_private;
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_unlock_irqrestore(&dio->bio_lock, flags);
466 return bio;
467}
468
469/*
470 * Process one completed BIO. No locks are held.
471 */
472static int dio_bio_complete(struct dio *dio, struct bio *bio)
473{
474 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
475 struct bio_vec *bvec = bio->bi_io_vec;
476 int page_no;
477
478 if (!uptodate)
Chen, Kenneth W174e27c2006-03-25 03:08:16 -0800479 dio->io_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (dio->is_async && dio->rw == READ) {
482 bio_check_pages_dirty(bio); /* transfers ownership */
483 } else {
484 for (page_no = 0; page_no < bio->bi_vcnt; page_no++) {
485 struct page *page = bvec[page_no].bv_page;
486
487 if (dio->rw == READ && !PageCompound(page))
488 set_page_dirty_lock(page);
489 page_cache_release(page);
490 }
491 bio_put(bio);
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return uptodate ? 0 : -EIO;
494}
495
496/*
Zach Brown02732012006-12-10 02:20:59 -0800497 * Wait on and process all in-flight BIOs. This must only be called once
498 * all bios have been issued so that the refcount can only decrease.
499 * This just waits for all bios to make it through dio_bio_complete. IO
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200500 * errors are propagated through dio->io_error and should be propagated via
Zach Brown02732012006-12-10 02:20:59 -0800501 * dio_complete().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
Zach Brown6d544bb2006-12-10 02:20:54 -0800503static void dio_await_completion(struct dio *dio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Zach Brown02732012006-12-10 02:20:59 -0800505 struct bio *bio;
506 do {
507 bio = dio_await_one(dio);
508 if (bio)
509 dio_bio_complete(dio, bio);
510 } while (bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/*
514 * A really large O_DIRECT read or write can generate a lot of BIOs. So
515 * to keep the memory consumption sane we periodically reap any completed BIOs
516 * during the BIO generation phase.
517 *
518 * This also helps to limit the peak amount of pinned userspace memory.
519 */
520static int dio_bio_reap(struct dio *dio)
521{
522 int ret = 0;
523
524 if (dio->reap_counter++ >= 64) {
525 while (dio->bio_list) {
526 unsigned long flags;
527 struct bio *bio;
528 int ret2;
529
530 spin_lock_irqsave(&dio->bio_lock, flags);
531 bio = dio->bio_list;
532 dio->bio_list = bio->bi_private;
533 spin_unlock_irqrestore(&dio->bio_lock, flags);
534 ret2 = dio_bio_complete(dio, bio);
535 if (ret == 0)
536 ret = ret2;
537 }
538 dio->reap_counter = 0;
539 }
540 return ret;
541}
542
543/*
544 * Call into the fs to map some more disk blocks. We record the current number
545 * of available blocks at dio->blocks_available. These are in units of the
546 * fs blocksize, (1 << inode->i_blkbits).
547 *
548 * The fs is allowed to map lots of blocks at once. If it wants to do that,
549 * it uses the passed inode-relative block number as the file offset, as usual.
550 *
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800551 * get_block() is passed the number of i_blkbits-sized blocks which direct_io
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * has remaining to do. The fs should not map more than this number of blocks.
553 *
554 * If the fs has mapped a lot of blocks, it should populate bh->b_size to
555 * indicate how much contiguous disk space has been made available at
556 * bh->b_blocknr.
557 *
558 * If *any* of the mapped blocks are new, then the fs must set buffer_new().
559 * This isn't very efficient...
560 *
561 * In the case of filesystem holes: the fs may return an arbitrarily-large
562 * hole by returning an appropriate value in b_size and by clearing
563 * buffer_mapped(). However the direct-io code will only process holes one
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800564 * block at a time - it will repeatedly call get_block() as it walks the hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566static int get_more_blocks(struct dio *dio)
567{
568 int ret;
569 struct buffer_head *map_bh = &dio->map_bh;
570 sector_t fs_startblk; /* Into file, in filesystem-sized blocks */
571 unsigned long fs_count; /* Number of filesystem-sized blocks */
572 unsigned long dio_count;/* Number of dio_block-sized blocks */
573 unsigned long blkmask;
574 int create;
575
576 /*
577 * If there was a memory error and we've overwritten all the
578 * mapped blocks then we can now return that memory error
579 */
580 ret = dio->page_errors;
581 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 BUG_ON(dio->block_in_file >= dio->final_block_in_request);
583 fs_startblk = dio->block_in_file >> dio->blkfactor;
584 dio_count = dio->final_block_in_request - dio->block_in_file;
585 fs_count = dio_count >> dio->blkfactor;
586 blkmask = (1 << dio->blkfactor) - 1;
587 if (dio_count & blkmask)
588 fs_count++;
589
Nathan Scott3c674e72006-03-29 09:26:15 +1000590 map_bh->b_state = 0;
591 map_bh->b_size = fs_count << dio->inode->i_blkbits;
592
Christoph Hellwig5fe878a2009-12-15 16:47:50 -0800593 /*
594 * For writes inside i_size on a DIO_SKIP_HOLES filesystem we
595 * forbid block creations: only overwrites are permitted.
596 * We will return early to the caller once we see an
597 * unmapped buffer head returned, and the caller will fall
598 * back to buffered I/O.
599 *
600 * Otherwise the decision is left to the get_blocks method,
601 * which may decide to handle it or also return an unmapped
602 * buffer head.
603 */
Jens Axboeb31dc662006-06-13 08:26:10 +0200604 create = dio->rw & WRITE;
Christoph Hellwig5fe878a2009-12-15 16:47:50 -0800605 if (dio->flags & DIO_SKIP_HOLES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (dio->block_in_file < (i_size_read(dio->inode) >>
607 dio->blkbits))
608 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Nathan Scott3c674e72006-03-29 09:26:15 +1000610
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800611 ret = (*dio->get_block)(dio->inode, fs_startblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 map_bh, create);
613 }
614 return ret;
615}
616
617/*
618 * There is no bio. Make one now.
619 */
620static int dio_new_bio(struct dio *dio, sector_t start_sector)
621{
622 sector_t sector;
623 int ret, nr_pages;
624
625 ret = dio_bio_reap(dio);
626 if (ret)
627 goto out;
628 sector = start_sector << (dio->blkbits - 9);
629 nr_pages = min(dio->pages_in_io, bio_get_nr_vecs(dio->map_bh.b_bdev));
David Dillow20d96002011-01-20 14:44:22 -0800630 nr_pages = min(nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 BUG_ON(nr_pages <= 0);
David Dillow20d96002011-01-20 14:44:22 -0800632 dio_bio_alloc(dio, dio->map_bh.b_bdev, sector, nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 dio->boundary = 0;
634out:
635 return ret;
636}
637
638/*
639 * Attempt to put the current chunk of 'cur_page' into the current BIO. If
640 * that was successful then update final_block_in_bio and take a ref against
641 * the just-added page.
642 *
643 * Return zero on success. Non-zero means the caller needs to start a new BIO.
644 */
645static int dio_bio_add_page(struct dio *dio)
646{
647 int ret;
648
649 ret = bio_add_page(dio->bio, dio->cur_page,
650 dio->cur_page_len, dio->cur_page_offset);
651 if (ret == dio->cur_page_len) {
652 /*
653 * Decrement count only, if we are done with this page
654 */
655 if ((dio->cur_page_len + dio->cur_page_offset) == PAGE_SIZE)
656 dio->pages_in_io--;
657 page_cache_get(dio->cur_page);
658 dio->final_block_in_bio = dio->cur_page_block +
659 (dio->cur_page_len >> dio->blkbits);
660 ret = 0;
661 } else {
662 ret = 1;
663 }
664 return ret;
665}
666
667/*
668 * Put cur_page under IO. The section of cur_page which is described by
669 * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
670 * starts on-disk at cur_page_block.
671 *
672 * We take a ref against the page here (on behalf of its presence in the bio).
673 *
674 * The caller of this function is responsible for removing cur_page from the
675 * dio, and for dropping the refcount which came from that presence.
676 */
677static int dio_send_cur_page(struct dio *dio)
678{
679 int ret = 0;
680
681 if (dio->bio) {
Jeff Moyer7a801ac2010-09-09 16:37:33 -0700682 loff_t cur_offset = dio->cur_page_fs_offset;
Josef Bacikc2c6ca42010-05-23 11:00:55 -0400683 loff_t bio_next_offset = dio->logical_offset_in_bio +
684 dio->bio->bi_size;
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
Josef Bacikc2c6ca42010-05-23 11:00:55 -0400687 * See whether this new request is contiguous with the old.
688 *
Namhyung Kimf0940ce2011-01-11 21:15:03 +0900689 * Btrfs cannot handle having logically non-contiguous requests
690 * submitted. For example if you have
Josef Bacikc2c6ca42010-05-23 11:00:55 -0400691 *
692 * Logical: [0-4095][HOLE][8192-12287]
Namhyung Kimf0940ce2011-01-11 21:15:03 +0900693 * Physical: [0-4095] [4096-8191]
Josef Bacikc2c6ca42010-05-23 11:00:55 -0400694 *
695 * We cannot submit those pages together as one BIO. So if our
696 * current logical offset in the file does not equal what would
697 * be the next logical offset in the bio, submit the bio we
698 * have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Josef Bacikc2c6ca42010-05-23 11:00:55 -0400700 if (dio->final_block_in_bio != dio->cur_page_block ||
701 cur_offset != bio_next_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 dio_bio_submit(dio);
703 /*
704 * Submit now if the underlying fs is about to perform a
705 * metadata read
706 */
Jeff Moyer7a801ac2010-09-09 16:37:33 -0700707 else if (dio->boundary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 dio_bio_submit(dio);
709 }
710
711 if (dio->bio == NULL) {
712 ret = dio_new_bio(dio, dio->cur_page_block);
713 if (ret)
714 goto out;
715 }
716
717 if (dio_bio_add_page(dio) != 0) {
718 dio_bio_submit(dio);
719 ret = dio_new_bio(dio, dio->cur_page_block);
720 if (ret == 0) {
721 ret = dio_bio_add_page(dio);
722 BUG_ON(ret != 0);
723 }
724 }
725out:
726 return ret;
727}
728
729/*
730 * An autonomous function to put a chunk of a page under deferred IO.
731 *
732 * The caller doesn't actually know (or care) whether this piece of page is in
733 * a BIO, or is under IO or whatever. We just take care of all possible
734 * situations here. The separation between the logic of do_direct_IO() and
735 * that of submit_page_section() is important for clarity. Please don't break.
736 *
737 * The chunk of page starts on-disk at blocknr.
738 *
739 * We perform deferred IO, by recording the last-submitted page inside our
740 * private part of the dio structure. If possible, we just expand the IO
741 * across that page here.
742 *
743 * If that doesn't work out then we put the old page into the bio and add this
744 * page to the dio instead.
745 */
746static int
747submit_page_section(struct dio *dio, struct page *page,
748 unsigned offset, unsigned len, sector_t blocknr)
749{
750 int ret = 0;
751
Andrew Morton98c4d572006-12-10 02:19:47 -0800752 if (dio->rw & WRITE) {
753 /*
754 * Read accounting is performed in submit_bio()
755 */
756 task_io_account_write(len);
757 }
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /*
760 * Can we just grow the current page's presence in the dio?
761 */
762 if ( (dio->cur_page == page) &&
763 (dio->cur_page_offset + dio->cur_page_len == offset) &&
764 (dio->cur_page_block +
765 (dio->cur_page_len >> dio->blkbits) == blocknr)) {
766 dio->cur_page_len += len;
767
768 /*
769 * If dio->boundary then we want to schedule the IO now to
770 * avoid metadata seeks.
771 */
772 if (dio->boundary) {
773 ret = dio_send_cur_page(dio);
774 page_cache_release(dio->cur_page);
775 dio->cur_page = NULL;
776 }
777 goto out;
778 }
779
780 /*
781 * If there's a deferred page already there then send it.
782 */
783 if (dio->cur_page) {
784 ret = dio_send_cur_page(dio);
785 page_cache_release(dio->cur_page);
786 dio->cur_page = NULL;
787 if (ret)
788 goto out;
789 }
790
791 page_cache_get(page); /* It is in dio */
792 dio->cur_page = page;
793 dio->cur_page_offset = offset;
794 dio->cur_page_len = len;
795 dio->cur_page_block = blocknr;
Josef Bacikfacd07b2010-05-23 11:00:55 -0400796 dio->cur_page_fs_offset = dio->block_in_file << dio->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797out:
798 return ret;
799}
800
801/*
802 * Clean any dirty buffers in the blockdev mapping which alias newly-created
803 * file blocks. Only called for S_ISREG files - blockdevs do not set
804 * buffer_new
805 */
806static void clean_blockdev_aliases(struct dio *dio)
807{
808 unsigned i;
809 unsigned nblocks;
810
811 nblocks = dio->map_bh.b_size >> dio->inode->i_blkbits;
812
813 for (i = 0; i < nblocks; i++) {
814 unmap_underlying_metadata(dio->map_bh.b_bdev,
815 dio->map_bh.b_blocknr + i);
816 }
817}
818
819/*
820 * If we are not writing the entire block and get_block() allocated
821 * the block for us, we need to fill-in the unused portion of the
822 * block with zeros. This happens only if user-buffer, fileoffset or
823 * io length is not filesystem block-size multiple.
824 *
825 * `end' is zero if we're doing the start of the IO, 1 at the end of the
826 * IO.
827 */
828static void dio_zero_block(struct dio *dio, int end)
829{
830 unsigned dio_blocks_per_fs_block;
831 unsigned this_chunk_blocks; /* In dio_blocks */
832 unsigned this_chunk_bytes;
833 struct page *page;
834
835 dio->start_zero_done = 1;
836 if (!dio->blkfactor || !buffer_new(&dio->map_bh))
837 return;
838
839 dio_blocks_per_fs_block = 1 << dio->blkfactor;
840 this_chunk_blocks = dio->block_in_file & (dio_blocks_per_fs_block - 1);
841
842 if (!this_chunk_blocks)
843 return;
844
845 /*
846 * We need to zero out part of an fs block. It is either at the
847 * beginning or the end of the fs block.
848 */
849 if (end)
850 this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
851
852 this_chunk_bytes = this_chunk_blocks << dio->blkbits;
853
Nick Piggin557ed1f2007-10-16 01:24:40 -0700854 page = ZERO_PAGE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (submit_page_section(dio, page, 0, this_chunk_bytes,
856 dio->next_block_for_io))
857 return;
858
859 dio->next_block_for_io += this_chunk_blocks;
860}
861
862/*
863 * Walk the user pages, and the file, mapping blocks to disk and generating
864 * a sequence of (page,offset,len,block) mappings. These mappings are injected
865 * into submit_page_section(), which takes care of the next stage of submission
866 *
867 * Direct IO against a blockdev is different from a file. Because we can
868 * happily perform page-sized but 512-byte aligned IOs. It is important that
869 * blockdev IO be able to have fine alignment and large sizes.
870 *
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800871 * So what we do is to permit the ->get_block function to populate bh.b_size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * with the size of IO which is permitted at this offset and this i_blkbits.
873 *
874 * For best results, the blockdev should be set up with 512-byte i_blkbits and
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800875 * it should set b_size to PAGE_SIZE or more inside get_block(). This gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * fine alignment but still allows this function to work in PAGE_SIZE units.
877 */
878static int do_direct_IO(struct dio *dio)
879{
880 const unsigned blkbits = dio->blkbits;
881 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
882 struct page *page;
883 unsigned block_in_page;
884 struct buffer_head *map_bh = &dio->map_bh;
885 int ret = 0;
886
887 /* The I/O can start at any block offset within the first page */
888 block_in_page = dio->first_block_in_page;
889
890 while (dio->block_in_file < dio->final_block_in_request) {
891 page = dio_get_page(dio);
892 if (IS_ERR(page)) {
893 ret = PTR_ERR(page);
894 goto out;
895 }
896
897 while (block_in_page < blocks_per_page) {
898 unsigned offset_in_page = block_in_page << blkbits;
899 unsigned this_chunk_bytes; /* # of bytes mapped */
900 unsigned this_chunk_blocks; /* # of blocks */
901 unsigned u;
902
903 if (dio->blocks_available == 0) {
904 /*
905 * Need to go and map some more disk
906 */
907 unsigned long blkmask;
908 unsigned long dio_remainder;
909
910 ret = get_more_blocks(dio);
911 if (ret) {
912 page_cache_release(page);
913 goto out;
914 }
915 if (!buffer_mapped(map_bh))
916 goto do_holes;
917
918 dio->blocks_available =
919 map_bh->b_size >> dio->blkbits;
920 dio->next_block_for_io =
921 map_bh->b_blocknr << dio->blkfactor;
922 if (buffer_new(map_bh))
923 clean_blockdev_aliases(dio);
924
925 if (!dio->blkfactor)
926 goto do_holes;
927
928 blkmask = (1 << dio->blkfactor) - 1;
929 dio_remainder = (dio->block_in_file & blkmask);
930
931 /*
932 * If we are at the start of IO and that IO
933 * starts partway into a fs-block,
934 * dio_remainder will be non-zero. If the IO
935 * is a read then we can simply advance the IO
936 * cursor to the first block which is to be
937 * read. But if the IO is a write and the
938 * block was newly allocated we cannot do that;
939 * the start of the fs block must be zeroed out
940 * on-disk
941 */
942 if (!buffer_new(map_bh))
943 dio->next_block_for_io += dio_remainder;
944 dio->blocks_available -= dio_remainder;
945 }
946do_holes:
947 /* Handle holes */
948 if (!buffer_mapped(map_bh)) {
Jeff Moyer35dc8162006-02-03 03:04:27 -0800949 loff_t i_size_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* AKPM: eargh, -ENOTBLK is a hack */
Jens Axboeb31dc662006-06-13 08:26:10 +0200952 if (dio->rw & WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 page_cache_release(page);
954 return -ENOTBLK;
955 }
956
Jeff Moyer35dc8162006-02-03 03:04:27 -0800957 /*
958 * Be sure to account for a partial block as the
959 * last block in the file
960 */
961 i_size_aligned = ALIGN(i_size_read(dio->inode),
962 1 << blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (dio->block_in_file >=
Jeff Moyer35dc8162006-02-03 03:04:27 -0800964 i_size_aligned >> blkbits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* We hit eof */
966 page_cache_release(page);
967 goto out;
968 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800969 zero_user(page, block_in_page << blkbits,
970 1 << blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 dio->block_in_file++;
972 block_in_page++;
973 goto next_block;
974 }
975
976 /*
977 * If we're performing IO which has an alignment which
978 * is finer than the underlying fs, go check to see if
979 * we must zero out the start of this block.
980 */
981 if (unlikely(dio->blkfactor && !dio->start_zero_done))
982 dio_zero_block(dio, 0);
983
984 /*
985 * Work out, in this_chunk_blocks, how much disk we
986 * can add to this page
987 */
988 this_chunk_blocks = dio->blocks_available;
989 u = (PAGE_SIZE - offset_in_page) >> blkbits;
990 if (this_chunk_blocks > u)
991 this_chunk_blocks = u;
992 u = dio->final_block_in_request - dio->block_in_file;
993 if (this_chunk_blocks > u)
994 this_chunk_blocks = u;
995 this_chunk_bytes = this_chunk_blocks << blkbits;
996 BUG_ON(this_chunk_bytes == 0);
997
998 dio->boundary = buffer_boundary(map_bh);
999 ret = submit_page_section(dio, page, offset_in_page,
1000 this_chunk_bytes, dio->next_block_for_io);
1001 if (ret) {
1002 page_cache_release(page);
1003 goto out;
1004 }
1005 dio->next_block_for_io += this_chunk_blocks;
1006
1007 dio->block_in_file += this_chunk_blocks;
1008 block_in_page += this_chunk_blocks;
1009 dio->blocks_available -= this_chunk_blocks;
1010next_block:
Eric Sesterhennd4569d22006-04-01 01:10:13 +02001011 BUG_ON(dio->block_in_file > dio->final_block_in_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (dio->block_in_file == dio->final_block_in_request)
1013 break;
1014 }
1015
1016 /* Drop the ref which was taken in get_user_pages() */
1017 page_cache_release(page);
1018 block_in_page = 0;
1019 }
1020out:
1021 return ret;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static ssize_t
1025direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
1026 const struct iovec *iov, loff_t offset, unsigned long nr_segs,
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -08001027 unsigned blkbits, get_block_t get_block, dio_iodone_t end_io,
Josef Bacikfacd07b2010-05-23 11:00:55 -04001028 dio_submit_t submit_io, struct dio *dio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 unsigned long user_addr;
Zach Brown5eb6c7a2006-12-10 02:21:07 -08001031 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int seg;
1033 ssize_t ret = 0;
1034 ssize_t ret2;
1035 size_t bytes;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dio->inode = inode;
1038 dio->rw = rw;
1039 dio->blkbits = blkbits;
1040 dio->blkfactor = inode->i_blkbits - blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 dio->block_in_file = offset >> blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -08001043 dio->get_block = get_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dio->end_io = end_io;
Josef Bacikfacd07b2010-05-23 11:00:55 -04001045 dio->submit_io = submit_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 dio->final_block_in_bio = -1;
1047 dio->next_block_for_io = -1;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 dio->iocb = iocb;
Daniel McNeil29504ff2005-04-16 15:25:50 -07001050 dio->i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 spin_lock_init(&dio->bio_lock);
Zach Brown5eb6c7a2006-12-10 02:21:07 -08001053 dio->refcount = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 /*
1056 * In case of non-aligned buffers, we may need 2 more
1057 * pages since we need to zero out first and last block.
1058 */
1059 if (unlikely(dio->blkfactor))
1060 dio->pages_in_io = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 for (seg = 0; seg < nr_segs; seg++) {
1063 user_addr = (unsigned long)iov[seg].iov_base;
1064 dio->pages_in_io +=
1065 ((user_addr+iov[seg].iov_len +PAGE_SIZE-1)/PAGE_SIZE
1066 - user_addr/PAGE_SIZE);
1067 }
1068
1069 for (seg = 0; seg < nr_segs; seg++) {
1070 user_addr = (unsigned long)iov[seg].iov_base;
1071 dio->size += bytes = iov[seg].iov_len;
1072
1073 /* Index into the first page of the first block */
1074 dio->first_block_in_page = (user_addr & ~PAGE_MASK) >> blkbits;
1075 dio->final_block_in_request = dio->block_in_file +
1076 (bytes >> blkbits);
1077 /* Page fetching state */
1078 dio->head = 0;
1079 dio->tail = 0;
1080 dio->curr_page = 0;
1081
1082 dio->total_pages = 0;
1083 if (user_addr & (PAGE_SIZE-1)) {
1084 dio->total_pages++;
1085 bytes -= PAGE_SIZE - (user_addr & (PAGE_SIZE - 1));
1086 }
1087 dio->total_pages += (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1088 dio->curr_user_address = user_addr;
1089
1090 ret = do_direct_IO(dio);
1091
1092 dio->result += iov[seg].iov_len -
1093 ((dio->final_block_in_request - dio->block_in_file) <<
1094 blkbits);
1095
1096 if (ret) {
1097 dio_cleanup(dio);
1098 break;
1099 }
1100 } /* end iovec loop */
1101
Josef Bacikfacd07b2010-05-23 11:00:55 -04001102 if (ret == -ENOTBLK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
1104 * The remaining part of the request will be
1105 * be handled by buffered I/O when we return
1106 */
1107 ret = 0;
1108 }
1109 /*
1110 * There may be some unwritten disk at the end of a part-written
1111 * fs-block-sized block. Go zero that now.
1112 */
1113 dio_zero_block(dio, 1);
1114
1115 if (dio->cur_page) {
1116 ret2 = dio_send_cur_page(dio);
1117 if (ret == 0)
1118 ret = ret2;
1119 page_cache_release(dio->cur_page);
1120 dio->cur_page = NULL;
1121 }
1122 if (dio->bio)
1123 dio_bio_submit(dio);
1124
1125 /*
1126 * It is possible that, we return short IO due to end of file.
1127 * In that case, we need to release all the pages we got hold on.
1128 */
1129 dio_cleanup(dio);
1130
1131 /*
1132 * All block lookups have been performed. For READ requests
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001133 * we can let i_mutex go now that its achieved its purpose
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * of protecting us from looking up uninitialized blocks.
1135 */
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001136 if (rw == READ && (dio->flags & DIO_LOCKING))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001137 mutex_unlock(&dio->inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /*
Zach Brown8459d862006-12-10 02:21:05 -08001140 * The only time we want to leave bios in flight is when a successful
1141 * partial aio read or full aio write have been setup. In that case
1142 * bio completion will call aio_complete. The only time it's safe to
1143 * call aio_complete is when we return -EIOCBQUEUED, so we key on that.
1144 * This had *better* be the only place that raises -EIOCBQUEUED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
Zach Brown8459d862006-12-10 02:21:05 -08001146 BUG_ON(ret == -EIOCBQUEUED);
1147 if (dio->is_async && ret == 0 && dio->result &&
1148 ((rw & READ) || (dio->result == dio->size)))
1149 ret = -EIOCBQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jens Axboe7eaceac2011-03-10 08:52:07 +01001151 if (ret != -EIOCBQUEUED)
Zach Brown6d544bb2006-12-10 02:20:54 -08001152 dio_await_completion(dio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Zach Brown8459d862006-12-10 02:21:05 -08001154 /*
1155 * Sync will always be dropping the final ref and completing the
Zach Brown5eb6c7a2006-12-10 02:21:07 -08001156 * operation. AIO can if it was a broken operation described above or
1157 * in fact if all the bios race to complete before we get here. In
1158 * that case dio_complete() translates the EIOCBQUEUED into the proper
1159 * return code that the caller will hand to aio_complete().
1160 *
1161 * This is managed by the bio_lock instead of being an atomic_t so that
1162 * completion paths can drop their ref and use the remaining count to
1163 * decide to wake the submission path atomically.
Zach Brown8459d862006-12-10 02:21:05 -08001164 */
Zach Brown5eb6c7a2006-12-10 02:21:07 -08001165 spin_lock_irqsave(&dio->bio_lock, flags);
1166 ret2 = --dio->refcount;
1167 spin_unlock_irqrestore(&dio->bio_lock, flags);
Zach Brownfcb82f82007-07-03 15:28:55 -07001168
Zach Brown5eb6c7a2006-12-10 02:21:07 -08001169 if (ret2 == 0) {
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001170 ret = dio_complete(dio, offset, ret, false);
Zach Brown8459d862006-12-10 02:21:05 -08001171 kfree(dio);
1172 } else
1173 BUG_ON(ret != -EIOCBQUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return ret;
1176}
1177
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001178/*
1179 * This is a library function for use by filesystem drivers.
1180 *
1181 * The locking rules are governed by the flags parameter:
1182 * - if the flags value contains DIO_LOCKING we use a fancy locking
1183 * scheme for dumb filesystems.
1184 * For writes this function is called under i_mutex and returns with
1185 * i_mutex held, for reads, i_mutex is not held on entry, but it is
1186 * taken and dropped again before returning.
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001187 * - if the flags value does NOT contain DIO_LOCKING we don't use any
1188 * internal locking but rather rely on the filesystem to synchronize
1189 * direct I/O reads/writes versus each other and truncate.
Christoph Hellwigdf2d6f22011-06-24 14:29:46 -04001190 *
1191 * To help with locking against truncate we incremented the i_dio_count
1192 * counter before starting direct I/O, and decrement it once we are done.
1193 * Truncate can wait for it to reach zero to provide exclusion. It is
1194 * expected that filesystem provide exclusion between new direct I/O
1195 * and truncates. For DIO_LOCKING filesystems this is done by i_mutex,
1196 * but other filesystems need to take care of this on their own.
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198ssize_t
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001199__blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct block_device *bdev, const struct iovec *iov, loff_t offset,
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -08001201 unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
Josef Bacikfacd07b2010-05-23 11:00:55 -04001202 dio_submit_t submit_io, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 int seg;
1205 size_t size;
1206 unsigned long addr;
1207 unsigned blkbits = inode->i_blkbits;
1208 unsigned bdev_blkbits = 0;
1209 unsigned blocksize_mask = (1 << blkbits) - 1;
1210 ssize_t retval = -EINVAL;
1211 loff_t end = offset;
1212 struct dio *dio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (rw & WRITE)
Jens Axboe721a9602011-03-09 11:56:30 +01001215 rw = WRITE_ODIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (bdev)
Martin K. Petersene1defc42009-05-22 17:17:49 -04001218 bdev_blkbits = blksize_bits(bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 if (offset & blocksize_mask) {
1221 if (bdev)
1222 blkbits = bdev_blkbits;
1223 blocksize_mask = (1 << blkbits) - 1;
1224 if (offset & blocksize_mask)
1225 goto out;
1226 }
1227
1228 /* Check the memory alignment. Blocks cannot straddle pages */
1229 for (seg = 0; seg < nr_segs; seg++) {
1230 addr = (unsigned long)iov[seg].iov_base;
1231 size = iov[seg].iov_len;
1232 end += size;
1233 if ((addr & blocksize_mask) || (size & blocksize_mask)) {
1234 if (bdev)
1235 blkbits = bdev_blkbits;
1236 blocksize_mask = (1 << blkbits) - 1;
1237 if ((addr & blocksize_mask) || (size & blocksize_mask))
1238 goto out;
1239 }
1240 }
1241
Christoph Hellwigf9b55702011-06-24 14:29:42 -04001242 /* watch out for a 0 len io from a tricksy fs */
1243 if (rw == READ && end == offset)
1244 return 0;
1245
Jeff Moyer23aee092009-12-15 16:47:49 -08001246 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 retval = -ENOMEM;
1248 if (!dio)
1249 goto out;
Jeff Moyer23aee092009-12-15 16:47:49 -08001250 /*
1251 * Believe it or not, zeroing out the page array caused a .5%
1252 * performance regression in a database benchmark. So, we take
1253 * care to only zero out what's needed.
1254 */
1255 memset(dio, 0, offsetof(struct dio, pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001257 dio->flags = flags;
1258 if (dio->flags & DIO_LOCKING) {
Christoph Hellwigf9b55702011-06-24 14:29:42 -04001259 if (rw == READ) {
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001260 struct address_space *mapping =
1261 iocb->ki_filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001263 /* will be released by direct_io_worker */
1264 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 retval = filemap_write_and_wait_range(mapping, offset,
1267 end - 1);
1268 if (retval) {
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001269 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 kfree(dio);
1271 goto out;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
1276 /*
Christoph Hellwigdf2d6f22011-06-24 14:29:46 -04001277 * Will be decremented at I/O completion time.
1278 */
1279 atomic_inc(&inode->i_dio_count);
1280
1281 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * For file extending writes updating i_size before data
1283 * writeouts complete can expose uninitialized blocks. So
1284 * even for AIO, we need to wait for i/o to complete before
1285 * returning in this case.
1286 */
Jens Axboeb31dc662006-06-13 08:26:10 +02001287 dio->is_async = !is_sync_kiocb(iocb) && !((rw & WRITE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 (end > i_size_read(inode)));
1289
1290 retval = direct_io_worker(rw, iocb, inode, iov, offset,
Josef Bacikfacd07b2010-05-23 11:00:55 -04001291 nr_segs, blkbits, get_block, end_io,
1292 submit_io, dio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
npiggin@suse.de7bb46a62010-05-27 01:05:33 +10001294out:
1295 return retval;
1296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297EXPORT_SYMBOL(__blockdev_direct_IO);