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/types.h>
26 #include <linux/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/pagemap.h>
29 #include <linux/bio.h>
30 #include <linux/wait.h>
31 #include <linux/err.h>
32 #include <linux/blkdev.h>
33 #include <linux/buffer_head.h>
34 #include <linux/rwsem.h>
35 #include <linux/uio.h>
36 #include <asm/atomic.h>
39 * How many user pages to map in one call to get_user_pages(). This determines
40 * the size of a structure on the stack.
45 * This code generally works in units of "dio_blocks". A dio_block is
46 * somewhere between the hard sector size and the filesystem block size. it
47 * is determined on a per-invocation basis. When talking to the filesystem
48 * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
49 * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted
50 * to bio_block quantities by shifting left by blkfactor.
52 * If blkfactor is zero then the user's request was aligned to the filesystem's
57 /* BIO submission state */
58 struct bio
*bio
; /* bio under assembly */
61 unsigned blkbits
; /* doesn't change */
62 unsigned blkfactor
; /* When we're using an alignment which
63 is finer than the filesystem's soft
64 blocksize, this specifies how much
65 finer. blkfactor=2 means 1/4-block
66 alignment. Does not change */
67 unsigned start_zero_done
; /* flag: sub-blocksize zeroing has
68 been performed at the start of a
70 int pages_in_io
; /* approximate total IO pages */
71 sector_t block_in_file
; /* Current offset into the underlying
72 file in dio_block units. */
73 unsigned blocks_available
; /* At block_in_file. changes */
74 sector_t final_block_in_request
;/* doesn't change */
75 unsigned first_block_in_page
; /* doesn't change, Used only once */
76 int boundary
; /* prev block is at a boundary */
77 int reap_counter
; /* rate limit reaping */
78 get_blocks_t
*get_blocks
; /* block mapping function */
79 dio_iodone_t
*end_io
; /* IO completion function */
80 sector_t final_block_in_bio
; /* current final block in bio + 1 */
81 sector_t next_block_for_io
; /* next block to be put under IO,
82 in dio_blocks units */
83 struct buffer_head map_bh
; /* last get_blocks() result */
86 * Deferred addition of a page to the dio. These variables are
87 * private to dio_send_cur_page(), submit_page_section() and
90 struct page
*cur_page
; /* The page */
91 unsigned cur_page_offset
; /* Offset into it, in bytes */
92 unsigned cur_page_len
; /* Nr of bytes at cur_page_offset */
93 sector_t cur_page_block
; /* Where it starts */
96 * Page fetching state. These variables belong to dio_refill_pages().
98 int curr_page
; /* changes */
99 int total_pages
; /* doesn't change */
100 unsigned long curr_user_address
;/* changes */
103 * Page queue. These variables belong to dio_refill_pages() and
106 struct page
*pages
[DIO_PAGES
]; /* page buffer */
107 unsigned head
; /* next page to process */
108 unsigned tail
; /* last valid page + 1 */
109 int page_errors
; /* errno from get_user_pages() */
111 /* BIO completion state */
112 atomic_t bio_count
; /* nr bios to be completed */
113 atomic_t bios_in_flight
; /* nr bios in flight */
114 spinlock_t bio_list_lock
; /* protects bio_list */
115 struct bio
*bio_list
; /* singly linked via bi_private */
116 struct task_struct
*waiter
; /* waiting task (NULL if none) */
118 /* AIO related stuff */
119 struct kiocb
*iocb
; /* kiocb */
120 int is_async
; /* is IO async ? */
121 int result
; /* IO result */
125 * How many pages are in the queue?
127 static inline unsigned dio_pages_present(struct dio
*dio
)
129 return dio
->tail
- dio
->head
;
133 * Go grab and pin some userspace pages. Typically we'll get 64 at a time.
135 static int dio_refill_pages(struct dio
*dio
)
140 nr_pages
= min(dio
->total_pages
- dio
->curr_page
, DIO_PAGES
);
141 down_read(¤t
->mm
->mmap_sem
);
142 ret
= get_user_pages(
143 current
, /* Task for fault acounting */
144 current
->mm
, /* whose pages? */
145 dio
->curr_user_address
, /* Where from? */
146 nr_pages
, /* How many pages? */
147 dio
->rw
== READ
, /* Write to memory? */
151 up_read(¤t
->mm
->mmap_sem
);
153 if (ret
< 0 && dio
->blocks_available
&& (dio
->rw
== WRITE
)) {
155 * A memory fault, but the filesystem has some outstanding
156 * mapped blocks. We need to use those blocks up to avoid
157 * leaking stale data in the file.
159 if (dio
->page_errors
== 0)
160 dio
->page_errors
= ret
;
161 dio
->pages
[0] = ZERO_PAGE(dio
->curr_user_address
);
169 dio
->curr_user_address
+= ret
* PAGE_SIZE
;
170 dio
->curr_page
+= ret
;
180 * Get another userspace page. Returns an ERR_PTR on error. Pages are
181 * buffered inside the dio so that we can call get_user_pages() against a
182 * decent number of pages, less frequently. To provide nicer use of the
185 static struct page
*dio_get_page(struct dio
*dio
)
187 if (dio_pages_present(dio
) == 0) {
190 ret
= dio_refill_pages(dio
);
193 BUG_ON(dio_pages_present(dio
) == 0);
195 return dio
->pages
[dio
->head
++];
199 * Called when all DIO BIO I/O has been completed - let the filesystem
200 * know, if it registered an interest earlier via get_blocks. Pass the
201 * private field of the map buffer_head so that filesystems can use it
202 * to hold additional state between get_blocks calls and dio_complete.
204 static void dio_complete(struct dio
*dio
, loff_t offset
, ssize_t bytes
)
207 dio
->end_io(dio
->inode
, offset
, bytes
, dio
->map_bh
.b_private
);
211 * Called when a BIO has been processed. If the count goes to zero then IO is
212 * complete and we can signal this to the AIO layer.
214 static void finished_one_bio(struct dio
*dio
)
216 if (atomic_dec_and_test(&dio
->bio_count
)) {
218 dio_complete(dio
, dio
->block_in_file
<< dio
->blkbits
,
220 aio_complete(dio
->iocb
, dio
->result
, 0);
226 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
);
228 * Asynchronous IO callback.
230 static int dio_bio_end_aio(struct bio
*bio
, unsigned int bytes_done
, int error
)
232 struct dio
*dio
= bio
->bi_private
;
237 /* cleanup the bio */
238 dio_bio_complete(dio
, bio
);
243 * The BIO completion handler simply queues the BIO up for the process-context
246 * During I/O bi_private points at the dio. After I/O, bi_private is used to
247 * implement a singly-linked list of completed BIOs, at dio->bio_list.
249 static int dio_bio_end_io(struct bio
*bio
, unsigned int bytes_done
, int error
)
251 struct dio
*dio
= bio
->bi_private
;
257 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
258 bio
->bi_private
= dio
->bio_list
;
260 atomic_dec(&dio
->bios_in_flight
);
261 if (dio
->waiter
&& atomic_read(&dio
->bios_in_flight
) == 0)
262 wake_up_process(dio
->waiter
);
263 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
268 dio_bio_alloc(struct dio
*dio
, struct block_device
*bdev
,
269 sector_t first_sector
, int nr_vecs
)
273 bio
= bio_alloc(GFP_KERNEL
, nr_vecs
);
278 bio
->bi_sector
= first_sector
;
280 bio
->bi_end_io
= dio_bio_end_aio
;
282 bio
->bi_end_io
= dio_bio_end_io
;
289 * In the AIO read case we speculatively dirty the pages before starting IO.
290 * During IO completion, any of these pages which happen to have been written
291 * back will be redirtied by bio_check_pages_dirty().
293 static void dio_bio_submit(struct dio
*dio
)
295 struct bio
*bio
= dio
->bio
;
297 bio
->bi_private
= dio
;
298 atomic_inc(&dio
->bio_count
);
299 atomic_inc(&dio
->bios_in_flight
);
300 if (dio
->is_async
&& dio
->rw
== READ
)
301 bio_set_pages_dirty(bio
);
302 submit_bio(dio
->rw
, bio
);
309 * Release any resources in case of a failure
311 static void dio_cleanup(struct dio
*dio
)
313 while (dio_pages_present(dio
))
314 page_cache_release(dio_get_page(dio
));
318 * Wait for the next BIO to complete. Remove it and return it.
320 static struct bio
*dio_await_one(struct dio
*dio
)
325 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
326 while (dio
->bio_list
== NULL
) {
327 set_current_state(TASK_UNINTERRUPTIBLE
);
328 if (dio
->bio_list
== NULL
) {
329 dio
->waiter
= current
;
330 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
333 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
336 set_current_state(TASK_RUNNING
);
339 dio
->bio_list
= bio
->bi_private
;
340 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
345 * Process one completed BIO. No locks are held.
347 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
)
349 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
350 struct bio_vec
*bvec
= bio
->bi_io_vec
;
356 if (dio
->is_async
&& dio
->rw
== READ
) {
357 bio_check_pages_dirty(bio
); /* transfers ownership */
359 for (page_no
= 0; page_no
< bio
->bi_vcnt
; page_no
++) {
360 struct page
*page
= bvec
[page_no
].bv_page
;
363 set_page_dirty_lock(page
);
364 page_cache_release(page
);
368 finished_one_bio(dio
);
369 return uptodate
? 0 : -EIO
;
373 * Wait on and process all in-flight BIOs.
375 static int dio_await_completion(struct dio
*dio
)
382 while (atomic_read(&dio
->bio_count
)) {
383 struct bio
*bio
= dio_await_one(dio
);
386 ret2
= dio_bio_complete(dio
, bio
);
394 * A really large O_DIRECT read or write can generate a lot of BIOs. So
395 * to keep the memory consumption sane we periodically reap any completed BIOs
396 * during the BIO generation phase.
398 * This also helps to limit the peak amount of pinned userspace memory.
400 static int dio_bio_reap(struct dio
*dio
)
404 if (dio
->reap_counter
++ >= 64) {
405 while (dio
->bio_list
) {
409 spin_lock_irqsave(&dio
->bio_list_lock
, flags
);
411 dio
->bio_list
= bio
->bi_private
;
412 spin_unlock_irqrestore(&dio
->bio_list_lock
, flags
);
413 ret
= dio_bio_complete(dio
, bio
);
415 dio
->reap_counter
= 0;
421 * Call into the fs to map some more disk blocks. We record the current number
422 * of available blocks at dio->blocks_available. These are in units of the
423 * fs blocksize, (1 << inode->i_blkbits).
425 * The fs is allowed to map lots of blocks at once. If it wants to do that,
426 * it uses the passed inode-relative block number as the file offset, as usual.
428 * get_blocks() is passed the number of i_blkbits-sized blocks which direct_io
429 * has remaining to do. The fs should not map more than this number of blocks.
431 * If the fs has mapped a lot of blocks, it should populate bh->b_size to
432 * indicate how much contiguous disk space has been made available at
435 * If *any* of the mapped blocks are new, then the fs must set buffer_new().
436 * This isn't very efficient...
438 * In the case of filesystem holes: the fs may return an arbitrarily-large
439 * hole by returning an appropriate value in b_size and by clearing
440 * buffer_mapped(). However the direct-io code will only process holes one
441 * block at a time - it will repeatedly call get_blocks() as it walks the hole.
443 static int get_more_blocks(struct dio
*dio
)
446 struct buffer_head
*map_bh
= &dio
->map_bh
;
447 sector_t fs_startblk
; /* Into file, in filesystem-sized blocks */
448 unsigned long fs_count
; /* Number of filesystem-sized blocks */
449 unsigned long dio_count
;/* Number of dio_block-sized blocks */
450 unsigned long blkmask
;
453 * If there was a memory error and we've overwritten all the
454 * mapped blocks then we can now return that memory error
456 ret
= dio
->page_errors
;
460 BUG_ON(dio
->block_in_file
>= dio
->final_block_in_request
);
461 fs_startblk
= dio
->block_in_file
>> dio
->blkfactor
;
462 dio_count
= dio
->final_block_in_request
- dio
->block_in_file
;
463 fs_count
= dio_count
>> dio
->blkfactor
;
464 blkmask
= (1 << dio
->blkfactor
) - 1;
465 if (dio_count
& blkmask
)
468 ret
= (*dio
->get_blocks
)(dio
->inode
, fs_startblk
, fs_count
,
469 map_bh
, dio
->rw
== WRITE
);
475 * There is no bio. Make one now.
477 static int dio_new_bio(struct dio
*dio
, sector_t start_sector
)
482 ret
= dio_bio_reap(dio
);
485 sector
= start_sector
<< (dio
->blkbits
- 9);
486 nr_pages
= min(dio
->pages_in_io
, bio_get_nr_vecs(dio
->map_bh
.b_bdev
));
487 BUG_ON(nr_pages
<= 0);
488 ret
= dio_bio_alloc(dio
, dio
->map_bh
.b_bdev
, sector
, nr_pages
);
495 * Attempt to put the current chunk of 'cur_page' into the current BIO. If
496 * that was successful then update final_block_in_bio and take a ref against
497 * the just-added page.
499 * Return zero on success. Non-zero means the caller needs to start a new BIO.
501 static int dio_bio_add_page(struct dio
*dio
)
505 ret
= bio_add_page(dio
->bio
, dio
->cur_page
,
506 dio
->cur_page_len
, dio
->cur_page_offset
);
507 if (ret
== dio
->cur_page_len
) {
509 page_cache_get(dio
->cur_page
);
510 dio
->final_block_in_bio
= dio
->cur_page_block
+
511 (dio
->cur_page_len
>> dio
->blkbits
);
520 * Put cur_page under IO. The section of cur_page which is described by
521 * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
522 * starts on-disk at cur_page_block.
524 * We take a ref against the page here (on behalf of its presence in the bio).
526 * The caller of this function is responsible for removing cur_page from the
527 * dio, and for dropping the refcount which came from that presence.
529 static int dio_send_cur_page(struct dio
*dio
)
535 * See whether this new request is contiguous with the old
537 if (dio
->final_block_in_bio
!= dio
->cur_page_block
)
540 * Submit now if the underlying fs is about to perform a
547 if (dio
->bio
== NULL
) {
548 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
553 if (dio_bio_add_page(dio
) != 0) {
555 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
557 ret
= dio_bio_add_page(dio
);
566 * An autonomous function to put a chunk of a page under deferred IO.
568 * The caller doesn't actually know (or care) whether this piece of page is in
569 * a BIO, or is under IO or whatever. We just take care of all possible
570 * situations here. The separation between the logic of do_direct_IO() and
571 * that of submit_page_section() is important for clarity. Please don't break.
573 * The chunk of page starts on-disk at blocknr.
575 * We perform deferred IO, by recording the last-submitted page inside our
576 * private part of the dio structure. If possible, we just expand the IO
577 * across that page here.
579 * If that doesn't work out then we put the old page into the bio and add this
580 * page to the dio instead.
583 submit_page_section(struct dio
*dio
, struct page
*page
,
584 unsigned offset
, unsigned len
, sector_t blocknr
)
589 * Can we just grow the current page's presence in the dio?
591 if ( (dio
->cur_page
== page
) &&
592 (dio
->cur_page_offset
+ dio
->cur_page_len
== offset
) &&
593 (dio
->cur_page_block
+
594 (dio
->cur_page_len
>> dio
->blkbits
) == blocknr
)) {
595 dio
->cur_page_len
+= len
;
598 * If dio->boundary then we want to schedule the IO now to
599 * avoid metadata seeks.
602 ret
= dio_send_cur_page(dio
);
603 page_cache_release(dio
->cur_page
);
604 dio
->cur_page
= NULL
;
610 * If there's a deferred page already there then send it.
613 ret
= dio_send_cur_page(dio
);
614 page_cache_release(dio
->cur_page
);
615 dio
->cur_page
= NULL
;
620 page_cache_get(page
); /* It is in dio */
621 dio
->cur_page
= page
;
622 dio
->cur_page_offset
= offset
;
623 dio
->cur_page_len
= len
;
624 dio
->cur_page_block
= blocknr
;
630 * Clean any dirty buffers in the blockdev mapping which alias newly-created
631 * file blocks. Only called for S_ISREG files - blockdevs do not set
634 static void clean_blockdev_aliases(struct dio
*dio
)
638 for (i
= 0; i
< dio
->blocks_available
; i
++) {
639 unmap_underlying_metadata(dio
->map_bh
.b_bdev
,
640 dio
->map_bh
.b_blocknr
+ i
);
645 * If we are not writing the entire block and get_block() allocated
646 * the block for us, we need to fill-in the unused portion of the
647 * block with zeros. This happens only if user-buffer, fileoffset or
648 * io length is not filesystem block-size multiple.
650 * `end' is zero if we're doing the start of the IO, 1 at the end of the
653 static void dio_zero_block(struct dio
*dio
, int end
)
655 unsigned dio_blocks_per_fs_block
;
656 unsigned this_chunk_blocks
; /* In dio_blocks */
657 unsigned this_chunk_bytes
;
660 dio
->start_zero_done
= 1;
661 if (!dio
->blkfactor
|| !buffer_new(&dio
->map_bh
))
664 dio_blocks_per_fs_block
= 1 << dio
->blkfactor
;
665 this_chunk_blocks
= dio
->block_in_file
& (dio_blocks_per_fs_block
- 1);
667 if (!this_chunk_blocks
)
671 * We need to zero out part of an fs block. It is either at the
672 * beginning or the end of the fs block.
675 this_chunk_blocks
= dio_blocks_per_fs_block
- this_chunk_blocks
;
677 this_chunk_bytes
= this_chunk_blocks
<< dio
->blkbits
;
679 page
= ZERO_PAGE(dio
->cur_user_address
);
680 if (submit_page_section(dio
, page
, 0, this_chunk_bytes
,
681 dio
->next_block_for_io
))
684 dio
->next_block_for_io
+= this_chunk_blocks
;
688 * Walk the user pages, and the file, mapping blocks to disk and generating
689 * a sequence of (page,offset,len,block) mappings. These mappings are injected
690 * into submit_page_section(), which takes care of the next stage of submission
692 * Direct IO against a blockdev is different from a file. Because we can
693 * happily perform page-sized but 512-byte aligned IOs. It is important that
694 * blockdev IO be able to have fine alignment and large sizes.
696 * So what we do is to permit the ->get_blocks function to populate bh.b_size
697 * with the size of IO which is permitted at this offset and this i_blkbits.
699 * For best results, the blockdev should be set up with 512-byte i_blkbits and
700 * it should set b_size to PAGE_SIZE or more inside get_blocks(). This gives
701 * fine alignment but still allows this function to work in PAGE_SIZE units.
703 static int do_direct_IO(struct dio
*dio
)
705 const unsigned blkbits
= dio
->blkbits
;
706 const unsigned blocks_per_page
= PAGE_SIZE
>> blkbits
;
708 unsigned block_in_page
;
709 struct buffer_head
*map_bh
= &dio
->map_bh
;
712 /* The I/O can start at any block offset within the first page */
713 block_in_page
= dio
->first_block_in_page
;
715 while (dio
->block_in_file
< dio
->final_block_in_request
) {
716 page
= dio_get_page(dio
);
722 while (block_in_page
< blocks_per_page
) {
723 unsigned offset_in_page
= block_in_page
<< blkbits
;
724 unsigned this_chunk_bytes
; /* # of bytes mapped */
725 unsigned this_chunk_blocks
; /* # of blocks */
728 if (dio
->blocks_available
== 0) {
730 * Need to go and map some more disk
732 unsigned long blkmask
;
733 unsigned long dio_remainder
;
735 ret
= get_more_blocks(dio
);
737 page_cache_release(page
);
740 if (!buffer_mapped(map_bh
))
743 dio
->blocks_available
=
744 map_bh
->b_size
>> dio
->blkbits
;
745 dio
->next_block_for_io
=
746 map_bh
->b_blocknr
<< dio
->blkfactor
;
747 if (buffer_new(map_bh
))
748 clean_blockdev_aliases(dio
);
753 blkmask
= (1 << dio
->blkfactor
) - 1;
754 dio_remainder
= (dio
->block_in_file
& blkmask
);
757 * If we are at the start of IO and that IO
758 * starts partway into a fs-block,
759 * dio_remainder will be non-zero. If the IO
760 * is a read then we can simply advance the IO
761 * cursor to the first block which is to be
762 * read. But if the IO is a write and the
763 * block was newly allocated we cannot do that;
764 * the start of the fs block must be zeroed out
767 if (!buffer_new(map_bh
))
768 dio
->next_block_for_io
+= dio_remainder
;
769 dio
->blocks_available
-= dio_remainder
;
773 if (!buffer_mapped(map_bh
)) {
776 if (dio
->block_in_file
>=
777 i_size_read(dio
->inode
)>>blkbits
) {
779 page_cache_release(page
);
782 kaddr
= kmap_atomic(page
, KM_USER0
);
783 memset(kaddr
+ (block_in_page
<< blkbits
),
785 flush_dcache_page(page
);
786 kunmap_atomic(kaddr
, KM_USER0
);
787 dio
->block_in_file
++;
793 * If we're performing IO which has an alignment which
794 * is finer than the underlying fs, go check to see if
795 * we must zero out the start of this block.
797 if (unlikely(dio
->blkfactor
&& !dio
->start_zero_done
))
798 dio_zero_block(dio
, 0);
801 * Work out, in this_chunk_blocks, how much disk we
802 * can add to this page
804 this_chunk_blocks
= dio
->blocks_available
;
805 u
= (PAGE_SIZE
- offset_in_page
) >> blkbits
;
806 if (this_chunk_blocks
> u
)
807 this_chunk_blocks
= u
;
808 u
= dio
->final_block_in_request
- dio
->block_in_file
;
809 if (this_chunk_blocks
> u
)
810 this_chunk_blocks
= u
;
811 this_chunk_bytes
= this_chunk_blocks
<< blkbits
;
812 BUG_ON(this_chunk_bytes
== 0);
814 dio
->boundary
= buffer_boundary(map_bh
);
815 ret
= submit_page_section(dio
, page
, offset_in_page
,
816 this_chunk_bytes
, dio
->next_block_for_io
);
818 page_cache_release(page
);
821 dio
->next_block_for_io
+= this_chunk_blocks
;
823 dio
->block_in_file
+= this_chunk_blocks
;
824 block_in_page
+= this_chunk_blocks
;
825 dio
->blocks_available
-= this_chunk_blocks
;
827 if (dio
->block_in_file
> dio
->final_block_in_request
)
829 if (dio
->block_in_file
== dio
->final_block_in_request
)
833 /* Drop the ref which was taken in get_user_pages() */
834 page_cache_release(page
);
842 direct_io_worker(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
843 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
,
844 unsigned blkbits
, get_blocks_t get_blocks
, dio_iodone_t end_io
)
846 unsigned long user_addr
;
853 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
856 dio
->is_async
= !is_sync_kiocb(iocb
);
861 dio
->blkbits
= blkbits
;
862 dio
->blkfactor
= inode
->i_blkbits
- blkbits
;
863 dio
->start_zero_done
= 0;
864 dio
->block_in_file
= offset
>> blkbits
;
865 dio
->blocks_available
= 0;
867 dio
->cur_page
= NULL
;
870 dio
->reap_counter
= 0;
871 dio
->get_blocks
= get_blocks
;
872 dio
->end_io
= end_io
;
873 dio
->map_bh
.b_private
= NULL
;
874 dio
->final_block_in_bio
= -1;
875 dio
->next_block_for_io
= -1;
877 dio
->page_errors
= 0;
882 * BIO completion state.
884 * ->bio_count starts out at one, and we decrement it to zero after all
885 * BIOs are submitted. This to avoid the situation where a really fast
886 * (or synchronous) device could take the count to zero while we're
887 * still submitting BIOs.
889 atomic_set(&dio
->bio_count
, 1);
890 atomic_set(&dio
->bios_in_flight
, 0);
891 spin_lock_init(&dio
->bio_list_lock
);
892 dio
->bio_list
= NULL
;
895 dio
->pages_in_io
= 0;
896 for (seg
= 0; seg
< nr_segs
; seg
++)
897 dio
->pages_in_io
+= (iov
[seg
].iov_len
>> blkbits
) + 2;
899 for (seg
= 0; seg
< nr_segs
; seg
++) {
900 user_addr
= (unsigned long)iov
[seg
].iov_base
;
901 bytes
= iov
[seg
].iov_len
;
903 /* Index into the first page of the first block */
904 dio
->first_block_in_page
= (user_addr
& ~PAGE_MASK
) >> blkbits
;
905 dio
->final_block_in_request
= dio
->block_in_file
+
907 /* Page fetching state */
912 dio
->total_pages
= 0;
913 if (user_addr
& (PAGE_SIZE
-1)) {
915 bytes
-= PAGE_SIZE
- (user_addr
& (PAGE_SIZE
- 1));
917 dio
->total_pages
+= (bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
918 dio
->curr_user_address
= user_addr
;
920 ret
= do_direct_IO(dio
);
922 dio
->result
+= iov
[seg
].iov_len
-
923 ((dio
->final_block_in_request
- dio
->block_in_file
) <<
930 } /* end iovec loop */
933 * There may be some unwritten disk at the end of a part-written
934 * fs-block-sized block. Go zero that now.
936 dio_zero_block(dio
, 1);
939 ret2
= dio_send_cur_page(dio
);
942 page_cache_release(dio
->cur_page
);
943 dio
->cur_page
= NULL
;
949 * OK, all BIOs are submitted, so we can decrement bio_count to truly
950 * reflect the number of to-be-processed BIOs.
954 ret
= dio
->result
; /* Bytes written */
955 finished_one_bio(dio
); /* This can free the dio */
958 finished_one_bio(dio
);
959 ret2
= dio_await_completion(dio
);
963 ret
= dio
->page_errors
;
964 if (ret
== 0 && dio
->result
) {
965 loff_t i_size
= i_size_read(inode
);
969 * Adjust the return value if the read crossed a
970 * non-block-aligned EOF.
972 if (rw
== READ
&& (offset
+ ret
> i_size
))
973 ret
= i_size
- offset
;
975 dio_complete(dio
, offset
, ret
);
982 * This is a library function for use by filesystem drivers.
985 blockdev_direct_IO(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
986 struct block_device
*bdev
, const struct iovec
*iov
, loff_t offset
,
987 unsigned long nr_segs
, get_blocks_t get_blocks
, dio_iodone_t end_io
)
992 unsigned blkbits
= inode
->i_blkbits
;
993 unsigned bdev_blkbits
= 0;
994 unsigned blocksize_mask
= (1 << blkbits
) - 1;
995 ssize_t retval
= -EINVAL
;
998 bdev_blkbits
= blksize_bits(bdev_hardsect_size(bdev
));
1000 if (offset
& blocksize_mask
) {
1002 blkbits
= bdev_blkbits
;
1003 blocksize_mask
= (1 << blkbits
) - 1;
1004 if (offset
& blocksize_mask
)
1008 /* Check the memory alignment. Blocks cannot straddle pages */
1009 for (seg
= 0; seg
< nr_segs
; seg
++) {
1010 addr
= (unsigned long)iov
[seg
].iov_base
;
1011 size
= iov
[seg
].iov_len
;
1012 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
)) {
1014 blkbits
= bdev_blkbits
;
1015 blocksize_mask
= (1 << blkbits
) - 1;
1016 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
1021 retval
= direct_io_worker(rw
, iocb
, inode
, iov
, offset
,
1022 nr_segs
, blkbits
, get_blocks
, end_io
);