raid10.c 131 KB

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