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