243d18b4ceb0ba312c9d8427c00cd02a6c3ae88a
[deliverable/linux.git] / block / blk-core.c
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11 /*
12 * This handles all read/write requests to block devices
13 */
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/bio.h>
18 #include <linux/blkdev.h>
19 #include <linux/highmem.h>
20 #include <linux/mm.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
24 #include <linux/completion.h>
25 #include <linux/slab.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/task_io_accounting_ops.h>
29 #include <linux/blktrace_api.h>
30 #include <linux/fault-inject.h>
31 #include <trace/block.h>
32
33 #include "blk.h"
34
35 DEFINE_TRACE(block_plug);
36 DEFINE_TRACE(block_unplug_io);
37 DEFINE_TRACE(block_unplug_timer);
38 DEFINE_TRACE(block_getrq);
39 DEFINE_TRACE(block_sleeprq);
40 DEFINE_TRACE(block_rq_requeue);
41 DEFINE_TRACE(block_bio_backmerge);
42 DEFINE_TRACE(block_bio_frontmerge);
43 DEFINE_TRACE(block_bio_queue);
44 DEFINE_TRACE(block_rq_complete);
45 DEFINE_TRACE(block_remap); /* Also used in drivers/md/dm.c */
46 EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
47
48 static int __make_request(struct request_queue *q, struct bio *bio);
49
50 /*
51 * For the allocated request tables
52 */
53 static struct kmem_cache *request_cachep;
54
55 /*
56 * For queue allocation
57 */
58 struct kmem_cache *blk_requestq_cachep;
59
60 /*
61 * Controlling structure to kblockd
62 */
63 static struct workqueue_struct *kblockd_workqueue;
64
65 static void drive_stat_acct(struct request *rq, int new_io)
66 {
67 struct hd_struct *part;
68 int rw = rq_data_dir(rq);
69 int cpu;
70
71 if (!blk_fs_request(rq) || !rq->rq_disk)
72 return;
73
74 cpu = part_stat_lock();
75 part = disk_map_sector_rcu(rq->rq_disk, rq->sector);
76
77 if (!new_io)
78 part_stat_inc(cpu, part, merges[rw]);
79 else {
80 part_round_stats(cpu, part);
81 part_inc_in_flight(part);
82 }
83
84 part_stat_unlock();
85 }
86
87 void blk_queue_congestion_threshold(struct request_queue *q)
88 {
89 int nr;
90
91 nr = q->nr_requests - (q->nr_requests / 8) + 1;
92 if (nr > q->nr_requests)
93 nr = q->nr_requests;
94 q->nr_congestion_on = nr;
95
96 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
97 if (nr < 1)
98 nr = 1;
99 q->nr_congestion_off = nr;
100 }
101
102 /**
103 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
104 * @bdev: device
105 *
106 * Locates the passed device's request queue and returns the address of its
107 * backing_dev_info
108 *
109 * Will return NULL if the request queue cannot be located.
110 */
111 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
112 {
113 struct backing_dev_info *ret = NULL;
114 struct request_queue *q = bdev_get_queue(bdev);
115
116 if (q)
117 ret = &q->backing_dev_info;
118 return ret;
119 }
120 EXPORT_SYMBOL(blk_get_backing_dev_info);
121
122 void blk_rq_init(struct request_queue *q, struct request *rq)
123 {
124 memset(rq, 0, sizeof(*rq));
125
126 INIT_LIST_HEAD(&rq->queuelist);
127 INIT_LIST_HEAD(&rq->timeout_list);
128 rq->cpu = -1;
129 rq->q = q;
130 rq->sector = rq->hard_sector = (sector_t) -1;
131 INIT_HLIST_NODE(&rq->hash);
132 RB_CLEAR_NODE(&rq->rb_node);
133 rq->cmd = rq->__cmd;
134 rq->tag = -1;
135 rq->ref_count = 1;
136 }
137 EXPORT_SYMBOL(blk_rq_init);
138
139 static void req_bio_endio(struct request *rq, struct bio *bio,
140 unsigned int nbytes, int error)
141 {
142 struct request_queue *q = rq->q;
143
144 if (&q->bar_rq != rq) {
145 if (error)
146 clear_bit(BIO_UPTODATE, &bio->bi_flags);
147 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
148 error = -EIO;
149
150 if (unlikely(nbytes > bio->bi_size)) {
151 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
152 __func__, nbytes, bio->bi_size);
153 nbytes = bio->bi_size;
154 }
155
156 bio->bi_size -= nbytes;
157 bio->bi_sector += (nbytes >> 9);
158
159 if (bio_integrity(bio))
160 bio_integrity_advance(bio, nbytes);
161
162 if (bio->bi_size == 0)
163 bio_endio(bio, error);
164 } else {
165
166 /*
167 * Okay, this is the barrier request in progress, just
168 * record the error;
169 */
170 if (error && !q->orderr)
171 q->orderr = error;
172 }
173 }
174
175 void blk_dump_rq_flags(struct request *rq, char *msg)
176 {
177 int bit;
178
179 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
180 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
181 rq->cmd_flags);
182
183 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
184 (unsigned long long)rq->sector,
185 rq->nr_sectors,
186 rq->current_nr_sectors);
187 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
188 rq->bio, rq->biotail,
189 rq->buffer, rq->data,
190 rq->data_len);
191
192 if (blk_pc_request(rq)) {
193 printk(KERN_INFO " cdb: ");
194 for (bit = 0; bit < BLK_MAX_CDB; bit++)
195 printk("%02x ", rq->cmd[bit]);
196 printk("\n");
197 }
198 }
199 EXPORT_SYMBOL(blk_dump_rq_flags);
200
201 /*
202 * "plug" the device if there are no outstanding requests: this will
203 * force the transfer to start only after we have put all the requests
204 * on the list.
205 *
206 * This is called with interrupts off and no requests on the queue and
207 * with the queue lock held.
208 */
209 void blk_plug_device(struct request_queue *q)
210 {
211 WARN_ON(!irqs_disabled());
212
213 /*
214 * don't plug a stopped queue, it must be paired with blk_start_queue()
215 * which will restart the queueing
216 */
217 if (blk_queue_stopped(q))
218 return;
219
220 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
221 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
222 trace_block_plug(q);
223 }
224 }
225 EXPORT_SYMBOL(blk_plug_device);
226
227 /**
228 * blk_plug_device_unlocked - plug a device without queue lock held
229 * @q: The &struct request_queue to plug
230 *
231 * Description:
232 * Like @blk_plug_device(), but grabs the queue lock and disables
233 * interrupts.
234 **/
235 void blk_plug_device_unlocked(struct request_queue *q)
236 {
237 unsigned long flags;
238
239 spin_lock_irqsave(q->queue_lock, flags);
240 blk_plug_device(q);
241 spin_unlock_irqrestore(q->queue_lock, flags);
242 }
243 EXPORT_SYMBOL(blk_plug_device_unlocked);
244
245 /*
246 * remove the queue from the plugged list, if present. called with
247 * queue lock held and interrupts disabled.
248 */
249 int blk_remove_plug(struct request_queue *q)
250 {
251 WARN_ON(!irqs_disabled());
252
253 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
254 return 0;
255
256 del_timer(&q->unplug_timer);
257 return 1;
258 }
259 EXPORT_SYMBOL(blk_remove_plug);
260
261 /*
262 * remove the plug and let it rip..
263 */
264 void __generic_unplug_device(struct request_queue *q)
265 {
266 if (unlikely(blk_queue_stopped(q)))
267 return;
268
269 if (!blk_remove_plug(q))
270 return;
271
272 q->request_fn(q);
273 }
274
275 /**
276 * generic_unplug_device - fire a request queue
277 * @q: The &struct request_queue in question
278 *
279 * Description:
280 * Linux uses plugging to build bigger requests queues before letting
281 * the device have at them. If a queue is plugged, the I/O scheduler
282 * is still adding and merging requests on the queue. Once the queue
283 * gets unplugged, the request_fn defined for the queue is invoked and
284 * transfers started.
285 **/
286 void generic_unplug_device(struct request_queue *q)
287 {
288 if (blk_queue_plugged(q)) {
289 spin_lock_irq(q->queue_lock);
290 __generic_unplug_device(q);
291 spin_unlock_irq(q->queue_lock);
292 }
293 }
294 EXPORT_SYMBOL(generic_unplug_device);
295
296 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
297 struct page *page)
298 {
299 struct request_queue *q = bdi->unplug_io_data;
300
301 blk_unplug(q);
302 }
303
304 void blk_unplug_work(struct work_struct *work)
305 {
306 struct request_queue *q =
307 container_of(work, struct request_queue, unplug_work);
308
309 trace_block_unplug_io(q);
310 q->unplug_fn(q);
311 }
312
313 void blk_unplug_timeout(unsigned long data)
314 {
315 struct request_queue *q = (struct request_queue *)data;
316
317 trace_block_unplug_timer(q);
318 kblockd_schedule_work(q, &q->unplug_work);
319 }
320
321 void blk_unplug(struct request_queue *q)
322 {
323 /*
324 * devices don't necessarily have an ->unplug_fn defined
325 */
326 if (q->unplug_fn) {
327 trace_block_unplug_io(q);
328 q->unplug_fn(q);
329 }
330 }
331 EXPORT_SYMBOL(blk_unplug);
332
333 static void blk_invoke_request_fn(struct request_queue *q)
334 {
335 if (unlikely(blk_queue_stopped(q)))
336 return;
337
338 /*
339 * one level of recursion is ok and is much faster than kicking
340 * the unplug handling
341 */
342 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
343 q->request_fn(q);
344 queue_flag_clear(QUEUE_FLAG_REENTER, q);
345 } else {
346 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
347 kblockd_schedule_work(q, &q->unplug_work);
348 }
349 }
350
351 /**
352 * blk_start_queue - restart a previously stopped queue
353 * @q: The &struct request_queue in question
354 *
355 * Description:
356 * blk_start_queue() will clear the stop flag on the queue, and call
357 * the request_fn for the queue if it was in a stopped state when
358 * entered. Also see blk_stop_queue(). Queue lock must be held.
359 **/
360 void blk_start_queue(struct request_queue *q)
361 {
362 WARN_ON(!irqs_disabled());
363
364 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
365 blk_invoke_request_fn(q);
366 }
367 EXPORT_SYMBOL(blk_start_queue);
368
369 /**
370 * blk_stop_queue - stop a queue
371 * @q: The &struct request_queue in question
372 *
373 * Description:
374 * The Linux block layer assumes that a block driver will consume all
375 * entries on the request queue when the request_fn strategy is called.
376 * Often this will not happen, because of hardware limitations (queue
377 * depth settings). If a device driver gets a 'queue full' response,
378 * or if it simply chooses not to queue more I/O at one point, it can
379 * call this function to prevent the request_fn from being called until
380 * the driver has signalled it's ready to go again. This happens by calling
381 * blk_start_queue() to restart queue operations. Queue lock must be held.
382 **/
383 void blk_stop_queue(struct request_queue *q)
384 {
385 blk_remove_plug(q);
386 queue_flag_set(QUEUE_FLAG_STOPPED, q);
387 }
388 EXPORT_SYMBOL(blk_stop_queue);
389
390 /**
391 * blk_sync_queue - cancel any pending callbacks on a queue
392 * @q: the queue
393 *
394 * Description:
395 * The block layer may perform asynchronous callback activity
396 * on a queue, such as calling the unplug function after a timeout.
397 * A block device may call blk_sync_queue to ensure that any
398 * such activity is cancelled, thus allowing it to release resources
399 * that the callbacks might use. The caller must already have made sure
400 * that its ->make_request_fn will not re-add plugging prior to calling
401 * this function.
402 *
403 */
404 void blk_sync_queue(struct request_queue *q)
405 {
406 del_timer_sync(&q->unplug_timer);
407 del_timer_sync(&q->timeout);
408 kblockd_flush_work(&q->unplug_work);
409 }
410 EXPORT_SYMBOL(blk_sync_queue);
411
412 /**
413 * __blk_run_queue - run a single device queue
414 * @q: The queue to run
415 *
416 * Description:
417 * See @blk_run_queue. This variant must be called with the queue lock
418 * held and interrupts disabled.
419 *
420 */
421 void __blk_run_queue(struct request_queue *q)
422 {
423 blk_remove_plug(q);
424
425 /*
426 * Only recurse once to avoid overrunning the stack, let the unplug
427 * handling reinvoke the handler shortly if we already got there.
428 */
429 if (!elv_queue_empty(q))
430 blk_invoke_request_fn(q);
431 }
432 EXPORT_SYMBOL(__blk_run_queue);
433
434 /**
435 * blk_run_queue - run a single device queue
436 * @q: The queue to run
437 *
438 * Description:
439 * Invoke request handling on this queue, if it has pending work to do.
440 * May be used to restart queueing when a request has completed. Also
441 * See @blk_start_queueing.
442 *
443 */
444 void blk_run_queue(struct request_queue *q)
445 {
446 unsigned long flags;
447
448 spin_lock_irqsave(q->queue_lock, flags);
449 __blk_run_queue(q);
450 spin_unlock_irqrestore(q->queue_lock, flags);
451 }
452 EXPORT_SYMBOL(blk_run_queue);
453
454 void blk_put_queue(struct request_queue *q)
455 {
456 kobject_put(&q->kobj);
457 }
458
459 void blk_cleanup_queue(struct request_queue *q)
460 {
461 /*
462 * We know we have process context here, so we can be a little
463 * cautious and ensure that pending block actions on this device
464 * are done before moving on. Going into this function, we should
465 * not have processes doing IO to this device.
466 */
467 blk_sync_queue(q);
468
469 mutex_lock(&q->sysfs_lock);
470 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
471 mutex_unlock(&q->sysfs_lock);
472
473 if (q->elevator)
474 elevator_exit(q->elevator);
475
476 blk_put_queue(q);
477 }
478 EXPORT_SYMBOL(blk_cleanup_queue);
479
480 static int blk_init_free_list(struct request_queue *q)
481 {
482 struct request_list *rl = &q->rq;
483
484 rl->count[READ] = rl->count[WRITE] = 0;
485 rl->starved[READ] = rl->starved[WRITE] = 0;
486 rl->elvpriv = 0;
487 init_waitqueue_head(&rl->wait[READ]);
488 init_waitqueue_head(&rl->wait[WRITE]);
489
490 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
491 mempool_free_slab, request_cachep, q->node);
492
493 if (!rl->rq_pool)
494 return -ENOMEM;
495
496 return 0;
497 }
498
499 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
500 {
501 return blk_alloc_queue_node(gfp_mask, -1);
502 }
503 EXPORT_SYMBOL(blk_alloc_queue);
504
505 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
506 {
507 struct request_queue *q;
508 int err;
509
510 q = kmem_cache_alloc_node(blk_requestq_cachep,
511 gfp_mask | __GFP_ZERO, node_id);
512 if (!q)
513 return NULL;
514
515 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
516 q->backing_dev_info.unplug_io_data = q;
517 err = bdi_init(&q->backing_dev_info);
518 if (err) {
519 kmem_cache_free(blk_requestq_cachep, q);
520 return NULL;
521 }
522
523 init_timer(&q->unplug_timer);
524 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
525 INIT_LIST_HEAD(&q->timeout_list);
526 INIT_WORK(&q->unplug_work, blk_unplug_work);
527
528 kobject_init(&q->kobj, &blk_queue_ktype);
529
530 mutex_init(&q->sysfs_lock);
531 spin_lock_init(&q->__queue_lock);
532
533 return q;
534 }
535 EXPORT_SYMBOL(blk_alloc_queue_node);
536
537 /**
538 * blk_init_queue - prepare a request queue for use with a block device
539 * @rfn: The function to be called to process requests that have been
540 * placed on the queue.
541 * @lock: Request queue spin lock
542 *
543 * Description:
544 * If a block device wishes to use the standard request handling procedures,
545 * which sorts requests and coalesces adjacent requests, then it must
546 * call blk_init_queue(). The function @rfn will be called when there
547 * are requests on the queue that need to be processed. If the device
548 * supports plugging, then @rfn may not be called immediately when requests
549 * are available on the queue, but may be called at some time later instead.
550 * Plugged queues are generally unplugged when a buffer belonging to one
551 * of the requests on the queue is needed, or due to memory pressure.
552 *
553 * @rfn is not required, or even expected, to remove all requests off the
554 * queue, but only as many as it can handle at a time. If it does leave
555 * requests on the queue, it is responsible for arranging that the requests
556 * get dealt with eventually.
557 *
558 * The queue spin lock must be held while manipulating the requests on the
559 * request queue; this lock will be taken also from interrupt context, so irq
560 * disabling is needed for it.
561 *
562 * Function returns a pointer to the initialized request queue, or %NULL if
563 * it didn't succeed.
564 *
565 * Note:
566 * blk_init_queue() must be paired with a blk_cleanup_queue() call
567 * when the block device is deactivated (such as at module unload).
568 **/
569
570 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
571 {
572 return blk_init_queue_node(rfn, lock, -1);
573 }
574 EXPORT_SYMBOL(blk_init_queue);
575
576 struct request_queue *
577 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
578 {
579 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
580
581 if (!q)
582 return NULL;
583
584 q->node = node_id;
585 if (blk_init_free_list(q)) {
586 kmem_cache_free(blk_requestq_cachep, q);
587 return NULL;
588 }
589
590 /*
591 * if caller didn't supply a lock, they get per-queue locking with
592 * our embedded lock
593 */
594 if (!lock)
595 lock = &q->__queue_lock;
596
597 q->request_fn = rfn;
598 q->prep_rq_fn = NULL;
599 q->unplug_fn = generic_unplug_device;
600 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER |
601 1 << QUEUE_FLAG_STACKABLE);
602 q->queue_lock = lock;
603
604 blk_queue_segment_boundary(q, BLK_SEG_BOUNDARY_MASK);
605
606 blk_queue_make_request(q, __make_request);
607 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
608
609 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
610 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
611
612 q->sg_reserved_size = INT_MAX;
613
614 blk_set_cmd_filter_defaults(&q->cmd_filter);
615
616 /*
617 * all done
618 */
619 if (!elevator_init(q, NULL)) {
620 blk_queue_congestion_threshold(q);
621 return q;
622 }
623
624 blk_put_queue(q);
625 return NULL;
626 }
627 EXPORT_SYMBOL(blk_init_queue_node);
628
629 int blk_get_queue(struct request_queue *q)
630 {
631 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
632 kobject_get(&q->kobj);
633 return 0;
634 }
635
636 return 1;
637 }
638
639 static inline void blk_free_request(struct request_queue *q, struct request *rq)
640 {
641 if (rq->cmd_flags & REQ_ELVPRIV)
642 elv_put_request(q, rq);
643 mempool_free(rq, q->rq.rq_pool);
644 }
645
646 static struct request *
647 blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
648 {
649 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
650
651 if (!rq)
652 return NULL;
653
654 blk_rq_init(q, rq);
655
656 rq->cmd_flags = rw | REQ_ALLOCED;
657
658 if (priv) {
659 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
660 mempool_free(rq, q->rq.rq_pool);
661 return NULL;
662 }
663 rq->cmd_flags |= REQ_ELVPRIV;
664 }
665
666 return rq;
667 }
668
669 /*
670 * ioc_batching returns true if the ioc is a valid batching request and
671 * should be given priority access to a request.
672 */
673 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
674 {
675 if (!ioc)
676 return 0;
677
678 /*
679 * Make sure the process is able to allocate at least 1 request
680 * even if the batch times out, otherwise we could theoretically
681 * lose wakeups.
682 */
683 return ioc->nr_batch_requests == q->nr_batching ||
684 (ioc->nr_batch_requests > 0
685 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
686 }
687
688 /*
689 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
690 * will cause the process to be a "batcher" on all queues in the system. This
691 * is the behaviour we want though - once it gets a wakeup it should be given
692 * a nice run.
693 */
694 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
695 {
696 if (!ioc || ioc_batching(q, ioc))
697 return;
698
699 ioc->nr_batch_requests = q->nr_batching;
700 ioc->last_waited = jiffies;
701 }
702
703 static void __freed_request(struct request_queue *q, int rw)
704 {
705 struct request_list *rl = &q->rq;
706
707 if (rl->count[rw] < queue_congestion_off_threshold(q))
708 blk_clear_queue_congested(q, rw);
709
710 if (rl->count[rw] + 1 <= q->nr_requests) {
711 if (waitqueue_active(&rl->wait[rw]))
712 wake_up(&rl->wait[rw]);
713
714 blk_clear_queue_full(q, rw);
715 }
716 }
717
718 /*
719 * A request has just been released. Account for it, update the full and
720 * congestion status, wake up any waiters. Called under q->queue_lock.
721 */
722 static void freed_request(struct request_queue *q, int rw, int priv)
723 {
724 struct request_list *rl = &q->rq;
725
726 rl->count[rw]--;
727 if (priv)
728 rl->elvpriv--;
729
730 __freed_request(q, rw);
731
732 if (unlikely(rl->starved[rw ^ 1]))
733 __freed_request(q, rw ^ 1);
734 }
735
736 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
737 /*
738 * Get a free request, queue_lock must be held.
739 * Returns NULL on failure, with queue_lock held.
740 * Returns !NULL on success, with queue_lock *not held*.
741 */
742 static struct request *get_request(struct request_queue *q, int rw_flags,
743 struct bio *bio, gfp_t gfp_mask)
744 {
745 struct request *rq = NULL;
746 struct request_list *rl = &q->rq;
747 struct io_context *ioc = NULL;
748 const int rw = rw_flags & 0x01;
749 int may_queue, priv;
750
751 may_queue = elv_may_queue(q, rw_flags);
752 if (may_queue == ELV_MQUEUE_NO)
753 goto rq_starved;
754
755 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
756 if (rl->count[rw]+1 >= q->nr_requests) {
757 ioc = current_io_context(GFP_ATOMIC, q->node);
758 /*
759 * The queue will fill after this allocation, so set
760 * it as full, and mark this process as "batching".
761 * This process will be allowed to complete a batch of
762 * requests, others will be blocked.
763 */
764 if (!blk_queue_full(q, rw)) {
765 ioc_set_batching(q, ioc);
766 blk_set_queue_full(q, rw);
767 } else {
768 if (may_queue != ELV_MQUEUE_MUST
769 && !ioc_batching(q, ioc)) {
770 /*
771 * The queue is full and the allocating
772 * process is not a "batcher", and not
773 * exempted by the IO scheduler
774 */
775 goto out;
776 }
777 }
778 }
779 blk_set_queue_congested(q, rw);
780 }
781
782 /*
783 * Only allow batching queuers to allocate up to 50% over the defined
784 * limit of requests, otherwise we could have thousands of requests
785 * allocated with any setting of ->nr_requests
786 */
787 if (rl->count[rw] >= (3 * q->nr_requests / 2))
788 goto out;
789
790 rl->count[rw]++;
791 rl->starved[rw] = 0;
792
793 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
794 if (priv)
795 rl->elvpriv++;
796
797 spin_unlock_irq(q->queue_lock);
798
799 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
800 if (unlikely(!rq)) {
801 /*
802 * Allocation failed presumably due to memory. Undo anything
803 * we might have messed up.
804 *
805 * Allocating task should really be put onto the front of the
806 * wait queue, but this is pretty rare.
807 */
808 spin_lock_irq(q->queue_lock);
809 freed_request(q, rw, priv);
810
811 /*
812 * in the very unlikely event that allocation failed and no
813 * requests for this direction was pending, mark us starved
814 * so that freeing of a request in the other direction will
815 * notice us. another possible fix would be to split the
816 * rq mempool into READ and WRITE
817 */
818 rq_starved:
819 if (unlikely(rl->count[rw] == 0))
820 rl->starved[rw] = 1;
821
822 goto out;
823 }
824
825 /*
826 * ioc may be NULL here, and ioc_batching will be false. That's
827 * OK, if the queue is under the request limit then requests need
828 * not count toward the nr_batch_requests limit. There will always
829 * be some limit enforced by BLK_BATCH_TIME.
830 */
831 if (ioc_batching(q, ioc))
832 ioc->nr_batch_requests--;
833
834 trace_block_getrq(q, bio, rw);
835 out:
836 return rq;
837 }
838
839 /*
840 * No available requests for this queue, unplug the device and wait for some
841 * requests to become available.
842 *
843 * Called with q->queue_lock held, and returns with it unlocked.
844 */
845 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
846 struct bio *bio)
847 {
848 const int rw = rw_flags & 0x01;
849 struct request *rq;
850
851 rq = get_request(q, rw_flags, bio, GFP_NOIO);
852 while (!rq) {
853 DEFINE_WAIT(wait);
854 struct io_context *ioc;
855 struct request_list *rl = &q->rq;
856
857 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
858 TASK_UNINTERRUPTIBLE);
859
860 trace_block_sleeprq(q, bio, rw);
861
862 __generic_unplug_device(q);
863 spin_unlock_irq(q->queue_lock);
864 io_schedule();
865
866 /*
867 * After sleeping, we become a "batching" process and
868 * will be able to allocate at least one request, and
869 * up to a big batch of them for a small period time.
870 * See ioc_batching, ioc_set_batching
871 */
872 ioc = current_io_context(GFP_NOIO, q->node);
873 ioc_set_batching(q, ioc);
874
875 spin_lock_irq(q->queue_lock);
876 finish_wait(&rl->wait[rw], &wait);
877
878 rq = get_request(q, rw_flags, bio, GFP_NOIO);
879 };
880
881 return rq;
882 }
883
884 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
885 {
886 struct request *rq;
887
888 BUG_ON(rw != READ && rw != WRITE);
889
890 spin_lock_irq(q->queue_lock);
891 if (gfp_mask & __GFP_WAIT) {
892 rq = get_request_wait(q, rw, NULL);
893 } else {
894 rq = get_request(q, rw, NULL, gfp_mask);
895 if (!rq)
896 spin_unlock_irq(q->queue_lock);
897 }
898 /* q->queue_lock is unlocked at this point */
899
900 return rq;
901 }
902 EXPORT_SYMBOL(blk_get_request);
903
904 /**
905 * blk_start_queueing - initiate dispatch of requests to device
906 * @q: request queue to kick into gear
907 *
908 * This is basically a helper to remove the need to know whether a queue
909 * is plugged or not if someone just wants to initiate dispatch of requests
910 * for this queue. Should be used to start queueing on a device outside
911 * of ->request_fn() context. Also see @blk_run_queue.
912 *
913 * The queue lock must be held with interrupts disabled.
914 */
915 void blk_start_queueing(struct request_queue *q)
916 {
917 if (!blk_queue_plugged(q)) {
918 if (unlikely(blk_queue_stopped(q)))
919 return;
920 q->request_fn(q);
921 } else
922 __generic_unplug_device(q);
923 }
924 EXPORT_SYMBOL(blk_start_queueing);
925
926 /**
927 * blk_requeue_request - put a request back on queue
928 * @q: request queue where request should be inserted
929 * @rq: request to be inserted
930 *
931 * Description:
932 * Drivers often keep queueing requests until the hardware cannot accept
933 * more, when that condition happens we need to put the request back
934 * on the queue. Must be called with queue lock held.
935 */
936 void blk_requeue_request(struct request_queue *q, struct request *rq)
937 {
938 blk_delete_timer(rq);
939 blk_clear_rq_complete(rq);
940 trace_block_rq_requeue(q, rq);
941
942 if (blk_rq_tagged(rq))
943 blk_queue_end_tag(q, rq);
944
945 elv_requeue_request(q, rq);
946 }
947 EXPORT_SYMBOL(blk_requeue_request);
948
949 /**
950 * blk_insert_request - insert a special request into a request queue
951 * @q: request queue where request should be inserted
952 * @rq: request to be inserted
953 * @at_head: insert request at head or tail of queue
954 * @data: private data
955 *
956 * Description:
957 * Many block devices need to execute commands asynchronously, so they don't
958 * block the whole kernel from preemption during request execution. This is
959 * accomplished normally by inserting aritficial requests tagged as
960 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
961 * be scheduled for actual execution by the request queue.
962 *
963 * We have the option of inserting the head or the tail of the queue.
964 * Typically we use the tail for new ioctls and so forth. We use the head
965 * of the queue for things like a QUEUE_FULL message from a device, or a
966 * host that is unable to accept a particular command.
967 */
968 void blk_insert_request(struct request_queue *q, struct request *rq,
969 int at_head, void *data)
970 {
971 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
972 unsigned long flags;
973
974 /*
975 * tell I/O scheduler that this isn't a regular read/write (ie it
976 * must not attempt merges on this) and that it acts as a soft
977 * barrier
978 */
979 rq->cmd_type = REQ_TYPE_SPECIAL;
980 rq->cmd_flags |= REQ_SOFTBARRIER;
981
982 rq->special = data;
983
984 spin_lock_irqsave(q->queue_lock, flags);
985
986 /*
987 * If command is tagged, release the tag
988 */
989 if (blk_rq_tagged(rq))
990 blk_queue_end_tag(q, rq);
991
992 drive_stat_acct(rq, 1);
993 __elv_add_request(q, rq, where, 0);
994 blk_start_queueing(q);
995 spin_unlock_irqrestore(q->queue_lock, flags);
996 }
997 EXPORT_SYMBOL(blk_insert_request);
998
999 /*
1000 * add-request adds a request to the linked list.
1001 * queue lock is held and interrupts disabled, as we muck with the
1002 * request queue list.
1003 */
1004 static inline void add_request(struct request_queue *q, struct request *req)
1005 {
1006 drive_stat_acct(req, 1);
1007
1008 /*
1009 * elevator indicated where it wants this request to be
1010 * inserted at elevator_merge time
1011 */
1012 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1013 }
1014
1015 static void part_round_stats_single(int cpu, struct hd_struct *part,
1016 unsigned long now)
1017 {
1018 if (now == part->stamp)
1019 return;
1020
1021 if (part->in_flight) {
1022 __part_stat_add(cpu, part, time_in_queue,
1023 part->in_flight * (now - part->stamp));
1024 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1025 }
1026 part->stamp = now;
1027 }
1028
1029 /**
1030 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1031 * @cpu: cpu number for stats access
1032 * @part: target partition
1033 *
1034 * The average IO queue length and utilisation statistics are maintained
1035 * by observing the current state of the queue length and the amount of
1036 * time it has been in this state for.
1037 *
1038 * Normally, that accounting is done on IO completion, but that can result
1039 * in more than a second's worth of IO being accounted for within any one
1040 * second, leading to >100% utilisation. To deal with that, we call this
1041 * function to do a round-off before returning the results when reading
1042 * /proc/diskstats. This accounts immediately for all queue usage up to
1043 * the current jiffies and restarts the counters again.
1044 */
1045 void part_round_stats(int cpu, struct hd_struct *part)
1046 {
1047 unsigned long now = jiffies;
1048
1049 if (part->partno)
1050 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1051 part_round_stats_single(cpu, part, now);
1052 }
1053 EXPORT_SYMBOL_GPL(part_round_stats);
1054
1055 /*
1056 * queue lock must be held
1057 */
1058 void __blk_put_request(struct request_queue *q, struct request *req)
1059 {
1060 if (unlikely(!q))
1061 return;
1062 if (unlikely(--req->ref_count))
1063 return;
1064
1065 elv_completed_request(q, req);
1066
1067 /*
1068 * Request may not have originated from ll_rw_blk. if not,
1069 * it didn't come out of our reserved rq pools
1070 */
1071 if (req->cmd_flags & REQ_ALLOCED) {
1072 int rw = rq_data_dir(req);
1073 int priv = req->cmd_flags & REQ_ELVPRIV;
1074
1075 BUG_ON(!list_empty(&req->queuelist));
1076 BUG_ON(!hlist_unhashed(&req->hash));
1077
1078 blk_free_request(q, req);
1079 freed_request(q, rw, priv);
1080 }
1081 }
1082 EXPORT_SYMBOL_GPL(__blk_put_request);
1083
1084 void blk_put_request(struct request *req)
1085 {
1086 unsigned long flags;
1087 struct request_queue *q = req->q;
1088
1089 spin_lock_irqsave(q->queue_lock, flags);
1090 __blk_put_request(q, req);
1091 spin_unlock_irqrestore(q->queue_lock, flags);
1092 }
1093 EXPORT_SYMBOL(blk_put_request);
1094
1095 void init_request_from_bio(struct request *req, struct bio *bio)
1096 {
1097 req->cpu = bio->bi_comp_cpu;
1098 req->cmd_type = REQ_TYPE_FS;
1099
1100 /*
1101 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1102 */
1103 if (bio_rw_ahead(bio))
1104 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1105 REQ_FAILFAST_DRIVER);
1106 if (bio_failfast_dev(bio))
1107 req->cmd_flags |= REQ_FAILFAST_DEV;
1108 if (bio_failfast_transport(bio))
1109 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1110 if (bio_failfast_driver(bio))
1111 req->cmd_flags |= REQ_FAILFAST_DRIVER;
1112
1113 /*
1114 * REQ_BARRIER implies no merging, but lets make it explicit
1115 */
1116 if (unlikely(bio_discard(bio))) {
1117 req->cmd_flags |= REQ_DISCARD;
1118 if (bio_barrier(bio))
1119 req->cmd_flags |= REQ_SOFTBARRIER;
1120 req->q->prepare_discard_fn(req->q, req);
1121 } else if (unlikely(bio_barrier(bio)))
1122 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
1123
1124 if (bio_sync(bio))
1125 req->cmd_flags |= REQ_RW_SYNC;
1126 if (bio_rw_meta(bio))
1127 req->cmd_flags |= REQ_RW_META;
1128
1129 req->errors = 0;
1130 req->hard_sector = req->sector = bio->bi_sector;
1131 req->ioprio = bio_prio(bio);
1132 req->start_time = jiffies;
1133 blk_rq_bio_prep(req->q, req, bio);
1134 }
1135
1136 static int __make_request(struct request_queue *q, struct bio *bio)
1137 {
1138 struct request *req;
1139 int el_ret, nr_sectors, barrier, discard, err;
1140 const unsigned short prio = bio_prio(bio);
1141 const int sync = bio_sync(bio);
1142 int rw_flags;
1143
1144 nr_sectors = bio_sectors(bio);
1145
1146 /*
1147 * low level driver can indicate that it wants pages above a
1148 * certain limit bounced to low memory (ie for highmem, or even
1149 * ISA dma in theory)
1150 */
1151 blk_queue_bounce(q, &bio);
1152
1153 barrier = bio_barrier(bio);
1154 if (unlikely(barrier) && bio_has_data(bio) &&
1155 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1156 err = -EOPNOTSUPP;
1157 goto end_io;
1158 }
1159
1160 discard = bio_discard(bio);
1161 if (unlikely(discard) && !q->prepare_discard_fn) {
1162 err = -EOPNOTSUPP;
1163 goto end_io;
1164 }
1165
1166 spin_lock_irq(q->queue_lock);
1167
1168 if (unlikely(barrier) || elv_queue_empty(q))
1169 goto get_rq;
1170
1171 el_ret = elv_merge(q, &req, bio);
1172 switch (el_ret) {
1173 case ELEVATOR_BACK_MERGE:
1174 BUG_ON(!rq_mergeable(req));
1175
1176 if (!ll_back_merge_fn(q, req, bio))
1177 break;
1178
1179 trace_block_bio_backmerge(q, bio);
1180
1181 req->biotail->bi_next = bio;
1182 req->biotail = bio;
1183 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1184 req->ioprio = ioprio_best(req->ioprio, prio);
1185 if (!blk_rq_cpu_valid(req))
1186 req->cpu = bio->bi_comp_cpu;
1187 drive_stat_acct(req, 0);
1188 if (!attempt_back_merge(q, req))
1189 elv_merged_request(q, req, el_ret);
1190 goto out;
1191
1192 case ELEVATOR_FRONT_MERGE:
1193 BUG_ON(!rq_mergeable(req));
1194
1195 if (!ll_front_merge_fn(q, req, bio))
1196 break;
1197
1198 trace_block_bio_frontmerge(q, bio);
1199
1200 bio->bi_next = req->bio;
1201 req->bio = bio;
1202
1203 /*
1204 * may not be valid. if the low level driver said
1205 * it didn't need a bounce buffer then it better
1206 * not touch req->buffer either...
1207 */
1208 req->buffer = bio_data(bio);
1209 req->current_nr_sectors = bio_cur_sectors(bio);
1210 req->hard_cur_sectors = req->current_nr_sectors;
1211 req->sector = req->hard_sector = bio->bi_sector;
1212 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1213 req->ioprio = ioprio_best(req->ioprio, prio);
1214 if (!blk_rq_cpu_valid(req))
1215 req->cpu = bio->bi_comp_cpu;
1216 drive_stat_acct(req, 0);
1217 if (!attempt_front_merge(q, req))
1218 elv_merged_request(q, req, el_ret);
1219 goto out;
1220
1221 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1222 default:
1223 ;
1224 }
1225
1226 get_rq:
1227 /*
1228 * This sync check and mask will be re-done in init_request_from_bio(),
1229 * but we need to set it earlier to expose the sync flag to the
1230 * rq allocator and io schedulers.
1231 */
1232 rw_flags = bio_data_dir(bio);
1233 if (sync)
1234 rw_flags |= REQ_RW_SYNC;
1235
1236 /*
1237 * Grab a free request. This is might sleep but can not fail.
1238 * Returns with the queue unlocked.
1239 */
1240 req = get_request_wait(q, rw_flags, bio);
1241
1242 /*
1243 * After dropping the lock and possibly sleeping here, our request
1244 * may now be mergeable after it had proven unmergeable (above).
1245 * We don't worry about that case for efficiency. It won't happen
1246 * often, and the elevators are able to handle it.
1247 */
1248 init_request_from_bio(req, bio);
1249
1250 spin_lock_irq(q->queue_lock);
1251 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1252 bio_flagged(bio, BIO_CPU_AFFINE))
1253 req->cpu = blk_cpu_to_group(smp_processor_id());
1254 if (elv_queue_empty(q))
1255 blk_plug_device(q);
1256 add_request(q, req);
1257 out:
1258 if (sync)
1259 __generic_unplug_device(q);
1260 spin_unlock_irq(q->queue_lock);
1261 return 0;
1262
1263 end_io:
1264 bio_endio(bio, err);
1265 return 0;
1266 }
1267
1268 /*
1269 * If bio->bi_dev is a partition, remap the location
1270 */
1271 static inline void blk_partition_remap(struct bio *bio)
1272 {
1273 struct block_device *bdev = bio->bi_bdev;
1274
1275 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1276 struct hd_struct *p = bdev->bd_part;
1277
1278 bio->bi_sector += p->start_sect;
1279 bio->bi_bdev = bdev->bd_contains;
1280
1281 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
1282 bdev->bd_dev, bio->bi_sector,
1283 bio->bi_sector - p->start_sect);
1284 }
1285 }
1286
1287 static void handle_bad_sector(struct bio *bio)
1288 {
1289 char b[BDEVNAME_SIZE];
1290
1291 printk(KERN_INFO "attempt to access beyond end of device\n");
1292 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1293 bdevname(bio->bi_bdev, b),
1294 bio->bi_rw,
1295 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1296 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1297
1298 set_bit(BIO_EOF, &bio->bi_flags);
1299 }
1300
1301 #ifdef CONFIG_FAIL_MAKE_REQUEST
1302
1303 static DECLARE_FAULT_ATTR(fail_make_request);
1304
1305 static int __init setup_fail_make_request(char *str)
1306 {
1307 return setup_fault_attr(&fail_make_request, str);
1308 }
1309 __setup("fail_make_request=", setup_fail_make_request);
1310
1311 static int should_fail_request(struct bio *bio)
1312 {
1313 struct hd_struct *part = bio->bi_bdev->bd_part;
1314
1315 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
1316 return should_fail(&fail_make_request, bio->bi_size);
1317
1318 return 0;
1319 }
1320
1321 static int __init fail_make_request_debugfs(void)
1322 {
1323 return init_fault_attr_dentries(&fail_make_request,
1324 "fail_make_request");
1325 }
1326
1327 late_initcall(fail_make_request_debugfs);
1328
1329 #else /* CONFIG_FAIL_MAKE_REQUEST */
1330
1331 static inline int should_fail_request(struct bio *bio)
1332 {
1333 return 0;
1334 }
1335
1336 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1337
1338 /*
1339 * Check whether this bio extends beyond the end of the device.
1340 */
1341 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1342 {
1343 sector_t maxsector;
1344
1345 if (!nr_sectors)
1346 return 0;
1347
1348 /* Test device or partition size, when known. */
1349 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1350 if (maxsector) {
1351 sector_t sector = bio->bi_sector;
1352
1353 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1354 /*
1355 * This may well happen - the kernel calls bread()
1356 * without checking the size of the device, e.g., when
1357 * mounting a device.
1358 */
1359 handle_bad_sector(bio);
1360 return 1;
1361 }
1362 }
1363
1364 return 0;
1365 }
1366
1367 /**
1368 * generic_make_request - hand a buffer to its device driver for I/O
1369 * @bio: The bio describing the location in memory and on the device.
1370 *
1371 * generic_make_request() is used to make I/O requests of block
1372 * devices. It is passed a &struct bio, which describes the I/O that needs
1373 * to be done.
1374 *
1375 * generic_make_request() does not return any status. The
1376 * success/failure status of the request, along with notification of
1377 * completion, is delivered asynchronously through the bio->bi_end_io
1378 * function described (one day) else where.
1379 *
1380 * The caller of generic_make_request must make sure that bi_io_vec
1381 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1382 * set to describe the device address, and the
1383 * bi_end_io and optionally bi_private are set to describe how
1384 * completion notification should be signaled.
1385 *
1386 * generic_make_request and the drivers it calls may use bi_next if this
1387 * bio happens to be merged with someone else, and may change bi_dev and
1388 * bi_sector for remaps as it sees fit. So the values of these fields
1389 * should NOT be depended on after the call to generic_make_request.
1390 */
1391 static inline void __generic_make_request(struct bio *bio)
1392 {
1393 struct request_queue *q;
1394 sector_t old_sector;
1395 int ret, nr_sectors = bio_sectors(bio);
1396 dev_t old_dev;
1397 int err = -EIO;
1398
1399 might_sleep();
1400
1401 if (bio_check_eod(bio, nr_sectors))
1402 goto end_io;
1403
1404 /*
1405 * Resolve the mapping until finished. (drivers are
1406 * still free to implement/resolve their own stacking
1407 * by explicitly returning 0)
1408 *
1409 * NOTE: we don't repeat the blk_size check for each new device.
1410 * Stacking drivers are expected to know what they are doing.
1411 */
1412 old_sector = -1;
1413 old_dev = 0;
1414 do {
1415 char b[BDEVNAME_SIZE];
1416
1417 q = bdev_get_queue(bio->bi_bdev);
1418 if (!q) {
1419 printk(KERN_ERR
1420 "generic_make_request: Trying to access "
1421 "nonexistent block-device %s (%Lu)\n",
1422 bdevname(bio->bi_bdev, b),
1423 (long long) bio->bi_sector);
1424 end_io:
1425 bio_endio(bio, err);
1426 break;
1427 }
1428
1429 if (unlikely(nr_sectors > q->max_hw_sectors)) {
1430 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1431 bdevname(bio->bi_bdev, b),
1432 bio_sectors(bio),
1433 q->max_hw_sectors);
1434 goto end_io;
1435 }
1436
1437 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1438 goto end_io;
1439
1440 if (should_fail_request(bio))
1441 goto end_io;
1442
1443 /*
1444 * If this device has partitions, remap block n
1445 * of partition p to block n+start(p) of the disk.
1446 */
1447 blk_partition_remap(bio);
1448
1449 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1450 goto end_io;
1451
1452 if (old_sector != -1)
1453 trace_block_remap(q, bio, old_dev, bio->bi_sector,
1454 old_sector);
1455
1456 trace_block_bio_queue(q, bio);
1457
1458 old_sector = bio->bi_sector;
1459 old_dev = bio->bi_bdev->bd_dev;
1460
1461 if (bio_check_eod(bio, nr_sectors))
1462 goto end_io;
1463 if ((bio_empty_barrier(bio) && !q->prepare_flush_fn) ||
1464 (bio_discard(bio) && !q->prepare_discard_fn)) {
1465 err = -EOPNOTSUPP;
1466 goto end_io;
1467 }
1468
1469 ret = q->make_request_fn(q, bio);
1470 } while (ret);
1471 }
1472
1473 /*
1474 * We only want one ->make_request_fn to be active at a time,
1475 * else stack usage with stacked devices could be a problem.
1476 * So use current->bio_{list,tail} to keep a list of requests
1477 * submited by a make_request_fn function.
1478 * current->bio_tail is also used as a flag to say if
1479 * generic_make_request is currently active in this task or not.
1480 * If it is NULL, then no make_request is active. If it is non-NULL,
1481 * then a make_request is active, and new requests should be added
1482 * at the tail
1483 */
1484 void generic_make_request(struct bio *bio)
1485 {
1486 if (current->bio_tail) {
1487 /* make_request is active */
1488 *(current->bio_tail) = bio;
1489 bio->bi_next = NULL;
1490 current->bio_tail = &bio->bi_next;
1491 return;
1492 }
1493 /* following loop may be a bit non-obvious, and so deserves some
1494 * explanation.
1495 * Before entering the loop, bio->bi_next is NULL (as all callers
1496 * ensure that) so we have a list with a single bio.
1497 * We pretend that we have just taken it off a longer list, so
1498 * we assign bio_list to the next (which is NULL) and bio_tail
1499 * to &bio_list, thus initialising the bio_list of new bios to be
1500 * added. __generic_make_request may indeed add some more bios
1501 * through a recursive call to generic_make_request. If it
1502 * did, we find a non-NULL value in bio_list and re-enter the loop
1503 * from the top. In this case we really did just take the bio
1504 * of the top of the list (no pretending) and so fixup bio_list and
1505 * bio_tail or bi_next, and call into __generic_make_request again.
1506 *
1507 * The loop was structured like this to make only one call to
1508 * __generic_make_request (which is important as it is large and
1509 * inlined) and to keep the structure simple.
1510 */
1511 BUG_ON(bio->bi_next);
1512 do {
1513 current->bio_list = bio->bi_next;
1514 if (bio->bi_next == NULL)
1515 current->bio_tail = &current->bio_list;
1516 else
1517 bio->bi_next = NULL;
1518 __generic_make_request(bio);
1519 bio = current->bio_list;
1520 } while (bio);
1521 current->bio_tail = NULL; /* deactivate */
1522 }
1523 EXPORT_SYMBOL(generic_make_request);
1524
1525 /**
1526 * submit_bio - submit a bio to the block device layer for I/O
1527 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1528 * @bio: The &struct bio which describes the I/O
1529 *
1530 * submit_bio() is very similar in purpose to generic_make_request(), and
1531 * uses that function to do most of the work. Both are fairly rough
1532 * interfaces; @bio must be presetup and ready for I/O.
1533 *
1534 */
1535 void submit_bio(int rw, struct bio *bio)
1536 {
1537 int count = bio_sectors(bio);
1538
1539 bio->bi_rw |= rw;
1540
1541 /*
1542 * If it's a regular read/write or a barrier with data attached,
1543 * go through the normal accounting stuff before submission.
1544 */
1545 if (bio_has_data(bio)) {
1546 if (rw & WRITE) {
1547 count_vm_events(PGPGOUT, count);
1548 } else {
1549 task_io_account_read(bio->bi_size);
1550 count_vm_events(PGPGIN, count);
1551 }
1552
1553 if (unlikely(block_dump)) {
1554 char b[BDEVNAME_SIZE];
1555 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
1556 current->comm, task_pid_nr(current),
1557 (rw & WRITE) ? "WRITE" : "READ",
1558 (unsigned long long)bio->bi_sector,
1559 bdevname(bio->bi_bdev, b));
1560 }
1561 }
1562
1563 generic_make_request(bio);
1564 }
1565 EXPORT_SYMBOL(submit_bio);
1566
1567 /**
1568 * blk_rq_check_limits - Helper function to check a request for the queue limit
1569 * @q: the queue
1570 * @rq: the request being checked
1571 *
1572 * Description:
1573 * @rq may have been made based on weaker limitations of upper-level queues
1574 * in request stacking drivers, and it may violate the limitation of @q.
1575 * Since the block layer and the underlying device driver trust @rq
1576 * after it is inserted to @q, it should be checked against @q before
1577 * the insertion using this generic function.
1578 *
1579 * This function should also be useful for request stacking drivers
1580 * in some cases below, so export this fuction.
1581 * Request stacking drivers like request-based dm may change the queue
1582 * limits while requests are in the queue (e.g. dm's table swapping).
1583 * Such request stacking drivers should check those requests agaist
1584 * the new queue limits again when they dispatch those requests,
1585 * although such checkings are also done against the old queue limits
1586 * when submitting requests.
1587 */
1588 int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1589 {
1590 if (rq->nr_sectors > q->max_sectors ||
1591 rq->data_len > q->max_hw_sectors << 9) {
1592 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1593 return -EIO;
1594 }
1595
1596 /*
1597 * queue's settings related to segment counting like q->bounce_pfn
1598 * may differ from that of other stacking queues.
1599 * Recalculate it to check the request correctly on this queue's
1600 * limitation.
1601 */
1602 blk_recalc_rq_segments(rq);
1603 if (rq->nr_phys_segments > q->max_phys_segments ||
1604 rq->nr_phys_segments > q->max_hw_segments) {
1605 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1606 return -EIO;
1607 }
1608
1609 return 0;
1610 }
1611 EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1612
1613 /**
1614 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1615 * @q: the queue to submit the request
1616 * @rq: the request being queued
1617 */
1618 int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1619 {
1620 unsigned long flags;
1621
1622 if (blk_rq_check_limits(q, rq))
1623 return -EIO;
1624
1625 #ifdef CONFIG_FAIL_MAKE_REQUEST
1626 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1627 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1628 return -EIO;
1629 #endif
1630
1631 spin_lock_irqsave(q->queue_lock, flags);
1632
1633 /*
1634 * Submitting request must be dequeued before calling this function
1635 * because it will be linked to another request_queue
1636 */
1637 BUG_ON(blk_queued_rq(rq));
1638
1639 drive_stat_acct(rq, 1);
1640 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1641
1642 spin_unlock_irqrestore(q->queue_lock, flags);
1643
1644 return 0;
1645 }
1646 EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1647
1648 /**
1649 * blkdev_dequeue_request - dequeue request and start timeout timer
1650 * @req: request to dequeue
1651 *
1652 * Dequeue @req and start timeout timer on it. This hands off the
1653 * request to the driver.
1654 *
1655 * Block internal functions which don't want to start timer should
1656 * call elv_dequeue_request().
1657 */
1658 void blkdev_dequeue_request(struct request *req)
1659 {
1660 elv_dequeue_request(req->q, req);
1661
1662 /*
1663 * We are now handing the request to the hardware, add the
1664 * timeout handler.
1665 */
1666 blk_add_timer(req);
1667 }
1668 EXPORT_SYMBOL(blkdev_dequeue_request);
1669
1670 /**
1671 * __end_that_request_first - end I/O on a request
1672 * @req: the request being processed
1673 * @error: %0 for success, < %0 for error
1674 * @nr_bytes: number of bytes to complete
1675 *
1676 * Description:
1677 * Ends I/O on a number of bytes attached to @req, and sets it up
1678 * for the next range of segments (if any) in the cluster.
1679 *
1680 * Return:
1681 * %0 - we are done with this request, call end_that_request_last()
1682 * %1 - still buffers pending for this request
1683 **/
1684 static int __end_that_request_first(struct request *req, int error,
1685 int nr_bytes)
1686 {
1687 int total_bytes, bio_nbytes, next_idx = 0;
1688 struct bio *bio;
1689
1690 trace_block_rq_complete(req->q, req);
1691
1692 /*
1693 * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
1694 * sense key with us all the way through
1695 */
1696 if (!blk_pc_request(req))
1697 req->errors = 0;
1698
1699 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1700 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1701 req->rq_disk ? req->rq_disk->disk_name : "?",
1702 (unsigned long long)req->sector);
1703 }
1704
1705 if (blk_fs_request(req) && req->rq_disk) {
1706 const int rw = rq_data_dir(req);
1707 struct hd_struct *part;
1708 int cpu;
1709
1710 cpu = part_stat_lock();
1711 part = disk_map_sector_rcu(req->rq_disk, req->sector);
1712 part_stat_add(cpu, part, sectors[rw], nr_bytes >> 9);
1713 part_stat_unlock();
1714 }
1715
1716 total_bytes = bio_nbytes = 0;
1717 while ((bio = req->bio) != NULL) {
1718 int nbytes;
1719
1720 /*
1721 * For an empty barrier request, the low level driver must
1722 * store a potential error location in ->sector. We pass
1723 * that back up in ->bi_sector.
1724 */
1725 if (blk_empty_barrier(req))
1726 bio->bi_sector = req->sector;
1727
1728 if (nr_bytes >= bio->bi_size) {
1729 req->bio = bio->bi_next;
1730 nbytes = bio->bi_size;
1731 req_bio_endio(req, bio, nbytes, error);
1732 next_idx = 0;
1733 bio_nbytes = 0;
1734 } else {
1735 int idx = bio->bi_idx + next_idx;
1736
1737 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1738 blk_dump_rq_flags(req, "__end_that");
1739 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1740 __func__, bio->bi_idx, bio->bi_vcnt);
1741 break;
1742 }
1743
1744 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1745 BIO_BUG_ON(nbytes > bio->bi_size);
1746
1747 /*
1748 * not a complete bvec done
1749 */
1750 if (unlikely(nbytes > nr_bytes)) {
1751 bio_nbytes += nr_bytes;
1752 total_bytes += nr_bytes;
1753 break;
1754 }
1755
1756 /*
1757 * advance to the next vector
1758 */
1759 next_idx++;
1760 bio_nbytes += nbytes;
1761 }
1762
1763 total_bytes += nbytes;
1764 nr_bytes -= nbytes;
1765
1766 bio = req->bio;
1767 if (bio) {
1768 /*
1769 * end more in this run, or just return 'not-done'
1770 */
1771 if (unlikely(nr_bytes <= 0))
1772 break;
1773 }
1774 }
1775
1776 /*
1777 * completely done
1778 */
1779 if (!req->bio)
1780 return 0;
1781
1782 /*
1783 * if the request wasn't completed, update state
1784 */
1785 if (bio_nbytes) {
1786 req_bio_endio(req, bio, bio_nbytes, error);
1787 bio->bi_idx += next_idx;
1788 bio_iovec(bio)->bv_offset += nr_bytes;
1789 bio_iovec(bio)->bv_len -= nr_bytes;
1790 }
1791
1792 blk_recalc_rq_sectors(req, total_bytes >> 9);
1793 blk_recalc_rq_segments(req);
1794 return 1;
1795 }
1796
1797 /*
1798 * queue lock must be held
1799 */
1800 static void end_that_request_last(struct request *req, int error)
1801 {
1802 struct gendisk *disk = req->rq_disk;
1803
1804 if (blk_rq_tagged(req))
1805 blk_queue_end_tag(req->q, req);
1806
1807 if (blk_queued_rq(req))
1808 elv_dequeue_request(req->q, req);
1809
1810 if (unlikely(laptop_mode) && blk_fs_request(req))
1811 laptop_io_completion();
1812
1813 blk_delete_timer(req);
1814
1815 /*
1816 * Account IO completion. bar_rq isn't accounted as a normal
1817 * IO on queueing nor completion. Accounting the containing
1818 * request is enough.
1819 */
1820 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
1821 unsigned long duration = jiffies - req->start_time;
1822 const int rw = rq_data_dir(req);
1823 struct hd_struct *part;
1824 int cpu;
1825
1826 cpu = part_stat_lock();
1827 part = disk_map_sector_rcu(disk, req->sector);
1828
1829 part_stat_inc(cpu, part, ios[rw]);
1830 part_stat_add(cpu, part, ticks[rw], duration);
1831 part_round_stats(cpu, part);
1832 part_dec_in_flight(part);
1833
1834 part_stat_unlock();
1835 }
1836
1837 if (req->end_io)
1838 req->end_io(req, error);
1839 else {
1840 if (blk_bidi_rq(req))
1841 __blk_put_request(req->next_rq->q, req->next_rq);
1842
1843 __blk_put_request(req->q, req);
1844 }
1845 }
1846
1847 /**
1848 * blk_rq_bytes - Returns bytes left to complete in the entire request
1849 * @rq: the request being processed
1850 **/
1851 unsigned int blk_rq_bytes(struct request *rq)
1852 {
1853 if (blk_fs_request(rq))
1854 return rq->hard_nr_sectors << 9;
1855
1856 return rq->data_len;
1857 }
1858 EXPORT_SYMBOL_GPL(blk_rq_bytes);
1859
1860 /**
1861 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1862 * @rq: the request being processed
1863 **/
1864 unsigned int blk_rq_cur_bytes(struct request *rq)
1865 {
1866 if (blk_fs_request(rq))
1867 return rq->current_nr_sectors << 9;
1868
1869 if (rq->bio)
1870 return rq->bio->bi_size;
1871
1872 return rq->data_len;
1873 }
1874 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
1875
1876 /**
1877 * end_request - end I/O on the current segment of the request
1878 * @req: the request being processed
1879 * @uptodate: error value or %0/%1 uptodate flag
1880 *
1881 * Description:
1882 * Ends I/O on the current segment of a request. If that is the only
1883 * remaining segment, the request is also completed and freed.
1884 *
1885 * This is a remnant of how older block drivers handled I/O completions.
1886 * Modern drivers typically end I/O on the full request in one go, unless
1887 * they have a residual value to account for. For that case this function
1888 * isn't really useful, unless the residual just happens to be the
1889 * full current segment. In other words, don't use this function in new
1890 * code. Use blk_end_request() or __blk_end_request() to end a request.
1891 **/
1892 void end_request(struct request *req, int uptodate)
1893 {
1894 int error = 0;
1895
1896 if (uptodate <= 0)
1897 error = uptodate ? uptodate : -EIO;
1898
1899 __blk_end_request(req, error, req->hard_cur_sectors << 9);
1900 }
1901 EXPORT_SYMBOL(end_request);
1902
1903 static int end_that_request_data(struct request *rq, int error,
1904 unsigned int nr_bytes, unsigned int bidi_bytes)
1905 {
1906 if (rq->bio) {
1907 if (__end_that_request_first(rq, error, nr_bytes))
1908 return 1;
1909
1910 /* Bidi request must be completed as a whole */
1911 if (blk_bidi_rq(rq) &&
1912 __end_that_request_first(rq->next_rq, error, bidi_bytes))
1913 return 1;
1914 }
1915
1916 return 0;
1917 }
1918
1919 /**
1920 * blk_end_io - Generic end_io function to complete a request.
1921 * @rq: the request being processed
1922 * @error: %0 for success, < %0 for error
1923 * @nr_bytes: number of bytes to complete @rq
1924 * @bidi_bytes: number of bytes to complete @rq->next_rq
1925 * @drv_callback: function called between completion of bios in the request
1926 * and completion of the request.
1927 * If the callback returns non %0, this helper returns without
1928 * completion of the request.
1929 *
1930 * Description:
1931 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1932 * If @rq has leftover, sets it up for the next range of segments.
1933 *
1934 * Return:
1935 * %0 - we are done with this request
1936 * %1 - this request is not freed yet, it still has pending buffers.
1937 **/
1938 static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1939 unsigned int bidi_bytes,
1940 int (drv_callback)(struct request *))
1941 {
1942 struct request_queue *q = rq->q;
1943 unsigned long flags = 0UL;
1944
1945 if (end_that_request_data(rq, error, nr_bytes, bidi_bytes))
1946 return 1;
1947
1948 /* Special feature for tricky drivers */
1949 if (drv_callback && drv_callback(rq))
1950 return 1;
1951
1952 add_disk_randomness(rq->rq_disk);
1953
1954 spin_lock_irqsave(q->queue_lock, flags);
1955 end_that_request_last(rq, error);
1956 spin_unlock_irqrestore(q->queue_lock, flags);
1957
1958 return 0;
1959 }
1960
1961 /**
1962 * blk_end_request - Helper function for drivers to complete the request.
1963 * @rq: the request being processed
1964 * @error: %0 for success, < %0 for error
1965 * @nr_bytes: number of bytes to complete
1966 *
1967 * Description:
1968 * Ends I/O on a number of bytes attached to @rq.
1969 * If @rq has leftover, sets it up for the next range of segments.
1970 *
1971 * Return:
1972 * %0 - we are done with this request
1973 * %1 - still buffers pending for this request
1974 **/
1975 int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1976 {
1977 return blk_end_io(rq, error, nr_bytes, 0, NULL);
1978 }
1979 EXPORT_SYMBOL_GPL(blk_end_request);
1980
1981 /**
1982 * __blk_end_request - Helper function for drivers to complete the request.
1983 * @rq: the request being processed
1984 * @error: %0 for success, < %0 for error
1985 * @nr_bytes: number of bytes to complete
1986 *
1987 * Description:
1988 * Must be called with queue lock held unlike blk_end_request().
1989 *
1990 * Return:
1991 * %0 - we are done with this request
1992 * %1 - still buffers pending for this request
1993 **/
1994 int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1995 {
1996 if (rq->bio && __end_that_request_first(rq, error, nr_bytes))
1997 return 1;
1998
1999 add_disk_randomness(rq->rq_disk);
2000
2001 end_that_request_last(rq, error);
2002
2003 return 0;
2004 }
2005 EXPORT_SYMBOL_GPL(__blk_end_request);
2006
2007 /**
2008 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
2009 * @rq: the bidi request being processed
2010 * @error: %0 for success, < %0 for error
2011 * @nr_bytes: number of bytes to complete @rq
2012 * @bidi_bytes: number of bytes to complete @rq->next_rq
2013 *
2014 * Description:
2015 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2016 *
2017 * Return:
2018 * %0 - we are done with this request
2019 * %1 - still buffers pending for this request
2020 **/
2021 int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
2022 unsigned int bidi_bytes)
2023 {
2024 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
2025 }
2026 EXPORT_SYMBOL_GPL(blk_end_bidi_request);
2027
2028 /**
2029 * blk_update_request - Special helper function for request stacking drivers
2030 * @rq: the request being processed
2031 * @error: %0 for success, < %0 for error
2032 * @nr_bytes: number of bytes to complete @rq
2033 *
2034 * Description:
2035 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
2036 * the request structure even if @rq doesn't have leftover.
2037 * If @rq has leftover, sets it up for the next range of segments.
2038 *
2039 * This special helper function is only for request stacking drivers
2040 * (e.g. request-based dm) so that they can handle partial completion.
2041 * Actual device drivers should use blk_end_request instead.
2042 */
2043 void blk_update_request(struct request *rq, int error, unsigned int nr_bytes)
2044 {
2045 if (!end_that_request_data(rq, error, nr_bytes, 0)) {
2046 /*
2047 * These members are not updated in end_that_request_data()
2048 * when all bios are completed.
2049 * Update them so that the request stacking driver can find
2050 * how many bytes remain in the request later.
2051 */
2052 rq->nr_sectors = rq->hard_nr_sectors = 0;
2053 rq->current_nr_sectors = rq->hard_cur_sectors = 0;
2054 }
2055 }
2056 EXPORT_SYMBOL_GPL(blk_update_request);
2057
2058 /**
2059 * blk_end_request_callback - Special helper function for tricky drivers
2060 * @rq: the request being processed
2061 * @error: %0 for success, < %0 for error
2062 * @nr_bytes: number of bytes to complete
2063 * @drv_callback: function called between completion of bios in the request
2064 * and completion of the request.
2065 * If the callback returns non %0, this helper returns without
2066 * completion of the request.
2067 *
2068 * Description:
2069 * Ends I/O on a number of bytes attached to @rq.
2070 * If @rq has leftover, sets it up for the next range of segments.
2071 *
2072 * This special helper function is used only for existing tricky drivers.
2073 * (e.g. cdrom_newpc_intr() of ide-cd)
2074 * This interface will be removed when such drivers are rewritten.
2075 * Don't use this interface in other places anymore.
2076 *
2077 * Return:
2078 * %0 - we are done with this request
2079 * %1 - this request is not freed yet.
2080 * this request still has pending buffers or
2081 * the driver doesn't want to finish this request yet.
2082 **/
2083 int blk_end_request_callback(struct request *rq, int error,
2084 unsigned int nr_bytes,
2085 int (drv_callback)(struct request *))
2086 {
2087 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
2088 }
2089 EXPORT_SYMBOL_GPL(blk_end_request_callback);
2090
2091 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2092 struct bio *bio)
2093 {
2094 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2095 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
2096 rq->cmd_flags |= (bio->bi_rw & 3);
2097
2098 if (bio_has_data(bio)) {
2099 rq->nr_phys_segments = bio_phys_segments(q, bio);
2100 rq->buffer = bio_data(bio);
2101 }
2102 rq->current_nr_sectors = bio_cur_sectors(bio);
2103 rq->hard_cur_sectors = rq->current_nr_sectors;
2104 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
2105 rq->data_len = bio->bi_size;
2106
2107 rq->bio = rq->biotail = bio;
2108
2109 if (bio->bi_bdev)
2110 rq->rq_disk = bio->bi_bdev->bd_disk;
2111 }
2112
2113 /**
2114 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2115 * @q : the queue of the device being checked
2116 *
2117 * Description:
2118 * Check if underlying low-level drivers of a device are busy.
2119 * If the drivers want to export their busy state, they must set own
2120 * exporting function using blk_queue_lld_busy() first.
2121 *
2122 * Basically, this function is used only by request stacking drivers
2123 * to stop dispatching requests to underlying devices when underlying
2124 * devices are busy. This behavior helps more I/O merging on the queue
2125 * of the request stacking driver and prevents I/O throughput regression
2126 * on burst I/O load.
2127 *
2128 * Return:
2129 * 0 - Not busy (The request stacking driver should dispatch request)
2130 * 1 - Busy (The request stacking driver should stop dispatching request)
2131 */
2132 int blk_lld_busy(struct request_queue *q)
2133 {
2134 if (q->lld_busy_fn)
2135 return q->lld_busy_fn(q);
2136
2137 return 0;
2138 }
2139 EXPORT_SYMBOL_GPL(blk_lld_busy);
2140
2141 int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
2142 {
2143 return queue_work(kblockd_workqueue, work);
2144 }
2145 EXPORT_SYMBOL(kblockd_schedule_work);
2146
2147 void kblockd_flush_work(struct work_struct *work)
2148 {
2149 cancel_work_sync(work);
2150 }
2151 EXPORT_SYMBOL(kblockd_flush_work);
2152
2153 int __init blk_dev_init(void)
2154 {
2155 kblockd_workqueue = create_workqueue("kblockd");
2156 if (!kblockd_workqueue)
2157 panic("Failed to create kblockd\n");
2158
2159 request_cachep = kmem_cache_create("blkdev_requests",
2160 sizeof(struct request), 0, SLAB_PANIC, NULL);
2161
2162 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2163 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
2164
2165 return 0;
2166 }
2167
This page took 0.105537 seconds and 4 git commands to generate.