blk-core.c 54 KB

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