blk-core.c 70 KB

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