blk-core.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  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> - July2000
  7. * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
  8. */
  9. /*
  10. * This handles all read/write requests to block devices
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/bio.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/highmem.h>
  18. #include <linux/mm.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/string.h>
  21. #include <linux/init.h>
  22. #include <linux/completion.h>
  23. #include <linux/slab.h>
  24. #include <linux/swap.h>
  25. #include <linux/writeback.h>
  26. #include <linux/task_io_accounting_ops.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/cpu.h>
  29. #include <linux/blktrace_api.h>
  30. #include <linux/fault-inject.h>
  31. #include <linux/scatterlist.h>
  32. #include "blk.h"
  33. static int __make_request(struct request_queue *q, struct bio *bio);
  34. /*
  35. * For the allocated request tables
  36. */
  37. struct kmem_cache *request_cachep;
  38. /*
  39. * For queue allocation
  40. */
  41. struct kmem_cache *blk_requestq_cachep = NULL;
  42. /*
  43. * Controlling structure to kblockd
  44. */
  45. static struct workqueue_struct *kblockd_workqueue;
  46. static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
  47. static void drive_stat_acct(struct request *rq, int new_io)
  48. {
  49. int rw = rq_data_dir(rq);
  50. if (!blk_fs_request(rq) || !rq->rq_disk)
  51. return;
  52. if (!new_io) {
  53. __disk_stat_inc(rq->rq_disk, merges[rw]);
  54. } else {
  55. disk_round_stats(rq->rq_disk);
  56. rq->rq_disk->in_flight++;
  57. }
  58. }
  59. void blk_queue_congestion_threshold(struct request_queue *q)
  60. {
  61. int nr;
  62. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  63. if (nr > q->nr_requests)
  64. nr = q->nr_requests;
  65. q->nr_congestion_on = nr;
  66. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  67. if (nr < 1)
  68. nr = 1;
  69. q->nr_congestion_off = nr;
  70. }
  71. /**
  72. * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
  73. * @bdev: device
  74. *
  75. * Locates the passed device's request queue and returns the address of its
  76. * backing_dev_info
  77. *
  78. * Will return NULL if the request queue cannot be located.
  79. */
  80. struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
  81. {
  82. struct backing_dev_info *ret = NULL;
  83. struct request_queue *q = bdev_get_queue(bdev);
  84. if (q)
  85. ret = &q->backing_dev_info;
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(blk_get_backing_dev_info);
  89. void rq_init(struct request_queue *q, struct request *rq)
  90. {
  91. INIT_LIST_HEAD(&rq->queuelist);
  92. INIT_LIST_HEAD(&rq->donelist);
  93. rq->errors = 0;
  94. rq->bio = rq->biotail = NULL;
  95. INIT_HLIST_NODE(&rq->hash);
  96. RB_CLEAR_NODE(&rq->rb_node);
  97. rq->ioprio = 0;
  98. rq->buffer = NULL;
  99. rq->ref_count = 1;
  100. rq->q = q;
  101. rq->special = NULL;
  102. rq->data_len = 0;
  103. rq->data = NULL;
  104. rq->nr_phys_segments = 0;
  105. rq->sense = NULL;
  106. rq->end_io = NULL;
  107. rq->end_io_data = NULL;
  108. rq->completion_data = NULL;
  109. rq->next_rq = NULL;
  110. }
  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("%s: want %u bytes done, only %u left\n",
  122. __FUNCTION__, nbytes, bio->bi_size);
  123. nbytes = bio->bi_size;
  124. }
  125. bio->bi_size -= nbytes;
  126. bio->bi_sector += (nbytes >> 9);
  127. if (bio->bi_size == 0)
  128. bio_endio(bio, error);
  129. } else {
  130. /*
  131. * Okay, this is the barrier request in progress, just
  132. * record the error;
  133. */
  134. if (error && !q->orderr)
  135. q->orderr = error;
  136. }
  137. }
  138. void blk_dump_rq_flags(struct request *rq, char *msg)
  139. {
  140. int bit;
  141. printk("%s: dev %s: type=%x, flags=%x\n", msg,
  142. rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
  143. rq->cmd_flags);
  144. printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
  145. rq->nr_sectors,
  146. rq->current_nr_sectors);
  147. printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
  148. if (blk_pc_request(rq)) {
  149. printk("cdb: ");
  150. for (bit = 0; bit < sizeof(rq->cmd); bit++)
  151. printk("%02x ", rq->cmd[bit]);
  152. printk("\n");
  153. }
  154. }
  155. EXPORT_SYMBOL(blk_dump_rq_flags);
  156. static void blk_recalc_rq_segments(struct request *rq)
  157. {
  158. int nr_phys_segs;
  159. int nr_hw_segs;
  160. unsigned int phys_size;
  161. unsigned int hw_size;
  162. struct bio_vec *bv, *bvprv = NULL;
  163. int seg_size;
  164. int hw_seg_size;
  165. int cluster;
  166. struct req_iterator iter;
  167. int high, highprv = 1;
  168. struct request_queue *q = rq->q;
  169. if (!rq->bio)
  170. return;
  171. cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
  172. hw_seg_size = seg_size = 0;
  173. phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
  174. rq_for_each_segment(bv, rq, iter) {
  175. /*
  176. * the trick here is making sure that a high page is never
  177. * considered part of another segment, since that might
  178. * change with the bounce page.
  179. */
  180. high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
  181. if (high || highprv)
  182. goto new_hw_segment;
  183. if (cluster) {
  184. if (seg_size + bv->bv_len > q->max_segment_size)
  185. goto new_segment;
  186. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
  187. goto new_segment;
  188. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
  189. goto new_segment;
  190. if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
  191. goto new_hw_segment;
  192. seg_size += bv->bv_len;
  193. hw_seg_size += bv->bv_len;
  194. bvprv = bv;
  195. continue;
  196. }
  197. new_segment:
  198. if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
  199. !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
  200. hw_seg_size += bv->bv_len;
  201. else {
  202. new_hw_segment:
  203. if (nr_hw_segs == 1 &&
  204. hw_seg_size > rq->bio->bi_hw_front_size)
  205. rq->bio->bi_hw_front_size = hw_seg_size;
  206. hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
  207. nr_hw_segs++;
  208. }
  209. nr_phys_segs++;
  210. bvprv = bv;
  211. seg_size = bv->bv_len;
  212. highprv = high;
  213. }
  214. if (nr_hw_segs == 1 &&
  215. hw_seg_size > rq->bio->bi_hw_front_size)
  216. rq->bio->bi_hw_front_size = hw_seg_size;
  217. if (hw_seg_size > rq->biotail->bi_hw_back_size)
  218. rq->biotail->bi_hw_back_size = hw_seg_size;
  219. rq->nr_phys_segments = nr_phys_segs;
  220. rq->nr_hw_segments = nr_hw_segs;
  221. }
  222. void blk_recount_segments(struct request_queue *q, struct bio *bio)
  223. {
  224. struct request rq;
  225. struct bio *nxt = bio->bi_next;
  226. rq.q = q;
  227. rq.bio = rq.biotail = bio;
  228. bio->bi_next = NULL;
  229. blk_recalc_rq_segments(&rq);
  230. bio->bi_next = nxt;
  231. bio->bi_phys_segments = rq.nr_phys_segments;
  232. bio->bi_hw_segments = rq.nr_hw_segments;
  233. bio->bi_flags |= (1 << BIO_SEG_VALID);
  234. }
  235. EXPORT_SYMBOL(blk_recount_segments);
  236. static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
  237. struct bio *nxt)
  238. {
  239. if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
  240. return 0;
  241. if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
  242. return 0;
  243. if (bio->bi_size + nxt->bi_size > q->max_segment_size)
  244. return 0;
  245. /*
  246. * bio and nxt are contigous in memory, check if the queue allows
  247. * these two to be merged into one
  248. */
  249. if (BIO_SEG_BOUNDARY(q, bio, nxt))
  250. return 1;
  251. return 0;
  252. }
  253. static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
  254. struct bio *nxt)
  255. {
  256. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  257. blk_recount_segments(q, bio);
  258. if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
  259. blk_recount_segments(q, nxt);
  260. if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
  261. BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size))
  262. return 0;
  263. if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size)
  264. return 0;
  265. return 1;
  266. }
  267. /*
  268. * map a request to scatterlist, return number of sg entries setup. Caller
  269. * must make sure sg can hold rq->nr_phys_segments entries
  270. */
  271. int blk_rq_map_sg(struct request_queue *q, struct request *rq,
  272. struct scatterlist *sglist)
  273. {
  274. struct bio_vec *bvec, *bvprv;
  275. struct req_iterator iter;
  276. struct scatterlist *sg;
  277. int nsegs, cluster;
  278. nsegs = 0;
  279. cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
  280. /*
  281. * for each bio in rq
  282. */
  283. bvprv = NULL;
  284. sg = NULL;
  285. rq_for_each_segment(bvec, rq, iter) {
  286. int nbytes = bvec->bv_len;
  287. if (bvprv && cluster) {
  288. if (sg->length + nbytes > q->max_segment_size)
  289. goto new_segment;
  290. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
  291. goto new_segment;
  292. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
  293. goto new_segment;
  294. sg->length += nbytes;
  295. } else {
  296. new_segment:
  297. if (!sg)
  298. sg = sglist;
  299. else {
  300. /*
  301. * If the driver previously mapped a shorter
  302. * list, we could see a termination bit
  303. * prematurely unless it fully inits the sg
  304. * table on each mapping. We KNOW that there
  305. * must be more entries here or the driver
  306. * would be buggy, so force clear the
  307. * termination bit to avoid doing a full
  308. * sg_init_table() in drivers for each command.
  309. */
  310. sg->page_link &= ~0x02;
  311. sg = sg_next(sg);
  312. }
  313. sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
  314. nsegs++;
  315. }
  316. bvprv = bvec;
  317. } /* segments in rq */
  318. if (q->dma_drain_size) {
  319. sg->page_link &= ~0x02;
  320. sg = sg_next(sg);
  321. sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
  322. q->dma_drain_size,
  323. ((unsigned long)q->dma_drain_buffer) &
  324. (PAGE_SIZE - 1));
  325. nsegs++;
  326. }
  327. if (sg)
  328. sg_mark_end(sg);
  329. return nsegs;
  330. }
  331. EXPORT_SYMBOL(blk_rq_map_sg);
  332. static inline int ll_new_mergeable(struct request_queue *q,
  333. struct request *req,
  334. struct bio *bio)
  335. {
  336. int nr_phys_segs = bio_phys_segments(q, bio);
  337. if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
  338. req->cmd_flags |= REQ_NOMERGE;
  339. if (req == q->last_merge)
  340. q->last_merge = NULL;
  341. return 0;
  342. }
  343. /*
  344. * A hw segment is just getting larger, bump just the phys
  345. * counter.
  346. */
  347. req->nr_phys_segments += nr_phys_segs;
  348. return 1;
  349. }
  350. static inline int ll_new_hw_segment(struct request_queue *q,
  351. struct request *req,
  352. struct bio *bio)
  353. {
  354. int nr_hw_segs = bio_hw_segments(q, bio);
  355. int nr_phys_segs = bio_phys_segments(q, bio);
  356. if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
  357. || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
  358. req->cmd_flags |= REQ_NOMERGE;
  359. if (req == q->last_merge)
  360. q->last_merge = NULL;
  361. return 0;
  362. }
  363. /*
  364. * This will form the start of a new hw segment. Bump both
  365. * counters.
  366. */
  367. req->nr_hw_segments += nr_hw_segs;
  368. req->nr_phys_segments += nr_phys_segs;
  369. return 1;
  370. }
  371. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  372. struct bio *bio)
  373. {
  374. unsigned short max_sectors;
  375. int len;
  376. if (unlikely(blk_pc_request(req)))
  377. max_sectors = q->max_hw_sectors;
  378. else
  379. max_sectors = q->max_sectors;
  380. if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
  381. req->cmd_flags |= REQ_NOMERGE;
  382. if (req == q->last_merge)
  383. q->last_merge = NULL;
  384. return 0;
  385. }
  386. if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
  387. blk_recount_segments(q, req->biotail);
  388. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  389. blk_recount_segments(q, bio);
  390. len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
  391. if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
  392. !BIOVEC_VIRT_OVERSIZE(len)) {
  393. int mergeable = ll_new_mergeable(q, req, bio);
  394. if (mergeable) {
  395. if (req->nr_hw_segments == 1)
  396. req->bio->bi_hw_front_size = len;
  397. if (bio->bi_hw_segments == 1)
  398. bio->bi_hw_back_size = len;
  399. }
  400. return mergeable;
  401. }
  402. return ll_new_hw_segment(q, req, bio);
  403. }
  404. static int ll_front_merge_fn(struct request_queue *q, struct request *req,
  405. struct bio *bio)
  406. {
  407. unsigned short max_sectors;
  408. int len;
  409. if (unlikely(blk_pc_request(req)))
  410. max_sectors = q->max_hw_sectors;
  411. else
  412. max_sectors = q->max_sectors;
  413. if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
  414. req->cmd_flags |= REQ_NOMERGE;
  415. if (req == q->last_merge)
  416. q->last_merge = NULL;
  417. return 0;
  418. }
  419. len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
  420. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  421. blk_recount_segments(q, bio);
  422. if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
  423. blk_recount_segments(q, req->bio);
  424. if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
  425. !BIOVEC_VIRT_OVERSIZE(len)) {
  426. int mergeable = ll_new_mergeable(q, req, bio);
  427. if (mergeable) {
  428. if (bio->bi_hw_segments == 1)
  429. bio->bi_hw_front_size = len;
  430. if (req->nr_hw_segments == 1)
  431. req->biotail->bi_hw_back_size = len;
  432. }
  433. return mergeable;
  434. }
  435. return ll_new_hw_segment(q, req, bio);
  436. }
  437. static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
  438. struct request *next)
  439. {
  440. int total_phys_segments;
  441. int total_hw_segments;
  442. /*
  443. * First check if the either of the requests are re-queued
  444. * requests. Can't merge them if they are.
  445. */
  446. if (req->special || next->special)
  447. return 0;
  448. /*
  449. * Will it become too large?
  450. */
  451. if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
  452. return 0;
  453. total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
  454. if (blk_phys_contig_segment(q, req->biotail, next->bio))
  455. total_phys_segments--;
  456. if (total_phys_segments > q->max_phys_segments)
  457. return 0;
  458. total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
  459. if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
  460. int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
  461. /*
  462. * propagate the combined length to the end of the requests
  463. */
  464. if (req->nr_hw_segments == 1)
  465. req->bio->bi_hw_front_size = len;
  466. if (next->nr_hw_segments == 1)
  467. next->biotail->bi_hw_back_size = len;
  468. total_hw_segments--;
  469. }
  470. if (total_hw_segments > q->max_hw_segments)
  471. return 0;
  472. /* Merge is OK... */
  473. req->nr_phys_segments = total_phys_segments;
  474. req->nr_hw_segments = total_hw_segments;
  475. return 1;
  476. }
  477. /*
  478. * "plug" the device if there are no outstanding requests: this will
  479. * force the transfer to start only after we have put all the requests
  480. * on the list.
  481. *
  482. * This is called with interrupts off and no requests on the queue and
  483. * with the queue lock held.
  484. */
  485. void blk_plug_device(struct request_queue *q)
  486. {
  487. WARN_ON(!irqs_disabled());
  488. /*
  489. * don't plug a stopped queue, it must be paired with blk_start_queue()
  490. * which will restart the queueing
  491. */
  492. if (blk_queue_stopped(q))
  493. return;
  494. if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
  495. mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
  496. blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
  497. }
  498. }
  499. EXPORT_SYMBOL(blk_plug_device);
  500. /*
  501. * remove the queue from the plugged list, if present. called with
  502. * queue lock held and interrupts disabled.
  503. */
  504. int blk_remove_plug(struct request_queue *q)
  505. {
  506. WARN_ON(!irqs_disabled());
  507. if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
  508. return 0;
  509. del_timer(&q->unplug_timer);
  510. return 1;
  511. }
  512. EXPORT_SYMBOL(blk_remove_plug);
  513. /*
  514. * remove the plug and let it rip..
  515. */
  516. void __generic_unplug_device(struct request_queue *q)
  517. {
  518. if (unlikely(blk_queue_stopped(q)))
  519. return;
  520. if (!blk_remove_plug(q))
  521. return;
  522. q->request_fn(q);
  523. }
  524. EXPORT_SYMBOL(__generic_unplug_device);
  525. /**
  526. * generic_unplug_device - fire a request queue
  527. * @q: The &struct request_queue in question
  528. *
  529. * Description:
  530. * Linux uses plugging to build bigger requests queues before letting
  531. * the device have at them. If a queue is plugged, the I/O scheduler
  532. * is still adding and merging requests on the queue. Once the queue
  533. * gets unplugged, the request_fn defined for the queue is invoked and
  534. * transfers started.
  535. **/
  536. void generic_unplug_device(struct request_queue *q)
  537. {
  538. spin_lock_irq(q->queue_lock);
  539. __generic_unplug_device(q);
  540. spin_unlock_irq(q->queue_lock);
  541. }
  542. EXPORT_SYMBOL(generic_unplug_device);
  543. static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
  544. struct page *page)
  545. {
  546. struct request_queue *q = bdi->unplug_io_data;
  547. blk_unplug(q);
  548. }
  549. void blk_unplug_work(struct work_struct *work)
  550. {
  551. struct request_queue *q =
  552. container_of(work, struct request_queue, unplug_work);
  553. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
  554. q->rq.count[READ] + q->rq.count[WRITE]);
  555. q->unplug_fn(q);
  556. }
  557. void blk_unplug_timeout(unsigned long data)
  558. {
  559. struct request_queue *q = (struct request_queue *)data;
  560. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
  561. q->rq.count[READ] + q->rq.count[WRITE]);
  562. kblockd_schedule_work(&q->unplug_work);
  563. }
  564. void blk_unplug(struct request_queue *q)
  565. {
  566. /*
  567. * devices don't necessarily have an ->unplug_fn defined
  568. */
  569. if (q->unplug_fn) {
  570. blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
  571. q->rq.count[READ] + q->rq.count[WRITE]);
  572. q->unplug_fn(q);
  573. }
  574. }
  575. EXPORT_SYMBOL(blk_unplug);
  576. /**
  577. * blk_start_queue - restart a previously stopped queue
  578. * @q: The &struct request_queue in question
  579. *
  580. * Description:
  581. * blk_start_queue() will clear the stop flag on the queue, and call
  582. * the request_fn for the queue if it was in a stopped state when
  583. * entered. Also see blk_stop_queue(). Queue lock must be held.
  584. **/
  585. void blk_start_queue(struct request_queue *q)
  586. {
  587. WARN_ON(!irqs_disabled());
  588. clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
  589. /*
  590. * one level of recursion is ok and is much faster than kicking
  591. * the unplug handling
  592. */
  593. if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
  594. q->request_fn(q);
  595. clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
  596. } else {
  597. blk_plug_device(q);
  598. kblockd_schedule_work(&q->unplug_work);
  599. }
  600. }
  601. EXPORT_SYMBOL(blk_start_queue);
  602. /**
  603. * blk_stop_queue - stop a queue
  604. * @q: The &struct request_queue in question
  605. *
  606. * Description:
  607. * The Linux block layer assumes that a block driver will consume all
  608. * entries on the request queue when the request_fn strategy is called.
  609. * Often this will not happen, because of hardware limitations (queue
  610. * depth settings). If a device driver gets a 'queue full' response,
  611. * or if it simply chooses not to queue more I/O at one point, it can
  612. * call this function to prevent the request_fn from being called until
  613. * the driver has signalled it's ready to go again. This happens by calling
  614. * blk_start_queue() to restart queue operations. Queue lock must be held.
  615. **/
  616. void blk_stop_queue(struct request_queue *q)
  617. {
  618. blk_remove_plug(q);
  619. set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
  620. }
  621. EXPORT_SYMBOL(blk_stop_queue);
  622. /**
  623. * blk_sync_queue - cancel any pending callbacks on a queue
  624. * @q: the queue
  625. *
  626. * Description:
  627. * The block layer may perform asynchronous callback activity
  628. * on a queue, such as calling the unplug function after a timeout.
  629. * A block device may call blk_sync_queue to ensure that any
  630. * such activity is cancelled, thus allowing it to release resources
  631. * that the callbacks might use. The caller must already have made sure
  632. * that its ->make_request_fn will not re-add plugging prior to calling
  633. * this function.
  634. *
  635. */
  636. void blk_sync_queue(struct request_queue *q)
  637. {
  638. del_timer_sync(&q->unplug_timer);
  639. kblockd_flush_work(&q->unplug_work);
  640. }
  641. EXPORT_SYMBOL(blk_sync_queue);
  642. /**
  643. * blk_run_queue - run a single device queue
  644. * @q: The queue to run
  645. */
  646. void blk_run_queue(struct request_queue *q)
  647. {
  648. unsigned long flags;
  649. spin_lock_irqsave(q->queue_lock, flags);
  650. blk_remove_plug(q);
  651. /*
  652. * Only recurse once to avoid overrunning the stack, let the unplug
  653. * handling reinvoke the handler shortly if we already got there.
  654. */
  655. if (!elv_queue_empty(q)) {
  656. if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
  657. q->request_fn(q);
  658. clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
  659. } else {
  660. blk_plug_device(q);
  661. kblockd_schedule_work(&q->unplug_work);
  662. }
  663. }
  664. spin_unlock_irqrestore(q->queue_lock, flags);
  665. }
  666. EXPORT_SYMBOL(blk_run_queue);
  667. void blk_put_queue(struct request_queue *q)
  668. {
  669. kobject_put(&q->kobj);
  670. }
  671. EXPORT_SYMBOL(blk_put_queue);
  672. void blk_cleanup_queue(struct request_queue * q)
  673. {
  674. mutex_lock(&q->sysfs_lock);
  675. set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
  676. mutex_unlock(&q->sysfs_lock);
  677. if (q->elevator)
  678. elevator_exit(q->elevator);
  679. blk_put_queue(q);
  680. }
  681. EXPORT_SYMBOL(blk_cleanup_queue);
  682. static int blk_init_free_list(struct request_queue *q)
  683. {
  684. struct request_list *rl = &q->rq;
  685. rl->count[READ] = rl->count[WRITE] = 0;
  686. rl->starved[READ] = rl->starved[WRITE] = 0;
  687. rl->elvpriv = 0;
  688. init_waitqueue_head(&rl->wait[READ]);
  689. init_waitqueue_head(&rl->wait[WRITE]);
  690. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  691. mempool_free_slab, request_cachep, q->node);
  692. if (!rl->rq_pool)
  693. return -ENOMEM;
  694. return 0;
  695. }
  696. struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
  697. {
  698. return blk_alloc_queue_node(gfp_mask, -1);
  699. }
  700. EXPORT_SYMBOL(blk_alloc_queue);
  701. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  702. {
  703. struct request_queue *q;
  704. int err;
  705. q = kmem_cache_alloc_node(blk_requestq_cachep,
  706. gfp_mask | __GFP_ZERO, node_id);
  707. if (!q)
  708. return NULL;
  709. q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
  710. q->backing_dev_info.unplug_io_data = q;
  711. err = bdi_init(&q->backing_dev_info);
  712. if (err) {
  713. kmem_cache_free(blk_requestq_cachep, q);
  714. return NULL;
  715. }
  716. init_timer(&q->unplug_timer);
  717. kobject_init(&q->kobj, &blk_queue_ktype);
  718. mutex_init(&q->sysfs_lock);
  719. return q;
  720. }
  721. EXPORT_SYMBOL(blk_alloc_queue_node);
  722. /**
  723. * blk_init_queue - prepare a request queue for use with a block device
  724. * @rfn: The function to be called to process requests that have been
  725. * placed on the queue.
  726. * @lock: Request queue spin lock
  727. *
  728. * Description:
  729. * If a block device wishes to use the standard request handling procedures,
  730. * which sorts requests and coalesces adjacent requests, then it must
  731. * call blk_init_queue(). The function @rfn will be called when there
  732. * are requests on the queue that need to be processed. If the device
  733. * supports plugging, then @rfn may not be called immediately when requests
  734. * are available on the queue, but may be called at some time later instead.
  735. * Plugged queues are generally unplugged when a buffer belonging to one
  736. * of the requests on the queue is needed, or due to memory pressure.
  737. *
  738. * @rfn is not required, or even expected, to remove all requests off the
  739. * queue, but only as many as it can handle at a time. If it does leave
  740. * requests on the queue, it is responsible for arranging that the requests
  741. * get dealt with eventually.
  742. *
  743. * The queue spin lock must be held while manipulating the requests on the
  744. * request queue; this lock will be taken also from interrupt context, so irq
  745. * disabling is needed for it.
  746. *
  747. * Function returns a pointer to the initialized request queue, or NULL if
  748. * it didn't succeed.
  749. *
  750. * Note:
  751. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  752. * when the block device is deactivated (such as at module unload).
  753. **/
  754. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  755. {
  756. return blk_init_queue_node(rfn, lock, -1);
  757. }
  758. EXPORT_SYMBOL(blk_init_queue);
  759. struct request_queue *
  760. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  761. {
  762. struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
  763. if (!q)
  764. return NULL;
  765. q->node = node_id;
  766. if (blk_init_free_list(q)) {
  767. kmem_cache_free(blk_requestq_cachep, q);
  768. return NULL;
  769. }
  770. /*
  771. * if caller didn't supply a lock, they get per-queue locking with
  772. * our embedded lock
  773. */
  774. if (!lock) {
  775. spin_lock_init(&q->__queue_lock);
  776. lock = &q->__queue_lock;
  777. }
  778. q->request_fn = rfn;
  779. q->prep_rq_fn = NULL;
  780. q->unplug_fn = generic_unplug_device;
  781. q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
  782. q->queue_lock = lock;
  783. blk_queue_segment_boundary(q, 0xffffffff);
  784. blk_queue_make_request(q, __make_request);
  785. blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
  786. blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
  787. blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
  788. q->sg_reserved_size = INT_MAX;
  789. /*
  790. * all done
  791. */
  792. if (!elevator_init(q, NULL)) {
  793. blk_queue_congestion_threshold(q);
  794. return q;
  795. }
  796. blk_put_queue(q);
  797. return NULL;
  798. }
  799. EXPORT_SYMBOL(blk_init_queue_node);
  800. int blk_get_queue(struct request_queue *q)
  801. {
  802. if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
  803. kobject_get(&q->kobj);
  804. return 0;
  805. }
  806. return 1;
  807. }
  808. EXPORT_SYMBOL(blk_get_queue);
  809. static inline void blk_free_request(struct request_queue *q, struct request *rq)
  810. {
  811. if (rq->cmd_flags & REQ_ELVPRIV)
  812. elv_put_request(q, rq);
  813. mempool_free(rq, q->rq.rq_pool);
  814. }
  815. static struct request *
  816. blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
  817. {
  818. struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
  819. if (!rq)
  820. return NULL;
  821. /*
  822. * first three bits are identical in rq->cmd_flags and bio->bi_rw,
  823. * see bio.h and blkdev.h
  824. */
  825. rq->cmd_flags = rw | REQ_ALLOCED;
  826. if (priv) {
  827. if (unlikely(elv_set_request(q, rq, gfp_mask))) {
  828. mempool_free(rq, q->rq.rq_pool);
  829. return NULL;
  830. }
  831. rq->cmd_flags |= REQ_ELVPRIV;
  832. }
  833. return rq;
  834. }
  835. /*
  836. * ioc_batching returns true if the ioc is a valid batching request and
  837. * should be given priority access to a request.
  838. */
  839. static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
  840. {
  841. if (!ioc)
  842. return 0;
  843. /*
  844. * Make sure the process is able to allocate at least 1 request
  845. * even if the batch times out, otherwise we could theoretically
  846. * lose wakeups.
  847. */
  848. return ioc->nr_batch_requests == q->nr_batching ||
  849. (ioc->nr_batch_requests > 0
  850. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  851. }
  852. /*
  853. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  854. * will cause the process to be a "batcher" on all queues in the system. This
  855. * is the behaviour we want though - once it gets a wakeup it should be given
  856. * a nice run.
  857. */
  858. static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
  859. {
  860. if (!ioc || ioc_batching(q, ioc))
  861. return;
  862. ioc->nr_batch_requests = q->nr_batching;
  863. ioc->last_waited = jiffies;
  864. }
  865. static void __freed_request(struct request_queue *q, int rw)
  866. {
  867. struct request_list *rl = &q->rq;
  868. if (rl->count[rw] < queue_congestion_off_threshold(q))
  869. blk_clear_queue_congested(q, rw);
  870. if (rl->count[rw] + 1 <= q->nr_requests) {
  871. if (waitqueue_active(&rl->wait[rw]))
  872. wake_up(&rl->wait[rw]);
  873. blk_clear_queue_full(q, rw);
  874. }
  875. }
  876. /*
  877. * A request has just been released. Account for it, update the full and
  878. * congestion status, wake up any waiters. Called under q->queue_lock.
  879. */
  880. static void freed_request(struct request_queue *q, int rw, int priv)
  881. {
  882. struct request_list *rl = &q->rq;
  883. rl->count[rw]--;
  884. if (priv)
  885. rl->elvpriv--;
  886. __freed_request(q, rw);
  887. if (unlikely(rl->starved[rw ^ 1]))
  888. __freed_request(q, rw ^ 1);
  889. }
  890. #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
  891. /*
  892. * Get a free request, queue_lock must be held.
  893. * Returns NULL on failure, with queue_lock held.
  894. * Returns !NULL on success, with queue_lock *not held*.
  895. */
  896. static struct request *get_request(struct request_queue *q, int rw_flags,
  897. struct bio *bio, gfp_t gfp_mask)
  898. {
  899. struct request *rq = NULL;
  900. struct request_list *rl = &q->rq;
  901. struct io_context *ioc = NULL;
  902. const int rw = rw_flags & 0x01;
  903. int may_queue, priv;
  904. may_queue = elv_may_queue(q, rw_flags);
  905. if (may_queue == ELV_MQUEUE_NO)
  906. goto rq_starved;
  907. if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
  908. if (rl->count[rw]+1 >= q->nr_requests) {
  909. ioc = current_io_context(GFP_ATOMIC, q->node);
  910. /*
  911. * The queue will fill after this allocation, so set
  912. * it as full, and mark this process as "batching".
  913. * This process will be allowed to complete a batch of
  914. * requests, others will be blocked.
  915. */
  916. if (!blk_queue_full(q, rw)) {
  917. ioc_set_batching(q, ioc);
  918. blk_set_queue_full(q, rw);
  919. } else {
  920. if (may_queue != ELV_MQUEUE_MUST
  921. && !ioc_batching(q, ioc)) {
  922. /*
  923. * The queue is full and the allocating
  924. * process is not a "batcher", and not
  925. * exempted by the IO scheduler
  926. */
  927. goto out;
  928. }
  929. }
  930. }
  931. blk_set_queue_congested(q, rw);
  932. }
  933. /*
  934. * Only allow batching queuers to allocate up to 50% over the defined
  935. * limit of requests, otherwise we could have thousands of requests
  936. * allocated with any setting of ->nr_requests
  937. */
  938. if (rl->count[rw] >= (3 * q->nr_requests / 2))
  939. goto out;
  940. rl->count[rw]++;
  941. rl->starved[rw] = 0;
  942. priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
  943. if (priv)
  944. rl->elvpriv++;
  945. spin_unlock_irq(q->queue_lock);
  946. rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
  947. if (unlikely(!rq)) {
  948. /*
  949. * Allocation failed presumably due to memory. Undo anything
  950. * we might have messed up.
  951. *
  952. * Allocating task should really be put onto the front of the
  953. * wait queue, but this is pretty rare.
  954. */
  955. spin_lock_irq(q->queue_lock);
  956. freed_request(q, rw, priv);
  957. /*
  958. * in the very unlikely event that allocation failed and no
  959. * requests for this direction was pending, mark us starved
  960. * so that freeing of a request in the other direction will
  961. * notice us. another possible fix would be to split the
  962. * rq mempool into READ and WRITE
  963. */
  964. rq_starved:
  965. if (unlikely(rl->count[rw] == 0))
  966. rl->starved[rw] = 1;
  967. goto out;
  968. }
  969. /*
  970. * ioc may be NULL here, and ioc_batching will be false. That's
  971. * OK, if the queue is under the request limit then requests need
  972. * not count toward the nr_batch_requests limit. There will always
  973. * be some limit enforced by BLK_BATCH_TIME.
  974. */
  975. if (ioc_batching(q, ioc))
  976. ioc->nr_batch_requests--;
  977. rq_init(q, rq);
  978. blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
  979. out:
  980. return rq;
  981. }
  982. /*
  983. * No available requests for this queue, unplug the device and wait for some
  984. * requests to become available.
  985. *
  986. * Called with q->queue_lock held, and returns with it unlocked.
  987. */
  988. static struct request *get_request_wait(struct request_queue *q, int rw_flags,
  989. struct bio *bio)
  990. {
  991. const int rw = rw_flags & 0x01;
  992. struct request *rq;
  993. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  994. while (!rq) {
  995. DEFINE_WAIT(wait);
  996. struct request_list *rl = &q->rq;
  997. prepare_to_wait_exclusive(&rl->wait[rw], &wait,
  998. TASK_UNINTERRUPTIBLE);
  999. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  1000. if (!rq) {
  1001. struct io_context *ioc;
  1002. blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
  1003. __generic_unplug_device(q);
  1004. spin_unlock_irq(q->queue_lock);
  1005. io_schedule();
  1006. /*
  1007. * After sleeping, we become a "batching" process and
  1008. * will be able to allocate at least one request, and
  1009. * up to a big batch of them for a small period time.
  1010. * See ioc_batching, ioc_set_batching
  1011. */
  1012. ioc = current_io_context(GFP_NOIO, q->node);
  1013. ioc_set_batching(q, ioc);
  1014. spin_lock_irq(q->queue_lock);
  1015. }
  1016. finish_wait(&rl->wait[rw], &wait);
  1017. }
  1018. return rq;
  1019. }
  1020. struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
  1021. {
  1022. struct request *rq;
  1023. BUG_ON(rw != READ && rw != WRITE);
  1024. spin_lock_irq(q->queue_lock);
  1025. if (gfp_mask & __GFP_WAIT) {
  1026. rq = get_request_wait(q, rw, NULL);
  1027. } else {
  1028. rq = get_request(q, rw, NULL, gfp_mask);
  1029. if (!rq)
  1030. spin_unlock_irq(q->queue_lock);
  1031. }
  1032. /* q->queue_lock is unlocked at this point */
  1033. return rq;
  1034. }
  1035. EXPORT_SYMBOL(blk_get_request);
  1036. /**
  1037. * blk_start_queueing - initiate dispatch of requests to device
  1038. * @q: request queue to kick into gear
  1039. *
  1040. * This is basically a helper to remove the need to know whether a queue
  1041. * is plugged or not if someone just wants to initiate dispatch of requests
  1042. * for this queue.
  1043. *
  1044. * The queue lock must be held with interrupts disabled.
  1045. */
  1046. void blk_start_queueing(struct request_queue *q)
  1047. {
  1048. if (!blk_queue_plugged(q))
  1049. q->request_fn(q);
  1050. else
  1051. __generic_unplug_device(q);
  1052. }
  1053. EXPORT_SYMBOL(blk_start_queueing);
  1054. /**
  1055. * blk_requeue_request - put a request back on queue
  1056. * @q: request queue where request should be inserted
  1057. * @rq: request to be inserted
  1058. *
  1059. * Description:
  1060. * Drivers often keep queueing requests until the hardware cannot accept
  1061. * more, when that condition happens we need to put the request back
  1062. * on the queue. Must be called with queue lock held.
  1063. */
  1064. void blk_requeue_request(struct request_queue *q, struct request *rq)
  1065. {
  1066. blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
  1067. if (blk_rq_tagged(rq))
  1068. blk_queue_end_tag(q, rq);
  1069. elv_requeue_request(q, rq);
  1070. }
  1071. EXPORT_SYMBOL(blk_requeue_request);
  1072. /**
  1073. * blk_insert_request - insert a special request in to a request queue
  1074. * @q: request queue where request should be inserted
  1075. * @rq: request to be inserted
  1076. * @at_head: insert request at head or tail of queue
  1077. * @data: private data
  1078. *
  1079. * Description:
  1080. * Many block devices need to execute commands asynchronously, so they don't
  1081. * block the whole kernel from preemption during request execution. This is
  1082. * accomplished normally by inserting aritficial requests tagged as
  1083. * REQ_SPECIAL in to the corresponding request queue, and letting them be
  1084. * scheduled for actual execution by the request queue.
  1085. *
  1086. * We have the option of inserting the head or the tail of the queue.
  1087. * Typically we use the tail for new ioctls and so forth. We use the head
  1088. * of the queue for things like a QUEUE_FULL message from a device, or a
  1089. * host that is unable to accept a particular command.
  1090. */
  1091. void blk_insert_request(struct request_queue *q, struct request *rq,
  1092. int at_head, void *data)
  1093. {
  1094. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  1095. unsigned long flags;
  1096. /*
  1097. * tell I/O scheduler that this isn't a regular read/write (ie it
  1098. * must not attempt merges on this) and that it acts as a soft
  1099. * barrier
  1100. */
  1101. rq->cmd_type = REQ_TYPE_SPECIAL;
  1102. rq->cmd_flags |= REQ_SOFTBARRIER;
  1103. rq->special = data;
  1104. spin_lock_irqsave(q->queue_lock, flags);
  1105. /*
  1106. * If command is tagged, release the tag
  1107. */
  1108. if (blk_rq_tagged(rq))
  1109. blk_queue_end_tag(q, rq);
  1110. drive_stat_acct(rq, 1);
  1111. __elv_add_request(q, rq, where, 0);
  1112. blk_start_queueing(q);
  1113. spin_unlock_irqrestore(q->queue_lock, flags);
  1114. }
  1115. EXPORT_SYMBOL(blk_insert_request);
  1116. /*
  1117. * add-request adds a request to the linked list.
  1118. * queue lock is held and interrupts disabled, as we muck with the
  1119. * request queue list.
  1120. */
  1121. static inline void add_request(struct request_queue * q, struct request * req)
  1122. {
  1123. drive_stat_acct(req, 1);
  1124. /*
  1125. * elevator indicated where it wants this request to be
  1126. * inserted at elevator_merge time
  1127. */
  1128. __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
  1129. }
  1130. /*
  1131. * disk_round_stats() - Round off the performance stats on a struct
  1132. * disk_stats.
  1133. *
  1134. * The average IO queue length and utilisation statistics are maintained
  1135. * by observing the current state of the queue length and the amount of
  1136. * time it has been in this state for.
  1137. *
  1138. * Normally, that accounting is done on IO completion, but that can result
  1139. * in more than a second's worth of IO being accounted for within any one
  1140. * second, leading to >100% utilisation. To deal with that, we call this
  1141. * function to do a round-off before returning the results when reading
  1142. * /proc/diskstats. This accounts immediately for all queue usage up to
  1143. * the current jiffies and restarts the counters again.
  1144. */
  1145. void disk_round_stats(struct gendisk *disk)
  1146. {
  1147. unsigned long now = jiffies;
  1148. if (now == disk->stamp)
  1149. return;
  1150. if (disk->in_flight) {
  1151. __disk_stat_add(disk, time_in_queue,
  1152. disk->in_flight * (now - disk->stamp));
  1153. __disk_stat_add(disk, io_ticks, (now - disk->stamp));
  1154. }
  1155. disk->stamp = now;
  1156. }
  1157. EXPORT_SYMBOL_GPL(disk_round_stats);
  1158. /*
  1159. * queue lock must be held
  1160. */
  1161. void __blk_put_request(struct request_queue *q, struct request *req)
  1162. {
  1163. if (unlikely(!q))
  1164. return;
  1165. if (unlikely(--req->ref_count))
  1166. return;
  1167. elv_completed_request(q, req);
  1168. /*
  1169. * Request may not have originated from ll_rw_blk. if not,
  1170. * it didn't come out of our reserved rq pools
  1171. */
  1172. if (req->cmd_flags & REQ_ALLOCED) {
  1173. int rw = rq_data_dir(req);
  1174. int priv = req->cmd_flags & REQ_ELVPRIV;
  1175. BUG_ON(!list_empty(&req->queuelist));
  1176. BUG_ON(!hlist_unhashed(&req->hash));
  1177. blk_free_request(q, req);
  1178. freed_request(q, rw, priv);
  1179. }
  1180. }
  1181. EXPORT_SYMBOL_GPL(__blk_put_request);
  1182. void blk_put_request(struct request *req)
  1183. {
  1184. unsigned long flags;
  1185. struct request_queue *q = req->q;
  1186. /*
  1187. * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
  1188. * following if (q) test.
  1189. */
  1190. if (q) {
  1191. spin_lock_irqsave(q->queue_lock, flags);
  1192. __blk_put_request(q, req);
  1193. spin_unlock_irqrestore(q->queue_lock, flags);
  1194. }
  1195. }
  1196. EXPORT_SYMBOL(blk_put_request);
  1197. /*
  1198. * Has to be called with the request spinlock acquired
  1199. */
  1200. static int attempt_merge(struct request_queue *q, struct request *req,
  1201. struct request *next)
  1202. {
  1203. if (!rq_mergeable(req) || !rq_mergeable(next))
  1204. return 0;
  1205. /*
  1206. * not contiguous
  1207. */
  1208. if (req->sector + req->nr_sectors != next->sector)
  1209. return 0;
  1210. if (rq_data_dir(req) != rq_data_dir(next)
  1211. || req->rq_disk != next->rq_disk
  1212. || next->special)
  1213. return 0;
  1214. /*
  1215. * If we are allowed to merge, then append bio list
  1216. * from next to rq and release next. merge_requests_fn
  1217. * will have updated segment counts, update sector
  1218. * counts here.
  1219. */
  1220. if (!ll_merge_requests_fn(q, req, next))
  1221. return 0;
  1222. /*
  1223. * At this point we have either done a back merge
  1224. * or front merge. We need the smaller start_time of
  1225. * the merged requests to be the current request
  1226. * for accounting purposes.
  1227. */
  1228. if (time_after(req->start_time, next->start_time))
  1229. req->start_time = next->start_time;
  1230. req->biotail->bi_next = next->bio;
  1231. req->biotail = next->biotail;
  1232. req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
  1233. elv_merge_requests(q, req, next);
  1234. if (req->rq_disk) {
  1235. disk_round_stats(req->rq_disk);
  1236. req->rq_disk->in_flight--;
  1237. }
  1238. req->ioprio = ioprio_best(req->ioprio, next->ioprio);
  1239. __blk_put_request(q, next);
  1240. return 1;
  1241. }
  1242. static inline int attempt_back_merge(struct request_queue *q,
  1243. struct request *rq)
  1244. {
  1245. struct request *next = elv_latter_request(q, rq);
  1246. if (next)
  1247. return attempt_merge(q, rq, next);
  1248. return 0;
  1249. }
  1250. static inline int attempt_front_merge(struct request_queue *q,
  1251. struct request *rq)
  1252. {
  1253. struct request *prev = elv_former_request(q, rq);
  1254. if (prev)
  1255. return attempt_merge(q, prev, rq);
  1256. return 0;
  1257. }
  1258. void init_request_from_bio(struct request *req, struct bio *bio)
  1259. {
  1260. req->cmd_type = REQ_TYPE_FS;
  1261. /*
  1262. * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
  1263. */
  1264. if (bio_rw_ahead(bio) || bio_failfast(bio))
  1265. req->cmd_flags |= REQ_FAILFAST;
  1266. /*
  1267. * REQ_BARRIER implies no merging, but lets make it explicit
  1268. */
  1269. if (unlikely(bio_barrier(bio)))
  1270. req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
  1271. if (bio_sync(bio))
  1272. req->cmd_flags |= REQ_RW_SYNC;
  1273. if (bio_rw_meta(bio))
  1274. req->cmd_flags |= REQ_RW_META;
  1275. req->errors = 0;
  1276. req->hard_sector = req->sector = bio->bi_sector;
  1277. req->ioprio = bio_prio(bio);
  1278. req->start_time = jiffies;
  1279. blk_rq_bio_prep(req->q, req, bio);
  1280. }
  1281. static int __make_request(struct request_queue *q, struct bio *bio)
  1282. {
  1283. struct request *req;
  1284. int el_ret, nr_sectors, barrier, err;
  1285. const unsigned short prio = bio_prio(bio);
  1286. const int sync = bio_sync(bio);
  1287. int rw_flags;
  1288. nr_sectors = bio_sectors(bio);
  1289. /*
  1290. * low level driver can indicate that it wants pages above a
  1291. * certain limit bounced to low memory (ie for highmem, or even
  1292. * ISA dma in theory)
  1293. */
  1294. blk_queue_bounce(q, &bio);
  1295. barrier = bio_barrier(bio);
  1296. if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
  1297. err = -EOPNOTSUPP;
  1298. goto end_io;
  1299. }
  1300. spin_lock_irq(q->queue_lock);
  1301. if (unlikely(barrier) || elv_queue_empty(q))
  1302. goto get_rq;
  1303. el_ret = elv_merge(q, &req, bio);
  1304. switch (el_ret) {
  1305. case ELEVATOR_BACK_MERGE:
  1306. BUG_ON(!rq_mergeable(req));
  1307. if (!ll_back_merge_fn(q, req, bio))
  1308. break;
  1309. blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
  1310. req->biotail->bi_next = bio;
  1311. req->biotail = bio;
  1312. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  1313. req->ioprio = ioprio_best(req->ioprio, prio);
  1314. drive_stat_acct(req, 0);
  1315. if (!attempt_back_merge(q, req))
  1316. elv_merged_request(q, req, el_ret);
  1317. goto out;
  1318. case ELEVATOR_FRONT_MERGE:
  1319. BUG_ON(!rq_mergeable(req));
  1320. if (!ll_front_merge_fn(q, req, bio))
  1321. break;
  1322. blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
  1323. bio->bi_next = req->bio;
  1324. req->bio = bio;
  1325. /*
  1326. * may not be valid. if the low level driver said
  1327. * it didn't need a bounce buffer then it better
  1328. * not touch req->buffer either...
  1329. */
  1330. req->buffer = bio_data(bio);
  1331. req->current_nr_sectors = bio_cur_sectors(bio);
  1332. req->hard_cur_sectors = req->current_nr_sectors;
  1333. req->sector = req->hard_sector = bio->bi_sector;
  1334. req->nr_sectors = req->hard_nr_sectors += nr_sectors;
  1335. req->ioprio = ioprio_best(req->ioprio, prio);
  1336. drive_stat_acct(req, 0);
  1337. if (!attempt_front_merge(q, req))
  1338. elv_merged_request(q, req, el_ret);
  1339. goto out;
  1340. /* ELV_NO_MERGE: elevator says don't/can't merge. */
  1341. default:
  1342. ;
  1343. }
  1344. get_rq:
  1345. /*
  1346. * This sync check and mask will be re-done in init_request_from_bio(),
  1347. * but we need to set it earlier to expose the sync flag to the
  1348. * rq allocator and io schedulers.
  1349. */
  1350. rw_flags = bio_data_dir(bio);
  1351. if (sync)
  1352. rw_flags |= REQ_RW_SYNC;
  1353. /*
  1354. * Grab a free request. This is might sleep but can not fail.
  1355. * Returns with the queue unlocked.
  1356. */
  1357. req = get_request_wait(q, rw_flags, bio);
  1358. /*
  1359. * After dropping the lock and possibly sleeping here, our request
  1360. * may now be mergeable after it had proven unmergeable (above).
  1361. * We don't worry about that case for efficiency. It won't happen
  1362. * often, and the elevators are able to handle it.
  1363. */
  1364. init_request_from_bio(req, bio);
  1365. spin_lock_irq(q->queue_lock);
  1366. if (elv_queue_empty(q))
  1367. blk_plug_device(q);
  1368. add_request(q, req);
  1369. out:
  1370. if (sync)
  1371. __generic_unplug_device(q);
  1372. spin_unlock_irq(q->queue_lock);
  1373. return 0;
  1374. end_io:
  1375. bio_endio(bio, err);
  1376. return 0;
  1377. }
  1378. /*
  1379. * If bio->bi_dev is a partition, remap the location
  1380. */
  1381. static inline void blk_partition_remap(struct bio *bio)
  1382. {
  1383. struct block_device *bdev = bio->bi_bdev;
  1384. if (bio_sectors(bio) && bdev != bdev->bd_contains) {
  1385. struct hd_struct *p = bdev->bd_part;
  1386. const int rw = bio_data_dir(bio);
  1387. p->sectors[rw] += bio_sectors(bio);
  1388. p->ios[rw]++;
  1389. bio->bi_sector += p->start_sect;
  1390. bio->bi_bdev = bdev->bd_contains;
  1391. blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
  1392. bdev->bd_dev, bio->bi_sector,
  1393. bio->bi_sector - p->start_sect);
  1394. }
  1395. }
  1396. static void handle_bad_sector(struct bio *bio)
  1397. {
  1398. char b[BDEVNAME_SIZE];
  1399. printk(KERN_INFO "attempt to access beyond end of device\n");
  1400. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  1401. bdevname(bio->bi_bdev, b),
  1402. bio->bi_rw,
  1403. (unsigned long long)bio->bi_sector + bio_sectors(bio),
  1404. (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
  1405. set_bit(BIO_EOF, &bio->bi_flags);
  1406. }
  1407. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1408. static DECLARE_FAULT_ATTR(fail_make_request);
  1409. static int __init setup_fail_make_request(char *str)
  1410. {
  1411. return setup_fault_attr(&fail_make_request, str);
  1412. }
  1413. __setup("fail_make_request=", setup_fail_make_request);
  1414. static int should_fail_request(struct bio *bio)
  1415. {
  1416. if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
  1417. (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
  1418. return should_fail(&fail_make_request, bio->bi_size);
  1419. return 0;
  1420. }
  1421. static int __init fail_make_request_debugfs(void)
  1422. {
  1423. return init_fault_attr_dentries(&fail_make_request,
  1424. "fail_make_request");
  1425. }
  1426. late_initcall(fail_make_request_debugfs);
  1427. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1428. static inline int should_fail_request(struct bio *bio)
  1429. {
  1430. return 0;
  1431. }
  1432. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1433. /*
  1434. * Check whether this bio extends beyond the end of the device.
  1435. */
  1436. static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
  1437. {
  1438. sector_t maxsector;
  1439. if (!nr_sectors)
  1440. return 0;
  1441. /* Test device or partition size, when known. */
  1442. maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
  1443. if (maxsector) {
  1444. sector_t sector = bio->bi_sector;
  1445. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  1446. /*
  1447. * This may well happen - the kernel calls bread()
  1448. * without checking the size of the device, e.g., when
  1449. * mounting a device.
  1450. */
  1451. handle_bad_sector(bio);
  1452. return 1;
  1453. }
  1454. }
  1455. return 0;
  1456. }
  1457. /**
  1458. * generic_make_request: hand a buffer to its device driver for I/O
  1459. * @bio: The bio describing the location in memory and on the device.
  1460. *
  1461. * generic_make_request() is used to make I/O requests of block
  1462. * devices. It is passed a &struct bio, which describes the I/O that needs
  1463. * to be done.
  1464. *
  1465. * generic_make_request() does not return any status. The
  1466. * success/failure status of the request, along with notification of
  1467. * completion, is delivered asynchronously through the bio->bi_end_io
  1468. * function described (one day) else where.
  1469. *
  1470. * The caller of generic_make_request must make sure that bi_io_vec
  1471. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  1472. * set to describe the device address, and the
  1473. * bi_end_io and optionally bi_private are set to describe how
  1474. * completion notification should be signaled.
  1475. *
  1476. * generic_make_request and the drivers it calls may use bi_next if this
  1477. * bio happens to be merged with someone else, and may change bi_dev and
  1478. * bi_sector for remaps as it sees fit. So the values of these fields
  1479. * should NOT be depended on after the call to generic_make_request.
  1480. */
  1481. static inline void __generic_make_request(struct bio *bio)
  1482. {
  1483. struct request_queue *q;
  1484. sector_t old_sector;
  1485. int ret, nr_sectors = bio_sectors(bio);
  1486. dev_t old_dev;
  1487. int err = -EIO;
  1488. might_sleep();
  1489. if (bio_check_eod(bio, nr_sectors))
  1490. goto end_io;
  1491. /*
  1492. * Resolve the mapping until finished. (drivers are
  1493. * still free to implement/resolve their own stacking
  1494. * by explicitly returning 0)
  1495. *
  1496. * NOTE: we don't repeat the blk_size check for each new device.
  1497. * Stacking drivers are expected to know what they are doing.
  1498. */
  1499. old_sector = -1;
  1500. old_dev = 0;
  1501. do {
  1502. char b[BDEVNAME_SIZE];
  1503. q = bdev_get_queue(bio->bi_bdev);
  1504. if (!q) {
  1505. printk(KERN_ERR
  1506. "generic_make_request: Trying to access "
  1507. "nonexistent block-device %s (%Lu)\n",
  1508. bdevname(bio->bi_bdev, b),
  1509. (long long) bio->bi_sector);
  1510. end_io:
  1511. bio_endio(bio, err);
  1512. break;
  1513. }
  1514. if (unlikely(nr_sectors > q->max_hw_sectors)) {
  1515. printk("bio too big device %s (%u > %u)\n",
  1516. bdevname(bio->bi_bdev, b),
  1517. bio_sectors(bio),
  1518. q->max_hw_sectors);
  1519. goto end_io;
  1520. }
  1521. if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
  1522. goto end_io;
  1523. if (should_fail_request(bio))
  1524. goto end_io;
  1525. /*
  1526. * If this device has partitions, remap block n
  1527. * of partition p to block n+start(p) of the disk.
  1528. */
  1529. blk_partition_remap(bio);
  1530. if (old_sector != -1)
  1531. blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
  1532. old_sector);
  1533. blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
  1534. old_sector = bio->bi_sector;
  1535. old_dev = bio->bi_bdev->bd_dev;
  1536. if (bio_check_eod(bio, nr_sectors))
  1537. goto end_io;
  1538. if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
  1539. err = -EOPNOTSUPP;
  1540. goto end_io;
  1541. }
  1542. ret = q->make_request_fn(q, bio);
  1543. } while (ret);
  1544. }
  1545. /*
  1546. * We only want one ->make_request_fn to be active at a time,
  1547. * else stack usage with stacked devices could be a problem.
  1548. * So use current->bio_{list,tail} to keep a list of requests
  1549. * submited by a make_request_fn function.
  1550. * current->bio_tail is also used as a flag to say if
  1551. * generic_make_request is currently active in this task or not.
  1552. * If it is NULL, then no make_request is active. If it is non-NULL,
  1553. * then a make_request is active, and new requests should be added
  1554. * at the tail
  1555. */
  1556. void generic_make_request(struct bio *bio)
  1557. {
  1558. if (current->bio_tail) {
  1559. /* make_request is active */
  1560. *(current->bio_tail) = bio;
  1561. bio->bi_next = NULL;
  1562. current->bio_tail = &bio->bi_next;
  1563. return;
  1564. }
  1565. /* following loop may be a bit non-obvious, and so deserves some
  1566. * explanation.
  1567. * Before entering the loop, bio->bi_next is NULL (as all callers
  1568. * ensure that) so we have a list with a single bio.
  1569. * We pretend that we have just taken it off a longer list, so
  1570. * we assign bio_list to the next (which is NULL) and bio_tail
  1571. * to &bio_list, thus initialising the bio_list of new bios to be
  1572. * added. __generic_make_request may indeed add some more bios
  1573. * through a recursive call to generic_make_request. If it
  1574. * did, we find a non-NULL value in bio_list and re-enter the loop
  1575. * from the top. In this case we really did just take the bio
  1576. * of the top of the list (no pretending) and so fixup bio_list and
  1577. * bio_tail or bi_next, and call into __generic_make_request again.
  1578. *
  1579. * The loop was structured like this to make only one call to
  1580. * __generic_make_request (which is important as it is large and
  1581. * inlined) and to keep the structure simple.
  1582. */
  1583. BUG_ON(bio->bi_next);
  1584. do {
  1585. current->bio_list = bio->bi_next;
  1586. if (bio->bi_next == NULL)
  1587. current->bio_tail = &current->bio_list;
  1588. else
  1589. bio->bi_next = NULL;
  1590. __generic_make_request(bio);
  1591. bio = current->bio_list;
  1592. } while (bio);
  1593. current->bio_tail = NULL; /* deactivate */
  1594. }
  1595. EXPORT_SYMBOL(generic_make_request);
  1596. /**
  1597. * submit_bio: submit a bio to the block device layer for I/O
  1598. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  1599. * @bio: The &struct bio which describes the I/O
  1600. *
  1601. * submit_bio() is very similar in purpose to generic_make_request(), and
  1602. * uses that function to do most of the work. Both are fairly rough
  1603. * interfaces, @bio must be presetup and ready for I/O.
  1604. *
  1605. */
  1606. void submit_bio(int rw, struct bio *bio)
  1607. {
  1608. int count = bio_sectors(bio);
  1609. bio->bi_rw |= rw;
  1610. /*
  1611. * If it's a regular read/write or a barrier with data attached,
  1612. * go through the normal accounting stuff before submission.
  1613. */
  1614. if (!bio_empty_barrier(bio)) {
  1615. BIO_BUG_ON(!bio->bi_size);
  1616. BIO_BUG_ON(!bio->bi_io_vec);
  1617. if (rw & WRITE) {
  1618. count_vm_events(PGPGOUT, count);
  1619. } else {
  1620. task_io_account_read(bio->bi_size);
  1621. count_vm_events(PGPGIN, count);
  1622. }
  1623. if (unlikely(block_dump)) {
  1624. char b[BDEVNAME_SIZE];
  1625. printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
  1626. current->comm, task_pid_nr(current),
  1627. (rw & WRITE) ? "WRITE" : "READ",
  1628. (unsigned long long)bio->bi_sector,
  1629. bdevname(bio->bi_bdev,b));
  1630. }
  1631. }
  1632. generic_make_request(bio);
  1633. }
  1634. EXPORT_SYMBOL(submit_bio);
  1635. static void blk_recalc_rq_sectors(struct request *rq, int nsect)
  1636. {
  1637. if (blk_fs_request(rq)) {
  1638. rq->hard_sector += nsect;
  1639. rq->hard_nr_sectors -= nsect;
  1640. /*
  1641. * Move the I/O submission pointers ahead if required.
  1642. */
  1643. if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
  1644. (rq->sector <= rq->hard_sector)) {
  1645. rq->sector = rq->hard_sector;
  1646. rq->nr_sectors = rq->hard_nr_sectors;
  1647. rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
  1648. rq->current_nr_sectors = rq->hard_cur_sectors;
  1649. rq->buffer = bio_data(rq->bio);
  1650. }
  1651. /*
  1652. * if total number of sectors is less than the first segment
  1653. * size, something has gone terribly wrong
  1654. */
  1655. if (rq->nr_sectors < rq->current_nr_sectors) {
  1656. printk("blk: request botched\n");
  1657. rq->nr_sectors = rq->current_nr_sectors;
  1658. }
  1659. }
  1660. }
  1661. /**
  1662. * __end_that_request_first - end I/O on a request
  1663. * @req: the request being processed
  1664. * @error: 0 for success, < 0 for error
  1665. * @nr_bytes: number of bytes to complete
  1666. *
  1667. * Description:
  1668. * Ends I/O on a number of bytes attached to @req, and sets it up
  1669. * for the next range of segments (if any) in the cluster.
  1670. *
  1671. * Return:
  1672. * 0 - we are done with this request, call end_that_request_last()
  1673. * 1 - still buffers pending for this request
  1674. **/
  1675. static int __end_that_request_first(struct request *req, int error,
  1676. int nr_bytes)
  1677. {
  1678. int total_bytes, bio_nbytes, next_idx = 0;
  1679. struct bio *bio;
  1680. blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
  1681. /*
  1682. * for a REQ_BLOCK_PC request, we want to carry any eventual
  1683. * sense key with us all the way through
  1684. */
  1685. if (!blk_pc_request(req))
  1686. req->errors = 0;
  1687. if (error) {
  1688. if (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))
  1689. printk("end_request: I/O error, dev %s, sector %llu\n",
  1690. req->rq_disk ? req->rq_disk->disk_name : "?",
  1691. (unsigned long long)req->sector);
  1692. }
  1693. if (blk_fs_request(req) && req->rq_disk) {
  1694. const int rw = rq_data_dir(req);
  1695. disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
  1696. }
  1697. total_bytes = bio_nbytes = 0;
  1698. while ((bio = req->bio) != NULL) {
  1699. int nbytes;
  1700. /*
  1701. * For an empty barrier request, the low level driver must
  1702. * store a potential error location in ->sector. We pass
  1703. * that back up in ->bi_sector.
  1704. */
  1705. if (blk_empty_barrier(req))
  1706. bio->bi_sector = req->sector;
  1707. if (nr_bytes >= bio->bi_size) {
  1708. req->bio = bio->bi_next;
  1709. nbytes = bio->bi_size;
  1710. req_bio_endio(req, bio, nbytes, error);
  1711. next_idx = 0;
  1712. bio_nbytes = 0;
  1713. } else {
  1714. int idx = bio->bi_idx + next_idx;
  1715. if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
  1716. blk_dump_rq_flags(req, "__end_that");
  1717. printk("%s: bio idx %d >= vcnt %d\n",
  1718. __FUNCTION__,
  1719. bio->bi_idx, bio->bi_vcnt);
  1720. break;
  1721. }
  1722. nbytes = bio_iovec_idx(bio, idx)->bv_len;
  1723. BIO_BUG_ON(nbytes > bio->bi_size);
  1724. /*
  1725. * not a complete bvec done
  1726. */
  1727. if (unlikely(nbytes > nr_bytes)) {
  1728. bio_nbytes += nr_bytes;
  1729. total_bytes += nr_bytes;
  1730. break;
  1731. }
  1732. /*
  1733. * advance to the next vector
  1734. */
  1735. next_idx++;
  1736. bio_nbytes += nbytes;
  1737. }
  1738. total_bytes += nbytes;
  1739. nr_bytes -= nbytes;
  1740. if ((bio = req->bio)) {
  1741. /*
  1742. * end more in this run, or just return 'not-done'
  1743. */
  1744. if (unlikely(nr_bytes <= 0))
  1745. break;
  1746. }
  1747. }
  1748. /*
  1749. * completely done
  1750. */
  1751. if (!req->bio)
  1752. return 0;
  1753. /*
  1754. * if the request wasn't completed, update state
  1755. */
  1756. if (bio_nbytes) {
  1757. req_bio_endio(req, bio, bio_nbytes, error);
  1758. bio->bi_idx += next_idx;
  1759. bio_iovec(bio)->bv_offset += nr_bytes;
  1760. bio_iovec(bio)->bv_len -= nr_bytes;
  1761. }
  1762. blk_recalc_rq_sectors(req, total_bytes >> 9);
  1763. blk_recalc_rq_segments(req);
  1764. return 1;
  1765. }
  1766. /*
  1767. * splice the completion data to a local structure and hand off to
  1768. * process_completion_queue() to complete the requests
  1769. */
  1770. static void blk_done_softirq(struct softirq_action *h)
  1771. {
  1772. struct list_head *cpu_list, local_list;
  1773. local_irq_disable();
  1774. cpu_list = &__get_cpu_var(blk_cpu_done);
  1775. list_replace_init(cpu_list, &local_list);
  1776. local_irq_enable();
  1777. while (!list_empty(&local_list)) {
  1778. struct request *rq = list_entry(local_list.next, struct request, donelist);
  1779. list_del_init(&rq->donelist);
  1780. rq->q->softirq_done_fn(rq);
  1781. }
  1782. }
  1783. static int __cpuinit blk_cpu_notify(struct notifier_block *self, unsigned long action,
  1784. void *hcpu)
  1785. {
  1786. /*
  1787. * If a CPU goes away, splice its entries to the current CPU
  1788. * and trigger a run of the softirq
  1789. */
  1790. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  1791. int cpu = (unsigned long) hcpu;
  1792. local_irq_disable();
  1793. list_splice_init(&per_cpu(blk_cpu_done, cpu),
  1794. &__get_cpu_var(blk_cpu_done));
  1795. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  1796. local_irq_enable();
  1797. }
  1798. return NOTIFY_OK;
  1799. }
  1800. static struct notifier_block blk_cpu_notifier __cpuinitdata = {
  1801. .notifier_call = blk_cpu_notify,
  1802. };
  1803. /**
  1804. * blk_complete_request - end I/O on a request
  1805. * @req: the request being processed
  1806. *
  1807. * Description:
  1808. * Ends all I/O on a request. It does not handle partial completions,
  1809. * unless the driver actually implements this in its completion callback
  1810. * through requeueing. The actual completion happens out-of-order,
  1811. * through a softirq handler. The user must have registered a completion
  1812. * callback through blk_queue_softirq_done().
  1813. **/
  1814. void blk_complete_request(struct request *req)
  1815. {
  1816. struct list_head *cpu_list;
  1817. unsigned long flags;
  1818. BUG_ON(!req->q->softirq_done_fn);
  1819. local_irq_save(flags);
  1820. cpu_list = &__get_cpu_var(blk_cpu_done);
  1821. list_add_tail(&req->donelist, cpu_list);
  1822. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  1823. local_irq_restore(flags);
  1824. }
  1825. EXPORT_SYMBOL(blk_complete_request);
  1826. /*
  1827. * queue lock must be held
  1828. */
  1829. static void end_that_request_last(struct request *req, int error)
  1830. {
  1831. struct gendisk *disk = req->rq_disk;
  1832. if (blk_rq_tagged(req))
  1833. blk_queue_end_tag(req->q, req);
  1834. if (blk_queued_rq(req))
  1835. blkdev_dequeue_request(req);
  1836. if (unlikely(laptop_mode) && blk_fs_request(req))
  1837. laptop_io_completion();
  1838. /*
  1839. * Account IO completion. bar_rq isn't accounted as a normal
  1840. * IO on queueing nor completion. Accounting the containing
  1841. * request is enough.
  1842. */
  1843. if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
  1844. unsigned long duration = jiffies - req->start_time;
  1845. const int rw = rq_data_dir(req);
  1846. __disk_stat_inc(disk, ios[rw]);
  1847. __disk_stat_add(disk, ticks[rw], duration);
  1848. disk_round_stats(disk);
  1849. disk->in_flight--;
  1850. }
  1851. if (req->end_io)
  1852. req->end_io(req, error);
  1853. else {
  1854. if (blk_bidi_rq(req))
  1855. __blk_put_request(req->next_rq->q, req->next_rq);
  1856. __blk_put_request(req->q, req);
  1857. }
  1858. }
  1859. static inline void __end_request(struct request *rq, int uptodate,
  1860. unsigned int nr_bytes)
  1861. {
  1862. int error = 0;
  1863. if (uptodate <= 0)
  1864. error = uptodate ? uptodate : -EIO;
  1865. __blk_end_request(rq, error, nr_bytes);
  1866. }
  1867. /**
  1868. * blk_rq_bytes - Returns bytes left to complete in the entire request
  1869. **/
  1870. unsigned int blk_rq_bytes(struct request *rq)
  1871. {
  1872. if (blk_fs_request(rq))
  1873. return rq->hard_nr_sectors << 9;
  1874. return rq->data_len;
  1875. }
  1876. EXPORT_SYMBOL_GPL(blk_rq_bytes);
  1877. /**
  1878. * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
  1879. **/
  1880. unsigned int blk_rq_cur_bytes(struct request *rq)
  1881. {
  1882. if (blk_fs_request(rq))
  1883. return rq->current_nr_sectors << 9;
  1884. if (rq->bio)
  1885. return rq->bio->bi_size;
  1886. return rq->data_len;
  1887. }
  1888. EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
  1889. /**
  1890. * end_queued_request - end all I/O on a queued request
  1891. * @rq: the request being processed
  1892. * @uptodate: error value or 0/1 uptodate flag
  1893. *
  1894. * Description:
  1895. * Ends all I/O on a request, and removes it from the block layer queues.
  1896. * Not suitable for normal IO completion, unless the driver still has
  1897. * the request attached to the block layer.
  1898. *
  1899. **/
  1900. void end_queued_request(struct request *rq, int uptodate)
  1901. {
  1902. __end_request(rq, uptodate, blk_rq_bytes(rq));
  1903. }
  1904. EXPORT_SYMBOL(end_queued_request);
  1905. /**
  1906. * end_dequeued_request - end all I/O on a dequeued request
  1907. * @rq: the request being processed
  1908. * @uptodate: error value or 0/1 uptodate flag
  1909. *
  1910. * Description:
  1911. * Ends all I/O on a request. The request must already have been
  1912. * dequeued using blkdev_dequeue_request(), as is normally the case
  1913. * for most drivers.
  1914. *
  1915. **/
  1916. void end_dequeued_request(struct request *rq, int uptodate)
  1917. {
  1918. __end_request(rq, uptodate, blk_rq_bytes(rq));
  1919. }
  1920. EXPORT_SYMBOL(end_dequeued_request);
  1921. /**
  1922. * end_request - end I/O on the current segment of the request
  1923. * @req: the request being processed
  1924. * @uptodate: error value or 0/1 uptodate flag
  1925. *
  1926. * Description:
  1927. * Ends I/O on the current segment of a request. If that is the only
  1928. * remaining segment, the request is also completed and freed.
  1929. *
  1930. * This is a remnant of how older block drivers handled IO completions.
  1931. * Modern drivers typically end IO on the full request in one go, unless
  1932. * they have a residual value to account for. For that case this function
  1933. * isn't really useful, unless the residual just happens to be the
  1934. * full current segment. In other words, don't use this function in new
  1935. * code. Either use end_request_completely(), or the
  1936. * end_that_request_chunk() (along with end_that_request_last()) for
  1937. * partial completions.
  1938. *
  1939. **/
  1940. void end_request(struct request *req, int uptodate)
  1941. {
  1942. __end_request(req, uptodate, req->hard_cur_sectors << 9);
  1943. }
  1944. EXPORT_SYMBOL(end_request);
  1945. /**
  1946. * blk_end_io - Generic end_io function to complete a request.
  1947. * @rq: the request being processed
  1948. * @error: 0 for success, < 0 for error
  1949. * @nr_bytes: number of bytes to complete @rq
  1950. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1951. * @drv_callback: function called between completion of bios in the request
  1952. * and completion of the request.
  1953. * If the callback returns non 0, this helper returns without
  1954. * completion of the request.
  1955. *
  1956. * Description:
  1957. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1958. * If @rq has leftover, sets it up for the next range of segments.
  1959. *
  1960. * Return:
  1961. * 0 - we are done with this request
  1962. * 1 - this request is not freed yet, it still has pending buffers.
  1963. **/
  1964. static int blk_end_io(struct request *rq, int error, int nr_bytes,
  1965. int bidi_bytes, int (drv_callback)(struct request *))
  1966. {
  1967. struct request_queue *q = rq->q;
  1968. unsigned long flags = 0UL;
  1969. if (blk_fs_request(rq) || blk_pc_request(rq)) {
  1970. if (__end_that_request_first(rq, error, nr_bytes))
  1971. return 1;
  1972. /* Bidi request must be completed as a whole */
  1973. if (blk_bidi_rq(rq) &&
  1974. __end_that_request_first(rq->next_rq, error, bidi_bytes))
  1975. return 1;
  1976. }
  1977. /* Special feature for tricky drivers */
  1978. if (drv_callback && drv_callback(rq))
  1979. return 1;
  1980. add_disk_randomness(rq->rq_disk);
  1981. spin_lock_irqsave(q->queue_lock, flags);
  1982. end_that_request_last(rq, error);
  1983. spin_unlock_irqrestore(q->queue_lock, flags);
  1984. return 0;
  1985. }
  1986. /**
  1987. * blk_end_request - Helper function for drivers to complete the request.
  1988. * @rq: the request being processed
  1989. * @error: 0 for success, < 0 for error
  1990. * @nr_bytes: number of bytes to complete
  1991. *
  1992. * Description:
  1993. * Ends I/O on a number of bytes attached to @rq.
  1994. * If @rq has leftover, sets it up for the next range of segments.
  1995. *
  1996. * Return:
  1997. * 0 - we are done with this request
  1998. * 1 - still buffers pending for this request
  1999. **/
  2000. int blk_end_request(struct request *rq, int error, int nr_bytes)
  2001. {
  2002. return blk_end_io(rq, error, nr_bytes, 0, NULL);
  2003. }
  2004. EXPORT_SYMBOL_GPL(blk_end_request);
  2005. /**
  2006. * __blk_end_request - Helper function for drivers to complete the request.
  2007. * @rq: the request being processed
  2008. * @error: 0 for success, < 0 for error
  2009. * @nr_bytes: number of bytes to complete
  2010. *
  2011. * Description:
  2012. * Must be called with queue lock held unlike blk_end_request().
  2013. *
  2014. * Return:
  2015. * 0 - we are done with this request
  2016. * 1 - still buffers pending for this request
  2017. **/
  2018. int __blk_end_request(struct request *rq, int error, int nr_bytes)
  2019. {
  2020. if (blk_fs_request(rq) || blk_pc_request(rq)) {
  2021. if (__end_that_request_first(rq, error, nr_bytes))
  2022. return 1;
  2023. }
  2024. add_disk_randomness(rq->rq_disk);
  2025. end_that_request_last(rq, error);
  2026. return 0;
  2027. }
  2028. EXPORT_SYMBOL_GPL(__blk_end_request);
  2029. /**
  2030. * blk_end_bidi_request - Helper function for drivers to complete bidi request.
  2031. * @rq: the bidi request being processed
  2032. * @error: 0 for success, < 0 for error
  2033. * @nr_bytes: number of bytes to complete @rq
  2034. * @bidi_bytes: number of bytes to complete @rq->next_rq
  2035. *
  2036. * Description:
  2037. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  2038. *
  2039. * Return:
  2040. * 0 - we are done with this request
  2041. * 1 - still buffers pending for this request
  2042. **/
  2043. int blk_end_bidi_request(struct request *rq, int error, int nr_bytes,
  2044. int bidi_bytes)
  2045. {
  2046. return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
  2047. }
  2048. EXPORT_SYMBOL_GPL(blk_end_bidi_request);
  2049. /**
  2050. * blk_end_request_callback - Special helper function for tricky drivers
  2051. * @rq: the request being processed
  2052. * @error: 0 for success, < 0 for error
  2053. * @nr_bytes: number of bytes to complete
  2054. * @drv_callback: function called between completion of bios in the request
  2055. * and completion of the request.
  2056. * If the callback returns non 0, this helper returns without
  2057. * completion of the request.
  2058. *
  2059. * Description:
  2060. * Ends I/O on a number of bytes attached to @rq.
  2061. * If @rq has leftover, sets it up for the next range of segments.
  2062. *
  2063. * This special helper function is used only for existing tricky drivers.
  2064. * (e.g. cdrom_newpc_intr() of ide-cd)
  2065. * This interface will be removed when such drivers are rewritten.
  2066. * Don't use this interface in other places anymore.
  2067. *
  2068. * Return:
  2069. * 0 - we are done with this request
  2070. * 1 - this request is not freed yet.
  2071. * this request still has pending buffers or
  2072. * the driver doesn't want to finish this request yet.
  2073. **/
  2074. int blk_end_request_callback(struct request *rq, int error, int nr_bytes,
  2075. int (drv_callback)(struct request *))
  2076. {
  2077. return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
  2078. }
  2079. EXPORT_SYMBOL_GPL(blk_end_request_callback);
  2080. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  2081. struct bio *bio)
  2082. {
  2083. /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
  2084. rq->cmd_flags |= (bio->bi_rw & 3);
  2085. rq->nr_phys_segments = bio_phys_segments(q, bio);
  2086. rq->nr_hw_segments = bio_hw_segments(q, bio);
  2087. rq->current_nr_sectors = bio_cur_sectors(bio);
  2088. rq->hard_cur_sectors = rq->current_nr_sectors;
  2089. rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
  2090. rq->buffer = bio_data(bio);
  2091. rq->data_len = bio->bi_size;
  2092. rq->bio = rq->biotail = bio;
  2093. if (bio->bi_bdev)
  2094. rq->rq_disk = bio->bi_bdev->bd_disk;
  2095. }
  2096. int kblockd_schedule_work(struct work_struct *work)
  2097. {
  2098. return queue_work(kblockd_workqueue, work);
  2099. }
  2100. EXPORT_SYMBOL(kblockd_schedule_work);
  2101. void kblockd_flush_work(struct work_struct *work)
  2102. {
  2103. cancel_work_sync(work);
  2104. }
  2105. EXPORT_SYMBOL(kblockd_flush_work);
  2106. int __init blk_dev_init(void)
  2107. {
  2108. int i;
  2109. kblockd_workqueue = create_workqueue("kblockd");
  2110. if (!kblockd_workqueue)
  2111. panic("Failed to create kblockd\n");
  2112. request_cachep = kmem_cache_create("blkdev_requests",
  2113. sizeof(struct request), 0, SLAB_PANIC, NULL);
  2114. blk_requestq_cachep = kmem_cache_create("blkdev_queue",
  2115. sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
  2116. for_each_possible_cpu(i)
  2117. INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
  2118. open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
  2119. register_hotcpu_notifier(&blk_cpu_notifier);
  2120. return 0;
  2121. }