blk-core.c 86 KB

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