blk-core.c 54 KB

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