blk-core.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  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);
  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. err = bdi_init(&q->backing_dev_info);
  431. if (err) {
  432. kmem_cache_free(blk_requestq_cachep, q);
  433. return NULL;
  434. }
  435. init_timer(&q->unplug_timer);
  436. setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
  437. INIT_LIST_HEAD(&q->timeout_list);
  438. INIT_WORK(&q->unplug_work, blk_unplug_work);
  439. kobject_init(&q->kobj, &blk_queue_ktype);
  440. mutex_init(&q->sysfs_lock);
  441. spin_lock_init(&q->__queue_lock);
  442. return q;
  443. }
  444. EXPORT_SYMBOL(blk_alloc_queue_node);
  445. /**
  446. * blk_init_queue - prepare a request queue for use with a block device
  447. * @rfn: The function to be called to process requests that have been
  448. * placed on the queue.
  449. * @lock: Request queue spin lock
  450. *
  451. * Description:
  452. * If a block device wishes to use the standard request handling procedures,
  453. * which sorts requests and coalesces adjacent requests, then it must
  454. * call blk_init_queue(). The function @rfn will be called when there
  455. * are requests on the queue that need to be processed. If the device
  456. * supports plugging, then @rfn may not be called immediately when requests
  457. * are available on the queue, but may be called at some time later instead.
  458. * Plugged queues are generally unplugged when a buffer belonging to one
  459. * of the requests on the queue is needed, or due to memory pressure.
  460. *
  461. * @rfn is not required, or even expected, to remove all requests off the
  462. * queue, but only as many as it can handle at a time. If it does leave
  463. * requests on the queue, it is responsible for arranging that the requests
  464. * get dealt with eventually.
  465. *
  466. * The queue spin lock must be held while manipulating the requests on the
  467. * request queue; this lock will be taken also from interrupt context, so irq
  468. * disabling is needed for it.
  469. *
  470. * Function returns a pointer to the initialized request queue, or %NULL if
  471. * it didn't succeed.
  472. *
  473. * Note:
  474. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  475. * when the block device is deactivated (such as at module unload).
  476. **/
  477. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  478. {
  479. return blk_init_queue_node(rfn, lock, -1);
  480. }
  481. EXPORT_SYMBOL(blk_init_queue);
  482. struct request_queue *
  483. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  484. {
  485. struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
  486. if (!q)
  487. return NULL;
  488. q->node = node_id;
  489. if (blk_init_free_list(q)) {
  490. kmem_cache_free(blk_requestq_cachep, q);
  491. return NULL;
  492. }
  493. /*
  494. * if caller didn't supply a lock, they get per-queue locking with
  495. * our embedded lock
  496. */
  497. if (!lock)
  498. lock = &q->__queue_lock;
  499. q->request_fn = rfn;
  500. q->prep_rq_fn = NULL;
  501. q->unplug_fn = generic_unplug_device;
  502. q->queue_flags = QUEUE_FLAG_DEFAULT;
  503. q->queue_lock = lock;
  504. /*
  505. * This also sets hw/phys segments, boundary and size
  506. */
  507. blk_queue_make_request(q, __make_request);
  508. q->sg_reserved_size = INT_MAX;
  509. /*
  510. * all done
  511. */
  512. if (!elevator_init(q, NULL)) {
  513. blk_queue_congestion_threshold(q);
  514. return q;
  515. }
  516. blk_put_queue(q);
  517. return NULL;
  518. }
  519. EXPORT_SYMBOL(blk_init_queue_node);
  520. int blk_get_queue(struct request_queue *q)
  521. {
  522. if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
  523. kobject_get(&q->kobj);
  524. return 0;
  525. }
  526. return 1;
  527. }
  528. static inline void blk_free_request(struct request_queue *q, struct request *rq)
  529. {
  530. if (rq->cmd_flags & REQ_ELVPRIV)
  531. elv_put_request(q, rq);
  532. mempool_free(rq, q->rq.rq_pool);
  533. }
  534. static struct request *
  535. blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
  536. {
  537. struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
  538. if (!rq)
  539. return NULL;
  540. blk_rq_init(q, rq);
  541. rq->cmd_flags = flags | REQ_ALLOCED;
  542. if (priv) {
  543. if (unlikely(elv_set_request(q, rq, gfp_mask))) {
  544. mempool_free(rq, q->rq.rq_pool);
  545. return NULL;
  546. }
  547. rq->cmd_flags |= REQ_ELVPRIV;
  548. }
  549. return rq;
  550. }
  551. /*
  552. * ioc_batching returns true if the ioc is a valid batching request and
  553. * should be given priority access to a request.
  554. */
  555. static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
  556. {
  557. if (!ioc)
  558. return 0;
  559. /*
  560. * Make sure the process is able to allocate at least 1 request
  561. * even if the batch times out, otherwise we could theoretically
  562. * lose wakeups.
  563. */
  564. return ioc->nr_batch_requests == q->nr_batching ||
  565. (ioc->nr_batch_requests > 0
  566. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  567. }
  568. /*
  569. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  570. * will cause the process to be a "batcher" on all queues in the system. This
  571. * is the behaviour we want though - once it gets a wakeup it should be given
  572. * a nice run.
  573. */
  574. static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
  575. {
  576. if (!ioc || ioc_batching(q, ioc))
  577. return;
  578. ioc->nr_batch_requests = q->nr_batching;
  579. ioc->last_waited = jiffies;
  580. }
  581. static void __freed_request(struct request_queue *q, int sync)
  582. {
  583. struct request_list *rl = &q->rq;
  584. if (rl->count[sync] < queue_congestion_off_threshold(q))
  585. blk_clear_queue_congested(q, sync);
  586. if (rl->count[sync] + 1 <= q->nr_requests) {
  587. if (waitqueue_active(&rl->wait[sync]))
  588. wake_up(&rl->wait[sync]);
  589. blk_clear_queue_full(q, sync);
  590. }
  591. }
  592. /*
  593. * A request has just been released. Account for it, update the full and
  594. * congestion status, wake up any waiters. Called under q->queue_lock.
  595. */
  596. static void freed_request(struct request_queue *q, int sync, int priv)
  597. {
  598. struct request_list *rl = &q->rq;
  599. rl->count[sync]--;
  600. if (priv)
  601. rl->elvpriv--;
  602. __freed_request(q, sync);
  603. if (unlikely(rl->starved[sync ^ 1]))
  604. __freed_request(q, sync ^ 1);
  605. }
  606. /*
  607. * Get a free request, queue_lock must be held.
  608. * Returns NULL on failure, with queue_lock held.
  609. * Returns !NULL on success, with queue_lock *not held*.
  610. */
  611. static struct request *get_request(struct request_queue *q, int rw_flags,
  612. struct bio *bio, gfp_t gfp_mask)
  613. {
  614. struct request *rq = NULL;
  615. struct request_list *rl = &q->rq;
  616. struct io_context *ioc = NULL;
  617. const bool is_sync = rw_is_sync(rw_flags) != 0;
  618. int may_queue, priv;
  619. may_queue = elv_may_queue(q, rw_flags);
  620. if (may_queue == ELV_MQUEUE_NO)
  621. goto rq_starved;
  622. if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
  623. if (rl->count[is_sync]+1 >= q->nr_requests) {
  624. ioc = current_io_context(GFP_ATOMIC, q->node);
  625. /*
  626. * The queue will fill after this allocation, so set
  627. * it as full, and mark this process as "batching".
  628. * This process will be allowed to complete a batch of
  629. * requests, others will be blocked.
  630. */
  631. if (!blk_queue_full(q, is_sync)) {
  632. ioc_set_batching(q, ioc);
  633. blk_set_queue_full(q, is_sync);
  634. } else {
  635. if (may_queue != ELV_MQUEUE_MUST
  636. && !ioc_batching(q, ioc)) {
  637. /*
  638. * The queue is full and the allocating
  639. * process is not a "batcher", and not
  640. * exempted by the IO scheduler
  641. */
  642. goto out;
  643. }
  644. }
  645. }
  646. blk_set_queue_congested(q, is_sync);
  647. }
  648. /*
  649. * Only allow batching queuers to allocate up to 50% over the defined
  650. * limit of requests, otherwise we could have thousands of requests
  651. * allocated with any setting of ->nr_requests
  652. */
  653. if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
  654. goto out;
  655. rl->count[is_sync]++;
  656. rl->starved[is_sync] = 0;
  657. priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
  658. if (priv)
  659. rl->elvpriv++;
  660. if (blk_queue_io_stat(q))
  661. rw_flags |= REQ_IO_STAT;
  662. spin_unlock_irq(q->queue_lock);
  663. rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
  664. if (unlikely(!rq)) {
  665. /*
  666. * Allocation failed presumably due to memory. Undo anything
  667. * we might have messed up.
  668. *
  669. * Allocating task should really be put onto the front of the
  670. * wait queue, but this is pretty rare.
  671. */
  672. spin_lock_irq(q->queue_lock);
  673. freed_request(q, is_sync, priv);
  674. /*
  675. * in the very unlikely event that allocation failed and no
  676. * requests for this direction was pending, mark us starved
  677. * so that freeing of a request in the other direction will
  678. * notice us. another possible fix would be to split the
  679. * rq mempool into READ and WRITE
  680. */
  681. rq_starved:
  682. if (unlikely(rl->count[is_sync] == 0))
  683. rl->starved[is_sync] = 1;
  684. goto out;
  685. }
  686. /*
  687. * ioc may be NULL here, and ioc_batching will be false. That's
  688. * OK, if the queue is under the request limit then requests need
  689. * not count toward the nr_batch_requests limit. There will always
  690. * be some limit enforced by BLK_BATCH_TIME.
  691. */
  692. if (ioc_batching(q, ioc))
  693. ioc->nr_batch_requests--;
  694. trace_block_getrq(q, bio, rw_flags & 1);
  695. out:
  696. return rq;
  697. }
  698. /*
  699. * No available requests for this queue, unplug the device and wait for some
  700. * requests to become available.
  701. *
  702. * Called with q->queue_lock held, and returns with it unlocked.
  703. */
  704. static struct request *get_request_wait(struct request_queue *q, int rw_flags,
  705. struct bio *bio)
  706. {
  707. const bool is_sync = rw_is_sync(rw_flags) != 0;
  708. struct request *rq;
  709. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  710. while (!rq) {
  711. DEFINE_WAIT(wait);
  712. struct io_context *ioc;
  713. struct request_list *rl = &q->rq;
  714. prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
  715. TASK_UNINTERRUPTIBLE);
  716. trace_block_sleeprq(q, bio, rw_flags & 1);
  717. __generic_unplug_device(q);
  718. spin_unlock_irq(q->queue_lock);
  719. io_schedule();
  720. /*
  721. * After sleeping, we become a "batching" process and
  722. * will be able to allocate at least one request, and
  723. * up to a big batch of them for a small period time.
  724. * See ioc_batching, ioc_set_batching
  725. */
  726. ioc = current_io_context(GFP_NOIO, q->node);
  727. ioc_set_batching(q, ioc);
  728. spin_lock_irq(q->queue_lock);
  729. finish_wait(&rl->wait[is_sync], &wait);
  730. rq = get_request(q, rw_flags, bio, GFP_NOIO);
  731. };
  732. return rq;
  733. }
  734. struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
  735. {
  736. struct request *rq;
  737. BUG_ON(rw != READ && rw != WRITE);
  738. spin_lock_irq(q->queue_lock);
  739. if (gfp_mask & __GFP_WAIT) {
  740. rq = get_request_wait(q, rw, NULL);
  741. } else {
  742. rq = get_request(q, rw, NULL, gfp_mask);
  743. if (!rq)
  744. spin_unlock_irq(q->queue_lock);
  745. }
  746. /* q->queue_lock is unlocked at this point */
  747. return rq;
  748. }
  749. EXPORT_SYMBOL(blk_get_request);
  750. /**
  751. * blk_make_request - given a bio, allocate a corresponding struct request.
  752. * @q: target request queue
  753. * @bio: The bio describing the memory mappings that will be submitted for IO.
  754. * It may be a chained-bio properly constructed by block/bio layer.
  755. * @gfp_mask: gfp flags to be used for memory allocation
  756. *
  757. * blk_make_request is the parallel of generic_make_request for BLOCK_PC
  758. * type commands. Where the struct request needs to be farther initialized by
  759. * the caller. It is passed a &struct bio, which describes the memory info of
  760. * the I/O transfer.
  761. *
  762. * The caller of blk_make_request must make sure that bi_io_vec
  763. * are set to describe the memory buffers. That bio_data_dir() will return
  764. * the needed direction of the request. (And all bio's in the passed bio-chain
  765. * are properly set accordingly)
  766. *
  767. * If called under none-sleepable conditions, mapped bio buffers must not
  768. * need bouncing, by calling the appropriate masked or flagged allocator,
  769. * suitable for the target device. Otherwise the call to blk_queue_bounce will
  770. * BUG.
  771. *
  772. * WARNING: When allocating/cloning a bio-chain, careful consideration should be
  773. * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
  774. * anything but the first bio in the chain. Otherwise you risk waiting for IO
  775. * completion of a bio that hasn't been submitted yet, thus resulting in a
  776. * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
  777. * of bio_alloc(), as that avoids the mempool deadlock.
  778. * If possible a big IO should be split into smaller parts when allocation
  779. * fails. Partial allocation should not be an error, or you risk a live-lock.
  780. */
  781. struct request *blk_make_request(struct request_queue *q, struct bio *bio,
  782. gfp_t gfp_mask)
  783. {
  784. struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
  785. if (unlikely(!rq))
  786. return ERR_PTR(-ENOMEM);
  787. for_each_bio(bio) {
  788. struct bio *bounce_bio = bio;
  789. int ret;
  790. blk_queue_bounce(q, &bounce_bio);
  791. ret = blk_rq_append_bio(q, rq, bounce_bio);
  792. if (unlikely(ret)) {
  793. blk_put_request(rq);
  794. return ERR_PTR(ret);
  795. }
  796. }
  797. return rq;
  798. }
  799. EXPORT_SYMBOL(blk_make_request);
  800. /**
  801. * blk_requeue_request - put a request back on queue
  802. * @q: request queue where request should be inserted
  803. * @rq: request to be inserted
  804. *
  805. * Description:
  806. * Drivers often keep queueing requests until the hardware cannot accept
  807. * more, when that condition happens we need to put the request back
  808. * on the queue. Must be called with queue lock held.
  809. */
  810. void blk_requeue_request(struct request_queue *q, struct request *rq)
  811. {
  812. blk_delete_timer(rq);
  813. blk_clear_rq_complete(rq);
  814. trace_block_rq_requeue(q, rq);
  815. if (blk_rq_tagged(rq))
  816. blk_queue_end_tag(q, rq);
  817. BUG_ON(blk_queued_rq(rq));
  818. elv_requeue_request(q, rq);
  819. }
  820. EXPORT_SYMBOL(blk_requeue_request);
  821. /**
  822. * blk_insert_request - insert a special request into a request queue
  823. * @q: request queue where request should be inserted
  824. * @rq: request to be inserted
  825. * @at_head: insert request at head or tail of queue
  826. * @data: private data
  827. *
  828. * Description:
  829. * Many block devices need to execute commands asynchronously, so they don't
  830. * block the whole kernel from preemption during request execution. This is
  831. * accomplished normally by inserting aritficial requests tagged as
  832. * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
  833. * be scheduled for actual execution by the request queue.
  834. *
  835. * We have the option of inserting the head or the tail of the queue.
  836. * Typically we use the tail for new ioctls and so forth. We use the head
  837. * of the queue for things like a QUEUE_FULL message from a device, or a
  838. * host that is unable to accept a particular command.
  839. */
  840. void blk_insert_request(struct request_queue *q, struct request *rq,
  841. int at_head, void *data)
  842. {
  843. int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
  844. unsigned long flags;
  845. /*
  846. * tell I/O scheduler that this isn't a regular read/write (ie it
  847. * must not attempt merges on this) and that it acts as a soft
  848. * barrier
  849. */
  850. rq->cmd_type = REQ_TYPE_SPECIAL;
  851. rq->special = data;
  852. spin_lock_irqsave(q->queue_lock, flags);
  853. /*
  854. * If command is tagged, release the tag
  855. */
  856. if (blk_rq_tagged(rq))
  857. blk_queue_end_tag(q, rq);
  858. drive_stat_acct(rq, 1);
  859. __elv_add_request(q, rq, where, 0);
  860. __blk_run_queue(q);
  861. spin_unlock_irqrestore(q->queue_lock, flags);
  862. }
  863. EXPORT_SYMBOL(blk_insert_request);
  864. /*
  865. * add-request adds a request to the linked list.
  866. * queue lock is held and interrupts disabled, as we muck with the
  867. * request queue list.
  868. */
  869. static inline void add_request(struct request_queue *q, struct request *req)
  870. {
  871. drive_stat_acct(req, 1);
  872. /*
  873. * elevator indicated where it wants this request to be
  874. * inserted at elevator_merge time
  875. */
  876. __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
  877. }
  878. static void part_round_stats_single(int cpu, struct hd_struct *part,
  879. unsigned long now)
  880. {
  881. if (now == part->stamp)
  882. return;
  883. if (part->in_flight) {
  884. __part_stat_add(cpu, part, time_in_queue,
  885. part->in_flight * (now - part->stamp));
  886. __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
  887. }
  888. part->stamp = now;
  889. }
  890. /**
  891. * part_round_stats() - Round off the performance stats on a struct disk_stats.
  892. * @cpu: cpu number for stats access
  893. * @part: target partition
  894. *
  895. * The average IO queue length and utilisation statistics are maintained
  896. * by observing the current state of the queue length and the amount of
  897. * time it has been in this state for.
  898. *
  899. * Normally, that accounting is done on IO completion, but that can result
  900. * in more than a second's worth of IO being accounted for within any one
  901. * second, leading to >100% utilisation. To deal with that, we call this
  902. * function to do a round-off before returning the results when reading
  903. * /proc/diskstats. This accounts immediately for all queue usage up to
  904. * the current jiffies and restarts the counters again.
  905. */
  906. void part_round_stats(int cpu, struct hd_struct *part)
  907. {
  908. unsigned long now = jiffies;
  909. if (part->partno)
  910. part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
  911. part_round_stats_single(cpu, part, now);
  912. }
  913. EXPORT_SYMBOL_GPL(part_round_stats);
  914. /*
  915. * queue lock must be held
  916. */
  917. void __blk_put_request(struct request_queue *q, struct request *req)
  918. {
  919. if (unlikely(!q))
  920. return;
  921. if (unlikely(--req->ref_count))
  922. return;
  923. elv_completed_request(q, req);
  924. /* this is a bio leak */
  925. WARN_ON(req->bio != NULL);
  926. /*
  927. * Request may not have originated from ll_rw_blk. if not,
  928. * it didn't come out of our reserved rq pools
  929. */
  930. if (req->cmd_flags & REQ_ALLOCED) {
  931. int is_sync = rq_is_sync(req) != 0;
  932. int priv = req->cmd_flags & REQ_ELVPRIV;
  933. BUG_ON(!list_empty(&req->queuelist));
  934. BUG_ON(!hlist_unhashed(&req->hash));
  935. blk_free_request(q, req);
  936. freed_request(q, is_sync, priv);
  937. }
  938. }
  939. EXPORT_SYMBOL_GPL(__blk_put_request);
  940. void blk_put_request(struct request *req)
  941. {
  942. unsigned long flags;
  943. struct request_queue *q = req->q;
  944. spin_lock_irqsave(q->queue_lock, flags);
  945. __blk_put_request(q, req);
  946. spin_unlock_irqrestore(q->queue_lock, flags);
  947. }
  948. EXPORT_SYMBOL(blk_put_request);
  949. void init_request_from_bio(struct request *req, struct bio *bio)
  950. {
  951. req->cpu = bio->bi_comp_cpu;
  952. req->cmd_type = REQ_TYPE_FS;
  953. /*
  954. * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
  955. */
  956. if (bio_rw_ahead(bio))
  957. req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  958. REQ_FAILFAST_DRIVER);
  959. if (bio_failfast_dev(bio))
  960. req->cmd_flags |= REQ_FAILFAST_DEV;
  961. if (bio_failfast_transport(bio))
  962. req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  963. if (bio_failfast_driver(bio))
  964. req->cmd_flags |= REQ_FAILFAST_DRIVER;
  965. if (unlikely(bio_discard(bio))) {
  966. req->cmd_flags |= REQ_DISCARD;
  967. if (bio_barrier(bio))
  968. req->cmd_flags |= REQ_SOFTBARRIER;
  969. req->q->prepare_discard_fn(req->q, req);
  970. } else if (unlikely(bio_barrier(bio)))
  971. req->cmd_flags |= REQ_HARDBARRIER;
  972. if (bio_sync(bio))
  973. req->cmd_flags |= REQ_RW_SYNC;
  974. if (bio_rw_meta(bio))
  975. req->cmd_flags |= REQ_RW_META;
  976. if (bio_noidle(bio))
  977. req->cmd_flags |= REQ_NOIDLE;
  978. req->errors = 0;
  979. req->__sector = bio->bi_sector;
  980. req->ioprio = bio_prio(bio);
  981. blk_rq_bio_prep(req->q, req, bio);
  982. }
  983. /*
  984. * Only disabling plugging for non-rotational devices if it does tagging
  985. * as well, otherwise we do need the proper merging
  986. */
  987. static inline bool queue_should_plug(struct request_queue *q)
  988. {
  989. return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
  990. }
  991. static int __make_request(struct request_queue *q, struct bio *bio)
  992. {
  993. struct request *req;
  994. int el_ret;
  995. unsigned int bytes = bio->bi_size;
  996. const unsigned short prio = bio_prio(bio);
  997. const int sync = bio_sync(bio);
  998. const int unplug = bio_unplug(bio);
  999. int rw_flags;
  1000. if (bio_barrier(bio) && bio_has_data(bio) &&
  1001. (q->next_ordered == QUEUE_ORDERED_NONE)) {
  1002. bio_endio(bio, -EOPNOTSUPP);
  1003. return 0;
  1004. }
  1005. /*
  1006. * low level driver can indicate that it wants pages above a
  1007. * certain limit bounced to low memory (ie for highmem, or even
  1008. * ISA dma in theory)
  1009. */
  1010. blk_queue_bounce(q, &bio);
  1011. spin_lock_irq(q->queue_lock);
  1012. if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
  1013. goto get_rq;
  1014. el_ret = elv_merge(q, &req, bio);
  1015. switch (el_ret) {
  1016. case ELEVATOR_BACK_MERGE:
  1017. BUG_ON(!rq_mergeable(req));
  1018. if (!ll_back_merge_fn(q, req, bio))
  1019. break;
  1020. trace_block_bio_backmerge(q, bio);
  1021. req->biotail->bi_next = bio;
  1022. req->biotail = bio;
  1023. req->__data_len += bytes;
  1024. req->ioprio = ioprio_best(req->ioprio, prio);
  1025. if (!blk_rq_cpu_valid(req))
  1026. req->cpu = bio->bi_comp_cpu;
  1027. drive_stat_acct(req, 0);
  1028. if (!attempt_back_merge(q, req))
  1029. elv_merged_request(q, req, el_ret);
  1030. goto out;
  1031. case ELEVATOR_FRONT_MERGE:
  1032. BUG_ON(!rq_mergeable(req));
  1033. if (!ll_front_merge_fn(q, req, bio))
  1034. break;
  1035. trace_block_bio_frontmerge(q, bio);
  1036. bio->bi_next = req->bio;
  1037. req->bio = bio;
  1038. /*
  1039. * may not be valid. if the low level driver said
  1040. * it didn't need a bounce buffer then it better
  1041. * not touch req->buffer either...
  1042. */
  1043. req->buffer = bio_data(bio);
  1044. req->__sector = bio->bi_sector;
  1045. req->__data_len += bytes;
  1046. req->ioprio = ioprio_best(req->ioprio, prio);
  1047. if (!blk_rq_cpu_valid(req))
  1048. req->cpu = bio->bi_comp_cpu;
  1049. drive_stat_acct(req, 0);
  1050. if (!attempt_front_merge(q, req))
  1051. elv_merged_request(q, req, el_ret);
  1052. goto out;
  1053. /* ELV_NO_MERGE: elevator says don't/can't merge. */
  1054. default:
  1055. ;
  1056. }
  1057. get_rq:
  1058. /*
  1059. * This sync check and mask will be re-done in init_request_from_bio(),
  1060. * but we need to set it earlier to expose the sync flag to the
  1061. * rq allocator and io schedulers.
  1062. */
  1063. rw_flags = bio_data_dir(bio);
  1064. if (sync)
  1065. rw_flags |= REQ_RW_SYNC;
  1066. /*
  1067. * Grab a free request. This is might sleep but can not fail.
  1068. * Returns with the queue unlocked.
  1069. */
  1070. req = get_request_wait(q, rw_flags, bio);
  1071. /*
  1072. * After dropping the lock and possibly sleeping here, our request
  1073. * may now be mergeable after it had proven unmergeable (above).
  1074. * We don't worry about that case for efficiency. It won't happen
  1075. * often, and the elevators are able to handle it.
  1076. */
  1077. init_request_from_bio(req, bio);
  1078. spin_lock_irq(q->queue_lock);
  1079. if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
  1080. bio_flagged(bio, BIO_CPU_AFFINE))
  1081. req->cpu = blk_cpu_to_group(smp_processor_id());
  1082. if (queue_should_plug(q) && elv_queue_empty(q))
  1083. blk_plug_device(q);
  1084. add_request(q, req);
  1085. out:
  1086. if (unplug || !queue_should_plug(q))
  1087. __generic_unplug_device(q);
  1088. spin_unlock_irq(q->queue_lock);
  1089. return 0;
  1090. }
  1091. /*
  1092. * If bio->bi_dev is a partition, remap the location
  1093. */
  1094. static inline void blk_partition_remap(struct bio *bio)
  1095. {
  1096. struct block_device *bdev = bio->bi_bdev;
  1097. if (bio_sectors(bio) && bdev != bdev->bd_contains) {
  1098. struct hd_struct *p = bdev->bd_part;
  1099. bio->bi_sector += p->start_sect;
  1100. bio->bi_bdev = bdev->bd_contains;
  1101. trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
  1102. bdev->bd_dev,
  1103. bio->bi_sector - p->start_sect);
  1104. }
  1105. }
  1106. static void handle_bad_sector(struct bio *bio)
  1107. {
  1108. char b[BDEVNAME_SIZE];
  1109. printk(KERN_INFO "attempt to access beyond end of device\n");
  1110. printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
  1111. bdevname(bio->bi_bdev, b),
  1112. bio->bi_rw,
  1113. (unsigned long long)bio->bi_sector + bio_sectors(bio),
  1114. (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
  1115. set_bit(BIO_EOF, &bio->bi_flags);
  1116. }
  1117. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1118. static DECLARE_FAULT_ATTR(fail_make_request);
  1119. static int __init setup_fail_make_request(char *str)
  1120. {
  1121. return setup_fault_attr(&fail_make_request, str);
  1122. }
  1123. __setup("fail_make_request=", setup_fail_make_request);
  1124. static int should_fail_request(struct bio *bio)
  1125. {
  1126. struct hd_struct *part = bio->bi_bdev->bd_part;
  1127. if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
  1128. return should_fail(&fail_make_request, bio->bi_size);
  1129. return 0;
  1130. }
  1131. static int __init fail_make_request_debugfs(void)
  1132. {
  1133. return init_fault_attr_dentries(&fail_make_request,
  1134. "fail_make_request");
  1135. }
  1136. late_initcall(fail_make_request_debugfs);
  1137. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1138. static inline int should_fail_request(struct bio *bio)
  1139. {
  1140. return 0;
  1141. }
  1142. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1143. /*
  1144. * Check whether this bio extends beyond the end of the device.
  1145. */
  1146. static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
  1147. {
  1148. sector_t maxsector;
  1149. if (!nr_sectors)
  1150. return 0;
  1151. /* Test device or partition size, when known. */
  1152. maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
  1153. if (maxsector) {
  1154. sector_t sector = bio->bi_sector;
  1155. if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
  1156. /*
  1157. * This may well happen - the kernel calls bread()
  1158. * without checking the size of the device, e.g., when
  1159. * mounting a device.
  1160. */
  1161. handle_bad_sector(bio);
  1162. return 1;
  1163. }
  1164. }
  1165. return 0;
  1166. }
  1167. /**
  1168. * generic_make_request - hand a buffer to its device driver for I/O
  1169. * @bio: The bio describing the location in memory and on the device.
  1170. *
  1171. * generic_make_request() is used to make I/O requests of block
  1172. * devices. It is passed a &struct bio, which describes the I/O that needs
  1173. * to be done.
  1174. *
  1175. * generic_make_request() does not return any status. The
  1176. * success/failure status of the request, along with notification of
  1177. * completion, is delivered asynchronously through the bio->bi_end_io
  1178. * function described (one day) else where.
  1179. *
  1180. * The caller of generic_make_request must make sure that bi_io_vec
  1181. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  1182. * set to describe the device address, and the
  1183. * bi_end_io and optionally bi_private are set to describe how
  1184. * completion notification should be signaled.
  1185. *
  1186. * generic_make_request and the drivers it calls may use bi_next if this
  1187. * bio happens to be merged with someone else, and may change bi_dev and
  1188. * bi_sector for remaps as it sees fit. So the values of these fields
  1189. * should NOT be depended on after the call to generic_make_request.
  1190. */
  1191. static inline void __generic_make_request(struct bio *bio)
  1192. {
  1193. struct request_queue *q;
  1194. sector_t old_sector;
  1195. int ret, nr_sectors = bio_sectors(bio);
  1196. dev_t old_dev;
  1197. int err = -EIO;
  1198. might_sleep();
  1199. if (bio_check_eod(bio, nr_sectors))
  1200. goto end_io;
  1201. /*
  1202. * Resolve the mapping until finished. (drivers are
  1203. * still free to implement/resolve their own stacking
  1204. * by explicitly returning 0)
  1205. *
  1206. * NOTE: we don't repeat the blk_size check for each new device.
  1207. * Stacking drivers are expected to know what they are doing.
  1208. */
  1209. old_sector = -1;
  1210. old_dev = 0;
  1211. do {
  1212. char b[BDEVNAME_SIZE];
  1213. q = bdev_get_queue(bio->bi_bdev);
  1214. if (unlikely(!q)) {
  1215. printk(KERN_ERR
  1216. "generic_make_request: Trying to access "
  1217. "nonexistent block-device %s (%Lu)\n",
  1218. bdevname(bio->bi_bdev, b),
  1219. (long long) bio->bi_sector);
  1220. goto end_io;
  1221. }
  1222. if (unlikely(nr_sectors > queue_max_hw_sectors(q))) {
  1223. printk(KERN_ERR "bio too big device %s (%u > %u)\n",
  1224. bdevname(bio->bi_bdev, b),
  1225. bio_sectors(bio),
  1226. queue_max_hw_sectors(q));
  1227. goto end_io;
  1228. }
  1229. if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
  1230. goto end_io;
  1231. if (should_fail_request(bio))
  1232. goto end_io;
  1233. /*
  1234. * If this device has partitions, remap block n
  1235. * of partition p to block n+start(p) of the disk.
  1236. */
  1237. blk_partition_remap(bio);
  1238. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
  1239. goto end_io;
  1240. if (old_sector != -1)
  1241. trace_block_remap(q, bio, old_dev, old_sector);
  1242. trace_block_bio_queue(q, bio);
  1243. old_sector = bio->bi_sector;
  1244. old_dev = bio->bi_bdev->bd_dev;
  1245. if (bio_check_eod(bio, nr_sectors))
  1246. goto end_io;
  1247. if (bio_discard(bio) && !q->prepare_discard_fn) {
  1248. err = -EOPNOTSUPP;
  1249. goto end_io;
  1250. }
  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. static void blk_account_io_completion(struct request *req, unsigned int bytes)
  1416. {
  1417. if (blk_do_io_stat(req)) {
  1418. const int rw = rq_data_dir(req);
  1419. struct hd_struct *part;
  1420. int cpu;
  1421. cpu = part_stat_lock();
  1422. part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
  1423. part_stat_add(cpu, part, sectors[rw], bytes >> 9);
  1424. part_stat_unlock();
  1425. }
  1426. }
  1427. static void blk_account_io_done(struct request *req)
  1428. {
  1429. /*
  1430. * Account IO completion. bar_rq isn't accounted as a normal
  1431. * IO on queueing nor completion. Accounting the containing
  1432. * request is enough.
  1433. */
  1434. if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
  1435. unsigned long duration = jiffies - req->start_time;
  1436. const int rw = rq_data_dir(req);
  1437. struct hd_struct *part;
  1438. int cpu;
  1439. cpu = part_stat_lock();
  1440. part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
  1441. part_stat_inc(cpu, part, ios[rw]);
  1442. part_stat_add(cpu, part, ticks[rw], duration);
  1443. part_round_stats(cpu, part);
  1444. part_dec_in_flight(part);
  1445. part_stat_unlock();
  1446. }
  1447. }
  1448. /**
  1449. * blk_peek_request - peek at the top of a request queue
  1450. * @q: request queue to peek at
  1451. *
  1452. * Description:
  1453. * Return the request at the top of @q. The returned request
  1454. * should be started using blk_start_request() before LLD starts
  1455. * processing it.
  1456. *
  1457. * Return:
  1458. * Pointer to the request at the top of @q if available. Null
  1459. * otherwise.
  1460. *
  1461. * Context:
  1462. * queue_lock must be held.
  1463. */
  1464. struct request *blk_peek_request(struct request_queue *q)
  1465. {
  1466. struct request *rq;
  1467. int ret;
  1468. while ((rq = __elv_next_request(q)) != NULL) {
  1469. if (!(rq->cmd_flags & REQ_STARTED)) {
  1470. /*
  1471. * This is the first time the device driver
  1472. * sees this request (possibly after
  1473. * requeueing). Notify IO scheduler.
  1474. */
  1475. if (blk_sorted_rq(rq))
  1476. elv_activate_rq(q, rq);
  1477. /*
  1478. * just mark as started even if we don't start
  1479. * it, a request that has been delayed should
  1480. * not be passed by new incoming requests
  1481. */
  1482. rq->cmd_flags |= REQ_STARTED;
  1483. trace_block_rq_issue(q, rq);
  1484. }
  1485. if (!q->boundary_rq || q->boundary_rq == rq) {
  1486. q->end_sector = rq_end_sector(rq);
  1487. q->boundary_rq = NULL;
  1488. }
  1489. if (rq->cmd_flags & REQ_DONTPREP)
  1490. break;
  1491. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  1492. /*
  1493. * make sure space for the drain appears we
  1494. * know we can do this because max_hw_segments
  1495. * has been adjusted to be one fewer than the
  1496. * device can handle
  1497. */
  1498. rq->nr_phys_segments++;
  1499. }
  1500. if (!q->prep_rq_fn)
  1501. break;
  1502. ret = q->prep_rq_fn(q, rq);
  1503. if (ret == BLKPREP_OK) {
  1504. break;
  1505. } else if (ret == BLKPREP_DEFER) {
  1506. /*
  1507. * the request may have been (partially) prepped.
  1508. * we need to keep this request in the front to
  1509. * avoid resource deadlock. REQ_STARTED will
  1510. * prevent other fs requests from passing this one.
  1511. */
  1512. if (q->dma_drain_size && blk_rq_bytes(rq) &&
  1513. !(rq->cmd_flags & REQ_DONTPREP)) {
  1514. /*
  1515. * remove the space for the drain we added
  1516. * so that we don't add it again
  1517. */
  1518. --rq->nr_phys_segments;
  1519. }
  1520. rq = NULL;
  1521. break;
  1522. } else if (ret == BLKPREP_KILL) {
  1523. rq->cmd_flags |= REQ_QUIET;
  1524. /*
  1525. * Mark this request as started so we don't trigger
  1526. * any debug logic in the end I/O path.
  1527. */
  1528. blk_start_request(rq);
  1529. __blk_end_request_all(rq, -EIO);
  1530. } else {
  1531. printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
  1532. break;
  1533. }
  1534. }
  1535. return rq;
  1536. }
  1537. EXPORT_SYMBOL(blk_peek_request);
  1538. void blk_dequeue_request(struct request *rq)
  1539. {
  1540. struct request_queue *q = rq->q;
  1541. BUG_ON(list_empty(&rq->queuelist));
  1542. BUG_ON(ELV_ON_HASH(rq));
  1543. list_del_init(&rq->queuelist);
  1544. /*
  1545. * the time frame between a request being removed from the lists
  1546. * and to it is freed is accounted as io that is in progress at
  1547. * the driver side.
  1548. */
  1549. if (blk_account_rq(rq))
  1550. q->in_flight[rq_is_sync(rq)]++;
  1551. }
  1552. /**
  1553. * blk_start_request - start request processing on the driver
  1554. * @req: request to dequeue
  1555. *
  1556. * Description:
  1557. * Dequeue @req and start timeout timer on it. This hands off the
  1558. * request to the driver.
  1559. *
  1560. * Block internal functions which don't want to start timer should
  1561. * call blk_dequeue_request().
  1562. *
  1563. * Context:
  1564. * queue_lock must be held.
  1565. */
  1566. void blk_start_request(struct request *req)
  1567. {
  1568. blk_dequeue_request(req);
  1569. /*
  1570. * We are now handing the request to the hardware, initialize
  1571. * resid_len to full count and add the timeout handler.
  1572. */
  1573. req->resid_len = blk_rq_bytes(req);
  1574. if (unlikely(blk_bidi_rq(req)))
  1575. req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
  1576. blk_add_timer(req);
  1577. }
  1578. EXPORT_SYMBOL(blk_start_request);
  1579. /**
  1580. * blk_fetch_request - fetch a request from a request queue
  1581. * @q: request queue to fetch a request from
  1582. *
  1583. * Description:
  1584. * Return the request at the top of @q. The request is started on
  1585. * return and LLD can start processing it immediately.
  1586. *
  1587. * Return:
  1588. * Pointer to the request at the top of @q if available. Null
  1589. * otherwise.
  1590. *
  1591. * Context:
  1592. * queue_lock must be held.
  1593. */
  1594. struct request *blk_fetch_request(struct request_queue *q)
  1595. {
  1596. struct request *rq;
  1597. rq = blk_peek_request(q);
  1598. if (rq)
  1599. blk_start_request(rq);
  1600. return rq;
  1601. }
  1602. EXPORT_SYMBOL(blk_fetch_request);
  1603. /**
  1604. * blk_update_request - Special helper function for request stacking drivers
  1605. * @req: the request being processed
  1606. * @error: %0 for success, < %0 for error
  1607. * @nr_bytes: number of bytes to complete @req
  1608. *
  1609. * Description:
  1610. * Ends I/O on a number of bytes attached to @req, but doesn't complete
  1611. * the request structure even if @req doesn't have leftover.
  1612. * If @req has leftover, sets it up for the next range of segments.
  1613. *
  1614. * This special helper function is only for request stacking drivers
  1615. * (e.g. request-based dm) so that they can handle partial completion.
  1616. * Actual device drivers should use blk_end_request instead.
  1617. *
  1618. * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
  1619. * %false return from this function.
  1620. *
  1621. * Return:
  1622. * %false - this request doesn't have any more data
  1623. * %true - this request has more data
  1624. **/
  1625. bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
  1626. {
  1627. int total_bytes, bio_nbytes, next_idx = 0;
  1628. struct bio *bio;
  1629. if (!req->bio)
  1630. return false;
  1631. trace_block_rq_complete(req->q, req);
  1632. /*
  1633. * For fs requests, rq is just carrier of independent bio's
  1634. * and each partial completion should be handled separately.
  1635. * Reset per-request error on each partial completion.
  1636. *
  1637. * TODO: tj: This is too subtle. It would be better to let
  1638. * low level drivers do what they see fit.
  1639. */
  1640. if (blk_fs_request(req))
  1641. req->errors = 0;
  1642. if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
  1643. printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
  1644. req->rq_disk ? req->rq_disk->disk_name : "?",
  1645. (unsigned long long)blk_rq_pos(req));
  1646. }
  1647. blk_account_io_completion(req, nr_bytes);
  1648. total_bytes = bio_nbytes = 0;
  1649. while ((bio = req->bio) != NULL) {
  1650. int nbytes;
  1651. if (nr_bytes >= bio->bi_size) {
  1652. req->bio = bio->bi_next;
  1653. nbytes = bio->bi_size;
  1654. req_bio_endio(req, bio, nbytes, error);
  1655. next_idx = 0;
  1656. bio_nbytes = 0;
  1657. } else {
  1658. int idx = bio->bi_idx + next_idx;
  1659. if (unlikely(idx >= bio->bi_vcnt)) {
  1660. blk_dump_rq_flags(req, "__end_that");
  1661. printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
  1662. __func__, idx, bio->bi_vcnt);
  1663. break;
  1664. }
  1665. nbytes = bio_iovec_idx(bio, idx)->bv_len;
  1666. BIO_BUG_ON(nbytes > bio->bi_size);
  1667. /*
  1668. * not a complete bvec done
  1669. */
  1670. if (unlikely(nbytes > nr_bytes)) {
  1671. bio_nbytes += nr_bytes;
  1672. total_bytes += nr_bytes;
  1673. break;
  1674. }
  1675. /*
  1676. * advance to the next vector
  1677. */
  1678. next_idx++;
  1679. bio_nbytes += nbytes;
  1680. }
  1681. total_bytes += nbytes;
  1682. nr_bytes -= nbytes;
  1683. bio = req->bio;
  1684. if (bio) {
  1685. /*
  1686. * end more in this run, or just return 'not-done'
  1687. */
  1688. if (unlikely(nr_bytes <= 0))
  1689. break;
  1690. }
  1691. }
  1692. /*
  1693. * completely done
  1694. */
  1695. if (!req->bio) {
  1696. /*
  1697. * Reset counters so that the request stacking driver
  1698. * can find how many bytes remain in the request
  1699. * later.
  1700. */
  1701. req->__data_len = 0;
  1702. return false;
  1703. }
  1704. /*
  1705. * if the request wasn't completed, update state
  1706. */
  1707. if (bio_nbytes) {
  1708. req_bio_endio(req, bio, bio_nbytes, error);
  1709. bio->bi_idx += next_idx;
  1710. bio_iovec(bio)->bv_offset += nr_bytes;
  1711. bio_iovec(bio)->bv_len -= nr_bytes;
  1712. }
  1713. req->__data_len -= total_bytes;
  1714. req->buffer = bio_data(req->bio);
  1715. /* update sector only for requests with clear definition of sector */
  1716. if (blk_fs_request(req) || blk_discard_rq(req))
  1717. req->__sector += total_bytes >> 9;
  1718. /*
  1719. * If total number of sectors is less than the first segment
  1720. * size, something has gone terribly wrong.
  1721. */
  1722. if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
  1723. printk(KERN_ERR "blk: request botched\n");
  1724. req->__data_len = blk_rq_cur_bytes(req);
  1725. }
  1726. /* recalculate the number of segments */
  1727. blk_recalc_rq_segments(req);
  1728. return true;
  1729. }
  1730. EXPORT_SYMBOL_GPL(blk_update_request);
  1731. static bool blk_update_bidi_request(struct request *rq, int error,
  1732. unsigned int nr_bytes,
  1733. unsigned int bidi_bytes)
  1734. {
  1735. if (blk_update_request(rq, error, nr_bytes))
  1736. return true;
  1737. /* Bidi request must be completed as a whole */
  1738. if (unlikely(blk_bidi_rq(rq)) &&
  1739. blk_update_request(rq->next_rq, error, bidi_bytes))
  1740. return true;
  1741. add_disk_randomness(rq->rq_disk);
  1742. return false;
  1743. }
  1744. /*
  1745. * queue lock must be held
  1746. */
  1747. static void blk_finish_request(struct request *req, int error)
  1748. {
  1749. if (blk_rq_tagged(req))
  1750. blk_queue_end_tag(req->q, req);
  1751. BUG_ON(blk_queued_rq(req));
  1752. if (unlikely(laptop_mode) && blk_fs_request(req))
  1753. laptop_io_completion();
  1754. blk_delete_timer(req);
  1755. blk_account_io_done(req);
  1756. if (req->end_io)
  1757. req->end_io(req, error);
  1758. else {
  1759. if (blk_bidi_rq(req))
  1760. __blk_put_request(req->next_rq->q, req->next_rq);
  1761. __blk_put_request(req->q, req);
  1762. }
  1763. }
  1764. /**
  1765. * blk_end_bidi_request - Complete a bidi request
  1766. * @rq: the request to complete
  1767. * @error: %0 for success, < %0 for error
  1768. * @nr_bytes: number of bytes to complete @rq
  1769. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1770. *
  1771. * Description:
  1772. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  1773. * Drivers that supports bidi can safely call this member for any
  1774. * type of request, bidi or uni. In the later case @bidi_bytes is
  1775. * just ignored.
  1776. *
  1777. * Return:
  1778. * %false - we are done with this request
  1779. * %true - still buffers pending for this request
  1780. **/
  1781. static bool blk_end_bidi_request(struct request *rq, int error,
  1782. unsigned int nr_bytes, unsigned int bidi_bytes)
  1783. {
  1784. struct request_queue *q = rq->q;
  1785. unsigned long flags;
  1786. if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
  1787. return true;
  1788. spin_lock_irqsave(q->queue_lock, flags);
  1789. blk_finish_request(rq, error);
  1790. spin_unlock_irqrestore(q->queue_lock, flags);
  1791. return false;
  1792. }
  1793. /**
  1794. * __blk_end_bidi_request - Complete a bidi request with queue lock held
  1795. * @rq: the request to complete
  1796. * @error: %0 for success, < %0 for error
  1797. * @nr_bytes: number of bytes to complete @rq
  1798. * @bidi_bytes: number of bytes to complete @rq->next_rq
  1799. *
  1800. * Description:
  1801. * Identical to blk_end_bidi_request() except that queue lock is
  1802. * assumed to be locked on entry and remains so on return.
  1803. *
  1804. * Return:
  1805. * %false - we are done with this request
  1806. * %true - still buffers pending for this request
  1807. **/
  1808. static bool __blk_end_bidi_request(struct request *rq, int error,
  1809. unsigned int nr_bytes, unsigned int bidi_bytes)
  1810. {
  1811. if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
  1812. return true;
  1813. blk_finish_request(rq, error);
  1814. return false;
  1815. }
  1816. /**
  1817. * blk_end_request - Helper function for drivers to complete the request.
  1818. * @rq: the request being processed
  1819. * @error: %0 for success, < %0 for error
  1820. * @nr_bytes: number of bytes to complete
  1821. *
  1822. * Description:
  1823. * Ends I/O on a number of bytes attached to @rq.
  1824. * If @rq has leftover, sets it up for the next range of segments.
  1825. *
  1826. * Return:
  1827. * %false - we are done with this request
  1828. * %true - still buffers pending for this request
  1829. **/
  1830. bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1831. {
  1832. return blk_end_bidi_request(rq, error, nr_bytes, 0);
  1833. }
  1834. EXPORT_SYMBOL_GPL(blk_end_request);
  1835. /**
  1836. * blk_end_request_all - Helper function for drives to finish the request.
  1837. * @rq: the request to finish
  1838. * @error: %0 for success, < %0 for error
  1839. *
  1840. * Description:
  1841. * Completely finish @rq.
  1842. */
  1843. void blk_end_request_all(struct request *rq, int error)
  1844. {
  1845. bool pending;
  1846. unsigned int bidi_bytes = 0;
  1847. if (unlikely(blk_bidi_rq(rq)))
  1848. bidi_bytes = blk_rq_bytes(rq->next_rq);
  1849. pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
  1850. BUG_ON(pending);
  1851. }
  1852. EXPORT_SYMBOL_GPL(blk_end_request_all);
  1853. /**
  1854. * blk_end_request_cur - Helper function to finish the current request chunk.
  1855. * @rq: the request to finish the current chunk for
  1856. * @error: %0 for success, < %0 for error
  1857. *
  1858. * Description:
  1859. * Complete the current consecutively mapped chunk from @rq.
  1860. *
  1861. * Return:
  1862. * %false - we are done with this request
  1863. * %true - still buffers pending for this request
  1864. */
  1865. bool blk_end_request_cur(struct request *rq, int error)
  1866. {
  1867. return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
  1868. }
  1869. EXPORT_SYMBOL_GPL(blk_end_request_cur);
  1870. /**
  1871. * __blk_end_request - Helper function for drivers to complete the request.
  1872. * @rq: the request being processed
  1873. * @error: %0 for success, < %0 for error
  1874. * @nr_bytes: number of bytes to complete
  1875. *
  1876. * Description:
  1877. * Must be called with queue lock held unlike blk_end_request().
  1878. *
  1879. * Return:
  1880. * %false - we are done with this request
  1881. * %true - still buffers pending for this request
  1882. **/
  1883. bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
  1884. {
  1885. return __blk_end_bidi_request(rq, error, nr_bytes, 0);
  1886. }
  1887. EXPORT_SYMBOL_GPL(__blk_end_request);
  1888. /**
  1889. * __blk_end_request_all - Helper function for drives to finish the request.
  1890. * @rq: the request to finish
  1891. * @error: %0 for success, < %0 for error
  1892. *
  1893. * Description:
  1894. * Completely finish @rq. Must be called with queue lock held.
  1895. */
  1896. void __blk_end_request_all(struct request *rq, int error)
  1897. {
  1898. bool pending;
  1899. unsigned int bidi_bytes = 0;
  1900. if (unlikely(blk_bidi_rq(rq)))
  1901. bidi_bytes = blk_rq_bytes(rq->next_rq);
  1902. pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
  1903. BUG_ON(pending);
  1904. }
  1905. EXPORT_SYMBOL_GPL(__blk_end_request_all);
  1906. /**
  1907. * __blk_end_request_cur - Helper function to finish the current request chunk.
  1908. * @rq: the request to finish the current chunk for
  1909. * @error: %0 for success, < %0 for error
  1910. *
  1911. * Description:
  1912. * Complete the current consecutively mapped chunk from @rq. Must
  1913. * be called with queue lock held.
  1914. *
  1915. * Return:
  1916. * %false - we are done with this request
  1917. * %true - still buffers pending for this request
  1918. */
  1919. bool __blk_end_request_cur(struct request *rq, int error)
  1920. {
  1921. return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
  1922. }
  1923. EXPORT_SYMBOL_GPL(__blk_end_request_cur);
  1924. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  1925. struct bio *bio)
  1926. {
  1927. /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
  1928. we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
  1929. rq->cmd_flags |= (bio->bi_rw & 3);
  1930. if (bio_has_data(bio)) {
  1931. rq->nr_phys_segments = bio_phys_segments(q, bio);
  1932. rq->buffer = bio_data(bio);
  1933. }
  1934. rq->__data_len = bio->bi_size;
  1935. rq->bio = rq->biotail = bio;
  1936. if (bio->bi_bdev)
  1937. rq->rq_disk = bio->bi_bdev->bd_disk;
  1938. }
  1939. /**
  1940. * blk_lld_busy - Check if underlying low-level drivers of a device are busy
  1941. * @q : the queue of the device being checked
  1942. *
  1943. * Description:
  1944. * Check if underlying low-level drivers of a device are busy.
  1945. * If the drivers want to export their busy state, they must set own
  1946. * exporting function using blk_queue_lld_busy() first.
  1947. *
  1948. * Basically, this function is used only by request stacking drivers
  1949. * to stop dispatching requests to underlying devices when underlying
  1950. * devices are busy. This behavior helps more I/O merging on the queue
  1951. * of the request stacking driver and prevents I/O throughput regression
  1952. * on burst I/O load.
  1953. *
  1954. * Return:
  1955. * 0 - Not busy (The request stacking driver should dispatch request)
  1956. * 1 - Busy (The request stacking driver should stop dispatching request)
  1957. */
  1958. int blk_lld_busy(struct request_queue *q)
  1959. {
  1960. if (q->lld_busy_fn)
  1961. return q->lld_busy_fn(q);
  1962. return 0;
  1963. }
  1964. EXPORT_SYMBOL_GPL(blk_lld_busy);
  1965. /**
  1966. * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
  1967. * @rq: the clone request to be cleaned up
  1968. *
  1969. * Description:
  1970. * Free all bios in @rq for a cloned request.
  1971. */
  1972. void blk_rq_unprep_clone(struct request *rq)
  1973. {
  1974. struct bio *bio;
  1975. while ((bio = rq->bio) != NULL) {
  1976. rq->bio = bio->bi_next;
  1977. bio_put(bio);
  1978. }
  1979. }
  1980. EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
  1981. /*
  1982. * Copy attributes of the original request to the clone request.
  1983. * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
  1984. */
  1985. static void __blk_rq_prep_clone(struct request *dst, struct request *src)
  1986. {
  1987. dst->cpu = src->cpu;
  1988. dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
  1989. dst->cmd_type = src->cmd_type;
  1990. dst->__sector = blk_rq_pos(src);
  1991. dst->__data_len = blk_rq_bytes(src);
  1992. dst->nr_phys_segments = src->nr_phys_segments;
  1993. dst->ioprio = src->ioprio;
  1994. dst->extra_len = src->extra_len;
  1995. }
  1996. /**
  1997. * blk_rq_prep_clone - Helper function to setup clone request
  1998. * @rq: the request to be setup
  1999. * @rq_src: original request to be cloned
  2000. * @bs: bio_set that bios for clone are allocated from
  2001. * @gfp_mask: memory allocation mask for bio
  2002. * @bio_ctr: setup function to be called for each clone bio.
  2003. * Returns %0 for success, non %0 for failure.
  2004. * @data: private data to be passed to @bio_ctr
  2005. *
  2006. * Description:
  2007. * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
  2008. * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
  2009. * are not copied, and copying such parts is the caller's responsibility.
  2010. * Also, pages which the original bios are pointing to are not copied
  2011. * and the cloned bios just point same pages.
  2012. * So cloned bios must be completed before original bios, which means
  2013. * the caller must complete @rq before @rq_src.
  2014. */
  2015. int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
  2016. struct bio_set *bs, gfp_t gfp_mask,
  2017. int (*bio_ctr)(struct bio *, struct bio *, void *),
  2018. void *data)
  2019. {
  2020. struct bio *bio, *bio_src;
  2021. if (!bs)
  2022. bs = fs_bio_set;
  2023. blk_rq_init(NULL, rq);
  2024. __rq_for_each_bio(bio_src, rq_src) {
  2025. bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
  2026. if (!bio)
  2027. goto free_and_out;
  2028. __bio_clone(bio, bio_src);
  2029. if (bio_integrity(bio_src) &&
  2030. bio_integrity_clone(bio, bio_src, gfp_mask, bs))
  2031. goto free_and_out;
  2032. if (bio_ctr && bio_ctr(bio, bio_src, data))
  2033. goto free_and_out;
  2034. if (rq->bio) {
  2035. rq->biotail->bi_next = bio;
  2036. rq->biotail = bio;
  2037. } else
  2038. rq->bio = rq->biotail = bio;
  2039. }
  2040. __blk_rq_prep_clone(rq, rq_src);
  2041. return 0;
  2042. free_and_out:
  2043. if (bio)
  2044. bio_free(bio, bs);
  2045. blk_rq_unprep_clone(rq);
  2046. return -ENOMEM;
  2047. }
  2048. EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
  2049. int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
  2050. {
  2051. return queue_work(kblockd_workqueue, work);
  2052. }
  2053. EXPORT_SYMBOL(kblockd_schedule_work);
  2054. int __init blk_dev_init(void)
  2055. {
  2056. BUILD_BUG_ON(__REQ_NR_BITS > 8 *
  2057. sizeof(((struct request *)0)->cmd_flags));
  2058. kblockd_workqueue = create_workqueue("kblockd");
  2059. if (!kblockd_workqueue)
  2060. panic("Failed to create kblockd\n");
  2061. request_cachep = kmem_cache_create("blkdev_requests",
  2062. sizeof(struct request), 0, SLAB_PANIC, NULL);
  2063. blk_requestq_cachep = kmem_cache_create("blkdev_queue",
  2064. sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
  2065. return 0;
  2066. }