ll_rw_blk.c 103 KB

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