blk-core.c 69 KB

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