raid5.c 168 KB

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