blk-core.c 69 KB

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