elevator.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. strstrip(elevator_name);
  865. e = elevator_get(elevator_name);
  866. if (!e) {
  867. printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
  868. return -EINVAL;
  869. }
  870. if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
  871. elevator_put(e);
  872. return count;
  873. }
  874. if (!elevator_switch(q, e))
  875. printk(KERN_ERR "elevator: switch to %s failed\n",
  876. elevator_name);
  877. return count;
  878. }
  879. ssize_t elv_iosched_show(struct request_queue *q, char *name)
  880. {
  881. struct elevator_queue *e = q->elevator;
  882. struct elevator_type *elv;
  883. struct elevator_type *__e;
  884. int len = 0;
  885. if (!q->elevator)
  886. return sprintf(name, "none\n");
  887. elv = e->elevator_type;
  888. spin_lock(&elv_list_lock);
  889. list_for_each_entry(__e, &elv_list, list) {
  890. if (!strcmp(elv->elevator_name, __e->elevator_name))
  891. len += sprintf(name+len, "[%s] ", elv->elevator_name);
  892. else
  893. len += sprintf(name+len, "%s ", __e->elevator_name);
  894. }
  895. spin_unlock(&elv_list_lock);
  896. len += sprintf(len+name, "\n");
  897. return len;
  898. }
  899. struct request *elv_rb_former_request(struct request_queue *q,
  900. struct request *rq)
  901. {
  902. struct rb_node *rbprev = rb_prev(&rq->rb_node);
  903. if (rbprev)
  904. return rb_entry_rq(rbprev);
  905. return NULL;
  906. }
  907. EXPORT_SYMBOL(elv_rb_former_request);
  908. struct request *elv_rb_latter_request(struct request_queue *q,
  909. struct request *rq)
  910. {
  911. struct rb_node *rbnext = rb_next(&rq->rb_node);
  912. if (rbnext)
  913. return rb_entry_rq(rbnext);
  914. return NULL;
  915. }
  916. EXPORT_SYMBOL(elv_rb_latter_request);