elevator.c 27 KB

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