blk-core.c 69 KB

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