blk-core.c 53 KB

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