elevator.c 26 KB

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