elevator.c 26 KB

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