blk-core.c 58 KB

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