blk-core.c 114 KB

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