ll_rw_blk.c 108 KB

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