blk-core.c 86 KB

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