blk-core.c 74 KB

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