blk-core.c 57 KB

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