blk-core.c 87 KB

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