blk-core.c 57 KB

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