blk-core.c 57 KB

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