raid10.c 135 KB

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