blk-core.c 58 KB

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