blk-core.c 54 KB

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