elevator.c 25 KB

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