blk-core.c 81 KB

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