blk-core.c 71 KB

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