2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
12 * This handles all read/write requests to block devices
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/bio.h>
18 #include <linux/blkdev.h>
19 #include <linux/highmem.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
24 #include <linux/completion.h>
25 #include <linux/slab.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/task_io_accounting_ops.h>
29 #include <linux/fault-inject.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/block.h>
36 EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap
);
37 EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap
);
38 EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete
);
40 static int __make_request(struct request_queue
*q
, struct bio
*bio
);
43 * For the allocated request tables
45 static struct kmem_cache
*request_cachep
;
48 * For queue allocation
50 struct kmem_cache
*blk_requestq_cachep
;
53 * Controlling structure to kblockd
55 static struct workqueue_struct
*kblockd_workqueue
;
57 static void drive_stat_acct(struct request
*rq
, int new_io
)
59 struct hd_struct
*part
;
60 int rw
= rq_data_dir(rq
);
63 if (!blk_do_io_stat(rq
))
66 cpu
= part_stat_lock();
67 part
= disk_map_sector_rcu(rq
->rq_disk
, blk_rq_pos(rq
));
70 part_stat_inc(cpu
, part
, merges
[rw
]);
72 part_round_stats(cpu
, part
);
73 part_inc_in_flight(part
, rw
);
79 void blk_queue_congestion_threshold(struct request_queue
*q
)
83 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
84 if (nr
> q
->nr_requests
)
86 q
->nr_congestion_on
= nr
;
88 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
91 q
->nr_congestion_off
= nr
;
95 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
98 * Locates the passed device's request queue and returns the address of its
101 * Will return NULL if the request queue cannot be located.
103 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
105 struct backing_dev_info
*ret
= NULL
;
106 struct request_queue
*q
= bdev_get_queue(bdev
);
109 ret
= &q
->backing_dev_info
;
112 EXPORT_SYMBOL(blk_get_backing_dev_info
);
114 void blk_rq_init(struct request_queue
*q
, struct request
*rq
)
116 memset(rq
, 0, sizeof(*rq
));
118 INIT_LIST_HEAD(&rq
->queuelist
);
119 INIT_LIST_HEAD(&rq
->timeout_list
);
122 rq
->__sector
= (sector_t
) -1;
123 INIT_HLIST_NODE(&rq
->hash
);
124 RB_CLEAR_NODE(&rq
->rb_node
);
126 rq
->cmd_len
= BLK_MAX_CDB
;
129 rq
->start_time
= jiffies
;
130 set_start_time_ns(rq
);
132 EXPORT_SYMBOL(blk_rq_init
);
134 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
135 unsigned int nbytes
, int error
)
137 struct request_queue
*q
= rq
->q
;
139 if (&q
->bar_rq
!= rq
) {
141 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
142 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
145 if (unlikely(nbytes
> bio
->bi_size
)) {
146 printk(KERN_ERR
"%s: want %u bytes done, %u left\n",
147 __func__
, nbytes
, bio
->bi_size
);
148 nbytes
= bio
->bi_size
;
151 if (unlikely(rq
->cmd_flags
& REQ_QUIET
))
152 set_bit(BIO_QUIET
, &bio
->bi_flags
);
154 bio
->bi_size
-= nbytes
;
155 bio
->bi_sector
+= (nbytes
>> 9);
157 if (bio_integrity(bio
))
158 bio_integrity_advance(bio
, nbytes
);
160 if (bio
->bi_size
== 0)
161 bio_endio(bio
, error
);
165 * Okay, this is the barrier request in progress, just
168 if (error
&& !q
->orderr
)
173 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
177 printk(KERN_INFO
"%s: dev %s: type=%x, flags=%x\n", msg
,
178 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
181 printk(KERN_INFO
" sector %llu, nr/cnr %u/%u\n",
182 (unsigned long long)blk_rq_pos(rq
),
183 blk_rq_sectors(rq
), blk_rq_cur_sectors(rq
));
184 printk(KERN_INFO
" bio %p, biotail %p, buffer %p, len %u\n",
185 rq
->bio
, rq
->biotail
, rq
->buffer
, blk_rq_bytes(rq
));
187 if (rq
->cmd_type
== REQ_TYPE_BLOCK_PC
) {
188 printk(KERN_INFO
" cdb: ");
189 for (bit
= 0; bit
< BLK_MAX_CDB
; bit
++)
190 printk("%02x ", rq
->cmd
[bit
]);
194 EXPORT_SYMBOL(blk_dump_rq_flags
);
197 * "plug" the device if there are no outstanding requests: this will
198 * force the transfer to start only after we have put all the requests
201 * This is called with interrupts off and no requests on the queue and
202 * with the queue lock held.
204 void blk_plug_device(struct request_queue
*q
)
206 WARN_ON(!irqs_disabled());
209 * don't plug a stopped queue, it must be paired with blk_start_queue()
210 * which will restart the queueing
212 if (blk_queue_stopped(q
))
215 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED
, q
)) {
216 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
220 EXPORT_SYMBOL(blk_plug_device
);
223 * blk_plug_device_unlocked - plug a device without queue lock held
224 * @q: The &struct request_queue to plug
227 * Like @blk_plug_device(), but grabs the queue lock and disables
230 void blk_plug_device_unlocked(struct request_queue
*q
)
234 spin_lock_irqsave(q
->queue_lock
, flags
);
236 spin_unlock_irqrestore(q
->queue_lock
, flags
);
238 EXPORT_SYMBOL(blk_plug_device_unlocked
);
241 * remove the queue from the plugged list, if present. called with
242 * queue lock held and interrupts disabled.
244 int blk_remove_plug(struct request_queue
*q
)
246 WARN_ON(!irqs_disabled());
248 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED
, q
))
251 del_timer(&q
->unplug_timer
);
254 EXPORT_SYMBOL(blk_remove_plug
);
257 * remove the plug and let it rip..
259 void __generic_unplug_device(struct request_queue
*q
)
261 if (unlikely(blk_queue_stopped(q
)))
263 if (!blk_remove_plug(q
) && !blk_queue_nonrot(q
))
270 * generic_unplug_device - fire a request queue
271 * @q: The &struct request_queue in question
274 * Linux uses plugging to build bigger requests queues before letting
275 * the device have at them. If a queue is plugged, the I/O scheduler
276 * is still adding and merging requests on the queue. Once the queue
277 * gets unplugged, the request_fn defined for the queue is invoked and
280 void generic_unplug_device(struct request_queue
*q
)
282 if (blk_queue_plugged(q
)) {
283 spin_lock_irq(q
->queue_lock
);
284 __generic_unplug_device(q
);
285 spin_unlock_irq(q
->queue_lock
);
288 EXPORT_SYMBOL(generic_unplug_device
);
290 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
293 struct request_queue
*q
= bdi
->unplug_io_data
;
298 void blk_unplug_work(struct work_struct
*work
)
300 struct request_queue
*q
=
301 container_of(work
, struct request_queue
, unplug_work
);
303 trace_block_unplug_io(q
);
307 void blk_unplug_timeout(unsigned long data
)
309 struct request_queue
*q
= (struct request_queue
*)data
;
311 trace_block_unplug_timer(q
);
312 kblockd_schedule_work(q
, &q
->unplug_work
);
315 void blk_unplug(struct request_queue
*q
)
318 * devices don't necessarily have an ->unplug_fn defined
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
);
385 throtl_shutdown_timer_wq(q
);
387 EXPORT_SYMBOL(blk_sync_queue
);
390 * __blk_run_queue - run a single device queue
391 * @q: The queue to run
394 * See @blk_run_queue. This variant must be called with the queue lock
395 * held and interrupts disabled.
398 void __blk_run_queue(struct request_queue
*q
)
402 if (unlikely(blk_queue_stopped(q
)))
405 if (elv_queue_empty(q
))
409 * Only recurse once to avoid overrunning the stack, let the unplug
410 * handling reinvoke the handler shortly if we already got there.
412 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
414 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
416 queue_flag_set(QUEUE_FLAG_PLUGGED
, q
);
417 kblockd_schedule_work(q
, &q
->unplug_work
);
420 EXPORT_SYMBOL(__blk_run_queue
);
423 * blk_run_queue - run a single device queue
424 * @q: The queue to run
427 * Invoke request handling on this queue, if it has pending work to do.
428 * May be used to restart queueing when a request has completed.
430 void blk_run_queue(struct request_queue
*q
)
434 spin_lock_irqsave(q
->queue_lock
, flags
);
436 spin_unlock_irqrestore(q
->queue_lock
, flags
);
438 EXPORT_SYMBOL(blk_run_queue
);
440 void blk_put_queue(struct request_queue
*q
)
442 kobject_put(&q
->kobj
);
445 void blk_cleanup_queue(struct request_queue
*q
)
448 * We know we have process context here, so we can be a little
449 * cautious and ensure that pending block actions on this device
450 * are done before moving on. Going into this function, we should
451 * not have processes doing IO to this device.
455 del_timer_sync(&q
->backing_dev_info
.laptop_mode_wb_timer
);
456 mutex_lock(&q
->sysfs_lock
);
457 queue_flag_set_unlocked(QUEUE_FLAG_DEAD
, q
);
458 mutex_unlock(&q
->sysfs_lock
);
461 elevator_exit(q
->elevator
);
467 EXPORT_SYMBOL(blk_cleanup_queue
);
469 static int blk_init_free_list(struct request_queue
*q
)
471 struct request_list
*rl
= &q
->rq
;
473 if (unlikely(rl
->rq_pool
))
476 rl
->count
[BLK_RW_SYNC
] = rl
->count
[BLK_RW_ASYNC
] = 0;
477 rl
->starved
[BLK_RW_SYNC
] = rl
->starved
[BLK_RW_ASYNC
] = 0;
479 init_waitqueue_head(&rl
->wait
[BLK_RW_SYNC
]);
480 init_waitqueue_head(&rl
->wait
[BLK_RW_ASYNC
]);
482 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
483 mempool_free_slab
, request_cachep
, q
->node
);
491 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
493 return blk_alloc_queue_node(gfp_mask
, -1);
495 EXPORT_SYMBOL(blk_alloc_queue
);
497 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
499 struct request_queue
*q
;
502 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
503 gfp_mask
| __GFP_ZERO
, node_id
);
507 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
508 q
->backing_dev_info
.unplug_io_data
= q
;
509 q
->backing_dev_info
.ra_pages
=
510 (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
511 q
->backing_dev_info
.state
= 0;
512 q
->backing_dev_info
.capabilities
= BDI_CAP_MAP_COPY
;
513 q
->backing_dev_info
.name
= "block";
515 err
= bdi_init(&q
->backing_dev_info
);
517 kmem_cache_free(blk_requestq_cachep
, q
);
521 if (blk_throtl_init(q
)) {
522 kmem_cache_free(blk_requestq_cachep
, q
);
526 setup_timer(&q
->backing_dev_info
.laptop_mode_wb_timer
,
527 laptop_mode_timer_fn
, (unsigned long) q
);
528 init_timer(&q
->unplug_timer
);
529 setup_timer(&q
->timeout
, blk_rq_timed_out_timer
, (unsigned long) q
);
530 INIT_LIST_HEAD(&q
->timeout_list
);
531 INIT_WORK(&q
->unplug_work
, blk_unplug_work
);
533 kobject_init(&q
->kobj
, &blk_queue_ktype
);
535 mutex_init(&q
->sysfs_lock
);
536 spin_lock_init(&q
->__queue_lock
);
540 EXPORT_SYMBOL(blk_alloc_queue_node
);
543 * blk_init_queue - prepare a request queue for use with a block device
544 * @rfn: The function to be called to process requests that have been
545 * placed on the queue.
546 * @lock: Request queue spin lock
549 * If a block device wishes to use the standard request handling procedures,
550 * which sorts requests and coalesces adjacent requests, then it must
551 * call blk_init_queue(). The function @rfn will be called when there
552 * are requests on the queue that need to be processed. If the device
553 * supports plugging, then @rfn may not be called immediately when requests
554 * are available on the queue, but may be called at some time later instead.
555 * Plugged queues are generally unplugged when a buffer belonging to one
556 * of the requests on the queue is needed, or due to memory pressure.
558 * @rfn is not required, or even expected, to remove all requests off the
559 * queue, but only as many as it can handle at a time. If it does leave
560 * requests on the queue, it is responsible for arranging that the requests
561 * get dealt with eventually.
563 * The queue spin lock must be held while manipulating the requests on the
564 * request queue; this lock will be taken also from interrupt context, so irq
565 * disabling is needed for it.
567 * Function returns a pointer to the initialized request queue, or %NULL if
571 * blk_init_queue() must be paired with a blk_cleanup_queue() call
572 * when the block device is deactivated (such as at module unload).
575 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
577 return blk_init_queue_node(rfn
, lock
, -1);
579 EXPORT_SYMBOL(blk_init_queue
);
581 struct request_queue
*
582 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
584 struct request_queue
*uninit_q
, *q
;
586 uninit_q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
590 q
= blk_init_allocated_queue_node(uninit_q
, rfn
, lock
, node_id
);
592 blk_cleanup_queue(uninit_q
);
596 EXPORT_SYMBOL(blk_init_queue_node
);
598 struct request_queue
*
599 blk_init_allocated_queue(struct request_queue
*q
, request_fn_proc
*rfn
,
602 return blk_init_allocated_queue_node(q
, rfn
, lock
, -1);
604 EXPORT_SYMBOL(blk_init_allocated_queue
);
606 struct request_queue
*
607 blk_init_allocated_queue_node(struct request_queue
*q
, request_fn_proc
*rfn
,
608 spinlock_t
*lock
, int node_id
)
614 if (blk_init_free_list(q
))
618 q
->prep_rq_fn
= NULL
;
619 q
->unprep_rq_fn
= NULL
;
620 q
->unplug_fn
= generic_unplug_device
;
621 q
->queue_flags
= QUEUE_FLAG_DEFAULT
;
622 q
->queue_lock
= lock
;
625 * This also sets hw/phys segments, boundary and size
627 blk_queue_make_request(q
, __make_request
);
629 q
->sg_reserved_size
= INT_MAX
;
634 if (!elevator_init(q
, NULL
)) {
635 blk_queue_congestion_threshold(q
);
641 EXPORT_SYMBOL(blk_init_allocated_queue_node
);
643 int blk_get_queue(struct request_queue
*q
)
645 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
646 kobject_get(&q
->kobj
);
653 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
655 if (rq
->cmd_flags
& REQ_ELVPRIV
)
656 elv_put_request(q
, rq
);
657 mempool_free(rq
, q
->rq
.rq_pool
);
660 static struct request
*
661 blk_alloc_request(struct request_queue
*q
, int flags
, int priv
, gfp_t gfp_mask
)
663 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
670 rq
->cmd_flags
= flags
| REQ_ALLOCED
;
673 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
674 mempool_free(rq
, q
->rq
.rq_pool
);
677 rq
->cmd_flags
|= REQ_ELVPRIV
;
684 * ioc_batching returns true if the ioc is a valid batching request and
685 * should be given priority access to a request.
687 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
693 * Make sure the process is able to allocate at least 1 request
694 * even if the batch times out, otherwise we could theoretically
697 return ioc
->nr_batch_requests
== q
->nr_batching
||
698 (ioc
->nr_batch_requests
> 0
699 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
703 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
704 * will cause the process to be a "batcher" on all queues in the system. This
705 * is the behaviour we want though - once it gets a wakeup it should be given
708 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
710 if (!ioc
|| ioc_batching(q
, ioc
))
713 ioc
->nr_batch_requests
= q
->nr_batching
;
714 ioc
->last_waited
= jiffies
;
717 static void __freed_request(struct request_queue
*q
, int sync
)
719 struct request_list
*rl
= &q
->rq
;
721 if (rl
->count
[sync
] < queue_congestion_off_threshold(q
))
722 blk_clear_queue_congested(q
, sync
);
724 if (rl
->count
[sync
] + 1 <= q
->nr_requests
) {
725 if (waitqueue_active(&rl
->wait
[sync
]))
726 wake_up(&rl
->wait
[sync
]);
728 blk_clear_queue_full(q
, sync
);
733 * A request has just been released. Account for it, update the full and
734 * congestion status, wake up any waiters. Called under q->queue_lock.
736 static void freed_request(struct request_queue
*q
, int sync
, int priv
)
738 struct request_list
*rl
= &q
->rq
;
744 __freed_request(q
, sync
);
746 if (unlikely(rl
->starved
[sync
^ 1]))
747 __freed_request(q
, sync
^ 1);
751 * Get a free request, queue_lock must be held.
752 * Returns NULL on failure, with queue_lock held.
753 * Returns !NULL on success, with queue_lock *not held*.
755 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
756 struct bio
*bio
, gfp_t gfp_mask
)
758 struct request
*rq
= NULL
;
759 struct request_list
*rl
= &q
->rq
;
760 struct io_context
*ioc
= NULL
;
761 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
764 may_queue
= elv_may_queue(q
, rw_flags
);
765 if (may_queue
== ELV_MQUEUE_NO
)
768 if (rl
->count
[is_sync
]+1 >= queue_congestion_on_threshold(q
)) {
769 if (rl
->count
[is_sync
]+1 >= q
->nr_requests
) {
770 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
772 * The queue will fill after this allocation, so set
773 * it as full, and mark this process as "batching".
774 * This process will be allowed to complete a batch of
775 * requests, others will be blocked.
777 if (!blk_queue_full(q
, is_sync
)) {
778 ioc_set_batching(q
, ioc
);
779 blk_set_queue_full(q
, is_sync
);
781 if (may_queue
!= ELV_MQUEUE_MUST
782 && !ioc_batching(q
, ioc
)) {
784 * The queue is full and the allocating
785 * process is not a "batcher", and not
786 * exempted by the IO scheduler
792 blk_set_queue_congested(q
, is_sync
);
796 * Only allow batching queuers to allocate up to 50% over the defined
797 * limit of requests, otherwise we could have thousands of requests
798 * allocated with any setting of ->nr_requests
800 if (rl
->count
[is_sync
] >= (3 * q
->nr_requests
/ 2))
803 rl
->count
[is_sync
]++;
804 rl
->starved
[is_sync
] = 0;
806 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
810 if (blk_queue_io_stat(q
))
811 rw_flags
|= REQ_IO_STAT
;
812 spin_unlock_irq(q
->queue_lock
);
814 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
817 * Allocation failed presumably due to memory. Undo anything
818 * we might have messed up.
820 * Allocating task should really be put onto the front of the
821 * wait queue, but this is pretty rare.
823 spin_lock_irq(q
->queue_lock
);
824 freed_request(q
, is_sync
, priv
);
827 * in the very unlikely event that allocation failed and no
828 * requests for this direction was pending, mark us starved
829 * so that freeing of a request in the other direction will
830 * notice us. another possible fix would be to split the
831 * rq mempool into READ and WRITE
834 if (unlikely(rl
->count
[is_sync
] == 0))
835 rl
->starved
[is_sync
] = 1;
841 * ioc may be NULL here, and ioc_batching will be false. That's
842 * OK, if the queue is under the request limit then requests need
843 * not count toward the nr_batch_requests limit. There will always
844 * be some limit enforced by BLK_BATCH_TIME.
846 if (ioc_batching(q
, ioc
))
847 ioc
->nr_batch_requests
--;
849 trace_block_getrq(q
, bio
, rw_flags
& 1);
855 * No available requests for this queue, unplug the device and wait for some
856 * requests to become available.
858 * Called with q->queue_lock held, and returns with it unlocked.
860 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
863 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
866 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
869 struct io_context
*ioc
;
870 struct request_list
*rl
= &q
->rq
;
872 prepare_to_wait_exclusive(&rl
->wait
[is_sync
], &wait
,
873 TASK_UNINTERRUPTIBLE
);
875 trace_block_sleeprq(q
, bio
, rw_flags
& 1);
877 __generic_unplug_device(q
);
878 spin_unlock_irq(q
->queue_lock
);
882 * After sleeping, we become a "batching" process and
883 * will be able to allocate at least one request, and
884 * up to a big batch of them for a small period time.
885 * See ioc_batching, ioc_set_batching
887 ioc
= current_io_context(GFP_NOIO
, q
->node
);
888 ioc_set_batching(q
, ioc
);
890 spin_lock_irq(q
->queue_lock
);
891 finish_wait(&rl
->wait
[is_sync
], &wait
);
893 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
899 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
903 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
905 spin_lock_irq(q
->queue_lock
);
906 if (gfp_mask
& __GFP_WAIT
) {
907 rq
= get_request_wait(q
, rw
, NULL
);
909 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
911 spin_unlock_irq(q
->queue_lock
);
913 /* q->queue_lock is unlocked at this point */
917 EXPORT_SYMBOL(blk_get_request
);
920 * blk_make_request - given a bio, allocate a corresponding struct request.
921 * @q: target request queue
922 * @bio: The bio describing the memory mappings that will be submitted for IO.
923 * It may be a chained-bio properly constructed by block/bio layer.
924 * @gfp_mask: gfp flags to be used for memory allocation
926 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
927 * type commands. Where the struct request needs to be farther initialized by
928 * the caller. It is passed a &struct bio, which describes the memory info of
931 * The caller of blk_make_request must make sure that bi_io_vec
932 * are set to describe the memory buffers. That bio_data_dir() will return
933 * the needed direction of the request. (And all bio's in the passed bio-chain
934 * are properly set accordingly)
936 * If called under none-sleepable conditions, mapped bio buffers must not
937 * need bouncing, by calling the appropriate masked or flagged allocator,
938 * suitable for the target device. Otherwise the call to blk_queue_bounce will
941 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
942 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
943 * anything but the first bio in the chain. Otherwise you risk waiting for IO
944 * completion of a bio that hasn't been submitted yet, thus resulting in a
945 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
946 * of bio_alloc(), as that avoids the mempool deadlock.
947 * If possible a big IO should be split into smaller parts when allocation
948 * fails. Partial allocation should not be an error, or you risk a live-lock.
950 struct request
*blk_make_request(struct request_queue
*q
, struct bio
*bio
,
953 struct request
*rq
= blk_get_request(q
, bio_data_dir(bio
), gfp_mask
);
956 return ERR_PTR(-ENOMEM
);
959 struct bio
*bounce_bio
= bio
;
962 blk_queue_bounce(q
, &bounce_bio
);
963 ret
= blk_rq_append_bio(q
, rq
, bounce_bio
);
972 EXPORT_SYMBOL(blk_make_request
);
975 * blk_requeue_request - put a request back on queue
976 * @q: request queue where request should be inserted
977 * @rq: request to be inserted
980 * Drivers often keep queueing requests until the hardware cannot accept
981 * more, when that condition happens we need to put the request back
982 * on the queue. Must be called with queue lock held.
984 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
986 blk_delete_timer(rq
);
987 blk_clear_rq_complete(rq
);
988 trace_block_rq_requeue(q
, rq
);
990 if (blk_rq_tagged(rq
))
991 blk_queue_end_tag(q
, rq
);
993 BUG_ON(blk_queued_rq(rq
));
995 elv_requeue_request(q
, rq
);
997 EXPORT_SYMBOL(blk_requeue_request
);
1000 * blk_insert_request - insert a special request into a request queue
1001 * @q: request queue where request should be inserted
1002 * @rq: request to be inserted
1003 * @at_head: insert request at head or tail of queue
1004 * @data: private data
1007 * Many block devices need to execute commands asynchronously, so they don't
1008 * block the whole kernel from preemption during request execution. This is
1009 * accomplished normally by inserting aritficial requests tagged as
1010 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1011 * be scheduled for actual execution by the request queue.
1013 * We have the option of inserting the head or the tail of the queue.
1014 * Typically we use the tail for new ioctls and so forth. We use the head
1015 * of the queue for things like a QUEUE_FULL message from a device, or a
1016 * host that is unable to accept a particular command.
1018 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
1019 int at_head
, void *data
)
1021 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
1022 unsigned long flags
;
1025 * tell I/O scheduler that this isn't a regular read/write (ie it
1026 * must not attempt merges on this) and that it acts as a soft
1029 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
1033 spin_lock_irqsave(q
->queue_lock
, flags
);
1036 * If command is tagged, release the tag
1038 if (blk_rq_tagged(rq
))
1039 blk_queue_end_tag(q
, rq
);
1041 drive_stat_acct(rq
, 1);
1042 __elv_add_request(q
, rq
, where
, 0);
1044 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1046 EXPORT_SYMBOL(blk_insert_request
);
1049 * add-request adds a request to the linked list.
1050 * queue lock is held and interrupts disabled, as we muck with the
1051 * request queue list.
1053 static inline void add_request(struct request_queue
*q
, struct request
*req
)
1055 drive_stat_acct(req
, 1);
1058 * elevator indicated where it wants this request to be
1059 * inserted at elevator_merge time
1061 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
1064 static void part_round_stats_single(int cpu
, struct hd_struct
*part
,
1067 if (now
== part
->stamp
)
1070 if (part_in_flight(part
)) {
1071 __part_stat_add(cpu
, part
, time_in_queue
,
1072 part_in_flight(part
) * (now
- part
->stamp
));
1073 __part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
1079 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1080 * @cpu: cpu number for stats access
1081 * @part: target partition
1083 * The average IO queue length and utilisation statistics are maintained
1084 * by observing the current state of the queue length and the amount of
1085 * time it has been in this state for.
1087 * Normally, that accounting is done on IO completion, but that can result
1088 * in more than a second's worth of IO being accounted for within any one
1089 * second, leading to >100% utilisation. To deal with that, we call this
1090 * function to do a round-off before returning the results when reading
1091 * /proc/diskstats. This accounts immediately for all queue usage up to
1092 * the current jiffies and restarts the counters again.
1094 void part_round_stats(int cpu
, struct hd_struct
*part
)
1096 unsigned long now
= jiffies
;
1099 part_round_stats_single(cpu
, &part_to_disk(part
)->part0
, now
);
1100 part_round_stats_single(cpu
, part
, now
);
1102 EXPORT_SYMBOL_GPL(part_round_stats
);
1105 * queue lock must be held
1107 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1111 if (unlikely(--req
->ref_count
))
1114 elv_completed_request(q
, req
);
1116 /* this is a bio leak */
1117 WARN_ON(req
->bio
!= NULL
);
1120 * Request may not have originated from ll_rw_blk. if not,
1121 * it didn't come out of our reserved rq pools
1123 if (req
->cmd_flags
& REQ_ALLOCED
) {
1124 int is_sync
= rq_is_sync(req
) != 0;
1125 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1127 BUG_ON(!list_empty(&req
->queuelist
));
1128 BUG_ON(!hlist_unhashed(&req
->hash
));
1130 blk_free_request(q
, req
);
1131 freed_request(q
, is_sync
, priv
);
1134 EXPORT_SYMBOL_GPL(__blk_put_request
);
1136 void blk_put_request(struct request
*req
)
1138 unsigned long flags
;
1139 struct request_queue
*q
= req
->q
;
1141 spin_lock_irqsave(q
->queue_lock
, flags
);
1142 __blk_put_request(q
, req
);
1143 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1145 EXPORT_SYMBOL(blk_put_request
);
1148 * blk_add_request_payload - add a payload to a request
1149 * @rq: request to update
1150 * @page: page backing the payload
1151 * @len: length of the payload.
1153 * This allows to later add a payload to an already submitted request by
1154 * a block driver. The driver needs to take care of freeing the payload
1157 * Note that this is a quite horrible hack and nothing but handling of
1158 * discard requests should ever use it.
1160 void blk_add_request_payload(struct request
*rq
, struct page
*page
,
1163 struct bio
*bio
= rq
->bio
;
1165 bio
->bi_io_vec
->bv_page
= page
;
1166 bio
->bi_io_vec
->bv_offset
= 0;
1167 bio
->bi_io_vec
->bv_len
= len
;
1171 bio
->bi_phys_segments
= 1;
1173 rq
->__data_len
= rq
->resid_len
= len
;
1174 rq
->nr_phys_segments
= 1;
1175 rq
->buffer
= bio_data(bio
);
1177 EXPORT_SYMBOL_GPL(blk_add_request_payload
);
1179 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1181 req
->cpu
= bio
->bi_comp_cpu
;
1182 req
->cmd_type
= REQ_TYPE_FS
;
1184 req
->cmd_flags
|= bio
->bi_rw
& REQ_COMMON_MASK
;
1185 if (bio
->bi_rw
& REQ_RAHEAD
)
1186 req
->cmd_flags
|= REQ_FAILFAST_MASK
;
1189 req
->__sector
= bio
->bi_sector
;
1190 req
->ioprio
= bio_prio(bio
);
1191 blk_rq_bio_prep(req
->q
, req
, bio
);
1195 * Only disabling plugging for non-rotational devices if it does tagging
1196 * as well, otherwise we do need the proper merging
1198 static inline bool queue_should_plug(struct request_queue
*q
)
1200 return !(blk_queue_nonrot(q
) && blk_queue_tagged(q
));
1203 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1205 struct request
*req
;
1207 unsigned int bytes
= bio
->bi_size
;
1208 const unsigned short prio
= bio_prio(bio
);
1209 const bool sync
= (bio
->bi_rw
& REQ_SYNC
);
1210 const bool unplug
= (bio
->bi_rw
& REQ_UNPLUG
);
1211 const unsigned int ff
= bio
->bi_rw
& REQ_FAILFAST_MASK
;
1214 if ((bio
->bi_rw
& REQ_HARDBARRIER
) &&
1215 (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
1216 bio_endio(bio
, -EOPNOTSUPP
);
1220 * low level driver can indicate that it wants pages above a
1221 * certain limit bounced to low memory (ie for highmem, or even
1222 * ISA dma in theory)
1224 blk_queue_bounce(q
, &bio
);
1226 spin_lock_irq(q
->queue_lock
);
1228 if (unlikely((bio
->bi_rw
& REQ_HARDBARRIER
)) || elv_queue_empty(q
))
1231 el_ret
= elv_merge(q
, &req
, bio
);
1233 case ELEVATOR_BACK_MERGE
:
1234 BUG_ON(!rq_mergeable(req
));
1236 if (!ll_back_merge_fn(q
, req
, bio
))
1239 trace_block_bio_backmerge(q
, bio
);
1241 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
)
1242 blk_rq_set_mixed_merge(req
);
1244 req
->biotail
->bi_next
= bio
;
1246 req
->__data_len
+= bytes
;
1247 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1248 if (!blk_rq_cpu_valid(req
))
1249 req
->cpu
= bio
->bi_comp_cpu
;
1250 drive_stat_acct(req
, 0);
1251 elv_bio_merged(q
, req
, bio
);
1252 if (!attempt_back_merge(q
, req
))
1253 elv_merged_request(q
, req
, el_ret
);
1256 case ELEVATOR_FRONT_MERGE
:
1257 BUG_ON(!rq_mergeable(req
));
1259 if (!ll_front_merge_fn(q
, req
, bio
))
1262 trace_block_bio_frontmerge(q
, bio
);
1264 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
) {
1265 blk_rq_set_mixed_merge(req
);
1266 req
->cmd_flags
&= ~REQ_FAILFAST_MASK
;
1267 req
->cmd_flags
|= ff
;
1270 bio
->bi_next
= req
->bio
;
1274 * may not be valid. if the low level driver said
1275 * it didn't need a bounce buffer then it better
1276 * not touch req->buffer either...
1278 req
->buffer
= bio_data(bio
);
1279 req
->__sector
= bio
->bi_sector
;
1280 req
->__data_len
+= bytes
;
1281 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1282 if (!blk_rq_cpu_valid(req
))
1283 req
->cpu
= bio
->bi_comp_cpu
;
1284 drive_stat_acct(req
, 0);
1285 elv_bio_merged(q
, req
, bio
);
1286 if (!attempt_front_merge(q
, req
))
1287 elv_merged_request(q
, req
, el_ret
);
1290 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1297 * This sync check and mask will be re-done in init_request_from_bio(),
1298 * but we need to set it earlier to expose the sync flag to the
1299 * rq allocator and io schedulers.
1301 rw_flags
= bio_data_dir(bio
);
1303 rw_flags
|= REQ_SYNC
;
1306 * Grab a free request. This is might sleep but can not fail.
1307 * Returns with the queue unlocked.
1309 req
= get_request_wait(q
, rw_flags
, bio
);
1312 * After dropping the lock and possibly sleeping here, our request
1313 * may now be mergeable after it had proven unmergeable (above).
1314 * We don't worry about that case for efficiency. It won't happen
1315 * often, and the elevators are able to handle it.
1317 init_request_from_bio(req
, bio
);
1319 spin_lock_irq(q
->queue_lock
);
1320 if (test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
) ||
1321 bio_flagged(bio
, BIO_CPU_AFFINE
))
1322 req
->cpu
= blk_cpu_to_group(smp_processor_id());
1323 if (queue_should_plug(q
) && elv_queue_empty(q
))
1325 add_request(q
, req
);
1327 if (unplug
|| !queue_should_plug(q
))
1328 __generic_unplug_device(q
);
1329 spin_unlock_irq(q
->queue_lock
);
1334 * If bio->bi_dev is a partition, remap the location
1336 static inline void blk_partition_remap(struct bio
*bio
)
1338 struct block_device
*bdev
= bio
->bi_bdev
;
1340 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1341 struct hd_struct
*p
= bdev
->bd_part
;
1343 bio
->bi_sector
+= p
->start_sect
;
1344 bio
->bi_bdev
= bdev
->bd_contains
;
1346 trace_block_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1348 bio
->bi_sector
- p
->start_sect
);
1352 static void handle_bad_sector(struct bio
*bio
)
1354 char b
[BDEVNAME_SIZE
];
1356 printk(KERN_INFO
"attempt to access beyond end of device\n");
1357 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1358 bdevname(bio
->bi_bdev
, b
),
1360 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1361 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
1363 set_bit(BIO_EOF
, &bio
->bi_flags
);
1366 #ifdef CONFIG_FAIL_MAKE_REQUEST
1368 static DECLARE_FAULT_ATTR(fail_make_request
);
1370 static int __init
setup_fail_make_request(char *str
)
1372 return setup_fault_attr(&fail_make_request
, str
);
1374 __setup("fail_make_request=", setup_fail_make_request
);
1376 static int should_fail_request(struct bio
*bio
)
1378 struct hd_struct
*part
= bio
->bi_bdev
->bd_part
;
1380 if (part_to_disk(part
)->part0
.make_it_fail
|| part
->make_it_fail
)
1381 return should_fail(&fail_make_request
, bio
->bi_size
);
1386 static int __init
fail_make_request_debugfs(void)
1388 return init_fault_attr_dentries(&fail_make_request
,
1389 "fail_make_request");
1392 late_initcall(fail_make_request_debugfs
);
1394 #else /* CONFIG_FAIL_MAKE_REQUEST */
1396 static inline int should_fail_request(struct bio
*bio
)
1401 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1404 * Check whether this bio extends beyond the end of the device.
1406 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1413 /* Test device or partition size, when known. */
1414 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
1416 sector_t sector
= bio
->bi_sector
;
1418 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1420 * This may well happen - the kernel calls bread()
1421 * without checking the size of the device, e.g., when
1422 * mounting a device.
1424 handle_bad_sector(bio
);
1433 * generic_make_request - hand a buffer to its device driver for I/O
1434 * @bio: The bio describing the location in memory and on the device.
1436 * generic_make_request() is used to make I/O requests of block
1437 * devices. It is passed a &struct bio, which describes the I/O that needs
1440 * generic_make_request() does not return any status. The
1441 * success/failure status of the request, along with notification of
1442 * completion, is delivered asynchronously through the bio->bi_end_io
1443 * function described (one day) else where.
1445 * The caller of generic_make_request must make sure that bi_io_vec
1446 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1447 * set to describe the device address, and the
1448 * bi_end_io and optionally bi_private are set to describe how
1449 * completion notification should be signaled.
1451 * generic_make_request and the drivers it calls may use bi_next if this
1452 * bio happens to be merged with someone else, and may change bi_dev and
1453 * bi_sector for remaps as it sees fit. So the values of these fields
1454 * should NOT be depended on after the call to generic_make_request.
1456 static inline void __generic_make_request(struct bio
*bio
)
1458 struct request_queue
*q
;
1459 sector_t old_sector
;
1460 int ret
, nr_sectors
= bio_sectors(bio
);
1466 if (bio_check_eod(bio
, nr_sectors
))
1470 * Resolve the mapping until finished. (drivers are
1471 * still free to implement/resolve their own stacking
1472 * by explicitly returning 0)
1474 * NOTE: we don't repeat the blk_size check for each new device.
1475 * Stacking drivers are expected to know what they are doing.
1480 char b
[BDEVNAME_SIZE
];
1482 q
= bdev_get_queue(bio
->bi_bdev
);
1485 "generic_make_request: Trying to access "
1486 "nonexistent block-device %s (%Lu)\n",
1487 bdevname(bio
->bi_bdev
, b
),
1488 (long long) bio
->bi_sector
);
1492 if (unlikely(!(bio
->bi_rw
& REQ_DISCARD
) &&
1493 nr_sectors
> queue_max_hw_sectors(q
))) {
1494 printk(KERN_ERR
"bio too big device %s (%u > %u)\n",
1495 bdevname(bio
->bi_bdev
, b
),
1497 queue_max_hw_sectors(q
));
1501 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1504 if (should_fail_request(bio
))
1508 * If this device has partitions, remap block n
1509 * of partition p to block n+start(p) of the disk.
1511 blk_partition_remap(bio
);
1513 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
))
1516 if (old_sector
!= -1)
1517 trace_block_remap(q
, bio
, old_dev
, old_sector
);
1519 old_sector
= bio
->bi_sector
;
1520 old_dev
= bio
->bi_bdev
->bd_dev
;
1522 if (bio_check_eod(bio
, nr_sectors
))
1525 if ((bio
->bi_rw
& REQ_DISCARD
) &&
1526 (!blk_queue_discard(q
) ||
1527 ((bio
->bi_rw
& REQ_SECURE
) &&
1528 !blk_queue_secdiscard(q
)))) {
1533 blk_throtl_bio(q
, &bio
);
1536 * If bio = NULL, bio has been throttled and will be submitted
1542 trace_block_bio_queue(q
, bio
);
1544 ret
= q
->make_request_fn(q
, bio
);
1550 bio_endio(bio
, err
);
1554 * We only want one ->make_request_fn to be active at a time,
1555 * else stack usage with stacked devices could be a problem.
1556 * So use current->bio_list to keep a list of requests
1557 * submited by a make_request_fn function.
1558 * current->bio_list is also used as a flag to say if
1559 * generic_make_request is currently active in this task or not.
1560 * If it is NULL, then no make_request is active. If it is non-NULL,
1561 * then a make_request is active, and new requests should be added
1564 void generic_make_request(struct bio
*bio
)
1566 struct bio_list bio_list_on_stack
;
1568 if (current
->bio_list
) {
1569 /* make_request is active */
1570 bio_list_add(current
->bio_list
, bio
);
1573 /* following loop may be a bit non-obvious, and so deserves some
1575 * Before entering the loop, bio->bi_next is NULL (as all callers
1576 * ensure that) so we have a list with a single bio.
1577 * We pretend that we have just taken it off a longer list, so
1578 * we assign bio_list to a pointer to the bio_list_on_stack,
1579 * thus initialising the bio_list of new bios to be
1580 * added. __generic_make_request may indeed add some more bios
1581 * through a recursive call to generic_make_request. If it
1582 * did, we find a non-NULL value in bio_list and re-enter the loop
1583 * from the top. In this case we really did just take the bio
1584 * of the top of the list (no pretending) and so remove it from
1585 * bio_list, and call into __generic_make_request again.
1587 * The loop was structured like this to make only one call to
1588 * __generic_make_request (which is important as it is large and
1589 * inlined) and to keep the structure simple.
1591 BUG_ON(bio
->bi_next
);
1592 bio_list_init(&bio_list_on_stack
);
1593 current
->bio_list
= &bio_list_on_stack
;
1595 __generic_make_request(bio
);
1596 bio
= bio_list_pop(current
->bio_list
);
1598 current
->bio_list
= NULL
; /* deactivate */
1600 EXPORT_SYMBOL(generic_make_request
);
1603 * submit_bio - submit a bio to the block device layer for I/O
1604 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1605 * @bio: The &struct bio which describes the I/O
1607 * submit_bio() is very similar in purpose to generic_make_request(), and
1608 * uses that function to do most of the work. Both are fairly rough
1609 * interfaces; @bio must be presetup and ready for I/O.
1612 void submit_bio(int rw
, struct bio
*bio
)
1614 int count
= bio_sectors(bio
);
1619 * If it's a regular read/write or a barrier with data attached,
1620 * go through the normal accounting stuff before submission.
1622 if (bio_has_data(bio
) && !(rw
& REQ_DISCARD
)) {
1624 count_vm_events(PGPGOUT
, count
);
1626 task_io_account_read(bio
->bi_size
);
1627 count_vm_events(PGPGIN
, count
);
1630 if (unlikely(block_dump
)) {
1631 char b
[BDEVNAME_SIZE
];
1632 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s (%u sectors)\n",
1633 current
->comm
, task_pid_nr(current
),
1634 (rw
& WRITE
) ? "WRITE" : "READ",
1635 (unsigned long long)bio
->bi_sector
,
1636 bdevname(bio
->bi_bdev
, b
),
1641 generic_make_request(bio
);
1643 EXPORT_SYMBOL(submit_bio
);
1646 * blk_rq_check_limits - Helper function to check a request for the queue limit
1648 * @rq: the request being checked
1651 * @rq may have been made based on weaker limitations of upper-level queues
1652 * in request stacking drivers, and it may violate the limitation of @q.
1653 * Since the block layer and the underlying device driver trust @rq
1654 * after it is inserted to @q, it should be checked against @q before
1655 * the insertion using this generic function.
1657 * This function should also be useful for request stacking drivers
1658 * in some cases below, so export this fuction.
1659 * Request stacking drivers like request-based dm may change the queue
1660 * limits while requests are in the queue (e.g. dm's table swapping).
1661 * Such request stacking drivers should check those requests agaist
1662 * the new queue limits again when they dispatch those requests,
1663 * although such checkings are also done against the old queue limits
1664 * when submitting requests.
1666 int blk_rq_check_limits(struct request_queue
*q
, struct request
*rq
)
1668 if (rq
->cmd_flags
& REQ_DISCARD
)
1671 if (blk_rq_sectors(rq
) > queue_max_sectors(q
) ||
1672 blk_rq_bytes(rq
) > queue_max_hw_sectors(q
) << 9) {
1673 printk(KERN_ERR
"%s: over max size limit.\n", __func__
);
1678 * queue's settings related to segment counting like q->bounce_pfn
1679 * may differ from that of other stacking queues.
1680 * Recalculate it to check the request correctly on this queue's
1683 blk_recalc_rq_segments(rq
);
1684 if (rq
->nr_phys_segments
> queue_max_segments(q
)) {
1685 printk(KERN_ERR
"%s: over max segments limit.\n", __func__
);
1691 EXPORT_SYMBOL_GPL(blk_rq_check_limits
);
1694 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1695 * @q: the queue to submit the request
1696 * @rq: the request being queued
1698 int blk_insert_cloned_request(struct request_queue
*q
, struct request
*rq
)
1700 unsigned long flags
;
1702 if (blk_rq_check_limits(q
, rq
))
1705 #ifdef CONFIG_FAIL_MAKE_REQUEST
1706 if (rq
->rq_disk
&& rq
->rq_disk
->part0
.make_it_fail
&&
1707 should_fail(&fail_make_request
, blk_rq_bytes(rq
)))
1711 spin_lock_irqsave(q
->queue_lock
, flags
);
1714 * Submitting request must be dequeued before calling this function
1715 * because it will be linked to another request_queue
1717 BUG_ON(blk_queued_rq(rq
));
1719 drive_stat_acct(rq
, 1);
1720 __elv_add_request(q
, rq
, ELEVATOR_INSERT_BACK
, 0);
1722 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1726 EXPORT_SYMBOL_GPL(blk_insert_cloned_request
);
1729 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1730 * @rq: request to examine
1733 * A request could be merge of IOs which require different failure
1734 * handling. This function determines the number of bytes which
1735 * can be failed from the beginning of the request without
1736 * crossing into area which need to be retried further.
1739 * The number of bytes to fail.
1742 * queue_lock must be held.
1744 unsigned int blk_rq_err_bytes(const struct request
*rq
)
1746 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
1747 unsigned int bytes
= 0;
1750 if (!(rq
->cmd_flags
& REQ_MIXED_MERGE
))
1751 return blk_rq_bytes(rq
);
1754 * Currently the only 'mixing' which can happen is between
1755 * different fastfail types. We can safely fail portions
1756 * which have all the failfast bits that the first one has -
1757 * the ones which are at least as eager to fail as the first
1760 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
1761 if ((bio
->bi_rw
& ff
) != ff
)
1763 bytes
+= bio
->bi_size
;
1766 /* this could lead to infinite loop */
1767 BUG_ON(blk_rq_bytes(rq
) && !bytes
);
1770 EXPORT_SYMBOL_GPL(blk_rq_err_bytes
);
1772 static void blk_account_io_completion(struct request
*req
, unsigned int bytes
)
1774 if (blk_do_io_stat(req
)) {
1775 const int rw
= rq_data_dir(req
);
1776 struct hd_struct
*part
;
1779 cpu
= part_stat_lock();
1780 part
= disk_map_sector_rcu(req
->rq_disk
, blk_rq_pos(req
));
1781 part_stat_add(cpu
, part
, sectors
[rw
], bytes
>> 9);
1786 static void blk_account_io_done(struct request
*req
)
1789 * Account IO completion. bar_rq isn't accounted as a normal
1790 * IO on queueing nor completion. Accounting the containing
1791 * request is enough.
1793 if (blk_do_io_stat(req
) && req
!= &req
->q
->bar_rq
) {
1794 unsigned long duration
= jiffies
- req
->start_time
;
1795 const int rw
= rq_data_dir(req
);
1796 struct hd_struct
*part
;
1799 cpu
= part_stat_lock();
1800 part
= disk_map_sector_rcu(req
->rq_disk
, blk_rq_pos(req
));
1802 part_stat_inc(cpu
, part
, ios
[rw
]);
1803 part_stat_add(cpu
, part
, ticks
[rw
], duration
);
1804 part_round_stats(cpu
, part
);
1805 part_dec_in_flight(part
, rw
);
1812 * blk_peek_request - peek at the top of a request queue
1813 * @q: request queue to peek at
1816 * Return the request at the top of @q. The returned request
1817 * should be started using blk_start_request() before LLD starts
1821 * Pointer to the request at the top of @q if available. Null
1825 * queue_lock must be held.
1827 struct request
*blk_peek_request(struct request_queue
*q
)
1832 while ((rq
= __elv_next_request(q
)) != NULL
) {
1833 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
1835 * This is the first time the device driver
1836 * sees this request (possibly after
1837 * requeueing). Notify IO scheduler.
1839 if (rq
->cmd_flags
& REQ_SORTED
)
1840 elv_activate_rq(q
, rq
);
1843 * just mark as started even if we don't start
1844 * it, a request that has been delayed should
1845 * not be passed by new incoming requests
1847 rq
->cmd_flags
|= REQ_STARTED
;
1848 trace_block_rq_issue(q
, rq
);
1851 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
1852 q
->end_sector
= rq_end_sector(rq
);
1853 q
->boundary_rq
= NULL
;
1856 if (rq
->cmd_flags
& REQ_DONTPREP
)
1859 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
1861 * make sure space for the drain appears we
1862 * know we can do this because max_hw_segments
1863 * has been adjusted to be one fewer than the
1866 rq
->nr_phys_segments
++;
1872 ret
= q
->prep_rq_fn(q
, rq
);
1873 if (ret
== BLKPREP_OK
) {
1875 } else if (ret
== BLKPREP_DEFER
) {
1877 * the request may have been (partially) prepped.
1878 * we need to keep this request in the front to
1879 * avoid resource deadlock. REQ_STARTED will
1880 * prevent other fs requests from passing this one.
1882 if (q
->dma_drain_size
&& blk_rq_bytes(rq
) &&
1883 !(rq
->cmd_flags
& REQ_DONTPREP
)) {
1885 * remove the space for the drain we added
1886 * so that we don't add it again
1888 --rq
->nr_phys_segments
;
1893 } else if (ret
== BLKPREP_KILL
) {
1894 rq
->cmd_flags
|= REQ_QUIET
;
1896 * Mark this request as started so we don't trigger
1897 * any debug logic in the end I/O path.
1899 blk_start_request(rq
);
1900 __blk_end_request_all(rq
, -EIO
);
1902 printk(KERN_ERR
"%s: bad return=%d\n", __func__
, ret
);
1909 EXPORT_SYMBOL(blk_peek_request
);
1911 void blk_dequeue_request(struct request
*rq
)
1913 struct request_queue
*q
= rq
->q
;
1915 BUG_ON(list_empty(&rq
->queuelist
));
1916 BUG_ON(ELV_ON_HASH(rq
));
1918 list_del_init(&rq
->queuelist
);
1921 * the time frame between a request being removed from the lists
1922 * and to it is freed is accounted as io that is in progress at
1925 if (blk_account_rq(rq
)) {
1926 q
->in_flight
[rq_is_sync(rq
)]++;
1927 set_io_start_time_ns(rq
);
1932 * blk_start_request - start request processing on the driver
1933 * @req: request to dequeue
1936 * Dequeue @req and start timeout timer on it. This hands off the
1937 * request to the driver.
1939 * Block internal functions which don't want to start timer should
1940 * call blk_dequeue_request().
1943 * queue_lock must be held.
1945 void blk_start_request(struct request
*req
)
1947 blk_dequeue_request(req
);
1950 * We are now handing the request to the hardware, initialize
1951 * resid_len to full count and add the timeout handler.
1953 req
->resid_len
= blk_rq_bytes(req
);
1954 if (unlikely(blk_bidi_rq(req
)))
1955 req
->next_rq
->resid_len
= blk_rq_bytes(req
->next_rq
);
1959 EXPORT_SYMBOL(blk_start_request
);
1962 * blk_fetch_request - fetch a request from a request queue
1963 * @q: request queue to fetch a request from
1966 * Return the request at the top of @q. The request is started on
1967 * return and LLD can start processing it immediately.
1970 * Pointer to the request at the top of @q if available. Null
1974 * queue_lock must be held.
1976 struct request
*blk_fetch_request(struct request_queue
*q
)
1980 rq
= blk_peek_request(q
);
1982 blk_start_request(rq
);
1985 EXPORT_SYMBOL(blk_fetch_request
);
1988 * blk_update_request - Special helper function for request stacking drivers
1989 * @req: the request being processed
1990 * @error: %0 for success, < %0 for error
1991 * @nr_bytes: number of bytes to complete @req
1994 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1995 * the request structure even if @req doesn't have leftover.
1996 * If @req has leftover, sets it up for the next range of segments.
1998 * This special helper function is only for request stacking drivers
1999 * (e.g. request-based dm) so that they can handle partial completion.
2000 * Actual device drivers should use blk_end_request instead.
2002 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2003 * %false return from this function.
2006 * %false - this request doesn't have any more data
2007 * %true - this request has more data
2009 bool blk_update_request(struct request
*req
, int error
, unsigned int nr_bytes
)
2011 int total_bytes
, bio_nbytes
, next_idx
= 0;
2017 trace_block_rq_complete(req
->q
, req
);
2020 * For fs requests, rq is just carrier of independent bio's
2021 * and each partial completion should be handled separately.
2022 * Reset per-request error on each partial completion.
2024 * TODO: tj: This is too subtle. It would be better to let
2025 * low level drivers do what they see fit.
2027 if (req
->cmd_type
== REQ_TYPE_FS
)
2030 if (error
&& req
->cmd_type
== REQ_TYPE_FS
&&
2031 !(req
->cmd_flags
& REQ_QUIET
)) {
2032 printk(KERN_ERR
"end_request: I/O error, dev %s, sector %llu\n",
2033 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
2034 (unsigned long long)blk_rq_pos(req
));
2037 blk_account_io_completion(req
, nr_bytes
);
2039 total_bytes
= bio_nbytes
= 0;
2040 while ((bio
= req
->bio
) != NULL
) {
2043 if (nr_bytes
>= bio
->bi_size
) {
2044 req
->bio
= bio
->bi_next
;
2045 nbytes
= bio
->bi_size
;
2046 req_bio_endio(req
, bio
, nbytes
, error
);
2050 int idx
= bio
->bi_idx
+ next_idx
;
2052 if (unlikely(idx
>= bio
->bi_vcnt
)) {
2053 blk_dump_rq_flags(req
, "__end_that");
2054 printk(KERN_ERR
"%s: bio idx %d >= vcnt %d\n",
2055 __func__
, idx
, bio
->bi_vcnt
);
2059 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
2060 BIO_BUG_ON(nbytes
> bio
->bi_size
);
2063 * not a complete bvec done
2065 if (unlikely(nbytes
> nr_bytes
)) {
2066 bio_nbytes
+= nr_bytes
;
2067 total_bytes
+= nr_bytes
;
2072 * advance to the next vector
2075 bio_nbytes
+= nbytes
;
2078 total_bytes
+= nbytes
;
2084 * end more in this run, or just return 'not-done'
2086 if (unlikely(nr_bytes
<= 0))
2096 * Reset counters so that the request stacking driver
2097 * can find how many bytes remain in the request
2100 req
->__data_len
= 0;
2105 * if the request wasn't completed, update state
2108 req_bio_endio(req
, bio
, bio_nbytes
, error
);
2109 bio
->bi_idx
+= next_idx
;
2110 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
2111 bio_iovec(bio
)->bv_len
-= nr_bytes
;
2114 req
->__data_len
-= total_bytes
;
2115 req
->buffer
= bio_data(req
->bio
);
2117 /* update sector only for requests with clear definition of sector */
2118 if (req
->cmd_type
== REQ_TYPE_FS
|| (req
->cmd_flags
& REQ_DISCARD
))
2119 req
->__sector
+= total_bytes
>> 9;
2121 /* mixed attributes always follow the first bio */
2122 if (req
->cmd_flags
& REQ_MIXED_MERGE
) {
2123 req
->cmd_flags
&= ~REQ_FAILFAST_MASK
;
2124 req
->cmd_flags
|= req
->bio
->bi_rw
& REQ_FAILFAST_MASK
;
2128 * If total number of sectors is less than the first segment
2129 * size, something has gone terribly wrong.
2131 if (blk_rq_bytes(req
) < blk_rq_cur_bytes(req
)) {
2132 printk(KERN_ERR
"blk: request botched\n");
2133 req
->__data_len
= blk_rq_cur_bytes(req
);
2136 /* recalculate the number of segments */
2137 blk_recalc_rq_segments(req
);
2141 EXPORT_SYMBOL_GPL(blk_update_request
);
2143 static bool blk_update_bidi_request(struct request
*rq
, int error
,
2144 unsigned int nr_bytes
,
2145 unsigned int bidi_bytes
)
2147 if (blk_update_request(rq
, error
, nr_bytes
))
2150 /* Bidi request must be completed as a whole */
2151 if (unlikely(blk_bidi_rq(rq
)) &&
2152 blk_update_request(rq
->next_rq
, error
, bidi_bytes
))
2155 if (blk_queue_add_random(rq
->q
))
2156 add_disk_randomness(rq
->rq_disk
);
2162 * blk_unprep_request - unprepare a request
2165 * This function makes a request ready for complete resubmission (or
2166 * completion). It happens only after all error handling is complete,
2167 * so represents the appropriate moment to deallocate any resources
2168 * that were allocated to the request in the prep_rq_fn. The queue
2169 * lock is held when calling this.
2171 void blk_unprep_request(struct request
*req
)
2173 struct request_queue
*q
= req
->q
;
2175 req
->cmd_flags
&= ~REQ_DONTPREP
;
2176 if (q
->unprep_rq_fn
)
2177 q
->unprep_rq_fn(q
, req
);
2179 EXPORT_SYMBOL_GPL(blk_unprep_request
);
2182 * queue lock must be held
2184 static void blk_finish_request(struct request
*req
, int error
)
2186 if (blk_rq_tagged(req
))
2187 blk_queue_end_tag(req
->q
, req
);
2189 BUG_ON(blk_queued_rq(req
));
2191 if (unlikely(laptop_mode
) && req
->cmd_type
== REQ_TYPE_FS
)
2192 laptop_io_completion(&req
->q
->backing_dev_info
);
2194 blk_delete_timer(req
);
2196 if (req
->cmd_flags
& REQ_DONTPREP
)
2197 blk_unprep_request(req
);
2200 blk_account_io_done(req
);
2203 req
->end_io(req
, error
);
2205 if (blk_bidi_rq(req
))
2206 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
2208 __blk_put_request(req
->q
, req
);
2213 * blk_end_bidi_request - Complete a bidi request
2214 * @rq: the request to complete
2215 * @error: %0 for success, < %0 for error
2216 * @nr_bytes: number of bytes to complete @rq
2217 * @bidi_bytes: number of bytes to complete @rq->next_rq
2220 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2221 * Drivers that supports bidi can safely call this member for any
2222 * type of request, bidi or uni. In the later case @bidi_bytes is
2226 * %false - we are done with this request
2227 * %true - still buffers pending for this request
2229 static bool blk_end_bidi_request(struct request
*rq
, int error
,
2230 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2232 struct request_queue
*q
= rq
->q
;
2233 unsigned long flags
;
2235 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2238 spin_lock_irqsave(q
->queue_lock
, flags
);
2239 blk_finish_request(rq
, error
);
2240 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2246 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2247 * @rq: the request to complete
2248 * @error: %0 for success, < %0 for error
2249 * @nr_bytes: number of bytes to complete @rq
2250 * @bidi_bytes: number of bytes to complete @rq->next_rq
2253 * Identical to blk_end_bidi_request() except that queue lock is
2254 * assumed to be locked on entry and remains so on return.
2257 * %false - we are done with this request
2258 * %true - still buffers pending for this request
2260 static bool __blk_end_bidi_request(struct request
*rq
, int error
,
2261 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2263 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2266 blk_finish_request(rq
, error
);
2272 * blk_end_request - Helper function for drivers to complete the request.
2273 * @rq: the request being processed
2274 * @error: %0 for success, < %0 for error
2275 * @nr_bytes: number of bytes to complete
2278 * Ends I/O on a number of bytes attached to @rq.
2279 * If @rq has leftover, sets it up for the next range of segments.
2282 * %false - we are done with this request
2283 * %true - still buffers pending for this request
2285 bool blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2287 return blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2289 EXPORT_SYMBOL(blk_end_request
);
2292 * blk_end_request_all - Helper function for drives to finish the request.
2293 * @rq: the request to finish
2294 * @error: %0 for success, < %0 for error
2297 * Completely finish @rq.
2299 void blk_end_request_all(struct request
*rq
, int error
)
2302 unsigned int bidi_bytes
= 0;
2304 if (unlikely(blk_bidi_rq(rq
)))
2305 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2307 pending
= blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2310 EXPORT_SYMBOL(blk_end_request_all
);
2313 * blk_end_request_cur - Helper function to finish the current request chunk.
2314 * @rq: the request to finish the current chunk for
2315 * @error: %0 for success, < %0 for error
2318 * Complete the current consecutively mapped chunk from @rq.
2321 * %false - we are done with this request
2322 * %true - still buffers pending for this request
2324 bool blk_end_request_cur(struct request
*rq
, int error
)
2326 return blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2328 EXPORT_SYMBOL(blk_end_request_cur
);
2331 * blk_end_request_err - Finish a request till the next failure boundary.
2332 * @rq: the request to finish till the next failure boundary for
2333 * @error: must be negative errno
2336 * Complete @rq till the next failure boundary.
2339 * %false - we are done with this request
2340 * %true - still buffers pending for this request
2342 bool blk_end_request_err(struct request
*rq
, int error
)
2344 WARN_ON(error
>= 0);
2345 return blk_end_request(rq
, error
, blk_rq_err_bytes(rq
));
2347 EXPORT_SYMBOL_GPL(blk_end_request_err
);
2350 * __blk_end_request - Helper function for drivers to complete the request.
2351 * @rq: the request being processed
2352 * @error: %0 for success, < %0 for error
2353 * @nr_bytes: number of bytes to complete
2356 * Must be called with queue lock held unlike blk_end_request().
2359 * %false - we are done with this request
2360 * %true - still buffers pending for this request
2362 bool __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2364 return __blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2366 EXPORT_SYMBOL(__blk_end_request
);
2369 * __blk_end_request_all - Helper function for drives to finish the request.
2370 * @rq: the request to finish
2371 * @error: %0 for success, < %0 for error
2374 * Completely finish @rq. Must be called with queue lock held.
2376 void __blk_end_request_all(struct request
*rq
, int error
)
2379 unsigned int bidi_bytes
= 0;
2381 if (unlikely(blk_bidi_rq(rq
)))
2382 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2384 pending
= __blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2387 EXPORT_SYMBOL(__blk_end_request_all
);
2390 * __blk_end_request_cur - Helper function to finish the current request chunk.
2391 * @rq: the request to finish the current chunk for
2392 * @error: %0 for success, < %0 for error
2395 * Complete the current consecutively mapped chunk from @rq. Must
2396 * be called with queue lock held.
2399 * %false - we are done with this request
2400 * %true - still buffers pending for this request
2402 bool __blk_end_request_cur(struct request
*rq
, int error
)
2404 return __blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2406 EXPORT_SYMBOL(__blk_end_request_cur
);
2409 * __blk_end_request_err - Finish a request till the next failure boundary.
2410 * @rq: the request to finish till the next failure boundary for
2411 * @error: must be negative errno
2414 * Complete @rq till the next failure boundary. Must be called
2415 * with queue lock held.
2418 * %false - we are done with this request
2419 * %true - still buffers pending for this request
2421 bool __blk_end_request_err(struct request
*rq
, int error
)
2423 WARN_ON(error
>= 0);
2424 return __blk_end_request(rq
, error
, blk_rq_err_bytes(rq
));
2426 EXPORT_SYMBOL_GPL(__blk_end_request_err
);
2428 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2431 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
2432 rq
->cmd_flags
|= bio
->bi_rw
& REQ_WRITE
;
2434 if (bio_has_data(bio
)) {
2435 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2436 rq
->buffer
= bio_data(bio
);
2438 rq
->__data_len
= bio
->bi_size
;
2439 rq
->bio
= rq
->biotail
= bio
;
2442 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2445 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2447 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2448 * @rq: the request to be flushed
2451 * Flush all pages in @rq.
2453 void rq_flush_dcache_pages(struct request
*rq
)
2455 struct req_iterator iter
;
2456 struct bio_vec
*bvec
;
2458 rq_for_each_segment(bvec
, rq
, iter
)
2459 flush_dcache_page(bvec
->bv_page
);
2461 EXPORT_SYMBOL_GPL(rq_flush_dcache_pages
);
2465 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2466 * @q : the queue of the device being checked
2469 * Check if underlying low-level drivers of a device are busy.
2470 * If the drivers want to export their busy state, they must set own
2471 * exporting function using blk_queue_lld_busy() first.
2473 * Basically, this function is used only by request stacking drivers
2474 * to stop dispatching requests to underlying devices when underlying
2475 * devices are busy. This behavior helps more I/O merging on the queue
2476 * of the request stacking driver and prevents I/O throughput regression
2477 * on burst I/O load.
2480 * 0 - Not busy (The request stacking driver should dispatch request)
2481 * 1 - Busy (The request stacking driver should stop dispatching request)
2483 int blk_lld_busy(struct request_queue
*q
)
2486 return q
->lld_busy_fn(q
);
2490 EXPORT_SYMBOL_GPL(blk_lld_busy
);
2493 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2494 * @rq: the clone request to be cleaned up
2497 * Free all bios in @rq for a cloned request.
2499 void blk_rq_unprep_clone(struct request
*rq
)
2503 while ((bio
= rq
->bio
) != NULL
) {
2504 rq
->bio
= bio
->bi_next
;
2509 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone
);
2512 * Copy attributes of the original request to the clone request.
2513 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2515 static void __blk_rq_prep_clone(struct request
*dst
, struct request
*src
)
2517 dst
->cpu
= src
->cpu
;
2518 dst
->cmd_flags
= (rq_data_dir(src
) | REQ_NOMERGE
);
2519 if (src
->cmd_flags
& REQ_DISCARD
)
2520 dst
->cmd_flags
|= REQ_DISCARD
;
2521 dst
->cmd_type
= src
->cmd_type
;
2522 dst
->__sector
= blk_rq_pos(src
);
2523 dst
->__data_len
= blk_rq_bytes(src
);
2524 dst
->nr_phys_segments
= src
->nr_phys_segments
;
2525 dst
->ioprio
= src
->ioprio
;
2526 dst
->extra_len
= src
->extra_len
;
2530 * blk_rq_prep_clone - Helper function to setup clone request
2531 * @rq: the request to be setup
2532 * @rq_src: original request to be cloned
2533 * @bs: bio_set that bios for clone are allocated from
2534 * @gfp_mask: memory allocation mask for bio
2535 * @bio_ctr: setup function to be called for each clone bio.
2536 * Returns %0 for success, non %0 for failure.
2537 * @data: private data to be passed to @bio_ctr
2540 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2541 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2542 * are not copied, and copying such parts is the caller's responsibility.
2543 * Also, pages which the original bios are pointing to are not copied
2544 * and the cloned bios just point same pages.
2545 * So cloned bios must be completed before original bios, which means
2546 * the caller must complete @rq before @rq_src.
2548 int blk_rq_prep_clone(struct request
*rq
, struct request
*rq_src
,
2549 struct bio_set
*bs
, gfp_t gfp_mask
,
2550 int (*bio_ctr
)(struct bio
*, struct bio
*, void *),
2553 struct bio
*bio
, *bio_src
;
2558 blk_rq_init(NULL
, rq
);
2560 __rq_for_each_bio(bio_src
, rq_src
) {
2561 bio
= bio_alloc_bioset(gfp_mask
, bio_src
->bi_max_vecs
, bs
);
2565 __bio_clone(bio
, bio_src
);
2567 if (bio_integrity(bio_src
) &&
2568 bio_integrity_clone(bio
, bio_src
, gfp_mask
, bs
))
2571 if (bio_ctr
&& bio_ctr(bio
, bio_src
, data
))
2575 rq
->biotail
->bi_next
= bio
;
2578 rq
->bio
= rq
->biotail
= bio
;
2581 __blk_rq_prep_clone(rq
, rq_src
);
2588 blk_rq_unprep_clone(rq
);
2592 EXPORT_SYMBOL_GPL(blk_rq_prep_clone
);
2594 int kblockd_schedule_work(struct request_queue
*q
, struct work_struct
*work
)
2596 return queue_work(kblockd_workqueue
, work
);
2598 EXPORT_SYMBOL(kblockd_schedule_work
);
2600 int kblockd_schedule_delayed_work(struct request_queue
*q
,
2601 struct delayed_work
*dwork
, unsigned long delay
)
2603 return queue_delayed_work(kblockd_workqueue
, dwork
, delay
);
2605 EXPORT_SYMBOL(kblockd_schedule_delayed_work
);
2607 int __init
blk_dev_init(void)
2609 BUILD_BUG_ON(__REQ_NR_BITS
> 8 *
2610 sizeof(((struct request
*)0)->cmd_flags
));
2612 kblockd_workqueue
= create_workqueue("kblockd");
2613 if (!kblockd_workqueue
)
2614 panic("Failed to create kblockd\n");
2616 request_cachep
= kmem_cache_create("blkdev_requests",
2617 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2619 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2620 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);