blk-core.c 74 KB

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