elevator.c 26 KB

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