elevator.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * Block device elevator/IO-scheduler.
  3. *
  4. * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  5. *
  6. * 30042000 Jens Axboe <axboe@kernel.dk> :
  7. *
  8. * Split the elevator a bit so that it is possible to choose a different
  9. * one or even write a new "plug in". There are three pieces:
  10. * - elevator_fn, inserts a new request in the queue list
  11. * - elevator_merge_fn, decides whether a new buffer can be merged with
  12. * an existing request
  13. * - elevator_dequeue_fn, called when a request is taken off the active list
  14. *
  15. * 20082000 Dave Jones <davej@suse.de> :
  16. * Removed tests for max-bomb-segments, which was breaking elvtune
  17. * when run without -bN
  18. *
  19. * Jens:
  20. * - Rework again to work with bio instead of buffer_heads
  21. * - loose bi_dev comparisons, partition handling is right now
  22. * - completely modularize elevator setup and teardown
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/fs.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/elevator.h>
  29. #include <linux/bio.h>
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/compiler.h>
  34. #include <linux/delay.h>
  35. #include <linux/blktrace_api.h>
  36. #include <linux/hash.h>
  37. #include <linux/uaccess.h>
  38. #include <trace/events/block.h>
  39. #include "blk.h"
  40. static DEFINE_SPINLOCK(elv_list_lock);
  41. static LIST_HEAD(elv_list);
  42. /*
  43. * Merge hash stuff.
  44. */
  45. static const int elv_hash_shift = 6;
  46. #define ELV_HASH_BLOCK(sec) ((sec) >> 3)
  47. #define ELV_HASH_FN(sec) \
  48. (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
  49. #define ELV_HASH_ENTRIES (1 << elv_hash_shift)
  50. #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
  51. /*
  52. * Query io scheduler to see if the current process issuing bio may be
  53. * merged with rq.
  54. */
  55. static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
  56. {
  57. struct request_queue *q = rq->q;
  58. struct elevator_queue *e = q->elevator;
  59. if (e->ops->elevator_allow_merge_fn)
  60. return e->ops->elevator_allow_merge_fn(q, rq, bio);
  61. return 1;
  62. }
  63. /*
  64. * can we safely merge with this request?
  65. */
  66. int elv_rq_merge_ok(struct request *rq, struct bio *bio)
  67. {
  68. if (!rq_mergeable(rq))
  69. return 0;
  70. /*
  71. * Don't merge file system requests and discard requests
  72. */
  73. if (bio_discard(bio) != bio_discard(rq->bio))
  74. return 0;
  75. /*
  76. * different data direction or already started, don't merge
  77. */
  78. if (bio_data_dir(bio) != rq_data_dir(rq))
  79. return 0;
  80. /*
  81. * must be same device and not a special request
  82. */
  83. if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
  84. return 0;
  85. /*
  86. * only merge integrity protected bio into ditto rq
  87. */
  88. if (bio_integrity(bio) != blk_integrity_rq(rq))
  89. return 0;
  90. /*
  91. * Don't merge if failfast settings don't match
  92. */
  93. if (bio_failfast_dev(bio) != blk_failfast_dev(rq) ||
  94. bio_failfast_transport(bio) != blk_failfast_transport(rq) ||
  95. bio_failfast_driver(bio) != blk_failfast_driver(rq))
  96. return 0;
  97. if (!elv_iosched_allow_merge(rq, bio))
  98. return 0;
  99. return 1;
  100. }
  101. EXPORT_SYMBOL(elv_rq_merge_ok);
  102. static inline int elv_try_merge(struct request *__rq, struct bio *bio)
  103. {
  104. int ret = ELEVATOR_NO_MERGE;
  105. /*
  106. * we can merge and sequence is ok, check if it's possible
  107. */
  108. if (elv_rq_merge_ok(__rq, bio)) {
  109. if (blk_rq_pos(__rq) + blk_rq_sectors(__rq) == bio->bi_sector)
  110. ret = ELEVATOR_BACK_MERGE;
  111. else if (blk_rq_pos(__rq) - bio_sectors(bio) == bio->bi_sector)
  112. ret = ELEVATOR_FRONT_MERGE;
  113. }
  114. return ret;
  115. }
  116. static struct elevator_type *elevator_find(const char *name)
  117. {
  118. struct elevator_type *e;
  119. list_for_each_entry(e, &elv_list, list) {
  120. if (!strcmp(e->elevator_name, name))
  121. return e;
  122. }
  123. return NULL;
  124. }
  125. static void elevator_put(struct elevator_type *e)
  126. {
  127. module_put(e->elevator_owner);
  128. }
  129. static struct elevator_type *elevator_get(const char *name)
  130. {
  131. struct elevator_type *e;
  132. spin_lock(&elv_list_lock);
  133. e = elevator_find(name);
  134. if (!e) {
  135. char elv[ELV_NAME_MAX + strlen("-iosched")];
  136. spin_unlock(&elv_list_lock);
  137. if (!strcmp(name, "anticipatory"))
  138. sprintf(elv, "as-iosched");
  139. else
  140. sprintf(elv, "%s-iosched", name);
  141. request_module("%s", elv);
  142. spin_lock(&elv_list_lock);
  143. e = elevator_find(name);
  144. }
  145. if (e && !try_module_get(e->elevator_owner))
  146. e = NULL;
  147. spin_unlock(&elv_list_lock);
  148. return e;
  149. }
  150. static void *elevator_init_queue(struct request_queue *q,
  151. struct elevator_queue *eq)
  152. {
  153. return eq->ops->elevator_init_fn(q);
  154. }
  155. static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
  156. void *data)
  157. {
  158. q->elevator = eq;
  159. eq->elevator_data = data;
  160. }
  161. static char chosen_elevator[16];
  162. static int __init elevator_setup(char *str)
  163. {
  164. /*
  165. * Be backwards-compatible with previous kernels, so users
  166. * won't get the wrong elevator.
  167. */
  168. if (!strcmp(str, "as"))
  169. strcpy(chosen_elevator, "anticipatory");
  170. else
  171. strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
  172. return 1;
  173. }
  174. __setup("elevator=", elevator_setup);
  175. static struct kobj_type elv_ktype;
  176. static struct elevator_queue *elevator_alloc(struct request_queue *q,
  177. struct elevator_type *e)
  178. {
  179. struct elevator_queue *eq;
  180. int i;
  181. eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
  182. if (unlikely(!eq))
  183. goto err;
  184. eq->ops = &e->ops;
  185. eq->elevator_type = e;
  186. kobject_init(&eq->kobj, &elv_ktype);
  187. mutex_init(&eq->sysfs_lock);
  188. eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
  189. GFP_KERNEL, q->node);
  190. if (!eq->hash)
  191. goto err;
  192. for (i = 0; i < ELV_HASH_ENTRIES; i++)
  193. INIT_HLIST_HEAD(&eq->hash[i]);
  194. return eq;
  195. err:
  196. kfree(eq);
  197. elevator_put(e);
  198. return NULL;
  199. }
  200. static void elevator_release(struct kobject *kobj)
  201. {
  202. struct elevator_queue *e;
  203. e = container_of(kobj, struct elevator_queue, kobj);
  204. elevator_put(e->elevator_type);
  205. kfree(e->hash);
  206. kfree(e);
  207. }
  208. int elevator_init(struct request_queue *q, char *name)
  209. {
  210. struct elevator_type *e = NULL;
  211. struct elevator_queue *eq;
  212. int ret = 0;
  213. void *data;
  214. INIT_LIST_HEAD(&q->queue_head);
  215. q->last_merge = NULL;
  216. q->end_sector = 0;
  217. q->boundary_rq = NULL;
  218. if (name) {
  219. e = elevator_get(name);
  220. if (!e)
  221. return -EINVAL;
  222. }
  223. if (!e && *chosen_elevator) {
  224. e = elevator_get(chosen_elevator);
  225. if (!e)
  226. printk(KERN_ERR "I/O scheduler %s not found\n",
  227. chosen_elevator);
  228. }
  229. if (!e) {
  230. e = elevator_get(CONFIG_DEFAULT_IOSCHED);
  231. if (!e) {
  232. printk(KERN_ERR
  233. "Default I/O scheduler not found. " \
  234. "Using noop.\n");
  235. e = elevator_get("noop");
  236. }
  237. }
  238. eq = elevator_alloc(q, e);
  239. if (!eq)
  240. return -ENOMEM;
  241. data = elevator_init_queue(q, eq);
  242. if (!data) {
  243. kobject_put(&eq->kobj);
  244. return -ENOMEM;
  245. }
  246. elevator_attach(q, eq, data);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL(elevator_init);
  250. void elevator_exit(struct elevator_queue *e)
  251. {
  252. mutex_lock(&e->sysfs_lock);
  253. if (e->ops->elevator_exit_fn)
  254. e->ops->elevator_exit_fn(e);
  255. e->ops = NULL;
  256. mutex_unlock(&e->sysfs_lock);
  257. kobject_put(&e->kobj);
  258. }
  259. EXPORT_SYMBOL(elevator_exit);
  260. static inline void __elv_rqhash_del(struct request *rq)
  261. {
  262. hlist_del_init(&rq->hash);
  263. }
  264. static void elv_rqhash_del(struct request_queue *q, struct request *rq)
  265. {
  266. if (ELV_ON_HASH(rq))
  267. __elv_rqhash_del(rq);
  268. }
  269. static void elv_rqhash_add(struct request_queue *q, struct request *rq)
  270. {
  271. struct elevator_queue *e = q->elevator;
  272. BUG_ON(ELV_ON_HASH(rq));
  273. hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
  274. }
  275. static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
  276. {
  277. __elv_rqhash_del(rq);
  278. elv_rqhash_add(q, rq);
  279. }
  280. static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
  281. {
  282. struct elevator_queue *e = q->elevator;
  283. struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
  284. struct hlist_node *entry, *next;
  285. struct request *rq;
  286. hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
  287. BUG_ON(!ELV_ON_HASH(rq));
  288. if (unlikely(!rq_mergeable(rq))) {
  289. __elv_rqhash_del(rq);
  290. continue;
  291. }
  292. if (rq_hash_key(rq) == offset)
  293. return rq;
  294. }
  295. return NULL;
  296. }
  297. /*
  298. * RB-tree support functions for inserting/lookup/removal of requests
  299. * in a sorted RB tree.
  300. */
  301. struct request *elv_rb_add(struct rb_root *root, struct request *rq)
  302. {
  303. struct rb_node **p = &root->rb_node;
  304. struct rb_node *parent = NULL;
  305. struct request *__rq;
  306. while (*p) {
  307. parent = *p;
  308. __rq = rb_entry(parent, struct request, rb_node);
  309. if (blk_rq_pos(rq) < blk_rq_pos(__rq))
  310. p = &(*p)->rb_left;
  311. else if (blk_rq_pos(rq) > blk_rq_pos(__rq))
  312. p = &(*p)->rb_right;
  313. else
  314. return __rq;
  315. }
  316. rb_link_node(&rq->rb_node, parent, p);
  317. rb_insert_color(&rq->rb_node, root);
  318. return NULL;
  319. }
  320. EXPORT_SYMBOL(elv_rb_add);
  321. void elv_rb_del(struct rb_root *root, struct request *rq)
  322. {
  323. BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
  324. rb_erase(&rq->rb_node, root);
  325. RB_CLEAR_NODE(&rq->rb_node);
  326. }
  327. EXPORT_SYMBOL(elv_rb_del);
  328. struct request *elv_rb_find(struct rb_root *root, sector_t sector)
  329. {
  330. struct rb_node *n = root->rb_node;
  331. struct request *rq;
  332. while (n) {
  333. rq = rb_entry(n, struct request, rb_node);
  334. if (sector < blk_rq_pos(rq))
  335. n = n->rb_left;
  336. else if (sector > blk_rq_pos(rq))
  337. n = n->rb_right;
  338. else
  339. return rq;
  340. }
  341. return NULL;
  342. }
  343. EXPORT_SYMBOL(elv_rb_find);
  344. /*
  345. * Insert rq into dispatch queue of q. Queue lock must be held on
  346. * entry. rq is sort instead into the dispatch queue. To be used by
  347. * specific elevators.
  348. */
  349. void elv_dispatch_sort(struct request_queue *q, struct request *rq)
  350. {
  351. sector_t boundary;
  352. struct list_head *entry;
  353. int stop_flags;
  354. if (q->last_merge == rq)
  355. q->last_merge = NULL;
  356. elv_rqhash_del(q, rq);
  357. q->nr_sorted--;
  358. boundary = q->end_sector;
  359. stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
  360. list_for_each_prev(entry, &q->queue_head) {
  361. struct request *pos = list_entry_rq(entry);
  362. if (blk_discard_rq(rq) != blk_discard_rq(pos))
  363. break;
  364. if (rq_data_dir(rq) != rq_data_dir(pos))
  365. break;
  366. if (pos->cmd_flags & stop_flags)
  367. break;
  368. if (blk_rq_pos(rq) >= boundary) {
  369. if (blk_rq_pos(pos) < boundary)
  370. continue;
  371. } else {
  372. if (blk_rq_pos(pos) >= boundary)
  373. break;
  374. }
  375. if (blk_rq_pos(rq) >= blk_rq_pos(pos))
  376. break;
  377. }
  378. list_add(&rq->queuelist, entry);
  379. }
  380. EXPORT_SYMBOL(elv_dispatch_sort);
  381. /*
  382. * Insert rq into dispatch queue of q. Queue lock must be held on
  383. * entry. rq is added to the back of the dispatch queue. To be used by
  384. * specific elevators.
  385. */
  386. void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
  387. {
  388. if (q->last_merge == rq)
  389. q->last_merge = NULL;
  390. elv_rqhash_del(q, rq);
  391. q->nr_sorted--;
  392. q->end_sector = rq_end_sector(rq);
  393. q->boundary_rq = rq;
  394. list_add_tail(&rq->queuelist, &q->queue_head);
  395. }
  396. EXPORT_SYMBOL(elv_dispatch_add_tail);
  397. int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
  398. {
  399. struct elevator_queue *e = q->elevator;
  400. struct request *__rq;
  401. int ret;
  402. /*
  403. * First try one-hit cache.
  404. */
  405. if (q->last_merge) {
  406. ret = elv_try_merge(q->last_merge, bio);
  407. if (ret != ELEVATOR_NO_MERGE) {
  408. *req = q->last_merge;
  409. return ret;
  410. }
  411. }
  412. if (blk_queue_nomerges(q))
  413. return ELEVATOR_NO_MERGE;
  414. /*
  415. * See if our hash lookup can find a potential backmerge.
  416. */
  417. __rq = elv_rqhash_find(q, bio->bi_sector);
  418. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  419. *req = __rq;
  420. return ELEVATOR_BACK_MERGE;
  421. }
  422. if (e->ops->elevator_merge_fn)
  423. return e->ops->elevator_merge_fn(q, req, bio);
  424. return ELEVATOR_NO_MERGE;
  425. }
  426. void elv_merged_request(struct request_queue *q, struct request *rq, int type)
  427. {
  428. struct elevator_queue *e = q->elevator;
  429. if (e->ops->elevator_merged_fn)
  430. e->ops->elevator_merged_fn(q, rq, type);
  431. if (type == ELEVATOR_BACK_MERGE)
  432. elv_rqhash_reposition(q, rq);
  433. q->last_merge = rq;
  434. }
  435. void elv_merge_requests(struct request_queue *q, struct request *rq,
  436. struct request *next)
  437. {
  438. struct elevator_queue *e = q->elevator;
  439. if (e->ops->elevator_merge_req_fn)
  440. e->ops->elevator_merge_req_fn(q, rq, next);
  441. elv_rqhash_reposition(q, rq);
  442. elv_rqhash_del(q, next);
  443. q->nr_sorted--;
  444. q->last_merge = rq;
  445. }
  446. void elv_requeue_request(struct request_queue *q, struct request *rq)
  447. {
  448. /*
  449. * it already went through dequeue, we need to decrement the
  450. * in_flight count again
  451. */
  452. if (blk_account_rq(rq)) {
  453. q->in_flight[rq_is_sync(rq)]--;
  454. if (blk_sorted_rq(rq))
  455. elv_deactivate_rq(q, rq);
  456. }
  457. rq->cmd_flags &= ~REQ_STARTED;
  458. elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
  459. }
  460. void elv_drain_elevator(struct request_queue *q)
  461. {
  462. static int printed;
  463. while (q->elevator->ops->elevator_dispatch_fn(q, 1))
  464. ;
  465. if (q->nr_sorted == 0)
  466. return;
  467. if (printed++ < 10) {
  468. printk(KERN_ERR "%s: forced dispatching is broken "
  469. "(nr_sorted=%u), please report this\n",
  470. q->elevator->elevator_type->elevator_name, q->nr_sorted);
  471. }
  472. }
  473. /*
  474. * Call with queue lock held, interrupts disabled
  475. */
  476. void elv_quiesce_start(struct request_queue *q)
  477. {
  478. if (!q->elevator)
  479. return;
  480. queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
  481. /*
  482. * make sure we don't have any requests in flight
  483. */
  484. elv_drain_elevator(q);
  485. while (q->rq.elvpriv) {
  486. __blk_run_queue(q);
  487. spin_unlock_irq(q->queue_lock);
  488. msleep(10);
  489. spin_lock_irq(q->queue_lock);
  490. elv_drain_elevator(q);
  491. }
  492. }
  493. void elv_quiesce_end(struct request_queue *q)
  494. {
  495. queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  496. }
  497. void elv_insert(struct request_queue *q, struct request *rq, int where)
  498. {
  499. struct list_head *pos;
  500. unsigned ordseq;
  501. int unplug_it = 1;
  502. trace_block_rq_insert(q, rq);
  503. rq->q = q;
  504. switch (where) {
  505. case ELEVATOR_INSERT_FRONT:
  506. rq->cmd_flags |= REQ_SOFTBARRIER;
  507. list_add(&rq->queuelist, &q->queue_head);
  508. break;
  509. case ELEVATOR_INSERT_BACK:
  510. rq->cmd_flags |= REQ_SOFTBARRIER;
  511. elv_drain_elevator(q);
  512. list_add_tail(&rq->queuelist, &q->queue_head);
  513. /*
  514. * We kick the queue here for the following reasons.
  515. * - The elevator might have returned NULL previously
  516. * to delay requests and returned them now. As the
  517. * queue wasn't empty before this request, ll_rw_blk
  518. * won't run the queue on return, resulting in hang.
  519. * - Usually, back inserted requests won't be merged
  520. * with anything. There's no point in delaying queue
  521. * processing.
  522. */
  523. __blk_run_queue(q);
  524. break;
  525. case ELEVATOR_INSERT_SORT:
  526. BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq));
  527. rq->cmd_flags |= REQ_SORTED;
  528. q->nr_sorted++;
  529. if (rq_mergeable(rq)) {
  530. elv_rqhash_add(q, rq);
  531. if (!q->last_merge)
  532. q->last_merge = rq;
  533. }
  534. /*
  535. * Some ioscheds (cfq) run q->request_fn directly, so
  536. * rq cannot be accessed after calling
  537. * elevator_add_req_fn.
  538. */
  539. q->elevator->ops->elevator_add_req_fn(q, rq);
  540. break;
  541. case ELEVATOR_INSERT_REQUEUE:
  542. /*
  543. * If ordered flush isn't in progress, we do front
  544. * insertion; otherwise, requests should be requeued
  545. * in ordseq order.
  546. */
  547. rq->cmd_flags |= REQ_SOFTBARRIER;
  548. /*
  549. * Most requeues happen because of a busy condition,
  550. * don't force unplug of the queue for that case.
  551. */
  552. unplug_it = 0;
  553. if (q->ordseq == 0) {
  554. list_add(&rq->queuelist, &q->queue_head);
  555. break;
  556. }
  557. ordseq = blk_ordered_req_seq(rq);
  558. list_for_each(pos, &q->queue_head) {
  559. struct request *pos_rq = list_entry_rq(pos);
  560. if (ordseq <= blk_ordered_req_seq(pos_rq))
  561. break;
  562. }
  563. list_add_tail(&rq->queuelist, pos);
  564. break;
  565. default:
  566. printk(KERN_ERR "%s: bad insertion point %d\n",
  567. __func__, where);
  568. BUG();
  569. }
  570. if (unplug_it && blk_queue_plugged(q)) {
  571. int nrq = q->rq.count[BLK_RW_SYNC] + q->rq.count[BLK_RW_ASYNC]
  572. - queue_in_flight(q);
  573. if (nrq >= q->unplug_thresh)
  574. __generic_unplug_device(q);
  575. }
  576. }
  577. void __elv_add_request(struct request_queue *q, struct request *rq, int where,
  578. int plug)
  579. {
  580. if (q->ordcolor)
  581. rq->cmd_flags |= REQ_ORDERED_COLOR;
  582. if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
  583. /*
  584. * toggle ordered color
  585. */
  586. if (blk_barrier_rq(rq))
  587. q->ordcolor ^= 1;
  588. /*
  589. * barriers implicitly indicate back insertion
  590. */
  591. if (where == ELEVATOR_INSERT_SORT)
  592. where = ELEVATOR_INSERT_BACK;
  593. /*
  594. * this request is scheduling boundary, update
  595. * end_sector
  596. */
  597. if (blk_fs_request(rq) || blk_discard_rq(rq)) {
  598. q->end_sector = rq_end_sector(rq);
  599. q->boundary_rq = rq;
  600. }
  601. } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
  602. where == ELEVATOR_INSERT_SORT)
  603. where = ELEVATOR_INSERT_BACK;
  604. if (plug)
  605. blk_plug_device(q);
  606. elv_insert(q, rq, where);
  607. }
  608. EXPORT_SYMBOL(__elv_add_request);
  609. void elv_add_request(struct request_queue *q, struct request *rq, int where,
  610. int plug)
  611. {
  612. unsigned long flags;
  613. spin_lock_irqsave(q->queue_lock, flags);
  614. __elv_add_request(q, rq, where, plug);
  615. spin_unlock_irqrestore(q->queue_lock, flags);
  616. }
  617. EXPORT_SYMBOL(elv_add_request);
  618. int elv_queue_empty(struct request_queue *q)
  619. {
  620. struct elevator_queue *e = q->elevator;
  621. if (!list_empty(&q->queue_head))
  622. return 0;
  623. if (e->ops->elevator_queue_empty_fn)
  624. return e->ops->elevator_queue_empty_fn(q);
  625. return 1;
  626. }
  627. EXPORT_SYMBOL(elv_queue_empty);
  628. struct request *elv_latter_request(struct request_queue *q, struct request *rq)
  629. {
  630. struct elevator_queue *e = q->elevator;
  631. if (e->ops->elevator_latter_req_fn)
  632. return e->ops->elevator_latter_req_fn(q, rq);
  633. return NULL;
  634. }
  635. struct request *elv_former_request(struct request_queue *q, struct request *rq)
  636. {
  637. struct elevator_queue *e = q->elevator;
  638. if (e->ops->elevator_former_req_fn)
  639. return e->ops->elevator_former_req_fn(q, rq);
  640. return NULL;
  641. }
  642. int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
  643. {
  644. struct elevator_queue *e = q->elevator;
  645. if (e->ops->elevator_set_req_fn)
  646. return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
  647. rq->elevator_private = NULL;
  648. return 0;
  649. }
  650. void elv_put_request(struct request_queue *q, struct request *rq)
  651. {
  652. struct elevator_queue *e = q->elevator;
  653. if (e->ops->elevator_put_req_fn)
  654. e->ops->elevator_put_req_fn(rq);
  655. }
  656. int elv_may_queue(struct request_queue *q, int rw)
  657. {
  658. struct elevator_queue *e = q->elevator;
  659. if (e->ops->elevator_may_queue_fn)
  660. return e->ops->elevator_may_queue_fn(q, rw);
  661. return ELV_MQUEUE_MAY;
  662. }
  663. void elv_abort_queue(struct request_queue *q)
  664. {
  665. struct request *rq;
  666. while (!list_empty(&q->queue_head)) {
  667. rq = list_entry_rq(q->queue_head.next);
  668. rq->cmd_flags |= REQ_QUIET;
  669. trace_block_rq_abort(q, rq);
  670. /*
  671. * Mark this request as started so we don't trigger
  672. * any debug logic in the end I/O path.
  673. */
  674. blk_start_request(rq);
  675. __blk_end_request_all(rq, -EIO);
  676. }
  677. }
  678. EXPORT_SYMBOL(elv_abort_queue);
  679. void elv_completed_request(struct request_queue *q, struct request *rq)
  680. {
  681. struct elevator_queue *e = q->elevator;
  682. /*
  683. * request is released from the driver, io must be done
  684. */
  685. if (blk_account_rq(rq)) {
  686. q->in_flight[rq_is_sync(rq)]--;
  687. if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
  688. e->ops->elevator_completed_req_fn(q, rq);
  689. }
  690. /*
  691. * Check if the queue is waiting for fs requests to be
  692. * drained for flush sequence.
  693. */
  694. if (unlikely(q->ordseq)) {
  695. struct request *next = NULL;
  696. if (!list_empty(&q->queue_head))
  697. next = list_entry_rq(q->queue_head.next);
  698. if (!queue_in_flight(q) &&
  699. blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
  700. (!next || blk_ordered_req_seq(next) > QUEUE_ORDSEQ_DRAIN)) {
  701. blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
  702. __blk_run_queue(q);
  703. }
  704. }
  705. }
  706. #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
  707. static ssize_t
  708. elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  709. {
  710. struct elv_fs_entry *entry = to_elv(attr);
  711. struct elevator_queue *e;
  712. ssize_t error;
  713. if (!entry->show)
  714. return -EIO;
  715. e = container_of(kobj, struct elevator_queue, kobj);
  716. mutex_lock(&e->sysfs_lock);
  717. error = e->ops ? entry->show(e, page) : -ENOENT;
  718. mutex_unlock(&e->sysfs_lock);
  719. return error;
  720. }
  721. static ssize_t
  722. elv_attr_store(struct kobject *kobj, struct attribute *attr,
  723. const char *page, size_t length)
  724. {
  725. struct elv_fs_entry *entry = to_elv(attr);
  726. struct elevator_queue *e;
  727. ssize_t error;
  728. if (!entry->store)
  729. return -EIO;
  730. e = container_of(kobj, struct elevator_queue, kobj);
  731. mutex_lock(&e->sysfs_lock);
  732. error = e->ops ? entry->store(e, page, length) : -ENOENT;
  733. mutex_unlock(&e->sysfs_lock);
  734. return error;
  735. }
  736. static struct sysfs_ops elv_sysfs_ops = {
  737. .show = elv_attr_show,
  738. .store = elv_attr_store,
  739. };
  740. static struct kobj_type elv_ktype = {
  741. .sysfs_ops = &elv_sysfs_ops,
  742. .release = elevator_release,
  743. };
  744. int elv_register_queue(struct request_queue *q)
  745. {
  746. struct elevator_queue *e = q->elevator;
  747. int error;
  748. error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
  749. if (!error) {
  750. struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
  751. if (attr) {
  752. while (attr->attr.name) {
  753. if (sysfs_create_file(&e->kobj, &attr->attr))
  754. break;
  755. attr++;
  756. }
  757. }
  758. kobject_uevent(&e->kobj, KOBJ_ADD);
  759. }
  760. return error;
  761. }
  762. static void __elv_unregister_queue(struct elevator_queue *e)
  763. {
  764. kobject_uevent(&e->kobj, KOBJ_REMOVE);
  765. kobject_del(&e->kobj);
  766. }
  767. void elv_unregister_queue(struct request_queue *q)
  768. {
  769. if (q)
  770. __elv_unregister_queue(q->elevator);
  771. }
  772. void elv_register(struct elevator_type *e)
  773. {
  774. char *def = "";
  775. spin_lock(&elv_list_lock);
  776. BUG_ON(elevator_find(e->elevator_name));
  777. list_add_tail(&e->list, &elv_list);
  778. spin_unlock(&elv_list_lock);
  779. if (!strcmp(e->elevator_name, chosen_elevator) ||
  780. (!*chosen_elevator &&
  781. !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
  782. def = " (default)";
  783. printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
  784. def);
  785. }
  786. EXPORT_SYMBOL_GPL(elv_register);
  787. void elv_unregister(struct elevator_type *e)
  788. {
  789. struct task_struct *g, *p;
  790. /*
  791. * Iterate every thread in the process to remove the io contexts.
  792. */
  793. if (e->ops.trim) {
  794. read_lock(&tasklist_lock);
  795. do_each_thread(g, p) {
  796. task_lock(p);
  797. if (p->io_context)
  798. e->ops.trim(p->io_context);
  799. task_unlock(p);
  800. } while_each_thread(g, p);
  801. read_unlock(&tasklist_lock);
  802. }
  803. spin_lock(&elv_list_lock);
  804. list_del_init(&e->list);
  805. spin_unlock(&elv_list_lock);
  806. }
  807. EXPORT_SYMBOL_GPL(elv_unregister);
  808. /*
  809. * switch to new_e io scheduler. be careful not to introduce deadlocks -
  810. * we don't free the old io scheduler, before we have allocated what we
  811. * need for the new one. this way we have a chance of going back to the old
  812. * one, if the new one fails init for some reason.
  813. */
  814. static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
  815. {
  816. struct elevator_queue *old_elevator, *e;
  817. void *data;
  818. /*
  819. * Allocate new elevator
  820. */
  821. e = elevator_alloc(q, new_e);
  822. if (!e)
  823. return 0;
  824. data = elevator_init_queue(q, e);
  825. if (!data) {
  826. kobject_put(&e->kobj);
  827. return 0;
  828. }
  829. /*
  830. * Turn on BYPASS and drain all requests w/ elevator private data
  831. */
  832. spin_lock_irq(q->queue_lock);
  833. elv_quiesce_start(q);
  834. /*
  835. * Remember old elevator.
  836. */
  837. old_elevator = q->elevator;
  838. /*
  839. * attach and start new elevator
  840. */
  841. elevator_attach(q, e, data);
  842. spin_unlock_irq(q->queue_lock);
  843. __elv_unregister_queue(old_elevator);
  844. if (elv_register_queue(q))
  845. goto fail_register;
  846. /*
  847. * finally exit old elevator and turn off BYPASS.
  848. */
  849. elevator_exit(old_elevator);
  850. spin_lock_irq(q->queue_lock);
  851. elv_quiesce_end(q);
  852. spin_unlock_irq(q->queue_lock);
  853. blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
  854. return 1;
  855. fail_register:
  856. /*
  857. * switch failed, exit the new io scheduler and reattach the old
  858. * one again (along with re-adding the sysfs dir)
  859. */
  860. elevator_exit(e);
  861. q->elevator = old_elevator;
  862. elv_register_queue(q);
  863. spin_lock_irq(q->queue_lock);
  864. queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  865. spin_unlock_irq(q->queue_lock);
  866. return 0;
  867. }
  868. ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  869. size_t count)
  870. {
  871. char elevator_name[ELV_NAME_MAX];
  872. struct elevator_type *e;
  873. if (!q->elevator)
  874. return count;
  875. strlcpy(elevator_name, name, sizeof(elevator_name));
  876. strstrip(elevator_name);
  877. e = elevator_get(elevator_name);
  878. if (!e) {
  879. printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
  880. return -EINVAL;
  881. }
  882. if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
  883. elevator_put(e);
  884. return count;
  885. }
  886. if (!elevator_switch(q, e))
  887. printk(KERN_ERR "elevator: switch to %s failed\n",
  888. elevator_name);
  889. return count;
  890. }
  891. ssize_t elv_iosched_show(struct request_queue *q, char *name)
  892. {
  893. struct elevator_queue *e = q->elevator;
  894. struct elevator_type *elv;
  895. struct elevator_type *__e;
  896. int len = 0;
  897. if (!q->elevator)
  898. return sprintf(name, "none\n");
  899. elv = e->elevator_type;
  900. spin_lock(&elv_list_lock);
  901. list_for_each_entry(__e, &elv_list, list) {
  902. if (!strcmp(elv->elevator_name, __e->elevator_name))
  903. len += sprintf(name+len, "[%s] ", elv->elevator_name);
  904. else
  905. len += sprintf(name+len, "%s ", __e->elevator_name);
  906. }
  907. spin_unlock(&elv_list_lock);
  908. len += sprintf(len+name, "\n");
  909. return len;
  910. }
  911. struct request *elv_rb_former_request(struct request_queue *q,
  912. struct request *rq)
  913. {
  914. struct rb_node *rbprev = rb_prev(&rq->rb_node);
  915. if (rbprev)
  916. return rb_entry_rq(rbprev);
  917. return NULL;
  918. }
  919. EXPORT_SYMBOL(elv_rb_former_request);
  920. struct request *elv_rb_latter_request(struct request_queue *q,
  921. struct request *rq)
  922. {
  923. struct rb_node *rbnext = rb_next(&rq->rb_node);
  924. if (rbnext)
  925. return rb_entry_rq(rbnext);
  926. return NULL;
  927. }
  928. EXPORT_SYMBOL(elv_rb_latter_request);