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