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_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. sprintf(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. * First try one-hit cache.
  392. */
  393. if (q->last_merge) {
  394. ret = elv_try_merge(q->last_merge, bio);
  395. if (ret != ELEVATOR_NO_MERGE) {
  396. *req = q->last_merge;
  397. return ret;
  398. }
  399. }
  400. if (blk_queue_nomerges(q))
  401. return ELEVATOR_NO_MERGE;
  402. /*
  403. * See if our hash lookup can find a potential backmerge.
  404. */
  405. __rq = elv_rqhash_find(q, bio->bi_sector);
  406. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  407. *req = __rq;
  408. return ELEVATOR_BACK_MERGE;
  409. }
  410. if (e->ops->elevator_merge_fn)
  411. return e->ops->elevator_merge_fn(q, req, bio);
  412. return ELEVATOR_NO_MERGE;
  413. }
  414. void elv_merged_request(struct request_queue *q, struct request *rq, int type)
  415. {
  416. struct elevator_queue *e = q->elevator;
  417. if (e->ops->elevator_merged_fn)
  418. e->ops->elevator_merged_fn(q, rq, type);
  419. if (type == ELEVATOR_BACK_MERGE)
  420. elv_rqhash_reposition(q, rq);
  421. q->last_merge = rq;
  422. }
  423. void elv_merge_requests(struct request_queue *q, struct request *rq,
  424. struct request *next)
  425. {
  426. struct elevator_queue *e = q->elevator;
  427. if (e->ops->elevator_merge_req_fn)
  428. e->ops->elevator_merge_req_fn(q, rq, next);
  429. elv_rqhash_reposition(q, rq);
  430. elv_rqhash_del(q, next);
  431. q->nr_sorted--;
  432. q->last_merge = rq;
  433. }
  434. void elv_requeue_request(struct request_queue *q, struct request *rq)
  435. {
  436. /*
  437. * it already went through dequeue, we need to decrement the
  438. * in_flight count again
  439. */
  440. if (blk_account_rq(rq)) {
  441. q->in_flight[rq_is_sync(rq)]--;
  442. if (blk_sorted_rq(rq))
  443. elv_deactivate_rq(q, rq);
  444. }
  445. rq->cmd_flags &= ~REQ_STARTED;
  446. elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
  447. }
  448. void elv_drain_elevator(struct request_queue *q)
  449. {
  450. static int printed;
  451. while (q->elevator->ops->elevator_dispatch_fn(q, 1))
  452. ;
  453. if (q->nr_sorted == 0)
  454. return;
  455. if (printed++ < 10) {
  456. printk(KERN_ERR "%s: forced dispatching is broken "
  457. "(nr_sorted=%u), please report this\n",
  458. q->elevator->elevator_type->elevator_name, q->nr_sorted);
  459. }
  460. }
  461. /*
  462. * Call with queue lock held, interrupts disabled
  463. */
  464. void elv_quiesce_start(struct request_queue *q)
  465. {
  466. if (!q->elevator)
  467. return;
  468. queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
  469. /*
  470. * make sure we don't have any requests in flight
  471. */
  472. elv_drain_elevator(q);
  473. while (q->rq.elvpriv) {
  474. __blk_run_queue(q);
  475. spin_unlock_irq(q->queue_lock);
  476. msleep(10);
  477. spin_lock_irq(q->queue_lock);
  478. elv_drain_elevator(q);
  479. }
  480. }
  481. void elv_quiesce_end(struct request_queue *q)
  482. {
  483. queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  484. }
  485. void elv_insert(struct request_queue *q, struct request *rq, int where)
  486. {
  487. struct list_head *pos;
  488. unsigned ordseq;
  489. int unplug_it = 1;
  490. trace_block_rq_insert(q, rq);
  491. rq->q = q;
  492. switch (where) {
  493. case ELEVATOR_INSERT_FRONT:
  494. rq->cmd_flags |= REQ_SOFTBARRIER;
  495. list_add(&rq->queuelist, &q->queue_head);
  496. break;
  497. case ELEVATOR_INSERT_BACK:
  498. rq->cmd_flags |= REQ_SOFTBARRIER;
  499. elv_drain_elevator(q);
  500. list_add_tail(&rq->queuelist, &q->queue_head);
  501. /*
  502. * We kick the queue here for the following reasons.
  503. * - The elevator might have returned NULL previously
  504. * to delay requests and returned them now. As the
  505. * queue wasn't empty before this request, ll_rw_blk
  506. * won't run the queue on return, resulting in hang.
  507. * - Usually, back inserted requests won't be merged
  508. * with anything. There's no point in delaying queue
  509. * processing.
  510. */
  511. __blk_run_queue(q);
  512. break;
  513. case ELEVATOR_INSERT_SORT:
  514. BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq));
  515. rq->cmd_flags |= REQ_SORTED;
  516. q->nr_sorted++;
  517. if (rq_mergeable(rq)) {
  518. elv_rqhash_add(q, rq);
  519. if (!q->last_merge)
  520. q->last_merge = rq;
  521. }
  522. /*
  523. * Some ioscheds (cfq) run q->request_fn directly, so
  524. * rq cannot be accessed after calling
  525. * elevator_add_req_fn.
  526. */
  527. q->elevator->ops->elevator_add_req_fn(q, rq);
  528. break;
  529. case ELEVATOR_INSERT_REQUEUE:
  530. /*
  531. * If ordered flush isn't in progress, we do front
  532. * insertion; otherwise, requests should be requeued
  533. * in ordseq order.
  534. */
  535. rq->cmd_flags |= REQ_SOFTBARRIER;
  536. /*
  537. * Most requeues happen because of a busy condition,
  538. * don't force unplug of the queue for that case.
  539. */
  540. unplug_it = 0;
  541. if (q->ordseq == 0) {
  542. list_add(&rq->queuelist, &q->queue_head);
  543. break;
  544. }
  545. ordseq = blk_ordered_req_seq(rq);
  546. list_for_each(pos, &q->queue_head) {
  547. struct request *pos_rq = list_entry_rq(pos);
  548. if (ordseq <= blk_ordered_req_seq(pos_rq))
  549. break;
  550. }
  551. list_add_tail(&rq->queuelist, pos);
  552. break;
  553. default:
  554. printk(KERN_ERR "%s: bad insertion point %d\n",
  555. __func__, where);
  556. BUG();
  557. }
  558. if (unplug_it && blk_queue_plugged(q)) {
  559. int nrq = q->rq.count[BLK_RW_SYNC] + q->rq.count[BLK_RW_ASYNC]
  560. - queue_in_flight(q);
  561. if (nrq >= q->unplug_thresh)
  562. __generic_unplug_device(q);
  563. }
  564. }
  565. void __elv_add_request(struct request_queue *q, struct request *rq, int where,
  566. int plug)
  567. {
  568. if (q->ordcolor)
  569. rq->cmd_flags |= REQ_ORDERED_COLOR;
  570. if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
  571. /*
  572. * toggle ordered color
  573. */
  574. if (blk_barrier_rq(rq))
  575. q->ordcolor ^= 1;
  576. /*
  577. * barriers implicitly indicate back insertion
  578. */
  579. if (where == ELEVATOR_INSERT_SORT)
  580. where = ELEVATOR_INSERT_BACK;
  581. /*
  582. * this request is scheduling boundary, update
  583. * end_sector
  584. */
  585. if (blk_fs_request(rq) || blk_discard_rq(rq)) {
  586. q->end_sector = rq_end_sector(rq);
  587. q->boundary_rq = rq;
  588. }
  589. } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
  590. where == ELEVATOR_INSERT_SORT)
  591. where = ELEVATOR_INSERT_BACK;
  592. if (plug)
  593. blk_plug_device(q);
  594. elv_insert(q, rq, where);
  595. }
  596. EXPORT_SYMBOL(__elv_add_request);
  597. void elv_add_request(struct request_queue *q, struct request *rq, int where,
  598. int plug)
  599. {
  600. unsigned long flags;
  601. spin_lock_irqsave(q->queue_lock, flags);
  602. __elv_add_request(q, rq, where, plug);
  603. spin_unlock_irqrestore(q->queue_lock, flags);
  604. }
  605. EXPORT_SYMBOL(elv_add_request);
  606. int elv_queue_empty(struct request_queue *q)
  607. {
  608. struct elevator_queue *e = q->elevator;
  609. if (!list_empty(&q->queue_head))
  610. return 0;
  611. if (e->ops->elevator_queue_empty_fn)
  612. return e->ops->elevator_queue_empty_fn(q);
  613. return 1;
  614. }
  615. EXPORT_SYMBOL(elv_queue_empty);
  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 = 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. while (!list_empty(&q->queue_head)) {
  655. rq = list_entry_rq(q->queue_head.next);
  656. rq->cmd_flags |= REQ_QUIET;
  657. trace_block_rq_abort(q, rq);
  658. /*
  659. * Mark this request as started so we don't trigger
  660. * any debug logic in the end I/O path.
  661. */
  662. blk_start_request(rq);
  663. __blk_end_request_all(rq, -EIO);
  664. }
  665. }
  666. EXPORT_SYMBOL(elv_abort_queue);
  667. void elv_completed_request(struct request_queue *q, struct request *rq)
  668. {
  669. struct elevator_queue *e = q->elevator;
  670. /*
  671. * request is released from the driver, io must be done
  672. */
  673. if (blk_account_rq(rq)) {
  674. q->in_flight[rq_is_sync(rq)]--;
  675. if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
  676. e->ops->elevator_completed_req_fn(q, rq);
  677. }
  678. /*
  679. * Check if the queue is waiting for fs requests to be
  680. * drained for flush sequence.
  681. */
  682. if (unlikely(q->ordseq)) {
  683. struct request *next = NULL;
  684. if (!list_empty(&q->queue_head))
  685. next = list_entry_rq(q->queue_head.next);
  686. if (!queue_in_flight(q) &&
  687. blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
  688. (!next || blk_ordered_req_seq(next) > QUEUE_ORDSEQ_DRAIN)) {
  689. blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
  690. __blk_run_queue(q);
  691. }
  692. }
  693. }
  694. #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
  695. static ssize_t
  696. elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  697. {
  698. struct elv_fs_entry *entry = to_elv(attr);
  699. struct elevator_queue *e;
  700. ssize_t error;
  701. if (!entry->show)
  702. return -EIO;
  703. e = container_of(kobj, struct elevator_queue, kobj);
  704. mutex_lock(&e->sysfs_lock);
  705. error = e->ops ? entry->show(e, page) : -ENOENT;
  706. mutex_unlock(&e->sysfs_lock);
  707. return error;
  708. }
  709. static ssize_t
  710. elv_attr_store(struct kobject *kobj, struct attribute *attr,
  711. const char *page, size_t length)
  712. {
  713. struct elv_fs_entry *entry = to_elv(attr);
  714. struct elevator_queue *e;
  715. ssize_t error;
  716. if (!entry->store)
  717. return -EIO;
  718. e = container_of(kobj, struct elevator_queue, kobj);
  719. mutex_lock(&e->sysfs_lock);
  720. error = e->ops ? entry->store(e, page, length) : -ENOENT;
  721. mutex_unlock(&e->sysfs_lock);
  722. return error;
  723. }
  724. static struct sysfs_ops elv_sysfs_ops = {
  725. .show = elv_attr_show,
  726. .store = elv_attr_store,
  727. };
  728. static struct kobj_type elv_ktype = {
  729. .sysfs_ops = &elv_sysfs_ops,
  730. .release = elevator_release,
  731. };
  732. int elv_register_queue(struct request_queue *q)
  733. {
  734. struct elevator_queue *e = q->elevator;
  735. int error;
  736. error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
  737. if (!error) {
  738. struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
  739. if (attr) {
  740. while (attr->attr.name) {
  741. if (sysfs_create_file(&e->kobj, &attr->attr))
  742. break;
  743. attr++;
  744. }
  745. }
  746. kobject_uevent(&e->kobj, KOBJ_ADD);
  747. }
  748. return error;
  749. }
  750. static void __elv_unregister_queue(struct elevator_queue *e)
  751. {
  752. kobject_uevent(&e->kobj, KOBJ_REMOVE);
  753. kobject_del(&e->kobj);
  754. }
  755. void elv_unregister_queue(struct request_queue *q)
  756. {
  757. if (q)
  758. __elv_unregister_queue(q->elevator);
  759. }
  760. void elv_register(struct elevator_type *e)
  761. {
  762. char *def = "";
  763. spin_lock(&elv_list_lock);
  764. BUG_ON(elevator_find(e->elevator_name));
  765. list_add_tail(&e->list, &elv_list);
  766. spin_unlock(&elv_list_lock);
  767. if (!strcmp(e->elevator_name, chosen_elevator) ||
  768. (!*chosen_elevator &&
  769. !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
  770. def = " (default)";
  771. printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
  772. def);
  773. }
  774. EXPORT_SYMBOL_GPL(elv_register);
  775. void elv_unregister(struct elevator_type *e)
  776. {
  777. struct task_struct *g, *p;
  778. /*
  779. * Iterate every thread in the process to remove the io contexts.
  780. */
  781. if (e->ops.trim) {
  782. read_lock(&tasklist_lock);
  783. do_each_thread(g, p) {
  784. task_lock(p);
  785. if (p->io_context)
  786. e->ops.trim(p->io_context);
  787. task_unlock(p);
  788. } while_each_thread(g, p);
  789. read_unlock(&tasklist_lock);
  790. }
  791. spin_lock(&elv_list_lock);
  792. list_del_init(&e->list);
  793. spin_unlock(&elv_list_lock);
  794. }
  795. EXPORT_SYMBOL_GPL(elv_unregister);
  796. /*
  797. * switch to new_e io scheduler. be careful not to introduce deadlocks -
  798. * we don't free the old io scheduler, before we have allocated what we
  799. * need for the new one. this way we have a chance of going back to the old
  800. * one, if the new one fails init for some reason.
  801. */
  802. static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
  803. {
  804. struct elevator_queue *old_elevator, *e;
  805. void *data;
  806. /*
  807. * Allocate new elevator
  808. */
  809. e = elevator_alloc(q, new_e);
  810. if (!e)
  811. return 0;
  812. data = elevator_init_queue(q, e);
  813. if (!data) {
  814. kobject_put(&e->kobj);
  815. return 0;
  816. }
  817. /*
  818. * Turn on BYPASS and drain all requests w/ elevator private data
  819. */
  820. spin_lock_irq(q->queue_lock);
  821. elv_quiesce_start(q);
  822. /*
  823. * Remember old elevator.
  824. */
  825. old_elevator = q->elevator;
  826. /*
  827. * attach and start new elevator
  828. */
  829. elevator_attach(q, e, data);
  830. spin_unlock_irq(q->queue_lock);
  831. __elv_unregister_queue(old_elevator);
  832. if (elv_register_queue(q))
  833. goto fail_register;
  834. /*
  835. * finally exit old elevator and turn off BYPASS.
  836. */
  837. elevator_exit(old_elevator);
  838. spin_lock_irq(q->queue_lock);
  839. elv_quiesce_end(q);
  840. spin_unlock_irq(q->queue_lock);
  841. blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
  842. return 1;
  843. fail_register:
  844. /*
  845. * switch failed, exit the new io scheduler and reattach the old
  846. * one again (along with re-adding the sysfs dir)
  847. */
  848. elevator_exit(e);
  849. q->elevator = old_elevator;
  850. elv_register_queue(q);
  851. spin_lock_irq(q->queue_lock);
  852. queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  853. spin_unlock_irq(q->queue_lock);
  854. return 0;
  855. }
  856. ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  857. size_t count)
  858. {
  859. char elevator_name[ELV_NAME_MAX];
  860. struct elevator_type *e;
  861. if (!q->elevator)
  862. return count;
  863. strlcpy(elevator_name, name, sizeof(elevator_name));
  864. e = elevator_get(strstrip(elevator_name));
  865. if (!e) {
  866. printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
  867. return -EINVAL;
  868. }
  869. if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
  870. elevator_put(e);
  871. return count;
  872. }
  873. if (!elevator_switch(q, e))
  874. printk(KERN_ERR "elevator: switch to %s failed\n",
  875. elevator_name);
  876. return count;
  877. }
  878. ssize_t elv_iosched_show(struct request_queue *q, char *name)
  879. {
  880. struct elevator_queue *e = q->elevator;
  881. struct elevator_type *elv;
  882. struct elevator_type *__e;
  883. int len = 0;
  884. if (!q->elevator)
  885. return sprintf(name, "none\n");
  886. elv = e->elevator_type;
  887. spin_lock(&elv_list_lock);
  888. list_for_each_entry(__e, &elv_list, list) {
  889. if (!strcmp(elv->elevator_name, __e->elevator_name))
  890. len += sprintf(name+len, "[%s] ", elv->elevator_name);
  891. else
  892. len += sprintf(name+len, "%s ", __e->elevator_name);
  893. }
  894. spin_unlock(&elv_list_lock);
  895. len += sprintf(len+name, "\n");
  896. return len;
  897. }
  898. struct request *elv_rb_former_request(struct request_queue *q,
  899. struct request *rq)
  900. {
  901. struct rb_node *rbprev = rb_prev(&rq->rb_node);
  902. if (rbprev)
  903. return rb_entry_rq(rbprev);
  904. return NULL;
  905. }
  906. EXPORT_SYMBOL(elv_rb_former_request);
  907. struct request *elv_rb_latter_request(struct request_queue *q,
  908. struct request *rq)
  909. {
  910. struct rb_node *rbnext = rb_next(&rq->rb_node);
  911. if (rbnext)
  912. return rb_entry_rq(rbnext);
  913. return NULL;
  914. }
  915. EXPORT_SYMBOL(elv_rb_latter_request);