raid5.c 168 KB

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