raid10.c 138 KB

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