as-iosched.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  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. if (arq->state != AS_RQ_PRESCHED) {
  1149. printk("arq->state: %d\n", arq->state);
  1150. WARN_ON(1);
  1151. }
  1152. arq->state = AS_RQ_NEW;
  1153. if (rq_data_dir(arq->request) == READ
  1154. || current->flags&PF_SYNCWRITE)
  1155. arq->is_sync = 1;
  1156. else
  1157. arq->is_sync = 0;
  1158. data_dir = arq->is_sync;
  1159. arq->io_context = as_get_io_context();
  1160. if (arq->io_context) {
  1161. as_update_iohist(ad, arq->io_context->aic, arq->request);
  1162. atomic_inc(&arq->io_context->aic->nr_queued);
  1163. }
  1164. alias = as_add_arq_rb(ad, arq);
  1165. if (!alias) {
  1166. /*
  1167. * set expire time (only used for reads) and add to fifo list
  1168. */
  1169. arq->expires = jiffies + ad->fifo_expire[data_dir];
  1170. list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
  1171. if (rq_mergeable(arq->request))
  1172. as_add_arq_hash(ad, arq);
  1173. as_update_arq(ad, arq); /* keep state machine up to date */
  1174. } else {
  1175. as_add_aliased_request(ad, arq, alias);
  1176. /*
  1177. * have we been anticipating this request?
  1178. * or does it come from the same process as the one we are
  1179. * anticipating for?
  1180. */
  1181. if (ad->antic_status == ANTIC_WAIT_REQ
  1182. || ad->antic_status == ANTIC_WAIT_NEXT) {
  1183. if (as_can_break_anticipation(ad, arq))
  1184. as_antic_stop(ad);
  1185. }
  1186. }
  1187. arq->state = AS_RQ_QUEUED;
  1188. }
  1189. static void as_activate_request(request_queue_t *q, struct request *rq)
  1190. {
  1191. struct as_rq *arq = RQ_DATA(rq);
  1192. WARN_ON(arq->state != AS_RQ_DISPATCHED);
  1193. arq->state = AS_RQ_REMOVED;
  1194. if (arq->io_context && arq->io_context->aic)
  1195. atomic_dec(&arq->io_context->aic->nr_dispatched);
  1196. }
  1197. static void as_deactivate_request(request_queue_t *q, struct request *rq)
  1198. {
  1199. struct as_rq *arq = RQ_DATA(rq);
  1200. WARN_ON(arq->state != AS_RQ_REMOVED);
  1201. arq->state = AS_RQ_DISPATCHED;
  1202. if (arq->io_context && arq->io_context->aic)
  1203. atomic_inc(&arq->io_context->aic->nr_dispatched);
  1204. }
  1205. /*
  1206. * as_queue_empty tells us if there are requests left in the device. It may
  1207. * not be the case that a driver can get the next request even if the queue
  1208. * is not empty - it is used in the block layer to check for plugging and
  1209. * merging opportunities
  1210. */
  1211. static int as_queue_empty(request_queue_t *q)
  1212. {
  1213. struct as_data *ad = q->elevator->elevator_data;
  1214. return list_empty(&ad->fifo_list[REQ_ASYNC])
  1215. && list_empty(&ad->fifo_list[REQ_SYNC]);
  1216. }
  1217. static struct request *as_former_request(request_queue_t *q,
  1218. struct request *rq)
  1219. {
  1220. struct as_rq *arq = RQ_DATA(rq);
  1221. struct rb_node *rbprev = rb_prev(&arq->rb_node);
  1222. struct request *ret = NULL;
  1223. if (rbprev)
  1224. ret = rb_entry_arq(rbprev)->request;
  1225. return ret;
  1226. }
  1227. static struct request *as_latter_request(request_queue_t *q,
  1228. struct request *rq)
  1229. {
  1230. struct as_rq *arq = RQ_DATA(rq);
  1231. struct rb_node *rbnext = rb_next(&arq->rb_node);
  1232. struct request *ret = NULL;
  1233. if (rbnext)
  1234. ret = rb_entry_arq(rbnext)->request;
  1235. return ret;
  1236. }
  1237. static int
  1238. as_merge(request_queue_t *q, struct request **req, struct bio *bio)
  1239. {
  1240. struct as_data *ad = q->elevator->elevator_data;
  1241. sector_t rb_key = bio->bi_sector + bio_sectors(bio);
  1242. struct request *__rq;
  1243. int ret;
  1244. /*
  1245. * see if the merge hash can satisfy a back merge
  1246. */
  1247. __rq = as_find_arq_hash(ad, bio->bi_sector);
  1248. if (__rq) {
  1249. BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
  1250. if (elv_rq_merge_ok(__rq, bio)) {
  1251. ret = ELEVATOR_BACK_MERGE;
  1252. goto out;
  1253. }
  1254. }
  1255. /*
  1256. * check for front merge
  1257. */
  1258. __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
  1259. if (__rq) {
  1260. BUG_ON(rb_key != rq_rb_key(__rq));
  1261. if (elv_rq_merge_ok(__rq, bio)) {
  1262. ret = ELEVATOR_FRONT_MERGE;
  1263. goto out;
  1264. }
  1265. }
  1266. return ELEVATOR_NO_MERGE;
  1267. out:
  1268. if (ret) {
  1269. if (rq_mergeable(__rq))
  1270. as_hot_arq_hash(ad, RQ_DATA(__rq));
  1271. }
  1272. *req = __rq;
  1273. return ret;
  1274. }
  1275. static void as_merged_request(request_queue_t *q, struct request *req)
  1276. {
  1277. struct as_data *ad = q->elevator->elevator_data;
  1278. struct as_rq *arq = RQ_DATA(req);
  1279. /*
  1280. * hash always needs to be repositioned, key is end sector
  1281. */
  1282. as_del_arq_hash(arq);
  1283. as_add_arq_hash(ad, arq);
  1284. /*
  1285. * if the merge was a front merge, we need to reposition request
  1286. */
  1287. if (rq_rb_key(req) != arq->rb_key) {
  1288. struct as_rq *alias, *next_arq = NULL;
  1289. if (ad->next_arq[arq->is_sync] == arq)
  1290. next_arq = as_find_next_arq(ad, arq);
  1291. /*
  1292. * Note! We should really be moving any old aliased requests
  1293. * off this request and try to insert them into the rbtree. We
  1294. * currently don't bother. Ditto the next function.
  1295. */
  1296. as_del_arq_rb(ad, arq);
  1297. if ((alias = as_add_arq_rb(ad, arq))) {
  1298. list_del_init(&arq->fifo);
  1299. as_add_aliased_request(ad, arq, alias);
  1300. if (next_arq)
  1301. ad->next_arq[arq->is_sync] = next_arq;
  1302. }
  1303. /*
  1304. * Note! At this stage of this and the next function, our next
  1305. * request may not be optimal - eg the request may have "grown"
  1306. * behind the disk head. We currently don't bother adjusting.
  1307. */
  1308. }
  1309. }
  1310. static void as_merged_requests(request_queue_t *q, struct request *req,
  1311. struct request *next)
  1312. {
  1313. struct as_data *ad = q->elevator->elevator_data;
  1314. struct as_rq *arq = RQ_DATA(req);
  1315. struct as_rq *anext = RQ_DATA(next);
  1316. BUG_ON(!arq);
  1317. BUG_ON(!anext);
  1318. /*
  1319. * reposition arq (this is the merged request) in hash, and in rbtree
  1320. * in case of a front merge
  1321. */
  1322. as_del_arq_hash(arq);
  1323. as_add_arq_hash(ad, arq);
  1324. if (rq_rb_key(req) != arq->rb_key) {
  1325. struct as_rq *alias, *next_arq = NULL;
  1326. if (ad->next_arq[arq->is_sync] == arq)
  1327. next_arq = as_find_next_arq(ad, arq);
  1328. as_del_arq_rb(ad, arq);
  1329. if ((alias = as_add_arq_rb(ad, arq))) {
  1330. list_del_init(&arq->fifo);
  1331. as_add_aliased_request(ad, arq, alias);
  1332. if (next_arq)
  1333. ad->next_arq[arq->is_sync] = next_arq;
  1334. }
  1335. }
  1336. /*
  1337. * if anext expires before arq, assign its expire time to arq
  1338. * and move into anext position (anext will be deleted) in fifo
  1339. */
  1340. if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
  1341. if (time_before(anext->expires, arq->expires)) {
  1342. list_move(&arq->fifo, &anext->fifo);
  1343. arq->expires = anext->expires;
  1344. /*
  1345. * Don't copy here but swap, because when anext is
  1346. * removed below, it must contain the unused context
  1347. */
  1348. swap_io_context(&arq->io_context, &anext->io_context);
  1349. }
  1350. }
  1351. /*
  1352. * Transfer list of aliases
  1353. */
  1354. while (!list_empty(&next->queuelist)) {
  1355. struct request *__rq = list_entry_rq(next->queuelist.next);
  1356. struct as_rq *__arq = RQ_DATA(__rq);
  1357. list_move_tail(&__rq->queuelist, &req->queuelist);
  1358. WARN_ON(__arq->state != AS_RQ_QUEUED);
  1359. }
  1360. /*
  1361. * kill knowledge of next, this one is a goner
  1362. */
  1363. as_remove_queued_request(q, next);
  1364. as_put_io_context(anext);
  1365. anext->state = AS_RQ_MERGED;
  1366. }
  1367. /*
  1368. * This is executed in a "deferred" process context, by kblockd. It calls the
  1369. * driver's request_fn so the driver can submit that request.
  1370. *
  1371. * IMPORTANT! This guy will reenter the elevator, so set up all queue global
  1372. * state before calling, and don't rely on any state over calls.
  1373. *
  1374. * FIXME! dispatch queue is not a queue at all!
  1375. */
  1376. static void as_work_handler(void *data)
  1377. {
  1378. struct request_queue *q = data;
  1379. unsigned long flags;
  1380. spin_lock_irqsave(q->queue_lock, flags);
  1381. if (!as_queue_empty(q))
  1382. q->request_fn(q);
  1383. spin_unlock_irqrestore(q->queue_lock, flags);
  1384. }
  1385. static void as_put_request(request_queue_t *q, struct request *rq)
  1386. {
  1387. struct as_data *ad = q->elevator->elevator_data;
  1388. struct as_rq *arq = RQ_DATA(rq);
  1389. if (!arq) {
  1390. WARN_ON(1);
  1391. return;
  1392. }
  1393. if (unlikely(arq->state != AS_RQ_POSTSCHED &&
  1394. arq->state != AS_RQ_PRESCHED &&
  1395. arq->state != AS_RQ_MERGED)) {
  1396. printk("arq->state %d\n", arq->state);
  1397. WARN_ON(1);
  1398. }
  1399. mempool_free(arq, ad->arq_pool);
  1400. rq->elevator_private = NULL;
  1401. }
  1402. static int as_set_request(request_queue_t *q, struct request *rq,
  1403. struct bio *bio, gfp_t gfp_mask)
  1404. {
  1405. struct as_data *ad = q->elevator->elevator_data;
  1406. struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
  1407. if (arq) {
  1408. memset(arq, 0, sizeof(*arq));
  1409. RB_CLEAR(&arq->rb_node);
  1410. arq->request = rq;
  1411. arq->state = AS_RQ_PRESCHED;
  1412. arq->io_context = NULL;
  1413. INIT_LIST_HEAD(&arq->hash);
  1414. arq->on_hash = 0;
  1415. INIT_LIST_HEAD(&arq->fifo);
  1416. rq->elevator_private = arq;
  1417. return 0;
  1418. }
  1419. return 1;
  1420. }
  1421. static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1422. {
  1423. int ret = ELV_MQUEUE_MAY;
  1424. struct as_data *ad = q->elevator->elevator_data;
  1425. struct io_context *ioc;
  1426. if (ad->antic_status == ANTIC_WAIT_REQ ||
  1427. ad->antic_status == ANTIC_WAIT_NEXT) {
  1428. ioc = as_get_io_context();
  1429. if (ad->io_context == ioc)
  1430. ret = ELV_MQUEUE_MUST;
  1431. put_io_context(ioc);
  1432. }
  1433. return ret;
  1434. }
  1435. static void as_exit_queue(elevator_t *e)
  1436. {
  1437. struct as_data *ad = e->elevator_data;
  1438. del_timer_sync(&ad->antic_timer);
  1439. kblockd_flush();
  1440. BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
  1441. BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
  1442. mempool_destroy(ad->arq_pool);
  1443. put_io_context(ad->io_context);
  1444. kfree(ad->hash);
  1445. kfree(ad);
  1446. }
  1447. /*
  1448. * initialize elevator private data (as_data), and alloc a arq for
  1449. * each request on the free lists
  1450. */
  1451. static int as_init_queue(request_queue_t *q, elevator_t *e)
  1452. {
  1453. struct as_data *ad;
  1454. int i;
  1455. if (!arq_pool)
  1456. return -ENOMEM;
  1457. ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
  1458. if (!ad)
  1459. return -ENOMEM;
  1460. memset(ad, 0, sizeof(*ad));
  1461. ad->q = q; /* Identify what queue the data belongs to */
  1462. ad->hash = kmalloc_node(sizeof(struct list_head)*AS_HASH_ENTRIES,
  1463. GFP_KERNEL, q->node);
  1464. if (!ad->hash) {
  1465. kfree(ad);
  1466. return -ENOMEM;
  1467. }
  1468. ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  1469. mempool_free_slab, arq_pool, q->node);
  1470. if (!ad->arq_pool) {
  1471. kfree(ad->hash);
  1472. kfree(ad);
  1473. return -ENOMEM;
  1474. }
  1475. /* anticipatory scheduling helpers */
  1476. ad->antic_timer.function = as_antic_timeout;
  1477. ad->antic_timer.data = (unsigned long)q;
  1478. init_timer(&ad->antic_timer);
  1479. INIT_WORK(&ad->antic_work, as_work_handler, q);
  1480. for (i = 0; i < AS_HASH_ENTRIES; i++)
  1481. INIT_LIST_HEAD(&ad->hash[i]);
  1482. INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
  1483. INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
  1484. ad->sort_list[REQ_SYNC] = RB_ROOT;
  1485. ad->sort_list[REQ_ASYNC] = RB_ROOT;
  1486. ad->fifo_expire[REQ_SYNC] = default_read_expire;
  1487. ad->fifo_expire[REQ_ASYNC] = default_write_expire;
  1488. ad->antic_expire = default_antic_expire;
  1489. ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
  1490. ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
  1491. e->elevator_data = ad;
  1492. ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
  1493. ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
  1494. if (ad->write_batch_count < 2)
  1495. ad->write_batch_count = 2;
  1496. return 0;
  1497. }
  1498. /*
  1499. * sysfs parts below
  1500. */
  1501. struct as_fs_entry {
  1502. struct attribute attr;
  1503. ssize_t (*show)(struct as_data *, char *);
  1504. ssize_t (*store)(struct as_data *, const char *, size_t);
  1505. };
  1506. static ssize_t
  1507. as_var_show(unsigned int var, char *page)
  1508. {
  1509. return sprintf(page, "%d\n", var);
  1510. }
  1511. static ssize_t
  1512. as_var_store(unsigned long *var, const char *page, size_t count)
  1513. {
  1514. char *p = (char *) page;
  1515. *var = simple_strtoul(p, &p, 10);
  1516. return count;
  1517. }
  1518. static ssize_t as_est_show(struct as_data *ad, char *page)
  1519. {
  1520. int pos = 0;
  1521. pos += sprintf(page+pos, "%lu %% exit probability\n",
  1522. 100*ad->exit_prob/256);
  1523. pos += sprintf(page+pos, "%lu %% probability of exiting without a "
  1524. "cooperating process submitting IO\n",
  1525. 100*ad->exit_no_coop/256);
  1526. pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
  1527. pos += sprintf(page+pos, "%llu sectors new seek distance\n",
  1528. (unsigned long long)ad->new_seek_mean);
  1529. return pos;
  1530. }
  1531. #define SHOW_FUNCTION(__FUNC, __VAR) \
  1532. static ssize_t __FUNC(struct as_data *ad, char *page) \
  1533. { \
  1534. return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
  1535. }
  1536. SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
  1537. SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
  1538. SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
  1539. SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
  1540. SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
  1541. #undef SHOW_FUNCTION
  1542. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
  1543. static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \
  1544. { \
  1545. int ret = as_var_store(__PTR, (page), count); \
  1546. if (*(__PTR) < (MIN)) \
  1547. *(__PTR) = (MIN); \
  1548. else if (*(__PTR) > (MAX)) \
  1549. *(__PTR) = (MAX); \
  1550. *(__PTR) = msecs_to_jiffies(*(__PTR)); \
  1551. return ret; \
  1552. }
  1553. STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
  1554. STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
  1555. STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
  1556. STORE_FUNCTION(as_read_batchexpire_store,
  1557. &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
  1558. STORE_FUNCTION(as_write_batchexpire_store,
  1559. &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
  1560. #undef STORE_FUNCTION
  1561. static struct as_fs_entry as_est_entry = {
  1562. .attr = {.name = "est_time", .mode = S_IRUGO },
  1563. .show = as_est_show,
  1564. };
  1565. static struct as_fs_entry as_readexpire_entry = {
  1566. .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
  1567. .show = as_readexpire_show,
  1568. .store = as_readexpire_store,
  1569. };
  1570. static struct as_fs_entry as_writeexpire_entry = {
  1571. .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
  1572. .show = as_writeexpire_show,
  1573. .store = as_writeexpire_store,
  1574. };
  1575. static struct as_fs_entry as_anticexpire_entry = {
  1576. .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
  1577. .show = as_anticexpire_show,
  1578. .store = as_anticexpire_store,
  1579. };
  1580. static struct as_fs_entry as_read_batchexpire_entry = {
  1581. .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
  1582. .show = as_read_batchexpire_show,
  1583. .store = as_read_batchexpire_store,
  1584. };
  1585. static struct as_fs_entry as_write_batchexpire_entry = {
  1586. .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
  1587. .show = as_write_batchexpire_show,
  1588. .store = as_write_batchexpire_store,
  1589. };
  1590. static struct attribute *default_attrs[] = {
  1591. &as_est_entry.attr,
  1592. &as_readexpire_entry.attr,
  1593. &as_writeexpire_entry.attr,
  1594. &as_anticexpire_entry.attr,
  1595. &as_read_batchexpire_entry.attr,
  1596. &as_write_batchexpire_entry.attr,
  1597. NULL,
  1598. };
  1599. #define to_as(atr) container_of((atr), struct as_fs_entry, attr)
  1600. static ssize_t
  1601. as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  1602. {
  1603. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1604. struct as_fs_entry *entry = to_as(attr);
  1605. if (!entry->show)
  1606. return -EIO;
  1607. return entry->show(e->elevator_data, page);
  1608. }
  1609. static ssize_t
  1610. as_attr_store(struct kobject *kobj, struct attribute *attr,
  1611. const char *page, size_t length)
  1612. {
  1613. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1614. struct as_fs_entry *entry = to_as(attr);
  1615. if (!entry->store)
  1616. return -EIO;
  1617. return entry->store(e->elevator_data, page, length);
  1618. }
  1619. static struct sysfs_ops as_sysfs_ops = {
  1620. .show = as_attr_show,
  1621. .store = as_attr_store,
  1622. };
  1623. static struct kobj_type as_ktype = {
  1624. .sysfs_ops = &as_sysfs_ops,
  1625. .default_attrs = default_attrs,
  1626. };
  1627. static struct elevator_type iosched_as = {
  1628. .ops = {
  1629. .elevator_merge_fn = as_merge,
  1630. .elevator_merged_fn = as_merged_request,
  1631. .elevator_merge_req_fn = as_merged_requests,
  1632. .elevator_dispatch_fn = as_dispatch_request,
  1633. .elevator_add_req_fn = as_add_request,
  1634. .elevator_activate_req_fn = as_activate_request,
  1635. .elevator_deactivate_req_fn = as_deactivate_request,
  1636. .elevator_queue_empty_fn = as_queue_empty,
  1637. .elevator_completed_req_fn = as_completed_request,
  1638. .elevator_former_req_fn = as_former_request,
  1639. .elevator_latter_req_fn = as_latter_request,
  1640. .elevator_set_req_fn = as_set_request,
  1641. .elevator_put_req_fn = as_put_request,
  1642. .elevator_may_queue_fn = as_may_queue,
  1643. .elevator_init_fn = as_init_queue,
  1644. .elevator_exit_fn = as_exit_queue,
  1645. },
  1646. .elevator_ktype = &as_ktype,
  1647. .elevator_name = "anticipatory",
  1648. .elevator_owner = THIS_MODULE,
  1649. };
  1650. static int __init as_init(void)
  1651. {
  1652. int ret;
  1653. arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
  1654. 0, 0, NULL, NULL);
  1655. if (!arq_pool)
  1656. return -ENOMEM;
  1657. ret = elv_register(&iosched_as);
  1658. if (!ret) {
  1659. /*
  1660. * don't allow AS to get unregistered, since we would have
  1661. * to browse all tasks in the system and release their
  1662. * as_io_context first
  1663. */
  1664. __module_get(THIS_MODULE);
  1665. return 0;
  1666. }
  1667. kmem_cache_destroy(arq_pool);
  1668. return ret;
  1669. }
  1670. static void __exit as_exit(void)
  1671. {
  1672. elv_unregister(&iosched_as);
  1673. kmem_cache_destroy(arq_pool);
  1674. }
  1675. module_init(as_init);
  1676. module_exit(as_exit);
  1677. MODULE_AUTHOR("Nick Piggin");
  1678. MODULE_LICENSE("GPL");
  1679. MODULE_DESCRIPTION("anticipatory IO scheduler");