raid10.c 130 KB

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