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_bio_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();
70 part_stat_inc(cpu
, part
, merges
[rw
]);
72 part
= disk_map_sector_rcu(rq
->rq_disk
, blk_rq_pos(rq
));
73 if (!hd_struct_try_get(part
)) {
75 * The partition is already being removed,
76 * the request will be accounted on the disk only
78 * We take a reference on disk->part0 although that
79 * partition will never be deleted, so we can treat
80 * it as any other partition.
82 part
= &rq
->rq_disk
->part0
;
85 part_round_stats(cpu
, part
);
86 part_inc_in_flight(part
, rw
);
93 void blk_queue_congestion_threshold(struct request_queue
*q
)
97 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
98 if (nr
> q
->nr_requests
)
100 q
->nr_congestion_on
= nr
;
102 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
105 q
->nr_congestion_off
= nr
;
109 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
112 * Locates the passed device's request queue and returns the address of its
115 * Will return NULL if the request queue cannot be located.
117 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
119 struct backing_dev_info
*ret
= NULL
;
120 struct request_queue
*q
= bdev_get_queue(bdev
);
123 ret
= &q
->backing_dev_info
;
126 EXPORT_SYMBOL(blk_get_backing_dev_info
);
128 void blk_rq_init(struct request_queue
*q
, struct request
*rq
)
130 memset(rq
, 0, sizeof(*rq
));
132 INIT_LIST_HEAD(&rq
->queuelist
);
133 INIT_LIST_HEAD(&rq
->timeout_list
);
136 rq
->__sector
= (sector_t
) -1;
137 INIT_HLIST_NODE(&rq
->hash
);
138 RB_CLEAR_NODE(&rq
->rb_node
);
140 rq
->cmd_len
= BLK_MAX_CDB
;
143 rq
->start_time
= jiffies
;
144 set_start_time_ns(rq
);
147 EXPORT_SYMBOL(blk_rq_init
);
149 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
150 unsigned int nbytes
, int error
)
152 struct request_queue
*q
= rq
->q
;
154 if (&q
->flush_rq
!= rq
) {
156 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
157 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
160 if (unlikely(nbytes
> bio
->bi_size
)) {
161 printk(KERN_ERR
"%s: want %u bytes done, %u left\n",
162 __func__
, nbytes
, bio
->bi_size
);
163 nbytes
= bio
->bi_size
;
166 if (unlikely(rq
->cmd_flags
& REQ_QUIET
))
167 set_bit(BIO_QUIET
, &bio
->bi_flags
);
169 bio
->bi_size
-= nbytes
;
170 bio
->bi_sector
+= (nbytes
>> 9);
172 if (bio_integrity(bio
))
173 bio_integrity_advance(bio
, nbytes
);
175 if (bio
->bi_size
== 0)
176 bio_endio(bio
, error
);
179 * Okay, this is the sequenced flush request in
180 * progress, just record the error;
182 if (error
&& !q
->flush_err
)
183 q
->flush_err
= error
;
187 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
191 printk(KERN_INFO
"%s: dev %s: type=%x, flags=%x\n", msg
,
192 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
195 printk(KERN_INFO
" sector %llu, nr/cnr %u/%u\n",
196 (unsigned long long)blk_rq_pos(rq
),
197 blk_rq_sectors(rq
), blk_rq_cur_sectors(rq
));
198 printk(KERN_INFO
" bio %p, biotail %p, buffer %p, len %u\n",
199 rq
->bio
, rq
->biotail
, rq
->buffer
, blk_rq_bytes(rq
));
201 if (rq
->cmd_type
== REQ_TYPE_BLOCK_PC
) {
202 printk(KERN_INFO
" cdb: ");
203 for (bit
= 0; bit
< BLK_MAX_CDB
; bit
++)
204 printk("%02x ", rq
->cmd
[bit
]);
208 EXPORT_SYMBOL(blk_dump_rq_flags
);
211 * "plug" the device if there are no outstanding requests: this will
212 * force the transfer to start only after we have put all the requests
215 * This is called with interrupts off and no requests on the queue and
216 * with the queue lock held.
218 void blk_plug_device(struct request_queue
*q
)
220 WARN_ON(!irqs_disabled());
223 * don't plug a stopped queue, it must be paired with blk_start_queue()
224 * which will restart the queueing
226 if (blk_queue_stopped(q
))
229 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED
, q
)) {
230 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
234 EXPORT_SYMBOL(blk_plug_device
);
237 * blk_plug_device_unlocked - plug a device without queue lock held
238 * @q: The &struct request_queue to plug
241 * Like @blk_plug_device(), but grabs the queue lock and disables
244 void blk_plug_device_unlocked(struct request_queue
*q
)
248 spin_lock_irqsave(q
->queue_lock
, flags
);
250 spin_unlock_irqrestore(q
->queue_lock
, flags
);
252 EXPORT_SYMBOL(blk_plug_device_unlocked
);
255 * remove the queue from the plugged list, if present. called with
256 * queue lock held and interrupts disabled.
258 int blk_remove_plug(struct request_queue
*q
)
260 WARN_ON(!irqs_disabled());
262 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED
, q
))
265 del_timer(&q
->unplug_timer
);
268 EXPORT_SYMBOL(blk_remove_plug
);
271 * remove the plug and let it rip..
273 void __generic_unplug_device(struct request_queue
*q
)
275 if (unlikely(blk_queue_stopped(q
)))
277 if (!blk_remove_plug(q
) && !blk_queue_nonrot(q
))
284 * generic_unplug_device - fire a request queue
285 * @q: The &struct request_queue in question
288 * Linux uses plugging to build bigger requests queues before letting
289 * the device have at them. If a queue is plugged, the I/O scheduler
290 * is still adding and merging requests on the queue. Once the queue
291 * gets unplugged, the request_fn defined for the queue is invoked and
294 void generic_unplug_device(struct request_queue
*q
)
296 if (blk_queue_plugged(q
)) {
297 spin_lock_irq(q
->queue_lock
);
298 __generic_unplug_device(q
);
299 spin_unlock_irq(q
->queue_lock
);
302 EXPORT_SYMBOL(generic_unplug_device
);
304 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
307 struct request_queue
*q
= bdi
->unplug_io_data
;
312 void blk_unplug_work(struct work_struct
*work
)
314 struct request_queue
*q
=
315 container_of(work
, struct request_queue
, unplug_work
);
317 trace_block_unplug_io(q
);
321 void blk_unplug_timeout(unsigned long data
)
323 struct request_queue
*q
= (struct request_queue
*)data
;
325 trace_block_unplug_timer(q
);
326 kblockd_schedule_work(q
, &q
->unplug_work
);
329 void blk_unplug(struct request_queue
*q
)
332 * devices don't necessarily have an ->unplug_fn defined
335 trace_block_unplug_io(q
);
339 EXPORT_SYMBOL(blk_unplug
);
342 * blk_start_queue - restart a previously stopped queue
343 * @q: The &struct request_queue in question
346 * blk_start_queue() will clear the stop flag on the queue, and call
347 * the request_fn for the queue if it was in a stopped state when
348 * entered. Also see blk_stop_queue(). Queue lock must be held.
350 void blk_start_queue(struct request_queue
*q
)
352 WARN_ON(!irqs_disabled());
354 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
355 __blk_run_queue(q
, false);
357 EXPORT_SYMBOL(blk_start_queue
);
360 * blk_stop_queue - stop a queue
361 * @q: The &struct request_queue in question
364 * The Linux block layer assumes that a block driver will consume all
365 * entries on the request queue when the request_fn strategy is called.
366 * Often this will not happen, because of hardware limitations (queue
367 * depth settings). If a device driver gets a 'queue full' response,
368 * or if it simply chooses not to queue more I/O at one point, it can
369 * call this function to prevent the request_fn from being called until
370 * the driver has signalled it's ready to go again. This happens by calling
371 * blk_start_queue() to restart queue operations. Queue lock must be held.
373 void blk_stop_queue(struct request_queue
*q
)
376 queue_flag_set(QUEUE_FLAG_STOPPED
, q
);
378 EXPORT_SYMBOL(blk_stop_queue
);
381 * blk_sync_queue - cancel any pending callbacks on a queue
385 * The block layer may perform asynchronous callback activity
386 * on a queue, such as calling the unplug function after a timeout.
387 * A block device may call blk_sync_queue to ensure that any
388 * such activity is cancelled, thus allowing it to release resources
389 * that the callbacks might use. The caller must already have made sure
390 * that its ->make_request_fn will not re-add plugging prior to calling
394 void blk_sync_queue(struct request_queue
*q
)
396 del_timer_sync(&q
->unplug_timer
);
397 del_timer_sync(&q
->timeout
);
398 cancel_work_sync(&q
->unplug_work
);
399 throtl_shutdown_timer_wq(q
);
401 EXPORT_SYMBOL(blk_sync_queue
);
404 * __blk_run_queue - run a single device queue
405 * @q: The queue to run
406 * @force_kblockd: Don't run @q->request_fn directly. Use kblockd.
409 * See @blk_run_queue. This variant must be called with the queue lock
410 * held and interrupts disabled.
413 void __blk_run_queue(struct request_queue
*q
, bool force_kblockd
)
417 if (unlikely(blk_queue_stopped(q
)))
420 if (elv_queue_empty(q
))
424 * Only recurse once to avoid overrunning the stack, let the unplug
425 * handling reinvoke the handler shortly if we already got there.
427 if (!force_kblockd
&& !queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
429 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
431 queue_flag_set(QUEUE_FLAG_PLUGGED
, q
);
432 kblockd_schedule_work(q
, &q
->unplug_work
);
435 EXPORT_SYMBOL(__blk_run_queue
);
438 * blk_run_queue - run a single device queue
439 * @q: The queue to run
442 * Invoke request handling on this queue, if it has pending work to do.
443 * May be used to restart queueing when a request has completed.
445 void blk_run_queue(struct request_queue
*q
)
449 spin_lock_irqsave(q
->queue_lock
, flags
);
450 __blk_run_queue(q
, false);
451 spin_unlock_irqrestore(q
->queue_lock
, flags
);
453 EXPORT_SYMBOL(blk_run_queue
);
455 void blk_put_queue(struct request_queue
*q
)
457 kobject_put(&q
->kobj
);
460 void blk_cleanup_queue(struct request_queue
*q
)
463 * We know we have process context here, so we can be a little
464 * cautious and ensure that pending block actions on this device
465 * are done before moving on. Going into this function, we should
466 * not have processes doing IO to this device.
470 del_timer_sync(&q
->backing_dev_info
.laptop_mode_wb_timer
);
471 mutex_lock(&q
->sysfs_lock
);
472 queue_flag_set_unlocked(QUEUE_FLAG_DEAD
, q
);
473 mutex_unlock(&q
->sysfs_lock
);
476 elevator_exit(q
->elevator
);
480 EXPORT_SYMBOL(blk_cleanup_queue
);
482 static int blk_init_free_list(struct request_queue
*q
)
484 struct request_list
*rl
= &q
->rq
;
486 if (unlikely(rl
->rq_pool
))
489 rl
->count
[BLK_RW_SYNC
] = rl
->count
[BLK_RW_ASYNC
] = 0;
490 rl
->starved
[BLK_RW_SYNC
] = rl
->starved
[BLK_RW_ASYNC
] = 0;
492 init_waitqueue_head(&rl
->wait
[BLK_RW_SYNC
]);
493 init_waitqueue_head(&rl
->wait
[BLK_RW_ASYNC
]);
495 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
496 mempool_free_slab
, request_cachep
, q
->node
);
504 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
506 return blk_alloc_queue_node(gfp_mask
, -1);
508 EXPORT_SYMBOL(blk_alloc_queue
);
510 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
512 struct request_queue
*q
;
515 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
516 gfp_mask
| __GFP_ZERO
, node_id
);
520 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
521 q
->backing_dev_info
.unplug_io_data
= q
;
522 q
->backing_dev_info
.ra_pages
=
523 (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
524 q
->backing_dev_info
.state
= 0;
525 q
->backing_dev_info
.capabilities
= BDI_CAP_MAP_COPY
;
526 q
->backing_dev_info
.name
= "block";
528 err
= bdi_init(&q
->backing_dev_info
);
530 kmem_cache_free(blk_requestq_cachep
, q
);
534 if (blk_throtl_init(q
)) {
535 kmem_cache_free(blk_requestq_cachep
, q
);
539 setup_timer(&q
->backing_dev_info
.laptop_mode_wb_timer
,
540 laptop_mode_timer_fn
, (unsigned long) q
);
541 init_timer(&q
->unplug_timer
);
542 setup_timer(&q
->timeout
, blk_rq_timed_out_timer
, (unsigned long) q
);
543 INIT_LIST_HEAD(&q
->timeout_list
);
544 INIT_LIST_HEAD(&q
->pending_flushes
);
545 INIT_WORK(&q
->unplug_work
, blk_unplug_work
);
547 kobject_init(&q
->kobj
, &blk_queue_ktype
);
549 mutex_init(&q
->sysfs_lock
);
550 spin_lock_init(&q
->__queue_lock
);
554 EXPORT_SYMBOL(blk_alloc_queue_node
);
557 * blk_init_queue - prepare a request queue for use with a block device
558 * @rfn: The function to be called to process requests that have been
559 * placed on the queue.
560 * @lock: Request queue spin lock
563 * If a block device wishes to use the standard request handling procedures,
564 * which sorts requests and coalesces adjacent requests, then it must
565 * call blk_init_queue(). The function @rfn will be called when there
566 * are requests on the queue that need to be processed. If the device
567 * supports plugging, then @rfn may not be called immediately when requests
568 * are available on the queue, but may be called at some time later instead.
569 * Plugged queues are generally unplugged when a buffer belonging to one
570 * of the requests on the queue is needed, or due to memory pressure.
572 * @rfn is not required, or even expected, to remove all requests off the
573 * queue, but only as many as it can handle at a time. If it does leave
574 * requests on the queue, it is responsible for arranging that the requests
575 * get dealt with eventually.
577 * The queue spin lock must be held while manipulating the requests on the
578 * request queue; this lock will be taken also from interrupt context, so irq
579 * disabling is needed for it.
581 * Function returns a pointer to the initialized request queue, or %NULL if
585 * blk_init_queue() must be paired with a blk_cleanup_queue() call
586 * when the block device is deactivated (such as at module unload).
589 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
591 return blk_init_queue_node(rfn
, lock
, -1);
593 EXPORT_SYMBOL(blk_init_queue
);
595 struct request_queue
*
596 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
598 struct request_queue
*uninit_q
, *q
;
600 uninit_q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
604 q
= blk_init_allocated_queue_node(uninit_q
, rfn
, lock
, node_id
);
606 blk_cleanup_queue(uninit_q
);
610 EXPORT_SYMBOL(blk_init_queue_node
);
612 struct request_queue
*
613 blk_init_allocated_queue(struct request_queue
*q
, request_fn_proc
*rfn
,
616 return blk_init_allocated_queue_node(q
, rfn
, lock
, -1);
618 EXPORT_SYMBOL(blk_init_allocated_queue
);
620 struct request_queue
*
621 blk_init_allocated_queue_node(struct request_queue
*q
, request_fn_proc
*rfn
,
622 spinlock_t
*lock
, int node_id
)
628 if (blk_init_free_list(q
))
632 q
->prep_rq_fn
= NULL
;
633 q
->unprep_rq_fn
= NULL
;
634 q
->unplug_fn
= generic_unplug_device
;
635 q
->queue_flags
= QUEUE_FLAG_DEFAULT
;
636 q
->queue_lock
= lock
;
639 * This also sets hw/phys segments, boundary and size
641 blk_queue_make_request(q
, __make_request
);
643 q
->sg_reserved_size
= INT_MAX
;
648 if (!elevator_init(q
, NULL
)) {
649 blk_queue_congestion_threshold(q
);
655 EXPORT_SYMBOL(blk_init_allocated_queue_node
);
657 int blk_get_queue(struct request_queue
*q
)
659 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
660 kobject_get(&q
->kobj
);
667 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
669 if (rq
->cmd_flags
& REQ_ELVPRIV
)
670 elv_put_request(q
, rq
);
671 mempool_free(rq
, q
->rq
.rq_pool
);
674 static struct request
*
675 blk_alloc_request(struct request_queue
*q
, int flags
, int priv
, gfp_t gfp_mask
)
677 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
684 rq
->cmd_flags
= flags
| REQ_ALLOCED
;
687 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
688 mempool_free(rq
, q
->rq
.rq_pool
);
691 rq
->cmd_flags
|= REQ_ELVPRIV
;
698 * ioc_batching returns true if the ioc is a valid batching request and
699 * should be given priority access to a request.
701 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
707 * Make sure the process is able to allocate at least 1 request
708 * even if the batch times out, otherwise we could theoretically
711 return ioc
->nr_batch_requests
== q
->nr_batching
||
712 (ioc
->nr_batch_requests
> 0
713 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
717 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
718 * will cause the process to be a "batcher" on all queues in the system. This
719 * is the behaviour we want though - once it gets a wakeup it should be given
722 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
724 if (!ioc
|| ioc_batching(q
, ioc
))
727 ioc
->nr_batch_requests
= q
->nr_batching
;
728 ioc
->last_waited
= jiffies
;
731 static void __freed_request(struct request_queue
*q
, int sync
)
733 struct request_list
*rl
= &q
->rq
;
735 if (rl
->count
[sync
] < queue_congestion_off_threshold(q
))
736 blk_clear_queue_congested(q
, sync
);
738 if (rl
->count
[sync
] + 1 <= q
->nr_requests
) {
739 if (waitqueue_active(&rl
->wait
[sync
]))
740 wake_up(&rl
->wait
[sync
]);
742 blk_clear_queue_full(q
, sync
);
747 * A request has just been released. Account for it, update the full and
748 * congestion status, wake up any waiters. Called under q->queue_lock.
750 static void freed_request(struct request_queue
*q
, int sync
, int priv
)
752 struct request_list
*rl
= &q
->rq
;
758 __freed_request(q
, sync
);
760 if (unlikely(rl
->starved
[sync
^ 1]))
761 __freed_request(q
, sync
^ 1);
765 * Get a free request, queue_lock must be held.
766 * Returns NULL on failure, with queue_lock held.
767 * Returns !NULL on success, with queue_lock *not held*.
769 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
770 struct bio
*bio
, gfp_t gfp_mask
)
772 struct request
*rq
= NULL
;
773 struct request_list
*rl
= &q
->rq
;
774 struct io_context
*ioc
= NULL
;
775 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
778 may_queue
= elv_may_queue(q
, rw_flags
);
779 if (may_queue
== ELV_MQUEUE_NO
)
782 if (rl
->count
[is_sync
]+1 >= queue_congestion_on_threshold(q
)) {
783 if (rl
->count
[is_sync
]+1 >= q
->nr_requests
) {
784 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
786 * The queue will fill after this allocation, so set
787 * it as full, and mark this process as "batching".
788 * This process will be allowed to complete a batch of
789 * requests, others will be blocked.
791 if (!blk_queue_full(q
, is_sync
)) {
792 ioc_set_batching(q
, ioc
);
793 blk_set_queue_full(q
, is_sync
);
795 if (may_queue
!= ELV_MQUEUE_MUST
796 && !ioc_batching(q
, ioc
)) {
798 * The queue is full and the allocating
799 * process is not a "batcher", and not
800 * exempted by the IO scheduler
806 blk_set_queue_congested(q
, is_sync
);
810 * Only allow batching queuers to allocate up to 50% over the defined
811 * limit of requests, otherwise we could have thousands of requests
812 * allocated with any setting of ->nr_requests
814 if (rl
->count
[is_sync
] >= (3 * q
->nr_requests
/ 2))
817 rl
->count
[is_sync
]++;
818 rl
->starved
[is_sync
] = 0;
820 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
824 if (blk_queue_io_stat(q
))
825 rw_flags
|= REQ_IO_STAT
;
826 spin_unlock_irq(q
->queue_lock
);
828 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
831 * Allocation failed presumably due to memory. Undo anything
832 * we might have messed up.
834 * Allocating task should really be put onto the front of the
835 * wait queue, but this is pretty rare.
837 spin_lock_irq(q
->queue_lock
);
838 freed_request(q
, is_sync
, priv
);
841 * in the very unlikely event that allocation failed and no
842 * requests for this direction was pending, mark us starved
843 * so that freeing of a request in the other direction will
844 * notice us. another possible fix would be to split the
845 * rq mempool into READ and WRITE
848 if (unlikely(rl
->count
[is_sync
] == 0))
849 rl
->starved
[is_sync
] = 1;
855 * ioc may be NULL here, and ioc_batching will be false. That's
856 * OK, if the queue is under the request limit then requests need
857 * not count toward the nr_batch_requests limit. There will always
858 * be some limit enforced by BLK_BATCH_TIME.
860 if (ioc_batching(q
, ioc
))
861 ioc
->nr_batch_requests
--;
863 trace_block_getrq(q
, bio
, rw_flags
& 1);
869 * No available requests for this queue, unplug the device and wait for some
870 * requests to become available.
872 * Called with q->queue_lock held, and returns with it unlocked.
874 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
877 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
880 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
883 struct io_context
*ioc
;
884 struct request_list
*rl
= &q
->rq
;
886 prepare_to_wait_exclusive(&rl
->wait
[is_sync
], &wait
,
887 TASK_UNINTERRUPTIBLE
);
889 trace_block_sleeprq(q
, bio
, rw_flags
& 1);
891 __generic_unplug_device(q
);
892 spin_unlock_irq(q
->queue_lock
);
896 * After sleeping, we become a "batching" process and
897 * will be able to allocate at least one request, and
898 * up to a big batch of them for a small period time.
899 * See ioc_batching, ioc_set_batching
901 ioc
= current_io_context(GFP_NOIO
, q
->node
);
902 ioc_set_batching(q
, ioc
);
904 spin_lock_irq(q
->queue_lock
);
905 finish_wait(&rl
->wait
[is_sync
], &wait
);
907 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
913 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
917 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
919 spin_lock_irq(q
->queue_lock
);
920 if (gfp_mask
& __GFP_WAIT
) {
921 rq
= get_request_wait(q
, rw
, NULL
);
923 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
925 spin_unlock_irq(q
->queue_lock
);
927 /* q->queue_lock is unlocked at this point */
931 EXPORT_SYMBOL(blk_get_request
);
934 * blk_make_request - given a bio, allocate a corresponding struct request.
935 * @q: target request queue
936 * @bio: The bio describing the memory mappings that will be submitted for IO.
937 * It may be a chained-bio properly constructed by block/bio layer.
938 * @gfp_mask: gfp flags to be used for memory allocation
940 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
941 * type commands. Where the struct request needs to be farther initialized by
942 * the caller. It is passed a &struct bio, which describes the memory info of
945 * The caller of blk_make_request must make sure that bi_io_vec
946 * are set to describe the memory buffers. That bio_data_dir() will return
947 * the needed direction of the request. (And all bio's in the passed bio-chain
948 * are properly set accordingly)
950 * If called under none-sleepable conditions, mapped bio buffers must not
951 * need bouncing, by calling the appropriate masked or flagged allocator,
952 * suitable for the target device. Otherwise the call to blk_queue_bounce will
955 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
956 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
957 * anything but the first bio in the chain. Otherwise you risk waiting for IO
958 * completion of a bio that hasn't been submitted yet, thus resulting in a
959 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
960 * of bio_alloc(), as that avoids the mempool deadlock.
961 * If possible a big IO should be split into smaller parts when allocation
962 * fails. Partial allocation should not be an error, or you risk a live-lock.
964 struct request
*blk_make_request(struct request_queue
*q
, struct bio
*bio
,
967 struct request
*rq
= blk_get_request(q
, bio_data_dir(bio
), gfp_mask
);
970 return ERR_PTR(-ENOMEM
);
973 struct bio
*bounce_bio
= bio
;
976 blk_queue_bounce(q
, &bounce_bio
);
977 ret
= blk_rq_append_bio(q
, rq
, bounce_bio
);
986 EXPORT_SYMBOL(blk_make_request
);
989 * blk_requeue_request - put a request back on queue
990 * @q: request queue where request should be inserted
991 * @rq: request to be inserted
994 * Drivers often keep queueing requests until the hardware cannot accept
995 * more, when that condition happens we need to put the request back
996 * on the queue. Must be called with queue lock held.
998 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
1000 blk_delete_timer(rq
);
1001 blk_clear_rq_complete(rq
);
1002 trace_block_rq_requeue(q
, rq
);
1004 if (blk_rq_tagged(rq
))
1005 blk_queue_end_tag(q
, rq
);
1007 BUG_ON(blk_queued_rq(rq
));
1009 elv_requeue_request(q
, rq
);
1011 EXPORT_SYMBOL(blk_requeue_request
);
1014 * blk_insert_request - insert a special request into a request queue
1015 * @q: request queue where request should be inserted
1016 * @rq: request to be inserted
1017 * @at_head: insert request at head or tail of queue
1018 * @data: private data
1021 * Many block devices need to execute commands asynchronously, so they don't
1022 * block the whole kernel from preemption during request execution. This is
1023 * accomplished normally by inserting aritficial requests tagged as
1024 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1025 * be scheduled for actual execution by the request queue.
1027 * We have the option of inserting the head or the tail of the queue.
1028 * Typically we use the tail for new ioctls and so forth. We use the head
1029 * of the queue for things like a QUEUE_FULL message from a device, or a
1030 * host that is unable to accept a particular command.
1032 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
1033 int at_head
, void *data
)
1035 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
1036 unsigned long flags
;
1039 * tell I/O scheduler that this isn't a regular read/write (ie it
1040 * must not attempt merges on this) and that it acts as a soft
1043 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
1047 spin_lock_irqsave(q
->queue_lock
, flags
);
1050 * If command is tagged, release the tag
1052 if (blk_rq_tagged(rq
))
1053 blk_queue_end_tag(q
, rq
);
1055 drive_stat_acct(rq
, 1);
1056 __elv_add_request(q
, rq
, where
, 0);
1057 __blk_run_queue(q
, false);
1058 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1060 EXPORT_SYMBOL(blk_insert_request
);
1062 static void part_round_stats_single(int cpu
, struct hd_struct
*part
,
1065 if (now
== part
->stamp
)
1068 if (part_in_flight(part
)) {
1069 __part_stat_add(cpu
, part
, time_in_queue
,
1070 part_in_flight(part
) * (now
- part
->stamp
));
1071 __part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
1077 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1078 * @cpu: cpu number for stats access
1079 * @part: target partition
1081 * The average IO queue length and utilisation statistics are maintained
1082 * by observing the current state of the queue length and the amount of
1083 * time it has been in this state for.
1085 * Normally, that accounting is done on IO completion, but that can result
1086 * in more than a second's worth of IO being accounted for within any one
1087 * second, leading to >100% utilisation. To deal with that, we call this
1088 * function to do a round-off before returning the results when reading
1089 * /proc/diskstats. This accounts immediately for all queue usage up to
1090 * the current jiffies and restarts the counters again.
1092 void part_round_stats(int cpu
, struct hd_struct
*part
)
1094 unsigned long now
= jiffies
;
1097 part_round_stats_single(cpu
, &part_to_disk(part
)->part0
, now
);
1098 part_round_stats_single(cpu
, part
, now
);
1100 EXPORT_SYMBOL_GPL(part_round_stats
);
1103 * queue lock must be held
1105 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1109 if (unlikely(--req
->ref_count
))
1112 elv_completed_request(q
, req
);
1114 /* this is a bio leak */
1115 WARN_ON(req
->bio
!= NULL
);
1118 * Request may not have originated from ll_rw_blk. if not,
1119 * it didn't come out of our reserved rq pools
1121 if (req
->cmd_flags
& REQ_ALLOCED
) {
1122 int is_sync
= rq_is_sync(req
) != 0;
1123 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1125 BUG_ON(!list_empty(&req
->queuelist
));
1126 BUG_ON(!hlist_unhashed(&req
->hash
));
1128 blk_free_request(q
, req
);
1129 freed_request(q
, is_sync
, priv
);
1132 EXPORT_SYMBOL_GPL(__blk_put_request
);
1134 void blk_put_request(struct request
*req
)
1136 unsigned long flags
;
1137 struct request_queue
*q
= req
->q
;
1139 spin_lock_irqsave(q
->queue_lock
, flags
);
1140 __blk_put_request(q
, req
);
1141 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1143 EXPORT_SYMBOL(blk_put_request
);
1146 * blk_add_request_payload - add a payload to a request
1147 * @rq: request to update
1148 * @page: page backing the payload
1149 * @len: length of the payload.
1151 * This allows to later add a payload to an already submitted request by
1152 * a block driver. The driver needs to take care of freeing the payload
1155 * Note that this is a quite horrible hack and nothing but handling of
1156 * discard requests should ever use it.
1158 void blk_add_request_payload(struct request
*rq
, struct page
*page
,
1161 struct bio
*bio
= rq
->bio
;
1163 bio
->bi_io_vec
->bv_page
= page
;
1164 bio
->bi_io_vec
->bv_offset
= 0;
1165 bio
->bi_io_vec
->bv_len
= len
;
1169 bio
->bi_phys_segments
= 1;
1171 rq
->__data_len
= rq
->resid_len
= len
;
1172 rq
->nr_phys_segments
= 1;
1173 rq
->buffer
= bio_data(bio
);
1175 EXPORT_SYMBOL_GPL(blk_add_request_payload
);
1177 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1179 req
->cpu
= bio
->bi_comp_cpu
;
1180 req
->cmd_type
= REQ_TYPE_FS
;
1182 req
->cmd_flags
|= bio
->bi_rw
& REQ_COMMON_MASK
;
1183 if (bio
->bi_rw
& REQ_RAHEAD
)
1184 req
->cmd_flags
|= REQ_FAILFAST_MASK
;
1187 req
->__sector
= bio
->bi_sector
;
1188 req
->ioprio
= bio_prio(bio
);
1189 blk_rq_bio_prep(req
->q
, req
, bio
);
1193 * Only disabling plugging for non-rotational devices if it does tagging
1194 * as well, otherwise we do need the proper merging
1196 static inline bool queue_should_plug(struct request_queue
*q
)
1198 return !(blk_queue_nonrot(q
) && blk_queue_tagged(q
));
1201 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1203 struct request
*req
;
1205 unsigned int bytes
= bio
->bi_size
;
1206 const unsigned short prio
= bio_prio(bio
);
1207 const bool sync
= !!(bio
->bi_rw
& REQ_SYNC
);
1208 const bool unplug
= !!(bio
->bi_rw
& REQ_UNPLUG
);
1209 const unsigned long ff
= bio
->bi_rw
& REQ_FAILFAST_MASK
;
1210 int where
= ELEVATOR_INSERT_SORT
;
1214 * low level driver can indicate that it wants pages above a
1215 * certain limit bounced to low memory (ie for highmem, or even
1216 * ISA dma in theory)
1218 blk_queue_bounce(q
, &bio
);
1220 spin_lock_irq(q
->queue_lock
);
1222 if (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) {
1223 where
= ELEVATOR_INSERT_FRONT
;
1227 if (elv_queue_empty(q
))
1230 el_ret
= elv_merge(q
, &req
, bio
);
1232 case ELEVATOR_BACK_MERGE
:
1233 BUG_ON(!rq_mergeable(req
));
1235 if (!ll_back_merge_fn(q
, req
, bio
))
1238 trace_block_bio_backmerge(q
, bio
);
1240 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
)
1241 blk_rq_set_mixed_merge(req
);
1243 req
->biotail
->bi_next
= bio
;
1245 req
->__data_len
+= bytes
;
1246 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1247 if (!blk_rq_cpu_valid(req
))
1248 req
->cpu
= bio
->bi_comp_cpu
;
1249 drive_stat_acct(req
, 0);
1250 elv_bio_merged(q
, req
, bio
);
1251 if (!attempt_back_merge(q
, req
))
1252 elv_merged_request(q
, req
, el_ret
);
1255 case ELEVATOR_FRONT_MERGE
:
1256 BUG_ON(!rq_mergeable(req
));
1258 if (!ll_front_merge_fn(q
, req
, bio
))
1261 trace_block_bio_frontmerge(q
, bio
);
1263 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
) {
1264 blk_rq_set_mixed_merge(req
);
1265 req
->cmd_flags
&= ~REQ_FAILFAST_MASK
;
1266 req
->cmd_flags
|= ff
;
1269 bio
->bi_next
= req
->bio
;
1273 * may not be valid. if the low level driver said
1274 * it didn't need a bounce buffer then it better
1275 * not touch req->buffer either...
1277 req
->buffer
= bio_data(bio
);
1278 req
->__sector
= bio
->bi_sector
;
1279 req
->__data_len
+= bytes
;
1280 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1281 if (!blk_rq_cpu_valid(req
))
1282 req
->cpu
= bio
->bi_comp_cpu
;
1283 drive_stat_acct(req
, 0);
1284 elv_bio_merged(q
, req
, bio
);
1285 if (!attempt_front_merge(q
, req
))
1286 elv_merged_request(q
, req
, el_ret
);
1289 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1296 * This sync check and mask will be re-done in init_request_from_bio(),
1297 * but we need to set it earlier to expose the sync flag to the
1298 * rq allocator and io schedulers.
1300 rw_flags
= bio_data_dir(bio
);
1302 rw_flags
|= REQ_SYNC
;
1305 * Grab a free request. This is might sleep but can not fail.
1306 * Returns with the queue unlocked.
1308 req
= get_request_wait(q
, rw_flags
, bio
);
1311 * After dropping the lock and possibly sleeping here, our request
1312 * may now be mergeable after it had proven unmergeable (above).
1313 * We don't worry about that case for efficiency. It won't happen
1314 * often, and the elevators are able to handle it.
1316 init_request_from_bio(req
, bio
);
1318 spin_lock_irq(q
->queue_lock
);
1319 if (test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
) ||
1320 bio_flagged(bio
, BIO_CPU_AFFINE
))
1321 req
->cpu
= blk_cpu_to_group(smp_processor_id());
1322 if (queue_should_plug(q
) && elv_queue_empty(q
))
1325 /* insert the request into the elevator */
1326 drive_stat_acct(req
, 1);
1327 __elv_add_request(q
, req
, where
, 0);
1329 if (unplug
|| !queue_should_plug(q
))
1330 __generic_unplug_device(q
);
1331 spin_unlock_irq(q
->queue_lock
);
1336 * If bio->bi_dev is a partition, remap the location
1338 static inline void blk_partition_remap(struct bio
*bio
)
1340 struct block_device
*bdev
= bio
->bi_bdev
;
1342 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1343 struct hd_struct
*p
= bdev
->bd_part
;
1345 bio
->bi_sector
+= p
->start_sect
;
1346 bio
->bi_bdev
= bdev
->bd_contains
;
1348 trace_block_bio_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1350 bio
->bi_sector
- p
->start_sect
);
1354 static void handle_bad_sector(struct bio
*bio
)
1356 char b
[BDEVNAME_SIZE
];
1358 printk(KERN_INFO
"attempt to access beyond end of device\n");
1359 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1360 bdevname(bio
->bi_bdev
, b
),
1362 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1363 (long long)(i_size_read(bio
->bi_bdev
->bd_inode
) >> 9));
1365 set_bit(BIO_EOF
, &bio
->bi_flags
);
1368 #ifdef CONFIG_FAIL_MAKE_REQUEST
1370 static DECLARE_FAULT_ATTR(fail_make_request
);
1372 static int __init
setup_fail_make_request(char *str
)
1374 return setup_fault_attr(&fail_make_request
, str
);
1376 __setup("fail_make_request=", setup_fail_make_request
);
1378 static int should_fail_request(struct bio
*bio
)
1380 struct hd_struct
*part
= bio
->bi_bdev
->bd_part
;
1382 if (part_to_disk(part
)->part0
.make_it_fail
|| part
->make_it_fail
)
1383 return should_fail(&fail_make_request
, bio
->bi_size
);
1388 static int __init
fail_make_request_debugfs(void)
1390 return init_fault_attr_dentries(&fail_make_request
,
1391 "fail_make_request");
1394 late_initcall(fail_make_request_debugfs
);
1396 #else /* CONFIG_FAIL_MAKE_REQUEST */
1398 static inline int should_fail_request(struct bio
*bio
)
1403 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1406 * Check whether this bio extends beyond the end of the device.
1408 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1415 /* Test device or partition size, when known. */
1416 maxsector
= i_size_read(bio
->bi_bdev
->bd_inode
) >> 9;
1418 sector_t sector
= bio
->bi_sector
;
1420 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1422 * This may well happen - the kernel calls bread()
1423 * without checking the size of the device, e.g., when
1424 * mounting a device.
1426 handle_bad_sector(bio
);
1435 * generic_make_request - hand a buffer to its device driver for I/O
1436 * @bio: The bio describing the location in memory and on the device.
1438 * generic_make_request() is used to make I/O requests of block
1439 * devices. It is passed a &struct bio, which describes the I/O that needs
1442 * generic_make_request() does not return any status. The
1443 * success/failure status of the request, along with notification of
1444 * completion, is delivered asynchronously through the bio->bi_end_io
1445 * function described (one day) else where.
1447 * The caller of generic_make_request must make sure that bi_io_vec
1448 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1449 * set to describe the device address, and the
1450 * bi_end_io and optionally bi_private are set to describe how
1451 * completion notification should be signaled.
1453 * generic_make_request and the drivers it calls may use bi_next if this
1454 * bio happens to be merged with someone else, and may change bi_dev and
1455 * bi_sector for remaps as it sees fit. So the values of these fields
1456 * should NOT be depended on after the call to generic_make_request.
1458 static inline void __generic_make_request(struct bio
*bio
)
1460 struct request_queue
*q
;
1461 sector_t old_sector
;
1462 int ret
, nr_sectors
= bio_sectors(bio
);
1468 if (bio_check_eod(bio
, nr_sectors
))
1472 * Resolve the mapping until finished. (drivers are
1473 * still free to implement/resolve their own stacking
1474 * by explicitly returning 0)
1476 * NOTE: we don't repeat the blk_size check for each new device.
1477 * Stacking drivers are expected to know what they are doing.
1482 char b
[BDEVNAME_SIZE
];
1484 q
= bdev_get_queue(bio
->bi_bdev
);
1487 "generic_make_request: Trying to access "
1488 "nonexistent block-device %s (%Lu)\n",
1489 bdevname(bio
->bi_bdev
, b
),
1490 (long long) bio
->bi_sector
);
1494 if (unlikely(!(bio
->bi_rw
& REQ_DISCARD
) &&
1495 nr_sectors
> queue_max_hw_sectors(q
))) {
1496 printk(KERN_ERR
"bio too big device %s (%u > %u)\n",
1497 bdevname(bio
->bi_bdev
, b
),
1499 queue_max_hw_sectors(q
));
1503 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1506 if (should_fail_request(bio
))
1510 * If this device has partitions, remap block n
1511 * of partition p to block n+start(p) of the disk.
1513 blk_partition_remap(bio
);
1515 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
))
1518 if (old_sector
!= -1)
1519 trace_block_bio_remap(q
, bio
, old_dev
, old_sector
);
1521 old_sector
= bio
->bi_sector
;
1522 old_dev
= bio
->bi_bdev
->bd_dev
;
1524 if (bio_check_eod(bio
, nr_sectors
))
1528 * Filter flush bio's early so that make_request based
1529 * drivers without flush support don't have to worry
1532 if ((bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) && !q
->flush_flags
) {
1533 bio
->bi_rw
&= ~(REQ_FLUSH
| REQ_FUA
);
1540 if ((bio
->bi_rw
& REQ_DISCARD
) &&
1541 (!blk_queue_discard(q
) ||
1542 ((bio
->bi_rw
& REQ_SECURE
) &&
1543 !blk_queue_secdiscard(q
)))) {
1548 blk_throtl_bio(q
, &bio
);
1551 * If bio = NULL, bio has been throttled and will be submitted
1557 trace_block_bio_queue(q
, bio
);
1559 ret
= q
->make_request_fn(q
, bio
);
1565 bio_endio(bio
, err
);
1569 * We only want one ->make_request_fn to be active at a time,
1570 * else stack usage with stacked devices could be a problem.
1571 * So use current->bio_list to keep a list of requests
1572 * submited by a make_request_fn function.
1573 * current->bio_list is also used as a flag to say if
1574 * generic_make_request is currently active in this task or not.
1575 * If it is NULL, then no make_request is active. If it is non-NULL,
1576 * then a make_request is active, and new requests should be added
1579 void generic_make_request(struct bio
*bio
)
1581 struct bio_list bio_list_on_stack
;
1583 if (current
->bio_list
) {
1584 /* make_request is active */
1585 bio_list_add(current
->bio_list
, bio
);
1588 /* following loop may be a bit non-obvious, and so deserves some
1590 * Before entering the loop, bio->bi_next is NULL (as all callers
1591 * ensure that) so we have a list with a single bio.
1592 * We pretend that we have just taken it off a longer list, so
1593 * we assign bio_list to a pointer to the bio_list_on_stack,
1594 * thus initialising the bio_list of new bios to be
1595 * added. __generic_make_request may indeed add some more bios
1596 * through a recursive call to generic_make_request. If it
1597 * did, we find a non-NULL value in bio_list and re-enter the loop
1598 * from the top. In this case we really did just take the bio
1599 * of the top of the list (no pretending) and so remove it from
1600 * bio_list, and call into __generic_make_request again.
1602 * The loop was structured like this to make only one call to
1603 * __generic_make_request (which is important as it is large and
1604 * inlined) and to keep the structure simple.
1606 BUG_ON(bio
->bi_next
);
1607 bio_list_init(&bio_list_on_stack
);
1608 current
->bio_list
= &bio_list_on_stack
;
1610 __generic_make_request(bio
);
1611 bio
= bio_list_pop(current
->bio_list
);
1613 current
->bio_list
= NULL
; /* deactivate */
1615 EXPORT_SYMBOL(generic_make_request
);
1618 * submit_bio - submit a bio to the block device layer for I/O
1619 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1620 * @bio: The &struct bio which describes the I/O
1622 * submit_bio() is very similar in purpose to generic_make_request(), and
1623 * uses that function to do most of the work. Both are fairly rough
1624 * interfaces; @bio must be presetup and ready for I/O.
1627 void submit_bio(int rw
, struct bio
*bio
)
1629 int count
= bio_sectors(bio
);
1634 * If it's a regular read/write or a barrier with data attached,
1635 * go through the normal accounting stuff before submission.
1637 if (bio_has_data(bio
) && !(rw
& REQ_DISCARD
)) {
1639 count_vm_events(PGPGOUT
, count
);
1641 task_io_account_read(bio
->bi_size
);
1642 count_vm_events(PGPGIN
, count
);
1645 if (unlikely(block_dump
)) {
1646 char b
[BDEVNAME_SIZE
];
1647 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s (%u sectors)\n",
1648 current
->comm
, task_pid_nr(current
),
1649 (rw
& WRITE
) ? "WRITE" : "READ",
1650 (unsigned long long)bio
->bi_sector
,
1651 bdevname(bio
->bi_bdev
, b
),
1656 generic_make_request(bio
);
1658 EXPORT_SYMBOL(submit_bio
);
1661 * blk_rq_check_limits - Helper function to check a request for the queue limit
1663 * @rq: the request being checked
1666 * @rq may have been made based on weaker limitations of upper-level queues
1667 * in request stacking drivers, and it may violate the limitation of @q.
1668 * Since the block layer and the underlying device driver trust @rq
1669 * after it is inserted to @q, it should be checked against @q before
1670 * the insertion using this generic function.
1672 * This function should also be useful for request stacking drivers
1673 * in some cases below, so export this function.
1674 * Request stacking drivers like request-based dm may change the queue
1675 * limits while requests are in the queue (e.g. dm's table swapping).
1676 * Such request stacking drivers should check those requests agaist
1677 * the new queue limits again when they dispatch those requests,
1678 * although such checkings are also done against the old queue limits
1679 * when submitting requests.
1681 int blk_rq_check_limits(struct request_queue
*q
, struct request
*rq
)
1683 if (rq
->cmd_flags
& REQ_DISCARD
)
1686 if (blk_rq_sectors(rq
) > queue_max_sectors(q
) ||
1687 blk_rq_bytes(rq
) > queue_max_hw_sectors(q
) << 9) {
1688 printk(KERN_ERR
"%s: over max size limit.\n", __func__
);
1693 * queue's settings related to segment counting like q->bounce_pfn
1694 * may differ from that of other stacking queues.
1695 * Recalculate it to check the request correctly on this queue's
1698 blk_recalc_rq_segments(rq
);
1699 if (rq
->nr_phys_segments
> queue_max_segments(q
)) {
1700 printk(KERN_ERR
"%s: over max segments limit.\n", __func__
);
1706 EXPORT_SYMBOL_GPL(blk_rq_check_limits
);
1709 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1710 * @q: the queue to submit the request
1711 * @rq: the request being queued
1713 int blk_insert_cloned_request(struct request_queue
*q
, struct request
*rq
)
1715 unsigned long flags
;
1717 if (blk_rq_check_limits(q
, rq
))
1720 #ifdef CONFIG_FAIL_MAKE_REQUEST
1721 if (rq
->rq_disk
&& rq
->rq_disk
->part0
.make_it_fail
&&
1722 should_fail(&fail_make_request
, blk_rq_bytes(rq
)))
1726 spin_lock_irqsave(q
->queue_lock
, flags
);
1729 * Submitting request must be dequeued before calling this function
1730 * because it will be linked to another request_queue
1732 BUG_ON(blk_queued_rq(rq
));
1734 drive_stat_acct(rq
, 1);
1735 __elv_add_request(q
, rq
, ELEVATOR_INSERT_BACK
, 0);
1737 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1741 EXPORT_SYMBOL_GPL(blk_insert_cloned_request
);
1744 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1745 * @rq: request to examine
1748 * A request could be merge of IOs which require different failure
1749 * handling. This function determines the number of bytes which
1750 * can be failed from the beginning of the request without
1751 * crossing into area which need to be retried further.
1754 * The number of bytes to fail.
1757 * queue_lock must be held.
1759 unsigned int blk_rq_err_bytes(const struct request
*rq
)
1761 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
1762 unsigned int bytes
= 0;
1765 if (!(rq
->cmd_flags
& REQ_MIXED_MERGE
))
1766 return blk_rq_bytes(rq
);
1769 * Currently the only 'mixing' which can happen is between
1770 * different fastfail types. We can safely fail portions
1771 * which have all the failfast bits that the first one has -
1772 * the ones which are at least as eager to fail as the first
1775 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
1776 if ((bio
->bi_rw
& ff
) != ff
)
1778 bytes
+= bio
->bi_size
;
1781 /* this could lead to infinite loop */
1782 BUG_ON(blk_rq_bytes(rq
) && !bytes
);
1785 EXPORT_SYMBOL_GPL(blk_rq_err_bytes
);
1787 static void blk_account_io_completion(struct request
*req
, unsigned int bytes
)
1789 if (blk_do_io_stat(req
)) {
1790 const int rw
= rq_data_dir(req
);
1791 struct hd_struct
*part
;
1794 cpu
= part_stat_lock();
1796 part_stat_add(cpu
, part
, sectors
[rw
], bytes
>> 9);
1801 static void blk_account_io_done(struct request
*req
)
1804 * Account IO completion. flush_rq isn't accounted as a
1805 * normal IO on queueing nor completion. Accounting the
1806 * containing request is enough.
1808 if (blk_do_io_stat(req
) && req
!= &req
->q
->flush_rq
) {
1809 unsigned long duration
= jiffies
- req
->start_time
;
1810 const int rw
= rq_data_dir(req
);
1811 struct hd_struct
*part
;
1814 cpu
= part_stat_lock();
1817 part_stat_inc(cpu
, part
, ios
[rw
]);
1818 part_stat_add(cpu
, part
, ticks
[rw
], duration
);
1819 part_round_stats(cpu
, part
);
1820 part_dec_in_flight(part
, rw
);
1822 hd_struct_put(part
);
1828 * blk_peek_request - peek at the top of a request queue
1829 * @q: request queue to peek at
1832 * Return the request at the top of @q. The returned request
1833 * should be started using blk_start_request() before LLD starts
1837 * Pointer to the request at the top of @q if available. Null
1841 * queue_lock must be held.
1843 struct request
*blk_peek_request(struct request_queue
*q
)
1848 while ((rq
= __elv_next_request(q
)) != NULL
) {
1849 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
1851 * This is the first time the device driver
1852 * sees this request (possibly after
1853 * requeueing). Notify IO scheduler.
1855 if (rq
->cmd_flags
& REQ_SORTED
)
1856 elv_activate_rq(q
, rq
);
1859 * just mark as started even if we don't start
1860 * it, a request that has been delayed should
1861 * not be passed by new incoming requests
1863 rq
->cmd_flags
|= REQ_STARTED
;
1864 trace_block_rq_issue(q
, rq
);
1867 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
1868 q
->end_sector
= rq_end_sector(rq
);
1869 q
->boundary_rq
= NULL
;
1872 if (rq
->cmd_flags
& REQ_DONTPREP
)
1875 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
1877 * make sure space for the drain appears we
1878 * know we can do this because max_hw_segments
1879 * has been adjusted to be one fewer than the
1882 rq
->nr_phys_segments
++;
1888 ret
= q
->prep_rq_fn(q
, rq
);
1889 if (ret
== BLKPREP_OK
) {
1891 } else if (ret
== BLKPREP_DEFER
) {
1893 * the request may have been (partially) prepped.
1894 * we need to keep this request in the front to
1895 * avoid resource deadlock. REQ_STARTED will
1896 * prevent other fs requests from passing this one.
1898 if (q
->dma_drain_size
&& blk_rq_bytes(rq
) &&
1899 !(rq
->cmd_flags
& REQ_DONTPREP
)) {
1901 * remove the space for the drain we added
1902 * so that we don't add it again
1904 --rq
->nr_phys_segments
;
1909 } else if (ret
== BLKPREP_KILL
) {
1910 rq
->cmd_flags
|= REQ_QUIET
;
1912 * Mark this request as started so we don't trigger
1913 * any debug logic in the end I/O path.
1915 blk_start_request(rq
);
1916 __blk_end_request_all(rq
, -EIO
);
1918 printk(KERN_ERR
"%s: bad return=%d\n", __func__
, ret
);
1925 EXPORT_SYMBOL(blk_peek_request
);
1927 void blk_dequeue_request(struct request
*rq
)
1929 struct request_queue
*q
= rq
->q
;
1931 BUG_ON(list_empty(&rq
->queuelist
));
1932 BUG_ON(ELV_ON_HASH(rq
));
1934 list_del_init(&rq
->queuelist
);
1937 * the time frame between a request being removed from the lists
1938 * and to it is freed is accounted as io that is in progress at
1941 if (blk_account_rq(rq
)) {
1942 q
->in_flight
[rq_is_sync(rq
)]++;
1943 set_io_start_time_ns(rq
);
1948 * blk_start_request - start request processing on the driver
1949 * @req: request to dequeue
1952 * Dequeue @req and start timeout timer on it. This hands off the
1953 * request to the driver.
1955 * Block internal functions which don't want to start timer should
1956 * call blk_dequeue_request().
1959 * queue_lock must be held.
1961 void blk_start_request(struct request
*req
)
1963 blk_dequeue_request(req
);
1966 * We are now handing the request to the hardware, initialize
1967 * resid_len to full count and add the timeout handler.
1969 req
->resid_len
= blk_rq_bytes(req
);
1970 if (unlikely(blk_bidi_rq(req
)))
1971 req
->next_rq
->resid_len
= blk_rq_bytes(req
->next_rq
);
1975 EXPORT_SYMBOL(blk_start_request
);
1978 * blk_fetch_request - fetch a request from a request queue
1979 * @q: request queue to fetch a request from
1982 * Return the request at the top of @q. The request is started on
1983 * return and LLD can start processing it immediately.
1986 * Pointer to the request at the top of @q if available. Null
1990 * queue_lock must be held.
1992 struct request
*blk_fetch_request(struct request_queue
*q
)
1996 rq
= blk_peek_request(q
);
1998 blk_start_request(rq
);
2001 EXPORT_SYMBOL(blk_fetch_request
);
2004 * blk_update_request - Special helper function for request stacking drivers
2005 * @req: the request being processed
2006 * @error: %0 for success, < %0 for error
2007 * @nr_bytes: number of bytes to complete @req
2010 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2011 * the request structure even if @req doesn't have leftover.
2012 * If @req has leftover, sets it up for the next range of segments.
2014 * This special helper function is only for request stacking drivers
2015 * (e.g. request-based dm) so that they can handle partial completion.
2016 * Actual device drivers should use blk_end_request instead.
2018 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2019 * %false return from this function.
2022 * %false - this request doesn't have any more data
2023 * %true - this request has more data
2025 bool blk_update_request(struct request
*req
, int error
, unsigned int nr_bytes
)
2027 int total_bytes
, bio_nbytes
, next_idx
= 0;
2033 trace_block_rq_complete(req
->q
, req
);
2036 * For fs requests, rq is just carrier of independent bio's
2037 * and each partial completion should be handled separately.
2038 * Reset per-request error on each partial completion.
2040 * TODO: tj: This is too subtle. It would be better to let
2041 * low level drivers do what they see fit.
2043 if (req
->cmd_type
== REQ_TYPE_FS
)
2046 if (error
&& req
->cmd_type
== REQ_TYPE_FS
&&
2047 !(req
->cmd_flags
& REQ_QUIET
)) {
2048 printk(KERN_ERR
"end_request: I/O error, dev %s, sector %llu\n",
2049 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
2050 (unsigned long long)blk_rq_pos(req
));
2053 blk_account_io_completion(req
, nr_bytes
);
2055 total_bytes
= bio_nbytes
= 0;
2056 while ((bio
= req
->bio
) != NULL
) {
2059 if (nr_bytes
>= bio
->bi_size
) {
2060 req
->bio
= bio
->bi_next
;
2061 nbytes
= bio
->bi_size
;
2062 req_bio_endio(req
, bio
, nbytes
, error
);
2066 int idx
= bio
->bi_idx
+ next_idx
;
2068 if (unlikely(idx
>= bio
->bi_vcnt
)) {
2069 blk_dump_rq_flags(req
, "__end_that");
2070 printk(KERN_ERR
"%s: bio idx %d >= vcnt %d\n",
2071 __func__
, idx
, bio
->bi_vcnt
);
2075 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
2076 BIO_BUG_ON(nbytes
> bio
->bi_size
);
2079 * not a complete bvec done
2081 if (unlikely(nbytes
> nr_bytes
)) {
2082 bio_nbytes
+= nr_bytes
;
2083 total_bytes
+= nr_bytes
;
2088 * advance to the next vector
2091 bio_nbytes
+= nbytes
;
2094 total_bytes
+= nbytes
;
2100 * end more in this run, or just return 'not-done'
2102 if (unlikely(nr_bytes
<= 0))
2112 * Reset counters so that the request stacking driver
2113 * can find how many bytes remain in the request
2116 req
->__data_len
= 0;
2121 * if the request wasn't completed, update state
2124 req_bio_endio(req
, bio
, bio_nbytes
, error
);
2125 bio
->bi_idx
+= next_idx
;
2126 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
2127 bio_iovec(bio
)->bv_len
-= nr_bytes
;
2130 req
->__data_len
-= total_bytes
;
2131 req
->buffer
= bio_data(req
->bio
);
2133 /* update sector only for requests with clear definition of sector */
2134 if (req
->cmd_type
== REQ_TYPE_FS
|| (req
->cmd_flags
& REQ_DISCARD
))
2135 req
->__sector
+= total_bytes
>> 9;
2137 /* mixed attributes always follow the first bio */
2138 if (req
->cmd_flags
& REQ_MIXED_MERGE
) {
2139 req
->cmd_flags
&= ~REQ_FAILFAST_MASK
;
2140 req
->cmd_flags
|= req
->bio
->bi_rw
& REQ_FAILFAST_MASK
;
2144 * If total number of sectors is less than the first segment
2145 * size, something has gone terribly wrong.
2147 if (blk_rq_bytes(req
) < blk_rq_cur_bytes(req
)) {
2148 printk(KERN_ERR
"blk: request botched\n");
2149 req
->__data_len
= blk_rq_cur_bytes(req
);
2152 /* recalculate the number of segments */
2153 blk_recalc_rq_segments(req
);
2157 EXPORT_SYMBOL_GPL(blk_update_request
);
2159 static bool blk_update_bidi_request(struct request
*rq
, int error
,
2160 unsigned int nr_bytes
,
2161 unsigned int bidi_bytes
)
2163 if (blk_update_request(rq
, error
, nr_bytes
))
2166 /* Bidi request must be completed as a whole */
2167 if (unlikely(blk_bidi_rq(rq
)) &&
2168 blk_update_request(rq
->next_rq
, error
, bidi_bytes
))
2171 if (blk_queue_add_random(rq
->q
))
2172 add_disk_randomness(rq
->rq_disk
);
2178 * blk_unprep_request - unprepare a request
2181 * This function makes a request ready for complete resubmission (or
2182 * completion). It happens only after all error handling is complete,
2183 * so represents the appropriate moment to deallocate any resources
2184 * that were allocated to the request in the prep_rq_fn. The queue
2185 * lock is held when calling this.
2187 void blk_unprep_request(struct request
*req
)
2189 struct request_queue
*q
= req
->q
;
2191 req
->cmd_flags
&= ~REQ_DONTPREP
;
2192 if (q
->unprep_rq_fn
)
2193 q
->unprep_rq_fn(q
, req
);
2195 EXPORT_SYMBOL_GPL(blk_unprep_request
);
2198 * queue lock must be held
2200 static void blk_finish_request(struct request
*req
, int error
)
2202 if (blk_rq_tagged(req
))
2203 blk_queue_end_tag(req
->q
, req
);
2205 BUG_ON(blk_queued_rq(req
));
2207 if (unlikely(laptop_mode
) && req
->cmd_type
== REQ_TYPE_FS
)
2208 laptop_io_completion(&req
->q
->backing_dev_info
);
2210 blk_delete_timer(req
);
2212 if (req
->cmd_flags
& REQ_DONTPREP
)
2213 blk_unprep_request(req
);
2216 blk_account_io_done(req
);
2219 req
->end_io(req
, error
);
2221 if (blk_bidi_rq(req
))
2222 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
2224 __blk_put_request(req
->q
, req
);
2229 * blk_end_bidi_request - Complete a bidi request
2230 * @rq: the request to complete
2231 * @error: %0 for success, < %0 for error
2232 * @nr_bytes: number of bytes to complete @rq
2233 * @bidi_bytes: number of bytes to complete @rq->next_rq
2236 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2237 * Drivers that supports bidi can safely call this member for any
2238 * type of request, bidi or uni. In the later case @bidi_bytes is
2242 * %false - we are done with this request
2243 * %true - still buffers pending for this request
2245 static bool blk_end_bidi_request(struct request
*rq
, int error
,
2246 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2248 struct request_queue
*q
= rq
->q
;
2249 unsigned long flags
;
2251 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2254 spin_lock_irqsave(q
->queue_lock
, flags
);
2255 blk_finish_request(rq
, error
);
2256 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2262 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2263 * @rq: the request to complete
2264 * @error: %0 for success, < %0 for error
2265 * @nr_bytes: number of bytes to complete @rq
2266 * @bidi_bytes: number of bytes to complete @rq->next_rq
2269 * Identical to blk_end_bidi_request() except that queue lock is
2270 * assumed to be locked on entry and remains so on return.
2273 * %false - we are done with this request
2274 * %true - still buffers pending for this request
2276 static bool __blk_end_bidi_request(struct request
*rq
, int error
,
2277 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2279 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2282 blk_finish_request(rq
, error
);
2288 * blk_end_request - Helper function for drivers to complete the request.
2289 * @rq: the request being processed
2290 * @error: %0 for success, < %0 for error
2291 * @nr_bytes: number of bytes to complete
2294 * Ends I/O on a number of bytes attached to @rq.
2295 * If @rq has leftover, sets it up for the next range of segments.
2298 * %false - we are done with this request
2299 * %true - still buffers pending for this request
2301 bool blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2303 return blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2305 EXPORT_SYMBOL(blk_end_request
);
2308 * blk_end_request_all - Helper function for drives to finish the request.
2309 * @rq: the request to finish
2310 * @error: %0 for success, < %0 for error
2313 * Completely finish @rq.
2315 void blk_end_request_all(struct request
*rq
, int error
)
2318 unsigned int bidi_bytes
= 0;
2320 if (unlikely(blk_bidi_rq(rq
)))
2321 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2323 pending
= blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2326 EXPORT_SYMBOL(blk_end_request_all
);
2329 * blk_end_request_cur - Helper function to finish the current request chunk.
2330 * @rq: the request to finish the current chunk for
2331 * @error: %0 for success, < %0 for error
2334 * Complete the current consecutively mapped chunk from @rq.
2337 * %false - we are done with this request
2338 * %true - still buffers pending for this request
2340 bool blk_end_request_cur(struct request
*rq
, int error
)
2342 return blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2344 EXPORT_SYMBOL(blk_end_request_cur
);
2347 * blk_end_request_err - Finish a request till the next failure boundary.
2348 * @rq: the request to finish till the next failure boundary for
2349 * @error: must be negative errno
2352 * Complete @rq till the next failure boundary.
2355 * %false - we are done with this request
2356 * %true - still buffers pending for this request
2358 bool blk_end_request_err(struct request
*rq
, int error
)
2360 WARN_ON(error
>= 0);
2361 return blk_end_request(rq
, error
, blk_rq_err_bytes(rq
));
2363 EXPORT_SYMBOL_GPL(blk_end_request_err
);
2366 * __blk_end_request - Helper function for drivers to complete the request.
2367 * @rq: the request being processed
2368 * @error: %0 for success, < %0 for error
2369 * @nr_bytes: number of bytes to complete
2372 * Must be called with queue lock held unlike blk_end_request().
2375 * %false - we are done with this request
2376 * %true - still buffers pending for this request
2378 bool __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2380 return __blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2382 EXPORT_SYMBOL(__blk_end_request
);
2385 * __blk_end_request_all - Helper function for drives to finish the request.
2386 * @rq: the request to finish
2387 * @error: %0 for success, < %0 for error
2390 * Completely finish @rq. Must be called with queue lock held.
2392 void __blk_end_request_all(struct request
*rq
, int error
)
2395 unsigned int bidi_bytes
= 0;
2397 if (unlikely(blk_bidi_rq(rq
)))
2398 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2400 pending
= __blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2403 EXPORT_SYMBOL(__blk_end_request_all
);
2406 * __blk_end_request_cur - Helper function to finish the current request chunk.
2407 * @rq: the request to finish the current chunk for
2408 * @error: %0 for success, < %0 for error
2411 * Complete the current consecutively mapped chunk from @rq. Must
2412 * be called with queue lock held.
2415 * %false - we are done with this request
2416 * %true - still buffers pending for this request
2418 bool __blk_end_request_cur(struct request
*rq
, int error
)
2420 return __blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2422 EXPORT_SYMBOL(__blk_end_request_cur
);
2425 * __blk_end_request_err - Finish a request till the next failure boundary.
2426 * @rq: the request to finish till the next failure boundary for
2427 * @error: must be negative errno
2430 * Complete @rq till the next failure boundary. Must be called
2431 * with queue lock held.
2434 * %false - we are done with this request
2435 * %true - still buffers pending for this request
2437 bool __blk_end_request_err(struct request
*rq
, int error
)
2439 WARN_ON(error
>= 0);
2440 return __blk_end_request(rq
, error
, blk_rq_err_bytes(rq
));
2442 EXPORT_SYMBOL_GPL(__blk_end_request_err
);
2444 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2447 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
2448 rq
->cmd_flags
|= bio
->bi_rw
& REQ_WRITE
;
2450 if (bio_has_data(bio
)) {
2451 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2452 rq
->buffer
= bio_data(bio
);
2454 rq
->__data_len
= bio
->bi_size
;
2455 rq
->bio
= rq
->biotail
= bio
;
2458 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2461 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2463 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2464 * @rq: the request to be flushed
2467 * Flush all pages in @rq.
2469 void rq_flush_dcache_pages(struct request
*rq
)
2471 struct req_iterator iter
;
2472 struct bio_vec
*bvec
;
2474 rq_for_each_segment(bvec
, rq
, iter
)
2475 flush_dcache_page(bvec
->bv_page
);
2477 EXPORT_SYMBOL_GPL(rq_flush_dcache_pages
);
2481 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2482 * @q : the queue of the device being checked
2485 * Check if underlying low-level drivers of a device are busy.
2486 * If the drivers want to export their busy state, they must set own
2487 * exporting function using blk_queue_lld_busy() first.
2489 * Basically, this function is used only by request stacking drivers
2490 * to stop dispatching requests to underlying devices when underlying
2491 * devices are busy. This behavior helps more I/O merging on the queue
2492 * of the request stacking driver and prevents I/O throughput regression
2493 * on burst I/O load.
2496 * 0 - Not busy (The request stacking driver should dispatch request)
2497 * 1 - Busy (The request stacking driver should stop dispatching request)
2499 int blk_lld_busy(struct request_queue
*q
)
2502 return q
->lld_busy_fn(q
);
2506 EXPORT_SYMBOL_GPL(blk_lld_busy
);
2509 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2510 * @rq: the clone request to be cleaned up
2513 * Free all bios in @rq for a cloned request.
2515 void blk_rq_unprep_clone(struct request
*rq
)
2519 while ((bio
= rq
->bio
) != NULL
) {
2520 rq
->bio
= bio
->bi_next
;
2525 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone
);
2528 * Copy attributes of the original request to the clone request.
2529 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2531 static void __blk_rq_prep_clone(struct request
*dst
, struct request
*src
)
2533 dst
->cpu
= src
->cpu
;
2534 dst
->cmd_flags
= (src
->cmd_flags
& REQ_CLONE_MASK
) | REQ_NOMERGE
;
2535 dst
->cmd_type
= src
->cmd_type
;
2536 dst
->__sector
= blk_rq_pos(src
);
2537 dst
->__data_len
= blk_rq_bytes(src
);
2538 dst
->nr_phys_segments
= src
->nr_phys_segments
;
2539 dst
->ioprio
= src
->ioprio
;
2540 dst
->extra_len
= src
->extra_len
;
2544 * blk_rq_prep_clone - Helper function to setup clone request
2545 * @rq: the request to be setup
2546 * @rq_src: original request to be cloned
2547 * @bs: bio_set that bios for clone are allocated from
2548 * @gfp_mask: memory allocation mask for bio
2549 * @bio_ctr: setup function to be called for each clone bio.
2550 * Returns %0 for success, non %0 for failure.
2551 * @data: private data to be passed to @bio_ctr
2554 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2555 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2556 * are not copied, and copying such parts is the caller's responsibility.
2557 * Also, pages which the original bios are pointing to are not copied
2558 * and the cloned bios just point same pages.
2559 * So cloned bios must be completed before original bios, which means
2560 * the caller must complete @rq before @rq_src.
2562 int blk_rq_prep_clone(struct request
*rq
, struct request
*rq_src
,
2563 struct bio_set
*bs
, gfp_t gfp_mask
,
2564 int (*bio_ctr
)(struct bio
*, struct bio
*, void *),
2567 struct bio
*bio
, *bio_src
;
2572 blk_rq_init(NULL
, rq
);
2574 __rq_for_each_bio(bio_src
, rq_src
) {
2575 bio
= bio_alloc_bioset(gfp_mask
, bio_src
->bi_max_vecs
, bs
);
2579 __bio_clone(bio
, bio_src
);
2581 if (bio_integrity(bio_src
) &&
2582 bio_integrity_clone(bio
, bio_src
, gfp_mask
, bs
))
2585 if (bio_ctr
&& bio_ctr(bio
, bio_src
, data
))
2589 rq
->biotail
->bi_next
= bio
;
2592 rq
->bio
= rq
->biotail
= bio
;
2595 __blk_rq_prep_clone(rq
, rq_src
);
2602 blk_rq_unprep_clone(rq
);
2606 EXPORT_SYMBOL_GPL(blk_rq_prep_clone
);
2608 int kblockd_schedule_work(struct request_queue
*q
, struct work_struct
*work
)
2610 return queue_work(kblockd_workqueue
, work
);
2612 EXPORT_SYMBOL(kblockd_schedule_work
);
2614 int __init
blk_dev_init(void)
2616 BUILD_BUG_ON(__REQ_NR_BITS
> 8 *
2617 sizeof(((struct request
*)0)->cmd_flags
));
2619 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2620 kblockd_workqueue
= alloc_workqueue("kblockd",
2621 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
2622 if (!kblockd_workqueue
)
2623 panic("Failed to create kblockd\n");
2625 request_cachep
= kmem_cache_create("blkdev_requests",
2626 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2628 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2629 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);