blk-core.c 74 KB

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