raid10.c 131 KB

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