blk-core.c 54 KB

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