raid10.c 129 KB

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