blk-core.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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. rq->cpu = -1;
  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, &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. static void blk_invoke_request_fn(struct request_queue *q)
  274. {
  275. /*
  276. * one level of recursion is ok and is much faster than kicking
  277. * the unplug handling
  278. */
  279. if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
  280. q->request_fn(q);
  281. queue_flag_clear(QUEUE_FLAG_REENTER, q);
  282. } else {
  283. queue_flag_set(QUEUE_FLAG_PLUGGED, q);
  284. kblockd_schedule_work(q, &q->unplug_work);
  285. }
  286. }
  287. /**
  288. * blk_start_queue - restart a previously stopped queue
  289. * @q: The &struct request_queue in question
  290. *
  291. * Description:
  292. * blk_start_queue() will clear the stop flag on the queue, and call
  293. * the request_fn for the queue if it was in a stopped state when
  294. * entered. Also see blk_stop_queue(). Queue lock must be held.
  295. **/
  296. void blk_start_queue(struct request_queue *q)
  297. {
  298. WARN_ON(!irqs_disabled());
  299. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  300. blk_invoke_request_fn(q);
  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. blk_invoke_request_fn(q);
  356. }
  357. EXPORT_SYMBOL(__blk_run_queue);
  358. /**
  359. * blk_run_queue - run a single device queue
  360. * @q: The queue to run
  361. */
  362. void blk_run_queue(struct request_queue *q)
  363. {
  364. unsigned long flags;
  365. spin_lock_irqsave(q->queue_lock, flags);
  366. __blk_run_queue(q);
  367. spin_unlock_irqrestore(q->queue_lock, flags);
  368. }
  369. EXPORT_SYMBOL(blk_run_queue);
  370. void blk_put_queue(struct request_queue *q)
  371. {
  372. kobject_put(&q->kobj);
  373. }
  374. void blk_cleanup_queue(struct request_queue *q)
  375. {
  376. mutex_lock(&q->sysfs_lock);
  377. queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
  378. mutex_unlock(&q->sysfs_lock);
  379. if (q->elevator)
  380. elevator_exit(q->elevator);
  381. blk_put_queue(q);
  382. }
  383. EXPORT_SYMBOL(blk_cleanup_queue);
  384. static int blk_init_free_list(struct request_queue *q)
  385. {
  386. struct request_list *rl = &q->rq;
  387. rl->count[READ] = rl->count[WRITE] = 0;
  388. rl->starved[READ] = rl->starved[WRITE] = 0;
  389. rl->elvpriv = 0;
  390. init_waitqueue_head(&rl->wait[READ]);
  391. init_waitqueue_head(&rl->wait[WRITE]);
  392. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  393. mempool_free_slab, request_cachep, q->node);
  394. if (!rl->rq_pool)
  395. return -ENOMEM;
  396. return 0;
  397. }
  398. struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
  399. {
  400. return blk_alloc_queue_node(gfp_mask, -1);
  401. }
  402. EXPORT_SYMBOL(blk_alloc_queue);
  403. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  404. {
  405. struct request_queue *q;
  406. int err;
  407. q = kmem_cache_alloc_node(blk_requestq_cachep,
  408. gfp_mask | __GFP_ZERO, node_id);
  409. if (!q)
  410. return NULL;
  411. q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
  412. q->backing_dev_info.unplug_io_data = q;
  413. err = bdi_init(&q->backing_dev_info);
  414. if (err) {
  415. kmem_cache_free(blk_requestq_cachep, q);
  416. return NULL;
  417. }
  418. init_timer(&q->unplug_timer);
  419. kobject_init(&q->kobj, &blk_queue_ktype);
  420. mutex_init(&q->sysfs_lock);
  421. spin_lock_init(&q->__queue_lock);
  422. return q;
  423. }
  424. EXPORT_SYMBOL(blk_alloc_queue_node);
  425. /**
  426. * blk_init_queue - prepare a request queue for use with a block device
  427. * @rfn: The function to be called to process requests that have been
  428. * placed on the queue.
  429. * @lock: Request queue spin lock
  430. *
  431. * Description:
  432. * If a block device wishes to use the standard request handling procedures,
  433. * which sorts requests and coalesces adjacent requests, then it must
  434. * call blk_init_queue(). The function @rfn will be called when there
  435. * are requests on the queue that need to be processed. If the device
  436. * supports plugging, then @rfn may not be called immediately when requests
  437. * are available on the queue, but may be called at some time later instead.
  438. * Plugged queues are generally unplugged when a buffer belonging to one
  439. * of the requests on the queue is needed, or due to memory pressure.
  440. *
  441. * @rfn is not required, or even expected, to remove all requests off the
  442. * queue, but only as many as it can handle at a time. If it does leave
  443. * requests on the queue, it is responsible for arranging that the requests
  444. * get dealt with eventually.
  445. *
  446. * The queue spin lock must be held while manipulating the requests on the
  447. * request queue; this lock will be taken also from interrupt context, so irq
  448. * disabling is needed for it.
  449. *
  450. * Function returns a pointer to the initialized request queue, or %NULL if
  451. * it didn't succeed.
  452. *
  453. * Note:
  454. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  455. * when the block device is deactivated (such as at module unload).
  456. **/
  457. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  458. {
  459. return blk_init_queue_node(rfn, lock, -1);
  460. }
  461. EXPORT_SYMBOL(blk_init_queue);
  462. struct request_queue *
  463. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  464. {
  465. struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
  466. if (!q)
  467. return NULL;
  468. q->node = node_id;
  469. if (blk_init_free_list(q)) {
  470. kmem_cache_free(blk_requestq_cachep, q);
  471. return NULL;
  472. }
  473. /*
  474. * if caller didn't supply a lock, they get per-queue locking with
  475. * our embedded lock
  476. */
  477. if (!lock)
  478. lock = &q->__queue_lock;
  479. q->request_fn = rfn;
  480. q->prep_rq_fn = NULL;
  481. q->unplug_fn = generic_unplug_device;
  482. q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
  483. q->queue_lock = lock;
  484. blk_queue_segment_boundary(q, 0xffffffff);
  485. blk_queue_make_request(q, __make_request);
  486. blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
  487. blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
  488. blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
  489. q->sg_reserved_size = INT_MAX;
  490. blk_set_cmd_filter_defaults(&q->cmd_filter);
  491. /*
  492. * all done
  493. */
  494. if (!elevator_init(q, NULL)) {
  495. blk_queue_congestion_threshold(q);
  496. return q;
  497. }
  498. blk_put_queue(q);
  499. return NULL;
  500. }
  501. EXPORT_SYMBOL(blk_init_queue_node);
  502. int blk_get_queue(struct request_queue *q)
  503. {
  504. if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
  505. kobject_get(&q->kobj);
  506. return 0;
  507. }
  508. return 1;
  509. }
  510. static inline void blk_free_request(struct request_queue *q, struct request *rq)
  511. {
  512. if (rq->cmd_flags & REQ_ELVPRIV)
  513. elv_put_request(q, rq);
  514. mempool_free(rq, q->rq.rq_pool);
  515. }
  516. static struct request *
  517. blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
  518. {
  519. struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
  520. if (!rq)
  521. return NULL;
  522. blk_rq_init(q, rq);
  523. rq->cmd_flags = rw | REQ_ALLOCED;
  524. if (priv) {
  525. if (unlikely(elv_set_request(q, rq, gfp_mask))) {
  526. mempool_free(rq, q->rq.rq_pool);
  527. return NULL;
  528. }
  529. rq->cmd_flags |= REQ_ELVPRIV;
  530. }
  531. return rq;
  532. }
  533. /*
  534. * ioc_batching returns true if the ioc is a valid batching request and
  535. * should be given priority access to a request.
  536. */
  537. static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
  538. {
  539. if (!ioc)
  540. return 0;
  541. /*
  542. * Make sure the process is able to allocate at least 1 request
  543. * even if the batch times out, otherwise we could theoretically
  544. * lose wakeups.
  545. */
  546. return ioc->nr_batch_requests == q->nr_batching ||
  547. (ioc->nr_batch_requests > 0
  548. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  549. }
  550. /*
  551. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  552. * will cause the process to be a "batcher" on all queues in the system. This
  553. * is the behaviour we want though - once it gets a wakeup it should be given
  554. * a nice run.
  555. */
  556. static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
  557. {
  558. if (!ioc || ioc_batching(q, ioc))
  559. return;
  560. ioc->nr_batch_requests = q->nr_batching;
  561. ioc->last_waited = jiffies;
  562. }
  563. static void __freed_request(struct request_queue *q, int rw)
  564. {
  565. struct request_list *rl = &q->rq;
  566. if (rl->count[rw] < queue_congestion_off_threshold(q))
  567. blk_clear_queue_congested(q, rw);
  568. if (rl->count[rw] + 1 <= q->nr_requests) {
  569. if (waitqueue_active(&rl->wait[rw]))
  570. wake_up(&rl->wait[rw]);
  571. blk_clear_queue_full(q, rw);
  572. }
  573. }
  574. /*
  575. * A request has just been released. Account for it, update the full and
  576. * congestion status, wake up any waiters. Called under q->queue_lock.
  577. */
  578. static void freed_request(struct request_queue *q, int rw, int priv)
  579. {
  580. struct request_list *rl = &q->rq;
  581. rl->count[rw]--;
  582. if (priv)
  583. rl->elvpriv--;
  584. __freed_request(q, rw);
  585. if (unlikely(rl->starved[rw ^ 1]))
  586. __freed_request(q, rw ^ 1);
  587. }
  588. #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
  589. /*
  590. * Get a free request, queue_lock must be held.
  591. * Returns NULL on failure, with queue_lock held.
  592. * Returns !NULL on success, with queue_lock *not held*.
  593. */
  594. static struct request *get_request(struct request_queue *q, int rw_flags,
  595. struct bio *bio, gfp_t gfp_mask)
  596. {
  597. struct request *rq = NULL;
  598. struct request_list *rl = &q->rq;
  599. struct io_context *ioc = NULL;
  600. const int rw = rw_flags & 0x01;
  601. int may_queue, priv;
  602. may_queue = elv_may_queue(q, rw_flags);
  603. if (may_queue == ELV_MQUEUE_NO)
  604. goto rq_starved;
  605. if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
  606. if (rl->count[rw]+1 >= q->nr_requests) {
  607. ioc = current_io_context(GFP_ATOMIC, q->node);
  608. /*
  609. * The queue will fill after this allocation, so set
  610. * it as full, and mark this process as "batching".
  611. * This process will be allowed to complete a batch of
  612. * requests, others will be blocked.
  613. */
  614. if (!blk_queue_full(q, rw)) {
  615. ioc_set_batching(q, ioc);
  616. blk_set_queue_full(q, rw);
  617. } else {
  618. if (may_queue != ELV_MQUEUE_MUST
  619. && !ioc_batching(q, ioc)) {
  620. /*
  621. * The queue is full and the allocating
  622. * process is not a "batcher", and not
  623. * exempted by the IO scheduler
  624. */
  625. goto out;
  626. }
  627. }
  628. }
  629. blk_set_queue_congested(q, rw);
  630. }
  631. /*
  632. * Only allow batching queuers to allocate up to 50% over the defined
  633. * limit of requests, otherwise we could have thousands of requests
  634. * allocated with any setting of ->nr_requests
  635. */
  636. if (rl->count[rw] >= (3 * q->nr_requests / 2))
  637. goto out;
  638. rl->count[rw]++;
  639. rl->starved[rw] = 0;
  640. priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
  641. if (priv)
  642. rl->elvpriv++;
  643. spin_unlock_irq(q->queue_lock);
  644. rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
  645. if (unlikely(!rq)) {
  646. /*
  647. * Allocation failed presumably due to memory. Undo anything
  648. * we might have messed up.
  649. *
  650. * Allocating task should really be put onto the front of the
  651. * wait queue, but this is pretty rare.
  652. */
  653. spin_lock_irq(q->queue_lock);
  654. freed_request(q, rw, priv);
  655. /*
  656. * in the very unlikely event that allocation failed and no
  657. * requests for this direction was pending, mark us starved
  658. * so that freeing of a request in the other direction will
  659. * notice us. another possible fix would be to split the
  660. * rq mempool into READ and WRITE
  661. */
  662. rq_starved:
  663. if (unlikely(rl->count[rw] == 0))
  664. rl->starved[rw] = 1;
  665. goto out;
  666. }
  667. /*
  668. * ioc may be NULL here, and ioc_batching will be false. That's
  669. * OK, if the queue is under the request limit then requests need
  670. * not count toward the nr_batch_requests limit. There will always
  671. * be some limit enforced by BLK_BATCH_TIME.
  672. */
  673. if (ioc_batching(q, ioc))
  674. ioc->nr_batch_requests--;
  675. blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
  676. out:
  677. return rq;
  678. }
  679. /*
  680. * No available requests for this queue, unplug the device and wait for some
  681. * requests to become available.
  682. *
  683. * Called with q->queue_lock held, and returns with it unlocked.
  684. */
  685. static struct request *get_request_wait(struct request_queue *q, int rw_flags,
  686. struct bio *bio)
  687. {
  688. const int rw = rw_flags & 0x01;
  689. struct request *rq;
  690. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  691. while (!rq) {
  692. DEFINE_WAIT(wait);
  693. struct io_context *ioc;
  694. struct request_list *rl = &q->rq;
  695. prepare_to_wait_exclusive(&rl->wait[rw], &wait,
  696. TASK_UNINTERRUPTIBLE);
  697. blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
  698. __generic_unplug_device(q);
  699. spin_unlock_irq(q->queue_lock);
  700. io_schedule();
  701. /*
  702. * After sleeping, we become a "batching" process and
  703. * will be able to allocate at least one request, and
  704. * up to a big batch of them for a small period time.
  705. * See ioc_batching, ioc_set_batching
  706. */
  707. ioc = current_io_context(GFP_NOIO, q->node);
  708. ioc_set_batching(q, ioc);
  709. spin_lock_irq(q->queue_lock);
  710. finish_wait(&rl->wait[rw], &wait);
  711. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  712. };
  713. return rq;
  714. }
  715. struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
  716. {
  717. struct request *rq;
  718. BUG_ON(rw != READ && rw != WRITE);
  719. spin_lock_irq(q->queue_lock);
  720. if (gfp_mask & __GFP_WAIT) {
  721. rq = get_request_wait(q, rw, NULL);
  722. } else {
  723. rq = get_request(q, rw, NULL, gfp_mask);
  724. if (!rq)
  725. spin_unlock_irq(q->queue_lock);
  726. }
  727. /* q->queue_lock is unlocked at this point */
  728. return rq;
  729. }
  730. EXPORT_SYMBOL(blk_get_request);
  731. /**
  732. * blk_start_queueing - initiate dispatch of requests to device
  733. * @q: request queue to kick into gear
  734. *
  735. * This is basically a helper to remove the need to know whether a queue
  736. * is plugged or not if someone just wants to initiate dispatch of requests
  737. * for this queue.
  738. *
  739. * The queue lock must be held with interrupts disabled.
  740. */
  741. void blk_start_queueing(struct request_queue *q)
  742. {
  743. if (!blk_queue_plugged(q))
  744. q->request_fn(q);
  745. else
  746. __generic_unplug_device(q);
  747. }
  748. EXPORT_SYMBOL(blk_start_queueing);
  749. /**
  750. * blk_requeue_request - put a request back on queue
  751. * @q: request queue where request should be inserted
  752. * @rq: request to be inserted
  753. *
  754. * Description:
  755. * Drivers often keep queueing requests until the hardware cannot accept
  756. * more, when that condition happens we need to put the request back
  757. * on the queue. Must be called with queue lock held.
  758. */
  759. void blk_requeue_request(struct request_queue *q, struct request *rq)
  760. {
  761. blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
  762. if (blk_rq_tagged(rq))
  763. blk_queue_end_tag(q, rq);
  764. elv_requeue_request(q, rq);
  765. }
  766. EXPORT_SYMBOL(blk_requeue_request);
  767. /**
  768. * blk_insert_request - insert a special request into a request queue
  769. * @q: request queue where request should be inserted
  770. * @rq: request to be inserted
  771. * @at_head: insert request at head or tail of queue
  772. * @data: private data
  773. *
  774. * Description:
  775. * Many block devices need to execute commands asynchronously, so they don't
  776. * block the whole kernel from preemption during request execution. This is
  777. * accomplished normally by inserting aritficial requests tagged as
  778. * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
  779. * be scheduled for actual execution by the request queue.
  780. *
  781. * We have the option of inserting the head or the tail of the queue.
  782. * Typically we use the tail for new ioctls and so forth. We use the head
  783. * of the queue for things like a QUEUE_FULL message from a device, or a
  784. * host that is unable to accept a particular command.
  785. */
  786. void blk_insert_request(struct request_queue *q, struct request *rq,
  787. int at_head, void *data)
  788. {
  789. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  790. unsigned long flags;
  791. /*
  792. * tell I/O scheduler that this isn't a regular read/write (ie it
  793. * must not attempt merges on this) and that it acts as a soft
  794. * barrier
  795. */
  796. rq->cmd_type = REQ_TYPE_SPECIAL;
  797. rq->cmd_flags |= REQ_SOFTBARRIER;
  798. rq->special = data;
  799. spin_lock_irqsave(q->queue_lock, flags);
  800. /*
  801. * If command is tagged, release the tag
  802. */
  803. if (blk_rq_tagged(rq))
  804. blk_queue_end_tag(q, rq);
  805. drive_stat_acct(rq, 1);
  806. __elv_add_request(q, rq, where, 0);
  807. blk_start_queueing(q);
  808. spin_unlock_irqrestore(q->queue_lock, flags);
  809. }
  810. EXPORT_SYMBOL(blk_insert_request);
  811. /*
  812. * add-request adds a request to the linked list.
  813. * queue lock is held and interrupts disabled, as we muck with the
  814. * request queue list.
  815. */
  816. static inline void add_request(struct request_queue *q, struct request *req)
  817. {
  818. drive_stat_acct(req, 1);
  819. /*
  820. * elevator indicated where it wants this request to be
  821. * inserted at elevator_merge time
  822. */
  823. __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
  824. }
  825. static void part_round_stats_single(int cpu, struct hd_struct *part,
  826. unsigned long now)
  827. {
  828. if (now == part->stamp)
  829. return;
  830. if (part->in_flight) {
  831. __part_stat_add(cpu, part, time_in_queue,
  832. part->in_flight * (now - part->stamp));
  833. __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
  834. }
  835. part->stamp = now;
  836. }
  837. /**
  838. * part_round_stats() - Round off the performance stats on a struct
  839. * disk_stats.
  840. *
  841. * The average IO queue length and utilisation statistics are maintained
  842. * by observing the current state of the queue length and the amount of
  843. * time it has been in this state for.
  844. *
  845. * Normally, that accounting is done on IO completion, but that can result
  846. * in more than a second's worth of IO being accounted for within any one
  847. * second, leading to >100% utilisation. To deal with that, we call this
  848. * function to do a round-off before returning the results when reading
  849. * /proc/diskstats. This accounts immediately for all queue usage up to
  850. * the current jiffies and restarts the counters again.
  851. */
  852. void part_round_stats(int cpu, struct hd_struct *part)
  853. {
  854. unsigned long now = jiffies;
  855. if (part->partno)
  856. part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
  857. part_round_stats_single(cpu, part, now);
  858. }
  859. EXPORT_SYMBOL_GPL(part_round_stats);
  860. /*
  861. * queue lock must be held
  862. */
  863. void __blk_put_request(struct request_queue *q, struct request *req)
  864. {
  865. if (unlikely(!q))
  866. return;
  867. if (unlikely(--req->ref_count))
  868. return;
  869. elv_completed_request(q, req);
  870. /*
  871. * Request may not have originated from ll_rw_blk. if not,
  872. * it didn't come out of our reserved rq pools
  873. */
  874. if (req->cmd_flags & REQ_ALLOCED) {
  875. int rw = rq_data_dir(req);
  876. int priv = req->cmd_flags & REQ_ELVPRIV;
  877. BUG_ON(!list_empty(&req->queuelist));
  878. BUG_ON(!hlist_unhashed(&req->hash));
  879. blk_free_request(q, req);
  880. freed_request(q, rw, priv);
  881. }
  882. }
  883. EXPORT_SYMBOL_GPL(__blk_put_request);
  884. void blk_put_request(struct request *req)
  885. {
  886. unsigned long flags;
  887. struct request_queue *q = req->q;
  888. spin_lock_irqsave(q->queue_lock, flags);
  889. __blk_put_request(q, req);
  890. spin_unlock_irqrestore(q->queue_lock, flags);
  891. }
  892. EXPORT_SYMBOL(blk_put_request);
  893. void init_request_from_bio(struct request *req, struct bio *bio)
  894. {
  895. req->cpu = bio->bi_comp_cpu;
  896. req->cmd_type = REQ_TYPE_FS;
  897. /*
  898. * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
  899. */
  900. if (bio_rw_ahead(bio) || bio_failfast(bio))
  901. req->cmd_flags |= REQ_FAILFAST;
  902. /*
  903. * REQ_BARRIER implies no merging, but lets make it explicit
  904. */
  905. if (unlikely(bio_discard(bio))) {
  906. req->cmd_flags |= REQ_DISCARD;
  907. if (bio_barrier(bio))
  908. req->cmd_flags |= REQ_SOFTBARRIER;
  909. req->q->prepare_discard_fn(req->q, req);
  910. } else if (unlikely(bio_barrier(bio)))
  911. req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
  912. if (bio_sync(bio))
  913. req->cmd_flags |= REQ_RW_SYNC;
  914. if (bio_rw_meta(bio))
  915. req->cmd_flags |= REQ_RW_META;
  916. req->errors = 0;
  917. req->hard_sector = req->sector = bio->bi_sector;
  918. req->ioprio = bio_prio(bio);
  919. req->start_time = jiffies;
  920. blk_rq_bio_prep(req->q, req, bio);
  921. }
  922. static int __make_request(struct request_queue *q, struct bio *bio)
  923. {
  924. struct request *req;
  925. int el_ret, nr_sectors, barrier, discard, err;
  926. const unsigned short prio = bio_prio(bio);
  927. const int sync = bio_sync(bio);
  928. int rw_flags;
  929. nr_sectors = bio_sectors(bio);
  930. /*
  931. * low level driver can indicate that it wants pages above a
  932. * certain limit bounced to low memory (ie for highmem, or even
  933. * ISA dma in theory)
  934. */
  935. blk_queue_bounce(q, &bio);
  936. barrier = bio_barrier(bio);
  937. if (unlikely(barrier) && bio_has_data(bio) &&
  938. (q->next_ordered == QUEUE_ORDERED_NONE)) {
  939. err = -EOPNOTSUPP;
  940. goto end_io;
  941. }
  942. discard = bio_discard(bio);
  943. if (unlikely(discard) && !q->prepare_discard_fn) {
  944. err = -EOPNOTSUPP;
  945. goto end_io;
  946. }
  947. spin_lock_irq(q->queue_lock);
  948. if (unlikely(barrier) || elv_queue_empty(q))
  949. goto get_rq;
  950. el_ret = elv_merge(q, &req, bio);
  951. switch (el_ret) {
  952. case ELEVATOR_BACK_MERGE:
  953. BUG_ON(!rq_mergeable(req));
  954. if (!ll_back_merge_fn(q, req, bio))
  955. break;
  956. blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
  957. req->biotail->bi_next = bio;
  958. req->biotail = bio;
  959. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  960. req->ioprio = ioprio_best(req->ioprio, prio);
  961. if (!blk_rq_cpu_valid(req))
  962. req->cpu = bio->bi_comp_cpu;
  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. if (!blk_rq_cpu_valid(req))
  986. req->cpu = bio->bi_comp_cpu;
  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 (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
  1018. bio_flagged(bio, BIO_CPU_AFFINE))
  1019. req->cpu = blk_cpu_to_group(smp_processor_id());
  1020. if (elv_queue_empty(q))
  1021. blk_plug_device(q);
  1022. add_request(q, req);
  1023. out:
  1024. if (sync)
  1025. __generic_unplug_device(q);
  1026. spin_unlock_irq(q->queue_lock);
  1027. return 0;
  1028. end_io:
  1029. bio_endio(bio, err);
  1030. return 0;
  1031. }
  1032. /*
  1033. * If bio->bi_dev is a partition, remap the location
  1034. */
  1035. static inline void blk_partition_remap(struct bio *bio)
  1036. {
  1037. struct block_device *bdev = bio->bi_bdev;
  1038. if (bio_sectors(bio) && bdev != bdev->bd_contains) {
  1039. struct hd_struct *p = bdev->bd_part;
  1040. bio->bi_sector += p->start_sect;
  1041. bio->bi_bdev = bdev->bd_contains;
  1042. blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
  1043. bdev->bd_dev, bio->bi_sector,
  1044. bio->bi_sector - p->start_sect);
  1045. }
  1046. }
  1047. static void handle_bad_sector(struct bio *bio)
  1048. {
  1049. char b[BDEVNAME_SIZE];
  1050. printk(KERN_INFO "attempt to access beyond end of device\n");
  1051. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  1052. bdevname(bio->bi_bdev, b),
  1053. bio->bi_rw,
  1054. (unsigned long long)bio->bi_sector + bio_sectors(bio),
  1055. (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
  1056. set_bit(BIO_EOF, &bio->bi_flags);
  1057. }
  1058. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1059. static DECLARE_FAULT_ATTR(fail_make_request);
  1060. static int __init setup_fail_make_request(char *str)
  1061. {
  1062. return setup_fault_attr(&fail_make_request, str);
  1063. }
  1064. __setup("fail_make_request=", setup_fail_make_request);
  1065. static int should_fail_request(struct bio *bio)
  1066. {
  1067. struct hd_struct *part = bio->bi_bdev->bd_part;
  1068. if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
  1069. return should_fail(&fail_make_request, bio->bi_size);
  1070. return 0;
  1071. }
  1072. static int __init fail_make_request_debugfs(void)
  1073. {
  1074. return init_fault_attr_dentries(&fail_make_request,
  1075. "fail_make_request");
  1076. }
  1077. late_initcall(fail_make_request_debugfs);
  1078. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1079. static inline int should_fail_request(struct bio *bio)
  1080. {
  1081. return 0;
  1082. }
  1083. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1084. /*
  1085. * Check whether this bio extends beyond the end of the device.
  1086. */
  1087. static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
  1088. {
  1089. sector_t maxsector;
  1090. if (!nr_sectors)
  1091. return 0;
  1092. /* Test device or partition size, when known. */
  1093. maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
  1094. if (maxsector) {
  1095. sector_t sector = bio->bi_sector;
  1096. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  1097. /*
  1098. * This may well happen - the kernel calls bread()
  1099. * without checking the size of the device, e.g., when
  1100. * mounting a device.
  1101. */
  1102. handle_bad_sector(bio);
  1103. return 1;
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. /**
  1109. * generic_make_request - hand a buffer to its device driver for I/O
  1110. * @bio: The bio describing the location in memory and on the device.
  1111. *
  1112. * generic_make_request() is used to make I/O requests of block
  1113. * devices. It is passed a &struct bio, which describes the I/O that needs
  1114. * to be done.
  1115. *
  1116. * generic_make_request() does not return any status. The
  1117. * success/failure status of the request, along with notification of
  1118. * completion, is delivered asynchronously through the bio->bi_end_io
  1119. * function described (one day) else where.
  1120. *
  1121. * The caller of generic_make_request must make sure that bi_io_vec
  1122. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  1123. * set to describe the device address, and the
  1124. * bi_end_io and optionally bi_private are set to describe how
  1125. * completion notification should be signaled.
  1126. *
  1127. * generic_make_request and the drivers it calls may use bi_next if this
  1128. * bio happens to be merged with someone else, and may change bi_dev and
  1129. * bi_sector for remaps as it sees fit. So the values of these fields
  1130. * should NOT be depended on after the call to generic_make_request.
  1131. */
  1132. static inline void __generic_make_request(struct bio *bio)
  1133. {
  1134. struct request_queue *q;
  1135. sector_t old_sector;
  1136. int ret, nr_sectors = bio_sectors(bio);
  1137. dev_t old_dev;
  1138. int err = -EIO;
  1139. might_sleep();
  1140. if (bio_check_eod(bio, nr_sectors))
  1141. goto end_io;
  1142. /*
  1143. * Resolve the mapping until finished. (drivers are
  1144. * still free to implement/resolve their own stacking
  1145. * by explicitly returning 0)
  1146. *
  1147. * NOTE: we don't repeat the blk_size check for each new device.
  1148. * Stacking drivers are expected to know what they are doing.
  1149. */
  1150. old_sector = -1;
  1151. old_dev = 0;
  1152. do {
  1153. char b[BDEVNAME_SIZE];
  1154. q = bdev_get_queue(bio->bi_bdev);
  1155. if (!q) {
  1156. printk(KERN_ERR
  1157. "generic_make_request: Trying to access "
  1158. "nonexistent block-device %s (%Lu)\n",
  1159. bdevname(bio->bi_bdev, b),
  1160. (long long) bio->bi_sector);
  1161. end_io:
  1162. bio_endio(bio, err);
  1163. break;
  1164. }
  1165. if (unlikely(nr_sectors > q->max_hw_sectors)) {
  1166. printk(KERN_ERR "bio too big device %s (%u > %u)\n",
  1167. bdevname(bio->bi_bdev, b),
  1168. bio_sectors(bio),
  1169. q->max_hw_sectors);
  1170. goto end_io;
  1171. }
  1172. if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
  1173. goto end_io;
  1174. if (should_fail_request(bio))
  1175. goto end_io;
  1176. /*
  1177. * If this device has partitions, remap block n
  1178. * of partition p to block n+start(p) of the disk.
  1179. */
  1180. blk_partition_remap(bio);
  1181. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
  1182. goto end_io;
  1183. if (old_sector != -1)
  1184. blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
  1185. old_sector);
  1186. blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
  1187. old_sector = bio->bi_sector;
  1188. old_dev = bio->bi_bdev->bd_dev;
  1189. if (bio_check_eod(bio, nr_sectors))
  1190. goto end_io;
  1191. if ((bio_empty_barrier(bio) && !q->prepare_flush_fn) ||
  1192. (bio_discard(bio) && !q->prepare_discard_fn)) {
  1193. err = -EOPNOTSUPP;
  1194. goto end_io;
  1195. }
  1196. ret = q->make_request_fn(q, bio);
  1197. } while (ret);
  1198. }
  1199. /*
  1200. * We only want one ->make_request_fn to be active at a time,
  1201. * else stack usage with stacked devices could be a problem.
  1202. * So use current->bio_{list,tail} to keep a list of requests
  1203. * submited by a make_request_fn function.
  1204. * current->bio_tail is also used as a flag to say if
  1205. * generic_make_request is currently active in this task or not.
  1206. * If it is NULL, then no make_request is active. If it is non-NULL,
  1207. * then a make_request is active, and new requests should be added
  1208. * at the tail
  1209. */
  1210. void generic_make_request(struct bio *bio)
  1211. {
  1212. if (current->bio_tail) {
  1213. /* make_request is active */
  1214. *(current->bio_tail) = bio;
  1215. bio->bi_next = NULL;
  1216. current->bio_tail = &bio->bi_next;
  1217. return;
  1218. }
  1219. /* following loop may be a bit non-obvious, and so deserves some
  1220. * explanation.
  1221. * Before entering the loop, bio->bi_next is NULL (as all callers
  1222. * ensure that) so we have a list with a single bio.
  1223. * We pretend that we have just taken it off a longer list, so
  1224. * we assign bio_list to the next (which is NULL) and bio_tail
  1225. * to &bio_list, thus initialising the bio_list of new bios to be
  1226. * added. __generic_make_request may indeed add some more bios
  1227. * through a recursive call to generic_make_request. If it
  1228. * did, we find a non-NULL value in bio_list and re-enter the loop
  1229. * from the top. In this case we really did just take the bio
  1230. * of the top of the list (no pretending) and so fixup bio_list and
  1231. * bio_tail or bi_next, and call into __generic_make_request again.
  1232. *
  1233. * The loop was structured like this to make only one call to
  1234. * __generic_make_request (which is important as it is large and
  1235. * inlined) and to keep the structure simple.
  1236. */
  1237. BUG_ON(bio->bi_next);
  1238. do {
  1239. current->bio_list = bio->bi_next;
  1240. if (bio->bi_next == NULL)
  1241. current->bio_tail = &current->bio_list;
  1242. else
  1243. bio->bi_next = NULL;
  1244. __generic_make_request(bio);
  1245. bio = current->bio_list;
  1246. } while (bio);
  1247. current->bio_tail = NULL; /* deactivate */
  1248. }
  1249. EXPORT_SYMBOL(generic_make_request);
  1250. /**
  1251. * submit_bio - submit a bio to the block device layer for I/O
  1252. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  1253. * @bio: The &struct bio which describes the I/O
  1254. *
  1255. * submit_bio() is very similar in purpose to generic_make_request(), and
  1256. * uses that function to do most of the work. Both are fairly rough
  1257. * interfaces; @bio must be presetup and ready for I/O.
  1258. *
  1259. */
  1260. void submit_bio(int rw, struct bio *bio)
  1261. {
  1262. int count = bio_sectors(bio);
  1263. bio->bi_rw |= rw;
  1264. /*
  1265. * If it's a regular read/write or a barrier with data attached,
  1266. * go through the normal accounting stuff before submission.
  1267. */
  1268. if (bio_has_data(bio)) {
  1269. if (rw & WRITE) {
  1270. count_vm_events(PGPGOUT, count);
  1271. } else {
  1272. task_io_account_read(bio->bi_size);
  1273. count_vm_events(PGPGIN, count);
  1274. }
  1275. if (unlikely(block_dump)) {
  1276. char b[BDEVNAME_SIZE];
  1277. printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
  1278. current->comm, task_pid_nr(current),
  1279. (rw & WRITE) ? "WRITE" : "READ",
  1280. (unsigned long long)bio->bi_sector,
  1281. bdevname(bio->bi_bdev, b));
  1282. }
  1283. }
  1284. generic_make_request(bio);
  1285. }
  1286. EXPORT_SYMBOL(submit_bio);
  1287. /**
  1288. * __end_that_request_first - end I/O on a request
  1289. * @req: the request being processed
  1290. * @error: %0 for success, < %0 for error
  1291. * @nr_bytes: number of bytes to complete
  1292. *
  1293. * Description:
  1294. * Ends I/O on a number of bytes attached to @req, and sets it up
  1295. * for the next range of segments (if any) in the cluster.
  1296. *
  1297. * Return:
  1298. * %0 - we are done with this request, call end_that_request_last()
  1299. * %1 - still buffers pending for this request
  1300. **/
  1301. static int __end_that_request_first(struct request *req, int error,
  1302. int nr_bytes)
  1303. {
  1304. int total_bytes, bio_nbytes, next_idx = 0;
  1305. struct bio *bio;
  1306. blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
  1307. /*
  1308. * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
  1309. * sense key with us all the way through
  1310. */
  1311. if (!blk_pc_request(req))
  1312. req->errors = 0;
  1313. if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
  1314. printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
  1315. req->rq_disk ? req->rq_disk->disk_name : "?",
  1316. (unsigned long long)req->sector);
  1317. }
  1318. if (blk_fs_request(req) && req->rq_disk) {
  1319. const int rw = rq_data_dir(req);
  1320. struct hd_struct *part;
  1321. int cpu;
  1322. cpu = part_stat_lock();
  1323. part = disk_map_sector_rcu(req->rq_disk, req->sector);
  1324. part_stat_add(cpu, part, sectors[rw], nr_bytes >> 9);
  1325. part_stat_unlock();
  1326. }
  1327. total_bytes = bio_nbytes = 0;
  1328. while ((bio = req->bio) != NULL) {
  1329. int nbytes;
  1330. /*
  1331. * For an empty barrier request, the low level driver must
  1332. * store a potential error location in ->sector. We pass
  1333. * that back up in ->bi_sector.
  1334. */
  1335. if (blk_empty_barrier(req))
  1336. bio->bi_sector = req->sector;
  1337. if (nr_bytes >= bio->bi_size) {
  1338. req->bio = bio->bi_next;
  1339. nbytes = bio->bi_size;
  1340. req_bio_endio(req, bio, nbytes, error);
  1341. next_idx = 0;
  1342. bio_nbytes = 0;
  1343. } else {
  1344. int idx = bio->bi_idx + next_idx;
  1345. if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
  1346. blk_dump_rq_flags(req, "__end_that");
  1347. printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
  1348. __func__, bio->bi_idx, bio->bi_vcnt);
  1349. break;
  1350. }
  1351. nbytes = bio_iovec_idx(bio, idx)->bv_len;
  1352. BIO_BUG_ON(nbytes > bio->bi_size);
  1353. /*
  1354. * not a complete bvec done
  1355. */
  1356. if (unlikely(nbytes > nr_bytes)) {
  1357. bio_nbytes += nr_bytes;
  1358. total_bytes += nr_bytes;
  1359. break;
  1360. }
  1361. /*
  1362. * advance to the next vector
  1363. */
  1364. next_idx++;
  1365. bio_nbytes += nbytes;
  1366. }
  1367. total_bytes += nbytes;
  1368. nr_bytes -= nbytes;
  1369. bio = req->bio;
  1370. if (bio) {
  1371. /*
  1372. * end more in this run, or just return 'not-done'
  1373. */
  1374. if (unlikely(nr_bytes <= 0))
  1375. break;
  1376. }
  1377. }
  1378. /*
  1379. * completely done
  1380. */
  1381. if (!req->bio)
  1382. return 0;
  1383. /*
  1384. * if the request wasn't completed, update state
  1385. */
  1386. if (bio_nbytes) {
  1387. req_bio_endio(req, bio, bio_nbytes, error);
  1388. bio->bi_idx += next_idx;
  1389. bio_iovec(bio)->bv_offset += nr_bytes;
  1390. bio_iovec(bio)->bv_len -= nr_bytes;
  1391. }
  1392. blk_recalc_rq_sectors(req, total_bytes >> 9);
  1393. blk_recalc_rq_segments(req);
  1394. return 1;
  1395. }
  1396. /*
  1397. * queue lock must be held
  1398. */
  1399. static void end_that_request_last(struct request *req, int error)
  1400. {
  1401. struct gendisk *disk = req->rq_disk;
  1402. if (blk_rq_tagged(req))
  1403. blk_queue_end_tag(req->q, req);
  1404. if (blk_queued_rq(req))
  1405. blkdev_dequeue_request(req);
  1406. if (unlikely(laptop_mode) && blk_fs_request(req))
  1407. laptop_io_completion();
  1408. /*
  1409. * Account IO completion. bar_rq isn't accounted as a normal
  1410. * IO on queueing nor completion. Accounting the containing
  1411. * request is enough.
  1412. */
  1413. if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
  1414. unsigned long duration = jiffies - req->start_time;
  1415. const int rw = rq_data_dir(req);
  1416. struct hd_struct *part;
  1417. int cpu;
  1418. cpu = part_stat_lock();
  1419. part = disk_map_sector_rcu(disk, req->sector);
  1420. part_stat_inc(cpu, part, ios[rw]);
  1421. part_stat_add(cpu, part, ticks[rw], duration);
  1422. part_round_stats(cpu, part);
  1423. part_dec_in_flight(part);
  1424. part_stat_unlock();
  1425. }
  1426. if (req->end_io)
  1427. req->end_io(req, error);
  1428. else {
  1429. if (blk_bidi_rq(req))
  1430. __blk_put_request(req->next_rq->q, req->next_rq);
  1431. __blk_put_request(req->q, req);
  1432. }
  1433. }
  1434. static inline void __end_request(struct request *rq, int uptodate,
  1435. unsigned int nr_bytes)
  1436. {
  1437. int error = 0;
  1438. if (uptodate <= 0)
  1439. error = uptodate ? uptodate : -EIO;
  1440. __blk_end_request(rq, error, nr_bytes);
  1441. }
  1442. /**
  1443. * blk_rq_bytes - Returns bytes left to complete in the entire request
  1444. * @rq: the request being processed
  1445. **/
  1446. unsigned int blk_rq_bytes(struct request *rq)
  1447. {
  1448. if (blk_fs_request(rq))
  1449. return rq->hard_nr_sectors << 9;
  1450. return rq->data_len;
  1451. }
  1452. EXPORT_SYMBOL_GPL(blk_rq_bytes);
  1453. /**
  1454. * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
  1455. * @rq: the request being processed
  1456. **/
  1457. unsigned int blk_rq_cur_bytes(struct request *rq)
  1458. {
  1459. if (blk_fs_request(rq))
  1460. return rq->current_nr_sectors << 9;
  1461. if (rq->bio)
  1462. return rq->bio->bi_size;
  1463. return rq->data_len;
  1464. }
  1465. EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
  1466. /**
  1467. * end_queued_request - end all I/O on a queued request
  1468. * @rq: the request being processed
  1469. * @uptodate: error value or %0/%1 uptodate flag
  1470. *
  1471. * Description:
  1472. * Ends all I/O on a request, and removes it from the block layer queues.
  1473. * Not suitable for normal I/O completion, unless the driver still has
  1474. * the request attached to the block layer.
  1475. *
  1476. **/
  1477. void end_queued_request(struct request *rq, int uptodate)
  1478. {
  1479. __end_request(rq, uptodate, blk_rq_bytes(rq));
  1480. }
  1481. EXPORT_SYMBOL(end_queued_request);
  1482. /**
  1483. * end_dequeued_request - end all I/O on a dequeued request
  1484. * @rq: the request being processed
  1485. * @uptodate: error value or %0/%1 uptodate flag
  1486. *
  1487. * Description:
  1488. * Ends all I/O on a request. The request must already have been
  1489. * dequeued using blkdev_dequeue_request(), as is normally the case
  1490. * for most drivers.
  1491. *
  1492. **/
  1493. void end_dequeued_request(struct request *rq, int uptodate)
  1494. {
  1495. __end_request(rq, uptodate, blk_rq_bytes(rq));
  1496. }
  1497. EXPORT_SYMBOL(end_dequeued_request);
  1498. /**
  1499. * end_request - end I/O on the current segment of the request
  1500. * @req: the request being processed
  1501. * @uptodate: error value or %0/%1 uptodate flag
  1502. *
  1503. * Description:
  1504. * Ends I/O on the current segment of a request. If that is the only
  1505. * remaining segment, the request is also completed and freed.
  1506. *
  1507. * This is a remnant of how older block drivers handled I/O completions.
  1508. * Modern drivers typically end I/O on the full request in one go, unless
  1509. * they have a residual value to account for. For that case this function
  1510. * isn't really useful, unless the residual just happens to be the
  1511. * full current segment. In other words, don't use this function in new
  1512. * code. Use blk_end_request() or __blk_end_request() to end partial parts
  1513. * of a request, or end_dequeued_request() and end_queued_request() to
  1514. * completely end IO on a dequeued/queued request.
  1515. *
  1516. **/
  1517. void end_request(struct request *req, int uptodate)
  1518. {
  1519. __end_request(req, uptodate, req->hard_cur_sectors << 9);
  1520. }
  1521. EXPORT_SYMBOL(end_request);
  1522. /**
  1523. * blk_end_io - Generic end_io function to complete a request.
  1524. * @rq: the request being processed
  1525. * @error: %0 for success, < %0 for error
  1526. * @nr_bytes: number of bytes to complete @rq
  1527. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1528. * @drv_callback: function called between completion of bios in the request
  1529. * and completion of the request.
  1530. * If the callback returns non %0, this helper returns without
  1531. * completion of the request.
  1532. *
  1533. * Description:
  1534. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1535. * If @rq has leftover, sets it up for the next range of segments.
  1536. *
  1537. * Return:
  1538. * %0 - we are done with this request
  1539. * %1 - this request is not freed yet, it still has pending buffers.
  1540. **/
  1541. static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
  1542. unsigned int bidi_bytes,
  1543. int (drv_callback)(struct request *))
  1544. {
  1545. struct request_queue *q = rq->q;
  1546. unsigned long flags = 0UL;
  1547. if (rq->bio) {
  1548. if (__end_that_request_first(rq, error, nr_bytes))
  1549. return 1;
  1550. /* Bidi request must be completed as a whole */
  1551. if (blk_bidi_rq(rq) &&
  1552. __end_that_request_first(rq->next_rq, error, bidi_bytes))
  1553. return 1;
  1554. }
  1555. /* Special feature for tricky drivers */
  1556. if (drv_callback && drv_callback(rq))
  1557. return 1;
  1558. add_disk_randomness(rq->rq_disk);
  1559. spin_lock_irqsave(q->queue_lock, flags);
  1560. end_that_request_last(rq, error);
  1561. spin_unlock_irqrestore(q->queue_lock, flags);
  1562. return 0;
  1563. }
  1564. /**
  1565. * blk_end_request - Helper function for drivers to complete the request.
  1566. * @rq: the request being processed
  1567. * @error: %0 for success, < %0 for error
  1568. * @nr_bytes: number of bytes to complete
  1569. *
  1570. * Description:
  1571. * Ends I/O on a number of bytes attached to @rq.
  1572. * If @rq has leftover, sets it up for the next range of segments.
  1573. *
  1574. * Return:
  1575. * %0 - we are done with this request
  1576. * %1 - still buffers pending for this request
  1577. **/
  1578. int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1579. {
  1580. return blk_end_io(rq, error, nr_bytes, 0, NULL);
  1581. }
  1582. EXPORT_SYMBOL_GPL(blk_end_request);
  1583. /**
  1584. * __blk_end_request - Helper function for drivers to complete the request.
  1585. * @rq: the request being processed
  1586. * @error: %0 for success, < %0 for error
  1587. * @nr_bytes: number of bytes to complete
  1588. *
  1589. * Description:
  1590. * Must be called with queue lock held unlike blk_end_request().
  1591. *
  1592. * Return:
  1593. * %0 - we are done with this request
  1594. * %1 - still buffers pending for this request
  1595. **/
  1596. int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1597. {
  1598. if (rq->bio && __end_that_request_first(rq, error, nr_bytes))
  1599. return 1;
  1600. add_disk_randomness(rq->rq_disk);
  1601. end_that_request_last(rq, error);
  1602. return 0;
  1603. }
  1604. EXPORT_SYMBOL_GPL(__blk_end_request);
  1605. /**
  1606. * blk_end_bidi_request - Helper function for drivers to complete bidi request.
  1607. * @rq: the bidi request being processed
  1608. * @error: %0 for success, < %0 for error
  1609. * @nr_bytes: number of bytes to complete @rq
  1610. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1611. *
  1612. * Description:
  1613. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1614. *
  1615. * Return:
  1616. * %0 - we are done with this request
  1617. * %1 - still buffers pending for this request
  1618. **/
  1619. int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
  1620. unsigned int bidi_bytes)
  1621. {
  1622. return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
  1623. }
  1624. EXPORT_SYMBOL_GPL(blk_end_bidi_request);
  1625. /**
  1626. * blk_end_request_callback - Special helper function for tricky drivers
  1627. * @rq: the request being processed
  1628. * @error: %0 for success, < %0 for error
  1629. * @nr_bytes: number of bytes to complete
  1630. * @drv_callback: function called between completion of bios in the request
  1631. * and completion of the request.
  1632. * If the callback returns non %0, this helper returns without
  1633. * completion of the request.
  1634. *
  1635. * Description:
  1636. * Ends I/O on a number of bytes attached to @rq.
  1637. * If @rq has leftover, sets it up for the next range of segments.
  1638. *
  1639. * This special helper function is used only for existing tricky drivers.
  1640. * (e.g. cdrom_newpc_intr() of ide-cd)
  1641. * This interface will be removed when such drivers are rewritten.
  1642. * Don't use this interface in other places anymore.
  1643. *
  1644. * Return:
  1645. * %0 - we are done with this request
  1646. * %1 - this request is not freed yet.
  1647. * this request still has pending buffers or
  1648. * the driver doesn't want to finish this request yet.
  1649. **/
  1650. int blk_end_request_callback(struct request *rq, int error,
  1651. unsigned int nr_bytes,
  1652. int (drv_callback)(struct request *))
  1653. {
  1654. return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
  1655. }
  1656. EXPORT_SYMBOL_GPL(blk_end_request_callback);
  1657. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  1658. struct bio *bio)
  1659. {
  1660. /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
  1661. we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
  1662. rq->cmd_flags |= (bio->bi_rw & 3);
  1663. if (bio_has_data(bio)) {
  1664. rq->nr_phys_segments = bio_phys_segments(q, bio);
  1665. rq->buffer = bio_data(bio);
  1666. }
  1667. rq->current_nr_sectors = bio_cur_sectors(bio);
  1668. rq->hard_cur_sectors = rq->current_nr_sectors;
  1669. rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
  1670. rq->data_len = bio->bi_size;
  1671. rq->bio = rq->biotail = bio;
  1672. if (bio->bi_bdev)
  1673. rq->rq_disk = bio->bi_bdev->bd_disk;
  1674. }
  1675. int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
  1676. {
  1677. return queue_work(kblockd_workqueue, work);
  1678. }
  1679. EXPORT_SYMBOL(kblockd_schedule_work);
  1680. void kblockd_flush_work(struct work_struct *work)
  1681. {
  1682. cancel_work_sync(work);
  1683. }
  1684. EXPORT_SYMBOL(kblockd_flush_work);
  1685. int __init blk_dev_init(void)
  1686. {
  1687. kblockd_workqueue = create_workqueue("kblockd");
  1688. if (!kblockd_workqueue)
  1689. panic("Failed to create kblockd\n");
  1690. request_cachep = kmem_cache_create("blkdev_requests",
  1691. sizeof(struct request), 0, SLAB_PANIC, NULL);
  1692. blk_requestq_cachep = kmem_cache_create("blkdev_queue",
  1693. sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
  1694. return 0;
  1695. }