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