elevator.c 25 KB

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