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/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
57 * lock_type is DIO_LOCKING for regular files on direct-IO-naive filesystems.
58 * This determines whether we need to do the fancy locking which prevents
59 * direct-IO from being able to read uninitialised disk blocks. If its zero
60 * (blockdev) this locking is not done, and if it is DIO_OWN_LOCKING i_mutex is
61 * not held for the entire direct write (taken briefly, initially, during a
62 * direct read though, but its never held for the duration of a direct-IO).
66 /* BIO submission state */
67 struct bio
*bio
; /* bio under assembly */
70 loff_t i_size
; /* i_size when submitted */
71 int lock_type
; /* doesn't change */
72 unsigned blkbits
; /* doesn't change */
73 unsigned blkfactor
; /* When we're using an alignment which
74 is finer than the filesystem's soft
75 blocksize, this specifies how much
76 finer. blkfactor=2 means 1/4-block
77 alignment. Does not change */
78 unsigned start_zero_done
; /* flag: sub-blocksize zeroing has
79 been performed at the start of a
81 int pages_in_io
; /* approximate total IO pages */
82 size_t size
; /* total request size (doesn't change)*/
83 sector_t block_in_file
; /* Current offset into the underlying
84 file in dio_block units. */
85 unsigned blocks_available
; /* At block_in_file. changes */
86 sector_t final_block_in_request
;/* doesn't change */
87 unsigned first_block_in_page
; /* doesn't change, Used only once */
88 int boundary
; /* prev block is at a boundary */
89 int reap_counter
; /* rate limit reaping */
90 get_block_t
*get_block
; /* block mapping function */
91 dio_iodone_t
*end_io
; /* IO completion function */
92 sector_t final_block_in_bio
; /* current final block in bio + 1 */
93 sector_t next_block_for_io
; /* next block to be put under IO,
94 in dio_blocks units */
95 struct buffer_head map_bh
; /* last get_block() result */
98 * Deferred addition of a page to the dio. These variables are
99 * private to dio_send_cur_page(), submit_page_section() and
100 * dio_bio_add_page().
102 struct page
*cur_page
; /* The page */
103 unsigned cur_page_offset
; /* Offset into it, in bytes */
104 unsigned cur_page_len
; /* Nr of bytes at cur_page_offset */
105 sector_t cur_page_block
; /* Where it starts */
108 * Page fetching state. These variables belong to dio_refill_pages().
110 int curr_page
; /* changes */
111 int total_pages
; /* doesn't change */
112 unsigned long curr_user_address
;/* changes */
115 * Page queue. These variables belong to dio_refill_pages() and
118 struct page
*pages
[DIO_PAGES
]; /* page buffer */
119 unsigned head
; /* next page to process */
120 unsigned tail
; /* last valid page + 1 */
121 int page_errors
; /* errno from get_user_pages() */
123 /* BIO completion state */
124 spinlock_t bio_lock
; /* protects BIO fields below */
125 int bio_count
; /* nr bios to be completed */
126 int bios_in_flight
; /* nr bios in flight */
127 struct bio
*bio_list
; /* singly linked via bi_private */
128 struct task_struct
*waiter
; /* waiting task (NULL if none) */
130 /* AIO related stuff */
131 struct kiocb
*iocb
; /* kiocb */
132 int is_async
; /* is IO async ? */
133 int io_error
; /* IO error in completion path */
134 ssize_t result
; /* IO result */
138 * How many pages are in the queue?
140 static inline unsigned dio_pages_present(struct dio
*dio
)
142 return dio
->tail
- dio
->head
;
146 * Go grab and pin some userspace pages. Typically we'll get 64 at a time.
148 static int dio_refill_pages(struct dio
*dio
)
153 nr_pages
= min(dio
->total_pages
- dio
->curr_page
, DIO_PAGES
);
154 down_read(¤t
->mm
->mmap_sem
);
155 ret
= get_user_pages(
156 current
, /* Task for fault acounting */
157 current
->mm
, /* whose pages? */
158 dio
->curr_user_address
, /* Where from? */
159 nr_pages
, /* How many pages? */
160 dio
->rw
== READ
, /* Write to memory? */
164 up_read(¤t
->mm
->mmap_sem
);
166 if (ret
< 0 && dio
->blocks_available
&& (dio
->rw
& WRITE
)) {
167 struct page
*page
= ZERO_PAGE(dio
->curr_user_address
);
169 * A memory fault, but the filesystem has some outstanding
170 * mapped blocks. We need to use those blocks up to avoid
171 * leaking stale data in the file.
173 if (dio
->page_errors
== 0)
174 dio
->page_errors
= ret
;
175 page_cache_get(page
);
176 dio
->pages
[0] = page
;
184 dio
->curr_user_address
+= ret
* PAGE_SIZE
;
185 dio
->curr_page
+= ret
;
195 * Get another userspace page. Returns an ERR_PTR on error. Pages are
196 * buffered inside the dio so that we can call get_user_pages() against a
197 * decent number of pages, less frequently. To provide nicer use of the
200 static struct page
*dio_get_page(struct dio
*dio
)
202 if (dio_pages_present(dio
) == 0) {
205 ret
= dio_refill_pages(dio
);
208 BUG_ON(dio_pages_present(dio
) == 0);
210 return dio
->pages
[dio
->head
++];
214 * dio_complete() - called when all DIO BIO I/O has been completed
215 * @offset: the byte offset in the file of the completed operation
217 * This releases locks as dictated by the locking type, lets interested parties
218 * know that a DIO operation has completed, and calculates the resulting return
219 * code for the operation.
221 * It lets the filesystem know if it registered an interest earlier via
222 * get_block. Pass the private field of the map buffer_head so that
223 * filesystems can use it to hold additional state between get_block calls and
226 static int dio_complete(struct dio
*dio
, loff_t offset
, int ret
)
228 ssize_t transferred
= 0;
231 transferred
= dio
->result
;
233 /* Check for short read case */
234 if ((dio
->rw
== READ
) && ((offset
+ transferred
) > dio
->i_size
))
235 transferred
= dio
->i_size
- offset
;
238 if (dio
->end_io
&& dio
->result
)
239 dio
->end_io(dio
->iocb
, offset
, transferred
,
240 dio
->map_bh
.b_private
);
241 if (dio
->lock_type
== DIO_LOCKING
)
242 /* lockdep: non-owner release */
243 up_read_non_owner(&dio
->inode
->i_alloc_sem
);
246 ret
= dio
->page_errors
;
256 * Called when a BIO has been processed. If the count goes to zero then IO is
257 * complete and we can signal this to the AIO layer.
259 static void finished_one_bio(struct dio
*dio
)
263 spin_lock_irqsave(&dio
->bio_lock
, flags
);
264 if (dio
->bio_count
== 1) {
269 * Last reference to the dio is going away.
270 * Drop spinlock and complete the DIO.
272 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
274 ret
= dio_complete(dio
, dio
->iocb
->ki_pos
, 0);
276 /* Complete AIO later if falling back to buffered i/o */
277 if (dio
->result
== dio
->size
||
278 ((dio
->rw
== READ
) && dio
->result
)) {
279 aio_complete(dio
->iocb
, ret
, 0);
284 * Falling back to buffered
286 spin_lock_irqsave(&dio
->bio_lock
, flags
);
289 wake_up_process(dio
->waiter
);
290 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
296 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
299 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
);
301 * Asynchronous IO callback.
303 static int dio_bio_end_aio(struct bio
*bio
, unsigned int bytes_done
, int error
)
305 struct dio
*dio
= bio
->bi_private
;
310 /* cleanup the bio */
311 dio_bio_complete(dio
, bio
);
316 * The BIO completion handler simply queues the BIO up for the process-context
319 * During I/O bi_private points at the dio. After I/O, bi_private is used to
320 * implement a singly-linked list of completed BIOs, at dio->bio_list.
322 static int dio_bio_end_io(struct bio
*bio
, unsigned int bytes_done
, int error
)
324 struct dio
*dio
= bio
->bi_private
;
330 spin_lock_irqsave(&dio
->bio_lock
, flags
);
331 bio
->bi_private
= dio
->bio_list
;
333 dio
->bios_in_flight
--;
334 if (dio
->waiter
&& dio
->bios_in_flight
== 0)
335 wake_up_process(dio
->waiter
);
336 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
341 dio_bio_alloc(struct dio
*dio
, struct block_device
*bdev
,
342 sector_t first_sector
, int nr_vecs
)
346 bio
= bio_alloc(GFP_KERNEL
, nr_vecs
);
351 bio
->bi_sector
= first_sector
;
353 bio
->bi_end_io
= dio_bio_end_aio
;
355 bio
->bi_end_io
= dio_bio_end_io
;
362 * In the AIO read case we speculatively dirty the pages before starting IO.
363 * During IO completion, any of these pages which happen to have been written
364 * back will be redirtied by bio_check_pages_dirty().
366 static void dio_bio_submit(struct dio
*dio
)
368 struct bio
*bio
= dio
->bio
;
371 bio
->bi_private
= dio
;
372 spin_lock_irqsave(&dio
->bio_lock
, flags
);
374 dio
->bios_in_flight
++;
375 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
376 if (dio
->is_async
&& dio
->rw
== READ
)
377 bio_set_pages_dirty(bio
);
378 submit_bio(dio
->rw
, bio
);
385 * Release any resources in case of a failure
387 static void dio_cleanup(struct dio
*dio
)
389 while (dio_pages_present(dio
))
390 page_cache_release(dio_get_page(dio
));
394 * Wait for the next BIO to complete. Remove it and return it.
396 static struct bio
*dio_await_one(struct dio
*dio
)
401 spin_lock_irqsave(&dio
->bio_lock
, flags
);
402 while (dio
->bio_list
== NULL
) {
403 set_current_state(TASK_UNINTERRUPTIBLE
);
404 if (dio
->bio_list
== NULL
) {
405 dio
->waiter
= current
;
406 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
407 blk_run_address_space(dio
->inode
->i_mapping
);
409 spin_lock_irqsave(&dio
->bio_lock
, flags
);
412 set_current_state(TASK_RUNNING
);
415 dio
->bio_list
= bio
->bi_private
;
416 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
421 * Process one completed BIO. No locks are held.
423 static int dio_bio_complete(struct dio
*dio
, struct bio
*bio
)
425 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
426 struct bio_vec
*bvec
= bio
->bi_io_vec
;
430 dio
->io_error
= -EIO
;
432 if (dio
->is_async
&& dio
->rw
== READ
) {
433 bio_check_pages_dirty(bio
); /* transfers ownership */
435 for (page_no
= 0; page_no
< bio
->bi_vcnt
; page_no
++) {
436 struct page
*page
= bvec
[page_no
].bv_page
;
438 if (dio
->rw
== READ
&& !PageCompound(page
))
439 set_page_dirty_lock(page
);
440 page_cache_release(page
);
444 finished_one_bio(dio
);
445 return uptodate
? 0 : -EIO
;
449 * Wait on and process all in-flight BIOs.
451 static void dio_await_completion(struct dio
*dio
)
457 * The bio_lock is not held for the read of bio_count.
458 * This is ok since it is the dio_bio_complete() that changes
461 while (dio
->bio_count
) {
462 struct bio
*bio
= dio_await_one(dio
);
463 /* io errors are propogated through dio->io_error */
464 dio_bio_complete(dio
, bio
);
469 * A really large O_DIRECT read or write can generate a lot of BIOs. So
470 * to keep the memory consumption sane we periodically reap any completed BIOs
471 * during the BIO generation phase.
473 * This also helps to limit the peak amount of pinned userspace memory.
475 static int dio_bio_reap(struct dio
*dio
)
479 if (dio
->reap_counter
++ >= 64) {
480 while (dio
->bio_list
) {
485 spin_lock_irqsave(&dio
->bio_lock
, flags
);
487 dio
->bio_list
= bio
->bi_private
;
488 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
489 ret2
= dio_bio_complete(dio
, bio
);
493 dio
->reap_counter
= 0;
499 * Call into the fs to map some more disk blocks. We record the current number
500 * of available blocks at dio->blocks_available. These are in units of the
501 * fs blocksize, (1 << inode->i_blkbits).
503 * The fs is allowed to map lots of blocks at once. If it wants to do that,
504 * it uses the passed inode-relative block number as the file offset, as usual.
506 * get_block() is passed the number of i_blkbits-sized blocks which direct_io
507 * has remaining to do. The fs should not map more than this number of blocks.
509 * If the fs has mapped a lot of blocks, it should populate bh->b_size to
510 * indicate how much contiguous disk space has been made available at
513 * If *any* of the mapped blocks are new, then the fs must set buffer_new().
514 * This isn't very efficient...
516 * In the case of filesystem holes: the fs may return an arbitrarily-large
517 * hole by returning an appropriate value in b_size and by clearing
518 * buffer_mapped(). However the direct-io code will only process holes one
519 * block at a time - it will repeatedly call get_block() as it walks the hole.
521 static int get_more_blocks(struct dio
*dio
)
524 struct buffer_head
*map_bh
= &dio
->map_bh
;
525 sector_t fs_startblk
; /* Into file, in filesystem-sized blocks */
526 unsigned long fs_count
; /* Number of filesystem-sized blocks */
527 unsigned long dio_count
;/* Number of dio_block-sized blocks */
528 unsigned long blkmask
;
532 * If there was a memory error and we've overwritten all the
533 * mapped blocks then we can now return that memory error
535 ret
= dio
->page_errors
;
537 BUG_ON(dio
->block_in_file
>= dio
->final_block_in_request
);
538 fs_startblk
= dio
->block_in_file
>> dio
->blkfactor
;
539 dio_count
= dio
->final_block_in_request
- dio
->block_in_file
;
540 fs_count
= dio_count
>> dio
->blkfactor
;
541 blkmask
= (1 << dio
->blkfactor
) - 1;
542 if (dio_count
& blkmask
)
546 map_bh
->b_size
= fs_count
<< dio
->inode
->i_blkbits
;
548 create
= dio
->rw
& WRITE
;
549 if (dio
->lock_type
== DIO_LOCKING
) {
550 if (dio
->block_in_file
< (i_size_read(dio
->inode
) >>
553 } else if (dio
->lock_type
== DIO_NO_LOCKING
) {
558 * For writes inside i_size we forbid block creations: only
559 * overwrites are permitted. We fall back to buffered writes
560 * at a higher level for inside-i_size block-instantiating
563 ret
= (*dio
->get_block
)(dio
->inode
, fs_startblk
,
570 * There is no bio. Make one now.
572 static int dio_new_bio(struct dio
*dio
, sector_t start_sector
)
577 ret
= dio_bio_reap(dio
);
580 sector
= start_sector
<< (dio
->blkbits
- 9);
581 nr_pages
= min(dio
->pages_in_io
, bio_get_nr_vecs(dio
->map_bh
.b_bdev
));
582 BUG_ON(nr_pages
<= 0);
583 ret
= dio_bio_alloc(dio
, dio
->map_bh
.b_bdev
, sector
, nr_pages
);
590 * Attempt to put the current chunk of 'cur_page' into the current BIO. If
591 * that was successful then update final_block_in_bio and take a ref against
592 * the just-added page.
594 * Return zero on success. Non-zero means the caller needs to start a new BIO.
596 static int dio_bio_add_page(struct dio
*dio
)
600 ret
= bio_add_page(dio
->bio
, dio
->cur_page
,
601 dio
->cur_page_len
, dio
->cur_page_offset
);
602 if (ret
== dio
->cur_page_len
) {
604 * Decrement count only, if we are done with this page
606 if ((dio
->cur_page_len
+ dio
->cur_page_offset
) == PAGE_SIZE
)
608 page_cache_get(dio
->cur_page
);
609 dio
->final_block_in_bio
= dio
->cur_page_block
+
610 (dio
->cur_page_len
>> dio
->blkbits
);
619 * Put cur_page under IO. The section of cur_page which is described by
620 * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
621 * starts on-disk at cur_page_block.
623 * We take a ref against the page here (on behalf of its presence in the bio).
625 * The caller of this function is responsible for removing cur_page from the
626 * dio, and for dropping the refcount which came from that presence.
628 static int dio_send_cur_page(struct dio
*dio
)
634 * See whether this new request is contiguous with the old
636 if (dio
->final_block_in_bio
!= dio
->cur_page_block
)
639 * Submit now if the underlying fs is about to perform a
646 if (dio
->bio
== NULL
) {
647 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
652 if (dio_bio_add_page(dio
) != 0) {
654 ret
= dio_new_bio(dio
, dio
->cur_page_block
);
656 ret
= dio_bio_add_page(dio
);
665 * An autonomous function to put a chunk of a page under deferred IO.
667 * The caller doesn't actually know (or care) whether this piece of page is in
668 * a BIO, or is under IO or whatever. We just take care of all possible
669 * situations here. The separation between the logic of do_direct_IO() and
670 * that of submit_page_section() is important for clarity. Please don't break.
672 * The chunk of page starts on-disk at blocknr.
674 * We perform deferred IO, by recording the last-submitted page inside our
675 * private part of the dio structure. If possible, we just expand the IO
676 * across that page here.
678 * If that doesn't work out then we put the old page into the bio and add this
679 * page to the dio instead.
682 submit_page_section(struct dio
*dio
, struct page
*page
,
683 unsigned offset
, unsigned len
, sector_t blocknr
)
687 if (dio
->rw
& WRITE
) {
689 * Read accounting is performed in submit_bio()
691 task_io_account_write(len
);
695 * Can we just grow the current page's presence in the dio?
697 if ( (dio
->cur_page
== page
) &&
698 (dio
->cur_page_offset
+ dio
->cur_page_len
== offset
) &&
699 (dio
->cur_page_block
+
700 (dio
->cur_page_len
>> dio
->blkbits
) == blocknr
)) {
701 dio
->cur_page_len
+= len
;
704 * If dio->boundary then we want to schedule the IO now to
705 * avoid metadata seeks.
708 ret
= dio_send_cur_page(dio
);
709 page_cache_release(dio
->cur_page
);
710 dio
->cur_page
= NULL
;
716 * If there's a deferred page already there then send it.
719 ret
= dio_send_cur_page(dio
);
720 page_cache_release(dio
->cur_page
);
721 dio
->cur_page
= NULL
;
726 page_cache_get(page
); /* It is in dio */
727 dio
->cur_page
= page
;
728 dio
->cur_page_offset
= offset
;
729 dio
->cur_page_len
= len
;
730 dio
->cur_page_block
= blocknr
;
736 * Clean any dirty buffers in the blockdev mapping which alias newly-created
737 * file blocks. Only called for S_ISREG files - blockdevs do not set
740 static void clean_blockdev_aliases(struct dio
*dio
)
745 nblocks
= dio
->map_bh
.b_size
>> dio
->inode
->i_blkbits
;
747 for (i
= 0; i
< nblocks
; i
++) {
748 unmap_underlying_metadata(dio
->map_bh
.b_bdev
,
749 dio
->map_bh
.b_blocknr
+ i
);
754 * If we are not writing the entire block and get_block() allocated
755 * the block for us, we need to fill-in the unused portion of the
756 * block with zeros. This happens only if user-buffer, fileoffset or
757 * io length is not filesystem block-size multiple.
759 * `end' is zero if we're doing the start of the IO, 1 at the end of the
762 static void dio_zero_block(struct dio
*dio
, int end
)
764 unsigned dio_blocks_per_fs_block
;
765 unsigned this_chunk_blocks
; /* In dio_blocks */
766 unsigned this_chunk_bytes
;
769 dio
->start_zero_done
= 1;
770 if (!dio
->blkfactor
|| !buffer_new(&dio
->map_bh
))
773 dio_blocks_per_fs_block
= 1 << dio
->blkfactor
;
774 this_chunk_blocks
= dio
->block_in_file
& (dio_blocks_per_fs_block
- 1);
776 if (!this_chunk_blocks
)
780 * We need to zero out part of an fs block. It is either at the
781 * beginning or the end of the fs block.
784 this_chunk_blocks
= dio_blocks_per_fs_block
- this_chunk_blocks
;
786 this_chunk_bytes
= this_chunk_blocks
<< dio
->blkbits
;
788 page
= ZERO_PAGE(dio
->curr_user_address
);
789 if (submit_page_section(dio
, page
, 0, this_chunk_bytes
,
790 dio
->next_block_for_io
))
793 dio
->next_block_for_io
+= this_chunk_blocks
;
797 * Walk the user pages, and the file, mapping blocks to disk and generating
798 * a sequence of (page,offset,len,block) mappings. These mappings are injected
799 * into submit_page_section(), which takes care of the next stage of submission
801 * Direct IO against a blockdev is different from a file. Because we can
802 * happily perform page-sized but 512-byte aligned IOs. It is important that
803 * blockdev IO be able to have fine alignment and large sizes.
805 * So what we do is to permit the ->get_block function to populate bh.b_size
806 * with the size of IO which is permitted at this offset and this i_blkbits.
808 * For best results, the blockdev should be set up with 512-byte i_blkbits and
809 * it should set b_size to PAGE_SIZE or more inside get_block(). This gives
810 * fine alignment but still allows this function to work in PAGE_SIZE units.
812 static int do_direct_IO(struct dio
*dio
)
814 const unsigned blkbits
= dio
->blkbits
;
815 const unsigned blocks_per_page
= PAGE_SIZE
>> blkbits
;
817 unsigned block_in_page
;
818 struct buffer_head
*map_bh
= &dio
->map_bh
;
821 /* The I/O can start at any block offset within the first page */
822 block_in_page
= dio
->first_block_in_page
;
824 while (dio
->block_in_file
< dio
->final_block_in_request
) {
825 page
= dio_get_page(dio
);
831 while (block_in_page
< blocks_per_page
) {
832 unsigned offset_in_page
= block_in_page
<< blkbits
;
833 unsigned this_chunk_bytes
; /* # of bytes mapped */
834 unsigned this_chunk_blocks
; /* # of blocks */
837 if (dio
->blocks_available
== 0) {
839 * Need to go and map some more disk
841 unsigned long blkmask
;
842 unsigned long dio_remainder
;
844 ret
= get_more_blocks(dio
);
846 page_cache_release(page
);
849 if (!buffer_mapped(map_bh
))
852 dio
->blocks_available
=
853 map_bh
->b_size
>> dio
->blkbits
;
854 dio
->next_block_for_io
=
855 map_bh
->b_blocknr
<< dio
->blkfactor
;
856 if (buffer_new(map_bh
))
857 clean_blockdev_aliases(dio
);
862 blkmask
= (1 << dio
->blkfactor
) - 1;
863 dio_remainder
= (dio
->block_in_file
& blkmask
);
866 * If we are at the start of IO and that IO
867 * starts partway into a fs-block,
868 * dio_remainder will be non-zero. If the IO
869 * is a read then we can simply advance the IO
870 * cursor to the first block which is to be
871 * read. But if the IO is a write and the
872 * block was newly allocated we cannot do that;
873 * the start of the fs block must be zeroed out
876 if (!buffer_new(map_bh
))
877 dio
->next_block_for_io
+= dio_remainder
;
878 dio
->blocks_available
-= dio_remainder
;
882 if (!buffer_mapped(map_bh
)) {
884 loff_t i_size_aligned
;
886 /* AKPM: eargh, -ENOTBLK is a hack */
887 if (dio
->rw
& WRITE
) {
888 page_cache_release(page
);
893 * Be sure to account for a partial block as the
894 * last block in the file
896 i_size_aligned
= ALIGN(i_size_read(dio
->inode
),
898 if (dio
->block_in_file
>=
899 i_size_aligned
>> blkbits
) {
901 page_cache_release(page
);
904 kaddr
= kmap_atomic(page
, KM_USER0
);
905 memset(kaddr
+ (block_in_page
<< blkbits
),
907 flush_dcache_page(page
);
908 kunmap_atomic(kaddr
, KM_USER0
);
909 dio
->block_in_file
++;
915 * If we're performing IO which has an alignment which
916 * is finer than the underlying fs, go check to see if
917 * we must zero out the start of this block.
919 if (unlikely(dio
->blkfactor
&& !dio
->start_zero_done
))
920 dio_zero_block(dio
, 0);
923 * Work out, in this_chunk_blocks, how much disk we
924 * can add to this page
926 this_chunk_blocks
= dio
->blocks_available
;
927 u
= (PAGE_SIZE
- offset_in_page
) >> blkbits
;
928 if (this_chunk_blocks
> u
)
929 this_chunk_blocks
= u
;
930 u
= dio
->final_block_in_request
- dio
->block_in_file
;
931 if (this_chunk_blocks
> u
)
932 this_chunk_blocks
= u
;
933 this_chunk_bytes
= this_chunk_blocks
<< blkbits
;
934 BUG_ON(this_chunk_bytes
== 0);
936 dio
->boundary
= buffer_boundary(map_bh
);
937 ret
= submit_page_section(dio
, page
, offset_in_page
,
938 this_chunk_bytes
, dio
->next_block_for_io
);
940 page_cache_release(page
);
943 dio
->next_block_for_io
+= this_chunk_blocks
;
945 dio
->block_in_file
+= this_chunk_blocks
;
946 block_in_page
+= this_chunk_blocks
;
947 dio
->blocks_available
-= this_chunk_blocks
;
949 BUG_ON(dio
->block_in_file
> dio
->final_block_in_request
);
950 if (dio
->block_in_file
== dio
->final_block_in_request
)
954 /* Drop the ref which was taken in get_user_pages() */
955 page_cache_release(page
);
963 * Releases both i_mutex and i_alloc_sem
966 direct_io_worker(int rw
, struct kiocb
*iocb
, struct inode
*inode
,
967 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
,
968 unsigned blkbits
, get_block_t get_block
, dio_iodone_t end_io
,
971 unsigned long user_addr
;
980 dio
->blkbits
= blkbits
;
981 dio
->blkfactor
= inode
->i_blkbits
- blkbits
;
982 dio
->start_zero_done
= 0;
984 dio
->block_in_file
= offset
>> blkbits
;
985 dio
->blocks_available
= 0;
986 dio
->cur_page
= NULL
;
989 dio
->reap_counter
= 0;
990 dio
->get_block
= get_block
;
991 dio
->end_io
= end_io
;
992 dio
->map_bh
.b_private
= NULL
;
993 dio
->final_block_in_bio
= -1;
994 dio
->next_block_for_io
= -1;
996 dio
->page_errors
= 0;
1000 dio
->i_size
= i_size_read(inode
);
1003 * BIO completion state.
1005 * ->bio_count starts out at one, and we decrement it to zero after all
1006 * BIOs are submitted. This to avoid the situation where a really fast
1007 * (or synchronous) device could take the count to zero while we're
1008 * still submitting BIOs.
1011 dio
->bios_in_flight
= 0;
1012 spin_lock_init(&dio
->bio_lock
);
1013 dio
->bio_list
= NULL
;
1017 * In case of non-aligned buffers, we may need 2 more
1018 * pages since we need to zero out first and last block.
1020 if (unlikely(dio
->blkfactor
))
1021 dio
->pages_in_io
= 2;
1023 dio
->pages_in_io
= 0;
1025 for (seg
= 0; seg
< nr_segs
; seg
++) {
1026 user_addr
= (unsigned long)iov
[seg
].iov_base
;
1028 ((user_addr
+iov
[seg
].iov_len
+PAGE_SIZE
-1)/PAGE_SIZE
1029 - user_addr
/PAGE_SIZE
);
1032 for (seg
= 0; seg
< nr_segs
; seg
++) {
1033 user_addr
= (unsigned long)iov
[seg
].iov_base
;
1034 dio
->size
+= bytes
= iov
[seg
].iov_len
;
1036 /* Index into the first page of the first block */
1037 dio
->first_block_in_page
= (user_addr
& ~PAGE_MASK
) >> blkbits
;
1038 dio
->final_block_in_request
= dio
->block_in_file
+
1040 /* Page fetching state */
1045 dio
->total_pages
= 0;
1046 if (user_addr
& (PAGE_SIZE
-1)) {
1048 bytes
-= PAGE_SIZE
- (user_addr
& (PAGE_SIZE
- 1));
1050 dio
->total_pages
+= (bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1051 dio
->curr_user_address
= user_addr
;
1053 ret
= do_direct_IO(dio
);
1055 dio
->result
+= iov
[seg
].iov_len
-
1056 ((dio
->final_block_in_request
- dio
->block_in_file
) <<
1063 } /* end iovec loop */
1065 if (ret
== -ENOTBLK
&& (rw
& WRITE
)) {
1067 * The remaining part of the request will be
1068 * be handled by buffered I/O when we return
1073 * There may be some unwritten disk at the end of a part-written
1074 * fs-block-sized block. Go zero that now.
1076 dio_zero_block(dio
, 1);
1078 if (dio
->cur_page
) {
1079 ret2
= dio_send_cur_page(dio
);
1082 page_cache_release(dio
->cur_page
);
1083 dio
->cur_page
= NULL
;
1086 dio_bio_submit(dio
);
1089 * It is possible that, we return short IO due to end of file.
1090 * In that case, we need to release all the pages we got hold on.
1095 * All block lookups have been performed. For READ requests
1096 * we can let i_mutex go now that its achieved its purpose
1097 * of protecting us from looking up uninitialized blocks.
1099 if ((rw
== READ
) && (dio
->lock_type
== DIO_LOCKING
))
1100 mutex_unlock(&dio
->inode
->i_mutex
);
1103 * OK, all BIOs are submitted, so we can decrement bio_count to truly
1104 * reflect the number of to-be-processed BIOs.
1106 if (dio
->is_async
) {
1107 int should_wait
= 0;
1109 if (dio
->result
< dio
->size
&& (rw
& WRITE
)) {
1110 dio
->waiter
= current
;
1115 finished_one_bio(dio
); /* This can free the dio */
1116 blk_run_address_space(inode
->i_mapping
);
1118 unsigned long flags
;
1120 * Wait for already issued I/O to drain out and
1121 * release its references to user-space pages
1122 * before returning to fallback on buffered I/O
1125 spin_lock_irqsave(&dio
->bio_lock
, flags
);
1126 set_current_state(TASK_UNINTERRUPTIBLE
);
1127 while (dio
->bio_count
) {
1128 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
1130 spin_lock_irqsave(&dio
->bio_lock
, flags
);
1131 set_current_state(TASK_UNINTERRUPTIBLE
);
1133 spin_unlock_irqrestore(&dio
->bio_lock
, flags
);
1134 set_current_state(TASK_RUNNING
);
1138 finished_one_bio(dio
);
1139 dio_await_completion(dio
);
1141 ret
= dio_complete(dio
, offset
, ret
);
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_block_t get_block
, 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;
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 /* lockdep: not the owner will release it */
1264 down_read_non_owner(&inode
->i_alloc_sem
);
1268 * For file extending writes updating i_size before data
1269 * writeouts complete can expose uninitialized blocks. So
1270 * even for AIO, we need to wait for i/o to complete before
1271 * returning in this case.
1273 dio
->is_async
= !is_sync_kiocb(iocb
) && !((rw
& WRITE
) &&
1274 (end
> i_size_read(inode
)));
1276 retval
= direct_io_worker(rw
, iocb
, inode
, iov
, offset
,
1277 nr_segs
, blkbits
, get_block
, end_io
, dio
);
1279 if (rw
== READ
&& dio_lock_type
== DIO_LOCKING
)
1280 release_i_mutex
= 0;
1283 if (release_i_mutex
)
1284 mutex_unlock(&inode
->i_mutex
);
1285 else if (acquire_i_mutex
)
1286 mutex_lock(&inode
->i_mutex
);
1289 EXPORT_SYMBOL(__blockdev_direct_IO
);