blk-core.c 77 KB

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