blk-core.c 57 KB

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