raid5.c 167 KB

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