blk-core.c 67 KB

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