raid10.c 128 KB

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