blk-core.c 55 KB

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