elevator.c 24 KB

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