ll_rw_blk.c 92 KB

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