sched: Fix SD_POWERSAVING_BALANCE|SD_PREFER_LOCAL vs SD_WAKE_AFFINE
[linux-2.6/kvm.git] / block / blk-core.c
blobe695634882a61200c50a3ee9dc5be650abfbc632
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_bio_complete);
39 static int __make_request(struct request_queue *q, struct bio *bio);
42 * For the allocated request tables
44 static struct kmem_cache *request_cachep;
47 * For queue allocation
49 struct kmem_cache *blk_requestq_cachep;
52 * Controlling structure to kblockd
54 static struct workqueue_struct *kblockd_workqueue;
56 static void drive_stat_acct(struct request *rq, int new_io)
58 struct hd_struct *part;
59 int rw = rq_data_dir(rq);
60 int cpu;
62 if (!blk_do_io_stat(rq))
63 return;
65 cpu = part_stat_lock();
66 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
68 if (!new_io)
69 part_stat_inc(cpu, part, merges[rw]);
70 else {
71 part_round_stats(cpu, part);
72 part_inc_in_flight(part);
75 part_stat_unlock();
78 void blk_queue_congestion_threshold(struct request_queue *q)
80 int nr;
82 nr = q->nr_requests - (q->nr_requests / 8) + 1;
83 if (nr > q->nr_requests)
84 nr = q->nr_requests;
85 q->nr_congestion_on = nr;
87 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
88 if (nr < 1)
89 nr = 1;
90 q->nr_congestion_off = nr;
93 /**
94 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
95 * @bdev: device
97 * Locates the passed device's request queue and returns the address of its
98 * backing_dev_info
100 * Will return NULL if the request queue cannot be located.
102 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
104 struct backing_dev_info *ret = NULL;
105 struct request_queue *q = bdev_get_queue(bdev);
107 if (q)
108 ret = &q->backing_dev_info;
109 return ret;
111 EXPORT_SYMBOL(blk_get_backing_dev_info);
113 void blk_rq_init(struct request_queue *q, struct request *rq)
115 memset(rq, 0, sizeof(*rq));
117 INIT_LIST_HEAD(&rq->queuelist);
118 INIT_LIST_HEAD(&rq->timeout_list);
119 rq->cpu = -1;
120 rq->q = q;
121 rq->__sector = (sector_t) -1;
122 INIT_HLIST_NODE(&rq->hash);
123 RB_CLEAR_NODE(&rq->rb_node);
124 rq->cmd = rq->__cmd;
125 rq->cmd_len = BLK_MAX_CDB;
126 rq->tag = -1;
127 rq->ref_count = 1;
128 rq->start_time = jiffies;
130 EXPORT_SYMBOL(blk_rq_init);
132 static void req_bio_endio(struct request *rq, struct bio *bio,
133 unsigned int nbytes, int error)
135 struct request_queue *q = rq->q;
137 if (&q->bar_rq != rq) {
138 if (error)
139 clear_bit(BIO_UPTODATE, &bio->bi_flags);
140 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
141 error = -EIO;
143 if (unlikely(nbytes > bio->bi_size)) {
144 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
145 __func__, nbytes, bio->bi_size);
146 nbytes = bio->bi_size;
149 if (unlikely(rq->cmd_flags & REQ_QUIET))
150 set_bit(BIO_QUIET, &bio->bi_flags);
152 bio->bi_size -= nbytes;
153 bio->bi_sector += (nbytes >> 9);
155 if (bio_integrity(bio))
156 bio_integrity_advance(bio, nbytes);
158 if (bio->bi_size == 0)
159 bio_endio(bio, error);
160 } else {
163 * Okay, this is the barrier request in progress, just
164 * record the error;
166 if (error && !q->orderr)
167 q->orderr = error;
171 void blk_dump_rq_flags(struct request *rq, char *msg)
173 int bit;
175 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
176 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
177 rq->cmd_flags);
179 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
180 (unsigned long long)blk_rq_pos(rq),
181 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
182 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
183 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
185 if (blk_pc_request(rq)) {
186 printk(KERN_INFO " cdb: ");
187 for (bit = 0; bit < BLK_MAX_CDB; bit++)
188 printk("%02x ", rq->cmd[bit]);
189 printk("\n");
192 EXPORT_SYMBOL(blk_dump_rq_flags);
195 * "plug" the device if there are no outstanding requests: this will
196 * force the transfer to start only after we have put all the requests
197 * on the list.
199 * This is called with interrupts off and no requests on the queue and
200 * with the queue lock held.
202 void blk_plug_device(struct request_queue *q)
204 WARN_ON(!irqs_disabled());
207 * don't plug a stopped queue, it must be paired with blk_start_queue()
208 * which will restart the queueing
210 if (blk_queue_stopped(q))
211 return;
213 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
214 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
215 trace_block_plug(q);
218 EXPORT_SYMBOL(blk_plug_device);
221 * blk_plug_device_unlocked - plug a device without queue lock held
222 * @q: The &struct request_queue to plug
224 * Description:
225 * Like @blk_plug_device(), but grabs the queue lock and disables
226 * interrupts.
228 void blk_plug_device_unlocked(struct request_queue *q)
230 unsigned long flags;
232 spin_lock_irqsave(q->queue_lock, flags);
233 blk_plug_device(q);
234 spin_unlock_irqrestore(q->queue_lock, flags);
236 EXPORT_SYMBOL(blk_plug_device_unlocked);
239 * remove the queue from the plugged list, if present. called with
240 * queue lock held and interrupts disabled.
242 int blk_remove_plug(struct request_queue *q)
244 WARN_ON(!irqs_disabled());
246 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
247 return 0;
249 del_timer(&q->unplug_timer);
250 return 1;
252 EXPORT_SYMBOL(blk_remove_plug);
255 * remove the plug and let it rip..
257 void __generic_unplug_device(struct request_queue *q)
259 if (unlikely(blk_queue_stopped(q)))
260 return;
261 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
262 return;
264 q->request_fn(q);
268 * generic_unplug_device - fire a request queue
269 * @q: The &struct request_queue in question
271 * Description:
272 * Linux uses plugging to build bigger requests queues before letting
273 * the device have at them. If a queue is plugged, the I/O scheduler
274 * is still adding and merging requests on the queue. Once the queue
275 * gets unplugged, the request_fn defined for the queue is invoked and
276 * transfers started.
278 void generic_unplug_device(struct request_queue *q)
280 if (blk_queue_plugged(q)) {
281 spin_lock_irq(q->queue_lock);
282 __generic_unplug_device(q);
283 spin_unlock_irq(q->queue_lock);
286 EXPORT_SYMBOL(generic_unplug_device);
288 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
289 struct page *page)
291 struct request_queue *q = bdi->unplug_io_data;
293 blk_unplug(q);
296 void blk_unplug_work(struct work_struct *work)
298 struct request_queue *q =
299 container_of(work, struct request_queue, unplug_work);
301 trace_block_unplug_io(q);
302 q->unplug_fn(q);
305 void blk_unplug_timeout(unsigned long data)
307 struct request_queue *q = (struct request_queue *)data;
309 trace_block_unplug_timer(q);
310 kblockd_schedule_work(q, &q->unplug_work);
313 void blk_unplug(struct request_queue *q)
316 * devices don't necessarily have an ->unplug_fn defined
318 if (q->unplug_fn) {
319 trace_block_unplug_io(q);
320 q->unplug_fn(q);
323 EXPORT_SYMBOL(blk_unplug);
326 * blk_start_queue - restart a previously stopped queue
327 * @q: The &struct request_queue in question
329 * Description:
330 * blk_start_queue() will clear the stop flag on the queue, and call
331 * the request_fn for the queue if it was in a stopped state when
332 * entered. Also see blk_stop_queue(). Queue lock must be held.
334 void blk_start_queue(struct request_queue *q)
336 WARN_ON(!irqs_disabled());
338 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
339 __blk_run_queue(q);
341 EXPORT_SYMBOL(blk_start_queue);
344 * blk_stop_queue - stop a queue
345 * @q: The &struct request_queue in question
347 * Description:
348 * The Linux block layer assumes that a block driver will consume all
349 * entries on the request queue when the request_fn strategy is called.
350 * Often this will not happen, because of hardware limitations (queue
351 * depth settings). If a device driver gets a 'queue full' response,
352 * or if it simply chooses not to queue more I/O at one point, it can
353 * call this function to prevent the request_fn from being called until
354 * the driver has signalled it's ready to go again. This happens by calling
355 * blk_start_queue() to restart queue operations. Queue lock must be held.
357 void blk_stop_queue(struct request_queue *q)
359 blk_remove_plug(q);
360 queue_flag_set(QUEUE_FLAG_STOPPED, q);
362 EXPORT_SYMBOL(blk_stop_queue);
365 * blk_sync_queue - cancel any pending callbacks on a queue
366 * @q: the queue
368 * Description:
369 * The block layer may perform asynchronous callback activity
370 * on a queue, such as calling the unplug function after a timeout.
371 * A block device may call blk_sync_queue to ensure that any
372 * such activity is cancelled, thus allowing it to release resources
373 * that the callbacks might use. The caller must already have made sure
374 * that its ->make_request_fn will not re-add plugging prior to calling
375 * this function.
378 void blk_sync_queue(struct request_queue *q)
380 del_timer_sync(&q->unplug_timer);
381 del_timer_sync(&q->timeout);
382 cancel_work_sync(&q->unplug_work);
384 EXPORT_SYMBOL(blk_sync_queue);
387 * __blk_run_queue - run a single device queue
388 * @q: The queue to run
390 * Description:
391 * See @blk_run_queue. This variant must be called with the queue lock
392 * held and interrupts disabled.
395 void __blk_run_queue(struct request_queue *q)
397 blk_remove_plug(q);
399 if (unlikely(blk_queue_stopped(q)))
400 return;
402 if (elv_queue_empty(q))
403 return;
406 * Only recurse once to avoid overrunning the stack, let the unplug
407 * handling reinvoke the handler shortly if we already got there.
409 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
410 q->request_fn(q);
411 queue_flag_clear(QUEUE_FLAG_REENTER, q);
412 } else {
413 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
414 kblockd_schedule_work(q, &q->unplug_work);
417 EXPORT_SYMBOL(__blk_run_queue);
420 * blk_run_queue - run a single device queue
421 * @q: The queue to run
423 * Description:
424 * Invoke request handling on this queue, if it has pending work to do.
425 * May be used to restart queueing when a request has completed.
427 void blk_run_queue(struct request_queue *q)
429 unsigned long flags;
431 spin_lock_irqsave(q->queue_lock, flags);
432 __blk_run_queue(q);
433 spin_unlock_irqrestore(q->queue_lock, flags);
435 EXPORT_SYMBOL(blk_run_queue);
437 void blk_put_queue(struct request_queue *q)
439 kobject_put(&q->kobj);
442 void blk_cleanup_queue(struct request_queue *q)
445 * We know we have process context here, so we can be a little
446 * cautious and ensure that pending block actions on this device
447 * are done before moving on. Going into this function, we should
448 * not have processes doing IO to this device.
450 blk_sync_queue(q);
452 mutex_lock(&q->sysfs_lock);
453 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
454 mutex_unlock(&q->sysfs_lock);
456 if (q->elevator)
457 elevator_exit(q->elevator);
459 blk_put_queue(q);
461 EXPORT_SYMBOL(blk_cleanup_queue);
463 static int blk_init_free_list(struct request_queue *q)
465 struct request_list *rl = &q->rq;
467 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
468 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
469 rl->elvpriv = 0;
470 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
471 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
473 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
474 mempool_free_slab, request_cachep, q->node);
476 if (!rl->rq_pool)
477 return -ENOMEM;
479 return 0;
482 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
484 return blk_alloc_queue_node(gfp_mask, -1);
486 EXPORT_SYMBOL(blk_alloc_queue);
488 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
490 struct request_queue *q;
491 int err;
493 q = kmem_cache_alloc_node(blk_requestq_cachep,
494 gfp_mask | __GFP_ZERO, node_id);
495 if (!q)
496 return NULL;
498 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
499 q->backing_dev_info.unplug_io_data = q;
500 q->backing_dev_info.ra_pages =
501 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
502 q->backing_dev_info.state = 0;
503 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
504 q->backing_dev_info.name = "block";
506 err = bdi_init(&q->backing_dev_info);
507 if (err) {
508 kmem_cache_free(blk_requestq_cachep, q);
509 return NULL;
512 init_timer(&q->unplug_timer);
513 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
514 INIT_LIST_HEAD(&q->timeout_list);
515 INIT_WORK(&q->unplug_work, blk_unplug_work);
517 kobject_init(&q->kobj, &blk_queue_ktype);
519 mutex_init(&q->sysfs_lock);
520 spin_lock_init(&q->__queue_lock);
522 return q;
524 EXPORT_SYMBOL(blk_alloc_queue_node);
527 * blk_init_queue - prepare a request queue for use with a block device
528 * @rfn: The function to be called to process requests that have been
529 * placed on the queue.
530 * @lock: Request queue spin lock
532 * Description:
533 * If a block device wishes to use the standard request handling procedures,
534 * which sorts requests and coalesces adjacent requests, then it must
535 * call blk_init_queue(). The function @rfn will be called when there
536 * are requests on the queue that need to be processed. If the device
537 * supports plugging, then @rfn may not be called immediately when requests
538 * are available on the queue, but may be called at some time later instead.
539 * Plugged queues are generally unplugged when a buffer belonging to one
540 * of the requests on the queue is needed, or due to memory pressure.
542 * @rfn is not required, or even expected, to remove all requests off the
543 * queue, but only as many as it can handle at a time. If it does leave
544 * requests on the queue, it is responsible for arranging that the requests
545 * get dealt with eventually.
547 * The queue spin lock must be held while manipulating the requests on the
548 * request queue; this lock will be taken also from interrupt context, so irq
549 * disabling is needed for it.
551 * Function returns a pointer to the initialized request queue, or %NULL if
552 * it didn't succeed.
554 * Note:
555 * blk_init_queue() must be paired with a blk_cleanup_queue() call
556 * when the block device is deactivated (such as at module unload).
559 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
561 return blk_init_queue_node(rfn, lock, -1);
563 EXPORT_SYMBOL(blk_init_queue);
565 struct request_queue *
566 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
568 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
570 if (!q)
571 return NULL;
573 q->node = node_id;
574 if (blk_init_free_list(q)) {
575 kmem_cache_free(blk_requestq_cachep, q);
576 return NULL;
579 q->request_fn = rfn;
580 q->prep_rq_fn = NULL;
581 q->unplug_fn = generic_unplug_device;
582 q->queue_flags = QUEUE_FLAG_DEFAULT;
583 q->queue_lock = lock;
586 * This also sets hw/phys segments, boundary and size
588 blk_queue_make_request(q, __make_request);
590 q->sg_reserved_size = INT_MAX;
593 * all done
595 if (!elevator_init(q, NULL)) {
596 blk_queue_congestion_threshold(q);
597 return q;
600 blk_put_queue(q);
601 return NULL;
603 EXPORT_SYMBOL(blk_init_queue_node);
605 int blk_get_queue(struct request_queue *q)
607 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
608 kobject_get(&q->kobj);
609 return 0;
612 return 1;
615 static inline void blk_free_request(struct request_queue *q, struct request *rq)
617 if (rq->cmd_flags & REQ_ELVPRIV)
618 elv_put_request(q, rq);
619 mempool_free(rq, q->rq.rq_pool);
622 static struct request *
623 blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
625 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
627 if (!rq)
628 return NULL;
630 blk_rq_init(q, rq);
632 rq->cmd_flags = flags | REQ_ALLOCED;
634 if (priv) {
635 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
636 mempool_free(rq, q->rq.rq_pool);
637 return NULL;
639 rq->cmd_flags |= REQ_ELVPRIV;
642 return rq;
646 * ioc_batching returns true if the ioc is a valid batching request and
647 * should be given priority access to a request.
649 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
651 if (!ioc)
652 return 0;
655 * Make sure the process is able to allocate at least 1 request
656 * even if the batch times out, otherwise we could theoretically
657 * lose wakeups.
659 return ioc->nr_batch_requests == q->nr_batching ||
660 (ioc->nr_batch_requests > 0
661 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
665 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
666 * will cause the process to be a "batcher" on all queues in the system. This
667 * is the behaviour we want though - once it gets a wakeup it should be given
668 * a nice run.
670 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
672 if (!ioc || ioc_batching(q, ioc))
673 return;
675 ioc->nr_batch_requests = q->nr_batching;
676 ioc->last_waited = jiffies;
679 static void __freed_request(struct request_queue *q, int sync)
681 struct request_list *rl = &q->rq;
683 if (rl->count[sync] < queue_congestion_off_threshold(q))
684 blk_clear_queue_congested(q, sync);
686 if (rl->count[sync] + 1 <= q->nr_requests) {
687 if (waitqueue_active(&rl->wait[sync]))
688 wake_up(&rl->wait[sync]);
690 blk_clear_queue_full(q, sync);
695 * A request has just been released. Account for it, update the full and
696 * congestion status, wake up any waiters. Called under q->queue_lock.
698 static void freed_request(struct request_queue *q, int sync, int priv)
700 struct request_list *rl = &q->rq;
702 rl->count[sync]--;
703 if (priv)
704 rl->elvpriv--;
706 __freed_request(q, sync);
708 if (unlikely(rl->starved[sync ^ 1]))
709 __freed_request(q, sync ^ 1);
713 * Get a free request, queue_lock must be held.
714 * Returns NULL on failure, with queue_lock held.
715 * Returns !NULL on success, with queue_lock *not held*.
717 static struct request *get_request(struct request_queue *q, int rw_flags,
718 struct bio *bio, gfp_t gfp_mask)
720 struct request *rq = NULL;
721 struct request_list *rl = &q->rq;
722 struct io_context *ioc = NULL;
723 const bool is_sync = rw_is_sync(rw_flags) != 0;
724 int may_queue, priv;
726 may_queue = elv_may_queue(q, rw_flags);
727 if (may_queue == ELV_MQUEUE_NO)
728 goto rq_starved;
730 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
731 if (rl->count[is_sync]+1 >= q->nr_requests) {
732 ioc = current_io_context(GFP_ATOMIC, q->node);
734 * The queue will fill after this allocation, so set
735 * it as full, and mark this process as "batching".
736 * This process will be allowed to complete a batch of
737 * requests, others will be blocked.
739 if (!blk_queue_full(q, is_sync)) {
740 ioc_set_batching(q, ioc);
741 blk_set_queue_full(q, is_sync);
742 } else {
743 if (may_queue != ELV_MQUEUE_MUST
744 && !ioc_batching(q, ioc)) {
746 * The queue is full and the allocating
747 * process is not a "batcher", and not
748 * exempted by the IO scheduler
750 goto out;
754 blk_set_queue_congested(q, is_sync);
758 * Only allow batching queuers to allocate up to 50% over the defined
759 * limit of requests, otherwise we could have thousands of requests
760 * allocated with any setting of ->nr_requests
762 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
763 goto out;
765 rl->count[is_sync]++;
766 rl->starved[is_sync] = 0;
768 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
769 if (priv)
770 rl->elvpriv++;
772 if (blk_queue_io_stat(q))
773 rw_flags |= REQ_IO_STAT;
774 spin_unlock_irq(q->queue_lock);
776 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
777 if (unlikely(!rq)) {
779 * Allocation failed presumably due to memory. Undo anything
780 * we might have messed up.
782 * Allocating task should really be put onto the front of the
783 * wait queue, but this is pretty rare.
785 spin_lock_irq(q->queue_lock);
786 freed_request(q, is_sync, priv);
789 * in the very unlikely event that allocation failed and no
790 * requests for this direction was pending, mark us starved
791 * so that freeing of a request in the other direction will
792 * notice us. another possible fix would be to split the
793 * rq mempool into READ and WRITE
795 rq_starved:
796 if (unlikely(rl->count[is_sync] == 0))
797 rl->starved[is_sync] = 1;
799 goto out;
803 * ioc may be NULL here, and ioc_batching will be false. That's
804 * OK, if the queue is under the request limit then requests need
805 * not count toward the nr_batch_requests limit. There will always
806 * be some limit enforced by BLK_BATCH_TIME.
808 if (ioc_batching(q, ioc))
809 ioc->nr_batch_requests--;
811 trace_block_getrq(q, bio, rw_flags & 1);
812 out:
813 return rq;
817 * No available requests for this queue, unplug the device and wait for some
818 * requests to become available.
820 * Called with q->queue_lock held, and returns with it unlocked.
822 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
823 struct bio *bio)
825 const bool is_sync = rw_is_sync(rw_flags) != 0;
826 struct request *rq;
828 rq = get_request(q, rw_flags, bio, GFP_NOIO);
829 while (!rq) {
830 DEFINE_WAIT(wait);
831 struct io_context *ioc;
832 struct request_list *rl = &q->rq;
834 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
835 TASK_UNINTERRUPTIBLE);
837 trace_block_sleeprq(q, bio, rw_flags & 1);
839 __generic_unplug_device(q);
840 spin_unlock_irq(q->queue_lock);
841 io_schedule();
844 * After sleeping, we become a "batching" process and
845 * will be able to allocate at least one request, and
846 * up to a big batch of them for a small period time.
847 * See ioc_batching, ioc_set_batching
849 ioc = current_io_context(GFP_NOIO, q->node);
850 ioc_set_batching(q, ioc);
852 spin_lock_irq(q->queue_lock);
853 finish_wait(&rl->wait[is_sync], &wait);
855 rq = get_request(q, rw_flags, bio, GFP_NOIO);
858 return rq;
861 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
863 struct request *rq;
865 BUG_ON(rw != READ && rw != WRITE);
867 spin_lock_irq(q->queue_lock);
868 if (gfp_mask & __GFP_WAIT) {
869 rq = get_request_wait(q, rw, NULL);
870 } else {
871 rq = get_request(q, rw, NULL, gfp_mask);
872 if (!rq)
873 spin_unlock_irq(q->queue_lock);
875 /* q->queue_lock is unlocked at this point */
877 return rq;
879 EXPORT_SYMBOL(blk_get_request);
882 * blk_make_request - given a bio, allocate a corresponding struct request.
883 * @q: target request queue
884 * @bio: The bio describing the memory mappings that will be submitted for IO.
885 * It may be a chained-bio properly constructed by block/bio layer.
886 * @gfp_mask: gfp flags to be used for memory allocation
888 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
889 * type commands. Where the struct request needs to be farther initialized by
890 * the caller. It is passed a &struct bio, which describes the memory info of
891 * the I/O transfer.
893 * The caller of blk_make_request must make sure that bi_io_vec
894 * are set to describe the memory buffers. That bio_data_dir() will return
895 * the needed direction of the request. (And all bio's in the passed bio-chain
896 * are properly set accordingly)
898 * If called under none-sleepable conditions, mapped bio buffers must not
899 * need bouncing, by calling the appropriate masked or flagged allocator,
900 * suitable for the target device. Otherwise the call to blk_queue_bounce will
901 * BUG.
903 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
904 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
905 * anything but the first bio in the chain. Otherwise you risk waiting for IO
906 * completion of a bio that hasn't been submitted yet, thus resulting in a
907 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
908 * of bio_alloc(), as that avoids the mempool deadlock.
909 * If possible a big IO should be split into smaller parts when allocation
910 * fails. Partial allocation should not be an error, or you risk a live-lock.
912 struct request *blk_make_request(struct request_queue *q, struct bio *bio,
913 gfp_t gfp_mask)
915 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
917 if (unlikely(!rq))
918 return ERR_PTR(-ENOMEM);
920 for_each_bio(bio) {
921 struct bio *bounce_bio = bio;
922 int ret;
924 blk_queue_bounce(q, &bounce_bio);
925 ret = blk_rq_append_bio(q, rq, bounce_bio);
926 if (unlikely(ret)) {
927 blk_put_request(rq);
928 return ERR_PTR(ret);
932 return rq;
934 EXPORT_SYMBOL(blk_make_request);
937 * blk_requeue_request - put a request back on queue
938 * @q: request queue where request should be inserted
939 * @rq: request to be inserted
941 * Description:
942 * Drivers often keep queueing requests until the hardware cannot accept
943 * more, when that condition happens we need to put the request back
944 * on the queue. Must be called with queue lock held.
946 void blk_requeue_request(struct request_queue *q, struct request *rq)
948 blk_delete_timer(rq);
949 blk_clear_rq_complete(rq);
950 trace_block_rq_requeue(q, rq);
952 if (blk_rq_tagged(rq))
953 blk_queue_end_tag(q, rq);
955 BUG_ON(blk_queued_rq(rq));
957 elv_requeue_request(q, rq);
959 EXPORT_SYMBOL(blk_requeue_request);
962 * blk_insert_request - insert a special request into a request queue
963 * @q: request queue where request should be inserted
964 * @rq: request to be inserted
965 * @at_head: insert request at head or tail of queue
966 * @data: private data
968 * Description:
969 * Many block devices need to execute commands asynchronously, so they don't
970 * block the whole kernel from preemption during request execution. This is
971 * accomplished normally by inserting aritficial requests tagged as
972 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
973 * be scheduled for actual execution by the request queue.
975 * We have the option of inserting the head or the tail of the queue.
976 * Typically we use the tail for new ioctls and so forth. We use the head
977 * of the queue for things like a QUEUE_FULL message from a device, or a
978 * host that is unable to accept a particular command.
980 void blk_insert_request(struct request_queue *q, struct request *rq,
981 int at_head, void *data)
983 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
984 unsigned long flags;
987 * tell I/O scheduler that this isn't a regular read/write (ie it
988 * must not attempt merges on this) and that it acts as a soft
989 * barrier
991 rq->cmd_type = REQ_TYPE_SPECIAL;
993 rq->special = data;
995 spin_lock_irqsave(q->queue_lock, flags);
998 * If command is tagged, release the tag
1000 if (blk_rq_tagged(rq))
1001 blk_queue_end_tag(q, rq);
1003 drive_stat_acct(rq, 1);
1004 __elv_add_request(q, rq, where, 0);
1005 __blk_run_queue(q);
1006 spin_unlock_irqrestore(q->queue_lock, flags);
1008 EXPORT_SYMBOL(blk_insert_request);
1011 * add-request adds a request to the linked list.
1012 * queue lock is held and interrupts disabled, as we muck with the
1013 * request queue list.
1015 static inline void add_request(struct request_queue *q, struct request *req)
1017 drive_stat_acct(req, 1);
1020 * elevator indicated where it wants this request to be
1021 * inserted at elevator_merge time
1023 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1026 static void part_round_stats_single(int cpu, struct hd_struct *part,
1027 unsigned long now)
1029 if (now == part->stamp)
1030 return;
1032 if (part->in_flight) {
1033 __part_stat_add(cpu, part, time_in_queue,
1034 part->in_flight * (now - part->stamp));
1035 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1037 part->stamp = now;
1041 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1042 * @cpu: cpu number for stats access
1043 * @part: target partition
1045 * The average IO queue length and utilisation statistics are maintained
1046 * by observing the current state of the queue length and the amount of
1047 * time it has been in this state for.
1049 * Normally, that accounting is done on IO completion, but that can result
1050 * in more than a second's worth of IO being accounted for within any one
1051 * second, leading to >100% utilisation. To deal with that, we call this
1052 * function to do a round-off before returning the results when reading
1053 * /proc/diskstats. This accounts immediately for all queue usage up to
1054 * the current jiffies and restarts the counters again.
1056 void part_round_stats(int cpu, struct hd_struct *part)
1058 unsigned long now = jiffies;
1060 if (part->partno)
1061 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1062 part_round_stats_single(cpu, part, now);
1064 EXPORT_SYMBOL_GPL(part_round_stats);
1067 * queue lock must be held
1069 void __blk_put_request(struct request_queue *q, struct request *req)
1071 if (unlikely(!q))
1072 return;
1073 if (unlikely(--req->ref_count))
1074 return;
1076 elv_completed_request(q, req);
1078 /* this is a bio leak */
1079 WARN_ON(req->bio != NULL);
1082 * Request may not have originated from ll_rw_blk. if not,
1083 * it didn't come out of our reserved rq pools
1085 if (req->cmd_flags & REQ_ALLOCED) {
1086 int is_sync = rq_is_sync(req) != 0;
1087 int priv = req->cmd_flags & REQ_ELVPRIV;
1089 BUG_ON(!list_empty(&req->queuelist));
1090 BUG_ON(!hlist_unhashed(&req->hash));
1092 blk_free_request(q, req);
1093 freed_request(q, is_sync, priv);
1096 EXPORT_SYMBOL_GPL(__blk_put_request);
1098 void blk_put_request(struct request *req)
1100 unsigned long flags;
1101 struct request_queue *q = req->q;
1103 spin_lock_irqsave(q->queue_lock, flags);
1104 __blk_put_request(q, req);
1105 spin_unlock_irqrestore(q->queue_lock, flags);
1107 EXPORT_SYMBOL(blk_put_request);
1109 void init_request_from_bio(struct request *req, struct bio *bio)
1111 req->cpu = bio->bi_comp_cpu;
1112 req->cmd_type = REQ_TYPE_FS;
1115 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1117 if (bio_rw_ahead(bio))
1118 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1119 REQ_FAILFAST_DRIVER);
1120 if (bio_failfast_dev(bio))
1121 req->cmd_flags |= REQ_FAILFAST_DEV;
1122 if (bio_failfast_transport(bio))
1123 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1124 if (bio_failfast_driver(bio))
1125 req->cmd_flags |= REQ_FAILFAST_DRIVER;
1127 if (unlikely(bio_discard(bio))) {
1128 req->cmd_flags |= REQ_DISCARD;
1129 if (bio_barrier(bio))
1130 req->cmd_flags |= REQ_SOFTBARRIER;
1131 req->q->prepare_discard_fn(req->q, req);
1132 } else if (unlikely(bio_barrier(bio)))
1133 req->cmd_flags |= REQ_HARDBARRIER;
1135 if (bio_sync(bio))
1136 req->cmd_flags |= REQ_RW_SYNC;
1137 if (bio_rw_meta(bio))
1138 req->cmd_flags |= REQ_RW_META;
1139 if (bio_noidle(bio))
1140 req->cmd_flags |= REQ_NOIDLE;
1142 req->errors = 0;
1143 req->__sector = bio->bi_sector;
1144 req->ioprio = bio_prio(bio);
1145 blk_rq_bio_prep(req->q, req, bio);
1149 * Only disabling plugging for non-rotational devices if it does tagging
1150 * as well, otherwise we do need the proper merging
1152 static inline bool queue_should_plug(struct request_queue *q)
1154 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1157 static int __make_request(struct request_queue *q, struct bio *bio)
1159 struct request *req;
1160 int el_ret;
1161 unsigned int bytes = bio->bi_size;
1162 const unsigned short prio = bio_prio(bio);
1163 const int sync = bio_sync(bio);
1164 const int unplug = bio_unplug(bio);
1165 int rw_flags;
1167 if (bio_barrier(bio) && bio_has_data(bio) &&
1168 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1169 bio_endio(bio, -EOPNOTSUPP);
1170 return 0;
1173 * low level driver can indicate that it wants pages above a
1174 * certain limit bounced to low memory (ie for highmem, or even
1175 * ISA dma in theory)
1177 blk_queue_bounce(q, &bio);
1179 spin_lock_irq(q->queue_lock);
1181 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
1182 goto get_rq;
1184 el_ret = elv_merge(q, &req, bio);
1185 switch (el_ret) {
1186 case ELEVATOR_BACK_MERGE:
1187 BUG_ON(!rq_mergeable(req));
1189 if (!ll_back_merge_fn(q, req, bio))
1190 break;
1192 trace_block_bio_backmerge(q, bio);
1194 req->biotail->bi_next = bio;
1195 req->biotail = bio;
1196 req->__data_len += bytes;
1197 req->ioprio = ioprio_best(req->ioprio, prio);
1198 if (!blk_rq_cpu_valid(req))
1199 req->cpu = bio->bi_comp_cpu;
1200 drive_stat_acct(req, 0);
1201 if (!attempt_back_merge(q, req))
1202 elv_merged_request(q, req, el_ret);
1203 goto out;
1205 case ELEVATOR_FRONT_MERGE:
1206 BUG_ON(!rq_mergeable(req));
1208 if (!ll_front_merge_fn(q, req, bio))
1209 break;
1211 trace_block_bio_frontmerge(q, bio);
1213 bio->bi_next = req->bio;
1214 req->bio = bio;
1217 * may not be valid. if the low level driver said
1218 * it didn't need a bounce buffer then it better
1219 * not touch req->buffer either...
1221 req->buffer = bio_data(bio);
1222 req->__sector = bio->bi_sector;
1223 req->__data_len += bytes;
1224 req->ioprio = ioprio_best(req->ioprio, prio);
1225 if (!blk_rq_cpu_valid(req))
1226 req->cpu = bio->bi_comp_cpu;
1227 drive_stat_acct(req, 0);
1228 if (!attempt_front_merge(q, req))
1229 elv_merged_request(q, req, el_ret);
1230 goto out;
1232 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1233 default:
1237 get_rq:
1239 * This sync check and mask will be re-done in init_request_from_bio(),
1240 * but we need to set it earlier to expose the sync flag to the
1241 * rq allocator and io schedulers.
1243 rw_flags = bio_data_dir(bio);
1244 if (sync)
1245 rw_flags |= REQ_RW_SYNC;
1248 * Grab a free request. This is might sleep but can not fail.
1249 * Returns with the queue unlocked.
1251 req = get_request_wait(q, rw_flags, bio);
1254 * After dropping the lock and possibly sleeping here, our request
1255 * may now be mergeable after it had proven unmergeable (above).
1256 * We don't worry about that case for efficiency. It won't happen
1257 * often, and the elevators are able to handle it.
1259 init_request_from_bio(req, bio);
1261 spin_lock_irq(q->queue_lock);
1262 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1263 bio_flagged(bio, BIO_CPU_AFFINE))
1264 req->cpu = blk_cpu_to_group(smp_processor_id());
1265 if (queue_should_plug(q) && elv_queue_empty(q))
1266 blk_plug_device(q);
1267 add_request(q, req);
1268 out:
1269 if (unplug || !queue_should_plug(q))
1270 __generic_unplug_device(q);
1271 spin_unlock_irq(q->queue_lock);
1272 return 0;
1276 * If bio->bi_dev is a partition, remap the location
1278 static inline void blk_partition_remap(struct bio *bio)
1280 struct block_device *bdev = bio->bi_bdev;
1282 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1283 struct hd_struct *p = bdev->bd_part;
1285 bio->bi_sector += p->start_sect;
1286 bio->bi_bdev = bdev->bd_contains;
1288 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
1289 bdev->bd_dev,
1290 bio->bi_sector - p->start_sect);
1294 static void handle_bad_sector(struct bio *bio)
1296 char b[BDEVNAME_SIZE];
1298 printk(KERN_INFO "attempt to access beyond end of device\n");
1299 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1300 bdevname(bio->bi_bdev, b),
1301 bio->bi_rw,
1302 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1303 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1305 set_bit(BIO_EOF, &bio->bi_flags);
1308 #ifdef CONFIG_FAIL_MAKE_REQUEST
1310 static DECLARE_FAULT_ATTR(fail_make_request);
1312 static int __init setup_fail_make_request(char *str)
1314 return setup_fault_attr(&fail_make_request, str);
1316 __setup("fail_make_request=", setup_fail_make_request);
1318 static int should_fail_request(struct bio *bio)
1320 struct hd_struct *part = bio->bi_bdev->bd_part;
1322 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
1323 return should_fail(&fail_make_request, bio->bi_size);
1325 return 0;
1328 static int __init fail_make_request_debugfs(void)
1330 return init_fault_attr_dentries(&fail_make_request,
1331 "fail_make_request");
1334 late_initcall(fail_make_request_debugfs);
1336 #else /* CONFIG_FAIL_MAKE_REQUEST */
1338 static inline int should_fail_request(struct bio *bio)
1340 return 0;
1343 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1346 * Check whether this bio extends beyond the end of the device.
1348 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1350 sector_t maxsector;
1352 if (!nr_sectors)
1353 return 0;
1355 /* Test device or partition size, when known. */
1356 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1357 if (maxsector) {
1358 sector_t sector = bio->bi_sector;
1360 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1362 * This may well happen - the kernel calls bread()
1363 * without checking the size of the device, e.g., when
1364 * mounting a device.
1366 handle_bad_sector(bio);
1367 return 1;
1371 return 0;
1375 * generic_make_request - hand a buffer to its device driver for I/O
1376 * @bio: The bio describing the location in memory and on the device.
1378 * generic_make_request() is used to make I/O requests of block
1379 * devices. It is passed a &struct bio, which describes the I/O that needs
1380 * to be done.
1382 * generic_make_request() does not return any status. The
1383 * success/failure status of the request, along with notification of
1384 * completion, is delivered asynchronously through the bio->bi_end_io
1385 * function described (one day) else where.
1387 * The caller of generic_make_request must make sure that bi_io_vec
1388 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1389 * set to describe the device address, and the
1390 * bi_end_io and optionally bi_private are set to describe how
1391 * completion notification should be signaled.
1393 * generic_make_request and the drivers it calls may use bi_next if this
1394 * bio happens to be merged with someone else, and may change bi_dev and
1395 * bi_sector for remaps as it sees fit. So the values of these fields
1396 * should NOT be depended on after the call to generic_make_request.
1398 static inline void __generic_make_request(struct bio *bio)
1400 struct request_queue *q;
1401 sector_t old_sector;
1402 int ret, nr_sectors = bio_sectors(bio);
1403 dev_t old_dev;
1404 int err = -EIO;
1406 might_sleep();
1408 if (bio_check_eod(bio, nr_sectors))
1409 goto end_io;
1412 * Resolve the mapping until finished. (drivers are
1413 * still free to implement/resolve their own stacking
1414 * by explicitly returning 0)
1416 * NOTE: we don't repeat the blk_size check for each new device.
1417 * Stacking drivers are expected to know what they are doing.
1419 old_sector = -1;
1420 old_dev = 0;
1421 do {
1422 char b[BDEVNAME_SIZE];
1424 q = bdev_get_queue(bio->bi_bdev);
1425 if (unlikely(!q)) {
1426 printk(KERN_ERR
1427 "generic_make_request: Trying to access "
1428 "nonexistent block-device %s (%Lu)\n",
1429 bdevname(bio->bi_bdev, b),
1430 (long long) bio->bi_sector);
1431 goto end_io;
1434 if (unlikely(nr_sectors > queue_max_hw_sectors(q))) {
1435 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1436 bdevname(bio->bi_bdev, b),
1437 bio_sectors(bio),
1438 queue_max_hw_sectors(q));
1439 goto end_io;
1442 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1443 goto end_io;
1445 if (should_fail_request(bio))
1446 goto end_io;
1449 * If this device has partitions, remap block n
1450 * of partition p to block n+start(p) of the disk.
1452 blk_partition_remap(bio);
1454 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1455 goto end_io;
1457 if (old_sector != -1)
1458 trace_block_remap(q, bio, old_dev, old_sector);
1460 trace_block_bio_queue(q, bio);
1462 old_sector = bio->bi_sector;
1463 old_dev = bio->bi_bdev->bd_dev;
1465 if (bio_check_eod(bio, nr_sectors))
1466 goto end_io;
1468 if (bio_discard(bio) && !q->prepare_discard_fn) {
1469 err = -EOPNOTSUPP;
1470 goto end_io;
1473 ret = q->make_request_fn(q, bio);
1474 } while (ret);
1476 return;
1478 end_io:
1479 bio_endio(bio, err);
1483 * We only want one ->make_request_fn to be active at a time,
1484 * else stack usage with stacked devices could be a problem.
1485 * So use current->bio_{list,tail} to keep a list of requests
1486 * submited by a make_request_fn function.
1487 * current->bio_tail is also used as a flag to say if
1488 * generic_make_request is currently active in this task or not.
1489 * If it is NULL, then no make_request is active. If it is non-NULL,
1490 * then a make_request is active, and new requests should be added
1491 * at the tail
1493 void generic_make_request(struct bio *bio)
1495 if (current->bio_tail) {
1496 /* make_request is active */
1497 *(current->bio_tail) = bio;
1498 bio->bi_next = NULL;
1499 current->bio_tail = &bio->bi_next;
1500 return;
1502 /* following loop may be a bit non-obvious, and so deserves some
1503 * explanation.
1504 * Before entering the loop, bio->bi_next is NULL (as all callers
1505 * ensure that) so we have a list with a single bio.
1506 * We pretend that we have just taken it off a longer list, so
1507 * we assign bio_list to the next (which is NULL) and bio_tail
1508 * to &bio_list, thus initialising the bio_list of new bios to be
1509 * added. __generic_make_request may indeed add some more bios
1510 * through a recursive call to generic_make_request. If it
1511 * did, we find a non-NULL value in bio_list and re-enter the loop
1512 * from the top. In this case we really did just take the bio
1513 * of the top of the list (no pretending) and so fixup bio_list and
1514 * bio_tail or bi_next, and call into __generic_make_request again.
1516 * The loop was structured like this to make only one call to
1517 * __generic_make_request (which is important as it is large and
1518 * inlined) and to keep the structure simple.
1520 BUG_ON(bio->bi_next);
1521 do {
1522 current->bio_list = bio->bi_next;
1523 if (bio->bi_next == NULL)
1524 current->bio_tail = &current->bio_list;
1525 else
1526 bio->bi_next = NULL;
1527 __generic_make_request(bio);
1528 bio = current->bio_list;
1529 } while (bio);
1530 current->bio_tail = NULL; /* deactivate */
1532 EXPORT_SYMBOL(generic_make_request);
1535 * submit_bio - submit a bio to the block device layer for I/O
1536 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1537 * @bio: The &struct bio which describes the I/O
1539 * submit_bio() is very similar in purpose to generic_make_request(), and
1540 * uses that function to do most of the work. Both are fairly rough
1541 * interfaces; @bio must be presetup and ready for I/O.
1544 void submit_bio(int rw, struct bio *bio)
1546 int count = bio_sectors(bio);
1548 bio->bi_rw |= rw;
1551 * If it's a regular read/write or a barrier with data attached,
1552 * go through the normal accounting stuff before submission.
1554 if (bio_has_data(bio)) {
1555 if (rw & WRITE) {
1556 count_vm_events(PGPGOUT, count);
1557 } else {
1558 task_io_account_read(bio->bi_size);
1559 count_vm_events(PGPGIN, count);
1562 if (unlikely(block_dump)) {
1563 char b[BDEVNAME_SIZE];
1564 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
1565 current->comm, task_pid_nr(current),
1566 (rw & WRITE) ? "WRITE" : "READ",
1567 (unsigned long long)bio->bi_sector,
1568 bdevname(bio->bi_bdev, b));
1572 generic_make_request(bio);
1574 EXPORT_SYMBOL(submit_bio);
1577 * blk_rq_check_limits - Helper function to check a request for the queue limit
1578 * @q: the queue
1579 * @rq: the request being checked
1581 * Description:
1582 * @rq may have been made based on weaker limitations of upper-level queues
1583 * in request stacking drivers, and it may violate the limitation of @q.
1584 * Since the block layer and the underlying device driver trust @rq
1585 * after it is inserted to @q, it should be checked against @q before
1586 * the insertion using this generic function.
1588 * This function should also be useful for request stacking drivers
1589 * in some cases below, so export this fuction.
1590 * Request stacking drivers like request-based dm may change the queue
1591 * limits while requests are in the queue (e.g. dm's table swapping).
1592 * Such request stacking drivers should check those requests agaist
1593 * the new queue limits again when they dispatch those requests,
1594 * although such checkings are also done against the old queue limits
1595 * when submitting requests.
1597 int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1599 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1600 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
1601 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1602 return -EIO;
1606 * queue's settings related to segment counting like q->bounce_pfn
1607 * may differ from that of other stacking queues.
1608 * Recalculate it to check the request correctly on this queue's
1609 * limitation.
1611 blk_recalc_rq_segments(rq);
1612 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1613 rq->nr_phys_segments > queue_max_hw_segments(q)) {
1614 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1615 return -EIO;
1618 return 0;
1620 EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1623 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1624 * @q: the queue to submit the request
1625 * @rq: the request being queued
1627 int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1629 unsigned long flags;
1631 if (blk_rq_check_limits(q, rq))
1632 return -EIO;
1634 #ifdef CONFIG_FAIL_MAKE_REQUEST
1635 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1636 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1637 return -EIO;
1638 #endif
1640 spin_lock_irqsave(q->queue_lock, flags);
1643 * Submitting request must be dequeued before calling this function
1644 * because it will be linked to another request_queue
1646 BUG_ON(blk_queued_rq(rq));
1648 drive_stat_acct(rq, 1);
1649 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1651 spin_unlock_irqrestore(q->queue_lock, flags);
1653 return 0;
1655 EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1657 static void blk_account_io_completion(struct request *req, unsigned int bytes)
1659 if (blk_do_io_stat(req)) {
1660 const int rw = rq_data_dir(req);
1661 struct hd_struct *part;
1662 int cpu;
1664 cpu = part_stat_lock();
1665 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1666 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1667 part_stat_unlock();
1671 static void blk_account_io_done(struct request *req)
1674 * Account IO completion. bar_rq isn't accounted as a normal
1675 * IO on queueing nor completion. Accounting the containing
1676 * request is enough.
1678 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
1679 unsigned long duration = jiffies - req->start_time;
1680 const int rw = rq_data_dir(req);
1681 struct hd_struct *part;
1682 int cpu;
1684 cpu = part_stat_lock();
1685 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1687 part_stat_inc(cpu, part, ios[rw]);
1688 part_stat_add(cpu, part, ticks[rw], duration);
1689 part_round_stats(cpu, part);
1690 part_dec_in_flight(part);
1692 part_stat_unlock();
1697 * blk_peek_request - peek at the top of a request queue
1698 * @q: request queue to peek at
1700 * Description:
1701 * Return the request at the top of @q. The returned request
1702 * should be started using blk_start_request() before LLD starts
1703 * processing it.
1705 * Return:
1706 * Pointer to the request at the top of @q if available. Null
1707 * otherwise.
1709 * Context:
1710 * queue_lock must be held.
1712 struct request *blk_peek_request(struct request_queue *q)
1714 struct request *rq;
1715 int ret;
1717 while ((rq = __elv_next_request(q)) != NULL) {
1718 if (!(rq->cmd_flags & REQ_STARTED)) {
1720 * This is the first time the device driver
1721 * sees this request (possibly after
1722 * requeueing). Notify IO scheduler.
1724 if (blk_sorted_rq(rq))
1725 elv_activate_rq(q, rq);
1728 * just mark as started even if we don't start
1729 * it, a request that has been delayed should
1730 * not be passed by new incoming requests
1732 rq->cmd_flags |= REQ_STARTED;
1733 trace_block_rq_issue(q, rq);
1736 if (!q->boundary_rq || q->boundary_rq == rq) {
1737 q->end_sector = rq_end_sector(rq);
1738 q->boundary_rq = NULL;
1741 if (rq->cmd_flags & REQ_DONTPREP)
1742 break;
1744 if (q->dma_drain_size && blk_rq_bytes(rq)) {
1746 * make sure space for the drain appears we
1747 * know we can do this because max_hw_segments
1748 * has been adjusted to be one fewer than the
1749 * device can handle
1751 rq->nr_phys_segments++;
1754 if (!q->prep_rq_fn)
1755 break;
1757 ret = q->prep_rq_fn(q, rq);
1758 if (ret == BLKPREP_OK) {
1759 break;
1760 } else if (ret == BLKPREP_DEFER) {
1762 * the request may have been (partially) prepped.
1763 * we need to keep this request in the front to
1764 * avoid resource deadlock. REQ_STARTED will
1765 * prevent other fs requests from passing this one.
1767 if (q->dma_drain_size && blk_rq_bytes(rq) &&
1768 !(rq->cmd_flags & REQ_DONTPREP)) {
1770 * remove the space for the drain we added
1771 * so that we don't add it again
1773 --rq->nr_phys_segments;
1776 rq = NULL;
1777 break;
1778 } else if (ret == BLKPREP_KILL) {
1779 rq->cmd_flags |= REQ_QUIET;
1781 * Mark this request as started so we don't trigger
1782 * any debug logic in the end I/O path.
1784 blk_start_request(rq);
1785 __blk_end_request_all(rq, -EIO);
1786 } else {
1787 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1788 break;
1792 return rq;
1794 EXPORT_SYMBOL(blk_peek_request);
1796 void blk_dequeue_request(struct request *rq)
1798 struct request_queue *q = rq->q;
1800 BUG_ON(list_empty(&rq->queuelist));
1801 BUG_ON(ELV_ON_HASH(rq));
1803 list_del_init(&rq->queuelist);
1806 * the time frame between a request being removed from the lists
1807 * and to it is freed is accounted as io that is in progress at
1808 * the driver side.
1810 if (blk_account_rq(rq))
1811 q->in_flight[rq_is_sync(rq)]++;
1815 * blk_start_request - start request processing on the driver
1816 * @req: request to dequeue
1818 * Description:
1819 * Dequeue @req and start timeout timer on it. This hands off the
1820 * request to the driver.
1822 * Block internal functions which don't want to start timer should
1823 * call blk_dequeue_request().
1825 * Context:
1826 * queue_lock must be held.
1828 void blk_start_request(struct request *req)
1830 blk_dequeue_request(req);
1833 * We are now handing the request to the hardware, initialize
1834 * resid_len to full count and add the timeout handler.
1836 req->resid_len = blk_rq_bytes(req);
1837 if (unlikely(blk_bidi_rq(req)))
1838 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1840 blk_add_timer(req);
1842 EXPORT_SYMBOL(blk_start_request);
1845 * blk_fetch_request - fetch a request from a request queue
1846 * @q: request queue to fetch a request from
1848 * Description:
1849 * Return the request at the top of @q. The request is started on
1850 * return and LLD can start processing it immediately.
1852 * Return:
1853 * Pointer to the request at the top of @q if available. Null
1854 * otherwise.
1856 * Context:
1857 * queue_lock must be held.
1859 struct request *blk_fetch_request(struct request_queue *q)
1861 struct request *rq;
1863 rq = blk_peek_request(q);
1864 if (rq)
1865 blk_start_request(rq);
1866 return rq;
1868 EXPORT_SYMBOL(blk_fetch_request);
1871 * blk_update_request - Special helper function for request stacking drivers
1872 * @req: the request being processed
1873 * @error: %0 for success, < %0 for error
1874 * @nr_bytes: number of bytes to complete @req
1876 * Description:
1877 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1878 * the request structure even if @req doesn't have leftover.
1879 * If @req has leftover, sets it up for the next range of segments.
1881 * This special helper function is only for request stacking drivers
1882 * (e.g. request-based dm) so that they can handle partial completion.
1883 * Actual device drivers should use blk_end_request instead.
1885 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1886 * %false return from this function.
1888 * Return:
1889 * %false - this request doesn't have any more data
1890 * %true - this request has more data
1892 bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
1894 int total_bytes, bio_nbytes, next_idx = 0;
1895 struct bio *bio;
1897 if (!req->bio)
1898 return false;
1900 trace_block_rq_complete(req->q, req);
1903 * For fs requests, rq is just carrier of independent bio's
1904 * and each partial completion should be handled separately.
1905 * Reset per-request error on each partial completion.
1907 * TODO: tj: This is too subtle. It would be better to let
1908 * low level drivers do what they see fit.
1910 if (blk_fs_request(req))
1911 req->errors = 0;
1913 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1914 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1915 req->rq_disk ? req->rq_disk->disk_name : "?",
1916 (unsigned long long)blk_rq_pos(req));
1919 blk_account_io_completion(req, nr_bytes);
1921 total_bytes = bio_nbytes = 0;
1922 while ((bio = req->bio) != NULL) {
1923 int nbytes;
1925 if (nr_bytes >= bio->bi_size) {
1926 req->bio = bio->bi_next;
1927 nbytes = bio->bi_size;
1928 req_bio_endio(req, bio, nbytes, error);
1929 next_idx = 0;
1930 bio_nbytes = 0;
1931 } else {
1932 int idx = bio->bi_idx + next_idx;
1934 if (unlikely(idx >= bio->bi_vcnt)) {
1935 blk_dump_rq_flags(req, "__end_that");
1936 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1937 __func__, idx, bio->bi_vcnt);
1938 break;
1941 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1942 BIO_BUG_ON(nbytes > bio->bi_size);
1945 * not a complete bvec done
1947 if (unlikely(nbytes > nr_bytes)) {
1948 bio_nbytes += nr_bytes;
1949 total_bytes += nr_bytes;
1950 break;
1954 * advance to the next vector
1956 next_idx++;
1957 bio_nbytes += nbytes;
1960 total_bytes += nbytes;
1961 nr_bytes -= nbytes;
1963 bio = req->bio;
1964 if (bio) {
1966 * end more in this run, or just return 'not-done'
1968 if (unlikely(nr_bytes <= 0))
1969 break;
1974 * completely done
1976 if (!req->bio) {
1978 * Reset counters so that the request stacking driver
1979 * can find how many bytes remain in the request
1980 * later.
1982 req->__data_len = 0;
1983 return false;
1987 * if the request wasn't completed, update state
1989 if (bio_nbytes) {
1990 req_bio_endio(req, bio, bio_nbytes, error);
1991 bio->bi_idx += next_idx;
1992 bio_iovec(bio)->bv_offset += nr_bytes;
1993 bio_iovec(bio)->bv_len -= nr_bytes;
1996 req->__data_len -= total_bytes;
1997 req->buffer = bio_data(req->bio);
1999 /* update sector only for requests with clear definition of sector */
2000 if (blk_fs_request(req) || blk_discard_rq(req))
2001 req->__sector += total_bytes >> 9;
2004 * If total number of sectors is less than the first segment
2005 * size, something has gone terribly wrong.
2007 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2008 printk(KERN_ERR "blk: request botched\n");
2009 req->__data_len = blk_rq_cur_bytes(req);
2012 /* recalculate the number of segments */
2013 blk_recalc_rq_segments(req);
2015 return true;
2017 EXPORT_SYMBOL_GPL(blk_update_request);
2019 static bool blk_update_bidi_request(struct request *rq, int error,
2020 unsigned int nr_bytes,
2021 unsigned int bidi_bytes)
2023 if (blk_update_request(rq, error, nr_bytes))
2024 return true;
2026 /* Bidi request must be completed as a whole */
2027 if (unlikely(blk_bidi_rq(rq)) &&
2028 blk_update_request(rq->next_rq, error, bidi_bytes))
2029 return true;
2031 add_disk_randomness(rq->rq_disk);
2033 return false;
2037 * queue lock must be held
2039 static void blk_finish_request(struct request *req, int error)
2041 if (blk_rq_tagged(req))
2042 blk_queue_end_tag(req->q, req);
2044 BUG_ON(blk_queued_rq(req));
2046 if (unlikely(laptop_mode) && blk_fs_request(req))
2047 laptop_io_completion();
2049 blk_delete_timer(req);
2051 blk_account_io_done(req);
2053 if (req->end_io)
2054 req->end_io(req, error);
2055 else {
2056 if (blk_bidi_rq(req))
2057 __blk_put_request(req->next_rq->q, req->next_rq);
2059 __blk_put_request(req->q, req);
2064 * blk_end_bidi_request - Complete a bidi request
2065 * @rq: the request to complete
2066 * @error: %0 for success, < %0 for error
2067 * @nr_bytes: number of bytes to complete @rq
2068 * @bidi_bytes: number of bytes to complete @rq->next_rq
2070 * Description:
2071 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2072 * Drivers that supports bidi can safely call this member for any
2073 * type of request, bidi or uni. In the later case @bidi_bytes is
2074 * just ignored.
2076 * Return:
2077 * %false - we are done with this request
2078 * %true - still buffers pending for this request
2080 static bool blk_end_bidi_request(struct request *rq, int error,
2081 unsigned int nr_bytes, unsigned int bidi_bytes)
2083 struct request_queue *q = rq->q;
2084 unsigned long flags;
2086 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2087 return true;
2089 spin_lock_irqsave(q->queue_lock, flags);
2090 blk_finish_request(rq, error);
2091 spin_unlock_irqrestore(q->queue_lock, flags);
2093 return false;
2097 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2098 * @rq: the request to complete
2099 * @error: %0 for success, < %0 for error
2100 * @nr_bytes: number of bytes to complete @rq
2101 * @bidi_bytes: number of bytes to complete @rq->next_rq
2103 * Description:
2104 * Identical to blk_end_bidi_request() except that queue lock is
2105 * assumed to be locked on entry and remains so on return.
2107 * Return:
2108 * %false - we are done with this request
2109 * %true - still buffers pending for this request
2111 static bool __blk_end_bidi_request(struct request *rq, int error,
2112 unsigned int nr_bytes, unsigned int bidi_bytes)
2114 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2115 return true;
2117 blk_finish_request(rq, error);
2119 return false;
2123 * blk_end_request - Helper function for drivers to complete the request.
2124 * @rq: the request being processed
2125 * @error: %0 for success, < %0 for error
2126 * @nr_bytes: number of bytes to complete
2128 * Description:
2129 * Ends I/O on a number of bytes attached to @rq.
2130 * If @rq has leftover, sets it up for the next range of segments.
2132 * Return:
2133 * %false - we are done with this request
2134 * %true - still buffers pending for this request
2136 bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2138 return blk_end_bidi_request(rq, error, nr_bytes, 0);
2140 EXPORT_SYMBOL(blk_end_request);
2143 * blk_end_request_all - Helper function for drives to finish the request.
2144 * @rq: the request to finish
2145 * @error: %0 for success, < %0 for error
2147 * Description:
2148 * Completely finish @rq.
2150 void blk_end_request_all(struct request *rq, int error)
2152 bool pending;
2153 unsigned int bidi_bytes = 0;
2155 if (unlikely(blk_bidi_rq(rq)))
2156 bidi_bytes = blk_rq_bytes(rq->next_rq);
2158 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2159 BUG_ON(pending);
2161 EXPORT_SYMBOL(blk_end_request_all);
2164 * blk_end_request_cur - Helper function to finish the current request chunk.
2165 * @rq: the request to finish the current chunk for
2166 * @error: %0 for success, < %0 for error
2168 * Description:
2169 * Complete the current consecutively mapped chunk from @rq.
2171 * Return:
2172 * %false - we are done with this request
2173 * %true - still buffers pending for this request
2175 bool blk_end_request_cur(struct request *rq, int error)
2177 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2179 EXPORT_SYMBOL(blk_end_request_cur);
2182 * __blk_end_request - Helper function for drivers to complete the request.
2183 * @rq: the request being processed
2184 * @error: %0 for success, < %0 for error
2185 * @nr_bytes: number of bytes to complete
2187 * Description:
2188 * Must be called with queue lock held unlike blk_end_request().
2190 * Return:
2191 * %false - we are done with this request
2192 * %true - still buffers pending for this request
2194 bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2196 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
2198 EXPORT_SYMBOL(__blk_end_request);
2201 * __blk_end_request_all - Helper function for drives to finish the request.
2202 * @rq: the request to finish
2203 * @error: %0 for success, < %0 for error
2205 * Description:
2206 * Completely finish @rq. Must be called with queue lock held.
2208 void __blk_end_request_all(struct request *rq, int error)
2210 bool pending;
2211 unsigned int bidi_bytes = 0;
2213 if (unlikely(blk_bidi_rq(rq)))
2214 bidi_bytes = blk_rq_bytes(rq->next_rq);
2216 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2217 BUG_ON(pending);
2219 EXPORT_SYMBOL(__blk_end_request_all);
2222 * __blk_end_request_cur - Helper function to finish the current request chunk.
2223 * @rq: the request to finish the current chunk for
2224 * @error: %0 for success, < %0 for error
2226 * Description:
2227 * Complete the current consecutively mapped chunk from @rq. Must
2228 * be called with queue lock held.
2230 * Return:
2231 * %false - we are done with this request
2232 * %true - still buffers pending for this request
2234 bool __blk_end_request_cur(struct request *rq, int error)
2236 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2238 EXPORT_SYMBOL(__blk_end_request_cur);
2240 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2241 struct bio *bio)
2243 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2244 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
2245 rq->cmd_flags |= (bio->bi_rw & 3);
2247 if (bio_has_data(bio)) {
2248 rq->nr_phys_segments = bio_phys_segments(q, bio);
2249 rq->buffer = bio_data(bio);
2251 rq->__data_len = bio->bi_size;
2252 rq->bio = rq->biotail = bio;
2254 if (bio->bi_bdev)
2255 rq->rq_disk = bio->bi_bdev->bd_disk;
2259 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2260 * @q : the queue of the device being checked
2262 * Description:
2263 * Check if underlying low-level drivers of a device are busy.
2264 * If the drivers want to export their busy state, they must set own
2265 * exporting function using blk_queue_lld_busy() first.
2267 * Basically, this function is used only by request stacking drivers
2268 * to stop dispatching requests to underlying devices when underlying
2269 * devices are busy. This behavior helps more I/O merging on the queue
2270 * of the request stacking driver and prevents I/O throughput regression
2271 * on burst I/O load.
2273 * Return:
2274 * 0 - Not busy (The request stacking driver should dispatch request)
2275 * 1 - Busy (The request stacking driver should stop dispatching request)
2277 int blk_lld_busy(struct request_queue *q)
2279 if (q->lld_busy_fn)
2280 return q->lld_busy_fn(q);
2282 return 0;
2284 EXPORT_SYMBOL_GPL(blk_lld_busy);
2287 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2288 * @rq: the clone request to be cleaned up
2290 * Description:
2291 * Free all bios in @rq for a cloned request.
2293 void blk_rq_unprep_clone(struct request *rq)
2295 struct bio *bio;
2297 while ((bio = rq->bio) != NULL) {
2298 rq->bio = bio->bi_next;
2300 bio_put(bio);
2303 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2306 * Copy attributes of the original request to the clone request.
2307 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2309 static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2311 dst->cpu = src->cpu;
2312 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2313 dst->cmd_type = src->cmd_type;
2314 dst->__sector = blk_rq_pos(src);
2315 dst->__data_len = blk_rq_bytes(src);
2316 dst->nr_phys_segments = src->nr_phys_segments;
2317 dst->ioprio = src->ioprio;
2318 dst->extra_len = src->extra_len;
2322 * blk_rq_prep_clone - Helper function to setup clone request
2323 * @rq: the request to be setup
2324 * @rq_src: original request to be cloned
2325 * @bs: bio_set that bios for clone are allocated from
2326 * @gfp_mask: memory allocation mask for bio
2327 * @bio_ctr: setup function to be called for each clone bio.
2328 * Returns %0 for success, non %0 for failure.
2329 * @data: private data to be passed to @bio_ctr
2331 * Description:
2332 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2333 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2334 * are not copied, and copying such parts is the caller's responsibility.
2335 * Also, pages which the original bios are pointing to are not copied
2336 * and the cloned bios just point same pages.
2337 * So cloned bios must be completed before original bios, which means
2338 * the caller must complete @rq before @rq_src.
2340 int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2341 struct bio_set *bs, gfp_t gfp_mask,
2342 int (*bio_ctr)(struct bio *, struct bio *, void *),
2343 void *data)
2345 struct bio *bio, *bio_src;
2347 if (!bs)
2348 bs = fs_bio_set;
2350 blk_rq_init(NULL, rq);
2352 __rq_for_each_bio(bio_src, rq_src) {
2353 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2354 if (!bio)
2355 goto free_and_out;
2357 __bio_clone(bio, bio_src);
2359 if (bio_integrity(bio_src) &&
2360 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
2361 goto free_and_out;
2363 if (bio_ctr && bio_ctr(bio, bio_src, data))
2364 goto free_and_out;
2366 if (rq->bio) {
2367 rq->biotail->bi_next = bio;
2368 rq->biotail = bio;
2369 } else
2370 rq->bio = rq->biotail = bio;
2373 __blk_rq_prep_clone(rq, rq_src);
2375 return 0;
2377 free_and_out:
2378 if (bio)
2379 bio_free(bio, bs);
2380 blk_rq_unprep_clone(rq);
2382 return -ENOMEM;
2384 EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2386 int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
2388 return queue_work(kblockd_workqueue, work);
2390 EXPORT_SYMBOL(kblockd_schedule_work);
2392 int __init blk_dev_init(void)
2394 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2395 sizeof(((struct request *)0)->cmd_flags));
2397 kblockd_workqueue = create_workqueue("kblockd");
2398 if (!kblockd_workqueue)
2399 panic("Failed to create kblockd\n");
2401 request_cachep = kmem_cache_create("blkdev_requests",
2402 sizeof(struct request), 0, SLAB_PANIC, NULL);
2404 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2405 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
2407 return 0;