blk-core.c 54 KB

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