blk-core.c 54 KB

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