raid10.c 135 KB

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