raid10.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665
  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. while (n--) {
  1418. if (conf->mirrors[first].rdev &&
  1419. first != ignore)
  1420. cnt++;
  1421. first = (first+1) % geo->raid_disks;
  1422. }
  1423. if (cnt == 0)
  1424. return 0;
  1425. } while (first != 0);
  1426. return 1;
  1427. }
  1428. static int enough(struct r10conf *conf, int ignore)
  1429. {
  1430. return _enough(conf, &conf->geo, ignore) &&
  1431. _enough(conf, &conf->prev, ignore);
  1432. }
  1433. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1434. {
  1435. char b[BDEVNAME_SIZE];
  1436. struct r10conf *conf = mddev->private;
  1437. /*
  1438. * If it is not operational, then we have already marked it as dead
  1439. * else if it is the last working disks, ignore the error, let the
  1440. * next level up know.
  1441. * else mark the drive as failed
  1442. */
  1443. if (test_bit(In_sync, &rdev->flags)
  1444. && !enough(conf, rdev->raid_disk))
  1445. /*
  1446. * Don't fail the drive, just return an IO error.
  1447. */
  1448. return;
  1449. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1450. unsigned long flags;
  1451. spin_lock_irqsave(&conf->device_lock, flags);
  1452. mddev->degraded++;
  1453. spin_unlock_irqrestore(&conf->device_lock, flags);
  1454. /*
  1455. * if recovery is running, make sure it aborts.
  1456. */
  1457. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1458. }
  1459. set_bit(Blocked, &rdev->flags);
  1460. set_bit(Faulty, &rdev->flags);
  1461. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1462. printk(KERN_ALERT
  1463. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1464. "md/raid10:%s: Operation continuing on %d devices.\n",
  1465. mdname(mddev), bdevname(rdev->bdev, b),
  1466. mdname(mddev), conf->geo.raid_disks - mddev->degraded);
  1467. }
  1468. static void print_conf(struct r10conf *conf)
  1469. {
  1470. int i;
  1471. struct raid10_info *tmp;
  1472. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1473. if (!conf) {
  1474. printk(KERN_DEBUG "(!conf)\n");
  1475. return;
  1476. }
  1477. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
  1478. conf->geo.raid_disks);
  1479. for (i = 0; i < conf->geo.raid_disks; i++) {
  1480. char b[BDEVNAME_SIZE];
  1481. tmp = conf->mirrors + i;
  1482. if (tmp->rdev)
  1483. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1484. i, !test_bit(In_sync, &tmp->rdev->flags),
  1485. !test_bit(Faulty, &tmp->rdev->flags),
  1486. bdevname(tmp->rdev->bdev,b));
  1487. }
  1488. }
  1489. static void close_sync(struct r10conf *conf)
  1490. {
  1491. wait_barrier(conf);
  1492. allow_barrier(conf);
  1493. mempool_destroy(conf->r10buf_pool);
  1494. conf->r10buf_pool = NULL;
  1495. }
  1496. static int raid10_spare_active(struct mddev *mddev)
  1497. {
  1498. int i;
  1499. struct r10conf *conf = mddev->private;
  1500. struct raid10_info *tmp;
  1501. int count = 0;
  1502. unsigned long flags;
  1503. /*
  1504. * Find all non-in_sync disks within the RAID10 configuration
  1505. * and mark them in_sync
  1506. */
  1507. for (i = 0; i < conf->geo.raid_disks; i++) {
  1508. tmp = conf->mirrors + i;
  1509. if (tmp->replacement
  1510. && tmp->replacement->recovery_offset == MaxSector
  1511. && !test_bit(Faulty, &tmp->replacement->flags)
  1512. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1513. /* Replacement has just become active */
  1514. if (!tmp->rdev
  1515. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1516. count++;
  1517. if (tmp->rdev) {
  1518. /* Replaced device not technically faulty,
  1519. * but we need to be sure it gets removed
  1520. * and never re-added.
  1521. */
  1522. set_bit(Faulty, &tmp->rdev->flags);
  1523. sysfs_notify_dirent_safe(
  1524. tmp->rdev->sysfs_state);
  1525. }
  1526. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1527. } else if (tmp->rdev
  1528. && !test_bit(Faulty, &tmp->rdev->flags)
  1529. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1530. count++;
  1531. sysfs_notify_dirent(tmp->rdev->sysfs_state);
  1532. }
  1533. }
  1534. spin_lock_irqsave(&conf->device_lock, flags);
  1535. mddev->degraded -= count;
  1536. spin_unlock_irqrestore(&conf->device_lock, flags);
  1537. print_conf(conf);
  1538. return count;
  1539. }
  1540. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1541. {
  1542. struct r10conf *conf = mddev->private;
  1543. int err = -EEXIST;
  1544. int mirror;
  1545. int first = 0;
  1546. int last = conf->geo.raid_disks - 1;
  1547. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1548. if (mddev->recovery_cp < MaxSector)
  1549. /* only hot-add to in-sync arrays, as recovery is
  1550. * very different from resync
  1551. */
  1552. return -EBUSY;
  1553. if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
  1554. return -EINVAL;
  1555. if (rdev->raid_disk >= 0)
  1556. first = last = rdev->raid_disk;
  1557. if (q->merge_bvec_fn) {
  1558. set_bit(Unmerged, &rdev->flags);
  1559. mddev->merge_check_needed = 1;
  1560. }
  1561. if (rdev->saved_raid_disk >= first &&
  1562. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1563. mirror = rdev->saved_raid_disk;
  1564. else
  1565. mirror = first;
  1566. for ( ; mirror <= last ; mirror++) {
  1567. struct raid10_info *p = &conf->mirrors[mirror];
  1568. if (p->recovery_disabled == mddev->recovery_disabled)
  1569. continue;
  1570. if (p->rdev) {
  1571. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1572. p->replacement != NULL)
  1573. continue;
  1574. clear_bit(In_sync, &rdev->flags);
  1575. set_bit(Replacement, &rdev->flags);
  1576. rdev->raid_disk = mirror;
  1577. err = 0;
  1578. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1579. rdev->data_offset << 9);
  1580. conf->fullsync = 1;
  1581. rcu_assign_pointer(p->replacement, rdev);
  1582. break;
  1583. }
  1584. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1585. rdev->data_offset << 9);
  1586. p->head_position = 0;
  1587. p->recovery_disabled = mddev->recovery_disabled - 1;
  1588. rdev->raid_disk = mirror;
  1589. err = 0;
  1590. if (rdev->saved_raid_disk != mirror)
  1591. conf->fullsync = 1;
  1592. rcu_assign_pointer(p->rdev, rdev);
  1593. break;
  1594. }
  1595. if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
  1596. /* Some requests might not have seen this new
  1597. * merge_bvec_fn. We must wait for them to complete
  1598. * before merging the device fully.
  1599. * First we make sure any code which has tested
  1600. * our function has submitted the request, then
  1601. * we wait for all outstanding requests to complete.
  1602. */
  1603. synchronize_sched();
  1604. raise_barrier(conf, 0);
  1605. lower_barrier(conf);
  1606. clear_bit(Unmerged, &rdev->flags);
  1607. }
  1608. md_integrity_add_rdev(rdev, mddev);
  1609. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  1610. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  1611. print_conf(conf);
  1612. return err;
  1613. }
  1614. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1615. {
  1616. struct r10conf *conf = mddev->private;
  1617. int err = 0;
  1618. int number = rdev->raid_disk;
  1619. struct md_rdev **rdevp;
  1620. struct raid10_info *p = conf->mirrors + number;
  1621. print_conf(conf);
  1622. if (rdev == p->rdev)
  1623. rdevp = &p->rdev;
  1624. else if (rdev == p->replacement)
  1625. rdevp = &p->replacement;
  1626. else
  1627. return 0;
  1628. if (test_bit(In_sync, &rdev->flags) ||
  1629. atomic_read(&rdev->nr_pending)) {
  1630. err = -EBUSY;
  1631. goto abort;
  1632. }
  1633. /* Only remove faulty devices if recovery
  1634. * is not possible.
  1635. */
  1636. if (!test_bit(Faulty, &rdev->flags) &&
  1637. mddev->recovery_disabled != p->recovery_disabled &&
  1638. (!p->replacement || p->replacement == rdev) &&
  1639. number < conf->geo.raid_disks &&
  1640. enough(conf, -1)) {
  1641. err = -EBUSY;
  1642. goto abort;
  1643. }
  1644. *rdevp = NULL;
  1645. synchronize_rcu();
  1646. if (atomic_read(&rdev->nr_pending)) {
  1647. /* lost the race, try later */
  1648. err = -EBUSY;
  1649. *rdevp = rdev;
  1650. goto abort;
  1651. } else if (p->replacement) {
  1652. /* We must have just cleared 'rdev' */
  1653. p->rdev = p->replacement;
  1654. clear_bit(Replacement, &p->replacement->flags);
  1655. smp_mb(); /* Make sure other CPUs may see both as identical
  1656. * but will never see neither -- if they are careful.
  1657. */
  1658. p->replacement = NULL;
  1659. clear_bit(WantReplacement, &rdev->flags);
  1660. } else
  1661. /* We might have just remove the Replacement as faulty
  1662. * Clear the flag just in case
  1663. */
  1664. clear_bit(WantReplacement, &rdev->flags);
  1665. err = md_integrity_register(mddev);
  1666. abort:
  1667. print_conf(conf);
  1668. return err;
  1669. }
  1670. static void end_sync_read(struct bio *bio, int error)
  1671. {
  1672. struct r10bio *r10_bio = bio->bi_private;
  1673. struct r10conf *conf = r10_bio->mddev->private;
  1674. int d;
  1675. if (bio == r10_bio->master_bio) {
  1676. /* this is a reshape read */
  1677. d = r10_bio->read_slot; /* really the read dev */
  1678. } else
  1679. d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1680. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1681. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1682. else
  1683. /* The write handler will notice the lack of
  1684. * R10BIO_Uptodate and record any errors etc
  1685. */
  1686. atomic_add(r10_bio->sectors,
  1687. &conf->mirrors[d].rdev->corrected_errors);
  1688. /* for reconstruct, we always reschedule after a read.
  1689. * for resync, only after all reads
  1690. */
  1691. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1692. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1693. atomic_dec_and_test(&r10_bio->remaining)) {
  1694. /* we have read all the blocks,
  1695. * do the comparison in process context in raid10d
  1696. */
  1697. reschedule_retry(r10_bio);
  1698. }
  1699. }
  1700. static void end_sync_request(struct r10bio *r10_bio)
  1701. {
  1702. struct mddev *mddev = r10_bio->mddev;
  1703. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1704. if (r10_bio->master_bio == NULL) {
  1705. /* the primary of several recovery bios */
  1706. sector_t s = r10_bio->sectors;
  1707. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1708. test_bit(R10BIO_WriteError, &r10_bio->state))
  1709. reschedule_retry(r10_bio);
  1710. else
  1711. put_buf(r10_bio);
  1712. md_done_sync(mddev, s, 1);
  1713. break;
  1714. } else {
  1715. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1716. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1717. test_bit(R10BIO_WriteError, &r10_bio->state))
  1718. reschedule_retry(r10_bio);
  1719. else
  1720. put_buf(r10_bio);
  1721. r10_bio = r10_bio2;
  1722. }
  1723. }
  1724. }
  1725. static void end_sync_write(struct bio *bio, int error)
  1726. {
  1727. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1728. struct r10bio *r10_bio = bio->bi_private;
  1729. struct mddev *mddev = r10_bio->mddev;
  1730. struct r10conf *conf = mddev->private;
  1731. int d;
  1732. sector_t first_bad;
  1733. int bad_sectors;
  1734. int slot;
  1735. int repl;
  1736. struct md_rdev *rdev = NULL;
  1737. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1738. if (repl)
  1739. rdev = conf->mirrors[d].replacement;
  1740. else
  1741. rdev = conf->mirrors[d].rdev;
  1742. if (!uptodate) {
  1743. if (repl)
  1744. md_error(mddev, rdev);
  1745. else {
  1746. set_bit(WriteErrorSeen, &rdev->flags);
  1747. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1748. set_bit(MD_RECOVERY_NEEDED,
  1749. &rdev->mddev->recovery);
  1750. set_bit(R10BIO_WriteError, &r10_bio->state);
  1751. }
  1752. } else if (is_badblock(rdev,
  1753. r10_bio->devs[slot].addr,
  1754. r10_bio->sectors,
  1755. &first_bad, &bad_sectors))
  1756. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1757. rdev_dec_pending(rdev, mddev);
  1758. end_sync_request(r10_bio);
  1759. }
  1760. /*
  1761. * Note: sync and recover and handled very differently for raid10
  1762. * This code is for resync.
  1763. * For resync, we read through virtual addresses and read all blocks.
  1764. * If there is any error, we schedule a write. The lowest numbered
  1765. * drive is authoritative.
  1766. * However requests come for physical address, so we need to map.
  1767. * For every physical address there are raid_disks/copies virtual addresses,
  1768. * which is always are least one, but is not necessarly an integer.
  1769. * This means that a physical address can span multiple chunks, so we may
  1770. * have to submit multiple io requests for a single sync request.
  1771. */
  1772. /*
  1773. * We check if all blocks are in-sync and only write to blocks that
  1774. * aren't in sync
  1775. */
  1776. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1777. {
  1778. struct r10conf *conf = mddev->private;
  1779. int i, first;
  1780. struct bio *tbio, *fbio;
  1781. int vcnt;
  1782. atomic_set(&r10_bio->remaining, 1);
  1783. /* find the first device with a block */
  1784. for (i=0; i<conf->copies; i++)
  1785. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1786. break;
  1787. if (i == conf->copies)
  1788. goto done;
  1789. first = i;
  1790. fbio = r10_bio->devs[i].bio;
  1791. vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
  1792. /* now find blocks with errors */
  1793. for (i=0 ; i < conf->copies ; i++) {
  1794. int j, d;
  1795. tbio = r10_bio->devs[i].bio;
  1796. if (tbio->bi_end_io != end_sync_read)
  1797. continue;
  1798. if (i == first)
  1799. continue;
  1800. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1801. /* We know that the bi_io_vec layout is the same for
  1802. * both 'first' and 'i', so we just compare them.
  1803. * All vec entries are PAGE_SIZE;
  1804. */
  1805. for (j = 0; j < vcnt; j++)
  1806. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1807. page_address(tbio->bi_io_vec[j].bv_page),
  1808. fbio->bi_io_vec[j].bv_len))
  1809. break;
  1810. if (j == vcnt)
  1811. continue;
  1812. mddev->resync_mismatches += r10_bio->sectors;
  1813. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1814. /* Don't fix anything. */
  1815. continue;
  1816. }
  1817. /* Ok, we need to write this bio, either to correct an
  1818. * inconsistency or to correct an unreadable block.
  1819. * First we need to fixup bv_offset, bv_len and
  1820. * bi_vecs, as the read request might have corrupted these
  1821. */
  1822. tbio->bi_vcnt = vcnt;
  1823. tbio->bi_size = r10_bio->sectors << 9;
  1824. tbio->bi_idx = 0;
  1825. tbio->bi_phys_segments = 0;
  1826. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1827. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1828. tbio->bi_next = NULL;
  1829. tbio->bi_rw = WRITE;
  1830. tbio->bi_private = r10_bio;
  1831. tbio->bi_sector = r10_bio->devs[i].addr;
  1832. for (j=0; j < vcnt ; j++) {
  1833. tbio->bi_io_vec[j].bv_offset = 0;
  1834. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1835. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1836. page_address(fbio->bi_io_vec[j].bv_page),
  1837. PAGE_SIZE);
  1838. }
  1839. tbio->bi_end_io = end_sync_write;
  1840. d = r10_bio->devs[i].devnum;
  1841. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1842. atomic_inc(&r10_bio->remaining);
  1843. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1844. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1845. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1846. generic_make_request(tbio);
  1847. }
  1848. /* Now write out to any replacement devices
  1849. * that are active
  1850. */
  1851. for (i = 0; i < conf->copies; i++) {
  1852. int j, d;
  1853. tbio = r10_bio->devs[i].repl_bio;
  1854. if (!tbio || !tbio->bi_end_io)
  1855. continue;
  1856. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1857. && r10_bio->devs[i].bio != fbio)
  1858. for (j = 0; j < vcnt; j++)
  1859. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1860. page_address(fbio->bi_io_vec[j].bv_page),
  1861. PAGE_SIZE);
  1862. d = r10_bio->devs[i].devnum;
  1863. atomic_inc(&r10_bio->remaining);
  1864. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1865. tbio->bi_size >> 9);
  1866. generic_make_request(tbio);
  1867. }
  1868. done:
  1869. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1870. md_done_sync(mddev, r10_bio->sectors, 1);
  1871. put_buf(r10_bio);
  1872. }
  1873. }
  1874. /*
  1875. * Now for the recovery code.
  1876. * Recovery happens across physical sectors.
  1877. * We recover all non-is_sync drives by finding the virtual address of
  1878. * each, and then choose a working drive that also has that virt address.
  1879. * There is a separate r10_bio for each non-in_sync drive.
  1880. * Only the first two slots are in use. The first for reading,
  1881. * The second for writing.
  1882. *
  1883. */
  1884. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1885. {
  1886. /* We got a read error during recovery.
  1887. * We repeat the read in smaller page-sized sections.
  1888. * If a read succeeds, write it to the new device or record
  1889. * a bad block if we cannot.
  1890. * If a read fails, record a bad block on both old and
  1891. * new devices.
  1892. */
  1893. struct mddev *mddev = r10_bio->mddev;
  1894. struct r10conf *conf = mddev->private;
  1895. struct bio *bio = r10_bio->devs[0].bio;
  1896. sector_t sect = 0;
  1897. int sectors = r10_bio->sectors;
  1898. int idx = 0;
  1899. int dr = r10_bio->devs[0].devnum;
  1900. int dw = r10_bio->devs[1].devnum;
  1901. while (sectors) {
  1902. int s = sectors;
  1903. struct md_rdev *rdev;
  1904. sector_t addr;
  1905. int ok;
  1906. if (s > (PAGE_SIZE>>9))
  1907. s = PAGE_SIZE >> 9;
  1908. rdev = conf->mirrors[dr].rdev;
  1909. addr = r10_bio->devs[0].addr + sect,
  1910. ok = sync_page_io(rdev,
  1911. addr,
  1912. s << 9,
  1913. bio->bi_io_vec[idx].bv_page,
  1914. READ, false);
  1915. if (ok) {
  1916. rdev = conf->mirrors[dw].rdev;
  1917. addr = r10_bio->devs[1].addr + sect;
  1918. ok = sync_page_io(rdev,
  1919. addr,
  1920. s << 9,
  1921. bio->bi_io_vec[idx].bv_page,
  1922. WRITE, false);
  1923. if (!ok) {
  1924. set_bit(WriteErrorSeen, &rdev->flags);
  1925. if (!test_and_set_bit(WantReplacement,
  1926. &rdev->flags))
  1927. set_bit(MD_RECOVERY_NEEDED,
  1928. &rdev->mddev->recovery);
  1929. }
  1930. }
  1931. if (!ok) {
  1932. /* We don't worry if we cannot set a bad block -
  1933. * it really is bad so there is no loss in not
  1934. * recording it yet
  1935. */
  1936. rdev_set_badblocks(rdev, addr, s, 0);
  1937. if (rdev != conf->mirrors[dw].rdev) {
  1938. /* need bad block on destination too */
  1939. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  1940. addr = r10_bio->devs[1].addr + sect;
  1941. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  1942. if (!ok) {
  1943. /* just abort the recovery */
  1944. printk(KERN_NOTICE
  1945. "md/raid10:%s: recovery aborted"
  1946. " due to read error\n",
  1947. mdname(mddev));
  1948. conf->mirrors[dw].recovery_disabled
  1949. = mddev->recovery_disabled;
  1950. set_bit(MD_RECOVERY_INTR,
  1951. &mddev->recovery);
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. sectors -= s;
  1957. sect += s;
  1958. idx++;
  1959. }
  1960. }
  1961. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1962. {
  1963. struct r10conf *conf = mddev->private;
  1964. int d;
  1965. struct bio *wbio, *wbio2;
  1966. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  1967. fix_recovery_read_error(r10_bio);
  1968. end_sync_request(r10_bio);
  1969. return;
  1970. }
  1971. /*
  1972. * share the pages with the first bio
  1973. * and submit the write request
  1974. */
  1975. d = r10_bio->devs[1].devnum;
  1976. wbio = r10_bio->devs[1].bio;
  1977. wbio2 = r10_bio->devs[1].repl_bio;
  1978. if (wbio->bi_end_io) {
  1979. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1980. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1981. generic_make_request(wbio);
  1982. }
  1983. if (wbio2 && wbio2->bi_end_io) {
  1984. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  1985. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1986. wbio2->bi_size >> 9);
  1987. generic_make_request(wbio2);
  1988. }
  1989. }
  1990. /*
  1991. * Used by fix_read_error() to decay the per rdev read_errors.
  1992. * We halve the read error count for every hour that has elapsed
  1993. * since the last recorded read error.
  1994. *
  1995. */
  1996. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  1997. {
  1998. struct timespec cur_time_mon;
  1999. unsigned long hours_since_last;
  2000. unsigned int read_errors = atomic_read(&rdev->read_errors);
  2001. ktime_get_ts(&cur_time_mon);
  2002. if (rdev->last_read_error.tv_sec == 0 &&
  2003. rdev->last_read_error.tv_nsec == 0) {
  2004. /* first time we've seen a read error */
  2005. rdev->last_read_error = cur_time_mon;
  2006. return;
  2007. }
  2008. hours_since_last = (cur_time_mon.tv_sec -
  2009. rdev->last_read_error.tv_sec) / 3600;
  2010. rdev->last_read_error = cur_time_mon;
  2011. /*
  2012. * if hours_since_last is > the number of bits in read_errors
  2013. * just set read errors to 0. We do this to avoid
  2014. * overflowing the shift of read_errors by hours_since_last.
  2015. */
  2016. if (hours_since_last >= 8 * sizeof(read_errors))
  2017. atomic_set(&rdev->read_errors, 0);
  2018. else
  2019. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  2020. }
  2021. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  2022. int sectors, struct page *page, int rw)
  2023. {
  2024. sector_t first_bad;
  2025. int bad_sectors;
  2026. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  2027. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  2028. return -1;
  2029. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  2030. /* success */
  2031. return 1;
  2032. if (rw == WRITE) {
  2033. set_bit(WriteErrorSeen, &rdev->flags);
  2034. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  2035. set_bit(MD_RECOVERY_NEEDED,
  2036. &rdev->mddev->recovery);
  2037. }
  2038. /* need to record an error - either for the block or the device */
  2039. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  2040. md_error(rdev->mddev, rdev);
  2041. return 0;
  2042. }
  2043. /*
  2044. * This is a kernel thread which:
  2045. *
  2046. * 1. Retries failed read operations on working mirrors.
  2047. * 2. Updates the raid superblock when problems encounter.
  2048. * 3. Performs writes following reads for array synchronising.
  2049. */
  2050. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  2051. {
  2052. int sect = 0; /* Offset from r10_bio->sector */
  2053. int sectors = r10_bio->sectors;
  2054. struct md_rdev*rdev;
  2055. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  2056. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  2057. /* still own a reference to this rdev, so it cannot
  2058. * have been cleared recently.
  2059. */
  2060. rdev = conf->mirrors[d].rdev;
  2061. if (test_bit(Faulty, &rdev->flags))
  2062. /* drive has already been failed, just ignore any
  2063. more fix_read_error() attempts */
  2064. return;
  2065. check_decay_read_errors(mddev, rdev);
  2066. atomic_inc(&rdev->read_errors);
  2067. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  2068. char b[BDEVNAME_SIZE];
  2069. bdevname(rdev->bdev, b);
  2070. printk(KERN_NOTICE
  2071. "md/raid10:%s: %s: Raid device exceeded "
  2072. "read_error threshold [cur %d:max %d]\n",
  2073. mdname(mddev), b,
  2074. atomic_read(&rdev->read_errors), max_read_errors);
  2075. printk(KERN_NOTICE
  2076. "md/raid10:%s: %s: Failing raid device\n",
  2077. mdname(mddev), b);
  2078. md_error(mddev, conf->mirrors[d].rdev);
  2079. r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
  2080. return;
  2081. }
  2082. while(sectors) {
  2083. int s = sectors;
  2084. int sl = r10_bio->read_slot;
  2085. int success = 0;
  2086. int start;
  2087. if (s > (PAGE_SIZE>>9))
  2088. s = PAGE_SIZE >> 9;
  2089. rcu_read_lock();
  2090. do {
  2091. sector_t first_bad;
  2092. int bad_sectors;
  2093. d = r10_bio->devs[sl].devnum;
  2094. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2095. if (rdev &&
  2096. !test_bit(Unmerged, &rdev->flags) &&
  2097. test_bit(In_sync, &rdev->flags) &&
  2098. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  2099. &first_bad, &bad_sectors) == 0) {
  2100. atomic_inc(&rdev->nr_pending);
  2101. rcu_read_unlock();
  2102. success = sync_page_io(rdev,
  2103. r10_bio->devs[sl].addr +
  2104. sect,
  2105. s<<9,
  2106. conf->tmppage, READ, false);
  2107. rdev_dec_pending(rdev, mddev);
  2108. rcu_read_lock();
  2109. if (success)
  2110. break;
  2111. }
  2112. sl++;
  2113. if (sl == conf->copies)
  2114. sl = 0;
  2115. } while (!success && sl != r10_bio->read_slot);
  2116. rcu_read_unlock();
  2117. if (!success) {
  2118. /* Cannot read from anywhere, just mark the block
  2119. * as bad on the first device to discourage future
  2120. * reads.
  2121. */
  2122. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  2123. rdev = conf->mirrors[dn].rdev;
  2124. if (!rdev_set_badblocks(
  2125. rdev,
  2126. r10_bio->devs[r10_bio->read_slot].addr
  2127. + sect,
  2128. s, 0)) {
  2129. md_error(mddev, rdev);
  2130. r10_bio->devs[r10_bio->read_slot].bio
  2131. = IO_BLOCKED;
  2132. }
  2133. break;
  2134. }
  2135. start = sl;
  2136. /* write it back and re-read */
  2137. rcu_read_lock();
  2138. while (sl != r10_bio->read_slot) {
  2139. char b[BDEVNAME_SIZE];
  2140. if (sl==0)
  2141. sl = conf->copies;
  2142. sl--;
  2143. d = r10_bio->devs[sl].devnum;
  2144. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2145. if (!rdev ||
  2146. test_bit(Unmerged, &rdev->flags) ||
  2147. !test_bit(In_sync, &rdev->flags))
  2148. continue;
  2149. atomic_inc(&rdev->nr_pending);
  2150. rcu_read_unlock();
  2151. if (r10_sync_page_io(rdev,
  2152. r10_bio->devs[sl].addr +
  2153. sect,
  2154. s, conf->tmppage, WRITE)
  2155. == 0) {
  2156. /* Well, this device is dead */
  2157. printk(KERN_NOTICE
  2158. "md/raid10:%s: read correction "
  2159. "write failed"
  2160. " (%d sectors at %llu on %s)\n",
  2161. mdname(mddev), s,
  2162. (unsigned long long)(
  2163. sect +
  2164. choose_data_offset(r10_bio,
  2165. rdev)),
  2166. bdevname(rdev->bdev, b));
  2167. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2168. "drive\n",
  2169. mdname(mddev),
  2170. bdevname(rdev->bdev, b));
  2171. }
  2172. rdev_dec_pending(rdev, mddev);
  2173. rcu_read_lock();
  2174. }
  2175. sl = start;
  2176. while (sl != r10_bio->read_slot) {
  2177. char b[BDEVNAME_SIZE];
  2178. if (sl==0)
  2179. sl = conf->copies;
  2180. sl--;
  2181. d = r10_bio->devs[sl].devnum;
  2182. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2183. if (!rdev ||
  2184. !test_bit(In_sync, &rdev->flags))
  2185. continue;
  2186. atomic_inc(&rdev->nr_pending);
  2187. rcu_read_unlock();
  2188. switch (r10_sync_page_io(rdev,
  2189. r10_bio->devs[sl].addr +
  2190. sect,
  2191. s, conf->tmppage,
  2192. READ)) {
  2193. case 0:
  2194. /* Well, this device is dead */
  2195. printk(KERN_NOTICE
  2196. "md/raid10:%s: unable to read back "
  2197. "corrected sectors"
  2198. " (%d sectors at %llu on %s)\n",
  2199. mdname(mddev), s,
  2200. (unsigned long long)(
  2201. sect +
  2202. choose_data_offset(r10_bio, rdev)),
  2203. bdevname(rdev->bdev, b));
  2204. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2205. "drive\n",
  2206. mdname(mddev),
  2207. bdevname(rdev->bdev, b));
  2208. break;
  2209. case 1:
  2210. printk(KERN_INFO
  2211. "md/raid10:%s: read error corrected"
  2212. " (%d sectors at %llu on %s)\n",
  2213. mdname(mddev), s,
  2214. (unsigned long long)(
  2215. sect +
  2216. choose_data_offset(r10_bio, rdev)),
  2217. bdevname(rdev->bdev, b));
  2218. atomic_add(s, &rdev->corrected_errors);
  2219. }
  2220. rdev_dec_pending(rdev, mddev);
  2221. rcu_read_lock();
  2222. }
  2223. rcu_read_unlock();
  2224. sectors -= s;
  2225. sect += s;
  2226. }
  2227. }
  2228. static void bi_complete(struct bio *bio, int error)
  2229. {
  2230. complete((struct completion *)bio->bi_private);
  2231. }
  2232. static int submit_bio_wait(int rw, struct bio *bio)
  2233. {
  2234. struct completion event;
  2235. rw |= REQ_SYNC;
  2236. init_completion(&event);
  2237. bio->bi_private = &event;
  2238. bio->bi_end_io = bi_complete;
  2239. submit_bio(rw, bio);
  2240. wait_for_completion(&event);
  2241. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  2242. }
  2243. static int narrow_write_error(struct r10bio *r10_bio, int i)
  2244. {
  2245. struct bio *bio = r10_bio->master_bio;
  2246. struct mddev *mddev = r10_bio->mddev;
  2247. struct r10conf *conf = mddev->private;
  2248. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  2249. /* bio has the data to be written to slot 'i' where
  2250. * we just recently had a write error.
  2251. * We repeatedly clone the bio and trim down to one block,
  2252. * then try the write. Where the write fails we record
  2253. * a bad block.
  2254. * It is conceivable that the bio doesn't exactly align with
  2255. * blocks. We must handle this.
  2256. *
  2257. * We currently own a reference to the rdev.
  2258. */
  2259. int block_sectors;
  2260. sector_t sector;
  2261. int sectors;
  2262. int sect_to_write = r10_bio->sectors;
  2263. int ok = 1;
  2264. if (rdev->badblocks.shift < 0)
  2265. return 0;
  2266. block_sectors = 1 << rdev->badblocks.shift;
  2267. sector = r10_bio->sector;
  2268. sectors = ((r10_bio->sector + block_sectors)
  2269. & ~(sector_t)(block_sectors - 1))
  2270. - sector;
  2271. while (sect_to_write) {
  2272. struct bio *wbio;
  2273. if (sectors > sect_to_write)
  2274. sectors = sect_to_write;
  2275. /* Write at 'sector' for 'sectors' */
  2276. wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  2277. md_trim_bio(wbio, sector - bio->bi_sector, sectors);
  2278. wbio->bi_sector = (r10_bio->devs[i].addr+
  2279. choose_data_offset(r10_bio, rdev) +
  2280. (sector - r10_bio->sector));
  2281. wbio->bi_bdev = rdev->bdev;
  2282. if (submit_bio_wait(WRITE, wbio) == 0)
  2283. /* Failure! */
  2284. ok = rdev_set_badblocks(rdev, sector,
  2285. sectors, 0)
  2286. && ok;
  2287. bio_put(wbio);
  2288. sect_to_write -= sectors;
  2289. sector += sectors;
  2290. sectors = block_sectors;
  2291. }
  2292. return ok;
  2293. }
  2294. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2295. {
  2296. int slot = r10_bio->read_slot;
  2297. struct bio *bio;
  2298. struct r10conf *conf = mddev->private;
  2299. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2300. char b[BDEVNAME_SIZE];
  2301. unsigned long do_sync;
  2302. int max_sectors;
  2303. /* we got a read error. Maybe the drive is bad. Maybe just
  2304. * the block and we can fix it.
  2305. * We freeze all other IO, and try reading the block from
  2306. * other devices. When we find one, we re-write
  2307. * and check it that fixes the read error.
  2308. * This is all done synchronously while the array is
  2309. * frozen.
  2310. */
  2311. bio = r10_bio->devs[slot].bio;
  2312. bdevname(bio->bi_bdev, b);
  2313. bio_put(bio);
  2314. r10_bio->devs[slot].bio = NULL;
  2315. if (mddev->ro == 0) {
  2316. freeze_array(conf);
  2317. fix_read_error(conf, mddev, r10_bio);
  2318. unfreeze_array(conf);
  2319. } else
  2320. r10_bio->devs[slot].bio = IO_BLOCKED;
  2321. rdev_dec_pending(rdev, mddev);
  2322. read_more:
  2323. rdev = read_balance(conf, r10_bio, &max_sectors);
  2324. if (rdev == NULL) {
  2325. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  2326. " read error for block %llu\n",
  2327. mdname(mddev), b,
  2328. (unsigned long long)r10_bio->sector);
  2329. raid_end_bio_io(r10_bio);
  2330. return;
  2331. }
  2332. do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  2333. slot = r10_bio->read_slot;
  2334. printk_ratelimited(
  2335. KERN_ERR
  2336. "md/raid10:%s: %s: redirecting "
  2337. "sector %llu to another mirror\n",
  2338. mdname(mddev),
  2339. bdevname(rdev->bdev, b),
  2340. (unsigned long long)r10_bio->sector);
  2341. bio = bio_clone_mddev(r10_bio->master_bio,
  2342. GFP_NOIO, mddev);
  2343. md_trim_bio(bio,
  2344. r10_bio->sector - bio->bi_sector,
  2345. max_sectors);
  2346. r10_bio->devs[slot].bio = bio;
  2347. r10_bio->devs[slot].rdev = rdev;
  2348. bio->bi_sector = r10_bio->devs[slot].addr
  2349. + choose_data_offset(r10_bio, rdev);
  2350. bio->bi_bdev = rdev->bdev;
  2351. bio->bi_rw = READ | do_sync;
  2352. bio->bi_private = r10_bio;
  2353. bio->bi_end_io = raid10_end_read_request;
  2354. if (max_sectors < r10_bio->sectors) {
  2355. /* Drat - have to split this up more */
  2356. struct bio *mbio = r10_bio->master_bio;
  2357. int sectors_handled =
  2358. r10_bio->sector + max_sectors
  2359. - mbio->bi_sector;
  2360. r10_bio->sectors = max_sectors;
  2361. spin_lock_irq(&conf->device_lock);
  2362. if (mbio->bi_phys_segments == 0)
  2363. mbio->bi_phys_segments = 2;
  2364. else
  2365. mbio->bi_phys_segments++;
  2366. spin_unlock_irq(&conf->device_lock);
  2367. generic_make_request(bio);
  2368. r10_bio = mempool_alloc(conf->r10bio_pool,
  2369. GFP_NOIO);
  2370. r10_bio->master_bio = mbio;
  2371. r10_bio->sectors = (mbio->bi_size >> 9)
  2372. - sectors_handled;
  2373. r10_bio->state = 0;
  2374. set_bit(R10BIO_ReadError,
  2375. &r10_bio->state);
  2376. r10_bio->mddev = mddev;
  2377. r10_bio->sector = mbio->bi_sector
  2378. + sectors_handled;
  2379. goto read_more;
  2380. } else
  2381. generic_make_request(bio);
  2382. }
  2383. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2384. {
  2385. /* Some sort of write request has finished and it
  2386. * succeeded in writing where we thought there was a
  2387. * bad block. So forget the bad block.
  2388. * Or possibly if failed and we need to record
  2389. * a bad block.
  2390. */
  2391. int m;
  2392. struct md_rdev *rdev;
  2393. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2394. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2395. for (m = 0; m < conf->copies; m++) {
  2396. int dev = r10_bio->devs[m].devnum;
  2397. rdev = conf->mirrors[dev].rdev;
  2398. if (r10_bio->devs[m].bio == NULL)
  2399. continue;
  2400. if (test_bit(BIO_UPTODATE,
  2401. &r10_bio->devs[m].bio->bi_flags)) {
  2402. rdev_clear_badblocks(
  2403. rdev,
  2404. r10_bio->devs[m].addr,
  2405. r10_bio->sectors, 0);
  2406. } else {
  2407. if (!rdev_set_badblocks(
  2408. rdev,
  2409. r10_bio->devs[m].addr,
  2410. r10_bio->sectors, 0))
  2411. md_error(conf->mddev, rdev);
  2412. }
  2413. rdev = conf->mirrors[dev].replacement;
  2414. if (r10_bio->devs[m].repl_bio == NULL)
  2415. continue;
  2416. if (test_bit(BIO_UPTODATE,
  2417. &r10_bio->devs[m].repl_bio->bi_flags)) {
  2418. rdev_clear_badblocks(
  2419. rdev,
  2420. r10_bio->devs[m].addr,
  2421. r10_bio->sectors, 0);
  2422. } else {
  2423. if (!rdev_set_badblocks(
  2424. rdev,
  2425. r10_bio->devs[m].addr,
  2426. r10_bio->sectors, 0))
  2427. md_error(conf->mddev, rdev);
  2428. }
  2429. }
  2430. put_buf(r10_bio);
  2431. } else {
  2432. for (m = 0; m < conf->copies; m++) {
  2433. int dev = r10_bio->devs[m].devnum;
  2434. struct bio *bio = r10_bio->devs[m].bio;
  2435. rdev = conf->mirrors[dev].rdev;
  2436. if (bio == IO_MADE_GOOD) {
  2437. rdev_clear_badblocks(
  2438. rdev,
  2439. r10_bio->devs[m].addr,
  2440. r10_bio->sectors, 0);
  2441. rdev_dec_pending(rdev, conf->mddev);
  2442. } else if (bio != NULL &&
  2443. !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  2444. if (!narrow_write_error(r10_bio, m)) {
  2445. md_error(conf->mddev, rdev);
  2446. set_bit(R10BIO_Degraded,
  2447. &r10_bio->state);
  2448. }
  2449. rdev_dec_pending(rdev, conf->mddev);
  2450. }
  2451. bio = r10_bio->devs[m].repl_bio;
  2452. rdev = conf->mirrors[dev].replacement;
  2453. if (rdev && bio == IO_MADE_GOOD) {
  2454. rdev_clear_badblocks(
  2455. rdev,
  2456. r10_bio->devs[m].addr,
  2457. r10_bio->sectors, 0);
  2458. rdev_dec_pending(rdev, conf->mddev);
  2459. }
  2460. }
  2461. if (test_bit(R10BIO_WriteError,
  2462. &r10_bio->state))
  2463. close_write(r10_bio);
  2464. raid_end_bio_io(r10_bio);
  2465. }
  2466. }
  2467. static void raid10d(struct mddev *mddev)
  2468. {
  2469. struct r10bio *r10_bio;
  2470. unsigned long flags;
  2471. struct r10conf *conf = mddev->private;
  2472. struct list_head *head = &conf->retry_list;
  2473. struct blk_plug plug;
  2474. md_check_recovery(mddev);
  2475. blk_start_plug(&plug);
  2476. for (;;) {
  2477. flush_pending_writes(conf);
  2478. spin_lock_irqsave(&conf->device_lock, flags);
  2479. if (list_empty(head)) {
  2480. spin_unlock_irqrestore(&conf->device_lock, flags);
  2481. break;
  2482. }
  2483. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2484. list_del(head->prev);
  2485. conf->nr_queued--;
  2486. spin_unlock_irqrestore(&conf->device_lock, flags);
  2487. mddev = r10_bio->mddev;
  2488. conf = mddev->private;
  2489. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2490. test_bit(R10BIO_WriteError, &r10_bio->state))
  2491. handle_write_completed(conf, r10_bio);
  2492. else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
  2493. reshape_request_write(mddev, r10_bio);
  2494. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2495. sync_request_write(mddev, r10_bio);
  2496. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2497. recovery_request_write(mddev, r10_bio);
  2498. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2499. handle_read_error(mddev, r10_bio);
  2500. else {
  2501. /* just a partial read to be scheduled from a
  2502. * separate context
  2503. */
  2504. int slot = r10_bio->read_slot;
  2505. generic_make_request(r10_bio->devs[slot].bio);
  2506. }
  2507. cond_resched();
  2508. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2509. md_check_recovery(mddev);
  2510. }
  2511. blk_finish_plug(&plug);
  2512. }
  2513. static int init_resync(struct r10conf *conf)
  2514. {
  2515. int buffs;
  2516. int i;
  2517. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2518. BUG_ON(conf->r10buf_pool);
  2519. conf->have_replacement = 0;
  2520. for (i = 0; i < conf->geo.raid_disks; i++)
  2521. if (conf->mirrors[i].replacement)
  2522. conf->have_replacement = 1;
  2523. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  2524. if (!conf->r10buf_pool)
  2525. return -ENOMEM;
  2526. conf->next_resync = 0;
  2527. return 0;
  2528. }
  2529. /*
  2530. * perform a "sync" on one "block"
  2531. *
  2532. * We need to make sure that no normal I/O request - particularly write
  2533. * requests - conflict with active sync requests.
  2534. *
  2535. * This is achieved by tracking pending requests and a 'barrier' concept
  2536. * that can be installed to exclude normal IO requests.
  2537. *
  2538. * Resync and recovery are handled very differently.
  2539. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2540. *
  2541. * For resync, we iterate over virtual addresses, read all copies,
  2542. * and update if there are differences. If only one copy is live,
  2543. * skip it.
  2544. * For recovery, we iterate over physical addresses, read a good
  2545. * value for each non-in_sync drive, and over-write.
  2546. *
  2547. * So, for recovery we may have several outstanding complex requests for a
  2548. * given address, one for each out-of-sync device. We model this by allocating
  2549. * a number of r10_bio structures, one for each out-of-sync device.
  2550. * As we setup these structures, we collect all bio's together into a list
  2551. * which we then process collectively to add pages, and then process again
  2552. * to pass to generic_make_request.
  2553. *
  2554. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2555. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2556. * has its remaining count decremented to 0, the whole complex operation
  2557. * is complete.
  2558. *
  2559. */
  2560. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
  2561. int *skipped, int go_faster)
  2562. {
  2563. struct r10conf *conf = mddev->private;
  2564. struct r10bio *r10_bio;
  2565. struct bio *biolist = NULL, *bio;
  2566. sector_t max_sector, nr_sectors;
  2567. int i;
  2568. int max_sync;
  2569. sector_t sync_blocks;
  2570. sector_t sectors_skipped = 0;
  2571. int chunks_skipped = 0;
  2572. sector_t chunk_mask = conf->geo.chunk_mask;
  2573. if (!conf->r10buf_pool)
  2574. if (init_resync(conf))
  2575. return 0;
  2576. skipped:
  2577. max_sector = mddev->dev_sectors;
  2578. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
  2579. test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2580. max_sector = mddev->resync_max_sectors;
  2581. if (sector_nr >= max_sector) {
  2582. /* If we aborted, we need to abort the
  2583. * sync on the 'current' bitmap chucks (there can
  2584. * be several when recovering multiple devices).
  2585. * as we may have started syncing it but not finished.
  2586. * We can find the current address in
  2587. * mddev->curr_resync, but for recovery,
  2588. * we need to convert that to several
  2589. * virtual addresses.
  2590. */
  2591. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2592. end_reshape(conf);
  2593. return 0;
  2594. }
  2595. if (mddev->curr_resync < max_sector) { /* aborted */
  2596. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2597. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2598. &sync_blocks, 1);
  2599. else for (i = 0; i < conf->geo.raid_disks; i++) {
  2600. sector_t sect =
  2601. raid10_find_virt(conf, mddev->curr_resync, i);
  2602. bitmap_end_sync(mddev->bitmap, sect,
  2603. &sync_blocks, 1);
  2604. }
  2605. } else {
  2606. /* completed sync */
  2607. if ((!mddev->bitmap || conf->fullsync)
  2608. && conf->have_replacement
  2609. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2610. /* Completed a full sync so the replacements
  2611. * are now fully recovered.
  2612. */
  2613. for (i = 0; i < conf->geo.raid_disks; i++)
  2614. if (conf->mirrors[i].replacement)
  2615. conf->mirrors[i].replacement
  2616. ->recovery_offset
  2617. = MaxSector;
  2618. }
  2619. conf->fullsync = 0;
  2620. }
  2621. bitmap_close_sync(mddev->bitmap);
  2622. close_sync(conf);
  2623. *skipped = 1;
  2624. return sectors_skipped;
  2625. }
  2626. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2627. return reshape_request(mddev, sector_nr, skipped);
  2628. if (chunks_skipped >= conf->geo.raid_disks) {
  2629. /* if there has been nothing to do on any drive,
  2630. * then there is nothing to do at all..
  2631. */
  2632. *skipped = 1;
  2633. return (max_sector - sector_nr) + sectors_skipped;
  2634. }
  2635. if (max_sector > mddev->resync_max)
  2636. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2637. /* make sure whole request will fit in a chunk - if chunks
  2638. * are meaningful
  2639. */
  2640. if (conf->geo.near_copies < conf->geo.raid_disks &&
  2641. max_sector > (sector_nr | chunk_mask))
  2642. max_sector = (sector_nr | chunk_mask) + 1;
  2643. /*
  2644. * If there is non-resync activity waiting for us then
  2645. * put in a delay to throttle resync.
  2646. */
  2647. if (!go_faster && conf->nr_waiting)
  2648. msleep_interruptible(1000);
  2649. /* Again, very different code for resync and recovery.
  2650. * Both must result in an r10bio with a list of bios that
  2651. * have bi_end_io, bi_sector, bi_bdev set,
  2652. * and bi_private set to the r10bio.
  2653. * For recovery, we may actually create several r10bios
  2654. * with 2 bios in each, that correspond to the bios in the main one.
  2655. * In this case, the subordinate r10bios link back through a
  2656. * borrowed master_bio pointer, and the counter in the master
  2657. * includes a ref from each subordinate.
  2658. */
  2659. /* First, we decide what to do and set ->bi_end_io
  2660. * To end_sync_read if we want to read, and
  2661. * end_sync_write if we will want to write.
  2662. */
  2663. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2664. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2665. /* recovery... the complicated one */
  2666. int j;
  2667. r10_bio = NULL;
  2668. for (i = 0 ; i < conf->geo.raid_disks; i++) {
  2669. int still_degraded;
  2670. struct r10bio *rb2;
  2671. sector_t sect;
  2672. int must_sync;
  2673. int any_working;
  2674. struct raid10_info *mirror = &conf->mirrors[i];
  2675. if ((mirror->rdev == NULL ||
  2676. test_bit(In_sync, &mirror->rdev->flags))
  2677. &&
  2678. (mirror->replacement == NULL ||
  2679. test_bit(Faulty,
  2680. &mirror->replacement->flags)))
  2681. continue;
  2682. still_degraded = 0;
  2683. /* want to reconstruct this device */
  2684. rb2 = r10_bio;
  2685. sect = raid10_find_virt(conf, sector_nr, i);
  2686. if (sect >= mddev->resync_max_sectors) {
  2687. /* last stripe is not complete - don't
  2688. * try to recover this sector.
  2689. */
  2690. continue;
  2691. }
  2692. /* Unless we are doing a full sync, or a replacement
  2693. * we only need to recover the block if it is set in
  2694. * the bitmap
  2695. */
  2696. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2697. &sync_blocks, 1);
  2698. if (sync_blocks < max_sync)
  2699. max_sync = sync_blocks;
  2700. if (!must_sync &&
  2701. mirror->replacement == NULL &&
  2702. !conf->fullsync) {
  2703. /* yep, skip the sync_blocks here, but don't assume
  2704. * that there will never be anything to do here
  2705. */
  2706. chunks_skipped = -1;
  2707. continue;
  2708. }
  2709. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2710. raise_barrier(conf, rb2 != NULL);
  2711. atomic_set(&r10_bio->remaining, 0);
  2712. r10_bio->master_bio = (struct bio*)rb2;
  2713. if (rb2)
  2714. atomic_inc(&rb2->remaining);
  2715. r10_bio->mddev = mddev;
  2716. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2717. r10_bio->sector = sect;
  2718. raid10_find_phys(conf, r10_bio);
  2719. /* Need to check if the array will still be
  2720. * degraded
  2721. */
  2722. for (j = 0; j < conf->geo.raid_disks; j++)
  2723. if (conf->mirrors[j].rdev == NULL ||
  2724. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  2725. still_degraded = 1;
  2726. break;
  2727. }
  2728. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2729. &sync_blocks, still_degraded);
  2730. any_working = 0;
  2731. for (j=0; j<conf->copies;j++) {
  2732. int k;
  2733. int d = r10_bio->devs[j].devnum;
  2734. sector_t from_addr, to_addr;
  2735. struct md_rdev *rdev;
  2736. sector_t sector, first_bad;
  2737. int bad_sectors;
  2738. if (!conf->mirrors[d].rdev ||
  2739. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  2740. continue;
  2741. /* This is where we read from */
  2742. any_working = 1;
  2743. rdev = conf->mirrors[d].rdev;
  2744. sector = r10_bio->devs[j].addr;
  2745. if (is_badblock(rdev, sector, max_sync,
  2746. &first_bad, &bad_sectors)) {
  2747. if (first_bad > sector)
  2748. max_sync = first_bad - sector;
  2749. else {
  2750. bad_sectors -= (sector
  2751. - first_bad);
  2752. if (max_sync > bad_sectors)
  2753. max_sync = bad_sectors;
  2754. continue;
  2755. }
  2756. }
  2757. bio = r10_bio->devs[0].bio;
  2758. bio->bi_next = biolist;
  2759. biolist = bio;
  2760. bio->bi_private = r10_bio;
  2761. bio->bi_end_io = end_sync_read;
  2762. bio->bi_rw = READ;
  2763. from_addr = r10_bio->devs[j].addr;
  2764. bio->bi_sector = from_addr + rdev->data_offset;
  2765. bio->bi_bdev = rdev->bdev;
  2766. atomic_inc(&rdev->nr_pending);
  2767. /* and we write to 'i' (if not in_sync) */
  2768. for (k=0; k<conf->copies; k++)
  2769. if (r10_bio->devs[k].devnum == i)
  2770. break;
  2771. BUG_ON(k == conf->copies);
  2772. to_addr = r10_bio->devs[k].addr;
  2773. r10_bio->devs[0].devnum = d;
  2774. r10_bio->devs[0].addr = from_addr;
  2775. r10_bio->devs[1].devnum = i;
  2776. r10_bio->devs[1].addr = to_addr;
  2777. rdev = mirror->rdev;
  2778. if (!test_bit(In_sync, &rdev->flags)) {
  2779. bio = r10_bio->devs[1].bio;
  2780. bio->bi_next = biolist;
  2781. biolist = bio;
  2782. bio->bi_private = r10_bio;
  2783. bio->bi_end_io = end_sync_write;
  2784. bio->bi_rw = WRITE;
  2785. bio->bi_sector = to_addr
  2786. + rdev->data_offset;
  2787. bio->bi_bdev = rdev->bdev;
  2788. atomic_inc(&r10_bio->remaining);
  2789. } else
  2790. r10_bio->devs[1].bio->bi_end_io = NULL;
  2791. /* and maybe write to replacement */
  2792. bio = r10_bio->devs[1].repl_bio;
  2793. if (bio)
  2794. bio->bi_end_io = NULL;
  2795. rdev = mirror->replacement;
  2796. /* Note: if rdev != NULL, then bio
  2797. * cannot be NULL as r10buf_pool_alloc will
  2798. * have allocated it.
  2799. * So the second test here is pointless.
  2800. * But it keeps semantic-checkers happy, and
  2801. * this comment keeps human reviewers
  2802. * happy.
  2803. */
  2804. if (rdev == NULL || bio == NULL ||
  2805. test_bit(Faulty, &rdev->flags))
  2806. break;
  2807. bio->bi_next = biolist;
  2808. biolist = bio;
  2809. bio->bi_private = r10_bio;
  2810. bio->bi_end_io = end_sync_write;
  2811. bio->bi_rw = WRITE;
  2812. bio->bi_sector = to_addr + rdev->data_offset;
  2813. bio->bi_bdev = rdev->bdev;
  2814. atomic_inc(&r10_bio->remaining);
  2815. break;
  2816. }
  2817. if (j == conf->copies) {
  2818. /* Cannot recover, so abort the recovery or
  2819. * record a bad block */
  2820. put_buf(r10_bio);
  2821. if (rb2)
  2822. atomic_dec(&rb2->remaining);
  2823. r10_bio = rb2;
  2824. if (any_working) {
  2825. /* problem is that there are bad blocks
  2826. * on other device(s)
  2827. */
  2828. int k;
  2829. for (k = 0; k < conf->copies; k++)
  2830. if (r10_bio->devs[k].devnum == i)
  2831. break;
  2832. if (!test_bit(In_sync,
  2833. &mirror->rdev->flags)
  2834. && !rdev_set_badblocks(
  2835. mirror->rdev,
  2836. r10_bio->devs[k].addr,
  2837. max_sync, 0))
  2838. any_working = 0;
  2839. if (mirror->replacement &&
  2840. !rdev_set_badblocks(
  2841. mirror->replacement,
  2842. r10_bio->devs[k].addr,
  2843. max_sync, 0))
  2844. any_working = 0;
  2845. }
  2846. if (!any_working) {
  2847. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2848. &mddev->recovery))
  2849. printk(KERN_INFO "md/raid10:%s: insufficient "
  2850. "working devices for recovery.\n",
  2851. mdname(mddev));
  2852. mirror->recovery_disabled
  2853. = mddev->recovery_disabled;
  2854. }
  2855. break;
  2856. }
  2857. }
  2858. if (biolist == NULL) {
  2859. while (r10_bio) {
  2860. struct r10bio *rb2 = r10_bio;
  2861. r10_bio = (struct r10bio*) rb2->master_bio;
  2862. rb2->master_bio = NULL;
  2863. put_buf(rb2);
  2864. }
  2865. goto giveup;
  2866. }
  2867. } else {
  2868. /* resync. Schedule a read for every block at this virt offset */
  2869. int count = 0;
  2870. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2871. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2872. &sync_blocks, mddev->degraded) &&
  2873. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  2874. &mddev->recovery)) {
  2875. /* We can skip this block */
  2876. *skipped = 1;
  2877. return sync_blocks + sectors_skipped;
  2878. }
  2879. if (sync_blocks < max_sync)
  2880. max_sync = sync_blocks;
  2881. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2882. r10_bio->mddev = mddev;
  2883. atomic_set(&r10_bio->remaining, 0);
  2884. raise_barrier(conf, 0);
  2885. conf->next_resync = sector_nr;
  2886. r10_bio->master_bio = NULL;
  2887. r10_bio->sector = sector_nr;
  2888. set_bit(R10BIO_IsSync, &r10_bio->state);
  2889. raid10_find_phys(conf, r10_bio);
  2890. r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
  2891. for (i = 0; i < conf->copies; i++) {
  2892. int d = r10_bio->devs[i].devnum;
  2893. sector_t first_bad, sector;
  2894. int bad_sectors;
  2895. if (r10_bio->devs[i].repl_bio)
  2896. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  2897. bio = r10_bio->devs[i].bio;
  2898. bio->bi_end_io = NULL;
  2899. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2900. if (conf->mirrors[d].rdev == NULL ||
  2901. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  2902. continue;
  2903. sector = r10_bio->devs[i].addr;
  2904. if (is_badblock(conf->mirrors[d].rdev,
  2905. sector, max_sync,
  2906. &first_bad, &bad_sectors)) {
  2907. if (first_bad > sector)
  2908. max_sync = first_bad - sector;
  2909. else {
  2910. bad_sectors -= (sector - first_bad);
  2911. if (max_sync > bad_sectors)
  2912. max_sync = max_sync;
  2913. continue;
  2914. }
  2915. }
  2916. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2917. atomic_inc(&r10_bio->remaining);
  2918. bio->bi_next = biolist;
  2919. biolist = bio;
  2920. bio->bi_private = r10_bio;
  2921. bio->bi_end_io = end_sync_read;
  2922. bio->bi_rw = READ;
  2923. bio->bi_sector = sector +
  2924. conf->mirrors[d].rdev->data_offset;
  2925. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  2926. count++;
  2927. if (conf->mirrors[d].replacement == NULL ||
  2928. test_bit(Faulty,
  2929. &conf->mirrors[d].replacement->flags))
  2930. continue;
  2931. /* Need to set up for writing to the replacement */
  2932. bio = r10_bio->devs[i].repl_bio;
  2933. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2934. sector = r10_bio->devs[i].addr;
  2935. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2936. bio->bi_next = biolist;
  2937. biolist = bio;
  2938. bio->bi_private = r10_bio;
  2939. bio->bi_end_io = end_sync_write;
  2940. bio->bi_rw = WRITE;
  2941. bio->bi_sector = sector +
  2942. conf->mirrors[d].replacement->data_offset;
  2943. bio->bi_bdev = conf->mirrors[d].replacement->bdev;
  2944. count++;
  2945. }
  2946. if (count < 2) {
  2947. for (i=0; i<conf->copies; i++) {
  2948. int d = r10_bio->devs[i].devnum;
  2949. if (r10_bio->devs[i].bio->bi_end_io)
  2950. rdev_dec_pending(conf->mirrors[d].rdev,
  2951. mddev);
  2952. if (r10_bio->devs[i].repl_bio &&
  2953. r10_bio->devs[i].repl_bio->bi_end_io)
  2954. rdev_dec_pending(
  2955. conf->mirrors[d].replacement,
  2956. mddev);
  2957. }
  2958. put_buf(r10_bio);
  2959. biolist = NULL;
  2960. goto giveup;
  2961. }
  2962. }
  2963. for (bio = biolist; bio ; bio=bio->bi_next) {
  2964. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  2965. if (bio->bi_end_io)
  2966. bio->bi_flags |= 1 << BIO_UPTODATE;
  2967. bio->bi_vcnt = 0;
  2968. bio->bi_idx = 0;
  2969. bio->bi_phys_segments = 0;
  2970. bio->bi_size = 0;
  2971. }
  2972. nr_sectors = 0;
  2973. if (sector_nr + max_sync < max_sector)
  2974. max_sector = sector_nr + max_sync;
  2975. do {
  2976. struct page *page;
  2977. int len = PAGE_SIZE;
  2978. if (sector_nr + (len>>9) > max_sector)
  2979. len = (max_sector - sector_nr) << 9;
  2980. if (len == 0)
  2981. break;
  2982. for (bio= biolist ; bio ; bio=bio->bi_next) {
  2983. struct bio *bio2;
  2984. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2985. if (bio_add_page(bio, page, len, 0))
  2986. continue;
  2987. /* stop here */
  2988. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2989. for (bio2 = biolist;
  2990. bio2 && bio2 != bio;
  2991. bio2 = bio2->bi_next) {
  2992. /* remove last page from this bio */
  2993. bio2->bi_vcnt--;
  2994. bio2->bi_size -= len;
  2995. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  2996. }
  2997. goto bio_full;
  2998. }
  2999. nr_sectors += len>>9;
  3000. sector_nr += len>>9;
  3001. } while (biolist->bi_vcnt < RESYNC_PAGES);
  3002. bio_full:
  3003. r10_bio->sectors = nr_sectors;
  3004. while (biolist) {
  3005. bio = biolist;
  3006. biolist = biolist->bi_next;
  3007. bio->bi_next = NULL;
  3008. r10_bio = bio->bi_private;
  3009. r10_bio->sectors = nr_sectors;
  3010. if (bio->bi_end_io == end_sync_read) {
  3011. md_sync_acct(bio->bi_bdev, nr_sectors);
  3012. generic_make_request(bio);
  3013. }
  3014. }
  3015. if (sectors_skipped)
  3016. /* pretend they weren't skipped, it makes
  3017. * no important difference in this case
  3018. */
  3019. md_done_sync(mddev, sectors_skipped, 1);
  3020. return sectors_skipped + nr_sectors;
  3021. giveup:
  3022. /* There is nowhere to write, so all non-sync
  3023. * drives must be failed or in resync, all drives
  3024. * have a bad block, so try the next chunk...
  3025. */
  3026. if (sector_nr + max_sync < max_sector)
  3027. max_sector = sector_nr + max_sync;
  3028. sectors_skipped += (max_sector - sector_nr);
  3029. chunks_skipped ++;
  3030. sector_nr = max_sector;
  3031. goto skipped;
  3032. }
  3033. static sector_t
  3034. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  3035. {
  3036. sector_t size;
  3037. struct r10conf *conf = mddev->private;
  3038. if (!raid_disks)
  3039. raid_disks = min(conf->geo.raid_disks,
  3040. conf->prev.raid_disks);
  3041. if (!sectors)
  3042. sectors = conf->dev_sectors;
  3043. size = sectors >> conf->geo.chunk_shift;
  3044. sector_div(size, conf->geo.far_copies);
  3045. size = size * raid_disks;
  3046. sector_div(size, conf->geo.near_copies);
  3047. return size << conf->geo.chunk_shift;
  3048. }
  3049. static void calc_sectors(struct r10conf *conf, sector_t size)
  3050. {
  3051. /* Calculate the number of sectors-per-device that will
  3052. * actually be used, and set conf->dev_sectors and
  3053. * conf->stride
  3054. */
  3055. size = size >> conf->geo.chunk_shift;
  3056. sector_div(size, conf->geo.far_copies);
  3057. size = size * conf->geo.raid_disks;
  3058. sector_div(size, conf->geo.near_copies);
  3059. /* 'size' is now the number of chunks in the array */
  3060. /* calculate "used chunks per device" */
  3061. size = size * conf->copies;
  3062. /* We need to round up when dividing by raid_disks to
  3063. * get the stride size.
  3064. */
  3065. size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
  3066. conf->dev_sectors = size << conf->geo.chunk_shift;
  3067. if (conf->geo.far_offset)
  3068. conf->geo.stride = 1 << conf->geo.chunk_shift;
  3069. else {
  3070. sector_div(size, conf->geo.far_copies);
  3071. conf->geo.stride = size << conf->geo.chunk_shift;
  3072. }
  3073. }
  3074. enum geo_type {geo_new, geo_old, geo_start};
  3075. static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
  3076. {
  3077. int nc, fc, fo;
  3078. int layout, chunk, disks;
  3079. switch (new) {
  3080. case geo_old:
  3081. layout = mddev->layout;
  3082. chunk = mddev->chunk_sectors;
  3083. disks = mddev->raid_disks - mddev->delta_disks;
  3084. break;
  3085. case geo_new:
  3086. layout = mddev->new_layout;
  3087. chunk = mddev->new_chunk_sectors;
  3088. disks = mddev->raid_disks;
  3089. break;
  3090. default: /* avoid 'may be unused' warnings */
  3091. case geo_start: /* new when starting reshape - raid_disks not
  3092. * updated yet. */
  3093. layout = mddev->new_layout;
  3094. chunk = mddev->new_chunk_sectors;
  3095. disks = mddev->raid_disks + mddev->delta_disks;
  3096. break;
  3097. }
  3098. if (layout >> 17)
  3099. return -1;
  3100. if (chunk < (PAGE_SIZE >> 9) ||
  3101. !is_power_of_2(chunk))
  3102. return -2;
  3103. nc = layout & 255;
  3104. fc = (layout >> 8) & 255;
  3105. fo = layout & (1<<16);
  3106. geo->raid_disks = disks;
  3107. geo->near_copies = nc;
  3108. geo->far_copies = fc;
  3109. geo->far_offset = fo;
  3110. geo->chunk_mask = chunk - 1;
  3111. geo->chunk_shift = ffz(~chunk);
  3112. return nc*fc;
  3113. }
  3114. static struct r10conf *setup_conf(struct mddev *mddev)
  3115. {
  3116. struct r10conf *conf = NULL;
  3117. int err = -EINVAL;
  3118. struct geom geo;
  3119. int copies;
  3120. copies = setup_geo(&geo, mddev, geo_new);
  3121. if (copies == -2) {
  3122. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  3123. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  3124. mdname(mddev), PAGE_SIZE);
  3125. goto out;
  3126. }
  3127. if (copies < 2 || copies > mddev->raid_disks) {
  3128. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  3129. mdname(mddev), mddev->new_layout);
  3130. goto out;
  3131. }
  3132. err = -ENOMEM;
  3133. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  3134. if (!conf)
  3135. goto out;
  3136. /* FIXME calc properly */
  3137. conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
  3138. max(0,mddev->delta_disks)),
  3139. GFP_KERNEL);
  3140. if (!conf->mirrors)
  3141. goto out;
  3142. conf->tmppage = alloc_page(GFP_KERNEL);
  3143. if (!conf->tmppage)
  3144. goto out;
  3145. conf->geo = geo;
  3146. conf->copies = copies;
  3147. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  3148. r10bio_pool_free, conf);
  3149. if (!conf->r10bio_pool)
  3150. goto out;
  3151. calc_sectors(conf, mddev->dev_sectors);
  3152. if (mddev->reshape_position == MaxSector) {
  3153. conf->prev = conf->geo;
  3154. conf->reshape_progress = MaxSector;
  3155. } else {
  3156. if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
  3157. err = -EINVAL;
  3158. goto out;
  3159. }
  3160. conf->reshape_progress = mddev->reshape_position;
  3161. if (conf->prev.far_offset)
  3162. conf->prev.stride = 1 << conf->prev.chunk_shift;
  3163. else
  3164. /* far_copies must be 1 */
  3165. conf->prev.stride = conf->dev_sectors;
  3166. }
  3167. spin_lock_init(&conf->device_lock);
  3168. INIT_LIST_HEAD(&conf->retry_list);
  3169. spin_lock_init(&conf->resync_lock);
  3170. init_waitqueue_head(&conf->wait_barrier);
  3171. conf->thread = md_register_thread(raid10d, mddev, "raid10");
  3172. if (!conf->thread)
  3173. goto out;
  3174. conf->mddev = mddev;
  3175. return conf;
  3176. out:
  3177. if (err == -ENOMEM)
  3178. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  3179. mdname(mddev));
  3180. if (conf) {
  3181. if (conf->r10bio_pool)
  3182. mempool_destroy(conf->r10bio_pool);
  3183. kfree(conf->mirrors);
  3184. safe_put_page(conf->tmppage);
  3185. kfree(conf);
  3186. }
  3187. return ERR_PTR(err);
  3188. }
  3189. static int run(struct mddev *mddev)
  3190. {
  3191. struct r10conf *conf;
  3192. int i, disk_idx, chunk_size;
  3193. struct raid10_info *disk;
  3194. struct md_rdev *rdev;
  3195. sector_t size;
  3196. sector_t min_offset_diff = 0;
  3197. int first = 1;
  3198. bool discard_supported = false;
  3199. if (mddev->private == NULL) {
  3200. conf = setup_conf(mddev);
  3201. if (IS_ERR(conf))
  3202. return PTR_ERR(conf);
  3203. mddev->private = conf;
  3204. }
  3205. conf = mddev->private;
  3206. if (!conf)
  3207. goto out;
  3208. mddev->thread = conf->thread;
  3209. conf->thread = NULL;
  3210. chunk_size = mddev->chunk_sectors << 9;
  3211. if (mddev->queue) {
  3212. blk_queue_max_discard_sectors(mddev->queue,
  3213. mddev->chunk_sectors);
  3214. blk_queue_io_min(mddev->queue, chunk_size);
  3215. if (conf->geo.raid_disks % conf->geo.near_copies)
  3216. blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
  3217. else
  3218. blk_queue_io_opt(mddev->queue, chunk_size *
  3219. (conf->geo.raid_disks / conf->geo.near_copies));
  3220. }
  3221. rdev_for_each(rdev, mddev) {
  3222. long long diff;
  3223. struct request_queue *q;
  3224. disk_idx = rdev->raid_disk;
  3225. if (disk_idx < 0)
  3226. continue;
  3227. if (disk_idx >= conf->geo.raid_disks &&
  3228. disk_idx >= conf->prev.raid_disks)
  3229. continue;
  3230. disk = conf->mirrors + disk_idx;
  3231. if (test_bit(Replacement, &rdev->flags)) {
  3232. if (disk->replacement)
  3233. goto out_free_conf;
  3234. disk->replacement = rdev;
  3235. } else {
  3236. if (disk->rdev)
  3237. goto out_free_conf;
  3238. disk->rdev = rdev;
  3239. }
  3240. q = bdev_get_queue(rdev->bdev);
  3241. if (q->merge_bvec_fn)
  3242. mddev->merge_check_needed = 1;
  3243. diff = (rdev->new_data_offset - rdev->data_offset);
  3244. if (!mddev->reshape_backwards)
  3245. diff = -diff;
  3246. if (diff < 0)
  3247. diff = 0;
  3248. if (first || diff < min_offset_diff)
  3249. min_offset_diff = diff;
  3250. if (mddev->gendisk)
  3251. disk_stack_limits(mddev->gendisk, rdev->bdev,
  3252. rdev->data_offset << 9);
  3253. disk->head_position = 0;
  3254. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  3255. discard_supported = true;
  3256. }
  3257. if (discard_supported)
  3258. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  3259. else
  3260. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  3261. /* need to check that every block has at least one working mirror */
  3262. if (!enough(conf, -1)) {
  3263. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  3264. mdname(mddev));
  3265. goto out_free_conf;
  3266. }
  3267. if (conf->reshape_progress != MaxSector) {
  3268. /* must ensure that shape change is supported */
  3269. if (conf->geo.far_copies != 1 &&
  3270. conf->geo.far_offset == 0)
  3271. goto out_free_conf;
  3272. if (conf->prev.far_copies != 1 &&
  3273. conf->geo.far_offset == 0)
  3274. goto out_free_conf;
  3275. }
  3276. mddev->degraded = 0;
  3277. for (i = 0;
  3278. i < conf->geo.raid_disks
  3279. || i < conf->prev.raid_disks;
  3280. i++) {
  3281. disk = conf->mirrors + i;
  3282. if (!disk->rdev && disk->replacement) {
  3283. /* The replacement is all we have - use it */
  3284. disk->rdev = disk->replacement;
  3285. disk->replacement = NULL;
  3286. clear_bit(Replacement, &disk->rdev->flags);
  3287. }
  3288. if (!disk->rdev ||
  3289. !test_bit(In_sync, &disk->rdev->flags)) {
  3290. disk->head_position = 0;
  3291. mddev->degraded++;
  3292. if (disk->rdev)
  3293. conf->fullsync = 1;
  3294. }
  3295. disk->recovery_disabled = mddev->recovery_disabled - 1;
  3296. }
  3297. if (mddev->recovery_cp != MaxSector)
  3298. printk(KERN_NOTICE "md/raid10:%s: not clean"
  3299. " -- starting background reconstruction\n",
  3300. mdname(mddev));
  3301. printk(KERN_INFO
  3302. "md/raid10:%s: active with %d out of %d devices\n",
  3303. mdname(mddev), conf->geo.raid_disks - mddev->degraded,
  3304. conf->geo.raid_disks);
  3305. /*
  3306. * Ok, everything is just fine now
  3307. */
  3308. mddev->dev_sectors = conf->dev_sectors;
  3309. size = raid10_size(mddev, 0, 0);
  3310. md_set_array_sectors(mddev, size);
  3311. mddev->resync_max_sectors = size;
  3312. if (mddev->queue) {
  3313. int stripe = conf->geo.raid_disks *
  3314. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  3315. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  3316. mddev->queue->backing_dev_info.congested_data = mddev;
  3317. /* Calculate max read-ahead size.
  3318. * We need to readahead at least twice a whole stripe....
  3319. * maybe...
  3320. */
  3321. stripe /= conf->geo.near_copies;
  3322. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3323. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3324. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  3325. }
  3326. if (md_integrity_register(mddev))
  3327. goto out_free_conf;
  3328. if (conf->reshape_progress != MaxSector) {
  3329. unsigned long before_length, after_length;
  3330. before_length = ((1 << conf->prev.chunk_shift) *
  3331. conf->prev.far_copies);
  3332. after_length = ((1 << conf->geo.chunk_shift) *
  3333. conf->geo.far_copies);
  3334. if (max(before_length, after_length) > min_offset_diff) {
  3335. /* This cannot work */
  3336. printk("md/raid10: offset difference not enough to continue reshape\n");
  3337. goto out_free_conf;
  3338. }
  3339. conf->offset_diff = min_offset_diff;
  3340. conf->reshape_safe = conf->reshape_progress;
  3341. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3342. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3343. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3344. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3345. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3346. "reshape");
  3347. }
  3348. return 0;
  3349. out_free_conf:
  3350. md_unregister_thread(&mddev->thread);
  3351. if (conf->r10bio_pool)
  3352. mempool_destroy(conf->r10bio_pool);
  3353. safe_put_page(conf->tmppage);
  3354. kfree(conf->mirrors);
  3355. kfree(conf);
  3356. mddev->private = NULL;
  3357. out:
  3358. return -EIO;
  3359. }
  3360. static int stop(struct mddev *mddev)
  3361. {
  3362. struct r10conf *conf = mddev->private;
  3363. raise_barrier(conf, 0);
  3364. lower_barrier(conf);
  3365. md_unregister_thread(&mddev->thread);
  3366. if (mddev->queue)
  3367. /* the unplug fn references 'conf'*/
  3368. blk_sync_queue(mddev->queue);
  3369. if (conf->r10bio_pool)
  3370. mempool_destroy(conf->r10bio_pool);
  3371. kfree(conf->mirrors);
  3372. kfree(conf);
  3373. mddev->private = NULL;
  3374. return 0;
  3375. }
  3376. static void raid10_quiesce(struct mddev *mddev, int state)
  3377. {
  3378. struct r10conf *conf = mddev->private;
  3379. switch(state) {
  3380. case 1:
  3381. raise_barrier(conf, 0);
  3382. break;
  3383. case 0:
  3384. lower_barrier(conf);
  3385. break;
  3386. }
  3387. }
  3388. static int raid10_resize(struct mddev *mddev, sector_t sectors)
  3389. {
  3390. /* Resize of 'far' arrays is not supported.
  3391. * For 'near' and 'offset' arrays we can set the
  3392. * number of sectors used to be an appropriate multiple
  3393. * of the chunk size.
  3394. * For 'offset', this is far_copies*chunksize.
  3395. * For 'near' the multiplier is the LCM of
  3396. * near_copies and raid_disks.
  3397. * So if far_copies > 1 && !far_offset, fail.
  3398. * Else find LCM(raid_disks, near_copy)*far_copies and
  3399. * multiply by chunk_size. Then round to this number.
  3400. * This is mostly done by raid10_size()
  3401. */
  3402. struct r10conf *conf = mddev->private;
  3403. sector_t oldsize, size;
  3404. if (mddev->reshape_position != MaxSector)
  3405. return -EBUSY;
  3406. if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
  3407. return -EINVAL;
  3408. oldsize = raid10_size(mddev, 0, 0);
  3409. size = raid10_size(mddev, sectors, 0);
  3410. if (mddev->external_size &&
  3411. mddev->array_sectors > size)
  3412. return -EINVAL;
  3413. if (mddev->bitmap) {
  3414. int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
  3415. if (ret)
  3416. return ret;
  3417. }
  3418. md_set_array_sectors(mddev, size);
  3419. set_capacity(mddev->gendisk, mddev->array_sectors);
  3420. revalidate_disk(mddev->gendisk);
  3421. if (sectors > mddev->dev_sectors &&
  3422. mddev->recovery_cp > oldsize) {
  3423. mddev->recovery_cp = oldsize;
  3424. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3425. }
  3426. calc_sectors(conf, sectors);
  3427. mddev->dev_sectors = conf->dev_sectors;
  3428. mddev->resync_max_sectors = size;
  3429. return 0;
  3430. }
  3431. static void *raid10_takeover_raid0(struct mddev *mddev)
  3432. {
  3433. struct md_rdev *rdev;
  3434. struct r10conf *conf;
  3435. if (mddev->degraded > 0) {
  3436. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  3437. mdname(mddev));
  3438. return ERR_PTR(-EINVAL);
  3439. }
  3440. /* Set new parameters */
  3441. mddev->new_level = 10;
  3442. /* new layout: far_copies = 1, near_copies = 2 */
  3443. mddev->new_layout = (1<<8) + 2;
  3444. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3445. mddev->delta_disks = mddev->raid_disks;
  3446. mddev->raid_disks *= 2;
  3447. /* make sure it will be not marked as dirty */
  3448. mddev->recovery_cp = MaxSector;
  3449. conf = setup_conf(mddev);
  3450. if (!IS_ERR(conf)) {
  3451. rdev_for_each(rdev, mddev)
  3452. if (rdev->raid_disk >= 0)
  3453. rdev->new_raid_disk = rdev->raid_disk * 2;
  3454. conf->barrier = 1;
  3455. }
  3456. return conf;
  3457. }
  3458. static void *raid10_takeover(struct mddev *mddev)
  3459. {
  3460. struct r0conf *raid0_conf;
  3461. /* raid10 can take over:
  3462. * raid0 - providing it has only two drives
  3463. */
  3464. if (mddev->level == 0) {
  3465. /* for raid0 takeover only one zone is supported */
  3466. raid0_conf = mddev->private;
  3467. if (raid0_conf->nr_strip_zones > 1) {
  3468. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  3469. " with more than one zone.\n",
  3470. mdname(mddev));
  3471. return ERR_PTR(-EINVAL);
  3472. }
  3473. return raid10_takeover_raid0(mddev);
  3474. }
  3475. return ERR_PTR(-EINVAL);
  3476. }
  3477. static int raid10_check_reshape(struct mddev *mddev)
  3478. {
  3479. /* Called when there is a request to change
  3480. * - layout (to ->new_layout)
  3481. * - chunk size (to ->new_chunk_sectors)
  3482. * - raid_disks (by delta_disks)
  3483. * or when trying to restart a reshape that was ongoing.
  3484. *
  3485. * We need to validate the request and possibly allocate
  3486. * space if that might be an issue later.
  3487. *
  3488. * Currently we reject any reshape of a 'far' mode array,
  3489. * allow chunk size to change if new is generally acceptable,
  3490. * allow raid_disks to increase, and allow
  3491. * a switch between 'near' mode and 'offset' mode.
  3492. */
  3493. struct r10conf *conf = mddev->private;
  3494. struct geom geo;
  3495. if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
  3496. return -EINVAL;
  3497. if (setup_geo(&geo, mddev, geo_start) != conf->copies)
  3498. /* mustn't change number of copies */
  3499. return -EINVAL;
  3500. if (geo.far_copies > 1 && !geo.far_offset)
  3501. /* Cannot switch to 'far' mode */
  3502. return -EINVAL;
  3503. if (mddev->array_sectors & geo.chunk_mask)
  3504. /* not factor of array size */
  3505. return -EINVAL;
  3506. if (!enough(conf, -1))
  3507. return -EINVAL;
  3508. kfree(conf->mirrors_new);
  3509. conf->mirrors_new = NULL;
  3510. if (mddev->delta_disks > 0) {
  3511. /* allocate new 'mirrors' list */
  3512. conf->mirrors_new = kzalloc(
  3513. sizeof(struct raid10_info)
  3514. *(mddev->raid_disks +
  3515. mddev->delta_disks),
  3516. GFP_KERNEL);
  3517. if (!conf->mirrors_new)
  3518. return -ENOMEM;
  3519. }
  3520. return 0;
  3521. }
  3522. /*
  3523. * Need to check if array has failed when deciding whether to:
  3524. * - start an array
  3525. * - remove non-faulty devices
  3526. * - add a spare
  3527. * - allow a reshape
  3528. * This determination is simple when no reshape is happening.
  3529. * However if there is a reshape, we need to carefully check
  3530. * both the before and after sections.
  3531. * This is because some failed devices may only affect one
  3532. * of the two sections, and some non-in_sync devices may
  3533. * be insync in the section most affected by failed devices.
  3534. */
  3535. static int calc_degraded(struct r10conf *conf)
  3536. {
  3537. int degraded, degraded2;
  3538. int i;
  3539. rcu_read_lock();
  3540. degraded = 0;
  3541. /* 'prev' section first */
  3542. for (i = 0; i < conf->prev.raid_disks; i++) {
  3543. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3544. if (!rdev || test_bit(Faulty, &rdev->flags))
  3545. degraded++;
  3546. else if (!test_bit(In_sync, &rdev->flags))
  3547. /* When we can reduce the number of devices in
  3548. * an array, this might not contribute to
  3549. * 'degraded'. It does now.
  3550. */
  3551. degraded++;
  3552. }
  3553. rcu_read_unlock();
  3554. if (conf->geo.raid_disks == conf->prev.raid_disks)
  3555. return degraded;
  3556. rcu_read_lock();
  3557. degraded2 = 0;
  3558. for (i = 0; i < conf->geo.raid_disks; i++) {
  3559. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3560. if (!rdev || test_bit(Faulty, &rdev->flags))
  3561. degraded2++;
  3562. else if (!test_bit(In_sync, &rdev->flags)) {
  3563. /* If reshape is increasing the number of devices,
  3564. * this section has already been recovered, so
  3565. * it doesn't contribute to degraded.
  3566. * else it does.
  3567. */
  3568. if (conf->geo.raid_disks <= conf->prev.raid_disks)
  3569. degraded2++;
  3570. }
  3571. }
  3572. rcu_read_unlock();
  3573. if (degraded2 > degraded)
  3574. return degraded2;
  3575. return degraded;
  3576. }
  3577. static int raid10_start_reshape(struct mddev *mddev)
  3578. {
  3579. /* A 'reshape' has been requested. This commits
  3580. * the various 'new' fields and sets MD_RECOVER_RESHAPE
  3581. * This also checks if there are enough spares and adds them
  3582. * to the array.
  3583. * We currently require enough spares to make the final
  3584. * array non-degraded. We also require that the difference
  3585. * between old and new data_offset - on each device - is
  3586. * enough that we never risk over-writing.
  3587. */
  3588. unsigned long before_length, after_length;
  3589. sector_t min_offset_diff = 0;
  3590. int first = 1;
  3591. struct geom new;
  3592. struct r10conf *conf = mddev->private;
  3593. struct md_rdev *rdev;
  3594. int spares = 0;
  3595. int ret;
  3596. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3597. return -EBUSY;
  3598. if (setup_geo(&new, mddev, geo_start) != conf->copies)
  3599. return -EINVAL;
  3600. before_length = ((1 << conf->prev.chunk_shift) *
  3601. conf->prev.far_copies);
  3602. after_length = ((1 << conf->geo.chunk_shift) *
  3603. conf->geo.far_copies);
  3604. rdev_for_each(rdev, mddev) {
  3605. if (!test_bit(In_sync, &rdev->flags)
  3606. && !test_bit(Faulty, &rdev->flags))
  3607. spares++;
  3608. if (rdev->raid_disk >= 0) {
  3609. long long diff = (rdev->new_data_offset
  3610. - rdev->data_offset);
  3611. if (!mddev->reshape_backwards)
  3612. diff = -diff;
  3613. if (diff < 0)
  3614. diff = 0;
  3615. if (first || diff < min_offset_diff)
  3616. min_offset_diff = diff;
  3617. }
  3618. }
  3619. if (max(before_length, after_length) > min_offset_diff)
  3620. return -EINVAL;
  3621. if (spares < mddev->delta_disks)
  3622. return -EINVAL;
  3623. conf->offset_diff = min_offset_diff;
  3624. spin_lock_irq(&conf->device_lock);
  3625. if (conf->mirrors_new) {
  3626. memcpy(conf->mirrors_new, conf->mirrors,
  3627. sizeof(struct raid10_info)*conf->prev.raid_disks);
  3628. smp_mb();
  3629. kfree(conf->mirrors_old); /* FIXME and elsewhere */
  3630. conf->mirrors_old = conf->mirrors;
  3631. conf->mirrors = conf->mirrors_new;
  3632. conf->mirrors_new = NULL;
  3633. }
  3634. setup_geo(&conf->geo, mddev, geo_start);
  3635. smp_mb();
  3636. if (mddev->reshape_backwards) {
  3637. sector_t size = raid10_size(mddev, 0, 0);
  3638. if (size < mddev->array_sectors) {
  3639. spin_unlock_irq(&conf->device_lock);
  3640. printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
  3641. mdname(mddev));
  3642. return -EINVAL;
  3643. }
  3644. mddev->resync_max_sectors = size;
  3645. conf->reshape_progress = size;
  3646. } else
  3647. conf->reshape_progress = 0;
  3648. spin_unlock_irq(&conf->device_lock);
  3649. if (mddev->delta_disks && mddev->bitmap) {
  3650. ret = bitmap_resize(mddev->bitmap,
  3651. raid10_size(mddev, 0,
  3652. conf->geo.raid_disks),
  3653. 0, 0);
  3654. if (ret)
  3655. goto abort;
  3656. }
  3657. if (mddev->delta_disks > 0) {
  3658. rdev_for_each(rdev, mddev)
  3659. if (rdev->raid_disk < 0 &&
  3660. !test_bit(Faulty, &rdev->flags)) {
  3661. if (raid10_add_disk(mddev, rdev) == 0) {
  3662. if (rdev->raid_disk >=
  3663. conf->prev.raid_disks)
  3664. set_bit(In_sync, &rdev->flags);
  3665. else
  3666. rdev->recovery_offset = 0;
  3667. if (sysfs_link_rdev(mddev, rdev))
  3668. /* Failure here is OK */;
  3669. }
  3670. } else if (rdev->raid_disk >= conf->prev.raid_disks
  3671. && !test_bit(Faulty, &rdev->flags)) {
  3672. /* This is a spare that was manually added */
  3673. set_bit(In_sync, &rdev->flags);
  3674. }
  3675. }
  3676. /* When a reshape changes the number of devices,
  3677. * ->degraded is measured against the larger of the
  3678. * pre and post numbers.
  3679. */
  3680. spin_lock_irq(&conf->device_lock);
  3681. mddev->degraded = calc_degraded(conf);
  3682. spin_unlock_irq(&conf->device_lock);
  3683. mddev->raid_disks = conf->geo.raid_disks;
  3684. mddev->reshape_position = conf->reshape_progress;
  3685. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3686. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3687. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3688. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3689. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3690. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3691. "reshape");
  3692. if (!mddev->sync_thread) {
  3693. ret = -EAGAIN;
  3694. goto abort;
  3695. }
  3696. conf->reshape_checkpoint = jiffies;
  3697. md_wakeup_thread(mddev->sync_thread);
  3698. md_new_event(mddev);
  3699. return 0;
  3700. abort:
  3701. mddev->recovery = 0;
  3702. spin_lock_irq(&conf->device_lock);
  3703. conf->geo = conf->prev;
  3704. mddev->raid_disks = conf->geo.raid_disks;
  3705. rdev_for_each(rdev, mddev)
  3706. rdev->new_data_offset = rdev->data_offset;
  3707. smp_wmb();
  3708. conf->reshape_progress = MaxSector;
  3709. mddev->reshape_position = MaxSector;
  3710. spin_unlock_irq(&conf->device_lock);
  3711. return ret;
  3712. }
  3713. /* Calculate the last device-address that could contain
  3714. * any block from the chunk that includes the array-address 's'
  3715. * and report the next address.
  3716. * i.e. the address returned will be chunk-aligned and after
  3717. * any data that is in the chunk containing 's'.
  3718. */
  3719. static sector_t last_dev_address(sector_t s, struct geom *geo)
  3720. {
  3721. s = (s | geo->chunk_mask) + 1;
  3722. s >>= geo->chunk_shift;
  3723. s *= geo->near_copies;
  3724. s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
  3725. s *= geo->far_copies;
  3726. s <<= geo->chunk_shift;
  3727. return s;
  3728. }
  3729. /* Calculate the first device-address that could contain
  3730. * any block from the chunk that includes the array-address 's'.
  3731. * This too will be the start of a chunk
  3732. */
  3733. static sector_t first_dev_address(sector_t s, struct geom *geo)
  3734. {
  3735. s >>= geo->chunk_shift;
  3736. s *= geo->near_copies;
  3737. sector_div(s, geo->raid_disks);
  3738. s *= geo->far_copies;
  3739. s <<= geo->chunk_shift;
  3740. return s;
  3741. }
  3742. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  3743. int *skipped)
  3744. {
  3745. /* We simply copy at most one chunk (smallest of old and new)
  3746. * at a time, possibly less if that exceeds RESYNC_PAGES,
  3747. * or we hit a bad block or something.
  3748. * This might mean we pause for normal IO in the middle of
  3749. * a chunk, but that is not a problem was mddev->reshape_position
  3750. * can record any location.
  3751. *
  3752. * If we will want to write to a location that isn't
  3753. * yet recorded as 'safe' (i.e. in metadata on disk) then
  3754. * we need to flush all reshape requests and update the metadata.
  3755. *
  3756. * When reshaping forwards (e.g. to more devices), we interpret
  3757. * 'safe' as the earliest block which might not have been copied
  3758. * down yet. We divide this by previous stripe size and multiply
  3759. * by previous stripe length to get lowest device offset that we
  3760. * cannot write to yet.
  3761. * We interpret 'sector_nr' as an address that we want to write to.
  3762. * From this we use last_device_address() to find where we might
  3763. * write to, and first_device_address on the 'safe' position.
  3764. * If this 'next' write position is after the 'safe' position,
  3765. * we must update the metadata to increase the 'safe' position.
  3766. *
  3767. * When reshaping backwards, we round in the opposite direction
  3768. * and perform the reverse test: next write position must not be
  3769. * less than current safe position.
  3770. *
  3771. * In all this the minimum difference in data offsets
  3772. * (conf->offset_diff - always positive) allows a bit of slack,
  3773. * so next can be after 'safe', but not by more than offset_disk
  3774. *
  3775. * We need to prepare all the bios here before we start any IO
  3776. * to ensure the size we choose is acceptable to all devices.
  3777. * The means one for each copy for write-out and an extra one for
  3778. * read-in.
  3779. * We store the read-in bio in ->master_bio and the others in
  3780. * ->devs[x].bio and ->devs[x].repl_bio.
  3781. */
  3782. struct r10conf *conf = mddev->private;
  3783. struct r10bio *r10_bio;
  3784. sector_t next, safe, last;
  3785. int max_sectors;
  3786. int nr_sectors;
  3787. int s;
  3788. struct md_rdev *rdev;
  3789. int need_flush = 0;
  3790. struct bio *blist;
  3791. struct bio *bio, *read_bio;
  3792. int sectors_done = 0;
  3793. if (sector_nr == 0) {
  3794. /* If restarting in the middle, skip the initial sectors */
  3795. if (mddev->reshape_backwards &&
  3796. conf->reshape_progress < raid10_size(mddev, 0, 0)) {
  3797. sector_nr = (raid10_size(mddev, 0, 0)
  3798. - conf->reshape_progress);
  3799. } else if (!mddev->reshape_backwards &&
  3800. conf->reshape_progress > 0)
  3801. sector_nr = conf->reshape_progress;
  3802. if (sector_nr) {
  3803. mddev->curr_resync_completed = sector_nr;
  3804. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3805. *skipped = 1;
  3806. return sector_nr;
  3807. }
  3808. }
  3809. /* We don't use sector_nr to track where we are up to
  3810. * as that doesn't work well for ->reshape_backwards.
  3811. * So just use ->reshape_progress.
  3812. */
  3813. if (mddev->reshape_backwards) {
  3814. /* 'next' is the earliest device address that we might
  3815. * write to for this chunk in the new layout
  3816. */
  3817. next = first_dev_address(conf->reshape_progress - 1,
  3818. &conf->geo);
  3819. /* 'safe' is the last device address that we might read from
  3820. * in the old layout after a restart
  3821. */
  3822. safe = last_dev_address(conf->reshape_safe - 1,
  3823. &conf->prev);
  3824. if (next + conf->offset_diff < safe)
  3825. need_flush = 1;
  3826. last = conf->reshape_progress - 1;
  3827. sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
  3828. & conf->prev.chunk_mask);
  3829. if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
  3830. sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
  3831. } else {
  3832. /* 'next' is after the last device address that we
  3833. * might write to for this chunk in the new layout
  3834. */
  3835. next = last_dev_address(conf->reshape_progress, &conf->geo);
  3836. /* 'safe' is the earliest device address that we might
  3837. * read from in the old layout after a restart
  3838. */
  3839. safe = first_dev_address(conf->reshape_safe, &conf->prev);
  3840. /* Need to update metadata if 'next' might be beyond 'safe'
  3841. * as that would possibly corrupt data
  3842. */
  3843. if (next > safe + conf->offset_diff)
  3844. need_flush = 1;
  3845. sector_nr = conf->reshape_progress;
  3846. last = sector_nr | (conf->geo.chunk_mask
  3847. & conf->prev.chunk_mask);
  3848. if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
  3849. last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
  3850. }
  3851. if (need_flush ||
  3852. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3853. /* Need to update reshape_position in metadata */
  3854. wait_barrier(conf);
  3855. mddev->reshape_position = conf->reshape_progress;
  3856. if (mddev->reshape_backwards)
  3857. mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
  3858. - conf->reshape_progress;
  3859. else
  3860. mddev->curr_resync_completed = conf->reshape_progress;
  3861. conf->reshape_checkpoint = jiffies;
  3862. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3863. md_wakeup_thread(mddev->thread);
  3864. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3865. kthread_should_stop());
  3866. conf->reshape_safe = mddev->reshape_position;
  3867. allow_barrier(conf);
  3868. }
  3869. read_more:
  3870. /* Now schedule reads for blocks from sector_nr to last */
  3871. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  3872. raise_barrier(conf, sectors_done != 0);
  3873. atomic_set(&r10_bio->remaining, 0);
  3874. r10_bio->mddev = mddev;
  3875. r10_bio->sector = sector_nr;
  3876. set_bit(R10BIO_IsReshape, &r10_bio->state);
  3877. r10_bio->sectors = last - sector_nr + 1;
  3878. rdev = read_balance(conf, r10_bio, &max_sectors);
  3879. BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
  3880. if (!rdev) {
  3881. /* Cannot read from here, so need to record bad blocks
  3882. * on all the target devices.
  3883. */
  3884. // FIXME
  3885. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  3886. return sectors_done;
  3887. }
  3888. read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
  3889. read_bio->bi_bdev = rdev->bdev;
  3890. read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
  3891. + rdev->data_offset);
  3892. read_bio->bi_private = r10_bio;
  3893. read_bio->bi_end_io = end_sync_read;
  3894. read_bio->bi_rw = READ;
  3895. read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  3896. read_bio->bi_flags |= 1 << BIO_UPTODATE;
  3897. read_bio->bi_vcnt = 0;
  3898. read_bio->bi_idx = 0;
  3899. read_bio->bi_size = 0;
  3900. r10_bio->master_bio = read_bio;
  3901. r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
  3902. /* Now find the locations in the new layout */
  3903. __raid10_find_phys(&conf->geo, r10_bio);
  3904. blist = read_bio;
  3905. read_bio->bi_next = NULL;
  3906. for (s = 0; s < conf->copies*2; s++) {
  3907. struct bio *b;
  3908. int d = r10_bio->devs[s/2].devnum;
  3909. struct md_rdev *rdev2;
  3910. if (s&1) {
  3911. rdev2 = conf->mirrors[d].replacement;
  3912. b = r10_bio->devs[s/2].repl_bio;
  3913. } else {
  3914. rdev2 = conf->mirrors[d].rdev;
  3915. b = r10_bio->devs[s/2].bio;
  3916. }
  3917. if (!rdev2 || test_bit(Faulty, &rdev2->flags))
  3918. continue;
  3919. b->bi_bdev = rdev2->bdev;
  3920. b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
  3921. b->bi_private = r10_bio;
  3922. b->bi_end_io = end_reshape_write;
  3923. b->bi_rw = WRITE;
  3924. b->bi_flags &= ~(BIO_POOL_MASK - 1);
  3925. b->bi_flags |= 1 << BIO_UPTODATE;
  3926. b->bi_next = blist;
  3927. b->bi_vcnt = 0;
  3928. b->bi_idx = 0;
  3929. b->bi_size = 0;
  3930. blist = b;
  3931. }
  3932. /* Now add as many pages as possible to all of these bios. */
  3933. nr_sectors = 0;
  3934. for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
  3935. struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
  3936. int len = (max_sectors - s) << 9;
  3937. if (len > PAGE_SIZE)
  3938. len = PAGE_SIZE;
  3939. for (bio = blist; bio ; bio = bio->bi_next) {
  3940. struct bio *bio2;
  3941. if (bio_add_page(bio, page, len, 0))
  3942. continue;
  3943. /* Didn't fit, must stop */
  3944. for (bio2 = blist;
  3945. bio2 && bio2 != bio;
  3946. bio2 = bio2->bi_next) {
  3947. /* Remove last page from this bio */
  3948. bio2->bi_vcnt--;
  3949. bio2->bi_size -= len;
  3950. bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
  3951. }
  3952. goto bio_full;
  3953. }
  3954. sector_nr += len >> 9;
  3955. nr_sectors += len >> 9;
  3956. }
  3957. bio_full:
  3958. r10_bio->sectors = nr_sectors;
  3959. /* Now submit the read */
  3960. md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
  3961. atomic_inc(&r10_bio->remaining);
  3962. read_bio->bi_next = NULL;
  3963. generic_make_request(read_bio);
  3964. sector_nr += nr_sectors;
  3965. sectors_done += nr_sectors;
  3966. if (sector_nr <= last)
  3967. goto read_more;
  3968. /* Now that we have done the whole section we can
  3969. * update reshape_progress
  3970. */
  3971. if (mddev->reshape_backwards)
  3972. conf->reshape_progress -= sectors_done;
  3973. else
  3974. conf->reshape_progress += sectors_done;
  3975. return sectors_done;
  3976. }
  3977. static void end_reshape_request(struct r10bio *r10_bio);
  3978. static int handle_reshape_read_error(struct mddev *mddev,
  3979. struct r10bio *r10_bio);
  3980. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  3981. {
  3982. /* Reshape read completed. Hopefully we have a block
  3983. * to write out.
  3984. * If we got a read error then we do sync 1-page reads from
  3985. * elsewhere until we find the data - or give up.
  3986. */
  3987. struct r10conf *conf = mddev->private;
  3988. int s;
  3989. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  3990. if (handle_reshape_read_error(mddev, r10_bio) < 0) {
  3991. /* Reshape has been aborted */
  3992. md_done_sync(mddev, r10_bio->sectors, 0);
  3993. return;
  3994. }
  3995. /* We definitely have the data in the pages, schedule the
  3996. * writes.
  3997. */
  3998. atomic_set(&r10_bio->remaining, 1);
  3999. for (s = 0; s < conf->copies*2; s++) {
  4000. struct bio *b;
  4001. int d = r10_bio->devs[s/2].devnum;
  4002. struct md_rdev *rdev;
  4003. if (s&1) {
  4004. rdev = conf->mirrors[d].replacement;
  4005. b = r10_bio->devs[s/2].repl_bio;
  4006. } else {
  4007. rdev = conf->mirrors[d].rdev;
  4008. b = r10_bio->devs[s/2].bio;
  4009. }
  4010. if (!rdev || test_bit(Faulty, &rdev->flags))
  4011. continue;
  4012. atomic_inc(&rdev->nr_pending);
  4013. md_sync_acct(b->bi_bdev, r10_bio->sectors);
  4014. atomic_inc(&r10_bio->remaining);
  4015. b->bi_next = NULL;
  4016. generic_make_request(b);
  4017. }
  4018. end_reshape_request(r10_bio);
  4019. }
  4020. static void end_reshape(struct r10conf *conf)
  4021. {
  4022. if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
  4023. return;
  4024. spin_lock_irq(&conf->device_lock);
  4025. conf->prev = conf->geo;
  4026. md_finish_reshape(conf->mddev);
  4027. smp_wmb();
  4028. conf->reshape_progress = MaxSector;
  4029. spin_unlock_irq(&conf->device_lock);
  4030. /* read-ahead size must cover two whole stripes, which is
  4031. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4032. */
  4033. if (conf->mddev->queue) {
  4034. int stripe = conf->geo.raid_disks *
  4035. ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
  4036. stripe /= conf->geo.near_copies;
  4037. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4038. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4039. }
  4040. conf->fullsync = 0;
  4041. }
  4042. static int handle_reshape_read_error(struct mddev *mddev,
  4043. struct r10bio *r10_bio)
  4044. {
  4045. /* Use sync reads to get the blocks from somewhere else */
  4046. int sectors = r10_bio->sectors;
  4047. struct r10conf *conf = mddev->private;
  4048. struct {
  4049. struct r10bio r10_bio;
  4050. struct r10dev devs[conf->copies];
  4051. } on_stack;
  4052. struct r10bio *r10b = &on_stack.r10_bio;
  4053. int slot = 0;
  4054. int idx = 0;
  4055. struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
  4056. r10b->sector = r10_bio->sector;
  4057. __raid10_find_phys(&conf->prev, r10b);
  4058. while (sectors) {
  4059. int s = sectors;
  4060. int success = 0;
  4061. int first_slot = slot;
  4062. if (s > (PAGE_SIZE >> 9))
  4063. s = PAGE_SIZE >> 9;
  4064. while (!success) {
  4065. int d = r10b->devs[slot].devnum;
  4066. struct md_rdev *rdev = conf->mirrors[d].rdev;
  4067. sector_t addr;
  4068. if (rdev == NULL ||
  4069. test_bit(Faulty, &rdev->flags) ||
  4070. !test_bit(In_sync, &rdev->flags))
  4071. goto failed;
  4072. addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
  4073. success = sync_page_io(rdev,
  4074. addr,
  4075. s << 9,
  4076. bvec[idx].bv_page,
  4077. READ, false);
  4078. if (success)
  4079. break;
  4080. failed:
  4081. slot++;
  4082. if (slot >= conf->copies)
  4083. slot = 0;
  4084. if (slot == first_slot)
  4085. break;
  4086. }
  4087. if (!success) {
  4088. /* couldn't read this block, must give up */
  4089. set_bit(MD_RECOVERY_INTR,
  4090. &mddev->recovery);
  4091. return -EIO;
  4092. }
  4093. sectors -= s;
  4094. idx++;
  4095. }
  4096. return 0;
  4097. }
  4098. static void end_reshape_write(struct bio *bio, int error)
  4099. {
  4100. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  4101. struct r10bio *r10_bio = bio->bi_private;
  4102. struct mddev *mddev = r10_bio->mddev;
  4103. struct r10conf *conf = mddev->private;
  4104. int d;
  4105. int slot;
  4106. int repl;
  4107. struct md_rdev *rdev = NULL;
  4108. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  4109. if (repl)
  4110. rdev = conf->mirrors[d].replacement;
  4111. if (!rdev) {
  4112. smp_mb();
  4113. rdev = conf->mirrors[d].rdev;
  4114. }
  4115. if (!uptodate) {
  4116. /* FIXME should record badblock */
  4117. md_error(mddev, rdev);
  4118. }
  4119. rdev_dec_pending(rdev, mddev);
  4120. end_reshape_request(r10_bio);
  4121. }
  4122. static void end_reshape_request(struct r10bio *r10_bio)
  4123. {
  4124. if (!atomic_dec_and_test(&r10_bio->remaining))
  4125. return;
  4126. md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
  4127. bio_put(r10_bio->master_bio);
  4128. put_buf(r10_bio);
  4129. }
  4130. static void raid10_finish_reshape(struct mddev *mddev)
  4131. {
  4132. struct r10conf *conf = mddev->private;
  4133. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  4134. return;
  4135. if (mddev->delta_disks > 0) {
  4136. sector_t size = raid10_size(mddev, 0, 0);
  4137. md_set_array_sectors(mddev, size);
  4138. if (mddev->recovery_cp > mddev->resync_max_sectors) {
  4139. mddev->recovery_cp = mddev->resync_max_sectors;
  4140. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4141. }
  4142. mddev->resync_max_sectors = size;
  4143. set_capacity(mddev->gendisk, mddev->array_sectors);
  4144. revalidate_disk(mddev->gendisk);
  4145. } else {
  4146. int d;
  4147. for (d = conf->geo.raid_disks ;
  4148. d < conf->geo.raid_disks - mddev->delta_disks;
  4149. d++) {
  4150. struct md_rdev *rdev = conf->mirrors[d].rdev;
  4151. if (rdev)
  4152. clear_bit(In_sync, &rdev->flags);
  4153. rdev = conf->mirrors[d].replacement;
  4154. if (rdev)
  4155. clear_bit(In_sync, &rdev->flags);
  4156. }
  4157. }
  4158. mddev->layout = mddev->new_layout;
  4159. mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
  4160. mddev->reshape_position = MaxSector;
  4161. mddev->delta_disks = 0;
  4162. mddev->reshape_backwards = 0;
  4163. }
  4164. static struct md_personality raid10_personality =
  4165. {
  4166. .name = "raid10",
  4167. .level = 10,
  4168. .owner = THIS_MODULE,
  4169. .make_request = make_request,
  4170. .run = run,
  4171. .stop = stop,
  4172. .status = status,
  4173. .error_handler = error,
  4174. .hot_add_disk = raid10_add_disk,
  4175. .hot_remove_disk= raid10_remove_disk,
  4176. .spare_active = raid10_spare_active,
  4177. .sync_request = sync_request,
  4178. .quiesce = raid10_quiesce,
  4179. .size = raid10_size,
  4180. .resize = raid10_resize,
  4181. .takeover = raid10_takeover,
  4182. .check_reshape = raid10_check_reshape,
  4183. .start_reshape = raid10_start_reshape,
  4184. .finish_reshape = raid10_finish_reshape,
  4185. };
  4186. static int __init raid_init(void)
  4187. {
  4188. return register_md_personality(&raid10_personality);
  4189. }
  4190. static void raid_exit(void)
  4191. {
  4192. unregister_md_personality(&raid10_personality);
  4193. }
  4194. module_init(raid_init);
  4195. module_exit(raid_exit);
  4196. MODULE_LICENSE("GPL");
  4197. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  4198. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  4199. MODULE_ALIAS("md-raid10");
  4200. MODULE_ALIAS("md-level-10");
  4201. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);