raid10.c 128 KB

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