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
56 * lock_type is DIO_LOCKING for regular files on direct-IO-naive filesystems.
57 * This determines whether we need to do the fancy locking which prevents
58 * direct-IO from being able to read uninitialised disk blocks. If its zero
59 * (blockdev) this locking is not done, and if it is DIO_OWN_LOCKING i_mutex is
60 * not held for the entire direct write (taken briefly, initially, during a
61 * direct read though, but its never held for the duration of a direct-IO).
65 /* BIO submission state */
66 struct bio
*bio
; /* bio under assembly */
69 loff_t i_size
; /* i_size when submitted */
70 int lock_type
; /* doesn't change */
71 unsigned blkbits
; /* doesn't change */
72 unsigned blkfactor
; /* When we're using an alignment which
73 is finer than the filesystem's soft
74 blocksize, this specifies how much
75 finer. blkfactor=2 means 1/4-block
76 alignment. Does not change */
77 unsigned start_zero_done
; /* flag: sub-blocksize zeroing has
78 been performed at the start of a
80 int pages_in_io
; /* approximate total IO pages */
81 size_t size
; /* total request size (doesn't change)*/
82 sector_t block_in_file
; /* Current offset into the underlying
83 file in dio_block units. */
84 unsigned blocks_available
; /* At block_in_file. changes */
85 sector_t final_block_in_request
;/* doesn't change */
86 unsigned first_block_in_page
; /* doesn't change, Used only once */
87 int boundary
; /* prev block is at a boundary */
88 int reap_counter
; /* rate limit reaping */
89 get_blocks_t
*get_blocks
; /* block mapping function */
90 dio_iodone_t
*end_io
; /* IO completion function */
91 sector_t final_block_in_bio
; /* current final block in bio + 1 */
92 sector_t next_block_for_io
; /* next block to be put under IO,
93 in dio_blocks units */
94 struct buffer_head map_bh
; /* last get_blocks() result */
97 * Deferred addition of a page to the dio. These variables are
98 * private to dio_send_cur_page(), submit_page_section() and
101 struct page
*cur_page
; /* The page */
102 unsigned cur_page_offset
; /* Offset into it, in bytes */
103 unsigned cur_page_len
; /* Nr of bytes at cur_page_offset */
104 sector_t cur_page_block
; /* Where it starts */
107 * Page fetching state. These variables belong to dio_refill_pages().
109 int curr_page
; /* changes */
110 int total_pages
; /* doesn't change */
111 unsigned long curr_user_address
;/* changes */
114 * Page queue. These variables belong to dio_refill_pages() and
117 struct page
*pages
[DIO_PAGES
]; /* page buffer */
118 unsigned head
; /* next page to process */
119 unsigned tail
; /* last valid page + 1 */
120 int page_errors
; /* errno from get_user_pages() */
122 /* BIO completion state */
123 spinlock_t bio_lock
; /* protects BIO fields below */
124 int bio_count
; /* nr bios to be completed */
125 int bios_in_flight
; /* nr bios in flight */
126 struct bio
*bio_list
; /* singly linked via bi_private */
127 struct task_struct
*waiter
; /* waiting task (NULL if none) */
129 /* AIO related stuff */
130 struct kiocb
*iocb
; /* kiocb */
131 int is_async
; /* is IO async ? */
132 int io_error
; /* IO error in completion path */
133 ssize_t result
; /* IO result */
137 * How many pages are in the queue?
139 static inline unsigned dio_pages_present(struct dio
*dio
)
141 return dio
->tail
- dio
->head
;
145 * Go grab and pin some userspace pages. Typically we'll get 64 at a time.
147 static int dio_refill_pages(struct dio
*dio
)
152 nr_pages
= min(dio
->total_pages
- dio
->curr_page
, DIO_PAGES
);
153 down_read(¤t
->mm
->mmap_sem
);
154 ret
= get_user_pages(
155 current
, /* Task for fault acounting */
156 current
->mm
, /* whose pages? */
157 dio
->curr_user_address
, /* Where from? */
158 nr_pages
, /* How many pages? */
159 dio
->rw
== READ
, /* Write to memory? */
163 up_read(¤t
->mm
->mmap_sem
);
165 if (ret
< 0 && dio
->blocks_available
&& (dio
->rw
== WRITE
)) {
166 struct page
*page
= ZERO_PAGE(dio
->curr_user_address
);
168 * A memory fault, but the filesystem has some outstanding
169 * mapped blocks. We need to use those blocks up to avoid
170 * leaking stale data in the file.
172 if (dio
->page_errors
== 0)
173 dio
->page_errors
= ret
;
174 page_cache_get(page
);
175 dio
->pages
[0] = page
;
183 dio
->curr_user_address
+= ret
* PAGE_SIZE
;
184 dio
->curr_page
+= ret
;
194 * Get another userspace page. Returns an ERR_PTR on error. Pages are
195 * buffered inside the dio so that we can call get_user_pages() against a
196 * decent number of pages, less frequently. To provide nicer use of the
199 static struct page
*dio_get_page(struct dio
*dio
)
201 if (dio_pages_present(dio
) == 0) {
204 ret
= dio_refill_pages(dio
);
207 BUG_ON(dio_pages_present(dio
) == 0);
209 return dio
->pages
[dio
->head
++];
213 * Called when all DIO BIO I/O has been completed - let the filesystem
214 * know, if it registered an interest earlier via get_blocks. Pass the
215 * private field of the map buffer_head so that filesystems can use it
216 * to hold additional state between get_blocks calls and dio_complete.
218 static void dio_complete(struct dio
*dio
, loff_t offset
, ssize_t bytes
)
220 if (dio
->end_io
&& dio
->result
)
221 dio
->end_io(dio
->iocb
, offset
, bytes
, dio
->map_bh
.b_private
);
222 if (dio
->lock_type
== DIO_LOCKING
)
223 up_read(&dio
->inode
->i_alloc_sem
);
227 * Called when a BIO has been processed. If the count goes to zero then IO is
228 * complete and we can signal this to the AIO layer.
230 static void finished_one_bio(struct dio
*dio
)
234 spin_lock_irqsave(&dio
->bio_lock
, flags
);
235 if (dio
->bio_count
== 1) {
241 * Last reference to the dio is going away.
242 * Drop spinlock and complete the DIO.
244 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
246 /* Check for short read case */
247 transferred
= dio
->result
;
248 offset
= dio
->iocb
->ki_pos
;
250 if ((dio
->rw
== READ
) &&
251 ((offset
+ transferred
) > dio
->i_size
))
252 transferred
= dio
->i_size
- offset
;
254 /* check for error in completion path */
256 transferred
= dio
->io_error
;
258 dio_complete(dio
, offset
, transferred
);
260 /* Complete AIO later if falling back to buffered i/o */
261 if (dio
->result
== dio
->size
||
262 ((dio
->rw
== READ
) && dio
->result
)) {
263 aio_complete(dio
->iocb
, transferred
, 0);
268 * Falling back to buffered
270 spin_lock_irqsave(&dio
->bio_lock
, flags
);
273 wake_up_process(dio
->waiter
);
274 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
280 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
283 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
);
285 * Asynchronous IO callback.
287 static int dio_bio_end_aio(struct bio
*bio
, unsigned int bytes_done
, int error
)
289 struct dio
*dio
= bio
->bi_private
;
294 /* cleanup the bio */
295 dio_bio_complete(dio
, bio
);
300 * The BIO completion handler simply queues the BIO up for the process-context
303 * During I/O bi_private points at the dio. After I/O, bi_private is used to
304 * implement a singly-linked list of completed BIOs, at dio->bio_list.
306 static int dio_bio_end_io(struct bio
*bio
, unsigned int bytes_done
, int error
)
308 struct dio
*dio
= bio
->bi_private
;
314 spin_lock_irqsave(&dio
->bio_lock
, flags
);
315 bio
->bi_private
= dio
->bio_list
;
317 dio
->bios_in_flight
--;
318 if (dio
->waiter
&& dio
->bios_in_flight
== 0)
319 wake_up_process(dio
->waiter
);
320 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
325 dio_bio_alloc(struct dio
*dio
, struct block_device
*bdev
,
326 sector_t first_sector
, int nr_vecs
)
330 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
;
346 * In the AIO read case we speculatively dirty the pages before starting IO.
347 * During IO completion, any of these pages which happen to have been written
348 * back will be redirtied by bio_check_pages_dirty().
350 static void dio_bio_submit(struct dio
*dio
)
352 struct bio
*bio
= dio
->bio
;
355 bio
->bi_private
= dio
;
356 spin_lock_irqsave(&dio
->bio_lock
, flags
);
358 dio
->bios_in_flight
++;
359 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
360 if (dio
->is_async
&& dio
->rw
== READ
)
361 bio_set_pages_dirty(bio
);
362 submit_bio(dio
->rw
, bio
);
369 * Release any resources in case of a failure
371 static void dio_cleanup(struct dio
*dio
)
373 while (dio_pages_present(dio
))
374 page_cache_release(dio_get_page(dio
));
378 * Wait for the next BIO to complete. Remove it and return it.
380 static struct bio
*dio_await_one(struct dio
*dio
)
385 spin_lock_irqsave(&dio
->bio_lock
, flags
);
386 while (dio
->bio_list
== NULL
) {
387 set_current_state(TASK_UNINTERRUPTIBLE
);
388 if (dio
->bio_list
== NULL
) {
389 dio
->waiter
= current
;
390 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
391 blk_run_address_space(dio
->inode
->i_mapping
);
393 spin_lock_irqsave(&dio
->bio_lock
, flags
);
396 set_current_state(TASK_RUNNING
);
399 dio
->bio_list
= bio
->bi_private
;
400 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
405 * Process one completed BIO. No locks are held.
407 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
)
409 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
410 struct bio_vec
*bvec
= bio
->bi_io_vec
;
414 dio
->io_error
= -EIO
;
416 if (dio
->is_async
&& dio
->rw
== READ
) {
417 bio_check_pages_dirty(bio
); /* transfers ownership */
419 for (page_no
= 0; page_no
< bio
->bi_vcnt
; page_no
++) {
420 struct page
*page
= bvec
[page_no
].bv_page
;
422 if (dio
->rw
== READ
&& !PageCompound(page
))
423 set_page_dirty_lock(page
);
424 page_cache_release(page
);
428 finished_one_bio(dio
);
429 return uptodate
? 0 : -EIO
;
433 * Wait on and process all in-flight BIOs.
435 static int dio_await_completion(struct dio
*dio
)
443 * The bio_lock is not held for the read of bio_count.
444 * This is ok since it is the dio_bio_complete() that changes
447 while (dio
->bio_count
) {
448 struct bio
*bio
= dio_await_one(dio
);
451 ret2
= dio_bio_complete(dio
, bio
);
459 * A really large O_DIRECT read or write can generate a lot of BIOs. So
460 * to keep the memory consumption sane we periodically reap any completed BIOs
461 * during the BIO generation phase.
463 * This also helps to limit the peak amount of pinned userspace memory.
465 static int dio_bio_reap(struct dio
*dio
)
469 if (dio
->reap_counter
++ >= 64) {
470 while (dio
->bio_list
) {
475 spin_lock_irqsave(&dio
->bio_lock
, flags
);
477 dio
->bio_list
= bio
->bi_private
;
478 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
479 ret2
= dio_bio_complete(dio
, bio
);
483 dio
->reap_counter
= 0;
489 * Call into the fs to map some more disk blocks. We record the current number
490 * of available blocks at dio->blocks_available. These are in units of the
491 * fs blocksize, (1 << inode->i_blkbits).
493 * The fs is allowed to map lots of blocks at once. If it wants to do that,
494 * it uses the passed inode-relative block number as the file offset, as usual.
496 * get_blocks() is passed the number of i_blkbits-sized blocks which direct_io
497 * has remaining to do. The fs should not map more than this number of blocks.
499 * If the fs has mapped a lot of blocks, it should populate bh->b_size to
500 * indicate how much contiguous disk space has been made available at
503 * If *any* of the mapped blocks are new, then the fs must set buffer_new().
504 * This isn't very efficient...
506 * In the case of filesystem holes: the fs may return an arbitrarily-large
507 * hole by returning an appropriate value in b_size and by clearing
508 * buffer_mapped(). However the direct-io code will only process holes one
509 * block at a time - it will repeatedly call get_blocks() as it walks the hole.
511 static int get_more_blocks(struct dio
*dio
)
514 struct buffer_head
*map_bh
= &dio
->map_bh
;
515 sector_t fs_startblk
; /* Into file, in filesystem-sized blocks */
516 unsigned long fs_count
; /* Number of filesystem-sized blocks */
517 unsigned long dio_count
;/* Number of dio_block-sized blocks */
518 unsigned long blkmask
;
522 * If there was a memory error and we've overwritten all the
523 * mapped blocks then we can now return that memory error
525 ret
= dio
->page_errors
;
529 BUG_ON(dio
->block_in_file
>= dio
->final_block_in_request
);
530 fs_startblk
= dio
->block_in_file
>> dio
->blkfactor
;
531 dio_count
= dio
->final_block_in_request
- dio
->block_in_file
;
532 fs_count
= dio_count
>> dio
->blkfactor
;
533 blkmask
= (1 << dio
->blkfactor
) - 1;
534 if (dio_count
& blkmask
)
537 create
= dio
->rw
== WRITE
;
538 if (dio
->lock_type
== DIO_LOCKING
) {
539 if (dio
->block_in_file
< (i_size_read(dio
->inode
) >>
542 } else if (dio
->lock_type
== DIO_NO_LOCKING
) {
546 * For writes inside i_size we forbid block creations: only
547 * overwrites are permitted. We fall back to buffered writes
548 * at a higher level for inside-i_size block-instantiating
551 ret
= (*dio
->get_blocks
)(dio
->inode
, fs_startblk
, fs_count
,
558 * There is no bio. Make one now.
560 static int dio_new_bio(struct dio
*dio
, sector_t start_sector
)
565 ret
= dio_bio_reap(dio
);
568 sector
= start_sector
<< (dio
->blkbits
- 9);
569 nr_pages
= min(dio
->pages_in_io
, bio_get_nr_vecs(dio
->map_bh
.b_bdev
));
570 BUG_ON(nr_pages
<= 0);
571 ret
= dio_bio_alloc(dio
, dio
->map_bh
.b_bdev
, sector
, nr_pages
);
578 * Attempt to put the current chunk of 'cur_page' into the current BIO. If
579 * that was successful then update final_block_in_bio and take a ref against
580 * the just-added page.
582 * Return zero on success. Non-zero means the caller needs to start a new BIO.
584 static int dio_bio_add_page(struct dio
*dio
)
588 ret
= bio_add_page(dio
->bio
, dio
->cur_page
,
589 dio
->cur_page_len
, dio
->cur_page_offset
);
590 if (ret
== dio
->cur_page_len
) {
592 * Decrement count only, if we are done with this page
594 if ((dio
->cur_page_len
+ dio
->cur_page_offset
) == PAGE_SIZE
)
596 page_cache_get(dio
->cur_page
);
597 dio
->final_block_in_bio
= dio
->cur_page_block
+
598 (dio
->cur_page_len
>> dio
->blkbits
);
607 * Put cur_page under IO. The section of cur_page which is described by
608 * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
609 * starts on-disk at cur_page_block.
611 * We take a ref against the page here (on behalf of its presence in the bio).
613 * The caller of this function is responsible for removing cur_page from the
614 * dio, and for dropping the refcount which came from that presence.
616 static int dio_send_cur_page(struct dio
*dio
)
622 * See whether this new request is contiguous with the old
624 if (dio
->final_block_in_bio
!= dio
->cur_page_block
)
627 * Submit now if the underlying fs is about to perform a
634 if (dio
->bio
== NULL
) {
635 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
640 if (dio_bio_add_page(dio
) != 0) {
642 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
644 ret
= dio_bio_add_page(dio
);
653 * An autonomous function to put a chunk of a page under deferred IO.
655 * The caller doesn't actually know (or care) whether this piece of page is in
656 * a BIO, or is under IO or whatever. We just take care of all possible
657 * situations here. The separation between the logic of do_direct_IO() and
658 * that of submit_page_section() is important for clarity. Please don't break.
660 * The chunk of page starts on-disk at blocknr.
662 * We perform deferred IO, by recording the last-submitted page inside our
663 * private part of the dio structure. If possible, we just expand the IO
664 * across that page here.
666 * If that doesn't work out then we put the old page into the bio and add this
667 * page to the dio instead.
670 submit_page_section(struct dio
*dio
, struct page
*page
,
671 unsigned offset
, unsigned len
, sector_t blocknr
)
676 * Can we just grow the current page's presence in the dio?
678 if ( (dio
->cur_page
== page
) &&
679 (dio
->cur_page_offset
+ dio
->cur_page_len
== offset
) &&
680 (dio
->cur_page_block
+
681 (dio
->cur_page_len
>> dio
->blkbits
) == blocknr
)) {
682 dio
->cur_page_len
+= len
;
685 * If dio->boundary then we want to schedule the IO now to
686 * avoid metadata seeks.
689 ret
= dio_send_cur_page(dio
);
690 page_cache_release(dio
->cur_page
);
691 dio
->cur_page
= NULL
;
697 * If there's a deferred page already there then send it.
700 ret
= dio_send_cur_page(dio
);
701 page_cache_release(dio
->cur_page
);
702 dio
->cur_page
= NULL
;
707 page_cache_get(page
); /* It is in dio */
708 dio
->cur_page
= page
;
709 dio
->cur_page_offset
= offset
;
710 dio
->cur_page_len
= len
;
711 dio
->cur_page_block
= blocknr
;
717 * Clean any dirty buffers in the blockdev mapping which alias newly-created
718 * file blocks. Only called for S_ISREG files - blockdevs do not set
721 static void clean_blockdev_aliases(struct dio
*dio
)
726 nblocks
= dio
->map_bh
.b_size
>> dio
->inode
->i_blkbits
;
728 for (i
= 0; i
< nblocks
; i
++) {
729 unmap_underlying_metadata(dio
->map_bh
.b_bdev
,
730 dio
->map_bh
.b_blocknr
+ i
);
735 * If we are not writing the entire block and get_block() allocated
736 * the block for us, we need to fill-in the unused portion of the
737 * block with zeros. This happens only if user-buffer, fileoffset or
738 * io length is not filesystem block-size multiple.
740 * `end' is zero if we're doing the start of the IO, 1 at the end of the
743 static void dio_zero_block(struct dio
*dio
, int end
)
745 unsigned dio_blocks_per_fs_block
;
746 unsigned this_chunk_blocks
; /* In dio_blocks */
747 unsigned this_chunk_bytes
;
750 dio
->start_zero_done
= 1;
751 if (!dio
->blkfactor
|| !buffer_new(&dio
->map_bh
))
754 dio_blocks_per_fs_block
= 1 << dio
->blkfactor
;
755 this_chunk_blocks
= dio
->block_in_file
& (dio_blocks_per_fs_block
- 1);
757 if (!this_chunk_blocks
)
761 * We need to zero out part of an fs block. It is either at the
762 * beginning or the end of the fs block.
765 this_chunk_blocks
= dio_blocks_per_fs_block
- this_chunk_blocks
;
767 this_chunk_bytes
= this_chunk_blocks
<< dio
->blkbits
;
769 page
= ZERO_PAGE(dio
->curr_user_address
);
770 if (submit_page_section(dio
, page
, 0, this_chunk_bytes
,
771 dio
->next_block_for_io
))
774 dio
->next_block_for_io
+= this_chunk_blocks
;
778 * Walk the user pages, and the file, mapping blocks to disk and generating
779 * a sequence of (page,offset,len,block) mappings. These mappings are injected
780 * into submit_page_section(), which takes care of the next stage of submission
782 * Direct IO against a blockdev is different from a file. Because we can
783 * happily perform page-sized but 512-byte aligned IOs. It is important that
784 * blockdev IO be able to have fine alignment and large sizes.
786 * So what we do is to permit the ->get_blocks function to populate bh.b_size
787 * with the size of IO which is permitted at this offset and this i_blkbits.
789 * For best results, the blockdev should be set up with 512-byte i_blkbits and
790 * it should set b_size to PAGE_SIZE or more inside get_blocks(). This gives
791 * fine alignment but still allows this function to work in PAGE_SIZE units.
793 static int do_direct_IO(struct dio
*dio
)
795 const unsigned blkbits
= dio
->blkbits
;
796 const unsigned blocks_per_page
= PAGE_SIZE
>> blkbits
;
798 unsigned block_in_page
;
799 struct buffer_head
*map_bh
= &dio
->map_bh
;
802 /* The I/O can start at any block offset within the first page */
803 block_in_page
= dio
->first_block_in_page
;
805 while (dio
->block_in_file
< dio
->final_block_in_request
) {
806 page
= dio_get_page(dio
);
812 while (block_in_page
< blocks_per_page
) {
813 unsigned offset_in_page
= block_in_page
<< blkbits
;
814 unsigned this_chunk_bytes
; /* # of bytes mapped */
815 unsigned this_chunk_blocks
; /* # of blocks */
818 if (dio
->blocks_available
== 0) {
820 * Need to go and map some more disk
822 unsigned long blkmask
;
823 unsigned long dio_remainder
;
825 ret
= get_more_blocks(dio
);
827 page_cache_release(page
);
830 if (!buffer_mapped(map_bh
))
833 dio
->blocks_available
=
834 map_bh
->b_size
>> dio
->blkbits
;
835 dio
->next_block_for_io
=
836 map_bh
->b_blocknr
<< dio
->blkfactor
;
837 if (buffer_new(map_bh
))
838 clean_blockdev_aliases(dio
);
843 blkmask
= (1 << dio
->blkfactor
) - 1;
844 dio_remainder
= (dio
->block_in_file
& blkmask
);
847 * If we are at the start of IO and that IO
848 * starts partway into a fs-block,
849 * dio_remainder will be non-zero. If the IO
850 * is a read then we can simply advance the IO
851 * cursor to the first block which is to be
852 * read. But if the IO is a write and the
853 * block was newly allocated we cannot do that;
854 * the start of the fs block must be zeroed out
857 if (!buffer_new(map_bh
))
858 dio
->next_block_for_io
+= dio_remainder
;
859 dio
->blocks_available
-= dio_remainder
;
863 if (!buffer_mapped(map_bh
)) {
865 loff_t i_size_aligned
;
867 /* AKPM: eargh, -ENOTBLK is a hack */
868 if (dio
->rw
== WRITE
) {
869 page_cache_release(page
);
874 * Be sure to account for a partial block as the
875 * last block in the file
877 i_size_aligned
= ALIGN(i_size_read(dio
->inode
),
879 if (dio
->block_in_file
>=
880 i_size_aligned
>> blkbits
) {
882 page_cache_release(page
);
885 kaddr
= kmap_atomic(page
, KM_USER0
);
886 memset(kaddr
+ (block_in_page
<< blkbits
),
888 flush_dcache_page(page
);
889 kunmap_atomic(kaddr
, KM_USER0
);
890 dio
->block_in_file
++;
896 * If we're performing IO which has an alignment which
897 * is finer than the underlying fs, go check to see if
898 * we must zero out the start of this block.
900 if (unlikely(dio
->blkfactor
&& !dio
->start_zero_done
))
901 dio_zero_block(dio
, 0);
904 * Work out, in this_chunk_blocks, how much disk we
905 * can add to this page
907 this_chunk_blocks
= dio
->blocks_available
;
908 u
= (PAGE_SIZE
- offset_in_page
) >> blkbits
;
909 if (this_chunk_blocks
> u
)
910 this_chunk_blocks
= u
;
911 u
= dio
->final_block_in_request
- dio
->block_in_file
;
912 if (this_chunk_blocks
> u
)
913 this_chunk_blocks
= u
;
914 this_chunk_bytes
= this_chunk_blocks
<< blkbits
;
915 BUG_ON(this_chunk_bytes
== 0);
917 dio
->boundary
= buffer_boundary(map_bh
);
918 ret
= submit_page_section(dio
, page
, offset_in_page
,
919 this_chunk_bytes
, dio
->next_block_for_io
);
921 page_cache_release(page
);
924 dio
->next_block_for_io
+= this_chunk_blocks
;
926 dio
->block_in_file
+= this_chunk_blocks
;
927 block_in_page
+= this_chunk_blocks
;
928 dio
->blocks_available
-= this_chunk_blocks
;
930 if (dio
->block_in_file
> dio
->final_block_in_request
)
932 if (dio
->block_in_file
== dio
->final_block_in_request
)
936 /* Drop the ref which was taken in get_user_pages() */
937 page_cache_release(page
);
945 * Releases both i_mutex and i_alloc_sem
948 direct_io_worker(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
949 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
,
950 unsigned blkbits
, get_blocks_t get_blocks
, dio_iodone_t end_io
,
953 unsigned long user_addr
;
962 dio
->blkbits
= blkbits
;
963 dio
->blkfactor
= inode
->i_blkbits
- blkbits
;
964 dio
->start_zero_done
= 0;
966 dio
->block_in_file
= offset
>> blkbits
;
967 dio
->blocks_available
= 0;
968 dio
->cur_page
= NULL
;
971 dio
->reap_counter
= 0;
972 dio
->get_blocks
= get_blocks
;
973 dio
->end_io
= end_io
;
974 dio
->map_bh
.b_private
= NULL
;
975 dio
->final_block_in_bio
= -1;
976 dio
->next_block_for_io
= -1;
978 dio
->page_errors
= 0;
982 dio
->i_size
= i_size_read(inode
);
985 * BIO completion state.
987 * ->bio_count starts out at one, and we decrement it to zero after all
988 * BIOs are submitted. This to avoid the situation where a really fast
989 * (or synchronous) device could take the count to zero while we're
990 * still submitting BIOs.
993 dio
->bios_in_flight
= 0;
994 spin_lock_init(&dio
->bio_lock
);
995 dio
->bio_list
= NULL
;
999 * In case of non-aligned buffers, we may need 2 more
1000 * pages since we need to zero out first and last block.
1002 if (unlikely(dio
->blkfactor
))
1003 dio
->pages_in_io
= 2;
1005 dio
->pages_in_io
= 0;
1007 for (seg
= 0; seg
< nr_segs
; seg
++) {
1008 user_addr
= (unsigned long)iov
[seg
].iov_base
;
1010 ((user_addr
+iov
[seg
].iov_len
+PAGE_SIZE
-1)/PAGE_SIZE
1011 - user_addr
/PAGE_SIZE
);
1014 for (seg
= 0; seg
< nr_segs
; seg
++) {
1015 user_addr
= (unsigned long)iov
[seg
].iov_base
;
1016 dio
->size
+= bytes
= iov
[seg
].iov_len
;
1018 /* Index into the first page of the first block */
1019 dio
->first_block_in_page
= (user_addr
& ~PAGE_MASK
) >> blkbits
;
1020 dio
->final_block_in_request
= dio
->block_in_file
+
1022 /* Page fetching state */
1027 dio
->total_pages
= 0;
1028 if (user_addr
& (PAGE_SIZE
-1)) {
1030 bytes
-= PAGE_SIZE
- (user_addr
& (PAGE_SIZE
- 1));
1032 dio
->total_pages
+= (bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1033 dio
->curr_user_address
= user_addr
;
1035 ret
= do_direct_IO(dio
);
1037 dio
->result
+= iov
[seg
].iov_len
-
1038 ((dio
->final_block_in_request
- dio
->block_in_file
) <<
1045 } /* end iovec loop */
1047 if (ret
== -ENOTBLK
&& rw
== WRITE
) {
1049 * The remaining part of the request will be
1050 * be handled by buffered I/O when we return
1055 * There may be some unwritten disk at the end of a part-written
1056 * fs-block-sized block. Go zero that now.
1058 dio_zero_block(dio
, 1);
1060 if (dio
->cur_page
) {
1061 ret2
= dio_send_cur_page(dio
);
1064 page_cache_release(dio
->cur_page
);
1065 dio
->cur_page
= NULL
;
1068 dio_bio_submit(dio
);
1071 * It is possible that, we return short IO due to end of file.
1072 * In that case, we need to release all the pages we got hold on.
1077 * All block lookups have been performed. For READ requests
1078 * we can let i_mutex go now that its achieved its purpose
1079 * of protecting us from looking up uninitialized blocks.
1081 if ((rw
== READ
) && (dio
->lock_type
== DIO_LOCKING
))
1082 mutex_unlock(&dio
->inode
->i_mutex
);
1085 * OK, all BIOs are submitted, so we can decrement bio_count to truly
1086 * reflect the number of to-be-processed BIOs.
1088 if (dio
->is_async
) {
1089 int should_wait
= 0;
1091 if (dio
->result
< dio
->size
&& rw
== WRITE
) {
1092 dio
->waiter
= current
;
1097 finished_one_bio(dio
); /* This can free the dio */
1098 blk_run_address_space(inode
->i_mapping
);
1100 unsigned long flags
;
1102 * Wait for already issued I/O to drain out and
1103 * release its references to user-space pages
1104 * before returning to fallback on buffered I/O
1107 spin_lock_irqsave(&dio
->bio_lock
, flags
);
1108 set_current_state(TASK_UNINTERRUPTIBLE
);
1109 while (dio
->bio_count
) {
1110 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
1112 spin_lock_irqsave(&dio
->bio_lock
, flags
);
1113 set_current_state(TASK_UNINTERRUPTIBLE
);
1115 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
1116 set_current_state(TASK_RUNNING
);
1120 ssize_t transferred
= 0;
1122 finished_one_bio(dio
);
1123 ret2
= dio_await_completion(dio
);
1127 ret
= dio
->page_errors
;
1129 loff_t i_size
= i_size_read(inode
);
1131 transferred
= dio
->result
;
1133 * Adjust the return value if the read crossed a
1134 * non-block-aligned EOF.
1136 if (rw
== READ
&& (offset
+ transferred
> i_size
))
1137 transferred
= i_size
- offset
;
1139 dio_complete(dio
, offset
, transferred
);
1143 /* We could have also come here on an AIO file extend */
1144 if (!is_sync_kiocb(iocb
) && rw
== WRITE
&&
1145 ret
>= 0 && dio
->result
== dio
->size
)
1147 * For AIO writes where we have completed the
1148 * i/o, we have to mark the the aio complete.
1150 aio_complete(iocb
, ret
, 0);
1157 * This is a library function for use by filesystem drivers.
1158 * The locking rules are governed by the dio_lock_type parameter.
1160 * DIO_NO_LOCKING (no locking, for raw block device access)
1161 * For writes, i_mutex is not held on entry; it is never taken.
1163 * DIO_LOCKING (simple locking for regular files)
1164 * For writes we are called under i_mutex and return with i_mutex held, even
1165 * though it is internally dropped.
1166 * For reads, i_mutex is not held on entry, but it is taken and dropped before
1169 * DIO_OWN_LOCKING (filesystem provides synchronisation and handling of
1170 * uninitialised data, allowing parallel direct readers and writers)
1171 * For writes we are called without i_mutex, return without it, never touch it.
1172 * For reads we are called under i_mutex and return with i_mutex held, even
1173 * though it may be internally dropped.
1175 * Additional i_alloc_sem locking requirements described inline below.
1178 __blockdev_direct_IO(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
1179 struct block_device
*bdev
, const struct iovec
*iov
, loff_t offset
,
1180 unsigned long nr_segs
, get_blocks_t get_blocks
, dio_iodone_t end_io
,
1186 unsigned blkbits
= inode
->i_blkbits
;
1187 unsigned bdev_blkbits
= 0;
1188 unsigned blocksize_mask
= (1 << blkbits
) - 1;
1189 ssize_t retval
= -EINVAL
;
1190 loff_t end
= offset
;
1192 int release_i_mutex
= 0;
1193 int acquire_i_mutex
= 0;
1196 current
->flags
|= PF_SYNCWRITE
;
1199 bdev_blkbits
= blksize_bits(bdev_hardsect_size(bdev
));
1201 if (offset
& blocksize_mask
) {
1203 blkbits
= bdev_blkbits
;
1204 blocksize_mask
= (1 << blkbits
) - 1;
1205 if (offset
& blocksize_mask
)
1209 /* Check the memory alignment. Blocks cannot straddle pages */
1210 for (seg
= 0; seg
< nr_segs
; seg
++) {
1211 addr
= (unsigned long)iov
[seg
].iov_base
;
1212 size
= iov
[seg
].iov_len
;
1214 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
)) {
1216 blkbits
= bdev_blkbits
;
1217 blocksize_mask
= (1 << blkbits
) - 1;
1218 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
1223 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
1229 * For block device access DIO_NO_LOCKING is used,
1230 * neither readers nor writers do any locking at all
1231 * For regular files using DIO_LOCKING,
1232 * readers need to grab i_mutex and i_alloc_sem
1233 * writers need to grab i_alloc_sem only (i_mutex is already held)
1234 * For regular files using DIO_OWN_LOCKING,
1235 * neither readers nor writers take any locks here
1237 dio
->lock_type
= dio_lock_type
;
1238 if (dio_lock_type
!= DIO_NO_LOCKING
) {
1239 /* watch out for a 0 len io from a tricksy fs */
1240 if (rw
== READ
&& end
> offset
) {
1241 struct address_space
*mapping
;
1243 mapping
= iocb
->ki_filp
->f_mapping
;
1244 if (dio_lock_type
!= DIO_OWN_LOCKING
) {
1245 mutex_lock(&inode
->i_mutex
);
1246 release_i_mutex
= 1;
1249 retval
= filemap_write_and_wait_range(mapping
, offset
,
1256 if (dio_lock_type
== DIO_OWN_LOCKING
) {
1257 mutex_unlock(&inode
->i_mutex
);
1258 acquire_i_mutex
= 1;
1262 if (dio_lock_type
== DIO_LOCKING
)
1263 down_read(&inode
->i_alloc_sem
);
1267 * For file extending writes updating i_size before data
1268 * writeouts complete can expose uninitialized blocks. So
1269 * even for AIO, we need to wait for i/o to complete before
1270 * returning in this case.
1272 dio
->is_async
= !is_sync_kiocb(iocb
) && !((rw
== WRITE
) &&
1273 (end
> i_size_read(inode
)));
1275 retval
= direct_io_worker(rw
, iocb
, inode
, iov
, offset
,
1276 nr_segs
, blkbits
, get_blocks
, end_io
, dio
);
1278 if (rw
== READ
&& dio_lock_type
== DIO_LOCKING
)
1279 release_i_mutex
= 0;
1282 if (release_i_mutex
)
1283 mutex_unlock(&inode
->i_mutex
);
1284 else if (acquire_i_mutex
)
1285 mutex_lock(&inode
->i_mutex
);
1287 current
->flags
&= ~PF_SYNCWRITE
;
1290 EXPORT_SYMBOL(__blockdev_direct_IO
);