[PATCH] s390 (4/7): ctc driver.
[linux-2.6/history.git] / fs / direct-io.c
blob4deb53b7c325fccb3f39acfb5c4f04d029094c24
1 /*
2 * fs/direct-io.c
4 * Copyright (C) 2002, Linus Torvalds.
6 * O_DIRECT
8 * 04Jul2002 akpm@zip.com.au
9 * Initial version
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>
24 #include <linux/fs.h>
25 #include <linux/mm.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.
42 #define DIO_PAGES 64
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
53 * blocksize.
56 struct dio {
57 /* BIO submission state */
58 struct bio *bio; /* bio under assembly */
59 struct inode *inode;
60 int rw;
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
69 write */
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
88 * dio_bio_add_page().
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
104 * dio_get_page().
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)
137 int ret;
138 int nr_pages;
140 nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
141 down_read(&current->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? */
148 0, /* force (?) */
149 &dio->pages[0],
150 NULL); /* vmas */
151 up_read(&current->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);
162 dio->head = 0;
163 dio->tail = 1;
164 ret = 0;
165 goto out;
168 if (ret >= 0) {
169 dio->curr_user_address += ret * PAGE_SIZE;
170 dio->curr_page += ret;
171 dio->head = 0;
172 dio->tail = ret;
173 ret = 0;
175 out:
176 return 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
183 * L1 cache.
185 static struct page *dio_get_page(struct dio *dio)
187 if (dio_pages_present(dio) == 0) {
188 int ret;
190 ret = dio_refill_pages(dio);
191 if (ret)
192 return ERR_PTR(ret);
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)
206 if (dio->end_io)
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)) {
217 if (dio->is_async) {
218 dio_complete(dio, dio->block_in_file << dio->blkbits,
219 dio->result);
220 aio_complete(dio->iocb, dio->result, 0);
221 kfree(dio);
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;
234 if (bio->bi_size)
235 return 1;
237 /* cleanup the bio */
238 dio_bio_complete(dio, bio);
239 return 0;
243 * The BIO completion handler simply queues the BIO up for the process-context
244 * handler.
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;
252 unsigned long flags;
254 if (bio->bi_size)
255 return 1;
257 spin_lock_irqsave(&dio->bio_list_lock, flags);
258 bio->bi_private = dio->bio_list;
259 dio->bio_list = bio;
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);
264 return 0;
267 static int
268 dio_bio_alloc(struct dio *dio, struct block_device *bdev,
269 sector_t first_sector, int nr_vecs)
271 struct bio *bio;
273 bio = bio_alloc(GFP_KERNEL, nr_vecs);
274 if (bio == NULL)
275 return -ENOMEM;
277 bio->bi_bdev = bdev;
278 bio->bi_sector = first_sector;
279 if (dio->is_async)
280 bio->bi_end_io = dio_bio_end_aio;
281 else
282 bio->bi_end_io = dio_bio_end_io;
284 dio->bio = bio;
285 return 0;
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);
304 dio->bio = NULL;
305 dio->boundary = 0;
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)
322 unsigned long flags;
323 struct bio *bio;
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);
331 blk_run_queues();
332 io_schedule();
333 spin_lock_irqsave(&dio->bio_list_lock, flags);
334 dio->waiter = NULL;
336 set_current_state(TASK_RUNNING);
338 bio = dio->bio_list;
339 dio->bio_list = bio->bi_private;
340 spin_unlock_irqrestore(&dio->bio_list_lock, flags);
341 return bio;
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;
351 int page_no;
353 if (!uptodate)
354 dio->result = -EIO;
356 if (dio->is_async && dio->rw == READ) {
357 bio_check_pages_dirty(bio); /* transfers ownership */
358 } else {
359 for (page_no = 0; page_no < bio->bi_vcnt; page_no++) {
360 struct page *page = bvec[page_no].bv_page;
362 if (dio->rw == READ)
363 set_page_dirty_lock(page);
364 page_cache_release(page);
366 bio_put(bio);
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)
377 int ret = 0;
379 if (dio->bio)
380 dio_bio_submit(dio);
382 while (atomic_read(&dio->bio_count)) {
383 struct bio *bio = dio_await_one(dio);
384 int ret2;
386 ret2 = dio_bio_complete(dio, bio);
387 if (ret == 0)
388 ret = ret2;
390 return ret;
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)
402 int ret = 0;
404 if (dio->reap_counter++ >= 64) {
405 while (dio->bio_list) {
406 unsigned long flags;
407 struct bio *bio;
409 spin_lock_irqsave(&dio->bio_list_lock, flags);
410 bio = dio->bio_list;
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;
417 return ret;
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
433 * bh->b_blocknr.
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)
445 int ret;
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;
457 if (ret == 0) {
458 map_bh->b_state = 0;
459 map_bh->b_size = 0;
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)
466 fs_count++;
468 ret = (*dio->get_blocks)(dio->inode, fs_startblk, fs_count,
469 map_bh, dio->rw == WRITE);
471 return ret;
475 * There is no bio. Make one now.
477 static int dio_new_bio(struct dio *dio, sector_t start_sector)
479 sector_t sector;
480 int ret, nr_pages;
482 ret = dio_bio_reap(dio);
483 if (ret)
484 goto out;
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);
489 dio->boundary = 0;
490 out:
491 return ret;
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)
503 int ret;
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) {
508 dio->pages_in_io--;
509 page_cache_get(dio->cur_page);
510 dio->final_block_in_bio = dio->cur_page_block +
511 (dio->cur_page_len >> dio->blkbits);
512 ret = 0;
513 } else {
514 ret = 1;
516 return ret;
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)
531 int ret = 0;
533 if (dio->bio) {
535 * See whether this new request is contiguous with the old
537 if (dio->final_block_in_bio != dio->cur_page_block)
538 dio_bio_submit(dio);
540 * Submit now if the underlying fs is about to perform a
541 * metadata read
543 if (dio->boundary)
544 dio_bio_submit(dio);
547 if (dio->bio == NULL) {
548 ret = dio_new_bio(dio, dio->cur_page_block);
549 if (ret)
550 goto out;
553 if (dio_bio_add_page(dio) != 0) {
554 dio_bio_submit(dio);
555 ret = dio_new_bio(dio, dio->cur_page_block);
556 if (ret == 0) {
557 ret = dio_bio_add_page(dio);
558 BUG_ON(ret != 0);
561 out:
562 return ret;
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.
582 static int
583 submit_page_section(struct dio *dio, struct page *page,
584 unsigned offset, unsigned len, sector_t blocknr)
586 int ret = 0;
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.
601 if (dio->boundary) {
602 ret = dio_send_cur_page(dio);
603 page_cache_release(dio->cur_page);
604 dio->cur_page = NULL;
606 goto out;
610 * If there's a deferred page already there then send it.
612 if (dio->cur_page) {
613 ret = dio_send_cur_page(dio);
614 page_cache_release(dio->cur_page);
615 dio->cur_page = NULL;
616 if (ret)
617 goto out;
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;
625 out:
626 return ret;
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
632 * buffer_new
634 static void clean_blockdev_aliases(struct dio *dio)
636 unsigned i;
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
651 * IO.
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;
658 struct page *page;
660 dio->start_zero_done = 1;
661 if (!dio->blkfactor || !buffer_new(&dio->map_bh))
662 return;
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)
668 return;
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.
674 if (end)
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))
682 return;
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;
707 struct page *page;
708 unsigned block_in_page;
709 struct buffer_head *map_bh = &dio->map_bh;
710 int ret = 0;
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);
717 if (IS_ERR(page)) {
718 ret = PTR_ERR(page);
719 goto out;
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 */
726 unsigned u;
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);
736 if (ret) {
737 page_cache_release(page);
738 goto out;
740 if (!buffer_mapped(map_bh))
741 goto do_holes;
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);
750 if (!dio->blkfactor)
751 goto do_holes;
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
765 * on-disk
767 if (!buffer_new(map_bh))
768 dio->next_block_for_io += dio_remainder;
769 dio->blocks_available -= dio_remainder;
771 do_holes:
772 /* Handle holes */
773 if (!buffer_mapped(map_bh)) {
774 char *kaddr;
776 if (dio->block_in_file >=
777 i_size_read(dio->inode)>>blkbits) {
778 /* We hit eof */
779 page_cache_release(page);
780 goto out;
782 kaddr = kmap_atomic(page, KM_USER0);
783 memset(kaddr + (block_in_page << blkbits),
784 0, 1 << blkbits);
785 flush_dcache_page(page);
786 kunmap_atomic(kaddr, KM_USER0);
787 dio->block_in_file++;
788 block_in_page++;
789 goto next_block;
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);
817 if (ret) {
818 page_cache_release(page);
819 goto out;
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;
826 next_block:
827 if (dio->block_in_file > dio->final_block_in_request)
828 BUG();
829 if (dio->block_in_file == dio->final_block_in_request)
830 break;
833 /* Drop the ref which was taken in get_user_pages() */
834 page_cache_release(page);
835 block_in_page = 0;
837 out:
838 return ret;
841 static int
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;
847 int seg;
848 int ret = 0;
849 int ret2;
850 struct dio *dio;
851 size_t bytes;
853 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
854 if (!dio)
855 return -ENOMEM;
856 dio->is_async = !is_sync_kiocb(iocb);
858 dio->bio = NULL;
859 dio->inode = inode;
860 dio->rw = rw;
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;
869 dio->boundary = 0;
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;
878 dio->result = 0;
879 dio->iocb = iocb;
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;
893 dio->waiter = 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 +
906 (bytes >> blkbits);
907 /* Page fetching state */
908 dio->head = 0;
909 dio->tail = 0;
910 dio->curr_page = 0;
912 dio->total_pages = 0;
913 if (user_addr & (PAGE_SIZE-1)) {
914 dio->total_pages++;
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) <<
924 blkbits);
926 if (ret) {
927 dio_cleanup(dio);
928 break;
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);
938 if (dio->cur_page) {
939 ret2 = dio_send_cur_page(dio);
940 if (ret == 0)
941 ret = ret2;
942 page_cache_release(dio->cur_page);
943 dio->cur_page = NULL;
945 if (dio->bio)
946 dio_bio_submit(dio);
949 * OK, all BIOs are submitted, so we can decrement bio_count to truly
950 * reflect the number of to-be-processed BIOs.
952 if (dio->is_async) {
953 if (ret == 0)
954 ret = dio->result; /* Bytes written */
955 finished_one_bio(dio); /* This can free the dio */
956 blk_run_queues();
957 } else {
958 finished_one_bio(dio);
959 ret2 = dio_await_completion(dio);
960 if (ret == 0)
961 ret = ret2;
962 if (ret == 0)
963 ret = dio->page_errors;
964 if (ret == 0 && dio->result) {
965 loff_t i_size = i_size_read(inode);
967 ret = dio->result;
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);
976 kfree(dio);
978 return 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)
989 int seg;
990 size_t size;
991 unsigned long addr;
992 unsigned blkbits = inode->i_blkbits;
993 unsigned bdev_blkbits = 0;
994 unsigned blocksize_mask = (1 << blkbits) - 1;
995 ssize_t retval = -EINVAL;
997 if (bdev)
998 bdev_blkbits = blksize_bits(bdev_hardsect_size(bdev));
1000 if (offset & blocksize_mask) {
1001 if (bdev)
1002 blkbits = bdev_blkbits;
1003 blocksize_mask = (1 << blkbits) - 1;
1004 if (offset & blocksize_mask)
1005 goto out;
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)) {
1013 if (bdev)
1014 blkbits = bdev_blkbits;
1015 blocksize_mask = (1 << blkbits) - 1;
1016 if ((addr & blocksize_mask) || (size & blocksize_mask))
1017 goto out;
1021 retval = direct_io_worker(rw, iocb, inode, iov, offset,
1022 nr_segs, blkbits, get_blocks, end_io);
1023 out:
1024 return retval;