blk-core.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  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. #define CREATE_TRACE_POINTS
  31. #include <trace/events/block.h>
  32. #include "blk.h"
  33. EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
  34. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
  35. static int __make_request(struct request_queue *q, struct bio *bio);
  36. /*
  37. * For the allocated request tables
  38. */
  39. static struct kmem_cache *request_cachep;
  40. /*
  41. * For queue allocation
  42. */
  43. struct kmem_cache *blk_requestq_cachep;
  44. /*
  45. * Controlling structure to kblockd
  46. */
  47. static struct workqueue_struct *kblockd_workqueue;
  48. static void drive_stat_acct(struct request *rq, int new_io)
  49. {
  50. struct hd_struct *part;
  51. int rw = rq_data_dir(rq);
  52. int cpu;
  53. if (!blk_fs_request(rq) || !blk_do_io_stat(rq))
  54. return;
  55. cpu = part_stat_lock();
  56. part = disk_map_sector_rcu(rq->rq_disk, rq->sector);
  57. if (!new_io)
  58. part_stat_inc(cpu, part, merges[rw]);
  59. else {
  60. part_round_stats(cpu, part);
  61. part_inc_in_flight(part);
  62. }
  63. part_stat_unlock();
  64. }
  65. void blk_queue_congestion_threshold(struct request_queue *q)
  66. {
  67. int nr;
  68. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  69. if (nr > q->nr_requests)
  70. nr = q->nr_requests;
  71. q->nr_congestion_on = nr;
  72. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  73. if (nr < 1)
  74. nr = 1;
  75. q->nr_congestion_off = nr;
  76. }
  77. /**
  78. * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
  79. * @bdev: device
  80. *
  81. * Locates the passed device's request queue and returns the address of its
  82. * backing_dev_info
  83. *
  84. * Will return NULL if the request queue cannot be located.
  85. */
  86. struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
  87. {
  88. struct backing_dev_info *ret = NULL;
  89. struct request_queue *q = bdev_get_queue(bdev);
  90. if (q)
  91. ret = &q->backing_dev_info;
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(blk_get_backing_dev_info);
  95. void blk_rq_init(struct request_queue *q, struct request *rq)
  96. {
  97. memset(rq, 0, sizeof(*rq));
  98. INIT_LIST_HEAD(&rq->queuelist);
  99. INIT_LIST_HEAD(&rq->timeout_list);
  100. rq->cpu = -1;
  101. rq->q = q;
  102. rq->sector = rq->hard_sector = (sector_t) -1;
  103. INIT_HLIST_NODE(&rq->hash);
  104. RB_CLEAR_NODE(&rq->rb_node);
  105. rq->cmd = rq->__cmd;
  106. rq->cmd_len = BLK_MAX_CDB;
  107. rq->tag = -1;
  108. rq->ref_count = 1;
  109. }
  110. EXPORT_SYMBOL(blk_rq_init);
  111. static void req_bio_endio(struct request *rq, struct bio *bio,
  112. unsigned int nbytes, int error)
  113. {
  114. struct request_queue *q = rq->q;
  115. if (&q->bar_rq != rq) {
  116. if (error)
  117. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  118. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  119. error = -EIO;
  120. if (unlikely(nbytes > bio->bi_size)) {
  121. printk(KERN_ERR "%s: want %u bytes done, %u left\n",
  122. __func__, nbytes, bio->bi_size);
  123. nbytes = bio->bi_size;
  124. }
  125. if (unlikely(rq->cmd_flags & REQ_QUIET))
  126. set_bit(BIO_QUIET, &bio->bi_flags);
  127. bio->bi_size -= nbytes;
  128. bio->bi_sector += (nbytes >> 9);
  129. if (bio_integrity(bio))
  130. bio_integrity_advance(bio, nbytes);
  131. if (bio->bi_size == 0)
  132. bio_endio(bio, error);
  133. } else {
  134. /*
  135. * Okay, this is the barrier request in progress, just
  136. * record the error;
  137. */
  138. if (error && !q->orderr)
  139. q->orderr = error;
  140. }
  141. }
  142. void blk_dump_rq_flags(struct request *rq, char *msg)
  143. {
  144. int bit;
  145. printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
  146. rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
  147. rq->cmd_flags);
  148. printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
  149. (unsigned long long)rq->sector,
  150. rq->nr_sectors,
  151. rq->current_nr_sectors);
  152. printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
  153. rq->bio, rq->biotail,
  154. rq->buffer, rq->data,
  155. rq->data_len);
  156. if (blk_pc_request(rq)) {
  157. printk(KERN_INFO " cdb: ");
  158. for (bit = 0; bit < BLK_MAX_CDB; bit++)
  159. printk("%02x ", rq->cmd[bit]);
  160. printk("\n");
  161. }
  162. }
  163. EXPORT_SYMBOL(blk_dump_rq_flags);
  164. /*
  165. * "plug" the device if there are no outstanding requests: this will
  166. * force the transfer to start only after we have put all the requests
  167. * on the list.
  168. *
  169. * This is called with interrupts off and no requests on the queue and
  170. * with the queue lock held.
  171. */
  172. void blk_plug_device(struct request_queue *q)
  173. {
  174. WARN_ON(!irqs_disabled());
  175. /*
  176. * don't plug a stopped queue, it must be paired with blk_start_queue()
  177. * which will restart the queueing
  178. */
  179. if (blk_queue_stopped(q))
  180. return;
  181. if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
  182. mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
  183. trace_block_plug(q);
  184. }
  185. }
  186. EXPORT_SYMBOL(blk_plug_device);
  187. /**
  188. * blk_plug_device_unlocked - plug a device without queue lock held
  189. * @q: The &struct request_queue to plug
  190. *
  191. * Description:
  192. * Like @blk_plug_device(), but grabs the queue lock and disables
  193. * interrupts.
  194. **/
  195. void blk_plug_device_unlocked(struct request_queue *q)
  196. {
  197. unsigned long flags;
  198. spin_lock_irqsave(q->queue_lock, flags);
  199. blk_plug_device(q);
  200. spin_unlock_irqrestore(q->queue_lock, flags);
  201. }
  202. EXPORT_SYMBOL(blk_plug_device_unlocked);
  203. /*
  204. * remove the queue from the plugged list, if present. called with
  205. * queue lock held and interrupts disabled.
  206. */
  207. int blk_remove_plug(struct request_queue *q)
  208. {
  209. WARN_ON(!irqs_disabled());
  210. if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
  211. return 0;
  212. del_timer(&q->unplug_timer);
  213. return 1;
  214. }
  215. EXPORT_SYMBOL(blk_remove_plug);
  216. /*
  217. * remove the plug and let it rip..
  218. */
  219. void __generic_unplug_device(struct request_queue *q)
  220. {
  221. if (unlikely(blk_queue_stopped(q)))
  222. return;
  223. if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
  224. return;
  225. q->request_fn(q);
  226. }
  227. /**
  228. * generic_unplug_device - fire a request queue
  229. * @q: The &struct request_queue in question
  230. *
  231. * Description:
  232. * Linux uses plugging to build bigger requests queues before letting
  233. * the device have at them. If a queue is plugged, the I/O scheduler
  234. * is still adding and merging requests on the queue. Once the queue
  235. * gets unplugged, the request_fn defined for the queue is invoked and
  236. * transfers started.
  237. **/
  238. void generic_unplug_device(struct request_queue *q)
  239. {
  240. if (blk_queue_plugged(q)) {
  241. spin_lock_irq(q->queue_lock);
  242. __generic_unplug_device(q);
  243. spin_unlock_irq(q->queue_lock);
  244. }
  245. }
  246. EXPORT_SYMBOL(generic_unplug_device);
  247. static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
  248. struct page *page)
  249. {
  250. struct request_queue *q = bdi->unplug_io_data;
  251. blk_unplug(q);
  252. }
  253. void blk_unplug_work(struct work_struct *work)
  254. {
  255. struct request_queue *q =
  256. container_of(work, struct request_queue, unplug_work);
  257. trace_block_unplug_io(q);
  258. q->unplug_fn(q);
  259. }
  260. void blk_unplug_timeout(unsigned long data)
  261. {
  262. struct request_queue *q = (struct request_queue *)data;
  263. trace_block_unplug_timer(q);
  264. kblockd_schedule_work(q, &q->unplug_work);
  265. }
  266. void blk_unplug(struct request_queue *q)
  267. {
  268. /*
  269. * devices don't necessarily have an ->unplug_fn defined
  270. */
  271. if (q->unplug_fn) {
  272. trace_block_unplug_io(q);
  273. q->unplug_fn(q);
  274. }
  275. }
  276. EXPORT_SYMBOL(blk_unplug);
  277. static void blk_invoke_request_fn(struct request_queue *q)
  278. {
  279. if (unlikely(blk_queue_stopped(q)))
  280. return;
  281. /*
  282. * one level of recursion is ok and is much faster than kicking
  283. * the unplug handling
  284. */
  285. if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
  286. q->request_fn(q);
  287. queue_flag_clear(QUEUE_FLAG_REENTER, q);
  288. } else {
  289. queue_flag_set(QUEUE_FLAG_PLUGGED, q);
  290. kblockd_schedule_work(q, &q->unplug_work);
  291. }
  292. }
  293. /**
  294. * blk_start_queue - restart a previously stopped queue
  295. * @q: The &struct request_queue in question
  296. *
  297. * Description:
  298. * blk_start_queue() will clear the stop flag on the queue, and call
  299. * the request_fn for the queue if it was in a stopped state when
  300. * entered. Also see blk_stop_queue(). Queue lock must be held.
  301. **/
  302. void blk_start_queue(struct request_queue *q)
  303. {
  304. WARN_ON(!irqs_disabled());
  305. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  306. blk_invoke_request_fn(q);
  307. }
  308. EXPORT_SYMBOL(blk_start_queue);
  309. /**
  310. * blk_stop_queue - stop a queue
  311. * @q: The &struct request_queue in question
  312. *
  313. * Description:
  314. * The Linux block layer assumes that a block driver will consume all
  315. * entries on the request queue when the request_fn strategy is called.
  316. * Often this will not happen, because of hardware limitations (queue
  317. * depth settings). If a device driver gets a 'queue full' response,
  318. * or if it simply chooses not to queue more I/O at one point, it can
  319. * call this function to prevent the request_fn from being called until
  320. * the driver has signalled it's ready to go again. This happens by calling
  321. * blk_start_queue() to restart queue operations. Queue lock must be held.
  322. **/
  323. void blk_stop_queue(struct request_queue *q)
  324. {
  325. blk_remove_plug(q);
  326. queue_flag_set(QUEUE_FLAG_STOPPED, q);
  327. }
  328. EXPORT_SYMBOL(blk_stop_queue);
  329. /**
  330. * blk_sync_queue - cancel any pending callbacks on a queue
  331. * @q: the queue
  332. *
  333. * Description:
  334. * The block layer may perform asynchronous callback activity
  335. * on a queue, such as calling the unplug function after a timeout.
  336. * A block device may call blk_sync_queue to ensure that any
  337. * such activity is cancelled, thus allowing it to release resources
  338. * that the callbacks might use. The caller must already have made sure
  339. * that its ->make_request_fn will not re-add plugging prior to calling
  340. * this function.
  341. *
  342. */
  343. void blk_sync_queue(struct request_queue *q)
  344. {
  345. del_timer_sync(&q->unplug_timer);
  346. del_timer_sync(&q->timeout);
  347. cancel_work_sync(&q->unplug_work);
  348. }
  349. EXPORT_SYMBOL(blk_sync_queue);
  350. /**
  351. * __blk_run_queue - run a single device queue
  352. * @q: The queue to run
  353. *
  354. * Description:
  355. * See @blk_run_queue. This variant must be called with the queue lock
  356. * held and interrupts disabled.
  357. *
  358. */
  359. void __blk_run_queue(struct request_queue *q)
  360. {
  361. blk_remove_plug(q);
  362. /*
  363. * Only recurse once to avoid overrunning the stack, let the unplug
  364. * handling reinvoke the handler shortly if we already got there.
  365. */
  366. if (!elv_queue_empty(q))
  367. blk_invoke_request_fn(q);
  368. }
  369. EXPORT_SYMBOL(__blk_run_queue);
  370. /**
  371. * blk_run_queue - run a single device queue
  372. * @q: The queue to run
  373. *
  374. * Description:
  375. * Invoke request handling on this queue, if it has pending work to do.
  376. * May be used to restart queueing when a request has completed. Also
  377. * See @blk_start_queueing.
  378. *
  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_start_queueing - initiate dispatch of requests to device
  761. * @q: request queue to kick into gear
  762. *
  763. * This is basically a helper to remove the need to know whether a queue
  764. * is plugged or not if someone just wants to initiate dispatch of requests
  765. * for this queue. Should be used to start queueing on a device outside
  766. * of ->request_fn() context. Also see @blk_run_queue.
  767. *
  768. * The queue lock must be held with interrupts disabled.
  769. */
  770. void blk_start_queueing(struct request_queue *q)
  771. {
  772. if (!blk_queue_plugged(q)) {
  773. if (unlikely(blk_queue_stopped(q)))
  774. return;
  775. q->request_fn(q);
  776. } else
  777. __generic_unplug_device(q);
  778. }
  779. EXPORT_SYMBOL(blk_start_queueing);
  780. /**
  781. * blk_requeue_request - put a request back on queue
  782. * @q: request queue where request should be inserted
  783. * @rq: request to be inserted
  784. *
  785. * Description:
  786. * Drivers often keep queueing requests until the hardware cannot accept
  787. * more, when that condition happens we need to put the request back
  788. * on the queue. Must be called with queue lock held.
  789. */
  790. void blk_requeue_request(struct request_queue *q, struct request *rq)
  791. {
  792. blk_delete_timer(rq);
  793. blk_clear_rq_complete(rq);
  794. trace_block_rq_requeue(q, rq);
  795. if (blk_rq_tagged(rq))
  796. blk_queue_end_tag(q, rq);
  797. elv_requeue_request(q, rq);
  798. }
  799. EXPORT_SYMBOL(blk_requeue_request);
  800. /**
  801. * blk_insert_request - insert a special request into a request queue
  802. * @q: request queue where request should be inserted
  803. * @rq: request to be inserted
  804. * @at_head: insert request at head or tail of queue
  805. * @data: private data
  806. *
  807. * Description:
  808. * Many block devices need to execute commands asynchronously, so they don't
  809. * block the whole kernel from preemption during request execution. This is
  810. * accomplished normally by inserting aritficial requests tagged as
  811. * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
  812. * be scheduled for actual execution by the request queue.
  813. *
  814. * We have the option of inserting the head or the tail of the queue.
  815. * Typically we use the tail for new ioctls and so forth. We use the head
  816. * of the queue for things like a QUEUE_FULL message from a device, or a
  817. * host that is unable to accept a particular command.
  818. */
  819. void blk_insert_request(struct request_queue *q, struct request *rq,
  820. int at_head, void *data)
  821. {
  822. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  823. unsigned long flags;
  824. /*
  825. * tell I/O scheduler that this isn't a regular read/write (ie it
  826. * must not attempt merges on this) and that it acts as a soft
  827. * barrier
  828. */
  829. rq->cmd_type = REQ_TYPE_SPECIAL;
  830. rq->cmd_flags |= REQ_SOFTBARRIER;
  831. rq->special = data;
  832. spin_lock_irqsave(q->queue_lock, flags);
  833. /*
  834. * If command is tagged, release the tag
  835. */
  836. if (blk_rq_tagged(rq))
  837. blk_queue_end_tag(q, rq);
  838. drive_stat_acct(rq, 1);
  839. __elv_add_request(q, rq, where, 0);
  840. blk_start_queueing(q);
  841. spin_unlock_irqrestore(q->queue_lock, flags);
  842. }
  843. EXPORT_SYMBOL(blk_insert_request);
  844. /*
  845. * add-request adds a request to the linked list.
  846. * queue lock is held and interrupts disabled, as we muck with the
  847. * request queue list.
  848. */
  849. static inline void add_request(struct request_queue *q, struct request *req)
  850. {
  851. drive_stat_acct(req, 1);
  852. /*
  853. * elevator indicated where it wants this request to be
  854. * inserted at elevator_merge time
  855. */
  856. __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
  857. }
  858. static void part_round_stats_single(int cpu, struct hd_struct *part,
  859. unsigned long now)
  860. {
  861. if (now == part->stamp)
  862. return;
  863. if (part->in_flight) {
  864. __part_stat_add(cpu, part, time_in_queue,
  865. part->in_flight * (now - part->stamp));
  866. __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
  867. }
  868. part->stamp = now;
  869. }
  870. /**
  871. * part_round_stats() - Round off the performance stats on a struct disk_stats.
  872. * @cpu: cpu number for stats access
  873. * @part: target partition
  874. *
  875. * The average IO queue length and utilisation statistics are maintained
  876. * by observing the current state of the queue length and the amount of
  877. * time it has been in this state for.
  878. *
  879. * Normally, that accounting is done on IO completion, but that can result
  880. * in more than a second's worth of IO being accounted for within any one
  881. * second, leading to >100% utilisation. To deal with that, we call this
  882. * function to do a round-off before returning the results when reading
  883. * /proc/diskstats. This accounts immediately for all queue usage up to
  884. * the current jiffies and restarts the counters again.
  885. */
  886. void part_round_stats(int cpu, struct hd_struct *part)
  887. {
  888. unsigned long now = jiffies;
  889. if (part->partno)
  890. part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
  891. part_round_stats_single(cpu, part, now);
  892. }
  893. EXPORT_SYMBOL_GPL(part_round_stats);
  894. /*
  895. * queue lock must be held
  896. */
  897. void __blk_put_request(struct request_queue *q, struct request *req)
  898. {
  899. if (unlikely(!q))
  900. return;
  901. if (unlikely(--req->ref_count))
  902. return;
  903. elv_completed_request(q, req);
  904. /* this is a bio leak */
  905. WARN_ON(req->bio != NULL);
  906. /*
  907. * Request may not have originated from ll_rw_blk. if not,
  908. * it didn't come out of our reserved rq pools
  909. */
  910. if (req->cmd_flags & REQ_ALLOCED) {
  911. int is_sync = rq_is_sync(req) != 0;
  912. int priv = req->cmd_flags & REQ_ELVPRIV;
  913. BUG_ON(!list_empty(&req->queuelist));
  914. BUG_ON(!hlist_unhashed(&req->hash));
  915. blk_free_request(q, req);
  916. freed_request(q, is_sync, priv);
  917. }
  918. }
  919. EXPORT_SYMBOL_GPL(__blk_put_request);
  920. void blk_put_request(struct request *req)
  921. {
  922. unsigned long flags;
  923. struct request_queue *q = req->q;
  924. spin_lock_irqsave(q->queue_lock, flags);
  925. __blk_put_request(q, req);
  926. spin_unlock_irqrestore(q->queue_lock, flags);
  927. }
  928. EXPORT_SYMBOL(blk_put_request);
  929. void init_request_from_bio(struct request *req, struct bio *bio)
  930. {
  931. req->cpu = bio->bi_comp_cpu;
  932. req->cmd_type = REQ_TYPE_FS;
  933. /*
  934. * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
  935. */
  936. if (bio_rw_ahead(bio))
  937. req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  938. REQ_FAILFAST_DRIVER);
  939. if (bio_failfast_dev(bio))
  940. req->cmd_flags |= REQ_FAILFAST_DEV;
  941. if (bio_failfast_transport(bio))
  942. req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  943. if (bio_failfast_driver(bio))
  944. req->cmd_flags |= REQ_FAILFAST_DRIVER;
  945. /*
  946. * REQ_BARRIER implies no merging, but lets make it explicit
  947. */
  948. if (unlikely(bio_discard(bio))) {
  949. req->cmd_flags |= REQ_DISCARD;
  950. if (bio_barrier(bio))
  951. req->cmd_flags |= REQ_SOFTBARRIER;
  952. req->q->prepare_discard_fn(req->q, req);
  953. } else if (unlikely(bio_barrier(bio)))
  954. req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
  955. if (bio_sync(bio))
  956. req->cmd_flags |= REQ_RW_SYNC;
  957. if (bio_rw_meta(bio))
  958. req->cmd_flags |= REQ_RW_META;
  959. if (bio_noidle(bio))
  960. req->cmd_flags |= REQ_NOIDLE;
  961. req->errors = 0;
  962. req->hard_sector = req->sector = bio->bi_sector;
  963. req->ioprio = bio_prio(bio);
  964. req->start_time = jiffies;
  965. blk_rq_bio_prep(req->q, req, bio);
  966. }
  967. /*
  968. * Only disabling plugging for non-rotational devices if it does tagging
  969. * as well, otherwise we do need the proper merging
  970. */
  971. static inline bool queue_should_plug(struct request_queue *q)
  972. {
  973. return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
  974. }
  975. static int __make_request(struct request_queue *q, struct bio *bio)
  976. {
  977. struct request *req;
  978. int el_ret, nr_sectors;
  979. const unsigned short prio = bio_prio(bio);
  980. const int sync = bio_sync(bio);
  981. const int unplug = bio_unplug(bio);
  982. int rw_flags;
  983. nr_sectors = bio_sectors(bio);
  984. /*
  985. * low level driver can indicate that it wants pages above a
  986. * certain limit bounced to low memory (ie for highmem, or even
  987. * ISA dma in theory)
  988. */
  989. blk_queue_bounce(q, &bio);
  990. spin_lock_irq(q->queue_lock);
  991. if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
  992. goto get_rq;
  993. el_ret = elv_merge(q, &req, bio);
  994. switch (el_ret) {
  995. case ELEVATOR_BACK_MERGE:
  996. BUG_ON(!rq_mergeable(req));
  997. if (!ll_back_merge_fn(q, req, bio))
  998. break;
  999. trace_block_bio_backmerge(q, bio);
  1000. req->biotail->bi_next = bio;
  1001. req->biotail = bio;
  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_back_merge(q, req))
  1008. elv_merged_request(q, req, el_ret);
  1009. goto out;
  1010. case ELEVATOR_FRONT_MERGE:
  1011. BUG_ON(!rq_mergeable(req));
  1012. if (!ll_front_merge_fn(q, req, bio))
  1013. break;
  1014. trace_block_bio_frontmerge(q, bio);
  1015. bio->bi_next = req->bio;
  1016. req->bio = bio;
  1017. /*
  1018. * may not be valid. if the low level driver said
  1019. * it didn't need a bounce buffer then it better
  1020. * not touch req->buffer either...
  1021. */
  1022. req->buffer = bio_data(bio);
  1023. req->current_nr_sectors = bio_cur_sectors(bio);
  1024. req->hard_cur_sectors = req->current_nr_sectors;
  1025. req->sector = req->hard_sector = bio->bi_sector;
  1026. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  1027. req->ioprio = ioprio_best(req->ioprio, prio);
  1028. if (!blk_rq_cpu_valid(req))
  1029. req->cpu = bio->bi_comp_cpu;
  1030. drive_stat_acct(req, 0);
  1031. if (!attempt_front_merge(q, req))
  1032. elv_merged_request(q, req, el_ret);
  1033. goto out;
  1034. /* ELV_NO_MERGE: elevator says don't/can't merge. */
  1035. default:
  1036. ;
  1037. }
  1038. get_rq:
  1039. /*
  1040. * This sync check and mask will be re-done in init_request_from_bio(),
  1041. * but we need to set it earlier to expose the sync flag to the
  1042. * rq allocator and io schedulers.
  1043. */
  1044. rw_flags = bio_data_dir(bio);
  1045. if (sync)
  1046. rw_flags |= REQ_RW_SYNC;
  1047. /*
  1048. * Grab a free request. This is might sleep but can not fail.
  1049. * Returns with the queue unlocked.
  1050. */
  1051. req = get_request_wait(q, rw_flags, bio);
  1052. /*
  1053. * After dropping the lock and possibly sleeping here, our request
  1054. * may now be mergeable after it had proven unmergeable (above).
  1055. * We don't worry about that case for efficiency. It won't happen
  1056. * often, and the elevators are able to handle it.
  1057. */
  1058. init_request_from_bio(req, bio);
  1059. spin_lock_irq(q->queue_lock);
  1060. if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
  1061. bio_flagged(bio, BIO_CPU_AFFINE))
  1062. req->cpu = blk_cpu_to_group(smp_processor_id());
  1063. if (queue_should_plug(q) && elv_queue_empty(q))
  1064. blk_plug_device(q);
  1065. add_request(q, req);
  1066. out:
  1067. if (unplug || !queue_should_plug(q))
  1068. __generic_unplug_device(q);
  1069. spin_unlock_irq(q->queue_lock);
  1070. return 0;
  1071. }
  1072. /*
  1073. * If bio->bi_dev is a partition, remap the location
  1074. */
  1075. static inline void blk_partition_remap(struct bio *bio)
  1076. {
  1077. struct block_device *bdev = bio->bi_bdev;
  1078. if (bio_sectors(bio) && bdev != bdev->bd_contains) {
  1079. struct hd_struct *p = bdev->bd_part;
  1080. bio->bi_sector += p->start_sect;
  1081. bio->bi_bdev = bdev->bd_contains;
  1082. trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
  1083. bdev->bd_dev,
  1084. bio->bi_sector - p->start_sect);
  1085. }
  1086. }
  1087. static void handle_bad_sector(struct bio *bio)
  1088. {
  1089. char b[BDEVNAME_SIZE];
  1090. printk(KERN_INFO "attempt to access beyond end of device\n");
  1091. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  1092. bdevname(bio->bi_bdev, b),
  1093. bio->bi_rw,
  1094. (unsigned long long)bio->bi_sector + bio_sectors(bio),
  1095. (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
  1096. set_bit(BIO_EOF, &bio->bi_flags);
  1097. }
  1098. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1099. static DECLARE_FAULT_ATTR(fail_make_request);
  1100. static int __init setup_fail_make_request(char *str)
  1101. {
  1102. return setup_fault_attr(&fail_make_request, str);
  1103. }
  1104. __setup("fail_make_request=", setup_fail_make_request);
  1105. static int should_fail_request(struct bio *bio)
  1106. {
  1107. struct hd_struct *part = bio->bi_bdev->bd_part;
  1108. if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
  1109. return should_fail(&fail_make_request, bio->bi_size);
  1110. return 0;
  1111. }
  1112. static int __init fail_make_request_debugfs(void)
  1113. {
  1114. return init_fault_attr_dentries(&fail_make_request,
  1115. "fail_make_request");
  1116. }
  1117. late_initcall(fail_make_request_debugfs);
  1118. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1119. static inline int should_fail_request(struct bio *bio)
  1120. {
  1121. return 0;
  1122. }
  1123. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1124. /*
  1125. * Check whether this bio extends beyond the end of the device.
  1126. */
  1127. static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
  1128. {
  1129. sector_t maxsector;
  1130. if (!nr_sectors)
  1131. return 0;
  1132. /* Test device or partition size, when known. */
  1133. maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
  1134. if (maxsector) {
  1135. sector_t sector = bio->bi_sector;
  1136. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  1137. /*
  1138. * This may well happen - the kernel calls bread()
  1139. * without checking the size of the device, e.g., when
  1140. * mounting a device.
  1141. */
  1142. handle_bad_sector(bio);
  1143. return 1;
  1144. }
  1145. }
  1146. return 0;
  1147. }
  1148. /**
  1149. * generic_make_request - hand a buffer to its device driver for I/O
  1150. * @bio: The bio describing the location in memory and on the device.
  1151. *
  1152. * generic_make_request() is used to make I/O requests of block
  1153. * devices. It is passed a &struct bio, which describes the I/O that needs
  1154. * to be done.
  1155. *
  1156. * generic_make_request() does not return any status. The
  1157. * success/failure status of the request, along with notification of
  1158. * completion, is delivered asynchronously through the bio->bi_end_io
  1159. * function described (one day) else where.
  1160. *
  1161. * The caller of generic_make_request must make sure that bi_io_vec
  1162. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  1163. * set to describe the device address, and the
  1164. * bi_end_io and optionally bi_private are set to describe how
  1165. * completion notification should be signaled.
  1166. *
  1167. * generic_make_request and the drivers it calls may use bi_next if this
  1168. * bio happens to be merged with someone else, and may change bi_dev and
  1169. * bi_sector for remaps as it sees fit. So the values of these fields
  1170. * should NOT be depended on after the call to generic_make_request.
  1171. */
  1172. static inline void __generic_make_request(struct bio *bio)
  1173. {
  1174. struct request_queue *q;
  1175. sector_t old_sector;
  1176. int ret, nr_sectors = bio_sectors(bio);
  1177. dev_t old_dev;
  1178. int err = -EIO;
  1179. might_sleep();
  1180. if (bio_check_eod(bio, nr_sectors))
  1181. goto end_io;
  1182. /*
  1183. * Resolve the mapping until finished. (drivers are
  1184. * still free to implement/resolve their own stacking
  1185. * by explicitly returning 0)
  1186. *
  1187. * NOTE: we don't repeat the blk_size check for each new device.
  1188. * Stacking drivers are expected to know what they are doing.
  1189. */
  1190. old_sector = -1;
  1191. old_dev = 0;
  1192. do {
  1193. char b[BDEVNAME_SIZE];
  1194. q = bdev_get_queue(bio->bi_bdev);
  1195. if (unlikely(!q)) {
  1196. printk(KERN_ERR
  1197. "generic_make_request: Trying to access "
  1198. "nonexistent block-device %s (%Lu)\n",
  1199. bdevname(bio->bi_bdev, b),
  1200. (long long) bio->bi_sector);
  1201. goto end_io;
  1202. }
  1203. if (unlikely(nr_sectors > q->max_hw_sectors)) {
  1204. printk(KERN_ERR "bio too big device %s (%u > %u)\n",
  1205. bdevname(bio->bi_bdev, b),
  1206. bio_sectors(bio),
  1207. q->max_hw_sectors);
  1208. goto end_io;
  1209. }
  1210. if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
  1211. goto end_io;
  1212. if (should_fail_request(bio))
  1213. goto end_io;
  1214. /*
  1215. * If this device has partitions, remap block n
  1216. * of partition p to block n+start(p) of the disk.
  1217. */
  1218. blk_partition_remap(bio);
  1219. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
  1220. goto end_io;
  1221. if (old_sector != -1)
  1222. trace_block_remap(q, bio, old_dev, old_sector);
  1223. trace_block_bio_queue(q, bio);
  1224. old_sector = bio->bi_sector;
  1225. old_dev = bio->bi_bdev->bd_dev;
  1226. if (bio_check_eod(bio, nr_sectors))
  1227. goto end_io;
  1228. if (bio_discard(bio) && !q->prepare_discard_fn) {
  1229. err = -EOPNOTSUPP;
  1230. goto end_io;
  1231. }
  1232. if (bio_barrier(bio) && bio_has_data(bio) &&
  1233. (q->next_ordered == QUEUE_ORDERED_NONE)) {
  1234. err = -EOPNOTSUPP;
  1235. goto end_io;
  1236. }
  1237. ret = q->make_request_fn(q, bio);
  1238. } while (ret);
  1239. return;
  1240. end_io:
  1241. bio_endio(bio, err);
  1242. }
  1243. /*
  1244. * We only want one ->make_request_fn to be active at a time,
  1245. * else stack usage with stacked devices could be a problem.
  1246. * So use current->bio_{list,tail} to keep a list of requests
  1247. * submited by a make_request_fn function.
  1248. * current->bio_tail is also used as a flag to say if
  1249. * generic_make_request is currently active in this task or not.
  1250. * If it is NULL, then no make_request is active. If it is non-NULL,
  1251. * then a make_request is active, and new requests should be added
  1252. * at the tail
  1253. */
  1254. void generic_make_request(struct bio *bio)
  1255. {
  1256. if (current->bio_tail) {
  1257. /* make_request is active */
  1258. *(current->bio_tail) = bio;
  1259. bio->bi_next = NULL;
  1260. current->bio_tail = &bio->bi_next;
  1261. return;
  1262. }
  1263. /* following loop may be a bit non-obvious, and so deserves some
  1264. * explanation.
  1265. * Before entering the loop, bio->bi_next is NULL (as all callers
  1266. * ensure that) so we have a list with a single bio.
  1267. * We pretend that we have just taken it off a longer list, so
  1268. * we assign bio_list to the next (which is NULL) and bio_tail
  1269. * to &bio_list, thus initialising the bio_list of new bios to be
  1270. * added. __generic_make_request may indeed add some more bios
  1271. * through a recursive call to generic_make_request. If it
  1272. * did, we find a non-NULL value in bio_list and re-enter the loop
  1273. * from the top. In this case we really did just take the bio
  1274. * of the top of the list (no pretending) and so fixup bio_list and
  1275. * bio_tail or bi_next, and call into __generic_make_request again.
  1276. *
  1277. * The loop was structured like this to make only one call to
  1278. * __generic_make_request (which is important as it is large and
  1279. * inlined) and to keep the structure simple.
  1280. */
  1281. BUG_ON(bio->bi_next);
  1282. do {
  1283. current->bio_list = bio->bi_next;
  1284. if (bio->bi_next == NULL)
  1285. current->bio_tail = &current->bio_list;
  1286. else
  1287. bio->bi_next = NULL;
  1288. __generic_make_request(bio);
  1289. bio = current->bio_list;
  1290. } while (bio);
  1291. current->bio_tail = NULL; /* deactivate */
  1292. }
  1293. EXPORT_SYMBOL(generic_make_request);
  1294. /**
  1295. * submit_bio - submit a bio to the block device layer for I/O
  1296. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  1297. * @bio: The &struct bio which describes the I/O
  1298. *
  1299. * submit_bio() is very similar in purpose to generic_make_request(), and
  1300. * uses that function to do most of the work. Both are fairly rough
  1301. * interfaces; @bio must be presetup and ready for I/O.
  1302. *
  1303. */
  1304. void submit_bio(int rw, struct bio *bio)
  1305. {
  1306. int count = bio_sectors(bio);
  1307. bio->bi_rw |= rw;
  1308. /*
  1309. * If it's a regular read/write or a barrier with data attached,
  1310. * go through the normal accounting stuff before submission.
  1311. */
  1312. if (bio_has_data(bio)) {
  1313. if (rw & WRITE) {
  1314. count_vm_events(PGPGOUT, count);
  1315. } else {
  1316. task_io_account_read(bio->bi_size);
  1317. count_vm_events(PGPGIN, count);
  1318. }
  1319. if (unlikely(block_dump)) {
  1320. char b[BDEVNAME_SIZE];
  1321. printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
  1322. current->comm, task_pid_nr(current),
  1323. (rw & WRITE) ? "WRITE" : "READ",
  1324. (unsigned long long)bio->bi_sector,
  1325. bdevname(bio->bi_bdev, b));
  1326. }
  1327. }
  1328. generic_make_request(bio);
  1329. }
  1330. EXPORT_SYMBOL(submit_bio);
  1331. /**
  1332. * blk_rq_check_limits - Helper function to check a request for the queue limit
  1333. * @q: the queue
  1334. * @rq: the request being checked
  1335. *
  1336. * Description:
  1337. * @rq may have been made based on weaker limitations of upper-level queues
  1338. * in request stacking drivers, and it may violate the limitation of @q.
  1339. * Since the block layer and the underlying device driver trust @rq
  1340. * after it is inserted to @q, it should be checked against @q before
  1341. * the insertion using this generic function.
  1342. *
  1343. * This function should also be useful for request stacking drivers
  1344. * in some cases below, so export this fuction.
  1345. * Request stacking drivers like request-based dm may change the queue
  1346. * limits while requests are in the queue (e.g. dm's table swapping).
  1347. * Such request stacking drivers should check those requests agaist
  1348. * the new queue limits again when they dispatch those requests,
  1349. * although such checkings are also done against the old queue limits
  1350. * when submitting requests.
  1351. */
  1352. int blk_rq_check_limits(struct request_queue *q, struct request *rq)
  1353. {
  1354. if (rq->nr_sectors > q->max_sectors ||
  1355. rq->data_len > q->max_hw_sectors << 9) {
  1356. printk(KERN_ERR "%s: over max size limit.\n", __func__);
  1357. return -EIO;
  1358. }
  1359. /*
  1360. * queue's settings related to segment counting like q->bounce_pfn
  1361. * may differ from that of other stacking queues.
  1362. * Recalculate it to check the request correctly on this queue's
  1363. * limitation.
  1364. */
  1365. blk_recalc_rq_segments(rq);
  1366. if (rq->nr_phys_segments > q->max_phys_segments ||
  1367. rq->nr_phys_segments > q->max_hw_segments) {
  1368. printk(KERN_ERR "%s: over max segments limit.\n", __func__);
  1369. return -EIO;
  1370. }
  1371. return 0;
  1372. }
  1373. EXPORT_SYMBOL_GPL(blk_rq_check_limits);
  1374. /**
  1375. * blk_insert_cloned_request - Helper for stacking drivers to submit a request
  1376. * @q: the queue to submit the request
  1377. * @rq: the request being queued
  1378. */
  1379. int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
  1380. {
  1381. unsigned long flags;
  1382. if (blk_rq_check_limits(q, rq))
  1383. return -EIO;
  1384. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1385. if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
  1386. should_fail(&fail_make_request, blk_rq_bytes(rq)))
  1387. return -EIO;
  1388. #endif
  1389. spin_lock_irqsave(q->queue_lock, flags);
  1390. /*
  1391. * Submitting request must be dequeued before calling this function
  1392. * because it will be linked to another request_queue
  1393. */
  1394. BUG_ON(blk_queued_rq(rq));
  1395. drive_stat_acct(rq, 1);
  1396. __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
  1397. spin_unlock_irqrestore(q->queue_lock, flags);
  1398. return 0;
  1399. }
  1400. EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
  1401. /**
  1402. * blkdev_dequeue_request - dequeue request and start timeout timer
  1403. * @req: request to dequeue
  1404. *
  1405. * Dequeue @req and start timeout timer on it. This hands off the
  1406. * request to the driver.
  1407. *
  1408. * Block internal functions which don't want to start timer should
  1409. * call elv_dequeue_request().
  1410. */
  1411. void blkdev_dequeue_request(struct request *req)
  1412. {
  1413. elv_dequeue_request(req->q, req);
  1414. /*
  1415. * We are now handing the request to the hardware, add the
  1416. * timeout handler.
  1417. */
  1418. blk_add_timer(req);
  1419. }
  1420. EXPORT_SYMBOL(blkdev_dequeue_request);
  1421. static void blk_account_io_completion(struct request *req, unsigned int bytes)
  1422. {
  1423. if (!blk_do_io_stat(req))
  1424. return;
  1425. if (blk_fs_request(req)) {
  1426. const int rw = rq_data_dir(req);
  1427. struct hd_struct *part;
  1428. int cpu;
  1429. cpu = part_stat_lock();
  1430. part = disk_map_sector_rcu(req->rq_disk, req->sector);
  1431. part_stat_add(cpu, part, sectors[rw], bytes >> 9);
  1432. part_stat_unlock();
  1433. }
  1434. }
  1435. static void blk_account_io_done(struct request *req)
  1436. {
  1437. if (!blk_do_io_stat(req))
  1438. return;
  1439. /*
  1440. * Account IO completion. bar_rq isn't accounted as a normal
  1441. * IO on queueing nor completion. Accounting the containing
  1442. * request is enough.
  1443. */
  1444. if (blk_fs_request(req) && req != &req->q->bar_rq) {
  1445. unsigned long duration = jiffies - req->start_time;
  1446. const int rw = rq_data_dir(req);
  1447. struct hd_struct *part;
  1448. int cpu;
  1449. cpu = part_stat_lock();
  1450. part = disk_map_sector_rcu(req->rq_disk, req->sector);
  1451. part_stat_inc(cpu, part, ios[rw]);
  1452. part_stat_add(cpu, part, ticks[rw], duration);
  1453. part_round_stats(cpu, part);
  1454. part_dec_in_flight(part);
  1455. part_stat_unlock();
  1456. }
  1457. }
  1458. /**
  1459. * __end_that_request_first - end I/O on a request
  1460. * @req: the request being processed
  1461. * @error: %0 for success, < %0 for error
  1462. * @nr_bytes: number of bytes to complete
  1463. *
  1464. * Description:
  1465. * Ends I/O on a number of bytes attached to @req, and sets it up
  1466. * for the next range of segments (if any) in the cluster.
  1467. *
  1468. * Return:
  1469. * %0 - we are done with this request, call end_that_request_last()
  1470. * %1 - still buffers pending for this request
  1471. **/
  1472. static int __end_that_request_first(struct request *req, int error,
  1473. int nr_bytes)
  1474. {
  1475. int total_bytes, bio_nbytes, next_idx = 0;
  1476. struct bio *bio;
  1477. trace_block_rq_complete(req->q, req);
  1478. /*
  1479. * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
  1480. * sense key with us all the way through
  1481. */
  1482. if (!blk_pc_request(req))
  1483. req->errors = 0;
  1484. if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
  1485. printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
  1486. req->rq_disk ? req->rq_disk->disk_name : "?",
  1487. (unsigned long long)req->sector);
  1488. }
  1489. blk_account_io_completion(req, nr_bytes);
  1490. total_bytes = bio_nbytes = 0;
  1491. while ((bio = req->bio) != NULL) {
  1492. int nbytes;
  1493. if (nr_bytes >= bio->bi_size) {
  1494. req->bio = bio->bi_next;
  1495. nbytes = bio->bi_size;
  1496. req_bio_endio(req, bio, nbytes, error);
  1497. next_idx = 0;
  1498. bio_nbytes = 0;
  1499. } else {
  1500. int idx = bio->bi_idx + next_idx;
  1501. if (unlikely(idx >= bio->bi_vcnt)) {
  1502. blk_dump_rq_flags(req, "__end_that");
  1503. printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
  1504. __func__, idx, bio->bi_vcnt);
  1505. break;
  1506. }
  1507. nbytes = bio_iovec_idx(bio, idx)->bv_len;
  1508. BIO_BUG_ON(nbytes > bio->bi_size);
  1509. /*
  1510. * not a complete bvec done
  1511. */
  1512. if (unlikely(nbytes > nr_bytes)) {
  1513. bio_nbytes += nr_bytes;
  1514. total_bytes += nr_bytes;
  1515. break;
  1516. }
  1517. /*
  1518. * advance to the next vector
  1519. */
  1520. next_idx++;
  1521. bio_nbytes += nbytes;
  1522. }
  1523. total_bytes += nbytes;
  1524. nr_bytes -= nbytes;
  1525. bio = req->bio;
  1526. if (bio) {
  1527. /*
  1528. * end more in this run, or just return 'not-done'
  1529. */
  1530. if (unlikely(nr_bytes <= 0))
  1531. break;
  1532. }
  1533. }
  1534. /*
  1535. * completely done
  1536. */
  1537. if (!req->bio)
  1538. return 0;
  1539. /*
  1540. * if the request wasn't completed, update state
  1541. */
  1542. if (bio_nbytes) {
  1543. req_bio_endio(req, bio, bio_nbytes, error);
  1544. bio->bi_idx += next_idx;
  1545. bio_iovec(bio)->bv_offset += nr_bytes;
  1546. bio_iovec(bio)->bv_len -= nr_bytes;
  1547. }
  1548. blk_recalc_rq_sectors(req, total_bytes >> 9);
  1549. blk_recalc_rq_segments(req);
  1550. return 1;
  1551. }
  1552. /*
  1553. * queue lock must be held
  1554. */
  1555. static void end_that_request_last(struct request *req, int error)
  1556. {
  1557. if (blk_rq_tagged(req))
  1558. blk_queue_end_tag(req->q, req);
  1559. if (blk_queued_rq(req))
  1560. elv_dequeue_request(req->q, req);
  1561. if (unlikely(laptop_mode) && blk_fs_request(req))
  1562. laptop_io_completion();
  1563. blk_delete_timer(req);
  1564. blk_account_io_done(req);
  1565. if (req->end_io)
  1566. req->end_io(req, error);
  1567. else {
  1568. if (blk_bidi_rq(req))
  1569. __blk_put_request(req->next_rq->q, req->next_rq);
  1570. __blk_put_request(req->q, req);
  1571. }
  1572. }
  1573. /**
  1574. * blk_rq_bytes - Returns bytes left to complete in the entire request
  1575. * @rq: the request being processed
  1576. **/
  1577. unsigned int blk_rq_bytes(struct request *rq)
  1578. {
  1579. if (blk_fs_request(rq))
  1580. return rq->hard_nr_sectors << 9;
  1581. return rq->data_len;
  1582. }
  1583. EXPORT_SYMBOL_GPL(blk_rq_bytes);
  1584. /**
  1585. * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
  1586. * @rq: the request being processed
  1587. **/
  1588. unsigned int blk_rq_cur_bytes(struct request *rq)
  1589. {
  1590. if (blk_fs_request(rq))
  1591. return rq->current_nr_sectors << 9;
  1592. if (rq->bio)
  1593. return rq->bio->bi_size;
  1594. return rq->data_len;
  1595. }
  1596. EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
  1597. /**
  1598. * end_request - end I/O on the current segment of the request
  1599. * @req: the request being processed
  1600. * @uptodate: error value or %0/%1 uptodate flag
  1601. *
  1602. * Description:
  1603. * Ends I/O on the current segment of a request. If that is the only
  1604. * remaining segment, the request is also completed and freed.
  1605. *
  1606. * This is a remnant of how older block drivers handled I/O completions.
  1607. * Modern drivers typically end I/O on the full request in one go, unless
  1608. * they have a residual value to account for. For that case this function
  1609. * isn't really useful, unless the residual just happens to be the
  1610. * full current segment. In other words, don't use this function in new
  1611. * code. Use blk_end_request() or __blk_end_request() to end a request.
  1612. **/
  1613. void end_request(struct request *req, int uptodate)
  1614. {
  1615. int error = 0;
  1616. if (uptodate <= 0)
  1617. error = uptodate ? uptodate : -EIO;
  1618. __blk_end_request(req, error, req->hard_cur_sectors << 9);
  1619. }
  1620. EXPORT_SYMBOL(end_request);
  1621. static int end_that_request_data(struct request *rq, int error,
  1622. unsigned int nr_bytes, unsigned int bidi_bytes)
  1623. {
  1624. if (rq->bio) {
  1625. if (__end_that_request_first(rq, error, nr_bytes))
  1626. return 1;
  1627. /* Bidi request must be completed as a whole */
  1628. if (blk_bidi_rq(rq) &&
  1629. __end_that_request_first(rq->next_rq, error, bidi_bytes))
  1630. return 1;
  1631. }
  1632. return 0;
  1633. }
  1634. /**
  1635. * blk_end_io - Generic end_io function to complete a request.
  1636. * @rq: the request being processed
  1637. * @error: %0 for success, < %0 for error
  1638. * @nr_bytes: number of bytes to complete @rq
  1639. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1640. * @drv_callback: function called between completion of bios in the request
  1641. * and completion of the request.
  1642. * If the callback returns non %0, this helper returns without
  1643. * completion of the request.
  1644. *
  1645. * Description:
  1646. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1647. * If @rq has leftover, sets it up for the next range of segments.
  1648. *
  1649. * Return:
  1650. * %0 - we are done with this request
  1651. * %1 - this request is not freed yet, it still has pending buffers.
  1652. **/
  1653. static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
  1654. unsigned int bidi_bytes,
  1655. int (drv_callback)(struct request *))
  1656. {
  1657. struct request_queue *q = rq->q;
  1658. unsigned long flags = 0UL;
  1659. if (end_that_request_data(rq, error, nr_bytes, bidi_bytes))
  1660. return 1;
  1661. /* Special feature for tricky drivers */
  1662. if (drv_callback && drv_callback(rq))
  1663. return 1;
  1664. add_disk_randomness(rq->rq_disk);
  1665. spin_lock_irqsave(q->queue_lock, flags);
  1666. end_that_request_last(rq, error);
  1667. spin_unlock_irqrestore(q->queue_lock, flags);
  1668. return 0;
  1669. }
  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. * Ends I/O on a number of bytes attached to @rq.
  1678. * If @rq has leftover, sets it up for the next range of segments.
  1679. *
  1680. * Return:
  1681. * %0 - we are done with this request
  1682. * %1 - still buffers pending for this request
  1683. **/
  1684. int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1685. {
  1686. return blk_end_io(rq, error, nr_bytes, 0, NULL);
  1687. }
  1688. EXPORT_SYMBOL_GPL(blk_end_request);
  1689. /**
  1690. * __blk_end_request - Helper function for drivers to complete the request.
  1691. * @rq: the request being processed
  1692. * @error: %0 for success, < %0 for error
  1693. * @nr_bytes: number of bytes to complete
  1694. *
  1695. * Description:
  1696. * Must be called with queue lock held unlike blk_end_request().
  1697. *
  1698. * Return:
  1699. * %0 - we are done with this request
  1700. * %1 - still buffers pending for this request
  1701. **/
  1702. int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1703. {
  1704. if (rq->bio && __end_that_request_first(rq, error, nr_bytes))
  1705. return 1;
  1706. add_disk_randomness(rq->rq_disk);
  1707. end_that_request_last(rq, error);
  1708. return 0;
  1709. }
  1710. EXPORT_SYMBOL_GPL(__blk_end_request);
  1711. /**
  1712. * blk_end_bidi_request - Helper function for drivers to complete bidi request.
  1713. * @rq: the bidi request being processed
  1714. * @error: %0 for success, < %0 for error
  1715. * @nr_bytes: number of bytes to complete @rq
  1716. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1717. *
  1718. * Description:
  1719. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1720. *
  1721. * Return:
  1722. * %0 - we are done with this request
  1723. * %1 - still buffers pending for this request
  1724. **/
  1725. int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
  1726. unsigned int bidi_bytes)
  1727. {
  1728. return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
  1729. }
  1730. EXPORT_SYMBOL_GPL(blk_end_bidi_request);
  1731. /**
  1732. * blk_update_request - Special helper function for request stacking drivers
  1733. * @rq: the request being processed
  1734. * @error: %0 for success, < %0 for error
  1735. * @nr_bytes: number of bytes to complete @rq
  1736. *
  1737. * Description:
  1738. * Ends I/O on a number of bytes attached to @rq, but doesn't complete
  1739. * the request structure even if @rq doesn't have leftover.
  1740. * If @rq has leftover, sets it up for the next range of segments.
  1741. *
  1742. * This special helper function is only for request stacking drivers
  1743. * (e.g. request-based dm) so that they can handle partial completion.
  1744. * Actual device drivers should use blk_end_request instead.
  1745. */
  1746. void blk_update_request(struct request *rq, int error, unsigned int nr_bytes)
  1747. {
  1748. if (!end_that_request_data(rq, error, nr_bytes, 0)) {
  1749. /*
  1750. * These members are not updated in end_that_request_data()
  1751. * when all bios are completed.
  1752. * Update them so that the request stacking driver can find
  1753. * how many bytes remain in the request later.
  1754. */
  1755. rq->nr_sectors = rq->hard_nr_sectors = 0;
  1756. rq->current_nr_sectors = rq->hard_cur_sectors = 0;
  1757. }
  1758. }
  1759. EXPORT_SYMBOL_GPL(blk_update_request);
  1760. /**
  1761. * blk_end_request_callback - Special helper function for tricky drivers
  1762. * @rq: the request being processed
  1763. * @error: %0 for success, < %0 for error
  1764. * @nr_bytes: number of bytes to complete
  1765. * @drv_callback: function called between completion of bios in the request
  1766. * and completion of the request.
  1767. * If the callback returns non %0, this helper returns without
  1768. * completion of the request.
  1769. *
  1770. * Description:
  1771. * Ends I/O on a number of bytes attached to @rq.
  1772. * If @rq has leftover, sets it up for the next range of segments.
  1773. *
  1774. * This special helper function is used only for existing tricky drivers.
  1775. * (e.g. cdrom_newpc_intr() of ide-cd)
  1776. * This interface will be removed when such drivers are rewritten.
  1777. * Don't use this interface in other places anymore.
  1778. *
  1779. * Return:
  1780. * %0 - we are done with this request
  1781. * %1 - this request is not freed yet.
  1782. * this request still has pending buffers or
  1783. * the driver doesn't want to finish this request yet.
  1784. **/
  1785. int blk_end_request_callback(struct request *rq, int error,
  1786. unsigned int nr_bytes,
  1787. int (drv_callback)(struct request *))
  1788. {
  1789. return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
  1790. }
  1791. EXPORT_SYMBOL_GPL(blk_end_request_callback);
  1792. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  1793. struct bio *bio)
  1794. {
  1795. /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
  1796. we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
  1797. rq->cmd_flags |= (bio->bi_rw & 3);
  1798. if (bio_has_data(bio)) {
  1799. rq->nr_phys_segments = bio_phys_segments(q, bio);
  1800. rq->buffer = bio_data(bio);
  1801. }
  1802. rq->current_nr_sectors = bio_cur_sectors(bio);
  1803. rq->hard_cur_sectors = rq->current_nr_sectors;
  1804. rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
  1805. rq->data_len = bio->bi_size;
  1806. rq->bio = rq->biotail = bio;
  1807. if (bio->bi_bdev)
  1808. rq->rq_disk = bio->bi_bdev->bd_disk;
  1809. }
  1810. /**
  1811. * blk_lld_busy - Check if underlying low-level drivers of a device are busy
  1812. * @q : the queue of the device being checked
  1813. *
  1814. * Description:
  1815. * Check if underlying low-level drivers of a device are busy.
  1816. * If the drivers want to export their busy state, they must set own
  1817. * exporting function using blk_queue_lld_busy() first.
  1818. *
  1819. * Basically, this function is used only by request stacking drivers
  1820. * to stop dispatching requests to underlying devices when underlying
  1821. * devices are busy. This behavior helps more I/O merging on the queue
  1822. * of the request stacking driver and prevents I/O throughput regression
  1823. * on burst I/O load.
  1824. *
  1825. * Return:
  1826. * 0 - Not busy (The request stacking driver should dispatch request)
  1827. * 1 - Busy (The request stacking driver should stop dispatching request)
  1828. */
  1829. int blk_lld_busy(struct request_queue *q)
  1830. {
  1831. if (q->lld_busy_fn)
  1832. return q->lld_busy_fn(q);
  1833. return 0;
  1834. }
  1835. EXPORT_SYMBOL_GPL(blk_lld_busy);
  1836. int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
  1837. {
  1838. return queue_work(kblockd_workqueue, work);
  1839. }
  1840. EXPORT_SYMBOL(kblockd_schedule_work);
  1841. int __init blk_dev_init(void)
  1842. {
  1843. kblockd_workqueue = create_workqueue("kblockd");
  1844. if (!kblockd_workqueue)
  1845. panic("Failed to create kblockd\n");
  1846. request_cachep = kmem_cache_create("blkdev_requests",
  1847. sizeof(struct request), 0, SLAB_PANIC, NULL);
  1848. blk_requestq_cachep = kmem_cache_create("blkdev_queue",
  1849. sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
  1850. return 0;
  1851. }