ll_rw_blk.c 105 KB

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