blk-core.c 61 KB

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