blk-core.c 56 KB

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