blk-core.c 89 KB

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