blk-core.c 71 KB

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