blk-core.c 67 KB

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