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
;
131 EXPORT_SYMBOL(blk_rq_init
);
133 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
134 unsigned int nbytes
, int error
)
136 struct request_queue
*q
= rq
->q
;
138 if (&q
->bar_rq
!= rq
) {
140 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
141 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
144 if (unlikely(nbytes
> bio
->bi_size
)) {
145 printk(KERN_ERR
"%s: want %u bytes done, %u left\n",
146 __func__
, nbytes
, bio
->bi_size
);
147 nbytes
= bio
->bi_size
;
150 if (unlikely(rq
->cmd_flags
& REQ_QUIET
))
151 set_bit(BIO_QUIET
, &bio
->bi_flags
);
153 bio
->bi_size
-= nbytes
;
154 bio
->bi_sector
+= (nbytes
>> 9);
156 if (bio_integrity(bio
))
157 bio_integrity_advance(bio
, nbytes
);
159 if (bio
->bi_size
== 0)
160 bio_endio(bio
, error
);
164 * Okay, this is the barrier request in progress, just
167 if (error
&& !q
->orderr
)
172 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
176 printk(KERN_INFO
"%s: dev %s: type=%x, flags=%x\n", msg
,
177 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
180 printk(KERN_INFO
" sector %llu, nr/cnr %u/%u\n",
181 (unsigned long long)blk_rq_pos(rq
),
182 blk_rq_sectors(rq
), blk_rq_cur_sectors(rq
));
183 printk(KERN_INFO
" bio %p, biotail %p, buffer %p, len %u\n",
184 rq
->bio
, rq
->biotail
, rq
->buffer
, blk_rq_bytes(rq
));
186 if (blk_pc_request(rq
)) {
187 printk(KERN_INFO
" cdb: ");
188 for (bit
= 0; bit
< BLK_MAX_CDB
; bit
++)
189 printk("%02x ", rq
->cmd
[bit
]);
193 EXPORT_SYMBOL(blk_dump_rq_flags
);
196 * "plug" the device if there are no outstanding requests: this will
197 * force the transfer to start only after we have put all the requests
200 * This is called with interrupts off and no requests on the queue and
201 * with the queue lock held.
203 void blk_plug_device(struct request_queue
*q
)
205 WARN_ON(!irqs_disabled());
208 * don't plug a stopped queue, it must be paired with blk_start_queue()
209 * which will restart the queueing
211 if (blk_queue_stopped(q
))
214 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED
, q
)) {
215 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
219 EXPORT_SYMBOL(blk_plug_device
);
222 * blk_plug_device_unlocked - plug a device without queue lock held
223 * @q: The &struct request_queue to plug
226 * Like @blk_plug_device(), but grabs the queue lock and disables
229 void blk_plug_device_unlocked(struct request_queue
*q
)
233 spin_lock_irqsave(q
->queue_lock
, flags
);
235 spin_unlock_irqrestore(q
->queue_lock
, flags
);
237 EXPORT_SYMBOL(blk_plug_device_unlocked
);
240 * remove the queue from the plugged list, if present. called with
241 * queue lock held and interrupts disabled.
243 int blk_remove_plug(struct request_queue
*q
)
245 WARN_ON(!irqs_disabled());
247 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED
, q
))
250 del_timer(&q
->unplug_timer
);
253 EXPORT_SYMBOL(blk_remove_plug
);
256 * remove the plug and let it rip..
258 void __generic_unplug_device(struct request_queue
*q
)
260 if (unlikely(blk_queue_stopped(q
)))
262 if (!blk_remove_plug(q
) && !blk_queue_nonrot(q
))
269 * generic_unplug_device - fire a request queue
270 * @q: The &struct request_queue in question
273 * Linux uses plugging to build bigger requests queues before letting
274 * the device have at them. If a queue is plugged, the I/O scheduler
275 * is still adding and merging requests on the queue. Once the queue
276 * gets unplugged, the request_fn defined for the queue is invoked and
279 void generic_unplug_device(struct request_queue
*q
)
281 if (blk_queue_plugged(q
)) {
282 spin_lock_irq(q
->queue_lock
);
283 __generic_unplug_device(q
);
284 spin_unlock_irq(q
->queue_lock
);
287 EXPORT_SYMBOL(generic_unplug_device
);
289 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
292 struct request_queue
*q
= bdi
->unplug_io_data
;
297 void blk_unplug_work(struct work_struct
*work
)
299 struct request_queue
*q
=
300 container_of(work
, struct request_queue
, unplug_work
);
302 trace_block_unplug_io(q
);
306 void blk_unplug_timeout(unsigned long data
)
308 struct request_queue
*q
= (struct request_queue
*)data
;
310 trace_block_unplug_timer(q
);
311 kblockd_schedule_work(q
, &q
->unplug_work
);
313 EXPORT_SYMBOL(blk_put_queue
);
315 void blk_unplug(struct request_queue
*q
)
318 * devices don't necessarily have an ->unplug_fn defined
321 trace_block_unplug_io(q
);
325 EXPORT_SYMBOL(blk_unplug
);
328 * blk_start_queue - restart a previously stopped queue
329 * @q: The &struct request_queue in question
332 * blk_start_queue() will clear the stop flag on the queue, and call
333 * the request_fn for the queue if it was in a stopped state when
334 * entered. Also see blk_stop_queue(). Queue lock must be held.
336 void blk_start_queue(struct request_queue
*q
)
338 WARN_ON(!irqs_disabled());
340 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
343 EXPORT_SYMBOL(blk_start_queue
);
346 * blk_stop_queue - stop a queue
347 * @q: The &struct request_queue in question
350 * The Linux block layer assumes that a block driver will consume all
351 * entries on the request queue when the request_fn strategy is called.
352 * Often this will not happen, because of hardware limitations (queue
353 * depth settings). If a device driver gets a 'queue full' response,
354 * or if it simply chooses not to queue more I/O at one point, it can
355 * call this function to prevent the request_fn from being called until
356 * the driver has signalled it's ready to go again. This happens by calling
357 * blk_start_queue() to restart queue operations. Queue lock must be held.
359 void blk_stop_queue(struct request_queue
*q
)
362 queue_flag_set(QUEUE_FLAG_STOPPED
, q
);
364 EXPORT_SYMBOL(blk_stop_queue
);
367 * blk_sync_queue - cancel any pending callbacks on a queue
371 * The block layer may perform asynchronous callback activity
372 * on a queue, such as calling the unplug function after a timeout.
373 * A block device may call blk_sync_queue to ensure that any
374 * such activity is cancelled, thus allowing it to release resources
375 * that the callbacks might use. The caller must already have made sure
376 * that its ->make_request_fn will not re-add plugging prior to calling
380 void blk_sync_queue(struct request_queue
*q
)
382 del_timer_sync(&q
->unplug_timer
);
383 del_timer_sync(&q
->timeout
);
384 cancel_work_sync(&q
->unplug_work
);
386 EXPORT_SYMBOL(blk_sync_queue
);
389 * __blk_run_queue - run a single device queue
390 * @q: The queue to run
393 * See @blk_run_queue. This variant must be called with the queue lock
394 * held and interrupts disabled.
397 void __blk_run_queue(struct request_queue
*q
)
401 if (unlikely(blk_queue_stopped(q
)))
404 if (elv_queue_empty(q
))
408 * Only recurse once to avoid overrunning the stack, let the unplug
409 * handling reinvoke the handler shortly if we already got there.
411 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
413 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
415 queue_flag_set(QUEUE_FLAG_PLUGGED
, q
);
416 kblockd_schedule_work(q
, &q
->unplug_work
);
419 EXPORT_SYMBOL(__blk_run_queue
);
422 * blk_run_queue - run a single device queue
423 * @q: The queue to run
426 * Invoke request handling on this queue, if it has pending work to do.
427 * May be used to restart queueing when a request has completed.
429 void blk_run_queue(struct request_queue
*q
)
433 spin_lock_irqsave(q
->queue_lock
, flags
);
435 spin_unlock_irqrestore(q
->queue_lock
, flags
);
437 EXPORT_SYMBOL(blk_run_queue
);
439 void blk_put_queue(struct request_queue
*q
)
441 kobject_put(&q
->kobj
);
444 void blk_cleanup_queue(struct request_queue
*q
)
447 * We know we have process context here, so we can be a little
448 * cautious and ensure that pending block actions on this device
449 * are done before moving on. Going into this function, we should
450 * not have processes doing IO to this device.
454 mutex_lock(&q
->sysfs_lock
);
455 queue_flag_set_unlocked(QUEUE_FLAG_DEAD
, q
);
456 mutex_unlock(&q
->sysfs_lock
);
459 elevator_exit(q
->elevator
);
463 EXPORT_SYMBOL(blk_cleanup_queue
);
465 static int blk_init_free_list(struct request_queue
*q
)
467 struct request_list
*rl
= &q
->rq
;
469 rl
->count
[BLK_RW_SYNC
] = rl
->count
[BLK_RW_ASYNC
] = 0;
470 rl
->starved
[BLK_RW_SYNC
] = rl
->starved
[BLK_RW_ASYNC
] = 0;
472 init_waitqueue_head(&rl
->wait
[BLK_RW_SYNC
]);
473 init_waitqueue_head(&rl
->wait
[BLK_RW_ASYNC
]);
475 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
476 mempool_free_slab
, request_cachep
, q
->node
);
484 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
486 return blk_alloc_queue_node(gfp_mask
, -1);
488 EXPORT_SYMBOL(blk_alloc_queue
);
490 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
492 struct request_queue
*q
;
495 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
496 gfp_mask
| __GFP_ZERO
, node_id
);
500 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
501 q
->backing_dev_info
.unplug_io_data
= q
;
502 q
->backing_dev_info
.ra_pages
=
503 (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
504 q
->backing_dev_info
.state
= 0;
505 q
->backing_dev_info
.capabilities
= BDI_CAP_MAP_COPY
;
506 q
->backing_dev_info
.name
= "block";
508 err
= bdi_init(&q
->backing_dev_info
);
510 kmem_cache_free(blk_requestq_cachep
, q
);
514 init_timer(&q
->unplug_timer
);
515 setup_timer(&q
->timeout
, blk_rq_timed_out_timer
, (unsigned long) q
);
516 INIT_LIST_HEAD(&q
->timeout_list
);
517 INIT_WORK(&q
->unplug_work
, blk_unplug_work
);
519 kobject_init(&q
->kobj
, &blk_queue_ktype
);
521 mutex_init(&q
->sysfs_lock
);
522 spin_lock_init(&q
->__queue_lock
);
526 EXPORT_SYMBOL(blk_alloc_queue_node
);
529 * blk_init_queue - prepare a request queue for use with a block device
530 * @rfn: The function to be called to process requests that have been
531 * placed on the queue.
532 * @lock: Request queue spin lock
535 * If a block device wishes to use the standard request handling procedures,
536 * which sorts requests and coalesces adjacent requests, then it must
537 * call blk_init_queue(). The function @rfn will be called when there
538 * are requests on the queue that need to be processed. If the device
539 * supports plugging, then @rfn may not be called immediately when requests
540 * are available on the queue, but may be called at some time later instead.
541 * Plugged queues are generally unplugged when a buffer belonging to one
542 * of the requests on the queue is needed, or due to memory pressure.
544 * @rfn is not required, or even expected, to remove all requests off the
545 * queue, but only as many as it can handle at a time. If it does leave
546 * requests on the queue, it is responsible for arranging that the requests
547 * get dealt with eventually.
549 * The queue spin lock must be held while manipulating the requests on the
550 * request queue; this lock will be taken also from interrupt context, so irq
551 * disabling is needed for it.
553 * Function returns a pointer to the initialized request queue, or %NULL if
557 * blk_init_queue() must be paired with a blk_cleanup_queue() call
558 * when the block device is deactivated (such as at module unload).
561 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
563 return blk_init_queue_node(rfn
, lock
, -1);
565 EXPORT_SYMBOL(blk_init_queue
);
567 struct request_queue
*
568 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
570 struct request_queue
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
576 if (blk_init_free_list(q
)) {
577 kmem_cache_free(blk_requestq_cachep
, q
);
582 q
->prep_rq_fn
= NULL
;
583 q
->unplug_fn
= generic_unplug_device
;
584 q
->queue_flags
= QUEUE_FLAG_DEFAULT
;
585 q
->queue_lock
= lock
;
588 * This also sets hw/phys segments, boundary and size
590 blk_queue_make_request(q
, __make_request
);
592 q
->sg_reserved_size
= INT_MAX
;
597 if (!elevator_init(q
, NULL
)) {
598 blk_queue_congestion_threshold(q
);
605 EXPORT_SYMBOL(blk_init_queue_node
);
607 int blk_get_queue(struct request_queue
*q
)
609 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
610 kobject_get(&q
->kobj
);
616 EXPORT_SYMBOL(blk_get_queue
);
618 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
620 if (rq
->cmd_flags
& REQ_ELVPRIV
)
621 elv_put_request(q
, rq
);
622 mempool_free(rq
, q
->rq
.rq_pool
);
625 static struct request
*
626 blk_alloc_request(struct request_queue
*q
, int flags
, int priv
, gfp_t gfp_mask
)
628 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
635 rq
->cmd_flags
= flags
| REQ_ALLOCED
;
638 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
639 mempool_free(rq
, q
->rq
.rq_pool
);
642 rq
->cmd_flags
|= REQ_ELVPRIV
;
649 * ioc_batching returns true if the ioc is a valid batching request and
650 * should be given priority access to a request.
652 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
658 * Make sure the process is able to allocate at least 1 request
659 * even if the batch times out, otherwise we could theoretically
662 return ioc
->nr_batch_requests
== q
->nr_batching
||
663 (ioc
->nr_batch_requests
> 0
664 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
668 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
669 * will cause the process to be a "batcher" on all queues in the system. This
670 * is the behaviour we want though - once it gets a wakeup it should be given
673 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
675 if (!ioc
|| ioc_batching(q
, ioc
))
678 ioc
->nr_batch_requests
= q
->nr_batching
;
679 ioc
->last_waited
= jiffies
;
682 static void __freed_request(struct request_queue
*q
, int sync
)
684 struct request_list
*rl
= &q
->rq
;
686 if (rl
->count
[sync
] < queue_congestion_off_threshold(q
))
687 blk_clear_queue_congested(q
, sync
);
689 if (rl
->count
[sync
] + 1 <= q
->nr_requests
) {
690 if (waitqueue_active(&rl
->wait
[sync
]))
691 wake_up(&rl
->wait
[sync
]);
693 blk_clear_queue_full(q
, sync
);
698 * A request has just been released. Account for it, update the full and
699 * congestion status, wake up any waiters. Called under q->queue_lock.
701 static void freed_request(struct request_queue
*q
, int sync
, int priv
)
703 struct request_list
*rl
= &q
->rq
;
709 __freed_request(q
, sync
);
711 if (unlikely(rl
->starved
[sync
^ 1]))
712 __freed_request(q
, sync
^ 1);
716 * Get a free request, queue_lock must be held.
717 * Returns NULL on failure, with queue_lock held.
718 * Returns !NULL on success, with queue_lock *not held*.
720 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
721 struct bio
*bio
, gfp_t gfp_mask
)
723 struct request
*rq
= NULL
;
724 struct request_list
*rl
= &q
->rq
;
725 struct io_context
*ioc
= NULL
;
726 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
729 may_queue
= elv_may_queue(q
, rw_flags
);
730 if (may_queue
== ELV_MQUEUE_NO
)
733 if (rl
->count
[is_sync
]+1 >= queue_congestion_on_threshold(q
)) {
734 if (rl
->count
[is_sync
]+1 >= q
->nr_requests
) {
735 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
737 * The queue will fill after this allocation, so set
738 * it as full, and mark this process as "batching".
739 * This process will be allowed to complete a batch of
740 * requests, others will be blocked.
742 if (!blk_queue_full(q
, is_sync
)) {
743 ioc_set_batching(q
, ioc
);
744 blk_set_queue_full(q
, is_sync
);
746 if (may_queue
!= ELV_MQUEUE_MUST
747 && !ioc_batching(q
, ioc
)) {
749 * The queue is full and the allocating
750 * process is not a "batcher", and not
751 * exempted by the IO scheduler
757 blk_set_queue_congested(q
, is_sync
);
761 * Only allow batching queuers to allocate up to 50% over the defined
762 * limit of requests, otherwise we could have thousands of requests
763 * allocated with any setting of ->nr_requests
765 if (rl
->count
[is_sync
] >= (3 * q
->nr_requests
/ 2))
768 rl
->count
[is_sync
]++;
769 rl
->starved
[is_sync
] = 0;
771 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
775 if (blk_queue_io_stat(q
))
776 rw_flags
|= REQ_IO_STAT
;
777 spin_unlock_irq(q
->queue_lock
);
779 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
782 * Allocation failed presumably due to memory. Undo anything
783 * we might have messed up.
785 * Allocating task should really be put onto the front of the
786 * wait queue, but this is pretty rare.
788 spin_lock_irq(q
->queue_lock
);
789 freed_request(q
, is_sync
, priv
);
792 * in the very unlikely event that allocation failed and no
793 * requests for this direction was pending, mark us starved
794 * so that freeing of a request in the other direction will
795 * notice us. another possible fix would be to split the
796 * rq mempool into READ and WRITE
799 if (unlikely(rl
->count
[is_sync
] == 0))
800 rl
->starved
[is_sync
] = 1;
806 * ioc may be NULL here, and ioc_batching will be false. That's
807 * OK, if the queue is under the request limit then requests need
808 * not count toward the nr_batch_requests limit. There will always
809 * be some limit enforced by BLK_BATCH_TIME.
811 if (ioc_batching(q
, ioc
))
812 ioc
->nr_batch_requests
--;
814 trace_block_getrq(q
, bio
, rw_flags
& 1);
820 * No available requests for this queue, unplug the device and wait for some
821 * requests to become available.
823 * Called with q->queue_lock held, and returns with it unlocked.
825 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
828 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
831 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
834 struct io_context
*ioc
;
835 struct request_list
*rl
= &q
->rq
;
837 prepare_to_wait_exclusive(&rl
->wait
[is_sync
], &wait
,
838 TASK_UNINTERRUPTIBLE
);
840 trace_block_sleeprq(q
, bio
, rw_flags
& 1);
842 __generic_unplug_device(q
);
843 spin_unlock_irq(q
->queue_lock
);
847 * After sleeping, we become a "batching" process and
848 * will be able to allocate at least one request, and
849 * up to a big batch of them for a small period time.
850 * See ioc_batching, ioc_set_batching
852 ioc
= current_io_context(GFP_NOIO
, q
->node
);
853 ioc_set_batching(q
, ioc
);
855 spin_lock_irq(q
->queue_lock
);
856 finish_wait(&rl
->wait
[is_sync
], &wait
);
858 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
864 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
868 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
870 spin_lock_irq(q
->queue_lock
);
871 if (gfp_mask
& __GFP_WAIT
) {
872 rq
= get_request_wait(q
, rw
, NULL
);
874 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
876 spin_unlock_irq(q
->queue_lock
);
878 /* q->queue_lock is unlocked at this point */
882 EXPORT_SYMBOL(blk_get_request
);
885 * blk_make_request - given a bio, allocate a corresponding struct request.
886 * @q: target request queue
887 * @bio: The bio describing the memory mappings that will be submitted for IO.
888 * It may be a chained-bio properly constructed by block/bio layer.
889 * @gfp_mask: gfp flags to be used for memory allocation
891 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
892 * type commands. Where the struct request needs to be farther initialized by
893 * the caller. It is passed a &struct bio, which describes the memory info of
896 * The caller of blk_make_request must make sure that bi_io_vec
897 * are set to describe the memory buffers. That bio_data_dir() will return
898 * the needed direction of the request. (And all bio's in the passed bio-chain
899 * are properly set accordingly)
901 * If called under none-sleepable conditions, mapped bio buffers must not
902 * need bouncing, by calling the appropriate masked or flagged allocator,
903 * suitable for the target device. Otherwise the call to blk_queue_bounce will
906 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
907 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
908 * anything but the first bio in the chain. Otherwise you risk waiting for IO
909 * completion of a bio that hasn't been submitted yet, thus resulting in a
910 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
911 * of bio_alloc(), as that avoids the mempool deadlock.
912 * If possible a big IO should be split into smaller parts when allocation
913 * fails. Partial allocation should not be an error, or you risk a live-lock.
915 struct request
*blk_make_request(struct request_queue
*q
, struct bio
*bio
,
918 struct request
*rq
= blk_get_request(q
, bio_data_dir(bio
), gfp_mask
);
921 return ERR_PTR(-ENOMEM
);
924 struct bio
*bounce_bio
= bio
;
927 blk_queue_bounce(q
, &bounce_bio
);
928 ret
= blk_rq_append_bio(q
, rq
, bounce_bio
);
937 EXPORT_SYMBOL(blk_make_request
);
940 * blk_requeue_request - put a request back on queue
941 * @q: request queue where request should be inserted
942 * @rq: request to be inserted
945 * Drivers often keep queueing requests until the hardware cannot accept
946 * more, when that condition happens we need to put the request back
947 * on the queue. Must be called with queue lock held.
949 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
951 blk_delete_timer(rq
);
952 blk_clear_rq_complete(rq
);
953 trace_block_rq_requeue(q
, rq
);
955 if (blk_rq_tagged(rq
))
956 blk_queue_end_tag(q
, rq
);
958 BUG_ON(blk_queued_rq(rq
));
960 elv_requeue_request(q
, rq
);
962 EXPORT_SYMBOL(blk_requeue_request
);
965 * blk_insert_request - insert a special request into a request queue
966 * @q: request queue where request should be inserted
967 * @rq: request to be inserted
968 * @at_head: insert request at head or tail of queue
969 * @data: private data
972 * Many block devices need to execute commands asynchronously, so they don't
973 * block the whole kernel from preemption during request execution. This is
974 * accomplished normally by inserting aritficial requests tagged as
975 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
976 * be scheduled for actual execution by the request queue.
978 * We have the option of inserting the head or the tail of the queue.
979 * Typically we use the tail for new ioctls and so forth. We use the head
980 * of the queue for things like a QUEUE_FULL message from a device, or a
981 * host that is unable to accept a particular command.
983 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
984 int at_head
, void *data
)
986 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
990 * tell I/O scheduler that this isn't a regular read/write (ie it
991 * must not attempt merges on this) and that it acts as a soft
994 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
998 spin_lock_irqsave(q
->queue_lock
, flags
);
1001 * If command is tagged, release the tag
1003 if (blk_rq_tagged(rq
))
1004 blk_queue_end_tag(q
, rq
);
1006 drive_stat_acct(rq
, 1);
1007 __elv_add_request(q
, rq
, where
, 0);
1009 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1011 EXPORT_SYMBOL(blk_insert_request
);
1014 * add-request adds a request to the linked list.
1015 * queue lock is held and interrupts disabled, as we muck with the
1016 * request queue list.
1018 static inline void add_request(struct request_queue
*q
, struct request
*req
)
1020 drive_stat_acct(req
, 1);
1023 * elevator indicated where it wants this request to be
1024 * inserted at elevator_merge time
1026 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
1029 static void part_round_stats_single(int cpu
, struct hd_struct
*part
,
1032 if (now
== part
->stamp
)
1035 if (part_in_flight(part
)) {
1036 __part_stat_add(cpu
, part
, time_in_queue
,
1037 part_in_flight(part
) * (now
- part
->stamp
));
1038 __part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
1044 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1045 * @cpu: cpu number for stats access
1046 * @part: target partition
1048 * The average IO queue length and utilisation statistics are maintained
1049 * by observing the current state of the queue length and the amount of
1050 * time it has been in this state for.
1052 * Normally, that accounting is done on IO completion, but that can result
1053 * in more than a second's worth of IO being accounted for within any one
1054 * second, leading to >100% utilisation. To deal with that, we call this
1055 * function to do a round-off before returning the results when reading
1056 * /proc/diskstats. This accounts immediately for all queue usage up to
1057 * the current jiffies and restarts the counters again.
1059 void part_round_stats(int cpu
, struct hd_struct
*part
)
1061 unsigned long now
= jiffies
;
1064 part_round_stats_single(cpu
, &part_to_disk(part
)->part0
, now
);
1065 part_round_stats_single(cpu
, part
, now
);
1067 EXPORT_SYMBOL_GPL(part_round_stats
);
1070 * queue lock must be held
1072 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1076 if (unlikely(--req
->ref_count
))
1079 elv_completed_request(q
, req
);
1081 /* this is a bio leak */
1082 WARN_ON(req
->bio
!= NULL
);
1085 * Request may not have originated from ll_rw_blk. if not,
1086 * it didn't come out of our reserved rq pools
1088 if (req
->cmd_flags
& REQ_ALLOCED
) {
1089 int is_sync
= rq_is_sync(req
) != 0;
1090 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1092 BUG_ON(!list_empty(&req
->queuelist
));
1093 BUG_ON(!hlist_unhashed(&req
->hash
));
1095 blk_free_request(q
, req
);
1096 freed_request(q
, is_sync
, priv
);
1099 EXPORT_SYMBOL_GPL(__blk_put_request
);
1101 void blk_put_request(struct request
*req
)
1103 unsigned long flags
;
1104 struct request_queue
*q
= req
->q
;
1106 spin_lock_irqsave(q
->queue_lock
, flags
);
1107 __blk_put_request(q
, req
);
1108 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1110 EXPORT_SYMBOL(blk_put_request
);
1112 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1114 req
->cpu
= bio
->bi_comp_cpu
;
1115 req
->cmd_type
= REQ_TYPE_FS
;
1118 * Inherit FAILFAST from bio (for read-ahead, and explicit
1119 * FAILFAST). FAILFAST flags are identical for req and bio.
1121 if (bio_rw_flagged(bio
, BIO_RW_AHEAD
))
1122 req
->cmd_flags
|= REQ_FAILFAST_MASK
;
1124 req
->cmd_flags
|= bio
->bi_rw
& REQ_FAILFAST_MASK
;
1126 if (unlikely(bio_rw_flagged(bio
, BIO_RW_DISCARD
))) {
1127 req
->cmd_flags
|= REQ_DISCARD
;
1128 if (bio_rw_flagged(bio
, BIO_RW_BARRIER
))
1129 req
->cmd_flags
|= REQ_SOFTBARRIER
;
1130 } else if (unlikely(bio_rw_flagged(bio
, BIO_RW_BARRIER
)))
1131 req
->cmd_flags
|= REQ_HARDBARRIER
;
1133 if (bio_rw_flagged(bio
, BIO_RW_SYNCIO
))
1134 req
->cmd_flags
|= REQ_RW_SYNC
;
1135 if (bio_rw_flagged(bio
, BIO_RW_META
))
1136 req
->cmd_flags
|= REQ_RW_META
;
1137 if (bio_rw_flagged(bio
, BIO_RW_NOIDLE
))
1138 req
->cmd_flags
|= REQ_NOIDLE
;
1141 req
->__sector
= bio
->bi_sector
;
1142 req
->ioprio
= bio_prio(bio
);
1143 blk_rq_bio_prep(req
->q
, req
, bio
);
1147 * Only disabling plugging for non-rotational devices if it does tagging
1148 * as well, otherwise we do need the proper merging
1150 static inline bool queue_should_plug(struct request_queue
*q
)
1152 return !(blk_queue_nonrot(q
) && blk_queue_queuing(q
));
1155 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1157 struct request
*req
;
1159 unsigned int bytes
= bio
->bi_size
;
1160 const unsigned short prio
= bio_prio(bio
);
1161 const bool sync
= bio_rw_flagged(bio
, BIO_RW_SYNCIO
);
1162 const bool unplug
= bio_rw_flagged(bio
, BIO_RW_UNPLUG
);
1163 const unsigned int ff
= bio
->bi_rw
& REQ_FAILFAST_MASK
;
1166 if (bio_rw_flagged(bio
, BIO_RW_BARRIER
) &&
1167 (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
1168 bio_endio(bio
, -EOPNOTSUPP
);
1172 * low level driver can indicate that it wants pages above a
1173 * certain limit bounced to low memory (ie for highmem, or even
1174 * ISA dma in theory)
1176 blk_queue_bounce(q
, &bio
);
1178 spin_lock_irq(q
->queue_lock
);
1180 if (unlikely(bio_rw_flagged(bio
, BIO_RW_BARRIER
)) || elv_queue_empty(q
))
1183 el_ret
= elv_merge(q
, &req
, bio
);
1185 case ELEVATOR_BACK_MERGE
:
1186 BUG_ON(!rq_mergeable(req
));
1188 if (!ll_back_merge_fn(q
, req
, bio
))
1191 trace_block_bio_backmerge(q
, bio
);
1193 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
)
1194 blk_rq_set_mixed_merge(req
);
1196 req
->biotail
->bi_next
= bio
;
1198 req
->__data_len
+= bytes
;
1199 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1200 if (!blk_rq_cpu_valid(req
))
1201 req
->cpu
= bio
->bi_comp_cpu
;
1202 drive_stat_acct(req
, 0);
1203 if (!attempt_back_merge(q
, req
))
1204 elv_merged_request(q
, req
, el_ret
);
1207 case ELEVATOR_FRONT_MERGE
:
1208 BUG_ON(!rq_mergeable(req
));
1210 if (!ll_front_merge_fn(q
, req
, bio
))
1213 trace_block_bio_frontmerge(q
, bio
);
1215 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
) {
1216 blk_rq_set_mixed_merge(req
);
1217 req
->cmd_flags
&= ~REQ_FAILFAST_MASK
;
1218 req
->cmd_flags
|= ff
;
1221 bio
->bi_next
= req
->bio
;
1225 * may not be valid. if the low level driver said
1226 * it didn't need a bounce buffer then it better
1227 * not touch req->buffer either...
1229 req
->buffer
= bio_data(bio
);
1230 req
->__sector
= bio
->bi_sector
;
1231 req
->__data_len
+= bytes
;
1232 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1233 if (!blk_rq_cpu_valid(req
))
1234 req
->cpu
= bio
->bi_comp_cpu
;
1235 drive_stat_acct(req
, 0);
1236 if (!attempt_front_merge(q
, req
))
1237 elv_merged_request(q
, req
, el_ret
);
1240 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1247 * This sync check and mask will be re-done in init_request_from_bio(),
1248 * but we need to set it earlier to expose the sync flag to the
1249 * rq allocator and io schedulers.
1251 rw_flags
= bio_data_dir(bio
);
1253 rw_flags
|= REQ_RW_SYNC
;
1256 * Grab a free request. This is might sleep but can not fail.
1257 * Returns with the queue unlocked.
1259 req
= get_request_wait(q
, rw_flags
, bio
);
1262 * After dropping the lock and possibly sleeping here, our request
1263 * may now be mergeable after it had proven unmergeable (above).
1264 * We don't worry about that case for efficiency. It won't happen
1265 * often, and the elevators are able to handle it.
1267 init_request_from_bio(req
, bio
);
1269 spin_lock_irq(q
->queue_lock
);
1270 if (test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
) ||
1271 bio_flagged(bio
, BIO_CPU_AFFINE
))
1272 req
->cpu
= blk_cpu_to_group(smp_processor_id());
1273 if (queue_should_plug(q
) && elv_queue_empty(q
))
1275 add_request(q
, req
);
1277 if (unplug
|| !queue_should_plug(q
))
1278 __generic_unplug_device(q
);
1279 spin_unlock_irq(q
->queue_lock
);
1284 * If bio->bi_dev is a partition, remap the location
1286 static inline void blk_partition_remap(struct bio
*bio
)
1288 struct block_device
*bdev
= bio
->bi_bdev
;
1290 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1291 struct hd_struct
*p
= bdev
->bd_part
;
1293 bio
->bi_sector
+= p
->start_sect
;
1294 bio
->bi_bdev
= bdev
->bd_contains
;
1296 trace_block_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1298 bio
->bi_sector
- p
->start_sect
);
1302 static void handle_bad_sector(struct bio
*bio
)
1304 char b
[BDEVNAME_SIZE
];
1306 printk(KERN_INFO
"attempt to access beyond end of device\n");
1307 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1308 bdevname(bio
->bi_bdev
, b
),
1310 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1311 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
1313 set_bit(BIO_EOF
, &bio
->bi_flags
);
1316 #ifdef CONFIG_FAIL_MAKE_REQUEST
1318 static DECLARE_FAULT_ATTR(fail_make_request
);
1320 static int __init
setup_fail_make_request(char *str
)
1322 return setup_fault_attr(&fail_make_request
, str
);
1324 __setup("fail_make_request=", setup_fail_make_request
);
1326 static int should_fail_request(struct bio
*bio
)
1328 struct hd_struct
*part
= bio
->bi_bdev
->bd_part
;
1330 if (part_to_disk(part
)->part0
.make_it_fail
|| part
->make_it_fail
)
1331 return should_fail(&fail_make_request
, bio
->bi_size
);
1336 static int __init
fail_make_request_debugfs(void)
1338 return init_fault_attr_dentries(&fail_make_request
,
1339 "fail_make_request");
1342 late_initcall(fail_make_request_debugfs
);
1344 #else /* CONFIG_FAIL_MAKE_REQUEST */
1346 static inline int should_fail_request(struct bio
*bio
)
1351 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1354 * Check whether this bio extends beyond the end of the device.
1356 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1363 /* Test device or partition size, when known. */
1364 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
1366 sector_t sector
= bio
->bi_sector
;
1368 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1370 * This may well happen - the kernel calls bread()
1371 * without checking the size of the device, e.g., when
1372 * mounting a device.
1374 handle_bad_sector(bio
);
1383 * generic_make_request - hand a buffer to its device driver for I/O
1384 * @bio: The bio describing the location in memory and on the device.
1386 * generic_make_request() is used to make I/O requests of block
1387 * devices. It is passed a &struct bio, which describes the I/O that needs
1390 * generic_make_request() does not return any status. The
1391 * success/failure status of the request, along with notification of
1392 * completion, is delivered asynchronously through the bio->bi_end_io
1393 * function described (one day) else where.
1395 * The caller of generic_make_request must make sure that bi_io_vec
1396 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1397 * set to describe the device address, and the
1398 * bi_end_io and optionally bi_private are set to describe how
1399 * completion notification should be signaled.
1401 * generic_make_request and the drivers it calls may use bi_next if this
1402 * bio happens to be merged with someone else, and may change bi_dev and
1403 * bi_sector for remaps as it sees fit. So the values of these fields
1404 * should NOT be depended on after the call to generic_make_request.
1406 static inline void __generic_make_request(struct bio
*bio
)
1408 struct request_queue
*q
;
1409 sector_t old_sector
;
1410 int ret
, nr_sectors
= bio_sectors(bio
);
1416 if (bio_check_eod(bio
, nr_sectors
))
1420 * Resolve the mapping until finished. (drivers are
1421 * still free to implement/resolve their own stacking
1422 * by explicitly returning 0)
1424 * NOTE: we don't repeat the blk_size check for each new device.
1425 * Stacking drivers are expected to know what they are doing.
1430 char b
[BDEVNAME_SIZE
];
1432 q
= bdev_get_queue(bio
->bi_bdev
);
1435 "generic_make_request: Trying to access "
1436 "nonexistent block-device %s (%Lu)\n",
1437 bdevname(bio
->bi_bdev
, b
),
1438 (long long) bio
->bi_sector
);
1442 if (unlikely(!bio_rw_flagged(bio
, BIO_RW_DISCARD
) &&
1443 nr_sectors
> queue_max_hw_sectors(q
))) {
1444 printk(KERN_ERR
"bio too big device %s (%u > %u)\n",
1445 bdevname(bio
->bi_bdev
, b
),
1447 queue_max_hw_sectors(q
));
1451 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1454 if (should_fail_request(bio
))
1458 * If this device has partitions, remap block n
1459 * of partition p to block n+start(p) of the disk.
1461 blk_partition_remap(bio
);
1463 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
))
1466 if (old_sector
!= -1)
1467 trace_block_remap(q
, bio
, old_dev
, old_sector
);
1469 old_sector
= bio
->bi_sector
;
1470 old_dev
= bio
->bi_bdev
->bd_dev
;
1472 if (bio_check_eod(bio
, nr_sectors
))
1475 if (bio_rw_flagged(bio
, BIO_RW_DISCARD
) &&
1476 !blk_queue_discard(q
)) {
1481 trace_block_bio_queue(q
, bio
);
1483 ret
= q
->make_request_fn(q
, bio
);
1489 bio_endio(bio
, err
);
1493 * We only want one ->make_request_fn to be active at a time,
1494 * else stack usage with stacked devices could be a problem.
1495 * So use current->bio_{list,tail} to keep a list of requests
1496 * submited by a make_request_fn function.
1497 * current->bio_tail is also used as a flag to say if
1498 * generic_make_request is currently active in this task or not.
1499 * If it is NULL, then no make_request is active. If it is non-NULL,
1500 * then a make_request is active, and new requests should be added
1503 void generic_make_request(struct bio
*bio
)
1505 if (current
->bio_tail
) {
1506 /* make_request is active */
1507 *(current
->bio_tail
) = bio
;
1508 bio
->bi_next
= NULL
;
1509 current
->bio_tail
= &bio
->bi_next
;
1512 /* following loop may be a bit non-obvious, and so deserves some
1514 * Before entering the loop, bio->bi_next is NULL (as all callers
1515 * ensure that) so we have a list with a single bio.
1516 * We pretend that we have just taken it off a longer list, so
1517 * we assign bio_list to the next (which is NULL) and bio_tail
1518 * to &bio_list, thus initialising the bio_list of new bios to be
1519 * added. __generic_make_request may indeed add some more bios
1520 * through a recursive call to generic_make_request. If it
1521 * did, we find a non-NULL value in bio_list and re-enter the loop
1522 * from the top. In this case we really did just take the bio
1523 * of the top of the list (no pretending) and so fixup bio_list and
1524 * bio_tail or bi_next, and call into __generic_make_request again.
1526 * The loop was structured like this to make only one call to
1527 * __generic_make_request (which is important as it is large and
1528 * inlined) and to keep the structure simple.
1530 BUG_ON(bio
->bi_next
);
1532 current
->bio_list
= bio
->bi_next
;
1533 if (bio
->bi_next
== NULL
)
1534 current
->bio_tail
= ¤t
->bio_list
;
1536 bio
->bi_next
= NULL
;
1537 __generic_make_request(bio
);
1538 bio
= current
->bio_list
;
1540 current
->bio_tail
= NULL
; /* deactivate */
1542 EXPORT_SYMBOL(generic_make_request
);
1545 * submit_bio - submit a bio to the block device layer for I/O
1546 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1547 * @bio: The &struct bio which describes the I/O
1549 * submit_bio() is very similar in purpose to generic_make_request(), and
1550 * uses that function to do most of the work. Both are fairly rough
1551 * interfaces; @bio must be presetup and ready for I/O.
1554 void submit_bio(int rw
, struct bio
*bio
)
1556 int count
= bio_sectors(bio
);
1561 * If it's a regular read/write or a barrier with data attached,
1562 * go through the normal accounting stuff before submission.
1564 if (bio_has_data(bio
)) {
1566 count_vm_events(PGPGOUT
, count
);
1568 task_io_account_read(bio
->bi_size
);
1569 count_vm_events(PGPGIN
, count
);
1572 if (unlikely(block_dump
)) {
1573 char b
[BDEVNAME_SIZE
];
1574 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
1575 current
->comm
, task_pid_nr(current
),
1576 (rw
& WRITE
) ? "WRITE" : "READ",
1577 (unsigned long long)bio
->bi_sector
,
1578 bdevname(bio
->bi_bdev
, b
));
1582 generic_make_request(bio
);
1584 EXPORT_SYMBOL(submit_bio
);
1587 * blk_rq_check_limits - Helper function to check a request for the queue limit
1589 * @rq: the request being checked
1592 * @rq may have been made based on weaker limitations of upper-level queues
1593 * in request stacking drivers, and it may violate the limitation of @q.
1594 * Since the block layer and the underlying device driver trust @rq
1595 * after it is inserted to @q, it should be checked against @q before
1596 * the insertion using this generic function.
1598 * This function should also be useful for request stacking drivers
1599 * in some cases below, so export this fuction.
1600 * Request stacking drivers like request-based dm may change the queue
1601 * limits while requests are in the queue (e.g. dm's table swapping).
1602 * Such request stacking drivers should check those requests agaist
1603 * the new queue limits again when they dispatch those requests,
1604 * although such checkings are also done against the old queue limits
1605 * when submitting requests.
1607 int blk_rq_check_limits(struct request_queue
*q
, struct request
*rq
)
1609 if (blk_rq_sectors(rq
) > queue_max_sectors(q
) ||
1610 blk_rq_bytes(rq
) > queue_max_hw_sectors(q
) << 9) {
1611 printk(KERN_ERR
"%s: over max size limit.\n", __func__
);
1616 * queue's settings related to segment counting like q->bounce_pfn
1617 * may differ from that of other stacking queues.
1618 * Recalculate it to check the request correctly on this queue's
1621 blk_recalc_rq_segments(rq
);
1622 if (rq
->nr_phys_segments
> queue_max_phys_segments(q
) ||
1623 rq
->nr_phys_segments
> queue_max_hw_segments(q
)) {
1624 printk(KERN_ERR
"%s: over max segments limit.\n", __func__
);
1630 EXPORT_SYMBOL_GPL(blk_rq_check_limits
);
1633 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1634 * @q: the queue to submit the request
1635 * @rq: the request being queued
1637 int blk_insert_cloned_request(struct request_queue
*q
, struct request
*rq
)
1639 unsigned long flags
;
1641 if (blk_rq_check_limits(q
, rq
))
1644 #ifdef CONFIG_FAIL_MAKE_REQUEST
1645 if (rq
->rq_disk
&& rq
->rq_disk
->part0
.make_it_fail
&&
1646 should_fail(&fail_make_request
, blk_rq_bytes(rq
)))
1650 spin_lock_irqsave(q
->queue_lock
, flags
);
1653 * Submitting request must be dequeued before calling this function
1654 * because it will be linked to another request_queue
1656 BUG_ON(blk_queued_rq(rq
));
1658 drive_stat_acct(rq
, 1);
1659 __elv_add_request(q
, rq
, ELEVATOR_INSERT_BACK
, 0);
1661 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1665 EXPORT_SYMBOL_GPL(blk_insert_cloned_request
);
1668 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1669 * @rq: request to examine
1672 * A request could be merge of IOs which require different failure
1673 * handling. This function determines the number of bytes which
1674 * can be failed from the beginning of the request without
1675 * crossing into area which need to be retried further.
1678 * The number of bytes to fail.
1681 * queue_lock must be held.
1683 unsigned int blk_rq_err_bytes(const struct request
*rq
)
1685 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
1686 unsigned int bytes
= 0;
1689 if (!(rq
->cmd_flags
& REQ_MIXED_MERGE
))
1690 return blk_rq_bytes(rq
);
1693 * Currently the only 'mixing' which can happen is between
1694 * different fastfail types. We can safely fail portions
1695 * which have all the failfast bits that the first one has -
1696 * the ones which are at least as eager to fail as the first
1699 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
1700 if ((bio
->bi_rw
& ff
) != ff
)
1702 bytes
+= bio
->bi_size
;
1705 /* this could lead to infinite loop */
1706 BUG_ON(blk_rq_bytes(rq
) && !bytes
);
1709 EXPORT_SYMBOL_GPL(blk_rq_err_bytes
);
1711 static void blk_account_io_completion(struct request
*req
, unsigned int bytes
)
1713 if (blk_do_io_stat(req
)) {
1714 const int rw
= rq_data_dir(req
);
1715 struct hd_struct
*part
;
1718 cpu
= part_stat_lock();
1719 part
= disk_map_sector_rcu(req
->rq_disk
, blk_rq_pos(req
));
1720 part_stat_add(cpu
, part
, sectors
[rw
], bytes
>> 9);
1725 static void blk_account_io_done(struct request
*req
)
1728 * Account IO completion. bar_rq isn't accounted as a normal
1729 * IO on queueing nor completion. Accounting the containing
1730 * request is enough.
1732 if (blk_do_io_stat(req
) && req
!= &req
->q
->bar_rq
) {
1733 unsigned long duration
= jiffies
- req
->start_time
;
1734 const int rw
= rq_data_dir(req
);
1735 struct hd_struct
*part
;
1738 cpu
= part_stat_lock();
1739 part
= disk_map_sector_rcu(req
->rq_disk
, blk_rq_pos(req
));
1741 part_stat_inc(cpu
, part
, ios
[rw
]);
1742 part_stat_add(cpu
, part
, ticks
[rw
], duration
);
1743 part_round_stats(cpu
, part
);
1744 part_dec_in_flight(part
, rw
);
1751 * blk_peek_request - peek at the top of a request queue
1752 * @q: request queue to peek at
1755 * Return the request at the top of @q. The returned request
1756 * should be started using blk_start_request() before LLD starts
1760 * Pointer to the request at the top of @q if available. Null
1764 * queue_lock must be held.
1766 struct request
*blk_peek_request(struct request_queue
*q
)
1771 while ((rq
= __elv_next_request(q
)) != NULL
) {
1772 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
1774 * This is the first time the device driver
1775 * sees this request (possibly after
1776 * requeueing). Notify IO scheduler.
1778 if (blk_sorted_rq(rq
))
1779 elv_activate_rq(q
, rq
);
1782 * just mark as started even if we don't start
1783 * it, a request that has been delayed should
1784 * not be passed by new incoming requests
1786 rq
->cmd_flags
|= REQ_STARTED
;
1787 trace_block_rq_issue(q
, rq
);
1790 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
1791 q
->end_sector
= rq_end_sector(rq
);
1792 q
->boundary_rq
= NULL
;
1795 if (rq
->cmd_flags
& REQ_DONTPREP
)
1798 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
1800 * make sure space for the drain appears we
1801 * know we can do this because max_hw_segments
1802 * has been adjusted to be one fewer than the
1805 rq
->nr_phys_segments
++;
1811 ret
= q
->prep_rq_fn(q
, rq
);
1812 if (ret
== BLKPREP_OK
) {
1814 } else if (ret
== BLKPREP_DEFER
) {
1816 * the request may have been (partially) prepped.
1817 * we need to keep this request in the front to
1818 * avoid resource deadlock. REQ_STARTED will
1819 * prevent other fs requests from passing this one.
1821 if (q
->dma_drain_size
&& blk_rq_bytes(rq
) &&
1822 !(rq
->cmd_flags
& REQ_DONTPREP
)) {
1824 * remove the space for the drain we added
1825 * so that we don't add it again
1827 --rq
->nr_phys_segments
;
1832 } else if (ret
== BLKPREP_KILL
) {
1833 rq
->cmd_flags
|= REQ_QUIET
;
1835 * Mark this request as started so we don't trigger
1836 * any debug logic in the end I/O path.
1838 blk_start_request(rq
);
1839 __blk_end_request_all(rq
, -EIO
);
1841 printk(KERN_ERR
"%s: bad return=%d\n", __func__
, ret
);
1848 EXPORT_SYMBOL(blk_peek_request
);
1850 void blk_dequeue_request(struct request
*rq
)
1852 struct request_queue
*q
= rq
->q
;
1854 BUG_ON(list_empty(&rq
->queuelist
));
1855 BUG_ON(ELV_ON_HASH(rq
));
1857 list_del_init(&rq
->queuelist
);
1860 * the time frame between a request being removed from the lists
1861 * and to it is freed is accounted as io that is in progress at
1864 if (blk_account_rq(rq
)) {
1865 q
->in_flight
[rq_is_sync(rq
)]++;
1867 * Mark this device as supporting hardware queuing, if
1868 * we have more IOs in flight than 4.
1870 if (!blk_queue_queuing(q
) && queue_in_flight(q
) > 4)
1871 set_bit(QUEUE_FLAG_CQ
, &q
->queue_flags
);
1876 * blk_start_request - start request processing on the driver
1877 * @req: request to dequeue
1880 * Dequeue @req and start timeout timer on it. This hands off the
1881 * request to the driver.
1883 * Block internal functions which don't want to start timer should
1884 * call blk_dequeue_request().
1887 * queue_lock must be held.
1889 void blk_start_request(struct request
*req
)
1891 blk_dequeue_request(req
);
1894 * We are now handing the request to the hardware, initialize
1895 * resid_len to full count and add the timeout handler.
1897 req
->resid_len
= blk_rq_bytes(req
);
1898 if (unlikely(blk_bidi_rq(req
)))
1899 req
->next_rq
->resid_len
= blk_rq_bytes(req
->next_rq
);
1903 EXPORT_SYMBOL(blk_start_request
);
1906 * blk_fetch_request - fetch a request from a request queue
1907 * @q: request queue to fetch a request from
1910 * Return the request at the top of @q. The request is started on
1911 * return and LLD can start processing it immediately.
1914 * Pointer to the request at the top of @q if available. Null
1918 * queue_lock must be held.
1920 struct request
*blk_fetch_request(struct request_queue
*q
)
1924 rq
= blk_peek_request(q
);
1926 blk_start_request(rq
);
1929 EXPORT_SYMBOL(blk_fetch_request
);
1932 * blk_update_request - Special helper function for request stacking drivers
1933 * @req: the request being processed
1934 * @error: %0 for success, < %0 for error
1935 * @nr_bytes: number of bytes to complete @req
1938 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1939 * the request structure even if @req doesn't have leftover.
1940 * If @req has leftover, sets it up for the next range of segments.
1942 * This special helper function is only for request stacking drivers
1943 * (e.g. request-based dm) so that they can handle partial completion.
1944 * Actual device drivers should use blk_end_request instead.
1946 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1947 * %false return from this function.
1950 * %false - this request doesn't have any more data
1951 * %true - this request has more data
1953 bool blk_update_request(struct request
*req
, int error
, unsigned int nr_bytes
)
1955 int total_bytes
, bio_nbytes
, next_idx
= 0;
1961 trace_block_rq_complete(req
->q
, req
);
1964 * For fs requests, rq is just carrier of independent bio's
1965 * and each partial completion should be handled separately.
1966 * Reset per-request error on each partial completion.
1968 * TODO: tj: This is too subtle. It would be better to let
1969 * low level drivers do what they see fit.
1971 if (blk_fs_request(req
))
1974 if (error
&& (blk_fs_request(req
) && !(req
->cmd_flags
& REQ_QUIET
))) {
1975 printk(KERN_ERR
"end_request: I/O error, dev %s, sector %llu\n",
1976 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
1977 (unsigned long long)blk_rq_pos(req
));
1980 blk_account_io_completion(req
, nr_bytes
);
1982 total_bytes
= bio_nbytes
= 0;
1983 while ((bio
= req
->bio
) != NULL
) {
1986 if (nr_bytes
>= bio
->bi_size
) {
1987 req
->bio
= bio
->bi_next
;
1988 nbytes
= bio
->bi_size
;
1989 req_bio_endio(req
, bio
, nbytes
, error
);
1993 int idx
= bio
->bi_idx
+ next_idx
;
1995 if (unlikely(idx
>= bio
->bi_vcnt
)) {
1996 blk_dump_rq_flags(req
, "__end_that");
1997 printk(KERN_ERR
"%s: bio idx %d >= vcnt %d\n",
1998 __func__
, idx
, bio
->bi_vcnt
);
2002 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
2003 BIO_BUG_ON(nbytes
> bio
->bi_size
);
2006 * not a complete bvec done
2008 if (unlikely(nbytes
> nr_bytes
)) {
2009 bio_nbytes
+= nr_bytes
;
2010 total_bytes
+= nr_bytes
;
2015 * advance to the next vector
2018 bio_nbytes
+= nbytes
;
2021 total_bytes
+= nbytes
;
2027 * end more in this run, or just return 'not-done'
2029 if (unlikely(nr_bytes
<= 0))
2039 * Reset counters so that the request stacking driver
2040 * can find how many bytes remain in the request
2043 req
->__data_len
= 0;
2048 * if the request wasn't completed, update state
2051 req_bio_endio(req
, bio
, bio_nbytes
, error
);
2052 bio
->bi_idx
+= next_idx
;
2053 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
2054 bio_iovec(bio
)->bv_len
-= nr_bytes
;
2057 req
->__data_len
-= total_bytes
;
2058 req
->buffer
= bio_data(req
->bio
);
2060 /* update sector only for requests with clear definition of sector */
2061 if (blk_fs_request(req
) || blk_discard_rq(req
))
2062 req
->__sector
+= total_bytes
>> 9;
2064 /* mixed attributes always follow the first bio */
2065 if (req
->cmd_flags
& REQ_MIXED_MERGE
) {
2066 req
->cmd_flags
&= ~REQ_FAILFAST_MASK
;
2067 req
->cmd_flags
|= req
->bio
->bi_rw
& REQ_FAILFAST_MASK
;
2071 * If total number of sectors is less than the first segment
2072 * size, something has gone terribly wrong.
2074 if (blk_rq_bytes(req
) < blk_rq_cur_bytes(req
)) {
2075 printk(KERN_ERR
"blk: request botched\n");
2076 req
->__data_len
= blk_rq_cur_bytes(req
);
2079 /* recalculate the number of segments */
2080 blk_recalc_rq_segments(req
);
2084 EXPORT_SYMBOL_GPL(blk_update_request
);
2086 static bool blk_update_bidi_request(struct request
*rq
, int error
,
2087 unsigned int nr_bytes
,
2088 unsigned int bidi_bytes
)
2090 if (blk_update_request(rq
, error
, nr_bytes
))
2093 /* Bidi request must be completed as a whole */
2094 if (unlikely(blk_bidi_rq(rq
)) &&
2095 blk_update_request(rq
->next_rq
, error
, bidi_bytes
))
2098 add_disk_randomness(rq
->rq_disk
);
2104 * queue lock must be held
2106 static void blk_finish_request(struct request
*req
, int error
)
2108 if (blk_rq_tagged(req
))
2109 blk_queue_end_tag(req
->q
, req
);
2111 BUG_ON(blk_queued_rq(req
));
2113 if (unlikely(laptop_mode
) && blk_fs_request(req
))
2114 laptop_io_completion();
2116 blk_delete_timer(req
);
2118 blk_account_io_done(req
);
2121 req
->end_io(req
, error
);
2123 if (blk_bidi_rq(req
))
2124 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
2126 __blk_put_request(req
->q
, req
);
2131 * blk_end_bidi_request - Complete a bidi request
2132 * @rq: the request to complete
2133 * @error: %0 for success, < %0 for error
2134 * @nr_bytes: number of bytes to complete @rq
2135 * @bidi_bytes: number of bytes to complete @rq->next_rq
2138 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2139 * Drivers that supports bidi can safely call this member for any
2140 * type of request, bidi or uni. In the later case @bidi_bytes is
2144 * %false - we are done with this request
2145 * %true - still buffers pending for this request
2147 static bool blk_end_bidi_request(struct request
*rq
, int error
,
2148 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2150 struct request_queue
*q
= rq
->q
;
2151 unsigned long flags
;
2153 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2156 spin_lock_irqsave(q
->queue_lock
, flags
);
2157 blk_finish_request(rq
, error
);
2158 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2164 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2165 * @rq: the request to complete
2166 * @error: %0 for success, < %0 for error
2167 * @nr_bytes: number of bytes to complete @rq
2168 * @bidi_bytes: number of bytes to complete @rq->next_rq
2171 * Identical to blk_end_bidi_request() except that queue lock is
2172 * assumed to be locked on entry and remains so on return.
2175 * %false - we are done with this request
2176 * %true - still buffers pending for this request
2178 static bool __blk_end_bidi_request(struct request
*rq
, int error
,
2179 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2181 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2184 blk_finish_request(rq
, error
);
2190 * blk_end_request - Helper function for drivers to complete the request.
2191 * @rq: the request being processed
2192 * @error: %0 for success, < %0 for error
2193 * @nr_bytes: number of bytes to complete
2196 * Ends I/O on a number of bytes attached to @rq.
2197 * If @rq has leftover, sets it up for the next range of segments.
2200 * %false - we are done with this request
2201 * %true - still buffers pending for this request
2203 bool blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2205 return blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2207 EXPORT_SYMBOL(blk_end_request
);
2210 * blk_end_request_all - Helper function for drives to finish the request.
2211 * @rq: the request to finish
2212 * @error: %0 for success, < %0 for error
2215 * Completely finish @rq.
2217 void blk_end_request_all(struct request
*rq
, int error
)
2220 unsigned int bidi_bytes
= 0;
2222 if (unlikely(blk_bidi_rq(rq
)))
2223 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2225 pending
= blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2228 EXPORT_SYMBOL(blk_end_request_all
);
2231 * blk_end_request_cur - Helper function to finish the current request chunk.
2232 * @rq: the request to finish the current chunk for
2233 * @error: %0 for success, < %0 for error
2236 * Complete the current consecutively mapped chunk from @rq.
2239 * %false - we are done with this request
2240 * %true - still buffers pending for this request
2242 bool blk_end_request_cur(struct request
*rq
, int error
)
2244 return blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2246 EXPORT_SYMBOL(blk_end_request_cur
);
2249 * blk_end_request_err - Finish a request till the next failure boundary.
2250 * @rq: the request to finish till the next failure boundary for
2251 * @error: must be negative errno
2254 * Complete @rq till the next failure boundary.
2257 * %false - we are done with this request
2258 * %true - still buffers pending for this request
2260 bool blk_end_request_err(struct request
*rq
, int error
)
2262 WARN_ON(error
>= 0);
2263 return blk_end_request(rq
, error
, blk_rq_err_bytes(rq
));
2265 EXPORT_SYMBOL_GPL(blk_end_request_err
);
2268 * __blk_end_request - Helper function for drivers to complete the request.
2269 * @rq: the request being processed
2270 * @error: %0 for success, < %0 for error
2271 * @nr_bytes: number of bytes to complete
2274 * Must be called with queue lock held unlike blk_end_request().
2277 * %false - we are done with this request
2278 * %true - still buffers pending for this request
2280 bool __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2282 return __blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2284 EXPORT_SYMBOL(__blk_end_request
);
2287 * __blk_end_request_all - Helper function for drives to finish the request.
2288 * @rq: the request to finish
2289 * @error: %0 for success, < %0 for error
2292 * Completely finish @rq. Must be called with queue lock held.
2294 void __blk_end_request_all(struct request
*rq
, int error
)
2297 unsigned int bidi_bytes
= 0;
2299 if (unlikely(blk_bidi_rq(rq
)))
2300 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2302 pending
= __blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2305 EXPORT_SYMBOL(__blk_end_request_all
);
2308 * __blk_end_request_cur - Helper function to finish the current request chunk.
2309 * @rq: the request to finish the current chunk for
2310 * @error: %0 for success, < %0 for error
2313 * Complete the current consecutively mapped chunk from @rq. Must
2314 * be called with queue lock held.
2317 * %false - we are done with this request
2318 * %true - still buffers pending for this request
2320 bool __blk_end_request_cur(struct request
*rq
, int error
)
2322 return __blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2324 EXPORT_SYMBOL(__blk_end_request_cur
);
2327 * __blk_end_request_err - Finish a request till the next failure boundary.
2328 * @rq: the request to finish till the next failure boundary for
2329 * @error: must be negative errno
2332 * Complete @rq till the next failure boundary. Must be called
2333 * with queue lock held.
2336 * %false - we are done with this request
2337 * %true - still buffers pending for this request
2339 bool __blk_end_request_err(struct request
*rq
, int error
)
2341 WARN_ON(error
>= 0);
2342 return __blk_end_request(rq
, error
, blk_rq_err_bytes(rq
));
2344 EXPORT_SYMBOL_GPL(__blk_end_request_err
);
2346 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2349 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
2350 rq
->cmd_flags
|= bio
->bi_rw
& REQ_RW
;
2352 if (bio_has_data(bio
)) {
2353 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2354 rq
->buffer
= bio_data(bio
);
2356 rq
->__data_len
= bio
->bi_size
;
2357 rq
->bio
= rq
->biotail
= bio
;
2360 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2364 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2365 * @q : the queue of the device being checked
2368 * Check if underlying low-level drivers of a device are busy.
2369 * If the drivers want to export their busy state, they must set own
2370 * exporting function using blk_queue_lld_busy() first.
2372 * Basically, this function is used only by request stacking drivers
2373 * to stop dispatching requests to underlying devices when underlying
2374 * devices are busy. This behavior helps more I/O merging on the queue
2375 * of the request stacking driver and prevents I/O throughput regression
2376 * on burst I/O load.
2379 * 0 - Not busy (The request stacking driver should dispatch request)
2380 * 1 - Busy (The request stacking driver should stop dispatching request)
2382 int blk_lld_busy(struct request_queue
*q
)
2385 return q
->lld_busy_fn(q
);
2389 EXPORT_SYMBOL_GPL(blk_lld_busy
);
2392 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2393 * @rq: the clone request to be cleaned up
2396 * Free all bios in @rq for a cloned request.
2398 void blk_rq_unprep_clone(struct request
*rq
)
2402 while ((bio
= rq
->bio
) != NULL
) {
2403 rq
->bio
= bio
->bi_next
;
2408 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone
);
2411 * Copy attributes of the original request to the clone request.
2412 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2414 static void __blk_rq_prep_clone(struct request
*dst
, struct request
*src
)
2416 dst
->cpu
= src
->cpu
;
2417 dst
->cmd_flags
= (rq_data_dir(src
) | REQ_NOMERGE
);
2418 dst
->cmd_type
= src
->cmd_type
;
2419 dst
->__sector
= blk_rq_pos(src
);
2420 dst
->__data_len
= blk_rq_bytes(src
);
2421 dst
->nr_phys_segments
= src
->nr_phys_segments
;
2422 dst
->ioprio
= src
->ioprio
;
2423 dst
->extra_len
= src
->extra_len
;
2427 * blk_rq_prep_clone - Helper function to setup clone request
2428 * @rq: the request to be setup
2429 * @rq_src: original request to be cloned
2430 * @bs: bio_set that bios for clone are allocated from
2431 * @gfp_mask: memory allocation mask for bio
2432 * @bio_ctr: setup function to be called for each clone bio.
2433 * Returns %0 for success, non %0 for failure.
2434 * @data: private data to be passed to @bio_ctr
2437 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2438 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2439 * are not copied, and copying such parts is the caller's responsibility.
2440 * Also, pages which the original bios are pointing to are not copied
2441 * and the cloned bios just point same pages.
2442 * So cloned bios must be completed before original bios, which means
2443 * the caller must complete @rq before @rq_src.
2445 int blk_rq_prep_clone(struct request
*rq
, struct request
*rq_src
,
2446 struct bio_set
*bs
, gfp_t gfp_mask
,
2447 int (*bio_ctr
)(struct bio
*, struct bio
*, void *),
2450 struct bio
*bio
, *bio_src
;
2455 blk_rq_init(NULL
, rq
);
2457 __rq_for_each_bio(bio_src
, rq_src
) {
2458 bio
= bio_alloc_bioset(gfp_mask
, bio_src
->bi_max_vecs
, bs
);
2462 __bio_clone(bio
, bio_src
);
2464 if (bio_integrity(bio_src
) &&
2465 bio_integrity_clone(bio
, bio_src
, gfp_mask
, bs
))
2468 if (bio_ctr
&& bio_ctr(bio
, bio_src
, data
))
2472 rq
->biotail
->bi_next
= bio
;
2475 rq
->bio
= rq
->biotail
= bio
;
2478 __blk_rq_prep_clone(rq
, rq_src
);
2485 blk_rq_unprep_clone(rq
);
2489 EXPORT_SYMBOL_GPL(blk_rq_prep_clone
);
2491 int kblockd_schedule_work(struct request_queue
*q
, struct work_struct
*work
)
2493 return queue_work(kblockd_workqueue
, work
);
2495 EXPORT_SYMBOL(kblockd_schedule_work
);
2497 int __init
blk_dev_init(void)
2499 BUILD_BUG_ON(__REQ_NR_BITS
> 8 *
2500 sizeof(((struct request
*)0)->cmd_flags
));
2502 kblockd_workqueue
= create_workqueue("kblockd");
2503 if (!kblockd_workqueue
)
2504 panic("Failed to create kblockd\n");
2506 request_cachep
= kmem_cache_create("blkdev_requests",
2507 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2509 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2510 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);