blk-core.c 80 KB

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