blk-core.c 57 KB

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