blk-core.c 86 KB

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