ll_rw_blk.c 65.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8
/*
 *  linux/drivers/block/ll_rw_blk.c
 *
 * Copyright (C) 1991, 1992 Linus Torvalds
 * Copyright (C) 1994,      Karl Keyte: Added support for disk statistics
 * Elevator latency, (C) 2000  Andrea Arcangeli <andrea@suse.de> SuSE
 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> -  July2000
Linus Torvalds's avatar
Linus Torvalds committed
9
 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14 15
 */

/*
 * This handles all read/write requests to block devices
 */
#include <linux/config.h>
16 17 18
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/backing-dev.h>
19
#include <linux/bio.h>
20 21
#include <linux/blk.h>
#include <linux/highmem.h>
Linus Torvalds's avatar
Linus Torvalds committed
22
#include <linux/mm.h>
23 24
#include <linux/kernel_stat.h>
#include <linux/string.h>
Linus Torvalds's avatar
Linus Torvalds committed
25
#include <linux/init.h>
26
#include <linux/bootmem.h>	/* for max_pfn/max_low_pfn */
Linus Torvalds's avatar
Linus Torvalds committed
27
#include <linux/completion.h>
Linus Torvalds's avatar
Linus Torvalds committed
28
#include <linux/slab.h>
29
#include <linux/swap.h>
Linus Torvalds's avatar
Linus Torvalds committed
30

31
static void blk_unplug_work(void *data);
32
static void blk_unplug_timeout(unsigned long data);
33

Linus Torvalds's avatar
Linus Torvalds committed
34 35 36 37 38
/*
 * For the allocated request tables
 */
static kmem_cache_t *request_cachep;

Jens Axboe's avatar
Jens Axboe committed
39 40 41
/*
 * plug management
 */
Andrew Morton's avatar
Andrew Morton committed
42
static LIST_HEAD(blk_plug_list);
43
static spinlock_t blk_plug_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
44

45
static wait_queue_head_t congestion_wqh[2];
46

47 48 49 50 51 52 53
/*
 * Controlling structure to kblockd
 */
static struct workqueue_struct *kblockd_workqueue; 

unsigned long blk_max_low_pfn, blk_max_pfn;

54
static inline int batch_requests(struct request_queue *q)
55
{
56
	return q->nr_requests - min(q->nr_requests / 8, 8UL);
57 58
}

59
/*
60
 * Return the threshold (number of used requests) at which the queue is
61 62 63
 * considered to be congested.  It include a little hysteresis to keep the
 * context switch rate down.
 */
64
static inline int queue_congestion_on_threshold(struct request_queue *q)
65 66 67
{
	int ret;

68 69 70 71 72
	ret = q->nr_requests - (q->nr_requests / 8) + 1;

	if (ret > q->nr_requests)
		ret = q->nr_requests;

73 74 75 76 77 78
	return ret;
}

/*
 * The threshold at which a queue is considered to be uncongested
 */
79
static inline int queue_congestion_off_threshold(struct request_queue *q)
80 81 82
{
	int ret;

83 84 85 86 87
	ret = q->nr_requests - (q->nr_requests / 8) - 1;

	if (ret < 1)
		ret = 1;

88 89 90
	return ret;
}

91 92 93 94 95
/*
 * A queue has just exitted congestion.  Note this in the global counter of
 * congested queues, and wake up anyone who was waiting for requests to be
 * put back.
 */
96 97 98
static void clear_queue_congested(request_queue_t *q, int rw)
{
	enum bdi_state bit;
99
	wait_queue_head_t *wqh = &congestion_wqh[rw];
100 101

	bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
102 103 104
	clear_bit(bit, &q->backing_dev_info.state);
	if (waitqueue_active(wqh))
		wake_up(wqh);
105 106
}

107 108 109 110
/*
 * A queue has just entered congestion.  Flag that in the queue's VM-visible
 * state flags and increment the global gounter of congested queues.
 */
111 112 113 114 115
static void set_queue_congested(request_queue_t *q, int rw)
{
	enum bdi_state bit;

	bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
116
	set_bit(bit, &q->backing_dev_info.state);
117 118
}

Andrew Morton's avatar
Andrew Morton committed
119
/**
120
 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
Andrew Morton's avatar
Andrew Morton committed
121 122
 * @dev:	device
 *
Andrew Morton's avatar
Andrew Morton committed
123
 * Locates the passed device's request queue and returns the address of its
124
 * backing_dev_info
Andrew Morton's avatar
Andrew Morton committed
125
 *
Andrew Morton's avatar
Andrew Morton committed
126
 * Will return NULL if the request queue cannot be located.
Andrew Morton's avatar
Andrew Morton committed
127
 */
128
struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
Andrew Morton's avatar
Andrew Morton committed
129
{
130
	struct backing_dev_info *ret = NULL;
131
	request_queue_t *q = bdev_get_queue(bdev);
Andrew Morton's avatar
Andrew Morton committed
132

Andrew Morton's avatar
Andrew Morton committed
133
	if (q)
134
		ret = &q->backing_dev_info;
Andrew Morton's avatar
Andrew Morton committed
135 136 137
	return ret;
}

138 139 140 141 142 143 144 145 146 147 148
/**
 * blk_queue_prep_rq - set a prepare_request function for queue
 * @q:		queue
 * @pfn:	prepare_request function
 *
 * It's possible for a queue to register a prepare_request callback which
 * is invoked before the request is handed to the request_fn. The goal of
 * the function is to prepare a request for I/O, it can be used to build a
 * cdb from the request data for instance.
 *
 */
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152 153
void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn)
{
	q->prep_rq_fn = pfn;
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/**
 * blk_queue_merge_bvec - set a merge_bvec function for queue
 * @q:		queue
 * @mbfn:	merge_bvec_fn
 *
 * Usually queues have static limitations on the max sectors or segments that
 * we can put in a request. Stacking drivers may have some settings that
 * are dynamic, and thus we have to query the queue whether it is ok to
 * add a new bio_vec to a bio at a given offset or not. If the block device
 * has such limitations, it needs to register a merge_bvec_fn to control
 * the size of bio's sent to it. Per default now merge_bvec_fn is defined for
 * a queue, and only the fixed limits are honored.
 *
 */
void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn)
{
	q->merge_bvec_fn = mbfn;
}

Linus Torvalds's avatar
Linus Torvalds committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
 * blk_queue_make_request - define an alternate make_request function for a device
 * @q:  the request queue for the device to be affected
 * @mfn: the alternate make_request function
 *
 * Description:
 *    The normal way for &struct bios to be passed to a device
 *    driver is for them to be collected into requests on a request
 *    queue, and then to allow the device driver to select requests
 *    off that queue when it is ready.  This works well for many block
 *    devices. However some block devices (typically virtual devices
 *    such as md or lvm) do not benefit from the processing on the
 *    request queue, and are served best by having the requests passed
 *    directly to them.  This can be achieved by providing a function
 *    to blk_queue_make_request().
 *
 * Caveat:
 *    The driver that does this *must* be able to deal appropriately
 *    with buffers in "highmemory". This can be accomplished by either calling
Andrew Morton's avatar
Andrew Morton committed
192
 *    __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
Linus Torvalds's avatar
Linus Torvalds committed
193 194 195
 *    blk_queue_bounce() to create a buffer in normal memory.
 **/
void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
Linus Torvalds's avatar
Linus Torvalds committed
196
{
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199
	/*
	 * set defaults
	 */
200
	q->nr_requests = BLKDEV_MAX_RQ;
Linus Torvalds's avatar
Linus Torvalds committed
201 202
	q->max_phys_segments = MAX_PHYS_SEGMENTS;
	q->max_hw_segments = MAX_HW_SEGMENTS;
Linus Torvalds's avatar
Linus Torvalds committed
203
	q->make_request_fn = mfn;
204 205
	q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
	q->backing_dev_info.state = 0;
206
	q->backing_dev_info.memory_backed = 0;
Linus Torvalds's avatar
Linus Torvalds committed
207 208
	blk_queue_max_sectors(q, MAX_SECTORS);
	blk_queue_hardsect_size(q, 512);
Jens Axboe's avatar
Jens Axboe committed
209
	blk_queue_dma_alignment(q, 511);
Linus Torvalds's avatar
Linus Torvalds committed
210

211 212 213 214 215 216 217 218
	q->unplug_thresh = 4;		/* hmm */
	q->unplug_delay = (3 * HZ) / 1000;	/* 3 milliseconds */
	if (q->unplug_delay == 0)
		q->unplug_delay = 1;

	init_timer(&q->unplug_timer);
	INIT_WORK(&q->unplug_work, blk_unplug_work, q);

219 220 221
	q->unplug_timer.function = blk_unplug_timeout;
	q->unplug_timer.data = (unsigned long)q;

Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225 226
	/*
	 * by default assume old behaviour and bounce for any highmem page
	 */
	blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);

Linus Torvalds's avatar
Linus Torvalds committed
227
	init_waitqueue_head(&q->queue_wait);
Neil Brown's avatar
Neil Brown committed
228
	INIT_LIST_HEAD(&q->plug_list);
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231
}

/**
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234
 * blk_queue_bounce_limit - set bounce buffer limit for queue
 * @q:  the request queue for the device
 * @dma_addr:   bus address limit
Linus Torvalds's avatar
Linus Torvalds committed
235 236
 *
 * Description:
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239 240 241
 *    Different hardware can have different requirements as to what pages
 *    it can do I/O directly to. A low level driver can call
 *    blk_queue_bounce_limit to have lower memory pages allocated as bounce
 *    buffers for doing I/O to pages residing above @page. By default
 *    the block layer sets this to the highest numbered "low" memory page.
Linus Torvalds's avatar
Linus Torvalds committed
242
 **/
Linus Torvalds's avatar
Linus Torvalds committed
243
void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
Linus Torvalds's avatar
Linus Torvalds committed
244
{
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247
	unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
	unsigned long mb = dma_addr >> 20;
	static request_queue_t *last_q;
Linus Torvalds's avatar
Linus Torvalds committed
248

Linus Torvalds's avatar
Linus Torvalds committed
249 250 251 252 253
	/*
	 * set appropriate bounce gfp mask -- unfortunately we don't have a
	 * full 4GB zone, so we have to resort to low memory for any bounces.
	 * ISA has its own < 16MB zone.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
254 255
	if (bounce_pfn < blk_max_low_pfn) {
		BUG_ON(dma_addr < BLK_BOUNCE_ISA);
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258
		init_emergency_isa_pool();
		q->bounce_gfp = GFP_NOIO | GFP_DMA;
	} else
Andrew Morton's avatar
Andrew Morton committed
259
		q->bounce_gfp = GFP_NOIO;
Linus Torvalds's avatar
Linus Torvalds committed
260

Linus Torvalds's avatar
Linus Torvalds committed
261 262 263 264 265 266 267 268
	/*
	 * keep this for debugging for now...
	 */
	if (dma_addr != BLK_BOUNCE_HIGH && q != last_q) {
		printk("blk: queue %p, ", q);
		if (dma_addr == BLK_BOUNCE_ANY)
			printk("no I/O memory limit\n");
		else
Linus Torvalds's avatar
Linus Torvalds committed
269
			printk("I/O limit %luMb (mask 0x%Lx)\n", mb, (long long) dma_addr);
Linus Torvalds's avatar
Linus Torvalds committed
270
	}
Linus Torvalds's avatar
Linus Torvalds committed
271

Linus Torvalds's avatar
Linus Torvalds committed
272 273
	q->bounce_pfn = bounce_pfn;
	last_q = q;
Linus Torvalds's avatar
Linus Torvalds committed
274 275
}

Linus Torvalds's avatar
Linus Torvalds committed
276

Linus Torvalds's avatar
Linus Torvalds committed
277
/**
Linus Torvalds's avatar
Linus Torvalds committed
278 279 280
 * blk_queue_max_sectors - set max sectors for a request for this queue
 * @q:  the request queue for the device
 * @max_sectors:  max sectors in the usual 512b unit
Linus Torvalds's avatar
Linus Torvalds committed
281 282
 *
 * Description:
Linus Torvalds's avatar
Linus Torvalds committed
283 284 285 286 287
 *    Enables a low level driver to set an upper limit on the size of
 *    received requests.
 **/
void blk_queue_max_sectors(request_queue_t *q, unsigned short max_sectors)
{
288 289 290 291 292
	if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
		max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
		printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
	}

Linus Torvalds's avatar
Linus Torvalds committed
293 294 295 296
	q->max_sectors = max_sectors;
}

/**
Linus Torvalds's avatar
Linus Torvalds committed
297
 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
Linus Torvalds's avatar
Linus Torvalds committed
298 299
 * @q:  the request queue for the device
 * @max_segments:  max number of segments
Linus Torvalds's avatar
Linus Torvalds committed
300
 *
Linus Torvalds's avatar
Linus Torvalds committed
301 302
 * Description:
 *    Enables a low level driver to set an upper limit on the number of
Linus Torvalds's avatar
Linus Torvalds committed
303 304
 *    physical data segments in a request.  This would be the largest sized
 *    scatter list the driver could handle.
Linus Torvalds's avatar
Linus Torvalds committed
305
 **/
Linus Torvalds's avatar
Linus Torvalds committed
306
void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
Linus Torvalds's avatar
Linus Torvalds committed
307
{
308 309 310 311 312
	if (!max_segments) {
		max_segments = 1;
		printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
	}

Linus Torvalds's avatar
Linus Torvalds committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	q->max_phys_segments = max_segments;
}

/**
 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
 * @q:  the request queue for the device
 * @max_segments:  max number of segments
 *
 * Description:
 *    Enables a low level driver to set an upper limit on the number of
 *    hw data segments in a request.  This would be the largest number of
 *    address/length pairs the host adapter can actually give as once
 *    to the device.
 **/
void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments)
{
329 330 331 332 333
	if (!max_segments) {
		max_segments = 1;
		printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
	}

Linus Torvalds's avatar
Linus Torvalds committed
334
	q->max_hw_segments = max_segments;
Linus Torvalds's avatar
Linus Torvalds committed
335 336 337
}

/**
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340
 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
 * @q:  the request queue for the device
 * @max_size:  max size of segment in bytes
Linus Torvalds's avatar
Linus Torvalds committed
341 342
 *
 * Description:
Linus Torvalds's avatar
Linus Torvalds committed
343 344 345 346 347
 *    Enables a low level driver to set an upper limit on the size of a
 *    coalesced segment
 **/
void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size)
{
348 349 350 351 352
	if (max_size < PAGE_CACHE_SIZE) {
		max_size = PAGE_CACHE_SIZE;
		printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
	}

Linus Torvalds's avatar
Linus Torvalds committed
353 354 355 356 357 358 359
	q->max_segment_size = max_size;
}

/**
 * blk_queue_hardsect_size - set hardware sector size for the queue
 * @q:  the request queue for the device
 * @size:  the hardware sector size, in bytes
Linus Torvalds's avatar
Linus Torvalds committed
360
 *
Linus Torvalds's avatar
Linus Torvalds committed
361 362 363 364 365
 * Description:
 *   This should typically be set to the lowest possible sector size
 *   that the hardware can operate on (possible without reverting to
 *   even internal read-modify-write operations). Usually the default
 *   of 512 covers most hardware.
Linus Torvalds's avatar
Linus Torvalds committed
366
 **/
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370
void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
{
	q->hardsect_size = size;
}
Linus Torvalds's avatar
Linus Torvalds committed
371

Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378
/**
 * blk_queue_segment_boundary - set boundary rules for segment merging
 * @q:  the request queue for the device
 * @mask:  the memory boundary mask
 **/
void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask)
{
379 380 381 382 383
	if (mask < PAGE_CACHE_SIZE - 1) {
		mask = PAGE_CACHE_SIZE - 1;
		printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
	}

Linus Torvalds's avatar
Linus Torvalds committed
384 385 386
	q->seg_boundary_mask = mask;
}

Jens Axboe's avatar
Jens Axboe committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
/**
 * blk_queue_dma_alignment - set dma length and memory alignment
 * @q:  the request queue for the device
 * @dma_mask:  alignment mask
 *
 * description:
 *    set required memory and length aligment for direct dma transactions.
 *    this is used when buiding direct io requests for the queue.
 *
 **/
void blk_queue_dma_alignment(request_queue_t *q, int mask)
{
	q->dma_alignment = mask;
}

Jens Axboe's avatar
Jens Axboe committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/**
 * blk_queue_find_tag - find a request by its tag and queue
 *
 * @q:	 The request queue for the device
 * @tag: The tag of the request
 *
 * Notes:
 *    Should be used when a device returns a tag and you want to match
 *    it with a request.
 *
 *    no locks need be held.
 **/
struct request *blk_queue_find_tag(request_queue_t *q, int tag)
{
	struct blk_queue_tag *bqt = q->queue_tags;

Jens Axboe's avatar
Jens Axboe committed
418
	if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
Jens Axboe's avatar
Jens Axboe committed
419 420 421 422
		return NULL;

	return bqt->tag_index[tag];
}
Jens Axboe's avatar
Jens Axboe committed
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/**
 * blk_queue_free_tags - release tag maintenance info
 * @q:  the request queue for the device
 *
 *  Notes:
 *    blk_cleanup_queue() will take care of calling this function, if tagging
 *    has been used. So there's usually no need to call this directly, unless
 *    tagging is just being disabled but the queue remains in function.
 **/
void blk_queue_free_tags(request_queue_t *q)
{
	struct blk_queue_tag *bqt = q->queue_tags;

	if (!bqt)
		return;

	BUG_ON(bqt->busy);
	BUG_ON(!list_empty(&bqt->busy_list));

	kfree(bqt->tag_index);
	bqt->tag_index = NULL;

	kfree(bqt->tag_map);
	bqt->tag_map = NULL;

	kfree(bqt);
	q->queue_tags = NULL;
	q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED);
}

454 455
static int
init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
456 457 458
{
	int bits, i;

459 460 461 462
	if (depth > q->nr_requests * 2) {
		depth = q->nr_requests * 2;
		printk(KERN_ERR "%s: adjusted depth to %d\n",
				__FUNCTION__, depth);
463
	}
464 465 466

	tags->tag_index = kmalloc(depth * sizeof(struct request *), GFP_ATOMIC);
	if (!tags->tag_index)
Jens Axboe's avatar
Jens Axboe committed
467
		goto fail;
468 469 470 471

	bits = (depth / BLK_TAGS_PER_LONG) + 1;
	tags->tag_map = kmalloc(bits * sizeof(unsigned long), GFP_ATOMIC);
	if (!tags->tag_map)
Jens Axboe's avatar
Jens Axboe committed
472
		goto fail;
473

Jens Axboe's avatar
Jens Axboe committed
474 475
	memset(tags->tag_index, 0, depth * sizeof(struct request *));
	memset(tags->tag_map, 0, bits * sizeof(unsigned long));
476
	tags->max_depth = depth;
Jens Axboe's avatar
Jens Axboe committed
477
	tags->real_max_depth = bits * BITS_PER_LONG;
478 479 480 481 482

	/*
	 * set the upper bits if the depth isn't a multiple of the word size
	 */
	for (i = depth; i < bits * BLK_TAGS_PER_LONG; i++)
483
		__set_bit(i, tags->tag_map);
484

Jens Axboe's avatar
Jens Axboe committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	return 0;
fail:
	kfree(tags->tag_index);
	return -ENOMEM;
}

/**
 * blk_queue_init_tags - initialize the queue tag info
 * @q:  the request queue for the device
 * @depth:  the maximum queue depth supported
 **/
int blk_queue_init_tags(request_queue_t *q, int depth)
{
	struct blk_queue_tag *tags;

	tags = kmalloc(sizeof(struct blk_queue_tag),GFP_ATOMIC);
	if (!tags)
		goto fail;

504
	if (init_tag_map(q, tags, depth))
Jens Axboe's avatar
Jens Axboe committed
505 506 507 508 509
		goto fail;

	INIT_LIST_HEAD(&tags->busy_list);
	tags->busy = 0;

510 511 512 513 514 515 516
	/*
	 * assign it, all done
	 */
	q->queue_tags = tags;
	q->queue_flags |= (1 << QUEUE_FLAG_QUEUED);
	return 0;
fail:
Jens Axboe's avatar
Jens Axboe committed
517
	kfree(tags);
518 519 520
	return -ENOMEM;
}

Jens Axboe's avatar
Jens Axboe committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
/**
 * blk_queue_resize_tags - change the queueing depth
 * @q:  the request queue for the device
 * @new_depth: the new max command queueing depth
 *
 *  Notes:
 *    Must be called with the queue lock held.
 **/
int blk_queue_resize_tags(request_queue_t *q, int new_depth)
{
	struct blk_queue_tag *bqt = q->queue_tags;
	struct request **tag_index;
	unsigned long *tag_map;
	int bits, max_depth;

	if (!bqt)
		return -ENXIO;

	/*
	 * don't bother sizing down
	 */
	if (new_depth <= bqt->real_max_depth) {
		bqt->max_depth = new_depth;
		return 0;
	}

	/*
	 * save the old state info, so we can copy it back
	 */
	tag_index = bqt->tag_index;
	tag_map = bqt->tag_map;
	max_depth = bqt->real_max_depth;

554
	if (init_tag_map(q, bqt, new_depth))
Jens Axboe's avatar
Jens Axboe committed
555 556 557 558
		return -ENOMEM;

	memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
	bits = max_depth / BLK_TAGS_PER_LONG;
Jens Axboe's avatar
Jens Axboe committed
559
	memcpy(bqt->tag_map, tag_map, bits * sizeof(unsigned long));
Jens Axboe's avatar
Jens Axboe committed
560 561 562 563 564 565

	kfree(tag_index);
	kfree(tag_map);
	return 0;
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
/**
 * blk_queue_end_tag - end tag operations for a request
 * @q:  the request queue for the device
 * @tag:  the tag that has completed
 *
 *  Description:
 *    Typically called when end_that_request_first() returns 0, meaning
 *    all transfers have been done for a request. It's important to call
 *    this function before end_that_request_last(), as that will put the
 *    request back on the free list thus corrupting the internal tag list.
 *
 *  Notes:
 *   queue lock must be held.
 **/
void blk_queue_end_tag(request_queue_t *q, struct request *rq)
{
	struct blk_queue_tag *bqt = q->queue_tags;
	int tag = rq->tag;

	BUG_ON(tag == -1);

Jens Axboe's avatar
Jens Axboe committed
587
	if (unlikely(tag >= bqt->real_max_depth))
588 589 590 591 592 593 594
		return;

	if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) {
		printk("attempt to clear non-busy tag (%d)\n", tag);
		return;
	}

595
	list_del_init(&rq->queuelist);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	rq->flags &= ~REQ_QUEUED;
	rq->tag = -1;

	if (unlikely(bqt->tag_index[tag] == NULL))
		printk("tag %d is missing\n", tag);

	bqt->tag_index[tag] = NULL;
	bqt->busy--;
}

/**
 * blk_queue_start_tag - find a free tag and assign it
 * @q:  the request queue for the device
 * @rq:  the block request that needs tagging
 *
 *  Description:
 *    This can either be used as a stand-alone helper, or possibly be
 *    assigned as the queue &prep_rq_fn (in which case &struct request
Jens Axboe's avatar
Jens Axboe committed
614 615 616 617 618 619
 *    automagically gets a tag assigned). Note that this function
 *    assumes that any type of request can be queued! if this is not
 *    true for your device, you must check the request type before
 *    calling this function.  The request will also be removed from
 *    the request queue, so it's the drivers responsibility to readd
 *    it if it should need to be restarted for some reason.
620 621 622 623 624 625 626 627 628 629
 *
 *  Notes:
 *   queue lock must be held.
 **/
int blk_queue_start_tag(request_queue_t *q, struct request *rq)
{
	struct blk_queue_tag *bqt = q->queue_tags;
	unsigned long *map = bqt->tag_map;
	int tag = 0;

Jens Axboe's avatar
Jens Axboe committed
630 631
	if (unlikely((rq->flags & REQ_QUEUED))) {
		printk(KERN_ERR 
632 633
		       "request %p for device [%s] already tagged %d",
		       rq, rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
Jens Axboe's avatar
Jens Axboe committed
634 635
		BUG();
	}
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

	for (map = bqt->tag_map; *map == -1UL; map++) {
		tag += BLK_TAGS_PER_LONG;

		if (tag >= bqt->max_depth)
			return 1;
	}

	tag += ffz(*map);
	__set_bit(tag, bqt->tag_map);

	rq->flags |= REQ_QUEUED;
	rq->tag = tag;
	bqt->tag_index[tag] = rq;
	blkdev_dequeue_request(rq);
	list_add(&rq->queuelist, &bqt->busy_list);
	bqt->busy++;
	return 0;
}

/**
 * blk_queue_invalidate_tags - invalidate all pending tags
 * @q:  the request queue for the device
 *
 *  Description:
 *   Hardware conditions may dictate a need to stop all pending requests.
 *   In this case, we will safely clear the block side of the tag queue and
 *   readd all requests to the request queue in the right order.
 *
 *  Notes:
 *   queue lock must be held.
 **/
void blk_queue_invalidate_tags(request_queue_t *q)
{
	struct blk_queue_tag *bqt = q->queue_tags;
671
	struct list_head *tmp, *n;
672 673
	struct request *rq;

674
	list_for_each_safe(tmp, n, &bqt->busy_list) {
675 676
		rq = list_entry_rq(tmp);

677 678
		if (rq->tag == -1) {
			printk("bad tag found on list\n");
679
			list_del_init(&rq->queuelist);
Jens Axboe's avatar
Jens Axboe committed
680
			rq->flags &= ~REQ_QUEUED;
681 682 683
		} else
			blk_queue_end_tag(q, rq);

684
		rq->flags &= ~REQ_STARTED;
685
		__elv_add_request(q, rq, 0, 0);
686 687 688
	}
}

Martin Dalecki's avatar
Martin Dalecki committed
689 690 691
static char *rq_flags[] = {
	"REQ_RW",
	"REQ_RW_AHEAD",
Jens Axboe's avatar
Jens Axboe committed
692 693
	"REQ_SOFTBARRIER",
	"REQ_HARDBARRIER",
Martin Dalecki's avatar
Martin Dalecki committed
694 695 696 697 698 699 700 701
	"REQ_CMD",
	"REQ_NOMERGE",
	"REQ_STARTED",
	"REQ_DONTPREP",
	"REQ_QUEUED",
	"REQ_PC",
	"REQ_BLOCK_PC",
	"REQ_SENSE",
702 703
	"REQ_FAILED",
	"REQ_QUIET",
704
	"REQ_SPECIAL",
705 706 707
	"REQ_DRIVE_CMD",
	"REQ_DRIVE_TASK",
	"REQ_DRIVE_TASKFILE",
Martin Dalecki's avatar
Martin Dalecki committed
708
};
Linus Torvalds's avatar
Linus Torvalds committed
709 710 711 712 713

void blk_dump_rq_flags(struct request *rq, char *msg)
{
	int bit;

714 715
	printk("%s: dev %s: flags = ", msg,
		rq->rq_disk ? rq->rq_disk->disk_name : "?");
Linus Torvalds's avatar
Linus Torvalds committed
716 717 718 719 720 721 722
	bit = 0;
	do {
		if (rq->flags & (1 << bit))
			printk("%s ", rq_flags[bit]);
		bit++;
	} while (bit < __REQ_NR_BITS);

723
	printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
Linus Torvalds's avatar
Linus Torvalds committed
724 725
						       rq->nr_sectors,
						       rq->current_nr_sectors);
726 727 728 729 730 731 732 733
	printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);

	if (rq->flags & (REQ_BLOCK_PC | REQ_PC)) {
		printk("cdb: ");
		for (bit = 0; bit < sizeof(rq->cmd); bit++)
			printk("%02x ", rq->cmd[bit]);
		printk("\n");
	}
Linus Torvalds's avatar
Linus Torvalds committed
734 735
}

Linus Torvalds's avatar
Linus Torvalds committed
736 737 738
void blk_recount_segments(request_queue_t *q, struct bio *bio)
{
	struct bio_vec *bv, *bvprv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
739
	int i, nr_phys_segs, nr_hw_segs, seg_size, cluster;
Linus Torvalds's avatar
Linus Torvalds committed
740 741 742 743 744

	if (unlikely(!bio->bi_io_vec))
		return;

	cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
Linus Torvalds's avatar
Linus Torvalds committed
745
	seg_size = nr_phys_segs = nr_hw_segs = 0;
Linus Torvalds's avatar
Linus Torvalds committed
746 747
	bio_for_each_segment(bv, bio, i) {
		if (bvprv && cluster) {
748
			if (seg_size + bv->bv_len > q->max_segment_size)
Linus Torvalds's avatar
Linus Torvalds committed
749
				goto new_segment;
750
			if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
Linus Torvalds's avatar
Linus Torvalds committed
751
				goto new_segment;
752
			if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
Linus Torvalds's avatar
Linus Torvalds committed
753 754 755 756 757 758 759
				goto new_segment;

			seg_size += bv->bv_len;
			bvprv = bv;
			continue;
		}
new_segment:
760 761 762 763
		if (!bvprv || !BIOVEC_VIRT_MERGEABLE(bvprv, bv))
			nr_hw_segs++;

		nr_phys_segs++;
Linus Torvalds's avatar
Linus Torvalds committed
764
		bvprv = bv;
Linus Torvalds's avatar
Linus Torvalds committed
765
		seg_size = bv->bv_len;
Linus Torvalds's avatar
Linus Torvalds committed
766 767
	}

Linus Torvalds's avatar
Linus Torvalds committed
768 769
	bio->bi_phys_segments = nr_phys_segs;
	bio->bi_hw_segments = nr_hw_segs;
Linus Torvalds's avatar
Linus Torvalds committed
770 771 772 773
	bio->bi_flags |= (1 << BIO_SEG_VALID);
}


774
int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds's avatar
Linus Torvalds committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
				   struct bio *nxt)
{
	if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
		return 0;

	if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
		return 0;
	if (bio->bi_size + nxt->bi_size > q->max_segment_size)
		return 0;

	/*
	 * bio and nxt are contigous in memory, check if the queue allows
	 * these two to be merged into one
	 */
	if (BIO_SEG_BOUNDARY(q, bio, nxt))
		return 1;

	return 0;
}

795
int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds's avatar
Linus Torvalds committed
796
				 struct bio *nxt)
Linus Torvalds's avatar
Linus Torvalds committed
797
{
Linus Torvalds's avatar
Linus Torvalds committed
798 799 800
	if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
801
	if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
Linus Torvalds's avatar
Linus Torvalds committed
802
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
803 804
	if (bio->bi_size + nxt->bi_size > q->max_segment_size)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
805 806

	/*
Linus Torvalds's avatar
Linus Torvalds committed
807 808
	 * bio and nxt are contigous in memory, check if the queue allows
	 * these two to be merged into one
Linus Torvalds's avatar
Linus Torvalds committed
809
	 */
Linus Torvalds's avatar
Linus Torvalds committed
810
	if (BIO_SEG_BOUNDARY(q, bio, nxt))
Linus Torvalds's avatar
Linus Torvalds committed
811 812 813
		return 1;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
814 815
}

Linus Torvalds's avatar
Linus Torvalds committed
816 817
/*
 * map a request to scatterlist, return number of sg entries setup. Caller
Linus Torvalds's avatar
Linus Torvalds committed
818
 * must make sure sg can hold rq->nr_phys_segments entries
Linus Torvalds's avatar
Linus Torvalds committed
819 820
 */
int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
Linus Torvalds's avatar
Linus Torvalds committed
821
{
Linus Torvalds's avatar
Linus Torvalds committed
822
	struct bio_vec *bvec, *bvprv;
Linus Torvalds's avatar
Linus Torvalds committed
823
	struct bio *bio;
Linus Torvalds's avatar
Linus Torvalds committed
824
	int nsegs, i, cluster;
Linus Torvalds's avatar
Linus Torvalds committed
825 826

	nsegs = 0;
Linus Torvalds's avatar
Linus Torvalds committed
827
	cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
Linus Torvalds's avatar
Linus Torvalds committed
828 829 830 831

	/*
	 * for each bio in rq
	 */
Linus Torvalds's avatar
Linus Torvalds committed
832
	bvprv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
833 834 835 836 837 838 839
	rq_for_each_bio(bio, rq) {
		/*
		 * for each segment in bio
		 */
		bio_for_each_segment(bvec, bio, i) {
			int nbytes = bvec->bv_len;

Linus Torvalds's avatar
Linus Torvalds committed
840
			if (bvprv && cluster) {
Linus Torvalds's avatar
Linus Torvalds committed
841
				if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
Linus Torvalds's avatar
Linus Torvalds committed
842 843
					goto new_segment;

Linus Torvalds's avatar
Linus Torvalds committed
844
				if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Linus Torvalds's avatar
Linus Torvalds committed
845 846 847
					goto new_segment;
				if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
					goto new_segment;
Linus Torvalds's avatar
Linus Torvalds committed
848 849 850 851

				sg[nsegs - 1].length += nbytes;
			} else {
new_segment:
Linus Torvalds's avatar
Linus Torvalds committed
852
				memset(&sg[nsegs],0,sizeof(struct scatterlist));
Linus Torvalds's avatar
Linus Torvalds committed
853 854 855 856 857 858
				sg[nsegs].page = bvec->bv_page;
				sg[nsegs].length = nbytes;
				sg[nsegs].offset = bvec->bv_offset;

				nsegs++;
			}
Linus Torvalds's avatar
Linus Torvalds committed
859
			bvprv = bvec;
Linus Torvalds's avatar
Linus Torvalds committed
860 861 862 863 864 865 866 867 868 869
		} /* segments in bio */
	} /* bios in rq */

	return nsegs;
}

/*
 * the standard queue merge functions, can be overridden with device
 * specific ones if so desired
 */
Linus Torvalds's avatar
Linus Torvalds committed
870 871 872 873

static inline int ll_new_mergeable(request_queue_t *q,
				   struct request *req,
				   struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
874
{
Linus Torvalds's avatar
Linus Torvalds committed
875
	int nr_phys_segs = bio_phys_segments(q, bio);
Linus Torvalds's avatar
Linus Torvalds committed
876

Linus Torvalds's avatar
Linus Torvalds committed
877 878
	if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
879
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
880
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
881
	}
Linus Torvalds's avatar
Linus Torvalds committed
882

Linus Torvalds's avatar
Linus Torvalds committed
883 884 885 886 887 888 889 890 891 892 893 894 895
	/*
	 * A hw segment is just getting larger, bump just the phys
	 * counter.
	 */
	req->nr_phys_segments += nr_phys_segs;
	return 1;
}

static inline int ll_new_hw_segment(request_queue_t *q,
				    struct request *req,
				    struct bio *bio)
{
	int nr_hw_segs = bio_hw_segments(q, bio);
Linus Torvalds's avatar
Linus Torvalds committed
896
	int nr_phys_segs = bio_phys_segments(q, bio);
Linus Torvalds's avatar
Linus Torvalds committed
897

Linus Torvalds's avatar
Linus Torvalds committed
898 899
	if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
	    || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
Linus Torvalds's avatar
Linus Torvalds committed
900
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
901
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
902 903 904 905 906 907 908 909
		return 0;
	}

	/*
	 * This will form the start of a new hw segment.  Bump both
	 * counters.
	 */
	req->nr_hw_segments += nr_hw_segs;
Linus Torvalds's avatar
Linus Torvalds committed
910
	req->nr_phys_segments += nr_phys_segs;
Linus Torvalds's avatar
Linus Torvalds committed
911
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
912 913 914
}

static int ll_back_merge_fn(request_queue_t *q, struct request *req, 
Linus Torvalds's avatar
Linus Torvalds committed
915
			    struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
916
{
Linus Torvalds's avatar
Linus Torvalds committed
917 918
	if (req->nr_sectors + bio_sectors(bio) > q->max_sectors) {
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
919
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
920
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
921 922
	}

Linus Torvalds's avatar
Linus Torvalds committed
923
	if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)))
Linus Torvalds's avatar
Linus Torvalds committed
924 925 926
		return ll_new_mergeable(q, req, bio);

	return ll_new_hw_segment(q, req, bio);
Linus Torvalds's avatar
Linus Torvalds committed
927 928 929
}

static int ll_front_merge_fn(request_queue_t *q, struct request *req, 
Linus Torvalds's avatar
Linus Torvalds committed
930
			     struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
931
{
Linus Torvalds's avatar
Linus Torvalds committed
932 933
	if (req->nr_sectors + bio_sectors(bio) > q->max_sectors) {
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
934
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
935
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
936 937
	}

Linus Torvalds's avatar
Linus Torvalds committed
938
	if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)))
Linus Torvalds's avatar
Linus Torvalds committed
939 940 941
		return ll_new_mergeable(q, req, bio);

	return ll_new_hw_segment(q, req, bio);
Linus Torvalds's avatar
Linus Torvalds committed
942 943 944
}

static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
Linus Torvalds's avatar
Linus Torvalds committed
945
				struct request *next)
Linus Torvalds's avatar
Linus Torvalds committed
946
{
Linus Torvalds's avatar
Linus Torvalds committed
947
	int total_phys_segments = req->nr_phys_segments +next->nr_phys_segments;
Linus Torvalds's avatar
Linus Torvalds committed
948
	int total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
Linus Torvalds's avatar
Linus Torvalds committed
949

Linus Torvalds's avatar
Linus Torvalds committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
	/*
	 * First check if the either of the requests are re-queued
	 * requests.  Can't merge them if they are.
	 */
	if (req->special || next->special)
		return 0;

	/*
	 * Will it become to large?
	 */
	if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
		return 0;

	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
	if (blk_phys_contig_segment(q, req->biotail, next->bio))
		total_phys_segments--;

	if (total_phys_segments > q->max_phys_segments)
		return 0;

	total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
	if (blk_hw_contig_segment(q, req->biotail, next->bio))
		total_hw_segments--;
973

Linus Torvalds's avatar
Linus Torvalds committed
974
	if (total_hw_segments > q->max_hw_segments)
Linus Torvalds's avatar
Linus Torvalds committed
975 976
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
977 978 979
	/* Merge is OK... */
	req->nr_phys_segments = total_phys_segments;
	req->nr_hw_segments = total_hw_segments;
Linus Torvalds's avatar
Linus Torvalds committed
980 981 982 983 984 985 986 987
	return 1;
}

/*
 * "plug" the device if there are no outstanding requests: this will
 * force the transfer to start only after we have put all the requests
 * on the list.
 *
Jens Axboe's avatar
Jens Axboe committed
988 989
 * This is called with interrupts off and no requests on the queue and
 * with the queue lock held.
Linus Torvalds's avatar
Linus Torvalds committed
990
 */
Linus Torvalds's avatar
Linus Torvalds committed
991
void blk_plug_device(request_queue_t *q)
Linus Torvalds's avatar
Linus Torvalds committed
992
{
993
	WARN_ON(!irqs_disabled());
Jens Axboe's avatar
Jens Axboe committed
994
	if (!blk_queue_plugged(q)) {
995 996
		spin_lock(&blk_plug_lock);
		list_add_tail(&q->plug_list, &blk_plug_list);
997
		mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
998 999
		spin_unlock(&blk_plug_lock);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1000 1001
}

Jens Axboe's avatar
Jens Axboe committed
1002 1003 1004 1005
/*
 * remove the queue from the plugged list, if present. called with
 * queue lock held and interrupts disabled.
 */
1006
int blk_remove_plug(request_queue_t *q)
Jens Axboe's avatar
Jens Axboe committed
1007
{
1008
	WARN_ON(!irqs_disabled());
Jens Axboe's avatar
Jens Axboe committed
1009 1010 1011
	if (blk_queue_plugged(q)) {
		spin_lock(&blk_plug_lock);
		list_del_init(&q->plug_list);
1012
		del_timer(&q->unplug_timer);
Jens Axboe's avatar
Jens Axboe committed
1013 1014 1015 1016 1017 1018 1019
		spin_unlock(&blk_plug_lock);
		return 1;
	}

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1020 1021 1022 1023 1024
/*
 * remove the plug and let it rip..
 */
static inline void __generic_unplug_device(request_queue_t *q)
{
Jens Axboe's avatar
Jens Axboe committed
1025
	if (!blk_remove_plug(q))
Jens Axboe's avatar
Jens Axboe committed
1026 1027
		return;

1028
	if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
Linus Torvalds's avatar
Linus Torvalds committed
1029 1030
		return;

1031 1032
	del_timer(&q->unplug_timer);

Linus Torvalds's avatar
Linus Torvalds committed
1033 1034 1035
	/*
	 * was plugged, fire request_fn if queue has stuff to do
	 */
1036
	if (elv_next_request(q))
Linus Torvalds's avatar
Linus Torvalds committed
1037
		q->request_fn(q);
Linus Torvalds's avatar
Linus Torvalds committed
1038 1039
}

Linus Torvalds's avatar
Linus Torvalds committed
1040 1041
/**
 * generic_unplug_device - fire a request queue
Jens Axboe's avatar
Jens Axboe committed
1042
 * @data:    The &request_queue_t in question
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048
 *
 * Description:
 *   Linux uses plugging to build bigger requests queues before letting
 *   the device have at them. If a queue is plugged, the I/O scheduler
 *   is still adding and merging requests on the queue. Once the queue
 *   gets unplugged (either by manually calling this function, or by
1049
 *   calling blk_run_queues()), the request_fn defined for the
Linus Torvalds's avatar
Linus Torvalds committed
1050 1051
 *   queue is invoked and transfers started.
 **/
Linus Torvalds's avatar
Linus Torvalds committed
1052
void generic_unplug_device(void *data)
Jens Axboe's avatar
Jens Axboe committed
1053 1054
{
	request_queue_t *q = data;
Linus Torvalds's avatar
Linus Torvalds committed
1055

Jens Axboe's avatar
Jens Axboe committed
1056
	spin_lock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1057
	__generic_unplug_device(q);
Jens Axboe's avatar
Jens Axboe committed
1058
	spin_unlock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1059 1060
}

1061 1062
static void blk_unplug_work(void *data)
{
Andrew Morton's avatar
Andrew Morton committed
1063 1064
	request_queue_t *q = data;
	q->unplug_fn(q);
1065 1066 1067 1068 1069 1070
}

static void blk_unplug_timeout(unsigned long data)
{
	request_queue_t *q = (request_queue_t *)data;

1071
	kblockd_schedule_work(&q->unplug_work);
1072 1073
}

1074 1075 1076 1077 1078 1079 1080
/**
 * blk_start_queue - restart a previously stopped queue
 * @q:    The &request_queue_t in question
 *
 * Description:
 *   blk_start_queue() will clear the stop flag on the queue, and call
 *   the request_fn for the queue if it was in a stopped state when
1081
 *   entered. Also see blk_stop_queue(). Must not be called from driver
Jens Axboe's avatar
Jens Axboe committed
1082
 *   request function due to recursion issues. Queue lock must be held.
1083
 **/
Jens Axboe's avatar
Jens Axboe committed
1084 1085
void blk_start_queue(request_queue_t *q)
{
Andrew Morton's avatar
Andrew Morton committed
1086 1087
	clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
	schedule_work(&q->unplug_work);
1088 1089
}

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
/**
 * blk_stop_queue - stop a queue
 * @q:    The &request_queue_t in question
 *
 * Description:
 *   The Linux block layer assumes that a block driver will consume all
 *   entries on the request queue when the request_fn strategy is called.
 *   Often this will not happen, because of hardware limitations (queue
 *   depth settings). If a device driver gets a 'queue full' response,
 *   or if it simply chooses not to queue more I/O at one point, it can
 *   call this function to prevent the request_fn from being called until
 *   the driver has signalled it's ready to go again. This happens by calling
Jens Axboe's avatar
Jens Axboe committed
1102
 *   blk_start_queue() to restart queue operations. Queue lock must be held.
1103
 **/
Jens Axboe's avatar
Jens Axboe committed
1104 1105
void blk_stop_queue(request_queue_t *q)
{
Jens Axboe's avatar
Jens Axboe committed
1106 1107
	blk_remove_plug(q);
	set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
Jens Axboe's avatar
Jens Axboe committed
1108 1109
}

1110 1111 1112 1113
/**
 * blk_run_queue - run a single device queue
 * @q	The queue to run
 */
1114
void blk_run_queue(struct request_queue *q)
1115
{
1116 1117 1118
	unsigned long flags;

	spin_lock_irqsave(q->queue_lock, flags);
1119 1120
	blk_remove_plug(q);
	q->request_fn(q);
1121
	spin_unlock_irqrestore(q->queue_lock, flags);
1122 1123
}

1124 1125 1126 1127 1128 1129 1130 1131
/**
 * blk_run_queues - fire all plugged queues
 *
 * Description:
 *   Start I/O on all plugged queues known to the block layer. Queues that
 *   are currently stopped are ignored. This is equivalent to the older
 *   tq_disk task queue run.
 **/
Jens Axboe's avatar
Jens Axboe committed
1132
#define blk_plug_entry(entry) list_entry((entry), request_queue_t, plug_list)
Jens Axboe's avatar
Jens Axboe committed
1133 1134
void blk_run_queues(void)
{
1135
	LIST_HEAD(local_plug_list);
1136

Jens Axboe's avatar
Jens Axboe committed
1137 1138
	spin_lock_irq(&blk_plug_lock);

Jens Axboe's avatar
Jens Axboe committed
1139
	/*
1140
	 * this will happen fairly often
Jens Axboe's avatar
Jens Axboe committed
1141
	 */
1142 1143
	if (list_empty(&blk_plug_list))
		goto out;
1144

1145
	list_splice_init(&blk_plug_list, &local_plug_list);
Jens Axboe's avatar
Jens Axboe committed
1146 1147 1148
	
	while (!list_empty(&local_plug_list)) {
		request_queue_t *q = blk_plug_entry(local_plug_list.next);
Jens Axboe's avatar
Jens Axboe committed
1149

1150
		spin_unlock_irq(&blk_plug_lock);
Jens Axboe's avatar
Jens Axboe committed
1151
		q->unplug_fn(q);
1152
		spin_lock_irq(&blk_plug_lock);
1153
	}
1154 1155
out:
	spin_unlock_irq(&blk_plug_lock);
Jens Axboe's avatar
Jens Axboe committed
1156 1157
}

Linus Torvalds's avatar
Linus Torvalds committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
/**
 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
 * @q:    the request queue to be released
 *
 * Description:
 *     blk_cleanup_queue is the pair to blk_init_queue().  It should
 *     be called when a request queue is being released; typically
 *     when a block device is being de-registered.  Currently, its
 *     primary task it to free all the &struct request structures that
 *     were allocated to the queue.
1168
 * Caveat:
Linus Torvalds's avatar
Linus Torvalds committed
1169 1170 1171 1172 1173
 *     Hopefully the low level driver will have finished any
 *     outstanding requests first...
 **/
void blk_cleanup_queue(request_queue_t * q)
{
1174
	struct request_list *rl = &q->rq;
Linus Torvalds's avatar
Linus Torvalds committed
1175

1176 1177
	elevator_exit(q);

1178
	del_timer_sync(&q->unplug_timer);
1179
	kblockd_flush();
1180

1181
	mempool_destroy(rl->rq_pool);
Linus Torvalds's avatar
Linus Torvalds committed
1182

1183 1184 1185
	if (blk_queue_tagged(q))
		blk_queue_free_tags(q);

Linus Torvalds's avatar
Linus Torvalds committed
1186 1187 1188 1189
	memset(q, 0, sizeof(*q));
}

static int blk_init_free_list(request_queue_t *q)
Linus Torvalds's avatar
Linus Torvalds committed
1190
{
1191
	struct request_list *rl = &q->rq;
Linus Torvalds's avatar
Linus Torvalds committed
1192

1193
	rl->count[READ] = rl->count[WRITE] = 0;
1194 1195
	init_waitqueue_head(&rl->wait[READ]);
	init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds's avatar
Linus Torvalds committed
1196

1197
	rl->rq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, request_cachep);
Linus Torvalds's avatar
Linus Torvalds committed
1198

1199 1200
	if (!rl->rq_pool)
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1201

Linus Torvalds's avatar
Linus Torvalds committed
1202
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1203 1204
}

Linus Torvalds's avatar
Linus Torvalds committed
1205
static int __make_request(request_queue_t *, struct bio *);
Linus Torvalds's avatar
Linus Torvalds committed
1206

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
static elevator_t *chosen_elevator = &iosched_as;

static int __init elevator_setup(char *str)
{
	if (!strcmp(str, "deadline"))
		chosen_elevator = &iosched_deadline;
	if (!strcmp(str, "as"))
		chosen_elevator = &iosched_as;
	return 1;
}
__setup("elevator=", elevator_setup);

Linus Torvalds's avatar
Linus Torvalds committed
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
/**
 * blk_init_queue  - prepare a request queue for use with a block device
 * @q:    The &request_queue_t to be initialised
 * @rfn:  The function to be called to process requests that have been
 *        placed on the queue.
 *
 * Description:
 *    If a block device wishes to use the standard request handling procedures,
 *    which sorts requests and coalesces adjacent requests, then it must
 *    call blk_init_queue().  The function @rfn will be called when there
 *    are requests on the queue that need to be processed.  If the device
 *    supports plugging, then @rfn may not be called immediately when requests
 *    are available on the queue, but may be called at some time later instead.
 *    Plugged queues are generally unplugged when a buffer belonging to one
 *    of the requests on the queue is needed, or due to memory pressure.
 *
 *    @rfn is not required, or even expected, to remove all requests off the
 *    queue, but only as many as it can handle at a time.  If it does leave
 *    requests on the queue, it is responsible for arranging that the requests
 *    get dealt with eventually.
 *
Linus Torvalds's avatar
Linus Torvalds committed
1240 1241
 *    The queue spin lock must be held while manipulating the requests on the
 *    request queue.
Linus Torvalds's avatar
Linus Torvalds committed
1242 1243
 *
 * Note:
Linus Torvalds's avatar
Linus Torvalds committed
1244
 *    blk_init_queue() must be paired with a blk_cleanup_queue() call
Linus Torvalds's avatar
Linus Torvalds committed
1245 1246
 *    when the block device is deactivated (such as at module unload).
 **/
Linus Torvalds's avatar
Linus Torvalds committed
1247
int blk_init_queue(request_queue_t *q, request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds's avatar
Linus Torvalds committed
1248
{
Linus Torvalds's avatar
Linus Torvalds committed
1249
	int ret;
1250
	static int printed;
Linus Torvalds's avatar
Linus Torvalds committed
1251 1252 1253 1254

	if (blk_init_free_list(q))
		return -ENOMEM;

1255 1256 1257 1258 1259 1260 1261 1262 1263
	if (!printed) {
		printed = 1;
		if (chosen_elevator == &iosched_deadline)
			printk("deadline elevator\n");
		else if (chosen_elevator == &iosched_as)
			printk("anticipatory scheduling elevator\n");
	}

	if ((ret = elevator_init(q, chosen_elevator))) {
Linus Torvalds's avatar
Linus Torvalds committed
1264 1265 1266 1267
		blk_cleanup_queue(q);
		return ret;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1268
	q->request_fn		= rfn;
Linus Torvalds's avatar
Linus Torvalds committed
1269 1270 1271
	q->back_merge_fn       	= ll_back_merge_fn;
	q->front_merge_fn      	= ll_front_merge_fn;
	q->merge_requests_fn	= ll_merge_requests_fn;
Linus Torvalds's avatar
Linus Torvalds committed
1272
	q->prep_rq_fn		= NULL;
Jens Axboe's avatar
Jens Axboe committed
1273
	q->unplug_fn		= generic_unplug_device;
Linus Torvalds's avatar
Linus Torvalds committed
1274
	q->queue_flags		= (1 << QUEUE_FLAG_CLUSTER);
Linus Torvalds's avatar
Linus Torvalds committed
1275
	q->queue_lock		= lock;
Andrew Morton's avatar
Andrew Morton committed
1276

Linus Torvalds's avatar
Linus Torvalds committed
1277 1278
	blk_queue_segment_boundary(q, 0xffffffff);

Linus Torvalds's avatar
Linus Torvalds committed
1279 1280
	blk_queue_make_request(q, __make_request);
	blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
1281 1282 1283

	blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
	blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
Jens Axboe's avatar
Jens Axboe committed
1284

Linus Torvalds's avatar
Linus Torvalds committed
1285
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1286 1287
}

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
static inline void blk_free_request(request_queue_t *q, struct request *rq)
{
	elv_put_request(q, rq);
	mempool_free(rq, q->rq.rq_pool);
}

static inline struct request *blk_alloc_request(request_queue_t *q,int gfp_mask)
{
	struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);

	if (!rq)
		return NULL;

	if (!elv_set_request(q, rq, gfp_mask))
		return rq;

	mempool_free(rq, q->rq.rq_pool);
	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
1308
#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
Linus Torvalds's avatar
Linus Torvalds committed
1309
/*
1310
 * Get a free request, queue_lock must not be held
Linus Torvalds's avatar
Linus Torvalds committed
1311
 */
1312
static struct request *get_request(request_queue_t *q, int rw, int gfp_mask)
Linus Torvalds's avatar
Linus Torvalds committed
1313
{
Linus Torvalds's avatar
Linus Torvalds committed
1314
	struct request *rq = NULL;
1315 1316 1317
	struct request_list *rl = &q->rq;

	spin_lock_irq(q->queue_lock);
1318
	if (rl->count[rw] >= q->nr_requests || !elv_may_queue(q, rw)) {
1319 1320
		spin_unlock_irq(q->queue_lock);
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1321
	}
1322
	rl->count[rw]++;
1323
	if (rl->count[rw] >= queue_congestion_on_threshold(q))
1324 1325
		set_queue_congested(q, rw);
	spin_unlock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1326

1327 1328 1329 1330
	rq = blk_alloc_request(q, gfp_mask);
	if (!rq) {
		spin_lock_irq(q->queue_lock);
		rl->count[rw]--;
1331
		if (rl->count[rw] < queue_congestion_off_threshold(q))
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
                        clear_queue_congested(q, rw);
		spin_unlock_irq(q->queue_lock);
		goto out;
	}
	
	INIT_LIST_HEAD(&rq->queuelist);

	/*
	 * first three bits are identical in rq->flags and bio->bi_rw,
	 * see bio.h and blkdev.h
	 */
	rq->flags = rw;

	rq->errors = 0;
	rq->rq_status = RQ_ACTIVE;
	rq->bio = rq->biotail = NULL;
	rq->buffer = NULL;
	rq->ref_count = 1;
	rq->q = q;
	rq->rl = rl;
	rq->waiting = NULL;
	rq->special = NULL;
	rq->data = NULL;
	rq->sense = NULL;

out:
Linus Torvalds's avatar
Linus Torvalds committed
1358 1359 1360 1361
	return rq;
}

/*
1362 1363
 * No available requests for this queue, unplug the device and wait for some
 * requests to become available.
Linus Torvalds's avatar
Linus Torvalds committed
1364
 */
Linus Torvalds's avatar
Linus Torvalds committed
1365
static struct request *get_request_wait(request_queue_t *q, int rw)
Linus Torvalds's avatar
Linus Torvalds committed
1366
{
1367
	DEFINE_WAIT(wait);
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369
	struct request *rq;

Linus Torvalds's avatar
Linus Torvalds committed
1370 1371
	generic_unplug_device(q);
	do {
1372
		rq = get_request(q, rw, GFP_NOIO);
1373

1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
		if (!rq) {
			struct request_list *rl = &q->rq;

			prepare_to_wait_exclusive(&rl->wait[rw], &wait,
						TASK_UNINTERRUPTIBLE);
			/*
			 * If _all_ the requests were suddenly returned then
			 * no wakeup will be delivered.  So now we're on the
			 * waitqueue, go check for that.
			 */
			rq = get_request(q, rw, GFP_ATOMIC & ~__GFP_HIGH);
			if (!rq)
				io_schedule();
			finish_wait(&rl->wait[rw], &wait);
		}
1389
	} while (!rq);
1390

Linus Torvalds's avatar
Linus Torvalds committed
1391 1392 1393
	return rq;
}

Linus Torvalds's avatar
Linus Torvalds committed
1394 1395 1396 1397 1398 1399
struct request *blk_get_request(request_queue_t *q, int rw, int gfp_mask)
{
	struct request *rq;

	BUG_ON(rw != READ && rw != WRITE);

1400
	rq = get_request(q, rw, gfp_mask);
Linus Torvalds's avatar
Linus Torvalds committed
1401 1402 1403 1404

	if (!rq && (gfp_mask & __GFP_WAIT))
		rq = get_request_wait(q, rw);

Martin Dalecki's avatar
Martin Dalecki committed
1405 1406 1407
	return rq;
}

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
/**
 * blk_insert_request - insert a special request in to a request queue
 * @q:		request queue where request should be inserted
 * @rq:		request to be inserted
 * @at_head:	insert request at head or tail of queue
 * @data:	private data
 *
 * Description:
 *    Many block devices need to execute commands asynchronously, so they don't
 *    block the whole kernel from preemption during request execution.  This is
 *    accomplished normally by inserting aritficial requests tagged as
 *    REQ_SPECIAL in to the corresponding request queue, and letting them be
 *    scheduled for actual execution by the request queue.
 *
 *    We have the option of inserting the head or the tail of the queue.
 *    Typically we use the tail for new ioctls and so forth.  We use the head
 *    of the queue for things like a QUEUE_FULL message from a device, or a
 *    host that is unable to accept a particular command.
 */
void blk_insert_request(request_queue_t *q, struct request *rq,
1428
			int at_head, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
1429
{
1430 1431 1432 1433 1434 1435 1436
	unsigned long flags;

	/*
	 * tell I/O scheduler that this isn't a regular read/write (ie it
	 * must not attempt merges on this) and that it acts as a soft
	 * barrier
	 */
Jens Axboe's avatar
Jens Axboe committed
1437
	rq->flags |= REQ_SPECIAL | REQ_SOFTBARRIER;
1438 1439 1440 1441

	rq->special = data;

	spin_lock_irqsave(q->queue_lock, flags);
1442 1443 1444 1445 1446

	/*
	 * If command is tagged, release the tag
	 */
	if (blk_rq_tagged(rq))
1447
		blk_queue_end_tag(q, rq);
1448

Andrew Morton's avatar
Andrew Morton committed
1449
	drive_stat_acct(rq, rq->nr_sectors, 1);
1450
	__elv_add_request(q, rq, !at_head, 0);
1451 1452
	q->request_fn(q);
	spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1453 1454 1455
}

void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
Linus Torvalds's avatar
Linus Torvalds committed
1456
{
Linus Torvalds's avatar
Linus Torvalds committed
1457
	int rw = rq_data_dir(rq);
Linus Torvalds's avatar
Linus Torvalds committed
1458

1459 1460 1461 1462
	if (!rq->rq_disk)
		return;

	if (rw == READ) {
1463
		disk_stat_add(rq->rq_disk, read_sectors, nr_sectors);
1464
		if (!new_io)
1465
			disk_stat_inc(rq->rq_disk, read_merges);
1466
	} else if (rw == WRITE) {
1467
		disk_stat_add(rq->rq_disk, write_sectors, nr_sectors);
1468
		if (!new_io)
1469
			disk_stat_inc(rq->rq_disk, write_merges);
1470 1471 1472
	}
	if (new_io) {
		disk_round_stats(rq->rq_disk);
1473
		disk_stat_inc(rq->rq_disk, in_flight);
1474
	}
Linus Torvalds's avatar
Linus Torvalds committed
1475 1476 1477 1478
}

/*
 * add-request adds a request to the linked list.
Linus Torvalds's avatar
Linus Torvalds committed
1479
 * queue lock is held and interrupts disabled, as we muck with the
Linus Torvalds's avatar
Linus Torvalds committed
1480
 * request queue list.
Linus Torvalds's avatar
Linus Torvalds committed
1481 1482
 */
static inline void add_request(request_queue_t * q, struct request * req,
Linus Torvalds's avatar
Linus Torvalds committed
1483
			       struct list_head *insert_here)
Linus Torvalds's avatar
Linus Torvalds committed
1484
{
Linus Torvalds's avatar
Linus Torvalds committed
1485
	drive_stat_acct(req, req->nr_sectors, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1486 1487

	/*
Linus Torvalds's avatar
Linus Torvalds committed
1488 1489
	 * elevator indicated where it wants this request to be
	 * inserted at elevator_merge time
Linus Torvalds's avatar
Linus Torvalds committed
1490
	 */
1491
	__elv_add_request_pos(q, req, insert_here);
Linus Torvalds's avatar
Linus Torvalds committed
1492
}
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
 
/*
 * disk_round_stats()	- Round off the performance stats on a struct
 * disk_stats.
 *
 * The average IO queue length and utilisation statistics are maintained
 * by observing the current state of the queue length and the amount of
 * time it has been in this state for.
 *
 * Normally, that accounting is done on IO completion, but that can result
 * in more than a second's worth of IO being accounted for within any one
 * second, leading to >100% utilisation.  To deal with that, we call this
 * function to do a round-off before returning the results when reading
 * /proc/diskstats.  This accounts immediately for all queue usage up to
 * the current jiffies and restarts the counters again.
 */
void disk_round_stats(struct gendisk *disk)
{
	unsigned long now = jiffies;

1513 1514
	disk_stat_add(disk, time_in_queue, 
			disk_stat_read(disk, in_flight) * (now - disk->stamp));
1515 1516
	disk->stamp = now;

1517 1518
	if (disk_stat_read(disk, in_flight))
		disk_stat_add(disk, io_ticks, (now - disk->stamp_idle));
1519 1520
	disk->stamp_idle = now;
}
Linus Torvalds's avatar
Linus Torvalds committed
1521

1522 1523 1524
/*
 * queue lock must be held
 */
1525
void __blk_put_request(request_queue_t *q, struct request *req)
Linus Torvalds's avatar
Linus Torvalds committed
1526
{
Linus Torvalds's avatar
Linus Torvalds committed
1527
	struct request_list *rl = req->rl;
1528 1529 1530

	if (unlikely(!q))
		return;
1531 1532
	if (unlikely(--req->ref_count))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1533

1534 1535
	elv_completed_request(req->q, req);

Linus Torvalds's avatar
Linus Torvalds committed
1536
	req->rq_status = RQ_INACTIVE;
Linus Torvalds's avatar
Linus Torvalds committed
1537
	req->q = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1538
	req->rl = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1539 1540

	/*
Linus Torvalds's avatar
Linus Torvalds committed
1541
	 * Request may not have originated from ll_rw_blk. if not,
Linus Torvalds's avatar
Linus Torvalds committed
1542
	 * it didn't come out of our reserved rq pools
Linus Torvalds's avatar
Linus Torvalds committed
1543
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1544
	if (rl) {
1545
		int rw = rq_data_dir(req);
1546

1547 1548
		BUG_ON(!list_empty(&req->queuelist));

1549
		blk_free_request(q, req);
Linus Torvalds's avatar
Linus Torvalds committed
1550

1551
		rl->count[rw]--;
1552
		if (rl->count[rw] < queue_congestion_off_threshold(q))
1553
			clear_queue_congested(q, rw);
1554
		if (rl->count[rw] < batch_requests(q) &&
1555 1556
				waitqueue_active(&rl->wait[rw]))
			wake_up(&rl->wait[rw]);
Linus Torvalds's avatar
Linus Torvalds committed
1557 1558 1559
	}
}

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
void blk_put_request(struct request *req)
{
	request_queue_t *q = req->q;
	
	/*
	 * if req->q isn't set, this request didnt originate from the
	 * block layer, so it's safe to just disregard it
	 */
	if (q) {
		unsigned long flags;

		spin_lock_irqsave(q->queue_lock, flags);
		__blk_put_request(q, req);
		spin_unlock_irqrestore(q->queue_lock, flags);
	}
}

1577 1578 1579 1580 1581 1582
/**
 * blk_congestion_wait - wait for a queue to become uncongested
 * @rw: READ or WRITE
 * @timeout: timeout in jiffies
 *
 * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
1583 1584
 * If no queues are congested then just wait for the next request to be
 * returned.
1585 1586 1587
 */
void blk_congestion_wait(int rw, long timeout)
{
1588
	DEFINE_WAIT(wait);
1589
	wait_queue_head_t *wqh = &congestion_wqh[rw];
1590 1591

	blk_run_queues();
1592
	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1593
	io_schedule_timeout(timeout);
1594
	finish_wait(wqh, &wait);
1595 1596
}

Linus Torvalds's avatar
Linus Torvalds committed
1597 1598 1599
/*
 * Has to be called with the request spinlock acquired
 */
1600
static int attempt_merge(request_queue_t *q, struct request *req,
Linus Torvalds's avatar
Linus Torvalds committed
1601
			  struct request *next)
Linus Torvalds's avatar
Linus Torvalds committed
1602
{
Linus Torvalds's avatar
Linus Torvalds committed
1603
	if (!rq_mergeable(req) || !rq_mergeable(next))
1604
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1605 1606 1607 1608

	/*
	 * not contigious
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1609
	if (req->sector + req->nr_sectors != next->sector)
1610
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1611

Linus Torvalds's avatar
Linus Torvalds committed
1612
	if (rq_data_dir(req) != rq_data_dir(next)
1613
	    || req->rq_disk != next->rq_disk
Linus Torvalds's avatar
Linus Torvalds committed
1614
	    || next->waiting || next->special)
1615
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1616

Linus Torvalds's avatar
Linus Torvalds committed
1617
	/*
Linus Torvalds's avatar
Linus Torvalds committed
1618 1619 1620 1621
	 * If we are allowed to merge, then append bio list
	 * from next to rq and release next. merge_requests_fn
	 * will have updated segment counts, update sector
	 * counts here.
Linus Torvalds's avatar
Linus Torvalds committed
1622
	 */
1623 1624
	if (!q->merge_requests_fn(q, req, next))
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1625

1626 1627
	req->biotail->bi_next = next->bio;
	req->biotail = next->biotail;
Linus Torvalds's avatar
Linus Torvalds committed
1628

1629
	req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
1630

1631
	elv_merge_requests(q, req, next);
1632

1633 1634 1635
	if (req->rq_disk) {
		disk_round_stats(req->rq_disk);
		disk_stat_dec(req->rq_disk, in_flight);
Linus Torvalds's avatar
Linus Torvalds committed
1636
	}
1637

1638 1639
	__blk_put_request(q, next);
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1640 1641
}

1642
static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
1643
{
1644
	struct request *next = elv_latter_request(q, rq);
Linus Torvalds's avatar
Linus Torvalds committed
1645

1646 1647 1648 1649
	if (next)
		return attempt_merge(q, rq, next);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1650 1651
}

1652
static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
1653
{
1654
	struct request *prev = elv_former_request(q, rq);
Linus Torvalds's avatar
Linus Torvalds committed
1655

1656 1657 1658 1659
	if (prev)
		return attempt_merge(q, prev, rq);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1660
}
Linus Torvalds's avatar
Linus Torvalds committed
1661

Linus Torvalds's avatar
Linus Torvalds committed
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
/**
 * blk_attempt_remerge  - attempt to remerge active head with next request
 * @q:    The &request_queue_t belonging to the device
 * @rq:   The head request (usually)
 *
 * Description:
 *    For head-active devices, the queue can easily be unplugged so quickly
 *    that proper merging is not done on the front request. This may hurt
 *    performance greatly for some devices. The block layer cannot safely
 *    do merging on that first request for these queues, but the driver can
 *    call this function and make it happen any way. Only the driver knows
 *    when it is safe to do so.
 **/
void blk_attempt_remerge(request_queue_t *q, struct request *rq)
{
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1678

Linus Torvalds's avatar
Linus Torvalds committed
1679
	spin_lock_irqsave(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1680
	attempt_back_merge(q, rq);
Linus Torvalds's avatar
Linus Torvalds committed
1681
	spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1682
}
Linus Torvalds's avatar
Linus Torvalds committed
1683

Martin Dalecki's avatar
Martin Dalecki committed
1684 1685 1686 1687 1688 1689 1690 1691
/*
 * Non-locking blk_attempt_remerge variant.
 */
void __blk_attempt_remerge(request_queue_t *q, struct request *rq)
{
	attempt_back_merge(q, rq);
}

Linus Torvalds's avatar
Linus Torvalds committed
1692 1693 1694
static int __make_request(request_queue_t *q, struct bio *bio)
{
	struct request *req, *freereq = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1695
	int el_ret, rw, nr_sectors, cur_nr_sectors, barrier;
Linus Torvalds's avatar
Linus Torvalds committed
1696
	struct list_head *insert_here;
Linus Torvalds's avatar
Linus Torvalds committed
1697
	sector_t sector;
Linus Torvalds's avatar
Linus Torvalds committed
1698

Linus Torvalds's avatar
Linus Torvalds committed
1699 1700
	sector = bio->bi_sector;
	nr_sectors = bio_sectors(bio);
Jens Axboe's avatar
Jens Axboe committed
1701 1702
	cur_nr_sectors = bio_cur_sectors(bio);

Linus Torvalds's avatar
Linus Torvalds committed
1703
	rw = bio_data_dir(bio);
Linus Torvalds's avatar
Linus Torvalds committed
1704 1705

	/*
Linus Torvalds's avatar
Linus Torvalds committed
1706 1707 1708
	 * low level driver can indicate that it wants pages above a
	 * certain limit bounced to low memory (ie for highmem, or even
	 * ISA dma in theory)
Linus Torvalds's avatar
Linus Torvalds committed
1709
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1710 1711
	blk_queue_bounce(q, &bio);

Linus Torvalds's avatar
Linus Torvalds committed
1712
	spin_lock_prefetch(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1713

Linus Torvalds's avatar
Linus Torvalds committed
1714
	barrier = test_bit(BIO_RW_BARRIER, &bio->bi_rw);
Linus Torvalds's avatar
Linus Torvalds committed
1715

Linus Torvalds's avatar
Linus Torvalds committed
1716
again:
1717
	insert_here = NULL;
1718
	spin_lock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1719

Jens Axboe's avatar
Jens Axboe committed
1720
	if (elv_queue_empty(q)) {
Linus Torvalds's avatar
Linus Torvalds committed
1721
		blk_plug_device(q);
Linus Torvalds's avatar
Linus Torvalds committed
1722
		goto get_rq;
Linus Torvalds's avatar
Linus Torvalds committed
1723
	}
Jens Axboe's avatar
Jens Axboe committed
1724 1725
	if (barrier)
		goto get_rq;
Linus Torvalds's avatar
Linus Torvalds committed
1726

Jens Axboe's avatar
Jens Axboe committed
1727
	el_ret = elv_merge(q, &insert_here, bio);
Linus Torvalds's avatar
Linus Torvalds committed
1728
	switch (el_ret) {
Linus Torvalds's avatar
Linus Torvalds committed
1729
		case ELEVATOR_BACK_MERGE:
Jens Axboe's avatar
Jens Axboe committed
1730 1731
			req = list_entry_rq(insert_here);

Linus Torvalds's avatar
Linus Torvalds committed
1732
			BUG_ON(!rq_mergeable(req));
Jens Axboe's avatar
Jens Axboe committed
1733

Linus Torvalds's avatar
Linus Torvalds committed
1734 1735
			if (!q->back_merge_fn(q, req, bio)) {
				insert_here = &req->queuelist;
Linus Torvalds's avatar
Linus Torvalds committed
1736
				break;
Linus Torvalds's avatar
Linus Torvalds committed
1737
			}
Linus Torvalds's avatar
Linus Torvalds committed
1738

Linus Torvalds's avatar
Linus Torvalds committed
1739 1740 1741
			req->biotail->bi_next = bio;
			req->biotail = bio;
			req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
1742
			drive_stat_acct(req, nr_sectors, 0);
1743 1744
			if (!attempt_back_merge(q, req))
				elv_merged_request(q, req);
Linus Torvalds's avatar
Linus Torvalds committed
1745 1746 1747
			goto out;

		case ELEVATOR_FRONT_MERGE:
Jens Axboe's avatar
Jens Axboe committed
1748 1749
			req = list_entry_rq(insert_here);

Linus Torvalds's avatar
Linus Torvalds committed
1750
			BUG_ON(!rq_mergeable(req));
Jens Axboe's avatar
Jens Axboe committed
1751

Linus Torvalds's avatar
Linus Torvalds committed
1752 1753
			if (!q->front_merge_fn(q, req, bio)) {
				insert_here = req->queuelist.prev;
Linus Torvalds's avatar
Linus Torvalds committed
1754
				break;
Linus Torvalds's avatar
Linus Torvalds committed
1755
			}
Linus Torvalds's avatar
Linus Torvalds committed
1756

Linus Torvalds's avatar
Linus Torvalds committed
1757
			bio->bi_next = req->bio;
Jens Axboe's avatar
Jens Axboe committed
1758 1759 1760 1761
			req->cbio = req->bio = bio;
			req->nr_cbio_segments = bio_segments(bio);
			req->nr_cbio_sectors = bio_sectors(bio);

Linus Torvalds's avatar
Linus Torvalds committed
1762 1763 1764 1765 1766 1767 1768 1769
			/*
			 * may not be valid. if the low level driver said
			 * it didn't need a bounce buffer then it better
			 * not touch req->buffer either...
			 */
			req->buffer = bio_data(bio);
			req->current_nr_sectors = cur_nr_sectors;
			req->hard_cur_sectors = cur_nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
1770
			req->sector = req->hard_sector = sector;
Linus Torvalds's avatar
Linus Torvalds committed
1771
			req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
1772
			drive_stat_acct(req, nr_sectors, 0);
1773 1774
			if (!attempt_front_merge(q, req))
				elv_merged_request(q, req);
Linus Torvalds's avatar
Linus Torvalds committed
1775
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1776

Linus Torvalds's avatar
Linus Torvalds committed
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
		/*
		 * elevator says don't/can't merge. get new request
		 */
		case ELEVATOR_NO_MERGE:
			break;

		default:
			printk("elevator returned crap (%d)\n", el_ret);
			BUG();
	}
Linus Torvalds's avatar
Linus Torvalds committed
1787

Linus Torvalds's avatar
Linus Torvalds committed
1788
	/*
Linus Torvalds's avatar
Linus Torvalds committed
1789 1790 1791
	 * Grab a free request from the freelist - if that is empty, check
	 * if we are doing read ahead and abort instead of blocking for
	 * a free slot.
Linus Torvalds's avatar
Linus Torvalds committed
1792 1793 1794 1795 1796
	 */
get_rq:
	if (freereq) {
		req = freereq;
		freereq = NULL;
1797
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
1798
		spin_unlock_irq(q->queue_lock);
1799 1800 1801 1802 1803 1804 1805 1806 1807
		if ((freereq = get_request(q, rw, GFP_ATOMIC)) == NULL) {
			/*
			 * READA bit set
			 */
			if (bio_flagged(bio, BIO_RW_AHEAD))
				goto end_io;
	
			freereq = get_request_wait(q, rw);
		}
Linus Torvalds's avatar
Linus Torvalds committed
1808 1809 1810
		goto again;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
	/*
	 * first three bits are identical in rq->flags and bio->bi_rw,
	 * see bio.h and blkdev.h
	 */
	req->flags = (bio->bi_rw & 7) | REQ_CMD;

	/*
	 * REQ_BARRIER implies no merging, but lets make it explicit
	 */
	if (barrier)
Jens Axboe's avatar
Jens Axboe committed
1821
		req->flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Linus Torvalds's avatar
Linus Torvalds committed
1822

Linus Torvalds's avatar
Linus Torvalds committed
1823 1824
	req->errors = 0;
	req->hard_sector = req->sector = sector;
Linus Torvalds's avatar
Linus Torvalds committed
1825 1826
	req->hard_nr_sectors = req->nr_sectors = nr_sectors;
	req->current_nr_sectors = req->hard_cur_sectors = cur_nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
1827
	req->nr_phys_segments = bio_phys_segments(q, bio);
Linus Torvalds's avatar
Linus Torvalds committed
1828
	req->nr_hw_segments = bio_hw_segments(q, bio);
Jens Axboe's avatar
Jens Axboe committed
1829 1830
	req->nr_cbio_segments = bio_segments(bio);
	req->nr_cbio_sectors = bio_sectors(bio);
Linus Torvalds's avatar
Linus Torvalds committed
1831
	req->buffer = bio_data(bio);	/* see ->buffer comment above */
Linus Torvalds's avatar
Linus Torvalds committed
1832
	req->waiting = NULL;
Jens Axboe's avatar
Jens Axboe committed
1833
	req->cbio = req->bio = req->biotail = bio;
1834
	req->rq_disk = bio->bi_bdev->bd_disk;
1835
	req->start_time = jiffies;
1836

Linus Torvalds's avatar
Linus Torvalds committed
1837
	add_request(q, req, insert_here);
Linus Torvalds's avatar
Linus Torvalds committed
1838
out:
Linus Torvalds's avatar
Linus Torvalds committed
1839
	if (freereq)
1840
		__blk_put_request(q, freereq);
1841 1842

	if (blk_queue_plugged(q)) {
1843 1844
		int nr_queued = q->rq.count[0] + q->rq.count[1];

1845 1846 1847
		if (nr_queued == q->unplug_thresh)
			__generic_unplug_device(q);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1848
	spin_unlock_irq(q->queue_lock);
1849

Linus Torvalds's avatar
Linus Torvalds committed
1850
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1851

Linus Torvalds's avatar
Linus Torvalds committed
1852
end_io:
1853
	bio_endio(bio, nr_sectors << 9, -EWOULDBLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
1854 1855 1856
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1857 1858 1859 1860 1861 1862

/*
 * If bio->bi_dev is a partition, remap the location
 */
static inline void blk_partition_remap(struct bio *bio)
{
1863
	struct block_device *bdev = bio->bi_bdev;
1864 1865
	struct gendisk *disk = bdev->bd_disk;
	struct hd_struct *p;
1866 1867 1868
	if (bdev == bdev->bd_contains)
		return;

1869
	p = disk->part[bdev->bd_dev-MKDEV(disk->major,disk->first_minor)-1];
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
	switch (bio->bi_rw) {
	case READ:
		p->read_sectors += bio_sectors(bio);
		p->reads++;
		break;
	case WRITE:
		p->write_sectors += bio_sectors(bio);
		p->writes++;
		break;
	}
1880
	bio->bi_sector += bdev->bd_offset;
1881
	bio->bi_bdev = bdev->bd_contains;
Linus Torvalds's avatar
Linus Torvalds committed
1882 1883
}

Linus Torvalds's avatar
Linus Torvalds committed
1884
/**
1885
 * generic_make_request: hand a buffer to its device driver for I/O
Linus Torvalds's avatar
Linus Torvalds committed
1886
 * @bio:  The bio describing the location in memory and on the device.
Linus Torvalds's avatar
Linus Torvalds committed
1887 1888
 *
 * generic_make_request() is used to make I/O requests of block
Linus Torvalds's avatar
Linus Torvalds committed
1889 1890
 * devices. It is passed a &struct bio, which describes the I/O that needs
 * to be done.
Linus Torvalds's avatar
Linus Torvalds committed
1891 1892 1893
 *
 * generic_make_request() does not return any status.  The
 * success/failure status of the request, along with notification of
Linus Torvalds's avatar
Linus Torvalds committed
1894
 * completion, is delivered asynchronously through the bio->bi_end_io
Linus Torvalds's avatar
Linus Torvalds committed
1895 1896
 * function described (one day) else where.
 *
Linus Torvalds's avatar
Linus Torvalds committed
1897 1898
 * The caller of generic_make_request must make sure that bi_io_vec
 * are set to describe the memory buffer, and that bi_dev and bi_sector are
Linus Torvalds's avatar
Linus Torvalds committed
1899
 * set to describe the device address, and the
Linus Torvalds's avatar
Linus Torvalds committed
1900 1901
 * bi_end_io and optionally bi_private are set to describe how
 * completion notification should be signaled.
Linus Torvalds's avatar
Linus Torvalds committed
1902
 *
Linus Torvalds's avatar
Linus Torvalds committed
1903 1904
 * generic_make_request and the drivers it calls may use bi_next if this
 * bio happens to be merged with someone else, and may change bi_dev and
Linus Torvalds's avatar
Linus Torvalds committed
1905
 * bi_sector for remaps as it sees fit.  So the values of these fields
Linus Torvalds's avatar
Linus Torvalds committed
1906 1907 1908
 * should NOT be depended on after the call to generic_make_request.
 *
 * */
Linus Torvalds's avatar
Linus Torvalds committed
1909
void generic_make_request(struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
1910 1911
{
	request_queue_t *q;
Andries E. Brouwer's avatar
Andries E. Brouwer committed
1912
	sector_t maxsector;
Linus Torvalds's avatar
Linus Torvalds committed
1913
	int ret, nr_sectors = bio_sectors(bio);
Linus Torvalds's avatar
Linus Torvalds committed
1914

Linus Torvalds's avatar
Linus Torvalds committed
1915
	/* Test device or partition size, when known. */
1916
	maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
Andries E. Brouwer's avatar
Andries E. Brouwer committed
1917 1918 1919 1920 1921
	if (maxsector) {
		sector_t sector = bio->bi_sector;

		if (maxsector < nr_sectors ||
		    maxsector - nr_sectors < sector) {
1922
			char b[BDEVNAME_SIZE];
Andries E. Brouwer's avatar
Andries E. Brouwer committed
1923 1924 1925 1926 1927
			/* This may well happen - the kernel calls
			 * bread() without checking the size of the
			 * device, e.g., when mounting a device. */
			printk(KERN_INFO
			       "attempt to access beyond end of device\n");
1928
			printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1929
			       bdevname(bio->bi_bdev, b),
1930
			       bio->bi_rw,
1931
			       (unsigned long long) sector + nr_sectors,
Andries E. Brouwer's avatar
Andries E. Brouwer committed
1932 1933
			       (long long) maxsector);

Linus Torvalds's avatar
Linus Torvalds committed
1934 1935
			set_bit(BIO_EOF, &bio->bi_flags);
			goto end_io;
Linus Torvalds's avatar
Linus Torvalds committed
1936 1937 1938 1939 1940 1941 1942
		}
	}

	/*
	 * Resolve the mapping until finished. (drivers are
	 * still free to implement/resolve their own stacking
	 * by explicitly returning 0)
Linus Torvalds's avatar
Linus Torvalds committed
1943 1944
	 *
	 * NOTE: we don't repeat the blk_size check for each new device.
Linus Torvalds's avatar
Linus Torvalds committed
1945 1946 1947
	 * Stacking drivers are expected to know what they are doing.
	 */
	do {
1948 1949
		char b[BDEVNAME_SIZE];

1950
		q = bdev_get_queue(bio->bi_bdev);
Linus Torvalds's avatar
Linus Torvalds committed
1951 1952
		if (!q) {
			printk(KERN_ERR
1953 1954 1955 1956
			       "generic_make_request: Trying to access "
				"nonexistent block-device %s (%Lu)\n",
				bdevname(bio->bi_bdev, b),
				(long long) bio->bi_sector);
Linus Torvalds's avatar
Linus Torvalds committed
1957
end_io:
1958
			bio_endio(bio, bio->bi_size, -EIO);
Linus Torvalds's avatar
Linus Torvalds committed
1959 1960
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1961

1962
		if (unlikely(bio_sectors(bio) > q->max_sectors)) {
1963
			printk("bio too big device %s (%u > %u)\n", 
1964 1965 1966
				bdevname(bio->bi_bdev, b),
				bio_sectors(bio),
				q->max_sectors);
1967 1968
			goto end_io;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1969 1970 1971 1972 1973 1974 1975

		/*
		 * If this device has partitions, remap block n
		 * of partition p to block n+start(p) of the disk.
		 */
		blk_partition_remap(bio);

Linus Torvalds's avatar
Linus Torvalds committed
1976 1977
		ret = q->make_request_fn(q, bio);
	} while (ret);
Linus Torvalds's avatar
Linus Torvalds committed
1978 1979 1980
}

/**
Linus Torvalds's avatar
Linus Torvalds committed
1981
 * submit_bio: submit a bio to the block device layer for I/O
Linus Torvalds's avatar
Linus Torvalds committed
1982
 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
Linus Torvalds's avatar
Linus Torvalds committed
1983
 * @bio: The &struct bio which describes the I/O
Linus Torvalds's avatar
Linus Torvalds committed
1984
 *
Linus Torvalds's avatar
Linus Torvalds committed
1985 1986 1987
 * submit_bio() is very similar in purpose to generic_make_request(), and
 * uses that function to do most of the work. Both are fairly rough
 * interfaces, @bio must be presetup and ready for I/O.
Linus Torvalds's avatar
Linus Torvalds committed
1988 1989
 *
 */
Linus Torvalds's avatar
Linus Torvalds committed
1990
int submit_bio(int rw, struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
1991
{
1992
	int count = bio_sectors(bio);
Linus Torvalds's avatar
Linus Torvalds committed
1993

Linus Torvalds's avatar
Linus Torvalds committed
1994
	BIO_BUG_ON(!bio->bi_size);
Linus Torvalds's avatar
Linus Torvalds committed
1995 1996 1997
	BIO_BUG_ON(!bio->bi_io_vec);
	bio->bi_rw = rw;
	if (rw & WRITE)
1998
		mod_page_state(pgpgout, count);
Linus Torvalds's avatar
Linus Torvalds committed
1999
	else
2000
		mod_page_state(pgpgin, count);
Linus Torvalds's avatar
Linus Torvalds committed
2001 2002 2003 2004
	generic_make_request(bio);
	return 1;
}

Jens Axboe's avatar
Jens Axboe committed
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
/**
 * blk_rq_next_segment
 * @rq:		the request being processed
 *
 * Description:
 *	Points to the next segment in the request if the current segment
 *	is complete. Leaves things unchanged if this segment is not over
 *	or if no more segments are left in this request.
 *
 *	Meant to be used for bio traversal during I/O submission
 *	Does not affect any I/O completions or update completion state
 *	in the request, and does not modify any bio fields.
 *
 *	Decrementing rq->nr_sectors, rq->current_nr_sectors and
 *	rq->nr_cbio_sectors as data is transferred is the caller's
 *	responsibility and should be done before calling this routine.
 **/
void blk_rq_next_segment(struct request *rq)
{
	if (rq->current_nr_sectors > 0)
		return;

	if (rq->nr_cbio_sectors > 0) {
		--rq->nr_cbio_segments;
		rq->current_nr_sectors = blk_rq_vec(rq)->bv_len >> 9;
	} else {
		if ((rq->cbio = rq->cbio->bi_next)) {
			rq->nr_cbio_segments = bio_segments(rq->cbio);
			rq->nr_cbio_sectors = bio_sectors(rq->cbio);
 			rq->current_nr_sectors = bio_cur_sectors(rq->cbio);
		}
 	}

	/* remember the size of this segment before we start I/O */
	rq->hard_cur_sectors = rq->current_nr_sectors;
}

/**
 * process_that_request_first	-	process partial request submission
 * @req:	the request being processed
 * @nr_sectors:	number of sectors I/O has been submitted on
 *
 * Description:
 *	May be used for processing bio's while submitting I/O without
 *	signalling completion. Fails if more data is requested than is
 *	available in the request in which case it doesn't advance any
 *	pointers.
 *
 *	Assumes a request is correctly set up. No sanity checks.
 *
 * Return:
 *	0 - no more data left to submit (not processed)
 *	1 - data available to submit for this request (processed)
 **/
int process_that_request_first(struct request *req, unsigned int nr_sectors)
{
	unsigned int nsect;

	if (req->nr_sectors < nr_sectors)
		return 0;

	req->nr_sectors -= nr_sectors;
	req->sector += nr_sectors;
	while (nr_sectors) {
		nsect = min_t(unsigned, req->current_nr_sectors, nr_sectors);
		req->current_nr_sectors -= nsect;
		nr_sectors -= nsect;
		if (req->cbio) {
			req->nr_cbio_sectors -= nsect;
			blk_rq_next_segment(req);
		}
	}
	return 1;
}

2080
void blk_recalc_rq_segments(struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
2081 2082 2083 2084
{
	struct bio *bio;
	int nr_phys_segs, nr_hw_segs;

2085 2086 2087
	if (!rq->bio)
		return;

Linus Torvalds's avatar
Linus Torvalds committed
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
	nr_phys_segs = nr_hw_segs = 0;
	rq_for_each_bio(bio, rq) {
		/* Force bio hw/phys segs to be recalculated. */
		bio->bi_flags &= ~(1 << BIO_SEG_VALID);

		nr_phys_segs += bio_phys_segments(rq->q, bio);
		nr_hw_segs += bio_hw_segments(rq->q, bio);
	}

	rq->nr_phys_segments = nr_phys_segs;
	rq->nr_hw_segments = nr_hw_segs;
}

2101
void blk_recalc_rq_sectors(struct request *rq, int nsect)
Linus Torvalds's avatar
Linus Torvalds committed
2102
{
2103
	if (blk_fs_request(rq)) {
Linus Torvalds's avatar
Linus Torvalds committed
2104
		rq->hard_sector += nsect;
Jens Axboe's avatar
Jens Axboe committed
2105
		rq->hard_nr_sectors -= nsect;
Linus Torvalds's avatar
Linus Torvalds committed
2106

Jens Axboe's avatar
Jens Axboe committed
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
		/*
		 * Move the I/O submission pointers ahead if required,
		 * i.e. for drivers not aware of rq->cbio.
		 */
		if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
		    (rq->sector <= rq->hard_sector)) {
			rq->sector = rq->hard_sector;
			rq->nr_sectors = rq->hard_nr_sectors;
			rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
			rq->current_nr_sectors = rq->hard_cur_sectors;
			rq->nr_cbio_segments = bio_segments(rq->bio);
			rq->nr_cbio_sectors = bio_sectors(rq->bio);
			rq->buffer = bio_data(rq->bio);

			rq->cbio = rq->bio;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2123

Linus Torvalds's avatar
Linus Torvalds committed
2124 2125 2126 2127 2128 2129 2130 2131
		/*
		 * if total number of sectors is less than the first segment
		 * size, something has gone terribly wrong
		 */
		if (rq->nr_sectors < rq->current_nr_sectors) {
			printk("blk: request botched\n");
			rq->nr_sectors = rq->current_nr_sectors;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2132 2133 2134
	}
}

2135 2136
static int __end_that_request_first(struct request *req, int uptodate,
				    int nr_bytes)
Linus Torvalds's avatar
Linus Torvalds committed
2137
{
2138
	int total_bytes, bio_nbytes, error = 0, next_idx = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2139
	struct bio *bio;
Linus Torvalds's avatar
Linus Torvalds committed
2140

2141 2142 2143 2144 2145 2146 2147
	/*
	 * for a REQ_BLOCK_PC request, we want to carry any eventual
	 * sense key with us all the way through
	 */
	if (!blk_pc_request(req))
		req->errors = 0;

Jens Axboe's avatar
Jens Axboe committed
2148 2149
	if (!uptodate) {
		error = -EIO;
Jens Axboe's avatar
Jens Axboe committed
2150 2151
		if (!(req->flags & REQ_QUIET))
			printk("end_request: I/O error, dev %s, sector %llu\n",
2152
				req->rq_disk ? req->rq_disk->disk_name : "?",
Jens Axboe's avatar
Jens Axboe committed
2153
				(unsigned long long)req->sector);
Jens Axboe's avatar
Jens Axboe committed
2154
	}
Linus Torvalds's avatar
Linus Torvalds committed
2155

2156
	total_bytes = bio_nbytes = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2157
	while ((bio = req->bio)) {
2158
		int nbytes;
Jens Axboe's avatar
Jens Axboe committed
2159

2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
		if (nr_bytes >= bio->bi_size) {
			req->bio = bio->bi_next;
			nbytes = bio->bi_size;
			bio_endio(bio, nbytes, error);
			next_idx = 0;
			bio_nbytes = 0;
		} else {
			int idx = bio->bi_idx + next_idx;

			if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
				blk_dump_rq_flags(req, "__end_that");
				printk("%s: bio idx %d >= vcnt %d\n",
						__FUNCTION__,
Jens Axboe's avatar
Jens Axboe committed
2173
						bio->bi_idx, bio->bi_vcnt);
2174 2175
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
2176

2177 2178
			nbytes = bio_iovec_idx(bio, idx)->bv_len;
			BIO_BUG_ON(nbytes > bio->bi_size);
Linus Torvalds's avatar
Linus Torvalds committed
2179

2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
			/*
			 * not a complete bvec done
			 */
			if (unlikely(nbytes > nr_bytes)) {
				bio_iovec(bio)->bv_offset += nr_bytes;
				bio_iovec(bio)->bv_len -= nr_bytes;
				bio_nbytes += nr_bytes;
				total_bytes += nr_bytes;
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
2190

2191 2192 2193 2194 2195
			/*
			 * advance to the next vector
			 */
			next_idx++;
			bio_nbytes += nbytes;
2196
		}
Linus Torvalds's avatar
Linus Torvalds committed
2197

2198 2199
		total_bytes += nbytes;
		nr_bytes -= nbytes;
Jens Axboe's avatar
Jens Axboe committed
2200

Linus Torvalds's avatar
Linus Torvalds committed
2201
		if ((bio = req->bio)) {
Linus Torvalds's avatar
Linus Torvalds committed
2202 2203 2204
			/*
			 * end more in this run, or just return 'not-done'
			 */
2205
			if (unlikely(nr_bytes <= 0))
Jens Axboe's avatar
Jens Axboe committed
2206
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2207 2208
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
2209

Jens Axboe's avatar
Jens Axboe committed
2210 2211 2212 2213 2214 2215 2216 2217 2218
	/*
	 * completely done
	 */
	if (!req->bio)
		return 0;

	/*
	 * if the request wasn't completed, update state
	 */
2219 2220 2221 2222 2223 2224
	if (bio_nbytes) {
		bio_endio(bio, bio_nbytes, error);
		req->bio->bi_idx += next_idx;
	}

	blk_recalc_rq_sectors(req, total_bytes >> 9);
Jens Axboe's avatar
Jens Axboe committed
2225 2226
	blk_recalc_rq_segments(req);
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
2227 2228
}

2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
/**
 * end_that_request_first - end I/O on a request
 * @req:      the request being processed
 * @uptodate: 0 for I/O error
 * @nr_sectors: number of sectors to end I/O on
 *
 * Description:
 *     Ends I/O on a number of sectors attached to @req, and sets it up
 *     for the next range of segments (if any) in the cluster.
 *
 * Return:
 *     0 - we are done with this request, call end_that_request_last()
 *     1 - still buffers pending for this request
 **/
int end_that_request_first(struct request *req, int uptodate, int nr_sectors)
{
	return __end_that_request_first(req, uptodate, nr_sectors << 9);
}

/**
 * end_that_request_chunk - end I/O on a request
 * @req:      the request being processed
 * @uptodate: 0 for I/O error
 * @nr_bytes: number of bytes to complete
 *
 * Description:
 *     Ends I/O on a number of bytes attached to @req, and sets it up
 *     for the next range of segments (if any). Like end_that_request_first(),
 *     but deals with bytes instead of sectors.
 *
 * Return:
 *     0 - we are done with this request, call end_that_request_last()
 *     1 - still buffers pending for this request
 **/
int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes)
{
	return __end_that_request_first(req, uptodate, nr_bytes);
}

/*
 * queue lock must be held
 */
Linus Torvalds's avatar
Linus Torvalds committed
2271 2272
void end_that_request_last(struct request *req)
{
2273
	struct gendisk *disk = req->rq_disk;
2274
	struct completion *waiting = req->waiting;
Linus Torvalds's avatar
Linus Torvalds committed
2275

2276 2277 2278 2279
	if (disk) {
		unsigned long duration = jiffies - req->start_time;
		switch (rq_data_dir(req)) {
		    case WRITE:
2280 2281
			disk_stat_inc(disk, writes);
			disk_stat_add(disk, write_ticks, duration);
2282 2283
			break;
		    case READ:
2284 2285
			disk_stat_inc(disk, reads);
			disk_stat_add(disk, read_ticks, duration);
2286 2287 2288
			break;
		}
		disk_round_stats(disk);
2289
		disk_stat_dec(disk, in_flight);
2290
	}
2291
	__blk_put_request(req->q, req);
2292 2293 2294
	/* Do this LAST! The structure may be freed immediately afterwards */
	if (waiting)
		complete(waiting);
Linus Torvalds's avatar
Linus Torvalds committed
2295 2296
}

2297 2298 2299 2300 2301 2302 2303 2304 2305
void end_request(struct request *req, int uptodate)
{
	if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) {
		add_disk_randomness(req->rq_disk);
		blkdev_dequeue_request(req);
		end_that_request_last(req);
	}
}

2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio)
{
	/* first three bits are identical in rq->flags and bio->bi_rw */
	rq->flags |= (bio->bi_rw & 7);

	rq->nr_phys_segments = bio_phys_segments(q, bio);
	rq->nr_hw_segments = bio_hw_segments(q, bio);
	rq->current_nr_sectors = bio_cur_sectors(bio);
	rq->hard_cur_sectors = rq->current_nr_sectors;
	rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
Jens Axboe's avatar
Jens Axboe committed
2316 2317
	rq->nr_cbio_segments = bio_segments(bio);
	rq->nr_cbio_sectors = bio_sectors(bio);
2318 2319
	rq->buffer = bio_data(bio);

Jens Axboe's avatar
Jens Axboe committed
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
	rq->cbio = rq->bio = rq->biotail = bio;
}

void blk_rq_prep_restart(struct request *rq)
{
	struct bio *bio;

	bio = rq->cbio = rq->bio;
	if (bio) {
		rq->nr_cbio_segments = bio_segments(bio);
		rq->nr_cbio_sectors = bio_sectors(bio);
		rq->hard_cur_sectors = bio_cur_sectors(bio);
		rq->buffer = bio_data(bio);
	}
	rq->sector = rq->hard_sector;
	rq->nr_sectors = rq->hard_nr_sectors;
	rq->current_nr_sectors = rq->hard_cur_sectors;
2337 2338
}

2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
int kblockd_schedule_work(struct work_struct *work)
{
	return queue_work(kblockd_workqueue, work);
}

void kblockd_flush(void)
{
	flush_workqueue(kblockd_workqueue);
}

Linus Torvalds's avatar
Linus Torvalds committed
2349 2350
int __init blk_dev_init(void)
{
2351
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
2352

2353 2354 2355 2356
	kblockd_workqueue = create_workqueue("kblockd");
	if (!kblockd_workqueue)
		panic("Failed to create kblockd\n");

Linus Torvalds's avatar
Linus Torvalds committed
2357
	request_cachep = kmem_cache_create("blkdev_requests",
2358
			sizeof(struct request), 0, 0, NULL, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
2359 2360 2361
	if (!request_cachep)
		panic("Can't create request pool slab cache\n");

Linus Torvalds's avatar
Linus Torvalds committed
2362 2363 2364
	blk_max_low_pfn = max_low_pfn;
	blk_max_pfn = max_pfn;

2365 2366
	for (i = 0; i < ARRAY_SIZE(congestion_wqh); i++)
		init_waitqueue_head(&congestion_wqh[i]);
Linus Torvalds's avatar
Linus Torvalds committed
2367
	return 0;
2368
}
Linus Torvalds's avatar
Linus Torvalds committed
2369

2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
/*
 * sysfs parts below
 */
struct queue_sysfs_entry {
	struct attribute attr;
	ssize_t (*show)(struct request_queue *, char *);
	ssize_t (*store)(struct request_queue *, const char *, size_t);
};

static ssize_t
queue_var_show(unsigned int var, char *page)
{
	return sprintf(page, "%d\n", var);
}

static ssize_t
queue_var_store(unsigned long *var, const char *page, size_t count)
{
	char *p = (char *) page;

	*var = simple_strtoul(p, &p, 10);
	return count;
}

static ssize_t queue_requests_show(struct request_queue *q, char *page)
{
	return queue_var_show(q->nr_requests, (page));
}

static ssize_t
queue_requests_store(struct request_queue *q, const char *page, size_t count)
{
	struct request_list *rl = &q->rq;

	int ret = queue_var_store(&q->nr_requests, page, count);
	if (q->nr_requests < BLKDEV_MIN_RQ)
		q->nr_requests = BLKDEV_MIN_RQ;

2408
	if (rl->count[READ] >= queue_congestion_on_threshold(q))
2409
		set_queue_congested(q, READ);
2410
	else if (rl->count[READ] < queue_congestion_off_threshold(q))
2411 2412
		clear_queue_congested(q, READ);

2413 2414 2415 2416
	if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
		set_queue_congested(q, WRITE);
	else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
		clear_queue_congested(q, WRITE);
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512

	return ret;
}

static struct queue_sysfs_entry queue_requests_entry = {
	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
	.show = queue_requests_show,
	.store = queue_requests_store,
};

static struct attribute *default_attrs[] = {
	&queue_requests_entry.attr,
	NULL,
};

#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)

static ssize_t
queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
{
	struct queue_sysfs_entry *entry = to_queue(attr);
	struct request_queue *q;

	q = container_of(kobj, struct request_queue, kobj);
	if (!entry->show)
		return 0;

	return entry->show(q, page);
}

static ssize_t
queue_attr_store(struct kobject *kobj, struct attribute *attr,
		    const char *page, size_t length)
{
	struct queue_sysfs_entry *entry = to_queue(attr);
	struct request_queue *q;

	q = container_of(kobj, struct request_queue, kobj);
	if (!entry->store)
		return -EINVAL;

	return entry->store(q, page, length);
}

static struct sysfs_ops queue_sysfs_ops = {
	.show	= queue_attr_show,
	.store	= queue_attr_store,
};

struct kobj_type queue_ktype = {
	.sysfs_ops	= &queue_sysfs_ops,
	.default_attrs	= default_attrs,
};

int blk_register_queue(struct gendisk *disk)
{
	int ret;

	request_queue_t *q = disk->queue;

	if (!q)
		return -ENXIO;

	q->kobj.parent = kobject_get(&disk->kobj);
	if (!q->kobj.parent)
		return -EBUSY;

	snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue");
	q->kobj.ktype = &queue_ktype;

	ret = kobject_register(&q->kobj);
	if (ret < 0)
		return ret;

	ret = elv_register_queue(q);
	if (ret) {
		kobject_unregister(&q->kobj);
		return ret;
	}

	return 0;
}

void blk_unregister_queue(struct gendisk *disk)
{
	request_queue_t *q = disk->queue;

	if (q) {
		elv_unregister_queue(q);

		kobject_unregister(&q->kobj);
		kobject_put(&disk->kobj);
	}
}


Jens Axboe's avatar
Jens Axboe committed
2513
EXPORT_SYMBOL(process_that_request_first);
Linus Torvalds's avatar
Linus Torvalds committed
2514
EXPORT_SYMBOL(end_that_request_first);
2515
EXPORT_SYMBOL(end_that_request_chunk);
Linus Torvalds's avatar
Linus Torvalds committed
2516
EXPORT_SYMBOL(end_that_request_last);
2517
EXPORT_SYMBOL(end_request);
Linus Torvalds's avatar
Linus Torvalds committed
2518 2519 2520
EXPORT_SYMBOL(blk_init_queue);
EXPORT_SYMBOL(blk_cleanup_queue);
EXPORT_SYMBOL(blk_queue_make_request);
Linus Torvalds's avatar
Linus Torvalds committed
2521
EXPORT_SYMBOL(blk_queue_bounce_limit);
Linus Torvalds's avatar
Linus Torvalds committed
2522
EXPORT_SYMBOL(generic_make_request);
Linus Torvalds's avatar
Linus Torvalds committed
2523
EXPORT_SYMBOL(generic_unplug_device);
Jens Axboe's avatar
Jens Axboe committed
2524 2525
EXPORT_SYMBOL(blk_plug_device);
EXPORT_SYMBOL(blk_remove_plug);
Linus Torvalds's avatar
Linus Torvalds committed
2526
EXPORT_SYMBOL(blk_attempt_remerge);
Martin Dalecki's avatar
Martin Dalecki committed
2527
EXPORT_SYMBOL(__blk_attempt_remerge);
Linus Torvalds's avatar
Linus Torvalds committed
2528
EXPORT_SYMBOL(blk_max_low_pfn);
2529
EXPORT_SYMBOL(blk_max_pfn);
Linus Torvalds's avatar
Linus Torvalds committed
2530
EXPORT_SYMBOL(blk_queue_max_sectors);
Linus Torvalds's avatar
Linus Torvalds committed
2531 2532
EXPORT_SYMBOL(blk_queue_max_phys_segments);
EXPORT_SYMBOL(blk_queue_max_hw_segments);
Linus Torvalds's avatar
Linus Torvalds committed
2533 2534
EXPORT_SYMBOL(blk_queue_max_segment_size);
EXPORT_SYMBOL(blk_queue_hardsect_size);
Linus Torvalds's avatar
Linus Torvalds committed
2535
EXPORT_SYMBOL(blk_queue_segment_boundary);
Jens Axboe's avatar
Jens Axboe committed
2536
EXPORT_SYMBOL(blk_queue_dma_alignment);
Linus Torvalds's avatar
Linus Torvalds committed
2537
EXPORT_SYMBOL(blk_rq_map_sg);
Linus Torvalds's avatar
Linus Torvalds committed
2538
EXPORT_SYMBOL(blk_dump_rq_flags);
Linus Torvalds's avatar
Linus Torvalds committed
2539
EXPORT_SYMBOL(submit_bio);
Linus Torvalds's avatar
Linus Torvalds committed
2540 2541
EXPORT_SYMBOL(blk_phys_contig_segment);
EXPORT_SYMBOL(blk_hw_contig_segment);
2542 2543
EXPORT_SYMBOL(blk_get_request);
EXPORT_SYMBOL(blk_put_request);
2544
EXPORT_SYMBOL(blk_insert_request);
Linus Torvalds's avatar
Linus Torvalds committed
2545 2546

EXPORT_SYMBOL(blk_queue_prep_rq);
2547
EXPORT_SYMBOL(blk_queue_merge_bvec);
2548

Jens Axboe's avatar
Jens Axboe committed
2549
EXPORT_SYMBOL(blk_queue_find_tag);
2550 2551 2552 2553 2554
EXPORT_SYMBOL(blk_queue_init_tags);
EXPORT_SYMBOL(blk_queue_free_tags);
EXPORT_SYMBOL(blk_queue_start_tag);
EXPORT_SYMBOL(blk_queue_end_tag);
EXPORT_SYMBOL(blk_queue_invalidate_tags);
Jens Axboe's avatar
Jens Axboe committed
2555 2556 2557

EXPORT_SYMBOL(blk_start_queue);
EXPORT_SYMBOL(blk_stop_queue);
2558
EXPORT_SYMBOL(blk_run_queue);
Jens Axboe's avatar
Jens Axboe committed
2559
EXPORT_SYMBOL(blk_run_queues);
2560 2561

EXPORT_SYMBOL(blk_rq_bio_prep);