blk-core.c 73 KB

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