blk-core.c 70 KB

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