raid10.c 135 KB

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