2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
12 * This handles all read/write requests to block devices
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/bio.h>
18 #include <linux/blkdev.h>
19 #include <linux/highmem.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
24 #include <linux/completion.h>
25 #include <linux/slab.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/task_io_accounting_ops.h>
29 #include <linux/fault-inject.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/block.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);
63 if (!blk_do_io_stat(rq))
66 cpu = part_stat_lock();
67 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
70 part_stat_inc(cpu, part, merges[rw]);
72 part_round_stats(cpu, part);
73 part_inc_in_flight(part, rw);
79 void blk_queue_congestion_threshold(struct request_queue *q)
83 nr = q->nr_requests - (q->nr_requests / 8) + 1;
84 if (nr > q->nr_requests)
86 q->nr_congestion_on = nr;
88 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
91 q->nr_congestion_off = nr;
95 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
98 * Locates the passed device's request queue and returns the address of its
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);
109 ret = &q->backing_dev_info;
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);
122 rq->__sector = (sector_t) -1;
123 INIT_HLIST_NODE(&rq->hash);
124 RB_CLEAR_NODE(&rq->rb_node);
126 rq->cmd_len = BLK_MAX_CDB;
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)
138 clear_bit(BIO_UPTODATE, &bio->bi_flags);
139 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
142 if (unlikely(nbytes > bio->bi_size)) {
143 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
144 __func__, nbytes, bio->bi_size);
145 nbytes = bio->bi_size;
148 if (unlikely(rq->cmd_flags & REQ_QUIET))
149 set_bit(BIO_QUIET, &bio->bi_flags);
151 bio->bi_size -= nbytes;
152 bio->bi_sector += (nbytes >> 9);
154 if (bio_integrity(bio))
155 bio_integrity_advance(bio, nbytes);
157 /* don't actually finish bio if it's part of flush sequence */
158 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
159 bio_endio(bio, error);
162 void blk_dump_rq_flags(struct request *rq, char *msg)
166 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
167 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
170 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
171 (unsigned long long)blk_rq_pos(rq),
172 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
173 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
174 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
176 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
177 printk(KERN_INFO " cdb: ");
178 for (bit = 0; bit < BLK_MAX_CDB; bit++)
179 printk("%02x ", rq->cmd[bit]);
183 EXPORT_SYMBOL(blk_dump_rq_flags);
186 * "plug" the device if there are no outstanding requests: this will
187 * force the transfer to start only after we have put all the requests
190 * This is called with interrupts off and no requests on the queue and
191 * with the queue lock held.
193 void blk_plug_device(struct request_queue *q)
195 WARN_ON(!irqs_disabled());
198 * don't plug a stopped queue, it must be paired with blk_start_queue()
199 * which will restart the queueing
201 if (blk_queue_stopped(q))
204 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
205 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
209 EXPORT_SYMBOL(blk_plug_device);
212 * blk_plug_device_unlocked - plug a device without queue lock held
213 * @q: The &struct request_queue to plug
216 * Like @blk_plug_device(), but grabs the queue lock and disables
219 void blk_plug_device_unlocked(struct request_queue *q)
223 spin_lock_irqsave(q->queue_lock, flags);
225 spin_unlock_irqrestore(q->queue_lock, flags);
227 EXPORT_SYMBOL(blk_plug_device_unlocked);
230 * remove the queue from the plugged list, if present. called with
231 * queue lock held and interrupts disabled.
233 int blk_remove_plug(struct request_queue *q)
235 WARN_ON(!irqs_disabled());
237 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
240 del_timer(&q->unplug_timer);
243 EXPORT_SYMBOL(blk_remove_plug);
246 * remove the plug and let it rip..
248 void __generic_unplug_device(struct request_queue *q)
250 if (unlikely(blk_queue_stopped(q)))
252 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
259 * generic_unplug_device - fire a request queue
260 * @q: The &struct request_queue in question
263 * Linux uses plugging to build bigger requests queues before letting
264 * the device have at them. If a queue is plugged, the I/O scheduler
265 * is still adding and merging requests on the queue. Once the queue
266 * gets unplugged, the request_fn defined for the queue is invoked and
269 void generic_unplug_device(struct request_queue *q)
271 if (blk_queue_plugged(q)) {
272 spin_lock_irq(q->queue_lock);
273 __generic_unplug_device(q);
274 spin_unlock_irq(q->queue_lock);
277 EXPORT_SYMBOL(generic_unplug_device);
279 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
282 struct request_queue *q = bdi->unplug_io_data;
287 void blk_unplug_work(struct work_struct *work)
289 struct request_queue *q =
290 container_of(work, struct request_queue, unplug_work);
292 trace_block_unplug_io(q);
296 void blk_unplug_timeout(unsigned long data)
298 struct request_queue *q = (struct request_queue *)data;
300 trace_block_unplug_timer(q);
301 kblockd_schedule_work(q, &q->unplug_work);
304 void blk_unplug(struct request_queue *q)
307 * devices don't necessarily have an ->unplug_fn defined
310 trace_block_unplug_io(q);
314 EXPORT_SYMBOL(blk_unplug);
317 * blk_start_queue - restart a previously stopped queue
318 * @q: The &struct request_queue in question
321 * blk_start_queue() will clear the stop flag on the queue, and call
322 * the request_fn for the queue if it was in a stopped state when
323 * entered. Also see blk_stop_queue(). Queue lock must be held.
325 void blk_start_queue(struct request_queue *q)
327 WARN_ON(!irqs_disabled());
329 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
332 EXPORT_SYMBOL(blk_start_queue);
335 * blk_stop_queue - stop a queue
336 * @q: The &struct request_queue in question
339 * The Linux block layer assumes that a block driver will consume all
340 * entries on the request queue when the request_fn strategy is called.
341 * Often this will not happen, because of hardware limitations (queue
342 * depth settings). If a device driver gets a 'queue full' response,
343 * or if it simply chooses not to queue more I/O at one point, it can
344 * call this function to prevent the request_fn from being called until
345 * the driver has signalled it's ready to go again. This happens by calling
346 * blk_start_queue() to restart queue operations. Queue lock must be held.
348 void blk_stop_queue(struct request_queue *q)
351 queue_flag_set(QUEUE_FLAG_STOPPED, q);
353 EXPORT_SYMBOL(blk_stop_queue);
356 * blk_sync_queue - cancel any pending callbacks on a queue
360 * The block layer may perform asynchronous callback activity
361 * on a queue, such as calling the unplug function after a timeout.
362 * A block device may call blk_sync_queue to ensure that any
363 * such activity is cancelled, thus allowing it to release resources
364 * that the callbacks might use. The caller must already have made sure
365 * that its ->make_request_fn will not re-add plugging prior to calling
369 void blk_sync_queue(struct request_queue *q)
371 del_timer_sync(&q->unplug_timer);
372 del_timer_sync(&q->timeout);
373 cancel_work_sync(&q->unplug_work);
374 throtl_shutdown_timer_wq(q);
376 EXPORT_SYMBOL(blk_sync_queue);
379 * __blk_run_queue - run a single device queue
380 * @q: The queue to run
383 * See @blk_run_queue. This variant must be called with the queue lock
384 * held and interrupts disabled.
387 void __blk_run_queue(struct request_queue *q)
391 if (unlikely(blk_queue_stopped(q)))
394 if (elv_queue_empty(q))
398 * Only recurse once to avoid overrunning the stack, let the unplug
399 * handling reinvoke the handler shortly if we already got there.
401 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
403 queue_flag_clear(QUEUE_FLAG_REENTER, q);
405 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
406 kblockd_schedule_work(q, &q->unplug_work);
409 EXPORT_SYMBOL(__blk_run_queue);
412 * blk_run_queue - run a single device queue
413 * @q: The queue to run
416 * Invoke request handling on this queue, if it has pending work to do.
417 * May be used to restart queueing when a request has completed.
419 void blk_run_queue(struct request_queue *q)
423 spin_lock_irqsave(q->queue_lock, flags);
425 spin_unlock_irqrestore(q->queue_lock, flags);
427 EXPORT_SYMBOL(blk_run_queue);
429 void blk_put_queue(struct request_queue *q)
431 kobject_put(&q->kobj);
434 void blk_cleanup_queue(struct request_queue *q)
437 * We know we have process context here, so we can be a little
438 * cautious and ensure that pending block actions on this device
439 * are done before moving on. Going into this function, we should
440 * not have processes doing IO to this device.
444 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
445 mutex_lock(&q->sysfs_lock);
446 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
447 mutex_unlock(&q->sysfs_lock);
450 elevator_exit(q->elevator);
454 EXPORT_SYMBOL(blk_cleanup_queue);
456 static int blk_init_free_list(struct request_queue *q)
458 struct request_list *rl = &q->rq;
460 if (unlikely(rl->rq_pool))
463 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
464 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
466 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
467 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
469 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
470 mempool_free_slab, request_cachep, q->node);
478 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
480 return blk_alloc_queue_node(gfp_mask, -1);
482 EXPORT_SYMBOL(blk_alloc_queue);
484 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
486 struct request_queue *q;
489 q = kmem_cache_alloc_node(blk_requestq_cachep,
490 gfp_mask | __GFP_ZERO, node_id);
494 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
495 q->backing_dev_info.unplug_io_data = q;
496 q->backing_dev_info.ra_pages =
497 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
498 q->backing_dev_info.state = 0;
499 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
500 q->backing_dev_info.name = "block";
502 err = bdi_init(&q->backing_dev_info);
504 kmem_cache_free(blk_requestq_cachep, q);
508 if (blk_throtl_init(q)) {
509 kmem_cache_free(blk_requestq_cachep, q);
513 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
514 laptop_mode_timer_fn, (unsigned long) q);
515 init_timer(&q->unplug_timer);
516 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
517 INIT_LIST_HEAD(&q->timeout_list);
518 INIT_LIST_HEAD(&q->flush_queue[0]);
519 INIT_LIST_HEAD(&q->flush_queue[1]);
520 INIT_LIST_HEAD(&q->flush_data_in_flight);
521 INIT_WORK(&q->unplug_work, blk_unplug_work);
523 kobject_init(&q->kobj, &blk_queue_ktype);
525 mutex_init(&q->sysfs_lock);
526 spin_lock_init(&q->__queue_lock);
530 EXPORT_SYMBOL(blk_alloc_queue_node);
533 * blk_init_queue - prepare a request queue for use with a block device
534 * @rfn: The function to be called to process requests that have been
535 * placed on the queue.
536 * @lock: Request queue spin lock
539 * If a block device wishes to use the standard request handling procedures,
540 * which sorts requests and coalesces adjacent requests, then it must
541 * call blk_init_queue(). The function @rfn will be called when there
542 * are requests on the queue that need to be processed. If the device
543 * supports plugging, then @rfn may not be called immediately when requests
544 * are available on the queue, but may be called at some time later instead.
545 * Plugged queues are generally unplugged when a buffer belonging to one
546 * of the requests on the queue is needed, or due to memory pressure.
548 * @rfn is not required, or even expected, to remove all requests off the
549 * queue, but only as many as it can handle at a time. If it does leave
550 * requests on the queue, it is responsible for arranging that the requests
551 * get dealt with eventually.
553 * The queue spin lock must be held while manipulating the requests on the
554 * request queue; this lock will be taken also from interrupt context, so irq
555 * disabling is needed for it.
557 * Function returns a pointer to the initialized request queue, or %NULL if
561 * blk_init_queue() must be paired with a blk_cleanup_queue() call
562 * when the block device is deactivated (such as at module unload).
565 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
567 return blk_init_queue_node(rfn, lock, -1);
569 EXPORT_SYMBOL(blk_init_queue);
571 struct request_queue *
572 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
574 struct request_queue *uninit_q, *q;
576 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
580 q = blk_init_allocated_queue_node(uninit_q, rfn, lock, node_id);
582 blk_cleanup_queue(uninit_q);
586 EXPORT_SYMBOL(blk_init_queue_node);
588 struct request_queue *
589 blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
592 return blk_init_allocated_queue_node(q, rfn, lock, -1);
594 EXPORT_SYMBOL(blk_init_allocated_queue);
596 struct request_queue *
597 blk_init_allocated_queue_node(struct request_queue *q, request_fn_proc *rfn,
598 spinlock_t *lock, int node_id)
604 if (blk_init_free_list(q))
608 q->prep_rq_fn = NULL;
609 q->unprep_rq_fn = NULL;
610 q->unplug_fn = generic_unplug_device;
611 q->queue_flags = QUEUE_FLAG_DEFAULT;
612 q->queue_lock = lock;
615 * This also sets hw/phys segments, boundary and size
617 blk_queue_make_request(q, __make_request);
619 q->sg_reserved_size = INT_MAX;
624 if (!elevator_init(q, NULL)) {
625 blk_queue_congestion_threshold(q);
631 EXPORT_SYMBOL(blk_init_allocated_queue_node);
633 int blk_get_queue(struct request_queue *q)
635 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
636 kobject_get(&q->kobj);
643 static inline void blk_free_request(struct request_queue *q, struct request *rq)
645 if (rq->cmd_flags & REQ_ELVPRIV)
646 elv_put_request(q, rq);
647 mempool_free(rq, q->rq.rq_pool);
650 static struct request *
651 blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
653 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
660 rq->cmd_flags = flags | REQ_ALLOCED;
663 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
664 mempool_free(rq, q->rq.rq_pool);
667 rq->cmd_flags |= REQ_ELVPRIV;
674 * ioc_batching returns true if the ioc is a valid batching request and
675 * should be given priority access to a request.
677 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
683 * Make sure the process is able to allocate at least 1 request
684 * even if the batch times out, otherwise we could theoretically
687 return ioc->nr_batch_requests == q->nr_batching ||
688 (ioc->nr_batch_requests > 0
689 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
693 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
694 * will cause the process to be a "batcher" on all queues in the system. This
695 * is the behaviour we want though - once it gets a wakeup it should be given
698 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
700 if (!ioc || ioc_batching(q, ioc))
703 ioc->nr_batch_requests = q->nr_batching;
704 ioc->last_waited = jiffies;
707 static void __freed_request(struct request_queue *q, int sync)
709 struct request_list *rl = &q->rq;
711 if (rl->count[sync] < queue_congestion_off_threshold(q))
712 blk_clear_queue_congested(q, sync);
714 if (rl->count[sync] + 1 <= q->nr_requests) {
715 if (waitqueue_active(&rl->wait[sync]))
716 wake_up(&rl->wait[sync]);
718 blk_clear_queue_full(q, sync);
723 * A request has just been released. Account for it, update the full and
724 * congestion status, wake up any waiters. Called under q->queue_lock.
726 static void freed_request(struct request_queue *q, int sync, int priv)
728 struct request_list *rl = &q->rq;
734 __freed_request(q, sync);
736 if (unlikely(rl->starved[sync ^ 1]))
737 __freed_request(q, sync ^ 1);
741 * Determine if elevator data should be initialized when allocating the
742 * request associated with @bio.
744 static bool blk_rq_should_init_elevator(struct bio *bio)
750 * Flush requests do not use the elevator so skip initialization.
751 * This allows a request to share the flush and elevator data.
753 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
760 * Get a free request, queue_lock must be held.
761 * Returns NULL on failure, with queue_lock held.
762 * Returns !NULL on success, with queue_lock *not held*.
764 static struct request *get_request(struct request_queue *q, int rw_flags,
765 struct bio *bio, gfp_t gfp_mask)
767 struct request *rq = NULL;
768 struct request_list *rl = &q->rq;
769 struct io_context *ioc = NULL;
770 const bool is_sync = rw_is_sync(rw_flags) != 0;
771 int may_queue, priv = 0;
773 may_queue = elv_may_queue(q, rw_flags);
774 if (may_queue == ELV_MQUEUE_NO)
777 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
778 if (rl->count[is_sync]+1 >= q->nr_requests) {
779 ioc = current_io_context(GFP_ATOMIC, q->node);
781 * The queue will fill after this allocation, so set
782 * it as full, and mark this process as "batching".
783 * This process will be allowed to complete a batch of
784 * requests, others will be blocked.
786 if (!blk_queue_full(q, is_sync)) {
787 ioc_set_batching(q, ioc);
788 blk_set_queue_full(q, is_sync);
790 if (may_queue != ELV_MQUEUE_MUST
791 && !ioc_batching(q, ioc)) {
793 * The queue is full and the allocating
794 * process is not a "batcher", and not
795 * exempted by the IO scheduler
801 blk_set_queue_congested(q, is_sync);
805 * Only allow batching queuers to allocate up to 50% over the defined
806 * limit of requests, otherwise we could have thousands of requests
807 * allocated with any setting of ->nr_requests
809 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
812 rl->count[is_sync]++;
813 rl->starved[is_sync] = 0;
815 if (blk_rq_should_init_elevator(bio)) {
816 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
821 if (blk_queue_io_stat(q))
822 rw_flags |= REQ_IO_STAT;
823 spin_unlock_irq(q->queue_lock);
825 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
828 * Allocation failed presumably due to memory. Undo anything
829 * we might have messed up.
831 * Allocating task should really be put onto the front of the
832 * wait queue, but this is pretty rare.
834 spin_lock_irq(q->queue_lock);
835 freed_request(q, is_sync, priv);
838 * in the very unlikely event that allocation failed and no
839 * requests for this direction was pending, mark us starved
840 * so that freeing of a request in the other direction will
841 * notice us. another possible fix would be to split the
842 * rq mempool into READ and WRITE
845 if (unlikely(rl->count[is_sync] == 0))
846 rl->starved[is_sync] = 1;
852 * ioc may be NULL here, and ioc_batching will be false. That's
853 * OK, if the queue is under the request limit then requests need
854 * not count toward the nr_batch_requests limit. There will always
855 * be some limit enforced by BLK_BATCH_TIME.
857 if (ioc_batching(q, ioc))
858 ioc->nr_batch_requests--;
860 trace_block_getrq(q, bio, rw_flags & 1);
866 * No available requests for this queue, unplug the device and wait for some
867 * requests to become available.
869 * Called with q->queue_lock held, and returns with it unlocked.
871 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
874 const bool is_sync = rw_is_sync(rw_flags) != 0;
877 rq = get_request(q, rw_flags, bio, GFP_NOIO);
880 struct io_context *ioc;
881 struct request_list *rl = &q->rq;
883 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
884 TASK_UNINTERRUPTIBLE);
886 trace_block_sleeprq(q, bio, rw_flags & 1);
888 __generic_unplug_device(q);
889 spin_unlock_irq(q->queue_lock);
893 * After sleeping, we become a "batching" process and
894 * will be able to allocate at least one request, and
895 * up to a big batch of them for a small period time.
896 * See ioc_batching, ioc_set_batching
898 ioc = current_io_context(GFP_NOIO, q->node);
899 ioc_set_batching(q, ioc);
901 spin_lock_irq(q->queue_lock);
902 finish_wait(&rl->wait[is_sync], &wait);
904 rq = get_request(q, rw_flags, bio, GFP_NOIO);
910 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
914 BUG_ON(rw != READ && rw != WRITE);
916 spin_lock_irq(q->queue_lock);
917 if (gfp_mask & __GFP_WAIT) {
918 rq = get_request_wait(q, rw, NULL);
920 rq = get_request(q, rw, NULL, gfp_mask);
922 spin_unlock_irq(q->queue_lock);
924 /* q->queue_lock is unlocked at this point */
928 EXPORT_SYMBOL(blk_get_request);
931 * blk_make_request - given a bio, allocate a corresponding struct request.
932 * @q: target request queue
933 * @bio: The bio describing the memory mappings that will be submitted for IO.
934 * It may be a chained-bio properly constructed by block/bio layer.
935 * @gfp_mask: gfp flags to be used for memory allocation
937 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
938 * type commands. Where the struct request needs to be farther initialized by
939 * the caller. It is passed a &struct bio, which describes the memory info of
942 * The caller of blk_make_request must make sure that bi_io_vec
943 * are set to describe the memory buffers. That bio_data_dir() will return
944 * the needed direction of the request. (And all bio's in the passed bio-chain
945 * are properly set accordingly)
947 * If called under none-sleepable conditions, mapped bio buffers must not
948 * need bouncing, by calling the appropriate masked or flagged allocator,
949 * suitable for the target device. Otherwise the call to blk_queue_bounce will
952 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
953 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
954 * anything but the first bio in the chain. Otherwise you risk waiting for IO
955 * completion of a bio that hasn't been submitted yet, thus resulting in a
956 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
957 * of bio_alloc(), as that avoids the mempool deadlock.
958 * If possible a big IO should be split into smaller parts when allocation
959 * fails. Partial allocation should not be an error, or you risk a live-lock.
961 struct request *blk_make_request(struct request_queue *q, struct bio *bio,
964 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
967 return ERR_PTR(-ENOMEM);
970 struct bio *bounce_bio = bio;
973 blk_queue_bounce(q, &bounce_bio);
974 ret = blk_rq_append_bio(q, rq, bounce_bio);
983 EXPORT_SYMBOL(blk_make_request);
986 * blk_requeue_request - put a request back on queue
987 * @q: request queue where request should be inserted
988 * @rq: request to be inserted
991 * Drivers often keep queueing requests until the hardware cannot accept
992 * more, when that condition happens we need to put the request back
993 * on the queue. Must be called with queue lock held.
995 void blk_requeue_request(struct request_queue *q, struct request *rq)
997 blk_delete_timer(rq);
998 blk_clear_rq_complete(rq);
999 trace_block_rq_requeue(q, rq);
1001 if (blk_rq_tagged(rq))
1002 blk_queue_end_tag(q, rq);
1004 BUG_ON(blk_queued_rq(rq));
1006 elv_requeue_request(q, rq);
1008 EXPORT_SYMBOL(blk_requeue_request);
1011 * blk_insert_request - insert a special request into a request queue
1012 * @q: request queue where request should be inserted
1013 * @rq: request to be inserted
1014 * @at_head: insert request at head or tail of queue
1015 * @data: private data
1018 * Many block devices need to execute commands asynchronously, so they don't
1019 * block the whole kernel from preemption during request execution. This is
1020 * accomplished normally by inserting aritficial requests tagged as
1021 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1022 * be scheduled for actual execution by the request queue.
1024 * We have the option of inserting the head or the tail of the queue.
1025 * Typically we use the tail for new ioctls and so forth. We use the head
1026 * of the queue for things like a QUEUE_FULL message from a device, or a
1027 * host that is unable to accept a particular command.
1029 void blk_insert_request(struct request_queue *q, struct request *rq,
1030 int at_head, void *data)
1032 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
1033 unsigned long flags;
1036 * tell I/O scheduler that this isn't a regular read/write (ie it
1037 * must not attempt merges on this) and that it acts as a soft
1040 rq->cmd_type = REQ_TYPE_SPECIAL;
1044 spin_lock_irqsave(q->queue_lock, flags);
1047 * If command is tagged, release the tag
1049 if (blk_rq_tagged(rq))
1050 blk_queue_end_tag(q, rq);
1052 drive_stat_acct(rq, 1);
1053 __elv_add_request(q, rq, where, 0);
1055 spin_unlock_irqrestore(q->queue_lock, flags);
1057 EXPORT_SYMBOL(blk_insert_request);
1059 static void part_round_stats_single(int cpu, struct hd_struct *part,
1062 if (now == part->stamp)
1065 if (part_in_flight(part)) {
1066 __part_stat_add(cpu, part, time_in_queue,
1067 part_in_flight(part) * (now - part->stamp));
1068 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1074 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1075 * @cpu: cpu number for stats access
1076 * @part: target partition
1078 * The average IO queue length and utilisation statistics are maintained
1079 * by observing the current state of the queue length and the amount of
1080 * time it has been in this state for.
1082 * Normally, that accounting is done on IO completion, but that can result
1083 * in more than a second's worth of IO being accounted for within any one
1084 * second, leading to >100% utilisation. To deal with that, we call this
1085 * function to do a round-off before returning the results when reading
1086 * /proc/diskstats. This accounts immediately for all queue usage up to
1087 * the current jiffies and restarts the counters again.
1089 void part_round_stats(int cpu, struct hd_struct *part)
1091 unsigned long now = jiffies;
1094 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1095 part_round_stats_single(cpu, part, now);
1097 EXPORT_SYMBOL_GPL(part_round_stats);
1100 * queue lock must be held
1102 void __blk_put_request(struct request_queue *q, struct request *req)
1106 if (unlikely(--req->ref_count))
1109 elv_completed_request(q, req);
1111 /* this is a bio leak */
1112 WARN_ON(req->bio != NULL);
1115 * Request may not have originated from ll_rw_blk. if not,
1116 * it didn't come out of our reserved rq pools
1118 if (req->cmd_flags & REQ_ALLOCED) {
1119 int is_sync = rq_is_sync(req) != 0;
1120 int priv = req->cmd_flags & REQ_ELVPRIV;
1122 BUG_ON(!list_empty(&req->queuelist));
1123 BUG_ON(!hlist_unhashed(&req->hash));
1125 blk_free_request(q, req);
1126 freed_request(q, is_sync, priv);
1129 EXPORT_SYMBOL_GPL(__blk_put_request);
1131 void blk_put_request(struct request *req)
1133 unsigned long flags;
1134 struct request_queue *q = req->q;
1136 spin_lock_irqsave(q->queue_lock, flags);
1137 __blk_put_request(q, req);
1138 spin_unlock_irqrestore(q->queue_lock, flags);
1140 EXPORT_SYMBOL(blk_put_request);
1143 * blk_add_request_payload - add a payload to a request
1144 * @rq: request to update
1145 * @page: page backing the payload
1146 * @len: length of the payload.
1148 * This allows to later add a payload to an already submitted request by
1149 * a block driver. The driver needs to take care of freeing the payload
1152 * Note that this is a quite horrible hack and nothing but handling of
1153 * discard requests should ever use it.
1155 void blk_add_request_payload(struct request *rq, struct page *page,
1158 struct bio *bio = rq->bio;
1160 bio->bi_io_vec->bv_page = page;
1161 bio->bi_io_vec->bv_offset = 0;
1162 bio->bi_io_vec->bv_len = len;
1166 bio->bi_phys_segments = 1;
1168 rq->__data_len = rq->resid_len = len;
1169 rq->nr_phys_segments = 1;
1170 rq->buffer = bio_data(bio);
1172 EXPORT_SYMBOL_GPL(blk_add_request_payload);
1174 void init_request_from_bio(struct request *req, struct bio *bio)
1176 req->cpu = bio->bi_comp_cpu;
1177 req->cmd_type = REQ_TYPE_FS;
1179 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1180 if (bio->bi_rw & REQ_RAHEAD)
1181 req->cmd_flags |= REQ_FAILFAST_MASK;
1184 req->__sector = bio->bi_sector;
1185 req->ioprio = bio_prio(bio);
1186 blk_rq_bio_prep(req->q, req, bio);
1190 * Only disabling plugging for non-rotational devices if it does tagging
1191 * as well, otherwise we do need the proper merging
1193 static inline bool queue_should_plug(struct request_queue *q)
1195 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1198 static int __make_request(struct request_queue *q, struct bio *bio)
1200 struct request *req;
1202 unsigned int bytes = bio->bi_size;
1203 const unsigned short prio = bio_prio(bio);
1204 const bool sync = !!(bio->bi_rw & REQ_SYNC);
1205 const bool unplug = !!(bio->bi_rw & REQ_UNPLUG);
1206 const unsigned long ff = bio->bi_rw & REQ_FAILFAST_MASK;
1207 int where = ELEVATOR_INSERT_SORT;
1211 * low level driver can indicate that it wants pages above a
1212 * certain limit bounced to low memory (ie for highmem, or even
1213 * ISA dma in theory)
1215 blk_queue_bounce(q, &bio);
1217 spin_lock_irq(q->queue_lock);
1219 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
1220 where = ELEVATOR_INSERT_FLUSH;
1224 if (elv_queue_empty(q))
1227 el_ret = elv_merge(q, &req, bio);
1229 case ELEVATOR_BACK_MERGE:
1230 BUG_ON(!rq_mergeable(req));
1232 if (!ll_back_merge_fn(q, req, bio))
1235 trace_block_bio_backmerge(q, bio);
1237 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1238 blk_rq_set_mixed_merge(req);
1240 req->biotail->bi_next = bio;
1242 req->__data_len += bytes;
1243 req->ioprio = ioprio_best(req->ioprio, prio);
1244 if (!blk_rq_cpu_valid(req))
1245 req->cpu = bio->bi_comp_cpu;
1246 drive_stat_acct(req, 0);
1247 elv_bio_merged(q, req, bio);
1248 if (!attempt_back_merge(q, req))
1249 elv_merged_request(q, req, el_ret);
1252 case ELEVATOR_FRONT_MERGE:
1253 BUG_ON(!rq_mergeable(req));
1255 if (!ll_front_merge_fn(q, req, bio))
1258 trace_block_bio_frontmerge(q, bio);
1260 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1261 blk_rq_set_mixed_merge(req);
1262 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1263 req->cmd_flags |= ff;
1266 bio->bi_next = req->bio;
1270 * may not be valid. if the low level driver said
1271 * it didn't need a bounce buffer then it better
1272 * not touch req->buffer either...
1274 req->buffer = bio_data(bio);
1275 req->__sector = bio->bi_sector;
1276 req->__data_len += bytes;
1277 req->ioprio = ioprio_best(req->ioprio, prio);
1278 if (!blk_rq_cpu_valid(req))
1279 req->cpu = bio->bi_comp_cpu;
1280 drive_stat_acct(req, 0);
1281 elv_bio_merged(q, req, bio);
1282 if (!attempt_front_merge(q, req))
1283 elv_merged_request(q, req, el_ret);
1286 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1293 * This sync check and mask will be re-done in init_request_from_bio(),
1294 * but we need to set it earlier to expose the sync flag to the
1295 * rq allocator and io schedulers.
1297 rw_flags = bio_data_dir(bio);
1299 rw_flags |= REQ_SYNC;
1302 * Grab a free request. This is might sleep but can not fail.
1303 * Returns with the queue unlocked.
1305 req = get_request_wait(q, rw_flags, bio);
1308 * After dropping the lock and possibly sleeping here, our request
1309 * may now be mergeable after it had proven unmergeable (above).
1310 * We don't worry about that case for efficiency. It won't happen
1311 * often, and the elevators are able to handle it.
1313 init_request_from_bio(req, bio);
1315 spin_lock_irq(q->queue_lock);
1316 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1317 bio_flagged(bio, BIO_CPU_AFFINE))
1318 req->cpu = blk_cpu_to_group(smp_processor_id());
1319 if (queue_should_plug(q) && elv_queue_empty(q))
1322 /* insert the request into the elevator */
1323 drive_stat_acct(req, 1);
1324 __elv_add_request(q, req, where, 0);
1326 if (unplug || !queue_should_plug(q))
1327 __generic_unplug_device(q);
1328 spin_unlock_irq(q->queue_lock);
1333 * If bio->bi_dev is a partition, remap the location
1335 static inline void blk_partition_remap(struct bio *bio)
1337 struct block_device *bdev = bio->bi_bdev;
1339 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1340 struct hd_struct *p = bdev->bd_part;
1342 bio->bi_sector += p->start_sect;
1343 bio->bi_bdev = bdev->bd_contains;
1345 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
1347 bio->bi_sector - p->start_sect);
1351 static void handle_bad_sector(struct bio *bio)
1353 char b[BDEVNAME_SIZE];
1355 printk(KERN_INFO "attempt to access beyond end of device\n");
1356 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1357 bdevname(bio->bi_bdev, b),
1359 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1360 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
1362 set_bit(BIO_EOF, &bio->bi_flags);
1365 #ifdef CONFIG_FAIL_MAKE_REQUEST
1367 static DECLARE_FAULT_ATTR(fail_make_request);
1369 static int __init setup_fail_make_request(char *str)
1371 return setup_fault_attr(&fail_make_request, str);
1373 __setup("fail_make_request=", setup_fail_make_request);
1375 static int should_fail_request(struct bio *bio)
1377 struct hd_struct *part = bio->bi_bdev->bd_part;
1379 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
1380 return should_fail(&fail_make_request, bio->bi_size);
1385 static int __init fail_make_request_debugfs(void)
1387 return init_fault_attr_dentries(&fail_make_request,
1388 "fail_make_request");
1391 late_initcall(fail_make_request_debugfs);
1393 #else /* CONFIG_FAIL_MAKE_REQUEST */
1395 static inline int should_fail_request(struct bio *bio)
1400 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1403 * Check whether this bio extends beyond the end of the device.
1405 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1412 /* Test device or partition size, when known. */
1413 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
1415 sector_t sector = bio->bi_sector;
1417 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1419 * This may well happen - the kernel calls bread()
1420 * without checking the size of the device, e.g., when
1421 * mounting a device.
1423 handle_bad_sector(bio);
1432 * generic_make_request - hand a buffer to its device driver for I/O
1433 * @bio: The bio describing the location in memory and on the device.
1435 * generic_make_request() is used to make I/O requests of block
1436 * devices. It is passed a &struct bio, which describes the I/O that needs
1439 * generic_make_request() does not return any status. The
1440 * success/failure status of the request, along with notification of
1441 * completion, is delivered asynchronously through the bio->bi_end_io
1442 * function described (one day) else where.
1444 * The caller of generic_make_request must make sure that bi_io_vec
1445 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1446 * set to describe the device address, and the
1447 * bi_end_io and optionally bi_private are set to describe how
1448 * completion notification should be signaled.
1450 * generic_make_request and the drivers it calls may use bi_next if this
1451 * bio happens to be merged with someone else, and may change bi_dev and
1452 * bi_sector for remaps as it sees fit. So the values of these fields
1453 * should NOT be depended on after the call to generic_make_request.
1455 static inline void __generic_make_request(struct bio *bio)
1457 struct request_queue *q;
1458 sector_t old_sector;
1459 int ret, nr_sectors = bio_sectors(bio);
1465 if (bio_check_eod(bio, nr_sectors))
1469 * Resolve the mapping until finished. (drivers are
1470 * still free to implement/resolve their own stacking
1471 * by explicitly returning 0)
1473 * NOTE: we don't repeat the blk_size check for each new device.
1474 * Stacking drivers are expected to know what they are doing.
1479 char b[BDEVNAME_SIZE];
1481 q = bdev_get_queue(bio->bi_bdev);
1484 "generic_make_request: Trying to access "
1485 "nonexistent block-device %s (%Lu)\n",
1486 bdevname(bio->bi_bdev, b),
1487 (long long) bio->bi_sector);
1491 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
1492 nr_sectors > queue_max_hw_sectors(q))) {
1493 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1494 bdevname(bio->bi_bdev, b),
1496 queue_max_hw_sectors(q));
1500 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1503 if (should_fail_request(bio))
1507 * If this device has partitions, remap block n
1508 * of partition p to block n+start(p) of the disk.
1510 blk_partition_remap(bio);
1512 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1515 if (old_sector != -1)
1516 trace_block_remap(q, bio, old_dev, old_sector);
1518 old_sector = bio->bi_sector;
1519 old_dev = bio->bi_bdev->bd_dev;
1521 if (bio_check_eod(bio, nr_sectors))
1525 * Filter flush bio's early so that make_request based
1526 * drivers without flush support don't have to worry
1529 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1530 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1537 if ((bio->bi_rw & REQ_DISCARD) &&
1538 (!blk_queue_discard(q) ||
1539 ((bio->bi_rw & REQ_SECURE) &&
1540 !blk_queue_secdiscard(q)))) {
1545 blk_throtl_bio(q, &bio);
1548 * If bio = NULL, bio has been throttled and will be submitted
1554 trace_block_bio_queue(q, bio);
1556 ret = q->make_request_fn(q, bio);
1562 bio_endio(bio, err);
1566 * We only want one ->make_request_fn to be active at a time,
1567 * else stack usage with stacked devices could be a problem.
1568 * So use current->bio_list to keep a list of requests
1569 * submited by a make_request_fn function.
1570 * current->bio_list is also used as a flag to say if
1571 * generic_make_request is currently active in this task or not.
1572 * If it is NULL, then no make_request is active. If it is non-NULL,
1573 * then a make_request is active, and new requests should be added
1576 void generic_make_request(struct bio *bio)
1578 struct bio_list bio_list_on_stack;
1580 if (current->bio_list) {
1581 /* make_request is active */
1582 bio_list_add(current->bio_list, bio);
1585 /* following loop may be a bit non-obvious, and so deserves some
1587 * Before entering the loop, bio->bi_next is NULL (as all callers
1588 * ensure that) so we have a list with a single bio.
1589 * We pretend that we have just taken it off a longer list, so
1590 * we assign bio_list to a pointer to the bio_list_on_stack,
1591 * thus initialising the bio_list of new bios to be
1592 * added. __generic_make_request may indeed add some more bios
1593 * through a recursive call to generic_make_request. If it
1594 * did, we find a non-NULL value in bio_list and re-enter the loop
1595 * from the top. In this case we really did just take the bio
1596 * of the top of the list (no pretending) and so remove it from
1597 * bio_list, and call into __generic_make_request again.
1599 * The loop was structured like this to make only one call to
1600 * __generic_make_request (which is important as it is large and
1601 * inlined) and to keep the structure simple.
1603 BUG_ON(bio->bi_next);
1604 bio_list_init(&bio_list_on_stack);
1605 current->bio_list = &bio_list_on_stack;
1607 __generic_make_request(bio);
1608 bio = bio_list_pop(current->bio_list);
1610 current->bio_list = NULL; /* deactivate */
1612 EXPORT_SYMBOL(generic_make_request);
1615 * submit_bio - submit a bio to the block device layer for I/O
1616 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1617 * @bio: The &struct bio which describes the I/O
1619 * submit_bio() is very similar in purpose to generic_make_request(), and
1620 * uses that function to do most of the work. Both are fairly rough
1621 * interfaces; @bio must be presetup and ready for I/O.
1624 void submit_bio(int rw, struct bio *bio)
1626 int count = bio_sectors(bio);
1631 * If it's a regular read/write or a barrier with data attached,
1632 * go through the normal accounting stuff before submission.
1634 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
1636 count_vm_events(PGPGOUT, count);
1638 task_io_account_read(bio->bi_size);
1639 count_vm_events(PGPGIN, count);
1642 if (unlikely(block_dump)) {
1643 char b[BDEVNAME_SIZE];
1644 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
1645 current->comm, task_pid_nr(current),
1646 (rw & WRITE) ? "WRITE" : "READ",
1647 (unsigned long long)bio->bi_sector,
1648 bdevname(bio->bi_bdev, b),
1653 generic_make_request(bio);
1655 EXPORT_SYMBOL(submit_bio);
1658 * blk_rq_check_limits - Helper function to check a request for the queue limit
1660 * @rq: the request being checked
1663 * @rq may have been made based on weaker limitations of upper-level queues
1664 * in request stacking drivers, and it may violate the limitation of @q.
1665 * Since the block layer and the underlying device driver trust @rq
1666 * after it is inserted to @q, it should be checked against @q before
1667 * the insertion using this generic function.
1669 * This function should also be useful for request stacking drivers
1670 * in some cases below, so export this function.
1671 * Request stacking drivers like request-based dm may change the queue
1672 * limits while requests are in the queue (e.g. dm's table swapping).
1673 * Such request stacking drivers should check those requests agaist
1674 * the new queue limits again when they dispatch those requests,
1675 * although such checkings are also done against the old queue limits
1676 * when submitting requests.
1678 int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1680 if (rq->cmd_flags & REQ_DISCARD)
1683 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1684 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
1685 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1690 * queue's settings related to segment counting like q->bounce_pfn
1691 * may differ from that of other stacking queues.
1692 * Recalculate it to check the request correctly on this queue's
1695 blk_recalc_rq_segments(rq);
1696 if (rq->nr_phys_segments > queue_max_segments(q)) {
1697 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1703 EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1706 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1707 * @q: the queue to submit the request
1708 * @rq: the request being queued
1710 int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1712 unsigned long flags;
1714 if (blk_rq_check_limits(q, rq))
1717 #ifdef CONFIG_FAIL_MAKE_REQUEST
1718 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1719 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1723 spin_lock_irqsave(q->queue_lock, flags);
1726 * Submitting request must be dequeued before calling this function
1727 * because it will be linked to another request_queue
1729 BUG_ON(blk_queued_rq(rq));
1731 drive_stat_acct(rq, 1);
1732 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1734 spin_unlock_irqrestore(q->queue_lock, flags);
1738 EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1741 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1742 * @rq: request to examine
1745 * A request could be merge of IOs which require different failure
1746 * handling. This function determines the number of bytes which
1747 * can be failed from the beginning of the request without
1748 * crossing into area which need to be retried further.
1751 * The number of bytes to fail.
1754 * queue_lock must be held.
1756 unsigned int blk_rq_err_bytes(const struct request *rq)
1758 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1759 unsigned int bytes = 0;
1762 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1763 return blk_rq_bytes(rq);
1766 * Currently the only 'mixing' which can happen is between
1767 * different fastfail types. We can safely fail portions
1768 * which have all the failfast bits that the first one has -
1769 * the ones which are at least as eager to fail as the first
1772 for (bio = rq->bio; bio; bio = bio->bi_next) {
1773 if ((bio->bi_rw & ff) != ff)
1775 bytes += bio->bi_size;
1778 /* this could lead to infinite loop */
1779 BUG_ON(blk_rq_bytes(rq) && !bytes);
1782 EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1784 static void blk_account_io_completion(struct request *req, unsigned int bytes)
1786 if (blk_do_io_stat(req)) {
1787 const int rw = rq_data_dir(req);
1788 struct hd_struct *part;
1791 cpu = part_stat_lock();
1792 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1793 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1798 static void blk_account_io_done(struct request *req)
1801 * Account IO completion. flush_rq isn't accounted as a
1802 * normal IO on queueing nor completion. Accounting the
1803 * containing request is enough.
1805 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
1806 unsigned long duration = jiffies - req->start_time;
1807 const int rw = rq_data_dir(req);
1808 struct hd_struct *part;
1811 cpu = part_stat_lock();
1812 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1814 part_stat_inc(cpu, part, ios[rw]);
1815 part_stat_add(cpu, part, ticks[rw], duration);
1816 part_round_stats(cpu, part);
1817 part_dec_in_flight(part, rw);
1824 * blk_peek_request - peek at the top of a request queue
1825 * @q: request queue to peek at
1828 * Return the request at the top of @q. The returned request
1829 * should be started using blk_start_request() before LLD starts
1833 * Pointer to the request at the top of @q if available. Null
1837 * queue_lock must be held.
1839 struct request *blk_peek_request(struct request_queue *q)
1844 while ((rq = __elv_next_request(q)) != NULL) {
1845 if (!(rq->cmd_flags & REQ_STARTED)) {
1847 * This is the first time the device driver
1848 * sees this request (possibly after
1849 * requeueing). Notify IO scheduler.
1851 if (rq->cmd_flags & REQ_SORTED)
1852 elv_activate_rq(q, rq);
1855 * just mark as started even if we don't start
1856 * it, a request that has been delayed should
1857 * not be passed by new incoming requests
1859 rq->cmd_flags |= REQ_STARTED;
1860 trace_block_rq_issue(q, rq);
1863 if (!q->boundary_rq || q->boundary_rq == rq) {
1864 q->end_sector = rq_end_sector(rq);
1865 q->boundary_rq = NULL;
1868 if (rq->cmd_flags & REQ_DONTPREP)
1871 if (q->dma_drain_size && blk_rq_bytes(rq)) {
1873 * make sure space for the drain appears we
1874 * know we can do this because max_hw_segments
1875 * has been adjusted to be one fewer than the
1878 rq->nr_phys_segments++;
1884 ret = q->prep_rq_fn(q, rq);
1885 if (ret == BLKPREP_OK) {
1887 } else if (ret == BLKPREP_DEFER) {
1889 * the request may have been (partially) prepped.
1890 * we need to keep this request in the front to
1891 * avoid resource deadlock. REQ_STARTED will
1892 * prevent other fs requests from passing this one.
1894 if (q->dma_drain_size && blk_rq_bytes(rq) &&
1895 !(rq->cmd_flags & REQ_DONTPREP)) {
1897 * remove the space for the drain we added
1898 * so that we don't add it again
1900 --rq->nr_phys_segments;
1905 } else if (ret == BLKPREP_KILL) {
1906 rq->cmd_flags |= REQ_QUIET;
1908 * Mark this request as started so we don't trigger
1909 * any debug logic in the end I/O path.
1911 blk_start_request(rq);
1912 __blk_end_request_all(rq, -EIO);
1914 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1921 EXPORT_SYMBOL(blk_peek_request);
1923 void blk_dequeue_request(struct request *rq)
1925 struct request_queue *q = rq->q;
1927 BUG_ON(list_empty(&rq->queuelist));
1928 BUG_ON(ELV_ON_HASH(rq));
1930 list_del_init(&rq->queuelist);
1933 * the time frame between a request being removed from the lists
1934 * and to it is freed is accounted as io that is in progress at
1937 if (blk_account_rq(rq)) {
1938 q->in_flight[rq_is_sync(rq)]++;
1939 set_io_start_time_ns(rq);
1944 * blk_start_request - start request processing on the driver
1945 * @req: request to dequeue
1948 * Dequeue @req and start timeout timer on it. This hands off the
1949 * request to the driver.
1951 * Block internal functions which don't want to start timer should
1952 * call blk_dequeue_request().
1955 * queue_lock must be held.
1957 void blk_start_request(struct request *req)
1959 blk_dequeue_request(req);
1962 * We are now handing the request to the hardware, initialize
1963 * resid_len to full count and add the timeout handler.
1965 req->resid_len = blk_rq_bytes(req);
1966 if (unlikely(blk_bidi_rq(req)))
1967 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1971 EXPORT_SYMBOL(blk_start_request);
1974 * blk_fetch_request - fetch a request from a request queue
1975 * @q: request queue to fetch a request from
1978 * Return the request at the top of @q. The request is started on
1979 * return and LLD can start processing it immediately.
1982 * Pointer to the request at the top of @q if available. Null
1986 * queue_lock must be held.
1988 struct request *blk_fetch_request(struct request_queue *q)
1992 rq = blk_peek_request(q);
1994 blk_start_request(rq);
1997 EXPORT_SYMBOL(blk_fetch_request);
2000 * blk_update_request - Special helper function for request stacking drivers
2001 * @req: the request being processed
2002 * @error: %0 for success, < %0 for error
2003 * @nr_bytes: number of bytes to complete @req
2006 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2007 * the request structure even if @req doesn't have leftover.
2008 * If @req has leftover, sets it up for the next range of segments.
2010 * This special helper function is only for request stacking drivers
2011 * (e.g. request-based dm) so that they can handle partial completion.
2012 * Actual device drivers should use blk_end_request instead.
2014 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2015 * %false return from this function.
2018 * %false - this request doesn't have any more data
2019 * %true - this request has more data
2021 bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
2023 int total_bytes, bio_nbytes, next_idx = 0;
2029 trace_block_rq_complete(req->q, req);
2032 * For fs requests, rq is just carrier of independent bio's
2033 * and each partial completion should be handled separately.
2034 * Reset per-request error on each partial completion.
2036 * TODO: tj: This is too subtle. It would be better to let
2037 * low level drivers do what they see fit.
2039 if (req->cmd_type == REQ_TYPE_FS)
2042 if (error && req->cmd_type == REQ_TYPE_FS &&
2043 !(req->cmd_flags & REQ_QUIET)) {
2044 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
2045 req->rq_disk ? req->rq_disk->disk_name : "?",
2046 (unsigned long long)blk_rq_pos(req));
2049 blk_account_io_completion(req, nr_bytes);
2051 total_bytes = bio_nbytes = 0;
2052 while ((bio = req->bio) != NULL) {
2055 if (nr_bytes >= bio->bi_size) {
2056 req->bio = bio->bi_next;
2057 nbytes = bio->bi_size;
2058 req_bio_endio(req, bio, nbytes, error);
2062 int idx = bio->bi_idx + next_idx;
2064 if (unlikely(idx >= bio->bi_vcnt)) {
2065 blk_dump_rq_flags(req, "__end_that");
2066 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
2067 __func__, idx, bio->bi_vcnt);
2071 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2072 BIO_BUG_ON(nbytes > bio->bi_size);
2075 * not a complete bvec done
2077 if (unlikely(nbytes > nr_bytes)) {
2078 bio_nbytes += nr_bytes;
2079 total_bytes += nr_bytes;
2084 * advance to the next vector
2087 bio_nbytes += nbytes;
2090 total_bytes += nbytes;
2096 * end more in this run, or just return 'not-done'
2098 if (unlikely(nr_bytes <= 0))
2108 * Reset counters so that the request stacking driver
2109 * can find how many bytes remain in the request
2112 req->__data_len = 0;
2117 * if the request wasn't completed, update state
2120 req_bio_endio(req, bio, bio_nbytes, error);
2121 bio->bi_idx += next_idx;
2122 bio_iovec(bio)->bv_offset += nr_bytes;
2123 bio_iovec(bio)->bv_len -= nr_bytes;
2126 req->__data_len -= total_bytes;
2127 req->buffer = bio_data(req->bio);
2129 /* update sector only for requests with clear definition of sector */
2130 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
2131 req->__sector += total_bytes >> 9;
2133 /* mixed attributes always follow the first bio */
2134 if (req->cmd_flags & REQ_MIXED_MERGE) {
2135 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2136 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2140 * If total number of sectors is less than the first segment
2141 * size, something has gone terribly wrong.
2143 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2144 printk(KERN_ERR "blk: request botched\n");
2145 req->__data_len = blk_rq_cur_bytes(req);
2148 /* recalculate the number of segments */
2149 blk_recalc_rq_segments(req);
2153 EXPORT_SYMBOL_GPL(blk_update_request);
2155 static bool blk_update_bidi_request(struct request *rq, int error,
2156 unsigned int nr_bytes,
2157 unsigned int bidi_bytes)
2159 if (blk_update_request(rq, error, nr_bytes))
2162 /* Bidi request must be completed as a whole */
2163 if (unlikely(blk_bidi_rq(rq)) &&
2164 blk_update_request(rq->next_rq, error, bidi_bytes))
2167 if (blk_queue_add_random(rq->q))
2168 add_disk_randomness(rq->rq_disk);
2174 * blk_unprep_request - unprepare a request
2177 * This function makes a request ready for complete resubmission (or
2178 * completion). It happens only after all error handling is complete,
2179 * so represents the appropriate moment to deallocate any resources
2180 * that were allocated to the request in the prep_rq_fn. The queue
2181 * lock is held when calling this.
2183 void blk_unprep_request(struct request *req)
2185 struct request_queue *q = req->q;
2187 req->cmd_flags &= ~REQ_DONTPREP;
2188 if (q->unprep_rq_fn)
2189 q->unprep_rq_fn(q, req);
2191 EXPORT_SYMBOL_GPL(blk_unprep_request);
2194 * queue lock must be held
2196 static void blk_finish_request(struct request *req, int error)
2198 if (blk_rq_tagged(req))
2199 blk_queue_end_tag(req->q, req);
2201 BUG_ON(blk_queued_rq(req));
2203 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
2204 laptop_io_completion(&req->q->backing_dev_info);
2206 blk_delete_timer(req);
2208 if (req->cmd_flags & REQ_DONTPREP)
2209 blk_unprep_request(req);
2212 blk_account_io_done(req);
2215 req->end_io(req, error);
2217 if (blk_bidi_rq(req))
2218 __blk_put_request(req->next_rq->q, req->next_rq);
2220 __blk_put_request(req->q, req);
2225 * blk_end_bidi_request - Complete a bidi request
2226 * @rq: the request to complete
2227 * @error: %0 for success, < %0 for error
2228 * @nr_bytes: number of bytes to complete @rq
2229 * @bidi_bytes: number of bytes to complete @rq->next_rq
2232 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2233 * Drivers that supports bidi can safely call this member for any
2234 * type of request, bidi or uni. In the later case @bidi_bytes is
2238 * %false - we are done with this request
2239 * %true - still buffers pending for this request
2241 static bool blk_end_bidi_request(struct request *rq, int error,
2242 unsigned int nr_bytes, unsigned int bidi_bytes)
2244 struct request_queue *q = rq->q;
2245 unsigned long flags;
2247 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2250 spin_lock_irqsave(q->queue_lock, flags);
2251 blk_finish_request(rq, error);
2252 spin_unlock_irqrestore(q->queue_lock, flags);
2258 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2259 * @rq: the request to complete
2260 * @error: %0 for success, < %0 for error
2261 * @nr_bytes: number of bytes to complete @rq
2262 * @bidi_bytes: number of bytes to complete @rq->next_rq
2265 * Identical to blk_end_bidi_request() except that queue lock is
2266 * assumed to be locked on entry and remains so on return.
2269 * %false - we are done with this request
2270 * %true - still buffers pending for this request
2272 static bool __blk_end_bidi_request(struct request *rq, int error,
2273 unsigned int nr_bytes, unsigned int bidi_bytes)
2275 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2278 blk_finish_request(rq, error);
2284 * blk_end_request - Helper function for drivers to complete the request.
2285 * @rq: the request being processed
2286 * @error: %0 for success, < %0 for error
2287 * @nr_bytes: number of bytes to complete
2290 * Ends I/O on a number of bytes attached to @rq.
2291 * If @rq has leftover, sets it up for the next range of segments.
2294 * %false - we are done with this request
2295 * %true - still buffers pending for this request
2297 bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2299 return blk_end_bidi_request(rq, error, nr_bytes, 0);
2301 EXPORT_SYMBOL(blk_end_request);
2304 * blk_end_request_all - Helper function for drives to finish the request.
2305 * @rq: the request to finish
2306 * @error: %0 for success, < %0 for error
2309 * Completely finish @rq.
2311 void blk_end_request_all(struct request *rq, int error)
2314 unsigned int bidi_bytes = 0;
2316 if (unlikely(blk_bidi_rq(rq)))
2317 bidi_bytes = blk_rq_bytes(rq->next_rq);
2319 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2322 EXPORT_SYMBOL(blk_end_request_all);
2325 * blk_end_request_cur - Helper function to finish the current request chunk.
2326 * @rq: the request to finish the current chunk for
2327 * @error: %0 for success, < %0 for error
2330 * Complete the current consecutively mapped chunk from @rq.
2333 * %false - we are done with this request
2334 * %true - still buffers pending for this request
2336 bool blk_end_request_cur(struct request *rq, int error)
2338 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2340 EXPORT_SYMBOL(blk_end_request_cur);
2343 * blk_end_request_err - Finish a request till the next failure boundary.
2344 * @rq: the request to finish till the next failure boundary for
2345 * @error: must be negative errno
2348 * Complete @rq till the next failure boundary.
2351 * %false - we are done with this request
2352 * %true - still buffers pending for this request
2354 bool blk_end_request_err(struct request *rq, int error)
2356 WARN_ON(error >= 0);
2357 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2359 EXPORT_SYMBOL_GPL(blk_end_request_err);
2362 * __blk_end_request - Helper function for drivers to complete the request.
2363 * @rq: the request being processed
2364 * @error: %0 for success, < %0 for error
2365 * @nr_bytes: number of bytes to complete
2368 * Must be called with queue lock held unlike blk_end_request().
2371 * %false - we are done with this request
2372 * %true - still buffers pending for this request
2374 bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2376 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
2378 EXPORT_SYMBOL(__blk_end_request);
2381 * __blk_end_request_all - Helper function for drives to finish the request.
2382 * @rq: the request to finish
2383 * @error: %0 for success, < %0 for error
2386 * Completely finish @rq. Must be called with queue lock held.
2388 void __blk_end_request_all(struct request *rq, int error)
2391 unsigned int bidi_bytes = 0;
2393 if (unlikely(blk_bidi_rq(rq)))
2394 bidi_bytes = blk_rq_bytes(rq->next_rq);
2396 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2399 EXPORT_SYMBOL(__blk_end_request_all);
2402 * __blk_end_request_cur - Helper function to finish the current request chunk.
2403 * @rq: the request to finish the current chunk for
2404 * @error: %0 for success, < %0 for error
2407 * Complete the current consecutively mapped chunk from @rq. Must
2408 * be called with queue lock held.
2411 * %false - we are done with this request
2412 * %true - still buffers pending for this request
2414 bool __blk_end_request_cur(struct request *rq, int error)
2416 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2418 EXPORT_SYMBOL(__blk_end_request_cur);
2421 * __blk_end_request_err - Finish a request till the next failure boundary.
2422 * @rq: the request to finish till the next failure boundary for
2423 * @error: must be negative errno
2426 * Complete @rq till the next failure boundary. Must be called
2427 * with queue lock held.
2430 * %false - we are done with this request
2431 * %true - still buffers pending for this request
2433 bool __blk_end_request_err(struct request *rq, int error)
2435 WARN_ON(error >= 0);
2436 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2438 EXPORT_SYMBOL_GPL(__blk_end_request_err);
2440 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2443 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
2444 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
2446 if (bio_has_data(bio)) {
2447 rq->nr_phys_segments = bio_phys_segments(q, bio);
2448 rq->buffer = bio_data(bio);
2450 rq->__data_len = bio->bi_size;
2451 rq->bio = rq->biotail = bio;
2454 rq->rq_disk = bio->bi_bdev->bd_disk;
2457 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2459 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2460 * @rq: the request to be flushed
2463 * Flush all pages in @rq.
2465 void rq_flush_dcache_pages(struct request *rq)
2467 struct req_iterator iter;
2468 struct bio_vec *bvec;
2470 rq_for_each_segment(bvec, rq, iter)
2471 flush_dcache_page(bvec->bv_page);
2473 EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2477 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2478 * @q : the queue of the device being checked
2481 * Check if underlying low-level drivers of a device are busy.
2482 * If the drivers want to export their busy state, they must set own
2483 * exporting function using blk_queue_lld_busy() first.
2485 * Basically, this function is used only by request stacking drivers
2486 * to stop dispatching requests to underlying devices when underlying
2487 * devices are busy. This behavior helps more I/O merging on the queue
2488 * of the request stacking driver and prevents I/O throughput regression
2489 * on burst I/O load.
2492 * 0 - Not busy (The request stacking driver should dispatch request)
2493 * 1 - Busy (The request stacking driver should stop dispatching request)
2495 int blk_lld_busy(struct request_queue *q)
2498 return q->lld_busy_fn(q);
2502 EXPORT_SYMBOL_GPL(blk_lld_busy);
2505 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2506 * @rq: the clone request to be cleaned up
2509 * Free all bios in @rq for a cloned request.
2511 void blk_rq_unprep_clone(struct request *rq)
2515 while ((bio = rq->bio) != NULL) {
2516 rq->bio = bio->bi_next;
2521 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2524 * Copy attributes of the original request to the clone request.
2525 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2527 static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2529 dst->cpu = src->cpu;
2530 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
2531 dst->cmd_type = src->cmd_type;
2532 dst->__sector = blk_rq_pos(src);
2533 dst->__data_len = blk_rq_bytes(src);
2534 dst->nr_phys_segments = src->nr_phys_segments;
2535 dst->ioprio = src->ioprio;
2536 dst->extra_len = src->extra_len;
2540 * blk_rq_prep_clone - Helper function to setup clone request
2541 * @rq: the request to be setup
2542 * @rq_src: original request to be cloned
2543 * @bs: bio_set that bios for clone are allocated from
2544 * @gfp_mask: memory allocation mask for bio
2545 * @bio_ctr: setup function to be called for each clone bio.
2546 * Returns %0 for success, non %0 for failure.
2547 * @data: private data to be passed to @bio_ctr
2550 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2551 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2552 * are not copied, and copying such parts is the caller's responsibility.
2553 * Also, pages which the original bios are pointing to are not copied
2554 * and the cloned bios just point same pages.
2555 * So cloned bios must be completed before original bios, which means
2556 * the caller must complete @rq before @rq_src.
2558 int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2559 struct bio_set *bs, gfp_t gfp_mask,
2560 int (*bio_ctr)(struct bio *, struct bio *, void *),
2563 struct bio *bio, *bio_src;
2568 blk_rq_init(NULL, rq);
2570 __rq_for_each_bio(bio_src, rq_src) {
2571 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2575 __bio_clone(bio, bio_src);
2577 if (bio_integrity(bio_src) &&
2578 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
2581 if (bio_ctr && bio_ctr(bio, bio_src, data))
2585 rq->biotail->bi_next = bio;
2588 rq->bio = rq->biotail = bio;
2591 __blk_rq_prep_clone(rq, rq_src);
2598 blk_rq_unprep_clone(rq);
2602 EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2604 int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
2606 return queue_work(kblockd_workqueue, work);
2608 EXPORT_SYMBOL(kblockd_schedule_work);
2610 int kblockd_schedule_delayed_work(struct request_queue *q,
2611 struct delayed_work *dwork, unsigned long delay)
2613 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2615 EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2617 int __init blk_dev_init(void)
2619 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2620 sizeof(((struct request *)0)->cmd_flags));
2622 kblockd_workqueue = create_workqueue("kblockd");
2623 if (!kblockd_workqueue)
2624 panic("Failed to create kblockd\n");
2626 request_cachep = kmem_cache_create("blkdev_requests",
2627 sizeof(struct request), 0, SLAB_PANIC, NULL);
2629 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2630 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);