raid5.c 168 KB

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