raid5.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  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. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->bm_write is the number of the last batch successfully written.
  30. * conf->bm_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is bm_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/blkdev.h>
  45. #include <linux/kthread.h>
  46. #include <linux/raid/pq.h>
  47. #include <linux/async_tx.h>
  48. #include <linux/seq_file.h>
  49. #include "md.h"
  50. #include "raid5.h"
  51. #include "bitmap.h"
  52. /*
  53. * Stripe cache
  54. */
  55. #define NR_STRIPES 256
  56. #define STRIPE_SIZE PAGE_SIZE
  57. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  58. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  59. #define IO_THRESHOLD 1
  60. #define BYPASS_THRESHOLD 1
  61. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  62. #define HASH_MASK (NR_HASH - 1)
  63. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  64. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  65. * order without overlap. There may be several bio's per stripe+device, and
  66. * a bio could span several devices.
  67. * When walking this list for a particular stripe+device, we must never proceed
  68. * beyond a bio that extends past this device, as the next bio might no longer
  69. * be valid.
  70. * This macro is used to determine the 'next' bio in the list, given the sector
  71. * of the current stripe+device
  72. */
  73. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  74. /*
  75. * The following can be used to debug the driver
  76. */
  77. #define RAID5_PARANOIA 1
  78. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  79. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  80. #else
  81. # define CHECK_DEVLOCK()
  82. #endif
  83. #ifdef DEBUG
  84. #define inline
  85. #define __inline__
  86. #endif
  87. #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
  88. /*
  89. * We maintain a biased count of active stripes in the bottom 16 bits of
  90. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  91. */
  92. static inline int raid5_bi_phys_segments(struct bio *bio)
  93. {
  94. return bio->bi_phys_segments & 0xffff;
  95. }
  96. static inline int raid5_bi_hw_segments(struct bio *bio)
  97. {
  98. return (bio->bi_phys_segments >> 16) & 0xffff;
  99. }
  100. static inline int raid5_dec_bi_phys_segments(struct bio *bio)
  101. {
  102. --bio->bi_phys_segments;
  103. return raid5_bi_phys_segments(bio);
  104. }
  105. static inline int raid5_dec_bi_hw_segments(struct bio *bio)
  106. {
  107. unsigned short val = raid5_bi_hw_segments(bio);
  108. --val;
  109. bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
  110. return val;
  111. }
  112. static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
  113. {
  114. bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
  115. }
  116. /* Find first data disk in a raid6 stripe */
  117. static inline int raid6_d0(struct stripe_head *sh)
  118. {
  119. if (sh->ddf_layout)
  120. /* ddf always start from first device */
  121. return 0;
  122. /* md starts just after Q block */
  123. if (sh->qd_idx == sh->disks - 1)
  124. return 0;
  125. else
  126. return sh->qd_idx + 1;
  127. }
  128. static inline int raid6_next_disk(int disk, int raid_disks)
  129. {
  130. disk++;
  131. return (disk < raid_disks) ? disk : 0;
  132. }
  133. /* When walking through the disks in a raid5, starting at raid6_d0,
  134. * We need to map each disk to a 'slot', where the data disks are slot
  135. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  136. * is raid_disks-1. This help does that mapping.
  137. */
  138. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  139. int *count, int syndrome_disks)
  140. {
  141. int slot;
  142. if (idx == sh->pd_idx)
  143. return syndrome_disks;
  144. if (idx == sh->qd_idx)
  145. return syndrome_disks + 1;
  146. slot = (*count)++;
  147. return slot;
  148. }
  149. static void return_io(struct bio *return_bi)
  150. {
  151. struct bio *bi = return_bi;
  152. while (bi) {
  153. return_bi = bi->bi_next;
  154. bi->bi_next = NULL;
  155. bi->bi_size = 0;
  156. bio_endio(bi, 0);
  157. bi = return_bi;
  158. }
  159. }
  160. static void print_raid5_conf (raid5_conf_t *conf);
  161. static int stripe_operations_active(struct stripe_head *sh)
  162. {
  163. return sh->check_state || sh->reconstruct_state ||
  164. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  165. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  166. }
  167. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  168. {
  169. if (atomic_dec_and_test(&sh->count)) {
  170. BUG_ON(!list_empty(&sh->lru));
  171. BUG_ON(atomic_read(&conf->active_stripes)==0);
  172. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  173. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  174. list_add_tail(&sh->lru, &conf->delayed_list);
  175. blk_plug_device(conf->mddev->queue);
  176. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  177. sh->bm_seq - conf->seq_write > 0) {
  178. list_add_tail(&sh->lru, &conf->bitmap_list);
  179. blk_plug_device(conf->mddev->queue);
  180. } else {
  181. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  182. list_add_tail(&sh->lru, &conf->handle_list);
  183. }
  184. md_wakeup_thread(conf->mddev->thread);
  185. } else {
  186. BUG_ON(stripe_operations_active(sh));
  187. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  188. atomic_dec(&conf->preread_active_stripes);
  189. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  190. md_wakeup_thread(conf->mddev->thread);
  191. }
  192. atomic_dec(&conf->active_stripes);
  193. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  194. list_add_tail(&sh->lru, &conf->inactive_list);
  195. wake_up(&conf->wait_for_stripe);
  196. if (conf->retry_read_aligned)
  197. md_wakeup_thread(conf->mddev->thread);
  198. }
  199. }
  200. }
  201. }
  202. static void release_stripe(struct stripe_head *sh)
  203. {
  204. raid5_conf_t *conf = sh->raid_conf;
  205. unsigned long flags;
  206. spin_lock_irqsave(&conf->device_lock, flags);
  207. __release_stripe(conf, sh);
  208. spin_unlock_irqrestore(&conf->device_lock, flags);
  209. }
  210. static inline void remove_hash(struct stripe_head *sh)
  211. {
  212. pr_debug("remove_hash(), stripe %llu\n",
  213. (unsigned long long)sh->sector);
  214. hlist_del_init(&sh->hash);
  215. }
  216. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  217. {
  218. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  219. pr_debug("insert_hash(), stripe %llu\n",
  220. (unsigned long long)sh->sector);
  221. CHECK_DEVLOCK();
  222. hlist_add_head(&sh->hash, hp);
  223. }
  224. /* find an idle stripe, make sure it is unhashed, and return it. */
  225. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  226. {
  227. struct stripe_head *sh = NULL;
  228. struct list_head *first;
  229. CHECK_DEVLOCK();
  230. if (list_empty(&conf->inactive_list))
  231. goto out;
  232. first = conf->inactive_list.next;
  233. sh = list_entry(first, struct stripe_head, lru);
  234. list_del_init(first);
  235. remove_hash(sh);
  236. atomic_inc(&conf->active_stripes);
  237. out:
  238. return sh;
  239. }
  240. static void shrink_buffers(struct stripe_head *sh, int num)
  241. {
  242. struct page *p;
  243. int i;
  244. for (i=0; i<num ; i++) {
  245. p = sh->dev[i].page;
  246. if (!p)
  247. continue;
  248. sh->dev[i].page = NULL;
  249. put_page(p);
  250. }
  251. }
  252. static int grow_buffers(struct stripe_head *sh, int num)
  253. {
  254. int i;
  255. for (i=0; i<num; i++) {
  256. struct page *page;
  257. if (!(page = alloc_page(GFP_KERNEL))) {
  258. return 1;
  259. }
  260. sh->dev[i].page = page;
  261. }
  262. return 0;
  263. }
  264. static void raid5_build_block(struct stripe_head *sh, int i, int previous);
  265. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  266. struct stripe_head *sh);
  267. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  268. {
  269. raid5_conf_t *conf = sh->raid_conf;
  270. int i;
  271. BUG_ON(atomic_read(&sh->count) != 0);
  272. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  273. BUG_ON(stripe_operations_active(sh));
  274. CHECK_DEVLOCK();
  275. pr_debug("init_stripe called, stripe %llu\n",
  276. (unsigned long long)sh->sector);
  277. remove_hash(sh);
  278. sh->generation = conf->generation - previous;
  279. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  280. sh->sector = sector;
  281. stripe_set_idx(sector, conf, previous, sh);
  282. sh->state = 0;
  283. for (i = sh->disks; i--; ) {
  284. struct r5dev *dev = &sh->dev[i];
  285. if (dev->toread || dev->read || dev->towrite || dev->written ||
  286. test_bit(R5_LOCKED, &dev->flags)) {
  287. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  288. (unsigned long long)sh->sector, i, dev->toread,
  289. dev->read, dev->towrite, dev->written,
  290. test_bit(R5_LOCKED, &dev->flags));
  291. BUG();
  292. }
  293. dev->flags = 0;
  294. raid5_build_block(sh, i, previous);
  295. }
  296. insert_hash(conf, sh);
  297. }
  298. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
  299. short generation)
  300. {
  301. struct stripe_head *sh;
  302. struct hlist_node *hn;
  303. CHECK_DEVLOCK();
  304. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  305. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  306. if (sh->sector == sector && sh->generation == generation)
  307. return sh;
  308. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  309. return NULL;
  310. }
  311. static void unplug_slaves(mddev_t *mddev);
  312. static void raid5_unplug_device(struct request_queue *q);
  313. static struct stripe_head *
  314. get_active_stripe(raid5_conf_t *conf, sector_t sector,
  315. int previous, int noblock, int noquiesce)
  316. {
  317. struct stripe_head *sh;
  318. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  319. spin_lock_irq(&conf->device_lock);
  320. do {
  321. wait_event_lock_irq(conf->wait_for_stripe,
  322. conf->quiesce == 0 || noquiesce,
  323. conf->device_lock, /* nothing */);
  324. sh = __find_stripe(conf, sector, conf->generation - previous);
  325. if (!sh) {
  326. if (!conf->inactive_blocked)
  327. sh = get_free_stripe(conf);
  328. if (noblock && sh == NULL)
  329. break;
  330. if (!sh) {
  331. conf->inactive_blocked = 1;
  332. wait_event_lock_irq(conf->wait_for_stripe,
  333. !list_empty(&conf->inactive_list) &&
  334. (atomic_read(&conf->active_stripes)
  335. < (conf->max_nr_stripes *3/4)
  336. || !conf->inactive_blocked),
  337. conf->device_lock,
  338. raid5_unplug_device(conf->mddev->queue)
  339. );
  340. conf->inactive_blocked = 0;
  341. } else
  342. init_stripe(sh, sector, previous);
  343. } else {
  344. if (atomic_read(&sh->count)) {
  345. BUG_ON(!list_empty(&sh->lru)
  346. && !test_bit(STRIPE_EXPANDING, &sh->state));
  347. } else {
  348. if (!test_bit(STRIPE_HANDLE, &sh->state))
  349. atomic_inc(&conf->active_stripes);
  350. if (list_empty(&sh->lru) &&
  351. !test_bit(STRIPE_EXPANDING, &sh->state))
  352. BUG();
  353. list_del_init(&sh->lru);
  354. }
  355. }
  356. } while (sh == NULL);
  357. if (sh)
  358. atomic_inc(&sh->count);
  359. spin_unlock_irq(&conf->device_lock);
  360. return sh;
  361. }
  362. static void
  363. raid5_end_read_request(struct bio *bi, int error);
  364. static void
  365. raid5_end_write_request(struct bio *bi, int error);
  366. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  367. {
  368. raid5_conf_t *conf = sh->raid_conf;
  369. int i, disks = sh->disks;
  370. might_sleep();
  371. for (i = disks; i--; ) {
  372. int rw;
  373. struct bio *bi;
  374. mdk_rdev_t *rdev;
  375. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  376. rw = WRITE;
  377. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  378. rw = READ;
  379. else
  380. continue;
  381. bi = &sh->dev[i].req;
  382. bi->bi_rw = rw;
  383. if (rw == WRITE)
  384. bi->bi_end_io = raid5_end_write_request;
  385. else
  386. bi->bi_end_io = raid5_end_read_request;
  387. rcu_read_lock();
  388. rdev = rcu_dereference(conf->disks[i].rdev);
  389. if (rdev && test_bit(Faulty, &rdev->flags))
  390. rdev = NULL;
  391. if (rdev)
  392. atomic_inc(&rdev->nr_pending);
  393. rcu_read_unlock();
  394. if (rdev) {
  395. if (s->syncing || s->expanding || s->expanded)
  396. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  397. set_bit(STRIPE_IO_STARTED, &sh->state);
  398. bi->bi_bdev = rdev->bdev;
  399. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  400. __func__, (unsigned long long)sh->sector,
  401. bi->bi_rw, i);
  402. atomic_inc(&sh->count);
  403. bi->bi_sector = sh->sector + rdev->data_offset;
  404. bi->bi_flags = 1 << BIO_UPTODATE;
  405. bi->bi_vcnt = 1;
  406. bi->bi_max_vecs = 1;
  407. bi->bi_idx = 0;
  408. bi->bi_io_vec = &sh->dev[i].vec;
  409. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  410. bi->bi_io_vec[0].bv_offset = 0;
  411. bi->bi_size = STRIPE_SIZE;
  412. bi->bi_next = NULL;
  413. if (rw == WRITE &&
  414. test_bit(R5_ReWrite, &sh->dev[i].flags))
  415. atomic_add(STRIPE_SECTORS,
  416. &rdev->corrected_errors);
  417. generic_make_request(bi);
  418. } else {
  419. if (rw == WRITE)
  420. set_bit(STRIPE_DEGRADED, &sh->state);
  421. pr_debug("skip op %ld on disc %d for sector %llu\n",
  422. bi->bi_rw, i, (unsigned long long)sh->sector);
  423. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  424. set_bit(STRIPE_HANDLE, &sh->state);
  425. }
  426. }
  427. }
  428. static struct dma_async_tx_descriptor *
  429. async_copy_data(int frombio, struct bio *bio, struct page *page,
  430. sector_t sector, struct dma_async_tx_descriptor *tx)
  431. {
  432. struct bio_vec *bvl;
  433. struct page *bio_page;
  434. int i;
  435. int page_offset;
  436. if (bio->bi_sector >= sector)
  437. page_offset = (signed)(bio->bi_sector - sector) * 512;
  438. else
  439. page_offset = (signed)(sector - bio->bi_sector) * -512;
  440. bio_for_each_segment(bvl, bio, i) {
  441. int len = bio_iovec_idx(bio, i)->bv_len;
  442. int clen;
  443. int b_offset = 0;
  444. if (page_offset < 0) {
  445. b_offset = -page_offset;
  446. page_offset += b_offset;
  447. len -= b_offset;
  448. }
  449. if (len > 0 && page_offset + len > STRIPE_SIZE)
  450. clen = STRIPE_SIZE - page_offset;
  451. else
  452. clen = len;
  453. if (clen > 0) {
  454. b_offset += bio_iovec_idx(bio, i)->bv_offset;
  455. bio_page = bio_iovec_idx(bio, i)->bv_page;
  456. if (frombio)
  457. tx = async_memcpy(page, bio_page, page_offset,
  458. b_offset, clen,
  459. ASYNC_TX_DEP_ACK,
  460. tx, NULL, NULL);
  461. else
  462. tx = async_memcpy(bio_page, page, b_offset,
  463. page_offset, clen,
  464. ASYNC_TX_DEP_ACK,
  465. tx, NULL, NULL);
  466. }
  467. if (clen < len) /* hit end of page */
  468. break;
  469. page_offset += len;
  470. }
  471. return tx;
  472. }
  473. static void ops_complete_biofill(void *stripe_head_ref)
  474. {
  475. struct stripe_head *sh = stripe_head_ref;
  476. struct bio *return_bi = NULL;
  477. raid5_conf_t *conf = sh->raid_conf;
  478. int i;
  479. pr_debug("%s: stripe %llu\n", __func__,
  480. (unsigned long long)sh->sector);
  481. /* clear completed biofills */
  482. spin_lock_irq(&conf->device_lock);
  483. for (i = sh->disks; i--; ) {
  484. struct r5dev *dev = &sh->dev[i];
  485. /* acknowledge completion of a biofill operation */
  486. /* and check if we need to reply to a read request,
  487. * new R5_Wantfill requests are held off until
  488. * !STRIPE_BIOFILL_RUN
  489. */
  490. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  491. struct bio *rbi, *rbi2;
  492. BUG_ON(!dev->read);
  493. rbi = dev->read;
  494. dev->read = NULL;
  495. while (rbi && rbi->bi_sector <
  496. dev->sector + STRIPE_SECTORS) {
  497. rbi2 = r5_next_bio(rbi, dev->sector);
  498. if (!raid5_dec_bi_phys_segments(rbi)) {
  499. rbi->bi_next = return_bi;
  500. return_bi = rbi;
  501. }
  502. rbi = rbi2;
  503. }
  504. }
  505. }
  506. spin_unlock_irq(&conf->device_lock);
  507. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  508. return_io(return_bi);
  509. set_bit(STRIPE_HANDLE, &sh->state);
  510. release_stripe(sh);
  511. }
  512. static void ops_run_biofill(struct stripe_head *sh)
  513. {
  514. struct dma_async_tx_descriptor *tx = NULL;
  515. raid5_conf_t *conf = sh->raid_conf;
  516. int i;
  517. pr_debug("%s: stripe %llu\n", __func__,
  518. (unsigned long long)sh->sector);
  519. for (i = sh->disks; i--; ) {
  520. struct r5dev *dev = &sh->dev[i];
  521. if (test_bit(R5_Wantfill, &dev->flags)) {
  522. struct bio *rbi;
  523. spin_lock_irq(&conf->device_lock);
  524. dev->read = rbi = dev->toread;
  525. dev->toread = NULL;
  526. spin_unlock_irq(&conf->device_lock);
  527. while (rbi && rbi->bi_sector <
  528. dev->sector + STRIPE_SECTORS) {
  529. tx = async_copy_data(0, rbi, dev->page,
  530. dev->sector, tx);
  531. rbi = r5_next_bio(rbi, dev->sector);
  532. }
  533. }
  534. }
  535. atomic_inc(&sh->count);
  536. async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
  537. ops_complete_biofill, sh);
  538. }
  539. static void ops_complete_compute5(void *stripe_head_ref)
  540. {
  541. struct stripe_head *sh = stripe_head_ref;
  542. int target = sh->ops.target;
  543. struct r5dev *tgt = &sh->dev[target];
  544. pr_debug("%s: stripe %llu\n", __func__,
  545. (unsigned long long)sh->sector);
  546. set_bit(R5_UPTODATE, &tgt->flags);
  547. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  548. clear_bit(R5_Wantcompute, &tgt->flags);
  549. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  550. if (sh->check_state == check_state_compute_run)
  551. sh->check_state = check_state_compute_result;
  552. set_bit(STRIPE_HANDLE, &sh->state);
  553. release_stripe(sh);
  554. }
  555. static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
  556. {
  557. /* kernel stack size limits the total number of disks */
  558. int disks = sh->disks;
  559. struct page *xor_srcs[disks];
  560. int target = sh->ops.target;
  561. struct r5dev *tgt = &sh->dev[target];
  562. struct page *xor_dest = tgt->page;
  563. int count = 0;
  564. struct dma_async_tx_descriptor *tx;
  565. int i;
  566. pr_debug("%s: stripe %llu block: %d\n",
  567. __func__, (unsigned long long)sh->sector, target);
  568. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  569. for (i = disks; i--; )
  570. if (i != target)
  571. xor_srcs[count++] = sh->dev[i].page;
  572. atomic_inc(&sh->count);
  573. if (unlikely(count == 1))
  574. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  575. 0, NULL, ops_complete_compute5, sh);
  576. else
  577. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  578. ASYNC_TX_XOR_ZERO_DST, NULL,
  579. ops_complete_compute5, sh);
  580. return tx;
  581. }
  582. static void ops_complete_prexor(void *stripe_head_ref)
  583. {
  584. struct stripe_head *sh = stripe_head_ref;
  585. pr_debug("%s: stripe %llu\n", __func__,
  586. (unsigned long long)sh->sector);
  587. }
  588. static struct dma_async_tx_descriptor *
  589. ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  590. {
  591. /* kernel stack size limits the total number of disks */
  592. int disks = sh->disks;
  593. struct page *xor_srcs[disks];
  594. int count = 0, pd_idx = sh->pd_idx, i;
  595. /* existing parity data subtracted */
  596. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  597. pr_debug("%s: stripe %llu\n", __func__,
  598. (unsigned long long)sh->sector);
  599. for (i = disks; i--; ) {
  600. struct r5dev *dev = &sh->dev[i];
  601. /* Only process blocks that are known to be uptodate */
  602. if (test_bit(R5_Wantdrain, &dev->flags))
  603. xor_srcs[count++] = dev->page;
  604. }
  605. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  606. ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
  607. ops_complete_prexor, sh);
  608. return tx;
  609. }
  610. static struct dma_async_tx_descriptor *
  611. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  612. {
  613. int disks = sh->disks;
  614. int i;
  615. pr_debug("%s: stripe %llu\n", __func__,
  616. (unsigned long long)sh->sector);
  617. for (i = disks; i--; ) {
  618. struct r5dev *dev = &sh->dev[i];
  619. struct bio *chosen;
  620. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  621. struct bio *wbi;
  622. spin_lock(&sh->lock);
  623. chosen = dev->towrite;
  624. dev->towrite = NULL;
  625. BUG_ON(dev->written);
  626. wbi = dev->written = chosen;
  627. spin_unlock(&sh->lock);
  628. while (wbi && wbi->bi_sector <
  629. dev->sector + STRIPE_SECTORS) {
  630. tx = async_copy_data(1, wbi, dev->page,
  631. dev->sector, tx);
  632. wbi = r5_next_bio(wbi, dev->sector);
  633. }
  634. }
  635. }
  636. return tx;
  637. }
  638. static void ops_complete_postxor(void *stripe_head_ref)
  639. {
  640. struct stripe_head *sh = stripe_head_ref;
  641. int disks = sh->disks, i, pd_idx = sh->pd_idx;
  642. pr_debug("%s: stripe %llu\n", __func__,
  643. (unsigned long long)sh->sector);
  644. for (i = disks; i--; ) {
  645. struct r5dev *dev = &sh->dev[i];
  646. if (dev->written || i == pd_idx)
  647. set_bit(R5_UPTODATE, &dev->flags);
  648. }
  649. if (sh->reconstruct_state == reconstruct_state_drain_run)
  650. sh->reconstruct_state = reconstruct_state_drain_result;
  651. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  652. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  653. else {
  654. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  655. sh->reconstruct_state = reconstruct_state_result;
  656. }
  657. set_bit(STRIPE_HANDLE, &sh->state);
  658. release_stripe(sh);
  659. }
  660. static void
  661. ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  662. {
  663. /* kernel stack size limits the total number of disks */
  664. int disks = sh->disks;
  665. struct page *xor_srcs[disks];
  666. int count = 0, pd_idx = sh->pd_idx, i;
  667. struct page *xor_dest;
  668. int prexor = 0;
  669. unsigned long flags;
  670. pr_debug("%s: stripe %llu\n", __func__,
  671. (unsigned long long)sh->sector);
  672. /* check if prexor is active which means only process blocks
  673. * that are part of a read-modify-write (written)
  674. */
  675. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  676. prexor = 1;
  677. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  678. for (i = disks; i--; ) {
  679. struct r5dev *dev = &sh->dev[i];
  680. if (dev->written)
  681. xor_srcs[count++] = dev->page;
  682. }
  683. } else {
  684. xor_dest = sh->dev[pd_idx].page;
  685. for (i = disks; i--; ) {
  686. struct r5dev *dev = &sh->dev[i];
  687. if (i != pd_idx)
  688. xor_srcs[count++] = dev->page;
  689. }
  690. }
  691. /* 1/ if we prexor'd then the dest is reused as a source
  692. * 2/ if we did not prexor then we are redoing the parity
  693. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  694. * for the synchronous xor case
  695. */
  696. flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
  697. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  698. atomic_inc(&sh->count);
  699. if (unlikely(count == 1)) {
  700. flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
  701. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  702. flags, tx, ops_complete_postxor, sh);
  703. } else
  704. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  705. flags, tx, ops_complete_postxor, sh);
  706. }
  707. static void ops_complete_check(void *stripe_head_ref)
  708. {
  709. struct stripe_head *sh = stripe_head_ref;
  710. pr_debug("%s: stripe %llu\n", __func__,
  711. (unsigned long long)sh->sector);
  712. sh->check_state = check_state_check_result;
  713. set_bit(STRIPE_HANDLE, &sh->state);
  714. release_stripe(sh);
  715. }
  716. static void ops_run_check(struct stripe_head *sh)
  717. {
  718. /* kernel stack size limits the total number of disks */
  719. int disks = sh->disks;
  720. struct page *xor_srcs[disks];
  721. struct dma_async_tx_descriptor *tx;
  722. int count = 0, pd_idx = sh->pd_idx, i;
  723. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  724. pr_debug("%s: stripe %llu\n", __func__,
  725. (unsigned long long)sh->sector);
  726. for (i = disks; i--; ) {
  727. struct r5dev *dev = &sh->dev[i];
  728. if (i != pd_idx)
  729. xor_srcs[count++] = dev->page;
  730. }
  731. tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  732. &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
  733. atomic_inc(&sh->count);
  734. tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
  735. ops_complete_check, sh);
  736. }
  737. static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
  738. {
  739. int overlap_clear = 0, i, disks = sh->disks;
  740. struct dma_async_tx_descriptor *tx = NULL;
  741. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  742. ops_run_biofill(sh);
  743. overlap_clear++;
  744. }
  745. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  746. tx = ops_run_compute5(sh);
  747. /* terminate the chain if postxor is not set to be run */
  748. if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
  749. async_tx_ack(tx);
  750. }
  751. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  752. tx = ops_run_prexor(sh, tx);
  753. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  754. tx = ops_run_biodrain(sh, tx);
  755. overlap_clear++;
  756. }
  757. if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
  758. ops_run_postxor(sh, tx);
  759. if (test_bit(STRIPE_OP_CHECK, &ops_request))
  760. ops_run_check(sh);
  761. if (overlap_clear)
  762. for (i = disks; i--; ) {
  763. struct r5dev *dev = &sh->dev[i];
  764. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  765. wake_up(&sh->raid_conf->wait_for_overlap);
  766. }
  767. }
  768. static int grow_one_stripe(raid5_conf_t *conf)
  769. {
  770. struct stripe_head *sh;
  771. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  772. if (!sh)
  773. return 0;
  774. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  775. sh->raid_conf = conf;
  776. spin_lock_init(&sh->lock);
  777. if (grow_buffers(sh, conf->raid_disks)) {
  778. shrink_buffers(sh, conf->raid_disks);
  779. kmem_cache_free(conf->slab_cache, sh);
  780. return 0;
  781. }
  782. sh->disks = conf->raid_disks;
  783. /* we just created an active stripe so... */
  784. atomic_set(&sh->count, 1);
  785. atomic_inc(&conf->active_stripes);
  786. INIT_LIST_HEAD(&sh->lru);
  787. release_stripe(sh);
  788. return 1;
  789. }
  790. static int grow_stripes(raid5_conf_t *conf, int num)
  791. {
  792. struct kmem_cache *sc;
  793. int devs = conf->raid_disks;
  794. sprintf(conf->cache_name[0],
  795. "raid%d-%s", conf->level, mdname(conf->mddev));
  796. sprintf(conf->cache_name[1],
  797. "raid%d-%s-alt", conf->level, mdname(conf->mddev));
  798. conf->active_name = 0;
  799. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  800. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  801. 0, 0, NULL);
  802. if (!sc)
  803. return 1;
  804. conf->slab_cache = sc;
  805. conf->pool_size = devs;
  806. while (num--)
  807. if (!grow_one_stripe(conf))
  808. return 1;
  809. return 0;
  810. }
  811. static int resize_stripes(raid5_conf_t *conf, int newsize)
  812. {
  813. /* Make all the stripes able to hold 'newsize' devices.
  814. * New slots in each stripe get 'page' set to a new page.
  815. *
  816. * This happens in stages:
  817. * 1/ create a new kmem_cache and allocate the required number of
  818. * stripe_heads.
  819. * 2/ gather all the old stripe_heads and tranfer the pages across
  820. * to the new stripe_heads. This will have the side effect of
  821. * freezing the array as once all stripe_heads have been collected,
  822. * no IO will be possible. Old stripe heads are freed once their
  823. * pages have been transferred over, and the old kmem_cache is
  824. * freed when all stripes are done.
  825. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  826. * we simple return a failre status - no need to clean anything up.
  827. * 4/ allocate new pages for the new slots in the new stripe_heads.
  828. * If this fails, we don't bother trying the shrink the
  829. * stripe_heads down again, we just leave them as they are.
  830. * As each stripe_head is processed the new one is released into
  831. * active service.
  832. *
  833. * Once step2 is started, we cannot afford to wait for a write,
  834. * so we use GFP_NOIO allocations.
  835. */
  836. struct stripe_head *osh, *nsh;
  837. LIST_HEAD(newstripes);
  838. struct disk_info *ndisks;
  839. int err;
  840. struct kmem_cache *sc;
  841. int i;
  842. if (newsize <= conf->pool_size)
  843. return 0; /* never bother to shrink */
  844. err = md_allow_write(conf->mddev);
  845. if (err)
  846. return err;
  847. /* Step 1 */
  848. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  849. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  850. 0, 0, NULL);
  851. if (!sc)
  852. return -ENOMEM;
  853. for (i = conf->max_nr_stripes; i; i--) {
  854. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  855. if (!nsh)
  856. break;
  857. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  858. nsh->raid_conf = conf;
  859. spin_lock_init(&nsh->lock);
  860. list_add(&nsh->lru, &newstripes);
  861. }
  862. if (i) {
  863. /* didn't get enough, give up */
  864. while (!list_empty(&newstripes)) {
  865. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  866. list_del(&nsh->lru);
  867. kmem_cache_free(sc, nsh);
  868. }
  869. kmem_cache_destroy(sc);
  870. return -ENOMEM;
  871. }
  872. /* Step 2 - Must use GFP_NOIO now.
  873. * OK, we have enough stripes, start collecting inactive
  874. * stripes and copying them over
  875. */
  876. list_for_each_entry(nsh, &newstripes, lru) {
  877. spin_lock_irq(&conf->device_lock);
  878. wait_event_lock_irq(conf->wait_for_stripe,
  879. !list_empty(&conf->inactive_list),
  880. conf->device_lock,
  881. unplug_slaves(conf->mddev)
  882. );
  883. osh = get_free_stripe(conf);
  884. spin_unlock_irq(&conf->device_lock);
  885. atomic_set(&nsh->count, 1);
  886. for(i=0; i<conf->pool_size; i++)
  887. nsh->dev[i].page = osh->dev[i].page;
  888. for( ; i<newsize; i++)
  889. nsh->dev[i].page = NULL;
  890. kmem_cache_free(conf->slab_cache, osh);
  891. }
  892. kmem_cache_destroy(conf->slab_cache);
  893. /* Step 3.
  894. * At this point, we are holding all the stripes so the array
  895. * is completely stalled, so now is a good time to resize
  896. * conf->disks.
  897. */
  898. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  899. if (ndisks) {
  900. for (i=0; i<conf->raid_disks; i++)
  901. ndisks[i] = conf->disks[i];
  902. kfree(conf->disks);
  903. conf->disks = ndisks;
  904. } else
  905. err = -ENOMEM;
  906. /* Step 4, return new stripes to service */
  907. while(!list_empty(&newstripes)) {
  908. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  909. list_del_init(&nsh->lru);
  910. for (i=conf->raid_disks; i < newsize; i++)
  911. if (nsh->dev[i].page == NULL) {
  912. struct page *p = alloc_page(GFP_NOIO);
  913. nsh->dev[i].page = p;
  914. if (!p)
  915. err = -ENOMEM;
  916. }
  917. release_stripe(nsh);
  918. }
  919. /* critical section pass, GFP_NOIO no longer needed */
  920. conf->slab_cache = sc;
  921. conf->active_name = 1-conf->active_name;
  922. conf->pool_size = newsize;
  923. return err;
  924. }
  925. static int drop_one_stripe(raid5_conf_t *conf)
  926. {
  927. struct stripe_head *sh;
  928. spin_lock_irq(&conf->device_lock);
  929. sh = get_free_stripe(conf);
  930. spin_unlock_irq(&conf->device_lock);
  931. if (!sh)
  932. return 0;
  933. BUG_ON(atomic_read(&sh->count));
  934. shrink_buffers(sh, conf->pool_size);
  935. kmem_cache_free(conf->slab_cache, sh);
  936. atomic_dec(&conf->active_stripes);
  937. return 1;
  938. }
  939. static void shrink_stripes(raid5_conf_t *conf)
  940. {
  941. while (drop_one_stripe(conf))
  942. ;
  943. if (conf->slab_cache)
  944. kmem_cache_destroy(conf->slab_cache);
  945. conf->slab_cache = NULL;
  946. }
  947. static void raid5_end_read_request(struct bio * bi, int error)
  948. {
  949. struct stripe_head *sh = bi->bi_private;
  950. raid5_conf_t *conf = sh->raid_conf;
  951. int disks = sh->disks, i;
  952. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  953. char b[BDEVNAME_SIZE];
  954. mdk_rdev_t *rdev;
  955. for (i=0 ; i<disks; i++)
  956. if (bi == &sh->dev[i].req)
  957. break;
  958. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  959. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  960. uptodate);
  961. if (i == disks) {
  962. BUG();
  963. return;
  964. }
  965. if (uptodate) {
  966. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  967. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  968. rdev = conf->disks[i].rdev;
  969. printk_rl(KERN_INFO "raid5:%s: read error corrected"
  970. " (%lu sectors at %llu on %s)\n",
  971. mdname(conf->mddev), STRIPE_SECTORS,
  972. (unsigned long long)(sh->sector
  973. + rdev->data_offset),
  974. bdevname(rdev->bdev, b));
  975. clear_bit(R5_ReadError, &sh->dev[i].flags);
  976. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  977. }
  978. if (atomic_read(&conf->disks[i].rdev->read_errors))
  979. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  980. } else {
  981. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  982. int retry = 0;
  983. rdev = conf->disks[i].rdev;
  984. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  985. atomic_inc(&rdev->read_errors);
  986. if (conf->mddev->degraded)
  987. printk_rl(KERN_WARNING
  988. "raid5:%s: read error not correctable "
  989. "(sector %llu on %s).\n",
  990. mdname(conf->mddev),
  991. (unsigned long long)(sh->sector
  992. + rdev->data_offset),
  993. bdn);
  994. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  995. /* Oh, no!!! */
  996. printk_rl(KERN_WARNING
  997. "raid5:%s: read error NOT corrected!! "
  998. "(sector %llu on %s).\n",
  999. mdname(conf->mddev),
  1000. (unsigned long long)(sh->sector
  1001. + rdev->data_offset),
  1002. bdn);
  1003. else if (atomic_read(&rdev->read_errors)
  1004. > conf->max_nr_stripes)
  1005. printk(KERN_WARNING
  1006. "raid5:%s: Too many read errors, failing device %s.\n",
  1007. mdname(conf->mddev), bdn);
  1008. else
  1009. retry = 1;
  1010. if (retry)
  1011. set_bit(R5_ReadError, &sh->dev[i].flags);
  1012. else {
  1013. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1014. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1015. md_error(conf->mddev, rdev);
  1016. }
  1017. }
  1018. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1019. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1020. set_bit(STRIPE_HANDLE, &sh->state);
  1021. release_stripe(sh);
  1022. }
  1023. static void raid5_end_write_request(struct bio *bi, int error)
  1024. {
  1025. struct stripe_head *sh = bi->bi_private;
  1026. raid5_conf_t *conf = sh->raid_conf;
  1027. int disks = sh->disks, i;
  1028. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1029. for (i=0 ; i<disks; i++)
  1030. if (bi == &sh->dev[i].req)
  1031. break;
  1032. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1033. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1034. uptodate);
  1035. if (i == disks) {
  1036. BUG();
  1037. return;
  1038. }
  1039. if (!uptodate)
  1040. md_error(conf->mddev, conf->disks[i].rdev);
  1041. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1042. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1043. set_bit(STRIPE_HANDLE, &sh->state);
  1044. release_stripe(sh);
  1045. }
  1046. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
  1047. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  1048. {
  1049. struct r5dev *dev = &sh->dev[i];
  1050. bio_init(&dev->req);
  1051. dev->req.bi_io_vec = &dev->vec;
  1052. dev->req.bi_vcnt++;
  1053. dev->req.bi_max_vecs++;
  1054. dev->vec.bv_page = dev->page;
  1055. dev->vec.bv_len = STRIPE_SIZE;
  1056. dev->vec.bv_offset = 0;
  1057. dev->req.bi_sector = sh->sector;
  1058. dev->req.bi_private = sh;
  1059. dev->flags = 0;
  1060. dev->sector = compute_blocknr(sh, i, previous);
  1061. }
  1062. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1063. {
  1064. char b[BDEVNAME_SIZE];
  1065. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1066. pr_debug("raid5: error called\n");
  1067. if (!test_bit(Faulty, &rdev->flags)) {
  1068. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1069. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1070. unsigned long flags;
  1071. spin_lock_irqsave(&conf->device_lock, flags);
  1072. mddev->degraded++;
  1073. spin_unlock_irqrestore(&conf->device_lock, flags);
  1074. /*
  1075. * if recovery was running, make sure it aborts.
  1076. */
  1077. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1078. }
  1079. set_bit(Faulty, &rdev->flags);
  1080. printk(KERN_ALERT
  1081. "raid5: Disk failure on %s, disabling device.\n"
  1082. "raid5: Operation continuing on %d devices.\n",
  1083. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  1084. }
  1085. }
  1086. /*
  1087. * Input: a 'big' sector number,
  1088. * Output: index of the data and parity disk, and the sector # in them.
  1089. */
  1090. static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
  1091. int previous, int *dd_idx,
  1092. struct stripe_head *sh)
  1093. {
  1094. long stripe;
  1095. unsigned long chunk_number;
  1096. unsigned int chunk_offset;
  1097. int pd_idx, qd_idx;
  1098. int ddf_layout = 0;
  1099. sector_t new_sector;
  1100. int algorithm = previous ? conf->prev_algo
  1101. : conf->algorithm;
  1102. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1103. : conf->chunk_sectors;
  1104. int raid_disks = previous ? conf->previous_raid_disks
  1105. : conf->raid_disks;
  1106. int data_disks = raid_disks - conf->max_degraded;
  1107. /* First compute the information on this sector */
  1108. /*
  1109. * Compute the chunk number and the sector offset inside the chunk
  1110. */
  1111. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1112. chunk_number = r_sector;
  1113. BUG_ON(r_sector != chunk_number);
  1114. /*
  1115. * Compute the stripe number
  1116. */
  1117. stripe = chunk_number / data_disks;
  1118. /*
  1119. * Compute the data disk and parity disk indexes inside the stripe
  1120. */
  1121. *dd_idx = chunk_number % data_disks;
  1122. /*
  1123. * Select the parity disk based on the user selected algorithm.
  1124. */
  1125. pd_idx = qd_idx = ~0;
  1126. switch(conf->level) {
  1127. case 4:
  1128. pd_idx = data_disks;
  1129. break;
  1130. case 5:
  1131. switch (algorithm) {
  1132. case ALGORITHM_LEFT_ASYMMETRIC:
  1133. pd_idx = data_disks - stripe % raid_disks;
  1134. if (*dd_idx >= pd_idx)
  1135. (*dd_idx)++;
  1136. break;
  1137. case ALGORITHM_RIGHT_ASYMMETRIC:
  1138. pd_idx = stripe % raid_disks;
  1139. if (*dd_idx >= pd_idx)
  1140. (*dd_idx)++;
  1141. break;
  1142. case ALGORITHM_LEFT_SYMMETRIC:
  1143. pd_idx = data_disks - stripe % raid_disks;
  1144. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1145. break;
  1146. case ALGORITHM_RIGHT_SYMMETRIC:
  1147. pd_idx = stripe % raid_disks;
  1148. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1149. break;
  1150. case ALGORITHM_PARITY_0:
  1151. pd_idx = 0;
  1152. (*dd_idx)++;
  1153. break;
  1154. case ALGORITHM_PARITY_N:
  1155. pd_idx = data_disks;
  1156. break;
  1157. default:
  1158. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1159. algorithm);
  1160. BUG();
  1161. }
  1162. break;
  1163. case 6:
  1164. switch (algorithm) {
  1165. case ALGORITHM_LEFT_ASYMMETRIC:
  1166. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1167. qd_idx = pd_idx + 1;
  1168. if (pd_idx == raid_disks-1) {
  1169. (*dd_idx)++; /* Q D D D P */
  1170. qd_idx = 0;
  1171. } else if (*dd_idx >= pd_idx)
  1172. (*dd_idx) += 2; /* D D P Q D */
  1173. break;
  1174. case ALGORITHM_RIGHT_ASYMMETRIC:
  1175. pd_idx = stripe % raid_disks;
  1176. qd_idx = pd_idx + 1;
  1177. if (pd_idx == raid_disks-1) {
  1178. (*dd_idx)++; /* Q D D D P */
  1179. qd_idx = 0;
  1180. } else if (*dd_idx >= pd_idx)
  1181. (*dd_idx) += 2; /* D D P Q D */
  1182. break;
  1183. case ALGORITHM_LEFT_SYMMETRIC:
  1184. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1185. qd_idx = (pd_idx + 1) % raid_disks;
  1186. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1187. break;
  1188. case ALGORITHM_RIGHT_SYMMETRIC:
  1189. pd_idx = stripe % raid_disks;
  1190. qd_idx = (pd_idx + 1) % raid_disks;
  1191. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1192. break;
  1193. case ALGORITHM_PARITY_0:
  1194. pd_idx = 0;
  1195. qd_idx = 1;
  1196. (*dd_idx) += 2;
  1197. break;
  1198. case ALGORITHM_PARITY_N:
  1199. pd_idx = data_disks;
  1200. qd_idx = data_disks + 1;
  1201. break;
  1202. case ALGORITHM_ROTATING_ZERO_RESTART:
  1203. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1204. * of blocks for computing Q is different.
  1205. */
  1206. pd_idx = stripe % raid_disks;
  1207. qd_idx = pd_idx + 1;
  1208. if (pd_idx == raid_disks-1) {
  1209. (*dd_idx)++; /* Q D D D P */
  1210. qd_idx = 0;
  1211. } else if (*dd_idx >= pd_idx)
  1212. (*dd_idx) += 2; /* D D P Q D */
  1213. ddf_layout = 1;
  1214. break;
  1215. case ALGORITHM_ROTATING_N_RESTART:
  1216. /* Same a left_asymmetric, by first stripe is
  1217. * D D D P Q rather than
  1218. * Q D D D P
  1219. */
  1220. pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
  1221. qd_idx = pd_idx + 1;
  1222. if (pd_idx == raid_disks-1) {
  1223. (*dd_idx)++; /* Q D D D P */
  1224. qd_idx = 0;
  1225. } else if (*dd_idx >= pd_idx)
  1226. (*dd_idx) += 2; /* D D P Q D */
  1227. ddf_layout = 1;
  1228. break;
  1229. case ALGORITHM_ROTATING_N_CONTINUE:
  1230. /* Same as left_symmetric but Q is before P */
  1231. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1232. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1233. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1234. ddf_layout = 1;
  1235. break;
  1236. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1237. /* RAID5 left_asymmetric, with Q on last device */
  1238. pd_idx = data_disks - stripe % (raid_disks-1);
  1239. if (*dd_idx >= pd_idx)
  1240. (*dd_idx)++;
  1241. qd_idx = raid_disks - 1;
  1242. break;
  1243. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1244. pd_idx = stripe % (raid_disks-1);
  1245. if (*dd_idx >= pd_idx)
  1246. (*dd_idx)++;
  1247. qd_idx = raid_disks - 1;
  1248. break;
  1249. case ALGORITHM_LEFT_SYMMETRIC_6:
  1250. pd_idx = data_disks - stripe % (raid_disks-1);
  1251. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1252. qd_idx = raid_disks - 1;
  1253. break;
  1254. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1255. pd_idx = stripe % (raid_disks-1);
  1256. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1257. qd_idx = raid_disks - 1;
  1258. break;
  1259. case ALGORITHM_PARITY_0_6:
  1260. pd_idx = 0;
  1261. (*dd_idx)++;
  1262. qd_idx = raid_disks - 1;
  1263. break;
  1264. default:
  1265. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1266. algorithm);
  1267. BUG();
  1268. }
  1269. break;
  1270. }
  1271. if (sh) {
  1272. sh->pd_idx = pd_idx;
  1273. sh->qd_idx = qd_idx;
  1274. sh->ddf_layout = ddf_layout;
  1275. }
  1276. /*
  1277. * Finally, compute the new sector number
  1278. */
  1279. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1280. return new_sector;
  1281. }
  1282. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
  1283. {
  1284. raid5_conf_t *conf = sh->raid_conf;
  1285. int raid_disks = sh->disks;
  1286. int data_disks = raid_disks - conf->max_degraded;
  1287. sector_t new_sector = sh->sector, check;
  1288. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1289. : conf->chunk_sectors;
  1290. int algorithm = previous ? conf->prev_algo
  1291. : conf->algorithm;
  1292. sector_t stripe;
  1293. int chunk_offset;
  1294. int chunk_number, dummy1, dd_idx = i;
  1295. sector_t r_sector;
  1296. struct stripe_head sh2;
  1297. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1298. stripe = new_sector;
  1299. BUG_ON(new_sector != stripe);
  1300. if (i == sh->pd_idx)
  1301. return 0;
  1302. switch(conf->level) {
  1303. case 4: break;
  1304. case 5:
  1305. switch (algorithm) {
  1306. case ALGORITHM_LEFT_ASYMMETRIC:
  1307. case ALGORITHM_RIGHT_ASYMMETRIC:
  1308. if (i > sh->pd_idx)
  1309. i--;
  1310. break;
  1311. case ALGORITHM_LEFT_SYMMETRIC:
  1312. case ALGORITHM_RIGHT_SYMMETRIC:
  1313. if (i < sh->pd_idx)
  1314. i += raid_disks;
  1315. i -= (sh->pd_idx + 1);
  1316. break;
  1317. case ALGORITHM_PARITY_0:
  1318. i -= 1;
  1319. break;
  1320. case ALGORITHM_PARITY_N:
  1321. break;
  1322. default:
  1323. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1324. algorithm);
  1325. BUG();
  1326. }
  1327. break;
  1328. case 6:
  1329. if (i == sh->qd_idx)
  1330. return 0; /* It is the Q disk */
  1331. switch (algorithm) {
  1332. case ALGORITHM_LEFT_ASYMMETRIC:
  1333. case ALGORITHM_RIGHT_ASYMMETRIC:
  1334. case ALGORITHM_ROTATING_ZERO_RESTART:
  1335. case ALGORITHM_ROTATING_N_RESTART:
  1336. if (sh->pd_idx == raid_disks-1)
  1337. i--; /* Q D D D P */
  1338. else if (i > sh->pd_idx)
  1339. i -= 2; /* D D P Q D */
  1340. break;
  1341. case ALGORITHM_LEFT_SYMMETRIC:
  1342. case ALGORITHM_RIGHT_SYMMETRIC:
  1343. if (sh->pd_idx == raid_disks-1)
  1344. i--; /* Q D D D P */
  1345. else {
  1346. /* D D P Q D */
  1347. if (i < sh->pd_idx)
  1348. i += raid_disks;
  1349. i -= (sh->pd_idx + 2);
  1350. }
  1351. break;
  1352. case ALGORITHM_PARITY_0:
  1353. i -= 2;
  1354. break;
  1355. case ALGORITHM_PARITY_N:
  1356. break;
  1357. case ALGORITHM_ROTATING_N_CONTINUE:
  1358. if (sh->pd_idx == 0)
  1359. i--; /* P D D D Q */
  1360. else if (i > sh->pd_idx)
  1361. i -= 2; /* D D Q P D */
  1362. break;
  1363. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1364. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1365. if (i > sh->pd_idx)
  1366. i--;
  1367. break;
  1368. case ALGORITHM_LEFT_SYMMETRIC_6:
  1369. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1370. if (i < sh->pd_idx)
  1371. i += data_disks + 1;
  1372. i -= (sh->pd_idx + 1);
  1373. break;
  1374. case ALGORITHM_PARITY_0_6:
  1375. i -= 1;
  1376. break;
  1377. default:
  1378. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1379. algorithm);
  1380. BUG();
  1381. }
  1382. break;
  1383. }
  1384. chunk_number = stripe * data_disks + i;
  1385. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  1386. check = raid5_compute_sector(conf, r_sector,
  1387. previous, &dummy1, &sh2);
  1388. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  1389. || sh2.qd_idx != sh->qd_idx) {
  1390. printk(KERN_ERR "compute_blocknr: map not correct\n");
  1391. return 0;
  1392. }
  1393. return r_sector;
  1394. }
  1395. /*
  1396. * Copy data between a page in the stripe cache, and one or more bion
  1397. * The page could align with the middle of the bio, or there could be
  1398. * several bion, each with several bio_vecs, which cover part of the page
  1399. * Multiple bion are linked together on bi_next. There may be extras
  1400. * at the end of this list. We ignore them.
  1401. */
  1402. static void copy_data(int frombio, struct bio *bio,
  1403. struct page *page,
  1404. sector_t sector)
  1405. {
  1406. char *pa = page_address(page);
  1407. struct bio_vec *bvl;
  1408. int i;
  1409. int page_offset;
  1410. if (bio->bi_sector >= sector)
  1411. page_offset = (signed)(bio->bi_sector - sector) * 512;
  1412. else
  1413. page_offset = (signed)(sector - bio->bi_sector) * -512;
  1414. bio_for_each_segment(bvl, bio, i) {
  1415. int len = bio_iovec_idx(bio,i)->bv_len;
  1416. int clen;
  1417. int b_offset = 0;
  1418. if (page_offset < 0) {
  1419. b_offset = -page_offset;
  1420. page_offset += b_offset;
  1421. len -= b_offset;
  1422. }
  1423. if (len > 0 && page_offset + len > STRIPE_SIZE)
  1424. clen = STRIPE_SIZE - page_offset;
  1425. else clen = len;
  1426. if (clen > 0) {
  1427. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  1428. if (frombio)
  1429. memcpy(pa+page_offset, ba+b_offset, clen);
  1430. else
  1431. memcpy(ba+b_offset, pa+page_offset, clen);
  1432. __bio_kunmap_atomic(ba, KM_USER0);
  1433. }
  1434. if (clen < len) /* hit end of page */
  1435. break;
  1436. page_offset += len;
  1437. }
  1438. }
  1439. #define check_xor() do { \
  1440. if (count == MAX_XOR_BLOCKS) { \
  1441. xor_blocks(count, STRIPE_SIZE, dest, ptr);\
  1442. count = 0; \
  1443. } \
  1444. } while(0)
  1445. static void compute_parity6(struct stripe_head *sh, int method)
  1446. {
  1447. raid5_conf_t *conf = sh->raid_conf;
  1448. int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
  1449. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  1450. struct bio *chosen;
  1451. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1452. void *ptrs[syndrome_disks+2];
  1453. pd_idx = sh->pd_idx;
  1454. qd_idx = sh->qd_idx;
  1455. d0_idx = raid6_d0(sh);
  1456. pr_debug("compute_parity, stripe %llu, method %d\n",
  1457. (unsigned long long)sh->sector, method);
  1458. switch(method) {
  1459. case READ_MODIFY_WRITE:
  1460. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  1461. case RECONSTRUCT_WRITE:
  1462. for (i= disks; i-- ;)
  1463. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  1464. chosen = sh->dev[i].towrite;
  1465. sh->dev[i].towrite = NULL;
  1466. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1467. wake_up(&conf->wait_for_overlap);
  1468. BUG_ON(sh->dev[i].written);
  1469. sh->dev[i].written = chosen;
  1470. }
  1471. break;
  1472. case CHECK_PARITY:
  1473. BUG(); /* Not implemented yet */
  1474. }
  1475. for (i = disks; i--;)
  1476. if (sh->dev[i].written) {
  1477. sector_t sector = sh->dev[i].sector;
  1478. struct bio *wbi = sh->dev[i].written;
  1479. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  1480. copy_data(1, wbi, sh->dev[i].page, sector);
  1481. wbi = r5_next_bio(wbi, sector);
  1482. }
  1483. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1484. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1485. }
  1486. /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
  1487. for (i = 0; i < disks; i++)
  1488. ptrs[i] = (void *)raid6_empty_zero_page;
  1489. count = 0;
  1490. i = d0_idx;
  1491. do {
  1492. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1493. ptrs[slot] = page_address(sh->dev[i].page);
  1494. if (slot < syndrome_disks &&
  1495. !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  1496. printk(KERN_ERR "block %d/%d not uptodate "
  1497. "on parity calc\n", i, count);
  1498. BUG();
  1499. }
  1500. i = raid6_next_disk(i, disks);
  1501. } while (i != d0_idx);
  1502. BUG_ON(count != syndrome_disks);
  1503. raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
  1504. switch(method) {
  1505. case RECONSTRUCT_WRITE:
  1506. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1507. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1508. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1509. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  1510. break;
  1511. case UPDATE_PARITY:
  1512. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1513. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1514. break;
  1515. }
  1516. }
  1517. /* Compute one missing block */
  1518. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  1519. {
  1520. int i, count, disks = sh->disks;
  1521. void *ptr[MAX_XOR_BLOCKS], *dest, *p;
  1522. int qd_idx = sh->qd_idx;
  1523. pr_debug("compute_block_1, stripe %llu, idx %d\n",
  1524. (unsigned long long)sh->sector, dd_idx);
  1525. if ( dd_idx == qd_idx ) {
  1526. /* We're actually computing the Q drive */
  1527. compute_parity6(sh, UPDATE_PARITY);
  1528. } else {
  1529. dest = page_address(sh->dev[dd_idx].page);
  1530. if (!nozero) memset(dest, 0, STRIPE_SIZE);
  1531. count = 0;
  1532. for (i = disks ; i--; ) {
  1533. if (i == dd_idx || i == qd_idx)
  1534. continue;
  1535. p = page_address(sh->dev[i].page);
  1536. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1537. ptr[count++] = p;
  1538. else
  1539. printk("compute_block() %d, stripe %llu, %d"
  1540. " not present\n", dd_idx,
  1541. (unsigned long long)sh->sector, i);
  1542. check_xor();
  1543. }
  1544. if (count)
  1545. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  1546. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1547. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1548. }
  1549. }
  1550. /* Compute two missing blocks */
  1551. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1552. {
  1553. int i, count, disks = sh->disks;
  1554. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  1555. int d0_idx = raid6_d0(sh);
  1556. int faila = -1, failb = -1;
  1557. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1558. void *ptrs[syndrome_disks+2];
  1559. for (i = 0; i < disks ; i++)
  1560. ptrs[i] = (void *)raid6_empty_zero_page;
  1561. count = 0;
  1562. i = d0_idx;
  1563. do {
  1564. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1565. ptrs[slot] = page_address(sh->dev[i].page);
  1566. if (i == dd_idx1)
  1567. faila = slot;
  1568. if (i == dd_idx2)
  1569. failb = slot;
  1570. i = raid6_next_disk(i, disks);
  1571. } while (i != d0_idx);
  1572. BUG_ON(count != syndrome_disks);
  1573. BUG_ON(faila == failb);
  1574. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1575. pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1576. (unsigned long long)sh->sector, dd_idx1, dd_idx2,
  1577. faila, failb);
  1578. if (failb == syndrome_disks+1) {
  1579. /* Q disk is one of the missing disks */
  1580. if (faila == syndrome_disks) {
  1581. /* Missing P+Q, just recompute */
  1582. compute_parity6(sh, UPDATE_PARITY);
  1583. return;
  1584. } else {
  1585. /* We're missing D+Q; recompute D from P */
  1586. compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
  1587. dd_idx2 : dd_idx1),
  1588. 0);
  1589. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1590. return;
  1591. }
  1592. }
  1593. /* We're missing D+P or D+D; */
  1594. if (failb == syndrome_disks) {
  1595. /* We're missing D+P. */
  1596. raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
  1597. } else {
  1598. /* We're missing D+D. */
  1599. raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
  1600. ptrs);
  1601. }
  1602. /* Both the above update both missing blocks */
  1603. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1604. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1605. }
  1606. static void
  1607. schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
  1608. int rcw, int expand)
  1609. {
  1610. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1611. if (rcw) {
  1612. /* if we are not expanding this is a proper write request, and
  1613. * there will be bios with new data to be drained into the
  1614. * stripe cache
  1615. */
  1616. if (!expand) {
  1617. sh->reconstruct_state = reconstruct_state_drain_run;
  1618. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1619. } else
  1620. sh->reconstruct_state = reconstruct_state_run;
  1621. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1622. for (i = disks; i--; ) {
  1623. struct r5dev *dev = &sh->dev[i];
  1624. if (dev->towrite) {
  1625. set_bit(R5_LOCKED, &dev->flags);
  1626. set_bit(R5_Wantdrain, &dev->flags);
  1627. if (!expand)
  1628. clear_bit(R5_UPTODATE, &dev->flags);
  1629. s->locked++;
  1630. }
  1631. }
  1632. if (s->locked + 1 == disks)
  1633. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1634. atomic_inc(&sh->raid_conf->pending_full_writes);
  1635. } else {
  1636. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1637. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1638. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1639. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1640. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1641. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1642. for (i = disks; i--; ) {
  1643. struct r5dev *dev = &sh->dev[i];
  1644. if (i == pd_idx)
  1645. continue;
  1646. if (dev->towrite &&
  1647. (test_bit(R5_UPTODATE, &dev->flags) ||
  1648. test_bit(R5_Wantcompute, &dev->flags))) {
  1649. set_bit(R5_Wantdrain, &dev->flags);
  1650. set_bit(R5_LOCKED, &dev->flags);
  1651. clear_bit(R5_UPTODATE, &dev->flags);
  1652. s->locked++;
  1653. }
  1654. }
  1655. }
  1656. /* keep the parity disk locked while asynchronous operations
  1657. * are in flight
  1658. */
  1659. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1660. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1661. s->locked++;
  1662. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  1663. __func__, (unsigned long long)sh->sector,
  1664. s->locked, s->ops_request);
  1665. }
  1666. /*
  1667. * Each stripe/dev can have one or more bion attached.
  1668. * toread/towrite point to the first in a chain.
  1669. * The bi_next chain must be in order.
  1670. */
  1671. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1672. {
  1673. struct bio **bip;
  1674. raid5_conf_t *conf = sh->raid_conf;
  1675. int firstwrite=0;
  1676. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1677. (unsigned long long)bi->bi_sector,
  1678. (unsigned long long)sh->sector);
  1679. spin_lock(&sh->lock);
  1680. spin_lock_irq(&conf->device_lock);
  1681. if (forwrite) {
  1682. bip = &sh->dev[dd_idx].towrite;
  1683. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1684. firstwrite = 1;
  1685. } else
  1686. bip = &sh->dev[dd_idx].toread;
  1687. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1688. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1689. goto overlap;
  1690. bip = & (*bip)->bi_next;
  1691. }
  1692. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1693. goto overlap;
  1694. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1695. if (*bip)
  1696. bi->bi_next = *bip;
  1697. *bip = bi;
  1698. bi->bi_phys_segments++;
  1699. spin_unlock_irq(&conf->device_lock);
  1700. spin_unlock(&sh->lock);
  1701. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1702. (unsigned long long)bi->bi_sector,
  1703. (unsigned long long)sh->sector, dd_idx);
  1704. if (conf->mddev->bitmap && firstwrite) {
  1705. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1706. STRIPE_SECTORS, 0);
  1707. sh->bm_seq = conf->seq_flush+1;
  1708. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1709. }
  1710. if (forwrite) {
  1711. /* check if page is covered */
  1712. sector_t sector = sh->dev[dd_idx].sector;
  1713. for (bi=sh->dev[dd_idx].towrite;
  1714. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1715. bi && bi->bi_sector <= sector;
  1716. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1717. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1718. sector = bi->bi_sector + (bi->bi_size>>9);
  1719. }
  1720. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1721. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1722. }
  1723. return 1;
  1724. overlap:
  1725. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1726. spin_unlock_irq(&conf->device_lock);
  1727. spin_unlock(&sh->lock);
  1728. return 0;
  1729. }
  1730. static void end_reshape(raid5_conf_t *conf);
  1731. static int page_is_zero(struct page *p)
  1732. {
  1733. char *a = page_address(p);
  1734. return ((*(u32*)a) == 0 &&
  1735. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1736. }
  1737. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  1738. struct stripe_head *sh)
  1739. {
  1740. int sectors_per_chunk =
  1741. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  1742. int dd_idx;
  1743. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1744. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  1745. raid5_compute_sector(conf,
  1746. stripe * (disks - conf->max_degraded)
  1747. *sectors_per_chunk + chunk_offset,
  1748. previous,
  1749. &dd_idx, sh);
  1750. }
  1751. static void
  1752. handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
  1753. struct stripe_head_state *s, int disks,
  1754. struct bio **return_bi)
  1755. {
  1756. int i;
  1757. for (i = disks; i--; ) {
  1758. struct bio *bi;
  1759. int bitmap_end = 0;
  1760. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1761. mdk_rdev_t *rdev;
  1762. rcu_read_lock();
  1763. rdev = rcu_dereference(conf->disks[i].rdev);
  1764. if (rdev && test_bit(In_sync, &rdev->flags))
  1765. /* multiple read failures in one stripe */
  1766. md_error(conf->mddev, rdev);
  1767. rcu_read_unlock();
  1768. }
  1769. spin_lock_irq(&conf->device_lock);
  1770. /* fail all writes first */
  1771. bi = sh->dev[i].towrite;
  1772. sh->dev[i].towrite = NULL;
  1773. if (bi) {
  1774. s->to_write--;
  1775. bitmap_end = 1;
  1776. }
  1777. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1778. wake_up(&conf->wait_for_overlap);
  1779. while (bi && bi->bi_sector <
  1780. sh->dev[i].sector + STRIPE_SECTORS) {
  1781. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1782. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1783. if (!raid5_dec_bi_phys_segments(bi)) {
  1784. md_write_end(conf->mddev);
  1785. bi->bi_next = *return_bi;
  1786. *return_bi = bi;
  1787. }
  1788. bi = nextbi;
  1789. }
  1790. /* and fail all 'written' */
  1791. bi = sh->dev[i].written;
  1792. sh->dev[i].written = NULL;
  1793. if (bi) bitmap_end = 1;
  1794. while (bi && bi->bi_sector <
  1795. sh->dev[i].sector + STRIPE_SECTORS) {
  1796. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1797. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1798. if (!raid5_dec_bi_phys_segments(bi)) {
  1799. md_write_end(conf->mddev);
  1800. bi->bi_next = *return_bi;
  1801. *return_bi = bi;
  1802. }
  1803. bi = bi2;
  1804. }
  1805. /* fail any reads if this device is non-operational and
  1806. * the data has not reached the cache yet.
  1807. */
  1808. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  1809. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1810. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  1811. bi = sh->dev[i].toread;
  1812. sh->dev[i].toread = NULL;
  1813. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1814. wake_up(&conf->wait_for_overlap);
  1815. if (bi) s->to_read--;
  1816. while (bi && bi->bi_sector <
  1817. sh->dev[i].sector + STRIPE_SECTORS) {
  1818. struct bio *nextbi =
  1819. r5_next_bio(bi, sh->dev[i].sector);
  1820. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1821. if (!raid5_dec_bi_phys_segments(bi)) {
  1822. bi->bi_next = *return_bi;
  1823. *return_bi = bi;
  1824. }
  1825. bi = nextbi;
  1826. }
  1827. }
  1828. spin_unlock_irq(&conf->device_lock);
  1829. if (bitmap_end)
  1830. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1831. STRIPE_SECTORS, 0, 0);
  1832. }
  1833. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  1834. if (atomic_dec_and_test(&conf->pending_full_writes))
  1835. md_wakeup_thread(conf->mddev->thread);
  1836. }
  1837. /* fetch_block5 - checks the given member device to see if its data needs
  1838. * to be read or computed to satisfy a request.
  1839. *
  1840. * Returns 1 when no more member devices need to be checked, otherwise returns
  1841. * 0 to tell the loop in handle_stripe_fill5 to continue
  1842. */
  1843. static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
  1844. int disk_idx, int disks)
  1845. {
  1846. struct r5dev *dev = &sh->dev[disk_idx];
  1847. struct r5dev *failed_dev = &sh->dev[s->failed_num];
  1848. /* is the data in this block needed, and can we get it? */
  1849. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1850. !test_bit(R5_UPTODATE, &dev->flags) &&
  1851. (dev->toread ||
  1852. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1853. s->syncing || s->expanding ||
  1854. (s->failed &&
  1855. (failed_dev->toread ||
  1856. (failed_dev->towrite &&
  1857. !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
  1858. /* We would like to get this block, possibly by computing it,
  1859. * otherwise read it if the backing disk is insync
  1860. */
  1861. if ((s->uptodate == disks - 1) &&
  1862. (s->failed && disk_idx == s->failed_num)) {
  1863. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  1864. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  1865. set_bit(R5_Wantcompute, &dev->flags);
  1866. sh->ops.target = disk_idx;
  1867. s->req_compute = 1;
  1868. /* Careful: from this point on 'uptodate' is in the eye
  1869. * of raid5_run_ops which services 'compute' operations
  1870. * before writes. R5_Wantcompute flags a block that will
  1871. * be R5_UPTODATE by the time it is needed for a
  1872. * subsequent operation.
  1873. */
  1874. s->uptodate++;
  1875. return 1; /* uptodate + compute == disks */
  1876. } else if (test_bit(R5_Insync, &dev->flags)) {
  1877. set_bit(R5_LOCKED, &dev->flags);
  1878. set_bit(R5_Wantread, &dev->flags);
  1879. s->locked++;
  1880. pr_debug("Reading block %d (sync=%d)\n", disk_idx,
  1881. s->syncing);
  1882. }
  1883. }
  1884. return 0;
  1885. }
  1886. /**
  1887. * handle_stripe_fill5 - read or compute data to satisfy pending requests.
  1888. */
  1889. static void handle_stripe_fill5(struct stripe_head *sh,
  1890. struct stripe_head_state *s, int disks)
  1891. {
  1892. int i;
  1893. /* look for blocks to read/compute, skip this if a compute
  1894. * is already in flight, or if the stripe contents are in the
  1895. * midst of changing due to a write
  1896. */
  1897. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  1898. !sh->reconstruct_state)
  1899. for (i = disks; i--; )
  1900. if (fetch_block5(sh, s, i, disks))
  1901. break;
  1902. set_bit(STRIPE_HANDLE, &sh->state);
  1903. }
  1904. static void handle_stripe_fill6(struct stripe_head *sh,
  1905. struct stripe_head_state *s, struct r6_state *r6s,
  1906. int disks)
  1907. {
  1908. int i;
  1909. for (i = disks; i--; ) {
  1910. struct r5dev *dev = &sh->dev[i];
  1911. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1912. !test_bit(R5_UPTODATE, &dev->flags) &&
  1913. (dev->toread || (dev->towrite &&
  1914. !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1915. s->syncing || s->expanding ||
  1916. (s->failed >= 1 &&
  1917. (sh->dev[r6s->failed_num[0]].toread ||
  1918. s->to_write)) ||
  1919. (s->failed >= 2 &&
  1920. (sh->dev[r6s->failed_num[1]].toread ||
  1921. s->to_write)))) {
  1922. /* we would like to get this block, possibly
  1923. * by computing it, but we might not be able to
  1924. */
  1925. if ((s->uptodate == disks - 1) &&
  1926. (s->failed && (i == r6s->failed_num[0] ||
  1927. i == r6s->failed_num[1]))) {
  1928. pr_debug("Computing stripe %llu block %d\n",
  1929. (unsigned long long)sh->sector, i);
  1930. compute_block_1(sh, i, 0);
  1931. s->uptodate++;
  1932. } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
  1933. /* Computing 2-failure is *very* expensive; only
  1934. * do it if failed >= 2
  1935. */
  1936. int other;
  1937. for (other = disks; other--; ) {
  1938. if (other == i)
  1939. continue;
  1940. if (!test_bit(R5_UPTODATE,
  1941. &sh->dev[other].flags))
  1942. break;
  1943. }
  1944. BUG_ON(other < 0);
  1945. pr_debug("Computing stripe %llu blocks %d,%d\n",
  1946. (unsigned long long)sh->sector,
  1947. i, other);
  1948. compute_block_2(sh, i, other);
  1949. s->uptodate += 2;
  1950. } else if (test_bit(R5_Insync, &dev->flags)) {
  1951. set_bit(R5_LOCKED, &dev->flags);
  1952. set_bit(R5_Wantread, &dev->flags);
  1953. s->locked++;
  1954. pr_debug("Reading block %d (sync=%d)\n",
  1955. i, s->syncing);
  1956. }
  1957. }
  1958. }
  1959. set_bit(STRIPE_HANDLE, &sh->state);
  1960. }
  1961. /* handle_stripe_clean_event
  1962. * any written block on an uptodate or failed drive can be returned.
  1963. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1964. * never LOCKED, so we don't need to test 'failed' directly.
  1965. */
  1966. static void handle_stripe_clean_event(raid5_conf_t *conf,
  1967. struct stripe_head *sh, int disks, struct bio **return_bi)
  1968. {
  1969. int i;
  1970. struct r5dev *dev;
  1971. for (i = disks; i--; )
  1972. if (sh->dev[i].written) {
  1973. dev = &sh->dev[i];
  1974. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1975. test_bit(R5_UPTODATE, &dev->flags)) {
  1976. /* We can return any write requests */
  1977. struct bio *wbi, *wbi2;
  1978. int bitmap_end = 0;
  1979. pr_debug("Return write for disc %d\n", i);
  1980. spin_lock_irq(&conf->device_lock);
  1981. wbi = dev->written;
  1982. dev->written = NULL;
  1983. while (wbi && wbi->bi_sector <
  1984. dev->sector + STRIPE_SECTORS) {
  1985. wbi2 = r5_next_bio(wbi, dev->sector);
  1986. if (!raid5_dec_bi_phys_segments(wbi)) {
  1987. md_write_end(conf->mddev);
  1988. wbi->bi_next = *return_bi;
  1989. *return_bi = wbi;
  1990. }
  1991. wbi = wbi2;
  1992. }
  1993. if (dev->towrite == NULL)
  1994. bitmap_end = 1;
  1995. spin_unlock_irq(&conf->device_lock);
  1996. if (bitmap_end)
  1997. bitmap_endwrite(conf->mddev->bitmap,
  1998. sh->sector,
  1999. STRIPE_SECTORS,
  2000. !test_bit(STRIPE_DEGRADED, &sh->state),
  2001. 0);
  2002. }
  2003. }
  2004. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2005. if (atomic_dec_and_test(&conf->pending_full_writes))
  2006. md_wakeup_thread(conf->mddev->thread);
  2007. }
  2008. static void handle_stripe_dirtying5(raid5_conf_t *conf,
  2009. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  2010. {
  2011. int rmw = 0, rcw = 0, i;
  2012. for (i = disks; i--; ) {
  2013. /* would I have to read this buffer for read_modify_write */
  2014. struct r5dev *dev = &sh->dev[i];
  2015. if ((dev->towrite || i == sh->pd_idx) &&
  2016. !test_bit(R5_LOCKED, &dev->flags) &&
  2017. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2018. test_bit(R5_Wantcompute, &dev->flags))) {
  2019. if (test_bit(R5_Insync, &dev->flags))
  2020. rmw++;
  2021. else
  2022. rmw += 2*disks; /* cannot read it */
  2023. }
  2024. /* Would I have to read this buffer for reconstruct_write */
  2025. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2026. !test_bit(R5_LOCKED, &dev->flags) &&
  2027. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2028. test_bit(R5_Wantcompute, &dev->flags))) {
  2029. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2030. else
  2031. rcw += 2*disks;
  2032. }
  2033. }
  2034. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2035. (unsigned long long)sh->sector, rmw, rcw);
  2036. set_bit(STRIPE_HANDLE, &sh->state);
  2037. if (rmw < rcw && rmw > 0)
  2038. /* prefer read-modify-write, but need to get some data */
  2039. for (i = disks; i--; ) {
  2040. struct r5dev *dev = &sh->dev[i];
  2041. if ((dev->towrite || i == sh->pd_idx) &&
  2042. !test_bit(R5_LOCKED, &dev->flags) &&
  2043. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2044. test_bit(R5_Wantcompute, &dev->flags)) &&
  2045. test_bit(R5_Insync, &dev->flags)) {
  2046. if (
  2047. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2048. pr_debug("Read_old block "
  2049. "%d for r-m-w\n", i);
  2050. set_bit(R5_LOCKED, &dev->flags);
  2051. set_bit(R5_Wantread, &dev->flags);
  2052. s->locked++;
  2053. } else {
  2054. set_bit(STRIPE_DELAYED, &sh->state);
  2055. set_bit(STRIPE_HANDLE, &sh->state);
  2056. }
  2057. }
  2058. }
  2059. if (rcw <= rmw && rcw > 0)
  2060. /* want reconstruct write, but need to get some data */
  2061. for (i = disks; i--; ) {
  2062. struct r5dev *dev = &sh->dev[i];
  2063. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2064. i != sh->pd_idx &&
  2065. !test_bit(R5_LOCKED, &dev->flags) &&
  2066. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2067. test_bit(R5_Wantcompute, &dev->flags)) &&
  2068. test_bit(R5_Insync, &dev->flags)) {
  2069. if (
  2070. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2071. pr_debug("Read_old block "
  2072. "%d for Reconstruct\n", i);
  2073. set_bit(R5_LOCKED, &dev->flags);
  2074. set_bit(R5_Wantread, &dev->flags);
  2075. s->locked++;
  2076. } else {
  2077. set_bit(STRIPE_DELAYED, &sh->state);
  2078. set_bit(STRIPE_HANDLE, &sh->state);
  2079. }
  2080. }
  2081. }
  2082. /* now if nothing is locked, and if we have enough data,
  2083. * we can start a write request
  2084. */
  2085. /* since handle_stripe can be called at any time we need to handle the
  2086. * case where a compute block operation has been submitted and then a
  2087. * subsequent call wants to start a write request. raid5_run_ops only
  2088. * handles the case where compute block and postxor are requested
  2089. * simultaneously. If this is not the case then new writes need to be
  2090. * held off until the compute completes.
  2091. */
  2092. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2093. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2094. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2095. schedule_reconstruction5(sh, s, rcw == 0, 0);
  2096. }
  2097. static void handle_stripe_dirtying6(raid5_conf_t *conf,
  2098. struct stripe_head *sh, struct stripe_head_state *s,
  2099. struct r6_state *r6s, int disks)
  2100. {
  2101. int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
  2102. int qd_idx = sh->qd_idx;
  2103. for (i = disks; i--; ) {
  2104. struct r5dev *dev = &sh->dev[i];
  2105. /* Would I have to read this buffer for reconstruct_write */
  2106. if (!test_bit(R5_OVERWRITE, &dev->flags)
  2107. && i != pd_idx && i != qd_idx
  2108. && (!test_bit(R5_LOCKED, &dev->flags)
  2109. ) &&
  2110. !test_bit(R5_UPTODATE, &dev->flags)) {
  2111. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2112. else {
  2113. pr_debug("raid6: must_compute: "
  2114. "disk %d flags=%#lx\n", i, dev->flags);
  2115. must_compute++;
  2116. }
  2117. }
  2118. }
  2119. pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
  2120. (unsigned long long)sh->sector, rcw, must_compute);
  2121. set_bit(STRIPE_HANDLE, &sh->state);
  2122. if (rcw > 0)
  2123. /* want reconstruct write, but need to get some data */
  2124. for (i = disks; i--; ) {
  2125. struct r5dev *dev = &sh->dev[i];
  2126. if (!test_bit(R5_OVERWRITE, &dev->flags)
  2127. && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
  2128. && !test_bit(R5_LOCKED, &dev->flags) &&
  2129. !test_bit(R5_UPTODATE, &dev->flags) &&
  2130. test_bit(R5_Insync, &dev->flags)) {
  2131. if (
  2132. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2133. pr_debug("Read_old stripe %llu "
  2134. "block %d for Reconstruct\n",
  2135. (unsigned long long)sh->sector, i);
  2136. set_bit(R5_LOCKED, &dev->flags);
  2137. set_bit(R5_Wantread, &dev->flags);
  2138. s->locked++;
  2139. } else {
  2140. pr_debug("Request delayed stripe %llu "
  2141. "block %d for Reconstruct\n",
  2142. (unsigned long long)sh->sector, i);
  2143. set_bit(STRIPE_DELAYED, &sh->state);
  2144. set_bit(STRIPE_HANDLE, &sh->state);
  2145. }
  2146. }
  2147. }
  2148. /* now if nothing is locked, and if we have enough data, we can start a
  2149. * write request
  2150. */
  2151. if (s->locked == 0 && rcw == 0 &&
  2152. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2153. if (must_compute > 0) {
  2154. /* We have failed blocks and need to compute them */
  2155. switch (s->failed) {
  2156. case 0:
  2157. BUG();
  2158. case 1:
  2159. compute_block_1(sh, r6s->failed_num[0], 0);
  2160. break;
  2161. case 2:
  2162. compute_block_2(sh, r6s->failed_num[0],
  2163. r6s->failed_num[1]);
  2164. break;
  2165. default: /* This request should have been failed? */
  2166. BUG();
  2167. }
  2168. }
  2169. pr_debug("Computing parity for stripe %llu\n",
  2170. (unsigned long long)sh->sector);
  2171. compute_parity6(sh, RECONSTRUCT_WRITE);
  2172. /* now every locked buffer is ready to be written */
  2173. for (i = disks; i--; )
  2174. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2175. pr_debug("Writing stripe %llu block %d\n",
  2176. (unsigned long long)sh->sector, i);
  2177. s->locked++;
  2178. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2179. }
  2180. if (s->locked == disks)
  2181. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  2182. atomic_inc(&conf->pending_full_writes);
  2183. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2184. set_bit(STRIPE_INSYNC, &sh->state);
  2185. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2186. atomic_dec(&conf->preread_active_stripes);
  2187. if (atomic_read(&conf->preread_active_stripes) <
  2188. IO_THRESHOLD)
  2189. md_wakeup_thread(conf->mddev->thread);
  2190. }
  2191. }
  2192. }
  2193. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  2194. struct stripe_head_state *s, int disks)
  2195. {
  2196. struct r5dev *dev = NULL;
  2197. set_bit(STRIPE_HANDLE, &sh->state);
  2198. switch (sh->check_state) {
  2199. case check_state_idle:
  2200. /* start a new check operation if there are no failures */
  2201. if (s->failed == 0) {
  2202. BUG_ON(s->uptodate != disks);
  2203. sh->check_state = check_state_run;
  2204. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2205. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2206. s->uptodate--;
  2207. break;
  2208. }
  2209. dev = &sh->dev[s->failed_num];
  2210. /* fall through */
  2211. case check_state_compute_result:
  2212. sh->check_state = check_state_idle;
  2213. if (!dev)
  2214. dev = &sh->dev[sh->pd_idx];
  2215. /* check that a write has not made the stripe insync */
  2216. if (test_bit(STRIPE_INSYNC, &sh->state))
  2217. break;
  2218. /* either failed parity check, or recovery is happening */
  2219. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2220. BUG_ON(s->uptodate != disks);
  2221. set_bit(R5_LOCKED, &dev->flags);
  2222. s->locked++;
  2223. set_bit(R5_Wantwrite, &dev->flags);
  2224. clear_bit(STRIPE_DEGRADED, &sh->state);
  2225. set_bit(STRIPE_INSYNC, &sh->state);
  2226. break;
  2227. case check_state_run:
  2228. break; /* we will be called again upon completion */
  2229. case check_state_check_result:
  2230. sh->check_state = check_state_idle;
  2231. /* if a failure occurred during the check operation, leave
  2232. * STRIPE_INSYNC not set and let the stripe be handled again
  2233. */
  2234. if (s->failed)
  2235. break;
  2236. /* handle a successful check operation, if parity is correct
  2237. * we are done. Otherwise update the mismatch count and repair
  2238. * parity if !MD_RECOVERY_CHECK
  2239. */
  2240. if (sh->ops.zero_sum_result == 0)
  2241. /* parity is correct (on disc,
  2242. * not in buffer any more)
  2243. */
  2244. set_bit(STRIPE_INSYNC, &sh->state);
  2245. else {
  2246. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2247. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2248. /* don't try to repair!! */
  2249. set_bit(STRIPE_INSYNC, &sh->state);
  2250. else {
  2251. sh->check_state = check_state_compute_run;
  2252. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2253. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2254. set_bit(R5_Wantcompute,
  2255. &sh->dev[sh->pd_idx].flags);
  2256. sh->ops.target = sh->pd_idx;
  2257. s->uptodate++;
  2258. }
  2259. }
  2260. break;
  2261. case check_state_compute_run:
  2262. break;
  2263. default:
  2264. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2265. __func__, sh->check_state,
  2266. (unsigned long long) sh->sector);
  2267. BUG();
  2268. }
  2269. }
  2270. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  2271. struct stripe_head_state *s,
  2272. struct r6_state *r6s, struct page *tmp_page,
  2273. int disks)
  2274. {
  2275. int update_p = 0, update_q = 0;
  2276. struct r5dev *dev;
  2277. int pd_idx = sh->pd_idx;
  2278. int qd_idx = sh->qd_idx;
  2279. set_bit(STRIPE_HANDLE, &sh->state);
  2280. BUG_ON(s->failed > 2);
  2281. BUG_ON(s->uptodate < disks);
  2282. /* Want to check and possibly repair P and Q.
  2283. * However there could be one 'failed' device, in which
  2284. * case we can only check one of them, possibly using the
  2285. * other to generate missing data
  2286. */
  2287. /* If !tmp_page, we cannot do the calculations,
  2288. * but as we have set STRIPE_HANDLE, we will soon be called
  2289. * by stripe_handle with a tmp_page - just wait until then.
  2290. */
  2291. if (tmp_page) {
  2292. if (s->failed == r6s->q_failed) {
  2293. /* The only possible failed device holds 'Q', so it
  2294. * makes sense to check P (If anything else were failed,
  2295. * we would have used P to recreate it).
  2296. */
  2297. compute_block_1(sh, pd_idx, 1);
  2298. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2299. compute_block_1(sh, pd_idx, 0);
  2300. update_p = 1;
  2301. }
  2302. }
  2303. if (!r6s->q_failed && s->failed < 2) {
  2304. /* q is not failed, and we didn't use it to generate
  2305. * anything, so it makes sense to check it
  2306. */
  2307. memcpy(page_address(tmp_page),
  2308. page_address(sh->dev[qd_idx].page),
  2309. STRIPE_SIZE);
  2310. compute_parity6(sh, UPDATE_PARITY);
  2311. if (memcmp(page_address(tmp_page),
  2312. page_address(sh->dev[qd_idx].page),
  2313. STRIPE_SIZE) != 0) {
  2314. clear_bit(STRIPE_INSYNC, &sh->state);
  2315. update_q = 1;
  2316. }
  2317. }
  2318. if (update_p || update_q) {
  2319. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2320. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2321. /* don't try to repair!! */
  2322. update_p = update_q = 0;
  2323. }
  2324. /* now write out any block on a failed drive,
  2325. * or P or Q if they need it
  2326. */
  2327. if (s->failed == 2) {
  2328. dev = &sh->dev[r6s->failed_num[1]];
  2329. s->locked++;
  2330. set_bit(R5_LOCKED, &dev->flags);
  2331. set_bit(R5_Wantwrite, &dev->flags);
  2332. }
  2333. if (s->failed >= 1) {
  2334. dev = &sh->dev[r6s->failed_num[0]];
  2335. s->locked++;
  2336. set_bit(R5_LOCKED, &dev->flags);
  2337. set_bit(R5_Wantwrite, &dev->flags);
  2338. }
  2339. if (update_p) {
  2340. dev = &sh->dev[pd_idx];
  2341. s->locked++;
  2342. set_bit(R5_LOCKED, &dev->flags);
  2343. set_bit(R5_Wantwrite, &dev->flags);
  2344. }
  2345. if (update_q) {
  2346. dev = &sh->dev[qd_idx];
  2347. s->locked++;
  2348. set_bit(R5_LOCKED, &dev->flags);
  2349. set_bit(R5_Wantwrite, &dev->flags);
  2350. }
  2351. clear_bit(STRIPE_DEGRADED, &sh->state);
  2352. set_bit(STRIPE_INSYNC, &sh->state);
  2353. }
  2354. }
  2355. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  2356. struct r6_state *r6s)
  2357. {
  2358. int i;
  2359. /* We have read all the blocks in this stripe and now we need to
  2360. * copy some of them into a target stripe for expand.
  2361. */
  2362. struct dma_async_tx_descriptor *tx = NULL;
  2363. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2364. for (i = 0; i < sh->disks; i++)
  2365. if (i != sh->pd_idx && i != sh->qd_idx) {
  2366. int dd_idx, j;
  2367. struct stripe_head *sh2;
  2368. sector_t bn = compute_blocknr(sh, i, 1);
  2369. sector_t s = raid5_compute_sector(conf, bn, 0,
  2370. &dd_idx, NULL);
  2371. sh2 = get_active_stripe(conf, s, 0, 1, 1);
  2372. if (sh2 == NULL)
  2373. /* so far only the early blocks of this stripe
  2374. * have been requested. When later blocks
  2375. * get requested, we will try again
  2376. */
  2377. continue;
  2378. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2379. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2380. /* must have already done this block */
  2381. release_stripe(sh2);
  2382. continue;
  2383. }
  2384. /* place all the copies on one channel */
  2385. tx = async_memcpy(sh2->dev[dd_idx].page,
  2386. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2387. ASYNC_TX_DEP_ACK, tx, NULL, NULL);
  2388. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2389. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2390. for (j = 0; j < conf->raid_disks; j++)
  2391. if (j != sh2->pd_idx &&
  2392. (!r6s || j != sh2->qd_idx) &&
  2393. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2394. break;
  2395. if (j == conf->raid_disks) {
  2396. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2397. set_bit(STRIPE_HANDLE, &sh2->state);
  2398. }
  2399. release_stripe(sh2);
  2400. }
  2401. /* done submitting copies, wait for them to complete */
  2402. if (tx) {
  2403. async_tx_ack(tx);
  2404. dma_wait_for_async_tx(tx);
  2405. }
  2406. }
  2407. /*
  2408. * handle_stripe - do things to a stripe.
  2409. *
  2410. * We lock the stripe and then examine the state of various bits
  2411. * to see what needs to be done.
  2412. * Possible results:
  2413. * return some read request which now have data
  2414. * return some write requests which are safely on disc
  2415. * schedule a read on some buffers
  2416. * schedule a write of some buffers
  2417. * return confirmation of parity correctness
  2418. *
  2419. * buffers are taken off read_list or write_list, and bh_cache buffers
  2420. * get BH_Lock set before the stripe lock is released.
  2421. *
  2422. */
  2423. static bool handle_stripe5(struct stripe_head *sh)
  2424. {
  2425. raid5_conf_t *conf = sh->raid_conf;
  2426. int disks = sh->disks, i;
  2427. struct bio *return_bi = NULL;
  2428. struct stripe_head_state s;
  2429. struct r5dev *dev;
  2430. mdk_rdev_t *blocked_rdev = NULL;
  2431. int prexor;
  2432. memset(&s, 0, sizeof(s));
  2433. pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
  2434. "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
  2435. atomic_read(&sh->count), sh->pd_idx, sh->check_state,
  2436. sh->reconstruct_state);
  2437. spin_lock(&sh->lock);
  2438. clear_bit(STRIPE_HANDLE, &sh->state);
  2439. clear_bit(STRIPE_DELAYED, &sh->state);
  2440. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2441. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2442. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2443. /* Now to look around and see what can be done */
  2444. rcu_read_lock();
  2445. for (i=disks; i--; ) {
  2446. mdk_rdev_t *rdev;
  2447. struct r5dev *dev = &sh->dev[i];
  2448. clear_bit(R5_Insync, &dev->flags);
  2449. pr_debug("check %d: state 0x%lx toread %p read %p write %p "
  2450. "written %p\n", i, dev->flags, dev->toread, dev->read,
  2451. dev->towrite, dev->written);
  2452. /* maybe we can request a biofill operation
  2453. *
  2454. * new wantfill requests are only permitted while
  2455. * ops_complete_biofill is guaranteed to be inactive
  2456. */
  2457. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2458. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2459. set_bit(R5_Wantfill, &dev->flags);
  2460. /* now count some things */
  2461. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2462. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2463. if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
  2464. if (test_bit(R5_Wantfill, &dev->flags))
  2465. s.to_fill++;
  2466. else if (dev->toread)
  2467. s.to_read++;
  2468. if (dev->towrite) {
  2469. s.to_write++;
  2470. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2471. s.non_overwrite++;
  2472. }
  2473. if (dev->written)
  2474. s.written++;
  2475. rdev = rcu_dereference(conf->disks[i].rdev);
  2476. if (blocked_rdev == NULL &&
  2477. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2478. blocked_rdev = rdev;
  2479. atomic_inc(&rdev->nr_pending);
  2480. }
  2481. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2482. /* The ReadError flag will just be confusing now */
  2483. clear_bit(R5_ReadError, &dev->flags);
  2484. clear_bit(R5_ReWrite, &dev->flags);
  2485. }
  2486. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2487. || test_bit(R5_ReadError, &dev->flags)) {
  2488. s.failed++;
  2489. s.failed_num = i;
  2490. } else
  2491. set_bit(R5_Insync, &dev->flags);
  2492. }
  2493. rcu_read_unlock();
  2494. if (unlikely(blocked_rdev)) {
  2495. if (s.syncing || s.expanding || s.expanded ||
  2496. s.to_write || s.written) {
  2497. set_bit(STRIPE_HANDLE, &sh->state);
  2498. goto unlock;
  2499. }
  2500. /* There is nothing for the blocked_rdev to block */
  2501. rdev_dec_pending(blocked_rdev, conf->mddev);
  2502. blocked_rdev = NULL;
  2503. }
  2504. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2505. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2506. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2507. }
  2508. pr_debug("locked=%d uptodate=%d to_read=%d"
  2509. " to_write=%d failed=%d failed_num=%d\n",
  2510. s.locked, s.uptodate, s.to_read, s.to_write,
  2511. s.failed, s.failed_num);
  2512. /* check if the array has lost two devices and, if so, some requests might
  2513. * need to be failed
  2514. */
  2515. if (s.failed > 1 && s.to_read+s.to_write+s.written)
  2516. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2517. if (s.failed > 1 && s.syncing) {
  2518. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2519. clear_bit(STRIPE_SYNCING, &sh->state);
  2520. s.syncing = 0;
  2521. }
  2522. /* might be able to return some write requests if the parity block
  2523. * is safe, or on a failed drive
  2524. */
  2525. dev = &sh->dev[sh->pd_idx];
  2526. if ( s.written &&
  2527. ((test_bit(R5_Insync, &dev->flags) &&
  2528. !test_bit(R5_LOCKED, &dev->flags) &&
  2529. test_bit(R5_UPTODATE, &dev->flags)) ||
  2530. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  2531. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2532. /* Now we might consider reading some blocks, either to check/generate
  2533. * parity, or to satisfy requests
  2534. * or to load a block that is being partially written.
  2535. */
  2536. if (s.to_read || s.non_overwrite ||
  2537. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  2538. handle_stripe_fill5(sh, &s, disks);
  2539. /* Now we check to see if any write operations have recently
  2540. * completed
  2541. */
  2542. prexor = 0;
  2543. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  2544. prexor = 1;
  2545. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  2546. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  2547. sh->reconstruct_state = reconstruct_state_idle;
  2548. /* All the 'written' buffers and the parity block are ready to
  2549. * be written back to disk
  2550. */
  2551. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2552. for (i = disks; i--; ) {
  2553. dev = &sh->dev[i];
  2554. if (test_bit(R5_LOCKED, &dev->flags) &&
  2555. (i == sh->pd_idx || dev->written)) {
  2556. pr_debug("Writing block %d\n", i);
  2557. set_bit(R5_Wantwrite, &dev->flags);
  2558. if (prexor)
  2559. continue;
  2560. if (!test_bit(R5_Insync, &dev->flags) ||
  2561. (i == sh->pd_idx && s.failed == 0))
  2562. set_bit(STRIPE_INSYNC, &sh->state);
  2563. }
  2564. }
  2565. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2566. atomic_dec(&conf->preread_active_stripes);
  2567. if (atomic_read(&conf->preread_active_stripes) <
  2568. IO_THRESHOLD)
  2569. md_wakeup_thread(conf->mddev->thread);
  2570. }
  2571. }
  2572. /* Now to consider new write requests and what else, if anything
  2573. * should be read. We do not handle new writes when:
  2574. * 1/ A 'write' operation (copy+xor) is already in flight.
  2575. * 2/ A 'check' operation is in flight, as it may clobber the parity
  2576. * block.
  2577. */
  2578. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  2579. handle_stripe_dirtying5(conf, sh, &s, disks);
  2580. /* maybe we need to check and possibly fix the parity for this stripe
  2581. * Any reads will already have been scheduled, so we just see if enough
  2582. * data is available. The parity check is held off while parity
  2583. * dependent operations are in flight.
  2584. */
  2585. if (sh->check_state ||
  2586. (s.syncing && s.locked == 0 &&
  2587. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  2588. !test_bit(STRIPE_INSYNC, &sh->state)))
  2589. handle_parity_checks5(conf, sh, &s, disks);
  2590. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2591. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2592. clear_bit(STRIPE_SYNCING, &sh->state);
  2593. }
  2594. /* If the failed drive is just a ReadError, then we might need to progress
  2595. * the repair/check process
  2596. */
  2597. if (s.failed == 1 && !conf->mddev->ro &&
  2598. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  2599. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  2600. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  2601. ) {
  2602. dev = &sh->dev[s.failed_num];
  2603. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2604. set_bit(R5_Wantwrite, &dev->flags);
  2605. set_bit(R5_ReWrite, &dev->flags);
  2606. set_bit(R5_LOCKED, &dev->flags);
  2607. s.locked++;
  2608. } else {
  2609. /* let's read it back */
  2610. set_bit(R5_Wantread, &dev->flags);
  2611. set_bit(R5_LOCKED, &dev->flags);
  2612. s.locked++;
  2613. }
  2614. }
  2615. /* Finish reconstruct operations initiated by the expansion process */
  2616. if (sh->reconstruct_state == reconstruct_state_result) {
  2617. struct stripe_head *sh2
  2618. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  2619. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  2620. /* sh cannot be written until sh2 has been read.
  2621. * so arrange for sh to be delayed a little
  2622. */
  2623. set_bit(STRIPE_DELAYED, &sh->state);
  2624. set_bit(STRIPE_HANDLE, &sh->state);
  2625. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  2626. &sh2->state))
  2627. atomic_inc(&conf->preread_active_stripes);
  2628. release_stripe(sh2);
  2629. goto unlock;
  2630. }
  2631. if (sh2)
  2632. release_stripe(sh2);
  2633. sh->reconstruct_state = reconstruct_state_idle;
  2634. clear_bit(STRIPE_EXPANDING, &sh->state);
  2635. for (i = conf->raid_disks; i--; ) {
  2636. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2637. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2638. s.locked++;
  2639. }
  2640. }
  2641. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  2642. !sh->reconstruct_state) {
  2643. /* Need to write out all blocks after computing parity */
  2644. sh->disks = conf->raid_disks;
  2645. stripe_set_idx(sh->sector, conf, 0, sh);
  2646. schedule_reconstruction5(sh, &s, 1, 1);
  2647. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  2648. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2649. atomic_dec(&conf->reshape_stripes);
  2650. wake_up(&conf->wait_for_overlap);
  2651. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2652. }
  2653. if (s.expanding && s.locked == 0 &&
  2654. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2655. handle_stripe_expansion(conf, sh, NULL);
  2656. unlock:
  2657. spin_unlock(&sh->lock);
  2658. /* wait for this device to become unblocked */
  2659. if (unlikely(blocked_rdev))
  2660. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2661. if (s.ops_request)
  2662. raid5_run_ops(sh, s.ops_request);
  2663. ops_run_io(sh, &s);
  2664. return_io(return_bi);
  2665. return blocked_rdev == NULL;
  2666. }
  2667. static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  2668. {
  2669. raid5_conf_t *conf = sh->raid_conf;
  2670. int disks = sh->disks;
  2671. struct bio *return_bi = NULL;
  2672. int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
  2673. struct stripe_head_state s;
  2674. struct r6_state r6s;
  2675. struct r5dev *dev, *pdev, *qdev;
  2676. mdk_rdev_t *blocked_rdev = NULL;
  2677. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2678. "pd_idx=%d, qd_idx=%d\n",
  2679. (unsigned long long)sh->sector, sh->state,
  2680. atomic_read(&sh->count), pd_idx, qd_idx);
  2681. memset(&s, 0, sizeof(s));
  2682. spin_lock(&sh->lock);
  2683. clear_bit(STRIPE_HANDLE, &sh->state);
  2684. clear_bit(STRIPE_DELAYED, &sh->state);
  2685. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2686. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2687. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2688. /* Now to look around and see what can be done */
  2689. rcu_read_lock();
  2690. for (i=disks; i--; ) {
  2691. mdk_rdev_t *rdev;
  2692. dev = &sh->dev[i];
  2693. clear_bit(R5_Insync, &dev->flags);
  2694. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2695. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2696. /* maybe we can reply to a read */
  2697. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  2698. struct bio *rbi, *rbi2;
  2699. pr_debug("Return read for disc %d\n", i);
  2700. spin_lock_irq(&conf->device_lock);
  2701. rbi = dev->toread;
  2702. dev->toread = NULL;
  2703. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  2704. wake_up(&conf->wait_for_overlap);
  2705. spin_unlock_irq(&conf->device_lock);
  2706. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  2707. copy_data(0, rbi, dev->page, dev->sector);
  2708. rbi2 = r5_next_bio(rbi, dev->sector);
  2709. spin_lock_irq(&conf->device_lock);
  2710. if (!raid5_dec_bi_phys_segments(rbi)) {
  2711. rbi->bi_next = return_bi;
  2712. return_bi = rbi;
  2713. }
  2714. spin_unlock_irq(&conf->device_lock);
  2715. rbi = rbi2;
  2716. }
  2717. }
  2718. /* now count some things */
  2719. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2720. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2721. if (dev->toread)
  2722. s.to_read++;
  2723. if (dev->towrite) {
  2724. s.to_write++;
  2725. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2726. s.non_overwrite++;
  2727. }
  2728. if (dev->written)
  2729. s.written++;
  2730. rdev = rcu_dereference(conf->disks[i].rdev);
  2731. if (blocked_rdev == NULL &&
  2732. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2733. blocked_rdev = rdev;
  2734. atomic_inc(&rdev->nr_pending);
  2735. }
  2736. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2737. /* The ReadError flag will just be confusing now */
  2738. clear_bit(R5_ReadError, &dev->flags);
  2739. clear_bit(R5_ReWrite, &dev->flags);
  2740. }
  2741. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2742. || test_bit(R5_ReadError, &dev->flags)) {
  2743. if (s.failed < 2)
  2744. r6s.failed_num[s.failed] = i;
  2745. s.failed++;
  2746. } else
  2747. set_bit(R5_Insync, &dev->flags);
  2748. }
  2749. rcu_read_unlock();
  2750. if (unlikely(blocked_rdev)) {
  2751. if (s.syncing || s.expanding || s.expanded ||
  2752. s.to_write || s.written) {
  2753. set_bit(STRIPE_HANDLE, &sh->state);
  2754. goto unlock;
  2755. }
  2756. /* There is nothing for the blocked_rdev to block */
  2757. rdev_dec_pending(blocked_rdev, conf->mddev);
  2758. blocked_rdev = NULL;
  2759. }
  2760. pr_debug("locked=%d uptodate=%d to_read=%d"
  2761. " to_write=%d failed=%d failed_num=%d,%d\n",
  2762. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2763. r6s.failed_num[0], r6s.failed_num[1]);
  2764. /* check if the array has lost >2 devices and, if so, some requests
  2765. * might need to be failed
  2766. */
  2767. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  2768. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2769. if (s.failed > 2 && s.syncing) {
  2770. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2771. clear_bit(STRIPE_SYNCING, &sh->state);
  2772. s.syncing = 0;
  2773. }
  2774. /*
  2775. * might be able to return some write requests if the parity blocks
  2776. * are safe, or on a failed drive
  2777. */
  2778. pdev = &sh->dev[pd_idx];
  2779. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  2780. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  2781. qdev = &sh->dev[qd_idx];
  2782. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
  2783. || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
  2784. if ( s.written &&
  2785. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  2786. && !test_bit(R5_LOCKED, &pdev->flags)
  2787. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  2788. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  2789. && !test_bit(R5_LOCKED, &qdev->flags)
  2790. && test_bit(R5_UPTODATE, &qdev->flags)))))
  2791. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2792. /* Now we might consider reading some blocks, either to check/generate
  2793. * parity, or to satisfy requests
  2794. * or to load a block that is being partially written.
  2795. */
  2796. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  2797. (s.syncing && (s.uptodate < disks)) || s.expanding)
  2798. handle_stripe_fill6(sh, &s, &r6s, disks);
  2799. /* now to consider writing and what else, if anything should be read */
  2800. if (s.to_write)
  2801. handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
  2802. /* maybe we need to check and possibly fix the parity for this stripe
  2803. * Any reads will already have been scheduled, so we just see if enough
  2804. * data is available
  2805. */
  2806. if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
  2807. handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
  2808. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2809. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2810. clear_bit(STRIPE_SYNCING, &sh->state);
  2811. }
  2812. /* If the failed drives are just a ReadError, then we might need
  2813. * to progress the repair/check process
  2814. */
  2815. if (s.failed <= 2 && !conf->mddev->ro)
  2816. for (i = 0; i < s.failed; i++) {
  2817. dev = &sh->dev[r6s.failed_num[i]];
  2818. if (test_bit(R5_ReadError, &dev->flags)
  2819. && !test_bit(R5_LOCKED, &dev->flags)
  2820. && test_bit(R5_UPTODATE, &dev->flags)
  2821. ) {
  2822. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2823. set_bit(R5_Wantwrite, &dev->flags);
  2824. set_bit(R5_ReWrite, &dev->flags);
  2825. set_bit(R5_LOCKED, &dev->flags);
  2826. } else {
  2827. /* let's read it back */
  2828. set_bit(R5_Wantread, &dev->flags);
  2829. set_bit(R5_LOCKED, &dev->flags);
  2830. }
  2831. }
  2832. }
  2833. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  2834. struct stripe_head *sh2
  2835. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  2836. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  2837. /* sh cannot be written until sh2 has been read.
  2838. * so arrange for sh to be delayed a little
  2839. */
  2840. set_bit(STRIPE_DELAYED, &sh->state);
  2841. set_bit(STRIPE_HANDLE, &sh->state);
  2842. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  2843. &sh2->state))
  2844. atomic_inc(&conf->preread_active_stripes);
  2845. release_stripe(sh2);
  2846. goto unlock;
  2847. }
  2848. if (sh2)
  2849. release_stripe(sh2);
  2850. /* Need to write out all blocks after computing P&Q */
  2851. sh->disks = conf->raid_disks;
  2852. stripe_set_idx(sh->sector, conf, 0, sh);
  2853. compute_parity6(sh, RECONSTRUCT_WRITE);
  2854. for (i = conf->raid_disks ; i-- ; ) {
  2855. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2856. s.locked++;
  2857. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2858. }
  2859. clear_bit(STRIPE_EXPANDING, &sh->state);
  2860. } else if (s.expanded) {
  2861. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2862. atomic_dec(&conf->reshape_stripes);
  2863. wake_up(&conf->wait_for_overlap);
  2864. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2865. }
  2866. if (s.expanding && s.locked == 0 &&
  2867. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2868. handle_stripe_expansion(conf, sh, &r6s);
  2869. unlock:
  2870. spin_unlock(&sh->lock);
  2871. /* wait for this device to become unblocked */
  2872. if (unlikely(blocked_rdev))
  2873. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2874. ops_run_io(sh, &s);
  2875. return_io(return_bi);
  2876. return blocked_rdev == NULL;
  2877. }
  2878. /* returns true if the stripe was handled */
  2879. static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2880. {
  2881. if (sh->raid_conf->level == 6)
  2882. return handle_stripe6(sh, tmp_page);
  2883. else
  2884. return handle_stripe5(sh);
  2885. }
  2886. static void raid5_activate_delayed(raid5_conf_t *conf)
  2887. {
  2888. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2889. while (!list_empty(&conf->delayed_list)) {
  2890. struct list_head *l = conf->delayed_list.next;
  2891. struct stripe_head *sh;
  2892. sh = list_entry(l, struct stripe_head, lru);
  2893. list_del_init(l);
  2894. clear_bit(STRIPE_DELAYED, &sh->state);
  2895. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2896. atomic_inc(&conf->preread_active_stripes);
  2897. list_add_tail(&sh->lru, &conf->hold_list);
  2898. }
  2899. } else
  2900. blk_plug_device(conf->mddev->queue);
  2901. }
  2902. static void activate_bit_delay(raid5_conf_t *conf)
  2903. {
  2904. /* device_lock is held */
  2905. struct list_head head;
  2906. list_add(&head, &conf->bitmap_list);
  2907. list_del_init(&conf->bitmap_list);
  2908. while (!list_empty(&head)) {
  2909. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2910. list_del_init(&sh->lru);
  2911. atomic_inc(&sh->count);
  2912. __release_stripe(conf, sh);
  2913. }
  2914. }
  2915. static void unplug_slaves(mddev_t *mddev)
  2916. {
  2917. raid5_conf_t *conf = mddev->private;
  2918. int i;
  2919. rcu_read_lock();
  2920. for (i = 0; i < conf->raid_disks; i++) {
  2921. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2922. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2923. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  2924. atomic_inc(&rdev->nr_pending);
  2925. rcu_read_unlock();
  2926. blk_unplug(r_queue);
  2927. rdev_dec_pending(rdev, mddev);
  2928. rcu_read_lock();
  2929. }
  2930. }
  2931. rcu_read_unlock();
  2932. }
  2933. static void raid5_unplug_device(struct request_queue *q)
  2934. {
  2935. mddev_t *mddev = q->queuedata;
  2936. raid5_conf_t *conf = mddev->private;
  2937. unsigned long flags;
  2938. spin_lock_irqsave(&conf->device_lock, flags);
  2939. if (blk_remove_plug(q)) {
  2940. conf->seq_flush++;
  2941. raid5_activate_delayed(conf);
  2942. }
  2943. md_wakeup_thread(mddev->thread);
  2944. spin_unlock_irqrestore(&conf->device_lock, flags);
  2945. unplug_slaves(mddev);
  2946. }
  2947. static int raid5_congested(void *data, int bits)
  2948. {
  2949. mddev_t *mddev = data;
  2950. raid5_conf_t *conf = mddev->private;
  2951. /* No difference between reads and writes. Just check
  2952. * how busy the stripe_cache is
  2953. */
  2954. if (conf->inactive_blocked)
  2955. return 1;
  2956. if (conf->quiesce)
  2957. return 1;
  2958. if (list_empty_careful(&conf->inactive_list))
  2959. return 1;
  2960. return 0;
  2961. }
  2962. /* We want read requests to align with chunks where possible,
  2963. * but write requests don't need to.
  2964. */
  2965. static int raid5_mergeable_bvec(struct request_queue *q,
  2966. struct bvec_merge_data *bvm,
  2967. struct bio_vec *biovec)
  2968. {
  2969. mddev_t *mddev = q->queuedata;
  2970. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  2971. int max;
  2972. unsigned int chunk_sectors = mddev->chunk_sectors;
  2973. unsigned int bio_sectors = bvm->bi_size >> 9;
  2974. if ((bvm->bi_rw & 1) == WRITE)
  2975. return biovec->bv_len; /* always allow writes to be mergeable */
  2976. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  2977. chunk_sectors = mddev->new_chunk_sectors;
  2978. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2979. if (max < 0) max = 0;
  2980. if (max <= biovec->bv_len && bio_sectors == 0)
  2981. return biovec->bv_len;
  2982. else
  2983. return max;
  2984. }
  2985. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2986. {
  2987. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2988. unsigned int chunk_sectors = mddev->chunk_sectors;
  2989. unsigned int bio_sectors = bio->bi_size >> 9;
  2990. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  2991. chunk_sectors = mddev->new_chunk_sectors;
  2992. return chunk_sectors >=
  2993. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2994. }
  2995. /*
  2996. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2997. * later sampled by raid5d.
  2998. */
  2999. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  3000. {
  3001. unsigned long flags;
  3002. spin_lock_irqsave(&conf->device_lock, flags);
  3003. bi->bi_next = conf->retry_read_aligned_list;
  3004. conf->retry_read_aligned_list = bi;
  3005. spin_unlock_irqrestore(&conf->device_lock, flags);
  3006. md_wakeup_thread(conf->mddev->thread);
  3007. }
  3008. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  3009. {
  3010. struct bio *bi;
  3011. bi = conf->retry_read_aligned;
  3012. if (bi) {
  3013. conf->retry_read_aligned = NULL;
  3014. return bi;
  3015. }
  3016. bi = conf->retry_read_aligned_list;
  3017. if(bi) {
  3018. conf->retry_read_aligned_list = bi->bi_next;
  3019. bi->bi_next = NULL;
  3020. /*
  3021. * this sets the active strip count to 1 and the processed
  3022. * strip count to zero (upper 8 bits)
  3023. */
  3024. bi->bi_phys_segments = 1; /* biased count of active stripes */
  3025. }
  3026. return bi;
  3027. }
  3028. /*
  3029. * The "raid5_align_endio" should check if the read succeeded and if it
  3030. * did, call bio_endio on the original bio (having bio_put the new bio
  3031. * first).
  3032. * If the read failed..
  3033. */
  3034. static void raid5_align_endio(struct bio *bi, int error)
  3035. {
  3036. struct bio* raid_bi = bi->bi_private;
  3037. mddev_t *mddev;
  3038. raid5_conf_t *conf;
  3039. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3040. mdk_rdev_t *rdev;
  3041. bio_put(bi);
  3042. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  3043. conf = mddev->private;
  3044. rdev = (void*)raid_bi->bi_next;
  3045. raid_bi->bi_next = NULL;
  3046. rdev_dec_pending(rdev, conf->mddev);
  3047. if (!error && uptodate) {
  3048. bio_endio(raid_bi, 0);
  3049. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3050. wake_up(&conf->wait_for_stripe);
  3051. return;
  3052. }
  3053. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3054. add_bio_to_retry(raid_bi, conf);
  3055. }
  3056. static int bio_fits_rdev(struct bio *bi)
  3057. {
  3058. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3059. if ((bi->bi_size>>9) > queue_max_sectors(q))
  3060. return 0;
  3061. blk_recount_segments(q, bi);
  3062. if (bi->bi_phys_segments > queue_max_phys_segments(q))
  3063. return 0;
  3064. if (q->merge_bvec_fn)
  3065. /* it's too hard to apply the merge_bvec_fn at this stage,
  3066. * just just give up
  3067. */
  3068. return 0;
  3069. return 1;
  3070. }
  3071. static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
  3072. {
  3073. mddev_t *mddev = q->queuedata;
  3074. raid5_conf_t *conf = mddev->private;
  3075. unsigned int dd_idx;
  3076. struct bio* align_bi;
  3077. mdk_rdev_t *rdev;
  3078. if (!in_chunk_boundary(mddev, raid_bio)) {
  3079. pr_debug("chunk_aligned_read : non aligned\n");
  3080. return 0;
  3081. }
  3082. /*
  3083. * use bio_clone to make a copy of the bio
  3084. */
  3085. align_bi = bio_clone(raid_bio, GFP_NOIO);
  3086. if (!align_bi)
  3087. return 0;
  3088. /*
  3089. * set bi_end_io to a new function, and set bi_private to the
  3090. * original bio.
  3091. */
  3092. align_bi->bi_end_io = raid5_align_endio;
  3093. align_bi->bi_private = raid_bio;
  3094. /*
  3095. * compute position
  3096. */
  3097. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3098. 0,
  3099. &dd_idx, NULL);
  3100. rcu_read_lock();
  3101. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3102. if (rdev && test_bit(In_sync, &rdev->flags)) {
  3103. atomic_inc(&rdev->nr_pending);
  3104. rcu_read_unlock();
  3105. raid_bio->bi_next = (void*)rdev;
  3106. align_bi->bi_bdev = rdev->bdev;
  3107. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3108. align_bi->bi_sector += rdev->data_offset;
  3109. if (!bio_fits_rdev(align_bi)) {
  3110. /* too big in some way */
  3111. bio_put(align_bi);
  3112. rdev_dec_pending(rdev, mddev);
  3113. return 0;
  3114. }
  3115. spin_lock_irq(&conf->device_lock);
  3116. wait_event_lock_irq(conf->wait_for_stripe,
  3117. conf->quiesce == 0,
  3118. conf->device_lock, /* nothing */);
  3119. atomic_inc(&conf->active_aligned_reads);
  3120. spin_unlock_irq(&conf->device_lock);
  3121. generic_make_request(align_bi);
  3122. return 1;
  3123. } else {
  3124. rcu_read_unlock();
  3125. bio_put(align_bi);
  3126. return 0;
  3127. }
  3128. }
  3129. /* __get_priority_stripe - get the next stripe to process
  3130. *
  3131. * Full stripe writes are allowed to pass preread active stripes up until
  3132. * the bypass_threshold is exceeded. In general the bypass_count
  3133. * increments when the handle_list is handled before the hold_list; however, it
  3134. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3135. * stripe with in flight i/o. The bypass_count will be reset when the
  3136. * head of the hold_list has changed, i.e. the head was promoted to the
  3137. * handle_list.
  3138. */
  3139. static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
  3140. {
  3141. struct stripe_head *sh;
  3142. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3143. __func__,
  3144. list_empty(&conf->handle_list) ? "empty" : "busy",
  3145. list_empty(&conf->hold_list) ? "empty" : "busy",
  3146. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3147. if (!list_empty(&conf->handle_list)) {
  3148. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3149. if (list_empty(&conf->hold_list))
  3150. conf->bypass_count = 0;
  3151. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3152. if (conf->hold_list.next == conf->last_hold)
  3153. conf->bypass_count++;
  3154. else {
  3155. conf->last_hold = conf->hold_list.next;
  3156. conf->bypass_count -= conf->bypass_threshold;
  3157. if (conf->bypass_count < 0)
  3158. conf->bypass_count = 0;
  3159. }
  3160. }
  3161. } else if (!list_empty(&conf->hold_list) &&
  3162. ((conf->bypass_threshold &&
  3163. conf->bypass_count > conf->bypass_threshold) ||
  3164. atomic_read(&conf->pending_full_writes) == 0)) {
  3165. sh = list_entry(conf->hold_list.next,
  3166. typeof(*sh), lru);
  3167. conf->bypass_count -= conf->bypass_threshold;
  3168. if (conf->bypass_count < 0)
  3169. conf->bypass_count = 0;
  3170. } else
  3171. return NULL;
  3172. list_del_init(&sh->lru);
  3173. atomic_inc(&sh->count);
  3174. BUG_ON(atomic_read(&sh->count) != 1);
  3175. return sh;
  3176. }
  3177. static int make_request(struct request_queue *q, struct bio * bi)
  3178. {
  3179. mddev_t *mddev = q->queuedata;
  3180. raid5_conf_t *conf = mddev->private;
  3181. int dd_idx;
  3182. sector_t new_sector;
  3183. sector_t logical_sector, last_sector;
  3184. struct stripe_head *sh;
  3185. const int rw = bio_data_dir(bi);
  3186. int cpu, remaining;
  3187. if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) {
  3188. bio_endio(bi, -EOPNOTSUPP);
  3189. return 0;
  3190. }
  3191. md_write_start(mddev, bi);
  3192. cpu = part_stat_lock();
  3193. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  3194. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  3195. bio_sectors(bi));
  3196. part_stat_unlock();
  3197. if (rw == READ &&
  3198. mddev->reshape_position == MaxSector &&
  3199. chunk_aligned_read(q,bi))
  3200. return 0;
  3201. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3202. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3203. bi->bi_next = NULL;
  3204. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3205. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3206. DEFINE_WAIT(w);
  3207. int disks, data_disks;
  3208. int previous;
  3209. retry:
  3210. previous = 0;
  3211. disks = conf->raid_disks;
  3212. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3213. if (unlikely(conf->reshape_progress != MaxSector)) {
  3214. /* spinlock is needed as reshape_progress may be
  3215. * 64bit on a 32bit platform, and so it might be
  3216. * possible to see a half-updated value
  3217. * Ofcourse reshape_progress could change after
  3218. * the lock is dropped, so once we get a reference
  3219. * to the stripe that we think it is, we will have
  3220. * to check again.
  3221. */
  3222. spin_lock_irq(&conf->device_lock);
  3223. if (mddev->delta_disks < 0
  3224. ? logical_sector < conf->reshape_progress
  3225. : logical_sector >= conf->reshape_progress) {
  3226. disks = conf->previous_raid_disks;
  3227. previous = 1;
  3228. } else {
  3229. if (mddev->delta_disks < 0
  3230. ? logical_sector < conf->reshape_safe
  3231. : logical_sector >= conf->reshape_safe) {
  3232. spin_unlock_irq(&conf->device_lock);
  3233. schedule();
  3234. goto retry;
  3235. }
  3236. }
  3237. spin_unlock_irq(&conf->device_lock);
  3238. }
  3239. data_disks = disks - conf->max_degraded;
  3240. new_sector = raid5_compute_sector(conf, logical_sector,
  3241. previous,
  3242. &dd_idx, NULL);
  3243. pr_debug("raid5: make_request, sector %llu logical %llu\n",
  3244. (unsigned long long)new_sector,
  3245. (unsigned long long)logical_sector);
  3246. sh = get_active_stripe(conf, new_sector, previous,
  3247. (bi->bi_rw&RWA_MASK), 0);
  3248. if (sh) {
  3249. if (unlikely(previous)) {
  3250. /* expansion might have moved on while waiting for a
  3251. * stripe, so we must do the range check again.
  3252. * Expansion could still move past after this
  3253. * test, but as we are holding a reference to
  3254. * 'sh', we know that if that happens,
  3255. * STRIPE_EXPANDING will get set and the expansion
  3256. * won't proceed until we finish with the stripe.
  3257. */
  3258. int must_retry = 0;
  3259. spin_lock_irq(&conf->device_lock);
  3260. if (mddev->delta_disks < 0
  3261. ? logical_sector >= conf->reshape_progress
  3262. : logical_sector < conf->reshape_progress)
  3263. /* mismatch, need to try again */
  3264. must_retry = 1;
  3265. spin_unlock_irq(&conf->device_lock);
  3266. if (must_retry) {
  3267. release_stripe(sh);
  3268. schedule();
  3269. goto retry;
  3270. }
  3271. }
  3272. if (bio_data_dir(bi) == WRITE &&
  3273. logical_sector >= mddev->suspend_lo &&
  3274. logical_sector < mddev->suspend_hi) {
  3275. release_stripe(sh);
  3276. /* As the suspend_* range is controlled by
  3277. * userspace, we want an interruptible
  3278. * wait.
  3279. */
  3280. flush_signals(current);
  3281. prepare_to_wait(&conf->wait_for_overlap,
  3282. &w, TASK_INTERRUPTIBLE);
  3283. if (logical_sector >= mddev->suspend_lo &&
  3284. logical_sector < mddev->suspend_hi)
  3285. schedule();
  3286. goto retry;
  3287. }
  3288. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3289. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3290. /* Stripe is busy expanding or
  3291. * add failed due to overlap. Flush everything
  3292. * and wait a while
  3293. */
  3294. raid5_unplug_device(mddev->queue);
  3295. release_stripe(sh);
  3296. schedule();
  3297. goto retry;
  3298. }
  3299. finish_wait(&conf->wait_for_overlap, &w);
  3300. set_bit(STRIPE_HANDLE, &sh->state);
  3301. clear_bit(STRIPE_DELAYED, &sh->state);
  3302. release_stripe(sh);
  3303. } else {
  3304. /* cannot get stripe for read-ahead, just give-up */
  3305. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3306. finish_wait(&conf->wait_for_overlap, &w);
  3307. break;
  3308. }
  3309. }
  3310. spin_lock_irq(&conf->device_lock);
  3311. remaining = raid5_dec_bi_phys_segments(bi);
  3312. spin_unlock_irq(&conf->device_lock);
  3313. if (remaining == 0) {
  3314. if ( rw == WRITE )
  3315. md_write_end(mddev);
  3316. bio_endio(bi, 0);
  3317. }
  3318. return 0;
  3319. }
  3320. static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
  3321. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3322. {
  3323. /* reshaping is quite different to recovery/resync so it is
  3324. * handled quite separately ... here.
  3325. *
  3326. * On each call to sync_request, we gather one chunk worth of
  3327. * destination stripes and flag them as expanding.
  3328. * Then we find all the source stripes and request reads.
  3329. * As the reads complete, handle_stripe will copy the data
  3330. * into the destination stripe and release that stripe.
  3331. */
  3332. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3333. struct stripe_head *sh;
  3334. sector_t first_sector, last_sector;
  3335. int raid_disks = conf->previous_raid_disks;
  3336. int data_disks = raid_disks - conf->max_degraded;
  3337. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3338. int i;
  3339. int dd_idx;
  3340. sector_t writepos, readpos, safepos;
  3341. sector_t stripe_addr;
  3342. int reshape_sectors;
  3343. struct list_head stripes;
  3344. if (sector_nr == 0) {
  3345. /* If restarting in the middle, skip the initial sectors */
  3346. if (mddev->delta_disks < 0 &&
  3347. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  3348. sector_nr = raid5_size(mddev, 0, 0)
  3349. - conf->reshape_progress;
  3350. } else if (mddev->delta_disks >= 0 &&
  3351. conf->reshape_progress > 0)
  3352. sector_nr = conf->reshape_progress;
  3353. sector_div(sector_nr, new_data_disks);
  3354. if (sector_nr) {
  3355. *skipped = 1;
  3356. return sector_nr;
  3357. }
  3358. }
  3359. /* We need to process a full chunk at a time.
  3360. * If old and new chunk sizes differ, we need to process the
  3361. * largest of these
  3362. */
  3363. if (mddev->new_chunk_sectors > mddev->chunk_sectors)
  3364. reshape_sectors = mddev->new_chunk_sectors;
  3365. else
  3366. reshape_sectors = mddev->chunk_sectors;
  3367. /* we update the metadata when there is more than 3Meg
  3368. * in the block range (that is rather arbitrary, should
  3369. * probably be time based) or when the data about to be
  3370. * copied would over-write the source of the data at
  3371. * the front of the range.
  3372. * i.e. one new_stripe along from reshape_progress new_maps
  3373. * to after where reshape_safe old_maps to
  3374. */
  3375. writepos = conf->reshape_progress;
  3376. sector_div(writepos, new_data_disks);
  3377. readpos = conf->reshape_progress;
  3378. sector_div(readpos, data_disks);
  3379. safepos = conf->reshape_safe;
  3380. sector_div(safepos, data_disks);
  3381. if (mddev->delta_disks < 0) {
  3382. writepos -= min_t(sector_t, reshape_sectors, writepos);
  3383. readpos += reshape_sectors;
  3384. safepos += reshape_sectors;
  3385. } else {
  3386. writepos += reshape_sectors;
  3387. readpos -= min_t(sector_t, reshape_sectors, readpos);
  3388. safepos -= min_t(sector_t, reshape_sectors, safepos);
  3389. }
  3390. /* 'writepos' is the most advanced device address we might write.
  3391. * 'readpos' is the least advanced device address we might read.
  3392. * 'safepos' is the least address recorded in the metadata as having
  3393. * been reshaped.
  3394. * If 'readpos' is behind 'writepos', then there is no way that we can
  3395. * ensure safety in the face of a crash - that must be done by userspace
  3396. * making a backup of the data. So in that case there is no particular
  3397. * rush to update metadata.
  3398. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  3399. * update the metadata to advance 'safepos' to match 'readpos' so that
  3400. * we can be safe in the event of a crash.
  3401. * So we insist on updating metadata if safepos is behind writepos and
  3402. * readpos is beyond writepos.
  3403. * In any case, update the metadata every 10 seconds.
  3404. * Maybe that number should be configurable, but I'm not sure it is
  3405. * worth it.... maybe it could be a multiple of safemode_delay???
  3406. */
  3407. if ((mddev->delta_disks < 0
  3408. ? (safepos > writepos && readpos < writepos)
  3409. : (safepos < writepos && readpos > writepos)) ||
  3410. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3411. /* Cannot proceed until we've updated the superblock... */
  3412. wait_event(conf->wait_for_overlap,
  3413. atomic_read(&conf->reshape_stripes)==0);
  3414. mddev->reshape_position = conf->reshape_progress;
  3415. mddev->curr_resync_completed = mddev->curr_resync;
  3416. conf->reshape_checkpoint = jiffies;
  3417. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3418. md_wakeup_thread(mddev->thread);
  3419. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3420. kthread_should_stop());
  3421. spin_lock_irq(&conf->device_lock);
  3422. conf->reshape_safe = mddev->reshape_position;
  3423. spin_unlock_irq(&conf->device_lock);
  3424. wake_up(&conf->wait_for_overlap);
  3425. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3426. }
  3427. if (mddev->delta_disks < 0) {
  3428. BUG_ON(conf->reshape_progress == 0);
  3429. stripe_addr = writepos;
  3430. BUG_ON((mddev->dev_sectors &
  3431. ~((sector_t)reshape_sectors - 1))
  3432. - reshape_sectors - stripe_addr
  3433. != sector_nr);
  3434. } else {
  3435. BUG_ON(writepos != sector_nr + reshape_sectors);
  3436. stripe_addr = sector_nr;
  3437. }
  3438. INIT_LIST_HEAD(&stripes);
  3439. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  3440. int j;
  3441. int skipped = 0;
  3442. sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  3443. set_bit(STRIPE_EXPANDING, &sh->state);
  3444. atomic_inc(&conf->reshape_stripes);
  3445. /* If any of this stripe is beyond the end of the old
  3446. * array, then we need to zero those blocks
  3447. */
  3448. for (j=sh->disks; j--;) {
  3449. sector_t s;
  3450. if (j == sh->pd_idx)
  3451. continue;
  3452. if (conf->level == 6 &&
  3453. j == sh->qd_idx)
  3454. continue;
  3455. s = compute_blocknr(sh, j, 0);
  3456. if (s < raid5_size(mddev, 0, 0)) {
  3457. skipped = 1;
  3458. continue;
  3459. }
  3460. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3461. set_bit(R5_Expanded, &sh->dev[j].flags);
  3462. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3463. }
  3464. if (!skipped) {
  3465. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3466. set_bit(STRIPE_HANDLE, &sh->state);
  3467. }
  3468. list_add(&sh->lru, &stripes);
  3469. }
  3470. spin_lock_irq(&conf->device_lock);
  3471. if (mddev->delta_disks < 0)
  3472. conf->reshape_progress -= reshape_sectors * new_data_disks;
  3473. else
  3474. conf->reshape_progress += reshape_sectors * new_data_disks;
  3475. spin_unlock_irq(&conf->device_lock);
  3476. /* Ok, those stripe are ready. We can start scheduling
  3477. * reads on the source stripes.
  3478. * The source stripes are determined by mapping the first and last
  3479. * block on the destination stripes.
  3480. */
  3481. first_sector =
  3482. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  3483. 1, &dd_idx, NULL);
  3484. last_sector =
  3485. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  3486. * new_data_disks - 1),
  3487. 1, &dd_idx, NULL);
  3488. if (last_sector >= mddev->dev_sectors)
  3489. last_sector = mddev->dev_sectors - 1;
  3490. while (first_sector <= last_sector) {
  3491. sh = get_active_stripe(conf, first_sector, 1, 0, 1);
  3492. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3493. set_bit(STRIPE_HANDLE, &sh->state);
  3494. release_stripe(sh);
  3495. first_sector += STRIPE_SECTORS;
  3496. }
  3497. /* Now that the sources are clearly marked, we can release
  3498. * the destination stripes
  3499. */
  3500. while (!list_empty(&stripes)) {
  3501. sh = list_entry(stripes.next, struct stripe_head, lru);
  3502. list_del_init(&sh->lru);
  3503. release_stripe(sh);
  3504. }
  3505. /* If this takes us to the resync_max point where we have to pause,
  3506. * then we need to write out the superblock.
  3507. */
  3508. sector_nr += reshape_sectors;
  3509. if ((sector_nr - mddev->curr_resync_completed) * 2
  3510. >= mddev->resync_max - mddev->curr_resync_completed) {
  3511. /* Cannot proceed until we've updated the superblock... */
  3512. wait_event(conf->wait_for_overlap,
  3513. atomic_read(&conf->reshape_stripes) == 0);
  3514. mddev->reshape_position = conf->reshape_progress;
  3515. mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors;
  3516. conf->reshape_checkpoint = jiffies;
  3517. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3518. md_wakeup_thread(mddev->thread);
  3519. wait_event(mddev->sb_wait,
  3520. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3521. || kthread_should_stop());
  3522. spin_lock_irq(&conf->device_lock);
  3523. conf->reshape_safe = mddev->reshape_position;
  3524. spin_unlock_irq(&conf->device_lock);
  3525. wake_up(&conf->wait_for_overlap);
  3526. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3527. }
  3528. return reshape_sectors;
  3529. }
  3530. /* FIXME go_faster isn't used */
  3531. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3532. {
  3533. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3534. struct stripe_head *sh;
  3535. sector_t max_sector = mddev->dev_sectors;
  3536. int sync_blocks;
  3537. int still_degraded = 0;
  3538. int i;
  3539. if (sector_nr >= max_sector) {
  3540. /* just being told to finish up .. nothing much to do */
  3541. unplug_slaves(mddev);
  3542. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3543. end_reshape(conf);
  3544. return 0;
  3545. }
  3546. if (mddev->curr_resync < max_sector) /* aborted */
  3547. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3548. &sync_blocks, 1);
  3549. else /* completed sync */
  3550. conf->fullsync = 0;
  3551. bitmap_close_sync(mddev->bitmap);
  3552. return 0;
  3553. }
  3554. /* Allow raid5_quiesce to complete */
  3555. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  3556. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3557. return reshape_request(mddev, sector_nr, skipped);
  3558. /* No need to check resync_max as we never do more than one
  3559. * stripe, and as resync_max will always be on a chunk boundary,
  3560. * if the check in md_do_sync didn't fire, there is no chance
  3561. * of overstepping resync_max here
  3562. */
  3563. /* if there is too many failed drives and we are trying
  3564. * to resync, then assert that we are finished, because there is
  3565. * nothing we can do.
  3566. */
  3567. if (mddev->degraded >= conf->max_degraded &&
  3568. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3569. sector_t rv = mddev->dev_sectors - sector_nr;
  3570. *skipped = 1;
  3571. return rv;
  3572. }
  3573. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3574. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3575. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3576. /* we can skip this block, and probably more */
  3577. sync_blocks /= STRIPE_SECTORS;
  3578. *skipped = 1;
  3579. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3580. }
  3581. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3582. sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
  3583. if (sh == NULL) {
  3584. sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
  3585. /* make sure we don't swamp the stripe cache if someone else
  3586. * is trying to get access
  3587. */
  3588. schedule_timeout_uninterruptible(1);
  3589. }
  3590. /* Need to check if array will still be degraded after recovery/resync
  3591. * We don't need to check the 'failed' flag as when that gets set,
  3592. * recovery aborts.
  3593. */
  3594. for (i = 0; i < conf->raid_disks; i++)
  3595. if (conf->disks[i].rdev == NULL)
  3596. still_degraded = 1;
  3597. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3598. spin_lock(&sh->lock);
  3599. set_bit(STRIPE_SYNCING, &sh->state);
  3600. clear_bit(STRIPE_INSYNC, &sh->state);
  3601. spin_unlock(&sh->lock);
  3602. /* wait for any blocked device to be handled */
  3603. while(unlikely(!handle_stripe(sh, NULL)))
  3604. ;
  3605. release_stripe(sh);
  3606. return STRIPE_SECTORS;
  3607. }
  3608. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3609. {
  3610. /* We may not be able to submit a whole bio at once as there
  3611. * may not be enough stripe_heads available.
  3612. * We cannot pre-allocate enough stripe_heads as we may need
  3613. * more than exist in the cache (if we allow ever large chunks).
  3614. * So we do one stripe head at a time and record in
  3615. * ->bi_hw_segments how many have been done.
  3616. *
  3617. * We *know* that this entire raid_bio is in one chunk, so
  3618. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3619. */
  3620. struct stripe_head *sh;
  3621. int dd_idx;
  3622. sector_t sector, logical_sector, last_sector;
  3623. int scnt = 0;
  3624. int remaining;
  3625. int handled = 0;
  3626. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3627. sector = raid5_compute_sector(conf, logical_sector,
  3628. 0, &dd_idx, NULL);
  3629. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3630. for (; logical_sector < last_sector;
  3631. logical_sector += STRIPE_SECTORS,
  3632. sector += STRIPE_SECTORS,
  3633. scnt++) {
  3634. if (scnt < raid5_bi_hw_segments(raid_bio))
  3635. /* already done this stripe */
  3636. continue;
  3637. sh = get_active_stripe(conf, sector, 0, 1, 0);
  3638. if (!sh) {
  3639. /* failed to get a stripe - must wait */
  3640. raid5_set_bi_hw_segments(raid_bio, scnt);
  3641. conf->retry_read_aligned = raid_bio;
  3642. return handled;
  3643. }
  3644. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3645. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3646. release_stripe(sh);
  3647. raid5_set_bi_hw_segments(raid_bio, scnt);
  3648. conf->retry_read_aligned = raid_bio;
  3649. return handled;
  3650. }
  3651. handle_stripe(sh, NULL);
  3652. release_stripe(sh);
  3653. handled++;
  3654. }
  3655. spin_lock_irq(&conf->device_lock);
  3656. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3657. spin_unlock_irq(&conf->device_lock);
  3658. if (remaining == 0)
  3659. bio_endio(raid_bio, 0);
  3660. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3661. wake_up(&conf->wait_for_stripe);
  3662. return handled;
  3663. }
  3664. /*
  3665. * This is our raid5 kernel thread.
  3666. *
  3667. * We scan the hash table for stripes which can be handled now.
  3668. * During the scan, completed stripes are saved for us by the interrupt
  3669. * handler, so that they will not have to wait for our next wakeup.
  3670. */
  3671. static void raid5d(mddev_t *mddev)
  3672. {
  3673. struct stripe_head *sh;
  3674. raid5_conf_t *conf = mddev->private;
  3675. int handled;
  3676. pr_debug("+++ raid5d active\n");
  3677. md_check_recovery(mddev);
  3678. handled = 0;
  3679. spin_lock_irq(&conf->device_lock);
  3680. while (1) {
  3681. struct bio *bio;
  3682. if (conf->seq_flush != conf->seq_write) {
  3683. int seq = conf->seq_flush;
  3684. spin_unlock_irq(&conf->device_lock);
  3685. bitmap_unplug(mddev->bitmap);
  3686. spin_lock_irq(&conf->device_lock);
  3687. conf->seq_write = seq;
  3688. activate_bit_delay(conf);
  3689. }
  3690. while ((bio = remove_bio_from_retry(conf))) {
  3691. int ok;
  3692. spin_unlock_irq(&conf->device_lock);
  3693. ok = retry_aligned_read(conf, bio);
  3694. spin_lock_irq(&conf->device_lock);
  3695. if (!ok)
  3696. break;
  3697. handled++;
  3698. }
  3699. sh = __get_priority_stripe(conf);
  3700. if (!sh)
  3701. break;
  3702. spin_unlock_irq(&conf->device_lock);
  3703. handled++;
  3704. handle_stripe(sh, conf->spare_page);
  3705. release_stripe(sh);
  3706. spin_lock_irq(&conf->device_lock);
  3707. }
  3708. pr_debug("%d stripes handled\n", handled);
  3709. spin_unlock_irq(&conf->device_lock);
  3710. async_tx_issue_pending_all();
  3711. unplug_slaves(mddev);
  3712. pr_debug("--- raid5d inactive\n");
  3713. }
  3714. static ssize_t
  3715. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  3716. {
  3717. raid5_conf_t *conf = mddev->private;
  3718. if (conf)
  3719. return sprintf(page, "%d\n", conf->max_nr_stripes);
  3720. else
  3721. return 0;
  3722. }
  3723. static ssize_t
  3724. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  3725. {
  3726. raid5_conf_t *conf = mddev->private;
  3727. unsigned long new;
  3728. int err;
  3729. if (len >= PAGE_SIZE)
  3730. return -EINVAL;
  3731. if (!conf)
  3732. return -ENODEV;
  3733. if (strict_strtoul(page, 10, &new))
  3734. return -EINVAL;
  3735. if (new <= 16 || new > 32768)
  3736. return -EINVAL;
  3737. while (new < conf->max_nr_stripes) {
  3738. if (drop_one_stripe(conf))
  3739. conf->max_nr_stripes--;
  3740. else
  3741. break;
  3742. }
  3743. err = md_allow_write(mddev);
  3744. if (err)
  3745. return err;
  3746. while (new > conf->max_nr_stripes) {
  3747. if (grow_one_stripe(conf))
  3748. conf->max_nr_stripes++;
  3749. else break;
  3750. }
  3751. return len;
  3752. }
  3753. static struct md_sysfs_entry
  3754. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  3755. raid5_show_stripe_cache_size,
  3756. raid5_store_stripe_cache_size);
  3757. static ssize_t
  3758. raid5_show_preread_threshold(mddev_t *mddev, char *page)
  3759. {
  3760. raid5_conf_t *conf = mddev->private;
  3761. if (conf)
  3762. return sprintf(page, "%d\n", conf->bypass_threshold);
  3763. else
  3764. return 0;
  3765. }
  3766. static ssize_t
  3767. raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
  3768. {
  3769. raid5_conf_t *conf = mddev->private;
  3770. unsigned long new;
  3771. if (len >= PAGE_SIZE)
  3772. return -EINVAL;
  3773. if (!conf)
  3774. return -ENODEV;
  3775. if (strict_strtoul(page, 10, &new))
  3776. return -EINVAL;
  3777. if (new > conf->max_nr_stripes)
  3778. return -EINVAL;
  3779. conf->bypass_threshold = new;
  3780. return len;
  3781. }
  3782. static struct md_sysfs_entry
  3783. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  3784. S_IRUGO | S_IWUSR,
  3785. raid5_show_preread_threshold,
  3786. raid5_store_preread_threshold);
  3787. static ssize_t
  3788. stripe_cache_active_show(mddev_t *mddev, char *page)
  3789. {
  3790. raid5_conf_t *conf = mddev->private;
  3791. if (conf)
  3792. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  3793. else
  3794. return 0;
  3795. }
  3796. static struct md_sysfs_entry
  3797. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  3798. static struct attribute *raid5_attrs[] = {
  3799. &raid5_stripecache_size.attr,
  3800. &raid5_stripecache_active.attr,
  3801. &raid5_preread_bypass_threshold.attr,
  3802. NULL,
  3803. };
  3804. static struct attribute_group raid5_attrs_group = {
  3805. .name = NULL,
  3806. .attrs = raid5_attrs,
  3807. };
  3808. static sector_t
  3809. raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  3810. {
  3811. raid5_conf_t *conf = mddev->private;
  3812. if (!sectors)
  3813. sectors = mddev->dev_sectors;
  3814. if (!raid_disks) {
  3815. /* size is defined by the smallest of previous and new size */
  3816. if (conf->raid_disks < conf->previous_raid_disks)
  3817. raid_disks = conf->raid_disks;
  3818. else
  3819. raid_disks = conf->previous_raid_disks;
  3820. }
  3821. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  3822. sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
  3823. return sectors * (raid_disks - conf->max_degraded);
  3824. }
  3825. static void free_conf(raid5_conf_t *conf)
  3826. {
  3827. shrink_stripes(conf);
  3828. safe_put_page(conf->spare_page);
  3829. kfree(conf->disks);
  3830. kfree(conf->stripe_hashtbl);
  3831. kfree(conf);
  3832. }
  3833. static raid5_conf_t *setup_conf(mddev_t *mddev)
  3834. {
  3835. raid5_conf_t *conf;
  3836. int raid_disk, memory;
  3837. mdk_rdev_t *rdev;
  3838. struct disk_info *disk;
  3839. if (mddev->new_level != 5
  3840. && mddev->new_level != 4
  3841. && mddev->new_level != 6) {
  3842. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  3843. mdname(mddev), mddev->new_level);
  3844. return ERR_PTR(-EIO);
  3845. }
  3846. if ((mddev->new_level == 5
  3847. && !algorithm_valid_raid5(mddev->new_layout)) ||
  3848. (mddev->new_level == 6
  3849. && !algorithm_valid_raid6(mddev->new_layout))) {
  3850. printk(KERN_ERR "raid5: %s: layout %d not supported\n",
  3851. mdname(mddev), mddev->new_layout);
  3852. return ERR_PTR(-EIO);
  3853. }
  3854. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  3855. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3856. mdname(mddev), mddev->raid_disks);
  3857. return ERR_PTR(-EINVAL);
  3858. }
  3859. if (!mddev->new_chunk_sectors ||
  3860. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  3861. !is_power_of_2(mddev->new_chunk_sectors)) {
  3862. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3863. mddev->new_chunk_sectors << 9, mdname(mddev));
  3864. return ERR_PTR(-EINVAL);
  3865. }
  3866. conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
  3867. if (conf == NULL)
  3868. goto abort;
  3869. conf->raid_disks = mddev->raid_disks;
  3870. if (mddev->reshape_position == MaxSector)
  3871. conf->previous_raid_disks = mddev->raid_disks;
  3872. else
  3873. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3874. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3875. GFP_KERNEL);
  3876. if (!conf->disks)
  3877. goto abort;
  3878. conf->mddev = mddev;
  3879. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3880. goto abort;
  3881. if (mddev->new_level == 6) {
  3882. conf->spare_page = alloc_page(GFP_KERNEL);
  3883. if (!conf->spare_page)
  3884. goto abort;
  3885. }
  3886. spin_lock_init(&conf->device_lock);
  3887. init_waitqueue_head(&conf->wait_for_stripe);
  3888. init_waitqueue_head(&conf->wait_for_overlap);
  3889. INIT_LIST_HEAD(&conf->handle_list);
  3890. INIT_LIST_HEAD(&conf->hold_list);
  3891. INIT_LIST_HEAD(&conf->delayed_list);
  3892. INIT_LIST_HEAD(&conf->bitmap_list);
  3893. INIT_LIST_HEAD(&conf->inactive_list);
  3894. atomic_set(&conf->active_stripes, 0);
  3895. atomic_set(&conf->preread_active_stripes, 0);
  3896. atomic_set(&conf->active_aligned_reads, 0);
  3897. conf->bypass_threshold = BYPASS_THRESHOLD;
  3898. pr_debug("raid5: run(%s) called.\n", mdname(mddev));
  3899. list_for_each_entry(rdev, &mddev->disks, same_set) {
  3900. raid_disk = rdev->raid_disk;
  3901. if (raid_disk >= conf->raid_disks
  3902. || raid_disk < 0)
  3903. continue;
  3904. disk = conf->disks + raid_disk;
  3905. disk->rdev = rdev;
  3906. if (test_bit(In_sync, &rdev->flags)) {
  3907. char b[BDEVNAME_SIZE];
  3908. printk(KERN_INFO "raid5: device %s operational as raid"
  3909. " disk %d\n", bdevname(rdev->bdev,b),
  3910. raid_disk);
  3911. } else
  3912. /* Cannot rely on bitmap to complete recovery */
  3913. conf->fullsync = 1;
  3914. }
  3915. conf->chunk_sectors = mddev->new_chunk_sectors;
  3916. conf->level = mddev->new_level;
  3917. if (conf->level == 6)
  3918. conf->max_degraded = 2;
  3919. else
  3920. conf->max_degraded = 1;
  3921. conf->algorithm = mddev->new_layout;
  3922. conf->max_nr_stripes = NR_STRIPES;
  3923. conf->reshape_progress = mddev->reshape_position;
  3924. if (conf->reshape_progress != MaxSector) {
  3925. conf->prev_chunk_sectors = mddev->chunk_sectors;
  3926. conf->prev_algo = mddev->layout;
  3927. }
  3928. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3929. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3930. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3931. printk(KERN_ERR
  3932. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3933. goto abort;
  3934. } else
  3935. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3936. memory, mdname(mddev));
  3937. conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3938. if (!conf->thread) {
  3939. printk(KERN_ERR
  3940. "raid5: couldn't allocate thread for %s\n",
  3941. mdname(mddev));
  3942. goto abort;
  3943. }
  3944. return conf;
  3945. abort:
  3946. if (conf) {
  3947. free_conf(conf);
  3948. return ERR_PTR(-EIO);
  3949. } else
  3950. return ERR_PTR(-ENOMEM);
  3951. }
  3952. static int run(mddev_t *mddev)
  3953. {
  3954. raid5_conf_t *conf;
  3955. int working_disks = 0, chunk_size;
  3956. mdk_rdev_t *rdev;
  3957. if (mddev->recovery_cp != MaxSector)
  3958. printk(KERN_NOTICE "raid5: %s is not clean"
  3959. " -- starting background reconstruction\n",
  3960. mdname(mddev));
  3961. if (mddev->reshape_position != MaxSector) {
  3962. /* Check that we can continue the reshape.
  3963. * Currently only disks can change, it must
  3964. * increase, and we must be past the point where
  3965. * a stripe over-writes itself
  3966. */
  3967. sector_t here_new, here_old;
  3968. int old_disks;
  3969. int max_degraded = (mddev->level == 6 ? 2 : 1);
  3970. if (mddev->new_level != mddev->level) {
  3971. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3972. "required - aborting.\n",
  3973. mdname(mddev));
  3974. return -EINVAL;
  3975. }
  3976. old_disks = mddev->raid_disks - mddev->delta_disks;
  3977. /* reshape_position must be on a new-stripe boundary, and one
  3978. * further up in new geometry must map after here in old
  3979. * geometry.
  3980. */
  3981. here_new = mddev->reshape_position;
  3982. if (sector_div(here_new, mddev->new_chunk_sectors *
  3983. (mddev->raid_disks - max_degraded))) {
  3984. printk(KERN_ERR "raid5: reshape_position not "
  3985. "on a stripe boundary\n");
  3986. return -EINVAL;
  3987. }
  3988. /* here_new is the stripe we will write to */
  3989. here_old = mddev->reshape_position;
  3990. sector_div(here_old, mddev->chunk_sectors *
  3991. (old_disks-max_degraded));
  3992. /* here_old is the first stripe that we might need to read
  3993. * from */
  3994. if (mddev->delta_disks == 0) {
  3995. /* We cannot be sure it is safe to start an in-place
  3996. * reshape. It is only safe if user-space if monitoring
  3997. * and taking constant backups.
  3998. * mdadm always starts a situation like this in
  3999. * readonly mode so it can take control before
  4000. * allowing any writes. So just check for that.
  4001. */
  4002. if ((here_new * mddev->new_chunk_sectors !=
  4003. here_old * mddev->chunk_sectors) ||
  4004. mddev->ro == 0) {
  4005. printk(KERN_ERR "raid5: in-place reshape must be started"
  4006. " in read-only mode - aborting\n");
  4007. return -EINVAL;
  4008. }
  4009. } else if (mddev->delta_disks < 0
  4010. ? (here_new * mddev->new_chunk_sectors <=
  4011. here_old * mddev->chunk_sectors)
  4012. : (here_new * mddev->new_chunk_sectors >=
  4013. here_old * mddev->chunk_sectors)) {
  4014. /* Reading from the same stripe as writing to - bad */
  4015. printk(KERN_ERR "raid5: reshape_position too early for "
  4016. "auto-recovery - aborting.\n");
  4017. return -EINVAL;
  4018. }
  4019. printk(KERN_INFO "raid5: reshape will continue\n");
  4020. /* OK, we should be able to continue; */
  4021. } else {
  4022. BUG_ON(mddev->level != mddev->new_level);
  4023. BUG_ON(mddev->layout != mddev->new_layout);
  4024. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  4025. BUG_ON(mddev->delta_disks != 0);
  4026. }
  4027. if (mddev->private == NULL)
  4028. conf = setup_conf(mddev);
  4029. else
  4030. conf = mddev->private;
  4031. if (IS_ERR(conf))
  4032. return PTR_ERR(conf);
  4033. mddev->thread = conf->thread;
  4034. conf->thread = NULL;
  4035. mddev->private = conf;
  4036. /*
  4037. * 0 for a fully functional array, 1 or 2 for a degraded array.
  4038. */
  4039. list_for_each_entry(rdev, &mddev->disks, same_set)
  4040. if (rdev->raid_disk >= 0 &&
  4041. test_bit(In_sync, &rdev->flags))
  4042. working_disks++;
  4043. mddev->degraded = conf->raid_disks - working_disks;
  4044. if (mddev->degraded > conf->max_degraded) {
  4045. printk(KERN_ERR "raid5: not enough operational devices for %s"
  4046. " (%d/%d failed)\n",
  4047. mdname(mddev), mddev->degraded, conf->raid_disks);
  4048. goto abort;
  4049. }
  4050. /* device size must be a multiple of chunk size */
  4051. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  4052. mddev->resync_max_sectors = mddev->dev_sectors;
  4053. if (mddev->degraded > 0 &&
  4054. mddev->recovery_cp != MaxSector) {
  4055. if (mddev->ok_start_degraded)
  4056. printk(KERN_WARNING
  4057. "raid5: starting dirty degraded array: %s"
  4058. "- data corruption possible.\n",
  4059. mdname(mddev));
  4060. else {
  4061. printk(KERN_ERR
  4062. "raid5: cannot start dirty degraded array for %s\n",
  4063. mdname(mddev));
  4064. goto abort;
  4065. }
  4066. }
  4067. if (mddev->degraded == 0)
  4068. printk("raid5: raid level %d set %s active with %d out of %d"
  4069. " devices, algorithm %d\n", conf->level, mdname(mddev),
  4070. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  4071. mddev->new_layout);
  4072. else
  4073. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  4074. " out of %d devices, algorithm %d\n", conf->level,
  4075. mdname(mddev), mddev->raid_disks - mddev->degraded,
  4076. mddev->raid_disks, mddev->new_layout);
  4077. print_raid5_conf(conf);
  4078. if (conf->reshape_progress != MaxSector) {
  4079. printk("...ok start reshape thread\n");
  4080. conf->reshape_safe = conf->reshape_progress;
  4081. atomic_set(&conf->reshape_stripes, 0);
  4082. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4083. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4084. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4085. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4086. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4087. "%s_reshape");
  4088. }
  4089. /* read-ahead size must cover two whole stripes, which is
  4090. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4091. */
  4092. {
  4093. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4094. int stripe = data_disks *
  4095. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  4096. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4097. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4098. }
  4099. /* Ok, everything is just fine now */
  4100. if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  4101. printk(KERN_WARNING
  4102. "raid5: failed to create sysfs attributes for %s\n",
  4103. mdname(mddev));
  4104. mddev->queue->queue_lock = &conf->device_lock;
  4105. mddev->queue->unplug_fn = raid5_unplug_device;
  4106. mddev->queue->backing_dev_info.congested_data = mddev;
  4107. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  4108. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4109. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  4110. chunk_size = mddev->chunk_sectors << 9;
  4111. blk_queue_io_min(mddev->queue, chunk_size);
  4112. blk_queue_io_opt(mddev->queue, chunk_size *
  4113. (conf->raid_disks - conf->max_degraded));
  4114. list_for_each_entry(rdev, &mddev->disks, same_set)
  4115. disk_stack_limits(mddev->gendisk, rdev->bdev,
  4116. rdev->data_offset << 9);
  4117. return 0;
  4118. abort:
  4119. md_unregister_thread(mddev->thread);
  4120. mddev->thread = NULL;
  4121. if (conf) {
  4122. print_raid5_conf(conf);
  4123. free_conf(conf);
  4124. }
  4125. mddev->private = NULL;
  4126. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  4127. return -EIO;
  4128. }
  4129. static int stop(mddev_t *mddev)
  4130. {
  4131. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  4132. md_unregister_thread(mddev->thread);
  4133. mddev->thread = NULL;
  4134. mddev->queue->backing_dev_info.congested_fn = NULL;
  4135. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  4136. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  4137. free_conf(conf);
  4138. mddev->private = NULL;
  4139. return 0;
  4140. }
  4141. #ifdef DEBUG
  4142. static void print_sh(struct seq_file *seq, struct stripe_head *sh)
  4143. {
  4144. int i;
  4145. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  4146. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  4147. seq_printf(seq, "sh %llu, count %d.\n",
  4148. (unsigned long long)sh->sector, atomic_read(&sh->count));
  4149. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  4150. for (i = 0; i < sh->disks; i++) {
  4151. seq_printf(seq, "(cache%d: %p %ld) ",
  4152. i, sh->dev[i].page, sh->dev[i].flags);
  4153. }
  4154. seq_printf(seq, "\n");
  4155. }
  4156. static void printall(struct seq_file *seq, raid5_conf_t *conf)
  4157. {
  4158. struct stripe_head *sh;
  4159. struct hlist_node *hn;
  4160. int i;
  4161. spin_lock_irq(&conf->device_lock);
  4162. for (i = 0; i < NR_HASH; i++) {
  4163. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  4164. if (sh->raid_conf != conf)
  4165. continue;
  4166. print_sh(seq, sh);
  4167. }
  4168. }
  4169. spin_unlock_irq(&conf->device_lock);
  4170. }
  4171. #endif
  4172. static void status(struct seq_file *seq, mddev_t *mddev)
  4173. {
  4174. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  4175. int i;
  4176. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  4177. mddev->chunk_sectors / 2, mddev->layout);
  4178. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  4179. for (i = 0; i < conf->raid_disks; i++)
  4180. seq_printf (seq, "%s",
  4181. conf->disks[i].rdev &&
  4182. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  4183. seq_printf (seq, "]");
  4184. #ifdef DEBUG
  4185. seq_printf (seq, "\n");
  4186. printall(seq, conf);
  4187. #endif
  4188. }
  4189. static void print_raid5_conf (raid5_conf_t *conf)
  4190. {
  4191. int i;
  4192. struct disk_info *tmp;
  4193. printk("RAID5 conf printout:\n");
  4194. if (!conf) {
  4195. printk("(conf==NULL)\n");
  4196. return;
  4197. }
  4198. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  4199. conf->raid_disks - conf->mddev->degraded);
  4200. for (i = 0; i < conf->raid_disks; i++) {
  4201. char b[BDEVNAME_SIZE];
  4202. tmp = conf->disks + i;
  4203. if (tmp->rdev)
  4204. printk(" disk %d, o:%d, dev:%s\n",
  4205. i, !test_bit(Faulty, &tmp->rdev->flags),
  4206. bdevname(tmp->rdev->bdev,b));
  4207. }
  4208. }
  4209. static int raid5_spare_active(mddev_t *mddev)
  4210. {
  4211. int i;
  4212. raid5_conf_t *conf = mddev->private;
  4213. struct disk_info *tmp;
  4214. for (i = 0; i < conf->raid_disks; i++) {
  4215. tmp = conf->disks + i;
  4216. if (tmp->rdev
  4217. && !test_bit(Faulty, &tmp->rdev->flags)
  4218. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  4219. unsigned long flags;
  4220. spin_lock_irqsave(&conf->device_lock, flags);
  4221. mddev->degraded--;
  4222. spin_unlock_irqrestore(&conf->device_lock, flags);
  4223. }
  4224. }
  4225. print_raid5_conf(conf);
  4226. return 0;
  4227. }
  4228. static int raid5_remove_disk(mddev_t *mddev, int number)
  4229. {
  4230. raid5_conf_t *conf = mddev->private;
  4231. int err = 0;
  4232. mdk_rdev_t *rdev;
  4233. struct disk_info *p = conf->disks + number;
  4234. print_raid5_conf(conf);
  4235. rdev = p->rdev;
  4236. if (rdev) {
  4237. if (number >= conf->raid_disks &&
  4238. conf->reshape_progress == MaxSector)
  4239. clear_bit(In_sync, &rdev->flags);
  4240. if (test_bit(In_sync, &rdev->flags) ||
  4241. atomic_read(&rdev->nr_pending)) {
  4242. err = -EBUSY;
  4243. goto abort;
  4244. }
  4245. /* Only remove non-faulty devices if recovery
  4246. * isn't possible.
  4247. */
  4248. if (!test_bit(Faulty, &rdev->flags) &&
  4249. mddev->degraded <= conf->max_degraded &&
  4250. number < conf->raid_disks) {
  4251. err = -EBUSY;
  4252. goto abort;
  4253. }
  4254. p->rdev = NULL;
  4255. synchronize_rcu();
  4256. if (atomic_read(&rdev->nr_pending)) {
  4257. /* lost the race, try later */
  4258. err = -EBUSY;
  4259. p->rdev = rdev;
  4260. }
  4261. }
  4262. abort:
  4263. print_raid5_conf(conf);
  4264. return err;
  4265. }
  4266. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  4267. {
  4268. raid5_conf_t *conf = mddev->private;
  4269. int err = -EEXIST;
  4270. int disk;
  4271. struct disk_info *p;
  4272. int first = 0;
  4273. int last = conf->raid_disks - 1;
  4274. if (mddev->degraded > conf->max_degraded)
  4275. /* no point adding a device */
  4276. return -EINVAL;
  4277. if (rdev->raid_disk >= 0)
  4278. first = last = rdev->raid_disk;
  4279. /*
  4280. * find the disk ... but prefer rdev->saved_raid_disk
  4281. * if possible.
  4282. */
  4283. if (rdev->saved_raid_disk >= 0 &&
  4284. rdev->saved_raid_disk >= first &&
  4285. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  4286. disk = rdev->saved_raid_disk;
  4287. else
  4288. disk = first;
  4289. for ( ; disk <= last ; disk++)
  4290. if ((p=conf->disks + disk)->rdev == NULL) {
  4291. clear_bit(In_sync, &rdev->flags);
  4292. rdev->raid_disk = disk;
  4293. err = 0;
  4294. if (rdev->saved_raid_disk != disk)
  4295. conf->fullsync = 1;
  4296. rcu_assign_pointer(p->rdev, rdev);
  4297. break;
  4298. }
  4299. print_raid5_conf(conf);
  4300. return err;
  4301. }
  4302. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  4303. {
  4304. /* no resync is happening, and there is enough space
  4305. * on all devices, so we can resize.
  4306. * We need to make sure resync covers any new space.
  4307. * If the array is shrinking we should possibly wait until
  4308. * any io in the removed space completes, but it hardly seems
  4309. * worth it.
  4310. */
  4311. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4312. md_set_array_sectors(mddev, raid5_size(mddev, sectors,
  4313. mddev->raid_disks));
  4314. if (mddev->array_sectors >
  4315. raid5_size(mddev, sectors, mddev->raid_disks))
  4316. return -EINVAL;
  4317. set_capacity(mddev->gendisk, mddev->array_sectors);
  4318. mddev->changed = 1;
  4319. revalidate_disk(mddev->gendisk);
  4320. if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
  4321. mddev->recovery_cp = mddev->dev_sectors;
  4322. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4323. }
  4324. mddev->dev_sectors = sectors;
  4325. mddev->resync_max_sectors = sectors;
  4326. return 0;
  4327. }
  4328. static int check_stripe_cache(mddev_t *mddev)
  4329. {
  4330. /* Can only proceed if there are plenty of stripe_heads.
  4331. * We need a minimum of one full stripe,, and for sensible progress
  4332. * it is best to have about 4 times that.
  4333. * If we require 4 times, then the default 256 4K stripe_heads will
  4334. * allow for chunk sizes up to 256K, which is probably OK.
  4335. * If the chunk size is greater, user-space should request more
  4336. * stripe_heads first.
  4337. */
  4338. raid5_conf_t *conf = mddev->private;
  4339. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  4340. > conf->max_nr_stripes ||
  4341. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  4342. > conf->max_nr_stripes) {
  4343. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  4344. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  4345. / STRIPE_SIZE)*4);
  4346. return 0;
  4347. }
  4348. return 1;
  4349. }
  4350. static int check_reshape(mddev_t *mddev)
  4351. {
  4352. raid5_conf_t *conf = mddev->private;
  4353. if (mddev->delta_disks == 0 &&
  4354. mddev->new_layout == mddev->layout &&
  4355. mddev->new_chunk_sectors == mddev->chunk_sectors)
  4356. return 0; /* nothing to do */
  4357. if (mddev->bitmap)
  4358. /* Cannot grow a bitmap yet */
  4359. return -EBUSY;
  4360. if (mddev->degraded > conf->max_degraded)
  4361. return -EINVAL;
  4362. if (mddev->delta_disks < 0) {
  4363. /* We might be able to shrink, but the devices must
  4364. * be made bigger first.
  4365. * For raid6, 4 is the minimum size.
  4366. * Otherwise 2 is the minimum
  4367. */
  4368. int min = 2;
  4369. if (mddev->level == 6)
  4370. min = 4;
  4371. if (mddev->raid_disks + mddev->delta_disks < min)
  4372. return -EINVAL;
  4373. }
  4374. if (!check_stripe_cache(mddev))
  4375. return -ENOSPC;
  4376. return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4377. }
  4378. static int raid5_start_reshape(mddev_t *mddev)
  4379. {
  4380. raid5_conf_t *conf = mddev->private;
  4381. mdk_rdev_t *rdev;
  4382. int spares = 0;
  4383. int added_devices = 0;
  4384. unsigned long flags;
  4385. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4386. return -EBUSY;
  4387. if (!check_stripe_cache(mddev))
  4388. return -ENOSPC;
  4389. list_for_each_entry(rdev, &mddev->disks, same_set)
  4390. if (rdev->raid_disk < 0 &&
  4391. !test_bit(Faulty, &rdev->flags))
  4392. spares++;
  4393. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4394. /* Not enough devices even to make a degraded array
  4395. * of that size
  4396. */
  4397. return -EINVAL;
  4398. /* Refuse to reduce size of the array. Any reductions in
  4399. * array size must be through explicit setting of array_size
  4400. * attribute.
  4401. */
  4402. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  4403. < mddev->array_sectors) {
  4404. printk(KERN_ERR "md: %s: array size must be reduced "
  4405. "before number of disks\n", mdname(mddev));
  4406. return -EINVAL;
  4407. }
  4408. atomic_set(&conf->reshape_stripes, 0);
  4409. spin_lock_irq(&conf->device_lock);
  4410. conf->previous_raid_disks = conf->raid_disks;
  4411. conf->raid_disks += mddev->delta_disks;
  4412. conf->prev_chunk_sectors = conf->chunk_sectors;
  4413. conf->chunk_sectors = mddev->new_chunk_sectors;
  4414. conf->prev_algo = conf->algorithm;
  4415. conf->algorithm = mddev->new_layout;
  4416. if (mddev->delta_disks < 0)
  4417. conf->reshape_progress = raid5_size(mddev, 0, 0);
  4418. else
  4419. conf->reshape_progress = 0;
  4420. conf->reshape_safe = conf->reshape_progress;
  4421. conf->generation++;
  4422. spin_unlock_irq(&conf->device_lock);
  4423. /* Add some new drives, as many as will fit.
  4424. * We know there are enough to make the newly sized array work.
  4425. */
  4426. list_for_each_entry(rdev, &mddev->disks, same_set)
  4427. if (rdev->raid_disk < 0 &&
  4428. !test_bit(Faulty, &rdev->flags)) {
  4429. if (raid5_add_disk(mddev, rdev) == 0) {
  4430. char nm[20];
  4431. set_bit(In_sync, &rdev->flags);
  4432. added_devices++;
  4433. rdev->recovery_offset = 0;
  4434. sprintf(nm, "rd%d", rdev->raid_disk);
  4435. if (sysfs_create_link(&mddev->kobj,
  4436. &rdev->kobj, nm))
  4437. printk(KERN_WARNING
  4438. "raid5: failed to create "
  4439. " link %s for %s\n",
  4440. nm, mdname(mddev));
  4441. } else
  4442. break;
  4443. }
  4444. if (mddev->delta_disks > 0) {
  4445. spin_lock_irqsave(&conf->device_lock, flags);
  4446. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
  4447. - added_devices;
  4448. spin_unlock_irqrestore(&conf->device_lock, flags);
  4449. }
  4450. mddev->raid_disks = conf->raid_disks;
  4451. mddev->reshape_position = conf->reshape_progress;
  4452. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4453. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4454. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4455. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4456. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4457. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4458. "%s_reshape");
  4459. if (!mddev->sync_thread) {
  4460. mddev->recovery = 0;
  4461. spin_lock_irq(&conf->device_lock);
  4462. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4463. conf->reshape_progress = MaxSector;
  4464. spin_unlock_irq(&conf->device_lock);
  4465. return -EAGAIN;
  4466. }
  4467. conf->reshape_checkpoint = jiffies;
  4468. md_wakeup_thread(mddev->sync_thread);
  4469. md_new_event(mddev);
  4470. return 0;
  4471. }
  4472. /* This is called from the reshape thread and should make any
  4473. * changes needed in 'conf'
  4474. */
  4475. static void end_reshape(raid5_conf_t *conf)
  4476. {
  4477. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4478. spin_lock_irq(&conf->device_lock);
  4479. conf->previous_raid_disks = conf->raid_disks;
  4480. conf->reshape_progress = MaxSector;
  4481. spin_unlock_irq(&conf->device_lock);
  4482. wake_up(&conf->wait_for_overlap);
  4483. /* read-ahead size must cover two whole stripes, which is
  4484. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4485. */
  4486. {
  4487. int data_disks = conf->raid_disks - conf->max_degraded;
  4488. int stripe = data_disks * ((conf->chunk_sectors << 9)
  4489. / PAGE_SIZE);
  4490. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4491. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4492. }
  4493. }
  4494. }
  4495. /* This is called from the raid5d thread with mddev_lock held.
  4496. * It makes config changes to the device.
  4497. */
  4498. static void raid5_finish_reshape(mddev_t *mddev)
  4499. {
  4500. raid5_conf_t *conf = mddev->private;
  4501. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  4502. if (mddev->delta_disks > 0) {
  4503. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4504. set_capacity(mddev->gendisk, mddev->array_sectors);
  4505. mddev->changed = 1;
  4506. revalidate_disk(mddev->gendisk);
  4507. } else {
  4508. int d;
  4509. mddev->degraded = conf->raid_disks;
  4510. for (d = 0; d < conf->raid_disks ; d++)
  4511. if (conf->disks[d].rdev &&
  4512. test_bit(In_sync,
  4513. &conf->disks[d].rdev->flags))
  4514. mddev->degraded--;
  4515. for (d = conf->raid_disks ;
  4516. d < conf->raid_disks - mddev->delta_disks;
  4517. d++) {
  4518. mdk_rdev_t *rdev = conf->disks[d].rdev;
  4519. if (rdev && raid5_remove_disk(mddev, d) == 0) {
  4520. char nm[20];
  4521. sprintf(nm, "rd%d", rdev->raid_disk);
  4522. sysfs_remove_link(&mddev->kobj, nm);
  4523. rdev->raid_disk = -1;
  4524. }
  4525. }
  4526. }
  4527. mddev->layout = conf->algorithm;
  4528. mddev->chunk_sectors = conf->chunk_sectors;
  4529. mddev->reshape_position = MaxSector;
  4530. mddev->delta_disks = 0;
  4531. }
  4532. }
  4533. static void raid5_quiesce(mddev_t *mddev, int state)
  4534. {
  4535. raid5_conf_t *conf = mddev->private;
  4536. switch(state) {
  4537. case 2: /* resume for a suspend */
  4538. wake_up(&conf->wait_for_overlap);
  4539. break;
  4540. case 1: /* stop all writes */
  4541. spin_lock_irq(&conf->device_lock);
  4542. /* '2' tells resync/reshape to pause so that all
  4543. * active stripes can drain
  4544. */
  4545. conf->quiesce = 2;
  4546. wait_event_lock_irq(conf->wait_for_stripe,
  4547. atomic_read(&conf->active_stripes) == 0 &&
  4548. atomic_read(&conf->active_aligned_reads) == 0,
  4549. conf->device_lock, /* nothing */);
  4550. conf->quiesce = 1;
  4551. spin_unlock_irq(&conf->device_lock);
  4552. /* allow reshape to continue */
  4553. wake_up(&conf->wait_for_overlap);
  4554. break;
  4555. case 0: /* re-enable writes */
  4556. spin_lock_irq(&conf->device_lock);
  4557. conf->quiesce = 0;
  4558. wake_up(&conf->wait_for_stripe);
  4559. wake_up(&conf->wait_for_overlap);
  4560. spin_unlock_irq(&conf->device_lock);
  4561. break;
  4562. }
  4563. }
  4564. static void *raid5_takeover_raid1(mddev_t *mddev)
  4565. {
  4566. int chunksect;
  4567. if (mddev->raid_disks != 2 ||
  4568. mddev->degraded > 1)
  4569. return ERR_PTR(-EINVAL);
  4570. /* Should check if there are write-behind devices? */
  4571. chunksect = 64*2; /* 64K by default */
  4572. /* The array must be an exact multiple of chunksize */
  4573. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  4574. chunksect >>= 1;
  4575. if ((chunksect<<9) < STRIPE_SIZE)
  4576. /* array size does not allow a suitable chunk size */
  4577. return ERR_PTR(-EINVAL);
  4578. mddev->new_level = 5;
  4579. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  4580. mddev->new_chunk_sectors = chunksect;
  4581. return setup_conf(mddev);
  4582. }
  4583. static void *raid5_takeover_raid6(mddev_t *mddev)
  4584. {
  4585. int new_layout;
  4586. switch (mddev->layout) {
  4587. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4588. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  4589. break;
  4590. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4591. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  4592. break;
  4593. case ALGORITHM_LEFT_SYMMETRIC_6:
  4594. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  4595. break;
  4596. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4597. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  4598. break;
  4599. case ALGORITHM_PARITY_0_6:
  4600. new_layout = ALGORITHM_PARITY_0;
  4601. break;
  4602. case ALGORITHM_PARITY_N:
  4603. new_layout = ALGORITHM_PARITY_N;
  4604. break;
  4605. default:
  4606. return ERR_PTR(-EINVAL);
  4607. }
  4608. mddev->new_level = 5;
  4609. mddev->new_layout = new_layout;
  4610. mddev->delta_disks = -1;
  4611. mddev->raid_disks -= 1;
  4612. return setup_conf(mddev);
  4613. }
  4614. static int raid5_check_reshape(mddev_t *mddev)
  4615. {
  4616. /* For a 2-drive array, the layout and chunk size can be changed
  4617. * immediately as not restriping is needed.
  4618. * For larger arrays we record the new value - after validation
  4619. * to be used by a reshape pass.
  4620. */
  4621. raid5_conf_t *conf = mddev->private;
  4622. int new_chunk = mddev->new_chunk_sectors;
  4623. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  4624. return -EINVAL;
  4625. if (new_chunk > 0) {
  4626. if (!is_power_of_2(new_chunk))
  4627. return -EINVAL;
  4628. if (new_chunk < (PAGE_SIZE>>9))
  4629. return -EINVAL;
  4630. if (mddev->array_sectors & (new_chunk-1))
  4631. /* not factor of array size */
  4632. return -EINVAL;
  4633. }
  4634. /* They look valid */
  4635. if (mddev->raid_disks == 2) {
  4636. /* can make the change immediately */
  4637. if (mddev->new_layout >= 0) {
  4638. conf->algorithm = mddev->new_layout;
  4639. mddev->layout = mddev->new_layout;
  4640. }
  4641. if (new_chunk > 0) {
  4642. conf->chunk_sectors = new_chunk ;
  4643. mddev->chunk_sectors = new_chunk;
  4644. }
  4645. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4646. md_wakeup_thread(mddev->thread);
  4647. }
  4648. return check_reshape(mddev);
  4649. }
  4650. static int raid6_check_reshape(mddev_t *mddev)
  4651. {
  4652. int new_chunk = mddev->new_chunk_sectors;
  4653. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  4654. return -EINVAL;
  4655. if (new_chunk > 0) {
  4656. if (!is_power_of_2(new_chunk))
  4657. return -EINVAL;
  4658. if (new_chunk < (PAGE_SIZE >> 9))
  4659. return -EINVAL;
  4660. if (mddev->array_sectors & (new_chunk-1))
  4661. /* not factor of array size */
  4662. return -EINVAL;
  4663. }
  4664. /* They look valid */
  4665. return check_reshape(mddev);
  4666. }
  4667. static void *raid5_takeover(mddev_t *mddev)
  4668. {
  4669. /* raid5 can take over:
  4670. * raid0 - if all devices are the same - make it a raid4 layout
  4671. * raid1 - if there are two drives. We need to know the chunk size
  4672. * raid4 - trivial - just use a raid4 layout.
  4673. * raid6 - Providing it is a *_6 layout
  4674. */
  4675. if (mddev->level == 1)
  4676. return raid5_takeover_raid1(mddev);
  4677. if (mddev->level == 4) {
  4678. mddev->new_layout = ALGORITHM_PARITY_N;
  4679. mddev->new_level = 5;
  4680. return setup_conf(mddev);
  4681. }
  4682. if (mddev->level == 6)
  4683. return raid5_takeover_raid6(mddev);
  4684. return ERR_PTR(-EINVAL);
  4685. }
  4686. static struct mdk_personality raid5_personality;
  4687. static void *raid6_takeover(mddev_t *mddev)
  4688. {
  4689. /* Currently can only take over a raid5. We map the
  4690. * personality to an equivalent raid6 personality
  4691. * with the Q block at the end.
  4692. */
  4693. int new_layout;
  4694. if (mddev->pers != &raid5_personality)
  4695. return ERR_PTR(-EINVAL);
  4696. if (mddev->degraded > 1)
  4697. return ERR_PTR(-EINVAL);
  4698. if (mddev->raid_disks > 253)
  4699. return ERR_PTR(-EINVAL);
  4700. if (mddev->raid_disks < 3)
  4701. return ERR_PTR(-EINVAL);
  4702. switch (mddev->layout) {
  4703. case ALGORITHM_LEFT_ASYMMETRIC:
  4704. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  4705. break;
  4706. case ALGORITHM_RIGHT_ASYMMETRIC:
  4707. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  4708. break;
  4709. case ALGORITHM_LEFT_SYMMETRIC:
  4710. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  4711. break;
  4712. case ALGORITHM_RIGHT_SYMMETRIC:
  4713. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  4714. break;
  4715. case ALGORITHM_PARITY_0:
  4716. new_layout = ALGORITHM_PARITY_0_6;
  4717. break;
  4718. case ALGORITHM_PARITY_N:
  4719. new_layout = ALGORITHM_PARITY_N;
  4720. break;
  4721. default:
  4722. return ERR_PTR(-EINVAL);
  4723. }
  4724. mddev->new_level = 6;
  4725. mddev->new_layout = new_layout;
  4726. mddev->delta_disks = 1;
  4727. mddev->raid_disks += 1;
  4728. return setup_conf(mddev);
  4729. }
  4730. static struct mdk_personality raid6_personality =
  4731. {
  4732. .name = "raid6",
  4733. .level = 6,
  4734. .owner = THIS_MODULE,
  4735. .make_request = make_request,
  4736. .run = run,
  4737. .stop = stop,
  4738. .status = status,
  4739. .error_handler = error,
  4740. .hot_add_disk = raid5_add_disk,
  4741. .hot_remove_disk= raid5_remove_disk,
  4742. .spare_active = raid5_spare_active,
  4743. .sync_request = sync_request,
  4744. .resize = raid5_resize,
  4745. .size = raid5_size,
  4746. .check_reshape = raid6_check_reshape,
  4747. .start_reshape = raid5_start_reshape,
  4748. .finish_reshape = raid5_finish_reshape,
  4749. .quiesce = raid5_quiesce,
  4750. .takeover = raid6_takeover,
  4751. };
  4752. static struct mdk_personality raid5_personality =
  4753. {
  4754. .name = "raid5",
  4755. .level = 5,
  4756. .owner = THIS_MODULE,
  4757. .make_request = make_request,
  4758. .run = run,
  4759. .stop = stop,
  4760. .status = status,
  4761. .error_handler = error,
  4762. .hot_add_disk = raid5_add_disk,
  4763. .hot_remove_disk= raid5_remove_disk,
  4764. .spare_active = raid5_spare_active,
  4765. .sync_request = sync_request,
  4766. .resize = raid5_resize,
  4767. .size = raid5_size,
  4768. .check_reshape = raid5_check_reshape,
  4769. .start_reshape = raid5_start_reshape,
  4770. .finish_reshape = raid5_finish_reshape,
  4771. .quiesce = raid5_quiesce,
  4772. .takeover = raid5_takeover,
  4773. };
  4774. static struct mdk_personality raid4_personality =
  4775. {
  4776. .name = "raid4",
  4777. .level = 4,
  4778. .owner = THIS_MODULE,
  4779. .make_request = make_request,
  4780. .run = run,
  4781. .stop = stop,
  4782. .status = status,
  4783. .error_handler = error,
  4784. .hot_add_disk = raid5_add_disk,
  4785. .hot_remove_disk= raid5_remove_disk,
  4786. .spare_active = raid5_spare_active,
  4787. .sync_request = sync_request,
  4788. .resize = raid5_resize,
  4789. .size = raid5_size,
  4790. .check_reshape = raid5_check_reshape,
  4791. .start_reshape = raid5_start_reshape,
  4792. .finish_reshape = raid5_finish_reshape,
  4793. .quiesce = raid5_quiesce,
  4794. };
  4795. static int __init raid5_init(void)
  4796. {
  4797. register_md_personality(&raid6_personality);
  4798. register_md_personality(&raid5_personality);
  4799. register_md_personality(&raid4_personality);
  4800. return 0;
  4801. }
  4802. static void raid5_exit(void)
  4803. {
  4804. unregister_md_personality(&raid6_personality);
  4805. unregister_md_personality(&raid5_personality);
  4806. unregister_md_personality(&raid4_personality);
  4807. }
  4808. module_init(raid5_init);
  4809. module_exit(raid5_exit);
  4810. MODULE_LICENSE("GPL");
  4811. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  4812. MODULE_ALIAS("md-raid5");
  4813. MODULE_ALIAS("md-raid4");
  4814. MODULE_ALIAS("md-level-5");
  4815. MODULE_ALIAS("md-level-4");
  4816. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  4817. MODULE_ALIAS("md-raid6");
  4818. MODULE_ALIAS("md-level-6");
  4819. /* This used to be two separate modules, they were: */
  4820. MODULE_ALIAS("raid5");
  4821. MODULE_ALIAS("raid6");