blk-core.c 67 KB

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