blk-core.c 74 KB

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