blk-core.c 78 KB

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