blk-core.c 52 KB

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