blk-core.c 78 KB

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