4 * Copyright (C) 2002, Linus Torvalds.
8 * 04Jul2002 Andrew Morton
10 * 11Sep2002 janetinc@us.ibm.com
11 * added readv/writev support.
12 * 29Oct2002 Andrew Morton
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.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
29 #include <linux/pagemap.h>
30 #include <linux/task_io_accounting_ops.h>
31 #include <linux/bio.h>
32 #include <linux/wait.h>
33 #include <linux/err.h>
34 #include <linux/blkdev.h>
35 #include <linux/buffer_head.h>
36 #include <linux/rwsem.h>
37 #include <linux/uio.h>
38 #include <asm/atomic.h>
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.
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.
54 * If blkfactor is zero then the user's request was aligned to the filesystem's
59 /* BIO submission state */
60 struct bio
*bio
; /* bio under assembly */
63 loff_t i_size
; /* i_size when submitted */
64 int flags
; /* doesn't change */
65 unsigned blkbits
; /* doesn't change */
66 unsigned blkfactor
; /* When we're using an alignment which
67 is finer than the filesystem's soft
68 blocksize, this specifies how much
69 finer. blkfactor=2 means 1/4-block
70 alignment. Does not change */
71 unsigned start_zero_done
; /* flag: sub-blocksize zeroing has
72 been performed at the start of a
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 */
83 get_block_t
*get_block
; /* block mapping function */
84 dio_iodone_t
*end_io
; /* IO completion function */
85 dio_submit_t
*submit_io
; /* IO submition function */
86 loff_t logical_offset_in_bio
; /* current first logical block in bio */
87 sector_t final_block_in_bio
; /* current final block in bio + 1 */
88 sector_t next_block_for_io
; /* next block to be put under IO,
89 in dio_blocks units */
90 struct buffer_head map_bh
; /* last get_block() result */
93 * Deferred addition of a page to the dio. These variables are
94 * private to dio_send_cur_page(), submit_page_section() and
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 */
101 loff_t cur_page_fs_offset
; /* Offset in file */
103 /* BIO completion state */
104 spinlock_t bio_lock
; /* protects BIO fields below */
105 unsigned long refcount
; /* direct_io_worker() and bios */
106 struct bio
*bio_list
; /* singly linked via bi_private */
107 struct task_struct
*waiter
; /* waiting task (NULL if none) */
109 /* AIO related stuff */
110 struct kiocb
*iocb
; /* kiocb */
111 int is_async
; /* is IO async ? */
112 int io_error
; /* IO error in completion path */
113 ssize_t result
; /* IO result */
116 * Page fetching state. These variables belong to dio_refill_pages().
118 int curr_page
; /* changes */
119 int total_pages
; /* doesn't change */
120 unsigned long curr_user_address
;/* changes */
123 * Page queue. These variables belong to dio_refill_pages() and
126 unsigned head
; /* next page to process */
127 unsigned tail
; /* last valid page + 1 */
128 int page_errors
; /* errno from get_user_pages() */
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.
135 struct page
*pages
[DIO_PAGES
]; /* page buffer */
139 * How many pages are in the queue?
141 static inline unsigned dio_pages_present(struct dio
*dio
)
143 return dio
->tail
- dio
->head
;
147 * Go grab and pin some userspace pages. Typically we'll get 64 at a time.
149 static int dio_refill_pages(struct dio
*dio
)
154 nr_pages
= min(dio
->total_pages
- dio
->curr_page
, DIO_PAGES
);
155 ret
= get_user_pages_fast(
156 dio
->curr_user_address
, /* Where from? */
157 nr_pages
, /* How many pages? */
158 dio
->rw
== READ
, /* Write to memory? */
159 &dio
->pages
[0]); /* Put results here */
161 if (ret
< 0 && dio
->blocks_available
&& (dio
->rw
& WRITE
)) {
162 struct page
*page
= ZERO_PAGE(0);
164 * A memory fault, but the filesystem has some outstanding
165 * mapped blocks. We need to use those blocks up to avoid
166 * leaking stale data in the file.
168 if (dio
->page_errors
== 0)
169 dio
->page_errors
= ret
;
170 page_cache_get(page
);
171 dio
->pages
[0] = page
;
179 dio
->curr_user_address
+= ret
* PAGE_SIZE
;
180 dio
->curr_page
+= ret
;
190 * Get another userspace page. Returns an ERR_PTR on error. Pages are
191 * buffered inside the dio so that we can call get_user_pages() against a
192 * decent number of pages, less frequently. To provide nicer use of the
195 static struct page
*dio_get_page(struct dio
*dio
)
197 if (dio_pages_present(dio
) == 0) {
200 ret
= dio_refill_pages(dio
);
203 BUG_ON(dio_pages_present(dio
) == 0);
205 return dio
->pages
[dio
->head
++];
209 * dio_complete() - called when all DIO BIO I/O has been completed
210 * @offset: the byte offset in the file of the completed operation
212 * This releases locks as dictated by the locking type, lets interested parties
213 * know that a DIO operation has completed, and calculates the resulting return
214 * code for the operation.
216 * It lets the filesystem know if it registered an interest earlier via
217 * get_block. Pass the private field of the map buffer_head so that
218 * filesystems can use it to hold additional state between get_block calls and
221 static int dio_complete(struct dio
*dio
, loff_t offset
, int ret
)
223 ssize_t transferred
= 0;
226 * AIO submission can race with bio completion to get here while
227 * expecting to have the last io completed by bio completion.
228 * In that case -EIOCBQUEUED is in fact not an error we want
229 * to preserve through this call.
231 if (ret
== -EIOCBQUEUED
)
235 transferred
= dio
->result
;
237 /* Check for short read case */
238 if ((dio
->rw
== READ
) && ((offset
+ transferred
) > dio
->i_size
))
239 transferred
= dio
->i_size
- offset
;
242 if (dio
->end_io
&& dio
->result
)
243 dio
->end_io(dio
->iocb
, offset
, transferred
,
244 dio
->map_bh
.b_private
);
246 if (dio
->flags
& DIO_LOCKING
)
247 /* lockdep: non-owner release */
248 up_read_non_owner(&dio
->inode
->i_alloc_sem
);
251 ret
= dio
->page_errors
;
260 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
);
262 * Asynchronous IO callback.
264 static void dio_bio_end_aio(struct bio
*bio
, int error
)
266 struct dio
*dio
= bio
->bi_private
;
267 unsigned long remaining
;
270 /* cleanup the bio */
271 dio_bio_complete(dio
, bio
);
273 spin_lock_irqsave(&dio
->bio_lock
, flags
);
274 remaining
= --dio
->refcount
;
275 if (remaining
== 1 && dio
->waiter
)
276 wake_up_process(dio
->waiter
);
277 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
279 if (remaining
== 0) {
280 int ret
= dio_complete(dio
, dio
->iocb
->ki_pos
, 0);
281 aio_complete(dio
->iocb
, ret
, 0);
287 * The BIO completion handler simply queues the BIO up for the process-context
290 * During I/O bi_private points at the dio. After I/O, bi_private is used to
291 * implement a singly-linked list of completed BIOs, at dio->bio_list.
293 static void dio_bio_end_io(struct bio
*bio
, int error
)
295 struct dio
*dio
= bio
->bi_private
;
298 spin_lock_irqsave(&dio
->bio_lock
, flags
);
299 bio
->bi_private
= dio
->bio_list
;
301 if (--dio
->refcount
== 1 && dio
->waiter
)
302 wake_up_process(dio
->waiter
);
303 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
307 * dio_end_io - handle the end io action for the given bio
308 * @bio: The direct io bio thats being completed
309 * @error: Error if there was one
311 * This is meant to be called by any filesystem that uses their own dio_submit_t
312 * so that the DIO specific endio actions are dealt with after the filesystem
313 * has done it's completion work.
315 void dio_end_io(struct bio
*bio
, int error
)
317 struct dio
*dio
= bio
->bi_private
;
320 dio_bio_end_aio(bio
, error
);
322 dio_bio_end_io(bio
, error
);
324 EXPORT_SYMBOL_GPL(dio_end_io
);
327 dio_bio_alloc(struct dio
*dio
, struct block_device
*bdev
,
328 sector_t first_sector
, int nr_vecs
)
332 bio
= bio_alloc(GFP_KERNEL
, nr_vecs
);
335 bio
->bi_sector
= first_sector
;
337 bio
->bi_end_io
= dio_bio_end_aio
;
339 bio
->bi_end_io
= dio_bio_end_io
;
342 dio
->logical_offset_in_bio
= dio
->cur_page_fs_offset
;
347 * In the AIO read case we speculatively dirty the pages before starting IO.
348 * During IO completion, any of these pages which happen to have been written
349 * back will be redirtied by bio_check_pages_dirty().
351 * bios hold a dio reference between submit_bio and ->end_io.
353 static void dio_bio_submit(struct dio
*dio
)
355 struct bio
*bio
= dio
->bio
;
358 bio
->bi_private
= dio
;
360 spin_lock_irqsave(&dio
->bio_lock
, flags
);
362 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
364 if (dio
->is_async
&& dio
->rw
== READ
)
365 bio_set_pages_dirty(bio
);
368 dio
->submit_io(dio
->rw
, bio
, dio
->inode
,
369 dio
->logical_offset_in_bio
);
371 submit_bio(dio
->rw
, bio
);
375 dio
->logical_offset_in_bio
= 0;
379 * Release any resources in case of a failure
381 static void dio_cleanup(struct dio
*dio
)
383 while (dio_pages_present(dio
))
384 page_cache_release(dio_get_page(dio
));
388 * Wait for the next BIO to complete. Remove it and return it. NULL is
389 * returned once all BIOs have been completed. This must only be called once
390 * all bios have been issued so that dio->refcount can only decrease. This
391 * requires that that the caller hold a reference on the dio.
393 static struct bio
*dio_await_one(struct dio
*dio
)
396 struct bio
*bio
= NULL
;
398 spin_lock_irqsave(&dio
->bio_lock
, flags
);
401 * Wait as long as the list is empty and there are bios in flight. bio
402 * completion drops the count, maybe adds to the list, and wakes while
403 * holding the bio_lock so we don't need set_current_state()'s barrier
404 * and can call it after testing our condition.
406 while (dio
->refcount
> 1 && dio
->bio_list
== NULL
) {
407 __set_current_state(TASK_UNINTERRUPTIBLE
);
408 dio
->waiter
= current
;
409 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
411 /* wake up sets us TASK_RUNNING */
412 spin_lock_irqsave(&dio
->bio_lock
, flags
);
417 dio
->bio_list
= bio
->bi_private
;
419 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
424 * Process one completed BIO. No locks are held.
426 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
)
428 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
429 struct bio_vec
*bvec
= bio
->bi_io_vec
;
433 dio
->io_error
= -EIO
;
435 if (dio
->is_async
&& dio
->rw
== READ
) {
436 bio_check_pages_dirty(bio
); /* transfers ownership */
438 for (page_no
= 0; page_no
< bio
->bi_vcnt
; page_no
++) {
439 struct page
*page
= bvec
[page_no
].bv_page
;
441 if (dio
->rw
== READ
&& !PageCompound(page
))
442 set_page_dirty_lock(page
);
443 page_cache_release(page
);
447 return uptodate
? 0 : -EIO
;
451 * Wait on and process all in-flight BIOs. This must only be called once
452 * all bios have been issued so that the refcount can only decrease.
453 * This just waits for all bios to make it through dio_bio_complete. IO
454 * errors are propagated through dio->io_error and should be propagated via
457 static void dio_await_completion(struct dio
*dio
)
461 bio
= dio_await_one(dio
);
463 dio_bio_complete(dio
, bio
);
468 * A really large O_DIRECT read or write can generate a lot of BIOs. So
469 * to keep the memory consumption sane we periodically reap any completed BIOs
470 * during the BIO generation phase.
472 * This also helps to limit the peak amount of pinned userspace memory.
474 static int dio_bio_reap(struct dio
*dio
)
478 if (dio
->reap_counter
++ >= 64) {
479 while (dio
->bio_list
) {
484 spin_lock_irqsave(&dio
->bio_lock
, flags
);
486 dio
->bio_list
= bio
->bi_private
;
487 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
488 ret2
= dio_bio_complete(dio
, bio
);
492 dio
->reap_counter
= 0;
498 * Call into the fs to map some more disk blocks. We record the current number
499 * of available blocks at dio->blocks_available. These are in units of the
500 * fs blocksize, (1 << inode->i_blkbits).
502 * The fs is allowed to map lots of blocks at once. If it wants to do that,
503 * it uses the passed inode-relative block number as the file offset, as usual.
505 * get_block() is passed the number of i_blkbits-sized blocks which direct_io
506 * has remaining to do. The fs should not map more than this number of blocks.
508 * If the fs has mapped a lot of blocks, it should populate bh->b_size to
509 * indicate how much contiguous disk space has been made available at
512 * If *any* of the mapped blocks are new, then the fs must set buffer_new().
513 * This isn't very efficient...
515 * In the case of filesystem holes: the fs may return an arbitrarily-large
516 * hole by returning an appropriate value in b_size and by clearing
517 * buffer_mapped(). However the direct-io code will only process holes one
518 * block at a time - it will repeatedly call get_block() as it walks the hole.
520 static int get_more_blocks(struct dio
*dio
)
523 struct buffer_head
*map_bh
= &dio
->map_bh
;
524 sector_t fs_startblk
; /* Into file, in filesystem-sized blocks */
525 unsigned long fs_count
; /* Number of filesystem-sized blocks */
526 unsigned long dio_count
;/* Number of dio_block-sized blocks */
527 unsigned long blkmask
;
531 * If there was a memory error and we've overwritten all the
532 * mapped blocks then we can now return that memory error
534 ret
= dio
->page_errors
;
536 BUG_ON(dio
->block_in_file
>= dio
->final_block_in_request
);
537 fs_startblk
= dio
->block_in_file
>> dio
->blkfactor
;
538 dio_count
= dio
->final_block_in_request
- dio
->block_in_file
;
539 fs_count
= dio_count
>> dio
->blkfactor
;
540 blkmask
= (1 << dio
->blkfactor
) - 1;
541 if (dio_count
& blkmask
)
545 map_bh
->b_size
= fs_count
<< dio
->inode
->i_blkbits
;
548 * For writes inside i_size on a DIO_SKIP_HOLES filesystem we
549 * forbid block creations: only overwrites are permitted.
550 * We will return early to the caller once we see an
551 * unmapped buffer head returned, and the caller will fall
552 * back to buffered I/O.
554 * Otherwise the decision is left to the get_blocks method,
555 * which may decide to handle it or also return an unmapped
558 create
= dio
->rw
& WRITE
;
559 if (dio
->flags
& DIO_SKIP_HOLES
) {
560 if (dio
->block_in_file
< (i_size_read(dio
->inode
) >>
565 ret
= (*dio
->get_block
)(dio
->inode
, fs_startblk
,
572 * There is no bio. Make one now.
574 static int dio_new_bio(struct dio
*dio
, sector_t start_sector
)
579 ret
= dio_bio_reap(dio
);
582 sector
= start_sector
<< (dio
->blkbits
- 9);
583 nr_pages
= min(dio
->pages_in_io
, bio_get_nr_vecs(dio
->map_bh
.b_bdev
));
584 BUG_ON(nr_pages
<= 0);
585 ret
= dio_bio_alloc(dio
, dio
->map_bh
.b_bdev
, sector
, nr_pages
);
592 * Attempt to put the current chunk of 'cur_page' into the current BIO. If
593 * that was successful then update final_block_in_bio and take a ref against
594 * the just-added page.
596 * Return zero on success. Non-zero means the caller needs to start a new BIO.
598 static int dio_bio_add_page(struct dio
*dio
)
602 ret
= bio_add_page(dio
->bio
, dio
->cur_page
,
603 dio
->cur_page_len
, dio
->cur_page_offset
);
604 if (ret
== dio
->cur_page_len
) {
606 * Decrement count only, if we are done with this page
608 if ((dio
->cur_page_len
+ dio
->cur_page_offset
) == PAGE_SIZE
)
610 page_cache_get(dio
->cur_page
);
611 dio
->final_block_in_bio
= dio
->cur_page_block
+
612 (dio
->cur_page_len
>> dio
->blkbits
);
621 * Put cur_page under IO. The section of cur_page which is described by
622 * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
623 * starts on-disk at cur_page_block.
625 * We take a ref against the page here (on behalf of its presence in the bio).
627 * The caller of this function is responsible for removing cur_page from the
628 * dio, and for dropping the refcount which came from that presence.
630 static int dio_send_cur_page(struct dio
*dio
)
636 * See whether this new request is contiguous with the old
638 if (dio
->final_block_in_bio
!= dio
->cur_page_block
)
641 * Submit now if the underlying fs is about to perform a
648 if (dio
->bio
== NULL
) {
649 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
654 if (dio_bio_add_page(dio
) != 0) {
656 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
658 ret
= dio_bio_add_page(dio
);
667 * An autonomous function to put a chunk of a page under deferred IO.
669 * The caller doesn't actually know (or care) whether this piece of page is in
670 * a BIO, or is under IO or whatever. We just take care of all possible
671 * situations here. The separation between the logic of do_direct_IO() and
672 * that of submit_page_section() is important for clarity. Please don't break.
674 * The chunk of page starts on-disk at blocknr.
676 * We perform deferred IO, by recording the last-submitted page inside our
677 * private part of the dio structure. If possible, we just expand the IO
678 * across that page here.
680 * If that doesn't work out then we put the old page into the bio and add this
681 * page to the dio instead.
684 submit_page_section(struct dio
*dio
, struct page
*page
,
685 unsigned offset
, unsigned len
, sector_t blocknr
)
689 if (dio
->rw
& WRITE
) {
691 * Read accounting is performed in submit_bio()
693 task_io_account_write(len
);
697 * Can we just grow the current page's presence in the dio?
699 if ( (dio
->cur_page
== page
) &&
700 (dio
->cur_page_offset
+ dio
->cur_page_len
== offset
) &&
701 (dio
->cur_page_block
+
702 (dio
->cur_page_len
>> dio
->blkbits
) == blocknr
)) {
703 dio
->cur_page_len
+= len
;
706 * If dio->boundary then we want to schedule the IO now to
707 * avoid metadata seeks.
710 ret
= dio_send_cur_page(dio
);
711 page_cache_release(dio
->cur_page
);
712 dio
->cur_page
= NULL
;
718 * If there's a deferred page already there then send it.
721 ret
= dio_send_cur_page(dio
);
722 page_cache_release(dio
->cur_page
);
723 dio
->cur_page
= NULL
;
728 page_cache_get(page
); /* It is in dio */
729 dio
->cur_page
= page
;
730 dio
->cur_page_offset
= offset
;
731 dio
->cur_page_len
= len
;
732 dio
->cur_page_block
= blocknr
;
733 dio
->cur_page_fs_offset
= dio
->block_in_file
<< dio
->blkbits
;
739 * Clean any dirty buffers in the blockdev mapping which alias newly-created
740 * file blocks. Only called for S_ISREG files - blockdevs do not set
743 static void clean_blockdev_aliases(struct dio
*dio
)
748 nblocks
= dio
->map_bh
.b_size
>> dio
->inode
->i_blkbits
;
750 for (i
= 0; i
< nblocks
; i
++) {
751 unmap_underlying_metadata(dio
->map_bh
.b_bdev
,
752 dio
->map_bh
.b_blocknr
+ i
);
757 * If we are not writing the entire block and get_block() allocated
758 * the block for us, we need to fill-in the unused portion of the
759 * block with zeros. This happens only if user-buffer, fileoffset or
760 * io length is not filesystem block-size multiple.
762 * `end' is zero if we're doing the start of the IO, 1 at the end of the
765 static void dio_zero_block(struct dio
*dio
, int end
)
767 unsigned dio_blocks_per_fs_block
;
768 unsigned this_chunk_blocks
; /* In dio_blocks */
769 unsigned this_chunk_bytes
;
772 dio
->start_zero_done
= 1;
773 if (!dio
->blkfactor
|| !buffer_new(&dio
->map_bh
))
776 dio_blocks_per_fs_block
= 1 << dio
->blkfactor
;
777 this_chunk_blocks
= dio
->block_in_file
& (dio_blocks_per_fs_block
- 1);
779 if (!this_chunk_blocks
)
783 * We need to zero out part of an fs block. It is either at the
784 * beginning or the end of the fs block.
787 this_chunk_blocks
= dio_blocks_per_fs_block
- this_chunk_blocks
;
789 this_chunk_bytes
= this_chunk_blocks
<< dio
->blkbits
;
792 if (submit_page_section(dio
, page
, 0, this_chunk_bytes
,
793 dio
->next_block_for_io
))
796 dio
->next_block_for_io
+= this_chunk_blocks
;
800 * Walk the user pages, and the file, mapping blocks to disk and generating
801 * a sequence of (page,offset,len,block) mappings. These mappings are injected
802 * into submit_page_section(), which takes care of the next stage of submission
804 * Direct IO against a blockdev is different from a file. Because we can
805 * happily perform page-sized but 512-byte aligned IOs. It is important that
806 * blockdev IO be able to have fine alignment and large sizes.
808 * So what we do is to permit the ->get_block function to populate bh.b_size
809 * with the size of IO which is permitted at this offset and this i_blkbits.
811 * For best results, the blockdev should be set up with 512-byte i_blkbits and
812 * it should set b_size to PAGE_SIZE or more inside get_block(). This gives
813 * fine alignment but still allows this function to work in PAGE_SIZE units.
815 static int do_direct_IO(struct dio
*dio
)
817 const unsigned blkbits
= dio
->blkbits
;
818 const unsigned blocks_per_page
= PAGE_SIZE
>> blkbits
;
820 unsigned block_in_page
;
821 struct buffer_head
*map_bh
= &dio
->map_bh
;
824 /* The I/O can start at any block offset within the first page */
825 block_in_page
= dio
->first_block_in_page
;
827 while (dio
->block_in_file
< dio
->final_block_in_request
) {
828 page
= dio_get_page(dio
);
834 while (block_in_page
< blocks_per_page
) {
835 unsigned offset_in_page
= block_in_page
<< blkbits
;
836 unsigned this_chunk_bytes
; /* # of bytes mapped */
837 unsigned this_chunk_blocks
; /* # of blocks */
840 if (dio
->blocks_available
== 0) {
842 * Need to go and map some more disk
844 unsigned long blkmask
;
845 unsigned long dio_remainder
;
847 ret
= get_more_blocks(dio
);
849 page_cache_release(page
);
852 if (!buffer_mapped(map_bh
))
855 dio
->blocks_available
=
856 map_bh
->b_size
>> dio
->blkbits
;
857 dio
->next_block_for_io
=
858 map_bh
->b_blocknr
<< dio
->blkfactor
;
859 if (buffer_new(map_bh
))
860 clean_blockdev_aliases(dio
);
865 blkmask
= (1 << dio
->blkfactor
) - 1;
866 dio_remainder
= (dio
->block_in_file
& blkmask
);
869 * If we are at the start of IO and that IO
870 * starts partway into a fs-block,
871 * dio_remainder will be non-zero. If the IO
872 * is a read then we can simply advance the IO
873 * cursor to the first block which is to be
874 * read. But if the IO is a write and the
875 * block was newly allocated we cannot do that;
876 * the start of the fs block must be zeroed out
879 if (!buffer_new(map_bh
))
880 dio
->next_block_for_io
+= dio_remainder
;
881 dio
->blocks_available
-= dio_remainder
;
885 if (!buffer_mapped(map_bh
)) {
886 loff_t i_size_aligned
;
888 /* AKPM: eargh, -ENOTBLK is a hack */
889 if (dio
->rw
& WRITE
) {
890 page_cache_release(page
);
895 * Be sure to account for a partial block as the
896 * last block in the file
898 i_size_aligned
= ALIGN(i_size_read(dio
->inode
),
900 if (dio
->block_in_file
>=
901 i_size_aligned
>> blkbits
) {
903 page_cache_release(page
);
906 zero_user(page
, block_in_page
<< blkbits
,
908 dio
->block_in_file
++;
914 * If we're performing IO which has an alignment which
915 * is finer than the underlying fs, go check to see if
916 * we must zero out the start of this block.
918 if (unlikely(dio
->blkfactor
&& !dio
->start_zero_done
))
919 dio_zero_block(dio
, 0);
922 * Work out, in this_chunk_blocks, how much disk we
923 * can add to this page
925 this_chunk_blocks
= dio
->blocks_available
;
926 u
= (PAGE_SIZE
- offset_in_page
) >> blkbits
;
927 if (this_chunk_blocks
> u
)
928 this_chunk_blocks
= u
;
929 u
= dio
->final_block_in_request
- dio
->block_in_file
;
930 if (this_chunk_blocks
> u
)
931 this_chunk_blocks
= u
;
932 this_chunk_bytes
= this_chunk_blocks
<< blkbits
;
933 BUG_ON(this_chunk_bytes
== 0);
935 dio
->boundary
= buffer_boundary(map_bh
);
936 ret
= submit_page_section(dio
, page
, offset_in_page
,
937 this_chunk_bytes
, dio
->next_block_for_io
);
939 page_cache_release(page
);
942 dio
->next_block_for_io
+= this_chunk_blocks
;
944 dio
->block_in_file
+= this_chunk_blocks
;
945 block_in_page
+= this_chunk_blocks
;
946 dio
->blocks_available
-= this_chunk_blocks
;
948 BUG_ON(dio
->block_in_file
> dio
->final_block_in_request
);
949 if (dio
->block_in_file
== dio
->final_block_in_request
)
953 /* Drop the ref which was taken in get_user_pages() */
954 page_cache_release(page
);
962 * Releases both i_mutex and i_alloc_sem
965 direct_io_worker(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
966 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
,
967 unsigned blkbits
, get_block_t get_block
, dio_iodone_t end_io
,
968 dio_submit_t submit_io
, struct dio
*dio
)
970 unsigned long user_addr
;
979 dio
->blkbits
= blkbits
;
980 dio
->blkfactor
= inode
->i_blkbits
- blkbits
;
981 dio
->block_in_file
= offset
>> blkbits
;
983 dio
->get_block
= get_block
;
984 dio
->end_io
= end_io
;
985 dio
->submit_io
= submit_io
;
986 dio
->final_block_in_bio
= -1;
987 dio
->next_block_for_io
= -1;
990 dio
->i_size
= i_size_read(inode
);
992 spin_lock_init(&dio
->bio_lock
);
996 * In case of non-aligned buffers, we may need 2 more
997 * pages since we need to zero out first and last block.
999 if (unlikely(dio
->blkfactor
))
1000 dio
->pages_in_io
= 2;
1002 for (seg
= 0; seg
< nr_segs
; seg
++) {
1003 user_addr
= (unsigned long)iov
[seg
].iov_base
;
1005 ((user_addr
+iov
[seg
].iov_len
+PAGE_SIZE
-1)/PAGE_SIZE
1006 - user_addr
/PAGE_SIZE
);
1009 for (seg
= 0; seg
< nr_segs
; seg
++) {
1010 user_addr
= (unsigned long)iov
[seg
].iov_base
;
1011 dio
->size
+= bytes
= iov
[seg
].iov_len
;
1013 /* Index into the first page of the first block */
1014 dio
->first_block_in_page
= (user_addr
& ~PAGE_MASK
) >> blkbits
;
1015 dio
->final_block_in_request
= dio
->block_in_file
+
1017 /* Page fetching state */
1022 dio
->total_pages
= 0;
1023 if (user_addr
& (PAGE_SIZE
-1)) {
1025 bytes
-= PAGE_SIZE
- (user_addr
& (PAGE_SIZE
- 1));
1027 dio
->total_pages
+= (bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1028 dio
->curr_user_address
= user_addr
;
1030 ret
= do_direct_IO(dio
);
1032 dio
->result
+= iov
[seg
].iov_len
-
1033 ((dio
->final_block_in_request
- dio
->block_in_file
) <<
1040 } /* end iovec loop */
1042 if (ret
== -ENOTBLK
) {
1044 * The remaining part of the request will be
1045 * be handled by buffered I/O when we return
1050 * There may be some unwritten disk at the end of a part-written
1051 * fs-block-sized block. Go zero that now.
1053 dio_zero_block(dio
, 1);
1055 if (dio
->cur_page
) {
1056 ret2
= dio_send_cur_page(dio
);
1059 page_cache_release(dio
->cur_page
);
1060 dio
->cur_page
= NULL
;
1063 dio_bio_submit(dio
);
1066 * It is possible that, we return short IO due to end of file.
1067 * In that case, we need to release all the pages we got hold on.
1072 * All block lookups have been performed. For READ requests
1073 * we can let i_mutex go now that its achieved its purpose
1074 * of protecting us from looking up uninitialized blocks.
1076 if (rw
== READ
&& (dio
->flags
& DIO_LOCKING
))
1077 mutex_unlock(&dio
->inode
->i_mutex
);
1080 * The only time we want to leave bios in flight is when a successful
1081 * partial aio read or full aio write have been setup. In that case
1082 * bio completion will call aio_complete. The only time it's safe to
1083 * call aio_complete is when we return -EIOCBQUEUED, so we key on that.
1084 * This had *better* be the only place that raises -EIOCBQUEUED.
1086 BUG_ON(ret
== -EIOCBQUEUED
);
1087 if (dio
->is_async
&& ret
== 0 && dio
->result
&&
1088 ((rw
& READ
) || (dio
->result
== dio
->size
)))
1091 if (ret
!= -EIOCBQUEUED
) {
1092 /* All IO is now issued, send it on its way */
1093 blk_run_address_space(inode
->i_mapping
);
1094 dio_await_completion(dio
);
1098 * Sync will always be dropping the final ref and completing the
1099 * operation. AIO can if it was a broken operation described above or
1100 * in fact if all the bios race to complete before we get here. In
1101 * that case dio_complete() translates the EIOCBQUEUED into the proper
1102 * return code that the caller will hand to aio_complete().
1104 * This is managed by the bio_lock instead of being an atomic_t so that
1105 * completion paths can drop their ref and use the remaining count to
1106 * decide to wake the submission path atomically.
1108 spin_lock_irqsave(&dio
->bio_lock
, flags
);
1109 ret2
= --dio
->refcount
;
1110 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
1113 ret
= dio_complete(dio
, offset
, ret
);
1116 BUG_ON(ret
!= -EIOCBQUEUED
);
1122 * This is a library function for use by filesystem drivers.
1124 * The locking rules are governed by the flags parameter:
1125 * - if the flags value contains DIO_LOCKING we use a fancy locking
1126 * scheme for dumb filesystems.
1127 * For writes this function is called under i_mutex and returns with
1128 * i_mutex held, for reads, i_mutex is not held on entry, but it is
1129 * taken and dropped again before returning.
1130 * For reads and writes i_alloc_sem is taken in shared mode and released
1131 * on I/O completion (which may happen asynchronously after returning to
1134 * - if the flags value does NOT contain DIO_LOCKING we don't use any
1135 * internal locking but rather rely on the filesystem to synchronize
1136 * direct I/O reads/writes versus each other and truncate.
1137 * For reads and writes both i_mutex and i_alloc_sem are not held on
1138 * entry and are never taken.
1141 __blockdev_direct_IO(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
1142 struct block_device
*bdev
, const struct iovec
*iov
, loff_t offset
,
1143 unsigned long nr_segs
, get_block_t get_block
, dio_iodone_t end_io
,
1144 dio_submit_t submit_io
, int flags
)
1149 unsigned blkbits
= inode
->i_blkbits
;
1150 unsigned bdev_blkbits
= 0;
1151 unsigned blocksize_mask
= (1 << blkbits
) - 1;
1152 ssize_t retval
= -EINVAL
;
1153 loff_t end
= offset
;
1157 rw
= WRITE_ODIRECT_PLUG
;
1160 bdev_blkbits
= blksize_bits(bdev_logical_block_size(bdev
));
1162 if (offset
& blocksize_mask
) {
1164 blkbits
= bdev_blkbits
;
1165 blocksize_mask
= (1 << blkbits
) - 1;
1166 if (offset
& blocksize_mask
)
1170 /* Check the memory alignment. Blocks cannot straddle pages */
1171 for (seg
= 0; seg
< nr_segs
; seg
++) {
1172 addr
= (unsigned long)iov
[seg
].iov_base
;
1173 size
= iov
[seg
].iov_len
;
1175 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
)) {
1177 blkbits
= bdev_blkbits
;
1178 blocksize_mask
= (1 << blkbits
) - 1;
1179 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
1184 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
1189 * Believe it or not, zeroing out the page array caused a .5%
1190 * performance regression in a database benchmark. So, we take
1191 * care to only zero out what's needed.
1193 memset(dio
, 0, offsetof(struct dio
, pages
));
1196 if (dio
->flags
& DIO_LOCKING
) {
1197 /* watch out for a 0 len io from a tricksy fs */
1198 if (rw
== READ
&& end
> offset
) {
1199 struct address_space
*mapping
=
1200 iocb
->ki_filp
->f_mapping
;
1202 /* will be released by direct_io_worker */
1203 mutex_lock(&inode
->i_mutex
);
1205 retval
= filemap_write_and_wait_range(mapping
, offset
,
1208 mutex_unlock(&inode
->i_mutex
);
1215 * Will be released at I/O completion, possibly in a
1218 down_read_non_owner(&inode
->i_alloc_sem
);
1222 * For file extending writes updating i_size before data
1223 * writeouts complete can expose uninitialized blocks. So
1224 * even for AIO, we need to wait for i/o to complete before
1225 * returning in this case.
1227 dio
->is_async
= !is_sync_kiocb(iocb
) && !((rw
& WRITE
) &&
1228 (end
> i_size_read(inode
)));
1230 retval
= direct_io_worker(rw
, iocb
, inode
, iov
, offset
,
1231 nr_segs
, blkbits
, get_block
, end_io
,
1235 * In case of error extending write may have instantiated a few
1236 * blocks outside i_size. Trim these off again for DIO_LOCKING.
1238 * NOTE: filesystems with their own locking have to handle this
1241 if (flags
& DIO_LOCKING
) {
1242 if (unlikely((rw
& WRITE
) && retval
< 0)) {
1243 loff_t isize
= i_size_read(inode
);
1245 vmtruncate(inode
, isize
);
1252 EXPORT_SYMBOL(__blockdev_direct_IO
);