blk-core.c 64 KB

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