blk-core.c 68 KB

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