as-iosched.c 49 KB

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