ll_rw_blk.c 92 KB

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