blk-core.c 87 KB

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