deadline-iosched.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Deadline i/o scheduler.
  3. *
  4. * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/fs.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/elevator.h>
  10. #include <linux/bio.h>
  11. #include <linux/config.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/compiler.h>
  16. #include <linux/hash.h>
  17. #include <linux/rbtree.h>
  18. /*
  19. * See Documentation/block/deadline-iosched.txt
  20. */
  21. static const int read_expire = HZ / 2; /* max time before a read is submitted. */
  22. static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
  23. static const int writes_starved = 2; /* max times reads can starve a write */
  24. static const int fifo_batch = 16; /* # of sequential requests treated as one
  25. by the above parameters. For throughput. */
  26. static const int deadline_hash_shift = 5;
  27. #define DL_HASH_BLOCK(sec) ((sec) >> 3)
  28. #define DL_HASH_FN(sec) (hash_long(DL_HASH_BLOCK((sec)), deadline_hash_shift))
  29. #define DL_HASH_ENTRIES (1 << deadline_hash_shift)
  30. #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
  31. #define list_entry_hash(ptr) list_entry((ptr), struct deadline_rq, hash)
  32. #define ON_HASH(drq) (drq)->on_hash
  33. struct deadline_data {
  34. /*
  35. * run time data
  36. */
  37. /*
  38. * requests (deadline_rq s) are present on both sort_list and fifo_list
  39. */
  40. struct rb_root sort_list[2];
  41. struct list_head fifo_list[2];
  42. /*
  43. * next in sort order. read, write or both are NULL
  44. */
  45. struct deadline_rq *next_drq[2];
  46. struct list_head *hash; /* request hash */
  47. unsigned int batching; /* number of sequential requests made */
  48. sector_t last_sector; /* head position */
  49. unsigned int starved; /* times reads have starved writes */
  50. /*
  51. * settings that change how the i/o scheduler behaves
  52. */
  53. int fifo_expire[2];
  54. int fifo_batch;
  55. int writes_starved;
  56. int front_merges;
  57. mempool_t *drq_pool;
  58. };
  59. /*
  60. * pre-request data.
  61. */
  62. struct deadline_rq {
  63. /*
  64. * rbtree index, key is the starting offset
  65. */
  66. struct rb_node rb_node;
  67. sector_t rb_key;
  68. struct request *request;
  69. /*
  70. * request hash, key is the ending offset (for back merge lookup)
  71. */
  72. struct list_head hash;
  73. char on_hash;
  74. /*
  75. * expire fifo
  76. */
  77. struct list_head fifo;
  78. unsigned long expires;
  79. };
  80. static void deadline_move_request(struct deadline_data *dd, struct deadline_rq *drq);
  81. static kmem_cache_t *drq_pool;
  82. #define RQ_DATA(rq) ((struct deadline_rq *) (rq)->elevator_private)
  83. /*
  84. * the back merge hash support functions
  85. */
  86. static inline void __deadline_del_drq_hash(struct deadline_rq *drq)
  87. {
  88. drq->on_hash = 0;
  89. list_del_init(&drq->hash);
  90. }
  91. static inline void deadline_del_drq_hash(struct deadline_rq *drq)
  92. {
  93. if (ON_HASH(drq))
  94. __deadline_del_drq_hash(drq);
  95. }
  96. static inline void
  97. deadline_add_drq_hash(struct deadline_data *dd, struct deadline_rq *drq)
  98. {
  99. struct request *rq = drq->request;
  100. BUG_ON(ON_HASH(drq));
  101. drq->on_hash = 1;
  102. list_add(&drq->hash, &dd->hash[DL_HASH_FN(rq_hash_key(rq))]);
  103. }
  104. /*
  105. * move hot entry to front of chain
  106. */
  107. static inline void
  108. deadline_hot_drq_hash(struct deadline_data *dd, struct deadline_rq *drq)
  109. {
  110. struct request *rq = drq->request;
  111. struct list_head *head = &dd->hash[DL_HASH_FN(rq_hash_key(rq))];
  112. if (ON_HASH(drq) && drq->hash.prev != head) {
  113. list_del(&drq->hash);
  114. list_add(&drq->hash, head);
  115. }
  116. }
  117. static struct request *
  118. deadline_find_drq_hash(struct deadline_data *dd, sector_t offset)
  119. {
  120. struct list_head *hash_list = &dd->hash[DL_HASH_FN(offset)];
  121. struct list_head *entry, *next = hash_list->next;
  122. while ((entry = next) != hash_list) {
  123. struct deadline_rq *drq = list_entry_hash(entry);
  124. struct request *__rq = drq->request;
  125. next = entry->next;
  126. BUG_ON(!ON_HASH(drq));
  127. if (!rq_mergeable(__rq)) {
  128. __deadline_del_drq_hash(drq);
  129. continue;
  130. }
  131. if (rq_hash_key(__rq) == offset)
  132. return __rq;
  133. }
  134. return NULL;
  135. }
  136. /*
  137. * rb tree support functions
  138. */
  139. #define RB_EMPTY(root) ((root)->rb_node == NULL)
  140. #define ON_RB(node) (rb_parent(node) != node)
  141. #define RB_CLEAR(node) (rb_set_parent(node, node))
  142. #define rb_entry_drq(node) rb_entry((node), struct deadline_rq, rb_node)
  143. #define DRQ_RB_ROOT(dd, drq) (&(dd)->sort_list[rq_data_dir((drq)->request)])
  144. #define rq_rb_key(rq) (rq)->sector
  145. static struct deadline_rq *
  146. __deadline_add_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
  147. {
  148. struct rb_node **p = &DRQ_RB_ROOT(dd, drq)->rb_node;
  149. struct rb_node *parent = NULL;
  150. struct deadline_rq *__drq;
  151. while (*p) {
  152. parent = *p;
  153. __drq = rb_entry_drq(parent);
  154. if (drq->rb_key < __drq->rb_key)
  155. p = &(*p)->rb_left;
  156. else if (drq->rb_key > __drq->rb_key)
  157. p = &(*p)->rb_right;
  158. else
  159. return __drq;
  160. }
  161. rb_link_node(&drq->rb_node, parent, p);
  162. return NULL;
  163. }
  164. static void
  165. deadline_add_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
  166. {
  167. struct deadline_rq *__alias;
  168. drq->rb_key = rq_rb_key(drq->request);
  169. retry:
  170. __alias = __deadline_add_drq_rb(dd, drq);
  171. if (!__alias) {
  172. rb_insert_color(&drq->rb_node, DRQ_RB_ROOT(dd, drq));
  173. return;
  174. }
  175. deadline_move_request(dd, __alias);
  176. goto retry;
  177. }
  178. static inline void
  179. deadline_del_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
  180. {
  181. const int data_dir = rq_data_dir(drq->request);
  182. if (dd->next_drq[data_dir] == drq) {
  183. struct rb_node *rbnext = rb_next(&drq->rb_node);
  184. dd->next_drq[data_dir] = NULL;
  185. if (rbnext)
  186. dd->next_drq[data_dir] = rb_entry_drq(rbnext);
  187. }
  188. BUG_ON(!ON_RB(&drq->rb_node));
  189. rb_erase(&drq->rb_node, DRQ_RB_ROOT(dd, drq));
  190. RB_CLEAR(&drq->rb_node);
  191. }
  192. static struct request *
  193. deadline_find_drq_rb(struct deadline_data *dd, sector_t sector, int data_dir)
  194. {
  195. struct rb_node *n = dd->sort_list[data_dir].rb_node;
  196. struct deadline_rq *drq;
  197. while (n) {
  198. drq = rb_entry_drq(n);
  199. if (sector < drq->rb_key)
  200. n = n->rb_left;
  201. else if (sector > drq->rb_key)
  202. n = n->rb_right;
  203. else
  204. return drq->request;
  205. }
  206. return NULL;
  207. }
  208. /*
  209. * deadline_find_first_drq finds the first (lowest sector numbered) request
  210. * for the specified data_dir. Used to sweep back to the start of the disk
  211. * (1-way elevator) after we process the last (highest sector) request.
  212. */
  213. static struct deadline_rq *
  214. deadline_find_first_drq(struct deadline_data *dd, int data_dir)
  215. {
  216. struct rb_node *n = dd->sort_list[data_dir].rb_node;
  217. for (;;) {
  218. if (n->rb_left == NULL)
  219. return rb_entry_drq(n);
  220. n = n->rb_left;
  221. }
  222. }
  223. /*
  224. * add drq to rbtree and fifo
  225. */
  226. static void
  227. deadline_add_request(struct request_queue *q, struct request *rq)
  228. {
  229. struct deadline_data *dd = q->elevator->elevator_data;
  230. struct deadline_rq *drq = RQ_DATA(rq);
  231. const int data_dir = rq_data_dir(drq->request);
  232. deadline_add_drq_rb(dd, drq);
  233. /*
  234. * set expire time (only used for reads) and add to fifo list
  235. */
  236. drq->expires = jiffies + dd->fifo_expire[data_dir];
  237. list_add_tail(&drq->fifo, &dd->fifo_list[data_dir]);
  238. if (rq_mergeable(rq))
  239. deadline_add_drq_hash(dd, drq);
  240. }
  241. /*
  242. * remove rq from rbtree, fifo, and hash
  243. */
  244. static void deadline_remove_request(request_queue_t *q, struct request *rq)
  245. {
  246. struct deadline_rq *drq = RQ_DATA(rq);
  247. struct deadline_data *dd = q->elevator->elevator_data;
  248. list_del_init(&drq->fifo);
  249. deadline_del_drq_rb(dd, drq);
  250. deadline_del_drq_hash(drq);
  251. }
  252. static int
  253. deadline_merge(request_queue_t *q, struct request **req, struct bio *bio)
  254. {
  255. struct deadline_data *dd = q->elevator->elevator_data;
  256. struct request *__rq;
  257. int ret;
  258. /*
  259. * see if the merge hash can satisfy a back merge
  260. */
  261. __rq = deadline_find_drq_hash(dd, bio->bi_sector);
  262. if (__rq) {
  263. BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
  264. if (elv_rq_merge_ok(__rq, bio)) {
  265. ret = ELEVATOR_BACK_MERGE;
  266. goto out;
  267. }
  268. }
  269. /*
  270. * check for front merge
  271. */
  272. if (dd->front_merges) {
  273. sector_t rb_key = bio->bi_sector + bio_sectors(bio);
  274. __rq = deadline_find_drq_rb(dd, rb_key, bio_data_dir(bio));
  275. if (__rq) {
  276. BUG_ON(rb_key != rq_rb_key(__rq));
  277. if (elv_rq_merge_ok(__rq, bio)) {
  278. ret = ELEVATOR_FRONT_MERGE;
  279. goto out;
  280. }
  281. }
  282. }
  283. return ELEVATOR_NO_MERGE;
  284. out:
  285. if (ret)
  286. deadline_hot_drq_hash(dd, RQ_DATA(__rq));
  287. *req = __rq;
  288. return ret;
  289. }
  290. static void deadline_merged_request(request_queue_t *q, struct request *req)
  291. {
  292. struct deadline_data *dd = q->elevator->elevator_data;
  293. struct deadline_rq *drq = RQ_DATA(req);
  294. /*
  295. * hash always needs to be repositioned, key is end sector
  296. */
  297. deadline_del_drq_hash(drq);
  298. deadline_add_drq_hash(dd, drq);
  299. /*
  300. * if the merge was a front merge, we need to reposition request
  301. */
  302. if (rq_rb_key(req) != drq->rb_key) {
  303. deadline_del_drq_rb(dd, drq);
  304. deadline_add_drq_rb(dd, drq);
  305. }
  306. }
  307. static void
  308. deadline_merged_requests(request_queue_t *q, struct request *req,
  309. struct request *next)
  310. {
  311. struct deadline_data *dd = q->elevator->elevator_data;
  312. struct deadline_rq *drq = RQ_DATA(req);
  313. struct deadline_rq *dnext = RQ_DATA(next);
  314. BUG_ON(!drq);
  315. BUG_ON(!dnext);
  316. /*
  317. * reposition drq (this is the merged request) in hash, and in rbtree
  318. * in case of a front merge
  319. */
  320. deadline_del_drq_hash(drq);
  321. deadline_add_drq_hash(dd, drq);
  322. if (rq_rb_key(req) != drq->rb_key) {
  323. deadline_del_drq_rb(dd, drq);
  324. deadline_add_drq_rb(dd, drq);
  325. }
  326. /*
  327. * if dnext expires before drq, assign its expire time to drq
  328. * and move into dnext position (dnext will be deleted) in fifo
  329. */
  330. if (!list_empty(&drq->fifo) && !list_empty(&dnext->fifo)) {
  331. if (time_before(dnext->expires, drq->expires)) {
  332. list_move(&drq->fifo, &dnext->fifo);
  333. drq->expires = dnext->expires;
  334. }
  335. }
  336. /*
  337. * kill knowledge of next, this one is a goner
  338. */
  339. deadline_remove_request(q, next);
  340. }
  341. /*
  342. * move request from sort list to dispatch queue.
  343. */
  344. static inline void
  345. deadline_move_to_dispatch(struct deadline_data *dd, struct deadline_rq *drq)
  346. {
  347. request_queue_t *q = drq->request->q;
  348. deadline_remove_request(q, drq->request);
  349. elv_dispatch_add_tail(q, drq->request);
  350. }
  351. /*
  352. * move an entry to dispatch queue
  353. */
  354. static void
  355. deadline_move_request(struct deadline_data *dd, struct deadline_rq *drq)
  356. {
  357. const int data_dir = rq_data_dir(drq->request);
  358. struct rb_node *rbnext = rb_next(&drq->rb_node);
  359. dd->next_drq[READ] = NULL;
  360. dd->next_drq[WRITE] = NULL;
  361. if (rbnext)
  362. dd->next_drq[data_dir] = rb_entry_drq(rbnext);
  363. dd->last_sector = drq->request->sector + drq->request->nr_sectors;
  364. /*
  365. * take it off the sort and fifo list, move
  366. * to dispatch queue
  367. */
  368. deadline_move_to_dispatch(dd, drq);
  369. }
  370. #define list_entry_fifo(ptr) list_entry((ptr), struct deadline_rq, fifo)
  371. /*
  372. * deadline_check_fifo returns 0 if there are no expired reads on the fifo,
  373. * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
  374. */
  375. static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
  376. {
  377. struct deadline_rq *drq = list_entry_fifo(dd->fifo_list[ddir].next);
  378. /*
  379. * drq is expired!
  380. */
  381. if (time_after(jiffies, drq->expires))
  382. return 1;
  383. return 0;
  384. }
  385. /*
  386. * deadline_dispatch_requests selects the best request according to
  387. * read/write expire, fifo_batch, etc
  388. */
  389. static int deadline_dispatch_requests(request_queue_t *q, int force)
  390. {
  391. struct deadline_data *dd = q->elevator->elevator_data;
  392. const int reads = !list_empty(&dd->fifo_list[READ]);
  393. const int writes = !list_empty(&dd->fifo_list[WRITE]);
  394. struct deadline_rq *drq;
  395. int data_dir;
  396. /*
  397. * batches are currently reads XOR writes
  398. */
  399. if (dd->next_drq[WRITE])
  400. drq = dd->next_drq[WRITE];
  401. else
  402. drq = dd->next_drq[READ];
  403. if (drq) {
  404. /* we have a "next request" */
  405. if (dd->last_sector != drq->request->sector)
  406. /* end the batch on a non sequential request */
  407. dd->batching += dd->fifo_batch;
  408. if (dd->batching < dd->fifo_batch)
  409. /* we are still entitled to batch */
  410. goto dispatch_request;
  411. }
  412. /*
  413. * at this point we are not running a batch. select the appropriate
  414. * data direction (read / write)
  415. */
  416. if (reads) {
  417. BUG_ON(RB_EMPTY(&dd->sort_list[READ]));
  418. if (writes && (dd->starved++ >= dd->writes_starved))
  419. goto dispatch_writes;
  420. data_dir = READ;
  421. goto dispatch_find_request;
  422. }
  423. /*
  424. * there are either no reads or writes have been starved
  425. */
  426. if (writes) {
  427. dispatch_writes:
  428. BUG_ON(RB_EMPTY(&dd->sort_list[WRITE]));
  429. dd->starved = 0;
  430. data_dir = WRITE;
  431. goto dispatch_find_request;
  432. }
  433. return 0;
  434. dispatch_find_request:
  435. /*
  436. * we are not running a batch, find best request for selected data_dir
  437. */
  438. if (deadline_check_fifo(dd, data_dir)) {
  439. /* An expired request exists - satisfy it */
  440. dd->batching = 0;
  441. drq = list_entry_fifo(dd->fifo_list[data_dir].next);
  442. } else if (dd->next_drq[data_dir]) {
  443. /*
  444. * The last req was the same dir and we have a next request in
  445. * sort order. No expired requests so continue on from here.
  446. */
  447. drq = dd->next_drq[data_dir];
  448. } else {
  449. /*
  450. * The last req was the other direction or we have run out of
  451. * higher-sectored requests. Go back to the lowest sectored
  452. * request (1 way elevator) and start a new batch.
  453. */
  454. dd->batching = 0;
  455. drq = deadline_find_first_drq(dd, data_dir);
  456. }
  457. dispatch_request:
  458. /*
  459. * drq is the selected appropriate request.
  460. */
  461. dd->batching++;
  462. deadline_move_request(dd, drq);
  463. return 1;
  464. }
  465. static int deadline_queue_empty(request_queue_t *q)
  466. {
  467. struct deadline_data *dd = q->elevator->elevator_data;
  468. return list_empty(&dd->fifo_list[WRITE])
  469. && list_empty(&dd->fifo_list[READ]);
  470. }
  471. static struct request *
  472. deadline_former_request(request_queue_t *q, struct request *rq)
  473. {
  474. struct deadline_rq *drq = RQ_DATA(rq);
  475. struct rb_node *rbprev = rb_prev(&drq->rb_node);
  476. if (rbprev)
  477. return rb_entry_drq(rbprev)->request;
  478. return NULL;
  479. }
  480. static struct request *
  481. deadline_latter_request(request_queue_t *q, struct request *rq)
  482. {
  483. struct deadline_rq *drq = RQ_DATA(rq);
  484. struct rb_node *rbnext = rb_next(&drq->rb_node);
  485. if (rbnext)
  486. return rb_entry_drq(rbnext)->request;
  487. return NULL;
  488. }
  489. static void deadline_exit_queue(elevator_t *e)
  490. {
  491. struct deadline_data *dd = e->elevator_data;
  492. BUG_ON(!list_empty(&dd->fifo_list[READ]));
  493. BUG_ON(!list_empty(&dd->fifo_list[WRITE]));
  494. mempool_destroy(dd->drq_pool);
  495. kfree(dd->hash);
  496. kfree(dd);
  497. }
  498. /*
  499. * initialize elevator private data (deadline_data), and alloc a drq for
  500. * each request on the free lists
  501. */
  502. static void *deadline_init_queue(request_queue_t *q, elevator_t *e)
  503. {
  504. struct deadline_data *dd;
  505. int i;
  506. if (!drq_pool)
  507. return NULL;
  508. dd = kmalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
  509. if (!dd)
  510. return NULL;
  511. memset(dd, 0, sizeof(*dd));
  512. dd->hash = kmalloc_node(sizeof(struct list_head)*DL_HASH_ENTRIES,
  513. GFP_KERNEL, q->node);
  514. if (!dd->hash) {
  515. kfree(dd);
  516. return NULL;
  517. }
  518. dd->drq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  519. mempool_free_slab, drq_pool, q->node);
  520. if (!dd->drq_pool) {
  521. kfree(dd->hash);
  522. kfree(dd);
  523. return NULL;
  524. }
  525. for (i = 0; i < DL_HASH_ENTRIES; i++)
  526. INIT_LIST_HEAD(&dd->hash[i]);
  527. INIT_LIST_HEAD(&dd->fifo_list[READ]);
  528. INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
  529. dd->sort_list[READ] = RB_ROOT;
  530. dd->sort_list[WRITE] = RB_ROOT;
  531. dd->fifo_expire[READ] = read_expire;
  532. dd->fifo_expire[WRITE] = write_expire;
  533. dd->writes_starved = writes_starved;
  534. dd->front_merges = 1;
  535. dd->fifo_batch = fifo_batch;
  536. return dd;
  537. }
  538. static void deadline_put_request(request_queue_t *q, struct request *rq)
  539. {
  540. struct deadline_data *dd = q->elevator->elevator_data;
  541. struct deadline_rq *drq = RQ_DATA(rq);
  542. mempool_free(drq, dd->drq_pool);
  543. rq->elevator_private = NULL;
  544. }
  545. static int
  546. deadline_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
  547. gfp_t gfp_mask)
  548. {
  549. struct deadline_data *dd = q->elevator->elevator_data;
  550. struct deadline_rq *drq;
  551. drq = mempool_alloc(dd->drq_pool, gfp_mask);
  552. if (drq) {
  553. memset(drq, 0, sizeof(*drq));
  554. RB_CLEAR(&drq->rb_node);
  555. drq->request = rq;
  556. INIT_LIST_HEAD(&drq->hash);
  557. drq->on_hash = 0;
  558. INIT_LIST_HEAD(&drq->fifo);
  559. rq->elevator_private = drq;
  560. return 0;
  561. }
  562. return 1;
  563. }
  564. /*
  565. * sysfs parts below
  566. */
  567. static ssize_t
  568. deadline_var_show(int var, char *page)
  569. {
  570. return sprintf(page, "%d\n", var);
  571. }
  572. static ssize_t
  573. deadline_var_store(int *var, const char *page, size_t count)
  574. {
  575. char *p = (char *) page;
  576. *var = simple_strtol(p, &p, 10);
  577. return count;
  578. }
  579. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  580. static ssize_t __FUNC(elevator_t *e, char *page) \
  581. { \
  582. struct deadline_data *dd = e->elevator_data; \
  583. int __data = __VAR; \
  584. if (__CONV) \
  585. __data = jiffies_to_msecs(__data); \
  586. return deadline_var_show(__data, (page)); \
  587. }
  588. SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[READ], 1);
  589. SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[WRITE], 1);
  590. SHOW_FUNCTION(deadline_writes_starved_show, dd->writes_starved, 0);
  591. SHOW_FUNCTION(deadline_front_merges_show, dd->front_merges, 0);
  592. SHOW_FUNCTION(deadline_fifo_batch_show, dd->fifo_batch, 0);
  593. #undef SHOW_FUNCTION
  594. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  595. static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
  596. { \
  597. struct deadline_data *dd = e->elevator_data; \
  598. int __data; \
  599. int ret = deadline_var_store(&__data, (page), count); \
  600. if (__data < (MIN)) \
  601. __data = (MIN); \
  602. else if (__data > (MAX)) \
  603. __data = (MAX); \
  604. if (__CONV) \
  605. *(__PTR) = msecs_to_jiffies(__data); \
  606. else \
  607. *(__PTR) = __data; \
  608. return ret; \
  609. }
  610. STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);
  611. STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);
  612. STORE_FUNCTION(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);
  613. STORE_FUNCTION(deadline_front_merges_store, &dd->front_merges, 0, 1, 0);
  614. STORE_FUNCTION(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX, 0);
  615. #undef STORE_FUNCTION
  616. #define DD_ATTR(name) \
  617. __ATTR(name, S_IRUGO|S_IWUSR, deadline_##name##_show, \
  618. deadline_##name##_store)
  619. static struct elv_fs_entry deadline_attrs[] = {
  620. DD_ATTR(read_expire),
  621. DD_ATTR(write_expire),
  622. DD_ATTR(writes_starved),
  623. DD_ATTR(front_merges),
  624. DD_ATTR(fifo_batch),
  625. __ATTR_NULL
  626. };
  627. static struct elevator_type iosched_deadline = {
  628. .ops = {
  629. .elevator_merge_fn = deadline_merge,
  630. .elevator_merged_fn = deadline_merged_request,
  631. .elevator_merge_req_fn = deadline_merged_requests,
  632. .elevator_dispatch_fn = deadline_dispatch_requests,
  633. .elevator_add_req_fn = deadline_add_request,
  634. .elevator_queue_empty_fn = deadline_queue_empty,
  635. .elevator_former_req_fn = deadline_former_request,
  636. .elevator_latter_req_fn = deadline_latter_request,
  637. .elevator_set_req_fn = deadline_set_request,
  638. .elevator_put_req_fn = deadline_put_request,
  639. .elevator_init_fn = deadline_init_queue,
  640. .elevator_exit_fn = deadline_exit_queue,
  641. },
  642. .elevator_attrs = deadline_attrs,
  643. .elevator_name = "deadline",
  644. .elevator_owner = THIS_MODULE,
  645. };
  646. static int __init deadline_init(void)
  647. {
  648. int ret;
  649. drq_pool = kmem_cache_create("deadline_drq", sizeof(struct deadline_rq),
  650. 0, 0, NULL, NULL);
  651. if (!drq_pool)
  652. return -ENOMEM;
  653. ret = elv_register(&iosched_deadline);
  654. if (ret)
  655. kmem_cache_destroy(drq_pool);
  656. return ret;
  657. }
  658. static void __exit deadline_exit(void)
  659. {
  660. kmem_cache_destroy(drq_pool);
  661. elv_unregister(&iosched_deadline);
  662. }
  663. module_init(deadline_init);
  664. module_exit(deadline_exit);
  665. MODULE_AUTHOR("Jens Axboe");
  666. MODULE_LICENSE("GPL");
  667. MODULE_DESCRIPTION("deadline IO scheduler");