blk-core.c 73 KB

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