as-iosched.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  1. /*
  2. * Anticipatory & deadline i/o scheduler.
  3. *
  4. * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
  5. * Nick Piggin <nickpiggin@yahoo.com.au>
  6. *
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/fs.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/elevator.h>
  12. #include <linux/bio.h>
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/compiler.h>
  18. #include <linux/hash.h>
  19. #include <linux/rbtree.h>
  20. #include <linux/interrupt.h>
  21. #define REQ_SYNC 1
  22. #define REQ_ASYNC 0
  23. /*
  24. * See Documentation/block/as-iosched.txt
  25. */
  26. /*
  27. * max time before a read is submitted.
  28. */
  29. #define default_read_expire (HZ / 8)
  30. /*
  31. * ditto for writes, these limits are not hard, even
  32. * if the disk is capable of satisfying them.
  33. */
  34. #define default_write_expire (HZ / 4)
  35. /*
  36. * read_batch_expire describes how long we will allow a stream of reads to
  37. * persist before looking to see whether it is time to switch over to writes.
  38. */
  39. #define default_read_batch_expire (HZ / 2)
  40. /*
  41. * write_batch_expire describes how long we want a stream of writes to run for.
  42. * This is not a hard limit, but a target we set for the auto-tuning thingy.
  43. * See, the problem is: we can send a lot of writes to disk cache / TCQ in
  44. * a short amount of time...
  45. */
  46. #define default_write_batch_expire (HZ / 8)
  47. /*
  48. * max time we may wait to anticipate a read (default around 6ms)
  49. */
  50. #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
  51. /*
  52. * Keep track of up to 20ms thinktimes. We can go as big as we like here,
  53. * however huge values tend to interfere and not decay fast enough. A program
  54. * might be in a non-io phase of operation. Waiting on user input for example,
  55. * or doing a lengthy computation. A small penalty can be justified there, and
  56. * will still catch out those processes that constantly have large thinktimes.
  57. */
  58. #define MAX_THINKTIME (HZ/50UL)
  59. /* Bits in as_io_context.state */
  60. enum as_io_states {
  61. AS_TASK_RUNNING=0, /* Process has not exited */
  62. AS_TASK_IOSTARTED, /* Process has started some IO */
  63. AS_TASK_IORUNNING, /* Process has completed some IO */
  64. };
  65. enum anticipation_status {
  66. ANTIC_OFF=0, /* Not anticipating (normal operation) */
  67. ANTIC_WAIT_REQ, /* The last read has not yet completed */
  68. ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
  69. last read (which has completed) */
  70. ANTIC_FINISHED, /* Anticipating but have found a candidate
  71. * or timed out */
  72. };
  73. struct as_data {
  74. /*
  75. * run time data
  76. */
  77. struct request_queue *q; /* the "owner" queue */
  78. /*
  79. * requests (as_rq s) are present on both sort_list and fifo_list
  80. */
  81. struct rb_root sort_list[2];
  82. struct list_head fifo_list[2];
  83. struct as_rq *next_arq[2]; /* next in sort order */
  84. sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
  85. struct list_head *hash; /* request hash */
  86. unsigned long exit_prob; /* probability a task will exit while
  87. being waited on */
  88. unsigned long exit_no_coop; /* probablility an exited task will
  89. not be part of a later cooperating
  90. request */
  91. unsigned long new_ttime_total; /* mean thinktime on new proc */
  92. unsigned long new_ttime_mean;
  93. u64 new_seek_total; /* mean seek on new proc */
  94. sector_t new_seek_mean;
  95. unsigned long current_batch_expires;
  96. unsigned long last_check_fifo[2];
  97. int changed_batch; /* 1: waiting for old batch to end */
  98. int new_batch; /* 1: waiting on first read complete */
  99. int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */
  100. int write_batch_count; /* max # of reqs in a write batch */
  101. int current_write_count; /* how many requests left this batch */
  102. int write_batch_idled; /* has the write batch gone idle? */
  103. mempool_t *arq_pool;
  104. enum anticipation_status antic_status;
  105. unsigned long antic_start; /* jiffies: when it started */
  106. struct timer_list antic_timer; /* anticipatory scheduling timer */
  107. struct work_struct antic_work; /* Deferred unplugging */
  108. struct io_context *io_context; /* Identify the expected process */
  109. int ioc_finished; /* IO associated with io_context is finished */
  110. int nr_dispatched;
  111. /*
  112. * settings that change how the i/o scheduler behaves
  113. */
  114. unsigned long fifo_expire[2];
  115. unsigned long batch_expire[2];
  116. unsigned long antic_expire;
  117. };
  118. #define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
  119. /*
  120. * per-request data.
  121. */
  122. enum arq_state {
  123. AS_RQ_NEW=0, /* New - not referenced and not on any lists */
  124. AS_RQ_QUEUED, /* In the request queue. It belongs to the
  125. scheduler */
  126. AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
  127. driver now */
  128. AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
  129. AS_RQ_REMOVED,
  130. AS_RQ_MERGED,
  131. AS_RQ_POSTSCHED, /* when they shouldn't be */
  132. };
  133. struct as_rq {
  134. /*
  135. * rbtree index, key is the starting offset
  136. */
  137. struct rb_node rb_node;
  138. sector_t rb_key;
  139. struct request *request;
  140. struct io_context *io_context; /* The submitting task */
  141. /*
  142. * request hash, key is the ending offset (for back merge lookup)
  143. */
  144. struct list_head hash;
  145. unsigned int on_hash;
  146. /*
  147. * expire fifo
  148. */
  149. struct list_head fifo;
  150. unsigned long expires;
  151. unsigned int is_sync;
  152. enum arq_state state;
  153. };
  154. #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
  155. static kmem_cache_t *arq_pool;
  156. /*
  157. * IO Context helper functions
  158. */
  159. /* Called to deallocate the as_io_context */
  160. static void free_as_io_context(struct as_io_context *aic)
  161. {
  162. kfree(aic);
  163. }
  164. /* Called when the task exits */
  165. static void exit_as_io_context(struct as_io_context *aic)
  166. {
  167. WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
  168. clear_bit(AS_TASK_RUNNING, &aic->state);
  169. }
  170. static struct as_io_context *alloc_as_io_context(void)
  171. {
  172. struct as_io_context *ret;
  173. ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
  174. if (ret) {
  175. ret->dtor = free_as_io_context;
  176. ret->exit = exit_as_io_context;
  177. ret->state = 1 << AS_TASK_RUNNING;
  178. atomic_set(&ret->nr_queued, 0);
  179. atomic_set(&ret->nr_dispatched, 0);
  180. spin_lock_init(&ret->lock);
  181. ret->ttime_total = 0;
  182. ret->ttime_samples = 0;
  183. ret->ttime_mean = 0;
  184. ret->seek_total = 0;
  185. ret->seek_samples = 0;
  186. ret->seek_mean = 0;
  187. }
  188. return ret;
  189. }
  190. /*
  191. * If the current task has no AS IO context then create one and initialise it.
  192. * Then take a ref on the task's io context and return it.
  193. */
  194. static struct io_context *as_get_io_context(void)
  195. {
  196. struct io_context *ioc = get_io_context(GFP_ATOMIC);
  197. if (ioc && !ioc->aic) {
  198. ioc->aic = alloc_as_io_context();
  199. if (!ioc->aic) {
  200. put_io_context(ioc);
  201. ioc = NULL;
  202. }
  203. }
  204. return ioc;
  205. }
  206. static void as_put_io_context(struct as_rq *arq)
  207. {
  208. struct as_io_context *aic;
  209. if (unlikely(!arq->io_context))
  210. return;
  211. aic = arq->io_context->aic;
  212. if (arq->is_sync == REQ_SYNC && aic) {
  213. spin_lock(&aic->lock);
  214. set_bit(AS_TASK_IORUNNING, &aic->state);
  215. aic->last_end_request = jiffies;
  216. spin_unlock(&aic->lock);
  217. }
  218. put_io_context(arq->io_context);
  219. }
  220. /*
  221. * the back merge hash support functions
  222. */
  223. static const int as_hash_shift = 6;
  224. #define AS_HASH_BLOCK(sec) ((sec) >> 3)
  225. #define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
  226. #define AS_HASH_ENTRIES (1 << as_hash_shift)
  227. #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
  228. #define list_entry_hash(ptr) list_entry((ptr), struct as_rq, hash)
  229. static inline void __as_del_arq_hash(struct as_rq *arq)
  230. {
  231. arq->on_hash = 0;
  232. list_del_init(&arq->hash);
  233. }
  234. static inline void as_del_arq_hash(struct as_rq *arq)
  235. {
  236. if (arq->on_hash)
  237. __as_del_arq_hash(arq);
  238. }
  239. static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
  240. {
  241. struct request *rq = arq->request;
  242. BUG_ON(arq->on_hash);
  243. arq->on_hash = 1;
  244. list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
  245. }
  246. /*
  247. * move hot entry to front of chain
  248. */
  249. static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
  250. {
  251. struct request *rq = arq->request;
  252. struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
  253. if (!arq->on_hash) {
  254. WARN_ON(1);
  255. return;
  256. }
  257. if (arq->hash.prev != head) {
  258. list_del(&arq->hash);
  259. list_add(&arq->hash, head);
  260. }
  261. }
  262. static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
  263. {
  264. struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
  265. struct list_head *entry, *next = hash_list->next;
  266. while ((entry = next) != hash_list) {
  267. struct as_rq *arq = list_entry_hash(entry);
  268. struct request *__rq = arq->request;
  269. next = entry->next;
  270. BUG_ON(!arq->on_hash);
  271. if (!rq_mergeable(__rq)) {
  272. as_del_arq_hash(arq);
  273. continue;
  274. }
  275. if (rq_hash_key(__rq) == offset)
  276. return __rq;
  277. }
  278. return NULL;
  279. }
  280. /*
  281. * rb tree support functions
  282. */
  283. #define RB_NONE (2)
  284. #define RB_EMPTY(root) ((root)->rb_node == NULL)
  285. #define ON_RB(node) ((node)->rb_color != RB_NONE)
  286. #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
  287. #define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node)
  288. #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
  289. #define rq_rb_key(rq) (rq)->sector
  290. /*
  291. * as_find_first_arq finds the first (lowest sector numbered) request
  292. * for the specified data_dir. Used to sweep back to the start of the disk
  293. * (1-way elevator) after we process the last (highest sector) request.
  294. */
  295. static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
  296. {
  297. struct rb_node *n = ad->sort_list[data_dir].rb_node;
  298. if (n == NULL)
  299. return NULL;
  300. for (;;) {
  301. if (n->rb_left == NULL)
  302. return rb_entry_arq(n);
  303. n = n->rb_left;
  304. }
  305. }
  306. /*
  307. * Add the request to the rb tree if it is unique. If there is an alias (an
  308. * existing request against the same sector), which can happen when using
  309. * direct IO, then return the alias.
  310. */
  311. static struct as_rq *as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
  312. {
  313. struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
  314. struct rb_node *parent = NULL;
  315. struct as_rq *__arq;
  316. struct request *rq = arq->request;
  317. arq->rb_key = rq_rb_key(rq);
  318. while (*p) {
  319. parent = *p;
  320. __arq = rb_entry_arq(parent);
  321. if (arq->rb_key < __arq->rb_key)
  322. p = &(*p)->rb_left;
  323. else if (arq->rb_key > __arq->rb_key)
  324. p = &(*p)->rb_right;
  325. else
  326. return __arq;
  327. }
  328. rb_link_node(&arq->rb_node, parent, p);
  329. rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
  330. return NULL;
  331. }
  332. static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
  333. {
  334. if (!ON_RB(&arq->rb_node)) {
  335. WARN_ON(1);
  336. return;
  337. }
  338. rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
  339. RB_CLEAR(&arq->rb_node);
  340. }
  341. static struct request *
  342. as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
  343. {
  344. struct rb_node *n = ad->sort_list[data_dir].rb_node;
  345. struct as_rq *arq;
  346. while (n) {
  347. arq = rb_entry_arq(n);
  348. if (sector < arq->rb_key)
  349. n = n->rb_left;
  350. else if (sector > arq->rb_key)
  351. n = n->rb_right;
  352. else
  353. return arq->request;
  354. }
  355. return NULL;
  356. }
  357. /*
  358. * IO Scheduler proper
  359. */
  360. #define MAXBACK (1024 * 1024) /*
  361. * Maximum distance the disk will go backward
  362. * for a request.
  363. */
  364. #define BACK_PENALTY 2
  365. /*
  366. * as_choose_req selects the preferred one of two requests of the same data_dir
  367. * ignoring time - eg. timeouts, which is the job of as_dispatch_request
  368. */
  369. static struct as_rq *
  370. as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
  371. {
  372. int data_dir;
  373. sector_t last, s1, s2, d1, d2;
  374. int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
  375. const sector_t maxback = MAXBACK;
  376. if (arq1 == NULL || arq1 == arq2)
  377. return arq2;
  378. if (arq2 == NULL)
  379. return arq1;
  380. data_dir = arq1->is_sync;
  381. last = ad->last_sector[data_dir];
  382. s1 = arq1->request->sector;
  383. s2 = arq2->request->sector;
  384. BUG_ON(data_dir != arq2->is_sync);
  385. /*
  386. * Strict one way elevator _except_ in the case where we allow
  387. * short backward seeks which are biased as twice the cost of a
  388. * similar forward seek.
  389. */
  390. if (s1 >= last)
  391. d1 = s1 - last;
  392. else if (s1+maxback >= last)
  393. d1 = (last - s1)*BACK_PENALTY;
  394. else {
  395. r1_wrap = 1;
  396. d1 = 0; /* shut up, gcc */
  397. }
  398. if (s2 >= last)
  399. d2 = s2 - last;
  400. else if (s2+maxback >= last)
  401. d2 = (last - s2)*BACK_PENALTY;
  402. else {
  403. r2_wrap = 1;
  404. d2 = 0;
  405. }
  406. /* Found required data */
  407. if (!r1_wrap && r2_wrap)
  408. return arq1;
  409. else if (!r2_wrap && r1_wrap)
  410. return arq2;
  411. else if (r1_wrap && r2_wrap) {
  412. /* both behind the head */
  413. if (s1 <= s2)
  414. return arq1;
  415. else
  416. return arq2;
  417. }
  418. /* Both requests in front of the head */
  419. if (d1 < d2)
  420. return arq1;
  421. else if (d2 < d1)
  422. return arq2;
  423. else {
  424. if (s1 >= s2)
  425. return arq1;
  426. else
  427. return arq2;
  428. }
  429. }
  430. /*
  431. * as_find_next_arq finds the next request after @prev in elevator order.
  432. * this with as_choose_req form the basis for how the scheduler chooses
  433. * what request to process next. Anticipation works on top of this.
  434. */
  435. static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
  436. {
  437. const int data_dir = last->is_sync;
  438. struct as_rq *ret;
  439. struct rb_node *rbnext = rb_next(&last->rb_node);
  440. struct rb_node *rbprev = rb_prev(&last->rb_node);
  441. struct as_rq *arq_next, *arq_prev;
  442. BUG_ON(!ON_RB(&last->rb_node));
  443. if (rbprev)
  444. arq_prev = rb_entry_arq(rbprev);
  445. else
  446. arq_prev = NULL;
  447. if (rbnext)
  448. arq_next = rb_entry_arq(rbnext);
  449. else {
  450. arq_next = as_find_first_arq(ad, data_dir);
  451. if (arq_next == last)
  452. arq_next = NULL;
  453. }
  454. ret = as_choose_req(ad, arq_next, arq_prev);
  455. return ret;
  456. }
  457. /*
  458. * anticipatory scheduling functions follow
  459. */
  460. /*
  461. * as_antic_expired tells us when we have anticipated too long.
  462. * The funny "absolute difference" math on the elapsed time is to handle
  463. * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
  464. */
  465. static int as_antic_expired(struct as_data *ad)
  466. {
  467. long delta_jif;
  468. delta_jif = jiffies - ad->antic_start;
  469. if (unlikely(delta_jif < 0))
  470. delta_jif = -delta_jif;
  471. if (delta_jif < ad->antic_expire)
  472. return 0;
  473. return 1;
  474. }
  475. /*
  476. * as_antic_waitnext starts anticipating that a nice request will soon be
  477. * submitted. See also as_antic_waitreq
  478. */
  479. static void as_antic_waitnext(struct as_data *ad)
  480. {
  481. unsigned long timeout;
  482. BUG_ON(ad->antic_status != ANTIC_OFF
  483. && ad->antic_status != ANTIC_WAIT_REQ);
  484. timeout = ad->antic_start + ad->antic_expire;
  485. mod_timer(&ad->antic_timer, timeout);
  486. ad->antic_status = ANTIC_WAIT_NEXT;
  487. }
  488. /*
  489. * as_antic_waitreq starts anticipating. We don't start timing the anticipation
  490. * until the request that we're anticipating on has finished. This means we
  491. * are timing from when the candidate process wakes up hopefully.
  492. */
  493. static void as_antic_waitreq(struct as_data *ad)
  494. {
  495. BUG_ON(ad->antic_status == ANTIC_FINISHED);
  496. if (ad->antic_status == ANTIC_OFF) {
  497. if (!ad->io_context || ad->ioc_finished)
  498. as_antic_waitnext(ad);
  499. else
  500. ad->antic_status = ANTIC_WAIT_REQ;
  501. }
  502. }
  503. /*
  504. * This is called directly by the functions in this file to stop anticipation.
  505. * We kill the timer and schedule a call to the request_fn asap.
  506. */
  507. static void as_antic_stop(struct as_data *ad)
  508. {
  509. int status = ad->antic_status;
  510. if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
  511. if (status == ANTIC_WAIT_NEXT)
  512. del_timer(&ad->antic_timer);
  513. ad->antic_status = ANTIC_FINISHED;
  514. /* see as_work_handler */
  515. kblockd_schedule_work(&ad->antic_work);
  516. }
  517. }
  518. /*
  519. * as_antic_timeout is the timer function set by as_antic_waitnext.
  520. */
  521. static void as_antic_timeout(unsigned long data)
  522. {
  523. struct request_queue *q = (struct request_queue *)data;
  524. struct as_data *ad = q->elevator->elevator_data;
  525. unsigned long flags;
  526. spin_lock_irqsave(q->queue_lock, flags);
  527. if (ad->antic_status == ANTIC_WAIT_REQ
  528. || ad->antic_status == ANTIC_WAIT_NEXT) {
  529. struct as_io_context *aic = ad->io_context->aic;
  530. ad->antic_status = ANTIC_FINISHED;
  531. kblockd_schedule_work(&ad->antic_work);
  532. if (aic->ttime_samples == 0) {
  533. /* process anticipated on has exited or timed out*/
  534. ad->exit_prob = (7*ad->exit_prob + 256)/8;
  535. }
  536. if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
  537. /* process not "saved" by a cooperating request */
  538. ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
  539. }
  540. }
  541. spin_unlock_irqrestore(q->queue_lock, flags);
  542. }
  543. static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
  544. unsigned long ttime)
  545. {
  546. /* fixed point: 1.0 == 1<<8 */
  547. if (aic->ttime_samples == 0) {
  548. ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
  549. ad->new_ttime_mean = ad->new_ttime_total / 256;
  550. ad->exit_prob = (7*ad->exit_prob)/8;
  551. }
  552. aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
  553. aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
  554. aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
  555. }
  556. static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
  557. sector_t sdist)
  558. {
  559. u64 total;
  560. if (aic->seek_samples == 0) {
  561. ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
  562. ad->new_seek_mean = ad->new_seek_total / 256;
  563. }
  564. /*
  565. * Don't allow the seek distance to get too large from the
  566. * odd fragment, pagein, etc
  567. */
  568. if (aic->seek_samples <= 60) /* second&third seek */
  569. sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
  570. else
  571. sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
  572. aic->seek_samples = (7*aic->seek_samples + 256) / 8;
  573. aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
  574. total = aic->seek_total + (aic->seek_samples/2);
  575. do_div(total, aic->seek_samples);
  576. aic->seek_mean = (sector_t)total;
  577. }
  578. /*
  579. * as_update_iohist keeps a decaying histogram of IO thinktimes, and
  580. * updates @aic->ttime_mean based on that. It is called when a new
  581. * request is queued.
  582. */
  583. static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
  584. struct request *rq)
  585. {
  586. struct as_rq *arq = RQ_DATA(rq);
  587. int data_dir = arq->is_sync;
  588. unsigned long thinktime = 0;
  589. sector_t seek_dist;
  590. if (aic == NULL)
  591. return;
  592. if (data_dir == REQ_SYNC) {
  593. unsigned long in_flight = atomic_read(&aic->nr_queued)
  594. + atomic_read(&aic->nr_dispatched);
  595. spin_lock(&aic->lock);
  596. if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
  597. test_bit(AS_TASK_IOSTARTED, &aic->state)) {
  598. /* Calculate read -> read thinktime */
  599. if (test_bit(AS_TASK_IORUNNING, &aic->state)
  600. && in_flight == 0) {
  601. thinktime = jiffies - aic->last_end_request;
  602. thinktime = min(thinktime, MAX_THINKTIME-1);
  603. }
  604. as_update_thinktime(ad, aic, thinktime);
  605. /* Calculate read -> read seek distance */
  606. if (aic->last_request_pos < rq->sector)
  607. seek_dist = rq->sector - aic->last_request_pos;
  608. else
  609. seek_dist = aic->last_request_pos - rq->sector;
  610. as_update_seekdist(ad, aic, seek_dist);
  611. }
  612. aic->last_request_pos = rq->sector + rq->nr_sectors;
  613. set_bit(AS_TASK_IOSTARTED, &aic->state);
  614. spin_unlock(&aic->lock);
  615. }
  616. }
  617. /*
  618. * as_close_req decides if one request is considered "close" to the
  619. * previous one issued.
  620. */
  621. static int as_close_req(struct as_data *ad, struct as_io_context *aic,
  622. struct as_rq *arq)
  623. {
  624. unsigned long delay; /* milliseconds */
  625. sector_t last = ad->last_sector[ad->batch_data_dir];
  626. sector_t next = arq->request->sector;
  627. sector_t delta; /* acceptable close offset (in sectors) */
  628. sector_t s;
  629. if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
  630. delay = 0;
  631. else
  632. delay = ((jiffies - ad->antic_start) * 1000) / HZ;
  633. if (delay == 0)
  634. delta = 8192;
  635. else if (delay <= 20 && delay <= ad->antic_expire)
  636. delta = 8192 << delay;
  637. else
  638. return 1;
  639. if ((last <= next + (delta>>1)) && (next <= last + delta))
  640. return 1;
  641. if (last < next)
  642. s = next - last;
  643. else
  644. s = last - next;
  645. if (aic->seek_samples == 0) {
  646. /*
  647. * Process has just started IO. Use past statistics to
  648. * gauge success possibility
  649. */
  650. if (ad->new_seek_mean > s) {
  651. /* this request is better than what we're expecting */
  652. return 1;
  653. }
  654. } else {
  655. if (aic->seek_mean > s) {
  656. /* this request is better than what we're expecting */
  657. return 1;
  658. }
  659. }
  660. return 0;
  661. }
  662. /*
  663. * as_can_break_anticipation returns true if we have been anticipating this
  664. * request.
  665. *
  666. * It also returns true if the process against which we are anticipating
  667. * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
  668. * dispatch it ASAP, because we know that application will not be submitting
  669. * any new reads.
  670. *
  671. * If the task which has submitted the request has exited, break anticipation.
  672. *
  673. * If this task has queued some other IO, do not enter enticipation.
  674. */
  675. static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
  676. {
  677. struct io_context *ioc;
  678. struct as_io_context *aic;
  679. ioc = ad->io_context;
  680. BUG_ON(!ioc);
  681. if (arq && ioc == arq->io_context) {
  682. /* request from same process */
  683. return 1;
  684. }
  685. if (ad->ioc_finished && as_antic_expired(ad)) {
  686. /*
  687. * In this situation status should really be FINISHED,
  688. * however the timer hasn't had the chance to run yet.
  689. */
  690. return 1;
  691. }
  692. aic = ioc->aic;
  693. if (!aic)
  694. return 0;
  695. if (atomic_read(&aic->nr_queued) > 0) {
  696. /* process has more requests queued */
  697. return 1;
  698. }
  699. if (atomic_read(&aic->nr_dispatched) > 0) {
  700. /* process has more requests dispatched */
  701. return 1;
  702. }
  703. if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, aic, arq)) {
  704. /*
  705. * Found a close request that is not one of ours.
  706. *
  707. * This makes close requests from another process update
  708. * our IO history. Is generally useful when there are
  709. * two or more cooperating processes working in the same
  710. * area.
  711. */
  712. if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
  713. if (aic->ttime_samples == 0)
  714. ad->exit_prob = (7*ad->exit_prob + 256)/8;
  715. ad->exit_no_coop = (7*ad->exit_no_coop)/8;
  716. }
  717. as_update_iohist(ad, aic, arq->request);
  718. return 1;
  719. }
  720. if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
  721. /* process anticipated on has exited */
  722. if (aic->ttime_samples == 0)
  723. ad->exit_prob = (7*ad->exit_prob + 256)/8;
  724. if (ad->exit_no_coop > 128)
  725. return 1;
  726. }
  727. if (aic->ttime_samples == 0) {
  728. if (ad->new_ttime_mean > ad->antic_expire)
  729. return 1;
  730. if (ad->exit_prob * ad->exit_no_coop > 128*256)
  731. return 1;
  732. } else if (aic->ttime_mean > ad->antic_expire) {
  733. /* the process thinks too much between requests */
  734. return 1;
  735. }
  736. return 0;
  737. }
  738. /*
  739. * as_can_anticipate indicates weather we should either run arq
  740. * or keep anticipating a better request.
  741. */
  742. static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
  743. {
  744. if (!ad->io_context)
  745. /*
  746. * Last request submitted was a write
  747. */
  748. return 0;
  749. if (ad->antic_status == ANTIC_FINISHED)
  750. /*
  751. * Don't restart if we have just finished. Run the next request
  752. */
  753. return 0;
  754. if (as_can_break_anticipation(ad, arq))
  755. /*
  756. * This request is a good candidate. Don't keep anticipating,
  757. * run it.
  758. */
  759. return 0;
  760. /*
  761. * OK from here, we haven't finished, and don't have a decent request!
  762. * Status is either ANTIC_OFF so start waiting,
  763. * ANTIC_WAIT_REQ so continue waiting for request to finish
  764. * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
  765. */
  766. return 1;
  767. }
  768. /*
  769. * as_update_arq must be called whenever a request (arq) is added to
  770. * the sort_list. This function keeps caches up to date, and checks if the
  771. * request might be one we are "anticipating"
  772. */
  773. static void as_update_arq(struct as_data *ad, struct as_rq *arq)
  774. {
  775. const int data_dir = arq->is_sync;
  776. /* keep the next_arq cache up to date */
  777. ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
  778. /*
  779. * have we been anticipating this request?
  780. * or does it come from the same process as the one we are anticipating
  781. * for?
  782. */
  783. if (ad->antic_status == ANTIC_WAIT_REQ
  784. || ad->antic_status == ANTIC_WAIT_NEXT) {
  785. if (as_can_break_anticipation(ad, arq))
  786. as_antic_stop(ad);
  787. }
  788. }
  789. /*
  790. * Gathers timings and resizes the write batch automatically
  791. */
  792. static void update_write_batch(struct as_data *ad)
  793. {
  794. unsigned long batch = ad->batch_expire[REQ_ASYNC];
  795. long write_time;
  796. write_time = (jiffies - ad->current_batch_expires) + batch;
  797. if (write_time < 0)
  798. write_time = 0;
  799. if (write_time > batch && !ad->write_batch_idled) {
  800. if (write_time > batch * 3)
  801. ad->write_batch_count /= 2;
  802. else
  803. ad->write_batch_count--;
  804. } else if (write_time < batch && ad->current_write_count == 0) {
  805. if (batch > write_time * 3)
  806. ad->write_batch_count *= 2;
  807. else
  808. ad->write_batch_count++;
  809. }
  810. if (ad->write_batch_count < 1)
  811. ad->write_batch_count = 1;
  812. }
  813. /*
  814. * as_completed_request is to be called when a request has completed and
  815. * returned something to the requesting process, be it an error or data.
  816. */
  817. static void as_completed_request(request_queue_t *q, struct request *rq)
  818. {
  819. struct as_data *ad = q->elevator->elevator_data;
  820. struct as_rq *arq = RQ_DATA(rq);
  821. WARN_ON(!list_empty(&rq->queuelist));
  822. if (arq->state != AS_RQ_REMOVED) {
  823. printk("arq->state %d\n", arq->state);
  824. WARN_ON(1);
  825. goto out;
  826. }
  827. if (ad->changed_batch && ad->nr_dispatched == 1) {
  828. kblockd_schedule_work(&ad->antic_work);
  829. ad->changed_batch = 0;
  830. if (ad->batch_data_dir == REQ_SYNC)
  831. ad->new_batch = 1;
  832. }
  833. WARN_ON(ad->nr_dispatched == 0);
  834. ad->nr_dispatched--;
  835. /*
  836. * Start counting the batch from when a request of that direction is
  837. * actually serviced. This should help devices with big TCQ windows
  838. * and writeback caches
  839. */
  840. if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
  841. update_write_batch(ad);
  842. ad->current_batch_expires = jiffies +
  843. ad->batch_expire[REQ_SYNC];
  844. ad->new_batch = 0;
  845. }
  846. if (ad->io_context == arq->io_context && ad->io_context) {
  847. ad->antic_start = jiffies;
  848. ad->ioc_finished = 1;
  849. if (ad->antic_status == ANTIC_WAIT_REQ) {
  850. /*
  851. * We were waiting on this request, now anticipate
  852. * the next one
  853. */
  854. as_antic_waitnext(ad);
  855. }
  856. }
  857. as_put_io_context(arq);
  858. out:
  859. arq->state = AS_RQ_POSTSCHED;
  860. }
  861. /*
  862. * as_remove_queued_request removes a request from the pre dispatch queue
  863. * without updating refcounts. It is expected the caller will drop the
  864. * reference unless it replaces the request at somepart of the elevator
  865. * (ie. the dispatch queue)
  866. */
  867. static void as_remove_queued_request(request_queue_t *q, struct request *rq)
  868. {
  869. struct as_rq *arq = RQ_DATA(rq);
  870. const int data_dir = arq->is_sync;
  871. struct as_data *ad = q->elevator->elevator_data;
  872. WARN_ON(arq->state != AS_RQ_QUEUED);
  873. if (arq->io_context && arq->io_context->aic) {
  874. BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
  875. atomic_dec(&arq->io_context->aic->nr_queued);
  876. }
  877. /*
  878. * Update the "next_arq" cache if we are about to remove its
  879. * entry
  880. */
  881. if (ad->next_arq[data_dir] == arq)
  882. ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
  883. list_del_init(&arq->fifo);
  884. as_del_arq_hash(arq);
  885. as_del_arq_rb(ad, arq);
  886. }
  887. /*
  888. * as_fifo_expired returns 0 if there are no expired reads on the fifo,
  889. * 1 otherwise. It is ratelimited so that we only perform the check once per
  890. * `fifo_expire' interval. Otherwise a large number of expired requests
  891. * would create a hopeless seekstorm.
  892. *
  893. * See as_antic_expired comment.
  894. */
  895. static int as_fifo_expired(struct as_data *ad, int adir)
  896. {
  897. struct as_rq *arq;
  898. long delta_jif;
  899. delta_jif = jiffies - ad->last_check_fifo[adir];
  900. if (unlikely(delta_jif < 0))
  901. delta_jif = -delta_jif;
  902. if (delta_jif < ad->fifo_expire[adir])
  903. return 0;
  904. ad->last_check_fifo[adir] = jiffies;
  905. if (list_empty(&ad->fifo_list[adir]))
  906. return 0;
  907. arq = list_entry_fifo(ad->fifo_list[adir].next);
  908. return time_after(jiffies, arq->expires);
  909. }
  910. /*
  911. * as_batch_expired returns true if the current batch has expired. A batch
  912. * is a set of reads or a set of writes.
  913. */
  914. static inline int as_batch_expired(struct as_data *ad)
  915. {
  916. if (ad->changed_batch || ad->new_batch)
  917. return 0;
  918. if (ad->batch_data_dir == REQ_SYNC)
  919. /* TODO! add a check so a complete fifo gets written? */
  920. return time_after(jiffies, ad->current_batch_expires);
  921. return time_after(jiffies, ad->current_batch_expires)
  922. || ad->current_write_count == 0;
  923. }
  924. /*
  925. * move an entry to dispatch queue
  926. */
  927. static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
  928. {
  929. struct request *rq = arq->request;
  930. const int data_dir = arq->is_sync;
  931. BUG_ON(!ON_RB(&arq->rb_node));
  932. as_antic_stop(ad);
  933. ad->antic_status = ANTIC_OFF;
  934. /*
  935. * This has to be set in order to be correctly updated by
  936. * as_find_next_arq
  937. */
  938. ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
  939. if (data_dir == REQ_SYNC) {
  940. /* In case we have to anticipate after this */
  941. copy_io_context(&ad->io_context, &arq->io_context);
  942. } else {
  943. if (ad->io_context) {
  944. put_io_context(ad->io_context);
  945. ad->io_context = NULL;
  946. }
  947. if (ad->current_write_count != 0)
  948. ad->current_write_count--;
  949. }
  950. ad->ioc_finished = 0;
  951. ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
  952. /*
  953. * take it off the sort and fifo list, add to dispatch queue
  954. */
  955. while (!list_empty(&rq->queuelist)) {
  956. struct request *__rq = list_entry_rq(rq->queuelist.next);
  957. struct as_rq *__arq = RQ_DATA(__rq);
  958. list_del(&__rq->queuelist);
  959. elv_dispatch_add_tail(ad->q, __rq);
  960. if (__arq->io_context && __arq->io_context->aic)
  961. atomic_inc(&__arq->io_context->aic->nr_dispatched);
  962. WARN_ON(__arq->state != AS_RQ_QUEUED);
  963. __arq->state = AS_RQ_DISPATCHED;
  964. ad->nr_dispatched++;
  965. }
  966. as_remove_queued_request(ad->q, rq);
  967. WARN_ON(arq->state != AS_RQ_QUEUED);
  968. elv_dispatch_sort(ad->q, rq);
  969. arq->state = AS_RQ_DISPATCHED;
  970. if (arq->io_context && arq->io_context->aic)
  971. atomic_inc(&arq->io_context->aic->nr_dispatched);
  972. ad->nr_dispatched++;
  973. }
  974. /*
  975. * as_dispatch_request selects the best request according to
  976. * read/write expire, batch expire, etc, and moves it to the dispatch
  977. * queue. Returns 1 if a request was found, 0 otherwise.
  978. */
  979. static int as_dispatch_request(request_queue_t *q, int force)
  980. {
  981. struct as_data *ad = q->elevator->elevator_data;
  982. struct as_rq *arq;
  983. const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
  984. const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
  985. if (unlikely(force)) {
  986. /*
  987. * Forced dispatch, accounting is useless. Reset
  988. * accounting states and dump fifo_lists. Note that
  989. * batch_data_dir is reset to REQ_SYNC to avoid
  990. * screwing write batch accounting as write batch
  991. * accounting occurs on W->R transition.
  992. */
  993. int dispatched = 0;
  994. ad->batch_data_dir = REQ_SYNC;
  995. ad->changed_batch = 0;
  996. ad->new_batch = 0;
  997. while (ad->next_arq[REQ_SYNC]) {
  998. as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
  999. dispatched++;
  1000. }
  1001. ad->last_check_fifo[REQ_SYNC] = jiffies;
  1002. while (ad->next_arq[REQ_ASYNC]) {
  1003. as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
  1004. dispatched++;
  1005. }
  1006. ad->last_check_fifo[REQ_ASYNC] = jiffies;
  1007. return dispatched;
  1008. }
  1009. /* Signal that the write batch was uncontended, so we can't time it */
  1010. if (ad->batch_data_dir == REQ_ASYNC && !reads) {
  1011. if (ad->current_write_count == 0 || !writes)
  1012. ad->write_batch_idled = 1;
  1013. }
  1014. if (!(reads || writes)
  1015. || ad->antic_status == ANTIC_WAIT_REQ
  1016. || ad->antic_status == ANTIC_WAIT_NEXT
  1017. || ad->changed_batch)
  1018. return 0;
  1019. if (!(reads && writes && as_batch_expired(ad))) {
  1020. /*
  1021. * batch is still running or no reads or no writes
  1022. */
  1023. arq = ad->next_arq[ad->batch_data_dir];
  1024. if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
  1025. if (as_fifo_expired(ad, REQ_SYNC))
  1026. goto fifo_expired;
  1027. if (as_can_anticipate(ad, arq)) {
  1028. as_antic_waitreq(ad);
  1029. return 0;
  1030. }
  1031. }
  1032. if (arq) {
  1033. /* we have a "next request" */
  1034. if (reads && !writes)
  1035. ad->current_batch_expires =
  1036. jiffies + ad->batch_expire[REQ_SYNC];
  1037. goto dispatch_request;
  1038. }
  1039. }
  1040. /*
  1041. * at this point we are not running a batch. select the appropriate
  1042. * data direction (read / write)
  1043. */
  1044. if (reads) {
  1045. BUG_ON(RB_EMPTY(&ad->sort_list[REQ_SYNC]));
  1046. if (writes && ad->batch_data_dir == REQ_SYNC)
  1047. /*
  1048. * Last batch was a read, switch to writes
  1049. */
  1050. goto dispatch_writes;
  1051. if (ad->batch_data_dir == REQ_ASYNC) {
  1052. WARN_ON(ad->new_batch);
  1053. ad->changed_batch = 1;
  1054. }
  1055. ad->batch_data_dir = REQ_SYNC;
  1056. arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
  1057. ad->last_check_fifo[ad->batch_data_dir] = jiffies;
  1058. goto dispatch_request;
  1059. }
  1060. /*
  1061. * the last batch was a read
  1062. */
  1063. if (writes) {
  1064. dispatch_writes:
  1065. BUG_ON(RB_EMPTY(&ad->sort_list[REQ_ASYNC]));
  1066. if (ad->batch_data_dir == REQ_SYNC) {
  1067. ad->changed_batch = 1;
  1068. /*
  1069. * new_batch might be 1 when the queue runs out of
  1070. * reads. A subsequent submission of a write might
  1071. * cause a change of batch before the read is finished.
  1072. */
  1073. ad->new_batch = 0;
  1074. }
  1075. ad->batch_data_dir = REQ_ASYNC;
  1076. ad->current_write_count = ad->write_batch_count;
  1077. ad->write_batch_idled = 0;
  1078. arq = ad->next_arq[ad->batch_data_dir];
  1079. goto dispatch_request;
  1080. }
  1081. BUG();
  1082. return 0;
  1083. dispatch_request:
  1084. /*
  1085. * If a request has expired, service it.
  1086. */
  1087. if (as_fifo_expired(ad, ad->batch_data_dir)) {
  1088. fifo_expired:
  1089. arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
  1090. BUG_ON(arq == NULL);
  1091. }
  1092. if (ad->changed_batch) {
  1093. WARN_ON(ad->new_batch);
  1094. if (ad->nr_dispatched)
  1095. return 0;
  1096. if (ad->batch_data_dir == REQ_ASYNC)
  1097. ad->current_batch_expires = jiffies +
  1098. ad->batch_expire[REQ_ASYNC];
  1099. else
  1100. ad->new_batch = 1;
  1101. ad->changed_batch = 0;
  1102. }
  1103. /*
  1104. * arq is the selected appropriate request.
  1105. */
  1106. as_move_to_dispatch(ad, arq);
  1107. return 1;
  1108. }
  1109. /*
  1110. * Add arq to a list behind alias
  1111. */
  1112. static inline void
  1113. as_add_aliased_request(struct as_data *ad, struct as_rq *arq,
  1114. struct as_rq *alias)
  1115. {
  1116. struct request *req = arq->request;
  1117. struct list_head *insert = alias->request->queuelist.prev;
  1118. /*
  1119. * Transfer list of aliases
  1120. */
  1121. while (!list_empty(&req->queuelist)) {
  1122. struct request *__rq = list_entry_rq(req->queuelist.next);
  1123. struct as_rq *__arq = RQ_DATA(__rq);
  1124. list_move_tail(&__rq->queuelist, &alias->request->queuelist);
  1125. WARN_ON(__arq->state != AS_RQ_QUEUED);
  1126. }
  1127. /*
  1128. * Another request with the same start sector on the rbtree.
  1129. * Link this request to that sector. They are untangled in
  1130. * as_move_to_dispatch
  1131. */
  1132. list_add(&arq->request->queuelist, insert);
  1133. /*
  1134. * Don't want to have to handle merges.
  1135. */
  1136. as_del_arq_hash(arq);
  1137. arq->request->flags |= REQ_NOMERGE;
  1138. }
  1139. /*
  1140. * add arq to rbtree and fifo
  1141. */
  1142. static void as_add_request(request_queue_t *q, struct request *rq)
  1143. {
  1144. struct as_data *ad = q->elevator->elevator_data;
  1145. struct as_rq *arq = RQ_DATA(rq);
  1146. struct as_rq *alias;
  1147. int data_dir;
  1148. arq->state = AS_RQ_NEW;
  1149. if (rq_data_dir(arq->request) == READ
  1150. || current->flags&PF_SYNCWRITE)
  1151. arq->is_sync = 1;
  1152. else
  1153. arq->is_sync = 0;
  1154. data_dir = arq->is_sync;
  1155. arq->io_context = as_get_io_context();
  1156. if (arq->io_context) {
  1157. as_update_iohist(ad, arq->io_context->aic, arq->request);
  1158. atomic_inc(&arq->io_context->aic->nr_queued);
  1159. }
  1160. alias = as_add_arq_rb(ad, arq);
  1161. if (!alias) {
  1162. /*
  1163. * set expire time (only used for reads) and add to fifo list
  1164. */
  1165. arq->expires = jiffies + ad->fifo_expire[data_dir];
  1166. list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
  1167. if (rq_mergeable(arq->request))
  1168. as_add_arq_hash(ad, arq);
  1169. as_update_arq(ad, arq); /* keep state machine up to date */
  1170. } else {
  1171. as_add_aliased_request(ad, arq, alias);
  1172. /*
  1173. * have we been anticipating this request?
  1174. * or does it come from the same process as the one we are
  1175. * anticipating for?
  1176. */
  1177. if (ad->antic_status == ANTIC_WAIT_REQ
  1178. || ad->antic_status == ANTIC_WAIT_NEXT) {
  1179. if (as_can_break_anticipation(ad, arq))
  1180. as_antic_stop(ad);
  1181. }
  1182. }
  1183. arq->state = AS_RQ_QUEUED;
  1184. }
  1185. static void as_activate_request(request_queue_t *q, struct request *rq)
  1186. {
  1187. struct as_rq *arq = RQ_DATA(rq);
  1188. WARN_ON(arq->state != AS_RQ_DISPATCHED);
  1189. arq->state = AS_RQ_REMOVED;
  1190. if (arq->io_context && arq->io_context->aic)
  1191. atomic_dec(&arq->io_context->aic->nr_dispatched);
  1192. }
  1193. static void as_deactivate_request(request_queue_t *q, struct request *rq)
  1194. {
  1195. struct as_rq *arq = RQ_DATA(rq);
  1196. WARN_ON(arq->state != AS_RQ_REMOVED);
  1197. arq->state = AS_RQ_DISPATCHED;
  1198. if (arq->io_context && arq->io_context->aic)
  1199. atomic_inc(&arq->io_context->aic->nr_dispatched);
  1200. }
  1201. /*
  1202. * as_queue_empty tells us if there are requests left in the device. It may
  1203. * not be the case that a driver can get the next request even if the queue
  1204. * is not empty - it is used in the block layer to check for plugging and
  1205. * merging opportunities
  1206. */
  1207. static int as_queue_empty(request_queue_t *q)
  1208. {
  1209. struct as_data *ad = q->elevator->elevator_data;
  1210. return list_empty(&ad->fifo_list[REQ_ASYNC])
  1211. && list_empty(&ad->fifo_list[REQ_SYNC]);
  1212. }
  1213. static struct request *as_former_request(request_queue_t *q,
  1214. struct request *rq)
  1215. {
  1216. struct as_rq *arq = RQ_DATA(rq);
  1217. struct rb_node *rbprev = rb_prev(&arq->rb_node);
  1218. struct request *ret = NULL;
  1219. if (rbprev)
  1220. ret = rb_entry_arq(rbprev)->request;
  1221. return ret;
  1222. }
  1223. static struct request *as_latter_request(request_queue_t *q,
  1224. struct request *rq)
  1225. {
  1226. struct as_rq *arq = RQ_DATA(rq);
  1227. struct rb_node *rbnext = rb_next(&arq->rb_node);
  1228. struct request *ret = NULL;
  1229. if (rbnext)
  1230. ret = rb_entry_arq(rbnext)->request;
  1231. return ret;
  1232. }
  1233. static int
  1234. as_merge(request_queue_t *q, struct request **req, struct bio *bio)
  1235. {
  1236. struct as_data *ad = q->elevator->elevator_data;
  1237. sector_t rb_key = bio->bi_sector + bio_sectors(bio);
  1238. struct request *__rq;
  1239. int ret;
  1240. /*
  1241. * see if the merge hash can satisfy a back merge
  1242. */
  1243. __rq = as_find_arq_hash(ad, bio->bi_sector);
  1244. if (__rq) {
  1245. BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
  1246. if (elv_rq_merge_ok(__rq, bio)) {
  1247. ret = ELEVATOR_BACK_MERGE;
  1248. goto out;
  1249. }
  1250. }
  1251. /*
  1252. * check for front merge
  1253. */
  1254. __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
  1255. if (__rq) {
  1256. BUG_ON(rb_key != rq_rb_key(__rq));
  1257. if (elv_rq_merge_ok(__rq, bio)) {
  1258. ret = ELEVATOR_FRONT_MERGE;
  1259. goto out;
  1260. }
  1261. }
  1262. return ELEVATOR_NO_MERGE;
  1263. out:
  1264. if (ret) {
  1265. if (rq_mergeable(__rq))
  1266. as_hot_arq_hash(ad, RQ_DATA(__rq));
  1267. }
  1268. *req = __rq;
  1269. return ret;
  1270. }
  1271. static void as_merged_request(request_queue_t *q, struct request *req)
  1272. {
  1273. struct as_data *ad = q->elevator->elevator_data;
  1274. struct as_rq *arq = RQ_DATA(req);
  1275. /*
  1276. * hash always needs to be repositioned, key is end sector
  1277. */
  1278. as_del_arq_hash(arq);
  1279. as_add_arq_hash(ad, arq);
  1280. /*
  1281. * if the merge was a front merge, we need to reposition request
  1282. */
  1283. if (rq_rb_key(req) != arq->rb_key) {
  1284. struct as_rq *alias, *next_arq = NULL;
  1285. if (ad->next_arq[arq->is_sync] == arq)
  1286. next_arq = as_find_next_arq(ad, arq);
  1287. /*
  1288. * Note! We should really be moving any old aliased requests
  1289. * off this request and try to insert them into the rbtree. We
  1290. * currently don't bother. Ditto the next function.
  1291. */
  1292. as_del_arq_rb(ad, arq);
  1293. if ((alias = as_add_arq_rb(ad, arq))) {
  1294. list_del_init(&arq->fifo);
  1295. as_add_aliased_request(ad, arq, alias);
  1296. if (next_arq)
  1297. ad->next_arq[arq->is_sync] = next_arq;
  1298. }
  1299. /*
  1300. * Note! At this stage of this and the next function, our next
  1301. * request may not be optimal - eg the request may have "grown"
  1302. * behind the disk head. We currently don't bother adjusting.
  1303. */
  1304. }
  1305. }
  1306. static void as_merged_requests(request_queue_t *q, struct request *req,
  1307. struct request *next)
  1308. {
  1309. struct as_data *ad = q->elevator->elevator_data;
  1310. struct as_rq *arq = RQ_DATA(req);
  1311. struct as_rq *anext = RQ_DATA(next);
  1312. BUG_ON(!arq);
  1313. BUG_ON(!anext);
  1314. /*
  1315. * reposition arq (this is the merged request) in hash, and in rbtree
  1316. * in case of a front merge
  1317. */
  1318. as_del_arq_hash(arq);
  1319. as_add_arq_hash(ad, arq);
  1320. if (rq_rb_key(req) != arq->rb_key) {
  1321. struct as_rq *alias, *next_arq = NULL;
  1322. if (ad->next_arq[arq->is_sync] == arq)
  1323. next_arq = as_find_next_arq(ad, arq);
  1324. as_del_arq_rb(ad, arq);
  1325. if ((alias = as_add_arq_rb(ad, arq))) {
  1326. list_del_init(&arq->fifo);
  1327. as_add_aliased_request(ad, arq, alias);
  1328. if (next_arq)
  1329. ad->next_arq[arq->is_sync] = next_arq;
  1330. }
  1331. }
  1332. /*
  1333. * if anext expires before arq, assign its expire time to arq
  1334. * and move into anext position (anext will be deleted) in fifo
  1335. */
  1336. if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
  1337. if (time_before(anext->expires, arq->expires)) {
  1338. list_move(&arq->fifo, &anext->fifo);
  1339. arq->expires = anext->expires;
  1340. /*
  1341. * Don't copy here but swap, because when anext is
  1342. * removed below, it must contain the unused context
  1343. */
  1344. swap_io_context(&arq->io_context, &anext->io_context);
  1345. }
  1346. }
  1347. /*
  1348. * Transfer list of aliases
  1349. */
  1350. while (!list_empty(&next->queuelist)) {
  1351. struct request *__rq = list_entry_rq(next->queuelist.next);
  1352. struct as_rq *__arq = RQ_DATA(__rq);
  1353. list_move_tail(&__rq->queuelist, &req->queuelist);
  1354. WARN_ON(__arq->state != AS_RQ_QUEUED);
  1355. }
  1356. /*
  1357. * kill knowledge of next, this one is a goner
  1358. */
  1359. as_remove_queued_request(q, next);
  1360. as_put_io_context(anext);
  1361. anext->state = AS_RQ_MERGED;
  1362. }
  1363. /*
  1364. * This is executed in a "deferred" process context, by kblockd. It calls the
  1365. * driver's request_fn so the driver can submit that request.
  1366. *
  1367. * IMPORTANT! This guy will reenter the elevator, so set up all queue global
  1368. * state before calling, and don't rely on any state over calls.
  1369. *
  1370. * FIXME! dispatch queue is not a queue at all!
  1371. */
  1372. static void as_work_handler(void *data)
  1373. {
  1374. struct request_queue *q = data;
  1375. unsigned long flags;
  1376. spin_lock_irqsave(q->queue_lock, flags);
  1377. if (!as_queue_empty(q))
  1378. q->request_fn(q);
  1379. spin_unlock_irqrestore(q->queue_lock, flags);
  1380. }
  1381. static void as_put_request(request_queue_t *q, struct request *rq)
  1382. {
  1383. struct as_data *ad = q->elevator->elevator_data;
  1384. struct as_rq *arq = RQ_DATA(rq);
  1385. if (!arq) {
  1386. WARN_ON(1);
  1387. return;
  1388. }
  1389. if (unlikely(arq->state != AS_RQ_POSTSCHED &&
  1390. arq->state != AS_RQ_PRESCHED &&
  1391. arq->state != AS_RQ_MERGED)) {
  1392. printk("arq->state %d\n", arq->state);
  1393. WARN_ON(1);
  1394. }
  1395. mempool_free(arq, ad->arq_pool);
  1396. rq->elevator_private = NULL;
  1397. }
  1398. static int as_set_request(request_queue_t *q, struct request *rq,
  1399. struct bio *bio, gfp_t gfp_mask)
  1400. {
  1401. struct as_data *ad = q->elevator->elevator_data;
  1402. struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
  1403. if (arq) {
  1404. memset(arq, 0, sizeof(*arq));
  1405. RB_CLEAR(&arq->rb_node);
  1406. arq->request = rq;
  1407. arq->state = AS_RQ_PRESCHED;
  1408. arq->io_context = NULL;
  1409. INIT_LIST_HEAD(&arq->hash);
  1410. arq->on_hash = 0;
  1411. INIT_LIST_HEAD(&arq->fifo);
  1412. rq->elevator_private = arq;
  1413. return 0;
  1414. }
  1415. return 1;
  1416. }
  1417. static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1418. {
  1419. int ret = ELV_MQUEUE_MAY;
  1420. struct as_data *ad = q->elevator->elevator_data;
  1421. struct io_context *ioc;
  1422. if (ad->antic_status == ANTIC_WAIT_REQ ||
  1423. ad->antic_status == ANTIC_WAIT_NEXT) {
  1424. ioc = as_get_io_context();
  1425. if (ad->io_context == ioc)
  1426. ret = ELV_MQUEUE_MUST;
  1427. put_io_context(ioc);
  1428. }
  1429. return ret;
  1430. }
  1431. static void as_exit_queue(elevator_t *e)
  1432. {
  1433. struct as_data *ad = e->elevator_data;
  1434. del_timer_sync(&ad->antic_timer);
  1435. kblockd_flush();
  1436. BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
  1437. BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
  1438. mempool_destroy(ad->arq_pool);
  1439. put_io_context(ad->io_context);
  1440. kfree(ad->hash);
  1441. kfree(ad);
  1442. }
  1443. /*
  1444. * initialize elevator private data (as_data), and alloc a arq for
  1445. * each request on the free lists
  1446. */
  1447. static int as_init_queue(request_queue_t *q, elevator_t *e)
  1448. {
  1449. struct as_data *ad;
  1450. int i;
  1451. if (!arq_pool)
  1452. return -ENOMEM;
  1453. ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
  1454. if (!ad)
  1455. return -ENOMEM;
  1456. memset(ad, 0, sizeof(*ad));
  1457. ad->q = q; /* Identify what queue the data belongs to */
  1458. ad->hash = kmalloc_node(sizeof(struct list_head)*AS_HASH_ENTRIES,
  1459. GFP_KERNEL, q->node);
  1460. if (!ad->hash) {
  1461. kfree(ad);
  1462. return -ENOMEM;
  1463. }
  1464. ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  1465. mempool_free_slab, arq_pool, q->node);
  1466. if (!ad->arq_pool) {
  1467. kfree(ad->hash);
  1468. kfree(ad);
  1469. return -ENOMEM;
  1470. }
  1471. /* anticipatory scheduling helpers */
  1472. ad->antic_timer.function = as_antic_timeout;
  1473. ad->antic_timer.data = (unsigned long)q;
  1474. init_timer(&ad->antic_timer);
  1475. INIT_WORK(&ad->antic_work, as_work_handler, q);
  1476. for (i = 0; i < AS_HASH_ENTRIES; i++)
  1477. INIT_LIST_HEAD(&ad->hash[i]);
  1478. INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
  1479. INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
  1480. ad->sort_list[REQ_SYNC] = RB_ROOT;
  1481. ad->sort_list[REQ_ASYNC] = RB_ROOT;
  1482. ad->fifo_expire[REQ_SYNC] = default_read_expire;
  1483. ad->fifo_expire[REQ_ASYNC] = default_write_expire;
  1484. ad->antic_expire = default_antic_expire;
  1485. ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
  1486. ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
  1487. e->elevator_data = ad;
  1488. ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
  1489. ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
  1490. if (ad->write_batch_count < 2)
  1491. ad->write_batch_count = 2;
  1492. return 0;
  1493. }
  1494. /*
  1495. * sysfs parts below
  1496. */
  1497. struct as_fs_entry {
  1498. struct attribute attr;
  1499. ssize_t (*show)(struct as_data *, char *);
  1500. ssize_t (*store)(struct as_data *, const char *, size_t);
  1501. };
  1502. static ssize_t
  1503. as_var_show(unsigned int var, char *page)
  1504. {
  1505. return sprintf(page, "%d\n", var);
  1506. }
  1507. static ssize_t
  1508. as_var_store(unsigned long *var, const char *page, size_t count)
  1509. {
  1510. char *p = (char *) page;
  1511. *var = simple_strtoul(p, &p, 10);
  1512. return count;
  1513. }
  1514. static ssize_t as_est_show(struct as_data *ad, char *page)
  1515. {
  1516. int pos = 0;
  1517. pos += sprintf(page+pos, "%lu %% exit probability\n",
  1518. 100*ad->exit_prob/256);
  1519. pos += sprintf(page+pos, "%lu %% probability of exiting without a "
  1520. "cooperating process submitting IO\n",
  1521. 100*ad->exit_no_coop/256);
  1522. pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
  1523. pos += sprintf(page+pos, "%llu sectors new seek distance\n",
  1524. (unsigned long long)ad->new_seek_mean);
  1525. return pos;
  1526. }
  1527. #define SHOW_FUNCTION(__FUNC, __VAR) \
  1528. static ssize_t __FUNC(struct as_data *ad, char *page) \
  1529. { \
  1530. return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
  1531. }
  1532. SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
  1533. SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
  1534. SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
  1535. SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
  1536. SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
  1537. #undef SHOW_FUNCTION
  1538. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
  1539. static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \
  1540. { \
  1541. int ret = as_var_store(__PTR, (page), count); \
  1542. if (*(__PTR) < (MIN)) \
  1543. *(__PTR) = (MIN); \
  1544. else if (*(__PTR) > (MAX)) \
  1545. *(__PTR) = (MAX); \
  1546. *(__PTR) = msecs_to_jiffies(*(__PTR)); \
  1547. return ret; \
  1548. }
  1549. STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
  1550. STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
  1551. STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
  1552. STORE_FUNCTION(as_read_batchexpire_store,
  1553. &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
  1554. STORE_FUNCTION(as_write_batchexpire_store,
  1555. &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
  1556. #undef STORE_FUNCTION
  1557. static struct as_fs_entry as_est_entry = {
  1558. .attr = {.name = "est_time", .mode = S_IRUGO },
  1559. .show = as_est_show,
  1560. };
  1561. static struct as_fs_entry as_readexpire_entry = {
  1562. .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
  1563. .show = as_readexpire_show,
  1564. .store = as_readexpire_store,
  1565. };
  1566. static struct as_fs_entry as_writeexpire_entry = {
  1567. .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
  1568. .show = as_writeexpire_show,
  1569. .store = as_writeexpire_store,
  1570. };
  1571. static struct as_fs_entry as_anticexpire_entry = {
  1572. .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
  1573. .show = as_anticexpire_show,
  1574. .store = as_anticexpire_store,
  1575. };
  1576. static struct as_fs_entry as_read_batchexpire_entry = {
  1577. .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
  1578. .show = as_read_batchexpire_show,
  1579. .store = as_read_batchexpire_store,
  1580. };
  1581. static struct as_fs_entry as_write_batchexpire_entry = {
  1582. .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
  1583. .show = as_write_batchexpire_show,
  1584. .store = as_write_batchexpire_store,
  1585. };
  1586. static struct attribute *default_attrs[] = {
  1587. &as_est_entry.attr,
  1588. &as_readexpire_entry.attr,
  1589. &as_writeexpire_entry.attr,
  1590. &as_anticexpire_entry.attr,
  1591. &as_read_batchexpire_entry.attr,
  1592. &as_write_batchexpire_entry.attr,
  1593. NULL,
  1594. };
  1595. #define to_as(atr) container_of((atr), struct as_fs_entry, attr)
  1596. static ssize_t
  1597. as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  1598. {
  1599. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1600. struct as_fs_entry *entry = to_as(attr);
  1601. if (!entry->show)
  1602. return -EIO;
  1603. return entry->show(e->elevator_data, page);
  1604. }
  1605. static ssize_t
  1606. as_attr_store(struct kobject *kobj, struct attribute *attr,
  1607. const char *page, size_t length)
  1608. {
  1609. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1610. struct as_fs_entry *entry = to_as(attr);
  1611. if (!entry->store)
  1612. return -EIO;
  1613. return entry->store(e->elevator_data, page, length);
  1614. }
  1615. static struct sysfs_ops as_sysfs_ops = {
  1616. .show = as_attr_show,
  1617. .store = as_attr_store,
  1618. };
  1619. static struct kobj_type as_ktype = {
  1620. .sysfs_ops = &as_sysfs_ops,
  1621. .default_attrs = default_attrs,
  1622. };
  1623. static struct elevator_type iosched_as = {
  1624. .ops = {
  1625. .elevator_merge_fn = as_merge,
  1626. .elevator_merged_fn = as_merged_request,
  1627. .elevator_merge_req_fn = as_merged_requests,
  1628. .elevator_dispatch_fn = as_dispatch_request,
  1629. .elevator_add_req_fn = as_add_request,
  1630. .elevator_activate_req_fn = as_activate_request,
  1631. .elevator_deactivate_req_fn = as_deactivate_request,
  1632. .elevator_queue_empty_fn = as_queue_empty,
  1633. .elevator_completed_req_fn = as_completed_request,
  1634. .elevator_former_req_fn = as_former_request,
  1635. .elevator_latter_req_fn = as_latter_request,
  1636. .elevator_set_req_fn = as_set_request,
  1637. .elevator_put_req_fn = as_put_request,
  1638. .elevator_may_queue_fn = as_may_queue,
  1639. .elevator_init_fn = as_init_queue,
  1640. .elevator_exit_fn = as_exit_queue,
  1641. },
  1642. .elevator_ktype = &as_ktype,
  1643. .elevator_name = "anticipatory",
  1644. .elevator_owner = THIS_MODULE,
  1645. };
  1646. static int __init as_init(void)
  1647. {
  1648. int ret;
  1649. arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
  1650. 0, 0, NULL, NULL);
  1651. if (!arq_pool)
  1652. return -ENOMEM;
  1653. ret = elv_register(&iosched_as);
  1654. if (!ret) {
  1655. /*
  1656. * don't allow AS to get unregistered, since we would have
  1657. * to browse all tasks in the system and release their
  1658. * as_io_context first
  1659. */
  1660. __module_get(THIS_MODULE);
  1661. return 0;
  1662. }
  1663. kmem_cache_destroy(arq_pool);
  1664. return ret;
  1665. }
  1666. static void __exit as_exit(void)
  1667. {
  1668. elv_unregister(&iosched_as);
  1669. kmem_cache_destroy(arq_pool);
  1670. }
  1671. module_init(as_init);
  1672. module_exit(as_exit);
  1673. MODULE_AUTHOR("Nick Piggin");
  1674. MODULE_LICENSE("GPL");
  1675. MODULE_DESCRIPTION("anticipatory IO scheduler");