blk-core.c 53 KB

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