blk-core.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
  4. * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  5. * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
  6. * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
  7. * - July2000
  8. * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
  9. */
  10. /*
  11. * This handles all read/write requests to block devices
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/bio.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/highmem.h>
  19. #include <linux/mm.h>
  20. #include <linux/kernel_stat.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/completion.h>
  24. #include <linux/slab.h>
  25. #include <linux/swap.h>
  26. #include <linux/writeback.h>
  27. #include <linux/task_io_accounting_ops.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/cpu.h>
  30. #include <linux/blktrace_api.h>
  31. #include <linux/fault-inject.h>
  32. #include "blk.h"
  33. static int __make_request(struct request_queue *q, struct bio *bio);
  34. /*
  35. * For the allocated request tables
  36. */
  37. static struct kmem_cache *request_cachep;
  38. /*
  39. * For queue allocation
  40. */
  41. struct kmem_cache *blk_requestq_cachep;
  42. /*
  43. * Controlling structure to kblockd
  44. */
  45. static struct workqueue_struct *kblockd_workqueue;
  46. static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
  47. static void drive_stat_acct(struct request *rq, int new_io)
  48. {
  49. int rw = rq_data_dir(rq);
  50. if (!blk_fs_request(rq) || !rq->rq_disk)
  51. return;
  52. if (!new_io) {
  53. __all_stat_inc(rq->rq_disk, merges[rw], rq->sector);
  54. } else {
  55. struct hd_struct *part = get_part(rq->rq_disk, rq->sector);
  56. disk_round_stats(rq->rq_disk);
  57. rq->rq_disk->in_flight++;
  58. if (part) {
  59. part_round_stats(part);
  60. part->in_flight++;
  61. }
  62. }
  63. }
  64. void blk_queue_congestion_threshold(struct request_queue *q)
  65. {
  66. int nr;
  67. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  68. if (nr > q->nr_requests)
  69. nr = q->nr_requests;
  70. q->nr_congestion_on = nr;
  71. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  72. if (nr < 1)
  73. nr = 1;
  74. q->nr_congestion_off = nr;
  75. }
  76. /**
  77. * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
  78. * @bdev: device
  79. *
  80. * Locates the passed device's request queue and returns the address of its
  81. * backing_dev_info
  82. *
  83. * Will return NULL if the request queue cannot be located.
  84. */
  85. struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
  86. {
  87. struct backing_dev_info *ret = NULL;
  88. struct request_queue *q = bdev_get_queue(bdev);
  89. if (q)
  90. ret = &q->backing_dev_info;
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(blk_get_backing_dev_info);
  94. void blk_rq_init(struct request_queue *q, struct request *rq)
  95. {
  96. memset(rq, 0, sizeof(*rq));
  97. INIT_LIST_HEAD(&rq->queuelist);
  98. INIT_LIST_HEAD(&rq->donelist);
  99. rq->q = q;
  100. rq->sector = rq->hard_sector = (sector_t) -1;
  101. INIT_HLIST_NODE(&rq->hash);
  102. RB_CLEAR_NODE(&rq->rb_node);
  103. rq->cmd = rq->__cmd;
  104. rq->tag = -1;
  105. rq->ref_count = 1;
  106. }
  107. EXPORT_SYMBOL(blk_rq_init);
  108. static void req_bio_endio(struct request *rq, struct bio *bio,
  109. unsigned int nbytes, int error)
  110. {
  111. struct request_queue *q = rq->q;
  112. if (&q->bar_rq != rq) {
  113. if (error)
  114. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  115. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  116. error = -EIO;
  117. if (unlikely(nbytes > bio->bi_size)) {
  118. printk(KERN_ERR "%s: want %u bytes done, %u left\n",
  119. __FUNCTION__, nbytes, bio->bi_size);
  120. nbytes = bio->bi_size;
  121. }
  122. bio->bi_size -= nbytes;
  123. bio->bi_sector += (nbytes >> 9);
  124. if (bio->bi_size == 0)
  125. bio_endio(bio, error);
  126. } else {
  127. /*
  128. * Okay, this is the barrier request in progress, just
  129. * record the error;
  130. */
  131. if (error && !q->orderr)
  132. q->orderr = error;
  133. }
  134. }
  135. void blk_dump_rq_flags(struct request *rq, char *msg)
  136. {
  137. int bit;
  138. printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
  139. rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
  140. rq->cmd_flags);
  141. printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
  142. (unsigned long long)rq->sector,
  143. rq->nr_sectors,
  144. rq->current_nr_sectors);
  145. printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
  146. rq->bio, rq->biotail,
  147. rq->buffer, rq->data,
  148. rq->data_len);
  149. if (blk_pc_request(rq)) {
  150. printk(KERN_INFO " cdb: ");
  151. for (bit = 0; bit < BLK_MAX_CDB; bit++)
  152. printk("%02x ", rq->cmd[bit]);
  153. printk("\n");
  154. }
  155. }
  156. EXPORT_SYMBOL(blk_dump_rq_flags);
  157. /*
  158. * "plug" the device if there are no outstanding requests: this will
  159. * force the transfer to start only after we have put all the requests
  160. * on the list.
  161. *
  162. * This is called with interrupts off and no requests on the queue and
  163. * with the queue lock held.
  164. */
  165. void blk_plug_device(struct request_queue *q)
  166. {
  167. WARN_ON(!irqs_disabled());
  168. /*
  169. * don't plug a stopped queue, it must be paired with blk_start_queue()
  170. * which will restart the queueing
  171. */
  172. if (blk_queue_stopped(q))
  173. return;
  174. if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
  175. __set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
  176. mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
  177. blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
  178. }
  179. }
  180. EXPORT_SYMBOL(blk_plug_device);
  181. /*
  182. * remove the queue from the plugged list, if present. called with
  183. * queue lock held and interrupts disabled.
  184. */
  185. int blk_remove_plug(struct request_queue *q)
  186. {
  187. WARN_ON(!irqs_disabled());
  188. if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
  189. return 0;
  190. queue_flag_clear(QUEUE_FLAG_PLUGGED, q);
  191. del_timer(&q->unplug_timer);
  192. return 1;
  193. }
  194. EXPORT_SYMBOL(blk_remove_plug);
  195. /*
  196. * remove the plug and let it rip..
  197. */
  198. void __generic_unplug_device(struct request_queue *q)
  199. {
  200. if (unlikely(blk_queue_stopped(q)))
  201. return;
  202. if (!blk_remove_plug(q))
  203. return;
  204. q->request_fn(q);
  205. }
  206. EXPORT_SYMBOL(__generic_unplug_device);
  207. /**
  208. * generic_unplug_device - fire a request queue
  209. * @q: The &struct request_queue in question
  210. *
  211. * Description:
  212. * Linux uses plugging to build bigger requests queues before letting
  213. * the device have at them. If a queue is plugged, the I/O scheduler
  214. * is still adding and merging requests on the queue. Once the queue
  215. * gets unplugged, the request_fn defined for the queue is invoked and
  216. * transfers started.
  217. **/
  218. void generic_unplug_device(struct request_queue *q)
  219. {
  220. spin_lock_irq(q->queue_lock);
  221. __generic_unplug_device(q);
  222. spin_unlock_irq(q->queue_lock);
  223. }
  224. EXPORT_SYMBOL(generic_unplug_device);
  225. static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
  226. struct page *page)
  227. {
  228. struct request_queue *q = bdi->unplug_io_data;
  229. blk_unplug(q);
  230. }
  231. void blk_unplug_work(struct work_struct *work)
  232. {
  233. struct request_queue *q =
  234. container_of(work, struct request_queue, unplug_work);
  235. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
  236. q->rq.count[READ] + q->rq.count[WRITE]);
  237. q->unplug_fn(q);
  238. }
  239. void blk_unplug_timeout(unsigned long data)
  240. {
  241. struct request_queue *q = (struct request_queue *)data;
  242. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
  243. q->rq.count[READ] + q->rq.count[WRITE]);
  244. kblockd_schedule_work(&q->unplug_work);
  245. }
  246. void blk_unplug(struct request_queue *q)
  247. {
  248. /*
  249. * devices don't necessarily have an ->unplug_fn defined
  250. */
  251. if (q->unplug_fn) {
  252. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
  253. q->rq.count[READ] + q->rq.count[WRITE]);
  254. q->unplug_fn(q);
  255. }
  256. }
  257. EXPORT_SYMBOL(blk_unplug);
  258. /**
  259. * blk_start_queue - restart a previously stopped queue
  260. * @q: The &struct request_queue in question
  261. *
  262. * Description:
  263. * blk_start_queue() will clear the stop flag on the queue, and call
  264. * the request_fn for the queue if it was in a stopped state when
  265. * entered. Also see blk_stop_queue(). Queue lock must be held.
  266. **/
  267. void blk_start_queue(struct request_queue *q)
  268. {
  269. WARN_ON(!irqs_disabled());
  270. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  271. /*
  272. * one level of recursion is ok and is much faster than kicking
  273. * the unplug handling
  274. */
  275. if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
  276. queue_flag_set(QUEUE_FLAG_REENTER, q);
  277. q->request_fn(q);
  278. queue_flag_clear(QUEUE_FLAG_REENTER, q);
  279. } else {
  280. blk_plug_device(q);
  281. kblockd_schedule_work(&q->unplug_work);
  282. }
  283. }
  284. EXPORT_SYMBOL(blk_start_queue);
  285. /**
  286. * blk_stop_queue - stop a queue
  287. * @q: The &struct request_queue in question
  288. *
  289. * Description:
  290. * The Linux block layer assumes that a block driver will consume all
  291. * entries on the request queue when the request_fn strategy is called.
  292. * Often this will not happen, because of hardware limitations (queue
  293. * depth settings). If a device driver gets a 'queue full' response,
  294. * or if it simply chooses not to queue more I/O at one point, it can
  295. * call this function to prevent the request_fn from being called until
  296. * the driver has signalled it's ready to go again. This happens by calling
  297. * blk_start_queue() to restart queue operations. Queue lock must be held.
  298. **/
  299. void blk_stop_queue(struct request_queue *q)
  300. {
  301. blk_remove_plug(q);
  302. queue_flag_set(QUEUE_FLAG_STOPPED, q);
  303. }
  304. EXPORT_SYMBOL(blk_stop_queue);
  305. /**
  306. * blk_sync_queue - cancel any pending callbacks on a queue
  307. * @q: the queue
  308. *
  309. * Description:
  310. * The block layer may perform asynchronous callback activity
  311. * on a queue, such as calling the unplug function after a timeout.
  312. * A block device may call blk_sync_queue to ensure that any
  313. * such activity is cancelled, thus allowing it to release resources
  314. * that the callbacks might use. The caller must already have made sure
  315. * that its ->make_request_fn will not re-add plugging prior to calling
  316. * this function.
  317. *
  318. */
  319. void blk_sync_queue(struct request_queue *q)
  320. {
  321. del_timer_sync(&q->unplug_timer);
  322. kblockd_flush_work(&q->unplug_work);
  323. }
  324. EXPORT_SYMBOL(blk_sync_queue);
  325. /**
  326. * blk_run_queue - run a single device queue
  327. * @q: The queue to run
  328. */
  329. void __blk_run_queue(struct request_queue *q)
  330. {
  331. blk_remove_plug(q);
  332. /*
  333. * Only recurse once to avoid overrunning the stack, let the unplug
  334. * handling reinvoke the handler shortly if we already got there.
  335. */
  336. if (!elv_queue_empty(q)) {
  337. if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
  338. queue_flag_set(QUEUE_FLAG_REENTER, q);
  339. q->request_fn(q);
  340. queue_flag_clear(QUEUE_FLAG_REENTER, q);
  341. } else {
  342. blk_plug_device(q);
  343. kblockd_schedule_work(&q->unplug_work);
  344. }
  345. }
  346. }
  347. EXPORT_SYMBOL(__blk_run_queue);
  348. /**
  349. * blk_run_queue - run a single device queue
  350. * @q: The queue to run
  351. */
  352. void blk_run_queue(struct request_queue *q)
  353. {
  354. unsigned long flags;
  355. spin_lock_irqsave(q->queue_lock, flags);
  356. __blk_run_queue(q);
  357. spin_unlock_irqrestore(q->queue_lock, flags);
  358. }
  359. EXPORT_SYMBOL(blk_run_queue);
  360. void blk_put_queue(struct request_queue *q)
  361. {
  362. kobject_put(&q->kobj);
  363. }
  364. void blk_cleanup_queue(struct request_queue *q)
  365. {
  366. mutex_lock(&q->sysfs_lock);
  367. queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
  368. mutex_unlock(&q->sysfs_lock);
  369. if (q->elevator)
  370. elevator_exit(q->elevator);
  371. blk_put_queue(q);
  372. }
  373. EXPORT_SYMBOL(blk_cleanup_queue);
  374. static int blk_init_free_list(struct request_queue *q)
  375. {
  376. struct request_list *rl = &q->rq;
  377. rl->count[READ] = rl->count[WRITE] = 0;
  378. rl->starved[READ] = rl->starved[WRITE] = 0;
  379. rl->elvpriv = 0;
  380. init_waitqueue_head(&rl->wait[READ]);
  381. init_waitqueue_head(&rl->wait[WRITE]);
  382. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  383. mempool_free_slab, request_cachep, q->node);
  384. if (!rl->rq_pool)
  385. return -ENOMEM;
  386. return 0;
  387. }
  388. struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
  389. {
  390. return blk_alloc_queue_node(gfp_mask, -1);
  391. }
  392. EXPORT_SYMBOL(blk_alloc_queue);
  393. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  394. {
  395. struct request_queue *q;
  396. int err;
  397. q = kmem_cache_alloc_node(blk_requestq_cachep,
  398. gfp_mask | __GFP_ZERO, node_id);
  399. if (!q)
  400. return NULL;
  401. q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
  402. q->backing_dev_info.unplug_io_data = q;
  403. err = bdi_init(&q->backing_dev_info);
  404. if (err) {
  405. kmem_cache_free(blk_requestq_cachep, q);
  406. return NULL;
  407. }
  408. init_timer(&q->unplug_timer);
  409. kobject_init(&q->kobj, &blk_queue_ktype);
  410. mutex_init(&q->sysfs_lock);
  411. return q;
  412. }
  413. EXPORT_SYMBOL(blk_alloc_queue_node);
  414. /**
  415. * blk_init_queue - prepare a request queue for use with a block device
  416. * @rfn: The function to be called to process requests that have been
  417. * placed on the queue.
  418. * @lock: Request queue spin lock
  419. *
  420. * Description:
  421. * If a block device wishes to use the standard request handling procedures,
  422. * which sorts requests and coalesces adjacent requests, then it must
  423. * call blk_init_queue(). The function @rfn will be called when there
  424. * are requests on the queue that need to be processed. If the device
  425. * supports plugging, then @rfn may not be called immediately when requests
  426. * are available on the queue, but may be called at some time later instead.
  427. * Plugged queues are generally unplugged when a buffer belonging to one
  428. * of the requests on the queue is needed, or due to memory pressure.
  429. *
  430. * @rfn is not required, or even expected, to remove all requests off the
  431. * queue, but only as many as it can handle at a time. If it does leave
  432. * requests on the queue, it is responsible for arranging that the requests
  433. * get dealt with eventually.
  434. *
  435. * The queue spin lock must be held while manipulating the requests on the
  436. * request queue; this lock will be taken also from interrupt context, so irq
  437. * disabling is needed for it.
  438. *
  439. * Function returns a pointer to the initialized request queue, or NULL if
  440. * it didn't succeed.
  441. *
  442. * Note:
  443. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  444. * when the block device is deactivated (such as at module unload).
  445. **/
  446. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  447. {
  448. return blk_init_queue_node(rfn, lock, -1);
  449. }
  450. EXPORT_SYMBOL(blk_init_queue);
  451. struct request_queue *
  452. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  453. {
  454. struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
  455. if (!q)
  456. return NULL;
  457. q->node = node_id;
  458. if (blk_init_free_list(q)) {
  459. kmem_cache_free(blk_requestq_cachep, q);
  460. return NULL;
  461. }
  462. /*
  463. * if caller didn't supply a lock, they get per-queue locking with
  464. * our embedded lock
  465. */
  466. if (!lock) {
  467. spin_lock_init(&q->__queue_lock);
  468. lock = &q->__queue_lock;
  469. }
  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 request_list *rl = &q->rq;
  688. prepare_to_wait_exclusive(&rl->wait[rw], &wait,
  689. TASK_UNINTERRUPTIBLE);
  690. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  691. if (!rq) {
  692. struct io_context *ioc;
  693. blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
  694. __generic_unplug_device(q);
  695. spin_unlock_irq(q->queue_lock);
  696. io_schedule();
  697. /*
  698. * After sleeping, we become a "batching" process and
  699. * will be able to allocate at least one request, and
  700. * up to a big batch of them for a small period time.
  701. * See ioc_batching, ioc_set_batching
  702. */
  703. ioc = current_io_context(GFP_NOIO, q->node);
  704. ioc_set_batching(q, ioc);
  705. spin_lock_irq(q->queue_lock);
  706. }
  707. finish_wait(&rl->wait[rw], &wait);
  708. }
  709. return rq;
  710. }
  711. struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
  712. {
  713. struct request *rq;
  714. BUG_ON(rw != READ && rw != WRITE);
  715. spin_lock_irq(q->queue_lock);
  716. if (gfp_mask & __GFP_WAIT) {
  717. rq = get_request_wait(q, rw, NULL);
  718. } else {
  719. rq = get_request(q, rw, NULL, gfp_mask);
  720. if (!rq)
  721. spin_unlock_irq(q->queue_lock);
  722. }
  723. /* q->queue_lock is unlocked at this point */
  724. return rq;
  725. }
  726. EXPORT_SYMBOL(blk_get_request);
  727. /**
  728. * blk_start_queueing - initiate dispatch of requests to device
  729. * @q: request queue to kick into gear
  730. *
  731. * This is basically a helper to remove the need to know whether a queue
  732. * is plugged or not if someone just wants to initiate dispatch of requests
  733. * for this queue.
  734. *
  735. * The queue lock must be held with interrupts disabled.
  736. */
  737. void blk_start_queueing(struct request_queue *q)
  738. {
  739. if (!blk_queue_plugged(q))
  740. q->request_fn(q);
  741. else
  742. __generic_unplug_device(q);
  743. }
  744. EXPORT_SYMBOL(blk_start_queueing);
  745. /**
  746. * blk_requeue_request - put a request back on queue
  747. * @q: request queue where request should be inserted
  748. * @rq: request to be inserted
  749. *
  750. * Description:
  751. * Drivers often keep queueing requests until the hardware cannot accept
  752. * more, when that condition happens we need to put the request back
  753. * on the queue. Must be called with queue lock held.
  754. */
  755. void blk_requeue_request(struct request_queue *q, struct request *rq)
  756. {
  757. blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
  758. if (blk_rq_tagged(rq))
  759. blk_queue_end_tag(q, rq);
  760. elv_requeue_request(q, rq);
  761. }
  762. EXPORT_SYMBOL(blk_requeue_request);
  763. /**
  764. * blk_insert_request - insert a special request in to a request queue
  765. * @q: request queue where request should be inserted
  766. * @rq: request to be inserted
  767. * @at_head: insert request at head or tail of queue
  768. * @data: private data
  769. *
  770. * Description:
  771. * Many block devices need to execute commands asynchronously, so they don't
  772. * block the whole kernel from preemption during request execution. This is
  773. * accomplished normally by inserting aritficial requests tagged as
  774. * REQ_SPECIAL in to the corresponding request queue, and letting them be
  775. * scheduled for actual execution by the request queue.
  776. *
  777. * We have the option of inserting the head or the tail of the queue.
  778. * Typically we use the tail for new ioctls and so forth. We use the head
  779. * of the queue for things like a QUEUE_FULL message from a device, or a
  780. * host that is unable to accept a particular command.
  781. */
  782. void blk_insert_request(struct request_queue *q, struct request *rq,
  783. int at_head, void *data)
  784. {
  785. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  786. unsigned long flags;
  787. /*
  788. * tell I/O scheduler that this isn't a regular read/write (ie it
  789. * must not attempt merges on this) and that it acts as a soft
  790. * barrier
  791. */
  792. rq->cmd_type = REQ_TYPE_SPECIAL;
  793. rq->cmd_flags |= REQ_SOFTBARRIER;
  794. rq->special = data;
  795. spin_lock_irqsave(q->queue_lock, flags);
  796. /*
  797. * If command is tagged, release the tag
  798. */
  799. if (blk_rq_tagged(rq))
  800. blk_queue_end_tag(q, rq);
  801. drive_stat_acct(rq, 1);
  802. __elv_add_request(q, rq, where, 0);
  803. blk_start_queueing(q);
  804. spin_unlock_irqrestore(q->queue_lock, flags);
  805. }
  806. EXPORT_SYMBOL(blk_insert_request);
  807. /*
  808. * add-request adds a request to the linked list.
  809. * queue lock is held and interrupts disabled, as we muck with the
  810. * request queue list.
  811. */
  812. static inline void add_request(struct request_queue *q, struct request *req)
  813. {
  814. drive_stat_acct(req, 1);
  815. /*
  816. * elevator indicated where it wants this request to be
  817. * inserted at elevator_merge time
  818. */
  819. __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
  820. }
  821. /*
  822. * disk_round_stats() - Round off the performance stats on a struct
  823. * disk_stats.
  824. *
  825. * The average IO queue length and utilisation statistics are maintained
  826. * by observing the current state of the queue length and the amount of
  827. * time it has been in this state for.
  828. *
  829. * Normally, that accounting is done on IO completion, but that can result
  830. * in more than a second's worth of IO being accounted for within any one
  831. * second, leading to >100% utilisation. To deal with that, we call this
  832. * function to do a round-off before returning the results when reading
  833. * /proc/diskstats. This accounts immediately for all queue usage up to
  834. * the current jiffies and restarts the counters again.
  835. */
  836. void disk_round_stats(struct gendisk *disk)
  837. {
  838. unsigned long now = jiffies;
  839. if (now == disk->stamp)
  840. return;
  841. if (disk->in_flight) {
  842. __disk_stat_add(disk, time_in_queue,
  843. disk->in_flight * (now - disk->stamp));
  844. __disk_stat_add(disk, io_ticks, (now - disk->stamp));
  845. }
  846. disk->stamp = now;
  847. }
  848. EXPORT_SYMBOL_GPL(disk_round_stats);
  849. void part_round_stats(struct hd_struct *part)
  850. {
  851. unsigned long now = jiffies;
  852. if (now == part->stamp)
  853. return;
  854. if (part->in_flight) {
  855. __part_stat_add(part, time_in_queue,
  856. part->in_flight * (now - part->stamp));
  857. __part_stat_add(part, io_ticks, (now - part->stamp));
  858. }
  859. part->stamp = now;
  860. }
  861. /*
  862. * queue lock must be held
  863. */
  864. void __blk_put_request(struct request_queue *q, struct request *req)
  865. {
  866. if (unlikely(!q))
  867. return;
  868. if (unlikely(--req->ref_count))
  869. return;
  870. elv_completed_request(q, req);
  871. /*
  872. * Request may not have originated from ll_rw_blk. if not,
  873. * it didn't come out of our reserved rq pools
  874. */
  875. if (req->cmd_flags & REQ_ALLOCED) {
  876. int rw = rq_data_dir(req);
  877. int priv = req->cmd_flags & REQ_ELVPRIV;
  878. BUG_ON(!list_empty(&req->queuelist));
  879. BUG_ON(!hlist_unhashed(&req->hash));
  880. blk_free_request(q, req);
  881. freed_request(q, rw, priv);
  882. }
  883. }
  884. EXPORT_SYMBOL_GPL(__blk_put_request);
  885. void blk_put_request(struct request *req)
  886. {
  887. unsigned long flags;
  888. struct request_queue *q = req->q;
  889. /*
  890. * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
  891. * following if (q) test.
  892. */
  893. if (q) {
  894. spin_lock_irqsave(q->queue_lock, flags);
  895. __blk_put_request(q, req);
  896. spin_unlock_irqrestore(q->queue_lock, flags);
  897. }
  898. }
  899. EXPORT_SYMBOL(blk_put_request);
  900. void init_request_from_bio(struct request *req, struct bio *bio)
  901. {
  902. req->cmd_type = REQ_TYPE_FS;
  903. /*
  904. * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
  905. */
  906. if (bio_rw_ahead(bio) || bio_failfast(bio))
  907. req->cmd_flags |= REQ_FAILFAST;
  908. /*
  909. * REQ_BARRIER implies no merging, but lets make it explicit
  910. */
  911. if (unlikely(bio_barrier(bio)))
  912. req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
  913. if (bio_sync(bio))
  914. req->cmd_flags |= REQ_RW_SYNC;
  915. if (bio_rw_meta(bio))
  916. req->cmd_flags |= REQ_RW_META;
  917. req->errors = 0;
  918. req->hard_sector = req->sector = bio->bi_sector;
  919. req->ioprio = bio_prio(bio);
  920. req->start_time = jiffies;
  921. blk_rq_bio_prep(req->q, req, bio);
  922. }
  923. static int __make_request(struct request_queue *q, struct bio *bio)
  924. {
  925. struct request *req;
  926. int el_ret, nr_sectors, barrier, err;
  927. const unsigned short prio = bio_prio(bio);
  928. const int sync = bio_sync(bio);
  929. int rw_flags;
  930. nr_sectors = bio_sectors(bio);
  931. /*
  932. * low level driver can indicate that it wants pages above a
  933. * certain limit bounced to low memory (ie for highmem, or even
  934. * ISA dma in theory)
  935. */
  936. blk_queue_bounce(q, &bio);
  937. barrier = bio_barrier(bio);
  938. if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
  939. err = -EOPNOTSUPP;
  940. goto end_io;
  941. }
  942. spin_lock_irq(q->queue_lock);
  943. if (unlikely(barrier) || elv_queue_empty(q))
  944. goto get_rq;
  945. el_ret = elv_merge(q, &req, bio);
  946. switch (el_ret) {
  947. case ELEVATOR_BACK_MERGE:
  948. BUG_ON(!rq_mergeable(req));
  949. if (!ll_back_merge_fn(q, req, bio))
  950. break;
  951. blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
  952. req->biotail->bi_next = bio;
  953. req->biotail = bio;
  954. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  955. req->ioprio = ioprio_best(req->ioprio, prio);
  956. drive_stat_acct(req, 0);
  957. if (!attempt_back_merge(q, req))
  958. elv_merged_request(q, req, el_ret);
  959. goto out;
  960. case ELEVATOR_FRONT_MERGE:
  961. BUG_ON(!rq_mergeable(req));
  962. if (!ll_front_merge_fn(q, req, bio))
  963. break;
  964. blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
  965. bio->bi_next = req->bio;
  966. req->bio = bio;
  967. /*
  968. * may not be valid. if the low level driver said
  969. * it didn't need a bounce buffer then it better
  970. * not touch req->buffer either...
  971. */
  972. req->buffer = bio_data(bio);
  973. req->current_nr_sectors = bio_cur_sectors(bio);
  974. req->hard_cur_sectors = req->current_nr_sectors;
  975. req->sector = req->hard_sector = bio->bi_sector;
  976. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  977. req->ioprio = ioprio_best(req->ioprio, prio);
  978. drive_stat_acct(req, 0);
  979. if (!attempt_front_merge(q, req))
  980. elv_merged_request(q, req, el_ret);
  981. goto out;
  982. /* ELV_NO_MERGE: elevator says don't/can't merge. */
  983. default:
  984. ;
  985. }
  986. get_rq:
  987. /*
  988. * This sync check and mask will be re-done in init_request_from_bio(),
  989. * but we need to set it earlier to expose the sync flag to the
  990. * rq allocator and io schedulers.
  991. */
  992. rw_flags = bio_data_dir(bio);
  993. if (sync)
  994. rw_flags |= REQ_RW_SYNC;
  995. /*
  996. * Grab a free request. This is might sleep but can not fail.
  997. * Returns with the queue unlocked.
  998. */
  999. req = get_request_wait(q, rw_flags, bio);
  1000. /*
  1001. * After dropping the lock and possibly sleeping here, our request
  1002. * may now be mergeable after it had proven unmergeable (above).
  1003. * We don't worry about that case for efficiency. It won't happen
  1004. * often, and the elevators are able to handle it.
  1005. */
  1006. init_request_from_bio(req, bio);
  1007. spin_lock_irq(q->queue_lock);
  1008. if (elv_queue_empty(q))
  1009. blk_plug_device(q);
  1010. add_request(q, req);
  1011. out:
  1012. if (sync)
  1013. __generic_unplug_device(q);
  1014. spin_unlock_irq(q->queue_lock);
  1015. return 0;
  1016. end_io:
  1017. bio_endio(bio, err);
  1018. return 0;
  1019. }
  1020. /*
  1021. * If bio->bi_dev is a partition, remap the location
  1022. */
  1023. static inline void blk_partition_remap(struct bio *bio)
  1024. {
  1025. struct block_device *bdev = bio->bi_bdev;
  1026. if (bio_sectors(bio) && bdev != bdev->bd_contains) {
  1027. struct hd_struct *p = bdev->bd_part;
  1028. bio->bi_sector += p->start_sect;
  1029. bio->bi_bdev = bdev->bd_contains;
  1030. blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
  1031. bdev->bd_dev, bio->bi_sector,
  1032. bio->bi_sector - p->start_sect);
  1033. }
  1034. }
  1035. static void handle_bad_sector(struct bio *bio)
  1036. {
  1037. char b[BDEVNAME_SIZE];
  1038. printk(KERN_INFO "attempt to access beyond end of device\n");
  1039. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  1040. bdevname(bio->bi_bdev, b),
  1041. bio->bi_rw,
  1042. (unsigned long long)bio->bi_sector + bio_sectors(bio),
  1043. (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
  1044. set_bit(BIO_EOF, &bio->bi_flags);
  1045. }
  1046. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1047. static DECLARE_FAULT_ATTR(fail_make_request);
  1048. static int __init setup_fail_make_request(char *str)
  1049. {
  1050. return setup_fault_attr(&fail_make_request, str);
  1051. }
  1052. __setup("fail_make_request=", setup_fail_make_request);
  1053. static int should_fail_request(struct bio *bio)
  1054. {
  1055. if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
  1056. (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
  1057. return should_fail(&fail_make_request, bio->bi_size);
  1058. return 0;
  1059. }
  1060. static int __init fail_make_request_debugfs(void)
  1061. {
  1062. return init_fault_attr_dentries(&fail_make_request,
  1063. "fail_make_request");
  1064. }
  1065. late_initcall(fail_make_request_debugfs);
  1066. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1067. static inline int should_fail_request(struct bio *bio)
  1068. {
  1069. return 0;
  1070. }
  1071. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1072. /*
  1073. * Check whether this bio extends beyond the end of the device.
  1074. */
  1075. static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
  1076. {
  1077. sector_t maxsector;
  1078. if (!nr_sectors)
  1079. return 0;
  1080. /* Test device or partition size, when known. */
  1081. maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
  1082. if (maxsector) {
  1083. sector_t sector = bio->bi_sector;
  1084. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  1085. /*
  1086. * This may well happen - the kernel calls bread()
  1087. * without checking the size of the device, e.g., when
  1088. * mounting a device.
  1089. */
  1090. handle_bad_sector(bio);
  1091. return 1;
  1092. }
  1093. }
  1094. return 0;
  1095. }
  1096. /**
  1097. * generic_make_request: hand a buffer to its device driver for I/O
  1098. * @bio: The bio describing the location in memory and on the device.
  1099. *
  1100. * generic_make_request() is used to make I/O requests of block
  1101. * devices. It is passed a &struct bio, which describes the I/O that needs
  1102. * to be done.
  1103. *
  1104. * generic_make_request() does not return any status. The
  1105. * success/failure status of the request, along with notification of
  1106. * completion, is delivered asynchronously through the bio->bi_end_io
  1107. * function described (one day) else where.
  1108. *
  1109. * The caller of generic_make_request must make sure that bi_io_vec
  1110. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  1111. * set to describe the device address, and the
  1112. * bi_end_io and optionally bi_private are set to describe how
  1113. * completion notification should be signaled.
  1114. *
  1115. * generic_make_request and the drivers it calls may use bi_next if this
  1116. * bio happens to be merged with someone else, and may change bi_dev and
  1117. * bi_sector for remaps as it sees fit. So the values of these fields
  1118. * should NOT be depended on after the call to generic_make_request.
  1119. */
  1120. static inline void __generic_make_request(struct bio *bio)
  1121. {
  1122. struct request_queue *q;
  1123. sector_t old_sector;
  1124. int ret, nr_sectors = bio_sectors(bio);
  1125. dev_t old_dev;
  1126. int err = -EIO;
  1127. might_sleep();
  1128. if (bio_check_eod(bio, nr_sectors))
  1129. goto end_io;
  1130. /*
  1131. * Resolve the mapping until finished. (drivers are
  1132. * still free to implement/resolve their own stacking
  1133. * by explicitly returning 0)
  1134. *
  1135. * NOTE: we don't repeat the blk_size check for each new device.
  1136. * Stacking drivers are expected to know what they are doing.
  1137. */
  1138. old_sector = -1;
  1139. old_dev = 0;
  1140. do {
  1141. char b[BDEVNAME_SIZE];
  1142. q = bdev_get_queue(bio->bi_bdev);
  1143. if (!q) {
  1144. printk(KERN_ERR
  1145. "generic_make_request: Trying to access "
  1146. "nonexistent block-device %s (%Lu)\n",
  1147. bdevname(bio->bi_bdev, b),
  1148. (long long) bio->bi_sector);
  1149. end_io:
  1150. bio_endio(bio, err);
  1151. break;
  1152. }
  1153. if (unlikely(nr_sectors > q->max_hw_sectors)) {
  1154. printk(KERN_ERR "bio too big device %s (%u > %u)\n",
  1155. bdevname(bio->bi_bdev, b),
  1156. bio_sectors(bio),
  1157. q->max_hw_sectors);
  1158. goto end_io;
  1159. }
  1160. if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
  1161. goto end_io;
  1162. if (should_fail_request(bio))
  1163. goto end_io;
  1164. /*
  1165. * If this device has partitions, remap block n
  1166. * of partition p to block n+start(p) of the disk.
  1167. */
  1168. blk_partition_remap(bio);
  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. const int rw = rq_data_dir(req);
  1307. all_stat_add(req->rq_disk, sectors[rw],
  1308. nr_bytes >> 9, req->sector);
  1309. }
  1310. total_bytes = bio_nbytes = 0;
  1311. while ((bio = req->bio) != NULL) {
  1312. int nbytes;
  1313. /*
  1314. * For an empty barrier request, the low level driver must
  1315. * store a potential error location in ->sector. We pass
  1316. * that back up in ->bi_sector.
  1317. */
  1318. if (blk_empty_barrier(req))
  1319. bio->bi_sector = req->sector;
  1320. if (nr_bytes >= bio->bi_size) {
  1321. req->bio = bio->bi_next;
  1322. nbytes = bio->bi_size;
  1323. req_bio_endio(req, bio, nbytes, error);
  1324. next_idx = 0;
  1325. bio_nbytes = 0;
  1326. } else {
  1327. int idx = bio->bi_idx + next_idx;
  1328. if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
  1329. blk_dump_rq_flags(req, "__end_that");
  1330. printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
  1331. __FUNCTION__, bio->bi_idx,
  1332. 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, ios[rw], req->sector);
  1463. __all_stat_add(disk, 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, NULL);
  1743. register_hotcpu_notifier(&blk_cpu_notifier);
  1744. return 0;
  1745. }