elevator.c 26 KB

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