blk-core.c 70 KB

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