blk-core.c 86 KB

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