Skip to content
Snippets Groups Projects
cfq-iosched.c 43.3 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 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 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
/*
 *  linux/drivers/block/cfq-iosched.c
 *
 *  CFQ, or complete fairness queueing, disk scheduler.
 *
 *  Based on ideas from a previously unfinished io
 *  scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
 *
 *  Copyright (C) 2003 Jens Axboe <axboe@suse.de>
 */
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/elevator.h>
#include <linux/bio.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/compiler.h>
#include <linux/hash.h>
#include <linux/rbtree.h>
#include <linux/mempool.h>

static unsigned long max_elapsed_crq;
static unsigned long max_elapsed_dispatch;

/*
 * tunables
 */
static int cfq_quantum = 4;		/* max queue in one round of service */
static int cfq_queued = 8;		/* minimum rq allocate limit per-queue*/
static int cfq_service = HZ;		/* period over which service is avg */
static int cfq_fifo_expire_r = HZ / 2;	/* fifo timeout for sync requests */
static int cfq_fifo_expire_w = 5 * HZ;	/* fifo timeout for async requests */
static int cfq_fifo_rate = HZ / 8;	/* fifo expiry rate */
static int cfq_back_max = 16 * 1024;	/* maximum backwards seek, in KiB */
static int cfq_back_penalty = 2;	/* penalty of a backwards seek */

/*
 * for the hash of cfqq inside the cfqd
 */
#define CFQ_QHASH_SHIFT		6
#define CFQ_QHASH_ENTRIES	(1 << CFQ_QHASH_SHIFT)
#define list_entry_qhash(entry)	hlist_entry((entry), struct cfq_queue, cfq_hash)

/*
 * for the hash of crq inside the cfqq
 */
#define CFQ_MHASH_SHIFT		6
#define CFQ_MHASH_BLOCK(sec)	((sec) >> 3)
#define CFQ_MHASH_ENTRIES	(1 << CFQ_MHASH_SHIFT)
#define CFQ_MHASH_FN(sec)	hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
#define rq_hash_key(rq)		((rq)->sector + (rq)->nr_sectors)
#define list_entry_hash(ptr)	hlist_entry((ptr), struct cfq_rq, hash)

#define list_entry_cfqq(ptr)	list_entry((ptr), struct cfq_queue, cfq_list)

#define RQ_DATA(rq)		(rq)->elevator_private

/*
 * rb-tree defines
 */
#define RB_NONE			(2)
#define RB_EMPTY(node)		((node)->rb_node == NULL)
#define RB_CLEAR_COLOR(node)	(node)->rb_color = RB_NONE
#define RB_CLEAR(node)		do {	\
	(node)->rb_parent = NULL;	\
	RB_CLEAR_COLOR((node));		\
	(node)->rb_right = NULL;	\
	(node)->rb_left = NULL;		\
} while (0)
#define RB_CLEAR_ROOT(root)	((root)->rb_node = NULL)
#define ON_RB(node)		((node)->rb_color != RB_NONE)
#define rb_entry_crq(node)	rb_entry((node), struct cfq_rq, rb_node)
#define rq_rb_key(rq)		(rq)->sector

/*
 * threshold for switching off non-tag accounting
 */
#define CFQ_MAX_TAG		(4)

/*
 * sort key types and names
 */
enum {
	CFQ_KEY_PGID,
	CFQ_KEY_TGID,
	CFQ_KEY_UID,
	CFQ_KEY_GID,
	CFQ_KEY_LAST,
};

static char *cfq_key_types[] = { "pgid", "tgid", "uid", "gid", NULL };

static kmem_cache_t *crq_pool;
static kmem_cache_t *cfq_pool;
static kmem_cache_t *cfq_ioc_pool;

struct cfq_data {
	struct list_head rr_list;
	struct list_head empty_list;

	struct hlist_head *cfq_hash;
	struct hlist_head *crq_hash;

	/* queues on rr_list (ie they have pending requests */
	unsigned int busy_queues;

	unsigned int max_queued;

	atomic_t ref;

	int key_type;

	mempool_t *crq_pool;

	request_queue_t *queue;

	sector_t last_sector;

	int rq_in_driver;

	/*
	 * tunables, see top of file
	 */
	unsigned int cfq_quantum;
	unsigned int cfq_queued;
	unsigned int cfq_fifo_expire_r;
	unsigned int cfq_fifo_expire_w;
	unsigned int cfq_fifo_batch_expire;
	unsigned int cfq_back_penalty;
	unsigned int cfq_back_max;
	unsigned int find_best_crq;

	unsigned int cfq_tagged;
};

struct cfq_queue {
	/* reference count */
	atomic_t ref;
	/* parent cfq_data */
	struct cfq_data *cfqd;
	/* hash of mergeable requests */
	struct hlist_node cfq_hash;
	/* hash key */
	unsigned long key;
	/* whether queue is on rr (or empty) list */
	int on_rr;
	/* on either rr or empty list of cfqd */
	struct list_head cfq_list;
	/* sorted list of pending requests */
	struct rb_root sort_list;
	/* if fifo isn't expired, next request to serve */
	struct cfq_rq *next_crq;
	/* requests queued in sort_list */
	int queued[2];
	/* currently allocated requests */
	int allocated[2];
	/* fifo list of requests in sort_list */
	struct list_head fifo[2];
	/* last time fifo expired */
	unsigned long last_fifo_expire;

	int key_type;

	unsigned long service_start;
	unsigned long service_used;

	unsigned int max_rate;

	/* number of requests that have been handed to the driver */
	int in_flight;
	/* number of currently allocated requests */
	int alloc_limit[2];
};

struct cfq_rq {
	struct rb_node rb_node;
	sector_t rb_key;
	struct request *request;
	struct hlist_node hash;

	struct cfq_queue *cfq_queue;
	struct cfq_io_context *io_context;

	unsigned long service_start;
	unsigned long queue_start;

	unsigned int in_flight : 1;
	unsigned int accounted : 1;
	unsigned int is_sync   : 1;
	unsigned int is_write  : 1;
};

static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned long);
static void cfq_dispatch_sort(request_queue_t *, struct cfq_rq *);
static void cfq_update_next_crq(struct cfq_rq *);
static void cfq_put_cfqd(struct cfq_data *cfqd);

/*
 * what the fairness is based on (ie how processes are grouped and
 * differentiated)
 */
static inline unsigned long
cfq_hash_key(struct cfq_data *cfqd, struct task_struct *tsk)
{
	/*
	 * optimize this so that ->key_type is the offset into the struct
	 */
	switch (cfqd->key_type) {
		case CFQ_KEY_PGID:
			return process_group(tsk);
		default:
		case CFQ_KEY_TGID:
			return tsk->tgid;
		case CFQ_KEY_UID:
			return tsk->uid;
		case CFQ_KEY_GID:
			return tsk->gid;
	}
}

/*
 * lots of deadline iosched dupes, can be abstracted later...
 */
static inline void cfq_del_crq_hash(struct cfq_rq *crq)
{
	hlist_del_init(&crq->hash);
}

static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
{
	cfq_del_crq_hash(crq);

	if (q->last_merge == crq->request)
		q->last_merge = NULL;

	cfq_update_next_crq(crq);
}

static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
{
	const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));

	BUG_ON(!hlist_unhashed(&crq->hash));

	hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
}

static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
{
	struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
	struct hlist_node *entry, *next;

	hlist_for_each_safe(entry, next, hash_list) {
		struct cfq_rq *crq = list_entry_hash(entry);
		struct request *__rq = crq->request;

		BUG_ON(hlist_unhashed(&crq->hash));

		if (!rq_mergeable(__rq)) {
			cfq_del_crq_hash(crq);
			continue;
		}

		if (rq_hash_key(__rq) == offset)
			return __rq;
	}

	return NULL;
}

/*
 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
 * We choose the request that is closest to the head right now. Distance
 * behind the head are penalized and only allowed to a certain extent.
 */
static struct cfq_rq *
cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
{
	sector_t last, s1, s2, d1 = 0, d2 = 0;
	int r1_wrap = 0, r2_wrap = 0;	/* requests are behind the disk head */
	unsigned long back_max;

	if (crq1 == NULL || crq1 == crq2)
		return crq2;
	if (crq2 == NULL)
		return crq1;

	s1 = crq1->request->sector;
	s2 = crq2->request->sector;

	last = cfqd->last_sector;

#if 0
	if (!list_empty(&cfqd->queue->queue_head)) {
		struct list_head *entry = &cfqd->queue->queue_head;
		unsigned long distance = ~0UL;
		struct request *rq;

		while ((entry = entry->prev) != &cfqd->queue->queue_head) {
			rq = list_entry_rq(entry);

			if (blk_barrier_rq(rq))
				break;

			if (distance < abs(s1 - rq->sector + rq->nr_sectors)) {
				distance = abs(s1 - rq->sector +rq->nr_sectors);
				last = rq->sector + rq->nr_sectors;
			}
			if (distance < abs(s2 - rq->sector + rq->nr_sectors)) {
				distance = abs(s2 - rq->sector +rq->nr_sectors);
				last = rq->sector + rq->nr_sectors;
			}
		}
	}
#endif

	/*
	 * by definition, 1KiB is 2 sectors
	 */
	back_max = cfqd->cfq_back_max * 2;

	/*
	 * Strict one way elevator _except_ in the case where we allow
	 * short backward seeks which are biased as twice the cost of a
	 * similar forward seek.
	 */
	if (s1 >= last)
		d1 = s1 - last;
	else if (s1 + back_max >= last)
		d1 = (last - s1) * cfqd->cfq_back_penalty;
	else
		r1_wrap = 1;

	if (s2 >= last)
		d2 = s2 - last;
	else if (s2 + back_max >= last)
		d2 = (last - s2) * cfqd->cfq_back_penalty;
	else
		r2_wrap = 1;

	/* Found required data */
	if (!r1_wrap && r2_wrap)
		return crq1;
	else if (!r2_wrap && r1_wrap)
		return crq2;
	else if (r1_wrap && r2_wrap) {
		/* both behind the head */
		if (s1 <= s2)
			return crq1;
		else
			return crq2;
	}

	/* Both requests in front of the head */
	if (d1 < d2)
		return crq1;
	else if (d2 < d1)
		return crq2;
	else {
		if (s1 >= s2)
			return crq1;
		else
			return crq2;
	}
}

/*
 * would be nice to take fifo expire time into account as well
 */
static struct cfq_rq *
cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
		  struct cfq_rq *last)
{
	struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
	struct rb_node *rbnext, *rbprev;

	if (!ON_RB(&last->rb_node))
		return NULL;

	if ((rbnext = rb_next(&last->rb_node)) == NULL)
		rbnext = rb_first(&cfqq->sort_list);

	rbprev = rb_prev(&last->rb_node);

	if (rbprev)
		crq_prev = rb_entry_crq(rbprev);
	if (rbnext)
		crq_next = rb_entry_crq(rbnext);

	return cfq_choose_req(cfqd, crq_next, crq_prev);
}

static void cfq_update_next_crq(struct cfq_rq *crq)
{
	struct cfq_queue *cfqq = crq->cfq_queue;

	if (cfqq->next_crq == crq)
		cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
}

static int cfq_check_sort_rr_list(struct cfq_queue *cfqq)
{
	struct list_head *head = &cfqq->cfqd->rr_list;
	struct list_head *next, *prev;

	/*
	 * list might still be ordered
	 */
	next = cfqq->cfq_list.next;
	if (next != head) {
		struct cfq_queue *cnext = list_entry_cfqq(next);

		if (cfqq->service_used > cnext->service_used)
			return 1;
	}

	prev = cfqq->cfq_list.prev;
	if (prev != head) {
		struct cfq_queue *cprev = list_entry_cfqq(prev);

		if (cfqq->service_used < cprev->service_used)
			return 1;
	}

	return 0;
}

static void cfq_sort_rr_list(struct cfq_queue *cfqq, int new_queue)
{
	struct list_head *entry = &cfqq->cfqd->rr_list;

	if (!cfqq->on_rr)
		return;
	if (!new_queue && !cfq_check_sort_rr_list(cfqq))
		return;

	list_del(&cfqq->cfq_list);

	/*
	 * sort by our mean service_used, sub-sort by in-flight requests
	 */
	while ((entry = entry->prev) != &cfqq->cfqd->rr_list) {
		struct cfq_queue *__cfqq = list_entry_cfqq(entry);

		if (cfqq->service_used > __cfqq->service_used)
			break;
		else if (cfqq->service_used == __cfqq->service_used) {
			struct list_head *prv;

			while ((prv = entry->prev) != &cfqq->cfqd->rr_list) {
				__cfqq = list_entry_cfqq(prv);

				WARN_ON(__cfqq->service_used > cfqq->service_used);
				if (cfqq->service_used != __cfqq->service_used)
					break;
				if (cfqq->in_flight > __cfqq->in_flight)
					break;

				entry = prv;
			}
		}
	}

	list_add(&cfqq->cfq_list, entry);
}

/*
 * add to busy list of queues for service, trying to be fair in ordering
 * the pending list according to requests serviced
 */
static inline void
cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
{
	/*
	 * it's currently on the empty list
	 */
	cfqq->on_rr = 1;
	cfqd->busy_queues++;

	if (time_after(jiffies, cfqq->service_start + cfq_service))
		cfqq->service_used >>= 3;

	cfq_sort_rr_list(cfqq, 1);
}

static inline void
cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
{
	list_move(&cfqq->cfq_list, &cfqd->empty_list);
	cfqq->on_rr = 0;

	BUG_ON(!cfqd->busy_queues);
	cfqd->busy_queues--;
}

/*
 * rb tree support functions
 */
static inline void cfq_del_crq_rb(struct cfq_rq *crq)
{
	struct cfq_queue *cfqq = crq->cfq_queue;

	if (ON_RB(&crq->rb_node)) {
		struct cfq_data *cfqd = cfqq->cfqd;

		BUG_ON(!cfqq->queued[crq->is_sync]);

		cfq_update_next_crq(crq);

		cfqq->queued[crq->is_sync]--;
		rb_erase(&crq->rb_node, &cfqq->sort_list);
		RB_CLEAR_COLOR(&crq->rb_node);

		if (RB_EMPTY(&cfqq->sort_list) && cfqq->on_rr)
			cfq_del_cfqq_rr(cfqd, cfqq);
	}
}

static struct cfq_rq *
__cfq_add_crq_rb(struct cfq_rq *crq)
{
	struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
	struct rb_node *parent = NULL;
	struct cfq_rq *__crq;

	while (*p) {
		parent = *p;
		__crq = rb_entry_crq(parent);

		if (crq->rb_key < __crq->rb_key)
			p = &(*p)->rb_left;
		else if (crq->rb_key > __crq->rb_key)
			p = &(*p)->rb_right;
		else
			return __crq;
	}

	rb_link_node(&crq->rb_node, parent, p);
	return NULL;
}

static void cfq_add_crq_rb(struct cfq_rq *crq)
{
	struct cfq_queue *cfqq = crq->cfq_queue;
	struct cfq_data *cfqd = cfqq->cfqd;
	struct request *rq = crq->request;
	struct cfq_rq *__alias;

	crq->rb_key = rq_rb_key(rq);
	cfqq->queued[crq->is_sync]++;

	/*
	 * looks a little odd, but the first insert might return an alias.
	 * if that happens, put the alias on the dispatch list
	 */
	while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
		cfq_dispatch_sort(cfqd->queue, __alias);

	rb_insert_color(&crq->rb_node, &cfqq->sort_list);

	if (!cfqq->on_rr)
		cfq_add_cfqq_rr(cfqd, cfqq);

	/*
	 * check if this request is a better next-serve candidate
	 */
	cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
}

static inline void
cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
{
	if (ON_RB(&crq->rb_node)) {
		rb_erase(&crq->rb_node, &cfqq->sort_list);
		cfqq->queued[crq->is_sync]--;
	}

	cfq_add_crq_rb(crq);
}

static struct request *
cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
{
	const unsigned long key = cfq_hash_key(cfqd, current);
	struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, key);
	struct rb_node *n;

	if (!cfqq)
		goto out;

	n = cfqq->sort_list.rb_node;
	while (n) {
		struct cfq_rq *crq = rb_entry_crq(n);

		if (sector < crq->rb_key)
			n = n->rb_left;
		else if (sector > crq->rb_key)
			n = n->rb_right;
		else
			return crq->request;
	}

out:
	return NULL;
}

static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
{
	struct cfq_rq *crq = RQ_DATA(rq);

	if (crq) {
		struct cfq_queue *cfqq = crq->cfq_queue;

		if (cfqq->cfqd->cfq_tagged) {
			cfqq->service_used--;
			cfq_sort_rr_list(cfqq, 0);
		}

		if (crq->accounted) {
			crq->accounted = 0;
			cfqq->cfqd->rq_in_driver--;
		}
	}
}

/*
 * make sure the service time gets corrected on reissue of this request
 */
static void cfq_requeue_request(request_queue_t *q, struct request *rq)
{
	cfq_deactivate_request(q, rq);
	list_add(&rq->queuelist, &q->queue_head);
}

static void cfq_remove_request(request_queue_t *q, struct request *rq)
{
	struct cfq_rq *crq = RQ_DATA(rq);

	if (crq) {
		cfq_remove_merge_hints(q, crq);
		list_del_init(&rq->queuelist);

		if (crq->cfq_queue)
			cfq_del_crq_rb(crq);
	}
}

static int
cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
{
	struct cfq_data *cfqd = q->elevator->elevator_data;
	struct request *__rq;
	int ret;

	ret = elv_try_last_merge(q, bio);
	if (ret != ELEVATOR_NO_MERGE) {
		__rq = q->last_merge;
		goto out_insert;
	}

	__rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
	if (__rq) {
		BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);

		if (elv_rq_merge_ok(__rq, bio)) {
			ret = ELEVATOR_BACK_MERGE;
			goto out;
		}
	}

	__rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
	if (__rq) {
		if (elv_rq_merge_ok(__rq, bio)) {
			ret = ELEVATOR_FRONT_MERGE;
			goto out;
		}
	}

	return ELEVATOR_NO_MERGE;
out:
	q->last_merge = __rq;
out_insert:
	*req = __rq;
	return ret;
}

static void cfq_merged_request(request_queue_t *q, struct request *req)
{
	struct cfq_data *cfqd = q->elevator->elevator_data;
	struct cfq_rq *crq = RQ_DATA(req);

	cfq_del_crq_hash(crq);
	cfq_add_crq_hash(cfqd, crq);

	if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
		struct cfq_queue *cfqq = crq->cfq_queue;

		cfq_update_next_crq(crq);
		cfq_reposition_crq_rb(cfqq, crq);
	}

	q->last_merge = req;
}

static void
cfq_merged_requests(request_queue_t *q, struct request *rq,
		    struct request *next)
{
	struct cfq_rq *crq = RQ_DATA(rq);
	struct cfq_rq *cnext = RQ_DATA(next);

	cfq_merged_request(q, rq);

	if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist)) {
		if (time_before(cnext->queue_start, crq->queue_start)) {
			list_move(&rq->queuelist, &next->queuelist);
			crq->queue_start = cnext->queue_start;
		}
	}

	cfq_update_next_crq(cnext);
	cfq_remove_request(q, next);
}

/*
 * we dispatch cfqd->cfq_quantum requests in total from the rr_list queues,
 * this function sector sorts the selected request to minimize seeks. we start
 * at cfqd->last_sector, not 0.
 */
static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
{
	struct cfq_data *cfqd = q->elevator->elevator_data;
	struct cfq_queue *cfqq = crq->cfq_queue;
	struct list_head *head = &q->queue_head, *entry = head;
	struct request *__rq;
	sector_t last;

	cfq_del_crq_rb(crq);
	cfq_remove_merge_hints(q, crq);
	list_del(&crq->request->queuelist);

	last = cfqd->last_sector;
	while ((entry = entry->prev) != head) {
		__rq = list_entry_rq(entry);

		if (blk_barrier_rq(crq->request))
			break;
		if (!blk_fs_request(crq->request))
			break;

		if (crq->request->sector > __rq->sector)
			break;
		if (__rq->sector > last && crq->request->sector < last) {
			last = crq->request->sector;
			break;
		}
	}

	cfqd->last_sector = last;
	crq->in_flight = 1;
	cfqq->in_flight++;
	list_add(&crq->request->queuelist, entry);
}

/*
 * return expired entry, or NULL to just start from scratch in rbtree
 */
static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
{
	struct cfq_data *cfqd = cfqq->cfqd;
	const int reads = !list_empty(&cfqq->fifo[0]);
	const int writes = !list_empty(&cfqq->fifo[1]);
	unsigned long now = jiffies;
	struct cfq_rq *crq;

	if (time_before(now, cfqq->last_fifo_expire + cfqd->cfq_fifo_batch_expire))
		return NULL;

	crq = RQ_DATA(list_entry(cfqq->fifo[0].next, struct request, queuelist));
	if (reads && time_after(now, crq->queue_start + cfqd->cfq_fifo_expire_r)) {
		cfqq->last_fifo_expire = now;
		return crq;
	}

	crq = RQ_DATA(list_entry(cfqq->fifo[1].next, struct request, queuelist));
	if (writes && time_after(now, crq->queue_start + cfqd->cfq_fifo_expire_w)) {
		cfqq->last_fifo_expire = now;
		return crq;
	}

	return NULL;
}

/*
 * dispatch a single request from given queue
 */
static inline void
cfq_dispatch_request(request_queue_t *q, struct cfq_data *cfqd,
		     struct cfq_queue *cfqq)
{
	struct cfq_rq *crq;

	/*
	 * follow expired path, else get first next available
	 */
	if ((crq = cfq_check_fifo(cfqq)) == NULL) {
		if (cfqd->find_best_crq)
			crq = cfqq->next_crq;
		else
			crq = rb_entry_crq(rb_first(&cfqq->sort_list));
	}

	cfqd->last_sector = crq->request->sector + crq->request->nr_sectors;

	/*
	 * finally, insert request into driver list
	 */
	cfq_dispatch_sort(q, crq);
}

static int cfq_dispatch_requests(request_queue_t *q, int max_dispatch)
{
	struct cfq_data *cfqd = q->elevator->elevator_data;
	struct cfq_queue *cfqq;
	struct list_head *entry, *tmp;
	int queued, busy_queues, first_round;

	if (list_empty(&cfqd->rr_list))
		return 0;

	queued = 0;
	first_round = 1;
restart:
	busy_queues = 0;
	list_for_each_safe(entry, tmp, &cfqd->rr_list) {
		cfqq = list_entry_cfqq(entry);

		BUG_ON(RB_EMPTY(&cfqq->sort_list));

		/*
		 * first round of queueing, only select from queues that
		 * don't already have io in-flight
		 */
		if (first_round && cfqq->in_flight)
			continue;

		cfq_dispatch_request(q, cfqd, cfqq);

		if (!RB_EMPTY(&cfqq->sort_list))
			busy_queues++;

		queued++;
	}

	if ((queued < max_dispatch) && (busy_queues || first_round)) {
		first_round = 0;
		goto restart;
	}

	return queued;
}

static inline void cfq_account_dispatch(struct cfq_rq *crq)
{
	struct cfq_queue *cfqq = crq->cfq_queue;
	struct cfq_data *cfqd = cfqq->cfqd;
	unsigned long now, elapsed;

	if (!blk_fs_request(crq->request))
		return;

	/*
	 * accounted bit is necessary since some drivers will call
	 * elv_next_request() many times for the same request (eg ide)
	 */
	if (crq->accounted)
		return;

	now = jiffies;
	if (cfqq->service_start == ~0UL)
		cfqq->service_start = now;

	/*
	 * on drives with tagged command queueing, command turn-around time
	 * doesn't necessarily reflect the time spent processing this very
	 * command inside the drive. so do the accounting differently there,
	 * by just sorting on the number of requests
	 */
	if (cfqd->cfq_tagged) {
		if (time_after(now, cfqq->service_start + cfq_service)) {
			cfqq->service_start = now;
			cfqq->service_used /= 10;
		}

		cfqq->service_used++;
		cfq_sort_rr_list(cfqq, 0);
	}

	elapsed = now - crq->queue_start;
	if (elapsed > max_elapsed_dispatch)
		max_elapsed_dispatch = elapsed;

	crq->accounted = 1;
	crq->service_start = now;

	if (++cfqd->rq_in_driver >= CFQ_MAX_TAG && !cfqd->cfq_tagged) {
		cfqq->cfqd->cfq_tagged = 1;
		printk("cfq: depth %d reached, tagging now on\n", CFQ_MAX_TAG);
	}
}

static inline void
cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
{
	struct cfq_data *cfqd = cfqq->cfqd;

	if (!crq->accounted)
		return;

	WARN_ON(!cfqd->rq_in_driver);
	cfqd->rq_in_driver--;

	if (!cfqd->cfq_tagged) {
		unsigned long now = jiffies;
		unsigned long duration = now - crq->service_start;

		if (time_after(now, cfqq->service_start + cfq_service)) {
			cfqq->service_start = now;
			cfqq->service_used >>= 3;
		}

		cfqq->service_used += duration;
		cfq_sort_rr_list(cfqq, 0);

		if (duration > max_elapsed_crq)
			max_elapsed_crq = duration;
	}
}

static struct request *cfq_next_request(request_queue_t *q)
{
	struct cfq_data *cfqd = q->elevator->elevator_data;
	struct request *rq;

	if (!list_empty(&q->queue_head)) {
		struct cfq_rq *crq;
dispatch:
		rq = list_entry_rq(q->queue_head.next);

		if ((crq = RQ_DATA(rq)) != NULL) {
			cfq_remove_merge_hints(q, crq);
			cfq_account_dispatch(crq);
		}

		return rq;
	}

	if (cfq_dispatch_requests(q, cfqd->cfq_quantum))
		goto dispatch;

	return NULL;
}

/*
 * task holds one reference to the queue, dropped when task exits. each crq
 * in-flight on this queue also holds a reference, dropped when crq is freed.
 *
 * queue lock must be held here.
 */
static void cfq_put_queue(struct cfq_queue *cfqq)
{
	BUG_ON(!atomic_read(&cfqq->ref));

	if (!atomic_dec_and_test(&cfqq->ref))
		return;

	BUG_ON(rb_first(&cfqq->sort_list));
	BUG_ON(cfqq->on_rr);

	cfq_put_cfqd(cfqq->cfqd);

	/*
	 * it's on the empty list and still hashed
	 */
	list_del(&cfqq->cfq_list);
	hlist_del(&cfqq->cfq_hash);
	kmem_cache_free(cfq_pool, cfqq);
}

static inline struct cfq_queue *
__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned long key, const int hashval)
{
	struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
	struct hlist_node *entry, *next;

	hlist_for_each_safe(entry, next, hash_list) {
		struct cfq_queue *__cfqq = list_entry_qhash(entry);