raid10.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462
  1. /*
  2. * raid10.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 2000-2004 Neil Brown
  5. *
  6. * RAID-10 support for md.
  7. *
  8. * Base on code in raid1.c. See raid1.c for further copyright information.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/module.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/ratelimit.h>
  26. #include "md.h"
  27. #include "raid10.h"
  28. #include "raid0.h"
  29. #include "bitmap.h"
  30. /*
  31. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  32. * The layout of data is defined by
  33. * chunk_size
  34. * raid_disks
  35. * near_copies (stored in low byte of layout)
  36. * far_copies (stored in second byte of layout)
  37. * far_offset (stored in bit 16 of layout )
  38. *
  39. * The data to be stored is divided into chunks using chunksize.
  40. * Each device is divided into far_copies sections.
  41. * In each section, chunks are laid out in a style similar to raid0, but
  42. * near_copies copies of each chunk is stored (each on a different drive).
  43. * The starting device for each section is offset near_copies from the starting
  44. * device of the previous section.
  45. * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
  46. * drive.
  47. * near_copies and far_copies must be at least one, and their product is at most
  48. * raid_disks.
  49. *
  50. * If far_offset is true, then the far_copies are handled a bit differently.
  51. * The copies are still in different stripes, but instead of be very far apart
  52. * on disk, there are adjacent stripes.
  53. */
  54. /*
  55. * Number of guaranteed r10bios in case of extreme VM load:
  56. */
  57. #define NR_RAID10_BIOS 256
  58. /* When there are this many requests queue to be written by
  59. * the raid10 thread, we become 'congested' to provide back-pressure
  60. * for writeback.
  61. */
  62. static int max_queued_requests = 1024;
  63. static void allow_barrier(struct r10conf *conf);
  64. static void lower_barrier(struct r10conf *conf);
  65. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  66. {
  67. struct r10conf *conf = data;
  68. int size = offsetof(struct r10bio, devs[conf->copies]);
  69. /* allocate a r10bio with room for raid_disks entries in the
  70. * bios array */
  71. return kzalloc(size, gfp_flags);
  72. }
  73. static void r10bio_pool_free(void *r10_bio, void *data)
  74. {
  75. kfree(r10_bio);
  76. }
  77. /* Maximum size of each resync request */
  78. #define RESYNC_BLOCK_SIZE (64*1024)
  79. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  80. /* amount of memory to reserve for resync requests */
  81. #define RESYNC_WINDOW (1024*1024)
  82. /* maximum number of concurrent requests, memory permitting */
  83. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  84. /*
  85. * When performing a resync, we need to read and compare, so
  86. * we need as many pages are there are copies.
  87. * When performing a recovery, we need 2 bios, one for read,
  88. * one for write (we recover only one drive per r10buf)
  89. *
  90. */
  91. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  92. {
  93. struct r10conf *conf = data;
  94. struct page *page;
  95. struct r10bio *r10_bio;
  96. struct bio *bio;
  97. int i, j;
  98. int nalloc;
  99. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  100. if (!r10_bio)
  101. return NULL;
  102. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  103. nalloc = conf->copies; /* resync */
  104. else
  105. nalloc = 2; /* recovery */
  106. /*
  107. * Allocate bios.
  108. */
  109. for (j = nalloc ; j-- ; ) {
  110. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  111. if (!bio)
  112. goto out_free_bio;
  113. r10_bio->devs[j].bio = bio;
  114. if (!conf->have_replacement)
  115. continue;
  116. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  117. if (!bio)
  118. goto out_free_bio;
  119. r10_bio->devs[j].repl_bio = bio;
  120. }
  121. /*
  122. * Allocate RESYNC_PAGES data pages and attach them
  123. * where needed.
  124. */
  125. for (j = 0 ; j < nalloc; j++) {
  126. struct bio *rbio = r10_bio->devs[j].repl_bio;
  127. bio = r10_bio->devs[j].bio;
  128. for (i = 0; i < RESYNC_PAGES; i++) {
  129. if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
  130. &conf->mddev->recovery)) {
  131. /* we can share bv_page's during recovery */
  132. struct bio *rbio = r10_bio->devs[0].bio;
  133. page = rbio->bi_io_vec[i].bv_page;
  134. get_page(page);
  135. } else
  136. page = alloc_page(gfp_flags);
  137. if (unlikely(!page))
  138. goto out_free_pages;
  139. bio->bi_io_vec[i].bv_page = page;
  140. if (rbio)
  141. rbio->bi_io_vec[i].bv_page = page;
  142. }
  143. }
  144. return r10_bio;
  145. out_free_pages:
  146. for ( ; i > 0 ; i--)
  147. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  148. while (j--)
  149. for (i = 0; i < RESYNC_PAGES ; i++)
  150. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  151. j = -1;
  152. out_free_bio:
  153. while (++j < nalloc) {
  154. bio_put(r10_bio->devs[j].bio);
  155. if (r10_bio->devs[j].repl_bio)
  156. bio_put(r10_bio->devs[j].repl_bio);
  157. }
  158. r10bio_pool_free(r10_bio, conf);
  159. return NULL;
  160. }
  161. static void r10buf_pool_free(void *__r10_bio, void *data)
  162. {
  163. int i;
  164. struct r10conf *conf = data;
  165. struct r10bio *r10bio = __r10_bio;
  166. int j;
  167. for (j=0; j < conf->copies; j++) {
  168. struct bio *bio = r10bio->devs[j].bio;
  169. if (bio) {
  170. for (i = 0; i < RESYNC_PAGES; i++) {
  171. safe_put_page(bio->bi_io_vec[i].bv_page);
  172. bio->bi_io_vec[i].bv_page = NULL;
  173. }
  174. bio_put(bio);
  175. }
  176. bio = r10bio->devs[j].repl_bio;
  177. if (bio)
  178. bio_put(bio);
  179. }
  180. r10bio_pool_free(r10bio, conf);
  181. }
  182. static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
  183. {
  184. int i;
  185. for (i = 0; i < conf->copies; i++) {
  186. struct bio **bio = & r10_bio->devs[i].bio;
  187. if (!BIO_SPECIAL(*bio))
  188. bio_put(*bio);
  189. *bio = NULL;
  190. bio = &r10_bio->devs[i].repl_bio;
  191. if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
  192. bio_put(*bio);
  193. *bio = NULL;
  194. }
  195. }
  196. static void free_r10bio(struct r10bio *r10_bio)
  197. {
  198. struct r10conf *conf = r10_bio->mddev->private;
  199. put_all_bios(conf, r10_bio);
  200. mempool_free(r10_bio, conf->r10bio_pool);
  201. }
  202. static void put_buf(struct r10bio *r10_bio)
  203. {
  204. struct r10conf *conf = r10_bio->mddev->private;
  205. mempool_free(r10_bio, conf->r10buf_pool);
  206. lower_barrier(conf);
  207. }
  208. static void reschedule_retry(struct r10bio *r10_bio)
  209. {
  210. unsigned long flags;
  211. struct mddev *mddev = r10_bio->mddev;
  212. struct r10conf *conf = mddev->private;
  213. spin_lock_irqsave(&conf->device_lock, flags);
  214. list_add(&r10_bio->retry_list, &conf->retry_list);
  215. conf->nr_queued ++;
  216. spin_unlock_irqrestore(&conf->device_lock, flags);
  217. /* wake up frozen array... */
  218. wake_up(&conf->wait_barrier);
  219. md_wakeup_thread(mddev->thread);
  220. }
  221. /*
  222. * raid_end_bio_io() is called when we have finished servicing a mirrored
  223. * operation and are ready to return a success/failure code to the buffer
  224. * cache layer.
  225. */
  226. static void raid_end_bio_io(struct r10bio *r10_bio)
  227. {
  228. struct bio *bio = r10_bio->master_bio;
  229. int done;
  230. struct r10conf *conf = r10_bio->mddev->private;
  231. if (bio->bi_phys_segments) {
  232. unsigned long flags;
  233. spin_lock_irqsave(&conf->device_lock, flags);
  234. bio->bi_phys_segments--;
  235. done = (bio->bi_phys_segments == 0);
  236. spin_unlock_irqrestore(&conf->device_lock, flags);
  237. } else
  238. done = 1;
  239. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  240. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  241. if (done) {
  242. bio_endio(bio, 0);
  243. /*
  244. * Wake up any possible resync thread that waits for the device
  245. * to go idle.
  246. */
  247. allow_barrier(conf);
  248. }
  249. free_r10bio(r10_bio);
  250. }
  251. /*
  252. * Update disk head position estimator based on IRQ completion info.
  253. */
  254. static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  255. {
  256. struct r10conf *conf = r10_bio->mddev->private;
  257. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  258. r10_bio->devs[slot].addr + (r10_bio->sectors);
  259. }
  260. /*
  261. * Find the disk number which triggered given bio
  262. */
  263. static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
  264. struct bio *bio, int *slotp, int *replp)
  265. {
  266. int slot;
  267. int repl = 0;
  268. for (slot = 0; slot < conf->copies; slot++) {
  269. if (r10_bio->devs[slot].bio == bio)
  270. break;
  271. if (r10_bio->devs[slot].repl_bio == bio) {
  272. repl = 1;
  273. break;
  274. }
  275. }
  276. BUG_ON(slot == conf->copies);
  277. update_head_pos(slot, r10_bio);
  278. if (slotp)
  279. *slotp = slot;
  280. if (replp)
  281. *replp = repl;
  282. return r10_bio->devs[slot].devnum;
  283. }
  284. static void raid10_end_read_request(struct bio *bio, int error)
  285. {
  286. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  287. struct r10bio *r10_bio = bio->bi_private;
  288. int slot, dev;
  289. struct md_rdev *rdev;
  290. struct r10conf *conf = r10_bio->mddev->private;
  291. slot = r10_bio->read_slot;
  292. dev = r10_bio->devs[slot].devnum;
  293. rdev = r10_bio->devs[slot].rdev;
  294. /*
  295. * this branch is our 'one mirror IO has finished' event handler:
  296. */
  297. update_head_pos(slot, r10_bio);
  298. if (uptodate) {
  299. /*
  300. * Set R10BIO_Uptodate in our master bio, so that
  301. * we will return a good error code to the higher
  302. * levels even if IO on some other mirrored buffer fails.
  303. *
  304. * The 'master' represents the composite IO operation to
  305. * user-side. So if something waits for IO, then it will
  306. * wait for the 'master' bio.
  307. */
  308. set_bit(R10BIO_Uptodate, &r10_bio->state);
  309. raid_end_bio_io(r10_bio);
  310. rdev_dec_pending(rdev, conf->mddev);
  311. } else {
  312. /*
  313. * oops, read error - keep the refcount on the rdev
  314. */
  315. char b[BDEVNAME_SIZE];
  316. printk_ratelimited(KERN_ERR
  317. "md/raid10:%s: %s: rescheduling sector %llu\n",
  318. mdname(conf->mddev),
  319. bdevname(rdev->bdev, b),
  320. (unsigned long long)r10_bio->sector);
  321. set_bit(R10BIO_ReadError, &r10_bio->state);
  322. reschedule_retry(r10_bio);
  323. }
  324. }
  325. static void close_write(struct r10bio *r10_bio)
  326. {
  327. /* clear the bitmap if all writes complete successfully */
  328. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  329. r10_bio->sectors,
  330. !test_bit(R10BIO_Degraded, &r10_bio->state),
  331. 0);
  332. md_write_end(r10_bio->mddev);
  333. }
  334. static void one_write_done(struct r10bio *r10_bio)
  335. {
  336. if (atomic_dec_and_test(&r10_bio->remaining)) {
  337. if (test_bit(R10BIO_WriteError, &r10_bio->state))
  338. reschedule_retry(r10_bio);
  339. else {
  340. close_write(r10_bio);
  341. if (test_bit(R10BIO_MadeGood, &r10_bio->state))
  342. reschedule_retry(r10_bio);
  343. else
  344. raid_end_bio_io(r10_bio);
  345. }
  346. }
  347. }
  348. static void raid10_end_write_request(struct bio *bio, int error)
  349. {
  350. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  351. struct r10bio *r10_bio = bio->bi_private;
  352. int dev;
  353. int dec_rdev = 1;
  354. struct r10conf *conf = r10_bio->mddev->private;
  355. int slot, repl;
  356. struct md_rdev *rdev = NULL;
  357. dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  358. if (repl)
  359. rdev = conf->mirrors[dev].replacement;
  360. if (!rdev) {
  361. smp_rmb();
  362. repl = 0;
  363. rdev = conf->mirrors[dev].rdev;
  364. }
  365. /*
  366. * this branch is our 'one mirror IO has finished' event handler:
  367. */
  368. if (!uptodate) {
  369. if (repl)
  370. /* Never record new bad blocks to replacement,
  371. * just fail it.
  372. */
  373. md_error(rdev->mddev, rdev);
  374. else {
  375. set_bit(WriteErrorSeen, &rdev->flags);
  376. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  377. set_bit(MD_RECOVERY_NEEDED,
  378. &rdev->mddev->recovery);
  379. set_bit(R10BIO_WriteError, &r10_bio->state);
  380. dec_rdev = 0;
  381. }
  382. } else {
  383. /*
  384. * Set R10BIO_Uptodate in our master bio, so that
  385. * we will return a good error code for to the higher
  386. * levels even if IO on some other mirrored buffer fails.
  387. *
  388. * The 'master' represents the composite IO operation to
  389. * user-side. So if something waits for IO, then it will
  390. * wait for the 'master' bio.
  391. */
  392. sector_t first_bad;
  393. int bad_sectors;
  394. set_bit(R10BIO_Uptodate, &r10_bio->state);
  395. /* Maybe we can clear some bad blocks. */
  396. if (is_badblock(rdev,
  397. r10_bio->devs[slot].addr,
  398. r10_bio->sectors,
  399. &first_bad, &bad_sectors)) {
  400. bio_put(bio);
  401. if (repl)
  402. r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
  403. else
  404. r10_bio->devs[slot].bio = IO_MADE_GOOD;
  405. dec_rdev = 0;
  406. set_bit(R10BIO_MadeGood, &r10_bio->state);
  407. }
  408. }
  409. /*
  410. *
  411. * Let's see if all mirrored write operations have finished
  412. * already.
  413. */
  414. one_write_done(r10_bio);
  415. if (dec_rdev)
  416. rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
  417. }
  418. /*
  419. * RAID10 layout manager
  420. * As well as the chunksize and raid_disks count, there are two
  421. * parameters: near_copies and far_copies.
  422. * near_copies * far_copies must be <= raid_disks.
  423. * Normally one of these will be 1.
  424. * If both are 1, we get raid0.
  425. * If near_copies == raid_disks, we get raid1.
  426. *
  427. * Chunks are laid out in raid0 style with near_copies copies of the
  428. * first chunk, followed by near_copies copies of the next chunk and
  429. * so on.
  430. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  431. * as described above, we start again with a device offset of near_copies.
  432. * So we effectively have another copy of the whole array further down all
  433. * the drives, but with blocks on different drives.
  434. * With this layout, and block is never stored twice on the one device.
  435. *
  436. * raid10_find_phys finds the sector offset of a given virtual sector
  437. * on each device that it is on.
  438. *
  439. * raid10_find_virt does the reverse mapping, from a device and a
  440. * sector offset to a virtual address
  441. */
  442. static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
  443. {
  444. int n,f;
  445. sector_t sector;
  446. sector_t chunk;
  447. sector_t stripe;
  448. int dev;
  449. int slot = 0;
  450. /* now calculate first sector/dev */
  451. chunk = r10bio->sector >> conf->chunk_shift;
  452. sector = r10bio->sector & conf->chunk_mask;
  453. chunk *= conf->near_copies;
  454. stripe = chunk;
  455. dev = sector_div(stripe, conf->raid_disks);
  456. if (conf->far_offset)
  457. stripe *= conf->far_copies;
  458. sector += stripe << conf->chunk_shift;
  459. /* and calculate all the others */
  460. for (n=0; n < conf->near_copies; n++) {
  461. int d = dev;
  462. sector_t s = sector;
  463. r10bio->devs[slot].addr = sector;
  464. r10bio->devs[slot].devnum = d;
  465. slot++;
  466. for (f = 1; f < conf->far_copies; f++) {
  467. d += conf->near_copies;
  468. if (d >= conf->raid_disks)
  469. d -= conf->raid_disks;
  470. s += conf->stride;
  471. r10bio->devs[slot].devnum = d;
  472. r10bio->devs[slot].addr = s;
  473. slot++;
  474. }
  475. dev++;
  476. if (dev >= conf->raid_disks) {
  477. dev = 0;
  478. sector += (conf->chunk_mask + 1);
  479. }
  480. }
  481. BUG_ON(slot != conf->copies);
  482. }
  483. static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
  484. {
  485. sector_t offset, chunk, vchunk;
  486. offset = sector & conf->chunk_mask;
  487. if (conf->far_offset) {
  488. int fc;
  489. chunk = sector >> conf->chunk_shift;
  490. fc = sector_div(chunk, conf->far_copies);
  491. dev -= fc * conf->near_copies;
  492. if (dev < 0)
  493. dev += conf->raid_disks;
  494. } else {
  495. while (sector >= conf->stride) {
  496. sector -= conf->stride;
  497. if (dev < conf->near_copies)
  498. dev += conf->raid_disks - conf->near_copies;
  499. else
  500. dev -= conf->near_copies;
  501. }
  502. chunk = sector >> conf->chunk_shift;
  503. }
  504. vchunk = chunk * conf->raid_disks + dev;
  505. sector_div(vchunk, conf->near_copies);
  506. return (vchunk << conf->chunk_shift) + offset;
  507. }
  508. /**
  509. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  510. * @q: request queue
  511. * @bvm: properties of new bio
  512. * @biovec: the request that could be merged to it.
  513. *
  514. * Return amount of bytes we can accept at this offset
  515. * If near_copies == raid_disk, there are no striping issues,
  516. * but in that case, the function isn't called at all.
  517. */
  518. static int raid10_mergeable_bvec(struct request_queue *q,
  519. struct bvec_merge_data *bvm,
  520. struct bio_vec *biovec)
  521. {
  522. struct mddev *mddev = q->queuedata;
  523. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  524. int max;
  525. unsigned int chunk_sectors = mddev->chunk_sectors;
  526. unsigned int bio_sectors = bvm->bi_size >> 9;
  527. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  528. if (max < 0) max = 0; /* bio_add cannot handle a negative return */
  529. if (max <= biovec->bv_len && bio_sectors == 0)
  530. return biovec->bv_len;
  531. else
  532. return max;
  533. }
  534. /*
  535. * This routine returns the disk from which the requested read should
  536. * be done. There is a per-array 'next expected sequential IO' sector
  537. * number - if this matches on the next IO then we use the last disk.
  538. * There is also a per-disk 'last know head position' sector that is
  539. * maintained from IRQ contexts, both the normal and the resync IO
  540. * completion handlers update this position correctly. If there is no
  541. * perfect sequential match then we pick the disk whose head is closest.
  542. *
  543. * If there are 2 mirrors in the same 2 devices, performance degrades
  544. * because position is mirror, not device based.
  545. *
  546. * The rdev for the device selected will have nr_pending incremented.
  547. */
  548. /*
  549. * FIXME: possibly should rethink readbalancing and do it differently
  550. * depending on near_copies / far_copies geometry.
  551. */
  552. static struct md_rdev *read_balance(struct r10conf *conf,
  553. struct r10bio *r10_bio,
  554. int *max_sectors)
  555. {
  556. const sector_t this_sector = r10_bio->sector;
  557. int disk, slot;
  558. int sectors = r10_bio->sectors;
  559. int best_good_sectors;
  560. sector_t new_distance, best_dist;
  561. struct md_rdev *rdev, *best_rdev;
  562. int do_balance;
  563. int best_slot;
  564. raid10_find_phys(conf, r10_bio);
  565. rcu_read_lock();
  566. retry:
  567. sectors = r10_bio->sectors;
  568. best_slot = -1;
  569. best_rdev = NULL;
  570. best_dist = MaxSector;
  571. best_good_sectors = 0;
  572. do_balance = 1;
  573. /*
  574. * Check if we can balance. We can balance on the whole
  575. * device if no resync is going on (recovery is ok), or below
  576. * the resync window. We take the first readable disk when
  577. * above the resync window.
  578. */
  579. if (conf->mddev->recovery_cp < MaxSector
  580. && (this_sector + sectors >= conf->next_resync))
  581. do_balance = 0;
  582. for (slot = 0; slot < conf->copies ; slot++) {
  583. sector_t first_bad;
  584. int bad_sectors;
  585. sector_t dev_sector;
  586. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  587. continue;
  588. disk = r10_bio->devs[slot].devnum;
  589. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  590. if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
  591. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  592. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  593. if (rdev == NULL)
  594. continue;
  595. if (test_bit(Faulty, &rdev->flags))
  596. continue;
  597. if (!test_bit(In_sync, &rdev->flags) &&
  598. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  599. continue;
  600. dev_sector = r10_bio->devs[slot].addr;
  601. if (is_badblock(rdev, dev_sector, sectors,
  602. &first_bad, &bad_sectors)) {
  603. if (best_dist < MaxSector)
  604. /* Already have a better slot */
  605. continue;
  606. if (first_bad <= dev_sector) {
  607. /* Cannot read here. If this is the
  608. * 'primary' device, then we must not read
  609. * beyond 'bad_sectors' from another device.
  610. */
  611. bad_sectors -= (dev_sector - first_bad);
  612. if (!do_balance && sectors > bad_sectors)
  613. sectors = bad_sectors;
  614. if (best_good_sectors > sectors)
  615. best_good_sectors = sectors;
  616. } else {
  617. sector_t good_sectors =
  618. first_bad - dev_sector;
  619. if (good_sectors > best_good_sectors) {
  620. best_good_sectors = good_sectors;
  621. best_slot = slot;
  622. best_rdev = rdev;
  623. }
  624. if (!do_balance)
  625. /* Must read from here */
  626. break;
  627. }
  628. continue;
  629. } else
  630. best_good_sectors = sectors;
  631. if (!do_balance)
  632. break;
  633. /* This optimisation is debatable, and completely destroys
  634. * sequential read speed for 'far copies' arrays. So only
  635. * keep it for 'near' arrays, and review those later.
  636. */
  637. if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  638. break;
  639. /* for far > 1 always use the lowest address */
  640. if (conf->far_copies > 1)
  641. new_distance = r10_bio->devs[slot].addr;
  642. else
  643. new_distance = abs(r10_bio->devs[slot].addr -
  644. conf->mirrors[disk].head_position);
  645. if (new_distance < best_dist) {
  646. best_dist = new_distance;
  647. best_slot = slot;
  648. best_rdev = rdev;
  649. }
  650. }
  651. if (slot >= conf->copies) {
  652. slot = best_slot;
  653. rdev = best_rdev;
  654. }
  655. if (slot >= 0) {
  656. atomic_inc(&rdev->nr_pending);
  657. if (test_bit(Faulty, &rdev->flags)) {
  658. /* Cannot risk returning a device that failed
  659. * before we inc'ed nr_pending
  660. */
  661. rdev_dec_pending(rdev, conf->mddev);
  662. goto retry;
  663. }
  664. r10_bio->read_slot = slot;
  665. } else
  666. rdev = NULL;
  667. rcu_read_unlock();
  668. *max_sectors = best_good_sectors;
  669. return rdev;
  670. }
  671. static int raid10_congested(void *data, int bits)
  672. {
  673. struct mddev *mddev = data;
  674. struct r10conf *conf = mddev->private;
  675. int i, ret = 0;
  676. if ((bits & (1 << BDI_async_congested)) &&
  677. conf->pending_count >= max_queued_requests)
  678. return 1;
  679. if (mddev_congested(mddev, bits))
  680. return 1;
  681. rcu_read_lock();
  682. for (i = 0; i < conf->raid_disks && ret == 0; i++) {
  683. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  684. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  685. struct request_queue *q = bdev_get_queue(rdev->bdev);
  686. ret |= bdi_congested(&q->backing_dev_info, bits);
  687. }
  688. }
  689. rcu_read_unlock();
  690. return ret;
  691. }
  692. static void flush_pending_writes(struct r10conf *conf)
  693. {
  694. /* Any writes that have been queued but are awaiting
  695. * bitmap updates get flushed here.
  696. */
  697. spin_lock_irq(&conf->device_lock);
  698. if (conf->pending_bio_list.head) {
  699. struct bio *bio;
  700. bio = bio_list_get(&conf->pending_bio_list);
  701. conf->pending_count = 0;
  702. spin_unlock_irq(&conf->device_lock);
  703. /* flush any pending bitmap writes to disk
  704. * before proceeding w/ I/O */
  705. bitmap_unplug(conf->mddev->bitmap);
  706. wake_up(&conf->wait_barrier);
  707. while (bio) { /* submit pending writes */
  708. struct bio *next = bio->bi_next;
  709. bio->bi_next = NULL;
  710. generic_make_request(bio);
  711. bio = next;
  712. }
  713. } else
  714. spin_unlock_irq(&conf->device_lock);
  715. }
  716. /* Barriers....
  717. * Sometimes we need to suspend IO while we do something else,
  718. * either some resync/recovery, or reconfigure the array.
  719. * To do this we raise a 'barrier'.
  720. * The 'barrier' is a counter that can be raised multiple times
  721. * to count how many activities are happening which preclude
  722. * normal IO.
  723. * We can only raise the barrier if there is no pending IO.
  724. * i.e. if nr_pending == 0.
  725. * We choose only to raise the barrier if no-one is waiting for the
  726. * barrier to go down. This means that as soon as an IO request
  727. * is ready, no other operations which require a barrier will start
  728. * until the IO request has had a chance.
  729. *
  730. * So: regular IO calls 'wait_barrier'. When that returns there
  731. * is no backgroup IO happening, It must arrange to call
  732. * allow_barrier when it has finished its IO.
  733. * backgroup IO calls must call raise_barrier. Once that returns
  734. * there is no normal IO happeing. It must arrange to call
  735. * lower_barrier when the particular background IO completes.
  736. */
  737. static void raise_barrier(struct r10conf *conf, int force)
  738. {
  739. BUG_ON(force && !conf->barrier);
  740. spin_lock_irq(&conf->resync_lock);
  741. /* Wait until no block IO is waiting (unless 'force') */
  742. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  743. conf->resync_lock, );
  744. /* block any new IO from starting */
  745. conf->barrier++;
  746. /* Now wait for all pending IO to complete */
  747. wait_event_lock_irq(conf->wait_barrier,
  748. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  749. conf->resync_lock, );
  750. spin_unlock_irq(&conf->resync_lock);
  751. }
  752. static void lower_barrier(struct r10conf *conf)
  753. {
  754. unsigned long flags;
  755. spin_lock_irqsave(&conf->resync_lock, flags);
  756. conf->barrier--;
  757. spin_unlock_irqrestore(&conf->resync_lock, flags);
  758. wake_up(&conf->wait_barrier);
  759. }
  760. static void wait_barrier(struct r10conf *conf)
  761. {
  762. spin_lock_irq(&conf->resync_lock);
  763. if (conf->barrier) {
  764. conf->nr_waiting++;
  765. wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
  766. conf->resync_lock,
  767. );
  768. conf->nr_waiting--;
  769. }
  770. conf->nr_pending++;
  771. spin_unlock_irq(&conf->resync_lock);
  772. }
  773. static void allow_barrier(struct r10conf *conf)
  774. {
  775. unsigned long flags;
  776. spin_lock_irqsave(&conf->resync_lock, flags);
  777. conf->nr_pending--;
  778. spin_unlock_irqrestore(&conf->resync_lock, flags);
  779. wake_up(&conf->wait_barrier);
  780. }
  781. static void freeze_array(struct r10conf *conf)
  782. {
  783. /* stop syncio and normal IO and wait for everything to
  784. * go quiet.
  785. * We increment barrier and nr_waiting, and then
  786. * wait until nr_pending match nr_queued+1
  787. * This is called in the context of one normal IO request
  788. * that has failed. Thus any sync request that might be pending
  789. * will be blocked by nr_pending, and we need to wait for
  790. * pending IO requests to complete or be queued for re-try.
  791. * Thus the number queued (nr_queued) plus this request (1)
  792. * must match the number of pending IOs (nr_pending) before
  793. * we continue.
  794. */
  795. spin_lock_irq(&conf->resync_lock);
  796. conf->barrier++;
  797. conf->nr_waiting++;
  798. wait_event_lock_irq(conf->wait_barrier,
  799. conf->nr_pending == conf->nr_queued+1,
  800. conf->resync_lock,
  801. flush_pending_writes(conf));
  802. spin_unlock_irq(&conf->resync_lock);
  803. }
  804. static void unfreeze_array(struct r10conf *conf)
  805. {
  806. /* reverse the effect of the freeze */
  807. spin_lock_irq(&conf->resync_lock);
  808. conf->barrier--;
  809. conf->nr_waiting--;
  810. wake_up(&conf->wait_barrier);
  811. spin_unlock_irq(&conf->resync_lock);
  812. }
  813. static void make_request(struct mddev *mddev, struct bio * bio)
  814. {
  815. struct r10conf *conf = mddev->private;
  816. struct r10bio *r10_bio;
  817. struct bio *read_bio;
  818. int i;
  819. int chunk_sects = conf->chunk_mask + 1;
  820. const int rw = bio_data_dir(bio);
  821. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  822. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  823. unsigned long flags;
  824. struct md_rdev *blocked_rdev;
  825. int plugged;
  826. int sectors_handled;
  827. int max_sectors;
  828. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  829. md_flush_request(mddev, bio);
  830. return;
  831. }
  832. /* If this request crosses a chunk boundary, we need to
  833. * split it. This will only happen for 1 PAGE (or less) requests.
  834. */
  835. if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
  836. > chunk_sects &&
  837. conf->near_copies < conf->raid_disks)) {
  838. struct bio_pair *bp;
  839. /* Sanity check -- queue functions should prevent this happening */
  840. if (bio->bi_vcnt != 1 ||
  841. bio->bi_idx != 0)
  842. goto bad_map;
  843. /* This is a one page bio that upper layers
  844. * refuse to split for us, so we need to split it.
  845. */
  846. bp = bio_split(bio,
  847. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  848. /* Each of these 'make_request' calls will call 'wait_barrier'.
  849. * If the first succeeds but the second blocks due to the resync
  850. * thread raising the barrier, we will deadlock because the
  851. * IO to the underlying device will be queued in generic_make_request
  852. * and will never complete, so will never reduce nr_pending.
  853. * So increment nr_waiting here so no new raise_barriers will
  854. * succeed, and so the second wait_barrier cannot block.
  855. */
  856. spin_lock_irq(&conf->resync_lock);
  857. conf->nr_waiting++;
  858. spin_unlock_irq(&conf->resync_lock);
  859. make_request(mddev, &bp->bio1);
  860. make_request(mddev, &bp->bio2);
  861. spin_lock_irq(&conf->resync_lock);
  862. conf->nr_waiting--;
  863. wake_up(&conf->wait_barrier);
  864. spin_unlock_irq(&conf->resync_lock);
  865. bio_pair_release(bp);
  866. return;
  867. bad_map:
  868. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  869. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  870. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  871. bio_io_error(bio);
  872. return;
  873. }
  874. md_write_start(mddev, bio);
  875. /*
  876. * Register the new request and wait if the reconstruction
  877. * thread has put up a bar for new requests.
  878. * Continue immediately if no resync is active currently.
  879. */
  880. wait_barrier(conf);
  881. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  882. r10_bio->master_bio = bio;
  883. r10_bio->sectors = bio->bi_size >> 9;
  884. r10_bio->mddev = mddev;
  885. r10_bio->sector = bio->bi_sector;
  886. r10_bio->state = 0;
  887. /* We might need to issue multiple reads to different
  888. * devices if there are bad blocks around, so we keep
  889. * track of the number of reads in bio->bi_phys_segments.
  890. * If this is 0, there is only one r10_bio and no locking
  891. * will be needed when the request completes. If it is
  892. * non-zero, then it is the number of not-completed requests.
  893. */
  894. bio->bi_phys_segments = 0;
  895. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  896. if (rw == READ) {
  897. /*
  898. * read balancing logic:
  899. */
  900. struct md_rdev *rdev;
  901. int slot;
  902. read_again:
  903. rdev = read_balance(conf, r10_bio, &max_sectors);
  904. if (!rdev) {
  905. raid_end_bio_io(r10_bio);
  906. return;
  907. }
  908. slot = r10_bio->read_slot;
  909. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  910. md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
  911. max_sectors);
  912. r10_bio->devs[slot].bio = read_bio;
  913. r10_bio->devs[slot].rdev = rdev;
  914. read_bio->bi_sector = r10_bio->devs[slot].addr +
  915. rdev->data_offset;
  916. read_bio->bi_bdev = rdev->bdev;
  917. read_bio->bi_end_io = raid10_end_read_request;
  918. read_bio->bi_rw = READ | do_sync;
  919. read_bio->bi_private = r10_bio;
  920. if (max_sectors < r10_bio->sectors) {
  921. /* Could not read all from this device, so we will
  922. * need another r10_bio.
  923. */
  924. sectors_handled = (r10_bio->sectors + max_sectors
  925. - bio->bi_sector);
  926. r10_bio->sectors = max_sectors;
  927. spin_lock_irq(&conf->device_lock);
  928. if (bio->bi_phys_segments == 0)
  929. bio->bi_phys_segments = 2;
  930. else
  931. bio->bi_phys_segments++;
  932. spin_unlock(&conf->device_lock);
  933. /* Cannot call generic_make_request directly
  934. * as that will be queued in __generic_make_request
  935. * and subsequent mempool_alloc might block
  936. * waiting for it. so hand bio over to raid10d.
  937. */
  938. reschedule_retry(r10_bio);
  939. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  940. r10_bio->master_bio = bio;
  941. r10_bio->sectors = ((bio->bi_size >> 9)
  942. - sectors_handled);
  943. r10_bio->state = 0;
  944. r10_bio->mddev = mddev;
  945. r10_bio->sector = bio->bi_sector + sectors_handled;
  946. goto read_again;
  947. } else
  948. generic_make_request(read_bio);
  949. return;
  950. }
  951. /*
  952. * WRITE:
  953. */
  954. if (conf->pending_count >= max_queued_requests) {
  955. md_wakeup_thread(mddev->thread);
  956. wait_event(conf->wait_barrier,
  957. conf->pending_count < max_queued_requests);
  958. }
  959. /* first select target devices under rcu_lock and
  960. * inc refcount on their rdev. Record them by setting
  961. * bios[x] to bio
  962. * If there are known/acknowledged bad blocks on any device
  963. * on which we have seen a write error, we want to avoid
  964. * writing to those blocks. This potentially requires several
  965. * writes to write around the bad blocks. Each set of writes
  966. * gets its own r10_bio with a set of bios attached. The number
  967. * of r10_bios is recored in bio->bi_phys_segments just as with
  968. * the read case.
  969. */
  970. plugged = mddev_check_plugged(mddev);
  971. r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
  972. raid10_find_phys(conf, r10_bio);
  973. retry_write:
  974. blocked_rdev = NULL;
  975. rcu_read_lock();
  976. max_sectors = r10_bio->sectors;
  977. for (i = 0; i < conf->copies; i++) {
  978. int d = r10_bio->devs[i].devnum;
  979. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  980. struct md_rdev *rrdev = rcu_dereference(
  981. conf->mirrors[d].replacement);
  982. if (rdev == rrdev)
  983. rrdev = NULL;
  984. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  985. atomic_inc(&rdev->nr_pending);
  986. blocked_rdev = rdev;
  987. break;
  988. }
  989. if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
  990. atomic_inc(&rrdev->nr_pending);
  991. blocked_rdev = rrdev;
  992. break;
  993. }
  994. if (rrdev && test_bit(Faulty, &rrdev->flags))
  995. rrdev = NULL;
  996. r10_bio->devs[i].bio = NULL;
  997. r10_bio->devs[i].repl_bio = NULL;
  998. if (!rdev || test_bit(Faulty, &rdev->flags)) {
  999. set_bit(R10BIO_Degraded, &r10_bio->state);
  1000. continue;
  1001. }
  1002. if (test_bit(WriteErrorSeen, &rdev->flags)) {
  1003. sector_t first_bad;
  1004. sector_t dev_sector = r10_bio->devs[i].addr;
  1005. int bad_sectors;
  1006. int is_bad;
  1007. is_bad = is_badblock(rdev, dev_sector,
  1008. max_sectors,
  1009. &first_bad, &bad_sectors);
  1010. if (is_bad < 0) {
  1011. /* Mustn't write here until the bad block
  1012. * is acknowledged
  1013. */
  1014. atomic_inc(&rdev->nr_pending);
  1015. set_bit(BlockedBadBlocks, &rdev->flags);
  1016. blocked_rdev = rdev;
  1017. break;
  1018. }
  1019. if (is_bad && first_bad <= dev_sector) {
  1020. /* Cannot write here at all */
  1021. bad_sectors -= (dev_sector - first_bad);
  1022. if (bad_sectors < max_sectors)
  1023. /* Mustn't write more than bad_sectors
  1024. * to other devices yet
  1025. */
  1026. max_sectors = bad_sectors;
  1027. /* We don't set R10BIO_Degraded as that
  1028. * only applies if the disk is missing,
  1029. * so it might be re-added, and we want to
  1030. * know to recover this chunk.
  1031. * In this case the device is here, and the
  1032. * fact that this chunk is not in-sync is
  1033. * recorded in the bad block log.
  1034. */
  1035. continue;
  1036. }
  1037. if (is_bad) {
  1038. int good_sectors = first_bad - dev_sector;
  1039. if (good_sectors < max_sectors)
  1040. max_sectors = good_sectors;
  1041. }
  1042. }
  1043. r10_bio->devs[i].bio = bio;
  1044. atomic_inc(&rdev->nr_pending);
  1045. if (rrdev) {
  1046. r10_bio->devs[i].repl_bio = bio;
  1047. atomic_inc(&rrdev->nr_pending);
  1048. }
  1049. }
  1050. rcu_read_unlock();
  1051. if (unlikely(blocked_rdev)) {
  1052. /* Have to wait for this device to get unblocked, then retry */
  1053. int j;
  1054. int d;
  1055. for (j = 0; j < i; j++) {
  1056. if (r10_bio->devs[j].bio) {
  1057. d = r10_bio->devs[j].devnum;
  1058. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1059. }
  1060. if (r10_bio->devs[j].repl_bio) {
  1061. struct md_rdev *rdev;
  1062. d = r10_bio->devs[j].devnum;
  1063. rdev = conf->mirrors[d].replacement;
  1064. if (!rdev) {
  1065. /* Race with remove_disk */
  1066. smp_mb();
  1067. rdev = conf->mirrors[d].rdev;
  1068. }
  1069. rdev_dec_pending(rdev, mddev);
  1070. }
  1071. }
  1072. allow_barrier(conf);
  1073. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1074. wait_barrier(conf);
  1075. goto retry_write;
  1076. }
  1077. if (max_sectors < r10_bio->sectors) {
  1078. /* We are splitting this into multiple parts, so
  1079. * we need to prepare for allocating another r10_bio.
  1080. */
  1081. r10_bio->sectors = max_sectors;
  1082. spin_lock_irq(&conf->device_lock);
  1083. if (bio->bi_phys_segments == 0)
  1084. bio->bi_phys_segments = 2;
  1085. else
  1086. bio->bi_phys_segments++;
  1087. spin_unlock_irq(&conf->device_lock);
  1088. }
  1089. sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
  1090. atomic_set(&r10_bio->remaining, 1);
  1091. bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
  1092. for (i = 0; i < conf->copies; i++) {
  1093. struct bio *mbio;
  1094. int d = r10_bio->devs[i].devnum;
  1095. if (!r10_bio->devs[i].bio)
  1096. continue;
  1097. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1098. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1099. max_sectors);
  1100. r10_bio->devs[i].bio = mbio;
  1101. mbio->bi_sector = (r10_bio->devs[i].addr+
  1102. conf->mirrors[d].rdev->data_offset);
  1103. mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1104. mbio->bi_end_io = raid10_end_write_request;
  1105. mbio->bi_rw = WRITE | do_sync | do_fua;
  1106. mbio->bi_private = r10_bio;
  1107. atomic_inc(&r10_bio->remaining);
  1108. spin_lock_irqsave(&conf->device_lock, flags);
  1109. bio_list_add(&conf->pending_bio_list, mbio);
  1110. conf->pending_count++;
  1111. spin_unlock_irqrestore(&conf->device_lock, flags);
  1112. if (!r10_bio->devs[i].repl_bio)
  1113. continue;
  1114. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1115. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1116. max_sectors);
  1117. r10_bio->devs[i].repl_bio = mbio;
  1118. /* We are actively writing to the original device
  1119. * so it cannot disappear, so the replacement cannot
  1120. * become NULL here
  1121. */
  1122. mbio->bi_sector = (r10_bio->devs[i].addr+
  1123. conf->mirrors[d].replacement->data_offset);
  1124. mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
  1125. mbio->bi_end_io = raid10_end_write_request;
  1126. mbio->bi_rw = WRITE | do_sync | do_fua;
  1127. mbio->bi_private = r10_bio;
  1128. atomic_inc(&r10_bio->remaining);
  1129. spin_lock_irqsave(&conf->device_lock, flags);
  1130. bio_list_add(&conf->pending_bio_list, mbio);
  1131. conf->pending_count++;
  1132. spin_unlock_irqrestore(&conf->device_lock, flags);
  1133. }
  1134. /* Don't remove the bias on 'remaining' (one_write_done) until
  1135. * after checking if we need to go around again.
  1136. */
  1137. if (sectors_handled < (bio->bi_size >> 9)) {
  1138. one_write_done(r10_bio);
  1139. /* We need another r10_bio. It has already been counted
  1140. * in bio->bi_phys_segments.
  1141. */
  1142. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1143. r10_bio->master_bio = bio;
  1144. r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  1145. r10_bio->mddev = mddev;
  1146. r10_bio->sector = bio->bi_sector + sectors_handled;
  1147. r10_bio->state = 0;
  1148. goto retry_write;
  1149. }
  1150. one_write_done(r10_bio);
  1151. /* In case raid10d snuck in to freeze_array */
  1152. wake_up(&conf->wait_barrier);
  1153. if (do_sync || !mddev->bitmap || !plugged)
  1154. md_wakeup_thread(mddev->thread);
  1155. }
  1156. static void status(struct seq_file *seq, struct mddev *mddev)
  1157. {
  1158. struct r10conf *conf = mddev->private;
  1159. int i;
  1160. if (conf->near_copies < conf->raid_disks)
  1161. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1162. if (conf->near_copies > 1)
  1163. seq_printf(seq, " %d near-copies", conf->near_copies);
  1164. if (conf->far_copies > 1) {
  1165. if (conf->far_offset)
  1166. seq_printf(seq, " %d offset-copies", conf->far_copies);
  1167. else
  1168. seq_printf(seq, " %d far-copies", conf->far_copies);
  1169. }
  1170. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  1171. conf->raid_disks - mddev->degraded);
  1172. for (i = 0; i < conf->raid_disks; i++)
  1173. seq_printf(seq, "%s",
  1174. conf->mirrors[i].rdev &&
  1175. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  1176. seq_printf(seq, "]");
  1177. }
  1178. /* check if there are enough drives for
  1179. * every block to appear on atleast one.
  1180. * Don't consider the device numbered 'ignore'
  1181. * as we might be about to remove it.
  1182. */
  1183. static int enough(struct r10conf *conf, int ignore)
  1184. {
  1185. int first = 0;
  1186. do {
  1187. int n = conf->copies;
  1188. int cnt = 0;
  1189. while (n--) {
  1190. if (conf->mirrors[first].rdev &&
  1191. first != ignore)
  1192. cnt++;
  1193. first = (first+1) % conf->raid_disks;
  1194. }
  1195. if (cnt == 0)
  1196. return 0;
  1197. } while (first != 0);
  1198. return 1;
  1199. }
  1200. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1201. {
  1202. char b[BDEVNAME_SIZE];
  1203. struct r10conf *conf = mddev->private;
  1204. /*
  1205. * If it is not operational, then we have already marked it as dead
  1206. * else if it is the last working disks, ignore the error, let the
  1207. * next level up know.
  1208. * else mark the drive as failed
  1209. */
  1210. if (test_bit(In_sync, &rdev->flags)
  1211. && !enough(conf, rdev->raid_disk))
  1212. /*
  1213. * Don't fail the drive, just return an IO error.
  1214. */
  1215. return;
  1216. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1217. unsigned long flags;
  1218. spin_lock_irqsave(&conf->device_lock, flags);
  1219. mddev->degraded++;
  1220. spin_unlock_irqrestore(&conf->device_lock, flags);
  1221. /*
  1222. * if recovery is running, make sure it aborts.
  1223. */
  1224. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1225. }
  1226. set_bit(Blocked, &rdev->flags);
  1227. set_bit(Faulty, &rdev->flags);
  1228. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1229. printk(KERN_ALERT
  1230. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1231. "md/raid10:%s: Operation continuing on %d devices.\n",
  1232. mdname(mddev), bdevname(rdev->bdev, b),
  1233. mdname(mddev), conf->raid_disks - mddev->degraded);
  1234. }
  1235. static void print_conf(struct r10conf *conf)
  1236. {
  1237. int i;
  1238. struct mirror_info *tmp;
  1239. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1240. if (!conf) {
  1241. printk(KERN_DEBUG "(!conf)\n");
  1242. return;
  1243. }
  1244. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  1245. conf->raid_disks);
  1246. for (i = 0; i < conf->raid_disks; i++) {
  1247. char b[BDEVNAME_SIZE];
  1248. tmp = conf->mirrors + i;
  1249. if (tmp->rdev)
  1250. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1251. i, !test_bit(In_sync, &tmp->rdev->flags),
  1252. !test_bit(Faulty, &tmp->rdev->flags),
  1253. bdevname(tmp->rdev->bdev,b));
  1254. }
  1255. }
  1256. static void close_sync(struct r10conf *conf)
  1257. {
  1258. wait_barrier(conf);
  1259. allow_barrier(conf);
  1260. mempool_destroy(conf->r10buf_pool);
  1261. conf->r10buf_pool = NULL;
  1262. }
  1263. static int raid10_spare_active(struct mddev *mddev)
  1264. {
  1265. int i;
  1266. struct r10conf *conf = mddev->private;
  1267. struct mirror_info *tmp;
  1268. int count = 0;
  1269. unsigned long flags;
  1270. /*
  1271. * Find all non-in_sync disks within the RAID10 configuration
  1272. * and mark them in_sync
  1273. */
  1274. for (i = 0; i < conf->raid_disks; i++) {
  1275. tmp = conf->mirrors + i;
  1276. if (tmp->replacement
  1277. && tmp->replacement->recovery_offset == MaxSector
  1278. && !test_bit(Faulty, &tmp->replacement->flags)
  1279. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1280. /* Replacement has just become active */
  1281. if (!tmp->rdev
  1282. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1283. count++;
  1284. if (tmp->rdev) {
  1285. /* Replaced device not technically faulty,
  1286. * but we need to be sure it gets removed
  1287. * and never re-added.
  1288. */
  1289. set_bit(Faulty, &tmp->rdev->flags);
  1290. sysfs_notify_dirent_safe(
  1291. tmp->rdev->sysfs_state);
  1292. }
  1293. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1294. } else if (tmp->rdev
  1295. && !test_bit(Faulty, &tmp->rdev->flags)
  1296. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1297. count++;
  1298. sysfs_notify_dirent(tmp->rdev->sysfs_state);
  1299. }
  1300. }
  1301. spin_lock_irqsave(&conf->device_lock, flags);
  1302. mddev->degraded -= count;
  1303. spin_unlock_irqrestore(&conf->device_lock, flags);
  1304. print_conf(conf);
  1305. return count;
  1306. }
  1307. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1308. {
  1309. struct r10conf *conf = mddev->private;
  1310. int err = -EEXIST;
  1311. int mirror;
  1312. int first = 0;
  1313. int last = conf->raid_disks - 1;
  1314. if (mddev->recovery_cp < MaxSector)
  1315. /* only hot-add to in-sync arrays, as recovery is
  1316. * very different from resync
  1317. */
  1318. return -EBUSY;
  1319. if (!enough(conf, -1))
  1320. return -EINVAL;
  1321. if (rdev->raid_disk >= 0)
  1322. first = last = rdev->raid_disk;
  1323. if (rdev->saved_raid_disk >= first &&
  1324. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1325. mirror = rdev->saved_raid_disk;
  1326. else
  1327. mirror = first;
  1328. for ( ; mirror <= last ; mirror++) {
  1329. struct mirror_info *p = &conf->mirrors[mirror];
  1330. if (p->recovery_disabled == mddev->recovery_disabled)
  1331. continue;
  1332. if (p->rdev) {
  1333. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1334. p->replacement != NULL)
  1335. continue;
  1336. clear_bit(In_sync, &rdev->flags);
  1337. set_bit(Replacement, &rdev->flags);
  1338. rdev->raid_disk = mirror;
  1339. err = 0;
  1340. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1341. rdev->data_offset << 9);
  1342. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  1343. blk_queue_max_segments(mddev->queue, 1);
  1344. blk_queue_segment_boundary(mddev->queue,
  1345. PAGE_CACHE_SIZE - 1);
  1346. }
  1347. conf->fullsync = 1;
  1348. rcu_assign_pointer(p->replacement, rdev);
  1349. break;
  1350. }
  1351. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1352. rdev->data_offset << 9);
  1353. /* as we don't honour merge_bvec_fn, we must
  1354. * never risk violating it, so limit
  1355. * ->max_segments to one lying with a single
  1356. * page, as a one page request is never in
  1357. * violation.
  1358. */
  1359. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  1360. blk_queue_max_segments(mddev->queue, 1);
  1361. blk_queue_segment_boundary(mddev->queue,
  1362. PAGE_CACHE_SIZE - 1);
  1363. }
  1364. p->head_position = 0;
  1365. p->recovery_disabled = mddev->recovery_disabled - 1;
  1366. rdev->raid_disk = mirror;
  1367. err = 0;
  1368. if (rdev->saved_raid_disk != mirror)
  1369. conf->fullsync = 1;
  1370. rcu_assign_pointer(p->rdev, rdev);
  1371. break;
  1372. }
  1373. md_integrity_add_rdev(rdev, mddev);
  1374. print_conf(conf);
  1375. return err;
  1376. }
  1377. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1378. {
  1379. struct r10conf *conf = mddev->private;
  1380. int err = 0;
  1381. int number = rdev->raid_disk;
  1382. struct md_rdev **rdevp;
  1383. struct mirror_info *p = conf->mirrors + number;
  1384. print_conf(conf);
  1385. if (rdev == p->rdev)
  1386. rdevp = &p->rdev;
  1387. else if (rdev == p->replacement)
  1388. rdevp = &p->replacement;
  1389. else
  1390. return 0;
  1391. if (test_bit(In_sync, &rdev->flags) ||
  1392. atomic_read(&rdev->nr_pending)) {
  1393. err = -EBUSY;
  1394. goto abort;
  1395. }
  1396. /* Only remove faulty devices if recovery
  1397. * is not possible.
  1398. */
  1399. if (!test_bit(Faulty, &rdev->flags) &&
  1400. mddev->recovery_disabled != p->recovery_disabled &&
  1401. (!p->replacement || p->replacement == rdev) &&
  1402. enough(conf, -1)) {
  1403. err = -EBUSY;
  1404. goto abort;
  1405. }
  1406. *rdevp = NULL;
  1407. synchronize_rcu();
  1408. if (atomic_read(&rdev->nr_pending)) {
  1409. /* lost the race, try later */
  1410. err = -EBUSY;
  1411. *rdevp = rdev;
  1412. goto abort;
  1413. } else if (p->replacement) {
  1414. /* We must have just cleared 'rdev' */
  1415. p->rdev = p->replacement;
  1416. clear_bit(Replacement, &p->replacement->flags);
  1417. smp_mb(); /* Make sure other CPUs may see both as identical
  1418. * but will never see neither -- if they are careful.
  1419. */
  1420. p->replacement = NULL;
  1421. clear_bit(WantReplacement, &rdev->flags);
  1422. } else
  1423. /* We might have just remove the Replacement as faulty
  1424. * Clear the flag just in case
  1425. */
  1426. clear_bit(WantReplacement, &rdev->flags);
  1427. err = md_integrity_register(mddev);
  1428. abort:
  1429. print_conf(conf);
  1430. return err;
  1431. }
  1432. static void end_sync_read(struct bio *bio, int error)
  1433. {
  1434. struct r10bio *r10_bio = bio->bi_private;
  1435. struct r10conf *conf = r10_bio->mddev->private;
  1436. int d;
  1437. d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1438. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1439. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1440. else
  1441. /* The write handler will notice the lack of
  1442. * R10BIO_Uptodate and record any errors etc
  1443. */
  1444. atomic_add(r10_bio->sectors,
  1445. &conf->mirrors[d].rdev->corrected_errors);
  1446. /* for reconstruct, we always reschedule after a read.
  1447. * for resync, only after all reads
  1448. */
  1449. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1450. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1451. atomic_dec_and_test(&r10_bio->remaining)) {
  1452. /* we have read all the blocks,
  1453. * do the comparison in process context in raid10d
  1454. */
  1455. reschedule_retry(r10_bio);
  1456. }
  1457. }
  1458. static void end_sync_request(struct r10bio *r10_bio)
  1459. {
  1460. struct mddev *mddev = r10_bio->mddev;
  1461. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1462. if (r10_bio->master_bio == NULL) {
  1463. /* the primary of several recovery bios */
  1464. sector_t s = r10_bio->sectors;
  1465. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1466. test_bit(R10BIO_WriteError, &r10_bio->state))
  1467. reschedule_retry(r10_bio);
  1468. else
  1469. put_buf(r10_bio);
  1470. md_done_sync(mddev, s, 1);
  1471. break;
  1472. } else {
  1473. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1474. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1475. test_bit(R10BIO_WriteError, &r10_bio->state))
  1476. reschedule_retry(r10_bio);
  1477. else
  1478. put_buf(r10_bio);
  1479. r10_bio = r10_bio2;
  1480. }
  1481. }
  1482. }
  1483. static void end_sync_write(struct bio *bio, int error)
  1484. {
  1485. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1486. struct r10bio *r10_bio = bio->bi_private;
  1487. struct mddev *mddev = r10_bio->mddev;
  1488. struct r10conf *conf = mddev->private;
  1489. int d;
  1490. sector_t first_bad;
  1491. int bad_sectors;
  1492. int slot;
  1493. int repl;
  1494. struct md_rdev *rdev = NULL;
  1495. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1496. if (repl)
  1497. rdev = conf->mirrors[d].replacement;
  1498. if (!rdev) {
  1499. smp_mb();
  1500. rdev = conf->mirrors[d].rdev;
  1501. }
  1502. if (!uptodate) {
  1503. if (repl)
  1504. md_error(mddev, rdev);
  1505. else {
  1506. set_bit(WriteErrorSeen, &rdev->flags);
  1507. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1508. set_bit(MD_RECOVERY_NEEDED,
  1509. &rdev->mddev->recovery);
  1510. set_bit(R10BIO_WriteError, &r10_bio->state);
  1511. }
  1512. } else if (is_badblock(rdev,
  1513. r10_bio->devs[slot].addr,
  1514. r10_bio->sectors,
  1515. &first_bad, &bad_sectors))
  1516. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1517. rdev_dec_pending(rdev, mddev);
  1518. end_sync_request(r10_bio);
  1519. }
  1520. /*
  1521. * Note: sync and recover and handled very differently for raid10
  1522. * This code is for resync.
  1523. * For resync, we read through virtual addresses and read all blocks.
  1524. * If there is any error, we schedule a write. The lowest numbered
  1525. * drive is authoritative.
  1526. * However requests come for physical address, so we need to map.
  1527. * For every physical address there are raid_disks/copies virtual addresses,
  1528. * which is always are least one, but is not necessarly an integer.
  1529. * This means that a physical address can span multiple chunks, so we may
  1530. * have to submit multiple io requests for a single sync request.
  1531. */
  1532. /*
  1533. * We check if all blocks are in-sync and only write to blocks that
  1534. * aren't in sync
  1535. */
  1536. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1537. {
  1538. struct r10conf *conf = mddev->private;
  1539. int i, first;
  1540. struct bio *tbio, *fbio;
  1541. atomic_set(&r10_bio->remaining, 1);
  1542. /* find the first device with a block */
  1543. for (i=0; i<conf->copies; i++)
  1544. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1545. break;
  1546. if (i == conf->copies)
  1547. goto done;
  1548. first = i;
  1549. fbio = r10_bio->devs[i].bio;
  1550. /* now find blocks with errors */
  1551. for (i=0 ; i < conf->copies ; i++) {
  1552. int j, d;
  1553. int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
  1554. tbio = r10_bio->devs[i].bio;
  1555. if (tbio->bi_end_io != end_sync_read)
  1556. continue;
  1557. if (i == first)
  1558. continue;
  1559. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1560. /* We know that the bi_io_vec layout is the same for
  1561. * both 'first' and 'i', so we just compare them.
  1562. * All vec entries are PAGE_SIZE;
  1563. */
  1564. for (j = 0; j < vcnt; j++)
  1565. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1566. page_address(tbio->bi_io_vec[j].bv_page),
  1567. PAGE_SIZE))
  1568. break;
  1569. if (j == vcnt)
  1570. continue;
  1571. mddev->resync_mismatches += r10_bio->sectors;
  1572. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1573. /* Don't fix anything. */
  1574. continue;
  1575. }
  1576. /* Ok, we need to write this bio, either to correct an
  1577. * inconsistency or to correct an unreadable block.
  1578. * First we need to fixup bv_offset, bv_len and
  1579. * bi_vecs, as the read request might have corrupted these
  1580. */
  1581. tbio->bi_vcnt = vcnt;
  1582. tbio->bi_size = r10_bio->sectors << 9;
  1583. tbio->bi_idx = 0;
  1584. tbio->bi_phys_segments = 0;
  1585. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1586. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1587. tbio->bi_next = NULL;
  1588. tbio->bi_rw = WRITE;
  1589. tbio->bi_private = r10_bio;
  1590. tbio->bi_sector = r10_bio->devs[i].addr;
  1591. for (j=0; j < vcnt ; j++) {
  1592. tbio->bi_io_vec[j].bv_offset = 0;
  1593. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1594. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1595. page_address(fbio->bi_io_vec[j].bv_page),
  1596. PAGE_SIZE);
  1597. }
  1598. tbio->bi_end_io = end_sync_write;
  1599. d = r10_bio->devs[i].devnum;
  1600. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1601. atomic_inc(&r10_bio->remaining);
  1602. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1603. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1604. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1605. generic_make_request(tbio);
  1606. }
  1607. /* Now write out to any replacement devices
  1608. * that are active
  1609. */
  1610. for (i = 0; i < conf->copies; i++) {
  1611. int j, d;
  1612. int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
  1613. tbio = r10_bio->devs[i].repl_bio;
  1614. if (!tbio || !tbio->bi_end_io)
  1615. continue;
  1616. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1617. && r10_bio->devs[i].bio != fbio)
  1618. for (j = 0; j < vcnt; j++)
  1619. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1620. page_address(fbio->bi_io_vec[j].bv_page),
  1621. PAGE_SIZE);
  1622. d = r10_bio->devs[i].devnum;
  1623. atomic_inc(&r10_bio->remaining);
  1624. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1625. tbio->bi_size >> 9);
  1626. generic_make_request(tbio);
  1627. }
  1628. done:
  1629. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1630. md_done_sync(mddev, r10_bio->sectors, 1);
  1631. put_buf(r10_bio);
  1632. }
  1633. }
  1634. /*
  1635. * Now for the recovery code.
  1636. * Recovery happens across physical sectors.
  1637. * We recover all non-is_sync drives by finding the virtual address of
  1638. * each, and then choose a working drive that also has that virt address.
  1639. * There is a separate r10_bio for each non-in_sync drive.
  1640. * Only the first two slots are in use. The first for reading,
  1641. * The second for writing.
  1642. *
  1643. */
  1644. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1645. {
  1646. /* We got a read error during recovery.
  1647. * We repeat the read in smaller page-sized sections.
  1648. * If a read succeeds, write it to the new device or record
  1649. * a bad block if we cannot.
  1650. * If a read fails, record a bad block on both old and
  1651. * new devices.
  1652. */
  1653. struct mddev *mddev = r10_bio->mddev;
  1654. struct r10conf *conf = mddev->private;
  1655. struct bio *bio = r10_bio->devs[0].bio;
  1656. sector_t sect = 0;
  1657. int sectors = r10_bio->sectors;
  1658. int idx = 0;
  1659. int dr = r10_bio->devs[0].devnum;
  1660. int dw = r10_bio->devs[1].devnum;
  1661. while (sectors) {
  1662. int s = sectors;
  1663. struct md_rdev *rdev;
  1664. sector_t addr;
  1665. int ok;
  1666. if (s > (PAGE_SIZE>>9))
  1667. s = PAGE_SIZE >> 9;
  1668. rdev = conf->mirrors[dr].rdev;
  1669. addr = r10_bio->devs[0].addr + sect,
  1670. ok = sync_page_io(rdev,
  1671. addr,
  1672. s << 9,
  1673. bio->bi_io_vec[idx].bv_page,
  1674. READ, false);
  1675. if (ok) {
  1676. rdev = conf->mirrors[dw].rdev;
  1677. addr = r10_bio->devs[1].addr + sect;
  1678. ok = sync_page_io(rdev,
  1679. addr,
  1680. s << 9,
  1681. bio->bi_io_vec[idx].bv_page,
  1682. WRITE, false);
  1683. if (!ok) {
  1684. set_bit(WriteErrorSeen, &rdev->flags);
  1685. if (!test_and_set_bit(WantReplacement,
  1686. &rdev->flags))
  1687. set_bit(MD_RECOVERY_NEEDED,
  1688. &rdev->mddev->recovery);
  1689. }
  1690. }
  1691. if (!ok) {
  1692. /* We don't worry if we cannot set a bad block -
  1693. * it really is bad so there is no loss in not
  1694. * recording it yet
  1695. */
  1696. rdev_set_badblocks(rdev, addr, s, 0);
  1697. if (rdev != conf->mirrors[dw].rdev) {
  1698. /* need bad block on destination too */
  1699. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  1700. addr = r10_bio->devs[1].addr + sect;
  1701. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  1702. if (!ok) {
  1703. /* just abort the recovery */
  1704. printk(KERN_NOTICE
  1705. "md/raid10:%s: recovery aborted"
  1706. " due to read error\n",
  1707. mdname(mddev));
  1708. conf->mirrors[dw].recovery_disabled
  1709. = mddev->recovery_disabled;
  1710. set_bit(MD_RECOVERY_INTR,
  1711. &mddev->recovery);
  1712. break;
  1713. }
  1714. }
  1715. }
  1716. sectors -= s;
  1717. sect += s;
  1718. idx++;
  1719. }
  1720. }
  1721. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1722. {
  1723. struct r10conf *conf = mddev->private;
  1724. int d;
  1725. struct bio *wbio, *wbio2;
  1726. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  1727. fix_recovery_read_error(r10_bio);
  1728. end_sync_request(r10_bio);
  1729. return;
  1730. }
  1731. /*
  1732. * share the pages with the first bio
  1733. * and submit the write request
  1734. */
  1735. d = r10_bio->devs[1].devnum;
  1736. wbio = r10_bio->devs[1].bio;
  1737. wbio2 = r10_bio->devs[1].repl_bio;
  1738. if (wbio->bi_end_io) {
  1739. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1740. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1741. generic_make_request(wbio);
  1742. }
  1743. if (wbio2 && wbio2->bi_end_io) {
  1744. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  1745. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1746. wbio2->bi_size >> 9);
  1747. generic_make_request(wbio2);
  1748. }
  1749. }
  1750. /*
  1751. * Used by fix_read_error() to decay the per rdev read_errors.
  1752. * We halve the read error count for every hour that has elapsed
  1753. * since the last recorded read error.
  1754. *
  1755. */
  1756. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  1757. {
  1758. struct timespec cur_time_mon;
  1759. unsigned long hours_since_last;
  1760. unsigned int read_errors = atomic_read(&rdev->read_errors);
  1761. ktime_get_ts(&cur_time_mon);
  1762. if (rdev->last_read_error.tv_sec == 0 &&
  1763. rdev->last_read_error.tv_nsec == 0) {
  1764. /* first time we've seen a read error */
  1765. rdev->last_read_error = cur_time_mon;
  1766. return;
  1767. }
  1768. hours_since_last = (cur_time_mon.tv_sec -
  1769. rdev->last_read_error.tv_sec) / 3600;
  1770. rdev->last_read_error = cur_time_mon;
  1771. /*
  1772. * if hours_since_last is > the number of bits in read_errors
  1773. * just set read errors to 0. We do this to avoid
  1774. * overflowing the shift of read_errors by hours_since_last.
  1775. */
  1776. if (hours_since_last >= 8 * sizeof(read_errors))
  1777. atomic_set(&rdev->read_errors, 0);
  1778. else
  1779. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  1780. }
  1781. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  1782. int sectors, struct page *page, int rw)
  1783. {
  1784. sector_t first_bad;
  1785. int bad_sectors;
  1786. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  1787. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  1788. return -1;
  1789. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  1790. /* success */
  1791. return 1;
  1792. if (rw == WRITE) {
  1793. set_bit(WriteErrorSeen, &rdev->flags);
  1794. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1795. set_bit(MD_RECOVERY_NEEDED,
  1796. &rdev->mddev->recovery);
  1797. }
  1798. /* need to record an error - either for the block or the device */
  1799. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1800. md_error(rdev->mddev, rdev);
  1801. return 0;
  1802. }
  1803. /*
  1804. * This is a kernel thread which:
  1805. *
  1806. * 1. Retries failed read operations on working mirrors.
  1807. * 2. Updates the raid superblock when problems encounter.
  1808. * 3. Performs writes following reads for array synchronising.
  1809. */
  1810. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  1811. {
  1812. int sect = 0; /* Offset from r10_bio->sector */
  1813. int sectors = r10_bio->sectors;
  1814. struct md_rdev*rdev;
  1815. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  1816. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  1817. /* still own a reference to this rdev, so it cannot
  1818. * have been cleared recently.
  1819. */
  1820. rdev = conf->mirrors[d].rdev;
  1821. if (test_bit(Faulty, &rdev->flags))
  1822. /* drive has already been failed, just ignore any
  1823. more fix_read_error() attempts */
  1824. return;
  1825. check_decay_read_errors(mddev, rdev);
  1826. atomic_inc(&rdev->read_errors);
  1827. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  1828. char b[BDEVNAME_SIZE];
  1829. bdevname(rdev->bdev, b);
  1830. printk(KERN_NOTICE
  1831. "md/raid10:%s: %s: Raid device exceeded "
  1832. "read_error threshold [cur %d:max %d]\n",
  1833. mdname(mddev), b,
  1834. atomic_read(&rdev->read_errors), max_read_errors);
  1835. printk(KERN_NOTICE
  1836. "md/raid10:%s: %s: Failing raid device\n",
  1837. mdname(mddev), b);
  1838. md_error(mddev, conf->mirrors[d].rdev);
  1839. return;
  1840. }
  1841. while(sectors) {
  1842. int s = sectors;
  1843. int sl = r10_bio->read_slot;
  1844. int success = 0;
  1845. int start;
  1846. if (s > (PAGE_SIZE>>9))
  1847. s = PAGE_SIZE >> 9;
  1848. rcu_read_lock();
  1849. do {
  1850. sector_t first_bad;
  1851. int bad_sectors;
  1852. d = r10_bio->devs[sl].devnum;
  1853. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1854. if (rdev &&
  1855. test_bit(In_sync, &rdev->flags) &&
  1856. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  1857. &first_bad, &bad_sectors) == 0) {
  1858. atomic_inc(&rdev->nr_pending);
  1859. rcu_read_unlock();
  1860. success = sync_page_io(rdev,
  1861. r10_bio->devs[sl].addr +
  1862. sect,
  1863. s<<9,
  1864. conf->tmppage, READ, false);
  1865. rdev_dec_pending(rdev, mddev);
  1866. rcu_read_lock();
  1867. if (success)
  1868. break;
  1869. }
  1870. sl++;
  1871. if (sl == conf->copies)
  1872. sl = 0;
  1873. } while (!success && sl != r10_bio->read_slot);
  1874. rcu_read_unlock();
  1875. if (!success) {
  1876. /* Cannot read from anywhere, just mark the block
  1877. * as bad on the first device to discourage future
  1878. * reads.
  1879. */
  1880. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  1881. rdev = conf->mirrors[dn].rdev;
  1882. if (!rdev_set_badblocks(
  1883. rdev,
  1884. r10_bio->devs[r10_bio->read_slot].addr
  1885. + sect,
  1886. s, 0))
  1887. md_error(mddev, rdev);
  1888. break;
  1889. }
  1890. start = sl;
  1891. /* write it back and re-read */
  1892. rcu_read_lock();
  1893. while (sl != r10_bio->read_slot) {
  1894. char b[BDEVNAME_SIZE];
  1895. if (sl==0)
  1896. sl = conf->copies;
  1897. sl--;
  1898. d = r10_bio->devs[sl].devnum;
  1899. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1900. if (!rdev ||
  1901. !test_bit(In_sync, &rdev->flags))
  1902. continue;
  1903. atomic_inc(&rdev->nr_pending);
  1904. rcu_read_unlock();
  1905. if (r10_sync_page_io(rdev,
  1906. r10_bio->devs[sl].addr +
  1907. sect,
  1908. s<<9, conf->tmppage, WRITE)
  1909. == 0) {
  1910. /* Well, this device is dead */
  1911. printk(KERN_NOTICE
  1912. "md/raid10:%s: read correction "
  1913. "write failed"
  1914. " (%d sectors at %llu on %s)\n",
  1915. mdname(mddev), s,
  1916. (unsigned long long)(
  1917. sect + rdev->data_offset),
  1918. bdevname(rdev->bdev, b));
  1919. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  1920. "drive\n",
  1921. mdname(mddev),
  1922. bdevname(rdev->bdev, b));
  1923. }
  1924. rdev_dec_pending(rdev, mddev);
  1925. rcu_read_lock();
  1926. }
  1927. sl = start;
  1928. while (sl != r10_bio->read_slot) {
  1929. char b[BDEVNAME_SIZE];
  1930. if (sl==0)
  1931. sl = conf->copies;
  1932. sl--;
  1933. d = r10_bio->devs[sl].devnum;
  1934. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1935. if (!rdev ||
  1936. !test_bit(In_sync, &rdev->flags))
  1937. continue;
  1938. atomic_inc(&rdev->nr_pending);
  1939. rcu_read_unlock();
  1940. switch (r10_sync_page_io(rdev,
  1941. r10_bio->devs[sl].addr +
  1942. sect,
  1943. s<<9, conf->tmppage,
  1944. READ)) {
  1945. case 0:
  1946. /* Well, this device is dead */
  1947. printk(KERN_NOTICE
  1948. "md/raid10:%s: unable to read back "
  1949. "corrected sectors"
  1950. " (%d sectors at %llu on %s)\n",
  1951. mdname(mddev), s,
  1952. (unsigned long long)(
  1953. sect + rdev->data_offset),
  1954. bdevname(rdev->bdev, b));
  1955. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  1956. "drive\n",
  1957. mdname(mddev),
  1958. bdevname(rdev->bdev, b));
  1959. break;
  1960. case 1:
  1961. printk(KERN_INFO
  1962. "md/raid10:%s: read error corrected"
  1963. " (%d sectors at %llu on %s)\n",
  1964. mdname(mddev), s,
  1965. (unsigned long long)(
  1966. sect + rdev->data_offset),
  1967. bdevname(rdev->bdev, b));
  1968. atomic_add(s, &rdev->corrected_errors);
  1969. }
  1970. rdev_dec_pending(rdev, mddev);
  1971. rcu_read_lock();
  1972. }
  1973. rcu_read_unlock();
  1974. sectors -= s;
  1975. sect += s;
  1976. }
  1977. }
  1978. static void bi_complete(struct bio *bio, int error)
  1979. {
  1980. complete((struct completion *)bio->bi_private);
  1981. }
  1982. static int submit_bio_wait(int rw, struct bio *bio)
  1983. {
  1984. struct completion event;
  1985. rw |= REQ_SYNC;
  1986. init_completion(&event);
  1987. bio->bi_private = &event;
  1988. bio->bi_end_io = bi_complete;
  1989. submit_bio(rw, bio);
  1990. wait_for_completion(&event);
  1991. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  1992. }
  1993. static int narrow_write_error(struct r10bio *r10_bio, int i)
  1994. {
  1995. struct bio *bio = r10_bio->master_bio;
  1996. struct mddev *mddev = r10_bio->mddev;
  1997. struct r10conf *conf = mddev->private;
  1998. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  1999. /* bio has the data to be written to slot 'i' where
  2000. * we just recently had a write error.
  2001. * We repeatedly clone the bio and trim down to one block,
  2002. * then try the write. Where the write fails we record
  2003. * a bad block.
  2004. * It is conceivable that the bio doesn't exactly align with
  2005. * blocks. We must handle this.
  2006. *
  2007. * We currently own a reference to the rdev.
  2008. */
  2009. int block_sectors;
  2010. sector_t sector;
  2011. int sectors;
  2012. int sect_to_write = r10_bio->sectors;
  2013. int ok = 1;
  2014. if (rdev->badblocks.shift < 0)
  2015. return 0;
  2016. block_sectors = 1 << rdev->badblocks.shift;
  2017. sector = r10_bio->sector;
  2018. sectors = ((r10_bio->sector + block_sectors)
  2019. & ~(sector_t)(block_sectors - 1))
  2020. - sector;
  2021. while (sect_to_write) {
  2022. struct bio *wbio;
  2023. if (sectors > sect_to_write)
  2024. sectors = sect_to_write;
  2025. /* Write at 'sector' for 'sectors' */
  2026. wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  2027. md_trim_bio(wbio, sector - bio->bi_sector, sectors);
  2028. wbio->bi_sector = (r10_bio->devs[i].addr+
  2029. rdev->data_offset+
  2030. (sector - r10_bio->sector));
  2031. wbio->bi_bdev = rdev->bdev;
  2032. if (submit_bio_wait(WRITE, wbio) == 0)
  2033. /* Failure! */
  2034. ok = rdev_set_badblocks(rdev, sector,
  2035. sectors, 0)
  2036. && ok;
  2037. bio_put(wbio);
  2038. sect_to_write -= sectors;
  2039. sector += sectors;
  2040. sectors = block_sectors;
  2041. }
  2042. return ok;
  2043. }
  2044. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2045. {
  2046. int slot = r10_bio->read_slot;
  2047. struct bio *bio;
  2048. struct r10conf *conf = mddev->private;
  2049. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2050. char b[BDEVNAME_SIZE];
  2051. unsigned long do_sync;
  2052. int max_sectors;
  2053. /* we got a read error. Maybe the drive is bad. Maybe just
  2054. * the block and we can fix it.
  2055. * We freeze all other IO, and try reading the block from
  2056. * other devices. When we find one, we re-write
  2057. * and check it that fixes the read error.
  2058. * This is all done synchronously while the array is
  2059. * frozen.
  2060. */
  2061. if (mddev->ro == 0) {
  2062. freeze_array(conf);
  2063. fix_read_error(conf, mddev, r10_bio);
  2064. unfreeze_array(conf);
  2065. }
  2066. rdev_dec_pending(rdev, mddev);
  2067. bio = r10_bio->devs[slot].bio;
  2068. bdevname(bio->bi_bdev, b);
  2069. r10_bio->devs[slot].bio =
  2070. mddev->ro ? IO_BLOCKED : NULL;
  2071. read_more:
  2072. rdev = read_balance(conf, r10_bio, &max_sectors);
  2073. if (rdev == NULL) {
  2074. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  2075. " read error for block %llu\n",
  2076. mdname(mddev), b,
  2077. (unsigned long long)r10_bio->sector);
  2078. raid_end_bio_io(r10_bio);
  2079. bio_put(bio);
  2080. return;
  2081. }
  2082. do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  2083. if (bio)
  2084. bio_put(bio);
  2085. slot = r10_bio->read_slot;
  2086. printk_ratelimited(
  2087. KERN_ERR
  2088. "md/raid10:%s: %s: redirecting"
  2089. "sector %llu to another mirror\n",
  2090. mdname(mddev),
  2091. bdevname(rdev->bdev, b),
  2092. (unsigned long long)r10_bio->sector);
  2093. bio = bio_clone_mddev(r10_bio->master_bio,
  2094. GFP_NOIO, mddev);
  2095. md_trim_bio(bio,
  2096. r10_bio->sector - bio->bi_sector,
  2097. max_sectors);
  2098. r10_bio->devs[slot].bio = bio;
  2099. r10_bio->devs[slot].rdev = rdev;
  2100. bio->bi_sector = r10_bio->devs[slot].addr
  2101. + rdev->data_offset;
  2102. bio->bi_bdev = rdev->bdev;
  2103. bio->bi_rw = READ | do_sync;
  2104. bio->bi_private = r10_bio;
  2105. bio->bi_end_io = raid10_end_read_request;
  2106. if (max_sectors < r10_bio->sectors) {
  2107. /* Drat - have to split this up more */
  2108. struct bio *mbio = r10_bio->master_bio;
  2109. int sectors_handled =
  2110. r10_bio->sector + max_sectors
  2111. - mbio->bi_sector;
  2112. r10_bio->sectors = max_sectors;
  2113. spin_lock_irq(&conf->device_lock);
  2114. if (mbio->bi_phys_segments == 0)
  2115. mbio->bi_phys_segments = 2;
  2116. else
  2117. mbio->bi_phys_segments++;
  2118. spin_unlock_irq(&conf->device_lock);
  2119. generic_make_request(bio);
  2120. bio = NULL;
  2121. r10_bio = mempool_alloc(conf->r10bio_pool,
  2122. GFP_NOIO);
  2123. r10_bio->master_bio = mbio;
  2124. r10_bio->sectors = (mbio->bi_size >> 9)
  2125. - sectors_handled;
  2126. r10_bio->state = 0;
  2127. set_bit(R10BIO_ReadError,
  2128. &r10_bio->state);
  2129. r10_bio->mddev = mddev;
  2130. r10_bio->sector = mbio->bi_sector
  2131. + sectors_handled;
  2132. goto read_more;
  2133. } else
  2134. generic_make_request(bio);
  2135. }
  2136. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2137. {
  2138. /* Some sort of write request has finished and it
  2139. * succeeded in writing where we thought there was a
  2140. * bad block. So forget the bad block.
  2141. * Or possibly if failed and we need to record
  2142. * a bad block.
  2143. */
  2144. int m;
  2145. struct md_rdev *rdev;
  2146. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2147. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2148. for (m = 0; m < conf->copies; m++) {
  2149. int dev = r10_bio->devs[m].devnum;
  2150. rdev = conf->mirrors[dev].rdev;
  2151. if (r10_bio->devs[m].bio == NULL)
  2152. continue;
  2153. if (test_bit(BIO_UPTODATE,
  2154. &r10_bio->devs[m].bio->bi_flags)) {
  2155. rdev_clear_badblocks(
  2156. rdev,
  2157. r10_bio->devs[m].addr,
  2158. r10_bio->sectors);
  2159. } else {
  2160. if (!rdev_set_badblocks(
  2161. rdev,
  2162. r10_bio->devs[m].addr,
  2163. r10_bio->sectors, 0))
  2164. md_error(conf->mddev, rdev);
  2165. }
  2166. rdev = conf->mirrors[dev].replacement;
  2167. if (r10_bio->devs[m].repl_bio == NULL)
  2168. continue;
  2169. if (test_bit(BIO_UPTODATE,
  2170. &r10_bio->devs[m].repl_bio->bi_flags)) {
  2171. rdev_clear_badblocks(
  2172. rdev,
  2173. r10_bio->devs[m].addr,
  2174. r10_bio->sectors);
  2175. } else {
  2176. if (!rdev_set_badblocks(
  2177. rdev,
  2178. r10_bio->devs[m].addr,
  2179. r10_bio->sectors, 0))
  2180. md_error(conf->mddev, rdev);
  2181. }
  2182. }
  2183. put_buf(r10_bio);
  2184. } else {
  2185. for (m = 0; m < conf->copies; m++) {
  2186. int dev = r10_bio->devs[m].devnum;
  2187. struct bio *bio = r10_bio->devs[m].bio;
  2188. rdev = conf->mirrors[dev].rdev;
  2189. if (bio == IO_MADE_GOOD) {
  2190. rdev_clear_badblocks(
  2191. rdev,
  2192. r10_bio->devs[m].addr,
  2193. r10_bio->sectors);
  2194. rdev_dec_pending(rdev, conf->mddev);
  2195. } else if (bio != NULL &&
  2196. !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  2197. if (!narrow_write_error(r10_bio, m)) {
  2198. md_error(conf->mddev, rdev);
  2199. set_bit(R10BIO_Degraded,
  2200. &r10_bio->state);
  2201. }
  2202. rdev_dec_pending(rdev, conf->mddev);
  2203. }
  2204. bio = r10_bio->devs[m].repl_bio;
  2205. rdev = conf->mirrors[dev].replacement;
  2206. if (rdev && bio == IO_MADE_GOOD) {
  2207. rdev_clear_badblocks(
  2208. rdev,
  2209. r10_bio->devs[m].addr,
  2210. r10_bio->sectors);
  2211. rdev_dec_pending(rdev, conf->mddev);
  2212. }
  2213. }
  2214. if (test_bit(R10BIO_WriteError,
  2215. &r10_bio->state))
  2216. close_write(r10_bio);
  2217. raid_end_bio_io(r10_bio);
  2218. }
  2219. }
  2220. static void raid10d(struct mddev *mddev)
  2221. {
  2222. struct r10bio *r10_bio;
  2223. unsigned long flags;
  2224. struct r10conf *conf = mddev->private;
  2225. struct list_head *head = &conf->retry_list;
  2226. struct blk_plug plug;
  2227. md_check_recovery(mddev);
  2228. blk_start_plug(&plug);
  2229. for (;;) {
  2230. flush_pending_writes(conf);
  2231. spin_lock_irqsave(&conf->device_lock, flags);
  2232. if (list_empty(head)) {
  2233. spin_unlock_irqrestore(&conf->device_lock, flags);
  2234. break;
  2235. }
  2236. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2237. list_del(head->prev);
  2238. conf->nr_queued--;
  2239. spin_unlock_irqrestore(&conf->device_lock, flags);
  2240. mddev = r10_bio->mddev;
  2241. conf = mddev->private;
  2242. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2243. test_bit(R10BIO_WriteError, &r10_bio->state))
  2244. handle_write_completed(conf, r10_bio);
  2245. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2246. sync_request_write(mddev, r10_bio);
  2247. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2248. recovery_request_write(mddev, r10_bio);
  2249. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2250. handle_read_error(mddev, r10_bio);
  2251. else {
  2252. /* just a partial read to be scheduled from a
  2253. * separate context
  2254. */
  2255. int slot = r10_bio->read_slot;
  2256. generic_make_request(r10_bio->devs[slot].bio);
  2257. }
  2258. cond_resched();
  2259. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2260. md_check_recovery(mddev);
  2261. }
  2262. blk_finish_plug(&plug);
  2263. }
  2264. static int init_resync(struct r10conf *conf)
  2265. {
  2266. int buffs;
  2267. int i;
  2268. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2269. BUG_ON(conf->r10buf_pool);
  2270. conf->have_replacement = 0;
  2271. for (i = 0; i < conf->raid_disks; i++)
  2272. if (conf->mirrors[i].replacement)
  2273. conf->have_replacement = 1;
  2274. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  2275. if (!conf->r10buf_pool)
  2276. return -ENOMEM;
  2277. conf->next_resync = 0;
  2278. return 0;
  2279. }
  2280. /*
  2281. * perform a "sync" on one "block"
  2282. *
  2283. * We need to make sure that no normal I/O request - particularly write
  2284. * requests - conflict with active sync requests.
  2285. *
  2286. * This is achieved by tracking pending requests and a 'barrier' concept
  2287. * that can be installed to exclude normal IO requests.
  2288. *
  2289. * Resync and recovery are handled very differently.
  2290. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2291. *
  2292. * For resync, we iterate over virtual addresses, read all copies,
  2293. * and update if there are differences. If only one copy is live,
  2294. * skip it.
  2295. * For recovery, we iterate over physical addresses, read a good
  2296. * value for each non-in_sync drive, and over-write.
  2297. *
  2298. * So, for recovery we may have several outstanding complex requests for a
  2299. * given address, one for each out-of-sync device. We model this by allocating
  2300. * a number of r10_bio structures, one for each out-of-sync device.
  2301. * As we setup these structures, we collect all bio's together into a list
  2302. * which we then process collectively to add pages, and then process again
  2303. * to pass to generic_make_request.
  2304. *
  2305. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2306. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2307. * has its remaining count decremented to 0, the whole complex operation
  2308. * is complete.
  2309. *
  2310. */
  2311. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
  2312. int *skipped, int go_faster)
  2313. {
  2314. struct r10conf *conf = mddev->private;
  2315. struct r10bio *r10_bio;
  2316. struct bio *biolist = NULL, *bio;
  2317. sector_t max_sector, nr_sectors;
  2318. int i;
  2319. int max_sync;
  2320. sector_t sync_blocks;
  2321. sector_t sectors_skipped = 0;
  2322. int chunks_skipped = 0;
  2323. if (!conf->r10buf_pool)
  2324. if (init_resync(conf))
  2325. return 0;
  2326. skipped:
  2327. max_sector = mddev->dev_sectors;
  2328. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2329. max_sector = mddev->resync_max_sectors;
  2330. if (sector_nr >= max_sector) {
  2331. /* If we aborted, we need to abort the
  2332. * sync on the 'current' bitmap chucks (there can
  2333. * be several when recovering multiple devices).
  2334. * as we may have started syncing it but not finished.
  2335. * We can find the current address in
  2336. * mddev->curr_resync, but for recovery,
  2337. * we need to convert that to several
  2338. * virtual addresses.
  2339. */
  2340. if (mddev->curr_resync < max_sector) { /* aborted */
  2341. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2342. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2343. &sync_blocks, 1);
  2344. else for (i=0; i<conf->raid_disks; i++) {
  2345. sector_t sect =
  2346. raid10_find_virt(conf, mddev->curr_resync, i);
  2347. bitmap_end_sync(mddev->bitmap, sect,
  2348. &sync_blocks, 1);
  2349. }
  2350. } else {
  2351. /* completed sync */
  2352. if ((!mddev->bitmap || conf->fullsync)
  2353. && conf->have_replacement
  2354. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2355. /* Completed a full sync so the replacements
  2356. * are now fully recovered.
  2357. */
  2358. for (i = 0; i < conf->raid_disks; i++)
  2359. if (conf->mirrors[i].replacement)
  2360. conf->mirrors[i].replacement
  2361. ->recovery_offset
  2362. = MaxSector;
  2363. }
  2364. conf->fullsync = 0;
  2365. }
  2366. bitmap_close_sync(mddev->bitmap);
  2367. close_sync(conf);
  2368. *skipped = 1;
  2369. return sectors_skipped;
  2370. }
  2371. if (chunks_skipped >= conf->raid_disks) {
  2372. /* if there has been nothing to do on any drive,
  2373. * then there is nothing to do at all..
  2374. */
  2375. *skipped = 1;
  2376. return (max_sector - sector_nr) + sectors_skipped;
  2377. }
  2378. if (max_sector > mddev->resync_max)
  2379. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2380. /* make sure whole request will fit in a chunk - if chunks
  2381. * are meaningful
  2382. */
  2383. if (conf->near_copies < conf->raid_disks &&
  2384. max_sector > (sector_nr | conf->chunk_mask))
  2385. max_sector = (sector_nr | conf->chunk_mask) + 1;
  2386. /*
  2387. * If there is non-resync activity waiting for us then
  2388. * put in a delay to throttle resync.
  2389. */
  2390. if (!go_faster && conf->nr_waiting)
  2391. msleep_interruptible(1000);
  2392. /* Again, very different code for resync and recovery.
  2393. * Both must result in an r10bio with a list of bios that
  2394. * have bi_end_io, bi_sector, bi_bdev set,
  2395. * and bi_private set to the r10bio.
  2396. * For recovery, we may actually create several r10bios
  2397. * with 2 bios in each, that correspond to the bios in the main one.
  2398. * In this case, the subordinate r10bios link back through a
  2399. * borrowed master_bio pointer, and the counter in the master
  2400. * includes a ref from each subordinate.
  2401. */
  2402. /* First, we decide what to do and set ->bi_end_io
  2403. * To end_sync_read if we want to read, and
  2404. * end_sync_write if we will want to write.
  2405. */
  2406. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2407. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2408. /* recovery... the complicated one */
  2409. int j;
  2410. r10_bio = NULL;
  2411. for (i=0 ; i<conf->raid_disks; i++) {
  2412. int still_degraded;
  2413. struct r10bio *rb2;
  2414. sector_t sect;
  2415. int must_sync;
  2416. int any_working;
  2417. struct mirror_info *mirror = &conf->mirrors[i];
  2418. if ((mirror->rdev == NULL ||
  2419. test_bit(In_sync, &mirror->rdev->flags))
  2420. &&
  2421. (mirror->replacement == NULL ||
  2422. test_bit(Faulty,
  2423. &mirror->replacement->flags)))
  2424. continue;
  2425. still_degraded = 0;
  2426. /* want to reconstruct this device */
  2427. rb2 = r10_bio;
  2428. sect = raid10_find_virt(conf, sector_nr, i);
  2429. /* Unless we are doing a full sync, or a replacement
  2430. * we only need to recover the block if it is set in
  2431. * the bitmap
  2432. */
  2433. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2434. &sync_blocks, 1);
  2435. if (sync_blocks < max_sync)
  2436. max_sync = sync_blocks;
  2437. if (!must_sync &&
  2438. mirror->replacement == NULL &&
  2439. !conf->fullsync) {
  2440. /* yep, skip the sync_blocks here, but don't assume
  2441. * that there will never be anything to do here
  2442. */
  2443. chunks_skipped = -1;
  2444. continue;
  2445. }
  2446. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2447. raise_barrier(conf, rb2 != NULL);
  2448. atomic_set(&r10_bio->remaining, 0);
  2449. r10_bio->master_bio = (struct bio*)rb2;
  2450. if (rb2)
  2451. atomic_inc(&rb2->remaining);
  2452. r10_bio->mddev = mddev;
  2453. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2454. r10_bio->sector = sect;
  2455. raid10_find_phys(conf, r10_bio);
  2456. /* Need to check if the array will still be
  2457. * degraded
  2458. */
  2459. for (j=0; j<conf->raid_disks; j++)
  2460. if (conf->mirrors[j].rdev == NULL ||
  2461. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  2462. still_degraded = 1;
  2463. break;
  2464. }
  2465. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2466. &sync_blocks, still_degraded);
  2467. any_working = 0;
  2468. for (j=0; j<conf->copies;j++) {
  2469. int k;
  2470. int d = r10_bio->devs[j].devnum;
  2471. sector_t from_addr, to_addr;
  2472. struct md_rdev *rdev;
  2473. sector_t sector, first_bad;
  2474. int bad_sectors;
  2475. if (!conf->mirrors[d].rdev ||
  2476. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  2477. continue;
  2478. /* This is where we read from */
  2479. any_working = 1;
  2480. rdev = conf->mirrors[d].rdev;
  2481. sector = r10_bio->devs[j].addr;
  2482. if (is_badblock(rdev, sector, max_sync,
  2483. &first_bad, &bad_sectors)) {
  2484. if (first_bad > sector)
  2485. max_sync = first_bad - sector;
  2486. else {
  2487. bad_sectors -= (sector
  2488. - first_bad);
  2489. if (max_sync > bad_sectors)
  2490. max_sync = bad_sectors;
  2491. continue;
  2492. }
  2493. }
  2494. bio = r10_bio->devs[0].bio;
  2495. bio->bi_next = biolist;
  2496. biolist = bio;
  2497. bio->bi_private = r10_bio;
  2498. bio->bi_end_io = end_sync_read;
  2499. bio->bi_rw = READ;
  2500. from_addr = r10_bio->devs[j].addr;
  2501. bio->bi_sector = from_addr + rdev->data_offset;
  2502. bio->bi_bdev = rdev->bdev;
  2503. atomic_inc(&rdev->nr_pending);
  2504. /* and we write to 'i' (if not in_sync) */
  2505. for (k=0; k<conf->copies; k++)
  2506. if (r10_bio->devs[k].devnum == i)
  2507. break;
  2508. BUG_ON(k == conf->copies);
  2509. to_addr = r10_bio->devs[k].addr;
  2510. r10_bio->devs[0].devnum = d;
  2511. r10_bio->devs[0].addr = from_addr;
  2512. r10_bio->devs[1].devnum = i;
  2513. r10_bio->devs[1].addr = to_addr;
  2514. rdev = mirror->rdev;
  2515. if (!test_bit(In_sync, &rdev->flags)) {
  2516. bio = r10_bio->devs[1].bio;
  2517. bio->bi_next = biolist;
  2518. biolist = bio;
  2519. bio->bi_private = r10_bio;
  2520. bio->bi_end_io = end_sync_write;
  2521. bio->bi_rw = WRITE;
  2522. bio->bi_sector = to_addr
  2523. + rdev->data_offset;
  2524. bio->bi_bdev = rdev->bdev;
  2525. atomic_inc(&r10_bio->remaining);
  2526. } else
  2527. r10_bio->devs[1].bio->bi_end_io = NULL;
  2528. /* and maybe write to replacement */
  2529. bio = r10_bio->devs[1].repl_bio;
  2530. if (bio)
  2531. bio->bi_end_io = NULL;
  2532. rdev = mirror->replacement;
  2533. /* Note: if rdev != NULL, then bio
  2534. * cannot be NULL as r10buf_pool_alloc will
  2535. * have allocated it.
  2536. * So the second test here is pointless.
  2537. * But it keeps semantic-checkers happy, and
  2538. * this comment keeps human reviewers
  2539. * happy.
  2540. */
  2541. if (rdev == NULL || bio == NULL ||
  2542. test_bit(Faulty, &rdev->flags))
  2543. break;
  2544. bio->bi_next = biolist;
  2545. biolist = bio;
  2546. bio->bi_private = r10_bio;
  2547. bio->bi_end_io = end_sync_write;
  2548. bio->bi_rw = WRITE;
  2549. bio->bi_sector = to_addr + rdev->data_offset;
  2550. bio->bi_bdev = rdev->bdev;
  2551. atomic_inc(&r10_bio->remaining);
  2552. break;
  2553. }
  2554. if (j == conf->copies) {
  2555. /* Cannot recover, so abort the recovery or
  2556. * record a bad block */
  2557. put_buf(r10_bio);
  2558. if (rb2)
  2559. atomic_dec(&rb2->remaining);
  2560. r10_bio = rb2;
  2561. if (any_working) {
  2562. /* problem is that there are bad blocks
  2563. * on other device(s)
  2564. */
  2565. int k;
  2566. for (k = 0; k < conf->copies; k++)
  2567. if (r10_bio->devs[k].devnum == i)
  2568. break;
  2569. if (!test_bit(In_sync,
  2570. &mirror->rdev->flags)
  2571. && !rdev_set_badblocks(
  2572. mirror->rdev,
  2573. r10_bio->devs[k].addr,
  2574. max_sync, 0))
  2575. any_working = 0;
  2576. if (mirror->replacement &&
  2577. !rdev_set_badblocks(
  2578. mirror->replacement,
  2579. r10_bio->devs[k].addr,
  2580. max_sync, 0))
  2581. any_working = 0;
  2582. }
  2583. if (!any_working) {
  2584. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2585. &mddev->recovery))
  2586. printk(KERN_INFO "md/raid10:%s: insufficient "
  2587. "working devices for recovery.\n",
  2588. mdname(mddev));
  2589. mirror->recovery_disabled
  2590. = mddev->recovery_disabled;
  2591. }
  2592. break;
  2593. }
  2594. }
  2595. if (biolist == NULL) {
  2596. while (r10_bio) {
  2597. struct r10bio *rb2 = r10_bio;
  2598. r10_bio = (struct r10bio*) rb2->master_bio;
  2599. rb2->master_bio = NULL;
  2600. put_buf(rb2);
  2601. }
  2602. goto giveup;
  2603. }
  2604. } else {
  2605. /* resync. Schedule a read for every block at this virt offset */
  2606. int count = 0;
  2607. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2608. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2609. &sync_blocks, mddev->degraded) &&
  2610. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  2611. &mddev->recovery)) {
  2612. /* We can skip this block */
  2613. *skipped = 1;
  2614. return sync_blocks + sectors_skipped;
  2615. }
  2616. if (sync_blocks < max_sync)
  2617. max_sync = sync_blocks;
  2618. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2619. r10_bio->mddev = mddev;
  2620. atomic_set(&r10_bio->remaining, 0);
  2621. raise_barrier(conf, 0);
  2622. conf->next_resync = sector_nr;
  2623. r10_bio->master_bio = NULL;
  2624. r10_bio->sector = sector_nr;
  2625. set_bit(R10BIO_IsSync, &r10_bio->state);
  2626. raid10_find_phys(conf, r10_bio);
  2627. r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
  2628. for (i=0; i<conf->copies; i++) {
  2629. int d = r10_bio->devs[i].devnum;
  2630. sector_t first_bad, sector;
  2631. int bad_sectors;
  2632. if (r10_bio->devs[i].repl_bio)
  2633. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  2634. bio = r10_bio->devs[i].bio;
  2635. bio->bi_end_io = NULL;
  2636. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2637. if (conf->mirrors[d].rdev == NULL ||
  2638. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  2639. continue;
  2640. sector = r10_bio->devs[i].addr;
  2641. if (is_badblock(conf->mirrors[d].rdev,
  2642. sector, max_sync,
  2643. &first_bad, &bad_sectors)) {
  2644. if (first_bad > sector)
  2645. max_sync = first_bad - sector;
  2646. else {
  2647. bad_sectors -= (sector - first_bad);
  2648. if (max_sync > bad_sectors)
  2649. max_sync = max_sync;
  2650. continue;
  2651. }
  2652. }
  2653. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2654. atomic_inc(&r10_bio->remaining);
  2655. bio->bi_next = biolist;
  2656. biolist = bio;
  2657. bio->bi_private = r10_bio;
  2658. bio->bi_end_io = end_sync_read;
  2659. bio->bi_rw = READ;
  2660. bio->bi_sector = sector +
  2661. conf->mirrors[d].rdev->data_offset;
  2662. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  2663. count++;
  2664. if (conf->mirrors[d].replacement == NULL ||
  2665. test_bit(Faulty,
  2666. &conf->mirrors[d].replacement->flags))
  2667. continue;
  2668. /* Need to set up for writing to the replacement */
  2669. bio = r10_bio->devs[i].repl_bio;
  2670. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2671. sector = r10_bio->devs[i].addr;
  2672. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2673. bio->bi_next = biolist;
  2674. biolist = bio;
  2675. bio->bi_private = r10_bio;
  2676. bio->bi_end_io = end_sync_write;
  2677. bio->bi_rw = WRITE;
  2678. bio->bi_sector = sector +
  2679. conf->mirrors[d].replacement->data_offset;
  2680. bio->bi_bdev = conf->mirrors[d].replacement->bdev;
  2681. count++;
  2682. }
  2683. if (count < 2) {
  2684. for (i=0; i<conf->copies; i++) {
  2685. int d = r10_bio->devs[i].devnum;
  2686. if (r10_bio->devs[i].bio->bi_end_io)
  2687. rdev_dec_pending(conf->mirrors[d].rdev,
  2688. mddev);
  2689. if (r10_bio->devs[i].repl_bio &&
  2690. r10_bio->devs[i].repl_bio->bi_end_io)
  2691. rdev_dec_pending(
  2692. conf->mirrors[d].replacement,
  2693. mddev);
  2694. }
  2695. put_buf(r10_bio);
  2696. biolist = NULL;
  2697. goto giveup;
  2698. }
  2699. }
  2700. for (bio = biolist; bio ; bio=bio->bi_next) {
  2701. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  2702. if (bio->bi_end_io)
  2703. bio->bi_flags |= 1 << BIO_UPTODATE;
  2704. bio->bi_vcnt = 0;
  2705. bio->bi_idx = 0;
  2706. bio->bi_phys_segments = 0;
  2707. bio->bi_size = 0;
  2708. }
  2709. nr_sectors = 0;
  2710. if (sector_nr + max_sync < max_sector)
  2711. max_sector = sector_nr + max_sync;
  2712. do {
  2713. struct page *page;
  2714. int len = PAGE_SIZE;
  2715. if (sector_nr + (len>>9) > max_sector)
  2716. len = (max_sector - sector_nr) << 9;
  2717. if (len == 0)
  2718. break;
  2719. for (bio= biolist ; bio ; bio=bio->bi_next) {
  2720. struct bio *bio2;
  2721. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2722. if (bio_add_page(bio, page, len, 0))
  2723. continue;
  2724. /* stop here */
  2725. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2726. for (bio2 = biolist;
  2727. bio2 && bio2 != bio;
  2728. bio2 = bio2->bi_next) {
  2729. /* remove last page from this bio */
  2730. bio2->bi_vcnt--;
  2731. bio2->bi_size -= len;
  2732. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  2733. }
  2734. goto bio_full;
  2735. }
  2736. nr_sectors += len>>9;
  2737. sector_nr += len>>9;
  2738. } while (biolist->bi_vcnt < RESYNC_PAGES);
  2739. bio_full:
  2740. r10_bio->sectors = nr_sectors;
  2741. while (biolist) {
  2742. bio = biolist;
  2743. biolist = biolist->bi_next;
  2744. bio->bi_next = NULL;
  2745. r10_bio = bio->bi_private;
  2746. r10_bio->sectors = nr_sectors;
  2747. if (bio->bi_end_io == end_sync_read) {
  2748. md_sync_acct(bio->bi_bdev, nr_sectors);
  2749. generic_make_request(bio);
  2750. }
  2751. }
  2752. if (sectors_skipped)
  2753. /* pretend they weren't skipped, it makes
  2754. * no important difference in this case
  2755. */
  2756. md_done_sync(mddev, sectors_skipped, 1);
  2757. return sectors_skipped + nr_sectors;
  2758. giveup:
  2759. /* There is nowhere to write, so all non-sync
  2760. * drives must be failed or in resync, all drives
  2761. * have a bad block, so try the next chunk...
  2762. */
  2763. if (sector_nr + max_sync < max_sector)
  2764. max_sector = sector_nr + max_sync;
  2765. sectors_skipped += (max_sector - sector_nr);
  2766. chunks_skipped ++;
  2767. sector_nr = max_sector;
  2768. goto skipped;
  2769. }
  2770. static sector_t
  2771. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  2772. {
  2773. sector_t size;
  2774. struct r10conf *conf = mddev->private;
  2775. if (!raid_disks)
  2776. raid_disks = conf->raid_disks;
  2777. if (!sectors)
  2778. sectors = conf->dev_sectors;
  2779. size = sectors >> conf->chunk_shift;
  2780. sector_div(size, conf->far_copies);
  2781. size = size * raid_disks;
  2782. sector_div(size, conf->near_copies);
  2783. return size << conf->chunk_shift;
  2784. }
  2785. static struct r10conf *setup_conf(struct mddev *mddev)
  2786. {
  2787. struct r10conf *conf = NULL;
  2788. int nc, fc, fo;
  2789. sector_t stride, size;
  2790. int err = -EINVAL;
  2791. if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
  2792. !is_power_of_2(mddev->new_chunk_sectors)) {
  2793. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  2794. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  2795. mdname(mddev), PAGE_SIZE);
  2796. goto out;
  2797. }
  2798. nc = mddev->new_layout & 255;
  2799. fc = (mddev->new_layout >> 8) & 255;
  2800. fo = mddev->new_layout & (1<<16);
  2801. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  2802. (mddev->new_layout >> 17)) {
  2803. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  2804. mdname(mddev), mddev->new_layout);
  2805. goto out;
  2806. }
  2807. err = -ENOMEM;
  2808. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  2809. if (!conf)
  2810. goto out;
  2811. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  2812. GFP_KERNEL);
  2813. if (!conf->mirrors)
  2814. goto out;
  2815. conf->tmppage = alloc_page(GFP_KERNEL);
  2816. if (!conf->tmppage)
  2817. goto out;
  2818. conf->raid_disks = mddev->raid_disks;
  2819. conf->near_copies = nc;
  2820. conf->far_copies = fc;
  2821. conf->copies = nc*fc;
  2822. conf->far_offset = fo;
  2823. conf->chunk_mask = mddev->new_chunk_sectors - 1;
  2824. conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
  2825. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  2826. r10bio_pool_free, conf);
  2827. if (!conf->r10bio_pool)
  2828. goto out;
  2829. size = mddev->dev_sectors >> conf->chunk_shift;
  2830. sector_div(size, fc);
  2831. size = size * conf->raid_disks;
  2832. sector_div(size, nc);
  2833. /* 'size' is now the number of chunks in the array */
  2834. /* calculate "used chunks per device" in 'stride' */
  2835. stride = size * conf->copies;
  2836. /* We need to round up when dividing by raid_disks to
  2837. * get the stride size.
  2838. */
  2839. stride += conf->raid_disks - 1;
  2840. sector_div(stride, conf->raid_disks);
  2841. conf->dev_sectors = stride << conf->chunk_shift;
  2842. if (fo)
  2843. stride = 1;
  2844. else
  2845. sector_div(stride, fc);
  2846. conf->stride = stride << conf->chunk_shift;
  2847. spin_lock_init(&conf->device_lock);
  2848. INIT_LIST_HEAD(&conf->retry_list);
  2849. spin_lock_init(&conf->resync_lock);
  2850. init_waitqueue_head(&conf->wait_barrier);
  2851. conf->thread = md_register_thread(raid10d, mddev, NULL);
  2852. if (!conf->thread)
  2853. goto out;
  2854. conf->mddev = mddev;
  2855. return conf;
  2856. out:
  2857. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  2858. mdname(mddev));
  2859. if (conf) {
  2860. if (conf->r10bio_pool)
  2861. mempool_destroy(conf->r10bio_pool);
  2862. kfree(conf->mirrors);
  2863. safe_put_page(conf->tmppage);
  2864. kfree(conf);
  2865. }
  2866. return ERR_PTR(err);
  2867. }
  2868. static int run(struct mddev *mddev)
  2869. {
  2870. struct r10conf *conf;
  2871. int i, disk_idx, chunk_size;
  2872. struct mirror_info *disk;
  2873. struct md_rdev *rdev;
  2874. sector_t size;
  2875. /*
  2876. * copy the already verified devices into our private RAID10
  2877. * bookkeeping area. [whatever we allocate in run(),
  2878. * should be freed in stop()]
  2879. */
  2880. if (mddev->private == NULL) {
  2881. conf = setup_conf(mddev);
  2882. if (IS_ERR(conf))
  2883. return PTR_ERR(conf);
  2884. mddev->private = conf;
  2885. }
  2886. conf = mddev->private;
  2887. if (!conf)
  2888. goto out;
  2889. mddev->thread = conf->thread;
  2890. conf->thread = NULL;
  2891. chunk_size = mddev->chunk_sectors << 9;
  2892. blk_queue_io_min(mddev->queue, chunk_size);
  2893. if (conf->raid_disks % conf->near_copies)
  2894. blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
  2895. else
  2896. blk_queue_io_opt(mddev->queue, chunk_size *
  2897. (conf->raid_disks / conf->near_copies));
  2898. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2899. disk_idx = rdev->raid_disk;
  2900. if (disk_idx >= conf->raid_disks
  2901. || disk_idx < 0)
  2902. continue;
  2903. disk = conf->mirrors + disk_idx;
  2904. if (test_bit(Replacement, &rdev->flags)) {
  2905. if (disk->replacement)
  2906. goto out_free_conf;
  2907. disk->replacement = rdev;
  2908. } else {
  2909. if (disk->rdev)
  2910. goto out_free_conf;
  2911. disk->rdev = rdev;
  2912. }
  2913. disk->rdev = rdev;
  2914. disk_stack_limits(mddev->gendisk, rdev->bdev,
  2915. rdev->data_offset << 9);
  2916. /* as we don't honour merge_bvec_fn, we must never risk
  2917. * violating it, so limit max_segments to 1 lying
  2918. * within a single page.
  2919. */
  2920. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  2921. blk_queue_max_segments(mddev->queue, 1);
  2922. blk_queue_segment_boundary(mddev->queue,
  2923. PAGE_CACHE_SIZE - 1);
  2924. }
  2925. disk->head_position = 0;
  2926. }
  2927. /* need to check that every block has at least one working mirror */
  2928. if (!enough(conf, -1)) {
  2929. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  2930. mdname(mddev));
  2931. goto out_free_conf;
  2932. }
  2933. mddev->degraded = 0;
  2934. for (i = 0; i < conf->raid_disks; i++) {
  2935. disk = conf->mirrors + i;
  2936. if (!disk->rdev && disk->replacement) {
  2937. /* The replacement is all we have - use it */
  2938. disk->rdev = disk->replacement;
  2939. disk->replacement = NULL;
  2940. clear_bit(Replacement, &disk->rdev->flags);
  2941. }
  2942. if (!disk->rdev ||
  2943. !test_bit(In_sync, &disk->rdev->flags)) {
  2944. disk->head_position = 0;
  2945. mddev->degraded++;
  2946. if (disk->rdev)
  2947. conf->fullsync = 1;
  2948. }
  2949. disk->recovery_disabled = mddev->recovery_disabled - 1;
  2950. }
  2951. if (mddev->recovery_cp != MaxSector)
  2952. printk(KERN_NOTICE "md/raid10:%s: not clean"
  2953. " -- starting background reconstruction\n",
  2954. mdname(mddev));
  2955. printk(KERN_INFO
  2956. "md/raid10:%s: active with %d out of %d devices\n",
  2957. mdname(mddev), conf->raid_disks - mddev->degraded,
  2958. conf->raid_disks);
  2959. /*
  2960. * Ok, everything is just fine now
  2961. */
  2962. mddev->dev_sectors = conf->dev_sectors;
  2963. size = raid10_size(mddev, 0, 0);
  2964. md_set_array_sectors(mddev, size);
  2965. mddev->resync_max_sectors = size;
  2966. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  2967. mddev->queue->backing_dev_info.congested_data = mddev;
  2968. /* Calculate max read-ahead size.
  2969. * We need to readahead at least twice a whole stripe....
  2970. * maybe...
  2971. */
  2972. {
  2973. int stripe = conf->raid_disks *
  2974. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  2975. stripe /= conf->near_copies;
  2976. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  2977. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  2978. }
  2979. if (conf->near_copies < conf->raid_disks)
  2980. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  2981. if (md_integrity_register(mddev))
  2982. goto out_free_conf;
  2983. return 0;
  2984. out_free_conf:
  2985. md_unregister_thread(&mddev->thread);
  2986. if (conf->r10bio_pool)
  2987. mempool_destroy(conf->r10bio_pool);
  2988. safe_put_page(conf->tmppage);
  2989. kfree(conf->mirrors);
  2990. kfree(conf);
  2991. mddev->private = NULL;
  2992. out:
  2993. return -EIO;
  2994. }
  2995. static int stop(struct mddev *mddev)
  2996. {
  2997. struct r10conf *conf = mddev->private;
  2998. raise_barrier(conf, 0);
  2999. lower_barrier(conf);
  3000. md_unregister_thread(&mddev->thread);
  3001. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3002. if (conf->r10bio_pool)
  3003. mempool_destroy(conf->r10bio_pool);
  3004. kfree(conf->mirrors);
  3005. kfree(conf);
  3006. mddev->private = NULL;
  3007. return 0;
  3008. }
  3009. static void raid10_quiesce(struct mddev *mddev, int state)
  3010. {
  3011. struct r10conf *conf = mddev->private;
  3012. switch(state) {
  3013. case 1:
  3014. raise_barrier(conf, 0);
  3015. break;
  3016. case 0:
  3017. lower_barrier(conf);
  3018. break;
  3019. }
  3020. }
  3021. static void *raid10_takeover_raid0(struct mddev *mddev)
  3022. {
  3023. struct md_rdev *rdev;
  3024. struct r10conf *conf;
  3025. if (mddev->degraded > 0) {
  3026. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  3027. mdname(mddev));
  3028. return ERR_PTR(-EINVAL);
  3029. }
  3030. /* Set new parameters */
  3031. mddev->new_level = 10;
  3032. /* new layout: far_copies = 1, near_copies = 2 */
  3033. mddev->new_layout = (1<<8) + 2;
  3034. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3035. mddev->delta_disks = mddev->raid_disks;
  3036. mddev->raid_disks *= 2;
  3037. /* make sure it will be not marked as dirty */
  3038. mddev->recovery_cp = MaxSector;
  3039. conf = setup_conf(mddev);
  3040. if (!IS_ERR(conf)) {
  3041. list_for_each_entry(rdev, &mddev->disks, same_set)
  3042. if (rdev->raid_disk >= 0)
  3043. rdev->new_raid_disk = rdev->raid_disk * 2;
  3044. conf->barrier = 1;
  3045. }
  3046. return conf;
  3047. }
  3048. static void *raid10_takeover(struct mddev *mddev)
  3049. {
  3050. struct r0conf *raid0_conf;
  3051. /* raid10 can take over:
  3052. * raid0 - providing it has only two drives
  3053. */
  3054. if (mddev->level == 0) {
  3055. /* for raid0 takeover only one zone is supported */
  3056. raid0_conf = mddev->private;
  3057. if (raid0_conf->nr_strip_zones > 1) {
  3058. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  3059. " with more than one zone.\n",
  3060. mdname(mddev));
  3061. return ERR_PTR(-EINVAL);
  3062. }
  3063. return raid10_takeover_raid0(mddev);
  3064. }
  3065. return ERR_PTR(-EINVAL);
  3066. }
  3067. static struct md_personality raid10_personality =
  3068. {
  3069. .name = "raid10",
  3070. .level = 10,
  3071. .owner = THIS_MODULE,
  3072. .make_request = make_request,
  3073. .run = run,
  3074. .stop = stop,
  3075. .status = status,
  3076. .error_handler = error,
  3077. .hot_add_disk = raid10_add_disk,
  3078. .hot_remove_disk= raid10_remove_disk,
  3079. .spare_active = raid10_spare_active,
  3080. .sync_request = sync_request,
  3081. .quiesce = raid10_quiesce,
  3082. .size = raid10_size,
  3083. .takeover = raid10_takeover,
  3084. };
  3085. static int __init raid_init(void)
  3086. {
  3087. return register_md_personality(&raid10_personality);
  3088. }
  3089. static void raid_exit(void)
  3090. {
  3091. unregister_md_personality(&raid10_personality);
  3092. }
  3093. module_init(raid_init);
  3094. module_exit(raid_exit);
  3095. MODULE_LICENSE("GPL");
  3096. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  3097. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  3098. MODULE_ALIAS("md-raid10");
  3099. MODULE_ALIAS("md-level-10");
  3100. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);