elevator.c 24 KB

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