blk-core.c 67 KB

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