blk-core.c 78 KB

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