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. snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%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 insted 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. if (!(rq->cmd_flags & REQ_STARTED)) {
  578. /*
  579. * This is the first time the device driver
  580. * sees this request (possibly after
  581. * requeueing). Notify IO scheduler.
  582. */
  583. if (blk_sorted_rq(rq))
  584. elv_activate_rq(q, rq);
  585. /*
  586. * just mark as started even if we don't start
  587. * it, a request that has been delayed should
  588. * not be passed by new incoming requests
  589. */
  590. rq->cmd_flags |= REQ_STARTED;
  591. blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
  592. }
  593. if (!q->boundary_rq || q->boundary_rq == rq) {
  594. q->end_sector = rq_end_sector(rq);
  595. q->boundary_rq = NULL;
  596. }
  597. if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn)
  598. break;
  599. ret = q->prep_rq_fn(q, rq);
  600. if (ret == BLKPREP_OK) {
  601. break;
  602. } else if (ret == BLKPREP_DEFER) {
  603. /*
  604. * the request may have been (partially) prepped.
  605. * we need to keep this request in the front to
  606. * avoid resource deadlock. REQ_STARTED will
  607. * prevent other fs requests from passing this one.
  608. */
  609. rq = NULL;
  610. break;
  611. } else if (ret == BLKPREP_KILL) {
  612. int nr_bytes = rq->hard_nr_sectors << 9;
  613. if (!nr_bytes)
  614. nr_bytes = rq->data_len;
  615. blkdev_dequeue_request(rq);
  616. rq->cmd_flags |= REQ_QUIET;
  617. end_that_request_chunk(rq, 0, nr_bytes);
  618. end_that_request_last(rq, 0);
  619. } else {
  620. printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
  621. ret);
  622. break;
  623. }
  624. }
  625. return rq;
  626. }
  627. EXPORT_SYMBOL(elv_next_request);
  628. void elv_dequeue_request(struct request_queue *q, struct request *rq)
  629. {
  630. BUG_ON(list_empty(&rq->queuelist));
  631. BUG_ON(ELV_ON_HASH(rq));
  632. list_del_init(&rq->queuelist);
  633. /*
  634. * the time frame between a request being removed from the lists
  635. * and to it is freed is accounted as io that is in progress at
  636. * the driver side.
  637. */
  638. if (blk_account_rq(rq))
  639. q->in_flight++;
  640. }
  641. EXPORT_SYMBOL(elv_dequeue_request);
  642. int elv_queue_empty(struct request_queue *q)
  643. {
  644. elevator_t *e = q->elevator;
  645. if (!list_empty(&q->queue_head))
  646. return 0;
  647. if (e->ops->elevator_queue_empty_fn)
  648. return e->ops->elevator_queue_empty_fn(q);
  649. return 1;
  650. }
  651. EXPORT_SYMBOL(elv_queue_empty);
  652. struct request *elv_latter_request(struct request_queue *q, struct request *rq)
  653. {
  654. elevator_t *e = q->elevator;
  655. if (e->ops->elevator_latter_req_fn)
  656. return e->ops->elevator_latter_req_fn(q, rq);
  657. return NULL;
  658. }
  659. struct request *elv_former_request(struct request_queue *q, struct request *rq)
  660. {
  661. elevator_t *e = q->elevator;
  662. if (e->ops->elevator_former_req_fn)
  663. return e->ops->elevator_former_req_fn(q, rq);
  664. return NULL;
  665. }
  666. int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
  667. {
  668. elevator_t *e = q->elevator;
  669. if (e->ops->elevator_set_req_fn)
  670. return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
  671. rq->elevator_private = NULL;
  672. return 0;
  673. }
  674. void elv_put_request(struct request_queue *q, struct request *rq)
  675. {
  676. elevator_t *e = q->elevator;
  677. if (e->ops->elevator_put_req_fn)
  678. e->ops->elevator_put_req_fn(rq);
  679. }
  680. int elv_may_queue(struct request_queue *q, int rw)
  681. {
  682. elevator_t *e = q->elevator;
  683. if (e->ops->elevator_may_queue_fn)
  684. return e->ops->elevator_may_queue_fn(q, rw);
  685. return ELV_MQUEUE_MAY;
  686. }
  687. void elv_completed_request(struct request_queue *q, struct request *rq)
  688. {
  689. elevator_t *e = q->elevator;
  690. /*
  691. * request is released from the driver, io must be done
  692. */
  693. if (blk_account_rq(rq)) {
  694. q->in_flight--;
  695. if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
  696. e->ops->elevator_completed_req_fn(q, rq);
  697. }
  698. /*
  699. * Check if the queue is waiting for fs requests to be
  700. * drained for flush sequence.
  701. */
  702. if (unlikely(q->ordseq)) {
  703. struct request *first_rq = list_entry_rq(q->queue_head.next);
  704. if (q->in_flight == 0 &&
  705. blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
  706. blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
  707. blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
  708. q->request_fn(q);
  709. }
  710. }
  711. }
  712. #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
  713. static ssize_t
  714. elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  715. {
  716. elevator_t *e = container_of(kobj, elevator_t, kobj);
  717. struct elv_fs_entry *entry = to_elv(attr);
  718. ssize_t error;
  719. if (!entry->show)
  720. return -EIO;
  721. mutex_lock(&e->sysfs_lock);
  722. error = e->ops ? entry->show(e, page) : -ENOENT;
  723. mutex_unlock(&e->sysfs_lock);
  724. return error;
  725. }
  726. static ssize_t
  727. elv_attr_store(struct kobject *kobj, struct attribute *attr,
  728. const char *page, size_t length)
  729. {
  730. elevator_t *e = container_of(kobj, elevator_t, kobj);
  731. struct elv_fs_entry *entry = to_elv(attr);
  732. ssize_t error;
  733. if (!entry->store)
  734. return -EIO;
  735. mutex_lock(&e->sysfs_lock);
  736. error = e->ops ? entry->store(e, page, length) : -ENOENT;
  737. mutex_unlock(&e->sysfs_lock);
  738. return error;
  739. }
  740. static struct sysfs_ops elv_sysfs_ops = {
  741. .show = elv_attr_show,
  742. .store = elv_attr_store,
  743. };
  744. static struct kobj_type elv_ktype = {
  745. .sysfs_ops = &elv_sysfs_ops,
  746. .release = elevator_release,
  747. };
  748. int elv_register_queue(struct request_queue *q)
  749. {
  750. elevator_t *e = q->elevator;
  751. int error;
  752. e->kobj.parent = &q->kobj;
  753. error = kobject_add(&e->kobj);
  754. if (!error) {
  755. struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
  756. if (attr) {
  757. while (attr->attr.name) {
  758. if (sysfs_create_file(&e->kobj, &attr->attr))
  759. break;
  760. attr++;
  761. }
  762. }
  763. kobject_uevent(&e->kobj, KOBJ_ADD);
  764. }
  765. return error;
  766. }
  767. static void __elv_unregister_queue(elevator_t *e)
  768. {
  769. kobject_uevent(&e->kobj, KOBJ_REMOVE);
  770. kobject_del(&e->kobj);
  771. }
  772. void elv_unregister_queue(struct request_queue *q)
  773. {
  774. if (q)
  775. __elv_unregister_queue(q->elevator);
  776. }
  777. int elv_register(struct elevator_type *e)
  778. {
  779. char *def = "";
  780. spin_lock(&elv_list_lock);
  781. BUG_ON(elevator_find(e->elevator_name));
  782. list_add_tail(&e->list, &elv_list);
  783. spin_unlock(&elv_list_lock);
  784. if (!strcmp(e->elevator_name, chosen_elevator) ||
  785. (!*chosen_elevator &&
  786. !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
  787. def = " (default)";
  788. printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, def);
  789. return 0;
  790. }
  791. EXPORT_SYMBOL_GPL(elv_register);
  792. void elv_unregister(struct elevator_type *e)
  793. {
  794. struct task_struct *g, *p;
  795. /*
  796. * Iterate every thread in the process to remove the io contexts.
  797. */
  798. if (e->ops.trim) {
  799. read_lock(&tasklist_lock);
  800. do_each_thread(g, p) {
  801. task_lock(p);
  802. if (p->io_context)
  803. e->ops.trim(p->io_context);
  804. task_unlock(p);
  805. } while_each_thread(g, p);
  806. read_unlock(&tasklist_lock);
  807. }
  808. spin_lock(&elv_list_lock);
  809. list_del_init(&e->list);
  810. spin_unlock(&elv_list_lock);
  811. }
  812. EXPORT_SYMBOL_GPL(elv_unregister);
  813. /*
  814. * switch to new_e io scheduler. be careful not to introduce deadlocks -
  815. * we don't free the old io scheduler, before we have allocated what we
  816. * need for the new one. this way we have a chance of going back to the old
  817. * one, if the new one fails init for some reason.
  818. */
  819. static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
  820. {
  821. elevator_t *old_elevator, *e;
  822. void *data;
  823. /*
  824. * Allocate new elevator
  825. */
  826. e = elevator_alloc(q, new_e);
  827. if (!e)
  828. return 0;
  829. data = elevator_init_queue(q, e);
  830. if (!data) {
  831. kobject_put(&e->kobj);
  832. return 0;
  833. }
  834. /*
  835. * Turn on BYPASS and drain all requests w/ elevator private data
  836. */
  837. spin_lock_irq(q->queue_lock);
  838. set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
  839. elv_drain_elevator(q);
  840. while (q->rq.elvpriv) {
  841. blk_remove_plug(q);
  842. q->request_fn(q);
  843. spin_unlock_irq(q->queue_lock);
  844. msleep(10);
  845. spin_lock_irq(q->queue_lock);
  846. elv_drain_elevator(q);
  847. }
  848. /*
  849. * Remember old elevator.
  850. */
  851. old_elevator = q->elevator;
  852. /*
  853. * attach and start new elevator
  854. */
  855. elevator_attach(q, e, data);
  856. spin_unlock_irq(q->queue_lock);
  857. __elv_unregister_queue(old_elevator);
  858. if (elv_register_queue(q))
  859. goto fail_register;
  860. /*
  861. * finally exit old elevator and turn off BYPASS.
  862. */
  863. elevator_exit(old_elevator);
  864. clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
  865. return 1;
  866. fail_register:
  867. /*
  868. * switch failed, exit the new io scheduler and reattach the old
  869. * one again (along with re-adding the sysfs dir)
  870. */
  871. elevator_exit(e);
  872. q->elevator = old_elevator;
  873. elv_register_queue(q);
  874. clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
  875. return 0;
  876. }
  877. ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  878. size_t count)
  879. {
  880. char elevator_name[ELV_NAME_MAX];
  881. size_t len;
  882. struct elevator_type *e;
  883. elevator_name[sizeof(elevator_name) - 1] = '\0';
  884. strncpy(elevator_name, name, sizeof(elevator_name) - 1);
  885. len = strlen(elevator_name);
  886. if (len && elevator_name[len - 1] == '\n')
  887. elevator_name[len - 1] = '\0';
  888. e = elevator_get(elevator_name);
  889. if (!e) {
  890. printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
  891. return -EINVAL;
  892. }
  893. if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
  894. elevator_put(e);
  895. return count;
  896. }
  897. if (!elevator_switch(q, e))
  898. printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
  899. return count;
  900. }
  901. ssize_t elv_iosched_show(struct request_queue *q, char *name)
  902. {
  903. elevator_t *e = q->elevator;
  904. struct elevator_type *elv = e->elevator_type;
  905. struct elevator_type *__e;
  906. int len = 0;
  907. spin_lock(&elv_list_lock);
  908. list_for_each_entry(__e, &elv_list, list) {
  909. if (!strcmp(elv->elevator_name, __e->elevator_name))
  910. len += sprintf(name+len, "[%s] ", elv->elevator_name);
  911. else
  912. len += sprintf(name+len, "%s ", __e->elevator_name);
  913. }
  914. spin_unlock(&elv_list_lock);
  915. len += sprintf(len+name, "\n");
  916. return len;
  917. }
  918. struct request *elv_rb_former_request(struct request_queue *q,
  919. struct request *rq)
  920. {
  921. struct rb_node *rbprev = rb_prev(&rq->rb_node);
  922. if (rbprev)
  923. return rb_entry_rq(rbprev);
  924. return NULL;
  925. }
  926. EXPORT_SYMBOL(elv_rb_former_request);
  927. struct request *elv_rb_latter_request(struct request_queue *q,
  928. struct request *rq)
  929. {
  930. struct rb_node *rbnext = rb_next(&rq->rb_node);
  931. if (rbnext)
  932. return rb_entry_rq(rbnext);
  933. return NULL;
  934. }
  935. EXPORT_SYMBOL(elv_rb_latter_request);