blk-core.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245
  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>
  7. * - July2000
  8. * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
  9. */
  10. /*
  11. * This handles all read/write requests to block devices
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/bio.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/blk-mq.h>
  19. #include <linux/highmem.h>
  20. #include <linux/mm.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/string.h>
  23. #include <linux/init.h>
  24. #include <linux/completion.h>
  25. #include <linux/slab.h>
  26. #include <linux/swap.h>
  27. #include <linux/writeback.h>
  28. #include <linux/task_io_accounting_ops.h>
  29. #include <linux/fault-inject.h>
  30. #include <linux/list_sort.h>
  31. #include <linux/delay.h>
  32. #include <linux/ratelimit.h>
  33. #include <linux/pm_runtime.h>
  34. #define CREATE_TRACE_POINTS
  35. #include <trace/events/block.h>
  36. #include "blk.h"
  37. #include "blk-cgroup.h"
  38. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
  39. EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
  40. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
  41. EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
  42. DEFINE_IDA(blk_queue_ida);
  43. /*
  44. * For the allocated request tables
  45. */
  46. struct kmem_cache *request_cachep = NULL;
  47. /*
  48. * For queue allocation
  49. */
  50. struct kmem_cache *blk_requestq_cachep;
  51. /*
  52. * Controlling structure to kblockd
  53. */
  54. static struct workqueue_struct *kblockd_workqueue;
  55. void blk_queue_congestion_threshold(struct request_queue *q)
  56. {
  57. int nr;
  58. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  59. if (nr > q->nr_requests)
  60. nr = q->nr_requests;
  61. q->nr_congestion_on = nr;
  62. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  63. if (nr < 1)
  64. nr = 1;
  65. q->nr_congestion_off = nr;
  66. }
  67. /**
  68. * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
  69. * @bdev: device
  70. *
  71. * Locates the passed device's request queue and returns the address of its
  72. * backing_dev_info
  73. *
  74. * Will return NULL if the request queue cannot be located.
  75. */
  76. struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
  77. {
  78. struct backing_dev_info *ret = NULL;
  79. struct request_queue *q = bdev_get_queue(bdev);
  80. if (q)
  81. ret = &q->backing_dev_info;
  82. return ret;
  83. }
  84. EXPORT_SYMBOL(blk_get_backing_dev_info);
  85. void blk_rq_init(struct request_queue *q, struct request *rq)
  86. {
  87. memset(rq, 0, sizeof(*rq));
  88. INIT_LIST_HEAD(&rq->queuelist);
  89. INIT_LIST_HEAD(&rq->timeout_list);
  90. rq->cpu = -1;
  91. rq->q = q;
  92. rq->__sector = (sector_t) -1;
  93. INIT_HLIST_NODE(&rq->hash);
  94. RB_CLEAR_NODE(&rq->rb_node);
  95. rq->cmd = rq->__cmd;
  96. rq->cmd_len = BLK_MAX_CDB;
  97. rq->tag = -1;
  98. rq->start_time = jiffies;
  99. set_start_time_ns(rq);
  100. rq->part = NULL;
  101. }
  102. EXPORT_SYMBOL(blk_rq_init);
  103. static void req_bio_endio(struct request *rq, struct bio *bio,
  104. unsigned int nbytes, int error)
  105. {
  106. if (error)
  107. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  108. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  109. error = -EIO;
  110. if (unlikely(rq->cmd_flags & REQ_QUIET))
  111. set_bit(BIO_QUIET, &bio->bi_flags);
  112. bio_advance(bio, nbytes);
  113. /* don't actually finish bio if it's part of flush sequence */
  114. if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
  115. bio_endio(bio, error);
  116. }
  117. void blk_dump_rq_flags(struct request *rq, char *msg)
  118. {
  119. int bit;
  120. printk(KERN_INFO "%s: dev %s: type=%x, flags=%llx\n", msg,
  121. rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
  122. (unsigned long long) rq->cmd_flags);
  123. printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
  124. (unsigned long long)blk_rq_pos(rq),
  125. blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
  126. printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
  127. rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
  128. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  129. printk(KERN_INFO " cdb: ");
  130. for (bit = 0; bit < BLK_MAX_CDB; bit++)
  131. printk("%02x ", rq->cmd[bit]);
  132. printk("\n");
  133. }
  134. }
  135. EXPORT_SYMBOL(blk_dump_rq_flags);
  136. static void blk_delay_work(struct work_struct *work)
  137. {
  138. struct request_queue *q;
  139. q = container_of(work, struct request_queue, delay_work.work);
  140. spin_lock_irq(q->queue_lock);
  141. __blk_run_queue(q);
  142. spin_unlock_irq(q->queue_lock);
  143. }
  144. /**
  145. * blk_delay_queue - restart queueing after defined interval
  146. * @q: The &struct request_queue in question
  147. * @msecs: Delay in msecs
  148. *
  149. * Description:
  150. * Sometimes queueing needs to be postponed for a little while, to allow
  151. * resources to come back. This function will make sure that queueing is
  152. * restarted around the specified time. Queue lock must be held.
  153. */
  154. void blk_delay_queue(struct request_queue *q, unsigned long msecs)
  155. {
  156. if (likely(!blk_queue_dead(q)))
  157. queue_delayed_work(kblockd_workqueue, &q->delay_work,
  158. msecs_to_jiffies(msecs));
  159. }
  160. EXPORT_SYMBOL(blk_delay_queue);
  161. /**
  162. * blk_start_queue - restart a previously stopped queue
  163. * @q: The &struct request_queue in question
  164. *
  165. * Description:
  166. * blk_start_queue() will clear the stop flag on the queue, and call
  167. * the request_fn for the queue if it was in a stopped state when
  168. * entered. Also see blk_stop_queue(). Queue lock must be held.
  169. **/
  170. void blk_start_queue(struct request_queue *q)
  171. {
  172. WARN_ON(!irqs_disabled());
  173. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  174. __blk_run_queue(q);
  175. }
  176. EXPORT_SYMBOL(blk_start_queue);
  177. /**
  178. * blk_stop_queue - stop a queue
  179. * @q: The &struct request_queue in question
  180. *
  181. * Description:
  182. * The Linux block layer assumes that a block driver will consume all
  183. * entries on the request queue when the request_fn strategy is called.
  184. * Often this will not happen, because of hardware limitations (queue
  185. * depth settings). If a device driver gets a 'queue full' response,
  186. * or if it simply chooses not to queue more I/O at one point, it can
  187. * call this function to prevent the request_fn from being called until
  188. * the driver has signalled it's ready to go again. This happens by calling
  189. * blk_start_queue() to restart queue operations. Queue lock must be held.
  190. **/
  191. void blk_stop_queue(struct request_queue *q)
  192. {
  193. cancel_delayed_work(&q->delay_work);
  194. queue_flag_set(QUEUE_FLAG_STOPPED, q);
  195. }
  196. EXPORT_SYMBOL(blk_stop_queue);
  197. /**
  198. * blk_sync_queue - cancel any pending callbacks on a queue
  199. * @q: the queue
  200. *
  201. * Description:
  202. * The block layer may perform asynchronous callback activity
  203. * on a queue, such as calling the unplug function after a timeout.
  204. * A block device may call blk_sync_queue to ensure that any
  205. * such activity is cancelled, thus allowing it to release resources
  206. * that the callbacks might use. The caller must already have made sure
  207. * that its ->make_request_fn will not re-add plugging prior to calling
  208. * this function.
  209. *
  210. * This function does not cancel any asynchronous activity arising
  211. * out of elevator or throttling code. That would require elevaotor_exit()
  212. * and blkcg_exit_queue() to be called with queue lock initialized.
  213. *
  214. */
  215. void blk_sync_queue(struct request_queue *q)
  216. {
  217. del_timer_sync(&q->timeout);
  218. cancel_delayed_work_sync(&q->delay_work);
  219. }
  220. EXPORT_SYMBOL(blk_sync_queue);
  221. /**
  222. * __blk_run_queue_uncond - run a queue whether or not it has been stopped
  223. * @q: The queue to run
  224. *
  225. * Description:
  226. * Invoke request handling on a queue if there are any pending requests.
  227. * May be used to restart request handling after a request has completed.
  228. * This variant runs the queue whether or not the queue has been
  229. * stopped. Must be called with the queue lock held and interrupts
  230. * disabled. See also @blk_run_queue.
  231. */
  232. inline void __blk_run_queue_uncond(struct request_queue *q)
  233. {
  234. if (unlikely(blk_queue_dead(q)))
  235. return;
  236. /*
  237. * Some request_fn implementations, e.g. scsi_request_fn(), unlock
  238. * the queue lock internally. As a result multiple threads may be
  239. * running such a request function concurrently. Keep track of the
  240. * number of active request_fn invocations such that blk_drain_queue()
  241. * can wait until all these request_fn calls have finished.
  242. */
  243. q->request_fn_active++;
  244. q->request_fn(q);
  245. q->request_fn_active--;
  246. }
  247. /**
  248. * __blk_run_queue - run a single device queue
  249. * @q: The queue to run
  250. *
  251. * Description:
  252. * See @blk_run_queue. This variant must be called with the queue lock
  253. * held and interrupts disabled.
  254. */
  255. void __blk_run_queue(struct request_queue *q)
  256. {
  257. if (unlikely(blk_queue_stopped(q)))
  258. return;
  259. __blk_run_queue_uncond(q);
  260. }
  261. EXPORT_SYMBOL(__blk_run_queue);
  262. /**
  263. * blk_run_queue_async - run a single device queue in workqueue context
  264. * @q: The queue to run
  265. *
  266. * Description:
  267. * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
  268. * of us. The caller must hold the queue lock.
  269. */
  270. void blk_run_queue_async(struct request_queue *q)
  271. {
  272. if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
  273. mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
  274. }
  275. EXPORT_SYMBOL(blk_run_queue_async);
  276. /**
  277. * blk_run_queue - run a single device queue
  278. * @q: The queue to run
  279. *
  280. * Description:
  281. * Invoke request handling on this queue, if it has pending work to do.
  282. * May be used to restart queueing when a request has completed.
  283. */
  284. void blk_run_queue(struct request_queue *q)
  285. {
  286. unsigned long flags;
  287. spin_lock_irqsave(q->queue_lock, flags);
  288. __blk_run_queue(q);
  289. spin_unlock_irqrestore(q->queue_lock, flags);
  290. }
  291. EXPORT_SYMBOL(blk_run_queue);
  292. void blk_put_queue(struct request_queue *q)
  293. {
  294. kobject_put(&q->kobj);
  295. }
  296. EXPORT_SYMBOL(blk_put_queue);
  297. /**
  298. * __blk_drain_queue - drain requests from request_queue
  299. * @q: queue to drain
  300. * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
  301. *
  302. * Drain requests from @q. If @drain_all is set, all requests are drained.
  303. * If not, only ELVPRIV requests are drained. The caller is responsible
  304. * for ensuring that no new requests which need to be drained are queued.
  305. */
  306. static void __blk_drain_queue(struct request_queue *q, bool drain_all)
  307. __releases(q->queue_lock)
  308. __acquires(q->queue_lock)
  309. {
  310. int i;
  311. lockdep_assert_held(q->queue_lock);
  312. while (true) {
  313. bool drain = false;
  314. /*
  315. * The caller might be trying to drain @q before its
  316. * elevator is initialized.
  317. */
  318. if (q->elevator)
  319. elv_drain_elevator(q);
  320. blkcg_drain_queue(q);
  321. /*
  322. * This function might be called on a queue which failed
  323. * driver init after queue creation or is not yet fully
  324. * active yet. Some drivers (e.g. fd and loop) get unhappy
  325. * in such cases. Kick queue iff dispatch queue has
  326. * something on it and @q has request_fn set.
  327. */
  328. if (!list_empty(&q->queue_head) && q->request_fn)
  329. __blk_run_queue(q);
  330. drain |= q->nr_rqs_elvpriv;
  331. drain |= q->request_fn_active;
  332. /*
  333. * Unfortunately, requests are queued at and tracked from
  334. * multiple places and there's no single counter which can
  335. * be drained. Check all the queues and counters.
  336. */
  337. if (drain_all) {
  338. drain |= !list_empty(&q->queue_head);
  339. for (i = 0; i < 2; i++) {
  340. drain |= q->nr_rqs[i];
  341. drain |= q->in_flight[i];
  342. drain |= !list_empty(&q->flush_queue[i]);
  343. }
  344. }
  345. if (!drain)
  346. break;
  347. spin_unlock_irq(q->queue_lock);
  348. msleep(10);
  349. spin_lock_irq(q->queue_lock);
  350. }
  351. /*
  352. * With queue marked dead, any woken up waiter will fail the
  353. * allocation path, so the wakeup chaining is lost and we're
  354. * left with hung waiters. We need to wake up those waiters.
  355. */
  356. if (q->request_fn) {
  357. struct request_list *rl;
  358. blk_queue_for_each_rl(rl, q)
  359. for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
  360. wake_up_all(&rl->wait[i]);
  361. }
  362. }
  363. /**
  364. * blk_queue_bypass_start - enter queue bypass mode
  365. * @q: queue of interest
  366. *
  367. * In bypass mode, only the dispatch FIFO queue of @q is used. This
  368. * function makes @q enter bypass mode and drains all requests which were
  369. * throttled or issued before. On return, it's guaranteed that no request
  370. * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
  371. * inside queue or RCU read lock.
  372. */
  373. void blk_queue_bypass_start(struct request_queue *q)
  374. {
  375. bool drain;
  376. spin_lock_irq(q->queue_lock);
  377. drain = !q->bypass_depth++;
  378. queue_flag_set(QUEUE_FLAG_BYPASS, q);
  379. spin_unlock_irq(q->queue_lock);
  380. if (drain) {
  381. spin_lock_irq(q->queue_lock);
  382. __blk_drain_queue(q, false);
  383. spin_unlock_irq(q->queue_lock);
  384. /* ensure blk_queue_bypass() is %true inside RCU read lock */
  385. synchronize_rcu();
  386. }
  387. }
  388. EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
  389. /**
  390. * blk_queue_bypass_end - leave queue bypass mode
  391. * @q: queue of interest
  392. *
  393. * Leave bypass mode and restore the normal queueing behavior.
  394. */
  395. void blk_queue_bypass_end(struct request_queue *q)
  396. {
  397. spin_lock_irq(q->queue_lock);
  398. if (!--q->bypass_depth)
  399. queue_flag_clear(QUEUE_FLAG_BYPASS, q);
  400. WARN_ON_ONCE(q->bypass_depth < 0);
  401. spin_unlock_irq(q->queue_lock);
  402. }
  403. EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
  404. /**
  405. * blk_cleanup_queue - shutdown a request queue
  406. * @q: request queue to shutdown
  407. *
  408. * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
  409. * put it. All future requests will be failed immediately with -ENODEV.
  410. */
  411. void blk_cleanup_queue(struct request_queue *q)
  412. {
  413. spinlock_t *lock = q->queue_lock;
  414. /* mark @q DYING, no new request or merges will be allowed afterwards */
  415. mutex_lock(&q->sysfs_lock);
  416. queue_flag_set_unlocked(QUEUE_FLAG_DYING, q);
  417. spin_lock_irq(lock);
  418. /*
  419. * A dying queue is permanently in bypass mode till released. Note
  420. * that, unlike blk_queue_bypass_start(), we aren't performing
  421. * synchronize_rcu() after entering bypass mode to avoid the delay
  422. * as some drivers create and destroy a lot of queues while
  423. * probing. This is still safe because blk_release_queue() will be
  424. * called only after the queue refcnt drops to zero and nothing,
  425. * RCU or not, would be traversing the queue by then.
  426. */
  427. q->bypass_depth++;
  428. queue_flag_set(QUEUE_FLAG_BYPASS, q);
  429. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  430. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  431. queue_flag_set(QUEUE_FLAG_DYING, q);
  432. spin_unlock_irq(lock);
  433. mutex_unlock(&q->sysfs_lock);
  434. /*
  435. * Drain all requests queued before DYING marking. Set DEAD flag to
  436. * prevent that q->request_fn() gets invoked after draining finished.
  437. */
  438. spin_lock_irq(lock);
  439. __blk_drain_queue(q, true);
  440. queue_flag_set(QUEUE_FLAG_DEAD, q);
  441. spin_unlock_irq(lock);
  442. /* @q won't process any more request, flush async actions */
  443. del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
  444. blk_sync_queue(q);
  445. spin_lock_irq(lock);
  446. if (q->queue_lock != &q->__queue_lock)
  447. q->queue_lock = &q->__queue_lock;
  448. spin_unlock_irq(lock);
  449. /* @q is and will stay empty, shutdown and put */
  450. blk_put_queue(q);
  451. }
  452. EXPORT_SYMBOL(blk_cleanup_queue);
  453. int blk_init_rl(struct request_list *rl, struct request_queue *q,
  454. gfp_t gfp_mask)
  455. {
  456. if (unlikely(rl->rq_pool))
  457. return 0;
  458. rl->q = q;
  459. rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
  460. rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
  461. init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
  462. init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
  463. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  464. mempool_free_slab, request_cachep,
  465. gfp_mask, q->node);
  466. if (!rl->rq_pool)
  467. return -ENOMEM;
  468. return 0;
  469. }
  470. void blk_exit_rl(struct request_list *rl)
  471. {
  472. if (rl->rq_pool)
  473. mempool_destroy(rl->rq_pool);
  474. }
  475. struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
  476. {
  477. return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
  478. }
  479. EXPORT_SYMBOL(blk_alloc_queue);
  480. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  481. {
  482. struct request_queue *q;
  483. int err;
  484. q = kmem_cache_alloc_node(blk_requestq_cachep,
  485. gfp_mask | __GFP_ZERO, node_id);
  486. if (!q)
  487. return NULL;
  488. if (percpu_counter_init(&q->mq_usage_counter, 0))
  489. goto fail_q;
  490. q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
  491. if (q->id < 0)
  492. goto fail_c;
  493. q->backing_dev_info.ra_pages =
  494. (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  495. q->backing_dev_info.state = 0;
  496. q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
  497. q->backing_dev_info.name = "block";
  498. q->node = node_id;
  499. err = bdi_init(&q->backing_dev_info);
  500. if (err)
  501. goto fail_id;
  502. setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
  503. laptop_mode_timer_fn, (unsigned long) q);
  504. setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
  505. INIT_LIST_HEAD(&q->queue_head);
  506. INIT_LIST_HEAD(&q->timeout_list);
  507. INIT_LIST_HEAD(&q->icq_list);
  508. #ifdef CONFIG_BLK_CGROUP
  509. INIT_LIST_HEAD(&q->blkg_list);
  510. #endif
  511. INIT_LIST_HEAD(&q->flush_queue[0]);
  512. INIT_LIST_HEAD(&q->flush_queue[1]);
  513. INIT_LIST_HEAD(&q->flush_data_in_flight);
  514. INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
  515. kobject_init(&q->kobj, &blk_queue_ktype);
  516. mutex_init(&q->sysfs_lock);
  517. spin_lock_init(&q->__queue_lock);
  518. /*
  519. * By default initialize queue_lock to internal lock and driver can
  520. * override it later if need be.
  521. */
  522. q->queue_lock = &q->__queue_lock;
  523. /*
  524. * A queue starts its life with bypass turned on to avoid
  525. * unnecessary bypass on/off overhead and nasty surprises during
  526. * init. The initial bypass will be finished when the queue is
  527. * registered by blk_register_queue().
  528. */
  529. q->bypass_depth = 1;
  530. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  531. init_waitqueue_head(&q->mq_freeze_wq);
  532. if (blkcg_init_queue(q))
  533. goto fail_bdi;
  534. return q;
  535. fail_bdi:
  536. bdi_destroy(&q->backing_dev_info);
  537. fail_id:
  538. ida_simple_remove(&blk_queue_ida, q->id);
  539. fail_c:
  540. percpu_counter_destroy(&q->mq_usage_counter);
  541. fail_q:
  542. kmem_cache_free(blk_requestq_cachep, q);
  543. return NULL;
  544. }
  545. EXPORT_SYMBOL(blk_alloc_queue_node);
  546. /**
  547. * blk_init_queue - prepare a request queue for use with a block device
  548. * @rfn: The function to be called to process requests that have been
  549. * placed on the queue.
  550. * @lock: Request queue spin lock
  551. *
  552. * Description:
  553. * If a block device wishes to use the standard request handling procedures,
  554. * which sorts requests and coalesces adjacent requests, then it must
  555. * call blk_init_queue(). The function @rfn will be called when there
  556. * are requests on the queue that need to be processed. If the device
  557. * supports plugging, then @rfn may not be called immediately when requests
  558. * are available on the queue, but may be called at some time later instead.
  559. * Plugged queues are generally unplugged when a buffer belonging to one
  560. * of the requests on the queue is needed, or due to memory pressure.
  561. *
  562. * @rfn is not required, or even expected, to remove all requests off the
  563. * queue, but only as many as it can handle at a time. If it does leave
  564. * requests on the queue, it is responsible for arranging that the requests
  565. * get dealt with eventually.
  566. *
  567. * The queue spin lock must be held while manipulating the requests on the
  568. * request queue; this lock will be taken also from interrupt context, so irq
  569. * disabling is needed for it.
  570. *
  571. * Function returns a pointer to the initialized request queue, or %NULL if
  572. * it didn't succeed.
  573. *
  574. * Note:
  575. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  576. * when the block device is deactivated (such as at module unload).
  577. **/
  578. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  579. {
  580. return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
  581. }
  582. EXPORT_SYMBOL(blk_init_queue);
  583. struct request_queue *
  584. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  585. {
  586. struct request_queue *uninit_q, *q;
  587. uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
  588. if (!uninit_q)
  589. return NULL;
  590. q = blk_init_allocated_queue(uninit_q, rfn, lock);
  591. if (!q)
  592. blk_cleanup_queue(uninit_q);
  593. return q;
  594. }
  595. EXPORT_SYMBOL(blk_init_queue_node);
  596. struct request_queue *
  597. blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
  598. spinlock_t *lock)
  599. {
  600. if (!q)
  601. return NULL;
  602. if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
  603. return NULL;
  604. q->request_fn = rfn;
  605. q->prep_rq_fn = NULL;
  606. q->unprep_rq_fn = NULL;
  607. q->queue_flags |= QUEUE_FLAG_DEFAULT;
  608. /* Override internal queue lock with supplied lock pointer */
  609. if (lock)
  610. q->queue_lock = lock;
  611. /*
  612. * This also sets hw/phys segments, boundary and size
  613. */
  614. blk_queue_make_request(q, blk_queue_bio);
  615. q->sg_reserved_size = INT_MAX;
  616. /* Protect q->elevator from elevator_change */
  617. mutex_lock(&q->sysfs_lock);
  618. /* init elevator */
  619. if (elevator_init(q, NULL)) {
  620. mutex_unlock(&q->sysfs_lock);
  621. return NULL;
  622. }
  623. mutex_unlock(&q->sysfs_lock);
  624. return q;
  625. }
  626. EXPORT_SYMBOL(blk_init_allocated_queue);
  627. bool blk_get_queue(struct request_queue *q)
  628. {
  629. if (likely(!blk_queue_dying(q))) {
  630. __blk_get_queue(q);
  631. return true;
  632. }
  633. return false;
  634. }
  635. EXPORT_SYMBOL(blk_get_queue);
  636. static inline void blk_free_request(struct request_list *rl, struct request *rq)
  637. {
  638. if (rq->cmd_flags & REQ_ELVPRIV) {
  639. elv_put_request(rl->q, rq);
  640. if (rq->elv.icq)
  641. put_io_context(rq->elv.icq->ioc);
  642. }
  643. mempool_free(rq, rl->rq_pool);
  644. }
  645. /*
  646. * ioc_batching returns true if the ioc is a valid batching request and
  647. * should be given priority access to a request.
  648. */
  649. static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
  650. {
  651. if (!ioc)
  652. return 0;
  653. /*
  654. * Make sure the process is able to allocate at least 1 request
  655. * even if the batch times out, otherwise we could theoretically
  656. * lose wakeups.
  657. */
  658. return ioc->nr_batch_requests == q->nr_batching ||
  659. (ioc->nr_batch_requests > 0
  660. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  661. }
  662. /*
  663. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  664. * will cause the process to be a "batcher" on all queues in the system. This
  665. * is the behaviour we want though - once it gets a wakeup it should be given
  666. * a nice run.
  667. */
  668. static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
  669. {
  670. if (!ioc || ioc_batching(q, ioc))
  671. return;
  672. ioc->nr_batch_requests = q->nr_batching;
  673. ioc->last_waited = jiffies;
  674. }
  675. static void __freed_request(struct request_list *rl, int sync)
  676. {
  677. struct request_queue *q = rl->q;
  678. /*
  679. * bdi isn't aware of blkcg yet. As all async IOs end up root
  680. * blkcg anyway, just use root blkcg state.
  681. */
  682. if (rl == &q->root_rl &&
  683. rl->count[sync] < queue_congestion_off_threshold(q))
  684. blk_clear_queue_congested(q, sync);
  685. if (rl->count[sync] + 1 <= q->nr_requests) {
  686. if (waitqueue_active(&rl->wait[sync]))
  687. wake_up(&rl->wait[sync]);
  688. blk_clear_rl_full(rl, sync);
  689. }
  690. }
  691. /*
  692. * A request has just been released. Account for it, update the full and
  693. * congestion status, wake up any waiters. Called under q->queue_lock.
  694. */
  695. static void freed_request(struct request_list *rl, unsigned int flags)
  696. {
  697. struct request_queue *q = rl->q;
  698. int sync = rw_is_sync(flags);
  699. q->nr_rqs[sync]--;
  700. rl->count[sync]--;
  701. if (flags & REQ_ELVPRIV)
  702. q->nr_rqs_elvpriv--;
  703. __freed_request(rl, sync);
  704. if (unlikely(rl->starved[sync ^ 1]))
  705. __freed_request(rl, sync ^ 1);
  706. }
  707. /*
  708. * Determine if elevator data should be initialized when allocating the
  709. * request associated with @bio.
  710. */
  711. static bool blk_rq_should_init_elevator(struct bio *bio)
  712. {
  713. if (!bio)
  714. return true;
  715. /*
  716. * Flush requests do not use the elevator so skip initialization.
  717. * This allows a request to share the flush and elevator data.
  718. */
  719. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
  720. return false;
  721. return true;
  722. }
  723. /**
  724. * rq_ioc - determine io_context for request allocation
  725. * @bio: request being allocated is for this bio (can be %NULL)
  726. *
  727. * Determine io_context to use for request allocation for @bio. May return
  728. * %NULL if %current->io_context doesn't exist.
  729. */
  730. static struct io_context *rq_ioc(struct bio *bio)
  731. {
  732. #ifdef CONFIG_BLK_CGROUP
  733. if (bio && bio->bi_ioc)
  734. return bio->bi_ioc;
  735. #endif
  736. return current->io_context;
  737. }
  738. /**
  739. * __get_request - get a free request
  740. * @rl: request list to allocate from
  741. * @rw_flags: RW and SYNC flags
  742. * @bio: bio to allocate request for (can be %NULL)
  743. * @gfp_mask: allocation mask
  744. *
  745. * Get a free request from @q. This function may fail under memory
  746. * pressure or if @q is dead.
  747. *
  748. * Must be callled with @q->queue_lock held and,
  749. * Returns %NULL on failure, with @q->queue_lock held.
  750. * Returns !%NULL on success, with @q->queue_lock *not held*.
  751. */
  752. static struct request *__get_request(struct request_list *rl, int rw_flags,
  753. struct bio *bio, gfp_t gfp_mask)
  754. {
  755. struct request_queue *q = rl->q;
  756. struct request *rq;
  757. struct elevator_type *et = q->elevator->type;
  758. struct io_context *ioc = rq_ioc(bio);
  759. struct io_cq *icq = NULL;
  760. const bool is_sync = rw_is_sync(rw_flags) != 0;
  761. int may_queue;
  762. if (unlikely(blk_queue_dying(q)))
  763. return NULL;
  764. may_queue = elv_may_queue(q, rw_flags);
  765. if (may_queue == ELV_MQUEUE_NO)
  766. goto rq_starved;
  767. if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
  768. if (rl->count[is_sync]+1 >= q->nr_requests) {
  769. /*
  770. * The queue will fill after this allocation, so set
  771. * it as full, and mark this process as "batching".
  772. * This process will be allowed to complete a batch of
  773. * requests, others will be blocked.
  774. */
  775. if (!blk_rl_full(rl, is_sync)) {
  776. ioc_set_batching(q, ioc);
  777. blk_set_rl_full(rl, is_sync);
  778. } else {
  779. if (may_queue != ELV_MQUEUE_MUST
  780. && !ioc_batching(q, ioc)) {
  781. /*
  782. * The queue is full and the allocating
  783. * process is not a "batcher", and not
  784. * exempted by the IO scheduler
  785. */
  786. return NULL;
  787. }
  788. }
  789. }
  790. /*
  791. * bdi isn't aware of blkcg yet. As all async IOs end up
  792. * root blkcg anyway, just use root blkcg state.
  793. */
  794. if (rl == &q->root_rl)
  795. blk_set_queue_congested(q, is_sync);
  796. }
  797. /*
  798. * Only allow batching queuers to allocate up to 50% over the defined
  799. * limit of requests, otherwise we could have thousands of requests
  800. * allocated with any setting of ->nr_requests
  801. */
  802. if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
  803. return NULL;
  804. q->nr_rqs[is_sync]++;
  805. rl->count[is_sync]++;
  806. rl->starved[is_sync] = 0;
  807. /*
  808. * Decide whether the new request will be managed by elevator. If
  809. * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
  810. * prevent the current elevator from being destroyed until the new
  811. * request is freed. This guarantees icq's won't be destroyed and
  812. * makes creating new ones safe.
  813. *
  814. * Also, lookup icq while holding queue_lock. If it doesn't exist,
  815. * it will be created after releasing queue_lock.
  816. */
  817. if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
  818. rw_flags |= REQ_ELVPRIV;
  819. q->nr_rqs_elvpriv++;
  820. if (et->icq_cache && ioc)
  821. icq = ioc_lookup_icq(ioc, q);
  822. }
  823. if (blk_queue_io_stat(q))
  824. rw_flags |= REQ_IO_STAT;
  825. spin_unlock_irq(q->queue_lock);
  826. /* allocate and init request */
  827. rq = mempool_alloc(rl->rq_pool, gfp_mask);
  828. if (!rq)
  829. goto fail_alloc;
  830. blk_rq_init(q, rq);
  831. blk_rq_set_rl(rq, rl);
  832. rq->cmd_flags = rw_flags | REQ_ALLOCED;
  833. /* init elvpriv */
  834. if (rw_flags & REQ_ELVPRIV) {
  835. if (unlikely(et->icq_cache && !icq)) {
  836. if (ioc)
  837. icq = ioc_create_icq(ioc, q, gfp_mask);
  838. if (!icq)
  839. goto fail_elvpriv;
  840. }
  841. rq->elv.icq = icq;
  842. if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
  843. goto fail_elvpriv;
  844. /* @rq->elv.icq holds io_context until @rq is freed */
  845. if (icq)
  846. get_io_context(icq->ioc);
  847. }
  848. out:
  849. /*
  850. * ioc may be NULL here, and ioc_batching will be false. That's
  851. * OK, if the queue is under the request limit then requests need
  852. * not count toward the nr_batch_requests limit. There will always
  853. * be some limit enforced by BLK_BATCH_TIME.
  854. */
  855. if (ioc_batching(q, ioc))
  856. ioc->nr_batch_requests--;
  857. trace_block_getrq(q, bio, rw_flags & 1);
  858. return rq;
  859. fail_elvpriv:
  860. /*
  861. * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
  862. * and may fail indefinitely under memory pressure and thus
  863. * shouldn't stall IO. Treat this request as !elvpriv. This will
  864. * disturb iosched and blkcg but weird is bettern than dead.
  865. */
  866. printk_ratelimited(KERN_WARNING "%s: request aux data allocation failed, iosched may be disturbed\n",
  867. dev_name(q->backing_dev_info.dev));
  868. rq->cmd_flags &= ~REQ_ELVPRIV;
  869. rq->elv.icq = NULL;
  870. spin_lock_irq(q->queue_lock);
  871. q->nr_rqs_elvpriv--;
  872. spin_unlock_irq(q->queue_lock);
  873. goto out;
  874. fail_alloc:
  875. /*
  876. * Allocation failed presumably due to memory. Undo anything we
  877. * might have messed up.
  878. *
  879. * Allocating task should really be put onto the front of the wait
  880. * queue, but this is pretty rare.
  881. */
  882. spin_lock_irq(q->queue_lock);
  883. freed_request(rl, rw_flags);
  884. /*
  885. * in the very unlikely event that allocation failed and no
  886. * requests for this direction was pending, mark us starved so that
  887. * freeing of a request in the other direction will notice
  888. * us. another possible fix would be to split the rq mempool into
  889. * READ and WRITE
  890. */
  891. rq_starved:
  892. if (unlikely(rl->count[is_sync] == 0))
  893. rl->starved[is_sync] = 1;
  894. return NULL;
  895. }
  896. /**
  897. * get_request - get a free request
  898. * @q: request_queue to allocate request from
  899. * @rw_flags: RW and SYNC flags
  900. * @bio: bio to allocate request for (can be %NULL)
  901. * @gfp_mask: allocation mask
  902. *
  903. * Get a free request from @q. If %__GFP_WAIT is set in @gfp_mask, this
  904. * function keeps retrying under memory pressure and fails iff @q is dead.
  905. *
  906. * Must be callled with @q->queue_lock held and,
  907. * Returns %NULL on failure, with @q->queue_lock held.
  908. * Returns !%NULL on success, with @q->queue_lock *not held*.
  909. */
  910. static struct request *get_request(struct request_queue *q, int rw_flags,
  911. struct bio *bio, gfp_t gfp_mask)
  912. {
  913. const bool is_sync = rw_is_sync(rw_flags) != 0;
  914. DEFINE_WAIT(wait);
  915. struct request_list *rl;
  916. struct request *rq;
  917. rl = blk_get_rl(q, bio); /* transferred to @rq on success */
  918. retry:
  919. rq = __get_request(rl, rw_flags, bio, gfp_mask);
  920. if (rq)
  921. return rq;
  922. if (!(gfp_mask & __GFP_WAIT) || unlikely(blk_queue_dying(q))) {
  923. blk_put_rl(rl);
  924. return NULL;
  925. }
  926. /* wait on @rl and retry */
  927. prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
  928. TASK_UNINTERRUPTIBLE);
  929. trace_block_sleeprq(q, bio, rw_flags & 1);
  930. spin_unlock_irq(q->queue_lock);
  931. io_schedule();
  932. /*
  933. * After sleeping, we become a "batching" process and will be able
  934. * to allocate at least one request, and up to a big batch of them
  935. * for a small period time. See ioc_batching, ioc_set_batching
  936. */
  937. ioc_set_batching(q, current->io_context);
  938. spin_lock_irq(q->queue_lock);
  939. finish_wait(&rl->wait[is_sync], &wait);
  940. goto retry;
  941. }
  942. static struct request *blk_old_get_request(struct request_queue *q, int rw,
  943. gfp_t gfp_mask)
  944. {
  945. struct request *rq;
  946. BUG_ON(rw != READ && rw != WRITE);
  947. /* create ioc upfront */
  948. create_io_context(gfp_mask, q->node);
  949. spin_lock_irq(q->queue_lock);
  950. rq = get_request(q, rw, NULL, gfp_mask);
  951. if (!rq)
  952. spin_unlock_irq(q->queue_lock);
  953. /* q->queue_lock is unlocked at this point */
  954. return rq;
  955. }
  956. struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
  957. {
  958. if (q->mq_ops)
  959. return blk_mq_alloc_request(q, rw, gfp_mask, false);
  960. else
  961. return blk_old_get_request(q, rw, gfp_mask);
  962. }
  963. EXPORT_SYMBOL(blk_get_request);
  964. /**
  965. * blk_make_request - given a bio, allocate a corresponding struct request.
  966. * @q: target request queue
  967. * @bio: The bio describing the memory mappings that will be submitted for IO.
  968. * It may be a chained-bio properly constructed by block/bio layer.
  969. * @gfp_mask: gfp flags to be used for memory allocation
  970. *
  971. * blk_make_request is the parallel of generic_make_request for BLOCK_PC
  972. * type commands. Where the struct request needs to be farther initialized by
  973. * the caller. It is passed a &struct bio, which describes the memory info of
  974. * the I/O transfer.
  975. *
  976. * The caller of blk_make_request must make sure that bi_io_vec
  977. * are set to describe the memory buffers. That bio_data_dir() will return
  978. * the needed direction of the request. (And all bio's in the passed bio-chain
  979. * are properly set accordingly)
  980. *
  981. * If called under none-sleepable conditions, mapped bio buffers must not
  982. * need bouncing, by calling the appropriate masked or flagged allocator,
  983. * suitable for the target device. Otherwise the call to blk_queue_bounce will
  984. * BUG.
  985. *
  986. * WARNING: When allocating/cloning a bio-chain, careful consideration should be
  987. * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
  988. * anything but the first bio in the chain. Otherwise you risk waiting for IO
  989. * completion of a bio that hasn't been submitted yet, thus resulting in a
  990. * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
  991. * of bio_alloc(), as that avoids the mempool deadlock.
  992. * If possible a big IO should be split into smaller parts when allocation
  993. * fails. Partial allocation should not be an error, or you risk a live-lock.
  994. */
  995. struct request *blk_make_request(struct request_queue *q, struct bio *bio,
  996. gfp_t gfp_mask)
  997. {
  998. struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
  999. if (unlikely(!rq))
  1000. return ERR_PTR(-ENOMEM);
  1001. for_each_bio(bio) {
  1002. struct bio *bounce_bio = bio;
  1003. int ret;
  1004. blk_queue_bounce(q, &bounce_bio);
  1005. ret = blk_rq_append_bio(q, rq, bounce_bio);
  1006. if (unlikely(ret)) {
  1007. blk_put_request(rq);
  1008. return ERR_PTR(ret);
  1009. }
  1010. }
  1011. return rq;
  1012. }
  1013. EXPORT_SYMBOL(blk_make_request);
  1014. /**
  1015. * blk_requeue_request - put a request back on queue
  1016. * @q: request queue where request should be inserted
  1017. * @rq: request to be inserted
  1018. *
  1019. * Description:
  1020. * Drivers often keep queueing requests until the hardware cannot accept
  1021. * more, when that condition happens we need to put the request back
  1022. * on the queue. Must be called with queue lock held.
  1023. */
  1024. void blk_requeue_request(struct request_queue *q, struct request *rq)
  1025. {
  1026. blk_delete_timer(rq);
  1027. blk_clear_rq_complete(rq);
  1028. trace_block_rq_requeue(q, rq);
  1029. if (blk_rq_tagged(rq))
  1030. blk_queue_end_tag(q, rq);
  1031. BUG_ON(blk_queued_rq(rq));
  1032. elv_requeue_request(q, rq);
  1033. }
  1034. EXPORT_SYMBOL(blk_requeue_request);
  1035. static void add_acct_request(struct request_queue *q, struct request *rq,
  1036. int where)
  1037. {
  1038. blk_account_io_start(rq, true);
  1039. __elv_add_request(q, rq, where);
  1040. }
  1041. static void part_round_stats_single(int cpu, struct hd_struct *part,
  1042. unsigned long now)
  1043. {
  1044. if (now == part->stamp)
  1045. return;
  1046. if (part_in_flight(part)) {
  1047. __part_stat_add(cpu, part, time_in_queue,
  1048. part_in_flight(part) * (now - part->stamp));
  1049. __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
  1050. }
  1051. part->stamp = now;
  1052. }
  1053. /**
  1054. * part_round_stats() - Round off the performance stats on a struct disk_stats.
  1055. * @cpu: cpu number for stats access
  1056. * @part: target partition
  1057. *
  1058. * The average IO queue length and utilisation statistics are maintained
  1059. * by observing the current state of the queue length and the amount of
  1060. * time it has been in this state for.
  1061. *
  1062. * Normally, that accounting is done on IO completion, but that can result
  1063. * in more than a second's worth of IO being accounted for within any one
  1064. * second, leading to >100% utilisation. To deal with that, we call this
  1065. * function to do a round-off before returning the results when reading
  1066. * /proc/diskstats. This accounts immediately for all queue usage up to
  1067. * the current jiffies and restarts the counters again.
  1068. */
  1069. void part_round_stats(int cpu, struct hd_struct *part)
  1070. {
  1071. unsigned long now = jiffies;
  1072. if (part->partno)
  1073. part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
  1074. part_round_stats_single(cpu, part, now);
  1075. }
  1076. EXPORT_SYMBOL_GPL(part_round_stats);
  1077. #ifdef CONFIG_PM_RUNTIME
  1078. static void blk_pm_put_request(struct request *rq)
  1079. {
  1080. if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
  1081. pm_runtime_mark_last_busy(rq->q->dev);
  1082. }
  1083. #else
  1084. static inline void blk_pm_put_request(struct request *rq) {}
  1085. #endif
  1086. /*
  1087. * queue lock must be held
  1088. */
  1089. void __blk_put_request(struct request_queue *q, struct request *req)
  1090. {
  1091. if (unlikely(!q))
  1092. return;
  1093. blk_pm_put_request(req);
  1094. elv_completed_request(q, req);
  1095. /* this is a bio leak */
  1096. WARN_ON(req->bio != NULL);
  1097. /*
  1098. * Request may not have originated from ll_rw_blk. if not,
  1099. * it didn't come out of our reserved rq pools
  1100. */
  1101. if (req->cmd_flags & REQ_ALLOCED) {
  1102. unsigned int flags = req->cmd_flags;
  1103. struct request_list *rl = blk_rq_rl(req);
  1104. BUG_ON(!list_empty(&req->queuelist));
  1105. BUG_ON(!hlist_unhashed(&req->hash));
  1106. blk_free_request(rl, req);
  1107. freed_request(rl, flags);
  1108. blk_put_rl(rl);
  1109. }
  1110. }
  1111. EXPORT_SYMBOL_GPL(__blk_put_request);
  1112. void blk_put_request(struct request *req)
  1113. {
  1114. struct request_queue *q = req->q;
  1115. if (q->mq_ops)
  1116. blk_mq_free_request(req);
  1117. else {
  1118. unsigned long flags;
  1119. spin_lock_irqsave(q->queue_lock, flags);
  1120. __blk_put_request(q, req);
  1121. spin_unlock_irqrestore(q->queue_lock, flags);
  1122. }
  1123. }
  1124. EXPORT_SYMBOL(blk_put_request);
  1125. /**
  1126. * blk_add_request_payload - add a payload to a request
  1127. * @rq: request to update
  1128. * @page: page backing the payload
  1129. * @len: length of the payload.
  1130. *
  1131. * This allows to later add a payload to an already submitted request by
  1132. * a block driver. The driver needs to take care of freeing the payload
  1133. * itself.
  1134. *
  1135. * Note that this is a quite horrible hack and nothing but handling of
  1136. * discard requests should ever use it.
  1137. */
  1138. void blk_add_request_payload(struct request *rq, struct page *page,
  1139. unsigned int len)
  1140. {
  1141. struct bio *bio = rq->bio;
  1142. bio->bi_io_vec->bv_page = page;
  1143. bio->bi_io_vec->bv_offset = 0;
  1144. bio->bi_io_vec->bv_len = len;
  1145. bio->bi_size = len;
  1146. bio->bi_vcnt = 1;
  1147. bio->bi_phys_segments = 1;
  1148. rq->__data_len = rq->resid_len = len;
  1149. rq->nr_phys_segments = 1;
  1150. rq->buffer = bio_data(bio);
  1151. }
  1152. EXPORT_SYMBOL_GPL(blk_add_request_payload);
  1153. bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
  1154. struct bio *bio)
  1155. {
  1156. const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
  1157. if (!ll_back_merge_fn(q, req, bio))
  1158. return false;
  1159. trace_block_bio_backmerge(q, req, bio);
  1160. if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
  1161. blk_rq_set_mixed_merge(req);
  1162. req->biotail->bi_next = bio;
  1163. req->biotail = bio;
  1164. req->__data_len += bio->bi_size;
  1165. req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
  1166. blk_account_io_start(req, false);
  1167. return true;
  1168. }
  1169. bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
  1170. struct bio *bio)
  1171. {
  1172. const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
  1173. if (!ll_front_merge_fn(q, req, bio))
  1174. return false;
  1175. trace_block_bio_frontmerge(q, req, bio);
  1176. if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
  1177. blk_rq_set_mixed_merge(req);
  1178. bio->bi_next = req->bio;
  1179. req->bio = bio;
  1180. /*
  1181. * may not be valid. if the low level driver said
  1182. * it didn't need a bounce buffer then it better
  1183. * not touch req->buffer either...
  1184. */
  1185. req->buffer = bio_data(bio);
  1186. req->__sector = bio->bi_sector;
  1187. req->__data_len += bio->bi_size;
  1188. req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
  1189. blk_account_io_start(req, false);
  1190. return true;
  1191. }
  1192. /**
  1193. * blk_attempt_plug_merge - try to merge with %current's plugged list
  1194. * @q: request_queue new bio is being queued at
  1195. * @bio: new bio being queued
  1196. * @request_count: out parameter for number of traversed plugged requests
  1197. *
  1198. * Determine whether @bio being queued on @q can be merged with a request
  1199. * on %current's plugged list. Returns %true if merge was successful,
  1200. * otherwise %false.
  1201. *
  1202. * Plugging coalesces IOs from the same issuer for the same purpose without
  1203. * going through @q->queue_lock. As such it's more of an issuing mechanism
  1204. * than scheduling, and the request, while may have elvpriv data, is not
  1205. * added on the elevator at this point. In addition, we don't have
  1206. * reliable access to the elevator outside queue lock. Only check basic
  1207. * merging parameters without querying the elevator.
  1208. */
  1209. bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
  1210. unsigned int *request_count)
  1211. {
  1212. struct blk_plug *plug;
  1213. struct request *rq;
  1214. bool ret = false;
  1215. struct list_head *plug_list;
  1216. if (blk_queue_nomerges(q))
  1217. goto out;
  1218. plug = current->plug;
  1219. if (!plug)
  1220. goto out;
  1221. *request_count = 0;
  1222. if (q->mq_ops)
  1223. plug_list = &plug->mq_list;
  1224. else
  1225. plug_list = &plug->list;
  1226. list_for_each_entry_reverse(rq, plug_list, queuelist) {
  1227. int el_ret;
  1228. if (rq->q == q)
  1229. (*request_count)++;
  1230. if (rq->q != q || !blk_rq_merge_ok(rq, bio))
  1231. continue;
  1232. el_ret = blk_try_merge(rq, bio);
  1233. if (el_ret == ELEVATOR_BACK_MERGE) {
  1234. ret = bio_attempt_back_merge(q, rq, bio);
  1235. if (ret)
  1236. break;
  1237. } else if (el_ret == ELEVATOR_FRONT_MERGE) {
  1238. ret = bio_attempt_front_merge(q, rq, bio);
  1239. if (ret)
  1240. break;
  1241. }
  1242. }
  1243. out:
  1244. return ret;
  1245. }
  1246. void init_request_from_bio(struct request *req, struct bio *bio)
  1247. {
  1248. req->cmd_type = REQ_TYPE_FS;
  1249. req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
  1250. if (bio->bi_rw & REQ_RAHEAD)
  1251. req->cmd_flags |= REQ_FAILFAST_MASK;
  1252. req->errors = 0;
  1253. req->__sector = bio->bi_sector;
  1254. req->ioprio = bio_prio(bio);
  1255. blk_rq_bio_prep(req->q, req, bio);
  1256. }
  1257. void blk_queue_bio(struct request_queue *q, struct bio *bio)
  1258. {
  1259. const bool sync = !!(bio->bi_rw & REQ_SYNC);
  1260. struct blk_plug *plug;
  1261. int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
  1262. struct request *req;
  1263. unsigned int request_count = 0;
  1264. /*
  1265. * low level driver can indicate that it wants pages above a
  1266. * certain limit bounced to low memory (ie for highmem, or even
  1267. * ISA dma in theory)
  1268. */
  1269. blk_queue_bounce(q, &bio);
  1270. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  1271. bio_endio(bio, -EIO);
  1272. return;
  1273. }
  1274. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
  1275. spin_lock_irq(q->queue_lock);
  1276. where = ELEVATOR_INSERT_FLUSH;
  1277. goto get_rq;
  1278. }
  1279. /*
  1280. * Check if we can merge with the plugged list before grabbing
  1281. * any locks.
  1282. */
  1283. if (blk_attempt_plug_merge(q, bio, &request_count))
  1284. return;
  1285. spin_lock_irq(q->queue_lock);
  1286. el_ret = elv_merge(q, &req, bio);
  1287. if (el_ret == ELEVATOR_BACK_MERGE) {
  1288. if (bio_attempt_back_merge(q, req, bio)) {
  1289. elv_bio_merged(q, req, bio);
  1290. if (!attempt_back_merge(q, req))
  1291. elv_merged_request(q, req, el_ret);
  1292. goto out_unlock;
  1293. }
  1294. } else if (el_ret == ELEVATOR_FRONT_MERGE) {
  1295. if (bio_attempt_front_merge(q, req, bio)) {
  1296. elv_bio_merged(q, req, bio);
  1297. if (!attempt_front_merge(q, req))
  1298. elv_merged_request(q, req, el_ret);
  1299. goto out_unlock;
  1300. }
  1301. }
  1302. get_rq:
  1303. /*
  1304. * This sync check and mask will be re-done in init_request_from_bio(),
  1305. * but we need to set it earlier to expose the sync flag to the
  1306. * rq allocator and io schedulers.
  1307. */
  1308. rw_flags = bio_data_dir(bio);
  1309. if (sync)
  1310. rw_flags |= REQ_SYNC;
  1311. /*
  1312. * Grab a free request. This is might sleep but can not fail.
  1313. * Returns with the queue unlocked.
  1314. */
  1315. req = get_request(q, rw_flags, bio, GFP_NOIO);
  1316. if (unlikely(!req)) {
  1317. bio_endio(bio, -ENODEV); /* @q is dead */
  1318. goto out_unlock;
  1319. }
  1320. /*
  1321. * After dropping the lock and possibly sleeping here, our request
  1322. * may now be mergeable after it had proven unmergeable (above).
  1323. * We don't worry about that case for efficiency. It won't happen
  1324. * often, and the elevators are able to handle it.
  1325. */
  1326. init_request_from_bio(req, bio);
  1327. if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
  1328. req->cpu = raw_smp_processor_id();
  1329. plug = current->plug;
  1330. if (plug) {
  1331. /*
  1332. * If this is the first request added after a plug, fire
  1333. * of a plug trace.
  1334. */
  1335. if (!request_count)
  1336. trace_block_plug(q);
  1337. else {
  1338. if (request_count >= BLK_MAX_REQUEST_COUNT) {
  1339. blk_flush_plug_list(plug, false);
  1340. trace_block_plug(q);
  1341. }
  1342. }
  1343. list_add_tail(&req->queuelist, &plug->list);
  1344. blk_account_io_start(req, true);
  1345. } else {
  1346. spin_lock_irq(q->queue_lock);
  1347. add_acct_request(q, req, where);
  1348. __blk_run_queue(q);
  1349. out_unlock:
  1350. spin_unlock_irq(q->queue_lock);
  1351. }
  1352. }
  1353. EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
  1354. /*
  1355. * If bio->bi_dev is a partition, remap the location
  1356. */
  1357. static inline void blk_partition_remap(struct bio *bio)
  1358. {
  1359. struct block_device *bdev = bio->bi_bdev;
  1360. if (bio_sectors(bio) && bdev != bdev->bd_contains) {
  1361. struct hd_struct *p = bdev->bd_part;
  1362. bio->bi_sector += p->start_sect;
  1363. bio->bi_bdev = bdev->bd_contains;
  1364. trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
  1365. bdev->bd_dev,
  1366. bio->bi_sector - p->start_sect);
  1367. }
  1368. }
  1369. static void handle_bad_sector(struct bio *bio)
  1370. {
  1371. char b[BDEVNAME_SIZE];
  1372. printk(KERN_INFO "attempt to access beyond end of device\n");
  1373. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  1374. bdevname(bio->bi_bdev, b),
  1375. bio->bi_rw,
  1376. (unsigned long long)bio_end_sector(bio),
  1377. (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
  1378. set_bit(BIO_EOF, &bio->bi_flags);
  1379. }
  1380. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1381. static DECLARE_FAULT_ATTR(fail_make_request);
  1382. static int __init setup_fail_make_request(char *str)
  1383. {
  1384. return setup_fault_attr(&fail_make_request, str);
  1385. }
  1386. __setup("fail_make_request=", setup_fail_make_request);
  1387. static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
  1388. {
  1389. return part->make_it_fail && should_fail(&fail_make_request, bytes);
  1390. }
  1391. static int __init fail_make_request_debugfs(void)
  1392. {
  1393. struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
  1394. NULL, &fail_make_request);
  1395. return IS_ERR(dir) ? PTR_ERR(dir) : 0;
  1396. }
  1397. late_initcall(fail_make_request_debugfs);
  1398. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1399. static inline bool should_fail_request(struct hd_struct *part,
  1400. unsigned int bytes)
  1401. {
  1402. return false;
  1403. }
  1404. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1405. /*
  1406. * Check whether this bio extends beyond the end of the device.
  1407. */
  1408. static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
  1409. {
  1410. sector_t maxsector;
  1411. if (!nr_sectors)
  1412. return 0;
  1413. /* Test device or partition size, when known. */
  1414. maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
  1415. if (maxsector) {
  1416. sector_t sector = bio->bi_sector;
  1417. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  1418. /*
  1419. * This may well happen - the kernel calls bread()
  1420. * without checking the size of the device, e.g., when
  1421. * mounting a device.
  1422. */
  1423. handle_bad_sector(bio);
  1424. return 1;
  1425. }
  1426. }
  1427. return 0;
  1428. }
  1429. static noinline_for_stack bool
  1430. generic_make_request_checks(struct bio *bio)
  1431. {
  1432. struct request_queue *q;
  1433. int nr_sectors = bio_sectors(bio);
  1434. int err = -EIO;
  1435. char b[BDEVNAME_SIZE];
  1436. struct hd_struct *part;
  1437. might_sleep();
  1438. if (bio_check_eod(bio, nr_sectors))
  1439. goto end_io;
  1440. q = bdev_get_queue(bio->bi_bdev);
  1441. if (unlikely(!q)) {
  1442. printk(KERN_ERR
  1443. "generic_make_request: Trying to access "
  1444. "nonexistent block-device %s (%Lu)\n",
  1445. bdevname(bio->bi_bdev, b),
  1446. (long long) bio->bi_sector);
  1447. goto end_io;
  1448. }
  1449. if (likely(bio_is_rw(bio) &&
  1450. nr_sectors > queue_max_hw_sectors(q))) {
  1451. printk(KERN_ERR "bio too big device %s (%u > %u)\n",
  1452. bdevname(bio->bi_bdev, b),
  1453. bio_sectors(bio),
  1454. queue_max_hw_sectors(q));
  1455. goto end_io;
  1456. }
  1457. part = bio->bi_bdev->bd_part;
  1458. if (should_fail_request(part, bio->bi_size) ||
  1459. should_fail_request(&part_to_disk(part)->part0,
  1460. bio->bi_size))
  1461. goto end_io;
  1462. /*
  1463. * If this device has partitions, remap block n
  1464. * of partition p to block n+start(p) of the disk.
  1465. */
  1466. blk_partition_remap(bio);
  1467. if (bio_check_eod(bio, nr_sectors))
  1468. goto end_io;
  1469. /*
  1470. * Filter flush bio's early so that make_request based
  1471. * drivers without flush support don't have to worry
  1472. * about them.
  1473. */
  1474. if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
  1475. bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
  1476. if (!nr_sectors) {
  1477. err = 0;
  1478. goto end_io;
  1479. }
  1480. }
  1481. if ((bio->bi_rw & REQ_DISCARD) &&
  1482. (!blk_queue_discard(q) ||
  1483. ((bio->bi_rw & REQ_SECURE) && !blk_queue_secdiscard(q)))) {
  1484. err = -EOPNOTSUPP;
  1485. goto end_io;
  1486. }
  1487. if (bio->bi_rw & REQ_WRITE_SAME && !bdev_write_same(bio->bi_bdev)) {
  1488. err = -EOPNOTSUPP;
  1489. goto end_io;
  1490. }
  1491. /*
  1492. * Various block parts want %current->io_context and lazy ioc
  1493. * allocation ends up trading a lot of pain for a small amount of
  1494. * memory. Just allocate it upfront. This may fail and block
  1495. * layer knows how to live with it.
  1496. */
  1497. create_io_context(GFP_ATOMIC, q->node);
  1498. if (blk_throtl_bio(q, bio))
  1499. return false; /* throttled, will be resubmitted later */
  1500. trace_block_bio_queue(q, bio);
  1501. return true;
  1502. end_io:
  1503. bio_endio(bio, err);
  1504. return false;
  1505. }
  1506. /**
  1507. * generic_make_request - hand a buffer to its device driver for I/O
  1508. * @bio: The bio describing the location in memory and on the device.
  1509. *
  1510. * generic_make_request() is used to make I/O requests of block
  1511. * devices. It is passed a &struct bio, which describes the I/O that needs
  1512. * to be done.
  1513. *
  1514. * generic_make_request() does not return any status. The
  1515. * success/failure status of the request, along with notification of
  1516. * completion, is delivered asynchronously through the bio->bi_end_io
  1517. * function described (one day) else where.
  1518. *
  1519. * The caller of generic_make_request must make sure that bi_io_vec
  1520. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  1521. * set to describe the device address, and the
  1522. * bi_end_io and optionally bi_private are set to describe how
  1523. * completion notification should be signaled.
  1524. *
  1525. * generic_make_request and the drivers it calls may use bi_next if this
  1526. * bio happens to be merged with someone else, and may resubmit the bio to
  1527. * a lower device by calling into generic_make_request recursively, which
  1528. * means the bio should NOT be touched after the call to ->make_request_fn.
  1529. */
  1530. void generic_make_request(struct bio *bio)
  1531. {
  1532. struct bio_list bio_list_on_stack;
  1533. if (!generic_make_request_checks(bio))
  1534. return;
  1535. /*
  1536. * We only want one ->make_request_fn to be active at a time, else
  1537. * stack usage with stacked devices could be a problem. So use
  1538. * current->bio_list to keep a list of requests submited by a
  1539. * make_request_fn function. current->bio_list is also used as a
  1540. * flag to say if generic_make_request is currently active in this
  1541. * task or not. If it is NULL, then no make_request is active. If
  1542. * it is non-NULL, then a make_request is active, and new requests
  1543. * should be added at the tail
  1544. */
  1545. if (current->bio_list) {
  1546. bio_list_add(current->bio_list, bio);
  1547. return;
  1548. }
  1549. /* following loop may be a bit non-obvious, and so deserves some
  1550. * explanation.
  1551. * Before entering the loop, bio->bi_next is NULL (as all callers
  1552. * ensure that) so we have a list with a single bio.
  1553. * We pretend that we have just taken it off a longer list, so
  1554. * we assign bio_list to a pointer to the bio_list_on_stack,
  1555. * thus initialising the bio_list of new bios to be
  1556. * added. ->make_request() may indeed add some more bios
  1557. * through a recursive call to generic_make_request. If it
  1558. * did, we find a non-NULL value in bio_list and re-enter the loop
  1559. * from the top. In this case we really did just take the bio
  1560. * of the top of the list (no pretending) and so remove it from
  1561. * bio_list, and call into ->make_request() again.
  1562. */
  1563. BUG_ON(bio->bi_next);
  1564. bio_list_init(&bio_list_on_stack);
  1565. current->bio_list = &bio_list_on_stack;
  1566. do {
  1567. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  1568. q->make_request_fn(q, bio);
  1569. bio = bio_list_pop(current->bio_list);
  1570. } while (bio);
  1571. current->bio_list = NULL; /* deactivate */
  1572. }
  1573. EXPORT_SYMBOL(generic_make_request);
  1574. /**
  1575. * submit_bio - submit a bio to the block device layer for I/O
  1576. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  1577. * @bio: The &struct bio which describes the I/O
  1578. *
  1579. * submit_bio() is very similar in purpose to generic_make_request(), and
  1580. * uses that function to do most of the work. Both are fairly rough
  1581. * interfaces; @bio must be presetup and ready for I/O.
  1582. *
  1583. */
  1584. void submit_bio(int rw, struct bio *bio)
  1585. {
  1586. bio->bi_rw |= rw;
  1587. /*
  1588. * If it's a regular read/write or a barrier with data attached,
  1589. * go through the normal accounting stuff before submission.
  1590. */
  1591. if (bio_has_data(bio)) {
  1592. unsigned int count;
  1593. if (unlikely(rw & REQ_WRITE_SAME))
  1594. count = bdev_logical_block_size(bio->bi_bdev) >> 9;
  1595. else
  1596. count = bio_sectors(bio);
  1597. if (rw & WRITE) {
  1598. count_vm_events(PGPGOUT, count);
  1599. } else {
  1600. task_io_account_read(bio->bi_size);
  1601. count_vm_events(PGPGIN, count);
  1602. }
  1603. if (unlikely(block_dump)) {
  1604. char b[BDEVNAME_SIZE];
  1605. printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
  1606. current->comm, task_pid_nr(current),
  1607. (rw & WRITE) ? "WRITE" : "READ",
  1608. (unsigned long long)bio->bi_sector,
  1609. bdevname(bio->bi_bdev, b),
  1610. count);
  1611. }
  1612. }
  1613. generic_make_request(bio);
  1614. }
  1615. EXPORT_SYMBOL(submit_bio);
  1616. /**
  1617. * blk_rq_check_limits - Helper function to check a request for the queue limit
  1618. * @q: the queue
  1619. * @rq: the request being checked
  1620. *
  1621. * Description:
  1622. * @rq may have been made based on weaker limitations of upper-level queues
  1623. * in request stacking drivers, and it may violate the limitation of @q.
  1624. * Since the block layer and the underlying device driver trust @rq
  1625. * after it is inserted to @q, it should be checked against @q before
  1626. * the insertion using this generic function.
  1627. *
  1628. * This function should also be useful for request stacking drivers
  1629. * in some cases below, so export this function.
  1630. * Request stacking drivers like request-based dm may change the queue
  1631. * limits while requests are in the queue (e.g. dm's table swapping).
  1632. * Such request stacking drivers should check those requests agaist
  1633. * the new queue limits again when they dispatch those requests,
  1634. * although such checkings are also done against the old queue limits
  1635. * when submitting requests.
  1636. */
  1637. int blk_rq_check_limits(struct request_queue *q, struct request *rq)
  1638. {
  1639. if (!rq_mergeable(rq))
  1640. return 0;
  1641. if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, rq->cmd_flags)) {
  1642. printk(KERN_ERR "%s: over max size limit.\n", __func__);
  1643. return -EIO;
  1644. }
  1645. /*
  1646. * queue's settings related to segment counting like q->bounce_pfn
  1647. * may differ from that of other stacking queues.
  1648. * Recalculate it to check the request correctly on this queue's
  1649. * limitation.
  1650. */
  1651. blk_recalc_rq_segments(rq);
  1652. if (rq->nr_phys_segments > queue_max_segments(q)) {
  1653. printk(KERN_ERR "%s: over max segments limit.\n", __func__);
  1654. return -EIO;
  1655. }
  1656. return 0;
  1657. }
  1658. EXPORT_SYMBOL_GPL(blk_rq_check_limits);
  1659. /**
  1660. * blk_insert_cloned_request - Helper for stacking drivers to submit a request
  1661. * @q: the queue to submit the request
  1662. * @rq: the request being queued
  1663. */
  1664. int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
  1665. {
  1666. unsigned long flags;
  1667. int where = ELEVATOR_INSERT_BACK;
  1668. if (blk_rq_check_limits(q, rq))
  1669. return -EIO;
  1670. if (rq->rq_disk &&
  1671. should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
  1672. return -EIO;
  1673. spin_lock_irqsave(q->queue_lock, flags);
  1674. if (unlikely(blk_queue_dying(q))) {
  1675. spin_unlock_irqrestore(q->queue_lock, flags);
  1676. return -ENODEV;
  1677. }
  1678. /*
  1679. * Submitting request must be dequeued before calling this function
  1680. * because it will be linked to another request_queue
  1681. */
  1682. BUG_ON(blk_queued_rq(rq));
  1683. if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
  1684. where = ELEVATOR_INSERT_FLUSH;
  1685. add_acct_request(q, rq, where);
  1686. if (where == ELEVATOR_INSERT_FLUSH)
  1687. __blk_run_queue(q);
  1688. spin_unlock_irqrestore(q->queue_lock, flags);
  1689. return 0;
  1690. }
  1691. EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
  1692. /**
  1693. * blk_rq_err_bytes - determine number of bytes till the next failure boundary
  1694. * @rq: request to examine
  1695. *
  1696. * Description:
  1697. * A request could be merge of IOs which require different failure
  1698. * handling. This function determines the number of bytes which
  1699. * can be failed from the beginning of the request without
  1700. * crossing into area which need to be retried further.
  1701. *
  1702. * Return:
  1703. * The number of bytes to fail.
  1704. *
  1705. * Context:
  1706. * queue_lock must be held.
  1707. */
  1708. unsigned int blk_rq_err_bytes(const struct request *rq)
  1709. {
  1710. unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
  1711. unsigned int bytes = 0;
  1712. struct bio *bio;
  1713. if (!(rq->cmd_flags & REQ_MIXED_MERGE))
  1714. return blk_rq_bytes(rq);
  1715. /*
  1716. * Currently the only 'mixing' which can happen is between
  1717. * different fastfail types. We can safely fail portions
  1718. * which have all the failfast bits that the first one has -
  1719. * the ones which are at least as eager to fail as the first
  1720. * one.
  1721. */
  1722. for (bio = rq->bio; bio; bio = bio->bi_next) {
  1723. if ((bio->bi_rw & ff) != ff)
  1724. break;
  1725. bytes += bio->bi_size;
  1726. }
  1727. /* this could lead to infinite loop */
  1728. BUG_ON(blk_rq_bytes(rq) && !bytes);
  1729. return bytes;
  1730. }
  1731. EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
  1732. void blk_account_io_completion(struct request *req, unsigned int bytes)
  1733. {
  1734. if (blk_do_io_stat(req)) {
  1735. const int rw = rq_data_dir(req);
  1736. struct hd_struct *part;
  1737. int cpu;
  1738. cpu = part_stat_lock();
  1739. part = req->part;
  1740. part_stat_add(cpu, part, sectors[rw], bytes >> 9);
  1741. part_stat_unlock();
  1742. }
  1743. }
  1744. void blk_account_io_done(struct request *req)
  1745. {
  1746. /*
  1747. * Account IO completion. flush_rq isn't accounted as a
  1748. * normal IO on queueing nor completion. Accounting the
  1749. * containing request is enough.
  1750. */
  1751. if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
  1752. unsigned long duration = jiffies - req->start_time;
  1753. const int rw = rq_data_dir(req);
  1754. struct hd_struct *part;
  1755. int cpu;
  1756. cpu = part_stat_lock();
  1757. part = req->part;
  1758. part_stat_inc(cpu, part, ios[rw]);
  1759. part_stat_add(cpu, part, ticks[rw], duration);
  1760. part_round_stats(cpu, part);
  1761. part_dec_in_flight(part, rw);
  1762. hd_struct_put(part);
  1763. part_stat_unlock();
  1764. }
  1765. }
  1766. #ifdef CONFIG_PM_RUNTIME
  1767. /*
  1768. * Don't process normal requests when queue is suspended
  1769. * or in the process of suspending/resuming
  1770. */
  1771. static struct request *blk_pm_peek_request(struct request_queue *q,
  1772. struct request *rq)
  1773. {
  1774. if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
  1775. (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
  1776. return NULL;
  1777. else
  1778. return rq;
  1779. }
  1780. #else
  1781. static inline struct request *blk_pm_peek_request(struct request_queue *q,
  1782. struct request *rq)
  1783. {
  1784. return rq;
  1785. }
  1786. #endif
  1787. void blk_account_io_start(struct request *rq, bool new_io)
  1788. {
  1789. struct hd_struct *part;
  1790. int rw = rq_data_dir(rq);
  1791. int cpu;
  1792. if (!blk_do_io_stat(rq))
  1793. return;
  1794. cpu = part_stat_lock();
  1795. if (!new_io) {
  1796. part = rq->part;
  1797. part_stat_inc(cpu, part, merges[rw]);
  1798. } else {
  1799. part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
  1800. if (!hd_struct_try_get(part)) {
  1801. /*
  1802. * The partition is already being removed,
  1803. * the request will be accounted on the disk only
  1804. *
  1805. * We take a reference on disk->part0 although that
  1806. * partition will never be deleted, so we can treat
  1807. * it as any other partition.
  1808. */
  1809. part = &rq->rq_disk->part0;
  1810. hd_struct_get(part);
  1811. }
  1812. part_round_stats(cpu, part);
  1813. part_inc_in_flight(part, rw);
  1814. rq->part = part;
  1815. }
  1816. part_stat_unlock();
  1817. }
  1818. /**
  1819. * blk_peek_request - peek at the top of a request queue
  1820. * @q: request queue to peek at
  1821. *
  1822. * Description:
  1823. * Return the request at the top of @q. The returned request
  1824. * should be started using blk_start_request() before LLD starts
  1825. * processing it.
  1826. *
  1827. * Return:
  1828. * Pointer to the request at the top of @q if available. Null
  1829. * otherwise.
  1830. *
  1831. * Context:
  1832. * queue_lock must be held.
  1833. */
  1834. struct request *blk_peek_request(struct request_queue *q)
  1835. {
  1836. struct request *rq;
  1837. int ret;
  1838. while ((rq = __elv_next_request(q)) != NULL) {
  1839. rq = blk_pm_peek_request(q, rq);
  1840. if (!rq)
  1841. break;
  1842. if (!(rq->cmd_flags & REQ_STARTED)) {
  1843. /*
  1844. * This is the first time the device driver
  1845. * sees this request (possibly after
  1846. * requeueing). Notify IO scheduler.
  1847. */
  1848. if (rq->cmd_flags & REQ_SORTED)
  1849. elv_activate_rq(q, rq);
  1850. /*
  1851. * just mark as started even if we don't start
  1852. * it, a request that has been delayed should
  1853. * not be passed by new incoming requests
  1854. */
  1855. rq->cmd_flags |= REQ_STARTED;
  1856. trace_block_rq_issue(q, rq);
  1857. }
  1858. if (!q->boundary_rq || q->boundary_rq == rq) {
  1859. q->end_sector = rq_end_sector(rq);
  1860. q->boundary_rq = NULL;
  1861. }
  1862. if (rq->cmd_flags & REQ_DONTPREP)
  1863. break;
  1864. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  1865. /*
  1866. * make sure space for the drain appears we
  1867. * know we can do this because max_hw_segments
  1868. * has been adjusted to be one fewer than the
  1869. * device can handle
  1870. */
  1871. rq->nr_phys_segments++;
  1872. }
  1873. if (!q->prep_rq_fn)
  1874. break;
  1875. ret = q->prep_rq_fn(q, rq);
  1876. if (ret == BLKPREP_OK) {
  1877. break;
  1878. } else if (ret == BLKPREP_DEFER) {
  1879. /*
  1880. * the request may have been (partially) prepped.
  1881. * we need to keep this request in the front to
  1882. * avoid resource deadlock. REQ_STARTED will
  1883. * prevent other fs requests from passing this one.
  1884. */
  1885. if (q->dma_drain_size && blk_rq_bytes(rq) &&
  1886. !(rq->cmd_flags & REQ_DONTPREP)) {
  1887. /*
  1888. * remove the space for the drain we added
  1889. * so that we don't add it again
  1890. */
  1891. --rq->nr_phys_segments;
  1892. }
  1893. rq = NULL;
  1894. break;
  1895. } else if (ret == BLKPREP_KILL) {
  1896. rq->cmd_flags |= REQ_QUIET;
  1897. /*
  1898. * Mark this request as started so we don't trigger
  1899. * any debug logic in the end I/O path.
  1900. */
  1901. blk_start_request(rq);
  1902. __blk_end_request_all(rq, -EIO);
  1903. } else {
  1904. printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
  1905. break;
  1906. }
  1907. }
  1908. return rq;
  1909. }
  1910. EXPORT_SYMBOL(blk_peek_request);
  1911. void blk_dequeue_request(struct request *rq)
  1912. {
  1913. struct request_queue *q = rq->q;
  1914. BUG_ON(list_empty(&rq->queuelist));
  1915. BUG_ON(ELV_ON_HASH(rq));
  1916. list_del_init(&rq->queuelist);
  1917. /*
  1918. * the time frame between a request being removed from the lists
  1919. * and to it is freed is accounted as io that is in progress at
  1920. * the driver side.
  1921. */
  1922. if (blk_account_rq(rq)) {
  1923. q->in_flight[rq_is_sync(rq)]++;
  1924. set_io_start_time_ns(rq);
  1925. }
  1926. }
  1927. /**
  1928. * blk_start_request - start request processing on the driver
  1929. * @req: request to dequeue
  1930. *
  1931. * Description:
  1932. * Dequeue @req and start timeout timer on it. This hands off the
  1933. * request to the driver.
  1934. *
  1935. * Block internal functions which don't want to start timer should
  1936. * call blk_dequeue_request().
  1937. *
  1938. * Context:
  1939. * queue_lock must be held.
  1940. */
  1941. void blk_start_request(struct request *req)
  1942. {
  1943. blk_dequeue_request(req);
  1944. /*
  1945. * We are now handing the request to the hardware, initialize
  1946. * resid_len to full count and add the timeout handler.
  1947. */
  1948. req->resid_len = blk_rq_bytes(req);
  1949. if (unlikely(blk_bidi_rq(req)))
  1950. req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
  1951. BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
  1952. blk_add_timer(req);
  1953. }
  1954. EXPORT_SYMBOL(blk_start_request);
  1955. /**
  1956. * blk_fetch_request - fetch a request from a request queue
  1957. * @q: request queue to fetch a request from
  1958. *
  1959. * Description:
  1960. * Return the request at the top of @q. The request is started on
  1961. * return and LLD can start processing it immediately.
  1962. *
  1963. * Return:
  1964. * Pointer to the request at the top of @q if available. Null
  1965. * otherwise.
  1966. *
  1967. * Context:
  1968. * queue_lock must be held.
  1969. */
  1970. struct request *blk_fetch_request(struct request_queue *q)
  1971. {
  1972. struct request *rq;
  1973. rq = blk_peek_request(q);
  1974. if (rq)
  1975. blk_start_request(rq);
  1976. return rq;
  1977. }
  1978. EXPORT_SYMBOL(blk_fetch_request);
  1979. /**
  1980. * blk_update_request - Special helper function for request stacking drivers
  1981. * @req: the request being processed
  1982. * @error: %0 for success, < %0 for error
  1983. * @nr_bytes: number of bytes to complete @req
  1984. *
  1985. * Description:
  1986. * Ends I/O on a number of bytes attached to @req, but doesn't complete
  1987. * the request structure even if @req doesn't have leftover.
  1988. * If @req has leftover, sets it up for the next range of segments.
  1989. *
  1990. * This special helper function is only for request stacking drivers
  1991. * (e.g. request-based dm) so that they can handle partial completion.
  1992. * Actual device drivers should use blk_end_request instead.
  1993. *
  1994. * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
  1995. * %false return from this function.
  1996. *
  1997. * Return:
  1998. * %false - this request doesn't have any more data
  1999. * %true - this request has more data
  2000. **/
  2001. bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
  2002. {
  2003. int total_bytes;
  2004. if (!req->bio)
  2005. return false;
  2006. trace_block_rq_complete(req->q, req);
  2007. /*
  2008. * For fs requests, rq is just carrier of independent bio's
  2009. * and each partial completion should be handled separately.
  2010. * Reset per-request error on each partial completion.
  2011. *
  2012. * TODO: tj: This is too subtle. It would be better to let
  2013. * low level drivers do what they see fit.
  2014. */
  2015. if (req->cmd_type == REQ_TYPE_FS)
  2016. req->errors = 0;
  2017. if (error && req->cmd_type == REQ_TYPE_FS &&
  2018. !(req->cmd_flags & REQ_QUIET)) {
  2019. char *error_type;
  2020. switch (error) {
  2021. case -ENOLINK:
  2022. error_type = "recoverable transport";
  2023. break;
  2024. case -EREMOTEIO:
  2025. error_type = "critical target";
  2026. break;
  2027. case -EBADE:
  2028. error_type = "critical nexus";
  2029. break;
  2030. case -ETIMEDOUT:
  2031. error_type = "timeout";
  2032. break;
  2033. case -ENOSPC:
  2034. error_type = "critical space allocation";
  2035. break;
  2036. case -ENODATA:
  2037. error_type = "critical medium";
  2038. break;
  2039. case -EIO:
  2040. default:
  2041. error_type = "I/O";
  2042. break;
  2043. }
  2044. printk_ratelimited(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
  2045. error_type, req->rq_disk ?
  2046. req->rq_disk->disk_name : "?",
  2047. (unsigned long long)blk_rq_pos(req));
  2048. }
  2049. blk_account_io_completion(req, nr_bytes);
  2050. total_bytes = 0;
  2051. while (req->bio) {
  2052. struct bio *bio = req->bio;
  2053. unsigned bio_bytes = min(bio->bi_size, nr_bytes);
  2054. if (bio_bytes == bio->bi_size)
  2055. req->bio = bio->bi_next;
  2056. req_bio_endio(req, bio, bio_bytes, error);
  2057. total_bytes += bio_bytes;
  2058. nr_bytes -= bio_bytes;
  2059. if (!nr_bytes)
  2060. break;
  2061. }
  2062. /*
  2063. * completely done
  2064. */
  2065. if (!req->bio) {
  2066. /*
  2067. * Reset counters so that the request stacking driver
  2068. * can find how many bytes remain in the request
  2069. * later.
  2070. */
  2071. req->__data_len = 0;
  2072. return false;
  2073. }
  2074. req->__data_len -= total_bytes;
  2075. req->buffer = bio_data(req->bio);
  2076. /* update sector only for requests with clear definition of sector */
  2077. if (req->cmd_type == REQ_TYPE_FS)
  2078. req->__sector += total_bytes >> 9;
  2079. /* mixed attributes always follow the first bio */
  2080. if (req->cmd_flags & REQ_MIXED_MERGE) {
  2081. req->cmd_flags &= ~REQ_FAILFAST_MASK;
  2082. req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
  2083. }
  2084. /*
  2085. * If total number of sectors is less than the first segment
  2086. * size, something has gone terribly wrong.
  2087. */
  2088. if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
  2089. blk_dump_rq_flags(req, "request botched");
  2090. req->__data_len = blk_rq_cur_bytes(req);
  2091. }
  2092. /* recalculate the number of segments */
  2093. blk_recalc_rq_segments(req);
  2094. return true;
  2095. }
  2096. EXPORT_SYMBOL_GPL(blk_update_request);
  2097. static bool blk_update_bidi_request(struct request *rq, int error,
  2098. unsigned int nr_bytes,
  2099. unsigned int bidi_bytes)
  2100. {
  2101. if (blk_update_request(rq, error, nr_bytes))
  2102. return true;
  2103. /* Bidi request must be completed as a whole */
  2104. if (unlikely(blk_bidi_rq(rq)) &&
  2105. blk_update_request(rq->next_rq, error, bidi_bytes))
  2106. return true;
  2107. if (blk_queue_add_random(rq->q))
  2108. add_disk_randomness(rq->rq_disk);
  2109. return false;
  2110. }
  2111. /**
  2112. * blk_unprep_request - unprepare a request
  2113. * @req: the request
  2114. *
  2115. * This function makes a request ready for complete resubmission (or
  2116. * completion). It happens only after all error handling is complete,
  2117. * so represents the appropriate moment to deallocate any resources
  2118. * that were allocated to the request in the prep_rq_fn. The queue
  2119. * lock is held when calling this.
  2120. */
  2121. void blk_unprep_request(struct request *req)
  2122. {
  2123. struct request_queue *q = req->q;
  2124. req->cmd_flags &= ~REQ_DONTPREP;
  2125. if (q->unprep_rq_fn)
  2126. q->unprep_rq_fn(q, req);
  2127. }
  2128. EXPORT_SYMBOL_GPL(blk_unprep_request);
  2129. /*
  2130. * queue lock must be held
  2131. */
  2132. static void blk_finish_request(struct request *req, int error)
  2133. {
  2134. if (blk_rq_tagged(req))
  2135. blk_queue_end_tag(req->q, req);
  2136. BUG_ON(blk_queued_rq(req));
  2137. if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
  2138. laptop_io_completion(&req->q->backing_dev_info);
  2139. blk_delete_timer(req);
  2140. if (req->cmd_flags & REQ_DONTPREP)
  2141. blk_unprep_request(req);
  2142. blk_account_io_done(req);
  2143. if (req->end_io)
  2144. req->end_io(req, error);
  2145. else {
  2146. if (blk_bidi_rq(req))
  2147. __blk_put_request(req->next_rq->q, req->next_rq);
  2148. __blk_put_request(req->q, req);
  2149. }
  2150. }
  2151. /**
  2152. * blk_end_bidi_request - Complete a bidi request
  2153. * @rq: the request to complete
  2154. * @error: %0 for success, < %0 for error
  2155. * @nr_bytes: number of bytes to complete @rq
  2156. * @bidi_bytes: number of bytes to complete @rq->next_rq
  2157. *
  2158. * Description:
  2159. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  2160. * Drivers that supports bidi can safely call this member for any
  2161. * type of request, bidi or uni. In the later case @bidi_bytes is
  2162. * just ignored.
  2163. *
  2164. * Return:
  2165. * %false - we are done with this request
  2166. * %true - still buffers pending for this request
  2167. **/
  2168. static bool blk_end_bidi_request(struct request *rq, int error,
  2169. unsigned int nr_bytes, unsigned int bidi_bytes)
  2170. {
  2171. struct request_queue *q = rq->q;
  2172. unsigned long flags;
  2173. if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
  2174. return true;
  2175. spin_lock_irqsave(q->queue_lock, flags);
  2176. blk_finish_request(rq, error);
  2177. spin_unlock_irqrestore(q->queue_lock, flags);
  2178. return false;
  2179. }
  2180. /**
  2181. * __blk_end_bidi_request - Complete a bidi request with queue lock held
  2182. * @rq: the request to complete
  2183. * @error: %0 for success, < %0 for error
  2184. * @nr_bytes: number of bytes to complete @rq
  2185. * @bidi_bytes: number of bytes to complete @rq->next_rq
  2186. *
  2187. * Description:
  2188. * Identical to blk_end_bidi_request() except that queue lock is
  2189. * assumed to be locked on entry and remains so on return.
  2190. *
  2191. * Return:
  2192. * %false - we are done with this request
  2193. * %true - still buffers pending for this request
  2194. **/
  2195. bool __blk_end_bidi_request(struct request *rq, int error,
  2196. unsigned int nr_bytes, unsigned int bidi_bytes)
  2197. {
  2198. if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
  2199. return true;
  2200. blk_finish_request(rq, error);
  2201. return false;
  2202. }
  2203. /**
  2204. * blk_end_request - Helper function for drivers to complete the request.
  2205. * @rq: the request being processed
  2206. * @error: %0 for success, < %0 for error
  2207. * @nr_bytes: number of bytes to complete
  2208. *
  2209. * Description:
  2210. * Ends I/O on a number of bytes attached to @rq.
  2211. * If @rq has leftover, sets it up for the next range of segments.
  2212. *
  2213. * Return:
  2214. * %false - we are done with this request
  2215. * %true - still buffers pending for this request
  2216. **/
  2217. bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  2218. {
  2219. return blk_end_bidi_request(rq, error, nr_bytes, 0);
  2220. }
  2221. EXPORT_SYMBOL(blk_end_request);
  2222. /**
  2223. * blk_end_request_all - Helper function for drives to finish the request.
  2224. * @rq: the request to finish
  2225. * @error: %0 for success, < %0 for error
  2226. *
  2227. * Description:
  2228. * Completely finish @rq.
  2229. */
  2230. void blk_end_request_all(struct request *rq, int error)
  2231. {
  2232. bool pending;
  2233. unsigned int bidi_bytes = 0;
  2234. if (unlikely(blk_bidi_rq(rq)))
  2235. bidi_bytes = blk_rq_bytes(rq->next_rq);
  2236. pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
  2237. BUG_ON(pending);
  2238. }
  2239. EXPORT_SYMBOL(blk_end_request_all);
  2240. /**
  2241. * blk_end_request_cur - Helper function to finish the current request chunk.
  2242. * @rq: the request to finish the current chunk for
  2243. * @error: %0 for success, < %0 for error
  2244. *
  2245. * Description:
  2246. * Complete the current consecutively mapped chunk from @rq.
  2247. *
  2248. * Return:
  2249. * %false - we are done with this request
  2250. * %true - still buffers pending for this request
  2251. */
  2252. bool blk_end_request_cur(struct request *rq, int error)
  2253. {
  2254. return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
  2255. }
  2256. EXPORT_SYMBOL(blk_end_request_cur);
  2257. /**
  2258. * blk_end_request_err - Finish a request till the next failure boundary.
  2259. * @rq: the request to finish till the next failure boundary for
  2260. * @error: must be negative errno
  2261. *
  2262. * Description:
  2263. * Complete @rq till the next failure boundary.
  2264. *
  2265. * Return:
  2266. * %false - we are done with this request
  2267. * %true - still buffers pending for this request
  2268. */
  2269. bool blk_end_request_err(struct request *rq, int error)
  2270. {
  2271. WARN_ON(error >= 0);
  2272. return blk_end_request(rq, error, blk_rq_err_bytes(rq));
  2273. }
  2274. EXPORT_SYMBOL_GPL(blk_end_request_err);
  2275. /**
  2276. * __blk_end_request - Helper function for drivers to complete the request.
  2277. * @rq: the request being processed
  2278. * @error: %0 for success, < %0 for error
  2279. * @nr_bytes: number of bytes to complete
  2280. *
  2281. * Description:
  2282. * Must be called with queue lock held unlike blk_end_request().
  2283. *
  2284. * Return:
  2285. * %false - we are done with this request
  2286. * %true - still buffers pending for this request
  2287. **/
  2288. bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  2289. {
  2290. return __blk_end_bidi_request(rq, error, nr_bytes, 0);
  2291. }
  2292. EXPORT_SYMBOL(__blk_end_request);
  2293. /**
  2294. * __blk_end_request_all - Helper function for drives to finish the request.
  2295. * @rq: the request to finish
  2296. * @error: %0 for success, < %0 for error
  2297. *
  2298. * Description:
  2299. * Completely finish @rq. Must be called with queue lock held.
  2300. */
  2301. void __blk_end_request_all(struct request *rq, int error)
  2302. {
  2303. bool pending;
  2304. unsigned int bidi_bytes = 0;
  2305. if (unlikely(blk_bidi_rq(rq)))
  2306. bidi_bytes = blk_rq_bytes(rq->next_rq);
  2307. pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
  2308. BUG_ON(pending);
  2309. }
  2310. EXPORT_SYMBOL(__blk_end_request_all);
  2311. /**
  2312. * __blk_end_request_cur - Helper function to finish the current request chunk.
  2313. * @rq: the request to finish the current chunk for
  2314. * @error: %0 for success, < %0 for error
  2315. *
  2316. * Description:
  2317. * Complete the current consecutively mapped chunk from @rq. Must
  2318. * be called with queue lock held.
  2319. *
  2320. * Return:
  2321. * %false - we are done with this request
  2322. * %true - still buffers pending for this request
  2323. */
  2324. bool __blk_end_request_cur(struct request *rq, int error)
  2325. {
  2326. return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
  2327. }
  2328. EXPORT_SYMBOL(__blk_end_request_cur);
  2329. /**
  2330. * __blk_end_request_err - Finish a request till the next failure boundary.
  2331. * @rq: the request to finish till the next failure boundary for
  2332. * @error: must be negative errno
  2333. *
  2334. * Description:
  2335. * Complete @rq till the next failure boundary. Must be called
  2336. * with queue lock held.
  2337. *
  2338. * Return:
  2339. * %false - we are done with this request
  2340. * %true - still buffers pending for this request
  2341. */
  2342. bool __blk_end_request_err(struct request *rq, int error)
  2343. {
  2344. WARN_ON(error >= 0);
  2345. return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
  2346. }
  2347. EXPORT_SYMBOL_GPL(__blk_end_request_err);
  2348. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  2349. struct bio *bio)
  2350. {
  2351. /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
  2352. rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
  2353. if (bio_has_data(bio)) {
  2354. rq->nr_phys_segments = bio_phys_segments(q, bio);
  2355. rq->buffer = bio_data(bio);
  2356. }
  2357. rq->__data_len = bio->bi_size;
  2358. rq->bio = rq->biotail = bio;
  2359. if (bio->bi_bdev)
  2360. rq->rq_disk = bio->bi_bdev->bd_disk;
  2361. }
  2362. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  2363. /**
  2364. * rq_flush_dcache_pages - Helper function to flush all pages in a request
  2365. * @rq: the request to be flushed
  2366. *
  2367. * Description:
  2368. * Flush all pages in @rq.
  2369. */
  2370. void rq_flush_dcache_pages(struct request *rq)
  2371. {
  2372. struct req_iterator iter;
  2373. struct bio_vec *bvec;
  2374. rq_for_each_segment(bvec, rq, iter)
  2375. flush_dcache_page(bvec->bv_page);
  2376. }
  2377. EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
  2378. #endif
  2379. /**
  2380. * blk_lld_busy - Check if underlying low-level drivers of a device are busy
  2381. * @q : the queue of the device being checked
  2382. *
  2383. * Description:
  2384. * Check if underlying low-level drivers of a device are busy.
  2385. * If the drivers want to export their busy state, they must set own
  2386. * exporting function using blk_queue_lld_busy() first.
  2387. *
  2388. * Basically, this function is used only by request stacking drivers
  2389. * to stop dispatching requests to underlying devices when underlying
  2390. * devices are busy. This behavior helps more I/O merging on the queue
  2391. * of the request stacking driver and prevents I/O throughput regression
  2392. * on burst I/O load.
  2393. *
  2394. * Return:
  2395. * 0 - Not busy (The request stacking driver should dispatch request)
  2396. * 1 - Busy (The request stacking driver should stop dispatching request)
  2397. */
  2398. int blk_lld_busy(struct request_queue *q)
  2399. {
  2400. if (q->lld_busy_fn)
  2401. return q->lld_busy_fn(q);
  2402. return 0;
  2403. }
  2404. EXPORT_SYMBOL_GPL(blk_lld_busy);
  2405. /**
  2406. * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
  2407. * @rq: the clone request to be cleaned up
  2408. *
  2409. * Description:
  2410. * Free all bios in @rq for a cloned request.
  2411. */
  2412. void blk_rq_unprep_clone(struct request *rq)
  2413. {
  2414. struct bio *bio;
  2415. while ((bio = rq->bio) != NULL) {
  2416. rq->bio = bio->bi_next;
  2417. bio_put(bio);
  2418. }
  2419. }
  2420. EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
  2421. /*
  2422. * Copy attributes of the original request to the clone request.
  2423. * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
  2424. */
  2425. static void __blk_rq_prep_clone(struct request *dst, struct request *src)
  2426. {
  2427. dst->cpu = src->cpu;
  2428. dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
  2429. dst->cmd_type = src->cmd_type;
  2430. dst->__sector = blk_rq_pos(src);
  2431. dst->__data_len = blk_rq_bytes(src);
  2432. dst->nr_phys_segments = src->nr_phys_segments;
  2433. dst->ioprio = src->ioprio;
  2434. dst->extra_len = src->extra_len;
  2435. }
  2436. /**
  2437. * blk_rq_prep_clone - Helper function to setup clone request
  2438. * @rq: the request to be setup
  2439. * @rq_src: original request to be cloned
  2440. * @bs: bio_set that bios for clone are allocated from
  2441. * @gfp_mask: memory allocation mask for bio
  2442. * @bio_ctr: setup function to be called for each clone bio.
  2443. * Returns %0 for success, non %0 for failure.
  2444. * @data: private data to be passed to @bio_ctr
  2445. *
  2446. * Description:
  2447. * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
  2448. * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
  2449. * are not copied, and copying such parts is the caller's responsibility.
  2450. * Also, pages which the original bios are pointing to are not copied
  2451. * and the cloned bios just point same pages.
  2452. * So cloned bios must be completed before original bios, which means
  2453. * the caller must complete @rq before @rq_src.
  2454. */
  2455. int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
  2456. struct bio_set *bs, gfp_t gfp_mask,
  2457. int (*bio_ctr)(struct bio *, struct bio *, void *),
  2458. void *data)
  2459. {
  2460. struct bio *bio, *bio_src;
  2461. if (!bs)
  2462. bs = fs_bio_set;
  2463. blk_rq_init(NULL, rq);
  2464. __rq_for_each_bio(bio_src, rq_src) {
  2465. bio = bio_clone_bioset(bio_src, gfp_mask, bs);
  2466. if (!bio)
  2467. goto free_and_out;
  2468. if (bio_ctr && bio_ctr(bio, bio_src, data))
  2469. goto free_and_out;
  2470. if (rq->bio) {
  2471. rq->biotail->bi_next = bio;
  2472. rq->biotail = bio;
  2473. } else
  2474. rq->bio = rq->biotail = bio;
  2475. }
  2476. __blk_rq_prep_clone(rq, rq_src);
  2477. return 0;
  2478. free_and_out:
  2479. if (bio)
  2480. bio_put(bio);
  2481. blk_rq_unprep_clone(rq);
  2482. return -ENOMEM;
  2483. }
  2484. EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
  2485. int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
  2486. {
  2487. return queue_work(kblockd_workqueue, work);
  2488. }
  2489. EXPORT_SYMBOL(kblockd_schedule_work);
  2490. int kblockd_schedule_delayed_work(struct request_queue *q,
  2491. struct delayed_work *dwork, unsigned long delay)
  2492. {
  2493. return queue_delayed_work(kblockd_workqueue, dwork, delay);
  2494. }
  2495. EXPORT_SYMBOL(kblockd_schedule_delayed_work);
  2496. #define PLUG_MAGIC 0x91827364
  2497. /**
  2498. * blk_start_plug - initialize blk_plug and track it inside the task_struct
  2499. * @plug: The &struct blk_plug that needs to be initialized
  2500. *
  2501. * Description:
  2502. * Tracking blk_plug inside the task_struct will help with auto-flushing the
  2503. * pending I/O should the task end up blocking between blk_start_plug() and
  2504. * blk_finish_plug(). This is important from a performance perspective, but
  2505. * also ensures that we don't deadlock. For instance, if the task is blocking
  2506. * for a memory allocation, memory reclaim could end up wanting to free a
  2507. * page belonging to that request that is currently residing in our private
  2508. * plug. By flushing the pending I/O when the process goes to sleep, we avoid
  2509. * this kind of deadlock.
  2510. */
  2511. void blk_start_plug(struct blk_plug *plug)
  2512. {
  2513. struct task_struct *tsk = current;
  2514. plug->magic = PLUG_MAGIC;
  2515. INIT_LIST_HEAD(&plug->list);
  2516. INIT_LIST_HEAD(&plug->mq_list);
  2517. INIT_LIST_HEAD(&plug->cb_list);
  2518. /*
  2519. * If this is a nested plug, don't actually assign it. It will be
  2520. * flushed on its own.
  2521. */
  2522. if (!tsk->plug) {
  2523. /*
  2524. * Store ordering should not be needed here, since a potential
  2525. * preempt will imply a full memory barrier
  2526. */
  2527. tsk->plug = plug;
  2528. }
  2529. }
  2530. EXPORT_SYMBOL(blk_start_plug);
  2531. static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
  2532. {
  2533. struct request *rqa = container_of(a, struct request, queuelist);
  2534. struct request *rqb = container_of(b, struct request, queuelist);
  2535. return !(rqa->q < rqb->q ||
  2536. (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  2537. }
  2538. /*
  2539. * If 'from_schedule' is true, then postpone the dispatch of requests
  2540. * until a safe kblockd context. We due this to avoid accidental big
  2541. * additional stack usage in driver dispatch, in places where the originally
  2542. * plugger did not intend it.
  2543. */
  2544. static void queue_unplugged(struct request_queue *q, unsigned int depth,
  2545. bool from_schedule)
  2546. __releases(q->queue_lock)
  2547. {
  2548. trace_block_unplug(q, depth, !from_schedule);
  2549. if (from_schedule)
  2550. blk_run_queue_async(q);
  2551. else
  2552. __blk_run_queue(q);
  2553. spin_unlock(q->queue_lock);
  2554. }
  2555. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  2556. {
  2557. LIST_HEAD(callbacks);
  2558. while (!list_empty(&plug->cb_list)) {
  2559. list_splice_init(&plug->cb_list, &callbacks);
  2560. while (!list_empty(&callbacks)) {
  2561. struct blk_plug_cb *cb = list_first_entry(&callbacks,
  2562. struct blk_plug_cb,
  2563. list);
  2564. list_del(&cb->list);
  2565. cb->callback(cb, from_schedule);
  2566. }
  2567. }
  2568. }
  2569. struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
  2570. int size)
  2571. {
  2572. struct blk_plug *plug = current->plug;
  2573. struct blk_plug_cb *cb;
  2574. if (!plug)
  2575. return NULL;
  2576. list_for_each_entry(cb, &plug->cb_list, list)
  2577. if (cb->callback == unplug && cb->data == data)
  2578. return cb;
  2579. /* Not currently on the callback list */
  2580. BUG_ON(size < sizeof(*cb));
  2581. cb = kzalloc(size, GFP_ATOMIC);
  2582. if (cb) {
  2583. cb->data = data;
  2584. cb->callback = unplug;
  2585. list_add(&cb->list, &plug->cb_list);
  2586. }
  2587. return cb;
  2588. }
  2589. EXPORT_SYMBOL(blk_check_plugged);
  2590. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2591. {
  2592. struct request_queue *q;
  2593. unsigned long flags;
  2594. struct request *rq;
  2595. LIST_HEAD(list);
  2596. unsigned int depth;
  2597. BUG_ON(plug->magic != PLUG_MAGIC);
  2598. flush_plug_callbacks(plug, from_schedule);
  2599. if (!list_empty(&plug->mq_list))
  2600. blk_mq_flush_plug_list(plug, from_schedule);
  2601. if (list_empty(&plug->list))
  2602. return;
  2603. list_splice_init(&plug->list, &list);
  2604. list_sort(NULL, &list, plug_rq_cmp);
  2605. q = NULL;
  2606. depth = 0;
  2607. /*
  2608. * Save and disable interrupts here, to avoid doing it for every
  2609. * queue lock we have to take.
  2610. */
  2611. local_irq_save(flags);
  2612. while (!list_empty(&list)) {
  2613. rq = list_entry_rq(list.next);
  2614. list_del_init(&rq->queuelist);
  2615. BUG_ON(!rq->q);
  2616. if (rq->q != q) {
  2617. /*
  2618. * This drops the queue lock
  2619. */
  2620. if (q)
  2621. queue_unplugged(q, depth, from_schedule);
  2622. q = rq->q;
  2623. depth = 0;
  2624. spin_lock(q->queue_lock);
  2625. }
  2626. /*
  2627. * Short-circuit if @q is dead
  2628. */
  2629. if (unlikely(blk_queue_dying(q))) {
  2630. __blk_end_request_all(rq, -ENODEV);
  2631. continue;
  2632. }
  2633. /*
  2634. * rq is already accounted, so use raw insert
  2635. */
  2636. if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
  2637. __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
  2638. else
  2639. __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
  2640. depth++;
  2641. }
  2642. /*
  2643. * This drops the queue lock
  2644. */
  2645. if (q)
  2646. queue_unplugged(q, depth, from_schedule);
  2647. local_irq_restore(flags);
  2648. }
  2649. void blk_finish_plug(struct blk_plug *plug)
  2650. {
  2651. blk_flush_plug_list(plug, false);
  2652. if (plug == current->plug)
  2653. current->plug = NULL;
  2654. }
  2655. EXPORT_SYMBOL(blk_finish_plug);
  2656. #ifdef CONFIG_PM_RUNTIME
  2657. /**
  2658. * blk_pm_runtime_init - Block layer runtime PM initialization routine
  2659. * @q: the queue of the device
  2660. * @dev: the device the queue belongs to
  2661. *
  2662. * Description:
  2663. * Initialize runtime-PM-related fields for @q and start auto suspend for
  2664. * @dev. Drivers that want to take advantage of request-based runtime PM
  2665. * should call this function after @dev has been initialized, and its
  2666. * request queue @q has been allocated, and runtime PM for it can not happen
  2667. * yet(either due to disabled/forbidden or its usage_count > 0). In most
  2668. * cases, driver should call this function before any I/O has taken place.
  2669. *
  2670. * This function takes care of setting up using auto suspend for the device,
  2671. * the autosuspend delay is set to -1 to make runtime suspend impossible
  2672. * until an updated value is either set by user or by driver. Drivers do
  2673. * not need to touch other autosuspend settings.
  2674. *
  2675. * The block layer runtime PM is request based, so only works for drivers
  2676. * that use request as their IO unit instead of those directly use bio's.
  2677. */
  2678. void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
  2679. {
  2680. q->dev = dev;
  2681. q->rpm_status = RPM_ACTIVE;
  2682. pm_runtime_set_autosuspend_delay(q->dev, -1);
  2683. pm_runtime_use_autosuspend(q->dev);
  2684. }
  2685. EXPORT_SYMBOL(blk_pm_runtime_init);
  2686. /**
  2687. * blk_pre_runtime_suspend - Pre runtime suspend check
  2688. * @q: the queue of the device
  2689. *
  2690. * Description:
  2691. * This function will check if runtime suspend is allowed for the device
  2692. * by examining if there are any requests pending in the queue. If there
  2693. * are requests pending, the device can not be runtime suspended; otherwise,
  2694. * the queue's status will be updated to SUSPENDING and the driver can
  2695. * proceed to suspend the device.
  2696. *
  2697. * For the not allowed case, we mark last busy for the device so that
  2698. * runtime PM core will try to autosuspend it some time later.
  2699. *
  2700. * This function should be called near the start of the device's
  2701. * runtime_suspend callback.
  2702. *
  2703. * Return:
  2704. * 0 - OK to runtime suspend the device
  2705. * -EBUSY - Device should not be runtime suspended
  2706. */
  2707. int blk_pre_runtime_suspend(struct request_queue *q)
  2708. {
  2709. int ret = 0;
  2710. spin_lock_irq(q->queue_lock);
  2711. if (q->nr_pending) {
  2712. ret = -EBUSY;
  2713. pm_runtime_mark_last_busy(q->dev);
  2714. } else {
  2715. q->rpm_status = RPM_SUSPENDING;
  2716. }
  2717. spin_unlock_irq(q->queue_lock);
  2718. return ret;
  2719. }
  2720. EXPORT_SYMBOL(blk_pre_runtime_suspend);
  2721. /**
  2722. * blk_post_runtime_suspend - Post runtime suspend processing
  2723. * @q: the queue of the device
  2724. * @err: return value of the device's runtime_suspend function
  2725. *
  2726. * Description:
  2727. * Update the queue's runtime status according to the return value of the
  2728. * device's runtime suspend function and mark last busy for the device so
  2729. * that PM core will try to auto suspend the device at a later time.
  2730. *
  2731. * This function should be called near the end of the device's
  2732. * runtime_suspend callback.
  2733. */
  2734. void blk_post_runtime_suspend(struct request_queue *q, int err)
  2735. {
  2736. spin_lock_irq(q->queue_lock);
  2737. if (!err) {
  2738. q->rpm_status = RPM_SUSPENDED;
  2739. } else {
  2740. q->rpm_status = RPM_ACTIVE;
  2741. pm_runtime_mark_last_busy(q->dev);
  2742. }
  2743. spin_unlock_irq(q->queue_lock);
  2744. }
  2745. EXPORT_SYMBOL(blk_post_runtime_suspend);
  2746. /**
  2747. * blk_pre_runtime_resume - Pre runtime resume processing
  2748. * @q: the queue of the device
  2749. *
  2750. * Description:
  2751. * Update the queue's runtime status to RESUMING in preparation for the
  2752. * runtime resume of the device.
  2753. *
  2754. * This function should be called near the start of the device's
  2755. * runtime_resume callback.
  2756. */
  2757. void blk_pre_runtime_resume(struct request_queue *q)
  2758. {
  2759. spin_lock_irq(q->queue_lock);
  2760. q->rpm_status = RPM_RESUMING;
  2761. spin_unlock_irq(q->queue_lock);
  2762. }
  2763. EXPORT_SYMBOL(blk_pre_runtime_resume);
  2764. /**
  2765. * blk_post_runtime_resume - Post runtime resume processing
  2766. * @q: the queue of the device
  2767. * @err: return value of the device's runtime_resume function
  2768. *
  2769. * Description:
  2770. * Update the queue's runtime status according to the return value of the
  2771. * device's runtime_resume function. If it is successfully resumed, process
  2772. * the requests that are queued into the device's queue when it is resuming
  2773. * and then mark last busy and initiate autosuspend for it.
  2774. *
  2775. * This function should be called near the end of the device's
  2776. * runtime_resume callback.
  2777. */
  2778. void blk_post_runtime_resume(struct request_queue *q, int err)
  2779. {
  2780. spin_lock_irq(q->queue_lock);
  2781. if (!err) {
  2782. q->rpm_status = RPM_ACTIVE;
  2783. __blk_run_queue(q);
  2784. pm_runtime_mark_last_busy(q->dev);
  2785. pm_request_autosuspend(q->dev);
  2786. } else {
  2787. q->rpm_status = RPM_SUSPENDED;
  2788. }
  2789. spin_unlock_irq(q->queue_lock);
  2790. }
  2791. EXPORT_SYMBOL(blk_post_runtime_resume);
  2792. #endif
  2793. int __init blk_dev_init(void)
  2794. {
  2795. BUILD_BUG_ON(__REQ_NR_BITS > 8 *
  2796. sizeof(((struct request *)0)->cmd_flags));
  2797. /* used for unplugging and affects IO latency/throughput - HIGHPRI */
  2798. kblockd_workqueue = alloc_workqueue("kblockd",
  2799. WQ_MEM_RECLAIM | WQ_HIGHPRI |
  2800. WQ_POWER_EFFICIENT, 0);
  2801. if (!kblockd_workqueue)
  2802. panic("Failed to create kblockd\n");
  2803. request_cachep = kmem_cache_create("blkdev_requests",
  2804. sizeof(struct request), 0, SLAB_PANIC, NULL);
  2805. blk_requestq_cachep = kmem_cache_create("blkdev_queue",
  2806. sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
  2807. return 0;
  2808. }