blk-core.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  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/blktrace_api.h>
  29. #include <linux/fault-inject.h>
  30. #include "blk.h"
  31. static int __make_request(struct request_queue *q, struct bio *bio);
  32. /*
  33. * For the allocated request tables
  34. */
  35. static struct kmem_cache *request_cachep;
  36. /*
  37. * For queue allocation
  38. */
  39. struct kmem_cache *blk_requestq_cachep;
  40. /*
  41. * Controlling structure to kblockd
  42. */
  43. static struct workqueue_struct *kblockd_workqueue;
  44. static void drive_stat_acct(struct request *rq, int new_io)
  45. {
  46. struct hd_struct *part;
  47. int rw = rq_data_dir(rq);
  48. int cpu;
  49. if (!blk_fs_request(rq) || !rq->rq_disk)
  50. return;
  51. cpu = part_stat_lock();
  52. part = disk_map_sector_rcu(rq->rq_disk, rq->sector);
  53. if (!new_io)
  54. part_stat_inc(cpu, part, merges[rw]);
  55. else {
  56. part_round_stats(cpu, part);
  57. part_inc_in_flight(part);
  58. }
  59. part_stat_unlock();
  60. }
  61. void blk_queue_congestion_threshold(struct request_queue *q)
  62. {
  63. int nr;
  64. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  65. if (nr > q->nr_requests)
  66. nr = q->nr_requests;
  67. q->nr_congestion_on = nr;
  68. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  69. if (nr < 1)
  70. nr = 1;
  71. q->nr_congestion_off = nr;
  72. }
  73. /**
  74. * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
  75. * @bdev: device
  76. *
  77. * Locates the passed device's request queue and returns the address of its
  78. * backing_dev_info
  79. *
  80. * Will return NULL if the request queue cannot be located.
  81. */
  82. struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
  83. {
  84. struct backing_dev_info *ret = NULL;
  85. struct request_queue *q = bdev_get_queue(bdev);
  86. if (q)
  87. ret = &q->backing_dev_info;
  88. return ret;
  89. }
  90. EXPORT_SYMBOL(blk_get_backing_dev_info);
  91. void blk_rq_init(struct request_queue *q, struct request *rq)
  92. {
  93. memset(rq, 0, sizeof(*rq));
  94. INIT_LIST_HEAD(&rq->queuelist);
  95. INIT_LIST_HEAD(&rq->timeout_list);
  96. rq->cpu = -1;
  97. rq->q = q;
  98. rq->sector = rq->hard_sector = (sector_t) -1;
  99. INIT_HLIST_NODE(&rq->hash);
  100. RB_CLEAR_NODE(&rq->rb_node);
  101. rq->cmd = rq->__cmd;
  102. rq->tag = -1;
  103. rq->ref_count = 1;
  104. }
  105. EXPORT_SYMBOL(blk_rq_init);
  106. static void req_bio_endio(struct request *rq, struct bio *bio,
  107. unsigned int nbytes, int error)
  108. {
  109. struct request_queue *q = rq->q;
  110. if (&q->bar_rq != rq) {
  111. if (error)
  112. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  113. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  114. error = -EIO;
  115. if (unlikely(nbytes > bio->bi_size)) {
  116. printk(KERN_ERR "%s: want %u bytes done, %u left\n",
  117. __func__, nbytes, bio->bi_size);
  118. nbytes = bio->bi_size;
  119. }
  120. bio->bi_size -= nbytes;
  121. bio->bi_sector += (nbytes >> 9);
  122. if (bio_integrity(bio))
  123. bio_integrity_advance(bio, nbytes);
  124. if (bio->bi_size == 0)
  125. bio_endio(bio, error);
  126. } else {
  127. /*
  128. * Okay, this is the barrier request in progress, just
  129. * record the error;
  130. */
  131. if (error && !q->orderr)
  132. q->orderr = error;
  133. }
  134. }
  135. void blk_dump_rq_flags(struct request *rq, char *msg)
  136. {
  137. int bit;
  138. printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
  139. rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
  140. rq->cmd_flags);
  141. printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
  142. (unsigned long long)rq->sector,
  143. rq->nr_sectors,
  144. rq->current_nr_sectors);
  145. printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
  146. rq->bio, rq->biotail,
  147. rq->buffer, rq->data,
  148. rq->data_len);
  149. if (blk_pc_request(rq)) {
  150. printk(KERN_INFO " cdb: ");
  151. for (bit = 0; bit < BLK_MAX_CDB; bit++)
  152. printk("%02x ", rq->cmd[bit]);
  153. printk("\n");
  154. }
  155. }
  156. EXPORT_SYMBOL(blk_dump_rq_flags);
  157. /*
  158. * "plug" the device if there are no outstanding requests: this will
  159. * force the transfer to start only after we have put all the requests
  160. * on the list.
  161. *
  162. * This is called with interrupts off and no requests on the queue and
  163. * with the queue lock held.
  164. */
  165. void blk_plug_device(struct request_queue *q)
  166. {
  167. WARN_ON(!irqs_disabled());
  168. /*
  169. * don't plug a stopped queue, it must be paired with blk_start_queue()
  170. * which will restart the queueing
  171. */
  172. if (blk_queue_stopped(q))
  173. return;
  174. if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
  175. mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
  176. blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
  177. }
  178. }
  179. EXPORT_SYMBOL(blk_plug_device);
  180. /**
  181. * blk_plug_device_unlocked - plug a device without queue lock held
  182. * @q: The &struct request_queue to plug
  183. *
  184. * Description:
  185. * Like @blk_plug_device(), but grabs the queue lock and disables
  186. * interrupts.
  187. **/
  188. void blk_plug_device_unlocked(struct request_queue *q)
  189. {
  190. unsigned long flags;
  191. spin_lock_irqsave(q->queue_lock, flags);
  192. blk_plug_device(q);
  193. spin_unlock_irqrestore(q->queue_lock, flags);
  194. }
  195. EXPORT_SYMBOL(blk_plug_device_unlocked);
  196. /*
  197. * remove the queue from the plugged list, if present. called with
  198. * queue lock held and interrupts disabled.
  199. */
  200. int blk_remove_plug(struct request_queue *q)
  201. {
  202. WARN_ON(!irqs_disabled());
  203. if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
  204. return 0;
  205. del_timer(&q->unplug_timer);
  206. return 1;
  207. }
  208. EXPORT_SYMBOL(blk_remove_plug);
  209. /*
  210. * remove the plug and let it rip..
  211. */
  212. void __generic_unplug_device(struct request_queue *q)
  213. {
  214. if (unlikely(blk_queue_stopped(q)))
  215. return;
  216. if (!blk_remove_plug(q))
  217. return;
  218. q->request_fn(q);
  219. }
  220. EXPORT_SYMBOL(__generic_unplug_device);
  221. /**
  222. * generic_unplug_device - fire a request queue
  223. * @q: The &struct request_queue in question
  224. *
  225. * Description:
  226. * Linux uses plugging to build bigger requests queues before letting
  227. * the device have at them. If a queue is plugged, the I/O scheduler
  228. * is still adding and merging requests on the queue. Once the queue
  229. * gets unplugged, the request_fn defined for the queue is invoked and
  230. * transfers started.
  231. **/
  232. void generic_unplug_device(struct request_queue *q)
  233. {
  234. if (blk_queue_plugged(q)) {
  235. spin_lock_irq(q->queue_lock);
  236. __generic_unplug_device(q);
  237. spin_unlock_irq(q->queue_lock);
  238. }
  239. }
  240. EXPORT_SYMBOL(generic_unplug_device);
  241. static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
  242. struct page *page)
  243. {
  244. struct request_queue *q = bdi->unplug_io_data;
  245. blk_unplug(q);
  246. }
  247. void blk_unplug_work(struct work_struct *work)
  248. {
  249. struct request_queue *q =
  250. container_of(work, struct request_queue, unplug_work);
  251. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
  252. q->rq.count[READ] + q->rq.count[WRITE]);
  253. q->unplug_fn(q);
  254. }
  255. void blk_unplug_timeout(unsigned long data)
  256. {
  257. struct request_queue *q = (struct request_queue *)data;
  258. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
  259. q->rq.count[READ] + q->rq.count[WRITE]);
  260. kblockd_schedule_work(q, &q->unplug_work);
  261. }
  262. void blk_unplug(struct request_queue *q)
  263. {
  264. /*
  265. * devices don't necessarily have an ->unplug_fn defined
  266. */
  267. if (q->unplug_fn) {
  268. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
  269. q->rq.count[READ] + q->rq.count[WRITE]);
  270. q->unplug_fn(q);
  271. }
  272. }
  273. EXPORT_SYMBOL(blk_unplug);
  274. static void blk_invoke_request_fn(struct request_queue *q)
  275. {
  276. /*
  277. * one level of recursion is ok and is much faster than kicking
  278. * the unplug handling
  279. */
  280. if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
  281. q->request_fn(q);
  282. queue_flag_clear(QUEUE_FLAG_REENTER, q);
  283. } else {
  284. queue_flag_set(QUEUE_FLAG_PLUGGED, q);
  285. kblockd_schedule_work(q, &q->unplug_work);
  286. }
  287. }
  288. /**
  289. * blk_start_queue - restart a previously stopped queue
  290. * @q: The &struct request_queue in question
  291. *
  292. * Description:
  293. * blk_start_queue() will clear the stop flag on the queue, and call
  294. * the request_fn for the queue if it was in a stopped state when
  295. * entered. Also see blk_stop_queue(). Queue lock must be held.
  296. **/
  297. void blk_start_queue(struct request_queue *q)
  298. {
  299. WARN_ON(!irqs_disabled());
  300. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  301. blk_invoke_request_fn(q);
  302. }
  303. EXPORT_SYMBOL(blk_start_queue);
  304. /**
  305. * blk_stop_queue - stop a queue
  306. * @q: The &struct request_queue in question
  307. *
  308. * Description:
  309. * The Linux block layer assumes that a block driver will consume all
  310. * entries on the request queue when the request_fn strategy is called.
  311. * Often this will not happen, because of hardware limitations (queue
  312. * depth settings). If a device driver gets a 'queue full' response,
  313. * or if it simply chooses not to queue more I/O at one point, it can
  314. * call this function to prevent the request_fn from being called until
  315. * the driver has signalled it's ready to go again. This happens by calling
  316. * blk_start_queue() to restart queue operations. Queue lock must be held.
  317. **/
  318. void blk_stop_queue(struct request_queue *q)
  319. {
  320. blk_remove_plug(q);
  321. queue_flag_set(QUEUE_FLAG_STOPPED, q);
  322. }
  323. EXPORT_SYMBOL(blk_stop_queue);
  324. /**
  325. * blk_sync_queue - cancel any pending callbacks on a queue
  326. * @q: the queue
  327. *
  328. * Description:
  329. * The block layer may perform asynchronous callback activity
  330. * on a queue, such as calling the unplug function after a timeout.
  331. * A block device may call blk_sync_queue to ensure that any
  332. * such activity is cancelled, thus allowing it to release resources
  333. * that the callbacks might use. The caller must already have made sure
  334. * that its ->make_request_fn will not re-add plugging prior to calling
  335. * this function.
  336. *
  337. */
  338. void blk_sync_queue(struct request_queue *q)
  339. {
  340. del_timer_sync(&q->unplug_timer);
  341. kblockd_flush_work(&q->unplug_work);
  342. }
  343. EXPORT_SYMBOL(blk_sync_queue);
  344. /**
  345. * blk_run_queue - run a single device queue
  346. * @q: The queue to run
  347. */
  348. void __blk_run_queue(struct request_queue *q)
  349. {
  350. blk_remove_plug(q);
  351. /*
  352. * Only recurse once to avoid overrunning the stack, let the unplug
  353. * handling reinvoke the handler shortly if we already got there.
  354. */
  355. if (!elv_queue_empty(q))
  356. blk_invoke_request_fn(q);
  357. }
  358. EXPORT_SYMBOL(__blk_run_queue);
  359. /**
  360. * blk_run_queue - run a single device queue
  361. * @q: The queue to run
  362. */
  363. void blk_run_queue(struct request_queue *q)
  364. {
  365. unsigned long flags;
  366. spin_lock_irqsave(q->queue_lock, flags);
  367. __blk_run_queue(q);
  368. spin_unlock_irqrestore(q->queue_lock, flags);
  369. }
  370. EXPORT_SYMBOL(blk_run_queue);
  371. void blk_put_queue(struct request_queue *q)
  372. {
  373. kobject_put(&q->kobj);
  374. }
  375. void blk_cleanup_queue(struct request_queue *q)
  376. {
  377. /*
  378. * We know we have process context here, so we can be a little
  379. * cautious and ensure that pending block actions on this device
  380. * are done before moving on. Going into this function, we should
  381. * not have processes doing IO to this device.
  382. */
  383. blk_sync_queue(q);
  384. mutex_lock(&q->sysfs_lock);
  385. queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
  386. mutex_unlock(&q->sysfs_lock);
  387. if (q->elevator)
  388. elevator_exit(q->elevator);
  389. blk_put_queue(q);
  390. }
  391. EXPORT_SYMBOL(blk_cleanup_queue);
  392. static int blk_init_free_list(struct request_queue *q)
  393. {
  394. struct request_list *rl = &q->rq;
  395. rl->count[READ] = rl->count[WRITE] = 0;
  396. rl->starved[READ] = rl->starved[WRITE] = 0;
  397. rl->elvpriv = 0;
  398. init_waitqueue_head(&rl->wait[READ]);
  399. init_waitqueue_head(&rl->wait[WRITE]);
  400. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  401. mempool_free_slab, request_cachep, q->node);
  402. if (!rl->rq_pool)
  403. return -ENOMEM;
  404. return 0;
  405. }
  406. struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
  407. {
  408. return blk_alloc_queue_node(gfp_mask, -1);
  409. }
  410. EXPORT_SYMBOL(blk_alloc_queue);
  411. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  412. {
  413. struct request_queue *q;
  414. int err;
  415. q = kmem_cache_alloc_node(blk_requestq_cachep,
  416. gfp_mask | __GFP_ZERO, node_id);
  417. if (!q)
  418. return NULL;
  419. q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
  420. q->backing_dev_info.unplug_io_data = q;
  421. err = bdi_init(&q->backing_dev_info);
  422. if (err) {
  423. kmem_cache_free(blk_requestq_cachep, q);
  424. return NULL;
  425. }
  426. init_timer(&q->unplug_timer);
  427. setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
  428. INIT_LIST_HEAD(&q->timeout_list);
  429. kobject_init(&q->kobj, &blk_queue_ktype);
  430. mutex_init(&q->sysfs_lock);
  431. spin_lock_init(&q->__queue_lock);
  432. return q;
  433. }
  434. EXPORT_SYMBOL(blk_alloc_queue_node);
  435. /**
  436. * blk_init_queue - prepare a request queue for use with a block device
  437. * @rfn: The function to be called to process requests that have been
  438. * placed on the queue.
  439. * @lock: Request queue spin lock
  440. *
  441. * Description:
  442. * If a block device wishes to use the standard request handling procedures,
  443. * which sorts requests and coalesces adjacent requests, then it must
  444. * call blk_init_queue(). The function @rfn will be called when there
  445. * are requests on the queue that need to be processed. If the device
  446. * supports plugging, then @rfn may not be called immediately when requests
  447. * are available on the queue, but may be called at some time later instead.
  448. * Plugged queues are generally unplugged when a buffer belonging to one
  449. * of the requests on the queue is needed, or due to memory pressure.
  450. *
  451. * @rfn is not required, or even expected, to remove all requests off the
  452. * queue, but only as many as it can handle at a time. If it does leave
  453. * requests on the queue, it is responsible for arranging that the requests
  454. * get dealt with eventually.
  455. *
  456. * The queue spin lock must be held while manipulating the requests on the
  457. * request queue; this lock will be taken also from interrupt context, so irq
  458. * disabling is needed for it.
  459. *
  460. * Function returns a pointer to the initialized request queue, or %NULL if
  461. * it didn't succeed.
  462. *
  463. * Note:
  464. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  465. * when the block device is deactivated (such as at module unload).
  466. **/
  467. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  468. {
  469. return blk_init_queue_node(rfn, lock, -1);
  470. }
  471. EXPORT_SYMBOL(blk_init_queue);
  472. struct request_queue *
  473. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  474. {
  475. struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
  476. if (!q)
  477. return NULL;
  478. q->node = node_id;
  479. if (blk_init_free_list(q)) {
  480. kmem_cache_free(blk_requestq_cachep, q);
  481. return NULL;
  482. }
  483. /*
  484. * if caller didn't supply a lock, they get per-queue locking with
  485. * our embedded lock
  486. */
  487. if (!lock)
  488. lock = &q->__queue_lock;
  489. q->request_fn = rfn;
  490. q->prep_rq_fn = NULL;
  491. q->unplug_fn = generic_unplug_device;
  492. q->queue_flags = (1 << QUEUE_FLAG_CLUSTER |
  493. 1 << QUEUE_FLAG_STACKABLE);
  494. q->queue_lock = lock;
  495. blk_queue_segment_boundary(q, 0xffffffff);
  496. blk_queue_make_request(q, __make_request);
  497. blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
  498. blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
  499. blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
  500. q->sg_reserved_size = INT_MAX;
  501. blk_set_cmd_filter_defaults(&q->cmd_filter);
  502. /*
  503. * all done
  504. */
  505. if (!elevator_init(q, NULL)) {
  506. blk_queue_congestion_threshold(q);
  507. return q;
  508. }
  509. blk_put_queue(q);
  510. return NULL;
  511. }
  512. EXPORT_SYMBOL(blk_init_queue_node);
  513. int blk_get_queue(struct request_queue *q)
  514. {
  515. if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
  516. kobject_get(&q->kobj);
  517. return 0;
  518. }
  519. return 1;
  520. }
  521. static inline void blk_free_request(struct request_queue *q, struct request *rq)
  522. {
  523. if (rq->cmd_flags & REQ_ELVPRIV)
  524. elv_put_request(q, rq);
  525. mempool_free(rq, q->rq.rq_pool);
  526. }
  527. static struct request *
  528. blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
  529. {
  530. struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
  531. if (!rq)
  532. return NULL;
  533. blk_rq_init(q, rq);
  534. rq->cmd_flags = rw | REQ_ALLOCED;
  535. if (priv) {
  536. if (unlikely(elv_set_request(q, rq, gfp_mask))) {
  537. mempool_free(rq, q->rq.rq_pool);
  538. return NULL;
  539. }
  540. rq->cmd_flags |= REQ_ELVPRIV;
  541. }
  542. return rq;
  543. }
  544. /*
  545. * ioc_batching returns true if the ioc is a valid batching request and
  546. * should be given priority access to a request.
  547. */
  548. static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
  549. {
  550. if (!ioc)
  551. return 0;
  552. /*
  553. * Make sure the process is able to allocate at least 1 request
  554. * even if the batch times out, otherwise we could theoretically
  555. * lose wakeups.
  556. */
  557. return ioc->nr_batch_requests == q->nr_batching ||
  558. (ioc->nr_batch_requests > 0
  559. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  560. }
  561. /*
  562. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  563. * will cause the process to be a "batcher" on all queues in the system. This
  564. * is the behaviour we want though - once it gets a wakeup it should be given
  565. * a nice run.
  566. */
  567. static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
  568. {
  569. if (!ioc || ioc_batching(q, ioc))
  570. return;
  571. ioc->nr_batch_requests = q->nr_batching;
  572. ioc->last_waited = jiffies;
  573. }
  574. static void __freed_request(struct request_queue *q, int rw)
  575. {
  576. struct request_list *rl = &q->rq;
  577. if (rl->count[rw] < queue_congestion_off_threshold(q))
  578. blk_clear_queue_congested(q, rw);
  579. if (rl->count[rw] + 1 <= q->nr_requests) {
  580. if (waitqueue_active(&rl->wait[rw]))
  581. wake_up(&rl->wait[rw]);
  582. blk_clear_queue_full(q, rw);
  583. }
  584. }
  585. /*
  586. * A request has just been released. Account for it, update the full and
  587. * congestion status, wake up any waiters. Called under q->queue_lock.
  588. */
  589. static void freed_request(struct request_queue *q, int rw, int priv)
  590. {
  591. struct request_list *rl = &q->rq;
  592. rl->count[rw]--;
  593. if (priv)
  594. rl->elvpriv--;
  595. __freed_request(q, rw);
  596. if (unlikely(rl->starved[rw ^ 1]))
  597. __freed_request(q, rw ^ 1);
  598. }
  599. #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
  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 int rw = rw_flags & 0x01;
  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[rw]+1 >= queue_congestion_on_threshold(q)) {
  617. if (rl->count[rw]+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, rw)) {
  626. ioc_set_batching(q, ioc);
  627. blk_set_queue_full(q, rw);
  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, rw);
  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[rw] >= (3 * q->nr_requests / 2))
  648. goto out;
  649. rl->count[rw]++;
  650. rl->starved[rw] = 0;
  651. priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
  652. if (priv)
  653. rl->elvpriv++;
  654. spin_unlock_irq(q->queue_lock);
  655. rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
  656. if (unlikely(!rq)) {
  657. /*
  658. * Allocation failed presumably due to memory. Undo anything
  659. * we might have messed up.
  660. *
  661. * Allocating task should really be put onto the front of the
  662. * wait queue, but this is pretty rare.
  663. */
  664. spin_lock_irq(q->queue_lock);
  665. freed_request(q, rw, priv);
  666. /*
  667. * in the very unlikely event that allocation failed and no
  668. * requests for this direction was pending, mark us starved
  669. * so that freeing of a request in the other direction will
  670. * notice us. another possible fix would be to split the
  671. * rq mempool into READ and WRITE
  672. */
  673. rq_starved:
  674. if (unlikely(rl->count[rw] == 0))
  675. rl->starved[rw] = 1;
  676. goto out;
  677. }
  678. /*
  679. * ioc may be NULL here, and ioc_batching will be false. That's
  680. * OK, if the queue is under the request limit then requests need
  681. * not count toward the nr_batch_requests limit. There will always
  682. * be some limit enforced by BLK_BATCH_TIME.
  683. */
  684. if (ioc_batching(q, ioc))
  685. ioc->nr_batch_requests--;
  686. blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
  687. out:
  688. return rq;
  689. }
  690. /*
  691. * No available requests for this queue, unplug the device and wait for some
  692. * requests to become available.
  693. *
  694. * Called with q->queue_lock held, and returns with it unlocked.
  695. */
  696. static struct request *get_request_wait(struct request_queue *q, int rw_flags,
  697. struct bio *bio)
  698. {
  699. const int rw = rw_flags & 0x01;
  700. struct request *rq;
  701. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  702. while (!rq) {
  703. DEFINE_WAIT(wait);
  704. struct io_context *ioc;
  705. struct request_list *rl = &q->rq;
  706. prepare_to_wait_exclusive(&rl->wait[rw], &wait,
  707. TASK_UNINTERRUPTIBLE);
  708. blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
  709. __generic_unplug_device(q);
  710. spin_unlock_irq(q->queue_lock);
  711. io_schedule();
  712. /*
  713. * After sleeping, we become a "batching" process and
  714. * will be able to allocate at least one request, and
  715. * up to a big batch of them for a small period time.
  716. * See ioc_batching, ioc_set_batching
  717. */
  718. ioc = current_io_context(GFP_NOIO, q->node);
  719. ioc_set_batching(q, ioc);
  720. spin_lock_irq(q->queue_lock);
  721. finish_wait(&rl->wait[rw], &wait);
  722. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  723. };
  724. return rq;
  725. }
  726. struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
  727. {
  728. struct request *rq;
  729. BUG_ON(rw != READ && rw != WRITE);
  730. spin_lock_irq(q->queue_lock);
  731. if (gfp_mask & __GFP_WAIT) {
  732. rq = get_request_wait(q, rw, NULL);
  733. } else {
  734. rq = get_request(q, rw, NULL, gfp_mask);
  735. if (!rq)
  736. spin_unlock_irq(q->queue_lock);
  737. }
  738. /* q->queue_lock is unlocked at this point */
  739. return rq;
  740. }
  741. EXPORT_SYMBOL(blk_get_request);
  742. /**
  743. * blk_start_queueing - initiate dispatch of requests to device
  744. * @q: request queue to kick into gear
  745. *
  746. * This is basically a helper to remove the need to know whether a queue
  747. * is plugged or not if someone just wants to initiate dispatch of requests
  748. * for this queue.
  749. *
  750. * The queue lock must be held with interrupts disabled.
  751. */
  752. void blk_start_queueing(struct request_queue *q)
  753. {
  754. if (!blk_queue_plugged(q)) {
  755. if (unlikely(blk_queue_stopped(q)))
  756. return;
  757. q->request_fn(q);
  758. } else
  759. __generic_unplug_device(q);
  760. }
  761. EXPORT_SYMBOL(blk_start_queueing);
  762. /**
  763. * blk_requeue_request - put a request back on queue
  764. * @q: request queue where request should be inserted
  765. * @rq: request to be inserted
  766. *
  767. * Description:
  768. * Drivers often keep queueing requests until the hardware cannot accept
  769. * more, when that condition happens we need to put the request back
  770. * on the queue. Must be called with queue lock held.
  771. */
  772. void blk_requeue_request(struct request_queue *q, struct request *rq)
  773. {
  774. blk_delete_timer(rq);
  775. blk_clear_rq_complete(rq);
  776. blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
  777. if (blk_rq_tagged(rq))
  778. blk_queue_end_tag(q, rq);
  779. elv_requeue_request(q, rq);
  780. }
  781. EXPORT_SYMBOL(blk_requeue_request);
  782. /**
  783. * blk_insert_request - insert a special request into a request queue
  784. * @q: request queue where request should be inserted
  785. * @rq: request to be inserted
  786. * @at_head: insert request at head or tail of queue
  787. * @data: private data
  788. *
  789. * Description:
  790. * Many block devices need to execute commands asynchronously, so they don't
  791. * block the whole kernel from preemption during request execution. This is
  792. * accomplished normally by inserting aritficial requests tagged as
  793. * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
  794. * be scheduled for actual execution by the request queue.
  795. *
  796. * We have the option of inserting the head or the tail of the queue.
  797. * Typically we use the tail for new ioctls and so forth. We use the head
  798. * of the queue for things like a QUEUE_FULL message from a device, or a
  799. * host that is unable to accept a particular command.
  800. */
  801. void blk_insert_request(struct request_queue *q, struct request *rq,
  802. int at_head, void *data)
  803. {
  804. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  805. unsigned long flags;
  806. /*
  807. * tell I/O scheduler that this isn't a regular read/write (ie it
  808. * must not attempt merges on this) and that it acts as a soft
  809. * barrier
  810. */
  811. rq->cmd_type = REQ_TYPE_SPECIAL;
  812. rq->cmd_flags |= REQ_SOFTBARRIER;
  813. rq->special = data;
  814. spin_lock_irqsave(q->queue_lock, flags);
  815. /*
  816. * If command is tagged, release the tag
  817. */
  818. if (blk_rq_tagged(rq))
  819. blk_queue_end_tag(q, rq);
  820. drive_stat_acct(rq, 1);
  821. __elv_add_request(q, rq, where, 0);
  822. blk_start_queueing(q);
  823. spin_unlock_irqrestore(q->queue_lock, flags);
  824. }
  825. EXPORT_SYMBOL(blk_insert_request);
  826. /*
  827. * add-request adds a request to the linked list.
  828. * queue lock is held and interrupts disabled, as we muck with the
  829. * request queue list.
  830. */
  831. static inline void add_request(struct request_queue *q, struct request *req)
  832. {
  833. drive_stat_acct(req, 1);
  834. /*
  835. * elevator indicated where it wants this request to be
  836. * inserted at elevator_merge time
  837. */
  838. __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
  839. }
  840. static void part_round_stats_single(int cpu, struct hd_struct *part,
  841. unsigned long now)
  842. {
  843. if (now == part->stamp)
  844. return;
  845. if (part->in_flight) {
  846. __part_stat_add(cpu, part, time_in_queue,
  847. part->in_flight * (now - part->stamp));
  848. __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
  849. }
  850. part->stamp = now;
  851. }
  852. /**
  853. * part_round_stats() - Round off the performance stats on a struct
  854. * disk_stats.
  855. *
  856. * The average IO queue length and utilisation statistics are maintained
  857. * by observing the current state of the queue length and the amount of
  858. * time it has been in this state for.
  859. *
  860. * Normally, that accounting is done on IO completion, but that can result
  861. * in more than a second's worth of IO being accounted for within any one
  862. * second, leading to >100% utilisation. To deal with that, we call this
  863. * function to do a round-off before returning the results when reading
  864. * /proc/diskstats. This accounts immediately for all queue usage up to
  865. * the current jiffies and restarts the counters again.
  866. */
  867. void part_round_stats(int cpu, struct hd_struct *part)
  868. {
  869. unsigned long now = jiffies;
  870. if (part->partno)
  871. part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
  872. part_round_stats_single(cpu, part, now);
  873. }
  874. EXPORT_SYMBOL_GPL(part_round_stats);
  875. /*
  876. * queue lock must be held
  877. */
  878. void __blk_put_request(struct request_queue *q, struct request *req)
  879. {
  880. if (unlikely(!q))
  881. return;
  882. if (unlikely(--req->ref_count))
  883. return;
  884. elv_completed_request(q, req);
  885. /*
  886. * Request may not have originated from ll_rw_blk. if not,
  887. * it didn't come out of our reserved rq pools
  888. */
  889. if (req->cmd_flags & REQ_ALLOCED) {
  890. int rw = rq_data_dir(req);
  891. int priv = req->cmd_flags & REQ_ELVPRIV;
  892. BUG_ON(!list_empty(&req->queuelist));
  893. BUG_ON(!hlist_unhashed(&req->hash));
  894. blk_free_request(q, req);
  895. freed_request(q, rw, priv);
  896. }
  897. }
  898. EXPORT_SYMBOL_GPL(__blk_put_request);
  899. void blk_put_request(struct request *req)
  900. {
  901. unsigned long flags;
  902. struct request_queue *q = req->q;
  903. spin_lock_irqsave(q->queue_lock, flags);
  904. __blk_put_request(q, req);
  905. spin_unlock_irqrestore(q->queue_lock, flags);
  906. }
  907. EXPORT_SYMBOL(blk_put_request);
  908. void init_request_from_bio(struct request *req, struct bio *bio)
  909. {
  910. req->cpu = bio->bi_comp_cpu;
  911. req->cmd_type = REQ_TYPE_FS;
  912. /*
  913. * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
  914. */
  915. if (bio_rw_ahead(bio) || bio_failfast(bio))
  916. req->cmd_flags |= REQ_FAILFAST;
  917. /*
  918. * REQ_BARRIER implies no merging, but lets make it explicit
  919. */
  920. if (unlikely(bio_discard(bio))) {
  921. req->cmd_flags |= REQ_DISCARD;
  922. if (bio_barrier(bio))
  923. req->cmd_flags |= REQ_SOFTBARRIER;
  924. req->q->prepare_discard_fn(req->q, req);
  925. } else if (unlikely(bio_barrier(bio)))
  926. req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
  927. if (bio_sync(bio))
  928. req->cmd_flags |= REQ_RW_SYNC;
  929. if (bio_rw_meta(bio))
  930. req->cmd_flags |= REQ_RW_META;
  931. req->errors = 0;
  932. req->hard_sector = req->sector = bio->bi_sector;
  933. req->ioprio = bio_prio(bio);
  934. req->start_time = jiffies;
  935. blk_rq_bio_prep(req->q, req, bio);
  936. }
  937. static int __make_request(struct request_queue *q, struct bio *bio)
  938. {
  939. struct request *req;
  940. int el_ret, nr_sectors, barrier, discard, err;
  941. const unsigned short prio = bio_prio(bio);
  942. const int sync = bio_sync(bio);
  943. int rw_flags;
  944. nr_sectors = bio_sectors(bio);
  945. /*
  946. * low level driver can indicate that it wants pages above a
  947. * certain limit bounced to low memory (ie for highmem, or even
  948. * ISA dma in theory)
  949. */
  950. blk_queue_bounce(q, &bio);
  951. barrier = bio_barrier(bio);
  952. if (unlikely(barrier) && bio_has_data(bio) &&
  953. (q->next_ordered == QUEUE_ORDERED_NONE)) {
  954. err = -EOPNOTSUPP;
  955. goto end_io;
  956. }
  957. discard = bio_discard(bio);
  958. if (unlikely(discard) && !q->prepare_discard_fn) {
  959. err = -EOPNOTSUPP;
  960. goto end_io;
  961. }
  962. spin_lock_irq(q->queue_lock);
  963. if (unlikely(barrier) || elv_queue_empty(q))
  964. goto get_rq;
  965. el_ret = elv_merge(q, &req, bio);
  966. switch (el_ret) {
  967. case ELEVATOR_BACK_MERGE:
  968. BUG_ON(!rq_mergeable(req));
  969. if (!ll_back_merge_fn(q, req, bio))
  970. break;
  971. blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
  972. req->biotail->bi_next = bio;
  973. req->biotail = bio;
  974. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  975. req->ioprio = ioprio_best(req->ioprio, prio);
  976. if (!blk_rq_cpu_valid(req))
  977. req->cpu = bio->bi_comp_cpu;
  978. drive_stat_acct(req, 0);
  979. if (!attempt_back_merge(q, req))
  980. elv_merged_request(q, req, el_ret);
  981. goto out;
  982. case ELEVATOR_FRONT_MERGE:
  983. BUG_ON(!rq_mergeable(req));
  984. if (!ll_front_merge_fn(q, req, bio))
  985. break;
  986. blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
  987. bio->bi_next = req->bio;
  988. req->bio = bio;
  989. /*
  990. * may not be valid. if the low level driver said
  991. * it didn't need a bounce buffer then it better
  992. * not touch req->buffer either...
  993. */
  994. req->buffer = bio_data(bio);
  995. req->current_nr_sectors = bio_cur_sectors(bio);
  996. req->hard_cur_sectors = req->current_nr_sectors;
  997. req->sector = req->hard_sector = bio->bi_sector;
  998. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  999. req->ioprio = ioprio_best(req->ioprio, prio);
  1000. if (!blk_rq_cpu_valid(req))
  1001. req->cpu = bio->bi_comp_cpu;
  1002. drive_stat_acct(req, 0);
  1003. if (!attempt_front_merge(q, req))
  1004. elv_merged_request(q, req, el_ret);
  1005. goto out;
  1006. /* ELV_NO_MERGE: elevator says don't/can't merge. */
  1007. default:
  1008. ;
  1009. }
  1010. get_rq:
  1011. /*
  1012. * This sync check and mask will be re-done in init_request_from_bio(),
  1013. * but we need to set it earlier to expose the sync flag to the
  1014. * rq allocator and io schedulers.
  1015. */
  1016. rw_flags = bio_data_dir(bio);
  1017. if (sync)
  1018. rw_flags |= REQ_RW_SYNC;
  1019. /*
  1020. * Grab a free request. This is might sleep but can not fail.
  1021. * Returns with the queue unlocked.
  1022. */
  1023. req = get_request_wait(q, rw_flags, bio);
  1024. /*
  1025. * After dropping the lock and possibly sleeping here, our request
  1026. * may now be mergeable after it had proven unmergeable (above).
  1027. * We don't worry about that case for efficiency. It won't happen
  1028. * often, and the elevators are able to handle it.
  1029. */
  1030. init_request_from_bio(req, bio);
  1031. spin_lock_irq(q->queue_lock);
  1032. if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
  1033. bio_flagged(bio, BIO_CPU_AFFINE))
  1034. req->cpu = blk_cpu_to_group(smp_processor_id());
  1035. if (elv_queue_empty(q))
  1036. blk_plug_device(q);
  1037. add_request(q, req);
  1038. out:
  1039. if (sync)
  1040. __generic_unplug_device(q);
  1041. spin_unlock_irq(q->queue_lock);
  1042. return 0;
  1043. end_io:
  1044. bio_endio(bio, err);
  1045. return 0;
  1046. }
  1047. /*
  1048. * If bio->bi_dev is a partition, remap the location
  1049. */
  1050. static inline void blk_partition_remap(struct bio *bio)
  1051. {
  1052. struct block_device *bdev = bio->bi_bdev;
  1053. if (bio_sectors(bio) && bdev != bdev->bd_contains) {
  1054. struct hd_struct *p = bdev->bd_part;
  1055. bio->bi_sector += p->start_sect;
  1056. bio->bi_bdev = bdev->bd_contains;
  1057. blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
  1058. bdev->bd_dev, bio->bi_sector,
  1059. bio->bi_sector - p->start_sect);
  1060. }
  1061. }
  1062. static void handle_bad_sector(struct bio *bio)
  1063. {
  1064. char b[BDEVNAME_SIZE];
  1065. printk(KERN_INFO "attempt to access beyond end of device\n");
  1066. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  1067. bdevname(bio->bi_bdev, b),
  1068. bio->bi_rw,
  1069. (unsigned long long)bio->bi_sector + bio_sectors(bio),
  1070. (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
  1071. set_bit(BIO_EOF, &bio->bi_flags);
  1072. }
  1073. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1074. static DECLARE_FAULT_ATTR(fail_make_request);
  1075. static int __init setup_fail_make_request(char *str)
  1076. {
  1077. return setup_fault_attr(&fail_make_request, str);
  1078. }
  1079. __setup("fail_make_request=", setup_fail_make_request);
  1080. static int should_fail_request(struct bio *bio)
  1081. {
  1082. struct hd_struct *part = bio->bi_bdev->bd_part;
  1083. if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
  1084. return should_fail(&fail_make_request, bio->bi_size);
  1085. return 0;
  1086. }
  1087. static int __init fail_make_request_debugfs(void)
  1088. {
  1089. return init_fault_attr_dentries(&fail_make_request,
  1090. "fail_make_request");
  1091. }
  1092. late_initcall(fail_make_request_debugfs);
  1093. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1094. static inline int should_fail_request(struct bio *bio)
  1095. {
  1096. return 0;
  1097. }
  1098. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1099. /*
  1100. * Check whether this bio extends beyond the end of the device.
  1101. */
  1102. static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
  1103. {
  1104. sector_t maxsector;
  1105. if (!nr_sectors)
  1106. return 0;
  1107. /* Test device or partition size, when known. */
  1108. maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
  1109. if (maxsector) {
  1110. sector_t sector = bio->bi_sector;
  1111. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  1112. /*
  1113. * This may well happen - the kernel calls bread()
  1114. * without checking the size of the device, e.g., when
  1115. * mounting a device.
  1116. */
  1117. handle_bad_sector(bio);
  1118. return 1;
  1119. }
  1120. }
  1121. return 0;
  1122. }
  1123. /**
  1124. * generic_make_request - hand a buffer to its device driver for I/O
  1125. * @bio: The bio describing the location in memory and on the device.
  1126. *
  1127. * generic_make_request() is used to make I/O requests of block
  1128. * devices. It is passed a &struct bio, which describes the I/O that needs
  1129. * to be done.
  1130. *
  1131. * generic_make_request() does not return any status. The
  1132. * success/failure status of the request, along with notification of
  1133. * completion, is delivered asynchronously through the bio->bi_end_io
  1134. * function described (one day) else where.
  1135. *
  1136. * The caller of generic_make_request must make sure that bi_io_vec
  1137. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  1138. * set to describe the device address, and the
  1139. * bi_end_io and optionally bi_private are set to describe how
  1140. * completion notification should be signaled.
  1141. *
  1142. * generic_make_request and the drivers it calls may use bi_next if this
  1143. * bio happens to be merged with someone else, and may change bi_dev and
  1144. * bi_sector for remaps as it sees fit. So the values of these fields
  1145. * should NOT be depended on after the call to generic_make_request.
  1146. */
  1147. static inline void __generic_make_request(struct bio *bio)
  1148. {
  1149. struct request_queue *q;
  1150. sector_t old_sector;
  1151. int ret, nr_sectors = bio_sectors(bio);
  1152. dev_t old_dev;
  1153. int err = -EIO;
  1154. might_sleep();
  1155. if (bio_check_eod(bio, nr_sectors))
  1156. goto end_io;
  1157. /*
  1158. * Resolve the mapping until finished. (drivers are
  1159. * still free to implement/resolve their own stacking
  1160. * by explicitly returning 0)
  1161. *
  1162. * NOTE: we don't repeat the blk_size check for each new device.
  1163. * Stacking drivers are expected to know what they are doing.
  1164. */
  1165. old_sector = -1;
  1166. old_dev = 0;
  1167. do {
  1168. char b[BDEVNAME_SIZE];
  1169. q = bdev_get_queue(bio->bi_bdev);
  1170. if (!q) {
  1171. printk(KERN_ERR
  1172. "generic_make_request: Trying to access "
  1173. "nonexistent block-device %s (%Lu)\n",
  1174. bdevname(bio->bi_bdev, b),
  1175. (long long) bio->bi_sector);
  1176. end_io:
  1177. bio_endio(bio, err);
  1178. break;
  1179. }
  1180. if (unlikely(nr_sectors > q->max_hw_sectors)) {
  1181. printk(KERN_ERR "bio too big device %s (%u > %u)\n",
  1182. bdevname(bio->bi_bdev, b),
  1183. bio_sectors(bio),
  1184. q->max_hw_sectors);
  1185. goto end_io;
  1186. }
  1187. if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
  1188. goto end_io;
  1189. if (should_fail_request(bio))
  1190. goto end_io;
  1191. /*
  1192. * If this device has partitions, remap block n
  1193. * of partition p to block n+start(p) of the disk.
  1194. */
  1195. blk_partition_remap(bio);
  1196. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
  1197. goto end_io;
  1198. if (old_sector != -1)
  1199. blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
  1200. old_sector);
  1201. blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
  1202. old_sector = bio->bi_sector;
  1203. old_dev = bio->bi_bdev->bd_dev;
  1204. if (bio_check_eod(bio, nr_sectors))
  1205. goto end_io;
  1206. if ((bio_empty_barrier(bio) && !q->prepare_flush_fn) ||
  1207. (bio_discard(bio) && !q->prepare_discard_fn)) {
  1208. err = -EOPNOTSUPP;
  1209. goto end_io;
  1210. }
  1211. ret = q->make_request_fn(q, bio);
  1212. } while (ret);
  1213. }
  1214. /*
  1215. * We only want one ->make_request_fn to be active at a time,
  1216. * else stack usage with stacked devices could be a problem.
  1217. * So use current->bio_{list,tail} to keep a list of requests
  1218. * submited by a make_request_fn function.
  1219. * current->bio_tail is also used as a flag to say if
  1220. * generic_make_request is currently active in this task or not.
  1221. * If it is NULL, then no make_request is active. If it is non-NULL,
  1222. * then a make_request is active, and new requests should be added
  1223. * at the tail
  1224. */
  1225. void generic_make_request(struct bio *bio)
  1226. {
  1227. if (current->bio_tail) {
  1228. /* make_request is active */
  1229. *(current->bio_tail) = bio;
  1230. bio->bi_next = NULL;
  1231. current->bio_tail = &bio->bi_next;
  1232. return;
  1233. }
  1234. /* following loop may be a bit non-obvious, and so deserves some
  1235. * explanation.
  1236. * Before entering the loop, bio->bi_next is NULL (as all callers
  1237. * ensure that) so we have a list with a single bio.
  1238. * We pretend that we have just taken it off a longer list, so
  1239. * we assign bio_list to the next (which is NULL) and bio_tail
  1240. * to &bio_list, thus initialising the bio_list of new bios to be
  1241. * added. __generic_make_request may indeed add some more bios
  1242. * through a recursive call to generic_make_request. If it
  1243. * did, we find a non-NULL value in bio_list and re-enter the loop
  1244. * from the top. In this case we really did just take the bio
  1245. * of the top of the list (no pretending) and so fixup bio_list and
  1246. * bio_tail or bi_next, and call into __generic_make_request again.
  1247. *
  1248. * The loop was structured like this to make only one call to
  1249. * __generic_make_request (which is important as it is large and
  1250. * inlined) and to keep the structure simple.
  1251. */
  1252. BUG_ON(bio->bi_next);
  1253. do {
  1254. current->bio_list = bio->bi_next;
  1255. if (bio->bi_next == NULL)
  1256. current->bio_tail = &current->bio_list;
  1257. else
  1258. bio->bi_next = NULL;
  1259. __generic_make_request(bio);
  1260. bio = current->bio_list;
  1261. } while (bio);
  1262. current->bio_tail = NULL; /* deactivate */
  1263. }
  1264. EXPORT_SYMBOL(generic_make_request);
  1265. /**
  1266. * submit_bio - submit a bio to the block device layer for I/O
  1267. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  1268. * @bio: The &struct bio which describes the I/O
  1269. *
  1270. * submit_bio() is very similar in purpose to generic_make_request(), and
  1271. * uses that function to do most of the work. Both are fairly rough
  1272. * interfaces; @bio must be presetup and ready for I/O.
  1273. *
  1274. */
  1275. void submit_bio(int rw, struct bio *bio)
  1276. {
  1277. int count = bio_sectors(bio);
  1278. bio->bi_rw |= rw;
  1279. /*
  1280. * If it's a regular read/write or a barrier with data attached,
  1281. * go through the normal accounting stuff before submission.
  1282. */
  1283. if (bio_has_data(bio)) {
  1284. if (rw & WRITE) {
  1285. count_vm_events(PGPGOUT, count);
  1286. } else {
  1287. task_io_account_read(bio->bi_size);
  1288. count_vm_events(PGPGIN, count);
  1289. }
  1290. if (unlikely(block_dump)) {
  1291. char b[BDEVNAME_SIZE];
  1292. printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
  1293. current->comm, task_pid_nr(current),
  1294. (rw & WRITE) ? "WRITE" : "READ",
  1295. (unsigned long long)bio->bi_sector,
  1296. bdevname(bio->bi_bdev, b));
  1297. }
  1298. }
  1299. generic_make_request(bio);
  1300. }
  1301. EXPORT_SYMBOL(submit_bio);
  1302. /**
  1303. * blk_rq_check_limits - Helper function to check a request for the queue limit
  1304. * @q: the queue
  1305. * @rq: the request being checked
  1306. *
  1307. * Description:
  1308. * @rq may have been made based on weaker limitations of upper-level queues
  1309. * in request stacking drivers, and it may violate the limitation of @q.
  1310. * Since the block layer and the underlying device driver trust @rq
  1311. * after it is inserted to @q, it should be checked against @q before
  1312. * the insertion using this generic function.
  1313. *
  1314. * This function should also be useful for request stacking drivers
  1315. * in some cases below, so export this fuction.
  1316. * Request stacking drivers like request-based dm may change the queue
  1317. * limits while requests are in the queue (e.g. dm's table swapping).
  1318. * Such request stacking drivers should check those requests agaist
  1319. * the new queue limits again when they dispatch those requests,
  1320. * although such checkings are also done against the old queue limits
  1321. * when submitting requests.
  1322. */
  1323. int blk_rq_check_limits(struct request_queue *q, struct request *rq)
  1324. {
  1325. if (rq->nr_sectors > q->max_sectors ||
  1326. rq->data_len > q->max_hw_sectors << 9) {
  1327. printk(KERN_ERR "%s: over max size limit.\n", __func__);
  1328. return -EIO;
  1329. }
  1330. /*
  1331. * queue's settings related to segment counting like q->bounce_pfn
  1332. * may differ from that of other stacking queues.
  1333. * Recalculate it to check the request correctly on this queue's
  1334. * limitation.
  1335. */
  1336. blk_recalc_rq_segments(rq);
  1337. if (rq->nr_phys_segments > q->max_phys_segments ||
  1338. rq->nr_phys_segments > q->max_hw_segments) {
  1339. printk(KERN_ERR "%s: over max segments limit.\n", __func__);
  1340. return -EIO;
  1341. }
  1342. return 0;
  1343. }
  1344. EXPORT_SYMBOL_GPL(blk_rq_check_limits);
  1345. /**
  1346. * blk_insert_cloned_request - Helper for stacking drivers to submit a request
  1347. * @q: the queue to submit the request
  1348. * @rq: the request being queued
  1349. */
  1350. int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
  1351. {
  1352. unsigned long flags;
  1353. if (blk_rq_check_limits(q, rq))
  1354. return -EIO;
  1355. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1356. if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
  1357. should_fail(&fail_make_request, blk_rq_bytes(rq)))
  1358. return -EIO;
  1359. #endif
  1360. spin_lock_irqsave(q->queue_lock, flags);
  1361. /*
  1362. * Submitting request must be dequeued before calling this function
  1363. * because it will be linked to another request_queue
  1364. */
  1365. BUG_ON(blk_queued_rq(rq));
  1366. drive_stat_acct(rq, 1);
  1367. __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
  1368. spin_unlock_irqrestore(q->queue_lock, flags);
  1369. return 0;
  1370. }
  1371. EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
  1372. /**
  1373. * __end_that_request_first - end I/O on a request
  1374. * @req: the request being processed
  1375. * @error: %0 for success, < %0 for error
  1376. * @nr_bytes: number of bytes to complete
  1377. *
  1378. * Description:
  1379. * Ends I/O on a number of bytes attached to @req, and sets it up
  1380. * for the next range of segments (if any) in the cluster.
  1381. *
  1382. * Return:
  1383. * %0 - we are done with this request, call end_that_request_last()
  1384. * %1 - still buffers pending for this request
  1385. **/
  1386. static int __end_that_request_first(struct request *req, int error,
  1387. int nr_bytes)
  1388. {
  1389. int total_bytes, bio_nbytes, next_idx = 0;
  1390. struct bio *bio;
  1391. blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
  1392. /*
  1393. * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
  1394. * sense key with us all the way through
  1395. */
  1396. if (!blk_pc_request(req))
  1397. req->errors = 0;
  1398. if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
  1399. printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
  1400. req->rq_disk ? req->rq_disk->disk_name : "?",
  1401. (unsigned long long)req->sector);
  1402. }
  1403. if (blk_fs_request(req) && req->rq_disk) {
  1404. const int rw = rq_data_dir(req);
  1405. struct hd_struct *part;
  1406. int cpu;
  1407. cpu = part_stat_lock();
  1408. part = disk_map_sector_rcu(req->rq_disk, req->sector);
  1409. part_stat_add(cpu, part, sectors[rw], nr_bytes >> 9);
  1410. part_stat_unlock();
  1411. }
  1412. total_bytes = bio_nbytes = 0;
  1413. while ((bio = req->bio) != NULL) {
  1414. int nbytes;
  1415. /*
  1416. * For an empty barrier request, the low level driver must
  1417. * store a potential error location in ->sector. We pass
  1418. * that back up in ->bi_sector.
  1419. */
  1420. if (blk_empty_barrier(req))
  1421. bio->bi_sector = req->sector;
  1422. if (nr_bytes >= bio->bi_size) {
  1423. req->bio = bio->bi_next;
  1424. nbytes = bio->bi_size;
  1425. req_bio_endio(req, bio, nbytes, error);
  1426. next_idx = 0;
  1427. bio_nbytes = 0;
  1428. } else {
  1429. int idx = bio->bi_idx + next_idx;
  1430. if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
  1431. blk_dump_rq_flags(req, "__end_that");
  1432. printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
  1433. __func__, bio->bi_idx, bio->bi_vcnt);
  1434. break;
  1435. }
  1436. nbytes = bio_iovec_idx(bio, idx)->bv_len;
  1437. BIO_BUG_ON(nbytes > bio->bi_size);
  1438. /*
  1439. * not a complete bvec done
  1440. */
  1441. if (unlikely(nbytes > nr_bytes)) {
  1442. bio_nbytes += nr_bytes;
  1443. total_bytes += nr_bytes;
  1444. break;
  1445. }
  1446. /*
  1447. * advance to the next vector
  1448. */
  1449. next_idx++;
  1450. bio_nbytes += nbytes;
  1451. }
  1452. total_bytes += nbytes;
  1453. nr_bytes -= nbytes;
  1454. bio = req->bio;
  1455. if (bio) {
  1456. /*
  1457. * end more in this run, or just return 'not-done'
  1458. */
  1459. if (unlikely(nr_bytes <= 0))
  1460. break;
  1461. }
  1462. }
  1463. /*
  1464. * completely done
  1465. */
  1466. if (!req->bio)
  1467. return 0;
  1468. /*
  1469. * if the request wasn't completed, update state
  1470. */
  1471. if (bio_nbytes) {
  1472. req_bio_endio(req, bio, bio_nbytes, error);
  1473. bio->bi_idx += next_idx;
  1474. bio_iovec(bio)->bv_offset += nr_bytes;
  1475. bio_iovec(bio)->bv_len -= nr_bytes;
  1476. }
  1477. blk_recalc_rq_sectors(req, total_bytes >> 9);
  1478. blk_recalc_rq_segments(req);
  1479. return 1;
  1480. }
  1481. /*
  1482. * queue lock must be held
  1483. */
  1484. static void end_that_request_last(struct request *req, int error)
  1485. {
  1486. struct gendisk *disk = req->rq_disk;
  1487. blk_delete_timer(req);
  1488. if (blk_rq_tagged(req))
  1489. blk_queue_end_tag(req->q, req);
  1490. if (blk_queued_rq(req))
  1491. blkdev_dequeue_request(req);
  1492. if (unlikely(laptop_mode) && blk_fs_request(req))
  1493. laptop_io_completion();
  1494. /*
  1495. * Account IO completion. bar_rq isn't accounted as a normal
  1496. * IO on queueing nor completion. Accounting the containing
  1497. * request is enough.
  1498. */
  1499. if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
  1500. unsigned long duration = jiffies - req->start_time;
  1501. const int rw = rq_data_dir(req);
  1502. struct hd_struct *part;
  1503. int cpu;
  1504. cpu = part_stat_lock();
  1505. part = disk_map_sector_rcu(disk, req->sector);
  1506. part_stat_inc(cpu, part, ios[rw]);
  1507. part_stat_add(cpu, part, ticks[rw], duration);
  1508. part_round_stats(cpu, part);
  1509. part_dec_in_flight(part);
  1510. part_stat_unlock();
  1511. }
  1512. if (req->end_io)
  1513. req->end_io(req, error);
  1514. else {
  1515. if (blk_bidi_rq(req))
  1516. __blk_put_request(req->next_rq->q, req->next_rq);
  1517. __blk_put_request(req->q, req);
  1518. }
  1519. }
  1520. /**
  1521. * blk_rq_bytes - Returns bytes left to complete in the entire request
  1522. * @rq: the request being processed
  1523. **/
  1524. unsigned int blk_rq_bytes(struct request *rq)
  1525. {
  1526. if (blk_fs_request(rq))
  1527. return rq->hard_nr_sectors << 9;
  1528. return rq->data_len;
  1529. }
  1530. EXPORT_SYMBOL_GPL(blk_rq_bytes);
  1531. /**
  1532. * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
  1533. * @rq: the request being processed
  1534. **/
  1535. unsigned int blk_rq_cur_bytes(struct request *rq)
  1536. {
  1537. if (blk_fs_request(rq))
  1538. return rq->current_nr_sectors << 9;
  1539. if (rq->bio)
  1540. return rq->bio->bi_size;
  1541. return rq->data_len;
  1542. }
  1543. EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
  1544. /**
  1545. * end_request - end I/O on the current segment of the request
  1546. * @req: the request being processed
  1547. * @uptodate: error value or %0/%1 uptodate flag
  1548. *
  1549. * Description:
  1550. * Ends I/O on the current segment of a request. If that is the only
  1551. * remaining segment, the request is also completed and freed.
  1552. *
  1553. * This is a remnant of how older block drivers handled I/O completions.
  1554. * Modern drivers typically end I/O on the full request in one go, unless
  1555. * they have a residual value to account for. For that case this function
  1556. * isn't really useful, unless the residual just happens to be the
  1557. * full current segment. In other words, don't use this function in new
  1558. * code. Use blk_end_request() or __blk_end_request() to end a request.
  1559. **/
  1560. void end_request(struct request *req, int uptodate)
  1561. {
  1562. int error = 0;
  1563. if (uptodate <= 0)
  1564. error = uptodate ? uptodate : -EIO;
  1565. __blk_end_request(req, error, req->hard_cur_sectors << 9);
  1566. }
  1567. EXPORT_SYMBOL(end_request);
  1568. static int end_that_request_data(struct request *rq, int error,
  1569. unsigned int nr_bytes, unsigned int bidi_bytes)
  1570. {
  1571. if (rq->bio) {
  1572. if (__end_that_request_first(rq, error, nr_bytes))
  1573. return 1;
  1574. /* Bidi request must be completed as a whole */
  1575. if (blk_bidi_rq(rq) &&
  1576. __end_that_request_first(rq->next_rq, error, bidi_bytes))
  1577. return 1;
  1578. }
  1579. return 0;
  1580. }
  1581. /**
  1582. * blk_end_io - Generic end_io function to complete a request.
  1583. * @rq: the request being processed
  1584. * @error: %0 for success, < %0 for error
  1585. * @nr_bytes: number of bytes to complete @rq
  1586. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1587. * @drv_callback: function called between completion of bios in the request
  1588. * and completion of the request.
  1589. * If the callback returns non %0, this helper returns without
  1590. * completion of the request.
  1591. *
  1592. * Description:
  1593. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1594. * If @rq has leftover, sets it up for the next range of segments.
  1595. *
  1596. * Return:
  1597. * %0 - we are done with this request
  1598. * %1 - this request is not freed yet, it still has pending buffers.
  1599. **/
  1600. static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
  1601. unsigned int bidi_bytes,
  1602. int (drv_callback)(struct request *))
  1603. {
  1604. struct request_queue *q = rq->q;
  1605. unsigned long flags = 0UL;
  1606. if (end_that_request_data(rq, error, nr_bytes, bidi_bytes))
  1607. return 1;
  1608. /* Special feature for tricky drivers */
  1609. if (drv_callback && drv_callback(rq))
  1610. return 1;
  1611. add_disk_randomness(rq->rq_disk);
  1612. spin_lock_irqsave(q->queue_lock, flags);
  1613. end_that_request_last(rq, error);
  1614. spin_unlock_irqrestore(q->queue_lock, flags);
  1615. return 0;
  1616. }
  1617. /**
  1618. * blk_end_request - Helper function for drivers to complete the request.
  1619. * @rq: the request being processed
  1620. * @error: %0 for success, < %0 for error
  1621. * @nr_bytes: number of bytes to complete
  1622. *
  1623. * Description:
  1624. * Ends I/O on a number of bytes attached to @rq.
  1625. * If @rq has leftover, sets it up for the next range of segments.
  1626. *
  1627. * Return:
  1628. * %0 - we are done with this request
  1629. * %1 - still buffers pending for this request
  1630. **/
  1631. int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1632. {
  1633. return blk_end_io(rq, error, nr_bytes, 0, NULL);
  1634. }
  1635. EXPORT_SYMBOL_GPL(blk_end_request);
  1636. /**
  1637. * __blk_end_request - Helper function for drivers to complete the request.
  1638. * @rq: the request being processed
  1639. * @error: %0 for success, < %0 for error
  1640. * @nr_bytes: number of bytes to complete
  1641. *
  1642. * Description:
  1643. * Must be called with queue lock held unlike blk_end_request().
  1644. *
  1645. * Return:
  1646. * %0 - we are done with this request
  1647. * %1 - still buffers pending for this request
  1648. **/
  1649. int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1650. {
  1651. if (rq->bio && __end_that_request_first(rq, error, nr_bytes))
  1652. return 1;
  1653. add_disk_randomness(rq->rq_disk);
  1654. end_that_request_last(rq, error);
  1655. return 0;
  1656. }
  1657. EXPORT_SYMBOL_GPL(__blk_end_request);
  1658. /**
  1659. * blk_end_bidi_request - Helper function for drivers to complete bidi request.
  1660. * @rq: the bidi request being processed
  1661. * @error: %0 for success, < %0 for error
  1662. * @nr_bytes: number of bytes to complete @rq
  1663. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1664. *
  1665. * Description:
  1666. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1667. *
  1668. * Return:
  1669. * %0 - we are done with this request
  1670. * %1 - still buffers pending for this request
  1671. **/
  1672. int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
  1673. unsigned int bidi_bytes)
  1674. {
  1675. return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
  1676. }
  1677. EXPORT_SYMBOL_GPL(blk_end_bidi_request);
  1678. /**
  1679. * blk_update_request - Special helper function for request stacking drivers
  1680. * @rq: the request being processed
  1681. * @error: %0 for success, < %0 for error
  1682. * @nr_bytes: number of bytes to complete @rq
  1683. *
  1684. * Description:
  1685. * Ends I/O on a number of bytes attached to @rq, but doesn't complete
  1686. * the request structure even if @rq doesn't have leftover.
  1687. * If @rq has leftover, sets it up for the next range of segments.
  1688. *
  1689. * This special helper function is only for request stacking drivers
  1690. * (e.g. request-based dm) so that they can handle partial completion.
  1691. * Actual device drivers should use blk_end_request instead.
  1692. */
  1693. void blk_update_request(struct request *rq, int error, unsigned int nr_bytes)
  1694. {
  1695. if (!end_that_request_data(rq, error, nr_bytes, 0)) {
  1696. /*
  1697. * These members are not updated in end_that_request_data()
  1698. * when all bios are completed.
  1699. * Update them so that the request stacking driver can find
  1700. * how many bytes remain in the request later.
  1701. */
  1702. rq->nr_sectors = rq->hard_nr_sectors = 0;
  1703. rq->current_nr_sectors = rq->hard_cur_sectors = 0;
  1704. }
  1705. }
  1706. EXPORT_SYMBOL_GPL(blk_update_request);
  1707. /**
  1708. * blk_end_request_callback - Special helper function for tricky drivers
  1709. * @rq: the request being processed
  1710. * @error: %0 for success, < %0 for error
  1711. * @nr_bytes: number of bytes to complete
  1712. * @drv_callback: function called between completion of bios in the request
  1713. * and completion of the request.
  1714. * If the callback returns non %0, this helper returns without
  1715. * completion of the request.
  1716. *
  1717. * Description:
  1718. * Ends I/O on a number of bytes attached to @rq.
  1719. * If @rq has leftover, sets it up for the next range of segments.
  1720. *
  1721. * This special helper function is used only for existing tricky drivers.
  1722. * (e.g. cdrom_newpc_intr() of ide-cd)
  1723. * This interface will be removed when such drivers are rewritten.
  1724. * Don't use this interface in other places anymore.
  1725. *
  1726. * Return:
  1727. * %0 - we are done with this request
  1728. * %1 - this request is not freed yet.
  1729. * this request still has pending buffers or
  1730. * the driver doesn't want to finish this request yet.
  1731. **/
  1732. int blk_end_request_callback(struct request *rq, int error,
  1733. unsigned int nr_bytes,
  1734. int (drv_callback)(struct request *))
  1735. {
  1736. return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
  1737. }
  1738. EXPORT_SYMBOL_GPL(blk_end_request_callback);
  1739. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  1740. struct bio *bio)
  1741. {
  1742. /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
  1743. we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
  1744. rq->cmd_flags |= (bio->bi_rw & 3);
  1745. if (bio_has_data(bio)) {
  1746. rq->nr_phys_segments = bio_phys_segments(q, bio);
  1747. rq->buffer = bio_data(bio);
  1748. }
  1749. rq->current_nr_sectors = bio_cur_sectors(bio);
  1750. rq->hard_cur_sectors = rq->current_nr_sectors;
  1751. rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
  1752. rq->data_len = bio->bi_size;
  1753. rq->bio = rq->biotail = bio;
  1754. if (bio->bi_bdev)
  1755. rq->rq_disk = bio->bi_bdev->bd_disk;
  1756. }
  1757. /**
  1758. * blk_lld_busy - Check if underlying low-level drivers of a device are busy
  1759. * @q : the queue of the device being checked
  1760. *
  1761. * Description:
  1762. * Check if underlying low-level drivers of a device are busy.
  1763. * If the drivers want to export their busy state, they must set own
  1764. * exporting function using blk_queue_lld_busy() first.
  1765. *
  1766. * Basically, this function is used only by request stacking drivers
  1767. * to stop dispatching requests to underlying devices when underlying
  1768. * devices are busy. This behavior helps more I/O merging on the queue
  1769. * of the request stacking driver and prevents I/O throughput regression
  1770. * on burst I/O load.
  1771. *
  1772. * Return:
  1773. * 0 - Not busy (The request stacking driver should dispatch request)
  1774. * 1 - Busy (The request stacking driver should stop dispatching request)
  1775. */
  1776. int blk_lld_busy(struct request_queue *q)
  1777. {
  1778. if (q->lld_busy_fn)
  1779. return q->lld_busy_fn(q);
  1780. return 0;
  1781. }
  1782. EXPORT_SYMBOL_GPL(blk_lld_busy);
  1783. int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
  1784. {
  1785. return queue_work(kblockd_workqueue, work);
  1786. }
  1787. EXPORT_SYMBOL(kblockd_schedule_work);
  1788. void kblockd_flush_work(struct work_struct *work)
  1789. {
  1790. cancel_work_sync(work);
  1791. }
  1792. EXPORT_SYMBOL(kblockd_flush_work);
  1793. int __init blk_dev_init(void)
  1794. {
  1795. kblockd_workqueue = create_workqueue("kblockd");
  1796. if (!kblockd_workqueue)
  1797. panic("Failed to create kblockd\n");
  1798. request_cachep = kmem_cache_create("blkdev_requests",
  1799. sizeof(struct request), 0, SLAB_PANIC, NULL);
  1800. blk_requestq_cachep = kmem_cache_create("blkdev_queue",
  1801. sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
  1802. return 0;
  1803. }