blk-core.c 54 KB

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