blk-core.c 70 KB

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