2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
12 * This handles all read/write requests to block devices
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/bio.h>
18 #include <linux/blkdev.h>
19 #include <linux/highmem.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
24 #include <linux/completion.h>
25 #include <linux/slab.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/task_io_accounting_ops.h>
29 #include <linux/interrupt.h>
30 #include <linux/cpu.h>
31 #include <linux/blktrace_api.h>
32 #include <linux/fault-inject.h>
36 static int __make_request(struct request_queue *q, struct bio *bio);
39 * For the allocated request tables
41 struct kmem_cache *request_cachep;
44 * For queue allocation
46 struct kmem_cache *blk_requestq_cachep;
49 * Controlling structure to kblockd
51 static struct workqueue_struct *kblockd_workqueue;
53 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
55 static void drive_stat_acct(struct request *rq, int new_io)
57 int rw = rq_data_dir(rq);
59 if (!blk_fs_request(rq) || !rq->rq_disk)
63 __disk_stat_inc(rq->rq_disk, merges[rw]);
65 disk_round_stats(rq->rq_disk);
66 rq->rq_disk->in_flight++;
70 void blk_queue_congestion_threshold(struct request_queue *q)
74 nr = q->nr_requests - (q->nr_requests / 8) + 1;
75 if (nr > q->nr_requests)
77 q->nr_congestion_on = nr;
79 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
82 q->nr_congestion_off = nr;
86 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
89 * Locates the passed device's request queue and returns the address of its
92 * Will return NULL if the request queue cannot be located.
94 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
96 struct backing_dev_info *ret = NULL;
97 struct request_queue *q = bdev_get_queue(bdev);
100 ret = &q->backing_dev_info;
103 EXPORT_SYMBOL(blk_get_backing_dev_info);
105 void rq_init(struct request_queue *q, struct request *rq)
107 INIT_LIST_HEAD(&rq->queuelist);
108 INIT_LIST_HEAD(&rq->donelist);
111 rq->bio = rq->biotail = NULL;
112 INIT_HLIST_NODE(&rq->hash);
113 RB_CLEAR_NODE(&rq->rb_node);
121 rq->nr_phys_segments = 0;
124 rq->end_io_data = NULL;
125 rq->completion_data = NULL;
129 static void req_bio_endio(struct request *rq, struct bio *bio,
130 unsigned int nbytes, int error)
132 struct request_queue *q = rq->q;
134 if (&q->bar_rq != rq) {
136 clear_bit(BIO_UPTODATE, &bio->bi_flags);
137 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
140 if (unlikely(nbytes > bio->bi_size)) {
141 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
142 __FUNCTION__, nbytes, bio->bi_size);
143 nbytes = bio->bi_size;
146 bio->bi_size -= nbytes;
147 bio->bi_sector += (nbytes >> 9);
148 if (bio->bi_size == 0)
149 bio_endio(bio, error);
153 * Okay, this is the barrier request in progress, just
156 if (error && !q->orderr)
161 void blk_dump_rq_flags(struct request *rq, char *msg)
165 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
166 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
169 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
170 (unsigned long long)rq->sector,
172 rq->current_nr_sectors);
173 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
174 rq->bio, rq->biotail,
175 rq->buffer, rq->data,
178 if (blk_pc_request(rq)) {
179 printk(KERN_INFO " cdb: ");
180 for (bit = 0; bit < sizeof(rq->cmd); bit++)
181 printk("%02x ", rq->cmd[bit]);
185 EXPORT_SYMBOL(blk_dump_rq_flags);
188 * "plug" the device if there are no outstanding requests: this will
189 * force the transfer to start only after we have put all the requests
192 * This is called with interrupts off and no requests on the queue and
193 * with the queue lock held.
195 void blk_plug_device(struct request_queue *q)
197 WARN_ON(!irqs_disabled());
200 * don't plug a stopped queue, it must be paired with blk_start_queue()
201 * which will restart the queueing
203 if (blk_queue_stopped(q))
206 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
207 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
208 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
211 EXPORT_SYMBOL(blk_plug_device);
214 * remove the queue from the plugged list, if present. called with
215 * queue lock held and interrupts disabled.
217 int blk_remove_plug(struct request_queue *q)
219 WARN_ON(!irqs_disabled());
221 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
224 del_timer(&q->unplug_timer);
227 EXPORT_SYMBOL(blk_remove_plug);
230 * remove the plug and let it rip..
232 void __generic_unplug_device(struct request_queue *q)
234 if (unlikely(blk_queue_stopped(q)))
237 if (!blk_remove_plug(q))
242 EXPORT_SYMBOL(__generic_unplug_device);
245 * generic_unplug_device - fire a request queue
246 * @q: The &struct request_queue in question
249 * Linux uses plugging to build bigger requests queues before letting
250 * the device have at them. If a queue is plugged, the I/O scheduler
251 * is still adding and merging requests on the queue. Once the queue
252 * gets unplugged, the request_fn defined for the queue is invoked and
255 void generic_unplug_device(struct request_queue *q)
257 spin_lock_irq(q->queue_lock);
258 __generic_unplug_device(q);
259 spin_unlock_irq(q->queue_lock);
261 EXPORT_SYMBOL(generic_unplug_device);
263 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
266 struct request_queue *q = bdi->unplug_io_data;
271 void blk_unplug_work(struct work_struct *work)
273 struct request_queue *q =
274 container_of(work, struct request_queue, unplug_work);
276 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
277 q->rq.count[READ] + q->rq.count[WRITE]);
282 void blk_unplug_timeout(unsigned long data)
284 struct request_queue *q = (struct request_queue *)data;
286 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
287 q->rq.count[READ] + q->rq.count[WRITE]);
289 kblockd_schedule_work(&q->unplug_work);
292 void blk_unplug(struct request_queue *q)
295 * devices don't necessarily have an ->unplug_fn defined
298 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
299 q->rq.count[READ] + q->rq.count[WRITE]);
304 EXPORT_SYMBOL(blk_unplug);
307 * blk_start_queue - restart a previously stopped queue
308 * @q: The &struct request_queue in question
311 * blk_start_queue() will clear the stop flag on the queue, and call
312 * the request_fn for the queue if it was in a stopped state when
313 * entered. Also see blk_stop_queue(). Queue lock must be held.
315 void blk_start_queue(struct request_queue *q)
317 WARN_ON(!irqs_disabled());
319 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
322 * one level of recursion is ok and is much faster than kicking
323 * the unplug handling
325 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
327 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
330 kblockd_schedule_work(&q->unplug_work);
333 EXPORT_SYMBOL(blk_start_queue);
336 * blk_stop_queue - stop a queue
337 * @q: The &struct request_queue in question
340 * The Linux block layer assumes that a block driver will consume all
341 * entries on the request queue when the request_fn strategy is called.
342 * Often this will not happen, because of hardware limitations (queue
343 * depth settings). If a device driver gets a 'queue full' response,
344 * or if it simply chooses not to queue more I/O at one point, it can
345 * call this function to prevent the request_fn from being called until
346 * the driver has signalled it's ready to go again. This happens by calling
347 * blk_start_queue() to restart queue operations. Queue lock must be held.
349 void blk_stop_queue(struct request_queue *q)
352 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
354 EXPORT_SYMBOL(blk_stop_queue);
357 * blk_sync_queue - cancel any pending callbacks on a queue
361 * The block layer may perform asynchronous callback activity
362 * on a queue, such as calling the unplug function after a timeout.
363 * A block device may call blk_sync_queue to ensure that any
364 * such activity is cancelled, thus allowing it to release resources
365 * that the callbacks might use. The caller must already have made sure
366 * that its ->make_request_fn will not re-add plugging prior to calling
370 void blk_sync_queue(struct request_queue *q)
372 del_timer_sync(&q->unplug_timer);
373 kblockd_flush_work(&q->unplug_work);
375 EXPORT_SYMBOL(blk_sync_queue);
378 * blk_run_queue - run a single device queue
379 * @q: The queue to run
381 void blk_run_queue(struct request_queue *q)
385 spin_lock_irqsave(q->queue_lock, flags);
389 * Only recurse once to avoid overrunning the stack, let the unplug
390 * handling reinvoke the handler shortly if we already got there.
392 if (!elv_queue_empty(q)) {
393 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
395 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
398 kblockd_schedule_work(&q->unplug_work);
402 spin_unlock_irqrestore(q->queue_lock, flags);
404 EXPORT_SYMBOL(blk_run_queue);
406 void blk_put_queue(struct request_queue *q)
408 kobject_put(&q->kobj);
410 EXPORT_SYMBOL(blk_put_queue);
412 void blk_cleanup_queue(struct request_queue *q)
414 mutex_lock(&q->sysfs_lock);
415 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
416 mutex_unlock(&q->sysfs_lock);
419 elevator_exit(q->elevator);
423 EXPORT_SYMBOL(blk_cleanup_queue);
425 static int blk_init_free_list(struct request_queue *q)
427 struct request_list *rl = &q->rq;
429 rl->count[READ] = rl->count[WRITE] = 0;
430 rl->starved[READ] = rl->starved[WRITE] = 0;
432 init_waitqueue_head(&rl->wait[READ]);
433 init_waitqueue_head(&rl->wait[WRITE]);
435 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
436 mempool_free_slab, request_cachep, q->node);
444 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
446 return blk_alloc_queue_node(gfp_mask, -1);
448 EXPORT_SYMBOL(blk_alloc_queue);
450 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
452 struct request_queue *q;
455 q = kmem_cache_alloc_node(blk_requestq_cachep,
456 gfp_mask | __GFP_ZERO, node_id);
460 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
461 q->backing_dev_info.unplug_io_data = q;
462 err = bdi_init(&q->backing_dev_info);
464 kmem_cache_free(blk_requestq_cachep, q);
468 init_timer(&q->unplug_timer);
470 kobject_init(&q->kobj, &blk_queue_ktype);
472 mutex_init(&q->sysfs_lock);
476 EXPORT_SYMBOL(blk_alloc_queue_node);
479 * blk_init_queue - prepare a request queue for use with a block device
480 * @rfn: The function to be called to process requests that have been
481 * placed on the queue.
482 * @lock: Request queue spin lock
485 * If a block device wishes to use the standard request handling procedures,
486 * which sorts requests and coalesces adjacent requests, then it must
487 * call blk_init_queue(). The function @rfn will be called when there
488 * are requests on the queue that need to be processed. If the device
489 * supports plugging, then @rfn may not be called immediately when requests
490 * are available on the queue, but may be called at some time later instead.
491 * Plugged queues are generally unplugged when a buffer belonging to one
492 * of the requests on the queue is needed, or due to memory pressure.
494 * @rfn is not required, or even expected, to remove all requests off the
495 * queue, but only as many as it can handle at a time. If it does leave
496 * requests on the queue, it is responsible for arranging that the requests
497 * get dealt with eventually.
499 * The queue spin lock must be held while manipulating the requests on the
500 * request queue; this lock will be taken also from interrupt context, so irq
501 * disabling is needed for it.
503 * Function returns a pointer to the initialized request queue, or NULL if
507 * blk_init_queue() must be paired with a blk_cleanup_queue() call
508 * when the block device is deactivated (such as at module unload).
511 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
513 return blk_init_queue_node(rfn, lock, -1);
515 EXPORT_SYMBOL(blk_init_queue);
517 struct request_queue *
518 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
520 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
526 if (blk_init_free_list(q)) {
527 kmem_cache_free(blk_requestq_cachep, q);
532 * if caller didn't supply a lock, they get per-queue locking with
536 spin_lock_init(&q->__queue_lock);
537 lock = &q->__queue_lock;
541 q->prep_rq_fn = NULL;
542 q->unplug_fn = generic_unplug_device;
543 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
544 q->queue_lock = lock;
546 blk_queue_segment_boundary(q, 0xffffffff);
548 blk_queue_make_request(q, __make_request);
549 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
551 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
552 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
554 q->sg_reserved_size = INT_MAX;
559 if (!elevator_init(q, NULL)) {
560 blk_queue_congestion_threshold(q);
567 EXPORT_SYMBOL(blk_init_queue_node);
569 int blk_get_queue(struct request_queue *q)
571 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
572 kobject_get(&q->kobj);
578 EXPORT_SYMBOL(blk_get_queue);
580 static inline void blk_free_request(struct request_queue *q, struct request *rq)
582 if (rq->cmd_flags & REQ_ELVPRIV)
583 elv_put_request(q, rq);
584 mempool_free(rq, q->rq.rq_pool);
587 static struct request *
588 blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
590 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
596 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
597 * see bio.h and blkdev.h
599 rq->cmd_flags = rw | REQ_ALLOCED;
602 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
603 mempool_free(rq, q->rq.rq_pool);
606 rq->cmd_flags |= REQ_ELVPRIV;
613 * ioc_batching returns true if the ioc is a valid batching request and
614 * should be given priority access to a request.
616 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
622 * Make sure the process is able to allocate at least 1 request
623 * even if the batch times out, otherwise we could theoretically
626 return ioc->nr_batch_requests == q->nr_batching ||
627 (ioc->nr_batch_requests > 0
628 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
632 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
633 * will cause the process to be a "batcher" on all queues in the system. This
634 * is the behaviour we want though - once it gets a wakeup it should be given
637 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
639 if (!ioc || ioc_batching(q, ioc))
642 ioc->nr_batch_requests = q->nr_batching;
643 ioc->last_waited = jiffies;
646 static void __freed_request(struct request_queue *q, int rw)
648 struct request_list *rl = &q->rq;
650 if (rl->count[rw] < queue_congestion_off_threshold(q))
651 blk_clear_queue_congested(q, rw);
653 if (rl->count[rw] + 1 <= q->nr_requests) {
654 if (waitqueue_active(&rl->wait[rw]))
655 wake_up(&rl->wait[rw]);
657 blk_clear_queue_full(q, rw);
662 * A request has just been released. Account for it, update the full and
663 * congestion status, wake up any waiters. Called under q->queue_lock.
665 static void freed_request(struct request_queue *q, int rw, int priv)
667 struct request_list *rl = &q->rq;
673 __freed_request(q, rw);
675 if (unlikely(rl->starved[rw ^ 1]))
676 __freed_request(q, rw ^ 1);
679 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
681 * Get a free request, queue_lock must be held.
682 * Returns NULL on failure, with queue_lock held.
683 * Returns !NULL on success, with queue_lock *not held*.
685 static struct request *get_request(struct request_queue *q, int rw_flags,
686 struct bio *bio, gfp_t gfp_mask)
688 struct request *rq = NULL;
689 struct request_list *rl = &q->rq;
690 struct io_context *ioc = NULL;
691 const int rw = rw_flags & 0x01;
694 may_queue = elv_may_queue(q, rw_flags);
695 if (may_queue == ELV_MQUEUE_NO)
698 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
699 if (rl->count[rw]+1 >= q->nr_requests) {
700 ioc = current_io_context(GFP_ATOMIC, q->node);
702 * The queue will fill after this allocation, so set
703 * it as full, and mark this process as "batching".
704 * This process will be allowed to complete a batch of
705 * requests, others will be blocked.
707 if (!blk_queue_full(q, rw)) {
708 ioc_set_batching(q, ioc);
709 blk_set_queue_full(q, rw);
711 if (may_queue != ELV_MQUEUE_MUST
712 && !ioc_batching(q, ioc)) {
714 * The queue is full and the allocating
715 * process is not a "batcher", and not
716 * exempted by the IO scheduler
722 blk_set_queue_congested(q, rw);
726 * Only allow batching queuers to allocate up to 50% over the defined
727 * limit of requests, otherwise we could have thousands of requests
728 * allocated with any setting of ->nr_requests
730 if (rl->count[rw] >= (3 * q->nr_requests / 2))
736 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
740 spin_unlock_irq(q->queue_lock);
742 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
745 * Allocation failed presumably due to memory. Undo anything
746 * we might have messed up.
748 * Allocating task should really be put onto the front of the
749 * wait queue, but this is pretty rare.
751 spin_lock_irq(q->queue_lock);
752 freed_request(q, rw, priv);
755 * in the very unlikely event that allocation failed and no
756 * requests for this direction was pending, mark us starved
757 * so that freeing of a request in the other direction will
758 * notice us. another possible fix would be to split the
759 * rq mempool into READ and WRITE
762 if (unlikely(rl->count[rw] == 0))
769 * ioc may be NULL here, and ioc_batching will be false. That's
770 * OK, if the queue is under the request limit then requests need
771 * not count toward the nr_batch_requests limit. There will always
772 * be some limit enforced by BLK_BATCH_TIME.
774 if (ioc_batching(q, ioc))
775 ioc->nr_batch_requests--;
779 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
785 * No available requests for this queue, unplug the device and wait for some
786 * requests to become available.
788 * Called with q->queue_lock held, and returns with it unlocked.
790 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
793 const int rw = rw_flags & 0x01;
796 rq = get_request(q, rw_flags, bio, GFP_NOIO);
799 struct request_list *rl = &q->rq;
801 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
802 TASK_UNINTERRUPTIBLE);
804 rq = get_request(q, rw_flags, bio, GFP_NOIO);
807 struct io_context *ioc;
809 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
811 __generic_unplug_device(q);
812 spin_unlock_irq(q->queue_lock);
816 * After sleeping, we become a "batching" process and
817 * will be able to allocate at least one request, and
818 * up to a big batch of them for a small period time.
819 * See ioc_batching, ioc_set_batching
821 ioc = current_io_context(GFP_NOIO, q->node);
822 ioc_set_batching(q, ioc);
824 spin_lock_irq(q->queue_lock);
826 finish_wait(&rl->wait[rw], &wait);
832 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
836 BUG_ON(rw != READ && rw != WRITE);
838 spin_lock_irq(q->queue_lock);
839 if (gfp_mask & __GFP_WAIT) {
840 rq = get_request_wait(q, rw, NULL);
842 rq = get_request(q, rw, NULL, gfp_mask);
844 spin_unlock_irq(q->queue_lock);
846 /* q->queue_lock is unlocked at this point */
850 EXPORT_SYMBOL(blk_get_request);
853 * blk_start_queueing - initiate dispatch of requests to device
854 * @q: request queue to kick into gear
856 * This is basically a helper to remove the need to know whether a queue
857 * is plugged or not if someone just wants to initiate dispatch of requests
860 * The queue lock must be held with interrupts disabled.
862 void blk_start_queueing(struct request_queue *q)
864 if (!blk_queue_plugged(q))
867 __generic_unplug_device(q);
869 EXPORT_SYMBOL(blk_start_queueing);
872 * blk_requeue_request - put a request back on queue
873 * @q: request queue where request should be inserted
874 * @rq: request to be inserted
877 * Drivers often keep queueing requests until the hardware cannot accept
878 * more, when that condition happens we need to put the request back
879 * on the queue. Must be called with queue lock held.
881 void blk_requeue_request(struct request_queue *q, struct request *rq)
883 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
885 if (blk_rq_tagged(rq))
886 blk_queue_end_tag(q, rq);
888 elv_requeue_request(q, rq);
890 EXPORT_SYMBOL(blk_requeue_request);
893 * blk_insert_request - insert a special request in to a request queue
894 * @q: request queue where request should be inserted
895 * @rq: request to be inserted
896 * @at_head: insert request at head or tail of queue
897 * @data: private data
900 * Many block devices need to execute commands asynchronously, so they don't
901 * block the whole kernel from preemption during request execution. This is
902 * accomplished normally by inserting aritficial requests tagged as
903 * REQ_SPECIAL in to the corresponding request queue, and letting them be
904 * scheduled for actual execution by the request queue.
906 * We have the option of inserting the head or the tail of the queue.
907 * Typically we use the tail for new ioctls and so forth. We use the head
908 * of the queue for things like a QUEUE_FULL message from a device, or a
909 * host that is unable to accept a particular command.
911 void blk_insert_request(struct request_queue *q, struct request *rq,
912 int at_head, void *data)
914 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
918 * tell I/O scheduler that this isn't a regular read/write (ie it
919 * must not attempt merges on this) and that it acts as a soft
922 rq->cmd_type = REQ_TYPE_SPECIAL;
923 rq->cmd_flags |= REQ_SOFTBARRIER;
927 spin_lock_irqsave(q->queue_lock, flags);
930 * If command is tagged, release the tag
932 if (blk_rq_tagged(rq))
933 blk_queue_end_tag(q, rq);
935 drive_stat_acct(rq, 1);
936 __elv_add_request(q, rq, where, 0);
937 blk_start_queueing(q);
938 spin_unlock_irqrestore(q->queue_lock, flags);
940 EXPORT_SYMBOL(blk_insert_request);
943 * add-request adds a request to the linked list.
944 * queue lock is held and interrupts disabled, as we muck with the
945 * request queue list.
947 static inline void add_request(struct request_queue *q, struct request *req)
949 drive_stat_acct(req, 1);
952 * elevator indicated where it wants this request to be
953 * inserted at elevator_merge time
955 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
959 * disk_round_stats() - Round off the performance stats on a struct
962 * The average IO queue length and utilisation statistics are maintained
963 * by observing the current state of the queue length and the amount of
964 * time it has been in this state for.
966 * Normally, that accounting is done on IO completion, but that can result
967 * in more than a second's worth of IO being accounted for within any one
968 * second, leading to >100% utilisation. To deal with that, we call this
969 * function to do a round-off before returning the results when reading
970 * /proc/diskstats. This accounts immediately for all queue usage up to
971 * the current jiffies and restarts the counters again.
973 void disk_round_stats(struct gendisk *disk)
975 unsigned long now = jiffies;
977 if (now == disk->stamp)
980 if (disk->in_flight) {
981 __disk_stat_add(disk, time_in_queue,
982 disk->in_flight * (now - disk->stamp));
983 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
987 EXPORT_SYMBOL_GPL(disk_round_stats);
990 * queue lock must be held
992 void __blk_put_request(struct request_queue *q, struct request *req)
996 if (unlikely(--req->ref_count))
999 elv_completed_request(q, req);
1002 * Request may not have originated from ll_rw_blk. if not,
1003 * it didn't come out of our reserved rq pools
1005 if (req->cmd_flags & REQ_ALLOCED) {
1006 int rw = rq_data_dir(req);
1007 int priv = req->cmd_flags & REQ_ELVPRIV;
1009 BUG_ON(!list_empty(&req->queuelist));
1010 BUG_ON(!hlist_unhashed(&req->hash));
1012 blk_free_request(q, req);
1013 freed_request(q, rw, priv);
1016 EXPORT_SYMBOL_GPL(__blk_put_request);
1018 void blk_put_request(struct request *req)
1020 unsigned long flags;
1021 struct request_queue *q = req->q;
1024 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1025 * following if (q) test.
1028 spin_lock_irqsave(q->queue_lock, flags);
1029 __blk_put_request(q, req);
1030 spin_unlock_irqrestore(q->queue_lock, flags);
1033 EXPORT_SYMBOL(blk_put_request);
1035 void init_request_from_bio(struct request *req, struct bio *bio)
1037 req->cmd_type = REQ_TYPE_FS;
1040 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1042 if (bio_rw_ahead(bio) || bio_failfast(bio))
1043 req->cmd_flags |= REQ_FAILFAST;
1046 * REQ_BARRIER implies no merging, but lets make it explicit
1048 if (unlikely(bio_barrier(bio)))
1049 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
1052 req->cmd_flags |= REQ_RW_SYNC;
1053 if (bio_rw_meta(bio))
1054 req->cmd_flags |= REQ_RW_META;
1057 req->hard_sector = req->sector = bio->bi_sector;
1058 req->ioprio = bio_prio(bio);
1059 req->start_time = jiffies;
1060 blk_rq_bio_prep(req->q, req, bio);
1063 static int __make_request(struct request_queue *q, struct bio *bio)
1065 struct request *req;
1066 int el_ret, nr_sectors, barrier, err;
1067 const unsigned short prio = bio_prio(bio);
1068 const int sync = bio_sync(bio);
1071 nr_sectors = bio_sectors(bio);
1074 * low level driver can indicate that it wants pages above a
1075 * certain limit bounced to low memory (ie for highmem, or even
1076 * ISA dma in theory)
1078 blk_queue_bounce(q, &bio);
1080 barrier = bio_barrier(bio);
1081 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
1086 spin_lock_irq(q->queue_lock);
1088 if (unlikely(barrier) || elv_queue_empty(q))
1091 el_ret = elv_merge(q, &req, bio);
1093 case ELEVATOR_BACK_MERGE:
1094 BUG_ON(!rq_mergeable(req));
1096 if (!ll_back_merge_fn(q, req, bio))
1099 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
1101 req->biotail->bi_next = bio;
1103 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1104 req->ioprio = ioprio_best(req->ioprio, prio);
1105 drive_stat_acct(req, 0);
1106 if (!attempt_back_merge(q, req))
1107 elv_merged_request(q, req, el_ret);
1110 case ELEVATOR_FRONT_MERGE:
1111 BUG_ON(!rq_mergeable(req));
1113 if (!ll_front_merge_fn(q, req, bio))
1116 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
1118 bio->bi_next = req->bio;
1122 * may not be valid. if the low level driver said
1123 * it didn't need a bounce buffer then it better
1124 * not touch req->buffer either...
1126 req->buffer = bio_data(bio);
1127 req->current_nr_sectors = bio_cur_sectors(bio);
1128 req->hard_cur_sectors = req->current_nr_sectors;
1129 req->sector = req->hard_sector = bio->bi_sector;
1130 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1131 req->ioprio = ioprio_best(req->ioprio, prio);
1132 drive_stat_acct(req, 0);
1133 if (!attempt_front_merge(q, req))
1134 elv_merged_request(q, req, el_ret);
1137 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1144 * This sync check and mask will be re-done in init_request_from_bio(),
1145 * but we need to set it earlier to expose the sync flag to the
1146 * rq allocator and io schedulers.
1148 rw_flags = bio_data_dir(bio);
1150 rw_flags |= REQ_RW_SYNC;
1153 * Grab a free request. This is might sleep but can not fail.
1154 * Returns with the queue unlocked.
1156 req = get_request_wait(q, rw_flags, bio);
1159 * After dropping the lock and possibly sleeping here, our request
1160 * may now be mergeable after it had proven unmergeable (above).
1161 * We don't worry about that case for efficiency. It won't happen
1162 * often, and the elevators are able to handle it.
1164 init_request_from_bio(req, bio);
1166 spin_lock_irq(q->queue_lock);
1167 if (elv_queue_empty(q))
1169 add_request(q, req);
1172 __generic_unplug_device(q);
1174 spin_unlock_irq(q->queue_lock);
1178 bio_endio(bio, err);
1183 * If bio->bi_dev is a partition, remap the location
1185 static inline void blk_partition_remap(struct bio *bio)
1187 struct block_device *bdev = bio->bi_bdev;
1189 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1190 struct hd_struct *p = bdev->bd_part;
1191 const int rw = bio_data_dir(bio);
1193 p->sectors[rw] += bio_sectors(bio);
1196 bio->bi_sector += p->start_sect;
1197 bio->bi_bdev = bdev->bd_contains;
1199 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1200 bdev->bd_dev, bio->bi_sector,
1201 bio->bi_sector - p->start_sect);
1205 static void handle_bad_sector(struct bio *bio)
1207 char b[BDEVNAME_SIZE];
1209 printk(KERN_INFO "attempt to access beyond end of device\n");
1210 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1211 bdevname(bio->bi_bdev, b),
1213 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1214 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1216 set_bit(BIO_EOF, &bio->bi_flags);
1219 #ifdef CONFIG_FAIL_MAKE_REQUEST
1221 static DECLARE_FAULT_ATTR(fail_make_request);
1223 static int __init setup_fail_make_request(char *str)
1225 return setup_fault_attr(&fail_make_request, str);
1227 __setup("fail_make_request=", setup_fail_make_request);
1229 static int should_fail_request(struct bio *bio)
1231 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1232 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1233 return should_fail(&fail_make_request, bio->bi_size);
1238 static int __init fail_make_request_debugfs(void)
1240 return init_fault_attr_dentries(&fail_make_request,
1241 "fail_make_request");
1244 late_initcall(fail_make_request_debugfs);
1246 #else /* CONFIG_FAIL_MAKE_REQUEST */
1248 static inline int should_fail_request(struct bio *bio)
1253 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1256 * Check whether this bio extends beyond the end of the device.
1258 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1265 /* Test device or partition size, when known. */
1266 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1268 sector_t sector = bio->bi_sector;
1270 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1272 * This may well happen - the kernel calls bread()
1273 * without checking the size of the device, e.g., when
1274 * mounting a device.
1276 handle_bad_sector(bio);
1285 * generic_make_request: hand a buffer to its device driver for I/O
1286 * @bio: The bio describing the location in memory and on the device.
1288 * generic_make_request() is used to make I/O requests of block
1289 * devices. It is passed a &struct bio, which describes the I/O that needs
1292 * generic_make_request() does not return any status. The
1293 * success/failure status of the request, along with notification of
1294 * completion, is delivered asynchronously through the bio->bi_end_io
1295 * function described (one day) else where.
1297 * The caller of generic_make_request must make sure that bi_io_vec
1298 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1299 * set to describe the device address, and the
1300 * bi_end_io and optionally bi_private are set to describe how
1301 * completion notification should be signaled.
1303 * generic_make_request and the drivers it calls may use bi_next if this
1304 * bio happens to be merged with someone else, and may change bi_dev and
1305 * bi_sector for remaps as it sees fit. So the values of these fields
1306 * should NOT be depended on after the call to generic_make_request.
1308 static inline void __generic_make_request(struct bio *bio)
1310 struct request_queue *q;
1311 sector_t old_sector;
1312 int ret, nr_sectors = bio_sectors(bio);
1318 if (bio_check_eod(bio, nr_sectors))
1322 * Resolve the mapping until finished. (drivers are
1323 * still free to implement/resolve their own stacking
1324 * by explicitly returning 0)
1326 * NOTE: we don't repeat the blk_size check for each new device.
1327 * Stacking drivers are expected to know what they are doing.
1332 char b[BDEVNAME_SIZE];
1334 q = bdev_get_queue(bio->bi_bdev);
1337 "generic_make_request: Trying to access "
1338 "nonexistent block-device %s (%Lu)\n",
1339 bdevname(bio->bi_bdev, b),
1340 (long long) bio->bi_sector);
1342 bio_endio(bio, err);
1346 if (unlikely(nr_sectors > q->max_hw_sectors)) {
1347 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1348 bdevname(bio->bi_bdev, b),
1354 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1357 if (should_fail_request(bio))
1361 * If this device has partitions, remap block n
1362 * of partition p to block n+start(p) of the disk.
1364 blk_partition_remap(bio);
1366 if (old_sector != -1)
1367 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
1370 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1372 old_sector = bio->bi_sector;
1373 old_dev = bio->bi_bdev->bd_dev;
1375 if (bio_check_eod(bio, nr_sectors))
1377 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1382 ret = q->make_request_fn(q, bio);
1387 * We only want one ->make_request_fn to be active at a time,
1388 * else stack usage with stacked devices could be a problem.
1389 * So use current->bio_{list,tail} to keep a list of requests
1390 * submited by a make_request_fn function.
1391 * current->bio_tail is also used as a flag to say if
1392 * generic_make_request is currently active in this task or not.
1393 * If it is NULL, then no make_request is active. If it is non-NULL,
1394 * then a make_request is active, and new requests should be added
1397 void generic_make_request(struct bio *bio)
1399 if (current->bio_tail) {
1400 /* make_request is active */
1401 *(current->bio_tail) = bio;
1402 bio->bi_next = NULL;
1403 current->bio_tail = &bio->bi_next;
1406 /* following loop may be a bit non-obvious, and so deserves some
1408 * Before entering the loop, bio->bi_next is NULL (as all callers
1409 * ensure that) so we have a list with a single bio.
1410 * We pretend that we have just taken it off a longer list, so
1411 * we assign bio_list to the next (which is NULL) and bio_tail
1412 * to &bio_list, thus initialising the bio_list of new bios to be
1413 * added. __generic_make_request may indeed add some more bios
1414 * through a recursive call to generic_make_request. If it
1415 * did, we find a non-NULL value in bio_list and re-enter the loop
1416 * from the top. In this case we really did just take the bio
1417 * of the top of the list (no pretending) and so fixup bio_list and
1418 * bio_tail or bi_next, and call into __generic_make_request again.
1420 * The loop was structured like this to make only one call to
1421 * __generic_make_request (which is important as it is large and
1422 * inlined) and to keep the structure simple.
1424 BUG_ON(bio->bi_next);
1426 current->bio_list = bio->bi_next;
1427 if (bio->bi_next == NULL)
1428 current->bio_tail = ¤t->bio_list;
1430 bio->bi_next = NULL;
1431 __generic_make_request(bio);
1432 bio = current->bio_list;
1434 current->bio_tail = NULL; /* deactivate */
1436 EXPORT_SYMBOL(generic_make_request);
1439 * submit_bio: submit a bio to the block device layer for I/O
1440 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1441 * @bio: The &struct bio which describes the I/O
1443 * submit_bio() is very similar in purpose to generic_make_request(), and
1444 * uses that function to do most of the work. Both are fairly rough
1445 * interfaces, @bio must be presetup and ready for I/O.
1448 void submit_bio(int rw, struct bio *bio)
1450 int count = bio_sectors(bio);
1455 * If it's a regular read/write or a barrier with data attached,
1456 * go through the normal accounting stuff before submission.
1458 if (!bio_empty_barrier(bio)) {
1460 BIO_BUG_ON(!bio->bi_size);
1461 BIO_BUG_ON(!bio->bi_io_vec);
1464 count_vm_events(PGPGOUT, count);
1466 task_io_account_read(bio->bi_size);
1467 count_vm_events(PGPGIN, count);
1470 if (unlikely(block_dump)) {
1471 char b[BDEVNAME_SIZE];
1472 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
1473 current->comm, task_pid_nr(current),
1474 (rw & WRITE) ? "WRITE" : "READ",
1475 (unsigned long long)bio->bi_sector,
1476 bdevname(bio->bi_bdev, b));
1480 generic_make_request(bio);
1482 EXPORT_SYMBOL(submit_bio);
1485 * __end_that_request_first - end I/O on a request
1486 * @req: the request being processed
1487 * @error: 0 for success, < 0 for error
1488 * @nr_bytes: number of bytes to complete
1491 * Ends I/O on a number of bytes attached to @req, and sets it up
1492 * for the next range of segments (if any) in the cluster.
1495 * 0 - we are done with this request, call end_that_request_last()
1496 * 1 - still buffers pending for this request
1498 static int __end_that_request_first(struct request *req, int error,
1501 int total_bytes, bio_nbytes, next_idx = 0;
1504 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1507 * for a REQ_BLOCK_PC request, we want to carry any eventual
1508 * sense key with us all the way through
1510 if (!blk_pc_request(req))
1513 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1514 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1515 req->rq_disk ? req->rq_disk->disk_name : "?",
1516 (unsigned long long)req->sector);
1519 if (blk_fs_request(req) && req->rq_disk) {
1520 const int rw = rq_data_dir(req);
1522 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
1525 total_bytes = bio_nbytes = 0;
1526 while ((bio = req->bio) != NULL) {
1530 * For an empty barrier request, the low level driver must
1531 * store a potential error location in ->sector. We pass
1532 * that back up in ->bi_sector.
1534 if (blk_empty_barrier(req))
1535 bio->bi_sector = req->sector;
1537 if (nr_bytes >= bio->bi_size) {
1538 req->bio = bio->bi_next;
1539 nbytes = bio->bi_size;
1540 req_bio_endio(req, bio, nbytes, error);
1544 int idx = bio->bi_idx + next_idx;
1546 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1547 blk_dump_rq_flags(req, "__end_that");
1548 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1549 __FUNCTION__, bio->bi_idx,
1554 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1555 BIO_BUG_ON(nbytes > bio->bi_size);
1558 * not a complete bvec done
1560 if (unlikely(nbytes > nr_bytes)) {
1561 bio_nbytes += nr_bytes;
1562 total_bytes += nr_bytes;
1567 * advance to the next vector
1570 bio_nbytes += nbytes;
1573 total_bytes += nbytes;
1579 * end more in this run, or just return 'not-done'
1581 if (unlikely(nr_bytes <= 0))
1593 * if the request wasn't completed, update state
1596 req_bio_endio(req, bio, bio_nbytes, error);
1597 bio->bi_idx += next_idx;
1598 bio_iovec(bio)->bv_offset += nr_bytes;
1599 bio_iovec(bio)->bv_len -= nr_bytes;
1602 blk_recalc_rq_sectors(req, total_bytes >> 9);
1603 blk_recalc_rq_segments(req);
1608 * splice the completion data to a local structure and hand off to
1609 * process_completion_queue() to complete the requests
1611 static void blk_done_softirq(struct softirq_action *h)
1613 struct list_head *cpu_list, local_list;
1615 local_irq_disable();
1616 cpu_list = &__get_cpu_var(blk_cpu_done);
1617 list_replace_init(cpu_list, &local_list);
1620 while (!list_empty(&local_list)) {
1623 rq = list_entry(local_list.next, struct request, donelist);
1624 list_del_init(&rq->donelist);
1625 rq->q->softirq_done_fn(rq);
1629 static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1630 unsigned long action, void *hcpu)
1633 * If a CPU goes away, splice its entries to the current CPU
1634 * and trigger a run of the softirq
1636 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1637 int cpu = (unsigned long) hcpu;
1639 local_irq_disable();
1640 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1641 &__get_cpu_var(blk_cpu_done));
1642 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1650 static struct notifier_block blk_cpu_notifier __cpuinitdata = {
1651 .notifier_call = blk_cpu_notify,
1655 * blk_complete_request - end I/O on a request
1656 * @req: the request being processed
1659 * Ends all I/O on a request. It does not handle partial completions,
1660 * unless the driver actually implements this in its completion callback
1661 * through requeueing. The actual completion happens out-of-order,
1662 * through a softirq handler. The user must have registered a completion
1663 * callback through blk_queue_softirq_done().
1666 void blk_complete_request(struct request *req)
1668 struct list_head *cpu_list;
1669 unsigned long flags;
1671 BUG_ON(!req->q->softirq_done_fn);
1673 local_irq_save(flags);
1675 cpu_list = &__get_cpu_var(blk_cpu_done);
1676 list_add_tail(&req->donelist, cpu_list);
1677 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1679 local_irq_restore(flags);
1681 EXPORT_SYMBOL(blk_complete_request);
1684 * queue lock must be held
1686 static void end_that_request_last(struct request *req, int error)
1688 struct gendisk *disk = req->rq_disk;
1690 if (blk_rq_tagged(req))
1691 blk_queue_end_tag(req->q, req);
1693 if (blk_queued_rq(req))
1694 blkdev_dequeue_request(req);
1696 if (unlikely(laptop_mode) && blk_fs_request(req))
1697 laptop_io_completion();
1700 * Account IO completion. bar_rq isn't accounted as a normal
1701 * IO on queueing nor completion. Accounting the containing
1702 * request is enough.
1704 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
1705 unsigned long duration = jiffies - req->start_time;
1706 const int rw = rq_data_dir(req);
1708 __disk_stat_inc(disk, ios[rw]);
1709 __disk_stat_add(disk, ticks[rw], duration);
1710 disk_round_stats(disk);
1715 req->end_io(req, error);
1717 if (blk_bidi_rq(req))
1718 __blk_put_request(req->next_rq->q, req->next_rq);
1720 __blk_put_request(req->q, req);
1724 static inline void __end_request(struct request *rq, int uptodate,
1725 unsigned int nr_bytes)
1730 error = uptodate ? uptodate : -EIO;
1732 __blk_end_request(rq, error, nr_bytes);
1736 * blk_rq_bytes - Returns bytes left to complete in the entire request
1738 unsigned int blk_rq_bytes(struct request *rq)
1740 if (blk_fs_request(rq))
1741 return rq->hard_nr_sectors << 9;
1743 return rq->data_len;
1745 EXPORT_SYMBOL_GPL(blk_rq_bytes);
1748 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1750 unsigned int blk_rq_cur_bytes(struct request *rq)
1752 if (blk_fs_request(rq))
1753 return rq->current_nr_sectors << 9;
1756 return rq->bio->bi_size;
1758 return rq->data_len;
1760 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
1763 * end_queued_request - end all I/O on a queued request
1764 * @rq: the request being processed
1765 * @uptodate: error value or 0/1 uptodate flag
1768 * Ends all I/O on a request, and removes it from the block layer queues.
1769 * Not suitable for normal IO completion, unless the driver still has
1770 * the request attached to the block layer.
1773 void end_queued_request(struct request *rq, int uptodate)
1775 __end_request(rq, uptodate, blk_rq_bytes(rq));
1777 EXPORT_SYMBOL(end_queued_request);
1780 * end_dequeued_request - end all I/O on a dequeued request
1781 * @rq: the request being processed
1782 * @uptodate: error value or 0/1 uptodate flag
1785 * Ends all I/O on a request. The request must already have been
1786 * dequeued using blkdev_dequeue_request(), as is normally the case
1790 void end_dequeued_request(struct request *rq, int uptodate)
1792 __end_request(rq, uptodate, blk_rq_bytes(rq));
1794 EXPORT_SYMBOL(end_dequeued_request);
1798 * end_request - end I/O on the current segment of the request
1799 * @req: the request being processed
1800 * @uptodate: error value or 0/1 uptodate flag
1803 * Ends I/O on the current segment of a request. If that is the only
1804 * remaining segment, the request is also completed and freed.
1806 * This is a remnant of how older block drivers handled IO completions.
1807 * Modern drivers typically end IO on the full request in one go, unless
1808 * they have a residual value to account for. For that case this function
1809 * isn't really useful, unless the residual just happens to be the
1810 * full current segment. In other words, don't use this function in new
1811 * code. Either use end_request_completely(), or the
1812 * end_that_request_chunk() (along with end_that_request_last()) for
1813 * partial completions.
1816 void end_request(struct request *req, int uptodate)
1818 __end_request(req, uptodate, req->hard_cur_sectors << 9);
1820 EXPORT_SYMBOL(end_request);
1823 * blk_end_io - Generic end_io function to complete a request.
1824 * @rq: the request being processed
1825 * @error: 0 for success, < 0 for error
1826 * @nr_bytes: number of bytes to complete @rq
1827 * @bidi_bytes: number of bytes to complete @rq->next_rq
1828 * @drv_callback: function called between completion of bios in the request
1829 * and completion of the request.
1830 * If the callback returns non 0, this helper returns without
1831 * completion of the request.
1834 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1835 * If @rq has leftover, sets it up for the next range of segments.
1838 * 0 - we are done with this request
1839 * 1 - this request is not freed yet, it still has pending buffers.
1841 static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1842 unsigned int bidi_bytes,
1843 int (drv_callback)(struct request *))
1845 struct request_queue *q = rq->q;
1846 unsigned long flags = 0UL;
1848 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1849 if (__end_that_request_first(rq, error, nr_bytes))
1852 /* Bidi request must be completed as a whole */
1853 if (blk_bidi_rq(rq) &&
1854 __end_that_request_first(rq->next_rq, error, bidi_bytes))
1858 /* Special feature for tricky drivers */
1859 if (drv_callback && drv_callback(rq))
1862 add_disk_randomness(rq->rq_disk);
1864 spin_lock_irqsave(q->queue_lock, flags);
1865 end_that_request_last(rq, error);
1866 spin_unlock_irqrestore(q->queue_lock, flags);
1872 * blk_end_request - Helper function for drivers to complete the request.
1873 * @rq: the request being processed
1874 * @error: 0 for success, < 0 for error
1875 * @nr_bytes: number of bytes to complete
1878 * Ends I/O on a number of bytes attached to @rq.
1879 * If @rq has leftover, sets it up for the next range of segments.
1882 * 0 - we are done with this request
1883 * 1 - still buffers pending for this request
1885 int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1887 return blk_end_io(rq, error, nr_bytes, 0, NULL);
1889 EXPORT_SYMBOL_GPL(blk_end_request);
1892 * __blk_end_request - Helper function for drivers to complete the request.
1893 * @rq: the request being processed
1894 * @error: 0 for success, < 0 for error
1895 * @nr_bytes: number of bytes to complete
1898 * Must be called with queue lock held unlike blk_end_request().
1901 * 0 - we are done with this request
1902 * 1 - still buffers pending for this request
1904 int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1906 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1907 if (__end_that_request_first(rq, error, nr_bytes))
1911 add_disk_randomness(rq->rq_disk);
1913 end_that_request_last(rq, error);
1917 EXPORT_SYMBOL_GPL(__blk_end_request);
1920 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1921 * @rq: the bidi request being processed
1922 * @error: 0 for success, < 0 for error
1923 * @nr_bytes: number of bytes to complete @rq
1924 * @bidi_bytes: number of bytes to complete @rq->next_rq
1927 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1930 * 0 - we are done with this request
1931 * 1 - still buffers pending for this request
1933 int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1934 unsigned int bidi_bytes)
1936 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1938 EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1941 * blk_end_request_callback - Special helper function for tricky drivers
1942 * @rq: the request being processed
1943 * @error: 0 for success, < 0 for error
1944 * @nr_bytes: number of bytes to complete
1945 * @drv_callback: function called between completion of bios in the request
1946 * and completion of the request.
1947 * If the callback returns non 0, this helper returns without
1948 * completion of the request.
1951 * Ends I/O on a number of bytes attached to @rq.
1952 * If @rq has leftover, sets it up for the next range of segments.
1954 * This special helper function is used only for existing tricky drivers.
1955 * (e.g. cdrom_newpc_intr() of ide-cd)
1956 * This interface will be removed when such drivers are rewritten.
1957 * Don't use this interface in other places anymore.
1960 * 0 - we are done with this request
1961 * 1 - this request is not freed yet.
1962 * this request still has pending buffers or
1963 * the driver doesn't want to finish this request yet.
1965 int blk_end_request_callback(struct request *rq, int error,
1966 unsigned int nr_bytes,
1967 int (drv_callback)(struct request *))
1969 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
1971 EXPORT_SYMBOL_GPL(blk_end_request_callback);
1973 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1976 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
1977 rq->cmd_flags |= (bio->bi_rw & 3);
1979 rq->nr_phys_segments = bio_phys_segments(q, bio);
1980 rq->nr_hw_segments = bio_hw_segments(q, bio);
1981 rq->current_nr_sectors = bio_cur_sectors(bio);
1982 rq->hard_cur_sectors = rq->current_nr_sectors;
1983 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
1984 rq->buffer = bio_data(bio);
1985 rq->data_len = bio->bi_size;
1987 rq->bio = rq->biotail = bio;
1990 rq->rq_disk = bio->bi_bdev->bd_disk;
1993 int kblockd_schedule_work(struct work_struct *work)
1995 return queue_work(kblockd_workqueue, work);
1997 EXPORT_SYMBOL(kblockd_schedule_work);
1999 void kblockd_flush_work(struct work_struct *work)
2001 cancel_work_sync(work);
2003 EXPORT_SYMBOL(kblockd_flush_work);
2005 int __init blk_dev_init(void)
2009 kblockd_workqueue = create_workqueue("kblockd");
2010 if (!kblockd_workqueue)
2011 panic("Failed to create kblockd\n");
2013 request_cachep = kmem_cache_create("blkdev_requests",
2014 sizeof(struct request), 0, SLAB_PANIC, NULL);
2016 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2017 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
2019 for_each_possible_cpu(i)
2020 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2022 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
2023 register_hotcpu_notifier(&blk_cpu_notifier);