elevator.c 27 KB

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