ll_rw_blk.c 91 KB

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