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 (slot >= 0 && 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. r10_bio->read_slot = -1;
  1391. memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
  1392. if (bio_data_dir(bio) == READ)
  1393. raid10_read_request(mddev, bio, r10_bio);
  1394. else
  1395. raid10_write_request(mddev, bio, r10_bio);
  1396. }
  1397. static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
  1398. {
  1399. struct r10conf *conf = mddev->private;
  1400. sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
  1401. int chunk_sects = chunk_mask + 1;
  1402. int sectors = bio_sectors(bio);
  1403. if (unlikely(bio->bi_opf & REQ_PREFLUSH)
  1404. && md_flush_request(mddev, bio))
  1405. return true;
  1406. if (!md_write_start(mddev, bio))
  1407. return false;
  1408. /*
  1409. * If this request crosses a chunk boundary, we need to split
  1410. * it.
  1411. */
  1412. if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
  1413. sectors > chunk_sects
  1414. && (conf->geo.near_copies < conf->geo.raid_disks
  1415. || conf->prev.near_copies <
  1416. conf->prev.raid_disks)))
  1417. sectors = chunk_sects -
  1418. (bio->bi_iter.bi_sector &
  1419. (chunk_sects - 1));
  1420. __make_request(mddev, bio, sectors);
  1421. /* In case raid10d snuck in to freeze_array */
  1422. wake_up(&conf->wait_barrier);
  1423. return true;
  1424. }
  1425. static void raid10_status(struct seq_file *seq, struct mddev *mddev)
  1426. {
  1427. struct r10conf *conf = mddev->private;
  1428. int i;
  1429. if (conf->geo.near_copies < conf->geo.raid_disks)
  1430. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1431. if (conf->geo.near_copies > 1)
  1432. seq_printf(seq, " %d near-copies", conf->geo.near_copies);
  1433. if (conf->geo.far_copies > 1) {
  1434. if (conf->geo.far_offset)
  1435. seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
  1436. else
  1437. seq_printf(seq, " %d far-copies", conf->geo.far_copies);
  1438. if (conf->geo.far_set_size != conf->geo.raid_disks)
  1439. seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
  1440. }
  1441. seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
  1442. conf->geo.raid_disks - mddev->degraded);
  1443. rcu_read_lock();
  1444. for (i = 0; i < conf->geo.raid_disks; i++) {
  1445. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1446. seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  1447. }
  1448. rcu_read_unlock();
  1449. seq_printf(seq, "]");
  1450. }
  1451. /* check if there are enough drives for
  1452. * every block to appear on atleast one.
  1453. * Don't consider the device numbered 'ignore'
  1454. * as we might be about to remove it.
  1455. */
  1456. static int _enough(struct r10conf *conf, int previous, int ignore)
  1457. {
  1458. int first = 0;
  1459. int has_enough = 0;
  1460. int disks, ncopies;
  1461. if (previous) {
  1462. disks = conf->prev.raid_disks;
  1463. ncopies = conf->prev.near_copies;
  1464. } else {
  1465. disks = conf->geo.raid_disks;
  1466. ncopies = conf->geo.near_copies;
  1467. }
  1468. rcu_read_lock();
  1469. do {
  1470. int n = conf->copies;
  1471. int cnt = 0;
  1472. int this = first;
  1473. while (n--) {
  1474. struct md_rdev *rdev;
  1475. if (this != ignore &&
  1476. (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
  1477. test_bit(In_sync, &rdev->flags))
  1478. cnt++;
  1479. this = (this+1) % disks;
  1480. }
  1481. if (cnt == 0)
  1482. goto out;
  1483. first = (first + ncopies) % disks;
  1484. } while (first != 0);
  1485. has_enough = 1;
  1486. out:
  1487. rcu_read_unlock();
  1488. return has_enough;
  1489. }
  1490. static int enough(struct r10conf *conf, int ignore)
  1491. {
  1492. /* when calling 'enough', both 'prev' and 'geo' must
  1493. * be stable.
  1494. * This is ensured if ->reconfig_mutex or ->device_lock
  1495. * is held.
  1496. */
  1497. return _enough(conf, 0, ignore) &&
  1498. _enough(conf, 1, ignore);
  1499. }
  1500. static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
  1501. {
  1502. char b[BDEVNAME_SIZE];
  1503. struct r10conf *conf = mddev->private;
  1504. unsigned long flags;
  1505. /*
  1506. * If it is not operational, then we have already marked it as dead
  1507. * else if it is the last working disks, ignore the error, let the
  1508. * next level up know.
  1509. * else mark the drive as failed
  1510. */
  1511. spin_lock_irqsave(&conf->device_lock, flags);
  1512. if (test_bit(In_sync, &rdev->flags)
  1513. && !enough(conf, rdev->raid_disk)) {
  1514. /*
  1515. * Don't fail the drive, just return an IO error.
  1516. */
  1517. spin_unlock_irqrestore(&conf->device_lock, flags);
  1518. return;
  1519. }
  1520. if (test_and_clear_bit(In_sync, &rdev->flags))
  1521. mddev->degraded++;
  1522. /*
  1523. * If recovery is running, make sure it aborts.
  1524. */
  1525. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1526. set_bit(Blocked, &rdev->flags);
  1527. set_bit(Faulty, &rdev->flags);
  1528. set_mask_bits(&mddev->sb_flags, 0,
  1529. BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
  1530. spin_unlock_irqrestore(&conf->device_lock, flags);
  1531. pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
  1532. "md/raid10:%s: Operation continuing on %d devices.\n",
  1533. mdname(mddev), bdevname(rdev->bdev, b),
  1534. mdname(mddev), conf->geo.raid_disks - mddev->degraded);
  1535. }
  1536. static void print_conf(struct r10conf *conf)
  1537. {
  1538. int i;
  1539. struct md_rdev *rdev;
  1540. pr_debug("RAID10 conf printout:\n");
  1541. if (!conf) {
  1542. pr_debug("(!conf)\n");
  1543. return;
  1544. }
  1545. pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
  1546. conf->geo.raid_disks);
  1547. /* This is only called with ->reconfix_mutex held, so
  1548. * rcu protection of rdev is not needed */
  1549. for (i = 0; i < conf->geo.raid_disks; i++) {
  1550. char b[BDEVNAME_SIZE];
  1551. rdev = conf->mirrors[i].rdev;
  1552. if (rdev)
  1553. pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
  1554. i, !test_bit(In_sync, &rdev->flags),
  1555. !test_bit(Faulty, &rdev->flags),
  1556. bdevname(rdev->bdev,b));
  1557. }
  1558. }
  1559. static void close_sync(struct r10conf *conf)
  1560. {
  1561. wait_barrier(conf);
  1562. allow_barrier(conf);
  1563. mempool_exit(&conf->r10buf_pool);
  1564. }
  1565. static int raid10_spare_active(struct mddev *mddev)
  1566. {
  1567. int i;
  1568. struct r10conf *conf = mddev->private;
  1569. struct raid10_info *tmp;
  1570. int count = 0;
  1571. unsigned long flags;
  1572. /*
  1573. * Find all non-in_sync disks within the RAID10 configuration
  1574. * and mark them in_sync
  1575. */
  1576. for (i = 0; i < conf->geo.raid_disks; i++) {
  1577. tmp = conf->mirrors + i;
  1578. if (tmp->replacement
  1579. && tmp->replacement->recovery_offset == MaxSector
  1580. && !test_bit(Faulty, &tmp->replacement->flags)
  1581. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1582. /* Replacement has just become active */
  1583. if (!tmp->rdev
  1584. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1585. count++;
  1586. if (tmp->rdev) {
  1587. /* Replaced device not technically faulty,
  1588. * but we need to be sure it gets removed
  1589. * and never re-added.
  1590. */
  1591. set_bit(Faulty, &tmp->rdev->flags);
  1592. sysfs_notify_dirent_safe(
  1593. tmp->rdev->sysfs_state);
  1594. }
  1595. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1596. } else if (tmp->rdev
  1597. && tmp->rdev->recovery_offset == MaxSector
  1598. && !test_bit(Faulty, &tmp->rdev->flags)
  1599. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1600. count++;
  1601. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  1602. }
  1603. }
  1604. spin_lock_irqsave(&conf->device_lock, flags);
  1605. mddev->degraded -= count;
  1606. spin_unlock_irqrestore(&conf->device_lock, flags);
  1607. print_conf(conf);
  1608. return count;
  1609. }
  1610. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1611. {
  1612. struct r10conf *conf = mddev->private;
  1613. int err = -EEXIST;
  1614. int mirror;
  1615. int first = 0;
  1616. int last = conf->geo.raid_disks - 1;
  1617. if (mddev->recovery_cp < MaxSector)
  1618. /* only hot-add to in-sync arrays, as recovery is
  1619. * very different from resync
  1620. */
  1621. return -EBUSY;
  1622. if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
  1623. return -EINVAL;
  1624. if (md_integrity_add_rdev(rdev, mddev))
  1625. return -ENXIO;
  1626. if (rdev->raid_disk >= 0)
  1627. first = last = rdev->raid_disk;
  1628. if (rdev->saved_raid_disk >= first &&
  1629. rdev->saved_raid_disk < conf->geo.raid_disks &&
  1630. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1631. mirror = rdev->saved_raid_disk;
  1632. else
  1633. mirror = first;
  1634. for ( ; mirror <= last ; mirror++) {
  1635. struct raid10_info *p = &conf->mirrors[mirror];
  1636. if (p->recovery_disabled == mddev->recovery_disabled)
  1637. continue;
  1638. if (p->rdev) {
  1639. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1640. p->replacement != NULL)
  1641. continue;
  1642. clear_bit(In_sync, &rdev->flags);
  1643. set_bit(Replacement, &rdev->flags);
  1644. rdev->raid_disk = mirror;
  1645. err = 0;
  1646. if (mddev->gendisk)
  1647. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1648. rdev->data_offset << 9);
  1649. conf->fullsync = 1;
  1650. rcu_assign_pointer(p->replacement, rdev);
  1651. break;
  1652. }
  1653. if (mddev->gendisk)
  1654. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1655. rdev->data_offset << 9);
  1656. p->head_position = 0;
  1657. p->recovery_disabled = mddev->recovery_disabled - 1;
  1658. rdev->raid_disk = mirror;
  1659. err = 0;
  1660. if (rdev->saved_raid_disk != mirror)
  1661. conf->fullsync = 1;
  1662. rcu_assign_pointer(p->rdev, rdev);
  1663. break;
  1664. }
  1665. if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
  1666. blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
  1667. print_conf(conf);
  1668. return err;
  1669. }
  1670. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1671. {
  1672. struct r10conf *conf = mddev->private;
  1673. int err = 0;
  1674. int number = rdev->raid_disk;
  1675. struct md_rdev **rdevp;
  1676. struct raid10_info *p = conf->mirrors + number;
  1677. print_conf(conf);
  1678. if (rdev == p->rdev)
  1679. rdevp = &p->rdev;
  1680. else if (rdev == p->replacement)
  1681. rdevp = &p->replacement;
  1682. else
  1683. return 0;
  1684. if (test_bit(In_sync, &rdev->flags) ||
  1685. atomic_read(&rdev->nr_pending)) {
  1686. err = -EBUSY;
  1687. goto abort;
  1688. }
  1689. /* Only remove non-faulty devices if recovery
  1690. * is not possible.
  1691. */
  1692. if (!test_bit(Faulty, &rdev->flags) &&
  1693. mddev->recovery_disabled != p->recovery_disabled &&
  1694. (!p->replacement || p->replacement == rdev) &&
  1695. number < conf->geo.raid_disks &&
  1696. enough(conf, -1)) {
  1697. err = -EBUSY;
  1698. goto abort;
  1699. }
  1700. *rdevp = NULL;
  1701. if (!test_bit(RemoveSynchronized, &rdev->flags)) {
  1702. synchronize_rcu();
  1703. if (atomic_read(&rdev->nr_pending)) {
  1704. /* lost the race, try later */
  1705. err = -EBUSY;
  1706. *rdevp = rdev;
  1707. goto abort;
  1708. }
  1709. }
  1710. if (p->replacement) {
  1711. /* We must have just cleared 'rdev' */
  1712. p->rdev = p->replacement;
  1713. clear_bit(Replacement, &p->replacement->flags);
  1714. smp_mb(); /* Make sure other CPUs may see both as identical
  1715. * but will never see neither -- if they are careful.
  1716. */
  1717. p->replacement = NULL;
  1718. }
  1719. clear_bit(WantReplacement, &rdev->flags);
  1720. err = md_integrity_register(mddev);
  1721. abort:
  1722. print_conf(conf);
  1723. return err;
  1724. }
  1725. static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
  1726. {
  1727. struct r10conf *conf = r10_bio->mddev->private;
  1728. if (!bio->bi_status)
  1729. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1730. else
  1731. /* The write handler will notice the lack of
  1732. * R10BIO_Uptodate and record any errors etc
  1733. */
  1734. atomic_add(r10_bio->sectors,
  1735. &conf->mirrors[d].rdev->corrected_errors);
  1736. /* for reconstruct, we always reschedule after a read.
  1737. * for resync, only after all reads
  1738. */
  1739. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1740. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1741. atomic_dec_and_test(&r10_bio->remaining)) {
  1742. /* we have read all the blocks,
  1743. * do the comparison in process context in raid10d
  1744. */
  1745. reschedule_retry(r10_bio);
  1746. }
  1747. }
  1748. static void end_sync_read(struct bio *bio)
  1749. {
  1750. struct r10bio *r10_bio = get_resync_r10bio(bio);
  1751. struct r10conf *conf = r10_bio->mddev->private;
  1752. int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1753. __end_sync_read(r10_bio, bio, d);
  1754. }
  1755. static void end_reshape_read(struct bio *bio)
  1756. {
  1757. /* reshape read bio isn't allocated from r10buf_pool */
  1758. struct r10bio *r10_bio = bio->bi_private;
  1759. __end_sync_read(r10_bio, bio, r10_bio->read_slot);
  1760. }
  1761. static void end_sync_request(struct r10bio *r10_bio)
  1762. {
  1763. struct mddev *mddev = r10_bio->mddev;
  1764. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1765. if (r10_bio->master_bio == NULL) {
  1766. /* the primary of several recovery bios */
  1767. sector_t s = r10_bio->sectors;
  1768. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1769. test_bit(R10BIO_WriteError, &r10_bio->state))
  1770. reschedule_retry(r10_bio);
  1771. else
  1772. put_buf(r10_bio);
  1773. md_done_sync(mddev, s, 1);
  1774. break;
  1775. } else {
  1776. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1777. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1778. test_bit(R10BIO_WriteError, &r10_bio->state))
  1779. reschedule_retry(r10_bio);
  1780. else
  1781. put_buf(r10_bio);
  1782. r10_bio = r10_bio2;
  1783. }
  1784. }
  1785. }
  1786. static void end_sync_write(struct bio *bio)
  1787. {
  1788. struct r10bio *r10_bio = get_resync_r10bio(bio);
  1789. struct mddev *mddev = r10_bio->mddev;
  1790. struct r10conf *conf = mddev->private;
  1791. int d;
  1792. sector_t first_bad;
  1793. int bad_sectors;
  1794. int slot;
  1795. int repl;
  1796. struct md_rdev *rdev = NULL;
  1797. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1798. if (repl)
  1799. rdev = conf->mirrors[d].replacement;
  1800. else
  1801. rdev = conf->mirrors[d].rdev;
  1802. if (bio->bi_status) {
  1803. if (repl)
  1804. md_error(mddev, rdev);
  1805. else {
  1806. set_bit(WriteErrorSeen, &rdev->flags);
  1807. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1808. set_bit(MD_RECOVERY_NEEDED,
  1809. &rdev->mddev->recovery);
  1810. set_bit(R10BIO_WriteError, &r10_bio->state);
  1811. }
  1812. } else if (is_badblock(rdev,
  1813. r10_bio->devs[slot].addr,
  1814. r10_bio->sectors,
  1815. &first_bad, &bad_sectors))
  1816. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1817. rdev_dec_pending(rdev, mddev);
  1818. end_sync_request(r10_bio);
  1819. }
  1820. /*
  1821. * Note: sync and recover and handled very differently for raid10
  1822. * This code is for resync.
  1823. * For resync, we read through virtual addresses and read all blocks.
  1824. * If there is any error, we schedule a write. The lowest numbered
  1825. * drive is authoritative.
  1826. * However requests come for physical address, so we need to map.
  1827. * For every physical address there are raid_disks/copies virtual addresses,
  1828. * which is always are least one, but is not necessarly an integer.
  1829. * This means that a physical address can span multiple chunks, so we may
  1830. * have to submit multiple io requests for a single sync request.
  1831. */
  1832. /*
  1833. * We check if all blocks are in-sync and only write to blocks that
  1834. * aren't in sync
  1835. */
  1836. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1837. {
  1838. struct r10conf *conf = mddev->private;
  1839. int i, first;
  1840. struct bio *tbio, *fbio;
  1841. int vcnt;
  1842. struct page **tpages, **fpages;
  1843. atomic_set(&r10_bio->remaining, 1);
  1844. /* find the first device with a block */
  1845. for (i=0; i<conf->copies; i++)
  1846. if (!r10_bio->devs[i].bio->bi_status)
  1847. break;
  1848. if (i == conf->copies)
  1849. goto done;
  1850. first = i;
  1851. fbio = r10_bio->devs[i].bio;
  1852. fbio->bi_iter.bi_size = r10_bio->sectors << 9;
  1853. fbio->bi_iter.bi_idx = 0;
  1854. fpages = get_resync_pages(fbio)->pages;
  1855. vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
  1856. /* now find blocks with errors */
  1857. for (i=0 ; i < conf->copies ; i++) {
  1858. int j, d;
  1859. struct md_rdev *rdev;
  1860. struct resync_pages *rp;
  1861. tbio = r10_bio->devs[i].bio;
  1862. if (tbio->bi_end_io != end_sync_read)
  1863. continue;
  1864. if (i == first)
  1865. continue;
  1866. tpages = get_resync_pages(tbio)->pages;
  1867. d = r10_bio->devs[i].devnum;
  1868. rdev = conf->mirrors[d].rdev;
  1869. if (!r10_bio->devs[i].bio->bi_status) {
  1870. /* We know that the bi_io_vec layout is the same for
  1871. * both 'first' and 'i', so we just compare them.
  1872. * All vec entries are PAGE_SIZE;
  1873. */
  1874. int sectors = r10_bio->sectors;
  1875. for (j = 0; j < vcnt; j++) {
  1876. int len = PAGE_SIZE;
  1877. if (sectors < (len / 512))
  1878. len = sectors * 512;
  1879. if (memcmp(page_address(fpages[j]),
  1880. page_address(tpages[j]),
  1881. len))
  1882. break;
  1883. sectors -= len/512;
  1884. }
  1885. if (j == vcnt)
  1886. continue;
  1887. atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
  1888. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1889. /* Don't fix anything. */
  1890. continue;
  1891. } else if (test_bit(FailFast, &rdev->flags)) {
  1892. /* Just give up on this device */
  1893. md_error(rdev->mddev, rdev);
  1894. continue;
  1895. }
  1896. /* Ok, we need to write this bio, either to correct an
  1897. * inconsistency or to correct an unreadable block.
  1898. * First we need to fixup bv_offset, bv_len and
  1899. * bi_vecs, as the read request might have corrupted these
  1900. */
  1901. rp = get_resync_pages(tbio);
  1902. bio_reset(tbio);
  1903. md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
  1904. rp->raid_bio = r10_bio;
  1905. tbio->bi_private = rp;
  1906. tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
  1907. tbio->bi_end_io = end_sync_write;
  1908. bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
  1909. bio_copy_data(tbio, fbio);
  1910. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1911. atomic_inc(&r10_bio->remaining);
  1912. md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
  1913. if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
  1914. tbio->bi_opf |= MD_FAILFAST;
  1915. tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
  1916. bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
  1917. generic_make_request(tbio);
  1918. }
  1919. /* Now write out to any replacement devices
  1920. * that are active
  1921. */
  1922. for (i = 0; i < conf->copies; i++) {
  1923. int d;
  1924. tbio = r10_bio->devs[i].repl_bio;
  1925. if (!tbio || !tbio->bi_end_io)
  1926. continue;
  1927. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1928. && r10_bio->devs[i].bio != fbio)
  1929. bio_copy_data(tbio, fbio);
  1930. d = r10_bio->devs[i].devnum;
  1931. atomic_inc(&r10_bio->remaining);
  1932. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1933. bio_sectors(tbio));
  1934. generic_make_request(tbio);
  1935. }
  1936. done:
  1937. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1938. md_done_sync(mddev, r10_bio->sectors, 1);
  1939. put_buf(r10_bio);
  1940. }
  1941. }
  1942. /*
  1943. * Now for the recovery code.
  1944. * Recovery happens across physical sectors.
  1945. * We recover all non-is_sync drives by finding the virtual address of
  1946. * each, and then choose a working drive that also has that virt address.
  1947. * There is a separate r10_bio for each non-in_sync drive.
  1948. * Only the first two slots are in use. The first for reading,
  1949. * The second for writing.
  1950. *
  1951. */
  1952. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1953. {
  1954. /* We got a read error during recovery.
  1955. * We repeat the read in smaller page-sized sections.
  1956. * If a read succeeds, write it to the new device or record
  1957. * a bad block if we cannot.
  1958. * If a read fails, record a bad block on both old and
  1959. * new devices.
  1960. */
  1961. struct mddev *mddev = r10_bio->mddev;
  1962. struct r10conf *conf = mddev->private;
  1963. struct bio *bio = r10_bio->devs[0].bio;
  1964. sector_t sect = 0;
  1965. int sectors = r10_bio->sectors;
  1966. int idx = 0;
  1967. int dr = r10_bio->devs[0].devnum;
  1968. int dw = r10_bio->devs[1].devnum;
  1969. struct page **pages = get_resync_pages(bio)->pages;
  1970. while (sectors) {
  1971. int s = sectors;
  1972. struct md_rdev *rdev;
  1973. sector_t addr;
  1974. int ok;
  1975. if (s > (PAGE_SIZE>>9))
  1976. s = PAGE_SIZE >> 9;
  1977. rdev = conf->mirrors[dr].rdev;
  1978. addr = r10_bio->devs[0].addr + sect,
  1979. ok = sync_page_io(rdev,
  1980. addr,
  1981. s << 9,
  1982. pages[idx],
  1983. REQ_OP_READ, 0, false);
  1984. if (ok) {
  1985. rdev = conf->mirrors[dw].rdev;
  1986. addr = r10_bio->devs[1].addr + sect;
  1987. ok = sync_page_io(rdev,
  1988. addr,
  1989. s << 9,
  1990. pages[idx],
  1991. REQ_OP_WRITE, 0, false);
  1992. if (!ok) {
  1993. set_bit(WriteErrorSeen, &rdev->flags);
  1994. if (!test_and_set_bit(WantReplacement,
  1995. &rdev->flags))
  1996. set_bit(MD_RECOVERY_NEEDED,
  1997. &rdev->mddev->recovery);
  1998. }
  1999. }
  2000. if (!ok) {
  2001. /* We don't worry if we cannot set a bad block -
  2002. * it really is bad so there is no loss in not
  2003. * recording it yet
  2004. */
  2005. rdev_set_badblocks(rdev, addr, s, 0);
  2006. if (rdev != conf->mirrors[dw].rdev) {
  2007. /* need bad block on destination too */
  2008. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  2009. addr = r10_bio->devs[1].addr + sect;
  2010. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  2011. if (!ok) {
  2012. /* just abort the recovery */
  2013. pr_notice("md/raid10:%s: recovery aborted due to read error\n",
  2014. mdname(mddev));
  2015. conf->mirrors[dw].recovery_disabled
  2016. = mddev->recovery_disabled;
  2017. set_bit(MD_RECOVERY_INTR,
  2018. &mddev->recovery);
  2019. break;
  2020. }
  2021. }
  2022. }
  2023. sectors -= s;
  2024. sect += s;
  2025. idx++;
  2026. }
  2027. }
  2028. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  2029. {
  2030. struct r10conf *conf = mddev->private;
  2031. int d;
  2032. struct bio *wbio, *wbio2;
  2033. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  2034. fix_recovery_read_error(r10_bio);
  2035. end_sync_request(r10_bio);
  2036. return;
  2037. }
  2038. /*
  2039. * share the pages with the first bio
  2040. * and submit the write request
  2041. */
  2042. d = r10_bio->devs[1].devnum;
  2043. wbio = r10_bio->devs[1].bio;
  2044. wbio2 = r10_bio->devs[1].repl_bio;
  2045. /* Need to test wbio2->bi_end_io before we call
  2046. * generic_make_request as if the former is NULL,
  2047. * the latter is free to free wbio2.
  2048. */
  2049. if (wbio2 && !wbio2->bi_end_io)
  2050. wbio2 = NULL;
  2051. if (wbio->bi_end_io) {
  2052. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2053. md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
  2054. generic_make_request(wbio);
  2055. }
  2056. if (wbio2) {
  2057. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  2058. md_sync_acct(conf->mirrors[d].replacement->bdev,
  2059. bio_sectors(wbio2));
  2060. generic_make_request(wbio2);
  2061. }
  2062. }
  2063. /*
  2064. * Used by fix_read_error() to decay the per rdev read_errors.
  2065. * We halve the read error count for every hour that has elapsed
  2066. * since the last recorded read error.
  2067. *
  2068. */
  2069. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  2070. {
  2071. long cur_time_mon;
  2072. unsigned long hours_since_last;
  2073. unsigned int read_errors = atomic_read(&rdev->read_errors);
  2074. cur_time_mon = ktime_get_seconds();
  2075. if (rdev->last_read_error == 0) {
  2076. /* first time we've seen a read error */
  2077. rdev->last_read_error = cur_time_mon;
  2078. return;
  2079. }
  2080. hours_since_last = (long)(cur_time_mon -
  2081. rdev->last_read_error) / 3600;
  2082. rdev->last_read_error = cur_time_mon;
  2083. /*
  2084. * if hours_since_last is > the number of bits in read_errors
  2085. * just set read errors to 0. We do this to avoid
  2086. * overflowing the shift of read_errors by hours_since_last.
  2087. */
  2088. if (hours_since_last >= 8 * sizeof(read_errors))
  2089. atomic_set(&rdev->read_errors, 0);
  2090. else
  2091. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  2092. }
  2093. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  2094. int sectors, struct page *page, int rw)
  2095. {
  2096. sector_t first_bad;
  2097. int bad_sectors;
  2098. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  2099. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  2100. return -1;
  2101. if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
  2102. /* success */
  2103. return 1;
  2104. if (rw == WRITE) {
  2105. set_bit(WriteErrorSeen, &rdev->flags);
  2106. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  2107. set_bit(MD_RECOVERY_NEEDED,
  2108. &rdev->mddev->recovery);
  2109. }
  2110. /* need to record an error - either for the block or the device */
  2111. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  2112. md_error(rdev->mddev, rdev);
  2113. return 0;
  2114. }
  2115. /*
  2116. * This is a kernel thread which:
  2117. *
  2118. * 1. Retries failed read operations on working mirrors.
  2119. * 2. Updates the raid superblock when problems encounter.
  2120. * 3. Performs writes following reads for array synchronising.
  2121. */
  2122. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  2123. {
  2124. int sect = 0; /* Offset from r10_bio->sector */
  2125. int sectors = r10_bio->sectors;
  2126. struct md_rdev *rdev;
  2127. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  2128. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  2129. /* still own a reference to this rdev, so it cannot
  2130. * have been cleared recently.
  2131. */
  2132. rdev = conf->mirrors[d].rdev;
  2133. if (test_bit(Faulty, &rdev->flags))
  2134. /* drive has already been failed, just ignore any
  2135. more fix_read_error() attempts */
  2136. return;
  2137. check_decay_read_errors(mddev, rdev);
  2138. atomic_inc(&rdev->read_errors);
  2139. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  2140. char b[BDEVNAME_SIZE];
  2141. bdevname(rdev->bdev, b);
  2142. pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
  2143. mdname(mddev), b,
  2144. atomic_read(&rdev->read_errors), max_read_errors);
  2145. pr_notice("md/raid10:%s: %s: Failing raid device\n",
  2146. mdname(mddev), b);
  2147. md_error(mddev, rdev);
  2148. r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
  2149. return;
  2150. }
  2151. while(sectors) {
  2152. int s = sectors;
  2153. int sl = r10_bio->read_slot;
  2154. int success = 0;
  2155. int start;
  2156. if (s > (PAGE_SIZE>>9))
  2157. s = PAGE_SIZE >> 9;
  2158. rcu_read_lock();
  2159. do {
  2160. sector_t first_bad;
  2161. int bad_sectors;
  2162. d = r10_bio->devs[sl].devnum;
  2163. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2164. if (rdev &&
  2165. test_bit(In_sync, &rdev->flags) &&
  2166. !test_bit(Faulty, &rdev->flags) &&
  2167. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  2168. &first_bad, &bad_sectors) == 0) {
  2169. atomic_inc(&rdev->nr_pending);
  2170. rcu_read_unlock();
  2171. success = sync_page_io(rdev,
  2172. r10_bio->devs[sl].addr +
  2173. sect,
  2174. s<<9,
  2175. conf->tmppage,
  2176. REQ_OP_READ, 0, false);
  2177. rdev_dec_pending(rdev, mddev);
  2178. rcu_read_lock();
  2179. if (success)
  2180. break;
  2181. }
  2182. sl++;
  2183. if (sl == conf->copies)
  2184. sl = 0;
  2185. } while (!success && sl != r10_bio->read_slot);
  2186. rcu_read_unlock();
  2187. if (!success) {
  2188. /* Cannot read from anywhere, just mark the block
  2189. * as bad on the first device to discourage future
  2190. * reads.
  2191. */
  2192. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  2193. rdev = conf->mirrors[dn].rdev;
  2194. if (!rdev_set_badblocks(
  2195. rdev,
  2196. r10_bio->devs[r10_bio->read_slot].addr
  2197. + sect,
  2198. s, 0)) {
  2199. md_error(mddev, rdev);
  2200. r10_bio->devs[r10_bio->read_slot].bio
  2201. = IO_BLOCKED;
  2202. }
  2203. break;
  2204. }
  2205. start = sl;
  2206. /* write it back and re-read */
  2207. rcu_read_lock();
  2208. while (sl != r10_bio->read_slot) {
  2209. char b[BDEVNAME_SIZE];
  2210. if (sl==0)
  2211. sl = conf->copies;
  2212. sl--;
  2213. d = r10_bio->devs[sl].devnum;
  2214. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2215. if (!rdev ||
  2216. test_bit(Faulty, &rdev->flags) ||
  2217. !test_bit(In_sync, &rdev->flags))
  2218. continue;
  2219. atomic_inc(&rdev->nr_pending);
  2220. rcu_read_unlock();
  2221. if (r10_sync_page_io(rdev,
  2222. r10_bio->devs[sl].addr +
  2223. sect,
  2224. s, conf->tmppage, WRITE)
  2225. == 0) {
  2226. /* Well, this device is dead */
  2227. pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
  2228. mdname(mddev), s,
  2229. (unsigned long long)(
  2230. sect +
  2231. choose_data_offset(r10_bio,
  2232. rdev)),
  2233. bdevname(rdev->bdev, b));
  2234. pr_notice("md/raid10:%s: %s: failing drive\n",
  2235. mdname(mddev),
  2236. bdevname(rdev->bdev, b));
  2237. }
  2238. rdev_dec_pending(rdev, mddev);
  2239. rcu_read_lock();
  2240. }
  2241. sl = start;
  2242. while (sl != r10_bio->read_slot) {
  2243. char b[BDEVNAME_SIZE];
  2244. if (sl==0)
  2245. sl = conf->copies;
  2246. sl--;
  2247. d = r10_bio->devs[sl].devnum;
  2248. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2249. if (!rdev ||
  2250. test_bit(Faulty, &rdev->flags) ||
  2251. !test_bit(In_sync, &rdev->flags))
  2252. continue;
  2253. atomic_inc(&rdev->nr_pending);
  2254. rcu_read_unlock();
  2255. switch (r10_sync_page_io(rdev,
  2256. r10_bio->devs[sl].addr +
  2257. sect,
  2258. s, conf->tmppage,
  2259. READ)) {
  2260. case 0:
  2261. /* Well, this device is dead */
  2262. pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
  2263. mdname(mddev), s,
  2264. (unsigned long long)(
  2265. sect +
  2266. choose_data_offset(r10_bio, rdev)),
  2267. bdevname(rdev->bdev, b));
  2268. pr_notice("md/raid10:%s: %s: failing drive\n",
  2269. mdname(mddev),
  2270. bdevname(rdev->bdev, b));
  2271. break;
  2272. case 1:
  2273. pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
  2274. mdname(mddev), s,
  2275. (unsigned long long)(
  2276. sect +
  2277. choose_data_offset(r10_bio, rdev)),
  2278. bdevname(rdev->bdev, b));
  2279. atomic_add(s, &rdev->corrected_errors);
  2280. }
  2281. rdev_dec_pending(rdev, mddev);
  2282. rcu_read_lock();
  2283. }
  2284. rcu_read_unlock();
  2285. sectors -= s;
  2286. sect += s;
  2287. }
  2288. }
  2289. static int narrow_write_error(struct r10bio *r10_bio, int i)
  2290. {
  2291. struct bio *bio = r10_bio->master_bio;
  2292. struct mddev *mddev = r10_bio->mddev;
  2293. struct r10conf *conf = mddev->private;
  2294. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  2295. /* bio has the data to be written to slot 'i' where
  2296. * we just recently had a write error.
  2297. * We repeatedly clone the bio and trim down to one block,
  2298. * then try the write. Where the write fails we record
  2299. * a bad block.
  2300. * It is conceivable that the bio doesn't exactly align with
  2301. * blocks. We must handle this.
  2302. *
  2303. * We currently own a reference to the rdev.
  2304. */
  2305. int block_sectors;
  2306. sector_t sector;
  2307. int sectors;
  2308. int sect_to_write = r10_bio->sectors;
  2309. int ok = 1;
  2310. if (rdev->badblocks.shift < 0)
  2311. return 0;
  2312. block_sectors = roundup(1 << rdev->badblocks.shift,
  2313. bdev_logical_block_size(rdev->bdev) >> 9);
  2314. sector = r10_bio->sector;
  2315. sectors = ((r10_bio->sector + block_sectors)
  2316. & ~(sector_t)(block_sectors - 1))
  2317. - sector;
  2318. while (sect_to_write) {
  2319. struct bio *wbio;
  2320. sector_t wsector;
  2321. if (sectors > sect_to_write)
  2322. sectors = sect_to_write;
  2323. /* Write at 'sector' for 'sectors' */
  2324. wbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
  2325. bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
  2326. wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
  2327. wbio->bi_iter.bi_sector = wsector +
  2328. choose_data_offset(r10_bio, rdev);
  2329. bio_set_dev(wbio, rdev->bdev);
  2330. bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
  2331. if (submit_bio_wait(wbio) < 0)
  2332. /* Failure! */
  2333. ok = rdev_set_badblocks(rdev, wsector,
  2334. sectors, 0)
  2335. && ok;
  2336. bio_put(wbio);
  2337. sect_to_write -= sectors;
  2338. sector += sectors;
  2339. sectors = block_sectors;
  2340. }
  2341. return ok;
  2342. }
  2343. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2344. {
  2345. int slot = r10_bio->read_slot;
  2346. struct bio *bio;
  2347. struct r10conf *conf = mddev->private;
  2348. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2349. /* we got a read error. Maybe the drive is bad. Maybe just
  2350. * the block and we can fix it.
  2351. * We freeze all other IO, and try reading the block from
  2352. * other devices. When we find one, we re-write
  2353. * and check it that fixes the read error.
  2354. * This is all done synchronously while the array is
  2355. * frozen.
  2356. */
  2357. bio = r10_bio->devs[slot].bio;
  2358. bio_put(bio);
  2359. r10_bio->devs[slot].bio = NULL;
  2360. if (mddev->ro)
  2361. r10_bio->devs[slot].bio = IO_BLOCKED;
  2362. else if (!test_bit(FailFast, &rdev->flags)) {
  2363. freeze_array(conf, 1);
  2364. fix_read_error(conf, mddev, r10_bio);
  2365. unfreeze_array(conf);
  2366. } else
  2367. md_error(mddev, rdev);
  2368. rdev_dec_pending(rdev, mddev);
  2369. allow_barrier(conf);
  2370. r10_bio->state = 0;
  2371. raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
  2372. }
  2373. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2374. {
  2375. /* Some sort of write request has finished and it
  2376. * succeeded in writing where we thought there was a
  2377. * bad block. So forget the bad block.
  2378. * Or possibly if failed and we need to record
  2379. * a bad block.
  2380. */
  2381. int m;
  2382. struct md_rdev *rdev;
  2383. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2384. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2385. for (m = 0; m < conf->copies; m++) {
  2386. int dev = r10_bio->devs[m].devnum;
  2387. rdev = conf->mirrors[dev].rdev;
  2388. if (r10_bio->devs[m].bio == NULL ||
  2389. r10_bio->devs[m].bio->bi_end_io == NULL)
  2390. continue;
  2391. if (!r10_bio->devs[m].bio->bi_status) {
  2392. rdev_clear_badblocks(
  2393. rdev,
  2394. r10_bio->devs[m].addr,
  2395. r10_bio->sectors, 0);
  2396. } else {
  2397. if (!rdev_set_badblocks(
  2398. rdev,
  2399. r10_bio->devs[m].addr,
  2400. r10_bio->sectors, 0))
  2401. md_error(conf->mddev, rdev);
  2402. }
  2403. rdev = conf->mirrors[dev].replacement;
  2404. if (r10_bio->devs[m].repl_bio == NULL ||
  2405. r10_bio->devs[m].repl_bio->bi_end_io == NULL)
  2406. continue;
  2407. if (!r10_bio->devs[m].repl_bio->bi_status) {
  2408. rdev_clear_badblocks(
  2409. rdev,
  2410. r10_bio->devs[m].addr,
  2411. r10_bio->sectors, 0);
  2412. } else {
  2413. if (!rdev_set_badblocks(
  2414. rdev,
  2415. r10_bio->devs[m].addr,
  2416. r10_bio->sectors, 0))
  2417. md_error(conf->mddev, rdev);
  2418. }
  2419. }
  2420. put_buf(r10_bio);
  2421. } else {
  2422. bool fail = false;
  2423. for (m = 0; m < conf->copies; m++) {
  2424. int dev = r10_bio->devs[m].devnum;
  2425. struct bio *bio = r10_bio->devs[m].bio;
  2426. rdev = conf->mirrors[dev].rdev;
  2427. if (bio == IO_MADE_GOOD) {
  2428. rdev_clear_badblocks(
  2429. rdev,
  2430. r10_bio->devs[m].addr,
  2431. r10_bio->sectors, 0);
  2432. rdev_dec_pending(rdev, conf->mddev);
  2433. } else if (bio != NULL && bio->bi_status) {
  2434. fail = true;
  2435. if (!narrow_write_error(r10_bio, m)) {
  2436. md_error(conf->mddev, rdev);
  2437. set_bit(R10BIO_Degraded,
  2438. &r10_bio->state);
  2439. }
  2440. rdev_dec_pending(rdev, conf->mddev);
  2441. }
  2442. bio = r10_bio->devs[m].repl_bio;
  2443. rdev = conf->mirrors[dev].replacement;
  2444. if (rdev && bio == IO_MADE_GOOD) {
  2445. rdev_clear_badblocks(
  2446. rdev,
  2447. r10_bio->devs[m].addr,
  2448. r10_bio->sectors, 0);
  2449. rdev_dec_pending(rdev, conf->mddev);
  2450. }
  2451. }
  2452. if (fail) {
  2453. spin_lock_irq(&conf->device_lock);
  2454. list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
  2455. conf->nr_queued++;
  2456. spin_unlock_irq(&conf->device_lock);
  2457. /*
  2458. * In case freeze_array() is waiting for condition
  2459. * nr_pending == nr_queued + extra to be true.
  2460. */
  2461. wake_up(&conf->wait_barrier);
  2462. md_wakeup_thread(conf->mddev->thread);
  2463. } else {
  2464. if (test_bit(R10BIO_WriteError,
  2465. &r10_bio->state))
  2466. close_write(r10_bio);
  2467. raid_end_bio_io(r10_bio);
  2468. }
  2469. }
  2470. }
  2471. static void raid10d(struct md_thread *thread)
  2472. {
  2473. struct mddev *mddev = thread->mddev;
  2474. struct r10bio *r10_bio;
  2475. unsigned long flags;
  2476. struct r10conf *conf = mddev->private;
  2477. struct list_head *head = &conf->retry_list;
  2478. struct blk_plug plug;
  2479. md_check_recovery(mddev);
  2480. if (!list_empty_careful(&conf->bio_end_io_list) &&
  2481. !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
  2482. LIST_HEAD(tmp);
  2483. spin_lock_irqsave(&conf->device_lock, flags);
  2484. if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
  2485. while (!list_empty(&conf->bio_end_io_list)) {
  2486. list_move(conf->bio_end_io_list.prev, &tmp);
  2487. conf->nr_queued--;
  2488. }
  2489. }
  2490. spin_unlock_irqrestore(&conf->device_lock, flags);
  2491. while (!list_empty(&tmp)) {
  2492. r10_bio = list_first_entry(&tmp, struct r10bio,
  2493. retry_list);
  2494. list_del(&r10_bio->retry_list);
  2495. if (mddev->degraded)
  2496. set_bit(R10BIO_Degraded, &r10_bio->state);
  2497. if (test_bit(R10BIO_WriteError,
  2498. &r10_bio->state))
  2499. close_write(r10_bio);
  2500. raid_end_bio_io(r10_bio);
  2501. }
  2502. }
  2503. blk_start_plug(&plug);
  2504. for (;;) {
  2505. flush_pending_writes(conf);
  2506. spin_lock_irqsave(&conf->device_lock, flags);
  2507. if (list_empty(head)) {
  2508. spin_unlock_irqrestore(&conf->device_lock, flags);
  2509. break;
  2510. }
  2511. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2512. list_del(head->prev);
  2513. conf->nr_queued--;
  2514. spin_unlock_irqrestore(&conf->device_lock, flags);
  2515. mddev = r10_bio->mddev;
  2516. conf = mddev->private;
  2517. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2518. test_bit(R10BIO_WriteError, &r10_bio->state))
  2519. handle_write_completed(conf, r10_bio);
  2520. else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
  2521. reshape_request_write(mddev, r10_bio);
  2522. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2523. sync_request_write(mddev, r10_bio);
  2524. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2525. recovery_request_write(mddev, r10_bio);
  2526. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2527. handle_read_error(mddev, r10_bio);
  2528. else
  2529. WARN_ON_ONCE(1);
  2530. cond_resched();
  2531. if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
  2532. md_check_recovery(mddev);
  2533. }
  2534. blk_finish_plug(&plug);
  2535. }
  2536. static int init_resync(struct r10conf *conf)
  2537. {
  2538. int ret, buffs, i;
  2539. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2540. BUG_ON(mempool_initialized(&conf->r10buf_pool));
  2541. conf->have_replacement = 0;
  2542. for (i = 0; i < conf->geo.raid_disks; i++)
  2543. if (conf->mirrors[i].replacement)
  2544. conf->have_replacement = 1;
  2545. ret = mempool_init(&conf->r10buf_pool, buffs,
  2546. r10buf_pool_alloc, r10buf_pool_free, conf);
  2547. if (ret)
  2548. return ret;
  2549. conf->next_resync = 0;
  2550. return 0;
  2551. }
  2552. static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
  2553. {
  2554. struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
  2555. struct rsync_pages *rp;
  2556. struct bio *bio;
  2557. int nalloc;
  2558. int i;
  2559. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
  2560. test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
  2561. nalloc = conf->copies; /* resync */
  2562. else
  2563. nalloc = 2; /* recovery */
  2564. for (i = 0; i < nalloc; i++) {
  2565. bio = r10bio->devs[i].bio;
  2566. rp = bio->bi_private;
  2567. bio_reset(bio);
  2568. bio->bi_private = rp;
  2569. bio = r10bio->devs[i].repl_bio;
  2570. if (bio) {
  2571. rp = bio->bi_private;
  2572. bio_reset(bio);
  2573. bio->bi_private = rp;
  2574. }
  2575. }
  2576. return r10bio;
  2577. }
  2578. /*
  2579. * Set cluster_sync_high since we need other nodes to add the
  2580. * range [cluster_sync_low, cluster_sync_high] to suspend list.
  2581. */
  2582. static void raid10_set_cluster_sync_high(struct r10conf *conf)
  2583. {
  2584. sector_t window_size;
  2585. int extra_chunk, chunks;
  2586. /*
  2587. * First, here we define "stripe" as a unit which across
  2588. * all member devices one time, so we get chunks by use
  2589. * raid_disks / near_copies. Otherwise, if near_copies is
  2590. * close to raid_disks, then resync window could increases
  2591. * linearly with the increase of raid_disks, which means
  2592. * we will suspend a really large IO window while it is not
  2593. * necessary. If raid_disks is not divisible by near_copies,
  2594. * an extra chunk is needed to ensure the whole "stripe" is
  2595. * covered.
  2596. */
  2597. chunks = conf->geo.raid_disks / conf->geo.near_copies;
  2598. if (conf->geo.raid_disks % conf->geo.near_copies == 0)
  2599. extra_chunk = 0;
  2600. else
  2601. extra_chunk = 1;
  2602. window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
  2603. /*
  2604. * At least use a 32M window to align with raid1's resync window
  2605. */
  2606. window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
  2607. CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
  2608. conf->cluster_sync_high = conf->cluster_sync_low + window_size;
  2609. }
  2610. /*
  2611. * perform a "sync" on one "block"
  2612. *
  2613. * We need to make sure that no normal I/O request - particularly write
  2614. * requests - conflict with active sync requests.
  2615. *
  2616. * This is achieved by tracking pending requests and a 'barrier' concept
  2617. * that can be installed to exclude normal IO requests.
  2618. *
  2619. * Resync and recovery are handled very differently.
  2620. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2621. *
  2622. * For resync, we iterate over virtual addresses, read all copies,
  2623. * and update if there are differences. If only one copy is live,
  2624. * skip it.
  2625. * For recovery, we iterate over physical addresses, read a good
  2626. * value for each non-in_sync drive, and over-write.
  2627. *
  2628. * So, for recovery we may have several outstanding complex requests for a
  2629. * given address, one for each out-of-sync device. We model this by allocating
  2630. * a number of r10_bio structures, one for each out-of-sync device.
  2631. * As we setup these structures, we collect all bio's together into a list
  2632. * which we then process collectively to add pages, and then process again
  2633. * to pass to generic_make_request.
  2634. *
  2635. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2636. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2637. * has its remaining count decremented to 0, the whole complex operation
  2638. * is complete.
  2639. *
  2640. */
  2641. static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
  2642. int *skipped)
  2643. {
  2644. struct r10conf *conf = mddev->private;
  2645. struct r10bio *r10_bio;
  2646. struct bio *biolist = NULL, *bio;
  2647. sector_t max_sector, nr_sectors;
  2648. int i;
  2649. int max_sync;
  2650. sector_t sync_blocks;
  2651. sector_t sectors_skipped = 0;
  2652. int chunks_skipped = 0;
  2653. sector_t chunk_mask = conf->geo.chunk_mask;
  2654. int page_idx = 0;
  2655. if (!mempool_initialized(&conf->r10buf_pool))
  2656. if (init_resync(conf))
  2657. return 0;
  2658. /*
  2659. * Allow skipping a full rebuild for incremental assembly
  2660. * of a clean array, like RAID1 does.
  2661. */
  2662. if (mddev->bitmap == NULL &&
  2663. mddev->recovery_cp == MaxSector &&
  2664. mddev->reshape_position == MaxSector &&
  2665. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
  2666. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2667. !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  2668. conf->fullsync == 0) {
  2669. *skipped = 1;
  2670. return mddev->dev_sectors - sector_nr;
  2671. }
  2672. skipped:
  2673. max_sector = mddev->dev_sectors;
  2674. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
  2675. test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2676. max_sector = mddev->resync_max_sectors;
  2677. if (sector_nr >= max_sector) {
  2678. conf->cluster_sync_low = 0;
  2679. conf->cluster_sync_high = 0;
  2680. /* If we aborted, we need to abort the
  2681. * sync on the 'current' bitmap chucks (there can
  2682. * be several when recovering multiple devices).
  2683. * as we may have started syncing it but not finished.
  2684. * We can find the current address in
  2685. * mddev->curr_resync, but for recovery,
  2686. * we need to convert that to several
  2687. * virtual addresses.
  2688. */
  2689. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2690. end_reshape(conf);
  2691. close_sync(conf);
  2692. return 0;
  2693. }
  2694. if (mddev->curr_resync < max_sector) { /* aborted */
  2695. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2696. md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2697. &sync_blocks, 1);
  2698. else for (i = 0; i < conf->geo.raid_disks; i++) {
  2699. sector_t sect =
  2700. raid10_find_virt(conf, mddev->curr_resync, i);
  2701. md_bitmap_end_sync(mddev->bitmap, sect,
  2702. &sync_blocks, 1);
  2703. }
  2704. } else {
  2705. /* completed sync */
  2706. if ((!mddev->bitmap || conf->fullsync)
  2707. && conf->have_replacement
  2708. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2709. /* Completed a full sync so the replacements
  2710. * are now fully recovered.
  2711. */
  2712. rcu_read_lock();
  2713. for (i = 0; i < conf->geo.raid_disks; i++) {
  2714. struct md_rdev *rdev =
  2715. rcu_dereference(conf->mirrors[i].replacement);
  2716. if (rdev)
  2717. rdev->recovery_offset = MaxSector;
  2718. }
  2719. rcu_read_unlock();
  2720. }
  2721. conf->fullsync = 0;
  2722. }
  2723. md_bitmap_close_sync(mddev->bitmap);
  2724. close_sync(conf);
  2725. *skipped = 1;
  2726. return sectors_skipped;
  2727. }
  2728. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2729. return reshape_request(mddev, sector_nr, skipped);
  2730. if (chunks_skipped >= conf->geo.raid_disks) {
  2731. /* if there has been nothing to do on any drive,
  2732. * then there is nothing to do at all..
  2733. */
  2734. *skipped = 1;
  2735. return (max_sector - sector_nr) + sectors_skipped;
  2736. }
  2737. if (max_sector > mddev->resync_max)
  2738. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2739. /* make sure whole request will fit in a chunk - if chunks
  2740. * are meaningful
  2741. */
  2742. if (conf->geo.near_copies < conf->geo.raid_disks &&
  2743. max_sector > (sector_nr | chunk_mask))
  2744. max_sector = (sector_nr | chunk_mask) + 1;
  2745. /*
  2746. * If there is non-resync activity waiting for a turn, then let it
  2747. * though before starting on this new sync request.
  2748. */
  2749. if (conf->nr_waiting)
  2750. schedule_timeout_uninterruptible(1);
  2751. /* Again, very different code for resync and recovery.
  2752. * Both must result in an r10bio with a list of bios that
  2753. * have bi_end_io, bi_sector, bi_disk set,
  2754. * and bi_private set to the r10bio.
  2755. * For recovery, we may actually create several r10bios
  2756. * with 2 bios in each, that correspond to the bios in the main one.
  2757. * In this case, the subordinate r10bios link back through a
  2758. * borrowed master_bio pointer, and the counter in the master
  2759. * includes a ref from each subordinate.
  2760. */
  2761. /* First, we decide what to do and set ->bi_end_io
  2762. * To end_sync_read if we want to read, and
  2763. * end_sync_write if we will want to write.
  2764. */
  2765. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2766. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2767. /* recovery... the complicated one */
  2768. int j;
  2769. r10_bio = NULL;
  2770. for (i = 0 ; i < conf->geo.raid_disks; i++) {
  2771. int still_degraded;
  2772. struct r10bio *rb2;
  2773. sector_t sect;
  2774. int must_sync;
  2775. int any_working;
  2776. struct raid10_info *mirror = &conf->mirrors[i];
  2777. struct md_rdev *mrdev, *mreplace;
  2778. rcu_read_lock();
  2779. mrdev = rcu_dereference(mirror->rdev);
  2780. mreplace = rcu_dereference(mirror->replacement);
  2781. if ((mrdev == NULL ||
  2782. test_bit(Faulty, &mrdev->flags) ||
  2783. test_bit(In_sync, &mrdev->flags)) &&
  2784. (mreplace == NULL ||
  2785. test_bit(Faulty, &mreplace->flags))) {
  2786. rcu_read_unlock();
  2787. continue;
  2788. }
  2789. still_degraded = 0;
  2790. /* want to reconstruct this device */
  2791. rb2 = r10_bio;
  2792. sect = raid10_find_virt(conf, sector_nr, i);
  2793. if (sect >= mddev->resync_max_sectors) {
  2794. /* last stripe is not complete - don't
  2795. * try to recover this sector.
  2796. */
  2797. rcu_read_unlock();
  2798. continue;
  2799. }
  2800. if (mreplace && test_bit(Faulty, &mreplace->flags))
  2801. mreplace = NULL;
  2802. /* Unless we are doing a full sync, or a replacement
  2803. * we only need to recover the block if it is set in
  2804. * the bitmap
  2805. */
  2806. must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
  2807. &sync_blocks, 1);
  2808. if (sync_blocks < max_sync)
  2809. max_sync = sync_blocks;
  2810. if (!must_sync &&
  2811. mreplace == NULL &&
  2812. !conf->fullsync) {
  2813. /* yep, skip the sync_blocks here, but don't assume
  2814. * that there will never be anything to do here
  2815. */
  2816. chunks_skipped = -1;
  2817. rcu_read_unlock();
  2818. continue;
  2819. }
  2820. atomic_inc(&mrdev->nr_pending);
  2821. if (mreplace)
  2822. atomic_inc(&mreplace->nr_pending);
  2823. rcu_read_unlock();
  2824. r10_bio = raid10_alloc_init_r10buf(conf);
  2825. r10_bio->state = 0;
  2826. raise_barrier(conf, rb2 != NULL);
  2827. atomic_set(&r10_bio->remaining, 0);
  2828. r10_bio->master_bio = (struct bio*)rb2;
  2829. if (rb2)
  2830. atomic_inc(&rb2->remaining);
  2831. r10_bio->mddev = mddev;
  2832. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2833. r10_bio->sector = sect;
  2834. raid10_find_phys(conf, r10_bio);
  2835. /* Need to check if the array will still be
  2836. * degraded
  2837. */
  2838. rcu_read_lock();
  2839. for (j = 0; j < conf->geo.raid_disks; j++) {
  2840. struct md_rdev *rdev = rcu_dereference(
  2841. conf->mirrors[j].rdev);
  2842. if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
  2843. still_degraded = 1;
  2844. break;
  2845. }
  2846. }
  2847. must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
  2848. &sync_blocks, still_degraded);
  2849. any_working = 0;
  2850. for (j=0; j<conf->copies;j++) {
  2851. int k;
  2852. int d = r10_bio->devs[j].devnum;
  2853. sector_t from_addr, to_addr;
  2854. struct md_rdev *rdev =
  2855. rcu_dereference(conf->mirrors[d].rdev);
  2856. sector_t sector, first_bad;
  2857. int bad_sectors;
  2858. if (!rdev ||
  2859. !test_bit(In_sync, &rdev->flags))
  2860. continue;
  2861. /* This is where we read from */
  2862. any_working = 1;
  2863. sector = r10_bio->devs[j].addr;
  2864. if (is_badblock(rdev, sector, max_sync,
  2865. &first_bad, &bad_sectors)) {
  2866. if (first_bad > sector)
  2867. max_sync = first_bad - sector;
  2868. else {
  2869. bad_sectors -= (sector
  2870. - first_bad);
  2871. if (max_sync > bad_sectors)
  2872. max_sync = bad_sectors;
  2873. continue;
  2874. }
  2875. }
  2876. bio = r10_bio->devs[0].bio;
  2877. bio->bi_next = biolist;
  2878. biolist = bio;
  2879. bio->bi_end_io = end_sync_read;
  2880. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  2881. if (test_bit(FailFast, &rdev->flags))
  2882. bio->bi_opf |= MD_FAILFAST;
  2883. from_addr = r10_bio->devs[j].addr;
  2884. bio->bi_iter.bi_sector = from_addr +
  2885. rdev->data_offset;
  2886. bio_set_dev(bio, rdev->bdev);
  2887. atomic_inc(&rdev->nr_pending);
  2888. /* and we write to 'i' (if not in_sync) */
  2889. for (k=0; k<conf->copies; k++)
  2890. if (r10_bio->devs[k].devnum == i)
  2891. break;
  2892. BUG_ON(k == conf->copies);
  2893. to_addr = r10_bio->devs[k].addr;
  2894. r10_bio->devs[0].devnum = d;
  2895. r10_bio->devs[0].addr = from_addr;
  2896. r10_bio->devs[1].devnum = i;
  2897. r10_bio->devs[1].addr = to_addr;
  2898. if (!test_bit(In_sync, &mrdev->flags)) {
  2899. bio = r10_bio->devs[1].bio;
  2900. bio->bi_next = biolist;
  2901. biolist = bio;
  2902. bio->bi_end_io = end_sync_write;
  2903. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  2904. bio->bi_iter.bi_sector = to_addr
  2905. + mrdev->data_offset;
  2906. bio_set_dev(bio, mrdev->bdev);
  2907. atomic_inc(&r10_bio->remaining);
  2908. } else
  2909. r10_bio->devs[1].bio->bi_end_io = NULL;
  2910. /* and maybe write to replacement */
  2911. bio = r10_bio->devs[1].repl_bio;
  2912. if (bio)
  2913. bio->bi_end_io = NULL;
  2914. /* Note: if mreplace != NULL, then bio
  2915. * cannot be NULL as r10buf_pool_alloc will
  2916. * have allocated it.
  2917. * So the second test here is pointless.
  2918. * But it keeps semantic-checkers happy, and
  2919. * this comment keeps human reviewers
  2920. * happy.
  2921. */
  2922. if (mreplace == NULL || bio == NULL ||
  2923. test_bit(Faulty, &mreplace->flags))
  2924. break;
  2925. bio->bi_next = biolist;
  2926. biolist = bio;
  2927. bio->bi_end_io = end_sync_write;
  2928. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  2929. bio->bi_iter.bi_sector = to_addr +
  2930. mreplace->data_offset;
  2931. bio_set_dev(bio, mreplace->bdev);
  2932. atomic_inc(&r10_bio->remaining);
  2933. break;
  2934. }
  2935. rcu_read_unlock();
  2936. if (j == conf->copies) {
  2937. /* Cannot recover, so abort the recovery or
  2938. * record a bad block */
  2939. if (any_working) {
  2940. /* problem is that there are bad blocks
  2941. * on other device(s)
  2942. */
  2943. int k;
  2944. for (k = 0; k < conf->copies; k++)
  2945. if (r10_bio->devs[k].devnum == i)
  2946. break;
  2947. if (!test_bit(In_sync,
  2948. &mrdev->flags)
  2949. && !rdev_set_badblocks(
  2950. mrdev,
  2951. r10_bio->devs[k].addr,
  2952. max_sync, 0))
  2953. any_working = 0;
  2954. if (mreplace &&
  2955. !rdev_set_badblocks(
  2956. mreplace,
  2957. r10_bio->devs[k].addr,
  2958. max_sync, 0))
  2959. any_working = 0;
  2960. }
  2961. if (!any_working) {
  2962. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2963. &mddev->recovery))
  2964. pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
  2965. mdname(mddev));
  2966. mirror->recovery_disabled
  2967. = mddev->recovery_disabled;
  2968. }
  2969. put_buf(r10_bio);
  2970. if (rb2)
  2971. atomic_dec(&rb2->remaining);
  2972. r10_bio = rb2;
  2973. rdev_dec_pending(mrdev, mddev);
  2974. if (mreplace)
  2975. rdev_dec_pending(mreplace, mddev);
  2976. break;
  2977. }
  2978. rdev_dec_pending(mrdev, mddev);
  2979. if (mreplace)
  2980. rdev_dec_pending(mreplace, mddev);
  2981. if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
  2982. /* Only want this if there is elsewhere to
  2983. * read from. 'j' is currently the first
  2984. * readable copy.
  2985. */
  2986. int targets = 1;
  2987. for (; j < conf->copies; j++) {
  2988. int d = r10_bio->devs[j].devnum;
  2989. if (conf->mirrors[d].rdev &&
  2990. test_bit(In_sync,
  2991. &conf->mirrors[d].rdev->flags))
  2992. targets++;
  2993. }
  2994. if (targets == 1)
  2995. r10_bio->devs[0].bio->bi_opf
  2996. &= ~MD_FAILFAST;
  2997. }
  2998. }
  2999. if (biolist == NULL) {
  3000. while (r10_bio) {
  3001. struct r10bio *rb2 = r10_bio;
  3002. r10_bio = (struct r10bio*) rb2->master_bio;
  3003. rb2->master_bio = NULL;
  3004. put_buf(rb2);
  3005. }
  3006. goto giveup;
  3007. }
  3008. } else {
  3009. /* resync. Schedule a read for every block at this virt offset */
  3010. int count = 0;
  3011. /*
  3012. * Since curr_resync_completed could probably not update in
  3013. * time, and we will set cluster_sync_low based on it.
  3014. * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
  3015. * safety reason, which ensures curr_resync_completed is
  3016. * updated in bitmap_cond_end_sync.
  3017. */
  3018. md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
  3019. mddev_is_clustered(mddev) &&
  3020. (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
  3021. if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
  3022. &sync_blocks, mddev->degraded) &&
  3023. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  3024. &mddev->recovery)) {
  3025. /* We can skip this block */
  3026. *skipped = 1;
  3027. return sync_blocks + sectors_skipped;
  3028. }
  3029. if (sync_blocks < max_sync)
  3030. max_sync = sync_blocks;
  3031. r10_bio = raid10_alloc_init_r10buf(conf);
  3032. r10_bio->state = 0;
  3033. r10_bio->mddev = mddev;
  3034. atomic_set(&r10_bio->remaining, 0);
  3035. raise_barrier(conf, 0);
  3036. conf->next_resync = sector_nr;
  3037. r10_bio->master_bio = NULL;
  3038. r10_bio->sector = sector_nr;
  3039. set_bit(R10BIO_IsSync, &r10_bio->state);
  3040. raid10_find_phys(conf, r10_bio);
  3041. r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
  3042. for (i = 0; i < conf->copies; i++) {
  3043. int d = r10_bio->devs[i].devnum;
  3044. sector_t first_bad, sector;
  3045. int bad_sectors;
  3046. struct md_rdev *rdev;
  3047. if (r10_bio->devs[i].repl_bio)
  3048. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  3049. bio = r10_bio->devs[i].bio;
  3050. bio->bi_status = BLK_STS_IOERR;
  3051. rcu_read_lock();
  3052. rdev = rcu_dereference(conf->mirrors[d].rdev);
  3053. if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
  3054. rcu_read_unlock();
  3055. continue;
  3056. }
  3057. sector = r10_bio->devs[i].addr;
  3058. if (is_badblock(rdev, sector, max_sync,
  3059. &first_bad, &bad_sectors)) {
  3060. if (first_bad > sector)
  3061. max_sync = first_bad - sector;
  3062. else {
  3063. bad_sectors -= (sector - first_bad);
  3064. if (max_sync > bad_sectors)
  3065. max_sync = bad_sectors;
  3066. rcu_read_unlock();
  3067. continue;
  3068. }
  3069. }
  3070. atomic_inc(&rdev->nr_pending);
  3071. atomic_inc(&r10_bio->remaining);
  3072. bio->bi_next = biolist;
  3073. biolist = bio;
  3074. bio->bi_end_io = end_sync_read;
  3075. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  3076. if (test_bit(FailFast, &rdev->flags))
  3077. bio->bi_opf |= MD_FAILFAST;
  3078. bio->bi_iter.bi_sector = sector + rdev->data_offset;
  3079. bio_set_dev(bio, rdev->bdev);
  3080. count++;
  3081. rdev = rcu_dereference(conf->mirrors[d].replacement);
  3082. if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
  3083. rcu_read_unlock();
  3084. continue;
  3085. }
  3086. atomic_inc(&rdev->nr_pending);
  3087. /* Need to set up for writing to the replacement */
  3088. bio = r10_bio->devs[i].repl_bio;
  3089. bio->bi_status = BLK_STS_IOERR;
  3090. sector = r10_bio->devs[i].addr;
  3091. bio->bi_next = biolist;
  3092. biolist = bio;
  3093. bio->bi_end_io = end_sync_write;
  3094. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  3095. if (test_bit(FailFast, &rdev->flags))
  3096. bio->bi_opf |= MD_FAILFAST;
  3097. bio->bi_iter.bi_sector = sector + rdev->data_offset;
  3098. bio_set_dev(bio, rdev->bdev);
  3099. count++;
  3100. rcu_read_unlock();
  3101. }
  3102. if (count < 2) {
  3103. for (i=0; i<conf->copies; i++) {
  3104. int d = r10_bio->devs[i].devnum;
  3105. if (r10_bio->devs[i].bio->bi_end_io)
  3106. rdev_dec_pending(conf->mirrors[d].rdev,
  3107. mddev);
  3108. if (r10_bio->devs[i].repl_bio &&
  3109. r10_bio->devs[i].repl_bio->bi_end_io)
  3110. rdev_dec_pending(
  3111. conf->mirrors[d].replacement,
  3112. mddev);
  3113. }
  3114. put_buf(r10_bio);
  3115. biolist = NULL;
  3116. goto giveup;
  3117. }
  3118. }
  3119. nr_sectors = 0;
  3120. if (sector_nr + max_sync < max_sector)
  3121. max_sector = sector_nr + max_sync;
  3122. do {
  3123. struct page *page;
  3124. int len = PAGE_SIZE;
  3125. if (sector_nr + (len>>9) > max_sector)
  3126. len = (max_sector - sector_nr) << 9;
  3127. if (len == 0)
  3128. break;
  3129. for (bio= biolist ; bio ; bio=bio->bi_next) {
  3130. struct resync_pages *rp = get_resync_pages(bio);
  3131. page = resync_fetch_page(rp, page_idx);
  3132. /*
  3133. * won't fail because the vec table is big enough
  3134. * to hold all these pages
  3135. */
  3136. bio_add_page(bio, page, len, 0);
  3137. }
  3138. nr_sectors += len>>9;
  3139. sector_nr += len>>9;
  3140. } while (++page_idx < RESYNC_PAGES);
  3141. r10_bio->sectors = nr_sectors;
  3142. if (mddev_is_clustered(mddev) &&
  3143. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3144. /* It is resync not recovery */
  3145. if (conf->cluster_sync_high < sector_nr + nr_sectors) {
  3146. conf->cluster_sync_low = mddev->curr_resync_completed;
  3147. raid10_set_cluster_sync_high(conf);
  3148. /* Send resync message */
  3149. md_cluster_ops->resync_info_update(mddev,
  3150. conf->cluster_sync_low,
  3151. conf->cluster_sync_high);
  3152. }
  3153. } else if (mddev_is_clustered(mddev)) {
  3154. /* This is recovery not resync */
  3155. sector_t sect_va1, sect_va2;
  3156. bool broadcast_msg = false;
  3157. for (i = 0; i < conf->geo.raid_disks; i++) {
  3158. /*
  3159. * sector_nr is a device address for recovery, so we
  3160. * need translate it to array address before compare
  3161. * with cluster_sync_high.
  3162. */
  3163. sect_va1 = raid10_find_virt(conf, sector_nr, i);
  3164. if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
  3165. broadcast_msg = true;
  3166. /*
  3167. * curr_resync_completed is similar as
  3168. * sector_nr, so make the translation too.
  3169. */
  3170. sect_va2 = raid10_find_virt(conf,
  3171. mddev->curr_resync_completed, i);
  3172. if (conf->cluster_sync_low == 0 ||
  3173. conf->cluster_sync_low > sect_va2)
  3174. conf->cluster_sync_low = sect_va2;
  3175. }
  3176. }
  3177. if (broadcast_msg) {
  3178. raid10_set_cluster_sync_high(conf);
  3179. md_cluster_ops->resync_info_update(mddev,
  3180. conf->cluster_sync_low,
  3181. conf->cluster_sync_high);
  3182. }
  3183. }
  3184. while (biolist) {
  3185. bio = biolist;
  3186. biolist = biolist->bi_next;
  3187. bio->bi_next = NULL;
  3188. r10_bio = get_resync_r10bio(bio);
  3189. r10_bio->sectors = nr_sectors;
  3190. if (bio->bi_end_io == end_sync_read) {
  3191. md_sync_acct_bio(bio, nr_sectors);
  3192. bio->bi_status = 0;
  3193. generic_make_request(bio);
  3194. }
  3195. }
  3196. if (sectors_skipped)
  3197. /* pretend they weren't skipped, it makes
  3198. * no important difference in this case
  3199. */
  3200. md_done_sync(mddev, sectors_skipped, 1);
  3201. return sectors_skipped + nr_sectors;
  3202. giveup:
  3203. /* There is nowhere to write, so all non-sync
  3204. * drives must be failed or in resync, all drives
  3205. * have a bad block, so try the next chunk...
  3206. */
  3207. if (sector_nr + max_sync < max_sector)
  3208. max_sector = sector_nr + max_sync;
  3209. sectors_skipped += (max_sector - sector_nr);
  3210. chunks_skipped ++;
  3211. sector_nr = max_sector;
  3212. goto skipped;
  3213. }
  3214. static sector_t
  3215. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  3216. {
  3217. sector_t size;
  3218. struct r10conf *conf = mddev->private;
  3219. if (!raid_disks)
  3220. raid_disks = min(conf->geo.raid_disks,
  3221. conf->prev.raid_disks);
  3222. if (!sectors)
  3223. sectors = conf->dev_sectors;
  3224. size = sectors >> conf->geo.chunk_shift;
  3225. sector_div(size, conf->geo.far_copies);
  3226. size = size * raid_disks;
  3227. sector_div(size, conf->geo.near_copies);
  3228. return size << conf->geo.chunk_shift;
  3229. }
  3230. static void calc_sectors(struct r10conf *conf, sector_t size)
  3231. {
  3232. /* Calculate the number of sectors-per-device that will
  3233. * actually be used, and set conf->dev_sectors and
  3234. * conf->stride
  3235. */
  3236. size = size >> conf->geo.chunk_shift;
  3237. sector_div(size, conf->geo.far_copies);
  3238. size = size * conf->geo.raid_disks;
  3239. sector_div(size, conf->geo.near_copies);
  3240. /* 'size' is now the number of chunks in the array */
  3241. /* calculate "used chunks per device" */
  3242. size = size * conf->copies;
  3243. /* We need to round up when dividing by raid_disks to
  3244. * get the stride size.
  3245. */
  3246. size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
  3247. conf->dev_sectors = size << conf->geo.chunk_shift;
  3248. if (conf->geo.far_offset)
  3249. conf->geo.stride = 1 << conf->geo.chunk_shift;
  3250. else {
  3251. sector_div(size, conf->geo.far_copies);
  3252. conf->geo.stride = size << conf->geo.chunk_shift;
  3253. }
  3254. }
  3255. enum geo_type {geo_new, geo_old, geo_start};
  3256. static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
  3257. {
  3258. int nc, fc, fo;
  3259. int layout, chunk, disks;
  3260. switch (new) {
  3261. case geo_old:
  3262. layout = mddev->layout;
  3263. chunk = mddev->chunk_sectors;
  3264. disks = mddev->raid_disks - mddev->delta_disks;
  3265. break;
  3266. case geo_new:
  3267. layout = mddev->new_layout;
  3268. chunk = mddev->new_chunk_sectors;
  3269. disks = mddev->raid_disks;
  3270. break;
  3271. default: /* avoid 'may be unused' warnings */
  3272. case geo_start: /* new when starting reshape - raid_disks not
  3273. * updated yet. */
  3274. layout = mddev->new_layout;
  3275. chunk = mddev->new_chunk_sectors;
  3276. disks = mddev->raid_disks + mddev->delta_disks;
  3277. break;
  3278. }
  3279. if (layout >> 19)
  3280. return -1;
  3281. if (chunk < (PAGE_SIZE >> 9) ||
  3282. !is_power_of_2(chunk))
  3283. return -2;
  3284. nc = layout & 255;
  3285. fc = (layout >> 8) & 255;
  3286. fo = layout & (1<<16);
  3287. geo->raid_disks = disks;
  3288. geo->near_copies = nc;
  3289. geo->far_copies = fc;
  3290. geo->far_offset = fo;
  3291. switch (layout >> 17) {
  3292. case 0: /* original layout. simple but not always optimal */
  3293. geo->far_set_size = disks;
  3294. break;
  3295. case 1: /* "improved" layout which was buggy. Hopefully no-one is
  3296. * actually using this, but leave code here just in case.*/
  3297. geo->far_set_size = disks/fc;
  3298. WARN(geo->far_set_size < fc,
  3299. "This RAID10 layout does not provide data safety - please backup and create new array\n");
  3300. break;
  3301. case 2: /* "improved" layout fixed to match documentation */
  3302. geo->far_set_size = fc * nc;
  3303. break;
  3304. default: /* Not a valid layout */
  3305. return -1;
  3306. }
  3307. geo->chunk_mask = chunk - 1;
  3308. geo->chunk_shift = ffz(~chunk);
  3309. return nc*fc;
  3310. }
  3311. static struct r10conf *setup_conf(struct mddev *mddev)
  3312. {
  3313. struct r10conf *conf = NULL;
  3314. int err = -EINVAL;
  3315. struct geom geo;
  3316. int copies;
  3317. copies = setup_geo(&geo, mddev, geo_new);
  3318. if (copies == -2) {
  3319. pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
  3320. mdname(mddev), PAGE_SIZE);
  3321. goto out;
  3322. }
  3323. if (copies < 2 || copies > mddev->raid_disks) {
  3324. pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  3325. mdname(mddev), mddev->new_layout);
  3326. goto out;
  3327. }
  3328. err = -ENOMEM;
  3329. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  3330. if (!conf)
  3331. goto out;
  3332. /* FIXME calc properly */
  3333. conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
  3334. sizeof(struct raid10_info),
  3335. GFP_KERNEL);
  3336. if (!conf->mirrors)
  3337. goto out;
  3338. conf->tmppage = alloc_page(GFP_KERNEL);
  3339. if (!conf->tmppage)
  3340. goto out;
  3341. conf->geo = geo;
  3342. conf->copies = copies;
  3343. err = mempool_init(&conf->r10bio_pool, NR_RAID10_BIOS, r10bio_pool_alloc,
  3344. r10bio_pool_free, conf);
  3345. if (err)
  3346. goto out;
  3347. err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
  3348. if (err)
  3349. goto out;
  3350. calc_sectors(conf, mddev->dev_sectors);
  3351. if (mddev->reshape_position == MaxSector) {
  3352. conf->prev = conf->geo;
  3353. conf->reshape_progress = MaxSector;
  3354. } else {
  3355. if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
  3356. err = -EINVAL;
  3357. goto out;
  3358. }
  3359. conf->reshape_progress = mddev->reshape_position;
  3360. if (conf->prev.far_offset)
  3361. conf->prev.stride = 1 << conf->prev.chunk_shift;
  3362. else
  3363. /* far_copies must be 1 */
  3364. conf->prev.stride = conf->dev_sectors;
  3365. }
  3366. conf->reshape_safe = conf->reshape_progress;
  3367. spin_lock_init(&conf->device_lock);
  3368. INIT_LIST_HEAD(&conf->retry_list);
  3369. INIT_LIST_HEAD(&conf->bio_end_io_list);
  3370. spin_lock_init(&conf->resync_lock);
  3371. init_waitqueue_head(&conf->wait_barrier);
  3372. atomic_set(&conf->nr_pending, 0);
  3373. err = -ENOMEM;
  3374. conf->thread = md_register_thread(raid10d, mddev, "raid10");
  3375. if (!conf->thread)
  3376. goto out;
  3377. conf->mddev = mddev;
  3378. return conf;
  3379. out:
  3380. if (conf) {
  3381. mempool_exit(&conf->r10bio_pool);
  3382. kfree(conf->mirrors);
  3383. safe_put_page(conf->tmppage);
  3384. bioset_exit(&conf->bio_split);
  3385. kfree(conf);
  3386. }
  3387. return ERR_PTR(err);
  3388. }
  3389. static int raid10_run(struct mddev *mddev)
  3390. {
  3391. struct r10conf *conf;
  3392. int i, disk_idx, chunk_size;
  3393. struct raid10_info *disk;
  3394. struct md_rdev *rdev;
  3395. sector_t size;
  3396. sector_t min_offset_diff = 0;
  3397. int first = 1;
  3398. bool discard_supported = false;
  3399. if (mddev_init_writes_pending(mddev) < 0)
  3400. return -ENOMEM;
  3401. if (mddev->private == NULL) {
  3402. conf = setup_conf(mddev);
  3403. if (IS_ERR(conf))
  3404. return PTR_ERR(conf);
  3405. mddev->private = conf;
  3406. }
  3407. conf = mddev->private;
  3408. if (!conf)
  3409. goto out;
  3410. if (mddev_is_clustered(conf->mddev)) {
  3411. int fc, fo;
  3412. fc = (mddev->layout >> 8) & 255;
  3413. fo = mddev->layout & (1<<16);
  3414. if (fc > 1 || fo > 0) {
  3415. pr_err("only near layout is supported by clustered"
  3416. " raid10\n");
  3417. goto out_free_conf;
  3418. }
  3419. }
  3420. mddev->thread = conf->thread;
  3421. conf->thread = NULL;
  3422. chunk_size = mddev->chunk_sectors << 9;
  3423. if (mddev->queue) {
  3424. blk_queue_max_discard_sectors(mddev->queue,
  3425. mddev->chunk_sectors);
  3426. blk_queue_max_write_same_sectors(mddev->queue, 0);
  3427. blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
  3428. blk_queue_io_min(mddev->queue, chunk_size);
  3429. if (conf->geo.raid_disks % conf->geo.near_copies)
  3430. blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
  3431. else
  3432. blk_queue_io_opt(mddev->queue, chunk_size *
  3433. (conf->geo.raid_disks / conf->geo.near_copies));
  3434. }
  3435. rdev_for_each(rdev, mddev) {
  3436. long long diff;
  3437. disk_idx = rdev->raid_disk;
  3438. if (disk_idx < 0)
  3439. continue;
  3440. if (disk_idx >= conf->geo.raid_disks &&
  3441. disk_idx >= conf->prev.raid_disks)
  3442. continue;
  3443. disk = conf->mirrors + disk_idx;
  3444. if (test_bit(Replacement, &rdev->flags)) {
  3445. if (disk->replacement)
  3446. goto out_free_conf;
  3447. disk->replacement = rdev;
  3448. } else {
  3449. if (disk->rdev)
  3450. goto out_free_conf;
  3451. disk->rdev = rdev;
  3452. }
  3453. diff = (rdev->new_data_offset - rdev->data_offset);
  3454. if (!mddev->reshape_backwards)
  3455. diff = -diff;
  3456. if (diff < 0)
  3457. diff = 0;
  3458. if (first || diff < min_offset_diff)
  3459. min_offset_diff = diff;
  3460. if (mddev->gendisk)
  3461. disk_stack_limits(mddev->gendisk, rdev->bdev,
  3462. rdev->data_offset << 9);
  3463. disk->head_position = 0;
  3464. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  3465. discard_supported = true;
  3466. first = 0;
  3467. }
  3468. if (mddev->queue) {
  3469. if (discard_supported)
  3470. blk_queue_flag_set(QUEUE_FLAG_DISCARD,
  3471. mddev->queue);
  3472. else
  3473. blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
  3474. mddev->queue);
  3475. }
  3476. /* need to check that every block has at least one working mirror */
  3477. if (!enough(conf, -1)) {
  3478. pr_err("md/raid10:%s: not enough operational mirrors.\n",
  3479. mdname(mddev));
  3480. goto out_free_conf;
  3481. }
  3482. if (conf->reshape_progress != MaxSector) {
  3483. /* must ensure that shape change is supported */
  3484. if (conf->geo.far_copies != 1 &&
  3485. conf->geo.far_offset == 0)
  3486. goto out_free_conf;
  3487. if (conf->prev.far_copies != 1 &&
  3488. conf->prev.far_offset == 0)
  3489. goto out_free_conf;
  3490. }
  3491. mddev->degraded = 0;
  3492. for (i = 0;
  3493. i < conf->geo.raid_disks
  3494. || i < conf->prev.raid_disks;
  3495. i++) {
  3496. disk = conf->mirrors + i;
  3497. if (!disk->rdev && disk->replacement) {
  3498. /* The replacement is all we have - use it */
  3499. disk->rdev = disk->replacement;
  3500. disk->replacement = NULL;
  3501. clear_bit(Replacement, &disk->rdev->flags);
  3502. }
  3503. if (!disk->rdev ||
  3504. !test_bit(In_sync, &disk->rdev->flags)) {
  3505. disk->head_position = 0;
  3506. mddev->degraded++;
  3507. if (disk->rdev &&
  3508. disk->rdev->saved_raid_disk < 0)
  3509. conf->fullsync = 1;
  3510. }
  3511. if (disk->replacement &&
  3512. !test_bit(In_sync, &disk->replacement->flags) &&
  3513. disk->replacement->saved_raid_disk < 0) {
  3514. conf->fullsync = 1;
  3515. }
  3516. disk->recovery_disabled = mddev->recovery_disabled - 1;
  3517. }
  3518. if (mddev->recovery_cp != MaxSector)
  3519. pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
  3520. mdname(mddev));
  3521. pr_info("md/raid10:%s: active with %d out of %d devices\n",
  3522. mdname(mddev), conf->geo.raid_disks - mddev->degraded,
  3523. conf->geo.raid_disks);
  3524. /*
  3525. * Ok, everything is just fine now
  3526. */
  3527. mddev->dev_sectors = conf->dev_sectors;
  3528. size = raid10_size(mddev, 0, 0);
  3529. md_set_array_sectors(mddev, size);
  3530. mddev->resync_max_sectors = size;
  3531. set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
  3532. if (mddev->queue) {
  3533. int stripe = conf->geo.raid_disks *
  3534. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  3535. /* Calculate max read-ahead size.
  3536. * We need to readahead at least twice a whole stripe....
  3537. * maybe...
  3538. */
  3539. stripe /= conf->geo.near_copies;
  3540. if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
  3541. mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
  3542. }
  3543. if (md_integrity_register(mddev))
  3544. goto out_free_conf;
  3545. if (conf->reshape_progress != MaxSector) {
  3546. unsigned long before_length, after_length;
  3547. before_length = ((1 << conf->prev.chunk_shift) *
  3548. conf->prev.far_copies);
  3549. after_length = ((1 << conf->geo.chunk_shift) *
  3550. conf->geo.far_copies);
  3551. if (max(before_length, after_length) > min_offset_diff) {
  3552. /* This cannot work */
  3553. pr_warn("md/raid10: offset difference not enough to continue reshape\n");
  3554. goto out_free_conf;
  3555. }
  3556. conf->offset_diff = min_offset_diff;
  3557. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3558. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3559. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3560. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3561. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3562. "reshape");
  3563. if (!mddev->sync_thread)
  3564. goto out_free_conf;
  3565. }
  3566. return 0;
  3567. out_free_conf:
  3568. md_unregister_thread(&mddev->thread);
  3569. mempool_exit(&conf->r10bio_pool);
  3570. safe_put_page(conf->tmppage);
  3571. kfree(conf->mirrors);
  3572. kfree(conf);
  3573. mddev->private = NULL;
  3574. out:
  3575. return -EIO;
  3576. }
  3577. static void raid10_free(struct mddev *mddev, void *priv)
  3578. {
  3579. struct r10conf *conf = priv;
  3580. mempool_exit(&conf->r10bio_pool);
  3581. safe_put_page(conf->tmppage);
  3582. kfree(conf->mirrors);
  3583. kfree(conf->mirrors_old);
  3584. kfree(conf->mirrors_new);
  3585. bioset_exit(&conf->bio_split);
  3586. kfree(conf);
  3587. }
  3588. static void raid10_quiesce(struct mddev *mddev, int quiesce)
  3589. {
  3590. struct r10conf *conf = mddev->private;
  3591. if (quiesce)
  3592. raise_barrier(conf, 0);
  3593. else
  3594. lower_barrier(conf);
  3595. }
  3596. static int raid10_resize(struct mddev *mddev, sector_t sectors)
  3597. {
  3598. /* Resize of 'far' arrays is not supported.
  3599. * For 'near' and 'offset' arrays we can set the
  3600. * number of sectors used to be an appropriate multiple
  3601. * of the chunk size.
  3602. * For 'offset', this is far_copies*chunksize.
  3603. * For 'near' the multiplier is the LCM of
  3604. * near_copies and raid_disks.
  3605. * So if far_copies > 1 && !far_offset, fail.
  3606. * Else find LCM(raid_disks, near_copy)*far_copies and
  3607. * multiply by chunk_size. Then round to this number.
  3608. * This is mostly done by raid10_size()
  3609. */
  3610. struct r10conf *conf = mddev->private;
  3611. sector_t oldsize, size;
  3612. if (mddev->reshape_position != MaxSector)
  3613. return -EBUSY;
  3614. if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
  3615. return -EINVAL;
  3616. oldsize = raid10_size(mddev, 0, 0);
  3617. size = raid10_size(mddev, sectors, 0);
  3618. if (mddev->external_size &&
  3619. mddev->array_sectors > size)
  3620. return -EINVAL;
  3621. if (mddev->bitmap) {
  3622. int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
  3623. if (ret)
  3624. return ret;
  3625. }
  3626. md_set_array_sectors(mddev, size);
  3627. if (sectors > mddev->dev_sectors &&
  3628. mddev->recovery_cp > oldsize) {
  3629. mddev->recovery_cp = oldsize;
  3630. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3631. }
  3632. calc_sectors(conf, sectors);
  3633. mddev->dev_sectors = conf->dev_sectors;
  3634. mddev->resync_max_sectors = size;
  3635. return 0;
  3636. }
  3637. static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
  3638. {
  3639. struct md_rdev *rdev;
  3640. struct r10conf *conf;
  3641. if (mddev->degraded > 0) {
  3642. pr_warn("md/raid10:%s: Error: degraded raid0!\n",
  3643. mdname(mddev));
  3644. return ERR_PTR(-EINVAL);
  3645. }
  3646. sector_div(size, devs);
  3647. /* Set new parameters */
  3648. mddev->new_level = 10;
  3649. /* new layout: far_copies = 1, near_copies = 2 */
  3650. mddev->new_layout = (1<<8) + 2;
  3651. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3652. mddev->delta_disks = mddev->raid_disks;
  3653. mddev->raid_disks *= 2;
  3654. /* make sure it will be not marked as dirty */
  3655. mddev->recovery_cp = MaxSector;
  3656. mddev->dev_sectors = size;
  3657. conf = setup_conf(mddev);
  3658. if (!IS_ERR(conf)) {
  3659. rdev_for_each(rdev, mddev)
  3660. if (rdev->raid_disk >= 0) {
  3661. rdev->new_raid_disk = rdev->raid_disk * 2;
  3662. rdev->sectors = size;
  3663. }
  3664. conf->barrier = 1;
  3665. }
  3666. return conf;
  3667. }
  3668. static void *raid10_takeover(struct mddev *mddev)
  3669. {
  3670. struct r0conf *raid0_conf;
  3671. /* raid10 can take over:
  3672. * raid0 - providing it has only two drives
  3673. */
  3674. if (mddev->level == 0) {
  3675. /* for raid0 takeover only one zone is supported */
  3676. raid0_conf = mddev->private;
  3677. if (raid0_conf->nr_strip_zones > 1) {
  3678. pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
  3679. mdname(mddev));
  3680. return ERR_PTR(-EINVAL);
  3681. }
  3682. return raid10_takeover_raid0(mddev,
  3683. raid0_conf->strip_zone->zone_end,
  3684. raid0_conf->strip_zone->nb_dev);
  3685. }
  3686. return ERR_PTR(-EINVAL);
  3687. }
  3688. static int raid10_check_reshape(struct mddev *mddev)
  3689. {
  3690. /* Called when there is a request to change
  3691. * - layout (to ->new_layout)
  3692. * - chunk size (to ->new_chunk_sectors)
  3693. * - raid_disks (by delta_disks)
  3694. * or when trying to restart a reshape that was ongoing.
  3695. *
  3696. * We need to validate the request and possibly allocate
  3697. * space if that might be an issue later.
  3698. *
  3699. * Currently we reject any reshape of a 'far' mode array,
  3700. * allow chunk size to change if new is generally acceptable,
  3701. * allow raid_disks to increase, and allow
  3702. * a switch between 'near' mode and 'offset' mode.
  3703. */
  3704. struct r10conf *conf = mddev->private;
  3705. struct geom geo;
  3706. if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
  3707. return -EINVAL;
  3708. if (setup_geo(&geo, mddev, geo_start) != conf->copies)
  3709. /* mustn't change number of copies */
  3710. return -EINVAL;
  3711. if (geo.far_copies > 1 && !geo.far_offset)
  3712. /* Cannot switch to 'far' mode */
  3713. return -EINVAL;
  3714. if (mddev->array_sectors & geo.chunk_mask)
  3715. /* not factor of array size */
  3716. return -EINVAL;
  3717. if (!enough(conf, -1))
  3718. return -EINVAL;
  3719. kfree(conf->mirrors_new);
  3720. conf->mirrors_new = NULL;
  3721. if (mddev->delta_disks > 0) {
  3722. /* allocate new 'mirrors' list */
  3723. conf->mirrors_new =
  3724. kcalloc(mddev->raid_disks + mddev->delta_disks,
  3725. sizeof(struct raid10_info),
  3726. GFP_KERNEL);
  3727. if (!conf->mirrors_new)
  3728. return -ENOMEM;
  3729. }
  3730. return 0;
  3731. }
  3732. /*
  3733. * Need to check if array has failed when deciding whether to:
  3734. * - start an array
  3735. * - remove non-faulty devices
  3736. * - add a spare
  3737. * - allow a reshape
  3738. * This determination is simple when no reshape is happening.
  3739. * However if there is a reshape, we need to carefully check
  3740. * both the before and after sections.
  3741. * This is because some failed devices may only affect one
  3742. * of the two sections, and some non-in_sync devices may
  3743. * be insync in the section most affected by failed devices.
  3744. */
  3745. static int calc_degraded(struct r10conf *conf)
  3746. {
  3747. int degraded, degraded2;
  3748. int i;
  3749. rcu_read_lock();
  3750. degraded = 0;
  3751. /* 'prev' section first */
  3752. for (i = 0; i < conf->prev.raid_disks; i++) {
  3753. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3754. if (!rdev || test_bit(Faulty, &rdev->flags))
  3755. degraded++;
  3756. else if (!test_bit(In_sync, &rdev->flags))
  3757. /* When we can reduce the number of devices in
  3758. * an array, this might not contribute to
  3759. * 'degraded'. It does now.
  3760. */
  3761. degraded++;
  3762. }
  3763. rcu_read_unlock();
  3764. if (conf->geo.raid_disks == conf->prev.raid_disks)
  3765. return degraded;
  3766. rcu_read_lock();
  3767. degraded2 = 0;
  3768. for (i = 0; i < conf->geo.raid_disks; i++) {
  3769. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3770. if (!rdev || test_bit(Faulty, &rdev->flags))
  3771. degraded2++;
  3772. else if (!test_bit(In_sync, &rdev->flags)) {
  3773. /* If reshape is increasing the number of devices,
  3774. * this section has already been recovered, so
  3775. * it doesn't contribute to degraded.
  3776. * else it does.
  3777. */
  3778. if (conf->geo.raid_disks <= conf->prev.raid_disks)
  3779. degraded2++;
  3780. }
  3781. }
  3782. rcu_read_unlock();
  3783. if (degraded2 > degraded)
  3784. return degraded2;
  3785. return degraded;
  3786. }
  3787. static int raid10_start_reshape(struct mddev *mddev)
  3788. {
  3789. /* A 'reshape' has been requested. This commits
  3790. * the various 'new' fields and sets MD_RECOVER_RESHAPE
  3791. * This also checks if there are enough spares and adds them
  3792. * to the array.
  3793. * We currently require enough spares to make the final
  3794. * array non-degraded. We also require that the difference
  3795. * between old and new data_offset - on each device - is
  3796. * enough that we never risk over-writing.
  3797. */
  3798. unsigned long before_length, after_length;
  3799. sector_t min_offset_diff = 0;
  3800. int first = 1;
  3801. struct geom new;
  3802. struct r10conf *conf = mddev->private;
  3803. struct md_rdev *rdev;
  3804. int spares = 0;
  3805. int ret;
  3806. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3807. return -EBUSY;
  3808. if (setup_geo(&new, mddev, geo_start) != conf->copies)
  3809. return -EINVAL;
  3810. before_length = ((1 << conf->prev.chunk_shift) *
  3811. conf->prev.far_copies);
  3812. after_length = ((1 << conf->geo.chunk_shift) *
  3813. conf->geo.far_copies);
  3814. rdev_for_each(rdev, mddev) {
  3815. if (!test_bit(In_sync, &rdev->flags)
  3816. && !test_bit(Faulty, &rdev->flags))
  3817. spares++;
  3818. if (rdev->raid_disk >= 0) {
  3819. long long diff = (rdev->new_data_offset
  3820. - rdev->data_offset);
  3821. if (!mddev->reshape_backwards)
  3822. diff = -diff;
  3823. if (diff < 0)
  3824. diff = 0;
  3825. if (first || diff < min_offset_diff)
  3826. min_offset_diff = diff;
  3827. first = 0;
  3828. }
  3829. }
  3830. if (max(before_length, after_length) > min_offset_diff)
  3831. return -EINVAL;
  3832. if (spares < mddev->delta_disks)
  3833. return -EINVAL;
  3834. conf->offset_diff = min_offset_diff;
  3835. spin_lock_irq(&conf->device_lock);
  3836. if (conf->mirrors_new) {
  3837. memcpy(conf->mirrors_new, conf->mirrors,
  3838. sizeof(struct raid10_info)*conf->prev.raid_disks);
  3839. smp_mb();
  3840. kfree(conf->mirrors_old);
  3841. conf->mirrors_old = conf->mirrors;
  3842. conf->mirrors = conf->mirrors_new;
  3843. conf->mirrors_new = NULL;
  3844. }
  3845. setup_geo(&conf->geo, mddev, geo_start);
  3846. smp_mb();
  3847. if (mddev->reshape_backwards) {
  3848. sector_t size = raid10_size(mddev, 0, 0);
  3849. if (size < mddev->array_sectors) {
  3850. spin_unlock_irq(&conf->device_lock);
  3851. pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
  3852. mdname(mddev));
  3853. return -EINVAL;
  3854. }
  3855. mddev->resync_max_sectors = size;
  3856. conf->reshape_progress = size;
  3857. } else
  3858. conf->reshape_progress = 0;
  3859. conf->reshape_safe = conf->reshape_progress;
  3860. spin_unlock_irq(&conf->device_lock);
  3861. if (mddev->delta_disks && mddev->bitmap) {
  3862. ret = md_bitmap_resize(mddev->bitmap,
  3863. raid10_size(mddev, 0, conf->geo.raid_disks),
  3864. 0, 0);
  3865. if (ret)
  3866. goto abort;
  3867. }
  3868. if (mddev->delta_disks > 0) {
  3869. rdev_for_each(rdev, mddev)
  3870. if (rdev->raid_disk < 0 &&
  3871. !test_bit(Faulty, &rdev->flags)) {
  3872. if (raid10_add_disk(mddev, rdev) == 0) {
  3873. if (rdev->raid_disk >=
  3874. conf->prev.raid_disks)
  3875. set_bit(In_sync, &rdev->flags);
  3876. else
  3877. rdev->recovery_offset = 0;
  3878. if (sysfs_link_rdev(mddev, rdev))
  3879. /* Failure here is OK */;
  3880. }
  3881. } else if (rdev->raid_disk >= conf->prev.raid_disks
  3882. && !test_bit(Faulty, &rdev->flags)) {
  3883. /* This is a spare that was manually added */
  3884. set_bit(In_sync, &rdev->flags);
  3885. }
  3886. }
  3887. /* When a reshape changes the number of devices,
  3888. * ->degraded is measured against the larger of the
  3889. * pre and post numbers.
  3890. */
  3891. spin_lock_irq(&conf->device_lock);
  3892. mddev->degraded = calc_degraded(conf);
  3893. spin_unlock_irq(&conf->device_lock);
  3894. mddev->raid_disks = conf->geo.raid_disks;
  3895. mddev->reshape_position = conf->reshape_progress;
  3896. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  3897. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3898. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3899. clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
  3900. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3901. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3902. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3903. "reshape");
  3904. if (!mddev->sync_thread) {
  3905. ret = -EAGAIN;
  3906. goto abort;
  3907. }
  3908. conf->reshape_checkpoint = jiffies;
  3909. md_wakeup_thread(mddev->sync_thread);
  3910. md_new_event(mddev);
  3911. return 0;
  3912. abort:
  3913. mddev->recovery = 0;
  3914. spin_lock_irq(&conf->device_lock);
  3915. conf->geo = conf->prev;
  3916. mddev->raid_disks = conf->geo.raid_disks;
  3917. rdev_for_each(rdev, mddev)
  3918. rdev->new_data_offset = rdev->data_offset;
  3919. smp_wmb();
  3920. conf->reshape_progress = MaxSector;
  3921. conf->reshape_safe = MaxSector;
  3922. mddev->reshape_position = MaxSector;
  3923. spin_unlock_irq(&conf->device_lock);
  3924. return ret;
  3925. }
  3926. /* Calculate the last device-address that could contain
  3927. * any block from the chunk that includes the array-address 's'
  3928. * and report the next address.
  3929. * i.e. the address returned will be chunk-aligned and after
  3930. * any data that is in the chunk containing 's'.
  3931. */
  3932. static sector_t last_dev_address(sector_t s, struct geom *geo)
  3933. {
  3934. s = (s | geo->chunk_mask) + 1;
  3935. s >>= geo->chunk_shift;
  3936. s *= geo->near_copies;
  3937. s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
  3938. s *= geo->far_copies;
  3939. s <<= geo->chunk_shift;
  3940. return s;
  3941. }
  3942. /* Calculate the first device-address that could contain
  3943. * any block from the chunk that includes the array-address 's'.
  3944. * This too will be the start of a chunk
  3945. */
  3946. static sector_t first_dev_address(sector_t s, struct geom *geo)
  3947. {
  3948. s >>= geo->chunk_shift;
  3949. s *= geo->near_copies;
  3950. sector_div(s, geo->raid_disks);
  3951. s *= geo->far_copies;
  3952. s <<= geo->chunk_shift;
  3953. return s;
  3954. }
  3955. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  3956. int *skipped)
  3957. {
  3958. /* We simply copy at most one chunk (smallest of old and new)
  3959. * at a time, possibly less if that exceeds RESYNC_PAGES,
  3960. * or we hit a bad block or something.
  3961. * This might mean we pause for normal IO in the middle of
  3962. * a chunk, but that is not a problem as mddev->reshape_position
  3963. * can record any location.
  3964. *
  3965. * If we will want to write to a location that isn't
  3966. * yet recorded as 'safe' (i.e. in metadata on disk) then
  3967. * we need to flush all reshape requests and update the metadata.
  3968. *
  3969. * When reshaping forwards (e.g. to more devices), we interpret
  3970. * 'safe' as the earliest block which might not have been copied
  3971. * down yet. We divide this by previous stripe size and multiply
  3972. * by previous stripe length to get lowest device offset that we
  3973. * cannot write to yet.
  3974. * We interpret 'sector_nr' as an address that we want to write to.
  3975. * From this we use last_device_address() to find where we might
  3976. * write to, and first_device_address on the 'safe' position.
  3977. * If this 'next' write position is after the 'safe' position,
  3978. * we must update the metadata to increase the 'safe' position.
  3979. *
  3980. * When reshaping backwards, we round in the opposite direction
  3981. * and perform the reverse test: next write position must not be
  3982. * less than current safe position.
  3983. *
  3984. * In all this the minimum difference in data offsets
  3985. * (conf->offset_diff - always positive) allows a bit of slack,
  3986. * so next can be after 'safe', but not by more than offset_diff
  3987. *
  3988. * We need to prepare all the bios here before we start any IO
  3989. * to ensure the size we choose is acceptable to all devices.
  3990. * The means one for each copy for write-out and an extra one for
  3991. * read-in.
  3992. * We store the read-in bio in ->master_bio and the others in
  3993. * ->devs[x].bio and ->devs[x].repl_bio.
  3994. */
  3995. struct r10conf *conf = mddev->private;
  3996. struct r10bio *r10_bio;
  3997. sector_t next, safe, last;
  3998. int max_sectors;
  3999. int nr_sectors;
  4000. int s;
  4001. struct md_rdev *rdev;
  4002. int need_flush = 0;
  4003. struct bio *blist;
  4004. struct bio *bio, *read_bio;
  4005. int sectors_done = 0;
  4006. struct page **pages;
  4007. if (sector_nr == 0) {
  4008. /* If restarting in the middle, skip the initial sectors */
  4009. if (mddev->reshape_backwards &&
  4010. conf->reshape_progress < raid10_size(mddev, 0, 0)) {
  4011. sector_nr = (raid10_size(mddev, 0, 0)
  4012. - conf->reshape_progress);
  4013. } else if (!mddev->reshape_backwards &&
  4014. conf->reshape_progress > 0)
  4015. sector_nr = conf->reshape_progress;
  4016. if (sector_nr) {
  4017. mddev->curr_resync_completed = sector_nr;
  4018. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  4019. *skipped = 1;
  4020. return sector_nr;
  4021. }
  4022. }
  4023. /* We don't use sector_nr to track where we are up to
  4024. * as that doesn't work well for ->reshape_backwards.
  4025. * So just use ->reshape_progress.
  4026. */
  4027. if (mddev->reshape_backwards) {
  4028. /* 'next' is the earliest device address that we might
  4029. * write to for this chunk in the new layout
  4030. */
  4031. next = first_dev_address(conf->reshape_progress - 1,
  4032. &conf->geo);
  4033. /* 'safe' is the last device address that we might read from
  4034. * in the old layout after a restart
  4035. */
  4036. safe = last_dev_address(conf->reshape_safe - 1,
  4037. &conf->prev);
  4038. if (next + conf->offset_diff < safe)
  4039. need_flush = 1;
  4040. last = conf->reshape_progress - 1;
  4041. sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
  4042. & conf->prev.chunk_mask);
  4043. if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
  4044. sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
  4045. } else {
  4046. /* 'next' is after the last device address that we
  4047. * might write to for this chunk in the new layout
  4048. */
  4049. next = last_dev_address(conf->reshape_progress, &conf->geo);
  4050. /* 'safe' is the earliest device address that we might
  4051. * read from in the old layout after a restart
  4052. */
  4053. safe = first_dev_address(conf->reshape_safe, &conf->prev);
  4054. /* Need to update metadata if 'next' might be beyond 'safe'
  4055. * as that would possibly corrupt data
  4056. */
  4057. if (next > safe + conf->offset_diff)
  4058. need_flush = 1;
  4059. sector_nr = conf->reshape_progress;
  4060. last = sector_nr | (conf->geo.chunk_mask
  4061. & conf->prev.chunk_mask);
  4062. if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
  4063. last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
  4064. }
  4065. if (need_flush ||
  4066. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  4067. /* Need to update reshape_position in metadata */
  4068. wait_barrier(conf);
  4069. mddev->reshape_position = conf->reshape_progress;
  4070. if (mddev->reshape_backwards)
  4071. mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
  4072. - conf->reshape_progress;
  4073. else
  4074. mddev->curr_resync_completed = conf->reshape_progress;
  4075. conf->reshape_checkpoint = jiffies;
  4076. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  4077. md_wakeup_thread(mddev->thread);
  4078. wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
  4079. test_bit(MD_RECOVERY_INTR, &mddev->recovery));
  4080. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  4081. allow_barrier(conf);
  4082. return sectors_done;
  4083. }
  4084. conf->reshape_safe = mddev->reshape_position;
  4085. allow_barrier(conf);
  4086. }
  4087. raise_barrier(conf, 0);
  4088. read_more:
  4089. /* Now schedule reads for blocks from sector_nr to last */
  4090. r10_bio = raid10_alloc_init_r10buf(conf);
  4091. r10_bio->state = 0;
  4092. raise_barrier(conf, 1);
  4093. atomic_set(&r10_bio->remaining, 0);
  4094. r10_bio->mddev = mddev;
  4095. r10_bio->sector = sector_nr;
  4096. set_bit(R10BIO_IsReshape, &r10_bio->state);
  4097. r10_bio->sectors = last - sector_nr + 1;
  4098. rdev = read_balance(conf, r10_bio, &max_sectors);
  4099. BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
  4100. if (!rdev) {
  4101. /* Cannot read from here, so need to record bad blocks
  4102. * on all the target devices.
  4103. */
  4104. // FIXME
  4105. mempool_free(r10_bio, &conf->r10buf_pool);
  4106. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  4107. return sectors_done;
  4108. }
  4109. read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
  4110. bio_set_dev(read_bio, rdev->bdev);
  4111. read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
  4112. + rdev->data_offset);
  4113. read_bio->bi_private = r10_bio;
  4114. read_bio->bi_end_io = end_reshape_read;
  4115. bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
  4116. read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
  4117. read_bio->bi_status = 0;
  4118. read_bio->bi_vcnt = 0;
  4119. read_bio->bi_iter.bi_size = 0;
  4120. r10_bio->master_bio = read_bio;
  4121. r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
  4122. /* Now find the locations in the new layout */
  4123. __raid10_find_phys(&conf->geo, r10_bio);
  4124. blist = read_bio;
  4125. read_bio->bi_next = NULL;
  4126. rcu_read_lock();
  4127. for (s = 0; s < conf->copies*2; s++) {
  4128. struct bio *b;
  4129. int d = r10_bio->devs[s/2].devnum;
  4130. struct md_rdev *rdev2;
  4131. if (s&1) {
  4132. rdev2 = rcu_dereference(conf->mirrors[d].replacement);
  4133. b = r10_bio->devs[s/2].repl_bio;
  4134. } else {
  4135. rdev2 = rcu_dereference(conf->mirrors[d].rdev);
  4136. b = r10_bio->devs[s/2].bio;
  4137. }
  4138. if (!rdev2 || test_bit(Faulty, &rdev2->flags))
  4139. continue;
  4140. bio_set_dev(b, rdev2->bdev);
  4141. b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
  4142. rdev2->new_data_offset;
  4143. b->bi_end_io = end_reshape_write;
  4144. bio_set_op_attrs(b, REQ_OP_WRITE, 0);
  4145. b->bi_next = blist;
  4146. blist = b;
  4147. }
  4148. /* Now add as many pages as possible to all of these bios. */
  4149. nr_sectors = 0;
  4150. pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
  4151. for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
  4152. struct page *page = pages[s / (PAGE_SIZE >> 9)];
  4153. int len = (max_sectors - s) << 9;
  4154. if (len > PAGE_SIZE)
  4155. len = PAGE_SIZE;
  4156. for (bio = blist; bio ; bio = bio->bi_next) {
  4157. /*
  4158. * won't fail because the vec table is big enough
  4159. * to hold all these pages
  4160. */
  4161. bio_add_page(bio, page, len, 0);
  4162. }
  4163. sector_nr += len >> 9;
  4164. nr_sectors += len >> 9;
  4165. }
  4166. rcu_read_unlock();
  4167. r10_bio->sectors = nr_sectors;
  4168. /* Now submit the read */
  4169. md_sync_acct_bio(read_bio, r10_bio->sectors);
  4170. atomic_inc(&r10_bio->remaining);
  4171. read_bio->bi_next = NULL;
  4172. generic_make_request(read_bio);
  4173. sectors_done += nr_sectors;
  4174. if (sector_nr <= last)
  4175. goto read_more;
  4176. lower_barrier(conf);
  4177. /* Now that we have done the whole section we can
  4178. * update reshape_progress
  4179. */
  4180. if (mddev->reshape_backwards)
  4181. conf->reshape_progress -= sectors_done;
  4182. else
  4183. conf->reshape_progress += sectors_done;
  4184. return sectors_done;
  4185. }
  4186. static void end_reshape_request(struct r10bio *r10_bio);
  4187. static int handle_reshape_read_error(struct mddev *mddev,
  4188. struct r10bio *r10_bio);
  4189. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  4190. {
  4191. /* Reshape read completed. Hopefully we have a block
  4192. * to write out.
  4193. * If we got a read error then we do sync 1-page reads from
  4194. * elsewhere until we find the data - or give up.
  4195. */
  4196. struct r10conf *conf = mddev->private;
  4197. int s;
  4198. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  4199. if (handle_reshape_read_error(mddev, r10_bio) < 0) {
  4200. /* Reshape has been aborted */
  4201. md_done_sync(mddev, r10_bio->sectors, 0);
  4202. return;
  4203. }
  4204. /* We definitely have the data in the pages, schedule the
  4205. * writes.
  4206. */
  4207. atomic_set(&r10_bio->remaining, 1);
  4208. for (s = 0; s < conf->copies*2; s++) {
  4209. struct bio *b;
  4210. int d = r10_bio->devs[s/2].devnum;
  4211. struct md_rdev *rdev;
  4212. rcu_read_lock();
  4213. if (s&1) {
  4214. rdev = rcu_dereference(conf->mirrors[d].replacement);
  4215. b = r10_bio->devs[s/2].repl_bio;
  4216. } else {
  4217. rdev = rcu_dereference(conf->mirrors[d].rdev);
  4218. b = r10_bio->devs[s/2].bio;
  4219. }
  4220. if (!rdev || test_bit(Faulty, &rdev->flags)) {
  4221. rcu_read_unlock();
  4222. continue;
  4223. }
  4224. atomic_inc(&rdev->nr_pending);
  4225. rcu_read_unlock();
  4226. md_sync_acct_bio(b, r10_bio->sectors);
  4227. atomic_inc(&r10_bio->remaining);
  4228. b->bi_next = NULL;
  4229. generic_make_request(b);
  4230. }
  4231. end_reshape_request(r10_bio);
  4232. }
  4233. static void end_reshape(struct r10conf *conf)
  4234. {
  4235. if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
  4236. return;
  4237. spin_lock_irq(&conf->device_lock);
  4238. conf->prev = conf->geo;
  4239. md_finish_reshape(conf->mddev);
  4240. smp_wmb();
  4241. conf->reshape_progress = MaxSector;
  4242. conf->reshape_safe = MaxSector;
  4243. spin_unlock_irq(&conf->device_lock);
  4244. /* read-ahead size must cover two whole stripes, which is
  4245. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4246. */
  4247. if (conf->mddev->queue) {
  4248. int stripe = conf->geo.raid_disks *
  4249. ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
  4250. stripe /= conf->geo.near_copies;
  4251. if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
  4252. conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
  4253. }
  4254. conf->fullsync = 0;
  4255. }
  4256. static int handle_reshape_read_error(struct mddev *mddev,
  4257. struct r10bio *r10_bio)
  4258. {
  4259. /* Use sync reads to get the blocks from somewhere else */
  4260. int sectors = r10_bio->sectors;
  4261. struct r10conf *conf = mddev->private;
  4262. struct r10bio *r10b;
  4263. int slot = 0;
  4264. int idx = 0;
  4265. struct page **pages;
  4266. r10b = kmalloc(sizeof(*r10b) +
  4267. sizeof(struct r10dev) * conf->copies, GFP_NOIO);
  4268. if (!r10b) {
  4269. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  4270. return -ENOMEM;
  4271. }
  4272. /* reshape IOs share pages from .devs[0].bio */
  4273. pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
  4274. r10b->sector = r10_bio->sector;
  4275. __raid10_find_phys(&conf->prev, r10b);
  4276. while (sectors) {
  4277. int s = sectors;
  4278. int success = 0;
  4279. int first_slot = slot;
  4280. if (s > (PAGE_SIZE >> 9))
  4281. s = PAGE_SIZE >> 9;
  4282. rcu_read_lock();
  4283. while (!success) {
  4284. int d = r10b->devs[slot].devnum;
  4285. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  4286. sector_t addr;
  4287. if (rdev == NULL ||
  4288. test_bit(Faulty, &rdev->flags) ||
  4289. !test_bit(In_sync, &rdev->flags))
  4290. goto failed;
  4291. addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
  4292. atomic_inc(&rdev->nr_pending);
  4293. rcu_read_unlock();
  4294. success = sync_page_io(rdev,
  4295. addr,
  4296. s << 9,
  4297. pages[idx],
  4298. REQ_OP_READ, 0, false);
  4299. rdev_dec_pending(rdev, mddev);
  4300. rcu_read_lock();
  4301. if (success)
  4302. break;
  4303. failed:
  4304. slot++;
  4305. if (slot >= conf->copies)
  4306. slot = 0;
  4307. if (slot == first_slot)
  4308. break;
  4309. }
  4310. rcu_read_unlock();
  4311. if (!success) {
  4312. /* couldn't read this block, must give up */
  4313. set_bit(MD_RECOVERY_INTR,
  4314. &mddev->recovery);
  4315. kfree(r10b);
  4316. return -EIO;
  4317. }
  4318. sectors -= s;
  4319. idx++;
  4320. }
  4321. kfree(r10b);
  4322. return 0;
  4323. }
  4324. static void end_reshape_write(struct bio *bio)
  4325. {
  4326. struct r10bio *r10_bio = get_resync_r10bio(bio);
  4327. struct mddev *mddev = r10_bio->mddev;
  4328. struct r10conf *conf = mddev->private;
  4329. int d;
  4330. int slot;
  4331. int repl;
  4332. struct md_rdev *rdev = NULL;
  4333. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  4334. if (repl)
  4335. rdev = conf->mirrors[d].replacement;
  4336. if (!rdev) {
  4337. smp_mb();
  4338. rdev = conf->mirrors[d].rdev;
  4339. }
  4340. if (bio->bi_status) {
  4341. /* FIXME should record badblock */
  4342. md_error(mddev, rdev);
  4343. }
  4344. rdev_dec_pending(rdev, mddev);
  4345. end_reshape_request(r10_bio);
  4346. }
  4347. static void end_reshape_request(struct r10bio *r10_bio)
  4348. {
  4349. if (!atomic_dec_and_test(&r10_bio->remaining))
  4350. return;
  4351. md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
  4352. bio_put(r10_bio->master_bio);
  4353. put_buf(r10_bio);
  4354. }
  4355. static void raid10_finish_reshape(struct mddev *mddev)
  4356. {
  4357. struct r10conf *conf = mddev->private;
  4358. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  4359. return;
  4360. if (mddev->delta_disks > 0) {
  4361. if (mddev->recovery_cp > mddev->resync_max_sectors) {
  4362. mddev->recovery_cp = mddev->resync_max_sectors;
  4363. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4364. }
  4365. mddev->resync_max_sectors = mddev->array_sectors;
  4366. } else {
  4367. int d;
  4368. rcu_read_lock();
  4369. for (d = conf->geo.raid_disks ;
  4370. d < conf->geo.raid_disks - mddev->delta_disks;
  4371. d++) {
  4372. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  4373. if (rdev)
  4374. clear_bit(In_sync, &rdev->flags);
  4375. rdev = rcu_dereference(conf->mirrors[d].replacement);
  4376. if (rdev)
  4377. clear_bit(In_sync, &rdev->flags);
  4378. }
  4379. rcu_read_unlock();
  4380. }
  4381. mddev->layout = mddev->new_layout;
  4382. mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
  4383. mddev->reshape_position = MaxSector;
  4384. mddev->delta_disks = 0;
  4385. mddev->reshape_backwards = 0;
  4386. }
  4387. static struct md_personality raid10_personality =
  4388. {
  4389. .name = "raid10",
  4390. .level = 10,
  4391. .owner = THIS_MODULE,
  4392. .make_request = raid10_make_request,
  4393. .run = raid10_run,
  4394. .free = raid10_free,
  4395. .status = raid10_status,
  4396. .error_handler = raid10_error,
  4397. .hot_add_disk = raid10_add_disk,
  4398. .hot_remove_disk= raid10_remove_disk,
  4399. .spare_active = raid10_spare_active,
  4400. .sync_request = raid10_sync_request,
  4401. .quiesce = raid10_quiesce,
  4402. .size = raid10_size,
  4403. .resize = raid10_resize,
  4404. .takeover = raid10_takeover,
  4405. .check_reshape = raid10_check_reshape,
  4406. .start_reshape = raid10_start_reshape,
  4407. .finish_reshape = raid10_finish_reshape,
  4408. .congested = raid10_congested,
  4409. };
  4410. static int __init raid_init(void)
  4411. {
  4412. return register_md_personality(&raid10_personality);
  4413. }
  4414. static void raid_exit(void)
  4415. {
  4416. unregister_md_personality(&raid10_personality);
  4417. }
  4418. module_init(raid_init);
  4419. module_exit(raid_exit);
  4420. MODULE_LICENSE("GPL");
  4421. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  4422. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  4423. MODULE_ALIAS("md-raid10");
  4424. MODULE_ALIAS("md-level-10");
  4425. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);