raid5.c 168 KB

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