raid10.c 134 KB

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