blk-core.c 58 KB

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