blk-core.c 86 KB

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