elevator.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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_discard(bio) != bio_discard(rq->bio))
  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. if (!strcmp(name, "anticipatory"))
  131. sprintf(elv, "as-iosched");
  132. else
  133. sprintf(elv, "%s-iosched", name);
  134. request_module("%s", elv);
  135. spin_lock(&elv_list_lock);
  136. e = elevator_find(name);
  137. }
  138. if (e && !try_module_get(e->elevator_owner))
  139. e = NULL;
  140. spin_unlock(&elv_list_lock);
  141. return e;
  142. }
  143. static void *elevator_init_queue(struct request_queue *q,
  144. struct elevator_queue *eq)
  145. {
  146. return eq->ops->elevator_init_fn(q);
  147. }
  148. static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
  149. void *data)
  150. {
  151. q->elevator = eq;
  152. eq->elevator_data = data;
  153. }
  154. static char chosen_elevator[16];
  155. static int __init elevator_setup(char *str)
  156. {
  157. /*
  158. * Be backwards-compatible with previous kernels, so users
  159. * won't get the wrong elevator.
  160. */
  161. if (!strcmp(str, "as"))
  162. strcpy(chosen_elevator, "anticipatory");
  163. else
  164. strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
  165. return 1;
  166. }
  167. __setup("elevator=", elevator_setup);
  168. static struct kobj_type elv_ktype;
  169. static struct elevator_queue *elevator_alloc(struct request_queue *q,
  170. struct elevator_type *e)
  171. {
  172. struct elevator_queue *eq;
  173. int i;
  174. eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
  175. if (unlikely(!eq))
  176. goto err;
  177. eq->ops = &e->ops;
  178. eq->elevator_type = e;
  179. kobject_init(&eq->kobj, &elv_ktype);
  180. mutex_init(&eq->sysfs_lock);
  181. eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
  182. GFP_KERNEL, q->node);
  183. if (!eq->hash)
  184. goto err;
  185. for (i = 0; i < ELV_HASH_ENTRIES; i++)
  186. INIT_HLIST_HEAD(&eq->hash[i]);
  187. return eq;
  188. err:
  189. kfree(eq);
  190. elevator_put(e);
  191. return NULL;
  192. }
  193. static void elevator_release(struct kobject *kobj)
  194. {
  195. struct elevator_queue *e;
  196. e = container_of(kobj, struct elevator_queue, kobj);
  197. elevator_put(e->elevator_type);
  198. kfree(e->hash);
  199. kfree(e);
  200. }
  201. int elevator_init(struct request_queue *q, char *name)
  202. {
  203. struct elevator_type *e = NULL;
  204. struct elevator_queue *eq;
  205. int ret = 0;
  206. void *data;
  207. INIT_LIST_HEAD(&q->queue_head);
  208. q->last_merge = NULL;
  209. q->end_sector = 0;
  210. q->boundary_rq = NULL;
  211. if (name) {
  212. e = elevator_get(name);
  213. if (!e)
  214. return -EINVAL;
  215. }
  216. if (!e && *chosen_elevator) {
  217. e = elevator_get(chosen_elevator);
  218. if (!e)
  219. printk(KERN_ERR "I/O scheduler %s not found\n",
  220. chosen_elevator);
  221. }
  222. if (!e) {
  223. e = elevator_get(CONFIG_DEFAULT_IOSCHED);
  224. if (!e) {
  225. printk(KERN_ERR
  226. "Default I/O scheduler not found. " \
  227. "Using noop.\n");
  228. e = elevator_get("noop");
  229. }
  230. }
  231. eq = elevator_alloc(q, e);
  232. if (!eq)
  233. return -ENOMEM;
  234. data = elevator_init_queue(q, eq);
  235. if (!data) {
  236. kobject_put(&eq->kobj);
  237. return -ENOMEM;
  238. }
  239. elevator_attach(q, eq, data);
  240. return ret;
  241. }
  242. EXPORT_SYMBOL(elevator_init);
  243. void elevator_exit(struct elevator_queue *e)
  244. {
  245. mutex_lock(&e->sysfs_lock);
  246. if (e->ops->elevator_exit_fn)
  247. e->ops->elevator_exit_fn(e);
  248. e->ops = NULL;
  249. mutex_unlock(&e->sysfs_lock);
  250. kobject_put(&e->kobj);
  251. }
  252. EXPORT_SYMBOL(elevator_exit);
  253. static 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. struct elevator_queue *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. struct elevator_queue *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 (blk_rq_pos(rq) < blk_rq_pos(__rq))
  303. p = &(*p)->rb_left;
  304. else if (blk_rq_pos(rq) > blk_rq_pos(__rq))
  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 < blk_rq_pos(rq))
  328. n = n->rb_left;
  329. else if (sector > blk_rq_pos(rq))
  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 (blk_discard_rq(rq) != blk_discard_rq(pos))
  356. break;
  357. if (rq_data_dir(rq) != rq_data_dir(pos))
  358. break;
  359. if (pos->cmd_flags & stop_flags)
  360. break;
  361. if (blk_rq_pos(rq) >= boundary) {
  362. if (blk_rq_pos(pos) < boundary)
  363. continue;
  364. } else {
  365. if (blk_rq_pos(pos) >= boundary)
  366. break;
  367. }
  368. if (blk_rq_pos(rq) >= blk_rq_pos(pos))
  369. break;
  370. }
  371. list_add(&rq->queuelist, entry);
  372. }
  373. EXPORT_SYMBOL(elv_dispatch_sort);
  374. /*
  375. * Insert rq into dispatch queue of q. Queue lock must be held on
  376. * entry. rq is added to the back of the dispatch queue. To be used by
  377. * specific elevators.
  378. */
  379. void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
  380. {
  381. if (q->last_merge == rq)
  382. q->last_merge = NULL;
  383. elv_rqhash_del(q, rq);
  384. q->nr_sorted--;
  385. q->end_sector = rq_end_sector(rq);
  386. q->boundary_rq = rq;
  387. list_add_tail(&rq->queuelist, &q->queue_head);
  388. }
  389. EXPORT_SYMBOL(elv_dispatch_add_tail);
  390. int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
  391. {
  392. struct elevator_queue *e = q->elevator;
  393. struct request *__rq;
  394. int ret;
  395. /*
  396. * First try one-hit cache.
  397. */
  398. if (q->last_merge) {
  399. ret = elv_try_merge(q->last_merge, bio);
  400. if (ret != ELEVATOR_NO_MERGE) {
  401. *req = q->last_merge;
  402. return ret;
  403. }
  404. }
  405. if (blk_queue_nomerges(q))
  406. return ELEVATOR_NO_MERGE;
  407. /*
  408. * See if our hash lookup can find a potential backmerge.
  409. */
  410. __rq = elv_rqhash_find(q, bio->bi_sector);
  411. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  412. *req = __rq;
  413. return ELEVATOR_BACK_MERGE;
  414. }
  415. if (e->ops->elevator_merge_fn)
  416. return e->ops->elevator_merge_fn(q, req, bio);
  417. return ELEVATOR_NO_MERGE;
  418. }
  419. void elv_merged_request(struct request_queue *q, struct request *rq, int type)
  420. {
  421. struct elevator_queue *e = q->elevator;
  422. if (e->ops->elevator_merged_fn)
  423. e->ops->elevator_merged_fn(q, rq, type);
  424. if (type == ELEVATOR_BACK_MERGE)
  425. elv_rqhash_reposition(q, rq);
  426. q->last_merge = rq;
  427. }
  428. void elv_merge_requests(struct request_queue *q, struct request *rq,
  429. struct request *next)
  430. {
  431. struct elevator_queue *e = q->elevator;
  432. if (e->ops->elevator_merge_req_fn)
  433. e->ops->elevator_merge_req_fn(q, rq, next);
  434. elv_rqhash_reposition(q, rq);
  435. elv_rqhash_del(q, next);
  436. q->nr_sorted--;
  437. q->last_merge = rq;
  438. }
  439. void elv_requeue_request(struct request_queue *q, struct request *rq)
  440. {
  441. /*
  442. * it already went through dequeue, we need to decrement the
  443. * in_flight count again
  444. */
  445. if (blk_account_rq(rq)) {
  446. q->in_flight[rq_is_sync(rq)]--;
  447. if (blk_sorted_rq(rq))
  448. elv_deactivate_rq(q, rq);
  449. }
  450. rq->cmd_flags &= ~REQ_STARTED;
  451. elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
  452. }
  453. void elv_drain_elevator(struct request_queue *q)
  454. {
  455. static int printed;
  456. while (q->elevator->ops->elevator_dispatch_fn(q, 1))
  457. ;
  458. if (q->nr_sorted == 0)
  459. return;
  460. if (printed++ < 10) {
  461. printk(KERN_ERR "%s: forced dispatching is broken "
  462. "(nr_sorted=%u), please report this\n",
  463. q->elevator->elevator_type->elevator_name, q->nr_sorted);
  464. }
  465. }
  466. /*
  467. * Call with queue lock held, interrupts disabled
  468. */
  469. void elv_quiesce_start(struct request_queue *q)
  470. {
  471. if (!q->elevator)
  472. return;
  473. queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
  474. /*
  475. * make sure we don't have any requests in flight
  476. */
  477. elv_drain_elevator(q);
  478. while (q->rq.elvpriv) {
  479. __blk_run_queue(q);
  480. spin_unlock_irq(q->queue_lock);
  481. msleep(10);
  482. spin_lock_irq(q->queue_lock);
  483. elv_drain_elevator(q);
  484. }
  485. }
  486. void elv_quiesce_end(struct request_queue *q)
  487. {
  488. queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  489. }
  490. void elv_insert(struct request_queue *q, struct request *rq, int where)
  491. {
  492. struct list_head *pos;
  493. unsigned ordseq;
  494. int unplug_it = 1;
  495. trace_block_rq_insert(q, rq);
  496. rq->q = q;
  497. switch (where) {
  498. case ELEVATOR_INSERT_FRONT:
  499. rq->cmd_flags |= REQ_SOFTBARRIER;
  500. list_add(&rq->queuelist, &q->queue_head);
  501. break;
  502. case ELEVATOR_INSERT_BACK:
  503. rq->cmd_flags |= REQ_SOFTBARRIER;
  504. elv_drain_elevator(q);
  505. list_add_tail(&rq->queuelist, &q->queue_head);
  506. /*
  507. * We kick the queue here for the following reasons.
  508. * - The elevator might have returned NULL previously
  509. * to delay requests and returned them now. As the
  510. * queue wasn't empty before this request, ll_rw_blk
  511. * won't run the queue on return, resulting in hang.
  512. * - Usually, back inserted requests won't be merged
  513. * with anything. There's no point in delaying queue
  514. * processing.
  515. */
  516. __blk_run_queue(q);
  517. break;
  518. case ELEVATOR_INSERT_SORT:
  519. BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq));
  520. rq->cmd_flags |= REQ_SORTED;
  521. q->nr_sorted++;
  522. if (rq_mergeable(rq)) {
  523. elv_rqhash_add(q, rq);
  524. if (!q->last_merge)
  525. q->last_merge = rq;
  526. }
  527. /*
  528. * Some ioscheds (cfq) run q->request_fn directly, so
  529. * rq cannot be accessed after calling
  530. * elevator_add_req_fn.
  531. */
  532. q->elevator->ops->elevator_add_req_fn(q, rq);
  533. break;
  534. case ELEVATOR_INSERT_REQUEUE:
  535. /*
  536. * If ordered flush isn't in progress, we do front
  537. * insertion; otherwise, requests should be requeued
  538. * in ordseq order.
  539. */
  540. rq->cmd_flags |= REQ_SOFTBARRIER;
  541. /*
  542. * Most requeues happen because of a busy condition,
  543. * don't force unplug of the queue for that case.
  544. */
  545. unplug_it = 0;
  546. if (q->ordseq == 0) {
  547. list_add(&rq->queuelist, &q->queue_head);
  548. break;
  549. }
  550. ordseq = blk_ordered_req_seq(rq);
  551. list_for_each(pos, &q->queue_head) {
  552. struct request *pos_rq = list_entry_rq(pos);
  553. if (ordseq <= blk_ordered_req_seq(pos_rq))
  554. break;
  555. }
  556. list_add_tail(&rq->queuelist, pos);
  557. break;
  558. default:
  559. printk(KERN_ERR "%s: bad insertion point %d\n",
  560. __func__, where);
  561. BUG();
  562. }
  563. if (unplug_it && blk_queue_plugged(q)) {
  564. int nrq = q->rq.count[BLK_RW_SYNC] + q->rq.count[BLK_RW_ASYNC]
  565. - queue_in_flight(q);
  566. if (nrq >= q->unplug_thresh)
  567. __generic_unplug_device(q);
  568. }
  569. }
  570. void __elv_add_request(struct request_queue *q, struct request *rq, int where,
  571. int plug)
  572. {
  573. if (q->ordcolor)
  574. rq->cmd_flags |= REQ_ORDERED_COLOR;
  575. if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
  576. /*
  577. * toggle ordered color
  578. */
  579. if (blk_barrier_rq(rq))
  580. q->ordcolor ^= 1;
  581. /*
  582. * barriers implicitly indicate back insertion
  583. */
  584. if (where == ELEVATOR_INSERT_SORT)
  585. where = ELEVATOR_INSERT_BACK;
  586. /*
  587. * this request is scheduling boundary, update
  588. * end_sector
  589. */
  590. if (blk_fs_request(rq) || blk_discard_rq(rq)) {
  591. q->end_sector = rq_end_sector(rq);
  592. q->boundary_rq = rq;
  593. }
  594. } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
  595. where == ELEVATOR_INSERT_SORT)
  596. where = ELEVATOR_INSERT_BACK;
  597. if (plug)
  598. blk_plug_device(q);
  599. elv_insert(q, rq, where);
  600. }
  601. EXPORT_SYMBOL(__elv_add_request);
  602. void elv_add_request(struct request_queue *q, struct request *rq, int where,
  603. int plug)
  604. {
  605. unsigned long flags;
  606. spin_lock_irqsave(q->queue_lock, flags);
  607. __elv_add_request(q, rq, where, plug);
  608. spin_unlock_irqrestore(q->queue_lock, flags);
  609. }
  610. EXPORT_SYMBOL(elv_add_request);
  611. int elv_queue_empty(struct request_queue *q)
  612. {
  613. struct elevator_queue *e = q->elevator;
  614. if (!list_empty(&q->queue_head))
  615. return 0;
  616. if (e->ops->elevator_queue_empty_fn)
  617. return e->ops->elevator_queue_empty_fn(q);
  618. return 1;
  619. }
  620. EXPORT_SYMBOL(elv_queue_empty);
  621. struct request *elv_latter_request(struct request_queue *q, struct request *rq)
  622. {
  623. struct elevator_queue *e = q->elevator;
  624. if (e->ops->elevator_latter_req_fn)
  625. return e->ops->elevator_latter_req_fn(q, rq);
  626. return NULL;
  627. }
  628. struct request *elv_former_request(struct request_queue *q, struct request *rq)
  629. {
  630. struct elevator_queue *e = q->elevator;
  631. if (e->ops->elevator_former_req_fn)
  632. return e->ops->elevator_former_req_fn(q, rq);
  633. return NULL;
  634. }
  635. int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
  636. {
  637. struct elevator_queue *e = q->elevator;
  638. if (e->ops->elevator_set_req_fn)
  639. return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
  640. rq->elevator_private = NULL;
  641. return 0;
  642. }
  643. void elv_put_request(struct request_queue *q, struct request *rq)
  644. {
  645. struct elevator_queue *e = q->elevator;
  646. if (e->ops->elevator_put_req_fn)
  647. e->ops->elevator_put_req_fn(rq);
  648. }
  649. int elv_may_queue(struct request_queue *q, int rw)
  650. {
  651. struct elevator_queue *e = q->elevator;
  652. if (e->ops->elevator_may_queue_fn)
  653. return e->ops->elevator_may_queue_fn(q, rw);
  654. return ELV_MQUEUE_MAY;
  655. }
  656. void elv_abort_queue(struct request_queue *q)
  657. {
  658. struct request *rq;
  659. while (!list_empty(&q->queue_head)) {
  660. rq = list_entry_rq(q->queue_head.next);
  661. rq->cmd_flags |= REQ_QUIET;
  662. trace_block_rq_abort(q, rq);
  663. /*
  664. * Mark this request as started so we don't trigger
  665. * any debug logic in the end I/O path.
  666. */
  667. blk_start_request(rq);
  668. __blk_end_request_all(rq, -EIO);
  669. }
  670. }
  671. EXPORT_SYMBOL(elv_abort_queue);
  672. void elv_completed_request(struct request_queue *q, struct request *rq)
  673. {
  674. struct elevator_queue *e = q->elevator;
  675. /*
  676. * request is released from the driver, io must be done
  677. */
  678. if (blk_account_rq(rq)) {
  679. q->in_flight[rq_is_sync(rq)]--;
  680. if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
  681. e->ops->elevator_completed_req_fn(q, rq);
  682. }
  683. /*
  684. * Check if the queue is waiting for fs requests to be
  685. * drained for flush sequence.
  686. */
  687. if (unlikely(q->ordseq)) {
  688. struct request *next = NULL;
  689. if (!list_empty(&q->queue_head))
  690. next = list_entry_rq(q->queue_head.next);
  691. if (!queue_in_flight(q) &&
  692. blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
  693. (!next || blk_ordered_req_seq(next) > QUEUE_ORDSEQ_DRAIN)) {
  694. blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
  695. __blk_run_queue(q);
  696. }
  697. }
  698. }
  699. #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
  700. static ssize_t
  701. elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  702. {
  703. struct elv_fs_entry *entry = to_elv(attr);
  704. struct elevator_queue *e;
  705. ssize_t error;
  706. if (!entry->show)
  707. return -EIO;
  708. e = container_of(kobj, struct elevator_queue, kobj);
  709. mutex_lock(&e->sysfs_lock);
  710. error = e->ops ? entry->show(e, page) : -ENOENT;
  711. mutex_unlock(&e->sysfs_lock);
  712. return error;
  713. }
  714. static ssize_t
  715. elv_attr_store(struct kobject *kobj, struct attribute *attr,
  716. const char *page, size_t length)
  717. {
  718. struct elv_fs_entry *entry = to_elv(attr);
  719. struct elevator_queue *e;
  720. ssize_t error;
  721. if (!entry->store)
  722. return -EIO;
  723. e = container_of(kobj, struct elevator_queue, kobj);
  724. mutex_lock(&e->sysfs_lock);
  725. error = e->ops ? entry->store(e, page, length) : -ENOENT;
  726. mutex_unlock(&e->sysfs_lock);
  727. return error;
  728. }
  729. static struct sysfs_ops elv_sysfs_ops = {
  730. .show = elv_attr_show,
  731. .store = elv_attr_store,
  732. };
  733. static struct kobj_type elv_ktype = {
  734. .sysfs_ops = &elv_sysfs_ops,
  735. .release = elevator_release,
  736. };
  737. int elv_register_queue(struct request_queue *q)
  738. {
  739. struct elevator_queue *e = q->elevator;
  740. int error;
  741. error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
  742. if (!error) {
  743. struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
  744. if (attr) {
  745. while (attr->attr.name) {
  746. if (sysfs_create_file(&e->kobj, &attr->attr))
  747. break;
  748. attr++;
  749. }
  750. }
  751. kobject_uevent(&e->kobj, KOBJ_ADD);
  752. }
  753. return error;
  754. }
  755. static void __elv_unregister_queue(struct elevator_queue *e)
  756. {
  757. kobject_uevent(&e->kobj, KOBJ_REMOVE);
  758. kobject_del(&e->kobj);
  759. }
  760. void elv_unregister_queue(struct request_queue *q)
  761. {
  762. if (q)
  763. __elv_unregister_queue(q->elevator);
  764. }
  765. void elv_register(struct elevator_type *e)
  766. {
  767. char *def = "";
  768. spin_lock(&elv_list_lock);
  769. BUG_ON(elevator_find(e->elevator_name));
  770. list_add_tail(&e->list, &elv_list);
  771. spin_unlock(&elv_list_lock);
  772. if (!strcmp(e->elevator_name, chosen_elevator) ||
  773. (!*chosen_elevator &&
  774. !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
  775. def = " (default)";
  776. printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
  777. def);
  778. }
  779. EXPORT_SYMBOL_GPL(elv_register);
  780. void elv_unregister(struct elevator_type *e)
  781. {
  782. struct task_struct *g, *p;
  783. /*
  784. * Iterate every thread in the process to remove the io contexts.
  785. */
  786. if (e->ops.trim) {
  787. read_lock(&tasklist_lock);
  788. do_each_thread(g, p) {
  789. task_lock(p);
  790. if (p->io_context)
  791. e->ops.trim(p->io_context);
  792. task_unlock(p);
  793. } while_each_thread(g, p);
  794. read_unlock(&tasklist_lock);
  795. }
  796. spin_lock(&elv_list_lock);
  797. list_del_init(&e->list);
  798. spin_unlock(&elv_list_lock);
  799. }
  800. EXPORT_SYMBOL_GPL(elv_unregister);
  801. /*
  802. * switch to new_e io scheduler. be careful not to introduce deadlocks -
  803. * we don't free the old io scheduler, before we have allocated what we
  804. * need for the new one. this way we have a chance of going back to the old
  805. * one, if the new one fails init for some reason.
  806. */
  807. static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
  808. {
  809. struct elevator_queue *old_elevator, *e;
  810. void *data;
  811. /*
  812. * Allocate new elevator
  813. */
  814. e = elevator_alloc(q, new_e);
  815. if (!e)
  816. return 0;
  817. data = elevator_init_queue(q, e);
  818. if (!data) {
  819. kobject_put(&e->kobj);
  820. return 0;
  821. }
  822. /*
  823. * Turn on BYPASS and drain all requests w/ elevator private data
  824. */
  825. spin_lock_irq(q->queue_lock);
  826. elv_quiesce_start(q);
  827. /*
  828. * Remember old elevator.
  829. */
  830. old_elevator = q->elevator;
  831. /*
  832. * attach and start new elevator
  833. */
  834. elevator_attach(q, e, data);
  835. spin_unlock_irq(q->queue_lock);
  836. __elv_unregister_queue(old_elevator);
  837. if (elv_register_queue(q))
  838. goto fail_register;
  839. /*
  840. * finally exit old elevator and turn off BYPASS.
  841. */
  842. elevator_exit(old_elevator);
  843. spin_lock_irq(q->queue_lock);
  844. elv_quiesce_end(q);
  845. spin_unlock_irq(q->queue_lock);
  846. blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
  847. return 1;
  848. fail_register:
  849. /*
  850. * switch failed, exit the new io scheduler and reattach the old
  851. * one again (along with re-adding the sysfs dir)
  852. */
  853. elevator_exit(e);
  854. q->elevator = old_elevator;
  855. elv_register_queue(q);
  856. spin_lock_irq(q->queue_lock);
  857. queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  858. spin_unlock_irq(q->queue_lock);
  859. return 0;
  860. }
  861. ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  862. size_t count)
  863. {
  864. char elevator_name[ELV_NAME_MAX];
  865. struct elevator_type *e;
  866. if (!q->elevator)
  867. return count;
  868. strlcpy(elevator_name, name, sizeof(elevator_name));
  869. strstrip(elevator_name);
  870. e = elevator_get(elevator_name);
  871. if (!e) {
  872. printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
  873. return -EINVAL;
  874. }
  875. if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
  876. elevator_put(e);
  877. return count;
  878. }
  879. if (!elevator_switch(q, e))
  880. printk(KERN_ERR "elevator: switch to %s failed\n",
  881. elevator_name);
  882. return count;
  883. }
  884. ssize_t elv_iosched_show(struct request_queue *q, char *name)
  885. {
  886. struct elevator_queue *e = q->elevator;
  887. struct elevator_type *elv;
  888. struct elevator_type *__e;
  889. int len = 0;
  890. if (!q->elevator)
  891. return sprintf(name, "none\n");
  892. elv = e->elevator_type;
  893. spin_lock(&elv_list_lock);
  894. list_for_each_entry(__e, &elv_list, list) {
  895. if (!strcmp(elv->elevator_name, __e->elevator_name))
  896. len += sprintf(name+len, "[%s] ", elv->elevator_name);
  897. else
  898. len += sprintf(name+len, "%s ", __e->elevator_name);
  899. }
  900. spin_unlock(&elv_list_lock);
  901. len += sprintf(len+name, "\n");
  902. return len;
  903. }
  904. struct request *elv_rb_former_request(struct request_queue *q,
  905. struct request *rq)
  906. {
  907. struct rb_node *rbprev = rb_prev(&rq->rb_node);
  908. if (rbprev)
  909. return rb_entry_rq(rbprev);
  910. return NULL;
  911. }
  912. EXPORT_SYMBOL(elv_rb_former_request);
  913. struct request *elv_rb_latter_request(struct request_queue *q,
  914. struct request *rq)
  915. {
  916. struct rb_node *rbnext = rb_next(&rq->rb_node);
  917. if (rbnext)
  918. return rb_entry_rq(rbnext);
  919. return NULL;
  920. }
  921. EXPORT_SYMBOL(elv_rb_latter_request);