elevator.c 25 KB

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