elevator.c 24 KB

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