blk-core.c 53 KB

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