Linux 2.6.35-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / blk-core.c
blobf84cce42fc58da2dc0bc48186cbd9f6bd1d7dfc3
1 /*
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>
7 * - July2000
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
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>
20 #include <linux/mm.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/fault-inject.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/block.h>
34 #include "blk.h"
36 EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
37 EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
38 EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
40 static int __make_request(struct request_queue *q, struct bio *bio);
43 * For the allocated request tables
45 static struct kmem_cache *request_cachep;
48 * For queue allocation
50 struct kmem_cache *blk_requestq_cachep;
53 * Controlling structure to kblockd
55 static struct workqueue_struct *kblockd_workqueue;
57 static void drive_stat_acct(struct request *rq, int new_io)
59 struct hd_struct *part;
60 int rw = rq_data_dir(rq);
61 int cpu;
63 if (!blk_do_io_stat(rq))
64 return;
66 cpu = part_stat_lock();
67 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
69 if (!new_io)
70 part_stat_inc(cpu, part, merges[rw]);
71 else {
72 part_round_stats(cpu, part);
73 part_inc_in_flight(part, rw);
76 part_stat_unlock();
79 void blk_queue_congestion_threshold(struct request_queue *q)
81 int nr;
83 nr = q->nr_requests - (q->nr_requests / 8) + 1;
84 if (nr > q->nr_requests)
85 nr = q->nr_requests;
86 q->nr_congestion_on = nr;
88 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
89 if (nr < 1)
90 nr = 1;
91 q->nr_congestion_off = nr;
94 /**
95 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
96 * @bdev: device
98 * Locates the passed device's request queue and returns the address of its
99 * backing_dev_info
101 * Will return NULL if the request queue cannot be located.
103 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
105 struct backing_dev_info *ret = NULL;
106 struct request_queue *q = bdev_get_queue(bdev);
108 if (q)
109 ret = &q->backing_dev_info;
110 return ret;
112 EXPORT_SYMBOL(blk_get_backing_dev_info);
114 void blk_rq_init(struct request_queue *q, struct request *rq)
116 memset(rq, 0, sizeof(*rq));
118 INIT_LIST_HEAD(&rq->queuelist);
119 INIT_LIST_HEAD(&rq->timeout_list);
120 rq->cpu = -1;
121 rq->q = q;
122 rq->__sector = (sector_t) -1;
123 INIT_HLIST_NODE(&rq->hash);
124 RB_CLEAR_NODE(&rq->rb_node);
125 rq->cmd = rq->__cmd;
126 rq->cmd_len = BLK_MAX_CDB;
127 rq->tag = -1;
128 rq->ref_count = 1;
129 rq->start_time = jiffies;
130 set_start_time_ns(rq);
132 EXPORT_SYMBOL(blk_rq_init);
134 static void req_bio_endio(struct request *rq, struct bio *bio,
135 unsigned int nbytes, int error)
137 struct request_queue *q = rq->q;
139 if (&q->bar_rq != rq) {
140 if (error)
141 clear_bit(BIO_UPTODATE, &bio->bi_flags);
142 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
143 error = -EIO;
145 if (unlikely(nbytes > bio->bi_size)) {
146 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
147 __func__, nbytes, bio->bi_size);
148 nbytes = bio->bi_size;
151 if (unlikely(rq->cmd_flags & REQ_QUIET))
152 set_bit(BIO_QUIET, &bio->bi_flags);
154 bio->bi_size -= nbytes;
155 bio->bi_sector += (nbytes >> 9);
157 if (bio_integrity(bio))
158 bio_integrity_advance(bio, nbytes);
160 if (bio->bi_size == 0)
161 bio_endio(bio, error);
162 } else {
165 * Okay, this is the barrier request in progress, just
166 * record the error;
168 if (error && !q->orderr)
169 q->orderr = error;
173 void blk_dump_rq_flags(struct request *rq, char *msg)
175 int bit;
177 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
178 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
179 rq->cmd_flags);
181 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
182 (unsigned long long)blk_rq_pos(rq),
183 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
184 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
185 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
187 if (blk_pc_request(rq)) {
188 printk(KERN_INFO " cdb: ");
189 for (bit = 0; bit < BLK_MAX_CDB; bit++)
190 printk("%02x ", rq->cmd[bit]);
191 printk("\n");
194 EXPORT_SYMBOL(blk_dump_rq_flags);
197 * "plug" the device if there are no outstanding requests: this will
198 * force the transfer to start only after we have put all the requests
199 * on the list.
201 * This is called with interrupts off and no requests on the queue and
202 * with the queue lock held.
204 void blk_plug_device(struct request_queue *q)
206 WARN_ON(!irqs_disabled());
209 * don't plug a stopped queue, it must be paired with blk_start_queue()
210 * which will restart the queueing
212 if (blk_queue_stopped(q))
213 return;
215 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
216 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
217 trace_block_plug(q);
220 EXPORT_SYMBOL(blk_plug_device);
223 * blk_plug_device_unlocked - plug a device without queue lock held
224 * @q: The &struct request_queue to plug
226 * Description:
227 * Like @blk_plug_device(), but grabs the queue lock and disables
228 * interrupts.
230 void blk_plug_device_unlocked(struct request_queue *q)
232 unsigned long flags;
234 spin_lock_irqsave(q->queue_lock, flags);
235 blk_plug_device(q);
236 spin_unlock_irqrestore(q->queue_lock, flags);
238 EXPORT_SYMBOL(blk_plug_device_unlocked);
241 * remove the queue from the plugged list, if present. called with
242 * queue lock held and interrupts disabled.
244 int blk_remove_plug(struct request_queue *q)
246 WARN_ON(!irqs_disabled());
248 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
249 return 0;
251 del_timer(&q->unplug_timer);
252 return 1;
254 EXPORT_SYMBOL(blk_remove_plug);
257 * remove the plug and let it rip..
259 void __generic_unplug_device(struct request_queue *q)
261 if (unlikely(blk_queue_stopped(q)))
262 return;
263 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
264 return;
266 q->request_fn(q);
270 * generic_unplug_device - fire a request queue
271 * @q: The &struct request_queue in question
273 * Description:
274 * Linux uses plugging to build bigger requests queues before letting
275 * the device have at them. If a queue is plugged, the I/O scheduler
276 * is still adding and merging requests on the queue. Once the queue
277 * gets unplugged, the request_fn defined for the queue is invoked and
278 * transfers started.
280 void generic_unplug_device(struct request_queue *q)
282 if (blk_queue_plugged(q)) {
283 spin_lock_irq(q->queue_lock);
284 __generic_unplug_device(q);
285 spin_unlock_irq(q->queue_lock);
288 EXPORT_SYMBOL(generic_unplug_device);
290 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
291 struct page *page)
293 struct request_queue *q = bdi->unplug_io_data;
295 blk_unplug(q);
298 void blk_unplug_work(struct work_struct *work)
300 struct request_queue *q =
301 container_of(work, struct request_queue, unplug_work);
303 trace_block_unplug_io(q);
304 q->unplug_fn(q);
307 void blk_unplug_timeout(unsigned long data)
309 struct request_queue *q = (struct request_queue *)data;
311 trace_block_unplug_timer(q);
312 kblockd_schedule_work(q, &q->unplug_work);
315 void blk_unplug(struct request_queue *q)
318 * devices don't necessarily have an ->unplug_fn defined
320 if (q->unplug_fn) {
321 trace_block_unplug_io(q);
322 q->unplug_fn(q);
325 EXPORT_SYMBOL(blk_unplug);
328 * blk_start_queue - restart a previously stopped queue
329 * @q: The &struct request_queue in question
331 * Description:
332 * blk_start_queue() will clear the stop flag on the queue, and call
333 * the request_fn for the queue if it was in a stopped state when
334 * entered. Also see blk_stop_queue(). Queue lock must be held.
336 void blk_start_queue(struct request_queue *q)
338 WARN_ON(!irqs_disabled());
340 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
341 __blk_run_queue(q);
343 EXPORT_SYMBOL(blk_start_queue);
346 * blk_stop_queue - stop a queue
347 * @q: The &struct request_queue in question
349 * Description:
350 * The Linux block layer assumes that a block driver will consume all
351 * entries on the request queue when the request_fn strategy is called.
352 * Often this will not happen, because of hardware limitations (queue
353 * depth settings). If a device driver gets a 'queue full' response,
354 * or if it simply chooses not to queue more I/O at one point, it can
355 * call this function to prevent the request_fn from being called until
356 * the driver has signalled it's ready to go again. This happens by calling
357 * blk_start_queue() to restart queue operations. Queue lock must be held.
359 void blk_stop_queue(struct request_queue *q)
361 blk_remove_plug(q);
362 queue_flag_set(QUEUE_FLAG_STOPPED, q);
364 EXPORT_SYMBOL(blk_stop_queue);
367 * blk_sync_queue - cancel any pending callbacks on a queue
368 * @q: the queue
370 * Description:
371 * The block layer may perform asynchronous callback activity
372 * on a queue, such as calling the unplug function after a timeout.
373 * A block device may call blk_sync_queue to ensure that any
374 * such activity is cancelled, thus allowing it to release resources
375 * that the callbacks might use. The caller must already have made sure
376 * that its ->make_request_fn will not re-add plugging prior to calling
377 * this function.
380 void blk_sync_queue(struct request_queue *q)
382 del_timer_sync(&q->unplug_timer);
383 del_timer_sync(&q->timeout);
384 cancel_work_sync(&q->unplug_work);
386 EXPORT_SYMBOL(blk_sync_queue);
389 * __blk_run_queue - run a single device queue
390 * @q: The queue to run
392 * Description:
393 * See @blk_run_queue. This variant must be called with the queue lock
394 * held and interrupts disabled.
397 void __blk_run_queue(struct request_queue *q)
399 blk_remove_plug(q);
401 if (unlikely(blk_queue_stopped(q)))
402 return;
404 if (elv_queue_empty(q))
405 return;
408 * Only recurse once to avoid overrunning the stack, let the unplug
409 * handling reinvoke the handler shortly if we already got there.
411 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
412 q->request_fn(q);
413 queue_flag_clear(QUEUE_FLAG_REENTER, q);
414 } else {
415 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
416 kblockd_schedule_work(q, &q->unplug_work);
419 EXPORT_SYMBOL(__blk_run_queue);
422 * blk_run_queue - run a single device queue
423 * @q: The queue to run
425 * Description:
426 * Invoke request handling on this queue, if it has pending work to do.
427 * May be used to restart queueing when a request has completed.
429 void blk_run_queue(struct request_queue *q)
431 unsigned long flags;
433 spin_lock_irqsave(q->queue_lock, flags);
434 __blk_run_queue(q);
435 spin_unlock_irqrestore(q->queue_lock, flags);
437 EXPORT_SYMBOL(blk_run_queue);
439 void blk_put_queue(struct request_queue *q)
441 kobject_put(&q->kobj);
444 void blk_cleanup_queue(struct request_queue *q)
447 * We know we have process context here, so we can be a little
448 * cautious and ensure that pending block actions on this device
449 * are done before moving on. Going into this function, we should
450 * not have processes doing IO to this device.
452 blk_sync_queue(q);
454 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
455 mutex_lock(&q->sysfs_lock);
456 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
457 mutex_unlock(&q->sysfs_lock);
459 if (q->elevator)
460 elevator_exit(q->elevator);
462 blk_put_queue(q);
464 EXPORT_SYMBOL(blk_cleanup_queue);
466 static int blk_init_free_list(struct request_queue *q)
468 struct request_list *rl = &q->rq;
470 if (unlikely(rl->rq_pool))
471 return 0;
473 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
474 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
475 rl->elvpriv = 0;
476 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
477 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
479 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
480 mempool_free_slab, request_cachep, q->node);
482 if (!rl->rq_pool)
483 return -ENOMEM;
485 return 0;
488 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
490 return blk_alloc_queue_node(gfp_mask, -1);
492 EXPORT_SYMBOL(blk_alloc_queue);
494 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
496 struct request_queue *q;
497 int err;
499 q = kmem_cache_alloc_node(blk_requestq_cachep,
500 gfp_mask | __GFP_ZERO, node_id);
501 if (!q)
502 return NULL;
504 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
505 q->backing_dev_info.unplug_io_data = q;
506 q->backing_dev_info.ra_pages =
507 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
508 q->backing_dev_info.state = 0;
509 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
510 q->backing_dev_info.name = "block";
512 err = bdi_init(&q->backing_dev_info);
513 if (err) {
514 kmem_cache_free(blk_requestq_cachep, q);
515 return NULL;
518 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
519 laptop_mode_timer_fn, (unsigned long) q);
520 init_timer(&q->unplug_timer);
521 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
522 INIT_LIST_HEAD(&q->timeout_list);
523 INIT_WORK(&q->unplug_work, blk_unplug_work);
525 kobject_init(&q->kobj, &blk_queue_ktype);
527 mutex_init(&q->sysfs_lock);
528 spin_lock_init(&q->__queue_lock);
530 return q;
532 EXPORT_SYMBOL(blk_alloc_queue_node);
535 * blk_init_queue - prepare a request queue for use with a block device
536 * @rfn: The function to be called to process requests that have been
537 * placed on the queue.
538 * @lock: Request queue spin lock
540 * Description:
541 * If a block device wishes to use the standard request handling procedures,
542 * which sorts requests and coalesces adjacent requests, then it must
543 * call blk_init_queue(). The function @rfn will be called when there
544 * are requests on the queue that need to be processed. If the device
545 * supports plugging, then @rfn may not be called immediately when requests
546 * are available on the queue, but may be called at some time later instead.
547 * Plugged queues are generally unplugged when a buffer belonging to one
548 * of the requests on the queue is needed, or due to memory pressure.
550 * @rfn is not required, or even expected, to remove all requests off the
551 * queue, but only as many as it can handle at a time. If it does leave
552 * requests on the queue, it is responsible for arranging that the requests
553 * get dealt with eventually.
555 * The queue spin lock must be held while manipulating the requests on the
556 * request queue; this lock will be taken also from interrupt context, so irq
557 * disabling is needed for it.
559 * Function returns a pointer to the initialized request queue, or %NULL if
560 * it didn't succeed.
562 * Note:
563 * blk_init_queue() must be paired with a blk_cleanup_queue() call
564 * when the block device is deactivated (such as at module unload).
567 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
569 return blk_init_queue_node(rfn, lock, -1);
571 EXPORT_SYMBOL(blk_init_queue);
573 struct request_queue *
574 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
576 struct request_queue *uninit_q, *q;
578 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
579 if (!uninit_q)
580 return NULL;
582 q = blk_init_allocated_queue_node(uninit_q, rfn, lock, node_id);
583 if (!q)
584 blk_cleanup_queue(uninit_q);
586 return q;
588 EXPORT_SYMBOL(blk_init_queue_node);
590 struct request_queue *
591 blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
592 spinlock_t *lock)
594 return blk_init_allocated_queue_node(q, rfn, lock, -1);
596 EXPORT_SYMBOL(blk_init_allocated_queue);
598 struct request_queue *
599 blk_init_allocated_queue_node(struct request_queue *q, request_fn_proc *rfn,
600 spinlock_t *lock, int node_id)
602 if (!q)
603 return NULL;
605 q->node = node_id;
606 if (blk_init_free_list(q))
607 return NULL;
609 q->request_fn = rfn;
610 q->prep_rq_fn = NULL;
611 q->unplug_fn = generic_unplug_device;
612 q->queue_flags = QUEUE_FLAG_DEFAULT;
613 q->queue_lock = lock;
616 * This also sets hw/phys segments, boundary and size
618 blk_queue_make_request(q, __make_request);
620 q->sg_reserved_size = INT_MAX;
623 * all done
625 if (!elevator_init(q, NULL)) {
626 blk_queue_congestion_threshold(q);
627 return q;
630 return NULL;
632 EXPORT_SYMBOL(blk_init_allocated_queue_node);
634 int blk_get_queue(struct request_queue *q)
636 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
637 kobject_get(&q->kobj);
638 return 0;
641 return 1;
644 static inline void blk_free_request(struct request_queue *q, struct request *rq)
646 if (rq->cmd_flags & REQ_ELVPRIV)
647 elv_put_request(q, rq);
648 mempool_free(rq, q->rq.rq_pool);
651 static struct request *
652 blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
654 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
656 if (!rq)
657 return NULL;
659 blk_rq_init(q, rq);
661 rq->cmd_flags = flags | REQ_ALLOCED;
663 if (priv) {
664 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
665 mempool_free(rq, q->rq.rq_pool);
666 return NULL;
668 rq->cmd_flags |= REQ_ELVPRIV;
671 return rq;
675 * ioc_batching returns true if the ioc is a valid batching request and
676 * should be given priority access to a request.
678 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
680 if (!ioc)
681 return 0;
684 * Make sure the process is able to allocate at least 1 request
685 * even if the batch times out, otherwise we could theoretically
686 * lose wakeups.
688 return ioc->nr_batch_requests == q->nr_batching ||
689 (ioc->nr_batch_requests > 0
690 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
694 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
695 * will cause the process to be a "batcher" on all queues in the system. This
696 * is the behaviour we want though - once it gets a wakeup it should be given
697 * a nice run.
699 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
701 if (!ioc || ioc_batching(q, ioc))
702 return;
704 ioc->nr_batch_requests = q->nr_batching;
705 ioc->last_waited = jiffies;
708 static void __freed_request(struct request_queue *q, int sync)
710 struct request_list *rl = &q->rq;
712 if (rl->count[sync] < queue_congestion_off_threshold(q))
713 blk_clear_queue_congested(q, sync);
715 if (rl->count[sync] + 1 <= q->nr_requests) {
716 if (waitqueue_active(&rl->wait[sync]))
717 wake_up(&rl->wait[sync]);
719 blk_clear_queue_full(q, sync);
724 * A request has just been released. Account for it, update the full and
725 * congestion status, wake up any waiters. Called under q->queue_lock.
727 static void freed_request(struct request_queue *q, int sync, int priv)
729 struct request_list *rl = &q->rq;
731 rl->count[sync]--;
732 if (priv)
733 rl->elvpriv--;
735 __freed_request(q, sync);
737 if (unlikely(rl->starved[sync ^ 1]))
738 __freed_request(q, sync ^ 1);
742 * Get a free request, queue_lock must be held.
743 * Returns NULL on failure, with queue_lock held.
744 * Returns !NULL on success, with queue_lock *not held*.
746 static struct request *get_request(struct request_queue *q, int rw_flags,
747 struct bio *bio, gfp_t gfp_mask)
749 struct request *rq = NULL;
750 struct request_list *rl = &q->rq;
751 struct io_context *ioc = NULL;
752 const bool is_sync = rw_is_sync(rw_flags) != 0;
753 int may_queue, priv;
755 may_queue = elv_may_queue(q, rw_flags);
756 if (may_queue == ELV_MQUEUE_NO)
757 goto rq_starved;
759 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
760 if (rl->count[is_sync]+1 >= q->nr_requests) {
761 ioc = current_io_context(GFP_ATOMIC, q->node);
763 * The queue will fill after this allocation, so set
764 * it as full, and mark this process as "batching".
765 * This process will be allowed to complete a batch of
766 * requests, others will be blocked.
768 if (!blk_queue_full(q, is_sync)) {
769 ioc_set_batching(q, ioc);
770 blk_set_queue_full(q, is_sync);
771 } else {
772 if (may_queue != ELV_MQUEUE_MUST
773 && !ioc_batching(q, ioc)) {
775 * The queue is full and the allocating
776 * process is not a "batcher", and not
777 * exempted by the IO scheduler
779 goto out;
783 blk_set_queue_congested(q, is_sync);
787 * Only allow batching queuers to allocate up to 50% over the defined
788 * limit of requests, otherwise we could have thousands of requests
789 * allocated with any setting of ->nr_requests
791 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
792 goto out;
794 rl->count[is_sync]++;
795 rl->starved[is_sync] = 0;
797 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
798 if (priv)
799 rl->elvpriv++;
801 if (blk_queue_io_stat(q))
802 rw_flags |= REQ_IO_STAT;
803 spin_unlock_irq(q->queue_lock);
805 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
806 if (unlikely(!rq)) {
808 * Allocation failed presumably due to memory. Undo anything
809 * we might have messed up.
811 * Allocating task should really be put onto the front of the
812 * wait queue, but this is pretty rare.
814 spin_lock_irq(q->queue_lock);
815 freed_request(q, is_sync, priv);
818 * in the very unlikely event that allocation failed and no
819 * requests for this direction was pending, mark us starved
820 * so that freeing of a request in the other direction will
821 * notice us. another possible fix would be to split the
822 * rq mempool into READ and WRITE
824 rq_starved:
825 if (unlikely(rl->count[is_sync] == 0))
826 rl->starved[is_sync] = 1;
828 goto out;
832 * ioc may be NULL here, and ioc_batching will be false. That's
833 * OK, if the queue is under the request limit then requests need
834 * not count toward the nr_batch_requests limit. There will always
835 * be some limit enforced by BLK_BATCH_TIME.
837 if (ioc_batching(q, ioc))
838 ioc->nr_batch_requests--;
840 trace_block_getrq(q, bio, rw_flags & 1);
841 out:
842 return rq;
846 * No available requests for this queue, unplug the device and wait for some
847 * requests to become available.
849 * Called with q->queue_lock held, and returns with it unlocked.
851 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
852 struct bio *bio)
854 const bool is_sync = rw_is_sync(rw_flags) != 0;
855 struct request *rq;
857 rq = get_request(q, rw_flags, bio, GFP_NOIO);
858 while (!rq) {
859 DEFINE_WAIT(wait);
860 struct io_context *ioc;
861 struct request_list *rl = &q->rq;
863 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
864 TASK_UNINTERRUPTIBLE);
866 trace_block_sleeprq(q, bio, rw_flags & 1);
868 __generic_unplug_device(q);
869 spin_unlock_irq(q->queue_lock);
870 io_schedule();
873 * After sleeping, we become a "batching" process and
874 * will be able to allocate at least one request, and
875 * up to a big batch of them for a small period time.
876 * See ioc_batching, ioc_set_batching
878 ioc = current_io_context(GFP_NOIO, q->node);
879 ioc_set_batching(q, ioc);
881 spin_lock_irq(q->queue_lock);
882 finish_wait(&rl->wait[is_sync], &wait);
884 rq = get_request(q, rw_flags, bio, GFP_NOIO);
887 return rq;
890 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
892 struct request *rq;
894 BUG_ON(rw != READ && rw != WRITE);
896 spin_lock_irq(q->queue_lock);
897 if (gfp_mask & __GFP_WAIT) {
898 rq = get_request_wait(q, rw, NULL);
899 } else {
900 rq = get_request(q, rw, NULL, gfp_mask);
901 if (!rq)
902 spin_unlock_irq(q->queue_lock);
904 /* q->queue_lock is unlocked at this point */
906 return rq;
908 EXPORT_SYMBOL(blk_get_request);
911 * blk_make_request - given a bio, allocate a corresponding struct request.
912 * @q: target request queue
913 * @bio: The bio describing the memory mappings that will be submitted for IO.
914 * It may be a chained-bio properly constructed by block/bio layer.
915 * @gfp_mask: gfp flags to be used for memory allocation
917 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
918 * type commands. Where the struct request needs to be farther initialized by
919 * the caller. It is passed a &struct bio, which describes the memory info of
920 * the I/O transfer.
922 * The caller of blk_make_request must make sure that bi_io_vec
923 * are set to describe the memory buffers. That bio_data_dir() will return
924 * the needed direction of the request. (And all bio's in the passed bio-chain
925 * are properly set accordingly)
927 * If called under none-sleepable conditions, mapped bio buffers must not
928 * need bouncing, by calling the appropriate masked or flagged allocator,
929 * suitable for the target device. Otherwise the call to blk_queue_bounce will
930 * BUG.
932 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
933 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
934 * anything but the first bio in the chain. Otherwise you risk waiting for IO
935 * completion of a bio that hasn't been submitted yet, thus resulting in a
936 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
937 * of bio_alloc(), as that avoids the mempool deadlock.
938 * If possible a big IO should be split into smaller parts when allocation
939 * fails. Partial allocation should not be an error, or you risk a live-lock.
941 struct request *blk_make_request(struct request_queue *q, struct bio *bio,
942 gfp_t gfp_mask)
944 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
946 if (unlikely(!rq))
947 return ERR_PTR(-ENOMEM);
949 for_each_bio(bio) {
950 struct bio *bounce_bio = bio;
951 int ret;
953 blk_queue_bounce(q, &bounce_bio);
954 ret = blk_rq_append_bio(q, rq, bounce_bio);
955 if (unlikely(ret)) {
956 blk_put_request(rq);
957 return ERR_PTR(ret);
961 return rq;
963 EXPORT_SYMBOL(blk_make_request);
966 * blk_requeue_request - put a request back on queue
967 * @q: request queue where request should be inserted
968 * @rq: request to be inserted
970 * Description:
971 * Drivers often keep queueing requests until the hardware cannot accept
972 * more, when that condition happens we need to put the request back
973 * on the queue. Must be called with queue lock held.
975 void blk_requeue_request(struct request_queue *q, struct request *rq)
977 blk_delete_timer(rq);
978 blk_clear_rq_complete(rq);
979 trace_block_rq_requeue(q, rq);
981 if (blk_rq_tagged(rq))
982 blk_queue_end_tag(q, rq);
984 BUG_ON(blk_queued_rq(rq));
986 elv_requeue_request(q, rq);
988 EXPORT_SYMBOL(blk_requeue_request);
991 * blk_insert_request - insert a special request into a request queue
992 * @q: request queue where request should be inserted
993 * @rq: request to be inserted
994 * @at_head: insert request at head or tail of queue
995 * @data: private data
997 * Description:
998 * Many block devices need to execute commands asynchronously, so they don't
999 * block the whole kernel from preemption during request execution. This is
1000 * accomplished normally by inserting aritficial requests tagged as
1001 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1002 * be scheduled for actual execution by the request queue.
1004 * We have the option of inserting the head or the tail of the queue.
1005 * Typically we use the tail for new ioctls and so forth. We use the head
1006 * of the queue for things like a QUEUE_FULL message from a device, or a
1007 * host that is unable to accept a particular command.
1009 void blk_insert_request(struct request_queue *q, struct request *rq,
1010 int at_head, void *data)
1012 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
1013 unsigned long flags;
1016 * tell I/O scheduler that this isn't a regular read/write (ie it
1017 * must not attempt merges on this) and that it acts as a soft
1018 * barrier
1020 rq->cmd_type = REQ_TYPE_SPECIAL;
1022 rq->special = data;
1024 spin_lock_irqsave(q->queue_lock, flags);
1027 * If command is tagged, release the tag
1029 if (blk_rq_tagged(rq))
1030 blk_queue_end_tag(q, rq);
1032 drive_stat_acct(rq, 1);
1033 __elv_add_request(q, rq, where, 0);
1034 __blk_run_queue(q);
1035 spin_unlock_irqrestore(q->queue_lock, flags);
1037 EXPORT_SYMBOL(blk_insert_request);
1040 * add-request adds a request to the linked list.
1041 * queue lock is held and interrupts disabled, as we muck with the
1042 * request queue list.
1044 static inline void add_request(struct request_queue *q, struct request *req)
1046 drive_stat_acct(req, 1);
1049 * elevator indicated where it wants this request to be
1050 * inserted at elevator_merge time
1052 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1055 static void part_round_stats_single(int cpu, struct hd_struct *part,
1056 unsigned long now)
1058 if (now == part->stamp)
1059 return;
1061 if (part_in_flight(part)) {
1062 __part_stat_add(cpu, part, time_in_queue,
1063 part_in_flight(part) * (now - part->stamp));
1064 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1066 part->stamp = now;
1070 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1071 * @cpu: cpu number for stats access
1072 * @part: target partition
1074 * The average IO queue length and utilisation statistics are maintained
1075 * by observing the current state of the queue length and the amount of
1076 * time it has been in this state for.
1078 * Normally, that accounting is done on IO completion, but that can result
1079 * in more than a second's worth of IO being accounted for within any one
1080 * second, leading to >100% utilisation. To deal with that, we call this
1081 * function to do a round-off before returning the results when reading
1082 * /proc/diskstats. This accounts immediately for all queue usage up to
1083 * the current jiffies and restarts the counters again.
1085 void part_round_stats(int cpu, struct hd_struct *part)
1087 unsigned long now = jiffies;
1089 if (part->partno)
1090 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1091 part_round_stats_single(cpu, part, now);
1093 EXPORT_SYMBOL_GPL(part_round_stats);
1096 * queue lock must be held
1098 void __blk_put_request(struct request_queue *q, struct request *req)
1100 if (unlikely(!q))
1101 return;
1102 if (unlikely(--req->ref_count))
1103 return;
1105 elv_completed_request(q, req);
1107 /* this is a bio leak */
1108 WARN_ON(req->bio != NULL);
1111 * Request may not have originated from ll_rw_blk. if not,
1112 * it didn't come out of our reserved rq pools
1114 if (req->cmd_flags & REQ_ALLOCED) {
1115 int is_sync = rq_is_sync(req) != 0;
1116 int priv = req->cmd_flags & REQ_ELVPRIV;
1118 BUG_ON(!list_empty(&req->queuelist));
1119 BUG_ON(!hlist_unhashed(&req->hash));
1121 blk_free_request(q, req);
1122 freed_request(q, is_sync, priv);
1125 EXPORT_SYMBOL_GPL(__blk_put_request);
1127 void blk_put_request(struct request *req)
1129 unsigned long flags;
1130 struct request_queue *q = req->q;
1132 spin_lock_irqsave(q->queue_lock, flags);
1133 __blk_put_request(q, req);
1134 spin_unlock_irqrestore(q->queue_lock, flags);
1136 EXPORT_SYMBOL(blk_put_request);
1138 void init_request_from_bio(struct request *req, struct bio *bio)
1140 req->cpu = bio->bi_comp_cpu;
1141 req->cmd_type = REQ_TYPE_FS;
1144 * Inherit FAILFAST from bio (for read-ahead, and explicit
1145 * FAILFAST). FAILFAST flags are identical for req and bio.
1147 if (bio_rw_flagged(bio, BIO_RW_AHEAD))
1148 req->cmd_flags |= REQ_FAILFAST_MASK;
1149 else
1150 req->cmd_flags |= bio->bi_rw & REQ_FAILFAST_MASK;
1152 if (unlikely(bio_rw_flagged(bio, BIO_RW_DISCARD))) {
1153 req->cmd_flags |= REQ_DISCARD;
1154 if (bio_rw_flagged(bio, BIO_RW_BARRIER))
1155 req->cmd_flags |= REQ_SOFTBARRIER;
1156 } else if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER)))
1157 req->cmd_flags |= REQ_HARDBARRIER;
1159 if (bio_rw_flagged(bio, BIO_RW_SYNCIO))
1160 req->cmd_flags |= REQ_RW_SYNC;
1161 if (bio_rw_flagged(bio, BIO_RW_META))
1162 req->cmd_flags |= REQ_RW_META;
1163 if (bio_rw_flagged(bio, BIO_RW_NOIDLE))
1164 req->cmd_flags |= REQ_NOIDLE;
1166 req->errors = 0;
1167 req->__sector = bio->bi_sector;
1168 req->ioprio = bio_prio(bio);
1169 blk_rq_bio_prep(req->q, req, bio);
1173 * Only disabling plugging for non-rotational devices if it does tagging
1174 * as well, otherwise we do need the proper merging
1176 static inline bool queue_should_plug(struct request_queue *q)
1178 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1181 static int __make_request(struct request_queue *q, struct bio *bio)
1183 struct request *req;
1184 int el_ret;
1185 unsigned int bytes = bio->bi_size;
1186 const unsigned short prio = bio_prio(bio);
1187 const bool sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
1188 const bool unplug = bio_rw_flagged(bio, BIO_RW_UNPLUG);
1189 const unsigned int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1190 int rw_flags;
1192 if (bio_rw_flagged(bio, BIO_RW_BARRIER) &&
1193 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1194 bio_endio(bio, -EOPNOTSUPP);
1195 return 0;
1198 * low level driver can indicate that it wants pages above a
1199 * certain limit bounced to low memory (ie for highmem, or even
1200 * ISA dma in theory)
1202 blk_queue_bounce(q, &bio);
1204 spin_lock_irq(q->queue_lock);
1206 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER)) || elv_queue_empty(q))
1207 goto get_rq;
1209 el_ret = elv_merge(q, &req, bio);
1210 switch (el_ret) {
1211 case ELEVATOR_BACK_MERGE:
1212 BUG_ON(!rq_mergeable(req));
1214 if (!ll_back_merge_fn(q, req, bio))
1215 break;
1217 trace_block_bio_backmerge(q, bio);
1219 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1220 blk_rq_set_mixed_merge(req);
1222 req->biotail->bi_next = bio;
1223 req->biotail = bio;
1224 req->__data_len += bytes;
1225 req->ioprio = ioprio_best(req->ioprio, prio);
1226 if (!blk_rq_cpu_valid(req))
1227 req->cpu = bio->bi_comp_cpu;
1228 drive_stat_acct(req, 0);
1229 elv_bio_merged(q, req, bio);
1230 if (!attempt_back_merge(q, req))
1231 elv_merged_request(q, req, el_ret);
1232 goto out;
1234 case ELEVATOR_FRONT_MERGE:
1235 BUG_ON(!rq_mergeable(req));
1237 if (!ll_front_merge_fn(q, req, bio))
1238 break;
1240 trace_block_bio_frontmerge(q, bio);
1242 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1243 blk_rq_set_mixed_merge(req);
1244 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1245 req->cmd_flags |= ff;
1248 bio->bi_next = req->bio;
1249 req->bio = bio;
1252 * may not be valid. if the low level driver said
1253 * it didn't need a bounce buffer then it better
1254 * not touch req->buffer either...
1256 req->buffer = bio_data(bio);
1257 req->__sector = bio->bi_sector;
1258 req->__data_len += bytes;
1259 req->ioprio = ioprio_best(req->ioprio, prio);
1260 if (!blk_rq_cpu_valid(req))
1261 req->cpu = bio->bi_comp_cpu;
1262 drive_stat_acct(req, 0);
1263 elv_bio_merged(q, req, bio);
1264 if (!attempt_front_merge(q, req))
1265 elv_merged_request(q, req, el_ret);
1266 goto out;
1268 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1269 default:
1273 get_rq:
1275 * This sync check and mask will be re-done in init_request_from_bio(),
1276 * but we need to set it earlier to expose the sync flag to the
1277 * rq allocator and io schedulers.
1279 rw_flags = bio_data_dir(bio);
1280 if (sync)
1281 rw_flags |= REQ_RW_SYNC;
1284 * Grab a free request. This is might sleep but can not fail.
1285 * Returns with the queue unlocked.
1287 req = get_request_wait(q, rw_flags, bio);
1290 * After dropping the lock and possibly sleeping here, our request
1291 * may now be mergeable after it had proven unmergeable (above).
1292 * We don't worry about that case for efficiency. It won't happen
1293 * often, and the elevators are able to handle it.
1295 init_request_from_bio(req, bio);
1297 spin_lock_irq(q->queue_lock);
1298 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1299 bio_flagged(bio, BIO_CPU_AFFINE))
1300 req->cpu = blk_cpu_to_group(smp_processor_id());
1301 if (queue_should_plug(q) && elv_queue_empty(q))
1302 blk_plug_device(q);
1303 add_request(q, req);
1304 out:
1305 if (unplug || !queue_should_plug(q))
1306 __generic_unplug_device(q);
1307 spin_unlock_irq(q->queue_lock);
1308 return 0;
1312 * If bio->bi_dev is a partition, remap the location
1314 static inline void blk_partition_remap(struct bio *bio)
1316 struct block_device *bdev = bio->bi_bdev;
1318 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1319 struct hd_struct *p = bdev->bd_part;
1321 bio->bi_sector += p->start_sect;
1322 bio->bi_bdev = bdev->bd_contains;
1324 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
1325 bdev->bd_dev,
1326 bio->bi_sector - p->start_sect);
1330 static void handle_bad_sector(struct bio *bio)
1332 char b[BDEVNAME_SIZE];
1334 printk(KERN_INFO "attempt to access beyond end of device\n");
1335 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1336 bdevname(bio->bi_bdev, b),
1337 bio->bi_rw,
1338 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1339 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1341 set_bit(BIO_EOF, &bio->bi_flags);
1344 #ifdef CONFIG_FAIL_MAKE_REQUEST
1346 static DECLARE_FAULT_ATTR(fail_make_request);
1348 static int __init setup_fail_make_request(char *str)
1350 return setup_fault_attr(&fail_make_request, str);
1352 __setup("fail_make_request=", setup_fail_make_request);
1354 static int should_fail_request(struct bio *bio)
1356 struct hd_struct *part = bio->bi_bdev->bd_part;
1358 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
1359 return should_fail(&fail_make_request, bio->bi_size);
1361 return 0;
1364 static int __init fail_make_request_debugfs(void)
1366 return init_fault_attr_dentries(&fail_make_request,
1367 "fail_make_request");
1370 late_initcall(fail_make_request_debugfs);
1372 #else /* CONFIG_FAIL_MAKE_REQUEST */
1374 static inline int should_fail_request(struct bio *bio)
1376 return 0;
1379 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1382 * Check whether this bio extends beyond the end of the device.
1384 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1386 sector_t maxsector;
1388 if (!nr_sectors)
1389 return 0;
1391 /* Test device or partition size, when known. */
1392 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1393 if (maxsector) {
1394 sector_t sector = bio->bi_sector;
1396 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1398 * This may well happen - the kernel calls bread()
1399 * without checking the size of the device, e.g., when
1400 * mounting a device.
1402 handle_bad_sector(bio);
1403 return 1;
1407 return 0;
1411 * generic_make_request - hand a buffer to its device driver for I/O
1412 * @bio: The bio describing the location in memory and on the device.
1414 * generic_make_request() is used to make I/O requests of block
1415 * devices. It is passed a &struct bio, which describes the I/O that needs
1416 * to be done.
1418 * generic_make_request() does not return any status. The
1419 * success/failure status of the request, along with notification of
1420 * completion, is delivered asynchronously through the bio->bi_end_io
1421 * function described (one day) else where.
1423 * The caller of generic_make_request must make sure that bi_io_vec
1424 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1425 * set to describe the device address, and the
1426 * bi_end_io and optionally bi_private are set to describe how
1427 * completion notification should be signaled.
1429 * generic_make_request and the drivers it calls may use bi_next if this
1430 * bio happens to be merged with someone else, and may change bi_dev and
1431 * bi_sector for remaps as it sees fit. So the values of these fields
1432 * should NOT be depended on after the call to generic_make_request.
1434 static inline void __generic_make_request(struct bio *bio)
1436 struct request_queue *q;
1437 sector_t old_sector;
1438 int ret, nr_sectors = bio_sectors(bio);
1439 dev_t old_dev;
1440 int err = -EIO;
1442 might_sleep();
1444 if (bio_check_eod(bio, nr_sectors))
1445 goto end_io;
1448 * Resolve the mapping until finished. (drivers are
1449 * still free to implement/resolve their own stacking
1450 * by explicitly returning 0)
1452 * NOTE: we don't repeat the blk_size check for each new device.
1453 * Stacking drivers are expected to know what they are doing.
1455 old_sector = -1;
1456 old_dev = 0;
1457 do {
1458 char b[BDEVNAME_SIZE];
1460 q = bdev_get_queue(bio->bi_bdev);
1461 if (unlikely(!q)) {
1462 printk(KERN_ERR
1463 "generic_make_request: Trying to access "
1464 "nonexistent block-device %s (%Lu)\n",
1465 bdevname(bio->bi_bdev, b),
1466 (long long) bio->bi_sector);
1467 goto end_io;
1470 if (unlikely(!bio_rw_flagged(bio, BIO_RW_DISCARD) &&
1471 nr_sectors > queue_max_hw_sectors(q))) {
1472 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1473 bdevname(bio->bi_bdev, b),
1474 bio_sectors(bio),
1475 queue_max_hw_sectors(q));
1476 goto end_io;
1479 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1480 goto end_io;
1482 if (should_fail_request(bio))
1483 goto end_io;
1486 * If this device has partitions, remap block n
1487 * of partition p to block n+start(p) of the disk.
1489 blk_partition_remap(bio);
1491 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1492 goto end_io;
1494 if (old_sector != -1)
1495 trace_block_remap(q, bio, old_dev, old_sector);
1497 old_sector = bio->bi_sector;
1498 old_dev = bio->bi_bdev->bd_dev;
1500 if (bio_check_eod(bio, nr_sectors))
1501 goto end_io;
1503 if (bio_rw_flagged(bio, BIO_RW_DISCARD) &&
1504 !blk_queue_discard(q)) {
1505 err = -EOPNOTSUPP;
1506 goto end_io;
1509 trace_block_bio_queue(q, bio);
1511 ret = q->make_request_fn(q, bio);
1512 } while (ret);
1514 return;
1516 end_io:
1517 bio_endio(bio, err);
1521 * We only want one ->make_request_fn to be active at a time,
1522 * else stack usage with stacked devices could be a problem.
1523 * So use current->bio_list to keep a list of requests
1524 * submited by a make_request_fn function.
1525 * current->bio_list is also used as a flag to say if
1526 * generic_make_request is currently active in this task or not.
1527 * If it is NULL, then no make_request is active. If it is non-NULL,
1528 * then a make_request is active, and new requests should be added
1529 * at the tail
1531 void generic_make_request(struct bio *bio)
1533 struct bio_list bio_list_on_stack;
1535 if (current->bio_list) {
1536 /* make_request is active */
1537 bio_list_add(current->bio_list, bio);
1538 return;
1540 /* following loop may be a bit non-obvious, and so deserves some
1541 * explanation.
1542 * Before entering the loop, bio->bi_next is NULL (as all callers
1543 * ensure that) so we have a list with a single bio.
1544 * We pretend that we have just taken it off a longer list, so
1545 * we assign bio_list to a pointer to the bio_list_on_stack,
1546 * thus initialising the bio_list of new bios to be
1547 * added. __generic_make_request may indeed add some more bios
1548 * through a recursive call to generic_make_request. If it
1549 * did, we find a non-NULL value in bio_list and re-enter the loop
1550 * from the top. In this case we really did just take the bio
1551 * of the top of the list (no pretending) and so remove it from
1552 * bio_list, and call into __generic_make_request again.
1554 * The loop was structured like this to make only one call to
1555 * __generic_make_request (which is important as it is large and
1556 * inlined) and to keep the structure simple.
1558 BUG_ON(bio->bi_next);
1559 bio_list_init(&bio_list_on_stack);
1560 current->bio_list = &bio_list_on_stack;
1561 do {
1562 __generic_make_request(bio);
1563 bio = bio_list_pop(current->bio_list);
1564 } while (bio);
1565 current->bio_list = NULL; /* deactivate */
1567 EXPORT_SYMBOL(generic_make_request);
1570 * submit_bio - submit a bio to the block device layer for I/O
1571 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1572 * @bio: The &struct bio which describes the I/O
1574 * submit_bio() is very similar in purpose to generic_make_request(), and
1575 * uses that function to do most of the work. Both are fairly rough
1576 * interfaces; @bio must be presetup and ready for I/O.
1579 void submit_bio(int rw, struct bio *bio)
1581 int count = bio_sectors(bio);
1583 bio->bi_rw |= rw;
1586 * If it's a regular read/write or a barrier with data attached,
1587 * go through the normal accounting stuff before submission.
1589 if (bio_has_data(bio)) {
1590 if (rw & WRITE) {
1591 count_vm_events(PGPGOUT, count);
1592 } else {
1593 task_io_account_read(bio->bi_size);
1594 count_vm_events(PGPGIN, count);
1597 if (unlikely(block_dump)) {
1598 char b[BDEVNAME_SIZE];
1599 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
1600 current->comm, task_pid_nr(current),
1601 (rw & WRITE) ? "WRITE" : "READ",
1602 (unsigned long long)bio->bi_sector,
1603 bdevname(bio->bi_bdev, b));
1607 generic_make_request(bio);
1609 EXPORT_SYMBOL(submit_bio);
1612 * blk_rq_check_limits - Helper function to check a request for the queue limit
1613 * @q: the queue
1614 * @rq: the request being checked
1616 * Description:
1617 * @rq may have been made based on weaker limitations of upper-level queues
1618 * in request stacking drivers, and it may violate the limitation of @q.
1619 * Since the block layer and the underlying device driver trust @rq
1620 * after it is inserted to @q, it should be checked against @q before
1621 * the insertion using this generic function.
1623 * This function should also be useful for request stacking drivers
1624 * in some cases below, so export this fuction.
1625 * Request stacking drivers like request-based dm may change the queue
1626 * limits while requests are in the queue (e.g. dm's table swapping).
1627 * Such request stacking drivers should check those requests agaist
1628 * the new queue limits again when they dispatch those requests,
1629 * although such checkings are also done against the old queue limits
1630 * when submitting requests.
1632 int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1634 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1635 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
1636 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1637 return -EIO;
1641 * queue's settings related to segment counting like q->bounce_pfn
1642 * may differ from that of other stacking queues.
1643 * Recalculate it to check the request correctly on this queue's
1644 * limitation.
1646 blk_recalc_rq_segments(rq);
1647 if (rq->nr_phys_segments > queue_max_segments(q)) {
1648 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1649 return -EIO;
1652 return 0;
1654 EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1657 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1658 * @q: the queue to submit the request
1659 * @rq: the request being queued
1661 int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1663 unsigned long flags;
1665 if (blk_rq_check_limits(q, rq))
1666 return -EIO;
1668 #ifdef CONFIG_FAIL_MAKE_REQUEST
1669 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1670 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1671 return -EIO;
1672 #endif
1674 spin_lock_irqsave(q->queue_lock, flags);
1677 * Submitting request must be dequeued before calling this function
1678 * because it will be linked to another request_queue
1680 BUG_ON(blk_queued_rq(rq));
1682 drive_stat_acct(rq, 1);
1683 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1685 spin_unlock_irqrestore(q->queue_lock, flags);
1687 return 0;
1689 EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1692 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1693 * @rq: request to examine
1695 * Description:
1696 * A request could be merge of IOs which require different failure
1697 * handling. This function determines the number of bytes which
1698 * can be failed from the beginning of the request without
1699 * crossing into area which need to be retried further.
1701 * Return:
1702 * The number of bytes to fail.
1704 * Context:
1705 * queue_lock must be held.
1707 unsigned int blk_rq_err_bytes(const struct request *rq)
1709 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1710 unsigned int bytes = 0;
1711 struct bio *bio;
1713 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1714 return blk_rq_bytes(rq);
1717 * Currently the only 'mixing' which can happen is between
1718 * different fastfail types. We can safely fail portions
1719 * which have all the failfast bits that the first one has -
1720 * the ones which are at least as eager to fail as the first
1721 * one.
1723 for (bio = rq->bio; bio; bio = bio->bi_next) {
1724 if ((bio->bi_rw & ff) != ff)
1725 break;
1726 bytes += bio->bi_size;
1729 /* this could lead to infinite loop */
1730 BUG_ON(blk_rq_bytes(rq) && !bytes);
1731 return bytes;
1733 EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1735 static void blk_account_io_completion(struct request *req, unsigned int bytes)
1737 if (blk_do_io_stat(req)) {
1738 const int rw = rq_data_dir(req);
1739 struct hd_struct *part;
1740 int cpu;
1742 cpu = part_stat_lock();
1743 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1744 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1745 part_stat_unlock();
1749 static void blk_account_io_done(struct request *req)
1752 * Account IO completion. bar_rq isn't accounted as a normal
1753 * IO on queueing nor completion. Accounting the containing
1754 * request is enough.
1756 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
1757 unsigned long duration = jiffies - req->start_time;
1758 const int rw = rq_data_dir(req);
1759 struct hd_struct *part;
1760 int cpu;
1762 cpu = part_stat_lock();
1763 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1765 part_stat_inc(cpu, part, ios[rw]);
1766 part_stat_add(cpu, part, ticks[rw], duration);
1767 part_round_stats(cpu, part);
1768 part_dec_in_flight(part, rw);
1770 part_stat_unlock();
1775 * blk_peek_request - peek at the top of a request queue
1776 * @q: request queue to peek at
1778 * Description:
1779 * Return the request at the top of @q. The returned request
1780 * should be started using blk_start_request() before LLD starts
1781 * processing it.
1783 * Return:
1784 * Pointer to the request at the top of @q if available. Null
1785 * otherwise.
1787 * Context:
1788 * queue_lock must be held.
1790 struct request *blk_peek_request(struct request_queue *q)
1792 struct request *rq;
1793 int ret;
1795 while ((rq = __elv_next_request(q)) != NULL) {
1796 if (!(rq->cmd_flags & REQ_STARTED)) {
1798 * This is the first time the device driver
1799 * sees this request (possibly after
1800 * requeueing). Notify IO scheduler.
1802 if (blk_sorted_rq(rq))
1803 elv_activate_rq(q, rq);
1806 * just mark as started even if we don't start
1807 * it, a request that has been delayed should
1808 * not be passed by new incoming requests
1810 rq->cmd_flags |= REQ_STARTED;
1811 trace_block_rq_issue(q, rq);
1814 if (!q->boundary_rq || q->boundary_rq == rq) {
1815 q->end_sector = rq_end_sector(rq);
1816 q->boundary_rq = NULL;
1819 if (rq->cmd_flags & REQ_DONTPREP)
1820 break;
1822 if (q->dma_drain_size && blk_rq_bytes(rq)) {
1824 * make sure space for the drain appears we
1825 * know we can do this because max_hw_segments
1826 * has been adjusted to be one fewer than the
1827 * device can handle
1829 rq->nr_phys_segments++;
1832 if (!q->prep_rq_fn)
1833 break;
1835 ret = q->prep_rq_fn(q, rq);
1836 if (ret == BLKPREP_OK) {
1837 break;
1838 } else if (ret == BLKPREP_DEFER) {
1840 * the request may have been (partially) prepped.
1841 * we need to keep this request in the front to
1842 * avoid resource deadlock. REQ_STARTED will
1843 * prevent other fs requests from passing this one.
1845 if (q->dma_drain_size && blk_rq_bytes(rq) &&
1846 !(rq->cmd_flags & REQ_DONTPREP)) {
1848 * remove the space for the drain we added
1849 * so that we don't add it again
1851 --rq->nr_phys_segments;
1854 rq = NULL;
1855 break;
1856 } else if (ret == BLKPREP_KILL) {
1857 rq->cmd_flags |= REQ_QUIET;
1859 * Mark this request as started so we don't trigger
1860 * any debug logic in the end I/O path.
1862 blk_start_request(rq);
1863 __blk_end_request_all(rq, -EIO);
1864 } else {
1865 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1866 break;
1870 return rq;
1872 EXPORT_SYMBOL(blk_peek_request);
1874 void blk_dequeue_request(struct request *rq)
1876 struct request_queue *q = rq->q;
1878 BUG_ON(list_empty(&rq->queuelist));
1879 BUG_ON(ELV_ON_HASH(rq));
1881 list_del_init(&rq->queuelist);
1884 * the time frame between a request being removed from the lists
1885 * and to it is freed is accounted as io that is in progress at
1886 * the driver side.
1888 if (blk_account_rq(rq)) {
1889 q->in_flight[rq_is_sync(rq)]++;
1890 set_io_start_time_ns(rq);
1895 * blk_start_request - start request processing on the driver
1896 * @req: request to dequeue
1898 * Description:
1899 * Dequeue @req and start timeout timer on it. This hands off the
1900 * request to the driver.
1902 * Block internal functions which don't want to start timer should
1903 * call blk_dequeue_request().
1905 * Context:
1906 * queue_lock must be held.
1908 void blk_start_request(struct request *req)
1910 blk_dequeue_request(req);
1913 * We are now handing the request to the hardware, initialize
1914 * resid_len to full count and add the timeout handler.
1916 req->resid_len = blk_rq_bytes(req);
1917 if (unlikely(blk_bidi_rq(req)))
1918 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1920 blk_add_timer(req);
1922 EXPORT_SYMBOL(blk_start_request);
1925 * blk_fetch_request - fetch a request from a request queue
1926 * @q: request queue to fetch a request from
1928 * Description:
1929 * Return the request at the top of @q. The request is started on
1930 * return and LLD can start processing it immediately.
1932 * Return:
1933 * Pointer to the request at the top of @q if available. Null
1934 * otherwise.
1936 * Context:
1937 * queue_lock must be held.
1939 struct request *blk_fetch_request(struct request_queue *q)
1941 struct request *rq;
1943 rq = blk_peek_request(q);
1944 if (rq)
1945 blk_start_request(rq);
1946 return rq;
1948 EXPORT_SYMBOL(blk_fetch_request);
1951 * blk_update_request - Special helper function for request stacking drivers
1952 * @req: the request being processed
1953 * @error: %0 for success, < %0 for error
1954 * @nr_bytes: number of bytes to complete @req
1956 * Description:
1957 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1958 * the request structure even if @req doesn't have leftover.
1959 * If @req has leftover, sets it up for the next range of segments.
1961 * This special helper function is only for request stacking drivers
1962 * (e.g. request-based dm) so that they can handle partial completion.
1963 * Actual device drivers should use blk_end_request instead.
1965 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1966 * %false return from this function.
1968 * Return:
1969 * %false - this request doesn't have any more data
1970 * %true - this request has more data
1972 bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
1974 int total_bytes, bio_nbytes, next_idx = 0;
1975 struct bio *bio;
1977 if (!req->bio)
1978 return false;
1980 trace_block_rq_complete(req->q, req);
1983 * For fs requests, rq is just carrier of independent bio's
1984 * and each partial completion should be handled separately.
1985 * Reset per-request error on each partial completion.
1987 * TODO: tj: This is too subtle. It would be better to let
1988 * low level drivers do what they see fit.
1990 if (blk_fs_request(req))
1991 req->errors = 0;
1993 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1994 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1995 req->rq_disk ? req->rq_disk->disk_name : "?",
1996 (unsigned long long)blk_rq_pos(req));
1999 blk_account_io_completion(req, nr_bytes);
2001 total_bytes = bio_nbytes = 0;
2002 while ((bio = req->bio) != NULL) {
2003 int nbytes;
2005 if (nr_bytes >= bio->bi_size) {
2006 req->bio = bio->bi_next;
2007 nbytes = bio->bi_size;
2008 req_bio_endio(req, bio, nbytes, error);
2009 next_idx = 0;
2010 bio_nbytes = 0;
2011 } else {
2012 int idx = bio->bi_idx + next_idx;
2014 if (unlikely(idx >= bio->bi_vcnt)) {
2015 blk_dump_rq_flags(req, "__end_that");
2016 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
2017 __func__, idx, bio->bi_vcnt);
2018 break;
2021 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2022 BIO_BUG_ON(nbytes > bio->bi_size);
2025 * not a complete bvec done
2027 if (unlikely(nbytes > nr_bytes)) {
2028 bio_nbytes += nr_bytes;
2029 total_bytes += nr_bytes;
2030 break;
2034 * advance to the next vector
2036 next_idx++;
2037 bio_nbytes += nbytes;
2040 total_bytes += nbytes;
2041 nr_bytes -= nbytes;
2043 bio = req->bio;
2044 if (bio) {
2046 * end more in this run, or just return 'not-done'
2048 if (unlikely(nr_bytes <= 0))
2049 break;
2054 * completely done
2056 if (!req->bio) {
2058 * Reset counters so that the request stacking driver
2059 * can find how many bytes remain in the request
2060 * later.
2062 req->__data_len = 0;
2063 return false;
2067 * if the request wasn't completed, update state
2069 if (bio_nbytes) {
2070 req_bio_endio(req, bio, bio_nbytes, error);
2071 bio->bi_idx += next_idx;
2072 bio_iovec(bio)->bv_offset += nr_bytes;
2073 bio_iovec(bio)->bv_len -= nr_bytes;
2076 req->__data_len -= total_bytes;
2077 req->buffer = bio_data(req->bio);
2079 /* update sector only for requests with clear definition of sector */
2080 if (blk_fs_request(req) || blk_discard_rq(req))
2081 req->__sector += total_bytes >> 9;
2083 /* mixed attributes always follow the first bio */
2084 if (req->cmd_flags & REQ_MIXED_MERGE) {
2085 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2086 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2090 * If total number of sectors is less than the first segment
2091 * size, something has gone terribly wrong.
2093 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2094 printk(KERN_ERR "blk: request botched\n");
2095 req->__data_len = blk_rq_cur_bytes(req);
2098 /* recalculate the number of segments */
2099 blk_recalc_rq_segments(req);
2101 return true;
2103 EXPORT_SYMBOL_GPL(blk_update_request);
2105 static bool blk_update_bidi_request(struct request *rq, int error,
2106 unsigned int nr_bytes,
2107 unsigned int bidi_bytes)
2109 if (blk_update_request(rq, error, nr_bytes))
2110 return true;
2112 /* Bidi request must be completed as a whole */
2113 if (unlikely(blk_bidi_rq(rq)) &&
2114 blk_update_request(rq->next_rq, error, bidi_bytes))
2115 return true;
2117 add_disk_randomness(rq->rq_disk);
2119 return false;
2123 * queue lock must be held
2125 static void blk_finish_request(struct request *req, int error)
2127 if (blk_rq_tagged(req))
2128 blk_queue_end_tag(req->q, req);
2130 BUG_ON(blk_queued_rq(req));
2132 if (unlikely(laptop_mode) && blk_fs_request(req))
2133 laptop_io_completion(&req->q->backing_dev_info);
2135 blk_delete_timer(req);
2137 blk_account_io_done(req);
2139 if (req->end_io)
2140 req->end_io(req, error);
2141 else {
2142 if (blk_bidi_rq(req))
2143 __blk_put_request(req->next_rq->q, req->next_rq);
2145 __blk_put_request(req->q, req);
2150 * blk_end_bidi_request - Complete a bidi request
2151 * @rq: the request to complete
2152 * @error: %0 for success, < %0 for error
2153 * @nr_bytes: number of bytes to complete @rq
2154 * @bidi_bytes: number of bytes to complete @rq->next_rq
2156 * Description:
2157 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2158 * Drivers that supports bidi can safely call this member for any
2159 * type of request, bidi or uni. In the later case @bidi_bytes is
2160 * just ignored.
2162 * Return:
2163 * %false - we are done with this request
2164 * %true - still buffers pending for this request
2166 static bool blk_end_bidi_request(struct request *rq, int error,
2167 unsigned int nr_bytes, unsigned int bidi_bytes)
2169 struct request_queue *q = rq->q;
2170 unsigned long flags;
2172 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2173 return true;
2175 spin_lock_irqsave(q->queue_lock, flags);
2176 blk_finish_request(rq, error);
2177 spin_unlock_irqrestore(q->queue_lock, flags);
2179 return false;
2183 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2184 * @rq: the request to complete
2185 * @error: %0 for success, < %0 for error
2186 * @nr_bytes: number of bytes to complete @rq
2187 * @bidi_bytes: number of bytes to complete @rq->next_rq
2189 * Description:
2190 * Identical to blk_end_bidi_request() except that queue lock is
2191 * assumed to be locked on entry and remains so on return.
2193 * Return:
2194 * %false - we are done with this request
2195 * %true - still buffers pending for this request
2197 static bool __blk_end_bidi_request(struct request *rq, int error,
2198 unsigned int nr_bytes, unsigned int bidi_bytes)
2200 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2201 return true;
2203 blk_finish_request(rq, error);
2205 return false;
2209 * blk_end_request - Helper function for drivers to complete the request.
2210 * @rq: the request being processed
2211 * @error: %0 for success, < %0 for error
2212 * @nr_bytes: number of bytes to complete
2214 * Description:
2215 * Ends I/O on a number of bytes attached to @rq.
2216 * If @rq has leftover, sets it up for the next range of segments.
2218 * Return:
2219 * %false - we are done with this request
2220 * %true - still buffers pending for this request
2222 bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2224 return blk_end_bidi_request(rq, error, nr_bytes, 0);
2226 EXPORT_SYMBOL(blk_end_request);
2229 * blk_end_request_all - Helper function for drives to finish the request.
2230 * @rq: the request to finish
2231 * @error: %0 for success, < %0 for error
2233 * Description:
2234 * Completely finish @rq.
2236 void blk_end_request_all(struct request *rq, int error)
2238 bool pending;
2239 unsigned int bidi_bytes = 0;
2241 if (unlikely(blk_bidi_rq(rq)))
2242 bidi_bytes = blk_rq_bytes(rq->next_rq);
2244 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2245 BUG_ON(pending);
2247 EXPORT_SYMBOL(blk_end_request_all);
2250 * blk_end_request_cur - Helper function to finish the current request chunk.
2251 * @rq: the request to finish the current chunk for
2252 * @error: %0 for success, < %0 for error
2254 * Description:
2255 * Complete the current consecutively mapped chunk from @rq.
2257 * Return:
2258 * %false - we are done with this request
2259 * %true - still buffers pending for this request
2261 bool blk_end_request_cur(struct request *rq, int error)
2263 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2265 EXPORT_SYMBOL(blk_end_request_cur);
2268 * blk_end_request_err - Finish a request till the next failure boundary.
2269 * @rq: the request to finish till the next failure boundary for
2270 * @error: must be negative errno
2272 * Description:
2273 * Complete @rq till the next failure boundary.
2275 * Return:
2276 * %false - we are done with this request
2277 * %true - still buffers pending for this request
2279 bool blk_end_request_err(struct request *rq, int error)
2281 WARN_ON(error >= 0);
2282 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2284 EXPORT_SYMBOL_GPL(blk_end_request_err);
2287 * __blk_end_request - Helper function for drivers to complete the request.
2288 * @rq: the request being processed
2289 * @error: %0 for success, < %0 for error
2290 * @nr_bytes: number of bytes to complete
2292 * Description:
2293 * Must be called with queue lock held unlike blk_end_request().
2295 * Return:
2296 * %false - we are done with this request
2297 * %true - still buffers pending for this request
2299 bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2301 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
2303 EXPORT_SYMBOL(__blk_end_request);
2306 * __blk_end_request_all - Helper function for drives to finish the request.
2307 * @rq: the request to finish
2308 * @error: %0 for success, < %0 for error
2310 * Description:
2311 * Completely finish @rq. Must be called with queue lock held.
2313 void __blk_end_request_all(struct request *rq, int error)
2315 bool pending;
2316 unsigned int bidi_bytes = 0;
2318 if (unlikely(blk_bidi_rq(rq)))
2319 bidi_bytes = blk_rq_bytes(rq->next_rq);
2321 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2322 BUG_ON(pending);
2324 EXPORT_SYMBOL(__blk_end_request_all);
2327 * __blk_end_request_cur - Helper function to finish the current request chunk.
2328 * @rq: the request to finish the current chunk for
2329 * @error: %0 for success, < %0 for error
2331 * Description:
2332 * Complete the current consecutively mapped chunk from @rq. Must
2333 * be called with queue lock held.
2335 * Return:
2336 * %false - we are done with this request
2337 * %true - still buffers pending for this request
2339 bool __blk_end_request_cur(struct request *rq, int error)
2341 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2343 EXPORT_SYMBOL(__blk_end_request_cur);
2346 * __blk_end_request_err - Finish a request till the next failure boundary.
2347 * @rq: the request to finish till the next failure boundary for
2348 * @error: must be negative errno
2350 * Description:
2351 * Complete @rq till the next failure boundary. Must be called
2352 * with queue lock held.
2354 * Return:
2355 * %false - we are done with this request
2356 * %true - still buffers pending for this request
2358 bool __blk_end_request_err(struct request *rq, int error)
2360 WARN_ON(error >= 0);
2361 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2363 EXPORT_SYMBOL_GPL(__blk_end_request_err);
2365 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2366 struct bio *bio)
2368 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
2369 rq->cmd_flags |= bio->bi_rw & REQ_RW;
2371 if (bio_has_data(bio)) {
2372 rq->nr_phys_segments = bio_phys_segments(q, bio);
2373 rq->buffer = bio_data(bio);
2375 rq->__data_len = bio->bi_size;
2376 rq->bio = rq->biotail = bio;
2378 if (bio->bi_bdev)
2379 rq->rq_disk = bio->bi_bdev->bd_disk;
2382 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2384 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2385 * @rq: the request to be flushed
2387 * Description:
2388 * Flush all pages in @rq.
2390 void rq_flush_dcache_pages(struct request *rq)
2392 struct req_iterator iter;
2393 struct bio_vec *bvec;
2395 rq_for_each_segment(bvec, rq, iter)
2396 flush_dcache_page(bvec->bv_page);
2398 EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2399 #endif
2402 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2403 * @q : the queue of the device being checked
2405 * Description:
2406 * Check if underlying low-level drivers of a device are busy.
2407 * If the drivers want to export their busy state, they must set own
2408 * exporting function using blk_queue_lld_busy() first.
2410 * Basically, this function is used only by request stacking drivers
2411 * to stop dispatching requests to underlying devices when underlying
2412 * devices are busy. This behavior helps more I/O merging on the queue
2413 * of the request stacking driver and prevents I/O throughput regression
2414 * on burst I/O load.
2416 * Return:
2417 * 0 - Not busy (The request stacking driver should dispatch request)
2418 * 1 - Busy (The request stacking driver should stop dispatching request)
2420 int blk_lld_busy(struct request_queue *q)
2422 if (q->lld_busy_fn)
2423 return q->lld_busy_fn(q);
2425 return 0;
2427 EXPORT_SYMBOL_GPL(blk_lld_busy);
2430 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2431 * @rq: the clone request to be cleaned up
2433 * Description:
2434 * Free all bios in @rq for a cloned request.
2436 void blk_rq_unprep_clone(struct request *rq)
2438 struct bio *bio;
2440 while ((bio = rq->bio) != NULL) {
2441 rq->bio = bio->bi_next;
2443 bio_put(bio);
2446 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2449 * Copy attributes of the original request to the clone request.
2450 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2452 static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2454 dst->cpu = src->cpu;
2455 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2456 dst->cmd_type = src->cmd_type;
2457 dst->__sector = blk_rq_pos(src);
2458 dst->__data_len = blk_rq_bytes(src);
2459 dst->nr_phys_segments = src->nr_phys_segments;
2460 dst->ioprio = src->ioprio;
2461 dst->extra_len = src->extra_len;
2465 * blk_rq_prep_clone - Helper function to setup clone request
2466 * @rq: the request to be setup
2467 * @rq_src: original request to be cloned
2468 * @bs: bio_set that bios for clone are allocated from
2469 * @gfp_mask: memory allocation mask for bio
2470 * @bio_ctr: setup function to be called for each clone bio.
2471 * Returns %0 for success, non %0 for failure.
2472 * @data: private data to be passed to @bio_ctr
2474 * Description:
2475 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2476 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2477 * are not copied, and copying such parts is the caller's responsibility.
2478 * Also, pages which the original bios are pointing to are not copied
2479 * and the cloned bios just point same pages.
2480 * So cloned bios must be completed before original bios, which means
2481 * the caller must complete @rq before @rq_src.
2483 int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2484 struct bio_set *bs, gfp_t gfp_mask,
2485 int (*bio_ctr)(struct bio *, struct bio *, void *),
2486 void *data)
2488 struct bio *bio, *bio_src;
2490 if (!bs)
2491 bs = fs_bio_set;
2493 blk_rq_init(NULL, rq);
2495 __rq_for_each_bio(bio_src, rq_src) {
2496 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2497 if (!bio)
2498 goto free_and_out;
2500 __bio_clone(bio, bio_src);
2502 if (bio_integrity(bio_src) &&
2503 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
2504 goto free_and_out;
2506 if (bio_ctr && bio_ctr(bio, bio_src, data))
2507 goto free_and_out;
2509 if (rq->bio) {
2510 rq->biotail->bi_next = bio;
2511 rq->biotail = bio;
2512 } else
2513 rq->bio = rq->biotail = bio;
2516 __blk_rq_prep_clone(rq, rq_src);
2518 return 0;
2520 free_and_out:
2521 if (bio)
2522 bio_free(bio, bs);
2523 blk_rq_unprep_clone(rq);
2525 return -ENOMEM;
2527 EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2529 int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
2531 return queue_work(kblockd_workqueue, work);
2533 EXPORT_SYMBOL(kblockd_schedule_work);
2535 int __init blk_dev_init(void)
2537 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2538 sizeof(((struct request *)0)->cmd_flags));
2540 kblockd_workqueue = create_workqueue("kblockd");
2541 if (!kblockd_workqueue)
2542 panic("Failed to create kblockd\n");
2544 request_cachep = kmem_cache_create("blkdev_requests",
2545 sizeof(struct request), 0, SLAB_PANIC, NULL);
2547 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2548 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
2550 return 0;