ll_rw_blk.c 94 KB

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