elevator.c 26 KB

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