raid10.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554
  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 <linux/kthread.h>
  27. #include "md.h"
  28. #include "raid10.h"
  29. #include "raid0.h"
  30. #include "bitmap.h"
  31. /*
  32. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  33. * The layout of data is defined by
  34. * chunk_size
  35. * raid_disks
  36. * near_copies (stored in low byte of layout)
  37. * far_copies (stored in second byte of layout)
  38. * far_offset (stored in bit 16 of layout )
  39. *
  40. * The data to be stored is divided into chunks using chunksize.
  41. * Each device is divided into far_copies sections.
  42. * In each section, chunks are laid out in a style similar to raid0, but
  43. * near_copies copies of each chunk is stored (each on a different drive).
  44. * The starting device for each section is offset near_copies from the starting
  45. * device of the previous section.
  46. * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
  47. * drive.
  48. * near_copies and far_copies must be at least one, and their product is at most
  49. * raid_disks.
  50. *
  51. * If far_offset is true, then the far_copies are handled a bit differently.
  52. * The copies are still in different stripes, but instead of be very far apart
  53. * on disk, there are adjacent stripes.
  54. */
  55. /*
  56. * Number of guaranteed r10bios in case of extreme VM load:
  57. */
  58. #define NR_RAID10_BIOS 256
  59. /* When there are this many requests queue to be written by
  60. * the raid10 thread, we become 'congested' to provide back-pressure
  61. * for writeback.
  62. */
  63. static int max_queued_requests = 1024;
  64. static void allow_barrier(struct r10conf *conf);
  65. static void lower_barrier(struct r10conf *conf);
  66. static int enough(struct r10conf *conf, int ignore);
  67. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  68. int *skipped);
  69. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
  70. static void end_reshape_write(struct bio *bio, int error);
  71. static void end_reshape(struct r10conf *conf);
  72. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  73. {
  74. struct r10conf *conf = data;
  75. int size = offsetof(struct r10bio, devs[conf->copies]);
  76. /* allocate a r10bio with room for raid_disks entries in the
  77. * bios array */
  78. return kzalloc(size, gfp_flags);
  79. }
  80. static void r10bio_pool_free(void *r10_bio, void *data)
  81. {
  82. kfree(r10_bio);
  83. }
  84. /* Maximum size of each resync request */
  85. #define RESYNC_BLOCK_SIZE (64*1024)
  86. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  87. /* amount of memory to reserve for resync requests */
  88. #define RESYNC_WINDOW (1024*1024)
  89. /* maximum number of concurrent requests, memory permitting */
  90. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  91. /*
  92. * When performing a resync, we need to read and compare, so
  93. * we need as many pages are there are copies.
  94. * When performing a recovery, we need 2 bios, one for read,
  95. * one for write (we recover only one drive per r10buf)
  96. *
  97. */
  98. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  99. {
  100. struct r10conf *conf = data;
  101. struct page *page;
  102. struct r10bio *r10_bio;
  103. struct bio *bio;
  104. int i, j;
  105. int nalloc;
  106. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  107. if (!r10_bio)
  108. return NULL;
  109. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
  110. test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
  111. nalloc = conf->copies; /* resync */
  112. else
  113. nalloc = 2; /* recovery */
  114. /*
  115. * Allocate bios.
  116. */
  117. for (j = nalloc ; j-- ; ) {
  118. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  119. if (!bio)
  120. goto out_free_bio;
  121. r10_bio->devs[j].bio = bio;
  122. if (!conf->have_replacement)
  123. continue;
  124. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  125. if (!bio)
  126. goto out_free_bio;
  127. r10_bio->devs[j].repl_bio = bio;
  128. }
  129. /*
  130. * Allocate RESYNC_PAGES data pages and attach them
  131. * where needed.
  132. */
  133. for (j = 0 ; j < nalloc; j++) {
  134. struct bio *rbio = r10_bio->devs[j].repl_bio;
  135. bio = r10_bio->devs[j].bio;
  136. for (i = 0; i < RESYNC_PAGES; i++) {
  137. if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
  138. &conf->mddev->recovery)) {
  139. /* we can share bv_page's during recovery
  140. * and reshape */
  141. struct bio *rbio = r10_bio->devs[0].bio;
  142. page = rbio->bi_io_vec[i].bv_page;
  143. get_page(page);
  144. } else
  145. page = alloc_page(gfp_flags);
  146. if (unlikely(!page))
  147. goto out_free_pages;
  148. bio->bi_io_vec[i].bv_page = page;
  149. if (rbio)
  150. rbio->bi_io_vec[i].bv_page = page;
  151. }
  152. }
  153. return r10_bio;
  154. out_free_pages:
  155. for ( ; i > 0 ; i--)
  156. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  157. while (j--)
  158. for (i = 0; i < RESYNC_PAGES ; i++)
  159. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  160. j = 0;
  161. out_free_bio:
  162. for ( ; j < nalloc; j++) {
  163. if (r10_bio->devs[j].bio)
  164. bio_put(r10_bio->devs[j].bio);
  165. if (r10_bio->devs[j].repl_bio)
  166. bio_put(r10_bio->devs[j].repl_bio);
  167. }
  168. r10bio_pool_free(r10_bio, conf);
  169. return NULL;
  170. }
  171. static void r10buf_pool_free(void *__r10_bio, void *data)
  172. {
  173. int i;
  174. struct r10conf *conf = data;
  175. struct r10bio *r10bio = __r10_bio;
  176. int j;
  177. for (j=0; j < conf->copies; j++) {
  178. struct bio *bio = r10bio->devs[j].bio;
  179. if (bio) {
  180. for (i = 0; i < RESYNC_PAGES; i++) {
  181. safe_put_page(bio->bi_io_vec[i].bv_page);
  182. bio->bi_io_vec[i].bv_page = NULL;
  183. }
  184. bio_put(bio);
  185. }
  186. bio = r10bio->devs[j].repl_bio;
  187. if (bio)
  188. bio_put(bio);
  189. }
  190. r10bio_pool_free(r10bio, conf);
  191. }
  192. static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
  193. {
  194. int i;
  195. for (i = 0; i < conf->copies; i++) {
  196. struct bio **bio = & r10_bio->devs[i].bio;
  197. if (!BIO_SPECIAL(*bio))
  198. bio_put(*bio);
  199. *bio = NULL;
  200. bio = &r10_bio->devs[i].repl_bio;
  201. if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
  202. bio_put(*bio);
  203. *bio = NULL;
  204. }
  205. }
  206. static void free_r10bio(struct r10bio *r10_bio)
  207. {
  208. struct r10conf *conf = r10_bio->mddev->private;
  209. put_all_bios(conf, r10_bio);
  210. mempool_free(r10_bio, conf->r10bio_pool);
  211. }
  212. static void put_buf(struct r10bio *r10_bio)
  213. {
  214. struct r10conf *conf = r10_bio->mddev->private;
  215. mempool_free(r10_bio, conf->r10buf_pool);
  216. lower_barrier(conf);
  217. }
  218. static void reschedule_retry(struct r10bio *r10_bio)
  219. {
  220. unsigned long flags;
  221. struct mddev *mddev = r10_bio->mddev;
  222. struct r10conf *conf = mddev->private;
  223. spin_lock_irqsave(&conf->device_lock, flags);
  224. list_add(&r10_bio->retry_list, &conf->retry_list);
  225. conf->nr_queued ++;
  226. spin_unlock_irqrestore(&conf->device_lock, flags);
  227. /* wake up frozen array... */
  228. wake_up(&conf->wait_barrier);
  229. md_wakeup_thread(mddev->thread);
  230. }
  231. /*
  232. * raid_end_bio_io() is called when we have finished servicing a mirrored
  233. * operation and are ready to return a success/failure code to the buffer
  234. * cache layer.
  235. */
  236. static void raid_end_bio_io(struct r10bio *r10_bio)
  237. {
  238. struct bio *bio = r10_bio->master_bio;
  239. int done;
  240. struct r10conf *conf = r10_bio->mddev->private;
  241. if (bio->bi_phys_segments) {
  242. unsigned long flags;
  243. spin_lock_irqsave(&conf->device_lock, flags);
  244. bio->bi_phys_segments--;
  245. done = (bio->bi_phys_segments == 0);
  246. spin_unlock_irqrestore(&conf->device_lock, flags);
  247. } else
  248. done = 1;
  249. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  250. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  251. if (done) {
  252. bio_endio(bio, 0);
  253. /*
  254. * Wake up any possible resync thread that waits for the device
  255. * to go idle.
  256. */
  257. allow_barrier(conf);
  258. }
  259. free_r10bio(r10_bio);
  260. }
  261. /*
  262. * Update disk head position estimator based on IRQ completion info.
  263. */
  264. static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  265. {
  266. struct r10conf *conf = r10_bio->mddev->private;
  267. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  268. r10_bio->devs[slot].addr + (r10_bio->sectors);
  269. }
  270. /*
  271. * Find the disk number which triggered given bio
  272. */
  273. static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
  274. struct bio *bio, int *slotp, int *replp)
  275. {
  276. int slot;
  277. int repl = 0;
  278. for (slot = 0; slot < conf->copies; slot++) {
  279. if (r10_bio->devs[slot].bio == bio)
  280. break;
  281. if (r10_bio->devs[slot].repl_bio == bio) {
  282. repl = 1;
  283. break;
  284. }
  285. }
  286. BUG_ON(slot == conf->copies);
  287. update_head_pos(slot, r10_bio);
  288. if (slotp)
  289. *slotp = slot;
  290. if (replp)
  291. *replp = repl;
  292. return r10_bio->devs[slot].devnum;
  293. }
  294. static void raid10_end_read_request(struct bio *bio, int error)
  295. {
  296. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  297. struct r10bio *r10_bio = bio->bi_private;
  298. int slot, dev;
  299. struct md_rdev *rdev;
  300. struct r10conf *conf = r10_bio->mddev->private;
  301. slot = r10_bio->read_slot;
  302. dev = r10_bio->devs[slot].devnum;
  303. rdev = r10_bio->devs[slot].rdev;
  304. /*
  305. * this branch is our 'one mirror IO has finished' event handler:
  306. */
  307. update_head_pos(slot, r10_bio);
  308. if (uptodate) {
  309. /*
  310. * Set R10BIO_Uptodate in our master bio, so that
  311. * we will return a good error code to the higher
  312. * levels even if IO on some other mirrored buffer fails.
  313. *
  314. * The 'master' represents the composite IO operation to
  315. * user-side. So if something waits for IO, then it will
  316. * wait for the 'master' bio.
  317. */
  318. set_bit(R10BIO_Uptodate, &r10_bio->state);
  319. } else {
  320. /* If all other devices that store this block have
  321. * failed, we want to return the error upwards rather
  322. * than fail the last device. Here we redefine
  323. * "uptodate" to mean "Don't want to retry"
  324. */
  325. unsigned long flags;
  326. spin_lock_irqsave(&conf->device_lock, flags);
  327. if (!enough(conf, rdev->raid_disk))
  328. uptodate = 1;
  329. spin_unlock_irqrestore(&conf->device_lock, flags);
  330. }
  331. if (uptodate) {
  332. raid_end_bio_io(r10_bio);
  333. rdev_dec_pending(rdev, conf->mddev);
  334. } else {
  335. /*
  336. * oops, read error - keep the refcount on the rdev
  337. */
  338. char b[BDEVNAME_SIZE];
  339. printk_ratelimited(KERN_ERR
  340. "md/raid10:%s: %s: rescheduling sector %llu\n",
  341. mdname(conf->mddev),
  342. bdevname(rdev->bdev, b),
  343. (unsigned long long)r10_bio->sector);
  344. set_bit(R10BIO_ReadError, &r10_bio->state);
  345. reschedule_retry(r10_bio);
  346. }
  347. }
  348. static void close_write(struct r10bio *r10_bio)
  349. {
  350. /* clear the bitmap if all writes complete successfully */
  351. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  352. r10_bio->sectors,
  353. !test_bit(R10BIO_Degraded, &r10_bio->state),
  354. 0);
  355. md_write_end(r10_bio->mddev);
  356. }
  357. static void one_write_done(struct r10bio *r10_bio)
  358. {
  359. if (atomic_dec_and_test(&r10_bio->remaining)) {
  360. if (test_bit(R10BIO_WriteError, &r10_bio->state))
  361. reschedule_retry(r10_bio);
  362. else {
  363. close_write(r10_bio);
  364. if (test_bit(R10BIO_MadeGood, &r10_bio->state))
  365. reschedule_retry(r10_bio);
  366. else
  367. raid_end_bio_io(r10_bio);
  368. }
  369. }
  370. }
  371. static void raid10_end_write_request(struct bio *bio, int error)
  372. {
  373. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  374. struct r10bio *r10_bio = bio->bi_private;
  375. int dev;
  376. int dec_rdev = 1;
  377. struct r10conf *conf = r10_bio->mddev->private;
  378. int slot, repl;
  379. struct md_rdev *rdev = NULL;
  380. dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  381. if (repl)
  382. rdev = conf->mirrors[dev].replacement;
  383. if (!rdev) {
  384. smp_rmb();
  385. repl = 0;
  386. rdev = conf->mirrors[dev].rdev;
  387. }
  388. /*
  389. * this branch is our 'one mirror IO has finished' event handler:
  390. */
  391. if (!uptodate) {
  392. if (repl)
  393. /* Never record new bad blocks to replacement,
  394. * just fail it.
  395. */
  396. md_error(rdev->mddev, rdev);
  397. else {
  398. set_bit(WriteErrorSeen, &rdev->flags);
  399. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  400. set_bit(MD_RECOVERY_NEEDED,
  401. &rdev->mddev->recovery);
  402. set_bit(R10BIO_WriteError, &r10_bio->state);
  403. dec_rdev = 0;
  404. }
  405. } else {
  406. /*
  407. * Set R10BIO_Uptodate in our master bio, so that
  408. * we will return a good error code for to the higher
  409. * levels even if IO on some other mirrored buffer fails.
  410. *
  411. * The 'master' represents the composite IO operation to
  412. * user-side. So if something waits for IO, then it will
  413. * wait for the 'master' bio.
  414. */
  415. sector_t first_bad;
  416. int bad_sectors;
  417. set_bit(R10BIO_Uptodate, &r10_bio->state);
  418. /* Maybe we can clear some bad blocks. */
  419. if (is_badblock(rdev,
  420. r10_bio->devs[slot].addr,
  421. r10_bio->sectors,
  422. &first_bad, &bad_sectors)) {
  423. bio_put(bio);
  424. if (repl)
  425. r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
  426. else
  427. r10_bio->devs[slot].bio = IO_MADE_GOOD;
  428. dec_rdev = 0;
  429. set_bit(R10BIO_MadeGood, &r10_bio->state);
  430. }
  431. }
  432. /*
  433. *
  434. * Let's see if all mirrored write operations have finished
  435. * already.
  436. */
  437. one_write_done(r10_bio);
  438. if (dec_rdev)
  439. rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
  440. }
  441. /*
  442. * RAID10 layout manager
  443. * As well as the chunksize and raid_disks count, there are two
  444. * parameters: near_copies and far_copies.
  445. * near_copies * far_copies must be <= raid_disks.
  446. * Normally one of these will be 1.
  447. * If both are 1, we get raid0.
  448. * If near_copies == raid_disks, we get raid1.
  449. *
  450. * Chunks are laid out in raid0 style with near_copies copies of the
  451. * first chunk, followed by near_copies copies of the next chunk and
  452. * so on.
  453. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  454. * as described above, we start again with a device offset of near_copies.
  455. * So we effectively have another copy of the whole array further down all
  456. * the drives, but with blocks on different drives.
  457. * With this layout, and block is never stored twice on the one device.
  458. *
  459. * raid10_find_phys finds the sector offset of a given virtual sector
  460. * on each device that it is on.
  461. *
  462. * raid10_find_virt does the reverse mapping, from a device and a
  463. * sector offset to a virtual address
  464. */
  465. static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
  466. {
  467. int n,f;
  468. sector_t sector;
  469. sector_t chunk;
  470. sector_t stripe;
  471. int dev;
  472. int slot = 0;
  473. /* now calculate first sector/dev */
  474. chunk = r10bio->sector >> geo->chunk_shift;
  475. sector = r10bio->sector & geo->chunk_mask;
  476. chunk *= geo->near_copies;
  477. stripe = chunk;
  478. dev = sector_div(stripe, geo->raid_disks);
  479. if (geo->far_offset)
  480. stripe *= geo->far_copies;
  481. sector += stripe << geo->chunk_shift;
  482. /* and calculate all the others */
  483. for (n = 0; n < geo->near_copies; n++) {
  484. int d = dev;
  485. sector_t s = sector;
  486. r10bio->devs[slot].addr = sector;
  487. r10bio->devs[slot].devnum = d;
  488. slot++;
  489. for (f = 1; f < geo->far_copies; f++) {
  490. d += geo->near_copies;
  491. if (d >= geo->raid_disks)
  492. d -= geo->raid_disks;
  493. s += geo->stride;
  494. r10bio->devs[slot].devnum = d;
  495. r10bio->devs[slot].addr = s;
  496. slot++;
  497. }
  498. dev++;
  499. if (dev >= geo->raid_disks) {
  500. dev = 0;
  501. sector += (geo->chunk_mask + 1);
  502. }
  503. }
  504. }
  505. static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
  506. {
  507. struct geom *geo = &conf->geo;
  508. if (conf->reshape_progress != MaxSector &&
  509. ((r10bio->sector >= conf->reshape_progress) !=
  510. conf->mddev->reshape_backwards)) {
  511. set_bit(R10BIO_Previous, &r10bio->state);
  512. geo = &conf->prev;
  513. } else
  514. clear_bit(R10BIO_Previous, &r10bio->state);
  515. __raid10_find_phys(geo, r10bio);
  516. }
  517. static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
  518. {
  519. sector_t offset, chunk, vchunk;
  520. /* Never use conf->prev as this is only called during resync
  521. * or recovery, so reshape isn't happening
  522. */
  523. struct geom *geo = &conf->geo;
  524. offset = sector & geo->chunk_mask;
  525. if (geo->far_offset) {
  526. int fc;
  527. chunk = sector >> geo->chunk_shift;
  528. fc = sector_div(chunk, geo->far_copies);
  529. dev -= fc * geo->near_copies;
  530. if (dev < 0)
  531. dev += geo->raid_disks;
  532. } else {
  533. while (sector >= geo->stride) {
  534. sector -= geo->stride;
  535. if (dev < geo->near_copies)
  536. dev += geo->raid_disks - geo->near_copies;
  537. else
  538. dev -= geo->near_copies;
  539. }
  540. chunk = sector >> geo->chunk_shift;
  541. }
  542. vchunk = chunk * geo->raid_disks + dev;
  543. sector_div(vchunk, geo->near_copies);
  544. return (vchunk << geo->chunk_shift) + offset;
  545. }
  546. /**
  547. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  548. * @q: request queue
  549. * @bvm: properties of new bio
  550. * @biovec: the request that could be merged to it.
  551. *
  552. * Return amount of bytes we can accept at this offset
  553. * This requires checking for end-of-chunk if near_copies != raid_disks,
  554. * and for subordinate merge_bvec_fns if merge_check_needed.
  555. */
  556. static int raid10_mergeable_bvec(struct request_queue *q,
  557. struct bvec_merge_data *bvm,
  558. struct bio_vec *biovec)
  559. {
  560. struct mddev *mddev = q->queuedata;
  561. struct r10conf *conf = mddev->private;
  562. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  563. int max;
  564. unsigned int chunk_sectors;
  565. unsigned int bio_sectors = bvm->bi_size >> 9;
  566. struct geom *geo = &conf->geo;
  567. chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
  568. if (conf->reshape_progress != MaxSector &&
  569. ((sector >= conf->reshape_progress) !=
  570. conf->mddev->reshape_backwards))
  571. geo = &conf->prev;
  572. if (geo->near_copies < geo->raid_disks) {
  573. max = (chunk_sectors - ((sector & (chunk_sectors - 1))
  574. + bio_sectors)) << 9;
  575. if (max < 0)
  576. /* bio_add cannot handle a negative return */
  577. max = 0;
  578. if (max <= biovec->bv_len && bio_sectors == 0)
  579. return biovec->bv_len;
  580. } else
  581. max = biovec->bv_len;
  582. if (mddev->merge_check_needed) {
  583. struct r10bio r10_bio;
  584. int s;
  585. if (conf->reshape_progress != MaxSector) {
  586. /* Cannot give any guidance during reshape */
  587. if (max <= biovec->bv_len && bio_sectors == 0)
  588. return biovec->bv_len;
  589. return 0;
  590. }
  591. r10_bio.sector = sector;
  592. raid10_find_phys(conf, &r10_bio);
  593. rcu_read_lock();
  594. for (s = 0; s < conf->copies; s++) {
  595. int disk = r10_bio.devs[s].devnum;
  596. struct md_rdev *rdev = rcu_dereference(
  597. conf->mirrors[disk].rdev);
  598. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  599. struct request_queue *q =
  600. bdev_get_queue(rdev->bdev);
  601. if (q->merge_bvec_fn) {
  602. bvm->bi_sector = r10_bio.devs[s].addr
  603. + rdev->data_offset;
  604. bvm->bi_bdev = rdev->bdev;
  605. max = min(max, q->merge_bvec_fn(
  606. q, bvm, biovec));
  607. }
  608. }
  609. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  610. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  611. struct request_queue *q =
  612. bdev_get_queue(rdev->bdev);
  613. if (q->merge_bvec_fn) {
  614. bvm->bi_sector = r10_bio.devs[s].addr
  615. + rdev->data_offset;
  616. bvm->bi_bdev = rdev->bdev;
  617. max = min(max, q->merge_bvec_fn(
  618. q, bvm, biovec));
  619. }
  620. }
  621. }
  622. rcu_read_unlock();
  623. }
  624. return max;
  625. }
  626. /*
  627. * This routine returns the disk from which the requested read should
  628. * be done. There is a per-array 'next expected sequential IO' sector
  629. * number - if this matches on the next IO then we use the last disk.
  630. * There is also a per-disk 'last know head position' sector that is
  631. * maintained from IRQ contexts, both the normal and the resync IO
  632. * completion handlers update this position correctly. If there is no
  633. * perfect sequential match then we pick the disk whose head is closest.
  634. *
  635. * If there are 2 mirrors in the same 2 devices, performance degrades
  636. * because position is mirror, not device based.
  637. *
  638. * The rdev for the device selected will have nr_pending incremented.
  639. */
  640. /*
  641. * FIXME: possibly should rethink readbalancing and do it differently
  642. * depending on near_copies / far_copies geometry.
  643. */
  644. static struct md_rdev *read_balance(struct r10conf *conf,
  645. struct r10bio *r10_bio,
  646. int *max_sectors)
  647. {
  648. const sector_t this_sector = r10_bio->sector;
  649. int disk, slot;
  650. int sectors = r10_bio->sectors;
  651. int best_good_sectors;
  652. sector_t new_distance, best_dist;
  653. struct md_rdev *rdev, *best_rdev;
  654. int do_balance;
  655. int best_slot;
  656. struct geom *geo = &conf->geo;
  657. raid10_find_phys(conf, r10_bio);
  658. rcu_read_lock();
  659. retry:
  660. sectors = r10_bio->sectors;
  661. best_slot = -1;
  662. best_rdev = NULL;
  663. best_dist = MaxSector;
  664. best_good_sectors = 0;
  665. do_balance = 1;
  666. /*
  667. * Check if we can balance. We can balance on the whole
  668. * device if no resync is going on (recovery is ok), or below
  669. * the resync window. We take the first readable disk when
  670. * above the resync window.
  671. */
  672. if (conf->mddev->recovery_cp < MaxSector
  673. && (this_sector + sectors >= conf->next_resync))
  674. do_balance = 0;
  675. for (slot = 0; slot < conf->copies ; slot++) {
  676. sector_t first_bad;
  677. int bad_sectors;
  678. sector_t dev_sector;
  679. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  680. continue;
  681. disk = r10_bio->devs[slot].devnum;
  682. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  683. if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
  684. test_bit(Unmerged, &rdev->flags) ||
  685. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  686. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  687. if (rdev == NULL ||
  688. test_bit(Faulty, &rdev->flags) ||
  689. test_bit(Unmerged, &rdev->flags))
  690. continue;
  691. if (!test_bit(In_sync, &rdev->flags) &&
  692. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  693. continue;
  694. dev_sector = r10_bio->devs[slot].addr;
  695. if (is_badblock(rdev, dev_sector, sectors,
  696. &first_bad, &bad_sectors)) {
  697. if (best_dist < MaxSector)
  698. /* Already have a better slot */
  699. continue;
  700. if (first_bad <= dev_sector) {
  701. /* Cannot read here. If this is the
  702. * 'primary' device, then we must not read
  703. * beyond 'bad_sectors' from another device.
  704. */
  705. bad_sectors -= (dev_sector - first_bad);
  706. if (!do_balance && sectors > bad_sectors)
  707. sectors = bad_sectors;
  708. if (best_good_sectors > sectors)
  709. best_good_sectors = sectors;
  710. } else {
  711. sector_t good_sectors =
  712. first_bad - dev_sector;
  713. if (good_sectors > best_good_sectors) {
  714. best_good_sectors = good_sectors;
  715. best_slot = slot;
  716. best_rdev = rdev;
  717. }
  718. if (!do_balance)
  719. /* Must read from here */
  720. break;
  721. }
  722. continue;
  723. } else
  724. best_good_sectors = sectors;
  725. if (!do_balance)
  726. break;
  727. /* This optimisation is debatable, and completely destroys
  728. * sequential read speed for 'far copies' arrays. So only
  729. * keep it for 'near' arrays, and review those later.
  730. */
  731. if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  732. break;
  733. /* for far > 1 always use the lowest address */
  734. if (geo->far_copies > 1)
  735. new_distance = r10_bio->devs[slot].addr;
  736. else
  737. new_distance = abs(r10_bio->devs[slot].addr -
  738. conf->mirrors[disk].head_position);
  739. if (new_distance < best_dist) {
  740. best_dist = new_distance;
  741. best_slot = slot;
  742. best_rdev = rdev;
  743. }
  744. }
  745. if (slot >= conf->copies) {
  746. slot = best_slot;
  747. rdev = best_rdev;
  748. }
  749. if (slot >= 0) {
  750. atomic_inc(&rdev->nr_pending);
  751. if (test_bit(Faulty, &rdev->flags)) {
  752. /* Cannot risk returning a device that failed
  753. * before we inc'ed nr_pending
  754. */
  755. rdev_dec_pending(rdev, conf->mddev);
  756. goto retry;
  757. }
  758. r10_bio->read_slot = slot;
  759. } else
  760. rdev = NULL;
  761. rcu_read_unlock();
  762. *max_sectors = best_good_sectors;
  763. return rdev;
  764. }
  765. static int raid10_congested(void *data, int bits)
  766. {
  767. struct mddev *mddev = data;
  768. struct r10conf *conf = mddev->private;
  769. int i, ret = 0;
  770. if ((bits & (1 << BDI_async_congested)) &&
  771. conf->pending_count >= max_queued_requests)
  772. return 1;
  773. if (mddev_congested(mddev, bits))
  774. return 1;
  775. rcu_read_lock();
  776. for (i = 0;
  777. (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
  778. && ret == 0;
  779. i++) {
  780. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  781. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  782. struct request_queue *q = bdev_get_queue(rdev->bdev);
  783. ret |= bdi_congested(&q->backing_dev_info, bits);
  784. }
  785. }
  786. rcu_read_unlock();
  787. return ret;
  788. }
  789. static void flush_pending_writes(struct r10conf *conf)
  790. {
  791. /* Any writes that have been queued but are awaiting
  792. * bitmap updates get flushed here.
  793. */
  794. spin_lock_irq(&conf->device_lock);
  795. if (conf->pending_bio_list.head) {
  796. struct bio *bio;
  797. bio = bio_list_get(&conf->pending_bio_list);
  798. conf->pending_count = 0;
  799. spin_unlock_irq(&conf->device_lock);
  800. /* flush any pending bitmap writes to disk
  801. * before proceeding w/ I/O */
  802. bitmap_unplug(conf->mddev->bitmap);
  803. wake_up(&conf->wait_barrier);
  804. while (bio) { /* submit pending writes */
  805. struct bio *next = bio->bi_next;
  806. bio->bi_next = NULL;
  807. generic_make_request(bio);
  808. bio = next;
  809. }
  810. } else
  811. spin_unlock_irq(&conf->device_lock);
  812. }
  813. /* Barriers....
  814. * Sometimes we need to suspend IO while we do something else,
  815. * either some resync/recovery, or reconfigure the array.
  816. * To do this we raise a 'barrier'.
  817. * The 'barrier' is a counter that can be raised multiple times
  818. * to count how many activities are happening which preclude
  819. * normal IO.
  820. * We can only raise the barrier if there is no pending IO.
  821. * i.e. if nr_pending == 0.
  822. * We choose only to raise the barrier if no-one is waiting for the
  823. * barrier to go down. This means that as soon as an IO request
  824. * is ready, no other operations which require a barrier will start
  825. * until the IO request has had a chance.
  826. *
  827. * So: regular IO calls 'wait_barrier'. When that returns there
  828. * is no backgroup IO happening, It must arrange to call
  829. * allow_barrier when it has finished its IO.
  830. * backgroup IO calls must call raise_barrier. Once that returns
  831. * there is no normal IO happeing. It must arrange to call
  832. * lower_barrier when the particular background IO completes.
  833. */
  834. static void raise_barrier(struct r10conf *conf, int force)
  835. {
  836. BUG_ON(force && !conf->barrier);
  837. spin_lock_irq(&conf->resync_lock);
  838. /* Wait until no block IO is waiting (unless 'force') */
  839. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  840. conf->resync_lock, );
  841. /* block any new IO from starting */
  842. conf->barrier++;
  843. /* Now wait for all pending IO to complete */
  844. wait_event_lock_irq(conf->wait_barrier,
  845. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  846. conf->resync_lock, );
  847. spin_unlock_irq(&conf->resync_lock);
  848. }
  849. static void lower_barrier(struct r10conf *conf)
  850. {
  851. unsigned long flags;
  852. spin_lock_irqsave(&conf->resync_lock, flags);
  853. conf->barrier--;
  854. spin_unlock_irqrestore(&conf->resync_lock, flags);
  855. wake_up(&conf->wait_barrier);
  856. }
  857. static void wait_barrier(struct r10conf *conf)
  858. {
  859. spin_lock_irq(&conf->resync_lock);
  860. if (conf->barrier) {
  861. conf->nr_waiting++;
  862. /* Wait for the barrier to drop.
  863. * However if there are already pending
  864. * requests (preventing the barrier from
  865. * rising completely), and the
  866. * pre-process bio queue isn't empty,
  867. * then don't wait, as we need to empty
  868. * that queue to get the nr_pending
  869. * count down.
  870. */
  871. wait_event_lock_irq(conf->wait_barrier,
  872. !conf->barrier ||
  873. (conf->nr_pending &&
  874. current->bio_list &&
  875. !bio_list_empty(current->bio_list)),
  876. conf->resync_lock,
  877. );
  878. conf->nr_waiting--;
  879. }
  880. conf->nr_pending++;
  881. spin_unlock_irq(&conf->resync_lock);
  882. }
  883. static void allow_barrier(struct r10conf *conf)
  884. {
  885. unsigned long flags;
  886. spin_lock_irqsave(&conf->resync_lock, flags);
  887. conf->nr_pending--;
  888. spin_unlock_irqrestore(&conf->resync_lock, flags);
  889. wake_up(&conf->wait_barrier);
  890. }
  891. static void freeze_array(struct r10conf *conf)
  892. {
  893. /* stop syncio and normal IO and wait for everything to
  894. * go quiet.
  895. * We increment barrier and nr_waiting, and then
  896. * wait until nr_pending match nr_queued+1
  897. * This is called in the context of one normal IO request
  898. * that has failed. Thus any sync request that might be pending
  899. * will be blocked by nr_pending, and we need to wait for
  900. * pending IO requests to complete or be queued for re-try.
  901. * Thus the number queued (nr_queued) plus this request (1)
  902. * must match the number of pending IOs (nr_pending) before
  903. * we continue.
  904. */
  905. spin_lock_irq(&conf->resync_lock);
  906. conf->barrier++;
  907. conf->nr_waiting++;
  908. wait_event_lock_irq(conf->wait_barrier,
  909. conf->nr_pending == conf->nr_queued+1,
  910. conf->resync_lock,
  911. flush_pending_writes(conf));
  912. spin_unlock_irq(&conf->resync_lock);
  913. }
  914. static void unfreeze_array(struct r10conf *conf)
  915. {
  916. /* reverse the effect of the freeze */
  917. spin_lock_irq(&conf->resync_lock);
  918. conf->barrier--;
  919. conf->nr_waiting--;
  920. wake_up(&conf->wait_barrier);
  921. spin_unlock_irq(&conf->resync_lock);
  922. }
  923. static sector_t choose_data_offset(struct r10bio *r10_bio,
  924. struct md_rdev *rdev)
  925. {
  926. if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
  927. test_bit(R10BIO_Previous, &r10_bio->state))
  928. return rdev->data_offset;
  929. else
  930. return rdev->new_data_offset;
  931. }
  932. static void make_request(struct mddev *mddev, struct bio * bio)
  933. {
  934. struct r10conf *conf = mddev->private;
  935. struct r10bio *r10_bio;
  936. struct bio *read_bio;
  937. int i;
  938. sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
  939. int chunk_sects = chunk_mask + 1;
  940. const int rw = bio_data_dir(bio);
  941. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  942. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  943. unsigned long flags;
  944. struct md_rdev *blocked_rdev;
  945. int plugged;
  946. int sectors_handled;
  947. int max_sectors;
  948. int sectors;
  949. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  950. md_flush_request(mddev, bio);
  951. return;
  952. }
  953. /* If this request crosses a chunk boundary, we need to
  954. * split it. This will only happen for 1 PAGE (or less) requests.
  955. */
  956. if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
  957. > chunk_sects
  958. && (conf->geo.near_copies < conf->geo.raid_disks
  959. || conf->prev.near_copies < conf->prev.raid_disks))) {
  960. struct bio_pair *bp;
  961. /* Sanity check -- queue functions should prevent this happening */
  962. if (bio->bi_vcnt != 1 ||
  963. bio->bi_idx != 0)
  964. goto bad_map;
  965. /* This is a one page bio that upper layers
  966. * refuse to split for us, so we need to split it.
  967. */
  968. bp = bio_split(bio,
  969. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  970. /* Each of these 'make_request' calls will call 'wait_barrier'.
  971. * If the first succeeds but the second blocks due to the resync
  972. * thread raising the barrier, we will deadlock because the
  973. * IO to the underlying device will be queued in generic_make_request
  974. * and will never complete, so will never reduce nr_pending.
  975. * So increment nr_waiting here so no new raise_barriers will
  976. * succeed, and so the second wait_barrier cannot block.
  977. */
  978. spin_lock_irq(&conf->resync_lock);
  979. conf->nr_waiting++;
  980. spin_unlock_irq(&conf->resync_lock);
  981. make_request(mddev, &bp->bio1);
  982. make_request(mddev, &bp->bio2);
  983. spin_lock_irq(&conf->resync_lock);
  984. conf->nr_waiting--;
  985. wake_up(&conf->wait_barrier);
  986. spin_unlock_irq(&conf->resync_lock);
  987. bio_pair_release(bp);
  988. return;
  989. bad_map:
  990. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  991. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  992. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  993. bio_io_error(bio);
  994. return;
  995. }
  996. md_write_start(mddev, bio);
  997. /*
  998. * Register the new request and wait if the reconstruction
  999. * thread has put up a bar for new requests.
  1000. * Continue immediately if no resync is active currently.
  1001. */
  1002. wait_barrier(conf);
  1003. sectors = bio->bi_size >> 9;
  1004. while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  1005. bio->bi_sector < conf->reshape_progress &&
  1006. bio->bi_sector + sectors > conf->reshape_progress) {
  1007. /* IO spans the reshape position. Need to wait for
  1008. * reshape to pass
  1009. */
  1010. allow_barrier(conf);
  1011. wait_event(conf->wait_barrier,
  1012. conf->reshape_progress <= bio->bi_sector ||
  1013. conf->reshape_progress >= bio->bi_sector + sectors);
  1014. wait_barrier(conf);
  1015. }
  1016. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  1017. bio_data_dir(bio) == WRITE &&
  1018. (mddev->reshape_backwards
  1019. ? (bio->bi_sector < conf->reshape_safe &&
  1020. bio->bi_sector + sectors > conf->reshape_progress)
  1021. : (bio->bi_sector + sectors > conf->reshape_safe &&
  1022. bio->bi_sector < conf->reshape_progress))) {
  1023. /* Need to update reshape_position in metadata */
  1024. mddev->reshape_position = conf->reshape_progress;
  1025. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1026. set_bit(MD_CHANGE_PENDING, &mddev->flags);
  1027. md_wakeup_thread(mddev->thread);
  1028. wait_event(mddev->sb_wait,
  1029. !test_bit(MD_CHANGE_PENDING, &mddev->flags));
  1030. conf->reshape_safe = mddev->reshape_position;
  1031. }
  1032. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1033. r10_bio->master_bio = bio;
  1034. r10_bio->sectors = sectors;
  1035. r10_bio->mddev = mddev;
  1036. r10_bio->sector = bio->bi_sector;
  1037. r10_bio->state = 0;
  1038. /* We might need to issue multiple reads to different
  1039. * devices if there are bad blocks around, so we keep
  1040. * track of the number of reads in bio->bi_phys_segments.
  1041. * If this is 0, there is only one r10_bio and no locking
  1042. * will be needed when the request completes. If it is
  1043. * non-zero, then it is the number of not-completed requests.
  1044. */
  1045. bio->bi_phys_segments = 0;
  1046. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  1047. if (rw == READ) {
  1048. /*
  1049. * read balancing logic:
  1050. */
  1051. struct md_rdev *rdev;
  1052. int slot;
  1053. read_again:
  1054. rdev = read_balance(conf, r10_bio, &max_sectors);
  1055. if (!rdev) {
  1056. raid_end_bio_io(r10_bio);
  1057. return;
  1058. }
  1059. slot = r10_bio->read_slot;
  1060. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1061. md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
  1062. max_sectors);
  1063. r10_bio->devs[slot].bio = read_bio;
  1064. r10_bio->devs[slot].rdev = rdev;
  1065. read_bio->bi_sector = r10_bio->devs[slot].addr +
  1066. choose_data_offset(r10_bio, rdev);
  1067. read_bio->bi_bdev = rdev->bdev;
  1068. read_bio->bi_end_io = raid10_end_read_request;
  1069. read_bio->bi_rw = READ | do_sync;
  1070. read_bio->bi_private = r10_bio;
  1071. if (max_sectors < r10_bio->sectors) {
  1072. /* Could not read all from this device, so we will
  1073. * need another r10_bio.
  1074. */
  1075. sectors_handled = (r10_bio->sectors + max_sectors
  1076. - bio->bi_sector);
  1077. r10_bio->sectors = max_sectors;
  1078. spin_lock_irq(&conf->device_lock);
  1079. if (bio->bi_phys_segments == 0)
  1080. bio->bi_phys_segments = 2;
  1081. else
  1082. bio->bi_phys_segments++;
  1083. spin_unlock(&conf->device_lock);
  1084. /* Cannot call generic_make_request directly
  1085. * as that will be queued in __generic_make_request
  1086. * and subsequent mempool_alloc might block
  1087. * waiting for it. so hand bio over to raid10d.
  1088. */
  1089. reschedule_retry(r10_bio);
  1090. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1091. r10_bio->master_bio = bio;
  1092. r10_bio->sectors = ((bio->bi_size >> 9)
  1093. - sectors_handled);
  1094. r10_bio->state = 0;
  1095. r10_bio->mddev = mddev;
  1096. r10_bio->sector = bio->bi_sector + sectors_handled;
  1097. goto read_again;
  1098. } else
  1099. generic_make_request(read_bio);
  1100. return;
  1101. }
  1102. /*
  1103. * WRITE:
  1104. */
  1105. if (conf->pending_count >= max_queued_requests) {
  1106. md_wakeup_thread(mddev->thread);
  1107. wait_event(conf->wait_barrier,
  1108. conf->pending_count < max_queued_requests);
  1109. }
  1110. /* first select target devices under rcu_lock and
  1111. * inc refcount on their rdev. Record them by setting
  1112. * bios[x] to bio
  1113. * If there are known/acknowledged bad blocks on any device
  1114. * on which we have seen a write error, we want to avoid
  1115. * writing to those blocks. This potentially requires several
  1116. * writes to write around the bad blocks. Each set of writes
  1117. * gets its own r10_bio with a set of bios attached. The number
  1118. * of r10_bios is recored in bio->bi_phys_segments just as with
  1119. * the read case.
  1120. */
  1121. plugged = mddev_check_plugged(mddev);
  1122. r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
  1123. raid10_find_phys(conf, r10_bio);
  1124. retry_write:
  1125. blocked_rdev = NULL;
  1126. rcu_read_lock();
  1127. max_sectors = r10_bio->sectors;
  1128. for (i = 0; i < conf->copies; i++) {
  1129. int d = r10_bio->devs[i].devnum;
  1130. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  1131. struct md_rdev *rrdev = rcu_dereference(
  1132. conf->mirrors[d].replacement);
  1133. if (rdev == rrdev)
  1134. rrdev = NULL;
  1135. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1136. atomic_inc(&rdev->nr_pending);
  1137. blocked_rdev = rdev;
  1138. break;
  1139. }
  1140. if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
  1141. atomic_inc(&rrdev->nr_pending);
  1142. blocked_rdev = rrdev;
  1143. break;
  1144. }
  1145. if (rrdev && (test_bit(Faulty, &rrdev->flags)
  1146. || test_bit(Unmerged, &rrdev->flags)))
  1147. rrdev = NULL;
  1148. r10_bio->devs[i].bio = NULL;
  1149. r10_bio->devs[i].repl_bio = NULL;
  1150. if (!rdev || test_bit(Faulty, &rdev->flags) ||
  1151. test_bit(Unmerged, &rdev->flags)) {
  1152. set_bit(R10BIO_Degraded, &r10_bio->state);
  1153. continue;
  1154. }
  1155. if (test_bit(WriteErrorSeen, &rdev->flags)) {
  1156. sector_t first_bad;
  1157. sector_t dev_sector = r10_bio->devs[i].addr;
  1158. int bad_sectors;
  1159. int is_bad;
  1160. is_bad = is_badblock(rdev, dev_sector,
  1161. max_sectors,
  1162. &first_bad, &bad_sectors);
  1163. if (is_bad < 0) {
  1164. /* Mustn't write here until the bad block
  1165. * is acknowledged
  1166. */
  1167. atomic_inc(&rdev->nr_pending);
  1168. set_bit(BlockedBadBlocks, &rdev->flags);
  1169. blocked_rdev = rdev;
  1170. break;
  1171. }
  1172. if (is_bad && first_bad <= dev_sector) {
  1173. /* Cannot write here at all */
  1174. bad_sectors -= (dev_sector - first_bad);
  1175. if (bad_sectors < max_sectors)
  1176. /* Mustn't write more than bad_sectors
  1177. * to other devices yet
  1178. */
  1179. max_sectors = bad_sectors;
  1180. /* We don't set R10BIO_Degraded as that
  1181. * only applies if the disk is missing,
  1182. * so it might be re-added, and we want to
  1183. * know to recover this chunk.
  1184. * In this case the device is here, and the
  1185. * fact that this chunk is not in-sync is
  1186. * recorded in the bad block log.
  1187. */
  1188. continue;
  1189. }
  1190. if (is_bad) {
  1191. int good_sectors = first_bad - dev_sector;
  1192. if (good_sectors < max_sectors)
  1193. max_sectors = good_sectors;
  1194. }
  1195. }
  1196. r10_bio->devs[i].bio = bio;
  1197. atomic_inc(&rdev->nr_pending);
  1198. if (rrdev) {
  1199. r10_bio->devs[i].repl_bio = bio;
  1200. atomic_inc(&rrdev->nr_pending);
  1201. }
  1202. }
  1203. rcu_read_unlock();
  1204. if (unlikely(blocked_rdev)) {
  1205. /* Have to wait for this device to get unblocked, then retry */
  1206. int j;
  1207. int d;
  1208. for (j = 0; j < i; j++) {
  1209. if (r10_bio->devs[j].bio) {
  1210. d = r10_bio->devs[j].devnum;
  1211. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1212. }
  1213. if (r10_bio->devs[j].repl_bio) {
  1214. struct md_rdev *rdev;
  1215. d = r10_bio->devs[j].devnum;
  1216. rdev = conf->mirrors[d].replacement;
  1217. if (!rdev) {
  1218. /* Race with remove_disk */
  1219. smp_mb();
  1220. rdev = conf->mirrors[d].rdev;
  1221. }
  1222. rdev_dec_pending(rdev, mddev);
  1223. }
  1224. }
  1225. allow_barrier(conf);
  1226. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1227. wait_barrier(conf);
  1228. goto retry_write;
  1229. }
  1230. if (max_sectors < r10_bio->sectors) {
  1231. /* We are splitting this into multiple parts, so
  1232. * we need to prepare for allocating another r10_bio.
  1233. */
  1234. r10_bio->sectors = max_sectors;
  1235. spin_lock_irq(&conf->device_lock);
  1236. if (bio->bi_phys_segments == 0)
  1237. bio->bi_phys_segments = 2;
  1238. else
  1239. bio->bi_phys_segments++;
  1240. spin_unlock_irq(&conf->device_lock);
  1241. }
  1242. sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
  1243. atomic_set(&r10_bio->remaining, 1);
  1244. bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
  1245. for (i = 0; i < conf->copies; i++) {
  1246. struct bio *mbio;
  1247. int d = r10_bio->devs[i].devnum;
  1248. if (!r10_bio->devs[i].bio)
  1249. continue;
  1250. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1251. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1252. max_sectors);
  1253. r10_bio->devs[i].bio = mbio;
  1254. mbio->bi_sector = (r10_bio->devs[i].addr+
  1255. choose_data_offset(r10_bio,
  1256. conf->mirrors[d].rdev));
  1257. mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1258. mbio->bi_end_io = raid10_end_write_request;
  1259. mbio->bi_rw = WRITE | do_sync | do_fua;
  1260. mbio->bi_private = r10_bio;
  1261. atomic_inc(&r10_bio->remaining);
  1262. spin_lock_irqsave(&conf->device_lock, flags);
  1263. bio_list_add(&conf->pending_bio_list, mbio);
  1264. conf->pending_count++;
  1265. spin_unlock_irqrestore(&conf->device_lock, flags);
  1266. if (!r10_bio->devs[i].repl_bio)
  1267. continue;
  1268. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1269. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1270. max_sectors);
  1271. r10_bio->devs[i].repl_bio = mbio;
  1272. /* We are actively writing to the original device
  1273. * so it cannot disappear, so the replacement cannot
  1274. * become NULL here
  1275. */
  1276. mbio->bi_sector = (r10_bio->devs[i].addr +
  1277. choose_data_offset(
  1278. r10_bio,
  1279. conf->mirrors[d].replacement));
  1280. mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
  1281. mbio->bi_end_io = raid10_end_write_request;
  1282. mbio->bi_rw = WRITE | do_sync | do_fua;
  1283. mbio->bi_private = r10_bio;
  1284. atomic_inc(&r10_bio->remaining);
  1285. spin_lock_irqsave(&conf->device_lock, flags);
  1286. bio_list_add(&conf->pending_bio_list, mbio);
  1287. conf->pending_count++;
  1288. spin_unlock_irqrestore(&conf->device_lock, flags);
  1289. }
  1290. /* Don't remove the bias on 'remaining' (one_write_done) until
  1291. * after checking if we need to go around again.
  1292. */
  1293. if (sectors_handled < (bio->bi_size >> 9)) {
  1294. one_write_done(r10_bio);
  1295. /* We need another r10_bio. It has already been counted
  1296. * in bio->bi_phys_segments.
  1297. */
  1298. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1299. r10_bio->master_bio = bio;
  1300. r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  1301. r10_bio->mddev = mddev;
  1302. r10_bio->sector = bio->bi_sector + sectors_handled;
  1303. r10_bio->state = 0;
  1304. goto retry_write;
  1305. }
  1306. one_write_done(r10_bio);
  1307. /* In case raid10d snuck in to freeze_array */
  1308. wake_up(&conf->wait_barrier);
  1309. if (do_sync || !mddev->bitmap || !plugged)
  1310. md_wakeup_thread(mddev->thread);
  1311. }
  1312. static void status(struct seq_file *seq, struct mddev *mddev)
  1313. {
  1314. struct r10conf *conf = mddev->private;
  1315. int i;
  1316. if (conf->geo.near_copies < conf->geo.raid_disks)
  1317. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1318. if (conf->geo.near_copies > 1)
  1319. seq_printf(seq, " %d near-copies", conf->geo.near_copies);
  1320. if (conf->geo.far_copies > 1) {
  1321. if (conf->geo.far_offset)
  1322. seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
  1323. else
  1324. seq_printf(seq, " %d far-copies", conf->geo.far_copies);
  1325. }
  1326. seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
  1327. conf->geo.raid_disks - mddev->degraded);
  1328. for (i = 0; i < conf->geo.raid_disks; i++)
  1329. seq_printf(seq, "%s",
  1330. conf->mirrors[i].rdev &&
  1331. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  1332. seq_printf(seq, "]");
  1333. }
  1334. /* check if there are enough drives for
  1335. * every block to appear on atleast one.
  1336. * Don't consider the device numbered 'ignore'
  1337. * as we might be about to remove it.
  1338. */
  1339. static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
  1340. {
  1341. int first = 0;
  1342. do {
  1343. int n = conf->copies;
  1344. int cnt = 0;
  1345. while (n--) {
  1346. if (conf->mirrors[first].rdev &&
  1347. first != ignore)
  1348. cnt++;
  1349. first = (first+1) % geo->raid_disks;
  1350. }
  1351. if (cnt == 0)
  1352. return 0;
  1353. } while (first != 0);
  1354. return 1;
  1355. }
  1356. static int enough(struct r10conf *conf, int ignore)
  1357. {
  1358. return _enough(conf, &conf->geo, ignore) &&
  1359. _enough(conf, &conf->prev, ignore);
  1360. }
  1361. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1362. {
  1363. char b[BDEVNAME_SIZE];
  1364. struct r10conf *conf = mddev->private;
  1365. /*
  1366. * If it is not operational, then we have already marked it as dead
  1367. * else if it is the last working disks, ignore the error, let the
  1368. * next level up know.
  1369. * else mark the drive as failed
  1370. */
  1371. if (test_bit(In_sync, &rdev->flags)
  1372. && !enough(conf, rdev->raid_disk))
  1373. /*
  1374. * Don't fail the drive, just return an IO error.
  1375. */
  1376. return;
  1377. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1378. unsigned long flags;
  1379. spin_lock_irqsave(&conf->device_lock, flags);
  1380. mddev->degraded++;
  1381. spin_unlock_irqrestore(&conf->device_lock, flags);
  1382. /*
  1383. * if recovery is running, make sure it aborts.
  1384. */
  1385. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1386. }
  1387. set_bit(Blocked, &rdev->flags);
  1388. set_bit(Faulty, &rdev->flags);
  1389. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1390. printk(KERN_ALERT
  1391. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1392. "md/raid10:%s: Operation continuing on %d devices.\n",
  1393. mdname(mddev), bdevname(rdev->bdev, b),
  1394. mdname(mddev), conf->geo.raid_disks - mddev->degraded);
  1395. }
  1396. static void print_conf(struct r10conf *conf)
  1397. {
  1398. int i;
  1399. struct mirror_info *tmp;
  1400. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1401. if (!conf) {
  1402. printk(KERN_DEBUG "(!conf)\n");
  1403. return;
  1404. }
  1405. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
  1406. conf->geo.raid_disks);
  1407. for (i = 0; i < conf->geo.raid_disks; i++) {
  1408. char b[BDEVNAME_SIZE];
  1409. tmp = conf->mirrors + i;
  1410. if (tmp->rdev)
  1411. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1412. i, !test_bit(In_sync, &tmp->rdev->flags),
  1413. !test_bit(Faulty, &tmp->rdev->flags),
  1414. bdevname(tmp->rdev->bdev,b));
  1415. }
  1416. }
  1417. static void close_sync(struct r10conf *conf)
  1418. {
  1419. wait_barrier(conf);
  1420. allow_barrier(conf);
  1421. mempool_destroy(conf->r10buf_pool);
  1422. conf->r10buf_pool = NULL;
  1423. }
  1424. static int raid10_spare_active(struct mddev *mddev)
  1425. {
  1426. int i;
  1427. struct r10conf *conf = mddev->private;
  1428. struct mirror_info *tmp;
  1429. int count = 0;
  1430. unsigned long flags;
  1431. /*
  1432. * Find all non-in_sync disks within the RAID10 configuration
  1433. * and mark them in_sync
  1434. */
  1435. for (i = 0; i < conf->geo.raid_disks; i++) {
  1436. tmp = conf->mirrors + i;
  1437. if (tmp->replacement
  1438. && tmp->replacement->recovery_offset == MaxSector
  1439. && !test_bit(Faulty, &tmp->replacement->flags)
  1440. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1441. /* Replacement has just become active */
  1442. if (!tmp->rdev
  1443. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1444. count++;
  1445. if (tmp->rdev) {
  1446. /* Replaced device not technically faulty,
  1447. * but we need to be sure it gets removed
  1448. * and never re-added.
  1449. */
  1450. set_bit(Faulty, &tmp->rdev->flags);
  1451. sysfs_notify_dirent_safe(
  1452. tmp->rdev->sysfs_state);
  1453. }
  1454. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1455. } else if (tmp->rdev
  1456. && !test_bit(Faulty, &tmp->rdev->flags)
  1457. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1458. count++;
  1459. sysfs_notify_dirent(tmp->rdev->sysfs_state);
  1460. }
  1461. }
  1462. spin_lock_irqsave(&conf->device_lock, flags);
  1463. mddev->degraded -= count;
  1464. spin_unlock_irqrestore(&conf->device_lock, flags);
  1465. print_conf(conf);
  1466. return count;
  1467. }
  1468. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1469. {
  1470. struct r10conf *conf = mddev->private;
  1471. int err = -EEXIST;
  1472. int mirror;
  1473. int first = 0;
  1474. int last = conf->geo.raid_disks - 1;
  1475. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1476. if (mddev->recovery_cp < MaxSector)
  1477. /* only hot-add to in-sync arrays, as recovery is
  1478. * very different from resync
  1479. */
  1480. return -EBUSY;
  1481. if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
  1482. return -EINVAL;
  1483. if (rdev->raid_disk >= 0)
  1484. first = last = rdev->raid_disk;
  1485. if (q->merge_bvec_fn) {
  1486. set_bit(Unmerged, &rdev->flags);
  1487. mddev->merge_check_needed = 1;
  1488. }
  1489. if (rdev->saved_raid_disk >= first &&
  1490. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1491. mirror = rdev->saved_raid_disk;
  1492. else
  1493. mirror = first;
  1494. for ( ; mirror <= last ; mirror++) {
  1495. struct mirror_info *p = &conf->mirrors[mirror];
  1496. if (p->recovery_disabled == mddev->recovery_disabled)
  1497. continue;
  1498. if (p->rdev) {
  1499. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1500. p->replacement != NULL)
  1501. continue;
  1502. clear_bit(In_sync, &rdev->flags);
  1503. set_bit(Replacement, &rdev->flags);
  1504. rdev->raid_disk = mirror;
  1505. err = 0;
  1506. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1507. rdev->data_offset << 9);
  1508. conf->fullsync = 1;
  1509. rcu_assign_pointer(p->replacement, rdev);
  1510. break;
  1511. }
  1512. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1513. rdev->data_offset << 9);
  1514. p->head_position = 0;
  1515. p->recovery_disabled = mddev->recovery_disabled - 1;
  1516. rdev->raid_disk = mirror;
  1517. err = 0;
  1518. if (rdev->saved_raid_disk != mirror)
  1519. conf->fullsync = 1;
  1520. rcu_assign_pointer(p->rdev, rdev);
  1521. break;
  1522. }
  1523. if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
  1524. /* Some requests might not have seen this new
  1525. * merge_bvec_fn. We must wait for them to complete
  1526. * before merging the device fully.
  1527. * First we make sure any code which has tested
  1528. * our function has submitted the request, then
  1529. * we wait for all outstanding requests to complete.
  1530. */
  1531. synchronize_sched();
  1532. raise_barrier(conf, 0);
  1533. lower_barrier(conf);
  1534. clear_bit(Unmerged, &rdev->flags);
  1535. }
  1536. md_integrity_add_rdev(rdev, mddev);
  1537. print_conf(conf);
  1538. return err;
  1539. }
  1540. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1541. {
  1542. struct r10conf *conf = mddev->private;
  1543. int err = 0;
  1544. int number = rdev->raid_disk;
  1545. struct md_rdev **rdevp;
  1546. struct mirror_info *p = conf->mirrors + number;
  1547. print_conf(conf);
  1548. if (rdev == p->rdev)
  1549. rdevp = &p->rdev;
  1550. else if (rdev == p->replacement)
  1551. rdevp = &p->replacement;
  1552. else
  1553. return 0;
  1554. if (test_bit(In_sync, &rdev->flags) ||
  1555. atomic_read(&rdev->nr_pending)) {
  1556. err = -EBUSY;
  1557. goto abort;
  1558. }
  1559. /* Only remove faulty devices if recovery
  1560. * is not possible.
  1561. */
  1562. if (!test_bit(Faulty, &rdev->flags) &&
  1563. mddev->recovery_disabled != p->recovery_disabled &&
  1564. (!p->replacement || p->replacement == rdev) &&
  1565. number < conf->geo.raid_disks &&
  1566. enough(conf, -1)) {
  1567. err = -EBUSY;
  1568. goto abort;
  1569. }
  1570. *rdevp = NULL;
  1571. synchronize_rcu();
  1572. if (atomic_read(&rdev->nr_pending)) {
  1573. /* lost the race, try later */
  1574. err = -EBUSY;
  1575. *rdevp = rdev;
  1576. goto abort;
  1577. } else if (p->replacement) {
  1578. /* We must have just cleared 'rdev' */
  1579. p->rdev = p->replacement;
  1580. clear_bit(Replacement, &p->replacement->flags);
  1581. smp_mb(); /* Make sure other CPUs may see both as identical
  1582. * but will never see neither -- if they are careful.
  1583. */
  1584. p->replacement = NULL;
  1585. clear_bit(WantReplacement, &rdev->flags);
  1586. } else
  1587. /* We might have just remove the Replacement as faulty
  1588. * Clear the flag just in case
  1589. */
  1590. clear_bit(WantReplacement, &rdev->flags);
  1591. err = md_integrity_register(mddev);
  1592. abort:
  1593. print_conf(conf);
  1594. return err;
  1595. }
  1596. static void end_sync_read(struct bio *bio, int error)
  1597. {
  1598. struct r10bio *r10_bio = bio->bi_private;
  1599. struct r10conf *conf = r10_bio->mddev->private;
  1600. int d;
  1601. if (bio == r10_bio->master_bio) {
  1602. /* this is a reshape read */
  1603. d = r10_bio->read_slot; /* really the read dev */
  1604. } else
  1605. d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1606. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1607. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1608. else
  1609. /* The write handler will notice the lack of
  1610. * R10BIO_Uptodate and record any errors etc
  1611. */
  1612. atomic_add(r10_bio->sectors,
  1613. &conf->mirrors[d].rdev->corrected_errors);
  1614. /* for reconstruct, we always reschedule after a read.
  1615. * for resync, only after all reads
  1616. */
  1617. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1618. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1619. atomic_dec_and_test(&r10_bio->remaining)) {
  1620. /* we have read all the blocks,
  1621. * do the comparison in process context in raid10d
  1622. */
  1623. reschedule_retry(r10_bio);
  1624. }
  1625. }
  1626. static void end_sync_request(struct r10bio *r10_bio)
  1627. {
  1628. struct mddev *mddev = r10_bio->mddev;
  1629. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1630. if (r10_bio->master_bio == NULL) {
  1631. /* the primary of several recovery bios */
  1632. sector_t s = r10_bio->sectors;
  1633. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1634. test_bit(R10BIO_WriteError, &r10_bio->state))
  1635. reschedule_retry(r10_bio);
  1636. else
  1637. put_buf(r10_bio);
  1638. md_done_sync(mddev, s, 1);
  1639. break;
  1640. } else {
  1641. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1642. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1643. test_bit(R10BIO_WriteError, &r10_bio->state))
  1644. reschedule_retry(r10_bio);
  1645. else
  1646. put_buf(r10_bio);
  1647. r10_bio = r10_bio2;
  1648. }
  1649. }
  1650. }
  1651. static void end_sync_write(struct bio *bio, int error)
  1652. {
  1653. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1654. struct r10bio *r10_bio = bio->bi_private;
  1655. struct mddev *mddev = r10_bio->mddev;
  1656. struct r10conf *conf = mddev->private;
  1657. int d;
  1658. sector_t first_bad;
  1659. int bad_sectors;
  1660. int slot;
  1661. int repl;
  1662. struct md_rdev *rdev = NULL;
  1663. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1664. if (repl)
  1665. rdev = conf->mirrors[d].replacement;
  1666. else
  1667. rdev = conf->mirrors[d].rdev;
  1668. if (!uptodate) {
  1669. if (repl)
  1670. md_error(mddev, rdev);
  1671. else {
  1672. set_bit(WriteErrorSeen, &rdev->flags);
  1673. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1674. set_bit(MD_RECOVERY_NEEDED,
  1675. &rdev->mddev->recovery);
  1676. set_bit(R10BIO_WriteError, &r10_bio->state);
  1677. }
  1678. } else if (is_badblock(rdev,
  1679. r10_bio->devs[slot].addr,
  1680. r10_bio->sectors,
  1681. &first_bad, &bad_sectors))
  1682. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1683. rdev_dec_pending(rdev, mddev);
  1684. end_sync_request(r10_bio);
  1685. }
  1686. /*
  1687. * Note: sync and recover and handled very differently for raid10
  1688. * This code is for resync.
  1689. * For resync, we read through virtual addresses and read all blocks.
  1690. * If there is any error, we schedule a write. The lowest numbered
  1691. * drive is authoritative.
  1692. * However requests come for physical address, so we need to map.
  1693. * For every physical address there are raid_disks/copies virtual addresses,
  1694. * which is always are least one, but is not necessarly an integer.
  1695. * This means that a physical address can span multiple chunks, so we may
  1696. * have to submit multiple io requests for a single sync request.
  1697. */
  1698. /*
  1699. * We check if all blocks are in-sync and only write to blocks that
  1700. * aren't in sync
  1701. */
  1702. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1703. {
  1704. struct r10conf *conf = mddev->private;
  1705. int i, first;
  1706. struct bio *tbio, *fbio;
  1707. int vcnt;
  1708. atomic_set(&r10_bio->remaining, 1);
  1709. /* find the first device with a block */
  1710. for (i=0; i<conf->copies; i++)
  1711. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1712. break;
  1713. if (i == conf->copies)
  1714. goto done;
  1715. first = i;
  1716. fbio = r10_bio->devs[i].bio;
  1717. vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
  1718. /* now find blocks with errors */
  1719. for (i=0 ; i < conf->copies ; i++) {
  1720. int j, d;
  1721. tbio = r10_bio->devs[i].bio;
  1722. if (tbio->bi_end_io != end_sync_read)
  1723. continue;
  1724. if (i == first)
  1725. continue;
  1726. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1727. /* We know that the bi_io_vec layout is the same for
  1728. * both 'first' and 'i', so we just compare them.
  1729. * All vec entries are PAGE_SIZE;
  1730. */
  1731. for (j = 0; j < vcnt; j++)
  1732. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1733. page_address(tbio->bi_io_vec[j].bv_page),
  1734. fbio->bi_io_vec[j].bv_len))
  1735. break;
  1736. if (j == vcnt)
  1737. continue;
  1738. mddev->resync_mismatches += r10_bio->sectors;
  1739. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1740. /* Don't fix anything. */
  1741. continue;
  1742. }
  1743. /* Ok, we need to write this bio, either to correct an
  1744. * inconsistency or to correct an unreadable block.
  1745. * First we need to fixup bv_offset, bv_len and
  1746. * bi_vecs, as the read request might have corrupted these
  1747. */
  1748. tbio->bi_vcnt = vcnt;
  1749. tbio->bi_size = r10_bio->sectors << 9;
  1750. tbio->bi_idx = 0;
  1751. tbio->bi_phys_segments = 0;
  1752. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1753. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1754. tbio->bi_next = NULL;
  1755. tbio->bi_rw = WRITE;
  1756. tbio->bi_private = r10_bio;
  1757. tbio->bi_sector = r10_bio->devs[i].addr;
  1758. for (j=0; j < vcnt ; j++) {
  1759. tbio->bi_io_vec[j].bv_offset = 0;
  1760. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1761. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1762. page_address(fbio->bi_io_vec[j].bv_page),
  1763. PAGE_SIZE);
  1764. }
  1765. tbio->bi_end_io = end_sync_write;
  1766. d = r10_bio->devs[i].devnum;
  1767. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1768. atomic_inc(&r10_bio->remaining);
  1769. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1770. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1771. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1772. generic_make_request(tbio);
  1773. }
  1774. /* Now write out to any replacement devices
  1775. * that are active
  1776. */
  1777. for (i = 0; i < conf->copies; i++) {
  1778. int j, d;
  1779. tbio = r10_bio->devs[i].repl_bio;
  1780. if (!tbio || !tbio->bi_end_io)
  1781. continue;
  1782. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1783. && r10_bio->devs[i].bio != fbio)
  1784. for (j = 0; j < vcnt; j++)
  1785. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1786. page_address(fbio->bi_io_vec[j].bv_page),
  1787. PAGE_SIZE);
  1788. d = r10_bio->devs[i].devnum;
  1789. atomic_inc(&r10_bio->remaining);
  1790. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1791. tbio->bi_size >> 9);
  1792. generic_make_request(tbio);
  1793. }
  1794. done:
  1795. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1796. md_done_sync(mddev, r10_bio->sectors, 1);
  1797. put_buf(r10_bio);
  1798. }
  1799. }
  1800. /*
  1801. * Now for the recovery code.
  1802. * Recovery happens across physical sectors.
  1803. * We recover all non-is_sync drives by finding the virtual address of
  1804. * each, and then choose a working drive that also has that virt address.
  1805. * There is a separate r10_bio for each non-in_sync drive.
  1806. * Only the first two slots are in use. The first for reading,
  1807. * The second for writing.
  1808. *
  1809. */
  1810. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1811. {
  1812. /* We got a read error during recovery.
  1813. * We repeat the read in smaller page-sized sections.
  1814. * If a read succeeds, write it to the new device or record
  1815. * a bad block if we cannot.
  1816. * If a read fails, record a bad block on both old and
  1817. * new devices.
  1818. */
  1819. struct mddev *mddev = r10_bio->mddev;
  1820. struct r10conf *conf = mddev->private;
  1821. struct bio *bio = r10_bio->devs[0].bio;
  1822. sector_t sect = 0;
  1823. int sectors = r10_bio->sectors;
  1824. int idx = 0;
  1825. int dr = r10_bio->devs[0].devnum;
  1826. int dw = r10_bio->devs[1].devnum;
  1827. while (sectors) {
  1828. int s = sectors;
  1829. struct md_rdev *rdev;
  1830. sector_t addr;
  1831. int ok;
  1832. if (s > (PAGE_SIZE>>9))
  1833. s = PAGE_SIZE >> 9;
  1834. rdev = conf->mirrors[dr].rdev;
  1835. addr = r10_bio->devs[0].addr + sect,
  1836. ok = sync_page_io(rdev,
  1837. addr,
  1838. s << 9,
  1839. bio->bi_io_vec[idx].bv_page,
  1840. READ, false);
  1841. if (ok) {
  1842. rdev = conf->mirrors[dw].rdev;
  1843. addr = r10_bio->devs[1].addr + sect;
  1844. ok = sync_page_io(rdev,
  1845. addr,
  1846. s << 9,
  1847. bio->bi_io_vec[idx].bv_page,
  1848. WRITE, false);
  1849. if (!ok) {
  1850. set_bit(WriteErrorSeen, &rdev->flags);
  1851. if (!test_and_set_bit(WantReplacement,
  1852. &rdev->flags))
  1853. set_bit(MD_RECOVERY_NEEDED,
  1854. &rdev->mddev->recovery);
  1855. }
  1856. }
  1857. if (!ok) {
  1858. /* We don't worry if we cannot set a bad block -
  1859. * it really is bad so there is no loss in not
  1860. * recording it yet
  1861. */
  1862. rdev_set_badblocks(rdev, addr, s, 0);
  1863. if (rdev != conf->mirrors[dw].rdev) {
  1864. /* need bad block on destination too */
  1865. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  1866. addr = r10_bio->devs[1].addr + sect;
  1867. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  1868. if (!ok) {
  1869. /* just abort the recovery */
  1870. printk(KERN_NOTICE
  1871. "md/raid10:%s: recovery aborted"
  1872. " due to read error\n",
  1873. mdname(mddev));
  1874. conf->mirrors[dw].recovery_disabled
  1875. = mddev->recovery_disabled;
  1876. set_bit(MD_RECOVERY_INTR,
  1877. &mddev->recovery);
  1878. break;
  1879. }
  1880. }
  1881. }
  1882. sectors -= s;
  1883. sect += s;
  1884. idx++;
  1885. }
  1886. }
  1887. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1888. {
  1889. struct r10conf *conf = mddev->private;
  1890. int d;
  1891. struct bio *wbio, *wbio2;
  1892. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  1893. fix_recovery_read_error(r10_bio);
  1894. end_sync_request(r10_bio);
  1895. return;
  1896. }
  1897. /*
  1898. * share the pages with the first bio
  1899. * and submit the write request
  1900. */
  1901. d = r10_bio->devs[1].devnum;
  1902. wbio = r10_bio->devs[1].bio;
  1903. wbio2 = r10_bio->devs[1].repl_bio;
  1904. if (wbio->bi_end_io) {
  1905. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1906. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1907. generic_make_request(wbio);
  1908. }
  1909. if (wbio2 && wbio2->bi_end_io) {
  1910. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  1911. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1912. wbio2->bi_size >> 9);
  1913. generic_make_request(wbio2);
  1914. }
  1915. }
  1916. /*
  1917. * Used by fix_read_error() to decay the per rdev read_errors.
  1918. * We halve the read error count for every hour that has elapsed
  1919. * since the last recorded read error.
  1920. *
  1921. */
  1922. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  1923. {
  1924. struct timespec cur_time_mon;
  1925. unsigned long hours_since_last;
  1926. unsigned int read_errors = atomic_read(&rdev->read_errors);
  1927. ktime_get_ts(&cur_time_mon);
  1928. if (rdev->last_read_error.tv_sec == 0 &&
  1929. rdev->last_read_error.tv_nsec == 0) {
  1930. /* first time we've seen a read error */
  1931. rdev->last_read_error = cur_time_mon;
  1932. return;
  1933. }
  1934. hours_since_last = (cur_time_mon.tv_sec -
  1935. rdev->last_read_error.tv_sec) / 3600;
  1936. rdev->last_read_error = cur_time_mon;
  1937. /*
  1938. * if hours_since_last is > the number of bits in read_errors
  1939. * just set read errors to 0. We do this to avoid
  1940. * overflowing the shift of read_errors by hours_since_last.
  1941. */
  1942. if (hours_since_last >= 8 * sizeof(read_errors))
  1943. atomic_set(&rdev->read_errors, 0);
  1944. else
  1945. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  1946. }
  1947. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  1948. int sectors, struct page *page, int rw)
  1949. {
  1950. sector_t first_bad;
  1951. int bad_sectors;
  1952. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  1953. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  1954. return -1;
  1955. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  1956. /* success */
  1957. return 1;
  1958. if (rw == WRITE) {
  1959. set_bit(WriteErrorSeen, &rdev->flags);
  1960. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1961. set_bit(MD_RECOVERY_NEEDED,
  1962. &rdev->mddev->recovery);
  1963. }
  1964. /* need to record an error - either for the block or the device */
  1965. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1966. md_error(rdev->mddev, rdev);
  1967. return 0;
  1968. }
  1969. /*
  1970. * This is a kernel thread which:
  1971. *
  1972. * 1. Retries failed read operations on working mirrors.
  1973. * 2. Updates the raid superblock when problems encounter.
  1974. * 3. Performs writes following reads for array synchronising.
  1975. */
  1976. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  1977. {
  1978. int sect = 0; /* Offset from r10_bio->sector */
  1979. int sectors = r10_bio->sectors;
  1980. struct md_rdev*rdev;
  1981. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  1982. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  1983. /* still own a reference to this rdev, so it cannot
  1984. * have been cleared recently.
  1985. */
  1986. rdev = conf->mirrors[d].rdev;
  1987. if (test_bit(Faulty, &rdev->flags))
  1988. /* drive has already been failed, just ignore any
  1989. more fix_read_error() attempts */
  1990. return;
  1991. check_decay_read_errors(mddev, rdev);
  1992. atomic_inc(&rdev->read_errors);
  1993. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  1994. char b[BDEVNAME_SIZE];
  1995. bdevname(rdev->bdev, b);
  1996. printk(KERN_NOTICE
  1997. "md/raid10:%s: %s: Raid device exceeded "
  1998. "read_error threshold [cur %d:max %d]\n",
  1999. mdname(mddev), b,
  2000. atomic_read(&rdev->read_errors), max_read_errors);
  2001. printk(KERN_NOTICE
  2002. "md/raid10:%s: %s: Failing raid device\n",
  2003. mdname(mddev), b);
  2004. md_error(mddev, conf->mirrors[d].rdev);
  2005. r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
  2006. return;
  2007. }
  2008. while(sectors) {
  2009. int s = sectors;
  2010. int sl = r10_bio->read_slot;
  2011. int success = 0;
  2012. int start;
  2013. if (s > (PAGE_SIZE>>9))
  2014. s = PAGE_SIZE >> 9;
  2015. rcu_read_lock();
  2016. do {
  2017. sector_t first_bad;
  2018. int bad_sectors;
  2019. d = r10_bio->devs[sl].devnum;
  2020. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2021. if (rdev &&
  2022. !test_bit(Unmerged, &rdev->flags) &&
  2023. test_bit(In_sync, &rdev->flags) &&
  2024. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  2025. &first_bad, &bad_sectors) == 0) {
  2026. atomic_inc(&rdev->nr_pending);
  2027. rcu_read_unlock();
  2028. success = sync_page_io(rdev,
  2029. r10_bio->devs[sl].addr +
  2030. sect,
  2031. s<<9,
  2032. conf->tmppage, READ, false);
  2033. rdev_dec_pending(rdev, mddev);
  2034. rcu_read_lock();
  2035. if (success)
  2036. break;
  2037. }
  2038. sl++;
  2039. if (sl == conf->copies)
  2040. sl = 0;
  2041. } while (!success && sl != r10_bio->read_slot);
  2042. rcu_read_unlock();
  2043. if (!success) {
  2044. /* Cannot read from anywhere, just mark the block
  2045. * as bad on the first device to discourage future
  2046. * reads.
  2047. */
  2048. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  2049. rdev = conf->mirrors[dn].rdev;
  2050. if (!rdev_set_badblocks(
  2051. rdev,
  2052. r10_bio->devs[r10_bio->read_slot].addr
  2053. + sect,
  2054. s, 0)) {
  2055. md_error(mddev, rdev);
  2056. r10_bio->devs[r10_bio->read_slot].bio
  2057. = IO_BLOCKED;
  2058. }
  2059. break;
  2060. }
  2061. start = sl;
  2062. /* write it back and re-read */
  2063. rcu_read_lock();
  2064. while (sl != r10_bio->read_slot) {
  2065. char b[BDEVNAME_SIZE];
  2066. if (sl==0)
  2067. sl = conf->copies;
  2068. sl--;
  2069. d = r10_bio->devs[sl].devnum;
  2070. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2071. if (!rdev ||
  2072. test_bit(Unmerged, &rdev->flags) ||
  2073. !test_bit(In_sync, &rdev->flags))
  2074. continue;
  2075. atomic_inc(&rdev->nr_pending);
  2076. rcu_read_unlock();
  2077. if (r10_sync_page_io(rdev,
  2078. r10_bio->devs[sl].addr +
  2079. sect,
  2080. s<<9, conf->tmppage, WRITE)
  2081. == 0) {
  2082. /* Well, this device is dead */
  2083. printk(KERN_NOTICE
  2084. "md/raid10:%s: read correction "
  2085. "write failed"
  2086. " (%d sectors at %llu on %s)\n",
  2087. mdname(mddev), s,
  2088. (unsigned long long)(
  2089. sect +
  2090. choose_data_offset(r10_bio,
  2091. rdev)),
  2092. bdevname(rdev->bdev, b));
  2093. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2094. "drive\n",
  2095. mdname(mddev),
  2096. bdevname(rdev->bdev, b));
  2097. }
  2098. rdev_dec_pending(rdev, mddev);
  2099. rcu_read_lock();
  2100. }
  2101. sl = start;
  2102. while (sl != r10_bio->read_slot) {
  2103. char b[BDEVNAME_SIZE];
  2104. if (sl==0)
  2105. sl = conf->copies;
  2106. sl--;
  2107. d = r10_bio->devs[sl].devnum;
  2108. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2109. if (!rdev ||
  2110. !test_bit(In_sync, &rdev->flags))
  2111. continue;
  2112. atomic_inc(&rdev->nr_pending);
  2113. rcu_read_unlock();
  2114. switch (r10_sync_page_io(rdev,
  2115. r10_bio->devs[sl].addr +
  2116. sect,
  2117. s<<9, conf->tmppage,
  2118. READ)) {
  2119. case 0:
  2120. /* Well, this device is dead */
  2121. printk(KERN_NOTICE
  2122. "md/raid10:%s: unable to read back "
  2123. "corrected sectors"
  2124. " (%d sectors at %llu on %s)\n",
  2125. mdname(mddev), s,
  2126. (unsigned long long)(
  2127. sect +
  2128. choose_data_offset(r10_bio, rdev)),
  2129. bdevname(rdev->bdev, b));
  2130. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2131. "drive\n",
  2132. mdname(mddev),
  2133. bdevname(rdev->bdev, b));
  2134. break;
  2135. case 1:
  2136. printk(KERN_INFO
  2137. "md/raid10:%s: read error corrected"
  2138. " (%d sectors at %llu on %s)\n",
  2139. mdname(mddev), s,
  2140. (unsigned long long)(
  2141. sect +
  2142. choose_data_offset(r10_bio, rdev)),
  2143. bdevname(rdev->bdev, b));
  2144. atomic_add(s, &rdev->corrected_errors);
  2145. }
  2146. rdev_dec_pending(rdev, mddev);
  2147. rcu_read_lock();
  2148. }
  2149. rcu_read_unlock();
  2150. sectors -= s;
  2151. sect += s;
  2152. }
  2153. }
  2154. static void bi_complete(struct bio *bio, int error)
  2155. {
  2156. complete((struct completion *)bio->bi_private);
  2157. }
  2158. static int submit_bio_wait(int rw, struct bio *bio)
  2159. {
  2160. struct completion event;
  2161. rw |= REQ_SYNC;
  2162. init_completion(&event);
  2163. bio->bi_private = &event;
  2164. bio->bi_end_io = bi_complete;
  2165. submit_bio(rw, bio);
  2166. wait_for_completion(&event);
  2167. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  2168. }
  2169. static int narrow_write_error(struct r10bio *r10_bio, int i)
  2170. {
  2171. struct bio *bio = r10_bio->master_bio;
  2172. struct mddev *mddev = r10_bio->mddev;
  2173. struct r10conf *conf = mddev->private;
  2174. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  2175. /* bio has the data to be written to slot 'i' where
  2176. * we just recently had a write error.
  2177. * We repeatedly clone the bio and trim down to one block,
  2178. * then try the write. Where the write fails we record
  2179. * a bad block.
  2180. * It is conceivable that the bio doesn't exactly align with
  2181. * blocks. We must handle this.
  2182. *
  2183. * We currently own a reference to the rdev.
  2184. */
  2185. int block_sectors;
  2186. sector_t sector;
  2187. int sectors;
  2188. int sect_to_write = r10_bio->sectors;
  2189. int ok = 1;
  2190. if (rdev->badblocks.shift < 0)
  2191. return 0;
  2192. block_sectors = 1 << rdev->badblocks.shift;
  2193. sector = r10_bio->sector;
  2194. sectors = ((r10_bio->sector + block_sectors)
  2195. & ~(sector_t)(block_sectors - 1))
  2196. - sector;
  2197. while (sect_to_write) {
  2198. struct bio *wbio;
  2199. if (sectors > sect_to_write)
  2200. sectors = sect_to_write;
  2201. /* Write at 'sector' for 'sectors' */
  2202. wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  2203. md_trim_bio(wbio, sector - bio->bi_sector, sectors);
  2204. wbio->bi_sector = (r10_bio->devs[i].addr+
  2205. choose_data_offset(r10_bio, rdev) +
  2206. (sector - r10_bio->sector));
  2207. wbio->bi_bdev = rdev->bdev;
  2208. if (submit_bio_wait(WRITE, wbio) == 0)
  2209. /* Failure! */
  2210. ok = rdev_set_badblocks(rdev, sector,
  2211. sectors, 0)
  2212. && ok;
  2213. bio_put(wbio);
  2214. sect_to_write -= sectors;
  2215. sector += sectors;
  2216. sectors = block_sectors;
  2217. }
  2218. return ok;
  2219. }
  2220. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2221. {
  2222. int slot = r10_bio->read_slot;
  2223. struct bio *bio;
  2224. struct r10conf *conf = mddev->private;
  2225. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2226. char b[BDEVNAME_SIZE];
  2227. unsigned long do_sync;
  2228. int max_sectors;
  2229. /* we got a read error. Maybe the drive is bad. Maybe just
  2230. * the block and we can fix it.
  2231. * We freeze all other IO, and try reading the block from
  2232. * other devices. When we find one, we re-write
  2233. * and check it that fixes the read error.
  2234. * This is all done synchronously while the array is
  2235. * frozen.
  2236. */
  2237. bio = r10_bio->devs[slot].bio;
  2238. bdevname(bio->bi_bdev, b);
  2239. bio_put(bio);
  2240. r10_bio->devs[slot].bio = NULL;
  2241. if (mddev->ro == 0) {
  2242. freeze_array(conf);
  2243. fix_read_error(conf, mddev, r10_bio);
  2244. unfreeze_array(conf);
  2245. } else
  2246. r10_bio->devs[slot].bio = IO_BLOCKED;
  2247. rdev_dec_pending(rdev, mddev);
  2248. read_more:
  2249. rdev = read_balance(conf, r10_bio, &max_sectors);
  2250. if (rdev == NULL) {
  2251. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  2252. " read error for block %llu\n",
  2253. mdname(mddev), b,
  2254. (unsigned long long)r10_bio->sector);
  2255. raid_end_bio_io(r10_bio);
  2256. return;
  2257. }
  2258. do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  2259. slot = r10_bio->read_slot;
  2260. printk_ratelimited(
  2261. KERN_ERR
  2262. "md/raid10:%s: %s: redirecting"
  2263. "sector %llu to another mirror\n",
  2264. mdname(mddev),
  2265. bdevname(rdev->bdev, b),
  2266. (unsigned long long)r10_bio->sector);
  2267. bio = bio_clone_mddev(r10_bio->master_bio,
  2268. GFP_NOIO, mddev);
  2269. md_trim_bio(bio,
  2270. r10_bio->sector - bio->bi_sector,
  2271. max_sectors);
  2272. r10_bio->devs[slot].bio = bio;
  2273. r10_bio->devs[slot].rdev = rdev;
  2274. bio->bi_sector = r10_bio->devs[slot].addr
  2275. + choose_data_offset(r10_bio, rdev);
  2276. bio->bi_bdev = rdev->bdev;
  2277. bio->bi_rw = READ | do_sync;
  2278. bio->bi_private = r10_bio;
  2279. bio->bi_end_io = raid10_end_read_request;
  2280. if (max_sectors < r10_bio->sectors) {
  2281. /* Drat - have to split this up more */
  2282. struct bio *mbio = r10_bio->master_bio;
  2283. int sectors_handled =
  2284. r10_bio->sector + max_sectors
  2285. - mbio->bi_sector;
  2286. r10_bio->sectors = max_sectors;
  2287. spin_lock_irq(&conf->device_lock);
  2288. if (mbio->bi_phys_segments == 0)
  2289. mbio->bi_phys_segments = 2;
  2290. else
  2291. mbio->bi_phys_segments++;
  2292. spin_unlock_irq(&conf->device_lock);
  2293. generic_make_request(bio);
  2294. r10_bio = mempool_alloc(conf->r10bio_pool,
  2295. GFP_NOIO);
  2296. r10_bio->master_bio = mbio;
  2297. r10_bio->sectors = (mbio->bi_size >> 9)
  2298. - sectors_handled;
  2299. r10_bio->state = 0;
  2300. set_bit(R10BIO_ReadError,
  2301. &r10_bio->state);
  2302. r10_bio->mddev = mddev;
  2303. r10_bio->sector = mbio->bi_sector
  2304. + sectors_handled;
  2305. goto read_more;
  2306. } else
  2307. generic_make_request(bio);
  2308. }
  2309. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2310. {
  2311. /* Some sort of write request has finished and it
  2312. * succeeded in writing where we thought there was a
  2313. * bad block. So forget the bad block.
  2314. * Or possibly if failed and we need to record
  2315. * a bad block.
  2316. */
  2317. int m;
  2318. struct md_rdev *rdev;
  2319. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2320. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2321. for (m = 0; m < conf->copies; m++) {
  2322. int dev = r10_bio->devs[m].devnum;
  2323. rdev = conf->mirrors[dev].rdev;
  2324. if (r10_bio->devs[m].bio == NULL)
  2325. continue;
  2326. if (test_bit(BIO_UPTODATE,
  2327. &r10_bio->devs[m].bio->bi_flags)) {
  2328. rdev_clear_badblocks(
  2329. rdev,
  2330. r10_bio->devs[m].addr,
  2331. r10_bio->sectors, 0);
  2332. } else {
  2333. if (!rdev_set_badblocks(
  2334. rdev,
  2335. r10_bio->devs[m].addr,
  2336. r10_bio->sectors, 0))
  2337. md_error(conf->mddev, rdev);
  2338. }
  2339. rdev = conf->mirrors[dev].replacement;
  2340. if (r10_bio->devs[m].repl_bio == NULL)
  2341. continue;
  2342. if (test_bit(BIO_UPTODATE,
  2343. &r10_bio->devs[m].repl_bio->bi_flags)) {
  2344. rdev_clear_badblocks(
  2345. rdev,
  2346. r10_bio->devs[m].addr,
  2347. r10_bio->sectors, 0);
  2348. } else {
  2349. if (!rdev_set_badblocks(
  2350. rdev,
  2351. r10_bio->devs[m].addr,
  2352. r10_bio->sectors, 0))
  2353. md_error(conf->mddev, rdev);
  2354. }
  2355. }
  2356. put_buf(r10_bio);
  2357. } else {
  2358. for (m = 0; m < conf->copies; m++) {
  2359. int dev = r10_bio->devs[m].devnum;
  2360. struct bio *bio = r10_bio->devs[m].bio;
  2361. rdev = conf->mirrors[dev].rdev;
  2362. if (bio == IO_MADE_GOOD) {
  2363. rdev_clear_badblocks(
  2364. rdev,
  2365. r10_bio->devs[m].addr,
  2366. r10_bio->sectors, 0);
  2367. rdev_dec_pending(rdev, conf->mddev);
  2368. } else if (bio != NULL &&
  2369. !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  2370. if (!narrow_write_error(r10_bio, m)) {
  2371. md_error(conf->mddev, rdev);
  2372. set_bit(R10BIO_Degraded,
  2373. &r10_bio->state);
  2374. }
  2375. rdev_dec_pending(rdev, conf->mddev);
  2376. }
  2377. bio = r10_bio->devs[m].repl_bio;
  2378. rdev = conf->mirrors[dev].replacement;
  2379. if (rdev && bio == IO_MADE_GOOD) {
  2380. rdev_clear_badblocks(
  2381. rdev,
  2382. r10_bio->devs[m].addr,
  2383. r10_bio->sectors, 0);
  2384. rdev_dec_pending(rdev, conf->mddev);
  2385. }
  2386. }
  2387. if (test_bit(R10BIO_WriteError,
  2388. &r10_bio->state))
  2389. close_write(r10_bio);
  2390. raid_end_bio_io(r10_bio);
  2391. }
  2392. }
  2393. static void raid10d(struct mddev *mddev)
  2394. {
  2395. struct r10bio *r10_bio;
  2396. unsigned long flags;
  2397. struct r10conf *conf = mddev->private;
  2398. struct list_head *head = &conf->retry_list;
  2399. struct blk_plug plug;
  2400. md_check_recovery(mddev);
  2401. blk_start_plug(&plug);
  2402. for (;;) {
  2403. flush_pending_writes(conf);
  2404. spin_lock_irqsave(&conf->device_lock, flags);
  2405. if (list_empty(head)) {
  2406. spin_unlock_irqrestore(&conf->device_lock, flags);
  2407. break;
  2408. }
  2409. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2410. list_del(head->prev);
  2411. conf->nr_queued--;
  2412. spin_unlock_irqrestore(&conf->device_lock, flags);
  2413. mddev = r10_bio->mddev;
  2414. conf = mddev->private;
  2415. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2416. test_bit(R10BIO_WriteError, &r10_bio->state))
  2417. handle_write_completed(conf, r10_bio);
  2418. else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
  2419. reshape_request_write(mddev, r10_bio);
  2420. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2421. sync_request_write(mddev, r10_bio);
  2422. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2423. recovery_request_write(mddev, r10_bio);
  2424. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2425. handle_read_error(mddev, r10_bio);
  2426. else {
  2427. /* just a partial read to be scheduled from a
  2428. * separate context
  2429. */
  2430. int slot = r10_bio->read_slot;
  2431. generic_make_request(r10_bio->devs[slot].bio);
  2432. }
  2433. cond_resched();
  2434. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2435. md_check_recovery(mddev);
  2436. }
  2437. blk_finish_plug(&plug);
  2438. }
  2439. static int init_resync(struct r10conf *conf)
  2440. {
  2441. int buffs;
  2442. int i;
  2443. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2444. BUG_ON(conf->r10buf_pool);
  2445. conf->have_replacement = 0;
  2446. for (i = 0; i < conf->geo.raid_disks; i++)
  2447. if (conf->mirrors[i].replacement)
  2448. conf->have_replacement = 1;
  2449. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  2450. if (!conf->r10buf_pool)
  2451. return -ENOMEM;
  2452. conf->next_resync = 0;
  2453. return 0;
  2454. }
  2455. /*
  2456. * perform a "sync" on one "block"
  2457. *
  2458. * We need to make sure that no normal I/O request - particularly write
  2459. * requests - conflict with active sync requests.
  2460. *
  2461. * This is achieved by tracking pending requests and a 'barrier' concept
  2462. * that can be installed to exclude normal IO requests.
  2463. *
  2464. * Resync and recovery are handled very differently.
  2465. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2466. *
  2467. * For resync, we iterate over virtual addresses, read all copies,
  2468. * and update if there are differences. If only one copy is live,
  2469. * skip it.
  2470. * For recovery, we iterate over physical addresses, read a good
  2471. * value for each non-in_sync drive, and over-write.
  2472. *
  2473. * So, for recovery we may have several outstanding complex requests for a
  2474. * given address, one for each out-of-sync device. We model this by allocating
  2475. * a number of r10_bio structures, one for each out-of-sync device.
  2476. * As we setup these structures, we collect all bio's together into a list
  2477. * which we then process collectively to add pages, and then process again
  2478. * to pass to generic_make_request.
  2479. *
  2480. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2481. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2482. * has its remaining count decremented to 0, the whole complex operation
  2483. * is complete.
  2484. *
  2485. */
  2486. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
  2487. int *skipped, int go_faster)
  2488. {
  2489. struct r10conf *conf = mddev->private;
  2490. struct r10bio *r10_bio;
  2491. struct bio *biolist = NULL, *bio;
  2492. sector_t max_sector, nr_sectors;
  2493. int i;
  2494. int max_sync;
  2495. sector_t sync_blocks;
  2496. sector_t sectors_skipped = 0;
  2497. int chunks_skipped = 0;
  2498. sector_t chunk_mask = conf->geo.chunk_mask;
  2499. if (!conf->r10buf_pool)
  2500. if (init_resync(conf))
  2501. return 0;
  2502. skipped:
  2503. max_sector = mddev->dev_sectors;
  2504. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
  2505. test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2506. max_sector = mddev->resync_max_sectors;
  2507. if (sector_nr >= max_sector) {
  2508. /* If we aborted, we need to abort the
  2509. * sync on the 'current' bitmap chucks (there can
  2510. * be several when recovering multiple devices).
  2511. * as we may have started syncing it but not finished.
  2512. * We can find the current address in
  2513. * mddev->curr_resync, but for recovery,
  2514. * we need to convert that to several
  2515. * virtual addresses.
  2516. */
  2517. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2518. end_reshape(conf);
  2519. return 0;
  2520. }
  2521. if (mddev->curr_resync < max_sector) { /* aborted */
  2522. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2523. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2524. &sync_blocks, 1);
  2525. else for (i = 0; i < conf->geo.raid_disks; i++) {
  2526. sector_t sect =
  2527. raid10_find_virt(conf, mddev->curr_resync, i);
  2528. bitmap_end_sync(mddev->bitmap, sect,
  2529. &sync_blocks, 1);
  2530. }
  2531. } else {
  2532. /* completed sync */
  2533. if ((!mddev->bitmap || conf->fullsync)
  2534. && conf->have_replacement
  2535. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2536. /* Completed a full sync so the replacements
  2537. * are now fully recovered.
  2538. */
  2539. for (i = 0; i < conf->geo.raid_disks; i++)
  2540. if (conf->mirrors[i].replacement)
  2541. conf->mirrors[i].replacement
  2542. ->recovery_offset
  2543. = MaxSector;
  2544. }
  2545. conf->fullsync = 0;
  2546. }
  2547. bitmap_close_sync(mddev->bitmap);
  2548. close_sync(conf);
  2549. *skipped = 1;
  2550. return sectors_skipped;
  2551. }
  2552. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2553. return reshape_request(mddev, sector_nr, skipped);
  2554. if (chunks_skipped >= conf->geo.raid_disks) {
  2555. /* if there has been nothing to do on any drive,
  2556. * then there is nothing to do at all..
  2557. */
  2558. *skipped = 1;
  2559. return (max_sector - sector_nr) + sectors_skipped;
  2560. }
  2561. if (max_sector > mddev->resync_max)
  2562. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2563. /* make sure whole request will fit in a chunk - if chunks
  2564. * are meaningful
  2565. */
  2566. if (conf->geo.near_copies < conf->geo.raid_disks &&
  2567. max_sector > (sector_nr | chunk_mask))
  2568. max_sector = (sector_nr | chunk_mask) + 1;
  2569. /*
  2570. * If there is non-resync activity waiting for us then
  2571. * put in a delay to throttle resync.
  2572. */
  2573. if (!go_faster && conf->nr_waiting)
  2574. msleep_interruptible(1000);
  2575. /* Again, very different code for resync and recovery.
  2576. * Both must result in an r10bio with a list of bios that
  2577. * have bi_end_io, bi_sector, bi_bdev set,
  2578. * and bi_private set to the r10bio.
  2579. * For recovery, we may actually create several r10bios
  2580. * with 2 bios in each, that correspond to the bios in the main one.
  2581. * In this case, the subordinate r10bios link back through a
  2582. * borrowed master_bio pointer, and the counter in the master
  2583. * includes a ref from each subordinate.
  2584. */
  2585. /* First, we decide what to do and set ->bi_end_io
  2586. * To end_sync_read if we want to read, and
  2587. * end_sync_write if we will want to write.
  2588. */
  2589. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2590. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2591. /* recovery... the complicated one */
  2592. int j;
  2593. r10_bio = NULL;
  2594. for (i = 0 ; i < conf->geo.raid_disks; i++) {
  2595. int still_degraded;
  2596. struct r10bio *rb2;
  2597. sector_t sect;
  2598. int must_sync;
  2599. int any_working;
  2600. struct mirror_info *mirror = &conf->mirrors[i];
  2601. if ((mirror->rdev == NULL ||
  2602. test_bit(In_sync, &mirror->rdev->flags))
  2603. &&
  2604. (mirror->replacement == NULL ||
  2605. test_bit(Faulty,
  2606. &mirror->replacement->flags)))
  2607. continue;
  2608. still_degraded = 0;
  2609. /* want to reconstruct this device */
  2610. rb2 = r10_bio;
  2611. sect = raid10_find_virt(conf, sector_nr, i);
  2612. /* Unless we are doing a full sync, or a replacement
  2613. * we only need to recover the block if it is set in
  2614. * the bitmap
  2615. */
  2616. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2617. &sync_blocks, 1);
  2618. if (sync_blocks < max_sync)
  2619. max_sync = sync_blocks;
  2620. if (!must_sync &&
  2621. mirror->replacement == NULL &&
  2622. !conf->fullsync) {
  2623. /* yep, skip the sync_blocks here, but don't assume
  2624. * that there will never be anything to do here
  2625. */
  2626. chunks_skipped = -1;
  2627. continue;
  2628. }
  2629. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2630. raise_barrier(conf, rb2 != NULL);
  2631. atomic_set(&r10_bio->remaining, 0);
  2632. r10_bio->master_bio = (struct bio*)rb2;
  2633. if (rb2)
  2634. atomic_inc(&rb2->remaining);
  2635. r10_bio->mddev = mddev;
  2636. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2637. r10_bio->sector = sect;
  2638. raid10_find_phys(conf, r10_bio);
  2639. /* Need to check if the array will still be
  2640. * degraded
  2641. */
  2642. for (j = 0; j < conf->geo.raid_disks; j++)
  2643. if (conf->mirrors[j].rdev == NULL ||
  2644. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  2645. still_degraded = 1;
  2646. break;
  2647. }
  2648. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2649. &sync_blocks, still_degraded);
  2650. any_working = 0;
  2651. for (j=0; j<conf->copies;j++) {
  2652. int k;
  2653. int d = r10_bio->devs[j].devnum;
  2654. sector_t from_addr, to_addr;
  2655. struct md_rdev *rdev;
  2656. sector_t sector, first_bad;
  2657. int bad_sectors;
  2658. if (!conf->mirrors[d].rdev ||
  2659. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  2660. continue;
  2661. /* This is where we read from */
  2662. any_working = 1;
  2663. rdev = conf->mirrors[d].rdev;
  2664. sector = r10_bio->devs[j].addr;
  2665. if (is_badblock(rdev, sector, max_sync,
  2666. &first_bad, &bad_sectors)) {
  2667. if (first_bad > sector)
  2668. max_sync = first_bad - sector;
  2669. else {
  2670. bad_sectors -= (sector
  2671. - first_bad);
  2672. if (max_sync > bad_sectors)
  2673. max_sync = bad_sectors;
  2674. continue;
  2675. }
  2676. }
  2677. bio = r10_bio->devs[0].bio;
  2678. bio->bi_next = biolist;
  2679. biolist = bio;
  2680. bio->bi_private = r10_bio;
  2681. bio->bi_end_io = end_sync_read;
  2682. bio->bi_rw = READ;
  2683. from_addr = r10_bio->devs[j].addr;
  2684. bio->bi_sector = from_addr + rdev->data_offset;
  2685. bio->bi_bdev = rdev->bdev;
  2686. atomic_inc(&rdev->nr_pending);
  2687. /* and we write to 'i' (if not in_sync) */
  2688. for (k=0; k<conf->copies; k++)
  2689. if (r10_bio->devs[k].devnum == i)
  2690. break;
  2691. BUG_ON(k == conf->copies);
  2692. to_addr = r10_bio->devs[k].addr;
  2693. r10_bio->devs[0].devnum = d;
  2694. r10_bio->devs[0].addr = from_addr;
  2695. r10_bio->devs[1].devnum = i;
  2696. r10_bio->devs[1].addr = to_addr;
  2697. rdev = mirror->rdev;
  2698. if (!test_bit(In_sync, &rdev->flags)) {
  2699. bio = r10_bio->devs[1].bio;
  2700. bio->bi_next = biolist;
  2701. biolist = bio;
  2702. bio->bi_private = r10_bio;
  2703. bio->bi_end_io = end_sync_write;
  2704. bio->bi_rw = WRITE;
  2705. bio->bi_sector = to_addr
  2706. + rdev->data_offset;
  2707. bio->bi_bdev = rdev->bdev;
  2708. atomic_inc(&r10_bio->remaining);
  2709. } else
  2710. r10_bio->devs[1].bio->bi_end_io = NULL;
  2711. /* and maybe write to replacement */
  2712. bio = r10_bio->devs[1].repl_bio;
  2713. if (bio)
  2714. bio->bi_end_io = NULL;
  2715. rdev = mirror->replacement;
  2716. /* Note: if rdev != NULL, then bio
  2717. * cannot be NULL as r10buf_pool_alloc will
  2718. * have allocated it.
  2719. * So the second test here is pointless.
  2720. * But it keeps semantic-checkers happy, and
  2721. * this comment keeps human reviewers
  2722. * happy.
  2723. */
  2724. if (rdev == NULL || bio == NULL ||
  2725. test_bit(Faulty, &rdev->flags))
  2726. break;
  2727. bio->bi_next = biolist;
  2728. biolist = bio;
  2729. bio->bi_private = r10_bio;
  2730. bio->bi_end_io = end_sync_write;
  2731. bio->bi_rw = WRITE;
  2732. bio->bi_sector = to_addr + rdev->data_offset;
  2733. bio->bi_bdev = rdev->bdev;
  2734. atomic_inc(&r10_bio->remaining);
  2735. break;
  2736. }
  2737. if (j == conf->copies) {
  2738. /* Cannot recover, so abort the recovery or
  2739. * record a bad block */
  2740. put_buf(r10_bio);
  2741. if (rb2)
  2742. atomic_dec(&rb2->remaining);
  2743. r10_bio = rb2;
  2744. if (any_working) {
  2745. /* problem is that there are bad blocks
  2746. * on other device(s)
  2747. */
  2748. int k;
  2749. for (k = 0; k < conf->copies; k++)
  2750. if (r10_bio->devs[k].devnum == i)
  2751. break;
  2752. if (!test_bit(In_sync,
  2753. &mirror->rdev->flags)
  2754. && !rdev_set_badblocks(
  2755. mirror->rdev,
  2756. r10_bio->devs[k].addr,
  2757. max_sync, 0))
  2758. any_working = 0;
  2759. if (mirror->replacement &&
  2760. !rdev_set_badblocks(
  2761. mirror->replacement,
  2762. r10_bio->devs[k].addr,
  2763. max_sync, 0))
  2764. any_working = 0;
  2765. }
  2766. if (!any_working) {
  2767. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2768. &mddev->recovery))
  2769. printk(KERN_INFO "md/raid10:%s: insufficient "
  2770. "working devices for recovery.\n",
  2771. mdname(mddev));
  2772. mirror->recovery_disabled
  2773. = mddev->recovery_disabled;
  2774. }
  2775. break;
  2776. }
  2777. }
  2778. if (biolist == NULL) {
  2779. while (r10_bio) {
  2780. struct r10bio *rb2 = r10_bio;
  2781. r10_bio = (struct r10bio*) rb2->master_bio;
  2782. rb2->master_bio = NULL;
  2783. put_buf(rb2);
  2784. }
  2785. goto giveup;
  2786. }
  2787. } else {
  2788. /* resync. Schedule a read for every block at this virt offset */
  2789. int count = 0;
  2790. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2791. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2792. &sync_blocks, mddev->degraded) &&
  2793. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  2794. &mddev->recovery)) {
  2795. /* We can skip this block */
  2796. *skipped = 1;
  2797. return sync_blocks + sectors_skipped;
  2798. }
  2799. if (sync_blocks < max_sync)
  2800. max_sync = sync_blocks;
  2801. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2802. r10_bio->mddev = mddev;
  2803. atomic_set(&r10_bio->remaining, 0);
  2804. raise_barrier(conf, 0);
  2805. conf->next_resync = sector_nr;
  2806. r10_bio->master_bio = NULL;
  2807. r10_bio->sector = sector_nr;
  2808. set_bit(R10BIO_IsSync, &r10_bio->state);
  2809. raid10_find_phys(conf, r10_bio);
  2810. r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
  2811. for (i = 0; i < conf->copies; i++) {
  2812. int d = r10_bio->devs[i].devnum;
  2813. sector_t first_bad, sector;
  2814. int bad_sectors;
  2815. if (r10_bio->devs[i].repl_bio)
  2816. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  2817. bio = r10_bio->devs[i].bio;
  2818. bio->bi_end_io = NULL;
  2819. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2820. if (conf->mirrors[d].rdev == NULL ||
  2821. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  2822. continue;
  2823. sector = r10_bio->devs[i].addr;
  2824. if (is_badblock(conf->mirrors[d].rdev,
  2825. sector, max_sync,
  2826. &first_bad, &bad_sectors)) {
  2827. if (first_bad > sector)
  2828. max_sync = first_bad - sector;
  2829. else {
  2830. bad_sectors -= (sector - first_bad);
  2831. if (max_sync > bad_sectors)
  2832. max_sync = max_sync;
  2833. continue;
  2834. }
  2835. }
  2836. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2837. atomic_inc(&r10_bio->remaining);
  2838. bio->bi_next = biolist;
  2839. biolist = bio;
  2840. bio->bi_private = r10_bio;
  2841. bio->bi_end_io = end_sync_read;
  2842. bio->bi_rw = READ;
  2843. bio->bi_sector = sector +
  2844. conf->mirrors[d].rdev->data_offset;
  2845. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  2846. count++;
  2847. if (conf->mirrors[d].replacement == NULL ||
  2848. test_bit(Faulty,
  2849. &conf->mirrors[d].replacement->flags))
  2850. continue;
  2851. /* Need to set up for writing to the replacement */
  2852. bio = r10_bio->devs[i].repl_bio;
  2853. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2854. sector = r10_bio->devs[i].addr;
  2855. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2856. bio->bi_next = biolist;
  2857. biolist = bio;
  2858. bio->bi_private = r10_bio;
  2859. bio->bi_end_io = end_sync_write;
  2860. bio->bi_rw = WRITE;
  2861. bio->bi_sector = sector +
  2862. conf->mirrors[d].replacement->data_offset;
  2863. bio->bi_bdev = conf->mirrors[d].replacement->bdev;
  2864. count++;
  2865. }
  2866. if (count < 2) {
  2867. for (i=0; i<conf->copies; i++) {
  2868. int d = r10_bio->devs[i].devnum;
  2869. if (r10_bio->devs[i].bio->bi_end_io)
  2870. rdev_dec_pending(conf->mirrors[d].rdev,
  2871. mddev);
  2872. if (r10_bio->devs[i].repl_bio &&
  2873. r10_bio->devs[i].repl_bio->bi_end_io)
  2874. rdev_dec_pending(
  2875. conf->mirrors[d].replacement,
  2876. mddev);
  2877. }
  2878. put_buf(r10_bio);
  2879. biolist = NULL;
  2880. goto giveup;
  2881. }
  2882. }
  2883. for (bio = biolist; bio ; bio=bio->bi_next) {
  2884. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  2885. if (bio->bi_end_io)
  2886. bio->bi_flags |= 1 << BIO_UPTODATE;
  2887. bio->bi_vcnt = 0;
  2888. bio->bi_idx = 0;
  2889. bio->bi_phys_segments = 0;
  2890. bio->bi_size = 0;
  2891. }
  2892. nr_sectors = 0;
  2893. if (sector_nr + max_sync < max_sector)
  2894. max_sector = sector_nr + max_sync;
  2895. do {
  2896. struct page *page;
  2897. int len = PAGE_SIZE;
  2898. if (sector_nr + (len>>9) > max_sector)
  2899. len = (max_sector - sector_nr) << 9;
  2900. if (len == 0)
  2901. break;
  2902. for (bio= biolist ; bio ; bio=bio->bi_next) {
  2903. struct bio *bio2;
  2904. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2905. if (bio_add_page(bio, page, len, 0))
  2906. continue;
  2907. /* stop here */
  2908. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2909. for (bio2 = biolist;
  2910. bio2 && bio2 != bio;
  2911. bio2 = bio2->bi_next) {
  2912. /* remove last page from this bio */
  2913. bio2->bi_vcnt--;
  2914. bio2->bi_size -= len;
  2915. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  2916. }
  2917. goto bio_full;
  2918. }
  2919. nr_sectors += len>>9;
  2920. sector_nr += len>>9;
  2921. } while (biolist->bi_vcnt < RESYNC_PAGES);
  2922. bio_full:
  2923. r10_bio->sectors = nr_sectors;
  2924. while (biolist) {
  2925. bio = biolist;
  2926. biolist = biolist->bi_next;
  2927. bio->bi_next = NULL;
  2928. r10_bio = bio->bi_private;
  2929. r10_bio->sectors = nr_sectors;
  2930. if (bio->bi_end_io == end_sync_read) {
  2931. md_sync_acct(bio->bi_bdev, nr_sectors);
  2932. generic_make_request(bio);
  2933. }
  2934. }
  2935. if (sectors_skipped)
  2936. /* pretend they weren't skipped, it makes
  2937. * no important difference in this case
  2938. */
  2939. md_done_sync(mddev, sectors_skipped, 1);
  2940. return sectors_skipped + nr_sectors;
  2941. giveup:
  2942. /* There is nowhere to write, so all non-sync
  2943. * drives must be failed or in resync, all drives
  2944. * have a bad block, so try the next chunk...
  2945. */
  2946. if (sector_nr + max_sync < max_sector)
  2947. max_sector = sector_nr + max_sync;
  2948. sectors_skipped += (max_sector - sector_nr);
  2949. chunks_skipped ++;
  2950. sector_nr = max_sector;
  2951. goto skipped;
  2952. }
  2953. static sector_t
  2954. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  2955. {
  2956. sector_t size;
  2957. struct r10conf *conf = mddev->private;
  2958. if (!raid_disks)
  2959. raid_disks = min(conf->geo.raid_disks,
  2960. conf->prev.raid_disks);
  2961. if (!sectors)
  2962. sectors = conf->dev_sectors;
  2963. size = sectors >> conf->geo.chunk_shift;
  2964. sector_div(size, conf->geo.far_copies);
  2965. size = size * raid_disks;
  2966. sector_div(size, conf->geo.near_copies);
  2967. return size << conf->geo.chunk_shift;
  2968. }
  2969. static void calc_sectors(struct r10conf *conf, sector_t size)
  2970. {
  2971. /* Calculate the number of sectors-per-device that will
  2972. * actually be used, and set conf->dev_sectors and
  2973. * conf->stride
  2974. */
  2975. size = size >> conf->geo.chunk_shift;
  2976. sector_div(size, conf->geo.far_copies);
  2977. size = size * conf->geo.raid_disks;
  2978. sector_div(size, conf->geo.near_copies);
  2979. /* 'size' is now the number of chunks in the array */
  2980. /* calculate "used chunks per device" */
  2981. size = size * conf->copies;
  2982. /* We need to round up when dividing by raid_disks to
  2983. * get the stride size.
  2984. */
  2985. size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
  2986. conf->dev_sectors = size << conf->geo.chunk_shift;
  2987. if (conf->geo.far_offset)
  2988. conf->geo.stride = 1 << conf->geo.chunk_shift;
  2989. else {
  2990. sector_div(size, conf->geo.far_copies);
  2991. conf->geo.stride = size << conf->geo.chunk_shift;
  2992. }
  2993. }
  2994. enum geo_type {geo_new, geo_old, geo_start};
  2995. static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
  2996. {
  2997. int nc, fc, fo;
  2998. int layout, chunk, disks;
  2999. switch (new) {
  3000. case geo_old:
  3001. layout = mddev->layout;
  3002. chunk = mddev->chunk_sectors;
  3003. disks = mddev->raid_disks - mddev->delta_disks;
  3004. break;
  3005. case geo_new:
  3006. layout = mddev->new_layout;
  3007. chunk = mddev->new_chunk_sectors;
  3008. disks = mddev->raid_disks;
  3009. break;
  3010. default: /* avoid 'may be unused' warnings */
  3011. case geo_start: /* new when starting reshape - raid_disks not
  3012. * updated yet. */
  3013. layout = mddev->new_layout;
  3014. chunk = mddev->new_chunk_sectors;
  3015. disks = mddev->raid_disks + mddev->delta_disks;
  3016. break;
  3017. }
  3018. if (layout >> 17)
  3019. return -1;
  3020. if (chunk < (PAGE_SIZE >> 9) ||
  3021. !is_power_of_2(chunk))
  3022. return -2;
  3023. nc = layout & 255;
  3024. fc = (layout >> 8) & 255;
  3025. fo = layout & (1<<16);
  3026. geo->raid_disks = disks;
  3027. geo->near_copies = nc;
  3028. geo->far_copies = fc;
  3029. geo->far_offset = fo;
  3030. geo->chunk_mask = chunk - 1;
  3031. geo->chunk_shift = ffz(~chunk);
  3032. return nc*fc;
  3033. }
  3034. static struct r10conf *setup_conf(struct mddev *mddev)
  3035. {
  3036. struct r10conf *conf = NULL;
  3037. int err = -EINVAL;
  3038. struct geom geo;
  3039. int copies;
  3040. copies = setup_geo(&geo, mddev, geo_new);
  3041. if (copies == -2) {
  3042. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  3043. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  3044. mdname(mddev), PAGE_SIZE);
  3045. goto out;
  3046. }
  3047. if (copies < 2 || copies > mddev->raid_disks) {
  3048. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  3049. mdname(mddev), mddev->new_layout);
  3050. goto out;
  3051. }
  3052. err = -ENOMEM;
  3053. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  3054. if (!conf)
  3055. goto out;
  3056. /* FIXME calc properly */
  3057. conf->mirrors = kzalloc(sizeof(struct mirror_info)*(mddev->raid_disks +
  3058. max(0,mddev->delta_disks)),
  3059. GFP_KERNEL);
  3060. if (!conf->mirrors)
  3061. goto out;
  3062. conf->tmppage = alloc_page(GFP_KERNEL);
  3063. if (!conf->tmppage)
  3064. goto out;
  3065. conf->geo = geo;
  3066. conf->copies = copies;
  3067. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  3068. r10bio_pool_free, conf);
  3069. if (!conf->r10bio_pool)
  3070. goto out;
  3071. calc_sectors(conf, mddev->dev_sectors);
  3072. if (mddev->reshape_position == MaxSector) {
  3073. conf->prev = conf->geo;
  3074. conf->reshape_progress = MaxSector;
  3075. } else {
  3076. if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
  3077. err = -EINVAL;
  3078. goto out;
  3079. }
  3080. conf->reshape_progress = mddev->reshape_position;
  3081. if (conf->prev.far_offset)
  3082. conf->prev.stride = 1 << conf->prev.chunk_shift;
  3083. else
  3084. /* far_copies must be 1 */
  3085. conf->prev.stride = conf->dev_sectors;
  3086. }
  3087. spin_lock_init(&conf->device_lock);
  3088. INIT_LIST_HEAD(&conf->retry_list);
  3089. spin_lock_init(&conf->resync_lock);
  3090. init_waitqueue_head(&conf->wait_barrier);
  3091. conf->thread = md_register_thread(raid10d, mddev, NULL);
  3092. if (!conf->thread)
  3093. goto out;
  3094. conf->mddev = mddev;
  3095. return conf;
  3096. out:
  3097. if (err == -ENOMEM)
  3098. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  3099. mdname(mddev));
  3100. if (conf) {
  3101. if (conf->r10bio_pool)
  3102. mempool_destroy(conf->r10bio_pool);
  3103. kfree(conf->mirrors);
  3104. safe_put_page(conf->tmppage);
  3105. kfree(conf);
  3106. }
  3107. return ERR_PTR(err);
  3108. }
  3109. static int run(struct mddev *mddev)
  3110. {
  3111. struct r10conf *conf;
  3112. int i, disk_idx, chunk_size;
  3113. struct mirror_info *disk;
  3114. struct md_rdev *rdev;
  3115. sector_t size;
  3116. sector_t min_offset_diff = 0;
  3117. int first = 1;
  3118. if (mddev->private == NULL) {
  3119. conf = setup_conf(mddev);
  3120. if (IS_ERR(conf))
  3121. return PTR_ERR(conf);
  3122. mddev->private = conf;
  3123. }
  3124. conf = mddev->private;
  3125. if (!conf)
  3126. goto out;
  3127. mddev->thread = conf->thread;
  3128. conf->thread = NULL;
  3129. chunk_size = mddev->chunk_sectors << 9;
  3130. blk_queue_io_min(mddev->queue, chunk_size);
  3131. if (conf->geo.raid_disks % conf->geo.near_copies)
  3132. blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
  3133. else
  3134. blk_queue_io_opt(mddev->queue, chunk_size *
  3135. (conf->geo.raid_disks / conf->geo.near_copies));
  3136. rdev_for_each(rdev, mddev) {
  3137. long long diff;
  3138. struct request_queue *q;
  3139. disk_idx = rdev->raid_disk;
  3140. if (disk_idx < 0)
  3141. continue;
  3142. if (disk_idx >= conf->geo.raid_disks &&
  3143. disk_idx >= conf->prev.raid_disks)
  3144. continue;
  3145. disk = conf->mirrors + disk_idx;
  3146. if (test_bit(Replacement, &rdev->flags)) {
  3147. if (disk->replacement)
  3148. goto out_free_conf;
  3149. disk->replacement = rdev;
  3150. } else {
  3151. if (disk->rdev)
  3152. goto out_free_conf;
  3153. disk->rdev = rdev;
  3154. }
  3155. q = bdev_get_queue(rdev->bdev);
  3156. if (q->merge_bvec_fn)
  3157. mddev->merge_check_needed = 1;
  3158. diff = (rdev->new_data_offset - rdev->data_offset);
  3159. if (!mddev->reshape_backwards)
  3160. diff = -diff;
  3161. if (diff < 0)
  3162. diff = 0;
  3163. if (first || diff < min_offset_diff)
  3164. min_offset_diff = diff;
  3165. disk_stack_limits(mddev->gendisk, rdev->bdev,
  3166. rdev->data_offset << 9);
  3167. disk->head_position = 0;
  3168. }
  3169. /* need to check that every block has at least one working mirror */
  3170. if (!enough(conf, -1)) {
  3171. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  3172. mdname(mddev));
  3173. goto out_free_conf;
  3174. }
  3175. if (conf->reshape_progress != MaxSector) {
  3176. /* must ensure that shape change is supported */
  3177. if (conf->geo.far_copies != 1 &&
  3178. conf->geo.far_offset == 0)
  3179. goto out_free_conf;
  3180. if (conf->prev.far_copies != 1 &&
  3181. conf->geo.far_offset == 0)
  3182. goto out_free_conf;
  3183. }
  3184. mddev->degraded = 0;
  3185. for (i = 0;
  3186. i < conf->geo.raid_disks
  3187. || i < conf->prev.raid_disks;
  3188. i++) {
  3189. disk = conf->mirrors + i;
  3190. if (!disk->rdev && disk->replacement) {
  3191. /* The replacement is all we have - use it */
  3192. disk->rdev = disk->replacement;
  3193. disk->replacement = NULL;
  3194. clear_bit(Replacement, &disk->rdev->flags);
  3195. }
  3196. if (!disk->rdev ||
  3197. !test_bit(In_sync, &disk->rdev->flags)) {
  3198. disk->head_position = 0;
  3199. mddev->degraded++;
  3200. if (disk->rdev)
  3201. conf->fullsync = 1;
  3202. }
  3203. disk->recovery_disabled = mddev->recovery_disabled - 1;
  3204. }
  3205. if (mddev->recovery_cp != MaxSector)
  3206. printk(KERN_NOTICE "md/raid10:%s: not clean"
  3207. " -- starting background reconstruction\n",
  3208. mdname(mddev));
  3209. printk(KERN_INFO
  3210. "md/raid10:%s: active with %d out of %d devices\n",
  3211. mdname(mddev), conf->geo.raid_disks - mddev->degraded,
  3212. conf->geo.raid_disks);
  3213. /*
  3214. * Ok, everything is just fine now
  3215. */
  3216. mddev->dev_sectors = conf->dev_sectors;
  3217. size = raid10_size(mddev, 0, 0);
  3218. md_set_array_sectors(mddev, size);
  3219. mddev->resync_max_sectors = size;
  3220. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  3221. mddev->queue->backing_dev_info.congested_data = mddev;
  3222. /* Calculate max read-ahead size.
  3223. * We need to readahead at least twice a whole stripe....
  3224. * maybe...
  3225. */
  3226. {
  3227. int stripe = conf->geo.raid_disks *
  3228. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  3229. stripe /= conf->geo.near_copies;
  3230. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3231. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3232. }
  3233. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  3234. if (md_integrity_register(mddev))
  3235. goto out_free_conf;
  3236. if (conf->reshape_progress != MaxSector) {
  3237. unsigned long before_length, after_length;
  3238. before_length = ((1 << conf->prev.chunk_shift) *
  3239. conf->prev.far_copies);
  3240. after_length = ((1 << conf->geo.chunk_shift) *
  3241. conf->geo.far_copies);
  3242. if (max(before_length, after_length) > min_offset_diff) {
  3243. /* This cannot work */
  3244. printk("md/raid10: offset difference not enough to continue reshape\n");
  3245. goto out_free_conf;
  3246. }
  3247. conf->offset_diff = min_offset_diff;
  3248. conf->reshape_safe = conf->reshape_progress;
  3249. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3250. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3251. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3252. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3253. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3254. "reshape");
  3255. }
  3256. return 0;
  3257. out_free_conf:
  3258. md_unregister_thread(&mddev->thread);
  3259. if (conf->r10bio_pool)
  3260. mempool_destroy(conf->r10bio_pool);
  3261. safe_put_page(conf->tmppage);
  3262. kfree(conf->mirrors);
  3263. kfree(conf);
  3264. mddev->private = NULL;
  3265. out:
  3266. return -EIO;
  3267. }
  3268. static int stop(struct mddev *mddev)
  3269. {
  3270. struct r10conf *conf = mddev->private;
  3271. raise_barrier(conf, 0);
  3272. lower_barrier(conf);
  3273. md_unregister_thread(&mddev->thread);
  3274. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3275. if (conf->r10bio_pool)
  3276. mempool_destroy(conf->r10bio_pool);
  3277. kfree(conf->mirrors);
  3278. kfree(conf);
  3279. mddev->private = NULL;
  3280. return 0;
  3281. }
  3282. static void raid10_quiesce(struct mddev *mddev, int state)
  3283. {
  3284. struct r10conf *conf = mddev->private;
  3285. switch(state) {
  3286. case 1:
  3287. raise_barrier(conf, 0);
  3288. break;
  3289. case 0:
  3290. lower_barrier(conf);
  3291. break;
  3292. }
  3293. }
  3294. static int raid10_resize(struct mddev *mddev, sector_t sectors)
  3295. {
  3296. /* Resize of 'far' arrays is not supported.
  3297. * For 'near' and 'offset' arrays we can set the
  3298. * number of sectors used to be an appropriate multiple
  3299. * of the chunk size.
  3300. * For 'offset', this is far_copies*chunksize.
  3301. * For 'near' the multiplier is the LCM of
  3302. * near_copies and raid_disks.
  3303. * So if far_copies > 1 && !far_offset, fail.
  3304. * Else find LCM(raid_disks, near_copy)*far_copies and
  3305. * multiply by chunk_size. Then round to this number.
  3306. * This is mostly done by raid10_size()
  3307. */
  3308. struct r10conf *conf = mddev->private;
  3309. sector_t oldsize, size;
  3310. if (mddev->reshape_position != MaxSector)
  3311. return -EBUSY;
  3312. if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
  3313. return -EINVAL;
  3314. oldsize = raid10_size(mddev, 0, 0);
  3315. size = raid10_size(mddev, sectors, 0);
  3316. if (mddev->external_size &&
  3317. mddev->array_sectors > size)
  3318. return -EINVAL;
  3319. if (mddev->bitmap) {
  3320. int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
  3321. if (ret)
  3322. return ret;
  3323. }
  3324. md_set_array_sectors(mddev, size);
  3325. set_capacity(mddev->gendisk, mddev->array_sectors);
  3326. revalidate_disk(mddev->gendisk);
  3327. if (sectors > mddev->dev_sectors &&
  3328. mddev->recovery_cp > oldsize) {
  3329. mddev->recovery_cp = oldsize;
  3330. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3331. }
  3332. calc_sectors(conf, sectors);
  3333. mddev->dev_sectors = conf->dev_sectors;
  3334. mddev->resync_max_sectors = size;
  3335. return 0;
  3336. }
  3337. static void *raid10_takeover_raid0(struct mddev *mddev)
  3338. {
  3339. struct md_rdev *rdev;
  3340. struct r10conf *conf;
  3341. if (mddev->degraded > 0) {
  3342. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  3343. mdname(mddev));
  3344. return ERR_PTR(-EINVAL);
  3345. }
  3346. /* Set new parameters */
  3347. mddev->new_level = 10;
  3348. /* new layout: far_copies = 1, near_copies = 2 */
  3349. mddev->new_layout = (1<<8) + 2;
  3350. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3351. mddev->delta_disks = mddev->raid_disks;
  3352. mddev->raid_disks *= 2;
  3353. /* make sure it will be not marked as dirty */
  3354. mddev->recovery_cp = MaxSector;
  3355. conf = setup_conf(mddev);
  3356. if (!IS_ERR(conf)) {
  3357. rdev_for_each(rdev, mddev)
  3358. if (rdev->raid_disk >= 0)
  3359. rdev->new_raid_disk = rdev->raid_disk * 2;
  3360. conf->barrier = 1;
  3361. }
  3362. return conf;
  3363. }
  3364. static void *raid10_takeover(struct mddev *mddev)
  3365. {
  3366. struct r0conf *raid0_conf;
  3367. /* raid10 can take over:
  3368. * raid0 - providing it has only two drives
  3369. */
  3370. if (mddev->level == 0) {
  3371. /* for raid0 takeover only one zone is supported */
  3372. raid0_conf = mddev->private;
  3373. if (raid0_conf->nr_strip_zones > 1) {
  3374. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  3375. " with more than one zone.\n",
  3376. mdname(mddev));
  3377. return ERR_PTR(-EINVAL);
  3378. }
  3379. return raid10_takeover_raid0(mddev);
  3380. }
  3381. return ERR_PTR(-EINVAL);
  3382. }
  3383. static int raid10_check_reshape(struct mddev *mddev)
  3384. {
  3385. /* Called when there is a request to change
  3386. * - layout (to ->new_layout)
  3387. * - chunk size (to ->new_chunk_sectors)
  3388. * - raid_disks (by delta_disks)
  3389. * or when trying to restart a reshape that was ongoing.
  3390. *
  3391. * We need to validate the request and possibly allocate
  3392. * space if that might be an issue later.
  3393. *
  3394. * Currently we reject any reshape of a 'far' mode array,
  3395. * allow chunk size to change if new is generally acceptable,
  3396. * allow raid_disks to increase, and allow
  3397. * a switch between 'near' mode and 'offset' mode.
  3398. */
  3399. struct r10conf *conf = mddev->private;
  3400. struct geom geo;
  3401. if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
  3402. return -EINVAL;
  3403. if (setup_geo(&geo, mddev, geo_start) != conf->copies)
  3404. /* mustn't change number of copies */
  3405. return -EINVAL;
  3406. if (geo.far_copies > 1 && !geo.far_offset)
  3407. /* Cannot switch to 'far' mode */
  3408. return -EINVAL;
  3409. if (mddev->array_sectors & geo.chunk_mask)
  3410. /* not factor of array size */
  3411. return -EINVAL;
  3412. if (!enough(conf, -1))
  3413. return -EINVAL;
  3414. kfree(conf->mirrors_new);
  3415. conf->mirrors_new = NULL;
  3416. if (mddev->delta_disks > 0) {
  3417. /* allocate new 'mirrors' list */
  3418. conf->mirrors_new = kzalloc(
  3419. sizeof(struct mirror_info)
  3420. *(mddev->raid_disks +
  3421. mddev->delta_disks),
  3422. GFP_KERNEL);
  3423. if (!conf->mirrors_new)
  3424. return -ENOMEM;
  3425. }
  3426. return 0;
  3427. }
  3428. /*
  3429. * Need to check if array has failed when deciding whether to:
  3430. * - start an array
  3431. * - remove non-faulty devices
  3432. * - add a spare
  3433. * - allow a reshape
  3434. * This determination is simple when no reshape is happening.
  3435. * However if there is a reshape, we need to carefully check
  3436. * both the before and after sections.
  3437. * This is because some failed devices may only affect one
  3438. * of the two sections, and some non-in_sync devices may
  3439. * be insync in the section most affected by failed devices.
  3440. */
  3441. static int calc_degraded(struct r10conf *conf)
  3442. {
  3443. int degraded, degraded2;
  3444. int i;
  3445. rcu_read_lock();
  3446. degraded = 0;
  3447. /* 'prev' section first */
  3448. for (i = 0; i < conf->prev.raid_disks; i++) {
  3449. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3450. if (!rdev || test_bit(Faulty, &rdev->flags))
  3451. degraded++;
  3452. else if (!test_bit(In_sync, &rdev->flags))
  3453. /* When we can reduce the number of devices in
  3454. * an array, this might not contribute to
  3455. * 'degraded'. It does now.
  3456. */
  3457. degraded++;
  3458. }
  3459. rcu_read_unlock();
  3460. if (conf->geo.raid_disks == conf->prev.raid_disks)
  3461. return degraded;
  3462. rcu_read_lock();
  3463. degraded2 = 0;
  3464. for (i = 0; i < conf->geo.raid_disks; i++) {
  3465. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3466. if (!rdev || test_bit(Faulty, &rdev->flags))
  3467. degraded2++;
  3468. else if (!test_bit(In_sync, &rdev->flags)) {
  3469. /* If reshape is increasing the number of devices,
  3470. * this section has already been recovered, so
  3471. * it doesn't contribute to degraded.
  3472. * else it does.
  3473. */
  3474. if (conf->geo.raid_disks <= conf->prev.raid_disks)
  3475. degraded2++;
  3476. }
  3477. }
  3478. rcu_read_unlock();
  3479. if (degraded2 > degraded)
  3480. return degraded2;
  3481. return degraded;
  3482. }
  3483. static int raid10_start_reshape(struct mddev *mddev)
  3484. {
  3485. /* A 'reshape' has been requested. This commits
  3486. * the various 'new' fields and sets MD_RECOVER_RESHAPE
  3487. * This also checks if there are enough spares and adds them
  3488. * to the array.
  3489. * We currently require enough spares to make the final
  3490. * array non-degraded. We also require that the difference
  3491. * between old and new data_offset - on each device - is
  3492. * enough that we never risk over-writing.
  3493. */
  3494. unsigned long before_length, after_length;
  3495. sector_t min_offset_diff = 0;
  3496. int first = 1;
  3497. struct geom new;
  3498. struct r10conf *conf = mddev->private;
  3499. struct md_rdev *rdev;
  3500. int spares = 0;
  3501. int ret;
  3502. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3503. return -EBUSY;
  3504. if (setup_geo(&new, mddev, geo_start) != conf->copies)
  3505. return -EINVAL;
  3506. before_length = ((1 << conf->prev.chunk_shift) *
  3507. conf->prev.far_copies);
  3508. after_length = ((1 << conf->geo.chunk_shift) *
  3509. conf->geo.far_copies);
  3510. rdev_for_each(rdev, mddev) {
  3511. if (!test_bit(In_sync, &rdev->flags)
  3512. && !test_bit(Faulty, &rdev->flags))
  3513. spares++;
  3514. if (rdev->raid_disk >= 0) {
  3515. long long diff = (rdev->new_data_offset
  3516. - rdev->data_offset);
  3517. if (!mddev->reshape_backwards)
  3518. diff = -diff;
  3519. if (diff < 0)
  3520. diff = 0;
  3521. if (first || diff < min_offset_diff)
  3522. min_offset_diff = diff;
  3523. }
  3524. }
  3525. if (max(before_length, after_length) > min_offset_diff)
  3526. return -EINVAL;
  3527. if (spares < mddev->delta_disks)
  3528. return -EINVAL;
  3529. conf->offset_diff = min_offset_diff;
  3530. spin_lock_irq(&conf->device_lock);
  3531. if (conf->mirrors_new) {
  3532. memcpy(conf->mirrors_new, conf->mirrors,
  3533. sizeof(struct mirror_info)*conf->prev.raid_disks);
  3534. smp_mb();
  3535. kfree(conf->mirrors_old); /* FIXME and elsewhere */
  3536. conf->mirrors_old = conf->mirrors;
  3537. conf->mirrors = conf->mirrors_new;
  3538. conf->mirrors_new = NULL;
  3539. }
  3540. setup_geo(&conf->geo, mddev, geo_start);
  3541. smp_mb();
  3542. if (mddev->reshape_backwards) {
  3543. sector_t size = raid10_size(mddev, 0, 0);
  3544. if (size < mddev->array_sectors) {
  3545. spin_unlock_irq(&conf->device_lock);
  3546. printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
  3547. mdname(mddev));
  3548. return -EINVAL;
  3549. }
  3550. mddev->resync_max_sectors = size;
  3551. conf->reshape_progress = size;
  3552. } else
  3553. conf->reshape_progress = 0;
  3554. spin_unlock_irq(&conf->device_lock);
  3555. if (mddev->delta_disks && mddev->bitmap) {
  3556. ret = bitmap_resize(mddev->bitmap,
  3557. raid10_size(mddev, 0,
  3558. conf->geo.raid_disks),
  3559. 0, 0);
  3560. if (ret)
  3561. goto abort;
  3562. }
  3563. if (mddev->delta_disks > 0) {
  3564. rdev_for_each(rdev, mddev)
  3565. if (rdev->raid_disk < 0 &&
  3566. !test_bit(Faulty, &rdev->flags)) {
  3567. if (raid10_add_disk(mddev, rdev) == 0) {
  3568. if (rdev->raid_disk >=
  3569. conf->prev.raid_disks)
  3570. set_bit(In_sync, &rdev->flags);
  3571. else
  3572. rdev->recovery_offset = 0;
  3573. if (sysfs_link_rdev(mddev, rdev))
  3574. /* Failure here is OK */;
  3575. }
  3576. } else if (rdev->raid_disk >= conf->prev.raid_disks
  3577. && !test_bit(Faulty, &rdev->flags)) {
  3578. /* This is a spare that was manually added */
  3579. set_bit(In_sync, &rdev->flags);
  3580. }
  3581. }
  3582. /* When a reshape changes the number of devices,
  3583. * ->degraded is measured against the larger of the
  3584. * pre and post numbers.
  3585. */
  3586. spin_lock_irq(&conf->device_lock);
  3587. mddev->degraded = calc_degraded(conf);
  3588. spin_unlock_irq(&conf->device_lock);
  3589. mddev->raid_disks = conf->geo.raid_disks;
  3590. mddev->reshape_position = conf->reshape_progress;
  3591. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3592. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3593. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3594. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3595. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3596. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3597. "reshape");
  3598. if (!mddev->sync_thread) {
  3599. ret = -EAGAIN;
  3600. goto abort;
  3601. }
  3602. conf->reshape_checkpoint = jiffies;
  3603. md_wakeup_thread(mddev->sync_thread);
  3604. md_new_event(mddev);
  3605. return 0;
  3606. abort:
  3607. mddev->recovery = 0;
  3608. spin_lock_irq(&conf->device_lock);
  3609. conf->geo = conf->prev;
  3610. mddev->raid_disks = conf->geo.raid_disks;
  3611. rdev_for_each(rdev, mddev)
  3612. rdev->new_data_offset = rdev->data_offset;
  3613. smp_wmb();
  3614. conf->reshape_progress = MaxSector;
  3615. mddev->reshape_position = MaxSector;
  3616. spin_unlock_irq(&conf->device_lock);
  3617. return ret;
  3618. }
  3619. /* Calculate the last device-address that could contain
  3620. * any block from the chunk that includes the array-address 's'
  3621. * and report the next address.
  3622. * i.e. the address returned will be chunk-aligned and after
  3623. * any data that is in the chunk containing 's'.
  3624. */
  3625. static sector_t last_dev_address(sector_t s, struct geom *geo)
  3626. {
  3627. s = (s | geo->chunk_mask) + 1;
  3628. s >>= geo->chunk_shift;
  3629. s *= geo->near_copies;
  3630. s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
  3631. s *= geo->far_copies;
  3632. s <<= geo->chunk_shift;
  3633. return s;
  3634. }
  3635. /* Calculate the first device-address that could contain
  3636. * any block from the chunk that includes the array-address 's'.
  3637. * This too will be the start of a chunk
  3638. */
  3639. static sector_t first_dev_address(sector_t s, struct geom *geo)
  3640. {
  3641. s >>= geo->chunk_shift;
  3642. s *= geo->near_copies;
  3643. sector_div(s, geo->raid_disks);
  3644. s *= geo->far_copies;
  3645. s <<= geo->chunk_shift;
  3646. return s;
  3647. }
  3648. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  3649. int *skipped)
  3650. {
  3651. /* We simply copy at most one chunk (smallest of old and new)
  3652. * at a time, possibly less if that exceeds RESYNC_PAGES,
  3653. * or we hit a bad block or something.
  3654. * This might mean we pause for normal IO in the middle of
  3655. * a chunk, but that is not a problem was mddev->reshape_position
  3656. * can record any location.
  3657. *
  3658. * If we will want to write to a location that isn't
  3659. * yet recorded as 'safe' (i.e. in metadata on disk) then
  3660. * we need to flush all reshape requests and update the metadata.
  3661. *
  3662. * When reshaping forwards (e.g. to more devices), we interpret
  3663. * 'safe' as the earliest block which might not have been copied
  3664. * down yet. We divide this by previous stripe size and multiply
  3665. * by previous stripe length to get lowest device offset that we
  3666. * cannot write to yet.
  3667. * We interpret 'sector_nr' as an address that we want to write to.
  3668. * From this we use last_device_address() to find where we might
  3669. * write to, and first_device_address on the 'safe' position.
  3670. * If this 'next' write position is after the 'safe' position,
  3671. * we must update the metadata to increase the 'safe' position.
  3672. *
  3673. * When reshaping backwards, we round in the opposite direction
  3674. * and perform the reverse test: next write position must not be
  3675. * less than current safe position.
  3676. *
  3677. * In all this the minimum difference in data offsets
  3678. * (conf->offset_diff - always positive) allows a bit of slack,
  3679. * so next can be after 'safe', but not by more than offset_disk
  3680. *
  3681. * We need to prepare all the bios here before we start any IO
  3682. * to ensure the size we choose is acceptable to all devices.
  3683. * The means one for each copy for write-out and an extra one for
  3684. * read-in.
  3685. * We store the read-in bio in ->master_bio and the others in
  3686. * ->devs[x].bio and ->devs[x].repl_bio.
  3687. */
  3688. struct r10conf *conf = mddev->private;
  3689. struct r10bio *r10_bio;
  3690. sector_t next, safe, last;
  3691. int max_sectors;
  3692. int nr_sectors;
  3693. int s;
  3694. struct md_rdev *rdev;
  3695. int need_flush = 0;
  3696. struct bio *blist;
  3697. struct bio *bio, *read_bio;
  3698. int sectors_done = 0;
  3699. if (sector_nr == 0) {
  3700. /* If restarting in the middle, skip the initial sectors */
  3701. if (mddev->reshape_backwards &&
  3702. conf->reshape_progress < raid10_size(mddev, 0, 0)) {
  3703. sector_nr = (raid10_size(mddev, 0, 0)
  3704. - conf->reshape_progress);
  3705. } else if (!mddev->reshape_backwards &&
  3706. conf->reshape_progress > 0)
  3707. sector_nr = conf->reshape_progress;
  3708. if (sector_nr) {
  3709. mddev->curr_resync_completed = sector_nr;
  3710. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3711. *skipped = 1;
  3712. return sector_nr;
  3713. }
  3714. }
  3715. /* We don't use sector_nr to track where we are up to
  3716. * as that doesn't work well for ->reshape_backwards.
  3717. * So just use ->reshape_progress.
  3718. */
  3719. if (mddev->reshape_backwards) {
  3720. /* 'next' is the earliest device address that we might
  3721. * write to for this chunk in the new layout
  3722. */
  3723. next = first_dev_address(conf->reshape_progress - 1,
  3724. &conf->geo);
  3725. /* 'safe' is the last device address that we might read from
  3726. * in the old layout after a restart
  3727. */
  3728. safe = last_dev_address(conf->reshape_safe - 1,
  3729. &conf->prev);
  3730. if (next + conf->offset_diff < safe)
  3731. need_flush = 1;
  3732. last = conf->reshape_progress - 1;
  3733. sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
  3734. & conf->prev.chunk_mask);
  3735. if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
  3736. sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
  3737. } else {
  3738. /* 'next' is after the last device address that we
  3739. * might write to for this chunk in the new layout
  3740. */
  3741. next = last_dev_address(conf->reshape_progress, &conf->geo);
  3742. /* 'safe' is the earliest device address that we might
  3743. * read from in the old layout after a restart
  3744. */
  3745. safe = first_dev_address(conf->reshape_safe, &conf->prev);
  3746. /* Need to update metadata if 'next' might be beyond 'safe'
  3747. * as that would possibly corrupt data
  3748. */
  3749. if (next > safe + conf->offset_diff)
  3750. need_flush = 1;
  3751. sector_nr = conf->reshape_progress;
  3752. last = sector_nr | (conf->geo.chunk_mask
  3753. & conf->prev.chunk_mask);
  3754. if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
  3755. last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
  3756. }
  3757. if (need_flush ||
  3758. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3759. /* Need to update reshape_position in metadata */
  3760. wait_barrier(conf);
  3761. mddev->reshape_position = conf->reshape_progress;
  3762. if (mddev->reshape_backwards)
  3763. mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
  3764. - conf->reshape_progress;
  3765. else
  3766. mddev->curr_resync_completed = conf->reshape_progress;
  3767. conf->reshape_checkpoint = jiffies;
  3768. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3769. md_wakeup_thread(mddev->thread);
  3770. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3771. kthread_should_stop());
  3772. conf->reshape_safe = mddev->reshape_position;
  3773. allow_barrier(conf);
  3774. }
  3775. read_more:
  3776. /* Now schedule reads for blocks from sector_nr to last */
  3777. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  3778. raise_barrier(conf, sectors_done != 0);
  3779. atomic_set(&r10_bio->remaining, 0);
  3780. r10_bio->mddev = mddev;
  3781. r10_bio->sector = sector_nr;
  3782. set_bit(R10BIO_IsReshape, &r10_bio->state);
  3783. r10_bio->sectors = last - sector_nr + 1;
  3784. rdev = read_balance(conf, r10_bio, &max_sectors);
  3785. BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
  3786. if (!rdev) {
  3787. /* Cannot read from here, so need to record bad blocks
  3788. * on all the target devices.
  3789. */
  3790. // FIXME
  3791. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  3792. return sectors_done;
  3793. }
  3794. read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
  3795. read_bio->bi_bdev = rdev->bdev;
  3796. read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
  3797. + rdev->data_offset);
  3798. read_bio->bi_private = r10_bio;
  3799. read_bio->bi_end_io = end_sync_read;
  3800. read_bio->bi_rw = READ;
  3801. read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  3802. read_bio->bi_flags |= 1 << BIO_UPTODATE;
  3803. read_bio->bi_vcnt = 0;
  3804. read_bio->bi_idx = 0;
  3805. read_bio->bi_size = 0;
  3806. r10_bio->master_bio = read_bio;
  3807. r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
  3808. /* Now find the locations in the new layout */
  3809. __raid10_find_phys(&conf->geo, r10_bio);
  3810. blist = read_bio;
  3811. read_bio->bi_next = NULL;
  3812. for (s = 0; s < conf->copies*2; s++) {
  3813. struct bio *b;
  3814. int d = r10_bio->devs[s/2].devnum;
  3815. struct md_rdev *rdev2;
  3816. if (s&1) {
  3817. rdev2 = conf->mirrors[d].replacement;
  3818. b = r10_bio->devs[s/2].repl_bio;
  3819. } else {
  3820. rdev2 = conf->mirrors[d].rdev;
  3821. b = r10_bio->devs[s/2].bio;
  3822. }
  3823. if (!rdev2 || test_bit(Faulty, &rdev2->flags))
  3824. continue;
  3825. b->bi_bdev = rdev2->bdev;
  3826. b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
  3827. b->bi_private = r10_bio;
  3828. b->bi_end_io = end_reshape_write;
  3829. b->bi_rw = WRITE;
  3830. b->bi_flags &= ~(BIO_POOL_MASK - 1);
  3831. b->bi_flags |= 1 << BIO_UPTODATE;
  3832. b->bi_next = blist;
  3833. b->bi_vcnt = 0;
  3834. b->bi_idx = 0;
  3835. b->bi_size = 0;
  3836. blist = b;
  3837. }
  3838. /* Now add as many pages as possible to all of these bios. */
  3839. nr_sectors = 0;
  3840. for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
  3841. struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
  3842. int len = (max_sectors - s) << 9;
  3843. if (len > PAGE_SIZE)
  3844. len = PAGE_SIZE;
  3845. for (bio = blist; bio ; bio = bio->bi_next) {
  3846. struct bio *bio2;
  3847. if (bio_add_page(bio, page, len, 0))
  3848. continue;
  3849. /* Didn't fit, must stop */
  3850. for (bio2 = blist;
  3851. bio2 && bio2 != bio;
  3852. bio2 = bio2->bi_next) {
  3853. /* Remove last page from this bio */
  3854. bio2->bi_vcnt--;
  3855. bio2->bi_size -= len;
  3856. bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
  3857. }
  3858. goto bio_full;
  3859. }
  3860. sector_nr += len >> 9;
  3861. nr_sectors += len >> 9;
  3862. }
  3863. bio_full:
  3864. r10_bio->sectors = nr_sectors;
  3865. /* Now submit the read */
  3866. md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
  3867. atomic_inc(&r10_bio->remaining);
  3868. read_bio->bi_next = NULL;
  3869. generic_make_request(read_bio);
  3870. sector_nr += nr_sectors;
  3871. sectors_done += nr_sectors;
  3872. if (sector_nr <= last)
  3873. goto read_more;
  3874. /* Now that we have done the whole section we can
  3875. * update reshape_progress
  3876. */
  3877. if (mddev->reshape_backwards)
  3878. conf->reshape_progress -= sectors_done;
  3879. else
  3880. conf->reshape_progress += sectors_done;
  3881. return sectors_done;
  3882. }
  3883. static void end_reshape_request(struct r10bio *r10_bio);
  3884. static int handle_reshape_read_error(struct mddev *mddev,
  3885. struct r10bio *r10_bio);
  3886. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  3887. {
  3888. /* Reshape read completed. Hopefully we have a block
  3889. * to write out.
  3890. * If we got a read error then we do sync 1-page reads from
  3891. * elsewhere until we find the data - or give up.
  3892. */
  3893. struct r10conf *conf = mddev->private;
  3894. int s;
  3895. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  3896. if (handle_reshape_read_error(mddev, r10_bio) < 0) {
  3897. /* Reshape has been aborted */
  3898. md_done_sync(mddev, r10_bio->sectors, 0);
  3899. return;
  3900. }
  3901. /* We definitely have the data in the pages, schedule the
  3902. * writes.
  3903. */
  3904. atomic_set(&r10_bio->remaining, 1);
  3905. for (s = 0; s < conf->copies*2; s++) {
  3906. struct bio *b;
  3907. int d = r10_bio->devs[s/2].devnum;
  3908. struct md_rdev *rdev;
  3909. if (s&1) {
  3910. rdev = conf->mirrors[d].replacement;
  3911. b = r10_bio->devs[s/2].repl_bio;
  3912. } else {
  3913. rdev = conf->mirrors[d].rdev;
  3914. b = r10_bio->devs[s/2].bio;
  3915. }
  3916. if (!rdev || test_bit(Faulty, &rdev->flags))
  3917. continue;
  3918. atomic_inc(&rdev->nr_pending);
  3919. md_sync_acct(b->bi_bdev, r10_bio->sectors);
  3920. atomic_inc(&r10_bio->remaining);
  3921. b->bi_next = NULL;
  3922. generic_make_request(b);
  3923. }
  3924. end_reshape_request(r10_bio);
  3925. }
  3926. static void end_reshape(struct r10conf *conf)
  3927. {
  3928. if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
  3929. return;
  3930. spin_lock_irq(&conf->device_lock);
  3931. conf->prev = conf->geo;
  3932. md_finish_reshape(conf->mddev);
  3933. smp_wmb();
  3934. conf->reshape_progress = MaxSector;
  3935. spin_unlock_irq(&conf->device_lock);
  3936. /* read-ahead size must cover two whole stripes, which is
  3937. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3938. */
  3939. if (conf->mddev->queue) {
  3940. int stripe = conf->geo.raid_disks *
  3941. ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
  3942. stripe /= conf->geo.near_copies;
  3943. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3944. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3945. }
  3946. conf->fullsync = 0;
  3947. }
  3948. static int handle_reshape_read_error(struct mddev *mddev,
  3949. struct r10bio *r10_bio)
  3950. {
  3951. /* Use sync reads to get the blocks from somewhere else */
  3952. int sectors = r10_bio->sectors;
  3953. struct r10bio r10b;
  3954. struct r10conf *conf = mddev->private;
  3955. int slot = 0;
  3956. int idx = 0;
  3957. struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
  3958. r10b.sector = r10_bio->sector;
  3959. __raid10_find_phys(&conf->prev, &r10b);
  3960. while (sectors) {
  3961. int s = sectors;
  3962. int success = 0;
  3963. int first_slot = slot;
  3964. if (s > (PAGE_SIZE >> 9))
  3965. s = PAGE_SIZE >> 9;
  3966. while (!success) {
  3967. int d = r10b.devs[slot].devnum;
  3968. struct md_rdev *rdev = conf->mirrors[d].rdev;
  3969. sector_t addr;
  3970. if (rdev == NULL ||
  3971. test_bit(Faulty, &rdev->flags) ||
  3972. !test_bit(In_sync, &rdev->flags))
  3973. goto failed;
  3974. addr = r10b.devs[slot].addr + idx * PAGE_SIZE;
  3975. success = sync_page_io(rdev,
  3976. addr,
  3977. s << 9,
  3978. bvec[idx].bv_page,
  3979. READ, false);
  3980. if (success)
  3981. break;
  3982. failed:
  3983. slot++;
  3984. if (slot >= conf->copies)
  3985. slot = 0;
  3986. if (slot == first_slot)
  3987. break;
  3988. }
  3989. if (!success) {
  3990. /* couldn't read this block, must give up */
  3991. set_bit(MD_RECOVERY_INTR,
  3992. &mddev->recovery);
  3993. return -EIO;
  3994. }
  3995. sectors -= s;
  3996. idx++;
  3997. }
  3998. return 0;
  3999. }
  4000. static void end_reshape_write(struct bio *bio, int error)
  4001. {
  4002. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  4003. struct r10bio *r10_bio = bio->bi_private;
  4004. struct mddev *mddev = r10_bio->mddev;
  4005. struct r10conf *conf = mddev->private;
  4006. int d;
  4007. int slot;
  4008. int repl;
  4009. struct md_rdev *rdev = NULL;
  4010. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  4011. if (repl)
  4012. rdev = conf->mirrors[d].replacement;
  4013. if (!rdev) {
  4014. smp_mb();
  4015. rdev = conf->mirrors[d].rdev;
  4016. }
  4017. if (!uptodate) {
  4018. /* FIXME should record badblock */
  4019. md_error(mddev, rdev);
  4020. }
  4021. rdev_dec_pending(rdev, mddev);
  4022. end_reshape_request(r10_bio);
  4023. }
  4024. static void end_reshape_request(struct r10bio *r10_bio)
  4025. {
  4026. if (!atomic_dec_and_test(&r10_bio->remaining))
  4027. return;
  4028. md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
  4029. bio_put(r10_bio->master_bio);
  4030. put_buf(r10_bio);
  4031. }
  4032. static void raid10_finish_reshape(struct mddev *mddev)
  4033. {
  4034. struct r10conf *conf = mddev->private;
  4035. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  4036. return;
  4037. if (mddev->delta_disks > 0) {
  4038. sector_t size = raid10_size(mddev, 0, 0);
  4039. md_set_array_sectors(mddev, size);
  4040. if (mddev->recovery_cp > mddev->resync_max_sectors) {
  4041. mddev->recovery_cp = mddev->resync_max_sectors;
  4042. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4043. }
  4044. mddev->resync_max_sectors = size;
  4045. set_capacity(mddev->gendisk, mddev->array_sectors);
  4046. revalidate_disk(mddev->gendisk);
  4047. } else {
  4048. int d;
  4049. for (d = conf->geo.raid_disks ;
  4050. d < conf->geo.raid_disks - mddev->delta_disks;
  4051. d++) {
  4052. struct md_rdev *rdev = conf->mirrors[d].rdev;
  4053. if (rdev)
  4054. clear_bit(In_sync, &rdev->flags);
  4055. rdev = conf->mirrors[d].replacement;
  4056. if (rdev)
  4057. clear_bit(In_sync, &rdev->flags);
  4058. }
  4059. }
  4060. mddev->layout = mddev->new_layout;
  4061. mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
  4062. mddev->reshape_position = MaxSector;
  4063. mddev->delta_disks = 0;
  4064. mddev->reshape_backwards = 0;
  4065. }
  4066. static struct md_personality raid10_personality =
  4067. {
  4068. .name = "raid10",
  4069. .level = 10,
  4070. .owner = THIS_MODULE,
  4071. .make_request = make_request,
  4072. .run = run,
  4073. .stop = stop,
  4074. .status = status,
  4075. .error_handler = error,
  4076. .hot_add_disk = raid10_add_disk,
  4077. .hot_remove_disk= raid10_remove_disk,
  4078. .spare_active = raid10_spare_active,
  4079. .sync_request = sync_request,
  4080. .quiesce = raid10_quiesce,
  4081. .size = raid10_size,
  4082. .resize = raid10_resize,
  4083. .takeover = raid10_takeover,
  4084. .check_reshape = raid10_check_reshape,
  4085. .start_reshape = raid10_start_reshape,
  4086. .finish_reshape = raid10_finish_reshape,
  4087. };
  4088. static int __init raid_init(void)
  4089. {
  4090. return register_md_personality(&raid10_personality);
  4091. }
  4092. static void raid_exit(void)
  4093. {
  4094. unregister_md_personality(&raid10_personality);
  4095. }
  4096. module_init(raid_init);
  4097. module_exit(raid_exit);
  4098. MODULE_LICENSE("GPL");
  4099. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  4100. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  4101. MODULE_ALIAS("md-raid10");
  4102. MODULE_ALIAS("md-level-10");
  4103. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);