blk-core.c 76 KB

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