blk-core.c 54 KB

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