4 * Copyright (C) 2002, Linus Torvalds.
8 * 04Jul2002 akpm@zip.com.au
10 * 11Sep2002 janetinc@us.ibm.com
11 * added readv/writev support.
12 * 29Oct2002 akpm@zip.com.au
13 * rewrote bio_add_page() support.
14 * 30Oct2002 pbadari@us.ibm.com
15 * added support for non-aligned IO.
16 * 06Nov2002 pbadari@us.ibm.com
17 * added asynchronous IO support.
18 * 21Jul2003 nathans@sgi.com
19 * added IO completion notifier.
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/bio.h>
31 #include <linux/wait.h>
32 #include <linux/err.h>
33 #include <linux/blkdev.h>
34 #include <linux/buffer_head.h>
35 #include <linux/rwsem.h>
36 #include <linux/uio.h>
37 #include <asm/atomic.h>
40 * How many user pages to map in one call to get_user_pages(). This determines
41 * the size of a structure on the stack.
46 * This code generally works in units of "dio_blocks". A dio_block is
47 * somewhere between the hard sector size and the filesystem block size. it
48 * is determined on a per-invocation basis. When talking to the filesystem
49 * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
50 * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted
51 * to bio_block quantities by shifting left by blkfactor.
53 * If blkfactor is zero then the user's request was aligned to the filesystem's
58 /* BIO submission state */
59 struct bio
*bio
; /* bio under assembly */
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
71 int pages_in_io
; /* approximate total IO pages */
72 sector_t block_in_file
; /* Current offset into the underlying
73 file in dio_block units. */
74 unsigned blocks_available
; /* At block_in_file. changes */
75 sector_t final_block_in_request
;/* doesn't change */
76 unsigned first_block_in_page
; /* doesn't change, Used only once */
77 int boundary
; /* prev block is at a boundary */
78 int reap_counter
; /* rate limit reaping */
79 get_blocks_t
*get_blocks
; /* block mapping function */
80 dio_iodone_t
*end_io
; /* IO completion function */
81 sector_t final_block_in_bio
; /* current final block in bio + 1 */
82 sector_t next_block_for_io
; /* next block to be put under IO,
83 in dio_blocks units */
84 struct buffer_head map_bh
; /* last get_blocks() result */
87 * Deferred addition of a page to the dio. These variables are
88 * private to dio_send_cur_page(), submit_page_section() and
91 struct page
*cur_page
; /* The page */
92 unsigned cur_page_offset
; /* Offset into it, in bytes */
93 unsigned cur_page_len
; /* Nr of bytes at cur_page_offset */
94 sector_t cur_page_block
; /* Where it starts */
97 * Page fetching state. These variables belong to dio_refill_pages().
99 int curr_page
; /* changes */
100 int total_pages
; /* doesn't change */
101 unsigned long curr_user_address
;/* changes */
104 * Page queue. These variables belong to dio_refill_pages() and
107 struct page
*pages
[DIO_PAGES
]; /* page buffer */
108 unsigned head
; /* next page to process */
109 unsigned tail
; /* last valid page + 1 */
110 int page_errors
; /* errno from get_user_pages() */
112 /* BIO completion state */
113 atomic_t bio_count
; /* nr bios to be completed */
114 atomic_t bios_in_flight
; /* nr bios in flight */
115 spinlock_t bio_list_lock
; /* protects bio_list */
116 struct bio
*bio_list
; /* singly linked via bi_private */
117 struct task_struct
*waiter
; /* waiting task (NULL if none) */
119 /* AIO related stuff */
120 struct kiocb
*iocb
; /* kiocb */
121 int is_async
; /* is IO async ? */
122 int result
; /* IO result */
126 * How many pages are in the queue?
128 static inline unsigned dio_pages_present(struct dio
*dio
)
130 return dio
->tail
- dio
->head
;
134 * Go grab and pin some userspace pages. Typically we'll get 64 at a time.
136 static int dio_refill_pages(struct dio
*dio
)
141 nr_pages
= min(dio
->total_pages
- dio
->curr_page
, DIO_PAGES
);
142 down_read(¤t
->mm
->mmap_sem
);
143 ret
= get_user_pages(
144 current
, /* Task for fault acounting */
145 current
->mm
, /* whose pages? */
146 dio
->curr_user_address
, /* Where from? */
147 nr_pages
, /* How many pages? */
148 dio
->rw
== READ
, /* Write to memory? */
152 up_read(¤t
->mm
->mmap_sem
);
154 if (ret
< 0 && dio
->blocks_available
&& (dio
->rw
== WRITE
)) {
156 * A memory fault, but the filesystem has some outstanding
157 * mapped blocks. We need to use those blocks up to avoid
158 * leaking stale data in the file.
160 if (dio
->page_errors
== 0)
161 dio
->page_errors
= ret
;
162 dio
->pages
[0] = ZERO_PAGE(dio
->curr_user_address
);
170 dio
->curr_user_address
+= ret
* PAGE_SIZE
;
171 dio
->curr_page
+= ret
;
181 * Get another userspace page. Returns an ERR_PTR on error. Pages are
182 * buffered inside the dio so that we can call get_user_pages() against a
183 * decent number of pages, less frequently. To provide nicer use of the
186 static struct page
*dio_get_page(struct dio
*dio
)
188 if (dio_pages_present(dio
) == 0) {
191 ret
= dio_refill_pages(dio
);
194 BUG_ON(dio_pages_present(dio
) == 0);
196 return dio
->pages
[dio
->head
++];
200 * Called when all DIO BIO I/O has been completed - let the filesystem
201 * know, if it registered an interest earlier via get_blocks. Pass the
202 * private field of the map buffer_head so that filesystems can use it
203 * to hold additional state between get_blocks calls and dio_complete.
205 static void dio_complete(struct dio
*dio
, loff_t offset
, ssize_t bytes
)
208 dio
->end_io(dio
->inode
, offset
, bytes
, dio
->map_bh
.b_private
);
212 * Called when a BIO has been processed. If the count goes to zero then IO is
213 * complete and we can signal this to the AIO layer.
215 static void finished_one_bio(struct dio
*dio
)
217 if (atomic_dec_and_test(&dio
->bio_count
)) {
219 dio_complete(dio
, dio
->block_in_file
<< dio
->blkbits
,
221 aio_complete(dio
->iocb
, dio
->result
, 0);
227 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
);
229 * Asynchronous IO callback.
231 static int dio_bio_end_aio(struct bio
*bio
, unsigned int bytes_done
, int error
)
233 struct dio
*dio
= bio
->bi_private
;
238 /* cleanup the bio */
239 dio_bio_complete(dio
, bio
);
244 * The BIO completion handler simply queues the BIO up for the process-context
247 * During I/O bi_private points at the dio. After I/O, bi_private is used to
248 * implement a singly-linked list of completed BIOs, at dio->bio_list.
250 static int dio_bio_end_io(struct bio
*bio
, unsigned int bytes_done
, int error
)
252 struct dio
*dio
= bio
->bi_private
;
258 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
259 bio
->bi_private
= dio
->bio_list
;
261 atomic_dec(&dio
->bios_in_flight
);
262 if (dio
->waiter
&& atomic_read(&dio
->bios_in_flight
) == 0)
263 wake_up_process(dio
->waiter
);
264 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
269 dio_bio_alloc(struct dio
*dio
, struct block_device
*bdev
,
270 sector_t first_sector
, int nr_vecs
)
274 bio
= bio_alloc(GFP_KERNEL
, nr_vecs
);
279 bio
->bi_sector
= first_sector
;
281 bio
->bi_end_io
= dio_bio_end_aio
;
283 bio
->bi_end_io
= dio_bio_end_io
;
290 * In the AIO read case we speculatively dirty the pages before starting IO.
291 * During IO completion, any of these pages which happen to have been written
292 * back will be redirtied by bio_check_pages_dirty().
294 static void dio_bio_submit(struct dio
*dio
)
296 struct bio
*bio
= dio
->bio
;
298 bio
->bi_private
= dio
;
299 atomic_inc(&dio
->bio_count
);
300 atomic_inc(&dio
->bios_in_flight
);
301 if (dio
->is_async
&& dio
->rw
== READ
)
302 bio_set_pages_dirty(bio
);
303 submit_bio(dio
->rw
, bio
);
310 * Release any resources in case of a failure
312 static void dio_cleanup(struct dio
*dio
)
314 while (dio_pages_present(dio
))
315 page_cache_release(dio_get_page(dio
));
319 * Wait for the next BIO to complete. Remove it and return it.
321 static struct bio
*dio_await_one(struct dio
*dio
)
326 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
327 while (dio
->bio_list
== NULL
) {
328 set_current_state(TASK_UNINTERRUPTIBLE
);
329 if (dio
->bio_list
== NULL
) {
330 dio
->waiter
= current
;
331 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
334 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
337 set_current_state(TASK_RUNNING
);
340 dio
->bio_list
= bio
->bi_private
;
341 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
346 * Process one completed BIO. No locks are held.
348 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
)
350 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
351 struct bio_vec
*bvec
= bio
->bi_io_vec
;
357 if (dio
->is_async
&& dio
->rw
== READ
) {
358 bio_check_pages_dirty(bio
); /* transfers ownership */
360 for (page_no
= 0; page_no
< bio
->bi_vcnt
; page_no
++) {
361 struct page
*page
= bvec
[page_no
].bv_page
;
364 set_page_dirty_lock(page
);
365 page_cache_release(page
);
369 finished_one_bio(dio
);
370 return uptodate
? 0 : -EIO
;
374 * Wait on and process all in-flight BIOs.
376 static int dio_await_completion(struct dio
*dio
)
383 while (atomic_read(&dio
->bio_count
)) {
384 struct bio
*bio
= dio_await_one(dio
);
387 ret2
= dio_bio_complete(dio
, bio
);
395 * A really large O_DIRECT read or write can generate a lot of BIOs. So
396 * to keep the memory consumption sane we periodically reap any completed BIOs
397 * during the BIO generation phase.
399 * This also helps to limit the peak amount of pinned userspace memory.
401 static int dio_bio_reap(struct dio
*dio
)
405 if (dio
->reap_counter
++ >= 64) {
406 while (dio
->bio_list
) {
410 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
412 dio
->bio_list
= bio
->bi_private
;
413 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
414 ret
= dio_bio_complete(dio
, bio
);
416 dio
->reap_counter
= 0;
422 * Call into the fs to map some more disk blocks. We record the current number
423 * of available blocks at dio->blocks_available. These are in units of the
424 * fs blocksize, (1 << inode->i_blkbits).
426 * The fs is allowed to map lots of blocks at once. If it wants to do that,
427 * it uses the passed inode-relative block number as the file offset, as usual.
429 * get_blocks() is passed the number of i_blkbits-sized blocks which direct_io
430 * has remaining to do. The fs should not map more than this number of blocks.
432 * If the fs has mapped a lot of blocks, it should populate bh->b_size to
433 * indicate how much contiguous disk space has been made available at
436 * If *any* of the mapped blocks are new, then the fs must set buffer_new().
437 * This isn't very efficient...
439 * In the case of filesystem holes: the fs may return an arbitrarily-large
440 * hole by returning an appropriate value in b_size and by clearing
441 * buffer_mapped(). However the direct-io code will only process holes one
442 * block at a time - it will repeatedly call get_blocks() as it walks the hole.
444 static int get_more_blocks(struct dio
*dio
)
447 struct buffer_head
*map_bh
= &dio
->map_bh
;
448 sector_t fs_startblk
; /* Into file, in filesystem-sized blocks */
449 unsigned long fs_count
; /* Number of filesystem-sized blocks */
450 unsigned long dio_count
;/* Number of dio_block-sized blocks */
451 unsigned long blkmask
;
454 * If there was a memory error and we've overwritten all the
455 * mapped blocks then we can now return that memory error
457 ret
= dio
->page_errors
;
461 BUG_ON(dio
->block_in_file
>= dio
->final_block_in_request
);
462 fs_startblk
= dio
->block_in_file
>> dio
->blkfactor
;
463 dio_count
= dio
->final_block_in_request
- dio
->block_in_file
;
464 fs_count
= dio_count
>> dio
->blkfactor
;
465 blkmask
= (1 << dio
->blkfactor
) - 1;
466 if (dio_count
& blkmask
)
469 ret
= (*dio
->get_blocks
)(dio
->inode
, fs_startblk
, fs_count
,
470 map_bh
, dio
->rw
== WRITE
);
476 * There is no bio. Make one now.
478 static int dio_new_bio(struct dio
*dio
, sector_t start_sector
)
483 ret
= dio_bio_reap(dio
);
486 sector
= start_sector
<< (dio
->blkbits
- 9);
487 nr_pages
= min(dio
->pages_in_io
, bio_get_nr_vecs(dio
->map_bh
.b_bdev
));
488 BUG_ON(nr_pages
<= 0);
489 ret
= dio_bio_alloc(dio
, dio
->map_bh
.b_bdev
, sector
, nr_pages
);
496 * Attempt to put the current chunk of 'cur_page' into the current BIO. If
497 * that was successful then update final_block_in_bio and take a ref against
498 * the just-added page.
500 * Return zero on success. Non-zero means the caller needs to start a new BIO.
502 static int dio_bio_add_page(struct dio
*dio
)
506 ret
= bio_add_page(dio
->bio
, dio
->cur_page
,
507 dio
->cur_page_len
, dio
->cur_page_offset
);
508 if (ret
== dio
->cur_page_len
) {
510 page_cache_get(dio
->cur_page
);
511 dio
->final_block_in_bio
= dio
->cur_page_block
+
512 (dio
->cur_page_len
>> dio
->blkbits
);
521 * Put cur_page under IO. The section of cur_page which is described by
522 * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
523 * starts on-disk at cur_page_block.
525 * We take a ref against the page here (on behalf of its presence in the bio).
527 * The caller of this function is responsible for removing cur_page from the
528 * dio, and for dropping the refcount which came from that presence.
530 static int dio_send_cur_page(struct dio
*dio
)
536 * See whether this new request is contiguous with the old
538 if (dio
->final_block_in_bio
!= dio
->cur_page_block
)
541 * Submit now if the underlying fs is about to perform a
548 if (dio
->bio
== NULL
) {
549 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
554 if (dio_bio_add_page(dio
) != 0) {
556 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
558 ret
= dio_bio_add_page(dio
);
567 * An autonomous function to put a chunk of a page under deferred IO.
569 * The caller doesn't actually know (or care) whether this piece of page is in
570 * a BIO, or is under IO or whatever. We just take care of all possible
571 * situations here. The separation between the logic of do_direct_IO() and
572 * that of submit_page_section() is important for clarity. Please don't break.
574 * The chunk of page starts on-disk at blocknr.
576 * We perform deferred IO, by recording the last-submitted page inside our
577 * private part of the dio structure. If possible, we just expand the IO
578 * across that page here.
580 * If that doesn't work out then we put the old page into the bio and add this
581 * page to the dio instead.
584 submit_page_section(struct dio
*dio
, struct page
*page
,
585 unsigned offset
, unsigned len
, sector_t blocknr
)
590 * Can we just grow the current page's presence in the dio?
592 if ( (dio
->cur_page
== page
) &&
593 (dio
->cur_page_offset
+ dio
->cur_page_len
== offset
) &&
594 (dio
->cur_page_block
+
595 (dio
->cur_page_len
>> dio
->blkbits
) == blocknr
)) {
596 dio
->cur_page_len
+= len
;
599 * If dio->boundary then we want to schedule the IO now to
600 * avoid metadata seeks.
603 ret
= dio_send_cur_page(dio
);
604 page_cache_release(dio
->cur_page
);
605 dio
->cur_page
= NULL
;
611 * If there's a deferred page already there then send it.
614 ret
= dio_send_cur_page(dio
);
615 page_cache_release(dio
->cur_page
);
616 dio
->cur_page
= NULL
;
621 page_cache_get(page
); /* It is in dio */
622 dio
->cur_page
= page
;
623 dio
->cur_page_offset
= offset
;
624 dio
->cur_page_len
= len
;
625 dio
->cur_page_block
= blocknr
;
631 * Clean any dirty buffers in the blockdev mapping which alias newly-created
632 * file blocks. Only called for S_ISREG files - blockdevs do not set
635 static void clean_blockdev_aliases(struct dio
*dio
)
639 for (i
= 0; i
< dio
->blocks_available
; i
++) {
640 unmap_underlying_metadata(dio
->map_bh
.b_bdev
,
641 dio
->map_bh
.b_blocknr
+ i
);
646 * If we are not writing the entire block and get_block() allocated
647 * the block for us, we need to fill-in the unused portion of the
648 * block with zeros. This happens only if user-buffer, fileoffset or
649 * io length is not filesystem block-size multiple.
651 * `end' is zero if we're doing the start of the IO, 1 at the end of the
654 static void dio_zero_block(struct dio
*dio
, int end
)
656 unsigned dio_blocks_per_fs_block
;
657 unsigned this_chunk_blocks
; /* In dio_blocks */
658 unsigned this_chunk_bytes
;
661 dio
->start_zero_done
= 1;
662 if (!dio
->blkfactor
|| !buffer_new(&dio
->map_bh
))
665 dio_blocks_per_fs_block
= 1 << dio
->blkfactor
;
666 this_chunk_blocks
= dio
->block_in_file
& (dio_blocks_per_fs_block
- 1);
668 if (!this_chunk_blocks
)
672 * We need to zero out part of an fs block. It is either at the
673 * beginning or the end of the fs block.
676 this_chunk_blocks
= dio_blocks_per_fs_block
- this_chunk_blocks
;
678 this_chunk_bytes
= this_chunk_blocks
<< dio
->blkbits
;
680 page
= ZERO_PAGE(dio
->curr_user_address
);
681 if (submit_page_section(dio
, page
, 0, this_chunk_bytes
,
682 dio
->next_block_for_io
))
685 dio
->next_block_for_io
+= this_chunk_blocks
;
689 * Walk the user pages, and the file, mapping blocks to disk and generating
690 * a sequence of (page,offset,len,block) mappings. These mappings are injected
691 * into submit_page_section(), which takes care of the next stage of submission
693 * Direct IO against a blockdev is different from a file. Because we can
694 * happily perform page-sized but 512-byte aligned IOs. It is important that
695 * blockdev IO be able to have fine alignment and large sizes.
697 * So what we do is to permit the ->get_blocks function to populate bh.b_size
698 * with the size of IO which is permitted at this offset and this i_blkbits.
700 * For best results, the blockdev should be set up with 512-byte i_blkbits and
701 * it should set b_size to PAGE_SIZE or more inside get_blocks(). This gives
702 * fine alignment but still allows this function to work in PAGE_SIZE units.
704 static int do_direct_IO(struct dio
*dio
)
706 const unsigned blkbits
= dio
->blkbits
;
707 const unsigned blocks_per_page
= PAGE_SIZE
>> blkbits
;
709 unsigned block_in_page
;
710 struct buffer_head
*map_bh
= &dio
->map_bh
;
713 /* The I/O can start at any block offset within the first page */
714 block_in_page
= dio
->first_block_in_page
;
716 while (dio
->block_in_file
< dio
->final_block_in_request
) {
717 page
= dio_get_page(dio
);
723 while (block_in_page
< blocks_per_page
) {
724 unsigned offset_in_page
= block_in_page
<< blkbits
;
725 unsigned this_chunk_bytes
; /* # of bytes mapped */
726 unsigned this_chunk_blocks
; /* # of blocks */
729 if (dio
->blocks_available
== 0) {
731 * Need to go and map some more disk
733 unsigned long blkmask
;
734 unsigned long dio_remainder
;
736 ret
= get_more_blocks(dio
);
738 page_cache_release(page
);
741 if (!buffer_mapped(map_bh
))
744 dio
->blocks_available
=
745 map_bh
->b_size
>> dio
->blkbits
;
746 dio
->next_block_for_io
=
747 map_bh
->b_blocknr
<< dio
->blkfactor
;
748 if (buffer_new(map_bh
))
749 clean_blockdev_aliases(dio
);
754 blkmask
= (1 << dio
->blkfactor
) - 1;
755 dio_remainder
= (dio
->block_in_file
& blkmask
);
758 * If we are at the start of IO and that IO
759 * starts partway into a fs-block,
760 * dio_remainder will be non-zero. If the IO
761 * is a read then we can simply advance the IO
762 * cursor to the first block which is to be
763 * read. But if the IO is a write and the
764 * block was newly allocated we cannot do that;
765 * the start of the fs block must be zeroed out
768 if (!buffer_new(map_bh
))
769 dio
->next_block_for_io
+= dio_remainder
;
770 dio
->blocks_available
-= dio_remainder
;
774 if (!buffer_mapped(map_bh
)) {
777 if (dio
->block_in_file
>=
778 i_size_read(dio
->inode
)>>blkbits
) {
780 page_cache_release(page
);
783 kaddr
= kmap_atomic(page
, KM_USER0
);
784 memset(kaddr
+ (block_in_page
<< blkbits
),
786 flush_dcache_page(page
);
787 kunmap_atomic(kaddr
, KM_USER0
);
788 dio
->block_in_file
++;
794 * If we're performing IO which has an alignment which
795 * is finer than the underlying fs, go check to see if
796 * we must zero out the start of this block.
798 if (unlikely(dio
->blkfactor
&& !dio
->start_zero_done
))
799 dio_zero_block(dio
, 0);
802 * Work out, in this_chunk_blocks, how much disk we
803 * can add to this page
805 this_chunk_blocks
= dio
->blocks_available
;
806 u
= (PAGE_SIZE
- offset_in_page
) >> blkbits
;
807 if (this_chunk_blocks
> u
)
808 this_chunk_blocks
= u
;
809 u
= dio
->final_block_in_request
- dio
->block_in_file
;
810 if (this_chunk_blocks
> u
)
811 this_chunk_blocks
= u
;
812 this_chunk_bytes
= this_chunk_blocks
<< blkbits
;
813 BUG_ON(this_chunk_bytes
== 0);
815 dio
->boundary
= buffer_boundary(map_bh
);
816 ret
= submit_page_section(dio
, page
, offset_in_page
,
817 this_chunk_bytes
, dio
->next_block_for_io
);
819 page_cache_release(page
);
822 dio
->next_block_for_io
+= this_chunk_blocks
;
824 dio
->block_in_file
+= this_chunk_blocks
;
825 block_in_page
+= this_chunk_blocks
;
826 dio
->blocks_available
-= this_chunk_blocks
;
828 if (dio
->block_in_file
> dio
->final_block_in_request
)
830 if (dio
->block_in_file
== dio
->final_block_in_request
)
834 /* Drop the ref which was taken in get_user_pages() */
835 page_cache_release(page
);
843 direct_io_worker(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
844 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
,
845 unsigned blkbits
, get_blocks_t get_blocks
, dio_iodone_t end_io
)
847 unsigned long user_addr
;
854 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
857 dio
->is_async
= !is_sync_kiocb(iocb
);
862 dio
->blkbits
= blkbits
;
863 dio
->blkfactor
= inode
->i_blkbits
- blkbits
;
864 dio
->start_zero_done
= 0;
865 dio
->block_in_file
= offset
>> blkbits
;
866 dio
->blocks_available
= 0;
868 dio
->cur_page
= NULL
;
871 dio
->reap_counter
= 0;
872 dio
->get_blocks
= get_blocks
;
873 dio
->end_io
= end_io
;
874 dio
->map_bh
.b_private
= NULL
;
875 dio
->final_block_in_bio
= -1;
876 dio
->next_block_for_io
= -1;
878 dio
->page_errors
= 0;
883 * BIO completion state.
885 * ->bio_count starts out at one, and we decrement it to zero after all
886 * BIOs are submitted. This to avoid the situation where a really fast
887 * (or synchronous) device could take the count to zero while we're
888 * still submitting BIOs.
890 atomic_set(&dio
->bio_count
, 1);
891 atomic_set(&dio
->bios_in_flight
, 0);
892 spin_lock_init(&dio
->bio_list_lock
);
893 dio
->bio_list
= NULL
;
896 dio
->pages_in_io
= 0;
897 for (seg
= 0; seg
< nr_segs
; seg
++)
898 dio
->pages_in_io
+= (iov
[seg
].iov_len
>> blkbits
) + 2;
900 for (seg
= 0; seg
< nr_segs
; seg
++) {
901 user_addr
= (unsigned long)iov
[seg
].iov_base
;
902 bytes
= iov
[seg
].iov_len
;
904 /* Index into the first page of the first block */
905 dio
->first_block_in_page
= (user_addr
& ~PAGE_MASK
) >> blkbits
;
906 dio
->final_block_in_request
= dio
->block_in_file
+
908 /* Page fetching state */
913 dio
->total_pages
= 0;
914 if (user_addr
& (PAGE_SIZE
-1)) {
916 bytes
-= PAGE_SIZE
- (user_addr
& (PAGE_SIZE
- 1));
918 dio
->total_pages
+= (bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
919 dio
->curr_user_address
= user_addr
;
921 ret
= do_direct_IO(dio
);
923 dio
->result
+= iov
[seg
].iov_len
-
924 ((dio
->final_block_in_request
- dio
->block_in_file
) <<
931 } /* end iovec loop */
934 * There may be some unwritten disk at the end of a part-written
935 * fs-block-sized block. Go zero that now.
937 dio_zero_block(dio
, 1);
940 ret2
= dio_send_cur_page(dio
);
943 page_cache_release(dio
->cur_page
);
944 dio
->cur_page
= NULL
;
950 * It is possible that, we return short IO due to end of file.
951 * In that case, we need to release all the pages we got hold on.
956 * OK, all BIOs are submitted, so we can decrement bio_count to truly
957 * reflect the number of to-be-processed BIOs.
961 ret
= dio
->result
; /* Bytes written */
962 finished_one_bio(dio
); /* This can free the dio */
965 finished_one_bio(dio
);
966 ret2
= dio_await_completion(dio
);
970 ret
= dio
->page_errors
;
971 if (ret
== 0 && dio
->result
) {
972 loff_t i_size
= i_size_read(inode
);
976 * Adjust the return value if the read crossed a
977 * non-block-aligned EOF.
979 if (rw
== READ
&& (offset
+ ret
> i_size
))
980 ret
= i_size
- offset
;
982 dio_complete(dio
, offset
, ret
);
989 * This is a library function for use by filesystem drivers.
992 blockdev_direct_IO(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
993 struct block_device
*bdev
, const struct iovec
*iov
, loff_t offset
,
994 unsigned long nr_segs
, get_blocks_t get_blocks
, dio_iodone_t end_io
)
999 unsigned blkbits
= inode
->i_blkbits
;
1000 unsigned bdev_blkbits
= 0;
1001 unsigned blocksize_mask
= (1 << blkbits
) - 1;
1002 ssize_t retval
= -EINVAL
;
1005 bdev_blkbits
= blksize_bits(bdev_hardsect_size(bdev
));
1007 if (offset
& blocksize_mask
) {
1009 blkbits
= bdev_blkbits
;
1010 blocksize_mask
= (1 << blkbits
) - 1;
1011 if (offset
& blocksize_mask
)
1015 /* Check the memory alignment. Blocks cannot straddle pages */
1016 for (seg
= 0; seg
< nr_segs
; seg
++) {
1017 addr
= (unsigned long)iov
[seg
].iov_base
;
1018 size
= iov
[seg
].iov_len
;
1019 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
)) {
1021 blkbits
= bdev_blkbits
;
1022 blocksize_mask
= (1 << blkbits
) - 1;
1023 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
1028 retval
= direct_io_worker(rw
, iocb
, inode
, iov
, offset
,
1029 nr_segs
, blkbits
, get_blocks
, end_io
);
1034 EXPORT_SYMBOL(blockdev_direct_IO
);