ll_rw_blk.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645
  1. /*
  2. * linux/drivers/block/ll_rw_blk.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
  6. * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  7. * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
  8. * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000
  9. * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
  10. */
  11. /*
  12. * This handles all read/write requests to block devices
  13. */
  14. #include <linux/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/backing-dev.h>
  18. #include <linux/bio.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/highmem.h>
  21. #include <linux/mm.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/string.h>
  24. #include <linux/init.h>
  25. #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
  26. #include <linux/completion.h>
  27. #include <linux/slab.h>
  28. #include <linux/swap.h>
  29. #include <linux/writeback.h>
  30. /*
  31. * for max sense size
  32. */
  33. #include <scsi/scsi_cmnd.h>
  34. static void blk_unplug_work(void *data);
  35. static void blk_unplug_timeout(unsigned long data);
  36. /*
  37. * For the allocated request tables
  38. */
  39. static kmem_cache_t *request_cachep;
  40. /*
  41. * For queue allocation
  42. */
  43. static kmem_cache_t *requestq_cachep;
  44. /*
  45. * For io context allocations
  46. */
  47. static kmem_cache_t *iocontext_cachep;
  48. static wait_queue_head_t congestion_wqh[2] = {
  49. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  50. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  51. };
  52. /*
  53. * Controlling structure to kblockd
  54. */
  55. static struct workqueue_struct *kblockd_workqueue;
  56. unsigned long blk_max_low_pfn, blk_max_pfn;
  57. EXPORT_SYMBOL(blk_max_low_pfn);
  58. EXPORT_SYMBOL(blk_max_pfn);
  59. /* Amount of time in which a process may batch requests */
  60. #define BLK_BATCH_TIME (HZ/50UL)
  61. /* Number of requests a "batching" process may submit */
  62. #define BLK_BATCH_REQ 32
  63. /*
  64. * Return the threshold (number of used requests) at which the queue is
  65. * considered to be congested. It include a little hysteresis to keep the
  66. * context switch rate down.
  67. */
  68. static inline int queue_congestion_on_threshold(struct request_queue *q)
  69. {
  70. return q->nr_congestion_on;
  71. }
  72. /*
  73. * The threshold at which a queue is considered to be uncongested
  74. */
  75. static inline int queue_congestion_off_threshold(struct request_queue *q)
  76. {
  77. return q->nr_congestion_off;
  78. }
  79. static void blk_queue_congestion_threshold(struct request_queue *q)
  80. {
  81. int nr;
  82. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  83. if (nr > q->nr_requests)
  84. nr = q->nr_requests;
  85. q->nr_congestion_on = nr;
  86. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  87. if (nr < 1)
  88. nr = 1;
  89. q->nr_congestion_off = nr;
  90. }
  91. /*
  92. * A queue has just exitted congestion. Note this in the global counter of
  93. * congested queues, and wake up anyone who was waiting for requests to be
  94. * put back.
  95. */
  96. static void clear_queue_congested(request_queue_t *q, int rw)
  97. {
  98. enum bdi_state bit;
  99. wait_queue_head_t *wqh = &congestion_wqh[rw];
  100. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  101. clear_bit(bit, &q->backing_dev_info.state);
  102. smp_mb__after_clear_bit();
  103. if (waitqueue_active(wqh))
  104. wake_up(wqh);
  105. }
  106. /*
  107. * A queue has just entered congestion. Flag that in the queue's VM-visible
  108. * state flags and increment the global gounter of congested queues.
  109. */
  110. static void set_queue_congested(request_queue_t *q, int rw)
  111. {
  112. enum bdi_state bit;
  113. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  114. set_bit(bit, &q->backing_dev_info.state);
  115. }
  116. /**
  117. * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
  118. * @bdev: device
  119. *
  120. * Locates the passed device's request queue and returns the address of its
  121. * backing_dev_info
  122. *
  123. * Will return NULL if the request queue cannot be located.
  124. */
  125. struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
  126. {
  127. struct backing_dev_info *ret = NULL;
  128. request_queue_t *q = bdev_get_queue(bdev);
  129. if (q)
  130. ret = &q->backing_dev_info;
  131. return ret;
  132. }
  133. EXPORT_SYMBOL(blk_get_backing_dev_info);
  134. void blk_queue_activity_fn(request_queue_t *q, activity_fn *fn, void *data)
  135. {
  136. q->activity_fn = fn;
  137. q->activity_data = data;
  138. }
  139. EXPORT_SYMBOL(blk_queue_activity_fn);
  140. /**
  141. * blk_queue_prep_rq - set a prepare_request function for queue
  142. * @q: queue
  143. * @pfn: prepare_request function
  144. *
  145. * It's possible for a queue to register a prepare_request callback which
  146. * is invoked before the request is handed to the request_fn. The goal of
  147. * the function is to prepare a request for I/O, it can be used to build a
  148. * cdb from the request data for instance.
  149. *
  150. */
  151. void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn)
  152. {
  153. q->prep_rq_fn = pfn;
  154. }
  155. EXPORT_SYMBOL(blk_queue_prep_rq);
  156. /**
  157. * blk_queue_merge_bvec - set a merge_bvec function for queue
  158. * @q: queue
  159. * @mbfn: merge_bvec_fn
  160. *
  161. * Usually queues have static limitations on the max sectors or segments that
  162. * we can put in a request. Stacking drivers may have some settings that
  163. * are dynamic, and thus we have to query the queue whether it is ok to
  164. * add a new bio_vec to a bio at a given offset or not. If the block device
  165. * has such limitations, it needs to register a merge_bvec_fn to control
  166. * the size of bio's sent to it. Note that a block device *must* allow a
  167. * single page to be added to an empty bio. The block device driver may want
  168. * to use the bio_split() function to deal with these bio's. By default
  169. * no merge_bvec_fn is defined for a queue, and only the fixed limits are
  170. * honored.
  171. */
  172. void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn)
  173. {
  174. q->merge_bvec_fn = mbfn;
  175. }
  176. EXPORT_SYMBOL(blk_queue_merge_bvec);
  177. /**
  178. * blk_queue_make_request - define an alternate make_request function for a device
  179. * @q: the request queue for the device to be affected
  180. * @mfn: the alternate make_request function
  181. *
  182. * Description:
  183. * The normal way for &struct bios to be passed to a device
  184. * driver is for them to be collected into requests on a request
  185. * queue, and then to allow the device driver to select requests
  186. * off that queue when it is ready. This works well for many block
  187. * devices. However some block devices (typically virtual devices
  188. * such as md or lvm) do not benefit from the processing on the
  189. * request queue, and are served best by having the requests passed
  190. * directly to them. This can be achieved by providing a function
  191. * to blk_queue_make_request().
  192. *
  193. * Caveat:
  194. * The driver that does this *must* be able to deal appropriately
  195. * with buffers in "highmemory". This can be accomplished by either calling
  196. * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
  197. * blk_queue_bounce() to create a buffer in normal memory.
  198. **/
  199. void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
  200. {
  201. /*
  202. * set defaults
  203. */
  204. q->nr_requests = BLKDEV_MAX_RQ;
  205. q->max_phys_segments = MAX_PHYS_SEGMENTS;
  206. q->max_hw_segments = MAX_HW_SEGMENTS;
  207. q->make_request_fn = mfn;
  208. q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  209. q->backing_dev_info.state = 0;
  210. q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
  211. blk_queue_max_sectors(q, MAX_SECTORS);
  212. blk_queue_hardsect_size(q, 512);
  213. blk_queue_dma_alignment(q, 511);
  214. blk_queue_congestion_threshold(q);
  215. q->nr_batching = BLK_BATCH_REQ;
  216. q->unplug_thresh = 4; /* hmm */
  217. q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
  218. if (q->unplug_delay == 0)
  219. q->unplug_delay = 1;
  220. INIT_WORK(&q->unplug_work, blk_unplug_work, q);
  221. q->unplug_timer.function = blk_unplug_timeout;
  222. q->unplug_timer.data = (unsigned long)q;
  223. /*
  224. * by default assume old behaviour and bounce for any highmem page
  225. */
  226. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  227. blk_queue_activity_fn(q, NULL, NULL);
  228. INIT_LIST_HEAD(&q->drain_list);
  229. }
  230. EXPORT_SYMBOL(blk_queue_make_request);
  231. static inline void rq_init(request_queue_t *q, struct request *rq)
  232. {
  233. INIT_LIST_HEAD(&rq->queuelist);
  234. rq->errors = 0;
  235. rq->rq_status = RQ_ACTIVE;
  236. rq->bio = rq->biotail = NULL;
  237. rq->buffer = NULL;
  238. rq->ref_count = 1;
  239. rq->q = q;
  240. rq->waiting = NULL;
  241. rq->special = NULL;
  242. rq->data_len = 0;
  243. rq->data = NULL;
  244. rq->sense = NULL;
  245. rq->end_io = NULL;
  246. rq->end_io_data = NULL;
  247. }
  248. /**
  249. * blk_queue_ordered - does this queue support ordered writes
  250. * @q: the request queue
  251. * @flag: see below
  252. *
  253. * Description:
  254. * For journalled file systems, doing ordered writes on a commit
  255. * block instead of explicitly doing wait_on_buffer (which is bad
  256. * for performance) can be a big win. Block drivers supporting this
  257. * feature should call this function and indicate so.
  258. *
  259. **/
  260. void blk_queue_ordered(request_queue_t *q, int flag)
  261. {
  262. switch (flag) {
  263. case QUEUE_ORDERED_NONE:
  264. if (q->flush_rq)
  265. kmem_cache_free(request_cachep, q->flush_rq);
  266. q->flush_rq = NULL;
  267. q->ordered = flag;
  268. break;
  269. case QUEUE_ORDERED_TAG:
  270. q->ordered = flag;
  271. break;
  272. case QUEUE_ORDERED_FLUSH:
  273. q->ordered = flag;
  274. if (!q->flush_rq)
  275. q->flush_rq = kmem_cache_alloc(request_cachep,
  276. GFP_KERNEL);
  277. break;
  278. default:
  279. printk("blk_queue_ordered: bad value %d\n", flag);
  280. break;
  281. }
  282. }
  283. EXPORT_SYMBOL(blk_queue_ordered);
  284. /**
  285. * blk_queue_issue_flush_fn - set function for issuing a flush
  286. * @q: the request queue
  287. * @iff: the function to be called issuing the flush
  288. *
  289. * Description:
  290. * If a driver supports issuing a flush command, the support is notified
  291. * to the block layer by defining it through this call.
  292. *
  293. **/
  294. void blk_queue_issue_flush_fn(request_queue_t *q, issue_flush_fn *iff)
  295. {
  296. q->issue_flush_fn = iff;
  297. }
  298. EXPORT_SYMBOL(blk_queue_issue_flush_fn);
  299. /*
  300. * Cache flushing for ordered writes handling
  301. */
  302. static void blk_pre_flush_end_io(struct request *flush_rq)
  303. {
  304. struct request *rq = flush_rq->end_io_data;
  305. request_queue_t *q = rq->q;
  306. rq->flags |= REQ_BAR_PREFLUSH;
  307. if (!flush_rq->errors)
  308. elv_requeue_request(q, rq);
  309. else {
  310. q->end_flush_fn(q, flush_rq);
  311. clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags);
  312. q->request_fn(q);
  313. }
  314. }
  315. static void blk_post_flush_end_io(struct request *flush_rq)
  316. {
  317. struct request *rq = flush_rq->end_io_data;
  318. request_queue_t *q = rq->q;
  319. rq->flags |= REQ_BAR_POSTFLUSH;
  320. q->end_flush_fn(q, flush_rq);
  321. clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags);
  322. q->request_fn(q);
  323. }
  324. struct request *blk_start_pre_flush(request_queue_t *q, struct request *rq)
  325. {
  326. struct request *flush_rq = q->flush_rq;
  327. BUG_ON(!blk_barrier_rq(rq));
  328. if (test_and_set_bit(QUEUE_FLAG_FLUSH, &q->queue_flags))
  329. return NULL;
  330. rq_init(q, flush_rq);
  331. flush_rq->elevator_private = NULL;
  332. flush_rq->flags = REQ_BAR_FLUSH;
  333. flush_rq->rq_disk = rq->rq_disk;
  334. flush_rq->rl = NULL;
  335. /*
  336. * prepare_flush returns 0 if no flush is needed, just mark both
  337. * pre and post flush as done in that case
  338. */
  339. if (!q->prepare_flush_fn(q, flush_rq)) {
  340. rq->flags |= REQ_BAR_PREFLUSH | REQ_BAR_POSTFLUSH;
  341. clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags);
  342. return rq;
  343. }
  344. /*
  345. * some drivers dequeue requests right away, some only after io
  346. * completion. make sure the request is dequeued.
  347. */
  348. if (!list_empty(&rq->queuelist))
  349. blkdev_dequeue_request(rq);
  350. elv_deactivate_request(q, rq);
  351. flush_rq->end_io_data = rq;
  352. flush_rq->end_io = blk_pre_flush_end_io;
  353. __elv_add_request(q, flush_rq, ELEVATOR_INSERT_FRONT, 0);
  354. return flush_rq;
  355. }
  356. static void blk_start_post_flush(request_queue_t *q, struct request *rq)
  357. {
  358. struct request *flush_rq = q->flush_rq;
  359. BUG_ON(!blk_barrier_rq(rq));
  360. rq_init(q, flush_rq);
  361. flush_rq->elevator_private = NULL;
  362. flush_rq->flags = REQ_BAR_FLUSH;
  363. flush_rq->rq_disk = rq->rq_disk;
  364. flush_rq->rl = NULL;
  365. if (q->prepare_flush_fn(q, flush_rq)) {
  366. flush_rq->end_io_data = rq;
  367. flush_rq->end_io = blk_post_flush_end_io;
  368. __elv_add_request(q, flush_rq, ELEVATOR_INSERT_FRONT, 0);
  369. q->request_fn(q);
  370. }
  371. }
  372. static inline int blk_check_end_barrier(request_queue_t *q, struct request *rq,
  373. int sectors)
  374. {
  375. if (sectors > rq->nr_sectors)
  376. sectors = rq->nr_sectors;
  377. rq->nr_sectors -= sectors;
  378. return rq->nr_sectors;
  379. }
  380. static int __blk_complete_barrier_rq(request_queue_t *q, struct request *rq,
  381. int sectors, int queue_locked)
  382. {
  383. if (q->ordered != QUEUE_ORDERED_FLUSH)
  384. return 0;
  385. if (!blk_fs_request(rq) || !blk_barrier_rq(rq))
  386. return 0;
  387. if (blk_barrier_postflush(rq))
  388. return 0;
  389. if (!blk_check_end_barrier(q, rq, sectors)) {
  390. unsigned long flags = 0;
  391. if (!queue_locked)
  392. spin_lock_irqsave(q->queue_lock, flags);
  393. blk_start_post_flush(q, rq);
  394. if (!queue_locked)
  395. spin_unlock_irqrestore(q->queue_lock, flags);
  396. }
  397. return 1;
  398. }
  399. /**
  400. * blk_complete_barrier_rq - complete possible barrier request
  401. * @q: the request queue for the device
  402. * @rq: the request
  403. * @sectors: number of sectors to complete
  404. *
  405. * Description:
  406. * Used in driver end_io handling to determine whether to postpone
  407. * completion of a barrier request until a post flush has been done. This
  408. * is the unlocked variant, used if the caller doesn't already hold the
  409. * queue lock.
  410. **/
  411. int blk_complete_barrier_rq(request_queue_t *q, struct request *rq, int sectors)
  412. {
  413. return __blk_complete_barrier_rq(q, rq, sectors, 0);
  414. }
  415. EXPORT_SYMBOL(blk_complete_barrier_rq);
  416. /**
  417. * blk_complete_barrier_rq_locked - complete possible barrier request
  418. * @q: the request queue for the device
  419. * @rq: the request
  420. * @sectors: number of sectors to complete
  421. *
  422. * Description:
  423. * See blk_complete_barrier_rq(). This variant must be used if the caller
  424. * holds the queue lock.
  425. **/
  426. int blk_complete_barrier_rq_locked(request_queue_t *q, struct request *rq,
  427. int sectors)
  428. {
  429. return __blk_complete_barrier_rq(q, rq, sectors, 1);
  430. }
  431. EXPORT_SYMBOL(blk_complete_barrier_rq_locked);
  432. /**
  433. * blk_queue_bounce_limit - set bounce buffer limit for queue
  434. * @q: the request queue for the device
  435. * @dma_addr: bus address limit
  436. *
  437. * Description:
  438. * Different hardware can have different requirements as to what pages
  439. * it can do I/O directly to. A low level driver can call
  440. * blk_queue_bounce_limit to have lower memory pages allocated as bounce
  441. * buffers for doing I/O to pages residing above @page. By default
  442. * the block layer sets this to the highest numbered "low" memory page.
  443. **/
  444. void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
  445. {
  446. unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
  447. /*
  448. * set appropriate bounce gfp mask -- unfortunately we don't have a
  449. * full 4GB zone, so we have to resort to low memory for any bounces.
  450. * ISA has its own < 16MB zone.
  451. */
  452. if (bounce_pfn < blk_max_low_pfn) {
  453. BUG_ON(dma_addr < BLK_BOUNCE_ISA);
  454. init_emergency_isa_pool();
  455. q->bounce_gfp = GFP_NOIO | GFP_DMA;
  456. } else
  457. q->bounce_gfp = GFP_NOIO;
  458. q->bounce_pfn = bounce_pfn;
  459. }
  460. EXPORT_SYMBOL(blk_queue_bounce_limit);
  461. /**
  462. * blk_queue_max_sectors - set max sectors for a request for this queue
  463. * @q: the request queue for the device
  464. * @max_sectors: max sectors in the usual 512b unit
  465. *
  466. * Description:
  467. * Enables a low level driver to set an upper limit on the size of
  468. * received requests.
  469. **/
  470. void blk_queue_max_sectors(request_queue_t *q, unsigned short max_sectors)
  471. {
  472. if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
  473. max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
  474. printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
  475. }
  476. q->max_sectors = q->max_hw_sectors = max_sectors;
  477. }
  478. EXPORT_SYMBOL(blk_queue_max_sectors);
  479. /**
  480. * blk_queue_max_phys_segments - set max phys segments for a request for this queue
  481. * @q: the request queue for the device
  482. * @max_segments: max number of segments
  483. *
  484. * Description:
  485. * Enables a low level driver to set an upper limit on the number of
  486. * physical data segments in a request. This would be the largest sized
  487. * scatter list the driver could handle.
  488. **/
  489. void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
  490. {
  491. if (!max_segments) {
  492. max_segments = 1;
  493. printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
  494. }
  495. q->max_phys_segments = max_segments;
  496. }
  497. EXPORT_SYMBOL(blk_queue_max_phys_segments);
  498. /**
  499. * blk_queue_max_hw_segments - set max hw segments for a request for this queue
  500. * @q: the request queue for the device
  501. * @max_segments: max number of segments
  502. *
  503. * Description:
  504. * Enables a low level driver to set an upper limit on the number of
  505. * hw data segments in a request. This would be the largest number of
  506. * address/length pairs the host adapter can actually give as once
  507. * to the device.
  508. **/
  509. void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments)
  510. {
  511. if (!max_segments) {
  512. max_segments = 1;
  513. printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
  514. }
  515. q->max_hw_segments = max_segments;
  516. }
  517. EXPORT_SYMBOL(blk_queue_max_hw_segments);
  518. /**
  519. * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
  520. * @q: the request queue for the device
  521. * @max_size: max size of segment in bytes
  522. *
  523. * Description:
  524. * Enables a low level driver to set an upper limit on the size of a
  525. * coalesced segment
  526. **/
  527. void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size)
  528. {
  529. if (max_size < PAGE_CACHE_SIZE) {
  530. max_size = PAGE_CACHE_SIZE;
  531. printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
  532. }
  533. q->max_segment_size = max_size;
  534. }
  535. EXPORT_SYMBOL(blk_queue_max_segment_size);
  536. /**
  537. * blk_queue_hardsect_size - set hardware sector size for the queue
  538. * @q: the request queue for the device
  539. * @size: the hardware sector size, in bytes
  540. *
  541. * Description:
  542. * This should typically be set to the lowest possible sector size
  543. * that the hardware can operate on (possible without reverting to
  544. * even internal read-modify-write operations). Usually the default
  545. * of 512 covers most hardware.
  546. **/
  547. void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
  548. {
  549. q->hardsect_size = size;
  550. }
  551. EXPORT_SYMBOL(blk_queue_hardsect_size);
  552. /*
  553. * Returns the minimum that is _not_ zero, unless both are zero.
  554. */
  555. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  556. /**
  557. * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
  558. * @t: the stacking driver (top)
  559. * @b: the underlying device (bottom)
  560. **/
  561. void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b)
  562. {
  563. /* zero is "infinity" */
  564. t->max_sectors = t->max_hw_sectors =
  565. min_not_zero(t->max_sectors,b->max_sectors);
  566. t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
  567. t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
  568. t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
  569. t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
  570. }
  571. EXPORT_SYMBOL(blk_queue_stack_limits);
  572. /**
  573. * blk_queue_segment_boundary - set boundary rules for segment merging
  574. * @q: the request queue for the device
  575. * @mask: the memory boundary mask
  576. **/
  577. void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask)
  578. {
  579. if (mask < PAGE_CACHE_SIZE - 1) {
  580. mask = PAGE_CACHE_SIZE - 1;
  581. printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
  582. }
  583. q->seg_boundary_mask = mask;
  584. }
  585. EXPORT_SYMBOL(blk_queue_segment_boundary);
  586. /**
  587. * blk_queue_dma_alignment - set dma length and memory alignment
  588. * @q: the request queue for the device
  589. * @mask: alignment mask
  590. *
  591. * description:
  592. * set required memory and length aligment for direct dma transactions.
  593. * this is used when buiding direct io requests for the queue.
  594. *
  595. **/
  596. void blk_queue_dma_alignment(request_queue_t *q, int mask)
  597. {
  598. q->dma_alignment = mask;
  599. }
  600. EXPORT_SYMBOL(blk_queue_dma_alignment);
  601. /**
  602. * blk_queue_find_tag - find a request by its tag and queue
  603. *
  604. * @q: The request queue for the device
  605. * @tag: The tag of the request
  606. *
  607. * Notes:
  608. * Should be used when a device returns a tag and you want to match
  609. * it with a request.
  610. *
  611. * no locks need be held.
  612. **/
  613. struct request *blk_queue_find_tag(request_queue_t *q, int tag)
  614. {
  615. struct blk_queue_tag *bqt = q->queue_tags;
  616. if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
  617. return NULL;
  618. return bqt->tag_index[tag];
  619. }
  620. EXPORT_SYMBOL(blk_queue_find_tag);
  621. /**
  622. * __blk_queue_free_tags - release tag maintenance info
  623. * @q: the request queue for the device
  624. *
  625. * Notes:
  626. * blk_cleanup_queue() will take care of calling this function, if tagging
  627. * has been used. So there's no need to call this directly.
  628. **/
  629. static void __blk_queue_free_tags(request_queue_t *q)
  630. {
  631. struct blk_queue_tag *bqt = q->queue_tags;
  632. if (!bqt)
  633. return;
  634. if (atomic_dec_and_test(&bqt->refcnt)) {
  635. BUG_ON(bqt->busy);
  636. BUG_ON(!list_empty(&bqt->busy_list));
  637. kfree(bqt->tag_index);
  638. bqt->tag_index = NULL;
  639. kfree(bqt->tag_map);
  640. bqt->tag_map = NULL;
  641. kfree(bqt);
  642. }
  643. q->queue_tags = NULL;
  644. q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED);
  645. }
  646. /**
  647. * blk_queue_free_tags - release tag maintenance info
  648. * @q: the request queue for the device
  649. *
  650. * Notes:
  651. * This is used to disabled tagged queuing to a device, yet leave
  652. * queue in function.
  653. **/
  654. void blk_queue_free_tags(request_queue_t *q)
  655. {
  656. clear_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
  657. }
  658. EXPORT_SYMBOL(blk_queue_free_tags);
  659. static int
  660. init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
  661. {
  662. int bits, i;
  663. struct request **tag_index;
  664. unsigned long *tag_map;
  665. if (depth > q->nr_requests * 2) {
  666. depth = q->nr_requests * 2;
  667. printk(KERN_ERR "%s: adjusted depth to %d\n",
  668. __FUNCTION__, depth);
  669. }
  670. tag_index = kmalloc(depth * sizeof(struct request *), GFP_ATOMIC);
  671. if (!tag_index)
  672. goto fail;
  673. bits = (depth / BLK_TAGS_PER_LONG) + 1;
  674. tag_map = kmalloc(bits * sizeof(unsigned long), GFP_ATOMIC);
  675. if (!tag_map)
  676. goto fail;
  677. memset(tag_index, 0, depth * sizeof(struct request *));
  678. memset(tag_map, 0, bits * sizeof(unsigned long));
  679. tags->max_depth = depth;
  680. tags->real_max_depth = bits * BITS_PER_LONG;
  681. tags->tag_index = tag_index;
  682. tags->tag_map = tag_map;
  683. /*
  684. * set the upper bits if the depth isn't a multiple of the word size
  685. */
  686. for (i = depth; i < bits * BLK_TAGS_PER_LONG; i++)
  687. __set_bit(i, tag_map);
  688. return 0;
  689. fail:
  690. kfree(tag_index);
  691. return -ENOMEM;
  692. }
  693. /**
  694. * blk_queue_init_tags - initialize the queue tag info
  695. * @q: the request queue for the device
  696. * @depth: the maximum queue depth supported
  697. * @tags: the tag to use
  698. **/
  699. int blk_queue_init_tags(request_queue_t *q, int depth,
  700. struct blk_queue_tag *tags)
  701. {
  702. int rc;
  703. BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
  704. if (!tags && !q->queue_tags) {
  705. tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
  706. if (!tags)
  707. goto fail;
  708. if (init_tag_map(q, tags, depth))
  709. goto fail;
  710. INIT_LIST_HEAD(&tags->busy_list);
  711. tags->busy = 0;
  712. atomic_set(&tags->refcnt, 1);
  713. } else if (q->queue_tags) {
  714. if ((rc = blk_queue_resize_tags(q, depth)))
  715. return rc;
  716. set_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
  717. return 0;
  718. } else
  719. atomic_inc(&tags->refcnt);
  720. /*
  721. * assign it, all done
  722. */
  723. q->queue_tags = tags;
  724. q->queue_flags |= (1 << QUEUE_FLAG_QUEUED);
  725. return 0;
  726. fail:
  727. kfree(tags);
  728. return -ENOMEM;
  729. }
  730. EXPORT_SYMBOL(blk_queue_init_tags);
  731. /**
  732. * blk_queue_resize_tags - change the queueing depth
  733. * @q: the request queue for the device
  734. * @new_depth: the new max command queueing depth
  735. *
  736. * Notes:
  737. * Must be called with the queue lock held.
  738. **/
  739. int blk_queue_resize_tags(request_queue_t *q, int new_depth)
  740. {
  741. struct blk_queue_tag *bqt = q->queue_tags;
  742. struct request **tag_index;
  743. unsigned long *tag_map;
  744. int bits, max_depth;
  745. if (!bqt)
  746. return -ENXIO;
  747. /*
  748. * don't bother sizing down
  749. */
  750. if (new_depth <= bqt->real_max_depth) {
  751. bqt->max_depth = new_depth;
  752. return 0;
  753. }
  754. /*
  755. * save the old state info, so we can copy it back
  756. */
  757. tag_index = bqt->tag_index;
  758. tag_map = bqt->tag_map;
  759. max_depth = bqt->real_max_depth;
  760. if (init_tag_map(q, bqt, new_depth))
  761. return -ENOMEM;
  762. memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
  763. bits = max_depth / BLK_TAGS_PER_LONG;
  764. memcpy(bqt->tag_map, tag_map, bits * sizeof(unsigned long));
  765. kfree(tag_index);
  766. kfree(tag_map);
  767. return 0;
  768. }
  769. EXPORT_SYMBOL(blk_queue_resize_tags);
  770. /**
  771. * blk_queue_end_tag - end tag operations for a request
  772. * @q: the request queue for the device
  773. * @rq: the request that has completed
  774. *
  775. * Description:
  776. * Typically called when end_that_request_first() returns 0, meaning
  777. * all transfers have been done for a request. It's important to call
  778. * this function before end_that_request_last(), as that will put the
  779. * request back on the free list thus corrupting the internal tag list.
  780. *
  781. * Notes:
  782. * queue lock must be held.
  783. **/
  784. void blk_queue_end_tag(request_queue_t *q, struct request *rq)
  785. {
  786. struct blk_queue_tag *bqt = q->queue_tags;
  787. int tag = rq->tag;
  788. BUG_ON(tag == -1);
  789. if (unlikely(tag >= bqt->real_max_depth))
  790. return;
  791. if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) {
  792. printk("attempt to clear non-busy tag (%d)\n", tag);
  793. return;
  794. }
  795. list_del_init(&rq->queuelist);
  796. rq->flags &= ~REQ_QUEUED;
  797. rq->tag = -1;
  798. if (unlikely(bqt->tag_index[tag] == NULL))
  799. printk("tag %d is missing\n", tag);
  800. bqt->tag_index[tag] = NULL;
  801. bqt->busy--;
  802. }
  803. EXPORT_SYMBOL(blk_queue_end_tag);
  804. /**
  805. * blk_queue_start_tag - find a free tag and assign it
  806. * @q: the request queue for the device
  807. * @rq: the block request that needs tagging
  808. *
  809. * Description:
  810. * This can either be used as a stand-alone helper, or possibly be
  811. * assigned as the queue &prep_rq_fn (in which case &struct request
  812. * automagically gets a tag assigned). Note that this function
  813. * assumes that any type of request can be queued! if this is not
  814. * true for your device, you must check the request type before
  815. * calling this function. The request will also be removed from
  816. * the request queue, so it's the drivers responsibility to readd
  817. * it if it should need to be restarted for some reason.
  818. *
  819. * Notes:
  820. * queue lock must be held.
  821. **/
  822. int blk_queue_start_tag(request_queue_t *q, struct request *rq)
  823. {
  824. struct blk_queue_tag *bqt = q->queue_tags;
  825. unsigned long *map = bqt->tag_map;
  826. int tag = 0;
  827. if (unlikely((rq->flags & REQ_QUEUED))) {
  828. printk(KERN_ERR
  829. "request %p for device [%s] already tagged %d",
  830. rq, rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
  831. BUG();
  832. }
  833. for (map = bqt->tag_map; *map == -1UL; map++) {
  834. tag += BLK_TAGS_PER_LONG;
  835. if (tag >= bqt->max_depth)
  836. return 1;
  837. }
  838. tag += ffz(*map);
  839. __set_bit(tag, bqt->tag_map);
  840. rq->flags |= REQ_QUEUED;
  841. rq->tag = tag;
  842. bqt->tag_index[tag] = rq;
  843. blkdev_dequeue_request(rq);
  844. list_add(&rq->queuelist, &bqt->busy_list);
  845. bqt->busy++;
  846. return 0;
  847. }
  848. EXPORT_SYMBOL(blk_queue_start_tag);
  849. /**
  850. * blk_queue_invalidate_tags - invalidate all pending tags
  851. * @q: the request queue for the device
  852. *
  853. * Description:
  854. * Hardware conditions may dictate a need to stop all pending requests.
  855. * In this case, we will safely clear the block side of the tag queue and
  856. * readd all requests to the request queue in the right order.
  857. *
  858. * Notes:
  859. * queue lock must be held.
  860. **/
  861. void blk_queue_invalidate_tags(request_queue_t *q)
  862. {
  863. struct blk_queue_tag *bqt = q->queue_tags;
  864. struct list_head *tmp, *n;
  865. struct request *rq;
  866. list_for_each_safe(tmp, n, &bqt->busy_list) {
  867. rq = list_entry_rq(tmp);
  868. if (rq->tag == -1) {
  869. printk("bad tag found on list\n");
  870. list_del_init(&rq->queuelist);
  871. rq->flags &= ~REQ_QUEUED;
  872. } else
  873. blk_queue_end_tag(q, rq);
  874. rq->flags &= ~REQ_STARTED;
  875. __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
  876. }
  877. }
  878. EXPORT_SYMBOL(blk_queue_invalidate_tags);
  879. static char *rq_flags[] = {
  880. "REQ_RW",
  881. "REQ_FAILFAST",
  882. "REQ_SOFTBARRIER",
  883. "REQ_HARDBARRIER",
  884. "REQ_CMD",
  885. "REQ_NOMERGE",
  886. "REQ_STARTED",
  887. "REQ_DONTPREP",
  888. "REQ_QUEUED",
  889. "REQ_PC",
  890. "REQ_BLOCK_PC",
  891. "REQ_SENSE",
  892. "REQ_FAILED",
  893. "REQ_QUIET",
  894. "REQ_SPECIAL",
  895. "REQ_DRIVE_CMD",
  896. "REQ_DRIVE_TASK",
  897. "REQ_DRIVE_TASKFILE",
  898. "REQ_PREEMPT",
  899. "REQ_PM_SUSPEND",
  900. "REQ_PM_RESUME",
  901. "REQ_PM_SHUTDOWN",
  902. };
  903. void blk_dump_rq_flags(struct request *rq, char *msg)
  904. {
  905. int bit;
  906. printk("%s: dev %s: flags = ", msg,
  907. rq->rq_disk ? rq->rq_disk->disk_name : "?");
  908. bit = 0;
  909. do {
  910. if (rq->flags & (1 << bit))
  911. printk("%s ", rq_flags[bit]);
  912. bit++;
  913. } while (bit < __REQ_NR_BITS);
  914. printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
  915. rq->nr_sectors,
  916. rq->current_nr_sectors);
  917. printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
  918. if (rq->flags & (REQ_BLOCK_PC | REQ_PC)) {
  919. printk("cdb: ");
  920. for (bit = 0; bit < sizeof(rq->cmd); bit++)
  921. printk("%02x ", rq->cmd[bit]);
  922. printk("\n");
  923. }
  924. }
  925. EXPORT_SYMBOL(blk_dump_rq_flags);
  926. void blk_recount_segments(request_queue_t *q, struct bio *bio)
  927. {
  928. struct bio_vec *bv, *bvprv = NULL;
  929. int i, nr_phys_segs, nr_hw_segs, seg_size, hw_seg_size, cluster;
  930. int high, highprv = 1;
  931. if (unlikely(!bio->bi_io_vec))
  932. return;
  933. cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
  934. hw_seg_size = seg_size = nr_phys_segs = nr_hw_segs = 0;
  935. bio_for_each_segment(bv, bio, i) {
  936. /*
  937. * the trick here is making sure that a high page is never
  938. * considered part of another segment, since that might
  939. * change with the bounce page.
  940. */
  941. high = page_to_pfn(bv->bv_page) >= q->bounce_pfn;
  942. if (high || highprv)
  943. goto new_hw_segment;
  944. if (cluster) {
  945. if (seg_size + bv->bv_len > q->max_segment_size)
  946. goto new_segment;
  947. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
  948. goto new_segment;
  949. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
  950. goto new_segment;
  951. if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
  952. goto new_hw_segment;
  953. seg_size += bv->bv_len;
  954. hw_seg_size += bv->bv_len;
  955. bvprv = bv;
  956. continue;
  957. }
  958. new_segment:
  959. if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
  960. !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) {
  961. hw_seg_size += bv->bv_len;
  962. } else {
  963. new_hw_segment:
  964. if (hw_seg_size > bio->bi_hw_front_size)
  965. bio->bi_hw_front_size = hw_seg_size;
  966. hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
  967. nr_hw_segs++;
  968. }
  969. nr_phys_segs++;
  970. bvprv = bv;
  971. seg_size = bv->bv_len;
  972. highprv = high;
  973. }
  974. if (hw_seg_size > bio->bi_hw_back_size)
  975. bio->bi_hw_back_size = hw_seg_size;
  976. if (nr_hw_segs == 1 && hw_seg_size > bio->bi_hw_front_size)
  977. bio->bi_hw_front_size = hw_seg_size;
  978. bio->bi_phys_segments = nr_phys_segs;
  979. bio->bi_hw_segments = nr_hw_segs;
  980. bio->bi_flags |= (1 << BIO_SEG_VALID);
  981. }
  982. int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
  983. struct bio *nxt)
  984. {
  985. if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
  986. return 0;
  987. if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
  988. return 0;
  989. if (bio->bi_size + nxt->bi_size > q->max_segment_size)
  990. return 0;
  991. /*
  992. * bio and nxt are contigous in memory, check if the queue allows
  993. * these two to be merged into one
  994. */
  995. if (BIO_SEG_BOUNDARY(q, bio, nxt))
  996. return 1;
  997. return 0;
  998. }
  999. EXPORT_SYMBOL(blk_phys_contig_segment);
  1000. int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
  1001. struct bio *nxt)
  1002. {
  1003. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  1004. blk_recount_segments(q, bio);
  1005. if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
  1006. blk_recount_segments(q, nxt);
  1007. if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
  1008. BIOVEC_VIRT_OVERSIZE(bio->bi_hw_front_size + bio->bi_hw_back_size))
  1009. return 0;
  1010. if (bio->bi_size + nxt->bi_size > q->max_segment_size)
  1011. return 0;
  1012. return 1;
  1013. }
  1014. EXPORT_SYMBOL(blk_hw_contig_segment);
  1015. /*
  1016. * map a request to scatterlist, return number of sg entries setup. Caller
  1017. * must make sure sg can hold rq->nr_phys_segments entries
  1018. */
  1019. int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
  1020. {
  1021. struct bio_vec *bvec, *bvprv;
  1022. struct bio *bio;
  1023. int nsegs, i, cluster;
  1024. nsegs = 0;
  1025. cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
  1026. /*
  1027. * for each bio in rq
  1028. */
  1029. bvprv = NULL;
  1030. rq_for_each_bio(bio, rq) {
  1031. /*
  1032. * for each segment in bio
  1033. */
  1034. bio_for_each_segment(bvec, bio, i) {
  1035. int nbytes = bvec->bv_len;
  1036. if (bvprv && cluster) {
  1037. if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
  1038. goto new_segment;
  1039. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
  1040. goto new_segment;
  1041. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
  1042. goto new_segment;
  1043. sg[nsegs - 1].length += nbytes;
  1044. } else {
  1045. new_segment:
  1046. memset(&sg[nsegs],0,sizeof(struct scatterlist));
  1047. sg[nsegs].page = bvec->bv_page;
  1048. sg[nsegs].length = nbytes;
  1049. sg[nsegs].offset = bvec->bv_offset;
  1050. nsegs++;
  1051. }
  1052. bvprv = bvec;
  1053. } /* segments in bio */
  1054. } /* bios in rq */
  1055. return nsegs;
  1056. }
  1057. EXPORT_SYMBOL(blk_rq_map_sg);
  1058. /*
  1059. * the standard queue merge functions, can be overridden with device
  1060. * specific ones if so desired
  1061. */
  1062. static inline int ll_new_mergeable(request_queue_t *q,
  1063. struct request *req,
  1064. struct bio *bio)
  1065. {
  1066. int nr_phys_segs = bio_phys_segments(q, bio);
  1067. if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
  1068. req->flags |= REQ_NOMERGE;
  1069. if (req == q->last_merge)
  1070. q->last_merge = NULL;
  1071. return 0;
  1072. }
  1073. /*
  1074. * A hw segment is just getting larger, bump just the phys
  1075. * counter.
  1076. */
  1077. req->nr_phys_segments += nr_phys_segs;
  1078. return 1;
  1079. }
  1080. static inline int ll_new_hw_segment(request_queue_t *q,
  1081. struct request *req,
  1082. struct bio *bio)
  1083. {
  1084. int nr_hw_segs = bio_hw_segments(q, bio);
  1085. int nr_phys_segs = bio_phys_segments(q, bio);
  1086. if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
  1087. || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
  1088. req->flags |= REQ_NOMERGE;
  1089. if (req == q->last_merge)
  1090. q->last_merge = NULL;
  1091. return 0;
  1092. }
  1093. /*
  1094. * This will form the start of a new hw segment. Bump both
  1095. * counters.
  1096. */
  1097. req->nr_hw_segments += nr_hw_segs;
  1098. req->nr_phys_segments += nr_phys_segs;
  1099. return 1;
  1100. }
  1101. static int ll_back_merge_fn(request_queue_t *q, struct request *req,
  1102. struct bio *bio)
  1103. {
  1104. int len;
  1105. if (req->nr_sectors + bio_sectors(bio) > q->max_sectors) {
  1106. req->flags |= REQ_NOMERGE;
  1107. if (req == q->last_merge)
  1108. q->last_merge = NULL;
  1109. return 0;
  1110. }
  1111. if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
  1112. blk_recount_segments(q, req->biotail);
  1113. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  1114. blk_recount_segments(q, bio);
  1115. len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
  1116. if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
  1117. !BIOVEC_VIRT_OVERSIZE(len)) {
  1118. int mergeable = ll_new_mergeable(q, req, bio);
  1119. if (mergeable) {
  1120. if (req->nr_hw_segments == 1)
  1121. req->bio->bi_hw_front_size = len;
  1122. if (bio->bi_hw_segments == 1)
  1123. bio->bi_hw_back_size = len;
  1124. }
  1125. return mergeable;
  1126. }
  1127. return ll_new_hw_segment(q, req, bio);
  1128. }
  1129. static int ll_front_merge_fn(request_queue_t *q, struct request *req,
  1130. struct bio *bio)
  1131. {
  1132. int len;
  1133. if (req->nr_sectors + bio_sectors(bio) > q->max_sectors) {
  1134. req->flags |= REQ_NOMERGE;
  1135. if (req == q->last_merge)
  1136. q->last_merge = NULL;
  1137. return 0;
  1138. }
  1139. len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
  1140. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  1141. blk_recount_segments(q, bio);
  1142. if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
  1143. blk_recount_segments(q, req->bio);
  1144. if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
  1145. !BIOVEC_VIRT_OVERSIZE(len)) {
  1146. int mergeable = ll_new_mergeable(q, req, bio);
  1147. if (mergeable) {
  1148. if (bio->bi_hw_segments == 1)
  1149. bio->bi_hw_front_size = len;
  1150. if (req->nr_hw_segments == 1)
  1151. req->biotail->bi_hw_back_size = len;
  1152. }
  1153. return mergeable;
  1154. }
  1155. return ll_new_hw_segment(q, req, bio);
  1156. }
  1157. static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
  1158. struct request *next)
  1159. {
  1160. int total_phys_segments = req->nr_phys_segments +next->nr_phys_segments;
  1161. int total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
  1162. /*
  1163. * First check if the either of the requests are re-queued
  1164. * requests. Can't merge them if they are.
  1165. */
  1166. if (req->special || next->special)
  1167. return 0;
  1168. /*
  1169. * Will it become to large?
  1170. */
  1171. if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
  1172. return 0;
  1173. total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
  1174. if (blk_phys_contig_segment(q, req->biotail, next->bio))
  1175. total_phys_segments--;
  1176. if (total_phys_segments > q->max_phys_segments)
  1177. return 0;
  1178. total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
  1179. if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
  1180. int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
  1181. /*
  1182. * propagate the combined length to the end of the requests
  1183. */
  1184. if (req->nr_hw_segments == 1)
  1185. req->bio->bi_hw_front_size = len;
  1186. if (next->nr_hw_segments == 1)
  1187. next->biotail->bi_hw_back_size = len;
  1188. total_hw_segments--;
  1189. }
  1190. if (total_hw_segments > q->max_hw_segments)
  1191. return 0;
  1192. /* Merge is OK... */
  1193. req->nr_phys_segments = total_phys_segments;
  1194. req->nr_hw_segments = total_hw_segments;
  1195. return 1;
  1196. }
  1197. /*
  1198. * "plug" the device if there are no outstanding requests: this will
  1199. * force the transfer to start only after we have put all the requests
  1200. * on the list.
  1201. *
  1202. * This is called with interrupts off and no requests on the queue and
  1203. * with the queue lock held.
  1204. */
  1205. void blk_plug_device(request_queue_t *q)
  1206. {
  1207. WARN_ON(!irqs_disabled());
  1208. /*
  1209. * don't plug a stopped queue, it must be paired with blk_start_queue()
  1210. * which will restart the queueing
  1211. */
  1212. if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
  1213. return;
  1214. if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
  1215. mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
  1216. }
  1217. EXPORT_SYMBOL(blk_plug_device);
  1218. /*
  1219. * remove the queue from the plugged list, if present. called with
  1220. * queue lock held and interrupts disabled.
  1221. */
  1222. int blk_remove_plug(request_queue_t *q)
  1223. {
  1224. WARN_ON(!irqs_disabled());
  1225. if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
  1226. return 0;
  1227. del_timer(&q->unplug_timer);
  1228. return 1;
  1229. }
  1230. EXPORT_SYMBOL(blk_remove_plug);
  1231. /*
  1232. * remove the plug and let it rip..
  1233. */
  1234. void __generic_unplug_device(request_queue_t *q)
  1235. {
  1236. if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
  1237. return;
  1238. if (!blk_remove_plug(q))
  1239. return;
  1240. /*
  1241. * was plugged, fire request_fn if queue has stuff to do
  1242. */
  1243. if (elv_next_request(q))
  1244. q->request_fn(q);
  1245. }
  1246. EXPORT_SYMBOL(__generic_unplug_device);
  1247. /**
  1248. * generic_unplug_device - fire a request queue
  1249. * @q: The &request_queue_t in question
  1250. *
  1251. * Description:
  1252. * Linux uses plugging to build bigger requests queues before letting
  1253. * the device have at them. If a queue is plugged, the I/O scheduler
  1254. * is still adding and merging requests on the queue. Once the queue
  1255. * gets unplugged, the request_fn defined for the queue is invoked and
  1256. * transfers started.
  1257. **/
  1258. void generic_unplug_device(request_queue_t *q)
  1259. {
  1260. spin_lock_irq(q->queue_lock);
  1261. __generic_unplug_device(q);
  1262. spin_unlock_irq(q->queue_lock);
  1263. }
  1264. EXPORT_SYMBOL(generic_unplug_device);
  1265. static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
  1266. struct page *page)
  1267. {
  1268. request_queue_t *q = bdi->unplug_io_data;
  1269. /*
  1270. * devices don't necessarily have an ->unplug_fn defined
  1271. */
  1272. if (q->unplug_fn)
  1273. q->unplug_fn(q);
  1274. }
  1275. static void blk_unplug_work(void *data)
  1276. {
  1277. request_queue_t *q = data;
  1278. q->unplug_fn(q);
  1279. }
  1280. static void blk_unplug_timeout(unsigned long data)
  1281. {
  1282. request_queue_t *q = (request_queue_t *)data;
  1283. kblockd_schedule_work(&q->unplug_work);
  1284. }
  1285. /**
  1286. * blk_start_queue - restart a previously stopped queue
  1287. * @q: The &request_queue_t in question
  1288. *
  1289. * Description:
  1290. * blk_start_queue() will clear the stop flag on the queue, and call
  1291. * the request_fn for the queue if it was in a stopped state when
  1292. * entered. Also see blk_stop_queue(). Queue lock must be held.
  1293. **/
  1294. void blk_start_queue(request_queue_t *q)
  1295. {
  1296. clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
  1297. /*
  1298. * one level of recursion is ok and is much faster than kicking
  1299. * the unplug handling
  1300. */
  1301. if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
  1302. q->request_fn(q);
  1303. clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
  1304. } else {
  1305. blk_plug_device(q);
  1306. kblockd_schedule_work(&q->unplug_work);
  1307. }
  1308. }
  1309. EXPORT_SYMBOL(blk_start_queue);
  1310. /**
  1311. * blk_stop_queue - stop a queue
  1312. * @q: The &request_queue_t in question
  1313. *
  1314. * Description:
  1315. * The Linux block layer assumes that a block driver will consume all
  1316. * entries on the request queue when the request_fn strategy is called.
  1317. * Often this will not happen, because of hardware limitations (queue
  1318. * depth settings). If a device driver gets a 'queue full' response,
  1319. * or if it simply chooses not to queue more I/O at one point, it can
  1320. * call this function to prevent the request_fn from being called until
  1321. * the driver has signalled it's ready to go again. This happens by calling
  1322. * blk_start_queue() to restart queue operations. Queue lock must be held.
  1323. **/
  1324. void blk_stop_queue(request_queue_t *q)
  1325. {
  1326. blk_remove_plug(q);
  1327. set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
  1328. }
  1329. EXPORT_SYMBOL(blk_stop_queue);
  1330. /**
  1331. * blk_sync_queue - cancel any pending callbacks on a queue
  1332. * @q: the queue
  1333. *
  1334. * Description:
  1335. * The block layer may perform asynchronous callback activity
  1336. * on a queue, such as calling the unplug function after a timeout.
  1337. * A block device may call blk_sync_queue to ensure that any
  1338. * such activity is cancelled, thus allowing it to release resources
  1339. * the the callbacks might use. The caller must already have made sure
  1340. * that its ->make_request_fn will not re-add plugging prior to calling
  1341. * this function.
  1342. *
  1343. */
  1344. void blk_sync_queue(struct request_queue *q)
  1345. {
  1346. del_timer_sync(&q->unplug_timer);
  1347. kblockd_flush();
  1348. }
  1349. EXPORT_SYMBOL(blk_sync_queue);
  1350. /**
  1351. * blk_run_queue - run a single device queue
  1352. * @q: The queue to run
  1353. */
  1354. void blk_run_queue(struct request_queue *q)
  1355. {
  1356. unsigned long flags;
  1357. spin_lock_irqsave(q->queue_lock, flags);
  1358. blk_remove_plug(q);
  1359. if (!elv_queue_empty(q))
  1360. q->request_fn(q);
  1361. spin_unlock_irqrestore(q->queue_lock, flags);
  1362. }
  1363. EXPORT_SYMBOL(blk_run_queue);
  1364. /**
  1365. * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
  1366. * @q: the request queue to be released
  1367. *
  1368. * Description:
  1369. * blk_cleanup_queue is the pair to blk_init_queue() or
  1370. * blk_queue_make_request(). It should be called when a request queue is
  1371. * being released; typically when a block device is being de-registered.
  1372. * Currently, its primary task it to free all the &struct request
  1373. * structures that were allocated to the queue and the queue itself.
  1374. *
  1375. * Caveat:
  1376. * Hopefully the low level driver will have finished any
  1377. * outstanding requests first...
  1378. **/
  1379. void blk_cleanup_queue(request_queue_t * q)
  1380. {
  1381. struct request_list *rl = &q->rq;
  1382. if (!atomic_dec_and_test(&q->refcnt))
  1383. return;
  1384. if (q->elevator)
  1385. elevator_exit(q->elevator);
  1386. blk_sync_queue(q);
  1387. if (rl->rq_pool)
  1388. mempool_destroy(rl->rq_pool);
  1389. if (q->queue_tags)
  1390. __blk_queue_free_tags(q);
  1391. blk_queue_ordered(q, QUEUE_ORDERED_NONE);
  1392. kmem_cache_free(requestq_cachep, q);
  1393. }
  1394. EXPORT_SYMBOL(blk_cleanup_queue);
  1395. static int blk_init_free_list(request_queue_t *q)
  1396. {
  1397. struct request_list *rl = &q->rq;
  1398. rl->count[READ] = rl->count[WRITE] = 0;
  1399. rl->starved[READ] = rl->starved[WRITE] = 0;
  1400. init_waitqueue_head(&rl->wait[READ]);
  1401. init_waitqueue_head(&rl->wait[WRITE]);
  1402. init_waitqueue_head(&rl->drain);
  1403. rl->rq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, request_cachep);
  1404. if (!rl->rq_pool)
  1405. return -ENOMEM;
  1406. return 0;
  1407. }
  1408. static int __make_request(request_queue_t *, struct bio *);
  1409. request_queue_t *blk_alloc_queue(int gfp_mask)
  1410. {
  1411. request_queue_t *q = kmem_cache_alloc(requestq_cachep, gfp_mask);
  1412. if (!q)
  1413. return NULL;
  1414. memset(q, 0, sizeof(*q));
  1415. init_timer(&q->unplug_timer);
  1416. atomic_set(&q->refcnt, 1);
  1417. q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
  1418. q->backing_dev_info.unplug_io_data = q;
  1419. return q;
  1420. }
  1421. EXPORT_SYMBOL(blk_alloc_queue);
  1422. /**
  1423. * blk_init_queue - prepare a request queue for use with a block device
  1424. * @rfn: The function to be called to process requests that have been
  1425. * placed on the queue.
  1426. * @lock: Request queue spin lock
  1427. *
  1428. * Description:
  1429. * If a block device wishes to use the standard request handling procedures,
  1430. * which sorts requests and coalesces adjacent requests, then it must
  1431. * call blk_init_queue(). The function @rfn will be called when there
  1432. * are requests on the queue that need to be processed. If the device
  1433. * supports plugging, then @rfn may not be called immediately when requests
  1434. * are available on the queue, but may be called at some time later instead.
  1435. * Plugged queues are generally unplugged when a buffer belonging to one
  1436. * of the requests on the queue is needed, or due to memory pressure.
  1437. *
  1438. * @rfn is not required, or even expected, to remove all requests off the
  1439. * queue, but only as many as it can handle at a time. If it does leave
  1440. * requests on the queue, it is responsible for arranging that the requests
  1441. * get dealt with eventually.
  1442. *
  1443. * The queue spin lock must be held while manipulating the requests on the
  1444. * request queue.
  1445. *
  1446. * Function returns a pointer to the initialized request queue, or NULL if
  1447. * it didn't succeed.
  1448. *
  1449. * Note:
  1450. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  1451. * when the block device is deactivated (such as at module unload).
  1452. **/
  1453. request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  1454. {
  1455. request_queue_t *q = blk_alloc_queue(GFP_KERNEL);
  1456. if (!q)
  1457. return NULL;
  1458. if (blk_init_free_list(q))
  1459. goto out_init;
  1460. /*
  1461. * if caller didn't supply a lock, they get per-queue locking with
  1462. * our embedded lock
  1463. */
  1464. if (!lock) {
  1465. spin_lock_init(&q->__queue_lock);
  1466. lock = &q->__queue_lock;
  1467. }
  1468. q->request_fn = rfn;
  1469. q->back_merge_fn = ll_back_merge_fn;
  1470. q->front_merge_fn = ll_front_merge_fn;
  1471. q->merge_requests_fn = ll_merge_requests_fn;
  1472. q->prep_rq_fn = NULL;
  1473. q->unplug_fn = generic_unplug_device;
  1474. q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
  1475. q->queue_lock = lock;
  1476. blk_queue_segment_boundary(q, 0xffffffff);
  1477. blk_queue_make_request(q, __make_request);
  1478. blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
  1479. blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
  1480. blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
  1481. /*
  1482. * all done
  1483. */
  1484. if (!elevator_init(q, NULL)) {
  1485. blk_queue_congestion_threshold(q);
  1486. return q;
  1487. }
  1488. blk_cleanup_queue(q);
  1489. out_init:
  1490. kmem_cache_free(requestq_cachep, q);
  1491. return NULL;
  1492. }
  1493. EXPORT_SYMBOL(blk_init_queue);
  1494. int blk_get_queue(request_queue_t *q)
  1495. {
  1496. if (!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
  1497. atomic_inc(&q->refcnt);
  1498. return 0;
  1499. }
  1500. return 1;
  1501. }
  1502. EXPORT_SYMBOL(blk_get_queue);
  1503. static inline void blk_free_request(request_queue_t *q, struct request *rq)
  1504. {
  1505. elv_put_request(q, rq);
  1506. mempool_free(rq, q->rq.rq_pool);
  1507. }
  1508. static inline struct request *blk_alloc_request(request_queue_t *q, int rw,
  1509. int gfp_mask)
  1510. {
  1511. struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
  1512. if (!rq)
  1513. return NULL;
  1514. /*
  1515. * first three bits are identical in rq->flags and bio->bi_rw,
  1516. * see bio.h and blkdev.h
  1517. */
  1518. rq->flags = rw;
  1519. if (!elv_set_request(q, rq, gfp_mask))
  1520. return rq;
  1521. mempool_free(rq, q->rq.rq_pool);
  1522. return NULL;
  1523. }
  1524. /*
  1525. * ioc_batching returns true if the ioc is a valid batching request and
  1526. * should be given priority access to a request.
  1527. */
  1528. static inline int ioc_batching(request_queue_t *q, struct io_context *ioc)
  1529. {
  1530. if (!ioc)
  1531. return 0;
  1532. /*
  1533. * Make sure the process is able to allocate at least 1 request
  1534. * even if the batch times out, otherwise we could theoretically
  1535. * lose wakeups.
  1536. */
  1537. return ioc->nr_batch_requests == q->nr_batching ||
  1538. (ioc->nr_batch_requests > 0
  1539. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  1540. }
  1541. /*
  1542. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  1543. * will cause the process to be a "batcher" on all queues in the system. This
  1544. * is the behaviour we want though - once it gets a wakeup it should be given
  1545. * a nice run.
  1546. */
  1547. void ioc_set_batching(request_queue_t *q, struct io_context *ioc)
  1548. {
  1549. if (!ioc || ioc_batching(q, ioc))
  1550. return;
  1551. ioc->nr_batch_requests = q->nr_batching;
  1552. ioc->last_waited = jiffies;
  1553. }
  1554. static void __freed_request(request_queue_t *q, int rw)
  1555. {
  1556. struct request_list *rl = &q->rq;
  1557. if (rl->count[rw] < queue_congestion_off_threshold(q))
  1558. clear_queue_congested(q, rw);
  1559. if (rl->count[rw] + 1 <= q->nr_requests) {
  1560. smp_mb();
  1561. if (waitqueue_active(&rl->wait[rw]))
  1562. wake_up(&rl->wait[rw]);
  1563. blk_clear_queue_full(q, rw);
  1564. }
  1565. }
  1566. /*
  1567. * A request has just been released. Account for it, update the full and
  1568. * congestion status, wake up any waiters. Called under q->queue_lock.
  1569. */
  1570. static void freed_request(request_queue_t *q, int rw)
  1571. {
  1572. struct request_list *rl = &q->rq;
  1573. rl->count[rw]--;
  1574. __freed_request(q, rw);
  1575. if (unlikely(rl->starved[rw ^ 1]))
  1576. __freed_request(q, rw ^ 1);
  1577. if (!rl->count[READ] && !rl->count[WRITE]) {
  1578. smp_mb();
  1579. if (unlikely(waitqueue_active(&rl->drain)))
  1580. wake_up(&rl->drain);
  1581. }
  1582. }
  1583. #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
  1584. /*
  1585. * Get a free request, queue_lock must not be held
  1586. */
  1587. static struct request *get_request(request_queue_t *q, int rw, int gfp_mask)
  1588. {
  1589. struct request *rq = NULL;
  1590. struct request_list *rl = &q->rq;
  1591. struct io_context *ioc = get_io_context(gfp_mask);
  1592. if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags)))
  1593. goto out;
  1594. spin_lock_irq(q->queue_lock);
  1595. if (rl->count[rw]+1 >= q->nr_requests) {
  1596. /*
  1597. * The queue will fill after this allocation, so set it as
  1598. * full, and mark this process as "batching". This process
  1599. * will be allowed to complete a batch of requests, others
  1600. * will be blocked.
  1601. */
  1602. if (!blk_queue_full(q, rw)) {
  1603. ioc_set_batching(q, ioc);
  1604. blk_set_queue_full(q, rw);
  1605. }
  1606. }
  1607. switch (elv_may_queue(q, rw)) {
  1608. case ELV_MQUEUE_NO:
  1609. goto rq_starved;
  1610. case ELV_MQUEUE_MAY:
  1611. break;
  1612. case ELV_MQUEUE_MUST:
  1613. goto get_rq;
  1614. }
  1615. if (blk_queue_full(q, rw) && !ioc_batching(q, ioc)) {
  1616. /*
  1617. * The queue is full and the allocating process is not a
  1618. * "batcher", and not exempted by the IO scheduler
  1619. */
  1620. spin_unlock_irq(q->queue_lock);
  1621. goto out;
  1622. }
  1623. get_rq:
  1624. rl->count[rw]++;
  1625. rl->starved[rw] = 0;
  1626. if (rl->count[rw] >= queue_congestion_on_threshold(q))
  1627. set_queue_congested(q, rw);
  1628. spin_unlock_irq(q->queue_lock);
  1629. rq = blk_alloc_request(q, rw, gfp_mask);
  1630. if (!rq) {
  1631. /*
  1632. * Allocation failed presumably due to memory. Undo anything
  1633. * we might have messed up.
  1634. *
  1635. * Allocating task should really be put onto the front of the
  1636. * wait queue, but this is pretty rare.
  1637. */
  1638. spin_lock_irq(q->queue_lock);
  1639. freed_request(q, rw);
  1640. /*
  1641. * in the very unlikely event that allocation failed and no
  1642. * requests for this direction was pending, mark us starved
  1643. * so that freeing of a request in the other direction will
  1644. * notice us. another possible fix would be to split the
  1645. * rq mempool into READ and WRITE
  1646. */
  1647. rq_starved:
  1648. if (unlikely(rl->count[rw] == 0))
  1649. rl->starved[rw] = 1;
  1650. spin_unlock_irq(q->queue_lock);
  1651. goto out;
  1652. }
  1653. if (ioc_batching(q, ioc))
  1654. ioc->nr_batch_requests--;
  1655. rq_init(q, rq);
  1656. rq->rl = rl;
  1657. out:
  1658. put_io_context(ioc);
  1659. return rq;
  1660. }
  1661. /*
  1662. * No available requests for this queue, unplug the device and wait for some
  1663. * requests to become available.
  1664. */
  1665. static struct request *get_request_wait(request_queue_t *q, int rw)
  1666. {
  1667. DEFINE_WAIT(wait);
  1668. struct request *rq;
  1669. generic_unplug_device(q);
  1670. do {
  1671. struct request_list *rl = &q->rq;
  1672. prepare_to_wait_exclusive(&rl->wait[rw], &wait,
  1673. TASK_UNINTERRUPTIBLE);
  1674. rq = get_request(q, rw, GFP_NOIO);
  1675. if (!rq) {
  1676. struct io_context *ioc;
  1677. io_schedule();
  1678. /*
  1679. * After sleeping, we become a "batching" process and
  1680. * will be able to allocate at least one request, and
  1681. * up to a big batch of them for a small period time.
  1682. * See ioc_batching, ioc_set_batching
  1683. */
  1684. ioc = get_io_context(GFP_NOIO);
  1685. ioc_set_batching(q, ioc);
  1686. put_io_context(ioc);
  1687. }
  1688. finish_wait(&rl->wait[rw], &wait);
  1689. } while (!rq);
  1690. return rq;
  1691. }
  1692. struct request *blk_get_request(request_queue_t *q, int rw, int gfp_mask)
  1693. {
  1694. struct request *rq;
  1695. BUG_ON(rw != READ && rw != WRITE);
  1696. if (gfp_mask & __GFP_WAIT)
  1697. rq = get_request_wait(q, rw);
  1698. else
  1699. rq = get_request(q, rw, gfp_mask);
  1700. return rq;
  1701. }
  1702. EXPORT_SYMBOL(blk_get_request);
  1703. /**
  1704. * blk_requeue_request - put a request back on queue
  1705. * @q: request queue where request should be inserted
  1706. * @rq: request to be inserted
  1707. *
  1708. * Description:
  1709. * Drivers often keep queueing requests until the hardware cannot accept
  1710. * more, when that condition happens we need to put the request back
  1711. * on the queue. Must be called with queue lock held.
  1712. */
  1713. void blk_requeue_request(request_queue_t *q, struct request *rq)
  1714. {
  1715. if (blk_rq_tagged(rq))
  1716. blk_queue_end_tag(q, rq);
  1717. elv_requeue_request(q, rq);
  1718. }
  1719. EXPORT_SYMBOL(blk_requeue_request);
  1720. /**
  1721. * blk_insert_request - insert a special request in to a request queue
  1722. * @q: request queue where request should be inserted
  1723. * @rq: request to be inserted
  1724. * @at_head: insert request at head or tail of queue
  1725. * @data: private data
  1726. *
  1727. * Description:
  1728. * Many block devices need to execute commands asynchronously, so they don't
  1729. * block the whole kernel from preemption during request execution. This is
  1730. * accomplished normally by inserting aritficial requests tagged as
  1731. * REQ_SPECIAL in to the corresponding request queue, and letting them be
  1732. * scheduled for actual execution by the request queue.
  1733. *
  1734. * We have the option of inserting the head or the tail of the queue.
  1735. * Typically we use the tail for new ioctls and so forth. We use the head
  1736. * of the queue for things like a QUEUE_FULL message from a device, or a
  1737. * host that is unable to accept a particular command.
  1738. */
  1739. void blk_insert_request(request_queue_t *q, struct request *rq,
  1740. int at_head, void *data)
  1741. {
  1742. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  1743. unsigned long flags;
  1744. /*
  1745. * tell I/O scheduler that this isn't a regular read/write (ie it
  1746. * must not attempt merges on this) and that it acts as a soft
  1747. * barrier
  1748. */
  1749. rq->flags |= REQ_SPECIAL | REQ_SOFTBARRIER;
  1750. rq->special = data;
  1751. spin_lock_irqsave(q->queue_lock, flags);
  1752. /*
  1753. * If command is tagged, release the tag
  1754. */
  1755. if (blk_rq_tagged(rq))
  1756. blk_queue_end_tag(q, rq);
  1757. drive_stat_acct(rq, rq->nr_sectors, 1);
  1758. __elv_add_request(q, rq, where, 0);
  1759. if (blk_queue_plugged(q))
  1760. __generic_unplug_device(q);
  1761. else
  1762. q->request_fn(q);
  1763. spin_unlock_irqrestore(q->queue_lock, flags);
  1764. }
  1765. EXPORT_SYMBOL(blk_insert_request);
  1766. /**
  1767. * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
  1768. * @q: request queue where request should be inserted
  1769. * @rw: READ or WRITE data
  1770. * @ubuf: the user buffer
  1771. * @len: length of user data
  1772. *
  1773. * Description:
  1774. * Data will be mapped directly for zero copy io, if possible. Otherwise
  1775. * a kernel bounce buffer is used.
  1776. *
  1777. * A matching blk_rq_unmap_user() must be issued at the end of io, while
  1778. * still in process context.
  1779. *
  1780. * Note: The mapped bio may need to be bounced through blk_queue_bounce()
  1781. * before being submitted to the device, as pages mapped may be out of
  1782. * reach. It's the callers responsibility to make sure this happens. The
  1783. * original bio must be passed back in to blk_rq_unmap_user() for proper
  1784. * unmapping.
  1785. */
  1786. struct request *blk_rq_map_user(request_queue_t *q, int rw, void __user *ubuf,
  1787. unsigned int len)
  1788. {
  1789. unsigned long uaddr;
  1790. struct request *rq;
  1791. struct bio *bio;
  1792. if (len > (q->max_sectors << 9))
  1793. return ERR_PTR(-EINVAL);
  1794. if ((!len && ubuf) || (len && !ubuf))
  1795. return ERR_PTR(-EINVAL);
  1796. rq = blk_get_request(q, rw, __GFP_WAIT);
  1797. if (!rq)
  1798. return ERR_PTR(-ENOMEM);
  1799. /*
  1800. * if alignment requirement is satisfied, map in user pages for
  1801. * direct dma. else, set up kernel bounce buffers
  1802. */
  1803. uaddr = (unsigned long) ubuf;
  1804. if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
  1805. bio = bio_map_user(q, NULL, uaddr, len, rw == READ);
  1806. else
  1807. bio = bio_copy_user(q, uaddr, len, rw == READ);
  1808. if (!IS_ERR(bio)) {
  1809. rq->bio = rq->biotail = bio;
  1810. blk_rq_bio_prep(q, rq, bio);
  1811. rq->buffer = rq->data = NULL;
  1812. rq->data_len = len;
  1813. return rq;
  1814. }
  1815. /*
  1816. * bio is the err-ptr
  1817. */
  1818. blk_put_request(rq);
  1819. return (struct request *) bio;
  1820. }
  1821. EXPORT_SYMBOL(blk_rq_map_user);
  1822. /**
  1823. * blk_rq_unmap_user - unmap a request with user data
  1824. * @rq: request to be unmapped
  1825. * @bio: bio for the request
  1826. * @ulen: length of user buffer
  1827. *
  1828. * Description:
  1829. * Unmap a request previously mapped by blk_rq_map_user().
  1830. */
  1831. int blk_rq_unmap_user(struct request *rq, struct bio *bio, unsigned int ulen)
  1832. {
  1833. int ret = 0;
  1834. if (bio) {
  1835. if (bio_flagged(bio, BIO_USER_MAPPED))
  1836. bio_unmap_user(bio);
  1837. else
  1838. ret = bio_uncopy_user(bio);
  1839. }
  1840. blk_put_request(rq);
  1841. return ret;
  1842. }
  1843. EXPORT_SYMBOL(blk_rq_unmap_user);
  1844. /**
  1845. * blk_execute_rq - insert a request into queue for execution
  1846. * @q: queue to insert the request in
  1847. * @bd_disk: matching gendisk
  1848. * @rq: request to insert
  1849. *
  1850. * Description:
  1851. * Insert a fully prepared request at the back of the io scheduler queue
  1852. * for execution.
  1853. */
  1854. int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk,
  1855. struct request *rq)
  1856. {
  1857. DECLARE_COMPLETION(wait);
  1858. char sense[SCSI_SENSE_BUFFERSIZE];
  1859. int err = 0;
  1860. rq->rq_disk = bd_disk;
  1861. /*
  1862. * we need an extra reference to the request, so we can look at
  1863. * it after io completion
  1864. */
  1865. rq->ref_count++;
  1866. if (!rq->sense) {
  1867. memset(sense, 0, sizeof(sense));
  1868. rq->sense = sense;
  1869. rq->sense_len = 0;
  1870. }
  1871. rq->flags |= REQ_NOMERGE;
  1872. rq->waiting = &wait;
  1873. rq->end_io = blk_end_sync_rq;
  1874. elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
  1875. generic_unplug_device(q);
  1876. wait_for_completion(&wait);
  1877. rq->waiting = NULL;
  1878. if (rq->errors)
  1879. err = -EIO;
  1880. return err;
  1881. }
  1882. EXPORT_SYMBOL(blk_execute_rq);
  1883. /**
  1884. * blkdev_issue_flush - queue a flush
  1885. * @bdev: blockdev to issue flush for
  1886. * @error_sector: error sector
  1887. *
  1888. * Description:
  1889. * Issue a flush for the block device in question. Caller can supply
  1890. * room for storing the error offset in case of a flush error, if they
  1891. * wish to. Caller must run wait_for_completion() on its own.
  1892. */
  1893. int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
  1894. {
  1895. request_queue_t *q;
  1896. if (bdev->bd_disk == NULL)
  1897. return -ENXIO;
  1898. q = bdev_get_queue(bdev);
  1899. if (!q)
  1900. return -ENXIO;
  1901. if (!q->issue_flush_fn)
  1902. return -EOPNOTSUPP;
  1903. return q->issue_flush_fn(q, bdev->bd_disk, error_sector);
  1904. }
  1905. EXPORT_SYMBOL(blkdev_issue_flush);
  1906. /**
  1907. * blkdev_scsi_issue_flush_fn - issue flush for SCSI devices
  1908. * @q: device queue
  1909. * @disk: gendisk
  1910. * @error_sector: error offset
  1911. *
  1912. * Description:
  1913. * Devices understanding the SCSI command set, can use this function as
  1914. * a helper for issuing a cache flush. Note: driver is required to store
  1915. * the error offset (in case of error flushing) in ->sector of struct
  1916. * request.
  1917. */
  1918. int blkdev_scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
  1919. sector_t *error_sector)
  1920. {
  1921. struct request *rq = blk_get_request(q, WRITE, __GFP_WAIT);
  1922. int ret;
  1923. rq->flags |= REQ_BLOCK_PC | REQ_SOFTBARRIER;
  1924. rq->sector = 0;
  1925. memset(rq->cmd, 0, sizeof(rq->cmd));
  1926. rq->cmd[0] = 0x35;
  1927. rq->cmd_len = 12;
  1928. rq->data = NULL;
  1929. rq->data_len = 0;
  1930. rq->timeout = 60 * HZ;
  1931. ret = blk_execute_rq(q, disk, rq);
  1932. if (ret && error_sector)
  1933. *error_sector = rq->sector;
  1934. blk_put_request(rq);
  1935. return ret;
  1936. }
  1937. EXPORT_SYMBOL(blkdev_scsi_issue_flush_fn);
  1938. void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
  1939. {
  1940. int rw = rq_data_dir(rq);
  1941. if (!blk_fs_request(rq) || !rq->rq_disk)
  1942. return;
  1943. if (rw == READ) {
  1944. __disk_stat_add(rq->rq_disk, read_sectors, nr_sectors);
  1945. if (!new_io)
  1946. __disk_stat_inc(rq->rq_disk, read_merges);
  1947. } else if (rw == WRITE) {
  1948. __disk_stat_add(rq->rq_disk, write_sectors, nr_sectors);
  1949. if (!new_io)
  1950. __disk_stat_inc(rq->rq_disk, write_merges);
  1951. }
  1952. if (new_io) {
  1953. disk_round_stats(rq->rq_disk);
  1954. rq->rq_disk->in_flight++;
  1955. }
  1956. }
  1957. /*
  1958. * add-request adds a request to the linked list.
  1959. * queue lock is held and interrupts disabled, as we muck with the
  1960. * request queue list.
  1961. */
  1962. static inline void add_request(request_queue_t * q, struct request * req)
  1963. {
  1964. drive_stat_acct(req, req->nr_sectors, 1);
  1965. if (q->activity_fn)
  1966. q->activity_fn(q->activity_data, rq_data_dir(req));
  1967. /*
  1968. * elevator indicated where it wants this request to be
  1969. * inserted at elevator_merge time
  1970. */
  1971. __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
  1972. }
  1973. /*
  1974. * disk_round_stats() - Round off the performance stats on a struct
  1975. * disk_stats.
  1976. *
  1977. * The average IO queue length and utilisation statistics are maintained
  1978. * by observing the current state of the queue length and the amount of
  1979. * time it has been in this state for.
  1980. *
  1981. * Normally, that accounting is done on IO completion, but that can result
  1982. * in more than a second's worth of IO being accounted for within any one
  1983. * second, leading to >100% utilisation. To deal with that, we call this
  1984. * function to do a round-off before returning the results when reading
  1985. * /proc/diskstats. This accounts immediately for all queue usage up to
  1986. * the current jiffies and restarts the counters again.
  1987. */
  1988. void disk_round_stats(struct gendisk *disk)
  1989. {
  1990. unsigned long now = jiffies;
  1991. __disk_stat_add(disk, time_in_queue,
  1992. disk->in_flight * (now - disk->stamp));
  1993. disk->stamp = now;
  1994. if (disk->in_flight)
  1995. __disk_stat_add(disk, io_ticks, (now - disk->stamp_idle));
  1996. disk->stamp_idle = now;
  1997. }
  1998. /*
  1999. * queue lock must be held
  2000. */
  2001. static void __blk_put_request(request_queue_t *q, struct request *req)
  2002. {
  2003. struct request_list *rl = req->rl;
  2004. if (unlikely(!q))
  2005. return;
  2006. if (unlikely(--req->ref_count))
  2007. return;
  2008. req->rq_status = RQ_INACTIVE;
  2009. req->q = NULL;
  2010. req->rl = NULL;
  2011. /*
  2012. * Request may not have originated from ll_rw_blk. if not,
  2013. * it didn't come out of our reserved rq pools
  2014. */
  2015. if (rl) {
  2016. int rw = rq_data_dir(req);
  2017. elv_completed_request(q, req);
  2018. BUG_ON(!list_empty(&req->queuelist));
  2019. blk_free_request(q, req);
  2020. freed_request(q, rw);
  2021. }
  2022. }
  2023. void blk_put_request(struct request *req)
  2024. {
  2025. /*
  2026. * if req->rl isn't set, this request didnt originate from the
  2027. * block layer, so it's safe to just disregard it
  2028. */
  2029. if (req->rl) {
  2030. unsigned long flags;
  2031. request_queue_t *q = req->q;
  2032. spin_lock_irqsave(q->queue_lock, flags);
  2033. __blk_put_request(q, req);
  2034. spin_unlock_irqrestore(q->queue_lock, flags);
  2035. }
  2036. }
  2037. EXPORT_SYMBOL(blk_put_request);
  2038. /**
  2039. * blk_end_sync_rq - executes a completion event on a request
  2040. * @rq: request to complete
  2041. */
  2042. void blk_end_sync_rq(struct request *rq)
  2043. {
  2044. struct completion *waiting = rq->waiting;
  2045. rq->waiting = NULL;
  2046. __blk_put_request(rq->q, rq);
  2047. /*
  2048. * complete last, if this is a stack request the process (and thus
  2049. * the rq pointer) could be invalid right after this complete()
  2050. */
  2051. complete(waiting);
  2052. }
  2053. EXPORT_SYMBOL(blk_end_sync_rq);
  2054. /**
  2055. * blk_congestion_wait - wait for a queue to become uncongested
  2056. * @rw: READ or WRITE
  2057. * @timeout: timeout in jiffies
  2058. *
  2059. * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
  2060. * If no queues are congested then just wait for the next request to be
  2061. * returned.
  2062. */
  2063. long blk_congestion_wait(int rw, long timeout)
  2064. {
  2065. long ret;
  2066. DEFINE_WAIT(wait);
  2067. wait_queue_head_t *wqh = &congestion_wqh[rw];
  2068. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  2069. ret = io_schedule_timeout(timeout);
  2070. finish_wait(wqh, &wait);
  2071. return ret;
  2072. }
  2073. EXPORT_SYMBOL(blk_congestion_wait);
  2074. /*
  2075. * Has to be called with the request spinlock acquired
  2076. */
  2077. static int attempt_merge(request_queue_t *q, struct request *req,
  2078. struct request *next)
  2079. {
  2080. if (!rq_mergeable(req) || !rq_mergeable(next))
  2081. return 0;
  2082. /*
  2083. * not contigious
  2084. */
  2085. if (req->sector + req->nr_sectors != next->sector)
  2086. return 0;
  2087. if (rq_data_dir(req) != rq_data_dir(next)
  2088. || req->rq_disk != next->rq_disk
  2089. || next->waiting || next->special)
  2090. return 0;
  2091. /*
  2092. * If we are allowed to merge, then append bio list
  2093. * from next to rq and release next. merge_requests_fn
  2094. * will have updated segment counts, update sector
  2095. * counts here.
  2096. */
  2097. if (!q->merge_requests_fn(q, req, next))
  2098. return 0;
  2099. /*
  2100. * At this point we have either done a back merge
  2101. * or front merge. We need the smaller start_time of
  2102. * the merged requests to be the current request
  2103. * for accounting purposes.
  2104. */
  2105. if (time_after(req->start_time, next->start_time))
  2106. req->start_time = next->start_time;
  2107. req->biotail->bi_next = next->bio;
  2108. req->biotail = next->biotail;
  2109. req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
  2110. elv_merge_requests(q, req, next);
  2111. if (req->rq_disk) {
  2112. disk_round_stats(req->rq_disk);
  2113. req->rq_disk->in_flight--;
  2114. }
  2115. __blk_put_request(q, next);
  2116. return 1;
  2117. }
  2118. static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
  2119. {
  2120. struct request *next = elv_latter_request(q, rq);
  2121. if (next)
  2122. return attempt_merge(q, rq, next);
  2123. return 0;
  2124. }
  2125. static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
  2126. {
  2127. struct request *prev = elv_former_request(q, rq);
  2128. if (prev)
  2129. return attempt_merge(q, prev, rq);
  2130. return 0;
  2131. }
  2132. /**
  2133. * blk_attempt_remerge - attempt to remerge active head with next request
  2134. * @q: The &request_queue_t belonging to the device
  2135. * @rq: The head request (usually)
  2136. *
  2137. * Description:
  2138. * For head-active devices, the queue can easily be unplugged so quickly
  2139. * that proper merging is not done on the front request. This may hurt
  2140. * performance greatly for some devices. The block layer cannot safely
  2141. * do merging on that first request for these queues, but the driver can
  2142. * call this function and make it happen any way. Only the driver knows
  2143. * when it is safe to do so.
  2144. **/
  2145. void blk_attempt_remerge(request_queue_t *q, struct request *rq)
  2146. {
  2147. unsigned long flags;
  2148. spin_lock_irqsave(q->queue_lock, flags);
  2149. attempt_back_merge(q, rq);
  2150. spin_unlock_irqrestore(q->queue_lock, flags);
  2151. }
  2152. EXPORT_SYMBOL(blk_attempt_remerge);
  2153. /*
  2154. * Non-locking blk_attempt_remerge variant.
  2155. */
  2156. void __blk_attempt_remerge(request_queue_t *q, struct request *rq)
  2157. {
  2158. attempt_back_merge(q, rq);
  2159. }
  2160. EXPORT_SYMBOL(__blk_attempt_remerge);
  2161. static int __make_request(request_queue_t *q, struct bio *bio)
  2162. {
  2163. struct request *req, *freereq = NULL;
  2164. int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, err, sync;
  2165. sector_t sector;
  2166. sector = bio->bi_sector;
  2167. nr_sectors = bio_sectors(bio);
  2168. cur_nr_sectors = bio_cur_sectors(bio);
  2169. rw = bio_data_dir(bio);
  2170. sync = bio_sync(bio);
  2171. /*
  2172. * low level driver can indicate that it wants pages above a
  2173. * certain limit bounced to low memory (ie for highmem, or even
  2174. * ISA dma in theory)
  2175. */
  2176. blk_queue_bounce(q, &bio);
  2177. spin_lock_prefetch(q->queue_lock);
  2178. barrier = bio_barrier(bio);
  2179. if (barrier && (q->ordered == QUEUE_ORDERED_NONE)) {
  2180. err = -EOPNOTSUPP;
  2181. goto end_io;
  2182. }
  2183. again:
  2184. spin_lock_irq(q->queue_lock);
  2185. if (elv_queue_empty(q)) {
  2186. blk_plug_device(q);
  2187. goto get_rq;
  2188. }
  2189. if (barrier)
  2190. goto get_rq;
  2191. el_ret = elv_merge(q, &req, bio);
  2192. switch (el_ret) {
  2193. case ELEVATOR_BACK_MERGE:
  2194. BUG_ON(!rq_mergeable(req));
  2195. if (!q->back_merge_fn(q, req, bio))
  2196. break;
  2197. req->biotail->bi_next = bio;
  2198. req->biotail = bio;
  2199. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  2200. drive_stat_acct(req, nr_sectors, 0);
  2201. if (!attempt_back_merge(q, req))
  2202. elv_merged_request(q, req);
  2203. goto out;
  2204. case ELEVATOR_FRONT_MERGE:
  2205. BUG_ON(!rq_mergeable(req));
  2206. if (!q->front_merge_fn(q, req, bio))
  2207. break;
  2208. bio->bi_next = req->bio;
  2209. req->bio = bio;
  2210. /*
  2211. * may not be valid. if the low level driver said
  2212. * it didn't need a bounce buffer then it better
  2213. * not touch req->buffer either...
  2214. */
  2215. req->buffer = bio_data(bio);
  2216. req->current_nr_sectors = cur_nr_sectors;
  2217. req->hard_cur_sectors = cur_nr_sectors;
  2218. req->sector = req->hard_sector = sector;
  2219. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  2220. drive_stat_acct(req, nr_sectors, 0);
  2221. if (!attempt_front_merge(q, req))
  2222. elv_merged_request(q, req);
  2223. goto out;
  2224. /*
  2225. * elevator says don't/can't merge. get new request
  2226. */
  2227. case ELEVATOR_NO_MERGE:
  2228. break;
  2229. default:
  2230. printk("elevator returned crap (%d)\n", el_ret);
  2231. BUG();
  2232. }
  2233. /*
  2234. * Grab a free request from the freelist - if that is empty, check
  2235. * if we are doing read ahead and abort instead of blocking for
  2236. * a free slot.
  2237. */
  2238. get_rq:
  2239. if (freereq) {
  2240. req = freereq;
  2241. freereq = NULL;
  2242. } else {
  2243. spin_unlock_irq(q->queue_lock);
  2244. if ((freereq = get_request(q, rw, GFP_ATOMIC)) == NULL) {
  2245. /*
  2246. * READA bit set
  2247. */
  2248. err = -EWOULDBLOCK;
  2249. if (bio_rw_ahead(bio))
  2250. goto end_io;
  2251. freereq = get_request_wait(q, rw);
  2252. }
  2253. goto again;
  2254. }
  2255. req->flags |= REQ_CMD;
  2256. /*
  2257. * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
  2258. */
  2259. if (bio_rw_ahead(bio) || bio_failfast(bio))
  2260. req->flags |= REQ_FAILFAST;
  2261. /*
  2262. * REQ_BARRIER implies no merging, but lets make it explicit
  2263. */
  2264. if (barrier)
  2265. req->flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
  2266. req->errors = 0;
  2267. req->hard_sector = req->sector = sector;
  2268. req->hard_nr_sectors = req->nr_sectors = nr_sectors;
  2269. req->current_nr_sectors = req->hard_cur_sectors = cur_nr_sectors;
  2270. req->nr_phys_segments = bio_phys_segments(q, bio);
  2271. req->nr_hw_segments = bio_hw_segments(q, bio);
  2272. req->buffer = bio_data(bio); /* see ->buffer comment above */
  2273. req->waiting = NULL;
  2274. req->bio = req->biotail = bio;
  2275. req->rq_disk = bio->bi_bdev->bd_disk;
  2276. req->start_time = jiffies;
  2277. add_request(q, req);
  2278. out:
  2279. if (freereq)
  2280. __blk_put_request(q, freereq);
  2281. if (sync)
  2282. __generic_unplug_device(q);
  2283. spin_unlock_irq(q->queue_lock);
  2284. return 0;
  2285. end_io:
  2286. bio_endio(bio, nr_sectors << 9, err);
  2287. return 0;
  2288. }
  2289. /*
  2290. * If bio->bi_dev is a partition, remap the location
  2291. */
  2292. static inline void blk_partition_remap(struct bio *bio)
  2293. {
  2294. struct block_device *bdev = bio->bi_bdev;
  2295. if (bdev != bdev->bd_contains) {
  2296. struct hd_struct *p = bdev->bd_part;
  2297. switch (bio->bi_rw) {
  2298. case READ:
  2299. p->read_sectors += bio_sectors(bio);
  2300. p->reads++;
  2301. break;
  2302. case WRITE:
  2303. p->write_sectors += bio_sectors(bio);
  2304. p->writes++;
  2305. break;
  2306. }
  2307. bio->bi_sector += p->start_sect;
  2308. bio->bi_bdev = bdev->bd_contains;
  2309. }
  2310. }
  2311. void blk_finish_queue_drain(request_queue_t *q)
  2312. {
  2313. struct request_list *rl = &q->rq;
  2314. struct request *rq;
  2315. spin_lock_irq(q->queue_lock);
  2316. clear_bit(QUEUE_FLAG_DRAIN, &q->queue_flags);
  2317. while (!list_empty(&q->drain_list)) {
  2318. rq = list_entry_rq(q->drain_list.next);
  2319. list_del_init(&rq->queuelist);
  2320. __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
  2321. }
  2322. spin_unlock_irq(q->queue_lock);
  2323. wake_up(&rl->wait[0]);
  2324. wake_up(&rl->wait[1]);
  2325. wake_up(&rl->drain);
  2326. }
  2327. static int wait_drain(request_queue_t *q, struct request_list *rl, int dispatch)
  2328. {
  2329. int wait = rl->count[READ] + rl->count[WRITE];
  2330. if (dispatch)
  2331. wait += !list_empty(&q->queue_head);
  2332. return wait;
  2333. }
  2334. /*
  2335. * We rely on the fact that only requests allocated through blk_alloc_request()
  2336. * have io scheduler private data structures associated with them. Any other
  2337. * type of request (allocated on stack or through kmalloc()) should not go
  2338. * to the io scheduler core, but be attached to the queue head instead.
  2339. */
  2340. void blk_wait_queue_drained(request_queue_t *q, int wait_dispatch)
  2341. {
  2342. struct request_list *rl = &q->rq;
  2343. DEFINE_WAIT(wait);
  2344. spin_lock_irq(q->queue_lock);
  2345. set_bit(QUEUE_FLAG_DRAIN, &q->queue_flags);
  2346. while (wait_drain(q, rl, wait_dispatch)) {
  2347. prepare_to_wait(&rl->drain, &wait, TASK_UNINTERRUPTIBLE);
  2348. if (wait_drain(q, rl, wait_dispatch)) {
  2349. __generic_unplug_device(q);
  2350. spin_unlock_irq(q->queue_lock);
  2351. io_schedule();
  2352. spin_lock_irq(q->queue_lock);
  2353. }
  2354. finish_wait(&rl->drain, &wait);
  2355. }
  2356. spin_unlock_irq(q->queue_lock);
  2357. }
  2358. /*
  2359. * block waiting for the io scheduler being started again.
  2360. */
  2361. static inline void block_wait_queue_running(request_queue_t *q)
  2362. {
  2363. DEFINE_WAIT(wait);
  2364. while (test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags)) {
  2365. struct request_list *rl = &q->rq;
  2366. prepare_to_wait_exclusive(&rl->drain, &wait,
  2367. TASK_UNINTERRUPTIBLE);
  2368. /*
  2369. * re-check the condition. avoids using prepare_to_wait()
  2370. * in the fast path (queue is running)
  2371. */
  2372. if (test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags))
  2373. io_schedule();
  2374. finish_wait(&rl->drain, &wait);
  2375. }
  2376. }
  2377. static void handle_bad_sector(struct bio *bio)
  2378. {
  2379. char b[BDEVNAME_SIZE];
  2380. printk(KERN_INFO "attempt to access beyond end of device\n");
  2381. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  2382. bdevname(bio->bi_bdev, b),
  2383. bio->bi_rw,
  2384. (unsigned long long)bio->bi_sector + bio_sectors(bio),
  2385. (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
  2386. set_bit(BIO_EOF, &bio->bi_flags);
  2387. }
  2388. /**
  2389. * generic_make_request: hand a buffer to its device driver for I/O
  2390. * @bio: The bio describing the location in memory and on the device.
  2391. *
  2392. * generic_make_request() is used to make I/O requests of block
  2393. * devices. It is passed a &struct bio, which describes the I/O that needs
  2394. * to be done.
  2395. *
  2396. * generic_make_request() does not return any status. The
  2397. * success/failure status of the request, along with notification of
  2398. * completion, is delivered asynchronously through the bio->bi_end_io
  2399. * function described (one day) else where.
  2400. *
  2401. * The caller of generic_make_request must make sure that bi_io_vec
  2402. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  2403. * set to describe the device address, and the
  2404. * bi_end_io and optionally bi_private are set to describe how
  2405. * completion notification should be signaled.
  2406. *
  2407. * generic_make_request and the drivers it calls may use bi_next if this
  2408. * bio happens to be merged with someone else, and may change bi_dev and
  2409. * bi_sector for remaps as it sees fit. So the values of these fields
  2410. * should NOT be depended on after the call to generic_make_request.
  2411. */
  2412. void generic_make_request(struct bio *bio)
  2413. {
  2414. request_queue_t *q;
  2415. sector_t maxsector;
  2416. int ret, nr_sectors = bio_sectors(bio);
  2417. might_sleep();
  2418. /* Test device or partition size, when known. */
  2419. maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
  2420. if (maxsector) {
  2421. sector_t sector = bio->bi_sector;
  2422. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  2423. /*
  2424. * This may well happen - the kernel calls bread()
  2425. * without checking the size of the device, e.g., when
  2426. * mounting a device.
  2427. */
  2428. handle_bad_sector(bio);
  2429. goto end_io;
  2430. }
  2431. }
  2432. /*
  2433. * Resolve the mapping until finished. (drivers are
  2434. * still free to implement/resolve their own stacking
  2435. * by explicitly returning 0)
  2436. *
  2437. * NOTE: we don't repeat the blk_size check for each new device.
  2438. * Stacking drivers are expected to know what they are doing.
  2439. */
  2440. do {
  2441. char b[BDEVNAME_SIZE];
  2442. q = bdev_get_queue(bio->bi_bdev);
  2443. if (!q) {
  2444. printk(KERN_ERR
  2445. "generic_make_request: Trying to access "
  2446. "nonexistent block-device %s (%Lu)\n",
  2447. bdevname(bio->bi_bdev, b),
  2448. (long long) bio->bi_sector);
  2449. end_io:
  2450. bio_endio(bio, bio->bi_size, -EIO);
  2451. break;
  2452. }
  2453. if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) {
  2454. printk("bio too big device %s (%u > %u)\n",
  2455. bdevname(bio->bi_bdev, b),
  2456. bio_sectors(bio),
  2457. q->max_hw_sectors);
  2458. goto end_io;
  2459. }
  2460. if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))
  2461. goto end_io;
  2462. block_wait_queue_running(q);
  2463. /*
  2464. * If this device has partitions, remap block n
  2465. * of partition p to block n+start(p) of the disk.
  2466. */
  2467. blk_partition_remap(bio);
  2468. ret = q->make_request_fn(q, bio);
  2469. } while (ret);
  2470. }
  2471. EXPORT_SYMBOL(generic_make_request);
  2472. /**
  2473. * submit_bio: submit a bio to the block device layer for I/O
  2474. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  2475. * @bio: The &struct bio which describes the I/O
  2476. *
  2477. * submit_bio() is very similar in purpose to generic_make_request(), and
  2478. * uses that function to do most of the work. Both are fairly rough
  2479. * interfaces, @bio must be presetup and ready for I/O.
  2480. *
  2481. */
  2482. void submit_bio(int rw, struct bio *bio)
  2483. {
  2484. int count = bio_sectors(bio);
  2485. BIO_BUG_ON(!bio->bi_size);
  2486. BIO_BUG_ON(!bio->bi_io_vec);
  2487. bio->bi_rw = rw;
  2488. if (rw & WRITE)
  2489. mod_page_state(pgpgout, count);
  2490. else
  2491. mod_page_state(pgpgin, count);
  2492. if (unlikely(block_dump)) {
  2493. char b[BDEVNAME_SIZE];
  2494. printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
  2495. current->comm, current->pid,
  2496. (rw & WRITE) ? "WRITE" : "READ",
  2497. (unsigned long long)bio->bi_sector,
  2498. bdevname(bio->bi_bdev,b));
  2499. }
  2500. generic_make_request(bio);
  2501. }
  2502. EXPORT_SYMBOL(submit_bio);
  2503. void blk_recalc_rq_segments(struct request *rq)
  2504. {
  2505. struct bio *bio, *prevbio = NULL;
  2506. int nr_phys_segs, nr_hw_segs;
  2507. unsigned int phys_size, hw_size;
  2508. request_queue_t *q = rq->q;
  2509. if (!rq->bio)
  2510. return;
  2511. phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
  2512. rq_for_each_bio(bio, rq) {
  2513. /* Force bio hw/phys segs to be recalculated. */
  2514. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  2515. nr_phys_segs += bio_phys_segments(q, bio);
  2516. nr_hw_segs += bio_hw_segments(q, bio);
  2517. if (prevbio) {
  2518. int pseg = phys_size + prevbio->bi_size + bio->bi_size;
  2519. int hseg = hw_size + prevbio->bi_size + bio->bi_size;
  2520. if (blk_phys_contig_segment(q, prevbio, bio) &&
  2521. pseg <= q->max_segment_size) {
  2522. nr_phys_segs--;
  2523. phys_size += prevbio->bi_size + bio->bi_size;
  2524. } else
  2525. phys_size = 0;
  2526. if (blk_hw_contig_segment(q, prevbio, bio) &&
  2527. hseg <= q->max_segment_size) {
  2528. nr_hw_segs--;
  2529. hw_size += prevbio->bi_size + bio->bi_size;
  2530. } else
  2531. hw_size = 0;
  2532. }
  2533. prevbio = bio;
  2534. }
  2535. rq->nr_phys_segments = nr_phys_segs;
  2536. rq->nr_hw_segments = nr_hw_segs;
  2537. }
  2538. void blk_recalc_rq_sectors(struct request *rq, int nsect)
  2539. {
  2540. if (blk_fs_request(rq)) {
  2541. rq->hard_sector += nsect;
  2542. rq->hard_nr_sectors -= nsect;
  2543. /*
  2544. * Move the I/O submission pointers ahead if required.
  2545. */
  2546. if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
  2547. (rq->sector <= rq->hard_sector)) {
  2548. rq->sector = rq->hard_sector;
  2549. rq->nr_sectors = rq->hard_nr_sectors;
  2550. rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
  2551. rq->current_nr_sectors = rq->hard_cur_sectors;
  2552. rq->buffer = bio_data(rq->bio);
  2553. }
  2554. /*
  2555. * if total number of sectors is less than the first segment
  2556. * size, something has gone terribly wrong
  2557. */
  2558. if (rq->nr_sectors < rq->current_nr_sectors) {
  2559. printk("blk: request botched\n");
  2560. rq->nr_sectors = rq->current_nr_sectors;
  2561. }
  2562. }
  2563. }
  2564. static int __end_that_request_first(struct request *req, int uptodate,
  2565. int nr_bytes)
  2566. {
  2567. int total_bytes, bio_nbytes, error, next_idx = 0;
  2568. struct bio *bio;
  2569. /*
  2570. * extend uptodate bool to allow < 0 value to be direct io error
  2571. */
  2572. error = 0;
  2573. if (end_io_error(uptodate))
  2574. error = !uptodate ? -EIO : uptodate;
  2575. /*
  2576. * for a REQ_BLOCK_PC request, we want to carry any eventual
  2577. * sense key with us all the way through
  2578. */
  2579. if (!blk_pc_request(req))
  2580. req->errors = 0;
  2581. if (!uptodate) {
  2582. if (blk_fs_request(req) && !(req->flags & REQ_QUIET))
  2583. printk("end_request: I/O error, dev %s, sector %llu\n",
  2584. req->rq_disk ? req->rq_disk->disk_name : "?",
  2585. (unsigned long long)req->sector);
  2586. }
  2587. total_bytes = bio_nbytes = 0;
  2588. while ((bio = req->bio) != NULL) {
  2589. int nbytes;
  2590. if (nr_bytes >= bio->bi_size) {
  2591. req->bio = bio->bi_next;
  2592. nbytes = bio->bi_size;
  2593. bio_endio(bio, nbytes, error);
  2594. next_idx = 0;
  2595. bio_nbytes = 0;
  2596. } else {
  2597. int idx = bio->bi_idx + next_idx;
  2598. if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
  2599. blk_dump_rq_flags(req, "__end_that");
  2600. printk("%s: bio idx %d >= vcnt %d\n",
  2601. __FUNCTION__,
  2602. bio->bi_idx, bio->bi_vcnt);
  2603. break;
  2604. }
  2605. nbytes = bio_iovec_idx(bio, idx)->bv_len;
  2606. BIO_BUG_ON(nbytes > bio->bi_size);
  2607. /*
  2608. * not a complete bvec done
  2609. */
  2610. if (unlikely(nbytes > nr_bytes)) {
  2611. bio_nbytes += nr_bytes;
  2612. total_bytes += nr_bytes;
  2613. break;
  2614. }
  2615. /*
  2616. * advance to the next vector
  2617. */
  2618. next_idx++;
  2619. bio_nbytes += nbytes;
  2620. }
  2621. total_bytes += nbytes;
  2622. nr_bytes -= nbytes;
  2623. if ((bio = req->bio)) {
  2624. /*
  2625. * end more in this run, or just return 'not-done'
  2626. */
  2627. if (unlikely(nr_bytes <= 0))
  2628. break;
  2629. }
  2630. }
  2631. /*
  2632. * completely done
  2633. */
  2634. if (!req->bio)
  2635. return 0;
  2636. /*
  2637. * if the request wasn't completed, update state
  2638. */
  2639. if (bio_nbytes) {
  2640. bio_endio(bio, bio_nbytes, error);
  2641. bio->bi_idx += next_idx;
  2642. bio_iovec(bio)->bv_offset += nr_bytes;
  2643. bio_iovec(bio)->bv_len -= nr_bytes;
  2644. }
  2645. blk_recalc_rq_sectors(req, total_bytes >> 9);
  2646. blk_recalc_rq_segments(req);
  2647. return 1;
  2648. }
  2649. /**
  2650. * end_that_request_first - end I/O on a request
  2651. * @req: the request being processed
  2652. * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
  2653. * @nr_sectors: number of sectors to end I/O on
  2654. *
  2655. * Description:
  2656. * Ends I/O on a number of sectors attached to @req, and sets it up
  2657. * for the next range of segments (if any) in the cluster.
  2658. *
  2659. * Return:
  2660. * 0 - we are done with this request, call end_that_request_last()
  2661. * 1 - still buffers pending for this request
  2662. **/
  2663. int end_that_request_first(struct request *req, int uptodate, int nr_sectors)
  2664. {
  2665. return __end_that_request_first(req, uptodate, nr_sectors << 9);
  2666. }
  2667. EXPORT_SYMBOL(end_that_request_first);
  2668. /**
  2669. * end_that_request_chunk - end I/O on a request
  2670. * @req: the request being processed
  2671. * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
  2672. * @nr_bytes: number of bytes to complete
  2673. *
  2674. * Description:
  2675. * Ends I/O on a number of bytes attached to @req, and sets it up
  2676. * for the next range of segments (if any). Like end_that_request_first(),
  2677. * but deals with bytes instead of sectors.
  2678. *
  2679. * Return:
  2680. * 0 - we are done with this request, call end_that_request_last()
  2681. * 1 - still buffers pending for this request
  2682. **/
  2683. int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes)
  2684. {
  2685. return __end_that_request_first(req, uptodate, nr_bytes);
  2686. }
  2687. EXPORT_SYMBOL(end_that_request_chunk);
  2688. /*
  2689. * queue lock must be held
  2690. */
  2691. void end_that_request_last(struct request *req)
  2692. {
  2693. struct gendisk *disk = req->rq_disk;
  2694. if (unlikely(laptop_mode) && blk_fs_request(req))
  2695. laptop_io_completion();
  2696. if (disk && blk_fs_request(req)) {
  2697. unsigned long duration = jiffies - req->start_time;
  2698. switch (rq_data_dir(req)) {
  2699. case WRITE:
  2700. __disk_stat_inc(disk, writes);
  2701. __disk_stat_add(disk, write_ticks, duration);
  2702. break;
  2703. case READ:
  2704. __disk_stat_inc(disk, reads);
  2705. __disk_stat_add(disk, read_ticks, duration);
  2706. break;
  2707. }
  2708. disk_round_stats(disk);
  2709. disk->in_flight--;
  2710. }
  2711. if (req->end_io)
  2712. req->end_io(req);
  2713. else
  2714. __blk_put_request(req->q, req);
  2715. }
  2716. EXPORT_SYMBOL(end_that_request_last);
  2717. void end_request(struct request *req, int uptodate)
  2718. {
  2719. if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) {
  2720. add_disk_randomness(req->rq_disk);
  2721. blkdev_dequeue_request(req);
  2722. end_that_request_last(req);
  2723. }
  2724. }
  2725. EXPORT_SYMBOL(end_request);
  2726. void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio)
  2727. {
  2728. /* first three bits are identical in rq->flags and bio->bi_rw */
  2729. rq->flags |= (bio->bi_rw & 7);
  2730. rq->nr_phys_segments = bio_phys_segments(q, bio);
  2731. rq->nr_hw_segments = bio_hw_segments(q, bio);
  2732. rq->current_nr_sectors = bio_cur_sectors(bio);
  2733. rq->hard_cur_sectors = rq->current_nr_sectors;
  2734. rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
  2735. rq->buffer = bio_data(bio);
  2736. rq->bio = rq->biotail = bio;
  2737. }
  2738. EXPORT_SYMBOL(blk_rq_bio_prep);
  2739. int kblockd_schedule_work(struct work_struct *work)
  2740. {
  2741. return queue_work(kblockd_workqueue, work);
  2742. }
  2743. EXPORT_SYMBOL(kblockd_schedule_work);
  2744. void kblockd_flush(void)
  2745. {
  2746. flush_workqueue(kblockd_workqueue);
  2747. }
  2748. EXPORT_SYMBOL(kblockd_flush);
  2749. int __init blk_dev_init(void)
  2750. {
  2751. kblockd_workqueue = create_workqueue("kblockd");
  2752. if (!kblockd_workqueue)
  2753. panic("Failed to create kblockd\n");
  2754. request_cachep = kmem_cache_create("blkdev_requests",
  2755. sizeof(struct request), 0, SLAB_PANIC, NULL, NULL);
  2756. requestq_cachep = kmem_cache_create("blkdev_queue",
  2757. sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL);
  2758. iocontext_cachep = kmem_cache_create("blkdev_ioc",
  2759. sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL);
  2760. blk_max_low_pfn = max_low_pfn;
  2761. blk_max_pfn = max_pfn;
  2762. return 0;
  2763. }
  2764. /*
  2765. * IO Context helper functions
  2766. */
  2767. void put_io_context(struct io_context *ioc)
  2768. {
  2769. if (ioc == NULL)
  2770. return;
  2771. BUG_ON(atomic_read(&ioc->refcount) == 0);
  2772. if (atomic_dec_and_test(&ioc->refcount)) {
  2773. if (ioc->aic && ioc->aic->dtor)
  2774. ioc->aic->dtor(ioc->aic);
  2775. if (ioc->cic && ioc->cic->dtor)
  2776. ioc->cic->dtor(ioc->cic);
  2777. kmem_cache_free(iocontext_cachep, ioc);
  2778. }
  2779. }
  2780. EXPORT_SYMBOL(put_io_context);
  2781. /* Called by the exitting task */
  2782. void exit_io_context(void)
  2783. {
  2784. unsigned long flags;
  2785. struct io_context *ioc;
  2786. local_irq_save(flags);
  2787. ioc = current->io_context;
  2788. current->io_context = NULL;
  2789. local_irq_restore(flags);
  2790. if (ioc->aic && ioc->aic->exit)
  2791. ioc->aic->exit(ioc->aic);
  2792. if (ioc->cic && ioc->cic->exit)
  2793. ioc->cic->exit(ioc->cic);
  2794. put_io_context(ioc);
  2795. }
  2796. /*
  2797. * If the current task has no IO context then create one and initialise it.
  2798. * If it does have a context, take a ref on it.
  2799. *
  2800. * This is always called in the context of the task which submitted the I/O.
  2801. * But weird things happen, so we disable local interrupts to ensure exclusive
  2802. * access to *current.
  2803. */
  2804. struct io_context *get_io_context(int gfp_flags)
  2805. {
  2806. struct task_struct *tsk = current;
  2807. unsigned long flags;
  2808. struct io_context *ret;
  2809. local_irq_save(flags);
  2810. ret = tsk->io_context;
  2811. if (ret)
  2812. goto out;
  2813. local_irq_restore(flags);
  2814. ret = kmem_cache_alloc(iocontext_cachep, gfp_flags);
  2815. if (ret) {
  2816. atomic_set(&ret->refcount, 1);
  2817. ret->pid = tsk->pid;
  2818. ret->last_waited = jiffies; /* doesn't matter... */
  2819. ret->nr_batch_requests = 0; /* because this is 0 */
  2820. ret->aic = NULL;
  2821. ret->cic = NULL;
  2822. spin_lock_init(&ret->lock);
  2823. local_irq_save(flags);
  2824. /*
  2825. * very unlikely, someone raced with us in setting up the task
  2826. * io context. free new context and just grab a reference.
  2827. */
  2828. if (!tsk->io_context)
  2829. tsk->io_context = ret;
  2830. else {
  2831. kmem_cache_free(iocontext_cachep, ret);
  2832. ret = tsk->io_context;
  2833. }
  2834. out:
  2835. atomic_inc(&ret->refcount);
  2836. local_irq_restore(flags);
  2837. }
  2838. return ret;
  2839. }
  2840. EXPORT_SYMBOL(get_io_context);
  2841. void copy_io_context(struct io_context **pdst, struct io_context **psrc)
  2842. {
  2843. struct io_context *src = *psrc;
  2844. struct io_context *dst = *pdst;
  2845. if (src) {
  2846. BUG_ON(atomic_read(&src->refcount) == 0);
  2847. atomic_inc(&src->refcount);
  2848. put_io_context(dst);
  2849. *pdst = src;
  2850. }
  2851. }
  2852. EXPORT_SYMBOL(copy_io_context);
  2853. void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
  2854. {
  2855. struct io_context *temp;
  2856. temp = *ioc1;
  2857. *ioc1 = *ioc2;
  2858. *ioc2 = temp;
  2859. }
  2860. EXPORT_SYMBOL(swap_io_context);
  2861. /*
  2862. * sysfs parts below
  2863. */
  2864. struct queue_sysfs_entry {
  2865. struct attribute attr;
  2866. ssize_t (*show)(struct request_queue *, char *);
  2867. ssize_t (*store)(struct request_queue *, const char *, size_t);
  2868. };
  2869. static ssize_t
  2870. queue_var_show(unsigned int var, char *page)
  2871. {
  2872. return sprintf(page, "%d\n", var);
  2873. }
  2874. static ssize_t
  2875. queue_var_store(unsigned long *var, const char *page, size_t count)
  2876. {
  2877. char *p = (char *) page;
  2878. *var = simple_strtoul(p, &p, 10);
  2879. return count;
  2880. }
  2881. static ssize_t queue_requests_show(struct request_queue *q, char *page)
  2882. {
  2883. return queue_var_show(q->nr_requests, (page));
  2884. }
  2885. static ssize_t
  2886. queue_requests_store(struct request_queue *q, const char *page, size_t count)
  2887. {
  2888. struct request_list *rl = &q->rq;
  2889. int ret = queue_var_store(&q->nr_requests, page, count);
  2890. if (q->nr_requests < BLKDEV_MIN_RQ)
  2891. q->nr_requests = BLKDEV_MIN_RQ;
  2892. blk_queue_congestion_threshold(q);
  2893. if (rl->count[READ] >= queue_congestion_on_threshold(q))
  2894. set_queue_congested(q, READ);
  2895. else if (rl->count[READ] < queue_congestion_off_threshold(q))
  2896. clear_queue_congested(q, READ);
  2897. if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
  2898. set_queue_congested(q, WRITE);
  2899. else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
  2900. clear_queue_congested(q, WRITE);
  2901. if (rl->count[READ] >= q->nr_requests) {
  2902. blk_set_queue_full(q, READ);
  2903. } else if (rl->count[READ]+1 <= q->nr_requests) {
  2904. blk_clear_queue_full(q, READ);
  2905. wake_up(&rl->wait[READ]);
  2906. }
  2907. if (rl->count[WRITE] >= q->nr_requests) {
  2908. blk_set_queue_full(q, WRITE);
  2909. } else if (rl->count[WRITE]+1 <= q->nr_requests) {
  2910. blk_clear_queue_full(q, WRITE);
  2911. wake_up(&rl->wait[WRITE]);
  2912. }
  2913. return ret;
  2914. }
  2915. static ssize_t queue_ra_show(struct request_queue *q, char *page)
  2916. {
  2917. int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
  2918. return queue_var_show(ra_kb, (page));
  2919. }
  2920. static ssize_t
  2921. queue_ra_store(struct request_queue *q, const char *page, size_t count)
  2922. {
  2923. unsigned long ra_kb;
  2924. ssize_t ret = queue_var_store(&ra_kb, page, count);
  2925. spin_lock_irq(q->queue_lock);
  2926. if (ra_kb > (q->max_sectors >> 1))
  2927. ra_kb = (q->max_sectors >> 1);
  2928. q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
  2929. spin_unlock_irq(q->queue_lock);
  2930. return ret;
  2931. }
  2932. static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
  2933. {
  2934. int max_sectors_kb = q->max_sectors >> 1;
  2935. return queue_var_show(max_sectors_kb, (page));
  2936. }
  2937. static ssize_t
  2938. queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
  2939. {
  2940. unsigned long max_sectors_kb,
  2941. max_hw_sectors_kb = q->max_hw_sectors >> 1,
  2942. page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
  2943. ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
  2944. int ra_kb;
  2945. if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  2946. return -EINVAL;
  2947. /*
  2948. * Take the queue lock to update the readahead and max_sectors
  2949. * values synchronously:
  2950. */
  2951. spin_lock_irq(q->queue_lock);
  2952. /*
  2953. * Trim readahead window as well, if necessary:
  2954. */
  2955. ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
  2956. if (ra_kb > max_sectors_kb)
  2957. q->backing_dev_info.ra_pages =
  2958. max_sectors_kb >> (PAGE_CACHE_SHIFT - 10);
  2959. q->max_sectors = max_sectors_kb << 1;
  2960. spin_unlock_irq(q->queue_lock);
  2961. return ret;
  2962. }
  2963. static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
  2964. {
  2965. int max_hw_sectors_kb = q->max_hw_sectors >> 1;
  2966. return queue_var_show(max_hw_sectors_kb, (page));
  2967. }
  2968. static struct queue_sysfs_entry queue_requests_entry = {
  2969. .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  2970. .show = queue_requests_show,
  2971. .store = queue_requests_store,
  2972. };
  2973. static struct queue_sysfs_entry queue_ra_entry = {
  2974. .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  2975. .show = queue_ra_show,
  2976. .store = queue_ra_store,
  2977. };
  2978. static struct queue_sysfs_entry queue_max_sectors_entry = {
  2979. .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  2980. .show = queue_max_sectors_show,
  2981. .store = queue_max_sectors_store,
  2982. };
  2983. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  2984. .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  2985. .show = queue_max_hw_sectors_show,
  2986. };
  2987. static struct queue_sysfs_entry queue_iosched_entry = {
  2988. .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  2989. .show = elv_iosched_show,
  2990. .store = elv_iosched_store,
  2991. };
  2992. static struct attribute *default_attrs[] = {
  2993. &queue_requests_entry.attr,
  2994. &queue_ra_entry.attr,
  2995. &queue_max_hw_sectors_entry.attr,
  2996. &queue_max_sectors_entry.attr,
  2997. &queue_iosched_entry.attr,
  2998. NULL,
  2999. };
  3000. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  3001. static ssize_t
  3002. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  3003. {
  3004. struct queue_sysfs_entry *entry = to_queue(attr);
  3005. struct request_queue *q;
  3006. q = container_of(kobj, struct request_queue, kobj);
  3007. if (!entry->show)
  3008. return -EIO;
  3009. return entry->show(q, page);
  3010. }
  3011. static ssize_t
  3012. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  3013. const char *page, size_t length)
  3014. {
  3015. struct queue_sysfs_entry *entry = to_queue(attr);
  3016. struct request_queue *q;
  3017. q = container_of(kobj, struct request_queue, kobj);
  3018. if (!entry->store)
  3019. return -EIO;
  3020. return entry->store(q, page, length);
  3021. }
  3022. static struct sysfs_ops queue_sysfs_ops = {
  3023. .show = queue_attr_show,
  3024. .store = queue_attr_store,
  3025. };
  3026. struct kobj_type queue_ktype = {
  3027. .sysfs_ops = &queue_sysfs_ops,
  3028. .default_attrs = default_attrs,
  3029. };
  3030. int blk_register_queue(struct gendisk *disk)
  3031. {
  3032. int ret;
  3033. request_queue_t *q = disk->queue;
  3034. if (!q || !q->request_fn)
  3035. return -ENXIO;
  3036. q->kobj.parent = kobject_get(&disk->kobj);
  3037. if (!q->kobj.parent)
  3038. return -EBUSY;
  3039. snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue");
  3040. q->kobj.ktype = &queue_ktype;
  3041. ret = kobject_register(&q->kobj);
  3042. if (ret < 0)
  3043. return ret;
  3044. ret = elv_register_queue(q);
  3045. if (ret) {
  3046. kobject_unregister(&q->kobj);
  3047. return ret;
  3048. }
  3049. return 0;
  3050. }
  3051. void blk_unregister_queue(struct gendisk *disk)
  3052. {
  3053. request_queue_t *q = disk->queue;
  3054. if (q && q->request_fn) {
  3055. elv_unregister_queue(q);
  3056. kobject_unregister(&q->kobj);
  3057. kobject_put(&disk->kobj);
  3058. }
  3059. }