blk-core.c 57 KB

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