blk-core.c 76 KB

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