blk-core.c 69 KB

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