elevator.c 26 KB

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