ll_rw_blk.c 76.1 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
#include <linux/blkdev.h>
21
#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>
Andrew Morton's avatar
Andrew Morton committed
30
#include <linux/writeback.h>
Linus Torvalds's avatar
Linus Torvalds committed
31

32 33 34 35 36
/*
 * for max sense size
 */
#include <scsi/scsi_cmnd.h>

37
static void blk_unplug_work(void *data);
38
static void blk_unplug_timeout(unsigned long data);
39

Linus Torvalds's avatar
Linus Torvalds committed
40 41 42 43 44
/*
 * For the allocated request tables
 */
static kmem_cache_t *request_cachep;

45 46 47 48
static wait_queue_head_t congestion_wqh[2] = {
		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
	};
49

50 51 52 53 54 55 56
/*
 * Controlling structure to kblockd
 */
static struct workqueue_struct *kblockd_workqueue; 

unsigned long blk_max_low_pfn, blk_max_pfn;

57 58 59
EXPORT_SYMBOL(blk_max_low_pfn);
EXPORT_SYMBOL(blk_max_pfn);

60 61 62 63 64
/* Amount of time in which a process may batch requests */
#define BLK_BATCH_TIME	(HZ/50UL)

/* Number of requests a "batching" process may submit */
#define BLK_BATCH_REQ	32
65

66
/*
67
 * Return the threshold (number of used requests) at which the queue is
68 69 70
 * considered to be congested.  It include a little hysteresis to keep the
 * context switch rate down.
 */
71
static inline int queue_congestion_on_threshold(struct request_queue *q)
72 73 74
{
	int ret;

75 76 77 78 79
	ret = q->nr_requests - (q->nr_requests / 8) + 1;

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

80 81 82 83 84 85
	return ret;
}

/*
 * The threshold at which a queue is considered to be uncongested
 */
86
static inline int queue_congestion_off_threshold(struct request_queue *q)
87 88 89
{
	int ret;

90 91 92 93 94
	ret = q->nr_requests - (q->nr_requests / 8) - 1;

	if (ret < 1)
		ret = 1;

95 96 97
	return ret;
}

98 99 100 101 102
/*
 * 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.
 */
103 104 105
static void clear_queue_congested(request_queue_t *q, int rw)
{
	enum bdi_state bit;
106
	wait_queue_head_t *wqh = &congestion_wqh[rw];
107 108

	bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
109
	clear_bit(bit, &q->backing_dev_info.state);
110
	smp_mb__after_clear_bit();
111 112
	if (waitqueue_active(wqh))
		wake_up(wqh);
113 114
}

115 116 117 118
/*
 * A queue has just entered congestion.  Flag that in the queue's VM-visible
 * state flags and increment the global gounter of congested queues.
 */
119 120 121 122 123
static void set_queue_congested(request_queue_t *q, int rw)
{
	enum bdi_state bit;

	bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
124
	set_bit(bit, &q->backing_dev_info.state);
125 126
}

Andrew Morton's avatar
Andrew Morton committed
127
/**
128
 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
129
 * @bdev:	device
Andrew Morton's avatar
Andrew Morton committed
130
 *
Andrew Morton's avatar
Andrew Morton committed
131
 * Locates the passed device's request queue and returns the address of its
132
 * backing_dev_info
Andrew Morton's avatar
Andrew Morton committed
133
 *
Andrew Morton's avatar
Andrew Morton committed
134
 * Will return NULL if the request queue cannot be located.
Andrew Morton's avatar
Andrew Morton committed
135
 */
136
struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
Andrew Morton's avatar
Andrew Morton committed
137
{
138
	struct backing_dev_info *ret = NULL;
139
	request_queue_t *q = bdev_get_queue(bdev);
Andrew Morton's avatar
Andrew Morton committed
140

Andrew Morton's avatar
Andrew Morton committed
141
	if (q)
142
		ret = &q->backing_dev_info;
Andrew Morton's avatar
Andrew Morton committed
143 144 145
	return ret;
}

Jens Axboe's avatar
Jens Axboe committed
146 147 148 149 150 151
void blk_queue_activity_fn(request_queue_t *q, activity_fn *fn, void *data)
{
	q->activity_fn = fn;
	q->activity_data = data;
}

152 153
EXPORT_SYMBOL(blk_queue_activity_fn);

154 155 156 157 158 159 160 161 162 163 164
/**
 * 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
165 166 167 168 169
void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn)
{
	q->prep_rq_fn = pfn;
}

170 171
EXPORT_SYMBOL(blk_queue_prep_rq);

172 173 174 175 176 177 178 179 180 181
/**
 * 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
182 183 184 185 186
 * the size of bio's sent to it. Note that a block device *must* allow a
 * single page to be added to an empty bio. The block device driver may want
 * to use the bio_split() function to deal with these bio's. By default
 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
 * honored.
187 188 189 190 191 192
 */
void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn)
{
	q->merge_bvec_fn = mbfn;
}

193 194
EXPORT_SYMBOL(blk_queue_merge_bvec);

Linus Torvalds's avatar
Linus Torvalds committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
/**
 * 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
214
 *    __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
Linus Torvalds's avatar
Linus Torvalds committed
215 216 217
 *    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
218
{
Linus Torvalds's avatar
Linus Torvalds committed
219 220 221
	/*
	 * set defaults
	 */
222
	q->nr_requests = BLKDEV_MAX_RQ;
Linus Torvalds's avatar
Linus Torvalds committed
223 224
	q->max_phys_segments = MAX_PHYS_SEGMENTS;
	q->max_hw_segments = MAX_HW_SEGMENTS;
Linus Torvalds's avatar
Linus Torvalds committed
225
	q->make_request_fn = mfn;
226 227
	q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
	q->backing_dev_info.state = 0;
228
	q->backing_dev_info.memory_backed = 0;
Linus Torvalds's avatar
Linus Torvalds committed
229 230
	blk_queue_max_sectors(q, MAX_SECTORS);
	blk_queue_hardsect_size(q, 512);
Jens Axboe's avatar
Jens Axboe committed
231
	blk_queue_dma_alignment(q, 511);
Linus Torvalds's avatar
Linus Torvalds committed
232

233 234 235 236 237 238 239
	q->unplug_thresh = 4;		/* hmm */
	q->unplug_delay = (3 * HZ) / 1000;	/* 3 milliseconds */
	if (q->unplug_delay == 0)
		q->unplug_delay = 1;

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

240 241 242
	q->unplug_timer.function = blk_unplug_timeout;
	q->unplug_timer.data = (unsigned long)q;

Linus Torvalds's avatar
Linus Torvalds committed
243 244 245 246 247
	/*
	 * by default assume old behaviour and bounce for any highmem page
	 */
	blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);

Jens Axboe's avatar
Jens Axboe committed
248
	blk_queue_activity_fn(q, NULL, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
249 250
}

251 252
EXPORT_SYMBOL(blk_queue_make_request);

Linus Torvalds's avatar
Linus Torvalds committed
253
/**
Linus Torvalds's avatar
Linus Torvalds committed
254 255 256
 * 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
257 258
 *
 * Description:
Linus Torvalds's avatar
Linus Torvalds committed
259 260 261 262 263
 *    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
264
 **/
Linus Torvalds's avatar
Linus Torvalds committed
265
void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
Linus Torvalds's avatar
Linus Torvalds committed
266
{
Linus Torvalds's avatar
Linus Torvalds committed
267
	unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
268

Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273
	/*
	 * 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
274 275
	if (bounce_pfn < blk_max_low_pfn) {
		BUG_ON(dma_addr < BLK_BOUNCE_ISA);
Linus Torvalds's avatar
Linus Torvalds committed
276 277 278
		init_emergency_isa_pool();
		q->bounce_gfp = GFP_NOIO | GFP_DMA;
	} else
Andrew Morton's avatar
Andrew Morton committed
279
		q->bounce_gfp = GFP_NOIO;
Linus Torvalds's avatar
Linus Torvalds committed
280

Linus Torvalds's avatar
Linus Torvalds committed
281
	q->bounce_pfn = bounce_pfn;
Linus Torvalds's avatar
Linus Torvalds committed
282 283
}

284
EXPORT_SYMBOL(blk_queue_bounce_limit);
Linus Torvalds's avatar
Linus Torvalds committed
285

Linus Torvalds's avatar
Linus Torvalds committed
286
/**
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289
 * 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
290 291
 *
 * Description:
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296
 *    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)
{
297 298 299 300 301
	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
302 303 304
	q->max_sectors = max_sectors;
}

305 306
EXPORT_SYMBOL(blk_queue_max_sectors);

Linus Torvalds's avatar
Linus Torvalds committed
307
/**
Linus Torvalds's avatar
Linus Torvalds committed
308
 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
Linus Torvalds's avatar
Linus Torvalds committed
309 310
 * @q:  the request queue for the device
 * @max_segments:  max number of segments
Linus Torvalds's avatar
Linus Torvalds committed
311
 *
Linus Torvalds's avatar
Linus Torvalds committed
312 313
 * Description:
 *    Enables a low level driver to set an upper limit on the number of
Linus Torvalds's avatar
Linus Torvalds committed
314 315
 *    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
316
 **/
Linus Torvalds's avatar
Linus Torvalds committed
317
void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
Linus Torvalds's avatar
Linus Torvalds committed
318
{
319 320 321 322 323
	if (!max_segments) {
		max_segments = 1;
		printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
	}

Linus Torvalds's avatar
Linus Torvalds committed
324 325 326
	q->max_phys_segments = max_segments;
}

327 328
EXPORT_SYMBOL(blk_queue_max_phys_segments);

Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332 333 334 335 336 337 338 339 340 341
/**
 * 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)
{
342 343 344 345 346
	if (!max_segments) {
		max_segments = 1;
		printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
	}

Linus Torvalds's avatar
Linus Torvalds committed
347
	q->max_hw_segments = max_segments;
Linus Torvalds's avatar
Linus Torvalds committed
348 349
}

350 351
EXPORT_SYMBOL(blk_queue_max_hw_segments);

Linus Torvalds's avatar
Linus Torvalds committed
352
/**
Linus Torvalds's avatar
Linus Torvalds committed
353 354 355
 * 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
356 357
 *
 * Description:
Linus Torvalds's avatar
Linus Torvalds committed
358 359 360 361 362
 *    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)
{
363 364 365 366 367
	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
368 369 370
	q->max_segment_size = max_size;
}

371 372
EXPORT_SYMBOL(blk_queue_max_segment_size);

Linus Torvalds's avatar
Linus Torvalds committed
373 374 375 376
/**
 * 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
377
 *
Linus Torvalds's avatar
Linus Torvalds committed
378 379 380 381 382
 * 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
383
 **/
Linus Torvalds's avatar
Linus Torvalds committed
384 385 386 387
void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
{
	q->hardsect_size = size;
}
Linus Torvalds's avatar
Linus Torvalds committed
388

389 390
EXPORT_SYMBOL(blk_queue_hardsect_size);

391 392 393 394 395
/*
 * Returns the minimum that is _not_ zero, unless both are zero.
 */
#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))

396 397 398 399 400 401 402
/**
 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
 * @t:	the stacking driver (top)
 * @b:  the underlying device (bottom)
 **/
void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b)
{
403 404 405
	/* zero is "infinity" */
	t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);

406 407 408 409 410 411
	t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
	t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
	t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
	t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
}

412 413
EXPORT_SYMBOL(blk_queue_stack_limits);

Linus Torvalds's avatar
Linus Torvalds committed
414 415 416 417 418 419 420
/**
 * 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)
{
421 422 423 424 425
	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
426 427 428
	q->seg_boundary_mask = mask;
}

429 430
EXPORT_SYMBOL(blk_queue_segment_boundary);

Jens Axboe's avatar
Jens Axboe committed
431 432
/**
 * blk_queue_dma_alignment - set dma length and memory alignment
433 434
 * @q:     the request queue for the device
 * @mask:  alignment mask
Jens Axboe's avatar
Jens Axboe committed
435 436 437 438 439 440 441 442 443 444 445
 *
 * 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;
}

446 447
EXPORT_SYMBOL(blk_queue_dma_alignment);

Jens Axboe's avatar
Jens Axboe committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
/**
 * 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
464
	if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
Jens Axboe's avatar
Jens Axboe committed
465 466 467 468
		return NULL;

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

470 471
EXPORT_SYMBOL(blk_queue_find_tag);

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/**
 * 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;

488 489 490
	if (atomic_dec_and_test(&bqt->refcnt)) {
		BUG_ON(bqt->busy);
		BUG_ON(!list_empty(&bqt->busy_list));
491

492 493
		kfree(bqt->tag_index);
		bqt->tag_index = NULL;
494

495 496 497 498 499
		kfree(bqt->tag_map);
		bqt->tag_map = NULL;

		kfree(bqt);
	}
500 501 502 503 504

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

505 506
EXPORT_SYMBOL(blk_queue_free_tags);

507 508
static int
init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
509 510 511
{
	int bits, i;

512 513 514 515
	if (depth > q->nr_requests * 2) {
		depth = q->nr_requests * 2;
		printk(KERN_ERR "%s: adjusted depth to %d\n",
				__FUNCTION__, depth);
516
	}
517 518 519

	tags->tag_index = kmalloc(depth * sizeof(struct request *), GFP_ATOMIC);
	if (!tags->tag_index)
Jens Axboe's avatar
Jens Axboe committed
520
		goto fail;
521 522 523 524

	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
525
		goto fail;
526

Jens Axboe's avatar
Jens Axboe committed
527 528
	memset(tags->tag_index, 0, depth * sizeof(struct request *));
	memset(tags->tag_map, 0, bits * sizeof(unsigned long));
529
	tags->max_depth = depth;
Jens Axboe's avatar
Jens Axboe committed
530
	tags->real_max_depth = bits * BITS_PER_LONG;
531 532 533 534 535

	/*
	 * 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++)
536
		__set_bit(i, tags->tag_map);
537

538 539 540
	INIT_LIST_HEAD(&tags->busy_list);
	tags->busy = 0;
	atomic_set(&tags->refcnt, 1);
Jens Axboe's avatar
Jens Axboe committed
541 542 543 544 545 546 547 548 549 550 551
	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
 **/
552 553
int blk_queue_init_tags(request_queue_t *q, int depth,
			struct blk_queue_tag *tags)
Jens Axboe's avatar
Jens Axboe committed
554
{
555 556 557 558
	if (!tags) {
		tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
		if (!tags)
			goto fail;
Jens Axboe's avatar
Jens Axboe committed
559

560 561 562 563
		if (init_tag_map(q, tags, depth))
			goto fail;
	} else
		atomic_inc(&tags->refcnt);
Jens Axboe's avatar
Jens Axboe committed
564

565 566 567 568 569 570 571
	/*
	 * 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
572
	kfree(tags);
573 574 575
	return -ENOMEM;
}

576 577
EXPORT_SYMBOL(blk_queue_init_tags);

Jens Axboe's avatar
Jens Axboe committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
/**
 * 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;

611
	if (init_tag_map(q, bqt, new_depth))
Jens Axboe's avatar
Jens Axboe committed
612 613 614 615
		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
616
	memcpy(bqt->tag_map, tag_map, bits * sizeof(unsigned long));
Jens Axboe's avatar
Jens Axboe committed
617 618 619 620 621 622

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

623 624 625
/**
 * blk_queue_end_tag - end tag operations for a request
 * @q:  the request queue for the device
626
 * @rq: the request that has completed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
 *
 *  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
644
	if (unlikely(tag >= bqt->real_max_depth))
645 646 647 648 649 650 651
		return;

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

652
	list_del_init(&rq->queuelist);
653 654 655 656 657 658 659 660 661 662
	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--;
}

663 664
EXPORT_SYMBOL(blk_queue_end_tag);

665 666 667 668 669 670 671 672
/**
 * 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
673 674 675 676 677 678
 *    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.
679 680 681 682 683 684 685 686 687 688
 *
 *  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
689 690
	if (unlikely((rq->flags & REQ_QUEUED))) {
		printk(KERN_ERR 
691 692
		       "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
693 694
		BUG();
	}
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

	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;
}

715 716
EXPORT_SYMBOL(blk_queue_start_tag);

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
/**
 * 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;
732
	struct list_head *tmp, *n;
733 734
	struct request *rq;

735
	list_for_each_safe(tmp, n, &bqt->busy_list) {
736 737
		rq = list_entry_rq(tmp);

738 739
		if (rq->tag == -1) {
			printk("bad tag found on list\n");
740
			list_del_init(&rq->queuelist);
Jens Axboe's avatar
Jens Axboe committed
741
			rq->flags &= ~REQ_QUEUED;
742 743 744
		} else
			blk_queue_end_tag(q, rq);

745
		rq->flags &= ~REQ_STARTED;
Jens Axboe's avatar
Jens Axboe committed
746
		__elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
747 748 749
	}
}

750 751
EXPORT_SYMBOL(blk_queue_invalidate_tags);

Martin Dalecki's avatar
Martin Dalecki committed
752 753
static char *rq_flags[] = {
	"REQ_RW",
Jens Axboe's avatar
Jens Axboe committed
754
	"REQ_FAILFAST",
Jens Axboe's avatar
Jens Axboe committed
755 756
	"REQ_SOFTBARRIER",
	"REQ_HARDBARRIER",
Martin Dalecki's avatar
Martin Dalecki committed
757 758 759 760 761 762 763 764
	"REQ_CMD",
	"REQ_NOMERGE",
	"REQ_STARTED",
	"REQ_DONTPREP",
	"REQ_QUEUED",
	"REQ_PC",
	"REQ_BLOCK_PC",
	"REQ_SENSE",
765 766
	"REQ_FAILED",
	"REQ_QUIET",
767
	"REQ_SPECIAL",
768 769 770
	"REQ_DRIVE_CMD",
	"REQ_DRIVE_TASK",
	"REQ_DRIVE_TASKFILE",
Jens Axboe's avatar
Jens Axboe committed
771 772 773 774
	"REQ_PREEMPT",
	"REQ_PM_SUSPEND",
	"REQ_PM_RESUME",
	"REQ_PM_SHUTDOWN",
Martin Dalecki's avatar
Martin Dalecki committed
775
};
Linus Torvalds's avatar
Linus Torvalds committed
776 777 778 779 780

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

781 782
	printk("%s: dev %s: flags = ", msg,
		rq->rq_disk ? rq->rq_disk->disk_name : "?");
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786 787 788 789
	bit = 0;
	do {
		if (rq->flags & (1 << bit))
			printk("%s ", rq_flags[bit]);
		bit++;
	} while (bit < __REQ_NR_BITS);

790
	printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
Linus Torvalds's avatar
Linus Torvalds committed
791 792
						       rq->nr_sectors,
						       rq->current_nr_sectors);
793 794 795 796 797 798 799 800
	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
801 802
}

803 804
EXPORT_SYMBOL(blk_dump_rq_flags);

Linus Torvalds's avatar
Linus Torvalds committed
805 806 807
void blk_recount_segments(request_queue_t *q, struct bio *bio)
{
	struct bio_vec *bv, *bvprv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
808
	int i, nr_phys_segs, nr_hw_segs, seg_size, cluster;
809
	int high, highprv = 1;
Linus Torvalds's avatar
Linus Torvalds committed
810 811 812 813 814

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

	cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
Linus Torvalds's avatar
Linus Torvalds committed
815
	seg_size = nr_phys_segs = nr_hw_segs = 0;
Linus Torvalds's avatar
Linus Torvalds committed
816
	bio_for_each_segment(bv, bio, i) {
817 818 819 820 821 822 823 824 825
		/*
		 * the trick here is making sure that a high page is never
		 * considered part of another segment, since that might
		 * change with the bounce page.
		 */
		high = page_to_pfn(bv->bv_page) >= q->bounce_pfn;
		if (high || highprv)
			goto new_hw_segment;
		if (cluster) {
826
			if (seg_size + bv->bv_len > q->max_segment_size)
Linus Torvalds's avatar
Linus Torvalds committed
827
				goto new_segment;
828
			if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
Linus Torvalds's avatar
Linus Torvalds committed
829
				goto new_segment;
830
			if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
Linus Torvalds's avatar
Linus Torvalds committed
831 832 833 834 835 836 837
				goto new_segment;

			seg_size += bv->bv_len;
			bvprv = bv;
			continue;
		}
new_segment:
838 839
		if (!BIOVEC_VIRT_MERGEABLE(bvprv, bv))
new_hw_segment:
840 841 842
			nr_hw_segs++;

		nr_phys_segs++;
Linus Torvalds's avatar
Linus Torvalds committed
843
		bvprv = bv;
Linus Torvalds's avatar
Linus Torvalds committed
844
		seg_size = bv->bv_len;
845
		highprv = high;
Linus Torvalds's avatar
Linus Torvalds committed
846 847
	}

Linus Torvalds's avatar
Linus Torvalds committed
848 849
	bio->bi_phys_segments = nr_phys_segs;
	bio->bi_hw_segments = nr_hw_segs;
Linus Torvalds's avatar
Linus Torvalds committed
850 851 852 853
	bio->bi_flags |= (1 << BIO_SEG_VALID);
}


854
int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds's avatar
Linus Torvalds committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
				   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;
}

875 876
EXPORT_SYMBOL(blk_phys_contig_segment);

877
int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds's avatar
Linus Torvalds committed
878
				 struct bio *nxt)
Linus Torvalds's avatar
Linus Torvalds committed
879
{
Linus Torvalds's avatar
Linus Torvalds committed
880 881 882
	if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
883
	if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
Linus Torvalds's avatar
Linus Torvalds committed
884
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
885 886
	if (bio->bi_size + nxt->bi_size > q->max_segment_size)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
887 888

	/*
Linus Torvalds's avatar
Linus Torvalds committed
889 890
	 * 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
891
	 */
Linus Torvalds's avatar
Linus Torvalds committed
892
	if (BIO_SEG_BOUNDARY(q, bio, nxt))
Linus Torvalds's avatar
Linus Torvalds committed
893 894 895
		return 1;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
896 897
}

898 899
EXPORT_SYMBOL(blk_hw_contig_segment);

Linus Torvalds's avatar
Linus Torvalds committed
900 901
/*
 * map a request to scatterlist, return number of sg entries setup. Caller
Linus Torvalds's avatar
Linus Torvalds committed
902
 * must make sure sg can hold rq->nr_phys_segments entries
Linus Torvalds's avatar
Linus Torvalds committed
903 904
 */
int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
Linus Torvalds's avatar
Linus Torvalds committed
905
{
Linus Torvalds's avatar
Linus Torvalds committed
906
	struct bio_vec *bvec, *bvprv;
Linus Torvalds's avatar
Linus Torvalds committed
907
	struct bio *bio;
Linus Torvalds's avatar
Linus Torvalds committed
908
	int nsegs, i, cluster;
Linus Torvalds's avatar
Linus Torvalds committed
909 910

	nsegs = 0;
Linus Torvalds's avatar
Linus Torvalds committed
911
	cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
Linus Torvalds's avatar
Linus Torvalds committed
912 913 914 915

	/*
	 * for each bio in rq
	 */
Linus Torvalds's avatar
Linus Torvalds committed
916
	bvprv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
917 918 919 920 921 922 923
	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
924
			if (bvprv && cluster) {
Linus Torvalds's avatar
Linus Torvalds committed
925
				if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
Linus Torvalds's avatar
Linus Torvalds committed
926 927
					goto new_segment;

Linus Torvalds's avatar
Linus Torvalds committed
928
				if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Linus Torvalds's avatar
Linus Torvalds committed
929 930 931
					goto new_segment;
				if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
					goto new_segment;
Linus Torvalds's avatar
Linus Torvalds committed
932 933 934 935

				sg[nsegs - 1].length += nbytes;
			} else {
new_segment:
Linus Torvalds's avatar
Linus Torvalds committed
936
				memset(&sg[nsegs],0,sizeof(struct scatterlist));
Linus Torvalds's avatar
Linus Torvalds committed
937 938 939 940 941 942
				sg[nsegs].page = bvec->bv_page;
				sg[nsegs].length = nbytes;
				sg[nsegs].offset = bvec->bv_offset;

				nsegs++;
			}
Linus Torvalds's avatar
Linus Torvalds committed
943
			bvprv = bvec;
Linus Torvalds's avatar
Linus Torvalds committed
944 945 946 947 948 949
		} /* segments in bio */
	} /* bios in rq */

	return nsegs;
}

950 951
EXPORT_SYMBOL(blk_rq_map_sg);

Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955
/*
 * the standard queue merge functions, can be overridden with device
 * specific ones if so desired
 */
Linus Torvalds's avatar
Linus Torvalds committed
956 957 958 959

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

Linus Torvalds's avatar
Linus Torvalds committed
963 964
	if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
965
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
966
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
967
	}
Linus Torvalds's avatar
Linus Torvalds committed
968

Linus Torvalds's avatar
Linus Torvalds committed
969 970 971 972 973 974 975 976 977 978 979 980 981
	/*
	 * 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
982
	int nr_phys_segs = bio_phys_segments(q, bio);
Linus Torvalds's avatar
Linus Torvalds committed
983

Linus Torvalds's avatar
Linus Torvalds committed
984 985
	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
986
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
987
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990 991 992 993 994 995
		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
996
	req->nr_phys_segments += nr_phys_segs;
Linus Torvalds's avatar
Linus Torvalds committed
997
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
998 999 1000
}

static int ll_back_merge_fn(request_queue_t *q, struct request *req, 
Linus Torvalds's avatar
Linus Torvalds committed
1001
			    struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
1002
{
Linus Torvalds's avatar
Linus Torvalds committed
1003 1004
	if (req->nr_sectors + bio_sectors(bio) > q->max_sectors) {
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
1005
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1006
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1007 1008
	}

Linus Torvalds's avatar
Linus Torvalds committed
1009
	if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)))
Linus Torvalds's avatar
Linus Torvalds committed
1010 1011 1012
		return ll_new_mergeable(q, req, bio);

	return ll_new_hw_segment(q, req, bio);
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015
}

static int ll_front_merge_fn(request_queue_t *q, struct request *req, 
Linus Torvalds's avatar
Linus Torvalds committed
1016
			     struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
1017
{
Linus Torvalds's avatar
Linus Torvalds committed
1018 1019
	if (req->nr_sectors + bio_sectors(bio) > q->max_sectors) {
		req->flags |= REQ_NOMERGE;
Linus Torvalds's avatar
Linus Torvalds committed
1020
		q->last_merge = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1021
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1022 1023
	}

Linus Torvalds's avatar
Linus Torvalds committed
1024
	if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)))
Linus Torvalds's avatar
Linus Torvalds committed
1025 1026 1027
		return ll_new_mergeable(q, req, bio);

	return ll_new_hw_segment(q, req, bio);
Linus Torvalds's avatar
Linus Torvalds committed
1028 1029 1030
}

static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
Linus Torvalds's avatar
Linus Torvalds committed
1031
				struct request *next)
Linus Torvalds's avatar
Linus Torvalds committed
1032
{
Linus Torvalds's avatar
Linus Torvalds committed
1033
	int total_phys_segments = req->nr_phys_segments +next->nr_phys_segments;
Linus Torvalds's avatar
Linus Torvalds committed
1034
	int total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
Linus Torvalds's avatar
Linus Torvalds committed
1035

Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	/*
	 * 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--;
1059

Linus Torvalds's avatar
Linus Torvalds committed
1060
	if (total_hw_segments > q->max_hw_segments)
Linus Torvalds's avatar
Linus Torvalds committed
1061 1062
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
1063 1064 1065
	/* Merge is OK... */
	req->nr_phys_segments = total_phys_segments;
	req->nr_hw_segments = total_hw_segments;
Linus Torvalds's avatar
Linus Torvalds committed
1066 1067 1068 1069 1070 1071 1072 1073
	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
1074 1075
 * 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
1076
 */
Linus Torvalds's avatar
Linus Torvalds committed
1077
void blk_plug_device(request_queue_t *q)
Linus Torvalds's avatar
Linus Torvalds committed
1078
{
1079
	WARN_ON(!irqs_disabled());
Jens Axboe's avatar
Jens Axboe committed
1080 1081 1082 1083 1084

	/*
	 * don't plug a stopped queue, it must be paired with blk_start_queue()
	 * which will restart the queueing
	 */
1085 1086 1087 1088
	if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
		return;

	if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1089
		mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Linus Torvalds's avatar
Linus Torvalds committed
1090 1091
}

1092 1093
EXPORT_SYMBOL(blk_plug_device);

Jens Axboe's avatar
Jens Axboe committed
1094 1095 1096 1097
/*
 * remove the queue from the plugged list, if present. called with
 * queue lock held and interrupts disabled.
 */
1098
int blk_remove_plug(request_queue_t *q)
Jens Axboe's avatar
Jens Axboe committed
1099
{
1100
	WARN_ON(!irqs_disabled());
Jens Axboe's avatar
Jens Axboe committed
1101

1102 1103 1104 1105 1106
	if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
		return 0;

	del_timer(&q->unplug_timer);
	return 1;
Jens Axboe's avatar
Jens Axboe committed
1107 1108
}

1109 1110
EXPORT_SYMBOL(blk_remove_plug);

Linus Torvalds's avatar
Linus Torvalds committed
1111 1112 1113 1114 1115
/*
 * remove the plug and let it rip..
 */
static inline void __generic_unplug_device(request_queue_t *q)
{
1116
	if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
Jens Axboe's avatar
Jens Axboe committed
1117 1118
		return;

1119
	if (!blk_remove_plug(q))
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121 1122 1123 1124
		return;

	/*
	 * was plugged, fire request_fn if queue has stuff to do
	 */
1125
	if (elv_next_request(q))
Linus Torvalds's avatar
Linus Torvalds committed
1126
		q->request_fn(q);
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128
}

Linus Torvalds's avatar
Linus Torvalds committed
1129 1130
/**
 * generic_unplug_device - fire a request queue
1131
 * @q:    The &request_queue_t in question
Linus Torvalds's avatar
Linus Torvalds committed
1132 1133 1134 1135 1136
 *
 * 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
1137 1138
 *   gets unplugged, the request_fn defined for the queue is invoked and
 *   transfers started.
Linus Torvalds's avatar
Linus Torvalds committed
1139
 **/
1140
void generic_unplug_device(request_queue_t *q)
Jens Axboe's avatar
Jens Axboe committed
1141
{
Jens Axboe's avatar
Jens Axboe committed
1142
	spin_lock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1143
	__generic_unplug_device(q);
Jens Axboe's avatar
Jens Axboe committed
1144
	spin_unlock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1145
}
1146 1147
EXPORT_SYMBOL(generic_unplug_device);

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
static void blk_backing_dev_unplug(struct backing_dev_info *bdi)
{
	request_queue_t *q = bdi->unplug_io_data;

	/*
	 * devices don't necessarily have an ->unplug_fn defined
	 */
	if (q->unplug_fn)
		q->unplug_fn(q);
}

1159 1160
static void blk_unplug_work(void *data)
{
Andrew Morton's avatar
Andrew Morton committed
1161
	request_queue_t *q = data;
1162

Andrew Morton's avatar
Andrew Morton committed
1163
	q->unplug_fn(q);
1164 1165 1166 1167 1168 1169
}

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

1170
	kblockd_schedule_work(&q->unplug_work);
1171 1172
}

1173 1174 1175 1176 1177 1178 1179
/**
 * 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
Jens Axboe's avatar
Jens Axboe committed
1180
 *   entered. Also see blk_stop_queue(). Queue lock must be held.
1181
 **/
Jens Axboe's avatar
Jens Axboe committed
1182 1183
void blk_start_queue(request_queue_t *q)
{
Andrew Morton's avatar
Andrew Morton committed
1184
	clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
Jens Axboe's avatar
Jens Axboe committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

	/*
	 * one level of recursion is ok and is much faster than kicking
	 * the unplug handling
	 */
	if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
		q->request_fn(q);
		clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
	} else {
		blk_plug_device(q);
1195
		kblockd_schedule_work(&q->unplug_work);
Jens Axboe's avatar
Jens Axboe committed
1196
	}
1197 1198
}

1199 1200
EXPORT_SYMBOL(blk_start_queue);

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
/**
 * 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
1213
 *   blk_start_queue() to restart queue operations. Queue lock must be held.
1214
 **/
Jens Axboe's avatar
Jens Axboe committed
1215 1216
void blk_stop_queue(request_queue_t *q)
{
Jens Axboe's avatar
Jens Axboe committed
1217 1218
	blk_remove_plug(q);
	set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
Jens Axboe's avatar
Jens Axboe committed
1219 1220
}

1221 1222
EXPORT_SYMBOL(blk_stop_queue);

1223 1224
/**
 * blk_run_queue - run a single device queue
1225
 * @q:	The queue to run
1226
 */
1227
void blk_run_queue(struct request_queue *q)
1228
{
1229 1230 1231
	unsigned long flags;

	spin_lock_irqsave(q->queue_lock, flags);
1232 1233
	blk_remove_plug(q);
	q->request_fn(q);
1234
	spin_unlock_irqrestore(q->queue_lock, flags);
1235 1236
}

1237 1238
EXPORT_SYMBOL(blk_run_queue);

Linus Torvalds's avatar
Linus Torvalds committed
1239 1240 1241 1242 1243
/**
 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
 * @q:    the request queue to be released
 *
 * Description:
1244 1245 1246 1247 1248 1249
 *     blk_cleanup_queue is the pair to blk_init_queue() or
 *     blk_queue_make_request().  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 and the queue itself.
 *
1250
 * Caveat:
Linus Torvalds's avatar
Linus Torvalds committed
1251 1252 1253 1254 1255
 *     Hopefully the low level driver will have finished any
 *     outstanding requests first...
 **/
void blk_cleanup_queue(request_queue_t * q)
{
1256
	struct request_list *rl = &q->rq;
Linus Torvalds's avatar
Linus Torvalds committed
1257

1258 1259 1260
	if (!atomic_dec_and_test(&q->refcnt))
		return;

1261 1262
	elevator_exit(q);

1263
	del_timer_sync(&q->unplug_timer);
1264
	kblockd_flush();
1265

1266 1267
	if (rl->rq_pool)
		mempool_destroy(rl->rq_pool);
Linus Torvalds's avatar
Linus Torvalds committed
1268

1269 1270 1271
	if (blk_queue_tagged(q))
		blk_queue_free_tags(q);

1272
	kfree(q);
Linus Torvalds's avatar
Linus Torvalds committed
1273 1274
}

1275 1276
EXPORT_SYMBOL(blk_cleanup_queue);

Linus Torvalds's avatar
Linus Torvalds committed
1277
static int blk_init_free_list(request_queue_t *q)
Linus Torvalds's avatar
Linus Torvalds committed
1278
{
1279
	struct request_list *rl = &q->rq;
Linus Torvalds's avatar
Linus Torvalds committed
1280

1281
	rl->count[READ] = rl->count[WRITE] = 0;
1282 1283
	init_waitqueue_head(&rl->wait[READ]);
	init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds's avatar
Linus Torvalds committed
1284

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

1287 1288
	if (!rl->rq_pool)
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1289

Linus Torvalds's avatar
Linus Torvalds committed
1290
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1291 1292
}

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

1295 1296 1297 1298 1299
static elevator_t *chosen_elevator =
#if defined(CONFIG_IOSCHED_AS)
	&iosched_as;
#elif defined(CONFIG_IOSCHED_DEADLINE)
	&iosched_deadline;
Andrew Morton's avatar
Andrew Morton committed
1300 1301
#elif defined(CONFIG_IOSCHED_CFQ)
	&iosched_cfq;
1302
#elif defined(CONFIG_IOSCHED_NOOP)
1303
	&elevator_noop;
1304 1305 1306
#else
	NULL;
#error "You must have at least 1 I/O scheduler selected"
1307 1308
#endif

1309
#if defined(CONFIG_IOSCHED_AS) || defined(CONFIG_IOSCHED_DEADLINE) || defined (CONFIG_IOSCHED_NOOP)
1310 1311
static int __init elevator_setup(char *str)
{
1312
#ifdef CONFIG_IOSCHED_DEADLINE
1313 1314
	if (!strcmp(str, "deadline"))
		chosen_elevator = &iosched_deadline;
1315 1316
#endif
#ifdef CONFIG_IOSCHED_AS
1317 1318
	if (!strcmp(str, "as"))
		chosen_elevator = &iosched_as;
1319
#endif
Andrew Morton's avatar
Andrew Morton committed
1320 1321 1322 1323
#ifdef CONFIG_IOSCHED_CFQ
	if (!strcmp(str, "cfq"))
		chosen_elevator = &iosched_cfq;
#endif
1324 1325 1326
#ifdef CONFIG_IOSCHED_NOOP
	if (!strcmp(str, "noop"))
		chosen_elevator = &elevator_noop;
1327
#endif
1328 1329
	return 1;
}
1330

1331
__setup("elevator=", elevator_setup);
1332
#endif /* CONFIG_IOSCHED_AS || CONFIG_IOSCHED_DEADLINE || CONFIG_IOSCHED_NOOP */
1333

1334 1335 1336 1337 1338 1339 1340 1341
request_queue_t *blk_alloc_queue(int gfp_mask)
{
	request_queue_t *q = kmalloc(sizeof(*q), gfp_mask);

	if (!q)
		return NULL;

	memset(q, 0, sizeof(*q));
1342
	init_timer(&q->unplug_timer);
1343
	atomic_set(&q->refcnt, 1);
1344 1345 1346 1347

	q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
	q->backing_dev_info.unplug_io_data = q;

1348 1349 1350
	return q;
}

1351 1352
EXPORT_SYMBOL(blk_alloc_queue);

Linus Torvalds's avatar
Linus Torvalds committed
1353 1354 1355 1356
/**
 * blk_init_queue  - prepare a request queue for use with a block device
 * @rfn:  The function to be called to process requests that have been
 *        placed on the queue.
1357
 * @lock: Request queue spin lock
Linus Torvalds's avatar
Linus Torvalds committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
 *
 * 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
1374 1375
 *    The queue spin lock must be held while manipulating the requests on the
 *    request queue.
Linus Torvalds's avatar
Linus Torvalds committed
1376
 *
1377 1378 1379
 *    Function returns a pointer to the initialized request queue, or NULL if
 *    it didn't succeed.
 *
Linus Torvalds's avatar
Linus Torvalds committed
1380
 * Note:
Linus Torvalds's avatar
Linus Torvalds committed
1381
 *    blk_init_queue() must be paired with a blk_cleanup_queue() call
Linus Torvalds's avatar
Linus Torvalds committed
1382 1383
 *    when the block device is deactivated (such as at module unload).
 **/
1384
request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds's avatar
Linus Torvalds committed
1385
{
1386
	request_queue_t *q;
1387
	static int printed;
Linus Torvalds's avatar
Linus Torvalds committed
1388

1389 1390 1391 1392
	q = blk_alloc_queue(GFP_KERNEL);
	if (!q)
		return NULL;

Linus Torvalds's avatar
Linus Torvalds committed
1393
	if (blk_init_free_list(q))
1394
		goto out_init;
Linus Torvalds's avatar
Linus Torvalds committed
1395

1396 1397
	if (!printed) {
		printed = 1;
1398
		printk("Using %s io scheduler\n", chosen_elevator->elevator_name);
1399 1400
	}

1401 1402
	if (elevator_init(q, chosen_elevator))
		goto out_elv;
Linus Torvalds's avatar
Linus Torvalds committed
1403

Linus Torvalds's avatar
Linus Torvalds committed
1404
	q->request_fn		= rfn;
Linus Torvalds's avatar
Linus Torvalds committed
1405 1406 1407
	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
1408
	q->prep_rq_fn		= NULL;
Jens Axboe's avatar
Jens Axboe committed
1409
	q->unplug_fn		= generic_unplug_device;
Linus Torvalds's avatar
Linus Torvalds committed
1410
	q->queue_flags		= (1 << QUEUE_FLAG_CLUSTER);
Linus Torvalds's avatar
Linus Torvalds committed
1411
	q->queue_lock		= lock;
Andrew Morton's avatar
Andrew Morton committed
1412

Linus Torvalds's avatar
Linus Torvalds committed
1413 1414
	blk_queue_segment_boundary(q, 0xffffffff);

Linus Torvalds's avatar
Linus Torvalds committed
1415 1416
	blk_queue_make_request(q, __make_request);
	blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
1417 1418 1419

	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
1420

1421 1422 1423 1424 1425 1426 1427 1428
	return q;
out_elv:
	blk_cleanup_queue(q);
out_init:
	kfree(q);
	return NULL;
}

1429 1430
EXPORT_SYMBOL(blk_init_queue);

1431 1432 1433 1434 1435 1436 1437 1438
int blk_get_queue(request_queue_t *q)
{
	if (!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
		atomic_inc(&q->refcnt);
		return 0;
	}

	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1439 1440
}

1441 1442
EXPORT_SYMBOL(blk_get_queue);

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
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;
}

1463 1464 1465 1466 1467 1468 1469 1470 1471
/*
 * ioc_batching returns true if the ioc is a valid batching request and
 * should be given priority access to a request.
 */
static inline int ioc_batching(struct io_context *ioc)
{
	if (!ioc)
		return 0;

1472 1473 1474 1475 1476
	/*
	 * Make sure the process is able to allocate at least 1 request
	 * even if the batch times out, otherwise we could theoretically
	 * lose wakeups.
	 */
1477 1478 1479 1480 1481 1482
	return ioc->nr_batch_requests == BLK_BATCH_REQ ||
		(ioc->nr_batch_requests > 0
		&& time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
}

/*
1483 1484 1485 1486
 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
 * will cause the process to be a "batcher" on all queues in the system. This
 * is the behaviour we want though - once it gets a wakeup it should be given
 * a nice run.
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
 */
void ioc_set_batching(struct io_context *ioc)
{
	if (!ioc || ioc_batching(ioc))
		return;

	ioc->nr_batch_requests = BLK_BATCH_REQ;
	ioc->last_waited = jiffies;
}

/*
 * A request has just been released.  Account for it, update the full and
 * congestion status, wake up any waiters.   Called under q->queue_lock.
 */
static void freed_request(request_queue_t *q, int rw)
{
	struct request_list *rl = &q->rq;

	rl->count[rw]--;
	if (rl->count[rw] < queue_congestion_off_threshold(q))
		clear_queue_congested(q, rw);
	if (rl->count[rw]+1 <= q->nr_requests) {
		if (waitqueue_active(&rl->wait[rw]))
			wake_up(&rl->wait[rw]);
		if (!waitqueue_active(&rl->wait[rw]))
			blk_clear_queue_full(q, rw);
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
1516
#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
Linus Torvalds's avatar
Linus Torvalds committed
1517
/*
1518
 * Get a free request, queue_lock must not be held
Linus Torvalds's avatar
Linus Torvalds committed
1519
 */
1520
static struct request *get_request(request_queue_t *q, int rw, int gfp_mask)
Linus Torvalds's avatar
Linus Torvalds committed
1521
{
Linus Torvalds's avatar
Linus Torvalds committed
1522
	struct request *rq = NULL;
1523
	struct request_list *rl = &q->rq;
Andrew Morton's avatar
Andrew Morton committed
1524
	struct io_context *ioc = get_io_context(gfp_mask);
1525 1526

	spin_lock_irq(q->queue_lock);
1527
	if (rl->count[rw]+1 >= q->nr_requests) {
1528 1529 1530 1531 1532 1533
		/*
		 * The queue will fill after this allocation, so set it as
		 * full, and mark this process as "batching". This process
		 * will be allowed to complete a batch of requests, others
		 * will be blocked.
		 */
1534 1535 1536 1537 1538
		if (!blk_queue_full(q, rw)) {
			ioc_set_batching(ioc);
			blk_set_queue_full(q, rw);
		}
	}
Andrew Morton's avatar
Andrew Morton committed
1539

1540 1541
	if (blk_queue_full(q, rw)
			&& !ioc_batching(ioc) && !elv_may_queue(q, rw)) {
1542 1543 1544 1545
		/*
		 * The queue is full and the allocating process is not a
		 * "batcher", and not exempted by the IO scheduler
		 */
1546 1547
		spin_unlock_irq(q->queue_lock);
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1548
	}
1549

1550
	rl->count[rw]++;
1551
	if (rl->count[rw] >= queue_congestion_on_threshold(q))
1552 1553
		set_queue_congested(q, rw);
	spin_unlock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1554

1555 1556
	rq = blk_alloc_request(q, gfp_mask);
	if (!rq) {
1557 1558 1559 1560 1561 1562 1563
		/*
		 * Allocation failed presumably due to memory. Undo anything
		 * we might have messed up.
		 *
		 * Allocating task should really be put onto the front of the
		 * wait queue, but this is pretty rare.
		 */
1564
		spin_lock_irq(q->queue_lock);
1565
		freed_request(q, rw);
1566 1567 1568
		spin_unlock_irq(q->queue_lock);
		goto out;
	}
1569 1570 1571

	if (ioc_batching(ioc))
		ioc->nr_batch_requests--;
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
	
	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;
1590
	rq->data_len = 0;
1591 1592 1593 1594
	rq->data = NULL;
	rq->sense = NULL;

out:
1595
	put_io_context(ioc);
Linus Torvalds's avatar
Linus Torvalds committed
1596 1597 1598 1599
	return rq;
}

/*
1600 1601
 * No available requests for this queue, unplug the device and wait for some
 * requests to become available.
Linus Torvalds's avatar
Linus Torvalds committed
1602
 */
Linus Torvalds's avatar
Linus Torvalds committed
1603
static struct request *get_request_wait(request_queue_t *q, int rw)
Linus Torvalds's avatar
Linus Torvalds committed
1604
{
1605
	DEFINE_WAIT(wait);
Linus Torvalds's avatar
Linus Torvalds committed
1606 1607
	struct request *rq;

Linus Torvalds's avatar
Linus Torvalds committed
1608 1609
	generic_unplug_device(q);
	do {
1610
		struct request_list *rl = &q->rq;
1611

1612 1613
		prepare_to_wait_exclusive(&rl->wait[rw], &wait,
				TASK_UNINTERRUPTIBLE);
1614

1615
		rq = get_request(q, rw, GFP_NOIO);
1616 1617

		if (!rq) {
1618 1619
			struct io_context *ioc;

1620
			io_schedule();
1621 1622 1623 1624 1625 1626 1627

			/*
			 * After sleeping, we become a "batching" process and
			 * will be able to allocate at least one request, and
			 * up to a big batch of them for a small period time.
			 * See ioc_batching, ioc_set_batching
			 */
Andrew Morton's avatar
Andrew Morton committed
1628
			ioc = get_io_context(GFP_NOIO);
1629 1630
			ioc_set_batching(ioc);
			put_io_context(ioc);
1631
		}
1632
		finish_wait(&rl->wait[rw], &wait);
1633
	} while (!rq);
1634

Linus Torvalds's avatar
Linus Torvalds committed
1635 1636 1637
	return rq;
}

Linus Torvalds's avatar
Linus Torvalds committed
1638 1639 1640 1641 1642 1643
struct request *blk_get_request(request_queue_t *q, int rw, int gfp_mask)
{
	struct request *rq;

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

1644
	if (gfp_mask & __GFP_WAIT)
Linus Torvalds's avatar
Linus Torvalds committed
1645
		rq = get_request_wait(q, rw);
1646
	else
1647
		rq = get_request(q, rw, gfp_mask);
Linus Torvalds's avatar
Linus Torvalds committed
1648

Martin Dalecki's avatar
Martin Dalecki committed
1649 1650
	return rq;
}
1651 1652 1653

EXPORT_SYMBOL(blk_get_request);

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
/**
 * blk_requeue_request - put a request back on queue
 * @q:		request queue where request should be inserted
 * @rq:		request to be inserted
 *
 * Description:
 *    Drivers often keep queueing requests until the hardware cannot accept
 *    more, when that condition happens we need to put the request back
 *    on the queue. Must be called with queue lock held.
 */
void blk_requeue_request(request_queue_t *q, struct request *rq)
{
	if (blk_rq_tagged(rq))
		blk_queue_end_tag(q, rq);

	elv_requeue_request(q, rq);
}
Martin Dalecki's avatar
Martin Dalecki committed
1671

1672 1673
EXPORT_SYMBOL(blk_requeue_request);

1674 1675 1676 1677 1678 1679
/**
 * 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
1680
 * @reinsert:	true if request it a reinsertion of previously processed one
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
 *
 * 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,
1695
			int at_head, void *data, int reinsert)
Linus Torvalds's avatar
Linus Torvalds committed
1696
{
1697 1698 1699 1700 1701 1702 1703
	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
1704
	rq->flags |= REQ_SPECIAL | REQ_SOFTBARRIER;
1705 1706 1707 1708

	rq->special = data;

	spin_lock_irqsave(q->queue_lock, flags);
1709 1710 1711 1712

	/*
	 * If command is tagged, release the tag
	 */
1713
	if (reinsert)
1714
		blk_requeue_request(q, rq);
1715
	else {
Jens Axboe's avatar
Jens Axboe committed
1716 1717 1718 1719 1720
		int where = ELEVATOR_INSERT_BACK;

		if (at_head)
			where = ELEVATOR_INSERT_FRONT;

1721 1722
		if (blk_rq_tagged(rq))
			blk_queue_end_tag(q, rq);
1723

1724
		drive_stat_acct(rq, rq->nr_sectors, 1);
Jens Axboe's avatar
Jens Axboe committed
1725
		__elv_add_request(q, rq, where, 0);
1726
	}
1727 1728 1729 1730
	if (blk_queue_plugged(q))
		__generic_unplug_device(q);
	else
		q->request_fn(q);
1731
	spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1732 1733
}

1734 1735
EXPORT_SYMBOL(blk_insert_request);

1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
/**
 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
 * @q:		request queue where request should be inserted
 * @rw:		READ or WRITE data
 * @ubuf:	the user buffer
 * @len:	length of user data
 *
 * Description:
 *    Data will be mapped directly for zero copy io, if possible. Otherwise
 *    a kernel bounce buffer is used.
 *
 *    A matching blk_rq_unmap_user() must be issued at the end of io, while
 *    still in process context.
 */
struct request *blk_rq_map_user(request_queue_t *q, int rw, void __user *ubuf,
				unsigned int len)
{
	struct request *rq = NULL;
	char *buf = NULL;
	struct bio *bio;
	int ret;

	rq = blk_get_request(q, rw, __GFP_WAIT);
	if (!rq)
		return ERR_PTR(-ENOMEM);

	bio = bio_map_user(q, NULL, (unsigned long) ubuf, len, rw == READ);
	if (!bio) {
		int bytes = (len + 511) & ~511;

		buf = kmalloc(bytes, q->bounce_gfp | GFP_USER);
		if (!buf) {
			ret = -ENOMEM;
			goto fault;
		}

		if (rw == WRITE) {
			if (copy_from_user(buf, ubuf, len)) {
				ret = -EFAULT;
				goto fault;
			}
		} else
			memset(buf, 0, len);
	}

	rq->bio = rq->biotail = bio;
	if (rq->bio)
		blk_rq_bio_prep(q, rq, bio);

	rq->buffer = rq->data = buf;
	rq->data_len = len;
	return rq;
fault:
	if (buf)
		kfree(buf);
	if (bio)
		bio_unmap_user(bio, 1);
	if (rq)
		blk_put_request(rq);

	return ERR_PTR(ret);
}

EXPORT_SYMBOL(blk_rq_map_user);

/**
 * blk_rq_unmap_user - unmap a request with user data
 * @rq:		request to be unmapped
 * @ubuf:	user buffer
 * @ulen:	length of user buffer
 *
 * Description:
 *    Unmap a request previously mapped by blk_rq_map_user().
 */
Jens Axboe's avatar
Jens Axboe committed
1810 1811
int blk_rq_unmap_user(struct request *rq, void __user *ubuf, struct bio *bio,
		      unsigned int ulen)
1812 1813 1814 1815
{
	const int read = rq_data_dir(rq) == READ;
	int ret = 0;

Jens Axboe's avatar
Jens Axboe committed
1816 1817
	if (bio)
		bio_unmap_user(bio, read);
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
	if (rq->buffer) {
		if (read && copy_to_user(ubuf, rq->buffer, ulen))
			ret = -EFAULT;
		kfree(rq->buffer);
	}

	blk_put_request(rq);
	return ret;
}

EXPORT_SYMBOL(blk_rq_unmap_user);

/**
 * blk_execute_rq - insert a request into queue for execution
 * @q:		queue to insert the request in
 * @bd_disk:	matching gendisk
 * @rq:		request to insert
 *
 * Description:
 *    Insert a fully prepared request at the back of the io scheduler queue
 *    for execution.
 */
int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk,
		   struct request *rq)
{
	DECLARE_COMPLETION(wait);
	char sense[SCSI_SENSE_BUFFERSIZE];
	int err = 0;

	rq->rq_disk = bd_disk;

	/*
	 * we need an extra reference to the request, so we can look at
	 * it after io completion
	 */
	rq->ref_count++;

	if (!rq->sense) {
		memset(sense, 0, sizeof(sense));
		rq->sense = sense;
		rq->sense_len = 0;
	}

	rq->flags |= REQ_NOMERGE;
	rq->waiting = &wait;
	elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
	generic_unplug_device(q);
	wait_for_completion(&wait);

	if (rq->errors)
		err = -EIO;

	return err;
}

EXPORT_SYMBOL(blk_execute_rq);

Linus Torvalds's avatar
Linus Torvalds committed
1875
void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
Linus Torvalds's avatar
Linus Torvalds committed
1876
{
Linus Torvalds's avatar
Linus Torvalds committed
1877
	int rw = rq_data_dir(rq);
Linus Torvalds's avatar
Linus Torvalds committed
1878

Jens Axboe's avatar
Jens Axboe committed
1879
	if (!blk_fs_request(rq) || !rq->rq_disk)
1880 1881 1882
		return;

	if (rw == READ) {
1883
		disk_stat_add(rq->rq_disk, read_sectors, nr_sectors);
1884
		if (!new_io)
1885
			disk_stat_inc(rq->rq_disk, read_merges);
1886
	} else if (rw == WRITE) {
1887
		disk_stat_add(rq->rq_disk, write_sectors, nr_sectors);
1888
		if (!new_io)
1889
			disk_stat_inc(rq->rq_disk, write_merges);
1890 1891 1892
	}
	if (new_io) {
		disk_round_stats(rq->rq_disk);
1893
		rq->rq_disk->in_flight++;
1894
	}
Linus Torvalds's avatar
Linus Torvalds committed
1895 1896 1897 1898
}

/*
 * add-request adds a request to the linked list.
Linus Torvalds's avatar
Linus Torvalds committed
1899
 * queue lock is held and interrupts disabled, as we muck with the
Linus Torvalds's avatar
Linus Torvalds committed
1900
 * request queue list.
Linus Torvalds's avatar
Linus Torvalds committed
1901
 */
Jens Axboe's avatar
Jens Axboe committed
1902
static inline void add_request(request_queue_t * q, struct request * req)
Linus Torvalds's avatar
Linus Torvalds committed
1903
{
Linus Torvalds's avatar
Linus Torvalds committed
1904
	drive_stat_acct(req, req->nr_sectors, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1905

Jens Axboe's avatar
Jens Axboe committed
1906 1907 1908
	if (q->activity_fn)
		q->activity_fn(q->activity_data, rq_data_dir(req));

Linus Torvalds's avatar
Linus Torvalds committed
1909
	/*
Linus Torvalds's avatar
Linus Torvalds committed
1910 1911
	 * elevator indicated where it wants this request to be
	 * inserted at elevator_merge time
Linus Torvalds's avatar
Linus Torvalds committed
1912
	 */
Jens Axboe's avatar
Jens Axboe committed
1913
	__elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1914
}
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
 
/*
 * 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;

1935
	disk_stat_add(disk, time_in_queue, 
1936
			disk->in_flight * (now - disk->stamp));
1937 1938
	disk->stamp = now;

1939
	if (disk->in_flight)
1940
		disk_stat_add(disk, io_ticks, (now - disk->stamp_idle));
1941 1942
	disk->stamp_idle = now;
}
Linus Torvalds's avatar
Linus Torvalds committed
1943

1944 1945 1946
/*
 * queue lock must be held
 */
1947
void __blk_put_request(request_queue_t *q, struct request *req)
Linus Torvalds's avatar
Linus Torvalds committed
1948
{
Linus Torvalds's avatar
Linus Torvalds committed
1949
	struct request_list *rl = req->rl;
1950 1951 1952

	if (unlikely(!q))
		return;
1953 1954
	if (unlikely(--req->ref_count))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1955

Linus Torvalds's avatar
Linus Torvalds committed
1956
	req->rq_status = RQ_INACTIVE;
Linus Torvalds's avatar
Linus Torvalds committed
1957
	req->q = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1958
	req->rl = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1959 1960

	/*
Linus Torvalds's avatar
Linus Torvalds committed
1961
	 * Request may not have originated from ll_rw_blk. if not,
Linus Torvalds's avatar
Linus Torvalds committed
1962
	 * it didn't come out of our reserved rq pools
Linus Torvalds's avatar
Linus Torvalds committed
1963
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1964
	if (rl) {
1965
		int rw = rq_data_dir(req);
1966

1967 1968
		elv_completed_request(q, req);

1969 1970
		BUG_ON(!list_empty(&req->queuelist));

1971
		blk_free_request(q, req);
1972
		freed_request(q, rw);
Linus Torvalds's avatar
Linus Torvalds committed
1973 1974 1975
	}
}

1976 1977 1978
void blk_put_request(struct request *req)
{
	/*
1979
	 * if req->rl isn't set, this request didnt originate from the
1980 1981
	 * block layer, so it's safe to just disregard it
	 */
1982
	if (req->rl) {
1983
		unsigned long flags;
1984
		request_queue_t *q = req->q;
1985 1986 1987 1988 1989 1990 1991

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

1992 1993
EXPORT_SYMBOL(blk_put_request);

1994 1995 1996 1997 1998 1999
/**
 * 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.
2000 2001
 * If no queues are congested then just wait for the next request to be
 * returned.
2002
 */
2003
long blk_congestion_wait(int rw, long timeout)
2004
{
2005
	long ret;
2006
	DEFINE_WAIT(wait);
2007
	wait_queue_head_t *wqh = &congestion_wqh[rw];
2008

2009
	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
2010
	ret = io_schedule_timeout(timeout);
2011
	finish_wait(wqh, &wait);
2012
	return ret;
2013 2014
}

2015 2016
EXPORT_SYMBOL(blk_congestion_wait);

Linus Torvalds's avatar
Linus Torvalds committed
2017 2018 2019
/*
 * Has to be called with the request spinlock acquired
 */
2020
static int attempt_merge(request_queue_t *q, struct request *req,
Linus Torvalds's avatar
Linus Torvalds committed
2021
			  struct request *next)
Linus Torvalds's avatar
Linus Torvalds committed
2022
{
Linus Torvalds's avatar
Linus Torvalds committed
2023
	if (!rq_mergeable(req) || !rq_mergeable(next))
2024
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2025 2026 2027 2028

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

Linus Torvalds's avatar
Linus Torvalds committed
2032
	if (rq_data_dir(req) != rq_data_dir(next)
2033
	    || req->rq_disk != next->rq_disk
Linus Torvalds's avatar
Linus Torvalds committed
2034
	    || next->waiting || next->special)
2035
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2036

Linus Torvalds's avatar
Linus Torvalds committed
2037
	/*
Linus Torvalds's avatar
Linus Torvalds committed
2038 2039 2040 2041
	 * 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
2042
	 */
2043 2044
	if (!q->merge_requests_fn(q, req, next))
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2045

2046 2047 2048 2049 2050 2051 2052 2053 2054
	/*
	 * At this point we have either done a back merge
	 * or front merge. We need the smaller start_time of
	 * the merged requests to be the current request
	 * for accounting purposes.
	 */
	if (time_after(req->start_time, next->start_time))
		req->start_time = next->start_time;

2055 2056
	req->biotail->bi_next = next->bio;
	req->biotail = next->biotail;
Linus Torvalds's avatar
Linus Torvalds committed
2057

2058
	req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
2059

2060
	elv_merge_requests(q, req, next);
2061

2062 2063
	if (req->rq_disk) {
		disk_round_stats(req->rq_disk);
2064
		req->rq_disk->in_flight--;
Linus Torvalds's avatar
Linus Torvalds committed
2065
	}
2066

2067 2068
	__blk_put_request(q, next);
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
2069 2070
}

2071
static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
2072
{
2073
	struct request *next = elv_latter_request(q, rq);
Linus Torvalds's avatar
Linus Torvalds committed
2074

2075 2076 2077 2078
	if (next)
		return attempt_merge(q, rq, next);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2079 2080
}

2081
static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
2082
{
2083
	struct request *prev = elv_former_request(q, rq);
Linus Torvalds's avatar
Linus Torvalds committed
2084

2085 2086 2087 2088
	if (prev)
		return attempt_merge(q, prev, rq);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2089
}
Linus Torvalds's avatar
Linus Torvalds committed
2090

Linus Torvalds's avatar
Linus Torvalds committed
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
/**
 * 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
2107

Linus Torvalds's avatar
Linus Torvalds committed
2108
	spin_lock_irqsave(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2109
	attempt_back_merge(q, rq);
Linus Torvalds's avatar
Linus Torvalds committed
2110
	spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2111
}
Linus Torvalds's avatar
Linus Torvalds committed
2112

2113 2114
EXPORT_SYMBOL(blk_attempt_remerge);

Martin Dalecki's avatar
Martin Dalecki committed
2115 2116 2117 2118 2119 2120 2121 2122
/*
 * Non-locking blk_attempt_remerge variant.
 */
void __blk_attempt_remerge(request_queue_t *q, struct request *rq)
{
	attempt_back_merge(q, rq);
}

2123 2124
EXPORT_SYMBOL(__blk_attempt_remerge);

Linus Torvalds's avatar
Linus Torvalds committed
2125 2126 2127
static int __make_request(request_queue_t *q, struct bio *bio)
{
	struct request *req, *freereq = NULL;
Jens Axboe's avatar
Jens Axboe committed
2128
	int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, ra;
Linus Torvalds's avatar
Linus Torvalds committed
2129
	sector_t sector;
Linus Torvalds's avatar
Linus Torvalds committed
2130

Linus Torvalds's avatar
Linus Torvalds committed
2131 2132
	sector = bio->bi_sector;
	nr_sectors = bio_sectors(bio);
Jens Axboe's avatar
Jens Axboe committed
2133 2134
	cur_nr_sectors = bio_cur_sectors(bio);

Linus Torvalds's avatar
Linus Torvalds committed
2135
	rw = bio_data_dir(bio);
Linus Torvalds's avatar
Linus Torvalds committed
2136 2137

	/*
Linus Torvalds's avatar
Linus Torvalds committed
2138 2139 2140
	 * 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
2141
	 */
Linus Torvalds's avatar
Linus Torvalds committed
2142 2143
	blk_queue_bounce(q, &bio);

Linus Torvalds's avatar
Linus Torvalds committed
2144
	spin_lock_prefetch(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
2145

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

Jens Axboe's avatar
Jens Axboe committed
2148
	ra = bio->bi_rw & (1 << BIO_RW_AHEAD);
Jens Axboe's avatar
Jens Axboe committed
2149

Linus Torvalds's avatar
Linus Torvalds committed
2150
again:
2151
	spin_lock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
2152

Jens Axboe's avatar
Jens Axboe committed
2153
	if (elv_queue_empty(q)) {
Linus Torvalds's avatar
Linus Torvalds committed
2154
		blk_plug_device(q);
Linus Torvalds's avatar
Linus Torvalds committed
2155
		goto get_rq;
Linus Torvalds's avatar
Linus Torvalds committed
2156
	}
Jens Axboe's avatar
Jens Axboe committed
2157 2158
	if (barrier)
		goto get_rq;
Linus Torvalds's avatar
Linus Torvalds committed
2159

Jens Axboe's avatar
Jens Axboe committed
2160
	el_ret = elv_merge(q, &req, bio);
Linus Torvalds's avatar
Linus Torvalds committed
2161
	switch (el_ret) {
Linus Torvalds's avatar
Linus Torvalds committed
2162
		case ELEVATOR_BACK_MERGE:
Linus Torvalds's avatar
Linus Torvalds committed
2163
			BUG_ON(!rq_mergeable(req));
Jens Axboe's avatar
Jens Axboe committed
2164

Jens Axboe's avatar
Jens Axboe committed
2165
			if (!q->back_merge_fn(q, req, bio))
Linus Torvalds's avatar
Linus Torvalds committed
2166
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2167

Linus Torvalds's avatar
Linus Torvalds committed
2168 2169 2170
			req->biotail->bi_next = bio;
			req->biotail = bio;
			req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
2171
			drive_stat_acct(req, nr_sectors, 0);
2172 2173
			if (!attempt_back_merge(q, req))
				elv_merged_request(q, req);
Linus Torvalds's avatar
Linus Torvalds committed
2174 2175 2176
			goto out;

		case ELEVATOR_FRONT_MERGE:
Linus Torvalds's avatar
Linus Torvalds committed
2177
			BUG_ON(!rq_mergeable(req));
Jens Axboe's avatar
Jens Axboe committed
2178

Jens Axboe's avatar
Jens Axboe committed
2179
			if (!q->front_merge_fn(q, req, bio))
Linus Torvalds's avatar
Linus Torvalds committed
2180
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2181

Linus Torvalds's avatar
Linus Torvalds committed
2182
			bio->bi_next = req->bio;
Jens Axboe's avatar
Jens Axboe committed
2183 2184 2185 2186
			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
2187 2188 2189 2190 2191 2192 2193 2194
			/*
			 * 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
2195
			req->sector = req->hard_sector = sector;
Linus Torvalds's avatar
Linus Torvalds committed
2196
			req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
2197
			drive_stat_acct(req, nr_sectors, 0);
2198 2199
			if (!attempt_front_merge(q, req))
				elv_merged_request(q, req);
Linus Torvalds's avatar
Linus Torvalds committed
2200
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2201

Linus Torvalds's avatar
Linus Torvalds committed
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
		/*
		 * 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
2212

Linus Torvalds's avatar
Linus Torvalds committed
2213
	/*
Linus Torvalds's avatar
Linus Torvalds committed
2214 2215 2216
	 * 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
2217 2218 2219 2220 2221
	 */
get_rq:
	if (freereq) {
		req = freereq;
		freereq = NULL;
2222
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
2223
		spin_unlock_irq(q->queue_lock);
2224
		if ((freereq = get_request(q, rw, GFP_ATOMIC)) == NULL) {
2225 2226 2227
			/*
			 * READA bit set
			 */
Jens Axboe's avatar
Jens Axboe committed
2228
			if (ra)
2229 2230 2231 2232
				goto end_io;
	
			freereq = get_request_wait(q, rw);
		}
Linus Torvalds's avatar
Linus Torvalds committed
2233 2234 2235
		goto again;
	}

2236 2237
	req->flags |= REQ_CMD;

Linus Torvalds's avatar
Linus Torvalds committed
2238
	/*
2239 2240
	 * inherit FAILFAST from bio and don't stack up
	 * retries for read ahead
Linus Torvalds's avatar
Linus Torvalds committed
2241
	 */
2242 2243
	if (ra || test_bit(BIO_RW_FAILFAST, &bio->bi_rw))	
		req->flags |= REQ_FAILFAST;
Linus Torvalds's avatar
Linus Torvalds committed
2244 2245 2246 2247 2248

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

Linus Torvalds's avatar
Linus Torvalds committed
2251 2252
	req->errors = 0;
	req->hard_sector = req->sector = sector;
Linus Torvalds's avatar
Linus Torvalds committed
2253 2254
	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
2255
	req->nr_phys_segments = bio_phys_segments(q, bio);
Linus Torvalds's avatar
Linus Torvalds committed
2256
	req->nr_hw_segments = bio_hw_segments(q, bio);
Jens Axboe's avatar
Jens Axboe committed
2257 2258
	req->nr_cbio_segments = bio_segments(bio);
	req->nr_cbio_sectors = bio_sectors(bio);
Linus Torvalds's avatar
Linus Torvalds committed
2259
	req->buffer = bio_data(bio);	/* see ->buffer comment above */
Linus Torvalds's avatar
Linus Torvalds committed
2260
	req->waiting = NULL;
Jens Axboe's avatar
Jens Axboe committed
2261
	req->cbio = req->bio = req->biotail = bio;
2262
	req->rq_disk = bio->bi_bdev->bd_disk;
2263
	req->start_time = jiffies;
2264

Jens Axboe's avatar
Jens Axboe committed
2265
	add_request(q, req);
Linus Torvalds's avatar
Linus Torvalds committed
2266
out:
Linus Torvalds's avatar
Linus Torvalds committed
2267
	if (freereq)
2268
		__blk_put_request(q, freereq);
2269 2270

	if (blk_queue_plugged(q)) {
2271
		int nrq = q->rq.count[READ] + q->rq.count[WRITE] - q->in_flight;
2272

2273
		if (nrq == q->unplug_thresh || bio_sync(bio))
2274 2275
			__generic_unplug_device(q);
	}
Linus Torvalds's avatar
Linus Torvalds committed
2276
	spin_unlock_irq(q->queue_lock);
Linus Torvalds's avatar
Linus Torvalds committed
2277
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2278

Linus Torvalds's avatar
Linus Torvalds committed
2279
end_io:
2280
	bio_endio(bio, nr_sectors << 9, -EWOULDBLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
2281 2282 2283
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
2284 2285 2286 2287 2288
/*
 * If bio->bi_dev is a partition, remap the location
 */
static inline void blk_partition_remap(struct bio *bio)
{
2289 2290
	struct block_device *bdev = bio->bi_bdev;

2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
	if (bdev != bdev->bd_contains) {
		struct hd_struct *p = bdev->bd_part;

		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;
		}
		bio->bi_sector += p->start_sect;
		bio->bi_bdev = bdev->bd_contains;
2306
	}
Linus Torvalds's avatar
Linus Torvalds committed
2307 2308
}

Linus Torvalds's avatar
Linus Torvalds committed
2309
/**
2310
 * generic_make_request: hand a buffer to its device driver for I/O
Linus Torvalds's avatar
Linus Torvalds committed
2311
 * @bio:  The bio describing the location in memory and on the device.
Linus Torvalds's avatar
Linus Torvalds committed
2312 2313
 *
 * generic_make_request() is used to make I/O requests of block
Linus Torvalds's avatar
Linus Torvalds committed
2314 2315
 * devices. It is passed a &struct bio, which describes the I/O that needs
 * to be done.
Linus Torvalds's avatar
Linus Torvalds committed
2316 2317 2318
 *
 * 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
2319
 * completion, is delivered asynchronously through the bio->bi_end_io
Linus Torvalds's avatar
Linus Torvalds committed
2320 2321
 * function described (one day) else where.
 *
Linus Torvalds's avatar
Linus Torvalds committed
2322 2323
 * 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
2324
 * set to describe the device address, and the
Linus Torvalds's avatar
Linus Torvalds committed
2325 2326
 * bi_end_io and optionally bi_private are set to describe how
 * completion notification should be signaled.
Linus Torvalds's avatar
Linus Torvalds committed
2327
 *
Linus Torvalds's avatar
Linus Torvalds committed
2328 2329
 * 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
2330
 * bi_sector for remaps as it sees fit.  So the values of these fields
Linus Torvalds's avatar
Linus Torvalds committed
2331
 * should NOT be depended on after the call to generic_make_request.
2332
 */
Linus Torvalds's avatar
Linus Torvalds committed
2333
void generic_make_request(struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
2334 2335
{
	request_queue_t *q;
Andries E. Brouwer's avatar
Andries E. Brouwer committed
2336
	sector_t maxsector;
Linus Torvalds's avatar
Linus Torvalds committed
2337
	int ret, nr_sectors = bio_sectors(bio);
Linus Torvalds's avatar
Linus Torvalds committed
2338

Linus Torvalds's avatar
Linus Torvalds committed
2339
	/* Test device or partition size, when known. */
2340
	maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
Andries E. Brouwer's avatar
Andries E. Brouwer committed
2341 2342 2343 2344 2345
	if (maxsector) {
		sector_t sector = bio->bi_sector;

		if (maxsector < nr_sectors ||
		    maxsector - nr_sectors < sector) {
2346
			char b[BDEVNAME_SIZE];
Andries E. Brouwer's avatar
Andries E. Brouwer committed
2347 2348 2349 2350 2351
			/* 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");
2352
			printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
2353
			       bdevname(bio->bi_bdev, b),
2354
			       bio->bi_rw,
2355
			       (unsigned long long) sector + nr_sectors,
Andries E. Brouwer's avatar
Andries E. Brouwer committed
2356 2357
			       (long long) maxsector);

Linus Torvalds's avatar
Linus Torvalds committed
2358 2359
			set_bit(BIO_EOF, &bio->bi_flags);
			goto end_io;
Linus Torvalds's avatar
Linus Torvalds committed
2360 2361 2362 2363 2364 2365 2366
		}
	}

	/*
	 * 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
2367 2368
	 *
	 * NOTE: we don't repeat the blk_size check for each new device.
Linus Torvalds's avatar
Linus Torvalds committed
2369 2370 2371
	 * Stacking drivers are expected to know what they are doing.
	 */
	do {
2372 2373
		char b[BDEVNAME_SIZE];

2374
		q = bdev_get_queue(bio->bi_bdev);
Linus Torvalds's avatar
Linus Torvalds committed
2375 2376
		if (!q) {
			printk(KERN_ERR
2377 2378 2379 2380
			       "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
2381
end_io:
2382
			bio_endio(bio, bio->bi_size, -EIO);
Linus Torvalds's avatar
Linus Torvalds committed
2383 2384
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2385

2386
		if (unlikely(bio_sectors(bio) > q->max_sectors)) {
2387
			printk("bio too big device %s (%u > %u)\n", 
2388 2389 2390
				bdevname(bio->bi_bdev, b),
				bio_sectors(bio),
				q->max_sectors);
2391 2392
			goto end_io;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2393

2394 2395 2396
		if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))
			goto end_io;

Linus Torvalds's avatar
Linus Torvalds committed
2397 2398 2399 2400 2401 2402
		/*
		 * 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
2403 2404
		ret = q->make_request_fn(q, bio);
	} while (ret);
Linus Torvalds's avatar
Linus Torvalds committed
2405 2406
}

2407 2408
EXPORT_SYMBOL(generic_make_request);

Linus Torvalds's avatar
Linus Torvalds committed
2409
/**
Linus Torvalds's avatar
Linus Torvalds committed
2410
 * submit_bio: submit a bio to the block device layer for I/O
Linus Torvalds's avatar
Linus Torvalds committed
2411
 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
Linus Torvalds's avatar
Linus Torvalds committed
2412
 * @bio: The &struct bio which describes the I/O
Linus Torvalds's avatar
Linus Torvalds committed
2413
 *
Linus Torvalds's avatar
Linus Torvalds committed
2414 2415 2416
 * 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
2417 2418
 *
 */
2419
void submit_bio(int rw, struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
2420
{
2421
	int count = bio_sectors(bio);
Linus Torvalds's avatar
Linus Torvalds committed
2422

Linus Torvalds's avatar
Linus Torvalds committed
2423
	BIO_BUG_ON(!bio->bi_size);
Linus Torvalds's avatar
Linus Torvalds committed
2424 2425 2426
	BIO_BUG_ON(!bio->bi_io_vec);
	bio->bi_rw = rw;
	if (rw & WRITE)
2427
		mod_page_state(pgpgout, count);
Linus Torvalds's avatar
Linus Torvalds committed
2428
	else
2429
		mod_page_state(pgpgin, count);
Andrew Morton's avatar
Andrew Morton committed
2430 2431 2432

	if (unlikely(block_dump)) {
		char b[BDEVNAME_SIZE];
2433
		printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Andrew Morton's avatar
Andrew Morton committed
2434 2435 2436 2437 2438 2439
			current->comm, current->pid,
			(rw & WRITE) ? "WRITE" : "READ",
			(unsigned long long)bio->bi_sector,
			bdevname(bio->bi_bdev,b));
	}

Linus Torvalds's avatar
Linus Torvalds committed
2440 2441 2442
	generic_make_request(bio);
}

2443 2444
EXPORT_SYMBOL(submit_bio);

Jens Axboe's avatar
Jens Axboe committed
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 2513 2514 2515 2516 2517 2518 2519
/**
 * 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;
}

2520 2521
EXPORT_SYMBOL(process_that_request_first);

2522
void blk_recalc_rq_segments(struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
2523 2524 2525 2526
{
	struct bio *bio;
	int nr_phys_segs, nr_hw_segs;

2527 2528 2529
	if (!rq->bio)
		return;

Linus Torvalds's avatar
Linus Torvalds committed
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
	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;
}

2543
void blk_recalc_rq_sectors(struct request *rq, int nsect)
Linus Torvalds's avatar
Linus Torvalds committed
2544
{
2545
	if (blk_fs_request(rq)) {
Linus Torvalds's avatar
Linus Torvalds committed
2546
		rq->hard_sector += nsect;
Jens Axboe's avatar
Jens Axboe committed
2547
		rq->hard_nr_sectors -= nsect;
Linus Torvalds's avatar
Linus Torvalds committed
2548

Jens Axboe's avatar
Jens Axboe committed
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
		/*
		 * 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
2565

Linus Torvalds's avatar
Linus Torvalds committed
2566 2567 2568 2569 2570 2571 2572 2573
		/*
		 * 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
2574 2575 2576
	}
}

2577 2578
static int __end_that_request_first(struct request *req, int uptodate,
				    int nr_bytes)
Linus Torvalds's avatar
Linus Torvalds committed
2579
{
2580
	int total_bytes, bio_nbytes, error = 0, next_idx = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2581
	struct bio *bio;
Linus Torvalds's avatar
Linus Torvalds committed
2582

2583 2584 2585 2586 2587 2588 2589
	/*
	 * 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
2590 2591
	if (!uptodate) {
		error = -EIO;
2592
		if (blk_fs_request(req) && !(req->flags & REQ_QUIET))
Jens Axboe's avatar
Jens Axboe committed
2593
			printk("end_request: I/O error, dev %s, sector %llu\n",
2594
				req->rq_disk ? req->rq_disk->disk_name : "?",
Jens Axboe's avatar
Jens Axboe committed
2595
				(unsigned long long)req->sector);
Jens Axboe's avatar
Jens Axboe committed
2596
	}
Linus Torvalds's avatar
Linus Torvalds committed
2597

2598
	total_bytes = bio_nbytes = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2599
	while ((bio = req->bio)) {
2600
		int nbytes;
Jens Axboe's avatar
Jens Axboe committed
2601

2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
		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
2615
						bio->bi_idx, bio->bi_vcnt);
2616 2617
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
2618

2619 2620
			nbytes = bio_iovec_idx(bio, idx)->bv_len;
			BIO_BUG_ON(nbytes > bio->bi_size);
Linus Torvalds's avatar
Linus Torvalds committed
2621

2622 2623 2624 2625 2626 2627 2628 2629
			/*
			 * not a complete bvec done
			 */
			if (unlikely(nbytes > nr_bytes)) {
				bio_nbytes += nr_bytes;
				total_bytes += nr_bytes;
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
2630

2631 2632 2633 2634 2635
			/*
			 * advance to the next vector
			 */
			next_idx++;
			bio_nbytes += nbytes;
2636
		}
Linus Torvalds's avatar
Linus Torvalds committed
2637

2638 2639
		total_bytes += nbytes;
		nr_bytes -= nbytes;
Jens Axboe's avatar
Jens Axboe committed
2640

Linus Torvalds's avatar
Linus Torvalds committed
2641
		if ((bio = req->bio)) {
Linus Torvalds's avatar
Linus Torvalds committed
2642 2643 2644
			/*
			 * end more in this run, or just return 'not-done'
			 */
2645
			if (unlikely(nr_bytes <= 0))
Jens Axboe's avatar
Jens Axboe committed
2646
				break;
Linus Torvalds's avatar
Linus Torvalds committed
2647 2648
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
2649

Jens Axboe's avatar
Jens Axboe committed
2650 2651 2652 2653 2654 2655 2656 2657 2658
	/*
	 * completely done
	 */
	if (!req->bio)
		return 0;

	/*
	 * if the request wasn't completed, update state
	 */
2659 2660
	if (bio_nbytes) {
		bio_endio(bio, bio_nbytes, error);
2661 2662 2663
		bio->bi_idx += next_idx;
		bio_iovec(bio)->bv_offset += nr_bytes;
		bio_iovec(bio)->bv_len -= nr_bytes;
2664 2665 2666
	}

	blk_recalc_rq_sectors(req, total_bytes >> 9);
Jens Axboe's avatar
Jens Axboe committed
2667 2668
	blk_recalc_rq_segments(req);
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
2669 2670
}

2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
/**
 * 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);
}

2690 2691
EXPORT_SYMBOL(end_that_request_first);

2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
/**
 * 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);
}

2712 2713
EXPORT_SYMBOL(end_that_request_chunk);

2714 2715 2716
/*
 * queue lock must be held
 */
Linus Torvalds's avatar
Linus Torvalds committed
2717 2718
void end_that_request_last(struct request *req)
{
2719
	struct gendisk *disk = req->rq_disk;
2720
	struct completion *waiting = req->waiting;
Linus Torvalds's avatar
Linus Torvalds committed
2721

2722
	if (unlikely(laptop_mode) && blk_fs_request(req))
Andrew Morton's avatar
Andrew Morton committed
2723 2724
		laptop_io_completion();

2725
	if (disk && blk_fs_request(req)) {
2726 2727 2728
		unsigned long duration = jiffies - req->start_time;
		switch (rq_data_dir(req)) {
		    case WRITE:
2729 2730
			disk_stat_inc(disk, writes);
			disk_stat_add(disk, write_ticks, duration);
2731 2732
			break;
		    case READ:
2733 2734
			disk_stat_inc(disk, reads);
			disk_stat_add(disk, read_ticks, duration);
2735 2736 2737
			break;
		}
		disk_round_stats(disk);
2738
		disk->in_flight--;
2739
	}
2740
	__blk_put_request(req->q, req);
2741 2742 2743
	/* Do this LAST! The structure may be freed immediately afterwards */
	if (waiting)
		complete(waiting);
Linus Torvalds's avatar
Linus Torvalds committed
2744 2745
}

2746 2747
EXPORT_SYMBOL(end_that_request_last);

2748 2749 2750 2751 2752 2753 2754 2755 2756
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);
	}
}

2757 2758
EXPORT_SYMBOL(end_request);

2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
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
2769 2770
	rq->nr_cbio_segments = bio_segments(bio);
	rq->nr_cbio_sectors = bio_sectors(bio);
2771 2772
	rq->buffer = bio_data(bio);

Jens Axboe's avatar
Jens Axboe committed
2773 2774 2775
	rq->cbio = rq->bio = rq->biotail = bio;
}

2776 2777
EXPORT_SYMBOL(blk_rq_bio_prep);

Jens Axboe's avatar
Jens Axboe committed
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
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;
2792 2793
}

2794 2795
EXPORT_SYMBOL(blk_rq_prep_restart);

2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
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
2806 2807
int __init blk_dev_init(void)
{
2808 2809 2810 2811
	kblockd_workqueue = create_workqueue("kblockd");
	if (!kblockd_workqueue)
		panic("Failed to create kblockd\n");

Linus Torvalds's avatar
Linus Torvalds committed
2812
	request_cachep = kmem_cache_create("blkdev_requests",
2813
			sizeof(struct request), 0, 0, NULL, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
2814 2815 2816
	if (!request_cachep)
		panic("Can't create request pool slab cache\n");

Linus Torvalds's avatar
Linus Torvalds committed
2817 2818
	blk_max_low_pfn = max_low_pfn;
	blk_max_pfn = max_pfn;
Linus Torvalds's avatar
Linus Torvalds committed
2819
	return 0;
2820
}
Linus Torvalds's avatar
Linus Torvalds committed
2821

Andrew Morton's avatar
Andrew Morton committed
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851
/*
 * IO Context helper functions
 */
void put_io_context(struct io_context *ioc)
{
	if (ioc == NULL)
		return;

	BUG_ON(atomic_read(&ioc->refcount) == 0);

	if (atomic_dec_and_test(&ioc->refcount)) {
		if (ioc->aic && ioc->aic->dtor)
			ioc->aic->dtor(ioc->aic);
		kfree(ioc);
	}
}

/* Called by the exitting task */
void exit_io_context(void)
{
	unsigned long flags;
	struct io_context *ioc;

	local_irq_save(flags);
	ioc = current->io_context;
	if (ioc) {
		if (ioc->aic && ioc->aic->exit)
			ioc->aic->exit(ioc->aic);
		put_io_context(ioc);
		current->io_context = NULL;
2852 2853
	} else
		WARN_ON(1);
Andrew Morton's avatar
Andrew Morton committed
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
	local_irq_restore(flags);
}

/*
 * If the current task has no IO context then create one and initialise it.
 * If it does have a context, take a ref on it.
 *
 * This is always called in the context of the task which submitted the I/O.
 * But weird things happen, so we disable local interrupts to ensure exclusive
 * access to *current.
 */
Andrew Morton's avatar
Andrew Morton committed
2865
struct io_context *get_io_context(int gfp_flags)
Andrew Morton's avatar
Andrew Morton committed
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
{
	struct task_struct *tsk = current;
	unsigned long flags;
	struct io_context *ret;

	local_irq_save(flags);
	ret = tsk->io_context;
	if (ret == NULL) {
		ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
		if (ret) {
			atomic_set(&ret->refcount, 1);
			ret->pid = tsk->pid;
2878 2879
			ret->last_waited = jiffies; /* doesn't matter... */
			ret->nr_batch_requests = 0; /* because this is 0 */
Andrew Morton's avatar
Andrew Morton committed
2880 2881 2882 2883
			ret->aic = NULL;
			tsk->io_context = ret;
		}
	}
Andrew Morton's avatar
Andrew Morton committed
2884 2885
	if (ret)
		atomic_inc(&ret->refcount);
Andrew Morton's avatar
Andrew Morton committed
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
	local_irq_restore(flags);
	return ret;
}

void copy_io_context(struct io_context **pdst, struct io_context **psrc)
{
	struct io_context *src = *psrc;
	struct io_context *dst = *pdst;

	if (src) {
		BUG_ON(atomic_read(&src->refcount) == 0);
		atomic_inc(&src->refcount);
		put_io_context(dst);
		*pdst = src;
	}
}

void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
{
	struct io_context *temp;
	temp = *ioc1;
	*ioc1 = *ioc2;
	*ioc2 = temp;
}


2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
/*
 * 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;

2950
	if (rl->count[READ] >= queue_congestion_on_threshold(q))
2951
		set_queue_congested(q, READ);
2952
	else if (rl->count[READ] < queue_congestion_off_threshold(q))
2953 2954
		clear_queue_congested(q, READ);

2955 2956 2957 2958
	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);
2959

2960 2961
	if (rl->count[READ] >= q->nr_requests) {
		blk_set_queue_full(q, READ);
2962
	} else if (rl->count[READ]+1 <= q->nr_requests) {
2963
		blk_clear_queue_full(q, READ);
2964
		wake_up(&rl->wait[READ]);
2965 2966 2967 2968
	}

	if (rl->count[WRITE] >= q->nr_requests) {
		blk_set_queue_full(q, WRITE);
2969
	} else if (rl->count[WRITE]+1 <= q->nr_requests) {
2970
		blk_clear_queue_full(q, WRITE);
2971
		wake_up(&rl->wait[WRITE]);
2972
	}
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
	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;

3032
	if (!q || !q->request_fn)
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
		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;

3059
	if (q && q->request_fn) {
3060 3061 3062 3063 3064 3065
		elv_unregister_queue(q);

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