blk-core.c 55 KB

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