ll_rw_blk.c 95 KB

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