elevator.c 25 KB

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