blk-core.c 75 KB

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