raid5.c 171 KB

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