blk-core.c 70 KB

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