blk-core.c 71 KB

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