raid5.c 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038
  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. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1576. set_bit(MD_RECOVERY_NEEDED,
  1577. &rdev->mddev->recovery);
  1578. } else if (is_badblock(rdev, sh->sector,
  1579. STRIPE_SECTORS,
  1580. &first_bad, &bad_sectors))
  1581. set_bit(R5_MadeGood, &sh->dev[i].flags);
  1582. }
  1583. rdev_dec_pending(rdev, conf->mddev);
  1584. if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
  1585. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1586. set_bit(STRIPE_HANDLE, &sh->state);
  1587. release_stripe(sh);
  1588. }
  1589. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
  1590. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  1591. {
  1592. struct r5dev *dev = &sh->dev[i];
  1593. bio_init(&dev->req);
  1594. dev->req.bi_io_vec = &dev->vec;
  1595. dev->req.bi_vcnt++;
  1596. dev->req.bi_max_vecs++;
  1597. dev->req.bi_private = sh;
  1598. dev->vec.bv_page = dev->page;
  1599. bio_init(&dev->rreq);
  1600. dev->rreq.bi_io_vec = &dev->rvec;
  1601. dev->rreq.bi_vcnt++;
  1602. dev->rreq.bi_max_vecs++;
  1603. dev->rreq.bi_private = sh;
  1604. dev->rvec.bv_page = dev->page;
  1605. dev->flags = 0;
  1606. dev->sector = compute_blocknr(sh, i, previous);
  1607. }
  1608. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1609. {
  1610. char b[BDEVNAME_SIZE];
  1611. struct r5conf *conf = mddev->private;
  1612. unsigned long flags;
  1613. pr_debug("raid456: error called\n");
  1614. spin_lock_irqsave(&conf->device_lock, flags);
  1615. clear_bit(In_sync, &rdev->flags);
  1616. mddev->degraded = calc_degraded(conf);
  1617. spin_unlock_irqrestore(&conf->device_lock, flags);
  1618. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1619. set_bit(Blocked, &rdev->flags);
  1620. set_bit(Faulty, &rdev->flags);
  1621. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1622. printk(KERN_ALERT
  1623. "md/raid:%s: Disk failure on %s, disabling device.\n"
  1624. "md/raid:%s: Operation continuing on %d devices.\n",
  1625. mdname(mddev),
  1626. bdevname(rdev->bdev, b),
  1627. mdname(mddev),
  1628. conf->raid_disks - mddev->degraded);
  1629. }
  1630. /*
  1631. * Input: a 'big' sector number,
  1632. * Output: index of the data and parity disk, and the sector # in them.
  1633. */
  1634. static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
  1635. int previous, int *dd_idx,
  1636. struct stripe_head *sh)
  1637. {
  1638. sector_t stripe, stripe2;
  1639. sector_t chunk_number;
  1640. unsigned int chunk_offset;
  1641. int pd_idx, qd_idx;
  1642. int ddf_layout = 0;
  1643. sector_t new_sector;
  1644. int algorithm = previous ? conf->prev_algo
  1645. : conf->algorithm;
  1646. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1647. : conf->chunk_sectors;
  1648. int raid_disks = previous ? conf->previous_raid_disks
  1649. : conf->raid_disks;
  1650. int data_disks = raid_disks - conf->max_degraded;
  1651. /* First compute the information on this sector */
  1652. /*
  1653. * Compute the chunk number and the sector offset inside the chunk
  1654. */
  1655. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1656. chunk_number = r_sector;
  1657. /*
  1658. * Compute the stripe number
  1659. */
  1660. stripe = chunk_number;
  1661. *dd_idx = sector_div(stripe, data_disks);
  1662. stripe2 = stripe;
  1663. /*
  1664. * Select the parity disk based on the user selected algorithm.
  1665. */
  1666. pd_idx = qd_idx = -1;
  1667. switch(conf->level) {
  1668. case 4:
  1669. pd_idx = data_disks;
  1670. break;
  1671. case 5:
  1672. switch (algorithm) {
  1673. case ALGORITHM_LEFT_ASYMMETRIC:
  1674. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1675. if (*dd_idx >= pd_idx)
  1676. (*dd_idx)++;
  1677. break;
  1678. case ALGORITHM_RIGHT_ASYMMETRIC:
  1679. pd_idx = sector_div(stripe2, raid_disks);
  1680. if (*dd_idx >= pd_idx)
  1681. (*dd_idx)++;
  1682. break;
  1683. case ALGORITHM_LEFT_SYMMETRIC:
  1684. pd_idx = data_disks - sector_div(stripe2, raid_disks);
  1685. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1686. break;
  1687. case ALGORITHM_RIGHT_SYMMETRIC:
  1688. pd_idx = sector_div(stripe2, raid_disks);
  1689. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1690. break;
  1691. case ALGORITHM_PARITY_0:
  1692. pd_idx = 0;
  1693. (*dd_idx)++;
  1694. break;
  1695. case ALGORITHM_PARITY_N:
  1696. pd_idx = data_disks;
  1697. break;
  1698. default:
  1699. BUG();
  1700. }
  1701. break;
  1702. case 6:
  1703. switch (algorithm) {
  1704. case ALGORITHM_LEFT_ASYMMETRIC:
  1705. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1706. qd_idx = pd_idx + 1;
  1707. if (pd_idx == raid_disks-1) {
  1708. (*dd_idx)++; /* Q D D D P */
  1709. qd_idx = 0;
  1710. } else if (*dd_idx >= pd_idx)
  1711. (*dd_idx) += 2; /* D D P Q D */
  1712. break;
  1713. case ALGORITHM_RIGHT_ASYMMETRIC:
  1714. pd_idx = sector_div(stripe2, raid_disks);
  1715. qd_idx = pd_idx + 1;
  1716. if (pd_idx == raid_disks-1) {
  1717. (*dd_idx)++; /* Q D D D P */
  1718. qd_idx = 0;
  1719. } else if (*dd_idx >= pd_idx)
  1720. (*dd_idx) += 2; /* D D P Q D */
  1721. break;
  1722. case ALGORITHM_LEFT_SYMMETRIC:
  1723. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1724. qd_idx = (pd_idx + 1) % raid_disks;
  1725. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1726. break;
  1727. case ALGORITHM_RIGHT_SYMMETRIC:
  1728. pd_idx = sector_div(stripe2, raid_disks);
  1729. qd_idx = (pd_idx + 1) % raid_disks;
  1730. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1731. break;
  1732. case ALGORITHM_PARITY_0:
  1733. pd_idx = 0;
  1734. qd_idx = 1;
  1735. (*dd_idx) += 2;
  1736. break;
  1737. case ALGORITHM_PARITY_N:
  1738. pd_idx = data_disks;
  1739. qd_idx = data_disks + 1;
  1740. break;
  1741. case ALGORITHM_ROTATING_ZERO_RESTART:
  1742. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1743. * of blocks for computing Q is different.
  1744. */
  1745. pd_idx = sector_div(stripe2, raid_disks);
  1746. qd_idx = pd_idx + 1;
  1747. if (pd_idx == raid_disks-1) {
  1748. (*dd_idx)++; /* Q D D D P */
  1749. qd_idx = 0;
  1750. } else if (*dd_idx >= pd_idx)
  1751. (*dd_idx) += 2; /* D D P Q D */
  1752. ddf_layout = 1;
  1753. break;
  1754. case ALGORITHM_ROTATING_N_RESTART:
  1755. /* Same a left_asymmetric, by first stripe is
  1756. * D D D P Q rather than
  1757. * Q D D D P
  1758. */
  1759. stripe2 += 1;
  1760. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1761. qd_idx = pd_idx + 1;
  1762. if (pd_idx == raid_disks-1) {
  1763. (*dd_idx)++; /* Q D D D P */
  1764. qd_idx = 0;
  1765. } else if (*dd_idx >= pd_idx)
  1766. (*dd_idx) += 2; /* D D P Q D */
  1767. ddf_layout = 1;
  1768. break;
  1769. case ALGORITHM_ROTATING_N_CONTINUE:
  1770. /* Same as left_symmetric but Q is before P */
  1771. pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
  1772. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1773. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1774. ddf_layout = 1;
  1775. break;
  1776. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1777. /* RAID5 left_asymmetric, with Q on last device */
  1778. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1779. if (*dd_idx >= pd_idx)
  1780. (*dd_idx)++;
  1781. qd_idx = raid_disks - 1;
  1782. break;
  1783. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1784. pd_idx = sector_div(stripe2, raid_disks-1);
  1785. if (*dd_idx >= pd_idx)
  1786. (*dd_idx)++;
  1787. qd_idx = raid_disks - 1;
  1788. break;
  1789. case ALGORITHM_LEFT_SYMMETRIC_6:
  1790. pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
  1791. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1792. qd_idx = raid_disks - 1;
  1793. break;
  1794. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1795. pd_idx = sector_div(stripe2, raid_disks-1);
  1796. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1797. qd_idx = raid_disks - 1;
  1798. break;
  1799. case ALGORITHM_PARITY_0_6:
  1800. pd_idx = 0;
  1801. (*dd_idx)++;
  1802. qd_idx = raid_disks - 1;
  1803. break;
  1804. default:
  1805. BUG();
  1806. }
  1807. break;
  1808. }
  1809. if (sh) {
  1810. sh->pd_idx = pd_idx;
  1811. sh->qd_idx = qd_idx;
  1812. sh->ddf_layout = ddf_layout;
  1813. }
  1814. /*
  1815. * Finally, compute the new sector number
  1816. */
  1817. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1818. return new_sector;
  1819. }
  1820. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
  1821. {
  1822. struct r5conf *conf = sh->raid_conf;
  1823. int raid_disks = sh->disks;
  1824. int data_disks = raid_disks - conf->max_degraded;
  1825. sector_t new_sector = sh->sector, check;
  1826. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1827. : conf->chunk_sectors;
  1828. int algorithm = previous ? conf->prev_algo
  1829. : conf->algorithm;
  1830. sector_t stripe;
  1831. int chunk_offset;
  1832. sector_t chunk_number;
  1833. int dummy1, dd_idx = i;
  1834. sector_t r_sector;
  1835. struct stripe_head sh2;
  1836. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1837. stripe = new_sector;
  1838. if (i == sh->pd_idx)
  1839. return 0;
  1840. switch(conf->level) {
  1841. case 4: break;
  1842. case 5:
  1843. switch (algorithm) {
  1844. case ALGORITHM_LEFT_ASYMMETRIC:
  1845. case ALGORITHM_RIGHT_ASYMMETRIC:
  1846. if (i > sh->pd_idx)
  1847. i--;
  1848. break;
  1849. case ALGORITHM_LEFT_SYMMETRIC:
  1850. case ALGORITHM_RIGHT_SYMMETRIC:
  1851. if (i < sh->pd_idx)
  1852. i += raid_disks;
  1853. i -= (sh->pd_idx + 1);
  1854. break;
  1855. case ALGORITHM_PARITY_0:
  1856. i -= 1;
  1857. break;
  1858. case ALGORITHM_PARITY_N:
  1859. break;
  1860. default:
  1861. BUG();
  1862. }
  1863. break;
  1864. case 6:
  1865. if (i == sh->qd_idx)
  1866. return 0; /* It is the Q disk */
  1867. switch (algorithm) {
  1868. case ALGORITHM_LEFT_ASYMMETRIC:
  1869. case ALGORITHM_RIGHT_ASYMMETRIC:
  1870. case ALGORITHM_ROTATING_ZERO_RESTART:
  1871. case ALGORITHM_ROTATING_N_RESTART:
  1872. if (sh->pd_idx == raid_disks-1)
  1873. i--; /* Q D D D P */
  1874. else if (i > sh->pd_idx)
  1875. i -= 2; /* D D P Q D */
  1876. break;
  1877. case ALGORITHM_LEFT_SYMMETRIC:
  1878. case ALGORITHM_RIGHT_SYMMETRIC:
  1879. if (sh->pd_idx == raid_disks-1)
  1880. i--; /* Q D D D P */
  1881. else {
  1882. /* D D P Q D */
  1883. if (i < sh->pd_idx)
  1884. i += raid_disks;
  1885. i -= (sh->pd_idx + 2);
  1886. }
  1887. break;
  1888. case ALGORITHM_PARITY_0:
  1889. i -= 2;
  1890. break;
  1891. case ALGORITHM_PARITY_N:
  1892. break;
  1893. case ALGORITHM_ROTATING_N_CONTINUE:
  1894. /* Like left_symmetric, but P is before Q */
  1895. if (sh->pd_idx == 0)
  1896. i--; /* P D D D Q */
  1897. else {
  1898. /* D D Q P D */
  1899. if (i < sh->pd_idx)
  1900. i += raid_disks;
  1901. i -= (sh->pd_idx + 1);
  1902. }
  1903. break;
  1904. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1905. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1906. if (i > sh->pd_idx)
  1907. i--;
  1908. break;
  1909. case ALGORITHM_LEFT_SYMMETRIC_6:
  1910. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1911. if (i < sh->pd_idx)
  1912. i += data_disks + 1;
  1913. i -= (sh->pd_idx + 1);
  1914. break;
  1915. case ALGORITHM_PARITY_0_6:
  1916. i -= 1;
  1917. break;
  1918. default:
  1919. BUG();
  1920. }
  1921. break;
  1922. }
  1923. chunk_number = stripe * data_disks + i;
  1924. r_sector = chunk_number * sectors_per_chunk + chunk_offset;
  1925. check = raid5_compute_sector(conf, r_sector,
  1926. previous, &dummy1, &sh2);
  1927. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  1928. || sh2.qd_idx != sh->qd_idx) {
  1929. printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
  1930. mdname(conf->mddev));
  1931. return 0;
  1932. }
  1933. return r_sector;
  1934. }
  1935. static void
  1936. schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
  1937. int rcw, int expand)
  1938. {
  1939. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1940. struct r5conf *conf = sh->raid_conf;
  1941. int level = conf->level;
  1942. if (rcw) {
  1943. /* if we are not expanding this is a proper write request, and
  1944. * there will be bios with new data to be drained into the
  1945. * stripe cache
  1946. */
  1947. if (!expand) {
  1948. sh->reconstruct_state = reconstruct_state_drain_run;
  1949. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1950. } else
  1951. sh->reconstruct_state = reconstruct_state_run;
  1952. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1953. for (i = disks; i--; ) {
  1954. struct r5dev *dev = &sh->dev[i];
  1955. if (dev->towrite) {
  1956. set_bit(R5_LOCKED, &dev->flags);
  1957. set_bit(R5_Wantdrain, &dev->flags);
  1958. if (!expand)
  1959. clear_bit(R5_UPTODATE, &dev->flags);
  1960. s->locked++;
  1961. }
  1962. }
  1963. if (s->locked + conf->max_degraded == disks)
  1964. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1965. atomic_inc(&conf->pending_full_writes);
  1966. } else {
  1967. BUG_ON(level == 6);
  1968. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1969. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1970. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1971. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1972. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1973. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1974. for (i = disks; i--; ) {
  1975. struct r5dev *dev = &sh->dev[i];
  1976. if (i == pd_idx)
  1977. continue;
  1978. if (dev->towrite &&
  1979. (test_bit(R5_UPTODATE, &dev->flags) ||
  1980. test_bit(R5_Wantcompute, &dev->flags))) {
  1981. set_bit(R5_Wantdrain, &dev->flags);
  1982. set_bit(R5_LOCKED, &dev->flags);
  1983. clear_bit(R5_UPTODATE, &dev->flags);
  1984. s->locked++;
  1985. }
  1986. }
  1987. }
  1988. /* keep the parity disk(s) locked while asynchronous operations
  1989. * are in flight
  1990. */
  1991. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1992. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1993. s->locked++;
  1994. if (level == 6) {
  1995. int qd_idx = sh->qd_idx;
  1996. struct r5dev *dev = &sh->dev[qd_idx];
  1997. set_bit(R5_LOCKED, &dev->flags);
  1998. clear_bit(R5_UPTODATE, &dev->flags);
  1999. s->locked++;
  2000. }
  2001. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  2002. __func__, (unsigned long long)sh->sector,
  2003. s->locked, s->ops_request);
  2004. }
  2005. /*
  2006. * Each stripe/dev can have one or more bion attached.
  2007. * toread/towrite point to the first in a chain.
  2008. * The bi_next chain must be in order.
  2009. */
  2010. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  2011. {
  2012. struct bio **bip;
  2013. struct r5conf *conf = sh->raid_conf;
  2014. int firstwrite=0;
  2015. pr_debug("adding bi b#%llu to stripe s#%llu\n",
  2016. (unsigned long long)bi->bi_sector,
  2017. (unsigned long long)sh->sector);
  2018. spin_lock_irq(&conf->device_lock);
  2019. if (forwrite) {
  2020. bip = &sh->dev[dd_idx].towrite;
  2021. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  2022. firstwrite = 1;
  2023. } else
  2024. bip = &sh->dev[dd_idx].toread;
  2025. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  2026. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  2027. goto overlap;
  2028. bip = & (*bip)->bi_next;
  2029. }
  2030. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  2031. goto overlap;
  2032. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  2033. if (*bip)
  2034. bi->bi_next = *bip;
  2035. *bip = bi;
  2036. bi->bi_phys_segments++;
  2037. if (forwrite) {
  2038. /* check if page is covered */
  2039. sector_t sector = sh->dev[dd_idx].sector;
  2040. for (bi=sh->dev[dd_idx].towrite;
  2041. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  2042. bi && bi->bi_sector <= sector;
  2043. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  2044. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  2045. sector = bi->bi_sector + (bi->bi_size>>9);
  2046. }
  2047. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  2048. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  2049. }
  2050. spin_unlock_irq(&conf->device_lock);
  2051. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  2052. (unsigned long long)(*bip)->bi_sector,
  2053. (unsigned long long)sh->sector, dd_idx);
  2054. if (conf->mddev->bitmap && firstwrite) {
  2055. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  2056. STRIPE_SECTORS, 0);
  2057. sh->bm_seq = conf->seq_flush+1;
  2058. set_bit(STRIPE_BIT_DELAY, &sh->state);
  2059. }
  2060. return 1;
  2061. overlap:
  2062. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  2063. spin_unlock_irq(&conf->device_lock);
  2064. return 0;
  2065. }
  2066. static void end_reshape(struct r5conf *conf);
  2067. static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
  2068. struct stripe_head *sh)
  2069. {
  2070. int sectors_per_chunk =
  2071. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  2072. int dd_idx;
  2073. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  2074. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  2075. raid5_compute_sector(conf,
  2076. stripe * (disks - conf->max_degraded)
  2077. *sectors_per_chunk + chunk_offset,
  2078. previous,
  2079. &dd_idx, sh);
  2080. }
  2081. static void
  2082. handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
  2083. struct stripe_head_state *s, int disks,
  2084. struct bio **return_bi)
  2085. {
  2086. int i;
  2087. for (i = disks; i--; ) {
  2088. struct bio *bi;
  2089. int bitmap_end = 0;
  2090. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  2091. struct md_rdev *rdev;
  2092. rcu_read_lock();
  2093. rdev = rcu_dereference(conf->disks[i].rdev);
  2094. if (rdev && test_bit(In_sync, &rdev->flags))
  2095. atomic_inc(&rdev->nr_pending);
  2096. else
  2097. rdev = NULL;
  2098. rcu_read_unlock();
  2099. if (rdev) {
  2100. if (!rdev_set_badblocks(
  2101. rdev,
  2102. sh->sector,
  2103. STRIPE_SECTORS, 0))
  2104. md_error(conf->mddev, rdev);
  2105. rdev_dec_pending(rdev, conf->mddev);
  2106. }
  2107. }
  2108. spin_lock_irq(&conf->device_lock);
  2109. /* fail all writes first */
  2110. bi = sh->dev[i].towrite;
  2111. sh->dev[i].towrite = NULL;
  2112. if (bi) {
  2113. s->to_write--;
  2114. bitmap_end = 1;
  2115. }
  2116. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2117. wake_up(&conf->wait_for_overlap);
  2118. while (bi && bi->bi_sector <
  2119. sh->dev[i].sector + STRIPE_SECTORS) {
  2120. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  2121. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2122. if (!raid5_dec_bi_phys_segments(bi)) {
  2123. md_write_end(conf->mddev);
  2124. bi->bi_next = *return_bi;
  2125. *return_bi = bi;
  2126. }
  2127. bi = nextbi;
  2128. }
  2129. /* and fail all 'written' */
  2130. bi = sh->dev[i].written;
  2131. sh->dev[i].written = NULL;
  2132. if (bi) bitmap_end = 1;
  2133. while (bi && bi->bi_sector <
  2134. sh->dev[i].sector + STRIPE_SECTORS) {
  2135. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  2136. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2137. if (!raid5_dec_bi_phys_segments(bi)) {
  2138. md_write_end(conf->mddev);
  2139. bi->bi_next = *return_bi;
  2140. *return_bi = bi;
  2141. }
  2142. bi = bi2;
  2143. }
  2144. /* fail any reads if this device is non-operational and
  2145. * the data has not reached the cache yet.
  2146. */
  2147. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  2148. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  2149. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  2150. bi = sh->dev[i].toread;
  2151. sh->dev[i].toread = NULL;
  2152. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2153. wake_up(&conf->wait_for_overlap);
  2154. if (bi) s->to_read--;
  2155. while (bi && bi->bi_sector <
  2156. sh->dev[i].sector + STRIPE_SECTORS) {
  2157. struct bio *nextbi =
  2158. r5_next_bio(bi, sh->dev[i].sector);
  2159. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2160. if (!raid5_dec_bi_phys_segments(bi)) {
  2161. bi->bi_next = *return_bi;
  2162. *return_bi = bi;
  2163. }
  2164. bi = nextbi;
  2165. }
  2166. }
  2167. spin_unlock_irq(&conf->device_lock);
  2168. if (bitmap_end)
  2169. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  2170. STRIPE_SECTORS, 0, 0);
  2171. /* If we were in the middle of a write the parity block might
  2172. * still be locked - so just clear all R5_LOCKED flags
  2173. */
  2174. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2175. }
  2176. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2177. if (atomic_dec_and_test(&conf->pending_full_writes))
  2178. md_wakeup_thread(conf->mddev->thread);
  2179. }
  2180. static void
  2181. handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
  2182. struct stripe_head_state *s)
  2183. {
  2184. int abort = 0;
  2185. int i;
  2186. md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
  2187. clear_bit(STRIPE_SYNCING, &sh->state);
  2188. s->syncing = 0;
  2189. s->replacing = 0;
  2190. /* There is nothing more to do for sync/check/repair.
  2191. * For recover/replace we need to record a bad block on all
  2192. * non-sync devices, or abort the recovery
  2193. */
  2194. if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
  2195. return;
  2196. /* During recovery devices cannot be removed, so locking and
  2197. * refcounting of rdevs is not needed
  2198. */
  2199. for (i = 0; i < conf->raid_disks; i++) {
  2200. struct md_rdev *rdev = conf->disks[i].rdev;
  2201. if (rdev
  2202. && !test_bit(Faulty, &rdev->flags)
  2203. && !test_bit(In_sync, &rdev->flags)
  2204. && !rdev_set_badblocks(rdev, sh->sector,
  2205. STRIPE_SECTORS, 0))
  2206. abort = 1;
  2207. rdev = conf->disks[i].replacement;
  2208. if (rdev
  2209. && !test_bit(Faulty, &rdev->flags)
  2210. && !test_bit(In_sync, &rdev->flags)
  2211. && !rdev_set_badblocks(rdev, sh->sector,
  2212. STRIPE_SECTORS, 0))
  2213. abort = 1;
  2214. }
  2215. if (abort) {
  2216. conf->recovery_disabled = conf->mddev->recovery_disabled;
  2217. set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
  2218. }
  2219. }
  2220. static int want_replace(struct stripe_head *sh, int disk_idx)
  2221. {
  2222. struct md_rdev *rdev;
  2223. int rv = 0;
  2224. /* Doing recovery so rcu locking not required */
  2225. rdev = sh->raid_conf->disks[disk_idx].replacement;
  2226. if (rdev
  2227. && !test_bit(Faulty, &rdev->flags)
  2228. && !test_bit(In_sync, &rdev->flags)
  2229. && (rdev->recovery_offset <= sh->sector
  2230. || rdev->mddev->recovery_cp <= sh->sector))
  2231. rv = 1;
  2232. return rv;
  2233. }
  2234. /* fetch_block - checks the given member device to see if its data needs
  2235. * to be read or computed to satisfy a request.
  2236. *
  2237. * Returns 1 when no more member devices need to be checked, otherwise returns
  2238. * 0 to tell the loop in handle_stripe_fill to continue
  2239. */
  2240. static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
  2241. int disk_idx, int disks)
  2242. {
  2243. struct r5dev *dev = &sh->dev[disk_idx];
  2244. struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
  2245. &sh->dev[s->failed_num[1]] };
  2246. /* is the data in this block needed, and can we get it? */
  2247. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2248. !test_bit(R5_UPTODATE, &dev->flags) &&
  2249. (dev->toread ||
  2250. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2251. s->syncing || s->expanding ||
  2252. (s->replacing && want_replace(sh, disk_idx)) ||
  2253. (s->failed >= 1 && fdev[0]->toread) ||
  2254. (s->failed >= 2 && fdev[1]->toread) ||
  2255. (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
  2256. !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
  2257. (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
  2258. /* we would like to get this block, possibly by computing it,
  2259. * otherwise read it if the backing disk is insync
  2260. */
  2261. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  2262. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  2263. if ((s->uptodate == disks - 1) &&
  2264. (s->failed && (disk_idx == s->failed_num[0] ||
  2265. disk_idx == s->failed_num[1]))) {
  2266. /* have disk failed, and we're requested to fetch it;
  2267. * do compute it
  2268. */
  2269. pr_debug("Computing stripe %llu block %d\n",
  2270. (unsigned long long)sh->sector, disk_idx);
  2271. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2272. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2273. set_bit(R5_Wantcompute, &dev->flags);
  2274. sh->ops.target = disk_idx;
  2275. sh->ops.target2 = -1; /* no 2nd target */
  2276. s->req_compute = 1;
  2277. /* Careful: from this point on 'uptodate' is in the eye
  2278. * of raid_run_ops which services 'compute' operations
  2279. * before writes. R5_Wantcompute flags a block that will
  2280. * be R5_UPTODATE by the time it is needed for a
  2281. * subsequent operation.
  2282. */
  2283. s->uptodate++;
  2284. return 1;
  2285. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  2286. /* Computing 2-failure is *very* expensive; only
  2287. * do it if failed >= 2
  2288. */
  2289. int other;
  2290. for (other = disks; other--; ) {
  2291. if (other == disk_idx)
  2292. continue;
  2293. if (!test_bit(R5_UPTODATE,
  2294. &sh->dev[other].flags))
  2295. break;
  2296. }
  2297. BUG_ON(other < 0);
  2298. pr_debug("Computing stripe %llu blocks %d,%d\n",
  2299. (unsigned long long)sh->sector,
  2300. disk_idx, other);
  2301. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2302. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2303. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  2304. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  2305. sh->ops.target = disk_idx;
  2306. sh->ops.target2 = other;
  2307. s->uptodate += 2;
  2308. s->req_compute = 1;
  2309. return 1;
  2310. } else if (test_bit(R5_Insync, &dev->flags)) {
  2311. set_bit(R5_LOCKED, &dev->flags);
  2312. set_bit(R5_Wantread, &dev->flags);
  2313. s->locked++;
  2314. pr_debug("Reading block %d (sync=%d)\n",
  2315. disk_idx, s->syncing);
  2316. }
  2317. }
  2318. return 0;
  2319. }
  2320. /**
  2321. * handle_stripe_fill - read or compute data to satisfy pending requests.
  2322. */
  2323. static void handle_stripe_fill(struct stripe_head *sh,
  2324. struct stripe_head_state *s,
  2325. int disks)
  2326. {
  2327. int i;
  2328. /* look for blocks to read/compute, skip this if a compute
  2329. * is already in flight, or if the stripe contents are in the
  2330. * midst of changing due to a write
  2331. */
  2332. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2333. !sh->reconstruct_state)
  2334. for (i = disks; i--; )
  2335. if (fetch_block(sh, s, i, disks))
  2336. break;
  2337. set_bit(STRIPE_HANDLE, &sh->state);
  2338. }
  2339. /* handle_stripe_clean_event
  2340. * any written block on an uptodate or failed drive can be returned.
  2341. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  2342. * never LOCKED, so we don't need to test 'failed' directly.
  2343. */
  2344. static void handle_stripe_clean_event(struct r5conf *conf,
  2345. struct stripe_head *sh, int disks, struct bio **return_bi)
  2346. {
  2347. int i;
  2348. struct r5dev *dev;
  2349. for (i = disks; i--; )
  2350. if (sh->dev[i].written) {
  2351. dev = &sh->dev[i];
  2352. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2353. test_bit(R5_UPTODATE, &dev->flags)) {
  2354. /* We can return any write requests */
  2355. struct bio *wbi, *wbi2;
  2356. int bitmap_end = 0;
  2357. pr_debug("Return write for disc %d\n", i);
  2358. spin_lock_irq(&conf->device_lock);
  2359. wbi = dev->written;
  2360. dev->written = NULL;
  2361. while (wbi && wbi->bi_sector <
  2362. dev->sector + STRIPE_SECTORS) {
  2363. wbi2 = r5_next_bio(wbi, dev->sector);
  2364. if (!raid5_dec_bi_phys_segments(wbi)) {
  2365. md_write_end(conf->mddev);
  2366. wbi->bi_next = *return_bi;
  2367. *return_bi = wbi;
  2368. }
  2369. wbi = wbi2;
  2370. }
  2371. if (dev->towrite == NULL)
  2372. bitmap_end = 1;
  2373. spin_unlock_irq(&conf->device_lock);
  2374. if (bitmap_end)
  2375. bitmap_endwrite(conf->mddev->bitmap,
  2376. sh->sector,
  2377. STRIPE_SECTORS,
  2378. !test_bit(STRIPE_DEGRADED, &sh->state),
  2379. 0);
  2380. }
  2381. }
  2382. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2383. if (atomic_dec_and_test(&conf->pending_full_writes))
  2384. md_wakeup_thread(conf->mddev->thread);
  2385. }
  2386. static void handle_stripe_dirtying(struct r5conf *conf,
  2387. struct stripe_head *sh,
  2388. struct stripe_head_state *s,
  2389. int disks)
  2390. {
  2391. int rmw = 0, rcw = 0, i;
  2392. if (conf->max_degraded == 2) {
  2393. /* RAID6 requires 'rcw' in current implementation
  2394. * Calculate the real rcw later - for now fake it
  2395. * look like rcw is cheaper
  2396. */
  2397. rcw = 1; rmw = 2;
  2398. } else for (i = disks; i--; ) {
  2399. /* would I have to read this buffer for read_modify_write */
  2400. struct r5dev *dev = &sh->dev[i];
  2401. if ((dev->towrite || i == sh->pd_idx) &&
  2402. !test_bit(R5_LOCKED, &dev->flags) &&
  2403. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2404. test_bit(R5_Wantcompute, &dev->flags))) {
  2405. if (test_bit(R5_Insync, &dev->flags))
  2406. rmw++;
  2407. else
  2408. rmw += 2*disks; /* cannot read it */
  2409. }
  2410. /* Would I have to read this buffer for reconstruct_write */
  2411. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2412. !test_bit(R5_LOCKED, &dev->flags) &&
  2413. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2414. test_bit(R5_Wantcompute, &dev->flags))) {
  2415. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2416. else
  2417. rcw += 2*disks;
  2418. }
  2419. }
  2420. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2421. (unsigned long long)sh->sector, rmw, rcw);
  2422. set_bit(STRIPE_HANDLE, &sh->state);
  2423. if (rmw < rcw && rmw > 0)
  2424. /* prefer read-modify-write, but need to get some data */
  2425. for (i = disks; i--; ) {
  2426. struct r5dev *dev = &sh->dev[i];
  2427. if ((dev->towrite || i == sh->pd_idx) &&
  2428. !test_bit(R5_LOCKED, &dev->flags) &&
  2429. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2430. test_bit(R5_Wantcompute, &dev->flags)) &&
  2431. test_bit(R5_Insync, &dev->flags)) {
  2432. if (
  2433. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2434. pr_debug("Read_old block "
  2435. "%d for r-m-w\n", i);
  2436. set_bit(R5_LOCKED, &dev->flags);
  2437. set_bit(R5_Wantread, &dev->flags);
  2438. s->locked++;
  2439. } else {
  2440. set_bit(STRIPE_DELAYED, &sh->state);
  2441. set_bit(STRIPE_HANDLE, &sh->state);
  2442. }
  2443. }
  2444. }
  2445. if (rcw <= rmw && rcw > 0) {
  2446. /* want reconstruct write, but need to get some data */
  2447. rcw = 0;
  2448. for (i = disks; i--; ) {
  2449. struct r5dev *dev = &sh->dev[i];
  2450. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2451. i != sh->pd_idx && i != sh->qd_idx &&
  2452. !test_bit(R5_LOCKED, &dev->flags) &&
  2453. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2454. test_bit(R5_Wantcompute, &dev->flags))) {
  2455. rcw++;
  2456. if (!test_bit(R5_Insync, &dev->flags))
  2457. continue; /* it's a failed drive */
  2458. if (
  2459. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2460. pr_debug("Read_old block "
  2461. "%d for Reconstruct\n", i);
  2462. set_bit(R5_LOCKED, &dev->flags);
  2463. set_bit(R5_Wantread, &dev->flags);
  2464. s->locked++;
  2465. } else {
  2466. set_bit(STRIPE_DELAYED, &sh->state);
  2467. set_bit(STRIPE_HANDLE, &sh->state);
  2468. }
  2469. }
  2470. }
  2471. }
  2472. /* now if nothing is locked, and if we have enough data,
  2473. * we can start a write request
  2474. */
  2475. /* since handle_stripe can be called at any time we need to handle the
  2476. * case where a compute block operation has been submitted and then a
  2477. * subsequent call wants to start a write request. raid_run_ops only
  2478. * handles the case where compute block and reconstruct are requested
  2479. * simultaneously. If this is not the case then new writes need to be
  2480. * held off until the compute completes.
  2481. */
  2482. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2483. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2484. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2485. schedule_reconstruction(sh, s, rcw == 0, 0);
  2486. }
  2487. static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
  2488. struct stripe_head_state *s, int disks)
  2489. {
  2490. struct r5dev *dev = NULL;
  2491. set_bit(STRIPE_HANDLE, &sh->state);
  2492. switch (sh->check_state) {
  2493. case check_state_idle:
  2494. /* start a new check operation if there are no failures */
  2495. if (s->failed == 0) {
  2496. BUG_ON(s->uptodate != disks);
  2497. sh->check_state = check_state_run;
  2498. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2499. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2500. s->uptodate--;
  2501. break;
  2502. }
  2503. dev = &sh->dev[s->failed_num[0]];
  2504. /* fall through */
  2505. case check_state_compute_result:
  2506. sh->check_state = check_state_idle;
  2507. if (!dev)
  2508. dev = &sh->dev[sh->pd_idx];
  2509. /* check that a write has not made the stripe insync */
  2510. if (test_bit(STRIPE_INSYNC, &sh->state))
  2511. break;
  2512. /* either failed parity check, or recovery is happening */
  2513. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2514. BUG_ON(s->uptodate != disks);
  2515. set_bit(R5_LOCKED, &dev->flags);
  2516. s->locked++;
  2517. set_bit(R5_Wantwrite, &dev->flags);
  2518. clear_bit(STRIPE_DEGRADED, &sh->state);
  2519. set_bit(STRIPE_INSYNC, &sh->state);
  2520. break;
  2521. case check_state_run:
  2522. break; /* we will be called again upon completion */
  2523. case check_state_check_result:
  2524. sh->check_state = check_state_idle;
  2525. /* if a failure occurred during the check operation, leave
  2526. * STRIPE_INSYNC not set and let the stripe be handled again
  2527. */
  2528. if (s->failed)
  2529. break;
  2530. /* handle a successful check operation, if parity is correct
  2531. * we are done. Otherwise update the mismatch count and repair
  2532. * parity if !MD_RECOVERY_CHECK
  2533. */
  2534. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  2535. /* parity is correct (on disc,
  2536. * not in buffer any more)
  2537. */
  2538. set_bit(STRIPE_INSYNC, &sh->state);
  2539. else {
  2540. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2541. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2542. /* don't try to repair!! */
  2543. set_bit(STRIPE_INSYNC, &sh->state);
  2544. else {
  2545. sh->check_state = check_state_compute_run;
  2546. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2547. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2548. set_bit(R5_Wantcompute,
  2549. &sh->dev[sh->pd_idx].flags);
  2550. sh->ops.target = sh->pd_idx;
  2551. sh->ops.target2 = -1;
  2552. s->uptodate++;
  2553. }
  2554. }
  2555. break;
  2556. case check_state_compute_run:
  2557. break;
  2558. default:
  2559. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2560. __func__, sh->check_state,
  2561. (unsigned long long) sh->sector);
  2562. BUG();
  2563. }
  2564. }
  2565. static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
  2566. struct stripe_head_state *s,
  2567. int disks)
  2568. {
  2569. int pd_idx = sh->pd_idx;
  2570. int qd_idx = sh->qd_idx;
  2571. struct r5dev *dev;
  2572. set_bit(STRIPE_HANDLE, &sh->state);
  2573. BUG_ON(s->failed > 2);
  2574. /* Want to check and possibly repair P and Q.
  2575. * However there could be one 'failed' device, in which
  2576. * case we can only check one of them, possibly using the
  2577. * other to generate missing data
  2578. */
  2579. switch (sh->check_state) {
  2580. case check_state_idle:
  2581. /* start a new check operation if there are < 2 failures */
  2582. if (s->failed == s->q_failed) {
  2583. /* The only possible failed device holds Q, so it
  2584. * makes sense to check P (If anything else were failed,
  2585. * we would have used P to recreate it).
  2586. */
  2587. sh->check_state = check_state_run;
  2588. }
  2589. if (!s->q_failed && s->failed < 2) {
  2590. /* Q is not failed, and we didn't use it to generate
  2591. * anything, so it makes sense to check it
  2592. */
  2593. if (sh->check_state == check_state_run)
  2594. sh->check_state = check_state_run_pq;
  2595. else
  2596. sh->check_state = check_state_run_q;
  2597. }
  2598. /* discard potentially stale zero_sum_result */
  2599. sh->ops.zero_sum_result = 0;
  2600. if (sh->check_state == check_state_run) {
  2601. /* async_xor_zero_sum destroys the contents of P */
  2602. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2603. s->uptodate--;
  2604. }
  2605. if (sh->check_state >= check_state_run &&
  2606. sh->check_state <= check_state_run_pq) {
  2607. /* async_syndrome_zero_sum preserves P and Q, so
  2608. * no need to mark them !uptodate here
  2609. */
  2610. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2611. break;
  2612. }
  2613. /* we have 2-disk failure */
  2614. BUG_ON(s->failed != 2);
  2615. /* fall through */
  2616. case check_state_compute_result:
  2617. sh->check_state = check_state_idle;
  2618. /* check that a write has not made the stripe insync */
  2619. if (test_bit(STRIPE_INSYNC, &sh->state))
  2620. break;
  2621. /* now write out any block on a failed drive,
  2622. * or P or Q if they were recomputed
  2623. */
  2624. BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
  2625. if (s->failed == 2) {
  2626. dev = &sh->dev[s->failed_num[1]];
  2627. s->locked++;
  2628. set_bit(R5_LOCKED, &dev->flags);
  2629. set_bit(R5_Wantwrite, &dev->flags);
  2630. }
  2631. if (s->failed >= 1) {
  2632. dev = &sh->dev[s->failed_num[0]];
  2633. s->locked++;
  2634. set_bit(R5_LOCKED, &dev->flags);
  2635. set_bit(R5_Wantwrite, &dev->flags);
  2636. }
  2637. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2638. dev = &sh->dev[pd_idx];
  2639. s->locked++;
  2640. set_bit(R5_LOCKED, &dev->flags);
  2641. set_bit(R5_Wantwrite, &dev->flags);
  2642. }
  2643. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2644. dev = &sh->dev[qd_idx];
  2645. s->locked++;
  2646. set_bit(R5_LOCKED, &dev->flags);
  2647. set_bit(R5_Wantwrite, &dev->flags);
  2648. }
  2649. clear_bit(STRIPE_DEGRADED, &sh->state);
  2650. set_bit(STRIPE_INSYNC, &sh->state);
  2651. break;
  2652. case check_state_run:
  2653. case check_state_run_q:
  2654. case check_state_run_pq:
  2655. break; /* we will be called again upon completion */
  2656. case check_state_check_result:
  2657. sh->check_state = check_state_idle;
  2658. /* handle a successful check operation, if parity is correct
  2659. * we are done. Otherwise update the mismatch count and repair
  2660. * parity if !MD_RECOVERY_CHECK
  2661. */
  2662. if (sh->ops.zero_sum_result == 0) {
  2663. /* both parities are correct */
  2664. if (!s->failed)
  2665. set_bit(STRIPE_INSYNC, &sh->state);
  2666. else {
  2667. /* in contrast to the raid5 case we can validate
  2668. * parity, but still have a failure to write
  2669. * back
  2670. */
  2671. sh->check_state = check_state_compute_result;
  2672. /* Returning at this point means that we may go
  2673. * off and bring p and/or q uptodate again so
  2674. * we make sure to check zero_sum_result again
  2675. * to verify if p or q need writeback
  2676. */
  2677. }
  2678. } else {
  2679. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2680. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2681. /* don't try to repair!! */
  2682. set_bit(STRIPE_INSYNC, &sh->state);
  2683. else {
  2684. int *target = &sh->ops.target;
  2685. sh->ops.target = -1;
  2686. sh->ops.target2 = -1;
  2687. sh->check_state = check_state_compute_run;
  2688. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2689. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2690. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2691. set_bit(R5_Wantcompute,
  2692. &sh->dev[pd_idx].flags);
  2693. *target = pd_idx;
  2694. target = &sh->ops.target2;
  2695. s->uptodate++;
  2696. }
  2697. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2698. set_bit(R5_Wantcompute,
  2699. &sh->dev[qd_idx].flags);
  2700. *target = qd_idx;
  2701. s->uptodate++;
  2702. }
  2703. }
  2704. }
  2705. break;
  2706. case check_state_compute_run:
  2707. break;
  2708. default:
  2709. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2710. __func__, sh->check_state,
  2711. (unsigned long long) sh->sector);
  2712. BUG();
  2713. }
  2714. }
  2715. static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
  2716. {
  2717. int i;
  2718. /* We have read all the blocks in this stripe and now we need to
  2719. * copy some of them into a target stripe for expand.
  2720. */
  2721. struct dma_async_tx_descriptor *tx = NULL;
  2722. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2723. for (i = 0; i < sh->disks; i++)
  2724. if (i != sh->pd_idx && i != sh->qd_idx) {
  2725. int dd_idx, j;
  2726. struct stripe_head *sh2;
  2727. struct async_submit_ctl submit;
  2728. sector_t bn = compute_blocknr(sh, i, 1);
  2729. sector_t s = raid5_compute_sector(conf, bn, 0,
  2730. &dd_idx, NULL);
  2731. sh2 = get_active_stripe(conf, s, 0, 1, 1);
  2732. if (sh2 == NULL)
  2733. /* so far only the early blocks of this stripe
  2734. * have been requested. When later blocks
  2735. * get requested, we will try again
  2736. */
  2737. continue;
  2738. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2739. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2740. /* must have already done this block */
  2741. release_stripe(sh2);
  2742. continue;
  2743. }
  2744. /* place all the copies on one channel */
  2745. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  2746. tx = async_memcpy(sh2->dev[dd_idx].page,
  2747. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2748. &submit);
  2749. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2750. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2751. for (j = 0; j < conf->raid_disks; j++)
  2752. if (j != sh2->pd_idx &&
  2753. j != sh2->qd_idx &&
  2754. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2755. break;
  2756. if (j == conf->raid_disks) {
  2757. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2758. set_bit(STRIPE_HANDLE, &sh2->state);
  2759. }
  2760. release_stripe(sh2);
  2761. }
  2762. /* done submitting copies, wait for them to complete */
  2763. if (tx) {
  2764. async_tx_ack(tx);
  2765. dma_wait_for_async_tx(tx);
  2766. }
  2767. }
  2768. /*
  2769. * handle_stripe - do things to a stripe.
  2770. *
  2771. * We lock the stripe by setting STRIPE_ACTIVE and then examine the
  2772. * state of various bits to see what needs to be done.
  2773. * Possible results:
  2774. * return some read requests which now have data
  2775. * return some write requests which are safely on storage
  2776. * schedule a read on some buffers
  2777. * schedule a write of some buffers
  2778. * return confirmation of parity correctness
  2779. *
  2780. */
  2781. static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
  2782. {
  2783. struct r5conf *conf = sh->raid_conf;
  2784. int disks = sh->disks;
  2785. struct r5dev *dev;
  2786. int i;
  2787. int do_recovery = 0;
  2788. memset(s, 0, sizeof(*s));
  2789. s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2790. s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2791. s->failed_num[0] = -1;
  2792. s->failed_num[1] = -1;
  2793. /* Now to look around and see what can be done */
  2794. rcu_read_lock();
  2795. spin_lock_irq(&conf->device_lock);
  2796. for (i=disks; i--; ) {
  2797. struct md_rdev *rdev;
  2798. sector_t first_bad;
  2799. int bad_sectors;
  2800. int is_bad = 0;
  2801. dev = &sh->dev[i];
  2802. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2803. i, dev->flags,
  2804. dev->toread, dev->towrite, dev->written);
  2805. /* maybe we can reply to a read
  2806. *
  2807. * new wantfill requests are only permitted while
  2808. * ops_complete_biofill is guaranteed to be inactive
  2809. */
  2810. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2811. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2812. set_bit(R5_Wantfill, &dev->flags);
  2813. /* now count some things */
  2814. if (test_bit(R5_LOCKED, &dev->flags))
  2815. s->locked++;
  2816. if (test_bit(R5_UPTODATE, &dev->flags))
  2817. s->uptodate++;
  2818. if (test_bit(R5_Wantcompute, &dev->flags)) {
  2819. s->compute++;
  2820. BUG_ON(s->compute > 2);
  2821. }
  2822. if (test_bit(R5_Wantfill, &dev->flags))
  2823. s->to_fill++;
  2824. else if (dev->toread)
  2825. s->to_read++;
  2826. if (dev->towrite) {
  2827. s->to_write++;
  2828. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2829. s->non_overwrite++;
  2830. }
  2831. if (dev->written)
  2832. s->written++;
  2833. /* Prefer to use the replacement for reads, but only
  2834. * if it is recovered enough and has no bad blocks.
  2835. */
  2836. rdev = rcu_dereference(conf->disks[i].replacement);
  2837. if (rdev && !test_bit(Faulty, &rdev->flags) &&
  2838. rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
  2839. !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  2840. &first_bad, &bad_sectors))
  2841. set_bit(R5_ReadRepl, &dev->flags);
  2842. else {
  2843. if (rdev)
  2844. set_bit(R5_NeedReplace, &dev->flags);
  2845. rdev = rcu_dereference(conf->disks[i].rdev);
  2846. clear_bit(R5_ReadRepl, &dev->flags);
  2847. }
  2848. if (rdev && test_bit(Faulty, &rdev->flags))
  2849. rdev = NULL;
  2850. if (rdev) {
  2851. is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
  2852. &first_bad, &bad_sectors);
  2853. if (s->blocked_rdev == NULL
  2854. && (test_bit(Blocked, &rdev->flags)
  2855. || is_bad < 0)) {
  2856. if (is_bad < 0)
  2857. set_bit(BlockedBadBlocks,
  2858. &rdev->flags);
  2859. s->blocked_rdev = rdev;
  2860. atomic_inc(&rdev->nr_pending);
  2861. }
  2862. }
  2863. clear_bit(R5_Insync, &dev->flags);
  2864. if (!rdev)
  2865. /* Not in-sync */;
  2866. else if (is_bad) {
  2867. /* also not in-sync */
  2868. if (!test_bit(WriteErrorSeen, &rdev->flags)) {
  2869. /* treat as in-sync, but with a read error
  2870. * which we can now try to correct
  2871. */
  2872. set_bit(R5_Insync, &dev->flags);
  2873. set_bit(R5_ReadError, &dev->flags);
  2874. }
  2875. } else if (test_bit(In_sync, &rdev->flags))
  2876. set_bit(R5_Insync, &dev->flags);
  2877. else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
  2878. /* in sync if before recovery_offset */
  2879. set_bit(R5_Insync, &dev->flags);
  2880. else if (test_bit(R5_UPTODATE, &dev->flags) &&
  2881. test_bit(R5_Expanded, &dev->flags))
  2882. /* If we've reshaped into here, we assume it is Insync.
  2883. * We will shortly update recovery_offset to make
  2884. * it official.
  2885. */
  2886. set_bit(R5_Insync, &dev->flags);
  2887. if (rdev && test_bit(R5_WriteError, &dev->flags)) {
  2888. /* This flag does not apply to '.replacement'
  2889. * only to .rdev, so make sure to check that*/
  2890. struct md_rdev *rdev2 = rcu_dereference(
  2891. conf->disks[i].rdev);
  2892. if (rdev2 == rdev)
  2893. clear_bit(R5_Insync, &dev->flags);
  2894. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  2895. s->handle_bad_blocks = 1;
  2896. atomic_inc(&rdev2->nr_pending);
  2897. } else
  2898. clear_bit(R5_WriteError, &dev->flags);
  2899. }
  2900. if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
  2901. /* This flag does not apply to '.replacement'
  2902. * only to .rdev, so make sure to check that*/
  2903. struct md_rdev *rdev2 = rcu_dereference(
  2904. conf->disks[i].rdev);
  2905. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  2906. s->handle_bad_blocks = 1;
  2907. atomic_inc(&rdev2->nr_pending);
  2908. } else
  2909. clear_bit(R5_MadeGood, &dev->flags);
  2910. }
  2911. if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
  2912. struct md_rdev *rdev2 = rcu_dereference(
  2913. conf->disks[i].replacement);
  2914. if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
  2915. s->handle_bad_blocks = 1;
  2916. atomic_inc(&rdev2->nr_pending);
  2917. } else
  2918. clear_bit(R5_MadeGoodRepl, &dev->flags);
  2919. }
  2920. if (!test_bit(R5_Insync, &dev->flags)) {
  2921. /* The ReadError flag will just be confusing now */
  2922. clear_bit(R5_ReadError, &dev->flags);
  2923. clear_bit(R5_ReWrite, &dev->flags);
  2924. }
  2925. if (test_bit(R5_ReadError, &dev->flags))
  2926. clear_bit(R5_Insync, &dev->flags);
  2927. if (!test_bit(R5_Insync, &dev->flags)) {
  2928. if (s->failed < 2)
  2929. s->failed_num[s->failed] = i;
  2930. s->failed++;
  2931. if (rdev && !test_bit(Faulty, &rdev->flags))
  2932. do_recovery = 1;
  2933. }
  2934. }
  2935. spin_unlock_irq(&conf->device_lock);
  2936. if (test_bit(STRIPE_SYNCING, &sh->state)) {
  2937. /* If there is a failed device being replaced,
  2938. * we must be recovering.
  2939. * else if we are after recovery_cp, we must be syncing
  2940. * else we can only be replacing
  2941. * sync and recovery both need to read all devices, and so
  2942. * use the same flag.
  2943. */
  2944. if (do_recovery ||
  2945. sh->sector >= conf->mddev->recovery_cp)
  2946. s->syncing = 1;
  2947. else
  2948. s->replacing = 1;
  2949. }
  2950. rcu_read_unlock();
  2951. }
  2952. static void handle_stripe(struct stripe_head *sh)
  2953. {
  2954. struct stripe_head_state s;
  2955. struct r5conf *conf = sh->raid_conf;
  2956. int i;
  2957. int prexor;
  2958. int disks = sh->disks;
  2959. struct r5dev *pdev, *qdev;
  2960. clear_bit(STRIPE_HANDLE, &sh->state);
  2961. if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
  2962. /* already being handled, ensure it gets handled
  2963. * again when current action finishes */
  2964. set_bit(STRIPE_HANDLE, &sh->state);
  2965. return;
  2966. }
  2967. if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
  2968. set_bit(STRIPE_SYNCING, &sh->state);
  2969. clear_bit(STRIPE_INSYNC, &sh->state);
  2970. }
  2971. clear_bit(STRIPE_DELAYED, &sh->state);
  2972. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2973. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  2974. (unsigned long long)sh->sector, sh->state,
  2975. atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
  2976. sh->check_state, sh->reconstruct_state);
  2977. analyse_stripe(sh, &s);
  2978. if (s.handle_bad_blocks) {
  2979. set_bit(STRIPE_HANDLE, &sh->state);
  2980. goto finish;
  2981. }
  2982. if (unlikely(s.blocked_rdev)) {
  2983. if (s.syncing || s.expanding || s.expanded ||
  2984. s.replacing || s.to_write || s.written) {
  2985. set_bit(STRIPE_HANDLE, &sh->state);
  2986. goto finish;
  2987. }
  2988. /* There is nothing for the blocked_rdev to block */
  2989. rdev_dec_pending(s.blocked_rdev, conf->mddev);
  2990. s.blocked_rdev = NULL;
  2991. }
  2992. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2993. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2994. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2995. }
  2996. pr_debug("locked=%d uptodate=%d to_read=%d"
  2997. " to_write=%d failed=%d failed_num=%d,%d\n",
  2998. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2999. s.failed_num[0], s.failed_num[1]);
  3000. /* check if the array has lost more than max_degraded devices and,
  3001. * if so, some requests might need to be failed.
  3002. */
  3003. if (s.failed > conf->max_degraded) {
  3004. sh->check_state = 0;
  3005. sh->reconstruct_state = 0;
  3006. if (s.to_read+s.to_write+s.written)
  3007. handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
  3008. if (s.syncing + s.replacing)
  3009. handle_failed_sync(conf, sh, &s);
  3010. }
  3011. /*
  3012. * might be able to return some write requests if the parity blocks
  3013. * are safe, or on a failed drive
  3014. */
  3015. pdev = &sh->dev[sh->pd_idx];
  3016. s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
  3017. || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
  3018. qdev = &sh->dev[sh->qd_idx];
  3019. s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
  3020. || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
  3021. || conf->level < 6;
  3022. if (s.written &&
  3023. (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  3024. && !test_bit(R5_LOCKED, &pdev->flags)
  3025. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  3026. (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  3027. && !test_bit(R5_LOCKED, &qdev->flags)
  3028. && test_bit(R5_UPTODATE, &qdev->flags)))))
  3029. handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
  3030. /* Now we might consider reading some blocks, either to check/generate
  3031. * parity, or to satisfy requests
  3032. * or to load a block that is being partially written.
  3033. */
  3034. if (s.to_read || s.non_overwrite
  3035. || (conf->level == 6 && s.to_write && s.failed)
  3036. || (s.syncing && (s.uptodate + s.compute < disks))
  3037. || s.replacing
  3038. || s.expanding)
  3039. handle_stripe_fill(sh, &s, disks);
  3040. /* Now we check to see if any write operations have recently
  3041. * completed
  3042. */
  3043. prexor = 0;
  3044. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  3045. prexor = 1;
  3046. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  3047. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  3048. sh->reconstruct_state = reconstruct_state_idle;
  3049. /* All the 'written' buffers and the parity block are ready to
  3050. * be written back to disk
  3051. */
  3052. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  3053. BUG_ON(sh->qd_idx >= 0 &&
  3054. !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
  3055. for (i = disks; i--; ) {
  3056. struct r5dev *dev = &sh->dev[i];
  3057. if (test_bit(R5_LOCKED, &dev->flags) &&
  3058. (i == sh->pd_idx || i == sh->qd_idx ||
  3059. dev->written)) {
  3060. pr_debug("Writing block %d\n", i);
  3061. set_bit(R5_Wantwrite, &dev->flags);
  3062. if (prexor)
  3063. continue;
  3064. if (!test_bit(R5_Insync, &dev->flags) ||
  3065. ((i == sh->pd_idx || i == sh->qd_idx) &&
  3066. s.failed == 0))
  3067. set_bit(STRIPE_INSYNC, &sh->state);
  3068. }
  3069. }
  3070. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3071. s.dec_preread_active = 1;
  3072. }
  3073. /* Now to consider new write requests and what else, if anything
  3074. * should be read. We do not handle new writes when:
  3075. * 1/ A 'write' operation (copy+xor) is already in flight.
  3076. * 2/ A 'check' operation is in flight, as it may clobber the parity
  3077. * block.
  3078. */
  3079. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  3080. handle_stripe_dirtying(conf, sh, &s, disks);
  3081. /* maybe we need to check and possibly fix the parity for this stripe
  3082. * Any reads will already have been scheduled, so we just see if enough
  3083. * data is available. The parity check is held off while parity
  3084. * dependent operations are in flight.
  3085. */
  3086. if (sh->check_state ||
  3087. (s.syncing && s.locked == 0 &&
  3088. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  3089. !test_bit(STRIPE_INSYNC, &sh->state))) {
  3090. if (conf->level == 6)
  3091. handle_parity_checks6(conf, sh, &s, disks);
  3092. else
  3093. handle_parity_checks5(conf, sh, &s, disks);
  3094. }
  3095. if (s.replacing && s.locked == 0
  3096. && !test_bit(STRIPE_INSYNC, &sh->state)) {
  3097. /* Write out to replacement devices where possible */
  3098. for (i = 0; i < conf->raid_disks; i++)
  3099. if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
  3100. test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
  3101. set_bit(R5_WantReplace, &sh->dev[i].flags);
  3102. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3103. s.locked++;
  3104. }
  3105. set_bit(STRIPE_INSYNC, &sh->state);
  3106. }
  3107. if ((s.syncing || s.replacing) && s.locked == 0 &&
  3108. test_bit(STRIPE_INSYNC, &sh->state)) {
  3109. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3110. clear_bit(STRIPE_SYNCING, &sh->state);
  3111. }
  3112. /* If the failed drives are just a ReadError, then we might need
  3113. * to progress the repair/check process
  3114. */
  3115. if (s.failed <= conf->max_degraded && !conf->mddev->ro)
  3116. for (i = 0; i < s.failed; i++) {
  3117. struct r5dev *dev = &sh->dev[s.failed_num[i]];
  3118. if (test_bit(R5_ReadError, &dev->flags)
  3119. && !test_bit(R5_LOCKED, &dev->flags)
  3120. && test_bit(R5_UPTODATE, &dev->flags)
  3121. ) {
  3122. if (!test_bit(R5_ReWrite, &dev->flags)) {
  3123. set_bit(R5_Wantwrite, &dev->flags);
  3124. set_bit(R5_ReWrite, &dev->flags);
  3125. set_bit(R5_LOCKED, &dev->flags);
  3126. s.locked++;
  3127. } else {
  3128. /* let's read it back */
  3129. set_bit(R5_Wantread, &dev->flags);
  3130. set_bit(R5_LOCKED, &dev->flags);
  3131. s.locked++;
  3132. }
  3133. }
  3134. }
  3135. /* Finish reconstruct operations initiated by the expansion process */
  3136. if (sh->reconstruct_state == reconstruct_state_result) {
  3137. struct stripe_head *sh_src
  3138. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  3139. if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
  3140. /* sh cannot be written until sh_src has been read.
  3141. * so arrange for sh to be delayed a little
  3142. */
  3143. set_bit(STRIPE_DELAYED, &sh->state);
  3144. set_bit(STRIPE_HANDLE, &sh->state);
  3145. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  3146. &sh_src->state))
  3147. atomic_inc(&conf->preread_active_stripes);
  3148. release_stripe(sh_src);
  3149. goto finish;
  3150. }
  3151. if (sh_src)
  3152. release_stripe(sh_src);
  3153. sh->reconstruct_state = reconstruct_state_idle;
  3154. clear_bit(STRIPE_EXPANDING, &sh->state);
  3155. for (i = conf->raid_disks; i--; ) {
  3156. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  3157. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3158. s.locked++;
  3159. }
  3160. }
  3161. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  3162. !sh->reconstruct_state) {
  3163. /* Need to write out all blocks after computing parity */
  3164. sh->disks = conf->raid_disks;
  3165. stripe_set_idx(sh->sector, conf, 0, sh);
  3166. schedule_reconstruction(sh, &s, 1, 1);
  3167. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  3168. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  3169. atomic_dec(&conf->reshape_stripes);
  3170. wake_up(&conf->wait_for_overlap);
  3171. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3172. }
  3173. if (s.expanding && s.locked == 0 &&
  3174. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  3175. handle_stripe_expansion(conf, sh);
  3176. finish:
  3177. /* wait for this device to become unblocked */
  3178. if (conf->mddev->external && unlikely(s.blocked_rdev))
  3179. md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
  3180. if (s.handle_bad_blocks)
  3181. for (i = disks; i--; ) {
  3182. struct md_rdev *rdev;
  3183. struct r5dev *dev = &sh->dev[i];
  3184. if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
  3185. /* We own a safe reference to the rdev */
  3186. rdev = conf->disks[i].rdev;
  3187. if (!rdev_set_badblocks(rdev, sh->sector,
  3188. STRIPE_SECTORS, 0))
  3189. md_error(conf->mddev, rdev);
  3190. rdev_dec_pending(rdev, conf->mddev);
  3191. }
  3192. if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
  3193. rdev = conf->disks[i].rdev;
  3194. rdev_clear_badblocks(rdev, sh->sector,
  3195. STRIPE_SECTORS);
  3196. rdev_dec_pending(rdev, conf->mddev);
  3197. }
  3198. if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
  3199. rdev = conf->disks[i].replacement;
  3200. if (!rdev)
  3201. /* rdev have been moved down */
  3202. rdev = conf->disks[i].rdev;
  3203. rdev_clear_badblocks(rdev, sh->sector,
  3204. STRIPE_SECTORS);
  3205. rdev_dec_pending(rdev, conf->mddev);
  3206. }
  3207. }
  3208. if (s.ops_request)
  3209. raid_run_ops(sh, s.ops_request);
  3210. ops_run_io(sh, &s);
  3211. if (s.dec_preread_active) {
  3212. /* We delay this until after ops_run_io so that if make_request
  3213. * is waiting on a flush, it won't continue until the writes
  3214. * have actually been submitted.
  3215. */
  3216. atomic_dec(&conf->preread_active_stripes);
  3217. if (atomic_read(&conf->preread_active_stripes) <
  3218. IO_THRESHOLD)
  3219. md_wakeup_thread(conf->mddev->thread);
  3220. }
  3221. return_io(s.return_bi);
  3222. clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
  3223. }
  3224. static void raid5_activate_delayed(struct r5conf *conf)
  3225. {
  3226. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  3227. while (!list_empty(&conf->delayed_list)) {
  3228. struct list_head *l = conf->delayed_list.next;
  3229. struct stripe_head *sh;
  3230. sh = list_entry(l, struct stripe_head, lru);
  3231. list_del_init(l);
  3232. clear_bit(STRIPE_DELAYED, &sh->state);
  3233. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3234. atomic_inc(&conf->preread_active_stripes);
  3235. list_add_tail(&sh->lru, &conf->hold_list);
  3236. }
  3237. }
  3238. }
  3239. static void activate_bit_delay(struct r5conf *conf)
  3240. {
  3241. /* device_lock is held */
  3242. struct list_head head;
  3243. list_add(&head, &conf->bitmap_list);
  3244. list_del_init(&conf->bitmap_list);
  3245. while (!list_empty(&head)) {
  3246. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  3247. list_del_init(&sh->lru);
  3248. atomic_inc(&sh->count);
  3249. __release_stripe(conf, sh);
  3250. }
  3251. }
  3252. int md_raid5_congested(struct mddev *mddev, int bits)
  3253. {
  3254. struct r5conf *conf = mddev->private;
  3255. /* No difference between reads and writes. Just check
  3256. * how busy the stripe_cache is
  3257. */
  3258. if (conf->inactive_blocked)
  3259. return 1;
  3260. if (conf->quiesce)
  3261. return 1;
  3262. if (list_empty_careful(&conf->inactive_list))
  3263. return 1;
  3264. return 0;
  3265. }
  3266. EXPORT_SYMBOL_GPL(md_raid5_congested);
  3267. static int raid5_congested(void *data, int bits)
  3268. {
  3269. struct mddev *mddev = data;
  3270. return mddev_congested(mddev, bits) ||
  3271. md_raid5_congested(mddev, bits);
  3272. }
  3273. /* We want read requests to align with chunks where possible,
  3274. * but write requests don't need to.
  3275. */
  3276. static int raid5_mergeable_bvec(struct request_queue *q,
  3277. struct bvec_merge_data *bvm,
  3278. struct bio_vec *biovec)
  3279. {
  3280. struct mddev *mddev = q->queuedata;
  3281. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  3282. int max;
  3283. unsigned int chunk_sectors = mddev->chunk_sectors;
  3284. unsigned int bio_sectors = bvm->bi_size >> 9;
  3285. if ((bvm->bi_rw & 1) == WRITE)
  3286. return biovec->bv_len; /* always allow writes to be mergeable */
  3287. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3288. chunk_sectors = mddev->new_chunk_sectors;
  3289. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  3290. if (max < 0) max = 0;
  3291. if (max <= biovec->bv_len && bio_sectors == 0)
  3292. return biovec->bv_len;
  3293. else
  3294. return max;
  3295. }
  3296. static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
  3297. {
  3298. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  3299. unsigned int chunk_sectors = mddev->chunk_sectors;
  3300. unsigned int bio_sectors = bio->bi_size >> 9;
  3301. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3302. chunk_sectors = mddev->new_chunk_sectors;
  3303. return chunk_sectors >=
  3304. ((sector & (chunk_sectors - 1)) + bio_sectors);
  3305. }
  3306. /*
  3307. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  3308. * later sampled by raid5d.
  3309. */
  3310. static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
  3311. {
  3312. unsigned long flags;
  3313. spin_lock_irqsave(&conf->device_lock, flags);
  3314. bi->bi_next = conf->retry_read_aligned_list;
  3315. conf->retry_read_aligned_list = bi;
  3316. spin_unlock_irqrestore(&conf->device_lock, flags);
  3317. md_wakeup_thread(conf->mddev->thread);
  3318. }
  3319. static struct bio *remove_bio_from_retry(struct r5conf *conf)
  3320. {
  3321. struct bio *bi;
  3322. bi = conf->retry_read_aligned;
  3323. if (bi) {
  3324. conf->retry_read_aligned = NULL;
  3325. return bi;
  3326. }
  3327. bi = conf->retry_read_aligned_list;
  3328. if(bi) {
  3329. conf->retry_read_aligned_list = bi->bi_next;
  3330. bi->bi_next = NULL;
  3331. /*
  3332. * this sets the active strip count to 1 and the processed
  3333. * strip count to zero (upper 8 bits)
  3334. */
  3335. bi->bi_phys_segments = 1; /* biased count of active stripes */
  3336. }
  3337. return bi;
  3338. }
  3339. /*
  3340. * The "raid5_align_endio" should check if the read succeeded and if it
  3341. * did, call bio_endio on the original bio (having bio_put the new bio
  3342. * first).
  3343. * If the read failed..
  3344. */
  3345. static void raid5_align_endio(struct bio *bi, int error)
  3346. {
  3347. struct bio* raid_bi = bi->bi_private;
  3348. struct mddev *mddev;
  3349. struct r5conf *conf;
  3350. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3351. struct md_rdev *rdev;
  3352. bio_put(bi);
  3353. rdev = (void*)raid_bi->bi_next;
  3354. raid_bi->bi_next = NULL;
  3355. mddev = rdev->mddev;
  3356. conf = mddev->private;
  3357. rdev_dec_pending(rdev, conf->mddev);
  3358. if (!error && uptodate) {
  3359. bio_endio(raid_bi, 0);
  3360. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3361. wake_up(&conf->wait_for_stripe);
  3362. return;
  3363. }
  3364. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3365. add_bio_to_retry(raid_bi, conf);
  3366. }
  3367. static int bio_fits_rdev(struct bio *bi)
  3368. {
  3369. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3370. if ((bi->bi_size>>9) > queue_max_sectors(q))
  3371. return 0;
  3372. blk_recount_segments(q, bi);
  3373. if (bi->bi_phys_segments > queue_max_segments(q))
  3374. return 0;
  3375. if (q->merge_bvec_fn)
  3376. /* it's too hard to apply the merge_bvec_fn at this stage,
  3377. * just just give up
  3378. */
  3379. return 0;
  3380. return 1;
  3381. }
  3382. static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
  3383. {
  3384. struct r5conf *conf = mddev->private;
  3385. int dd_idx;
  3386. struct bio* align_bi;
  3387. struct md_rdev *rdev;
  3388. sector_t end_sector;
  3389. if (!in_chunk_boundary(mddev, raid_bio)) {
  3390. pr_debug("chunk_aligned_read : non aligned\n");
  3391. return 0;
  3392. }
  3393. /*
  3394. * use bio_clone_mddev to make a copy of the bio
  3395. */
  3396. align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
  3397. if (!align_bi)
  3398. return 0;
  3399. /*
  3400. * set bi_end_io to a new function, and set bi_private to the
  3401. * original bio.
  3402. */
  3403. align_bi->bi_end_io = raid5_align_endio;
  3404. align_bi->bi_private = raid_bio;
  3405. /*
  3406. * compute position
  3407. */
  3408. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3409. 0,
  3410. &dd_idx, NULL);
  3411. end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
  3412. rcu_read_lock();
  3413. rdev = rcu_dereference(conf->disks[dd_idx].replacement);
  3414. if (!rdev || test_bit(Faulty, &rdev->flags) ||
  3415. rdev->recovery_offset < end_sector) {
  3416. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3417. if (rdev &&
  3418. (test_bit(Faulty, &rdev->flags) ||
  3419. !(test_bit(In_sync, &rdev->flags) ||
  3420. rdev->recovery_offset >= end_sector)))
  3421. rdev = NULL;
  3422. }
  3423. if (rdev) {
  3424. sector_t first_bad;
  3425. int bad_sectors;
  3426. atomic_inc(&rdev->nr_pending);
  3427. rcu_read_unlock();
  3428. raid_bio->bi_next = (void*)rdev;
  3429. align_bi->bi_bdev = rdev->bdev;
  3430. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3431. align_bi->bi_sector += rdev->data_offset;
  3432. if (!bio_fits_rdev(align_bi) ||
  3433. is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
  3434. &first_bad, &bad_sectors)) {
  3435. /* too big in some way, or has a known bad block */
  3436. bio_put(align_bi);
  3437. rdev_dec_pending(rdev, mddev);
  3438. return 0;
  3439. }
  3440. spin_lock_irq(&conf->device_lock);
  3441. wait_event_lock_irq(conf->wait_for_stripe,
  3442. conf->quiesce == 0,
  3443. conf->device_lock, /* nothing */);
  3444. atomic_inc(&conf->active_aligned_reads);
  3445. spin_unlock_irq(&conf->device_lock);
  3446. generic_make_request(align_bi);
  3447. return 1;
  3448. } else {
  3449. rcu_read_unlock();
  3450. bio_put(align_bi);
  3451. return 0;
  3452. }
  3453. }
  3454. /* __get_priority_stripe - get the next stripe to process
  3455. *
  3456. * Full stripe writes are allowed to pass preread active stripes up until
  3457. * the bypass_threshold is exceeded. In general the bypass_count
  3458. * increments when the handle_list is handled before the hold_list; however, it
  3459. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3460. * stripe with in flight i/o. The bypass_count will be reset when the
  3461. * head of the hold_list has changed, i.e. the head was promoted to the
  3462. * handle_list.
  3463. */
  3464. static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
  3465. {
  3466. struct stripe_head *sh;
  3467. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3468. __func__,
  3469. list_empty(&conf->handle_list) ? "empty" : "busy",
  3470. list_empty(&conf->hold_list) ? "empty" : "busy",
  3471. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3472. if (!list_empty(&conf->handle_list)) {
  3473. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3474. if (list_empty(&conf->hold_list))
  3475. conf->bypass_count = 0;
  3476. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3477. if (conf->hold_list.next == conf->last_hold)
  3478. conf->bypass_count++;
  3479. else {
  3480. conf->last_hold = conf->hold_list.next;
  3481. conf->bypass_count -= conf->bypass_threshold;
  3482. if (conf->bypass_count < 0)
  3483. conf->bypass_count = 0;
  3484. }
  3485. }
  3486. } else if (!list_empty(&conf->hold_list) &&
  3487. ((conf->bypass_threshold &&
  3488. conf->bypass_count > conf->bypass_threshold) ||
  3489. atomic_read(&conf->pending_full_writes) == 0)) {
  3490. sh = list_entry(conf->hold_list.next,
  3491. typeof(*sh), lru);
  3492. conf->bypass_count -= conf->bypass_threshold;
  3493. if (conf->bypass_count < 0)
  3494. conf->bypass_count = 0;
  3495. } else
  3496. return NULL;
  3497. list_del_init(&sh->lru);
  3498. atomic_inc(&sh->count);
  3499. BUG_ON(atomic_read(&sh->count) != 1);
  3500. return sh;
  3501. }
  3502. static void make_request(struct mddev *mddev, struct bio * bi)
  3503. {
  3504. struct r5conf *conf = mddev->private;
  3505. int dd_idx;
  3506. sector_t new_sector;
  3507. sector_t logical_sector, last_sector;
  3508. struct stripe_head *sh;
  3509. const int rw = bio_data_dir(bi);
  3510. int remaining;
  3511. int plugged;
  3512. if (unlikely(bi->bi_rw & REQ_FLUSH)) {
  3513. md_flush_request(mddev, bi);
  3514. return;
  3515. }
  3516. md_write_start(mddev, bi);
  3517. if (rw == READ &&
  3518. mddev->reshape_position == MaxSector &&
  3519. chunk_aligned_read(mddev,bi))
  3520. return;
  3521. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3522. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3523. bi->bi_next = NULL;
  3524. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3525. plugged = mddev_check_plugged(mddev);
  3526. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3527. DEFINE_WAIT(w);
  3528. int disks, data_disks;
  3529. int previous;
  3530. retry:
  3531. previous = 0;
  3532. disks = conf->raid_disks;
  3533. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3534. if (unlikely(conf->reshape_progress != MaxSector)) {
  3535. /* spinlock is needed as reshape_progress may be
  3536. * 64bit on a 32bit platform, and so it might be
  3537. * possible to see a half-updated value
  3538. * Of course reshape_progress could change after
  3539. * the lock is dropped, so once we get a reference
  3540. * to the stripe that we think it is, we will have
  3541. * to check again.
  3542. */
  3543. spin_lock_irq(&conf->device_lock);
  3544. if (mddev->delta_disks < 0
  3545. ? logical_sector < conf->reshape_progress
  3546. : logical_sector >= conf->reshape_progress) {
  3547. disks = conf->previous_raid_disks;
  3548. previous = 1;
  3549. } else {
  3550. if (mddev->delta_disks < 0
  3551. ? logical_sector < conf->reshape_safe
  3552. : logical_sector >= conf->reshape_safe) {
  3553. spin_unlock_irq(&conf->device_lock);
  3554. schedule();
  3555. goto retry;
  3556. }
  3557. }
  3558. spin_unlock_irq(&conf->device_lock);
  3559. }
  3560. data_disks = disks - conf->max_degraded;
  3561. new_sector = raid5_compute_sector(conf, logical_sector,
  3562. previous,
  3563. &dd_idx, NULL);
  3564. pr_debug("raid456: make_request, sector %llu logical %llu\n",
  3565. (unsigned long long)new_sector,
  3566. (unsigned long long)logical_sector);
  3567. sh = get_active_stripe(conf, new_sector, previous,
  3568. (bi->bi_rw&RWA_MASK), 0);
  3569. if (sh) {
  3570. if (unlikely(previous)) {
  3571. /* expansion might have moved on while waiting for a
  3572. * stripe, so we must do the range check again.
  3573. * Expansion could still move past after this
  3574. * test, but as we are holding a reference to
  3575. * 'sh', we know that if that happens,
  3576. * STRIPE_EXPANDING will get set and the expansion
  3577. * won't proceed until we finish with the stripe.
  3578. */
  3579. int must_retry = 0;
  3580. spin_lock_irq(&conf->device_lock);
  3581. if (mddev->delta_disks < 0
  3582. ? logical_sector >= conf->reshape_progress
  3583. : logical_sector < conf->reshape_progress)
  3584. /* mismatch, need to try again */
  3585. must_retry = 1;
  3586. spin_unlock_irq(&conf->device_lock);
  3587. if (must_retry) {
  3588. release_stripe(sh);
  3589. schedule();
  3590. goto retry;
  3591. }
  3592. }
  3593. if (rw == WRITE &&
  3594. logical_sector >= mddev->suspend_lo &&
  3595. logical_sector < mddev->suspend_hi) {
  3596. release_stripe(sh);
  3597. /* As the suspend_* range is controlled by
  3598. * userspace, we want an interruptible
  3599. * wait.
  3600. */
  3601. flush_signals(current);
  3602. prepare_to_wait(&conf->wait_for_overlap,
  3603. &w, TASK_INTERRUPTIBLE);
  3604. if (logical_sector >= mddev->suspend_lo &&
  3605. logical_sector < mddev->suspend_hi)
  3606. schedule();
  3607. goto retry;
  3608. }
  3609. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3610. !add_stripe_bio(sh, bi, dd_idx, rw)) {
  3611. /* Stripe is busy expanding or
  3612. * add failed due to overlap. Flush everything
  3613. * and wait a while
  3614. */
  3615. md_wakeup_thread(mddev->thread);
  3616. release_stripe(sh);
  3617. schedule();
  3618. goto retry;
  3619. }
  3620. finish_wait(&conf->wait_for_overlap, &w);
  3621. set_bit(STRIPE_HANDLE, &sh->state);
  3622. clear_bit(STRIPE_DELAYED, &sh->state);
  3623. if ((bi->bi_rw & REQ_SYNC) &&
  3624. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3625. atomic_inc(&conf->preread_active_stripes);
  3626. release_stripe(sh);
  3627. } else {
  3628. /* cannot get stripe for read-ahead, just give-up */
  3629. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3630. finish_wait(&conf->wait_for_overlap, &w);
  3631. break;
  3632. }
  3633. }
  3634. if (!plugged)
  3635. md_wakeup_thread(mddev->thread);
  3636. spin_lock_irq(&conf->device_lock);
  3637. remaining = raid5_dec_bi_phys_segments(bi);
  3638. spin_unlock_irq(&conf->device_lock);
  3639. if (remaining == 0) {
  3640. if ( rw == WRITE )
  3641. md_write_end(mddev);
  3642. bio_endio(bi, 0);
  3643. }
  3644. }
  3645. static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
  3646. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
  3647. {
  3648. /* reshaping is quite different to recovery/resync so it is
  3649. * handled quite separately ... here.
  3650. *
  3651. * On each call to sync_request, we gather one chunk worth of
  3652. * destination stripes and flag them as expanding.
  3653. * Then we find all the source stripes and request reads.
  3654. * As the reads complete, handle_stripe will copy the data
  3655. * into the destination stripe and release that stripe.
  3656. */
  3657. struct r5conf *conf = mddev->private;
  3658. struct stripe_head *sh;
  3659. sector_t first_sector, last_sector;
  3660. int raid_disks = conf->previous_raid_disks;
  3661. int data_disks = raid_disks - conf->max_degraded;
  3662. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3663. int i;
  3664. int dd_idx;
  3665. sector_t writepos, readpos, safepos;
  3666. sector_t stripe_addr;
  3667. int reshape_sectors;
  3668. struct list_head stripes;
  3669. if (sector_nr == 0) {
  3670. /* If restarting in the middle, skip the initial sectors */
  3671. if (mddev->delta_disks < 0 &&
  3672. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  3673. sector_nr = raid5_size(mddev, 0, 0)
  3674. - conf->reshape_progress;
  3675. } else if (mddev->delta_disks >= 0 &&
  3676. conf->reshape_progress > 0)
  3677. sector_nr = conf->reshape_progress;
  3678. sector_div(sector_nr, new_data_disks);
  3679. if (sector_nr) {
  3680. mddev->curr_resync_completed = sector_nr;
  3681. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3682. *skipped = 1;
  3683. return sector_nr;
  3684. }
  3685. }
  3686. /* We need to process a full chunk at a time.
  3687. * If old and new chunk sizes differ, we need to process the
  3688. * largest of these
  3689. */
  3690. if (mddev->new_chunk_sectors > mddev->chunk_sectors)
  3691. reshape_sectors = mddev->new_chunk_sectors;
  3692. else
  3693. reshape_sectors = mddev->chunk_sectors;
  3694. /* we update the metadata when there is more than 3Meg
  3695. * in the block range (that is rather arbitrary, should
  3696. * probably be time based) or when the data about to be
  3697. * copied would over-write the source of the data at
  3698. * the front of the range.
  3699. * i.e. one new_stripe along from reshape_progress new_maps
  3700. * to after where reshape_safe old_maps to
  3701. */
  3702. writepos = conf->reshape_progress;
  3703. sector_div(writepos, new_data_disks);
  3704. readpos = conf->reshape_progress;
  3705. sector_div(readpos, data_disks);
  3706. safepos = conf->reshape_safe;
  3707. sector_div(safepos, data_disks);
  3708. if (mddev->delta_disks < 0) {
  3709. writepos -= min_t(sector_t, reshape_sectors, writepos);
  3710. readpos += reshape_sectors;
  3711. safepos += reshape_sectors;
  3712. } else {
  3713. writepos += reshape_sectors;
  3714. readpos -= min_t(sector_t, reshape_sectors, readpos);
  3715. safepos -= min_t(sector_t, reshape_sectors, safepos);
  3716. }
  3717. /* 'writepos' is the most advanced device address we might write.
  3718. * 'readpos' is the least advanced device address we might read.
  3719. * 'safepos' is the least address recorded in the metadata as having
  3720. * been reshaped.
  3721. * If 'readpos' is behind 'writepos', then there is no way that we can
  3722. * ensure safety in the face of a crash - that must be done by userspace
  3723. * making a backup of the data. So in that case there is no particular
  3724. * rush to update metadata.
  3725. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  3726. * update the metadata to advance 'safepos' to match 'readpos' so that
  3727. * we can be safe in the event of a crash.
  3728. * So we insist on updating metadata if safepos is behind writepos and
  3729. * readpos is beyond writepos.
  3730. * In any case, update the metadata every 10 seconds.
  3731. * Maybe that number should be configurable, but I'm not sure it is
  3732. * worth it.... maybe it could be a multiple of safemode_delay???
  3733. */
  3734. if ((mddev->delta_disks < 0
  3735. ? (safepos > writepos && readpos < writepos)
  3736. : (safepos < writepos && readpos > writepos)) ||
  3737. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3738. /* Cannot proceed until we've updated the superblock... */
  3739. wait_event(conf->wait_for_overlap,
  3740. atomic_read(&conf->reshape_stripes)==0);
  3741. mddev->reshape_position = conf->reshape_progress;
  3742. mddev->curr_resync_completed = sector_nr;
  3743. conf->reshape_checkpoint = jiffies;
  3744. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3745. md_wakeup_thread(mddev->thread);
  3746. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3747. kthread_should_stop());
  3748. spin_lock_irq(&conf->device_lock);
  3749. conf->reshape_safe = mddev->reshape_position;
  3750. spin_unlock_irq(&conf->device_lock);
  3751. wake_up(&conf->wait_for_overlap);
  3752. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3753. }
  3754. if (mddev->delta_disks < 0) {
  3755. BUG_ON(conf->reshape_progress == 0);
  3756. stripe_addr = writepos;
  3757. BUG_ON((mddev->dev_sectors &
  3758. ~((sector_t)reshape_sectors - 1))
  3759. - reshape_sectors - stripe_addr
  3760. != sector_nr);
  3761. } else {
  3762. BUG_ON(writepos != sector_nr + reshape_sectors);
  3763. stripe_addr = sector_nr;
  3764. }
  3765. INIT_LIST_HEAD(&stripes);
  3766. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  3767. int j;
  3768. int skipped_disk = 0;
  3769. sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  3770. set_bit(STRIPE_EXPANDING, &sh->state);
  3771. atomic_inc(&conf->reshape_stripes);
  3772. /* If any of this stripe is beyond the end of the old
  3773. * array, then we need to zero those blocks
  3774. */
  3775. for (j=sh->disks; j--;) {
  3776. sector_t s;
  3777. if (j == sh->pd_idx)
  3778. continue;
  3779. if (conf->level == 6 &&
  3780. j == sh->qd_idx)
  3781. continue;
  3782. s = compute_blocknr(sh, j, 0);
  3783. if (s < raid5_size(mddev, 0, 0)) {
  3784. skipped_disk = 1;
  3785. continue;
  3786. }
  3787. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3788. set_bit(R5_Expanded, &sh->dev[j].flags);
  3789. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3790. }
  3791. if (!skipped_disk) {
  3792. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3793. set_bit(STRIPE_HANDLE, &sh->state);
  3794. }
  3795. list_add(&sh->lru, &stripes);
  3796. }
  3797. spin_lock_irq(&conf->device_lock);
  3798. if (mddev->delta_disks < 0)
  3799. conf->reshape_progress -= reshape_sectors * new_data_disks;
  3800. else
  3801. conf->reshape_progress += reshape_sectors * new_data_disks;
  3802. spin_unlock_irq(&conf->device_lock);
  3803. /* Ok, those stripe are ready. We can start scheduling
  3804. * reads on the source stripes.
  3805. * The source stripes are determined by mapping the first and last
  3806. * block on the destination stripes.
  3807. */
  3808. first_sector =
  3809. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  3810. 1, &dd_idx, NULL);
  3811. last_sector =
  3812. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  3813. * new_data_disks - 1),
  3814. 1, &dd_idx, NULL);
  3815. if (last_sector >= mddev->dev_sectors)
  3816. last_sector = mddev->dev_sectors - 1;
  3817. while (first_sector <= last_sector) {
  3818. sh = get_active_stripe(conf, first_sector, 1, 0, 1);
  3819. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3820. set_bit(STRIPE_HANDLE, &sh->state);
  3821. release_stripe(sh);
  3822. first_sector += STRIPE_SECTORS;
  3823. }
  3824. /* Now that the sources are clearly marked, we can release
  3825. * the destination stripes
  3826. */
  3827. while (!list_empty(&stripes)) {
  3828. sh = list_entry(stripes.next, struct stripe_head, lru);
  3829. list_del_init(&sh->lru);
  3830. release_stripe(sh);
  3831. }
  3832. /* If this takes us to the resync_max point where we have to pause,
  3833. * then we need to write out the superblock.
  3834. */
  3835. sector_nr += reshape_sectors;
  3836. if ((sector_nr - mddev->curr_resync_completed) * 2
  3837. >= mddev->resync_max - mddev->curr_resync_completed) {
  3838. /* Cannot proceed until we've updated the superblock... */
  3839. wait_event(conf->wait_for_overlap,
  3840. atomic_read(&conf->reshape_stripes) == 0);
  3841. mddev->reshape_position = conf->reshape_progress;
  3842. mddev->curr_resync_completed = sector_nr;
  3843. conf->reshape_checkpoint = jiffies;
  3844. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3845. md_wakeup_thread(mddev->thread);
  3846. wait_event(mddev->sb_wait,
  3847. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3848. || kthread_should_stop());
  3849. spin_lock_irq(&conf->device_lock);
  3850. conf->reshape_safe = mddev->reshape_position;
  3851. spin_unlock_irq(&conf->device_lock);
  3852. wake_up(&conf->wait_for_overlap);
  3853. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3854. }
  3855. return reshape_sectors;
  3856. }
  3857. /* FIXME go_faster isn't used */
  3858. static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3859. {
  3860. struct r5conf *conf = mddev->private;
  3861. struct stripe_head *sh;
  3862. sector_t max_sector = mddev->dev_sectors;
  3863. sector_t sync_blocks;
  3864. int still_degraded = 0;
  3865. int i;
  3866. if (sector_nr >= max_sector) {
  3867. /* just being told to finish up .. nothing much to do */
  3868. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3869. end_reshape(conf);
  3870. return 0;
  3871. }
  3872. if (mddev->curr_resync < max_sector) /* aborted */
  3873. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3874. &sync_blocks, 1);
  3875. else /* completed sync */
  3876. conf->fullsync = 0;
  3877. bitmap_close_sync(mddev->bitmap);
  3878. return 0;
  3879. }
  3880. /* Allow raid5_quiesce to complete */
  3881. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  3882. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3883. return reshape_request(mddev, sector_nr, skipped);
  3884. /* No need to check resync_max as we never do more than one
  3885. * stripe, and as resync_max will always be on a chunk boundary,
  3886. * if the check in md_do_sync didn't fire, there is no chance
  3887. * of overstepping resync_max here
  3888. */
  3889. /* if there is too many failed drives and we are trying
  3890. * to resync, then assert that we are finished, because there is
  3891. * nothing we can do.
  3892. */
  3893. if (mddev->degraded >= conf->max_degraded &&
  3894. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3895. sector_t rv = mddev->dev_sectors - sector_nr;
  3896. *skipped = 1;
  3897. return rv;
  3898. }
  3899. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3900. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3901. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3902. /* we can skip this block, and probably more */
  3903. sync_blocks /= STRIPE_SECTORS;
  3904. *skipped = 1;
  3905. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3906. }
  3907. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3908. sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
  3909. if (sh == NULL) {
  3910. sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
  3911. /* make sure we don't swamp the stripe cache if someone else
  3912. * is trying to get access
  3913. */
  3914. schedule_timeout_uninterruptible(1);
  3915. }
  3916. /* Need to check if array will still be degraded after recovery/resync
  3917. * We don't need to check the 'failed' flag as when that gets set,
  3918. * recovery aborts.
  3919. */
  3920. for (i = 0; i < conf->raid_disks; i++)
  3921. if (conf->disks[i].rdev == NULL)
  3922. still_degraded = 1;
  3923. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3924. set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
  3925. handle_stripe(sh);
  3926. release_stripe(sh);
  3927. return STRIPE_SECTORS;
  3928. }
  3929. static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
  3930. {
  3931. /* We may not be able to submit a whole bio at once as there
  3932. * may not be enough stripe_heads available.
  3933. * We cannot pre-allocate enough stripe_heads as we may need
  3934. * more than exist in the cache (if we allow ever large chunks).
  3935. * So we do one stripe head at a time and record in
  3936. * ->bi_hw_segments how many have been done.
  3937. *
  3938. * We *know* that this entire raid_bio is in one chunk, so
  3939. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3940. */
  3941. struct stripe_head *sh;
  3942. int dd_idx;
  3943. sector_t sector, logical_sector, last_sector;
  3944. int scnt = 0;
  3945. int remaining;
  3946. int handled = 0;
  3947. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3948. sector = raid5_compute_sector(conf, logical_sector,
  3949. 0, &dd_idx, NULL);
  3950. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3951. for (; logical_sector < last_sector;
  3952. logical_sector += STRIPE_SECTORS,
  3953. sector += STRIPE_SECTORS,
  3954. scnt++) {
  3955. if (scnt < raid5_bi_hw_segments(raid_bio))
  3956. /* already done this stripe */
  3957. continue;
  3958. sh = get_active_stripe(conf, sector, 0, 1, 0);
  3959. if (!sh) {
  3960. /* failed to get a stripe - must wait */
  3961. raid5_set_bi_hw_segments(raid_bio, scnt);
  3962. conf->retry_read_aligned = raid_bio;
  3963. return handled;
  3964. }
  3965. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3966. release_stripe(sh);
  3967. raid5_set_bi_hw_segments(raid_bio, scnt);
  3968. conf->retry_read_aligned = raid_bio;
  3969. return handled;
  3970. }
  3971. handle_stripe(sh);
  3972. release_stripe(sh);
  3973. handled++;
  3974. }
  3975. spin_lock_irq(&conf->device_lock);
  3976. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3977. spin_unlock_irq(&conf->device_lock);
  3978. if (remaining == 0)
  3979. bio_endio(raid_bio, 0);
  3980. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3981. wake_up(&conf->wait_for_stripe);
  3982. return handled;
  3983. }
  3984. /*
  3985. * This is our raid5 kernel thread.
  3986. *
  3987. * We scan the hash table for stripes which can be handled now.
  3988. * During the scan, completed stripes are saved for us by the interrupt
  3989. * handler, so that they will not have to wait for our next wakeup.
  3990. */
  3991. static void raid5d(struct mddev *mddev)
  3992. {
  3993. struct stripe_head *sh;
  3994. struct r5conf *conf = mddev->private;
  3995. int handled;
  3996. struct blk_plug plug;
  3997. pr_debug("+++ raid5d active\n");
  3998. md_check_recovery(mddev);
  3999. blk_start_plug(&plug);
  4000. handled = 0;
  4001. spin_lock_irq(&conf->device_lock);
  4002. while (1) {
  4003. struct bio *bio;
  4004. if (atomic_read(&mddev->plug_cnt) == 0 &&
  4005. !list_empty(&conf->bitmap_list)) {
  4006. /* Now is a good time to flush some bitmap updates */
  4007. conf->seq_flush++;
  4008. spin_unlock_irq(&conf->device_lock);
  4009. bitmap_unplug(mddev->bitmap);
  4010. spin_lock_irq(&conf->device_lock);
  4011. conf->seq_write = conf->seq_flush;
  4012. activate_bit_delay(conf);
  4013. }
  4014. if (atomic_read(&mddev->plug_cnt) == 0)
  4015. raid5_activate_delayed(conf);
  4016. while ((bio = remove_bio_from_retry(conf))) {
  4017. int ok;
  4018. spin_unlock_irq(&conf->device_lock);
  4019. ok = retry_aligned_read(conf, bio);
  4020. spin_lock_irq(&conf->device_lock);
  4021. if (!ok)
  4022. break;
  4023. handled++;
  4024. }
  4025. sh = __get_priority_stripe(conf);
  4026. if (!sh)
  4027. break;
  4028. spin_unlock_irq(&conf->device_lock);
  4029. handled++;
  4030. handle_stripe(sh);
  4031. release_stripe(sh);
  4032. cond_resched();
  4033. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  4034. md_check_recovery(mddev);
  4035. spin_lock_irq(&conf->device_lock);
  4036. }
  4037. pr_debug("%d stripes handled\n", handled);
  4038. spin_unlock_irq(&conf->device_lock);
  4039. async_tx_issue_pending_all();
  4040. blk_finish_plug(&plug);
  4041. pr_debug("--- raid5d inactive\n");
  4042. }
  4043. static ssize_t
  4044. raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
  4045. {
  4046. struct r5conf *conf = mddev->private;
  4047. if (conf)
  4048. return sprintf(page, "%d\n", conf->max_nr_stripes);
  4049. else
  4050. return 0;
  4051. }
  4052. int
  4053. raid5_set_cache_size(struct mddev *mddev, int size)
  4054. {
  4055. struct r5conf *conf = mddev->private;
  4056. int err;
  4057. if (size <= 16 || size > 32768)
  4058. return -EINVAL;
  4059. while (size < conf->max_nr_stripes) {
  4060. if (drop_one_stripe(conf))
  4061. conf->max_nr_stripes--;
  4062. else
  4063. break;
  4064. }
  4065. err = md_allow_write(mddev);
  4066. if (err)
  4067. return err;
  4068. while (size > conf->max_nr_stripes) {
  4069. if (grow_one_stripe(conf))
  4070. conf->max_nr_stripes++;
  4071. else break;
  4072. }
  4073. return 0;
  4074. }
  4075. EXPORT_SYMBOL(raid5_set_cache_size);
  4076. static ssize_t
  4077. raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
  4078. {
  4079. struct r5conf *conf = mddev->private;
  4080. unsigned long new;
  4081. int err;
  4082. if (len >= PAGE_SIZE)
  4083. return -EINVAL;
  4084. if (!conf)
  4085. return -ENODEV;
  4086. if (strict_strtoul(page, 10, &new))
  4087. return -EINVAL;
  4088. err = raid5_set_cache_size(mddev, new);
  4089. if (err)
  4090. return err;
  4091. return len;
  4092. }
  4093. static struct md_sysfs_entry
  4094. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  4095. raid5_show_stripe_cache_size,
  4096. raid5_store_stripe_cache_size);
  4097. static ssize_t
  4098. raid5_show_preread_threshold(struct mddev *mddev, char *page)
  4099. {
  4100. struct r5conf *conf = mddev->private;
  4101. if (conf)
  4102. return sprintf(page, "%d\n", conf->bypass_threshold);
  4103. else
  4104. return 0;
  4105. }
  4106. static ssize_t
  4107. raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
  4108. {
  4109. struct r5conf *conf = mddev->private;
  4110. unsigned long new;
  4111. if (len >= PAGE_SIZE)
  4112. return -EINVAL;
  4113. if (!conf)
  4114. return -ENODEV;
  4115. if (strict_strtoul(page, 10, &new))
  4116. return -EINVAL;
  4117. if (new > conf->max_nr_stripes)
  4118. return -EINVAL;
  4119. conf->bypass_threshold = new;
  4120. return len;
  4121. }
  4122. static struct md_sysfs_entry
  4123. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  4124. S_IRUGO | S_IWUSR,
  4125. raid5_show_preread_threshold,
  4126. raid5_store_preread_threshold);
  4127. static ssize_t
  4128. stripe_cache_active_show(struct mddev *mddev, char *page)
  4129. {
  4130. struct r5conf *conf = mddev->private;
  4131. if (conf)
  4132. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  4133. else
  4134. return 0;
  4135. }
  4136. static struct md_sysfs_entry
  4137. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  4138. static struct attribute *raid5_attrs[] = {
  4139. &raid5_stripecache_size.attr,
  4140. &raid5_stripecache_active.attr,
  4141. &raid5_preread_bypass_threshold.attr,
  4142. NULL,
  4143. };
  4144. static struct attribute_group raid5_attrs_group = {
  4145. .name = NULL,
  4146. .attrs = raid5_attrs,
  4147. };
  4148. static sector_t
  4149. raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  4150. {
  4151. struct r5conf *conf = mddev->private;
  4152. if (!sectors)
  4153. sectors = mddev->dev_sectors;
  4154. if (!raid_disks)
  4155. /* size is defined by the smallest of previous and new size */
  4156. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  4157. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4158. sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
  4159. return sectors * (raid_disks - conf->max_degraded);
  4160. }
  4161. static void raid5_free_percpu(struct r5conf *conf)
  4162. {
  4163. struct raid5_percpu *percpu;
  4164. unsigned long cpu;
  4165. if (!conf->percpu)
  4166. return;
  4167. get_online_cpus();
  4168. for_each_possible_cpu(cpu) {
  4169. percpu = per_cpu_ptr(conf->percpu, cpu);
  4170. safe_put_page(percpu->spare_page);
  4171. kfree(percpu->scribble);
  4172. }
  4173. #ifdef CONFIG_HOTPLUG_CPU
  4174. unregister_cpu_notifier(&conf->cpu_notify);
  4175. #endif
  4176. put_online_cpus();
  4177. free_percpu(conf->percpu);
  4178. }
  4179. static void free_conf(struct r5conf *conf)
  4180. {
  4181. shrink_stripes(conf);
  4182. raid5_free_percpu(conf);
  4183. kfree(conf->disks);
  4184. kfree(conf->stripe_hashtbl);
  4185. kfree(conf);
  4186. }
  4187. #ifdef CONFIG_HOTPLUG_CPU
  4188. static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
  4189. void *hcpu)
  4190. {
  4191. struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
  4192. long cpu = (long)hcpu;
  4193. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  4194. switch (action) {
  4195. case CPU_UP_PREPARE:
  4196. case CPU_UP_PREPARE_FROZEN:
  4197. if (conf->level == 6 && !percpu->spare_page)
  4198. percpu->spare_page = alloc_page(GFP_KERNEL);
  4199. if (!percpu->scribble)
  4200. percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4201. if (!percpu->scribble ||
  4202. (conf->level == 6 && !percpu->spare_page)) {
  4203. safe_put_page(percpu->spare_page);
  4204. kfree(percpu->scribble);
  4205. pr_err("%s: failed memory allocation for cpu%ld\n",
  4206. __func__, cpu);
  4207. return notifier_from_errno(-ENOMEM);
  4208. }
  4209. break;
  4210. case CPU_DEAD:
  4211. case CPU_DEAD_FROZEN:
  4212. safe_put_page(percpu->spare_page);
  4213. kfree(percpu->scribble);
  4214. percpu->spare_page = NULL;
  4215. percpu->scribble = NULL;
  4216. break;
  4217. default:
  4218. break;
  4219. }
  4220. return NOTIFY_OK;
  4221. }
  4222. #endif
  4223. static int raid5_alloc_percpu(struct r5conf *conf)
  4224. {
  4225. unsigned long cpu;
  4226. struct page *spare_page;
  4227. struct raid5_percpu __percpu *allcpus;
  4228. void *scribble;
  4229. int err;
  4230. allcpus = alloc_percpu(struct raid5_percpu);
  4231. if (!allcpus)
  4232. return -ENOMEM;
  4233. conf->percpu = allcpus;
  4234. get_online_cpus();
  4235. err = 0;
  4236. for_each_present_cpu(cpu) {
  4237. if (conf->level == 6) {
  4238. spare_page = alloc_page(GFP_KERNEL);
  4239. if (!spare_page) {
  4240. err = -ENOMEM;
  4241. break;
  4242. }
  4243. per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
  4244. }
  4245. scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4246. if (!scribble) {
  4247. err = -ENOMEM;
  4248. break;
  4249. }
  4250. per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
  4251. }
  4252. #ifdef CONFIG_HOTPLUG_CPU
  4253. conf->cpu_notify.notifier_call = raid456_cpu_notify;
  4254. conf->cpu_notify.priority = 0;
  4255. if (err == 0)
  4256. err = register_cpu_notifier(&conf->cpu_notify);
  4257. #endif
  4258. put_online_cpus();
  4259. return err;
  4260. }
  4261. static struct r5conf *setup_conf(struct mddev *mddev)
  4262. {
  4263. struct r5conf *conf;
  4264. int raid_disk, memory, max_disks;
  4265. struct md_rdev *rdev;
  4266. struct disk_info *disk;
  4267. if (mddev->new_level != 5
  4268. && mddev->new_level != 4
  4269. && mddev->new_level != 6) {
  4270. printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
  4271. mdname(mddev), mddev->new_level);
  4272. return ERR_PTR(-EIO);
  4273. }
  4274. if ((mddev->new_level == 5
  4275. && !algorithm_valid_raid5(mddev->new_layout)) ||
  4276. (mddev->new_level == 6
  4277. && !algorithm_valid_raid6(mddev->new_layout))) {
  4278. printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
  4279. mdname(mddev), mddev->new_layout);
  4280. return ERR_PTR(-EIO);
  4281. }
  4282. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  4283. printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
  4284. mdname(mddev), mddev->raid_disks);
  4285. return ERR_PTR(-EINVAL);
  4286. }
  4287. if (!mddev->new_chunk_sectors ||
  4288. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  4289. !is_power_of_2(mddev->new_chunk_sectors)) {
  4290. printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
  4291. mdname(mddev), mddev->new_chunk_sectors << 9);
  4292. return ERR_PTR(-EINVAL);
  4293. }
  4294. conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
  4295. if (conf == NULL)
  4296. goto abort;
  4297. spin_lock_init(&conf->device_lock);
  4298. init_waitqueue_head(&conf->wait_for_stripe);
  4299. init_waitqueue_head(&conf->wait_for_overlap);
  4300. INIT_LIST_HEAD(&conf->handle_list);
  4301. INIT_LIST_HEAD(&conf->hold_list);
  4302. INIT_LIST_HEAD(&conf->delayed_list);
  4303. INIT_LIST_HEAD(&conf->bitmap_list);
  4304. INIT_LIST_HEAD(&conf->inactive_list);
  4305. atomic_set(&conf->active_stripes, 0);
  4306. atomic_set(&conf->preread_active_stripes, 0);
  4307. atomic_set(&conf->active_aligned_reads, 0);
  4308. conf->bypass_threshold = BYPASS_THRESHOLD;
  4309. conf->recovery_disabled = mddev->recovery_disabled - 1;
  4310. conf->raid_disks = mddev->raid_disks;
  4311. if (mddev->reshape_position == MaxSector)
  4312. conf->previous_raid_disks = mddev->raid_disks;
  4313. else
  4314. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  4315. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  4316. conf->scribble_len = scribble_len(max_disks);
  4317. conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
  4318. GFP_KERNEL);
  4319. if (!conf->disks)
  4320. goto abort;
  4321. conf->mddev = mddev;
  4322. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  4323. goto abort;
  4324. conf->level = mddev->new_level;
  4325. if (raid5_alloc_percpu(conf) != 0)
  4326. goto abort;
  4327. pr_debug("raid456: run(%s) called.\n", mdname(mddev));
  4328. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4329. raid_disk = rdev->raid_disk;
  4330. if (raid_disk >= max_disks
  4331. || raid_disk < 0)
  4332. continue;
  4333. disk = conf->disks + raid_disk;
  4334. if (test_bit(Replacement, &rdev->flags)) {
  4335. if (disk->replacement)
  4336. goto abort;
  4337. disk->replacement = rdev;
  4338. } else {
  4339. if (disk->rdev)
  4340. goto abort;
  4341. disk->rdev = rdev;
  4342. }
  4343. if (test_bit(In_sync, &rdev->flags)) {
  4344. char b[BDEVNAME_SIZE];
  4345. printk(KERN_INFO "md/raid:%s: device %s operational as raid"
  4346. " disk %d\n",
  4347. mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
  4348. } else if (rdev->saved_raid_disk != raid_disk)
  4349. /* Cannot rely on bitmap to complete recovery */
  4350. conf->fullsync = 1;
  4351. }
  4352. conf->chunk_sectors = mddev->new_chunk_sectors;
  4353. conf->level = mddev->new_level;
  4354. if (conf->level == 6)
  4355. conf->max_degraded = 2;
  4356. else
  4357. conf->max_degraded = 1;
  4358. conf->algorithm = mddev->new_layout;
  4359. conf->max_nr_stripes = NR_STRIPES;
  4360. conf->reshape_progress = mddev->reshape_position;
  4361. if (conf->reshape_progress != MaxSector) {
  4362. conf->prev_chunk_sectors = mddev->chunk_sectors;
  4363. conf->prev_algo = mddev->layout;
  4364. }
  4365. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  4366. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  4367. if (grow_stripes(conf, conf->max_nr_stripes)) {
  4368. printk(KERN_ERR
  4369. "md/raid:%s: couldn't allocate %dkB for buffers\n",
  4370. mdname(mddev), memory);
  4371. goto abort;
  4372. } else
  4373. printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
  4374. mdname(mddev), memory);
  4375. conf->thread = md_register_thread(raid5d, mddev, NULL);
  4376. if (!conf->thread) {
  4377. printk(KERN_ERR
  4378. "md/raid:%s: couldn't allocate thread.\n",
  4379. mdname(mddev));
  4380. goto abort;
  4381. }
  4382. return conf;
  4383. abort:
  4384. if (conf) {
  4385. free_conf(conf);
  4386. return ERR_PTR(-EIO);
  4387. } else
  4388. return ERR_PTR(-ENOMEM);
  4389. }
  4390. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  4391. {
  4392. switch (algo) {
  4393. case ALGORITHM_PARITY_0:
  4394. if (raid_disk < max_degraded)
  4395. return 1;
  4396. break;
  4397. case ALGORITHM_PARITY_N:
  4398. if (raid_disk >= raid_disks - max_degraded)
  4399. return 1;
  4400. break;
  4401. case ALGORITHM_PARITY_0_6:
  4402. if (raid_disk == 0 ||
  4403. raid_disk == raid_disks - 1)
  4404. return 1;
  4405. break;
  4406. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4407. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4408. case ALGORITHM_LEFT_SYMMETRIC_6:
  4409. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4410. if (raid_disk == raid_disks - 1)
  4411. return 1;
  4412. }
  4413. return 0;
  4414. }
  4415. static int run(struct mddev *mddev)
  4416. {
  4417. struct r5conf *conf;
  4418. int working_disks = 0;
  4419. int dirty_parity_disks = 0;
  4420. struct md_rdev *rdev;
  4421. sector_t reshape_offset = 0;
  4422. int i;
  4423. if (mddev->recovery_cp != MaxSector)
  4424. printk(KERN_NOTICE "md/raid:%s: not clean"
  4425. " -- starting background reconstruction\n",
  4426. mdname(mddev));
  4427. if (mddev->reshape_position != MaxSector) {
  4428. /* Check that we can continue the reshape.
  4429. * Currently only disks can change, it must
  4430. * increase, and we must be past the point where
  4431. * a stripe over-writes itself
  4432. */
  4433. sector_t here_new, here_old;
  4434. int old_disks;
  4435. int max_degraded = (mddev->level == 6 ? 2 : 1);
  4436. if (mddev->new_level != mddev->level) {
  4437. printk(KERN_ERR "md/raid:%s: unsupported reshape "
  4438. "required - aborting.\n",
  4439. mdname(mddev));
  4440. return -EINVAL;
  4441. }
  4442. old_disks = mddev->raid_disks - mddev->delta_disks;
  4443. /* reshape_position must be on a new-stripe boundary, and one
  4444. * further up in new geometry must map after here in old
  4445. * geometry.
  4446. */
  4447. here_new = mddev->reshape_position;
  4448. if (sector_div(here_new, mddev->new_chunk_sectors *
  4449. (mddev->raid_disks - max_degraded))) {
  4450. printk(KERN_ERR "md/raid:%s: reshape_position not "
  4451. "on a stripe boundary\n", mdname(mddev));
  4452. return -EINVAL;
  4453. }
  4454. reshape_offset = here_new * mddev->new_chunk_sectors;
  4455. /* here_new is the stripe we will write to */
  4456. here_old = mddev->reshape_position;
  4457. sector_div(here_old, mddev->chunk_sectors *
  4458. (old_disks-max_degraded));
  4459. /* here_old is the first stripe that we might need to read
  4460. * from */
  4461. if (mddev->delta_disks == 0) {
  4462. /* We cannot be sure it is safe to start an in-place
  4463. * reshape. It is only safe if user-space if monitoring
  4464. * and taking constant backups.
  4465. * mdadm always starts a situation like this in
  4466. * readonly mode so it can take control before
  4467. * allowing any writes. So just check for that.
  4468. */
  4469. if ((here_new * mddev->new_chunk_sectors !=
  4470. here_old * mddev->chunk_sectors) ||
  4471. mddev->ro == 0) {
  4472. printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
  4473. " in read-only mode - aborting\n",
  4474. mdname(mddev));
  4475. return -EINVAL;
  4476. }
  4477. } else if (mddev->delta_disks < 0
  4478. ? (here_new * mddev->new_chunk_sectors <=
  4479. here_old * mddev->chunk_sectors)
  4480. : (here_new * mddev->new_chunk_sectors >=
  4481. here_old * mddev->chunk_sectors)) {
  4482. /* Reading from the same stripe as writing to - bad */
  4483. printk(KERN_ERR "md/raid:%s: reshape_position too early for "
  4484. "auto-recovery - aborting.\n",
  4485. mdname(mddev));
  4486. return -EINVAL;
  4487. }
  4488. printk(KERN_INFO "md/raid:%s: reshape will continue\n",
  4489. mdname(mddev));
  4490. /* OK, we should be able to continue; */
  4491. } else {
  4492. BUG_ON(mddev->level != mddev->new_level);
  4493. BUG_ON(mddev->layout != mddev->new_layout);
  4494. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  4495. BUG_ON(mddev->delta_disks != 0);
  4496. }
  4497. if (mddev->private == NULL)
  4498. conf = setup_conf(mddev);
  4499. else
  4500. conf = mddev->private;
  4501. if (IS_ERR(conf))
  4502. return PTR_ERR(conf);
  4503. mddev->thread = conf->thread;
  4504. conf->thread = NULL;
  4505. mddev->private = conf;
  4506. for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
  4507. i++) {
  4508. rdev = conf->disks[i].rdev;
  4509. if (!rdev && conf->disks[i].replacement) {
  4510. /* The replacement is all we have yet */
  4511. rdev = conf->disks[i].replacement;
  4512. conf->disks[i].replacement = NULL;
  4513. clear_bit(Replacement, &rdev->flags);
  4514. conf->disks[i].rdev = rdev;
  4515. }
  4516. if (!rdev)
  4517. continue;
  4518. if (conf->disks[i].replacement &&
  4519. conf->reshape_progress != MaxSector) {
  4520. /* replacements and reshape simply do not mix. */
  4521. printk(KERN_ERR "md: cannot handle concurrent "
  4522. "replacement and reshape.\n");
  4523. goto abort;
  4524. }
  4525. if (test_bit(In_sync, &rdev->flags)) {
  4526. working_disks++;
  4527. continue;
  4528. }
  4529. /* This disc is not fully in-sync. However if it
  4530. * just stored parity (beyond the recovery_offset),
  4531. * when we don't need to be concerned about the
  4532. * array being dirty.
  4533. * When reshape goes 'backwards', we never have
  4534. * partially completed devices, so we only need
  4535. * to worry about reshape going forwards.
  4536. */
  4537. /* Hack because v0.91 doesn't store recovery_offset properly. */
  4538. if (mddev->major_version == 0 &&
  4539. mddev->minor_version > 90)
  4540. rdev->recovery_offset = reshape_offset;
  4541. if (rdev->recovery_offset < reshape_offset) {
  4542. /* We need to check old and new layout */
  4543. if (!only_parity(rdev->raid_disk,
  4544. conf->algorithm,
  4545. conf->raid_disks,
  4546. conf->max_degraded))
  4547. continue;
  4548. }
  4549. if (!only_parity(rdev->raid_disk,
  4550. conf->prev_algo,
  4551. conf->previous_raid_disks,
  4552. conf->max_degraded))
  4553. continue;
  4554. dirty_parity_disks++;
  4555. }
  4556. /*
  4557. * 0 for a fully functional array, 1 or 2 for a degraded array.
  4558. */
  4559. mddev->degraded = calc_degraded(conf);
  4560. if (has_failed(conf)) {
  4561. printk(KERN_ERR "md/raid:%s: not enough operational devices"
  4562. " (%d/%d failed)\n",
  4563. mdname(mddev), mddev->degraded, conf->raid_disks);
  4564. goto abort;
  4565. }
  4566. /* device size must be a multiple of chunk size */
  4567. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  4568. mddev->resync_max_sectors = mddev->dev_sectors;
  4569. if (mddev->degraded > dirty_parity_disks &&
  4570. mddev->recovery_cp != MaxSector) {
  4571. if (mddev->ok_start_degraded)
  4572. printk(KERN_WARNING
  4573. "md/raid:%s: starting dirty degraded array"
  4574. " - data corruption possible.\n",
  4575. mdname(mddev));
  4576. else {
  4577. printk(KERN_ERR
  4578. "md/raid:%s: cannot start dirty degraded array.\n",
  4579. mdname(mddev));
  4580. goto abort;
  4581. }
  4582. }
  4583. if (mddev->degraded == 0)
  4584. printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
  4585. " devices, algorithm %d\n", mdname(mddev), conf->level,
  4586. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  4587. mddev->new_layout);
  4588. else
  4589. printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
  4590. " out of %d devices, algorithm %d\n",
  4591. mdname(mddev), conf->level,
  4592. mddev->raid_disks - mddev->degraded,
  4593. mddev->raid_disks, mddev->new_layout);
  4594. print_raid5_conf(conf);
  4595. if (conf->reshape_progress != MaxSector) {
  4596. conf->reshape_safe = conf->reshape_progress;
  4597. atomic_set(&conf->reshape_stripes, 0);
  4598. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4599. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4600. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4601. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4602. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4603. "reshape");
  4604. }
  4605. /* Ok, everything is just fine now */
  4606. if (mddev->to_remove == &raid5_attrs_group)
  4607. mddev->to_remove = NULL;
  4608. else if (mddev->kobj.sd &&
  4609. sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  4610. printk(KERN_WARNING
  4611. "raid5: failed to create sysfs attributes for %s\n",
  4612. mdname(mddev));
  4613. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4614. if (mddev->queue) {
  4615. int chunk_size;
  4616. /* read-ahead size must cover two whole stripes, which
  4617. * is 2 * (datadisks) * chunksize where 'n' is the
  4618. * number of raid devices
  4619. */
  4620. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4621. int stripe = data_disks *
  4622. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  4623. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4624. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4625. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  4626. mddev->queue->backing_dev_info.congested_data = mddev;
  4627. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  4628. chunk_size = mddev->chunk_sectors << 9;
  4629. blk_queue_io_min(mddev->queue, chunk_size);
  4630. blk_queue_io_opt(mddev->queue, chunk_size *
  4631. (conf->raid_disks - conf->max_degraded));
  4632. list_for_each_entry(rdev, &mddev->disks, same_set)
  4633. disk_stack_limits(mddev->gendisk, rdev->bdev,
  4634. rdev->data_offset << 9);
  4635. }
  4636. return 0;
  4637. abort:
  4638. md_unregister_thread(&mddev->thread);
  4639. print_raid5_conf(conf);
  4640. free_conf(conf);
  4641. mddev->private = NULL;
  4642. printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
  4643. return -EIO;
  4644. }
  4645. static int stop(struct mddev *mddev)
  4646. {
  4647. struct r5conf *conf = mddev->private;
  4648. md_unregister_thread(&mddev->thread);
  4649. if (mddev->queue)
  4650. mddev->queue->backing_dev_info.congested_fn = NULL;
  4651. free_conf(conf);
  4652. mddev->private = NULL;
  4653. mddev->to_remove = &raid5_attrs_group;
  4654. return 0;
  4655. }
  4656. static void status(struct seq_file *seq, struct mddev *mddev)
  4657. {
  4658. struct r5conf *conf = mddev->private;
  4659. int i;
  4660. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  4661. mddev->chunk_sectors / 2, mddev->layout);
  4662. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  4663. for (i = 0; i < conf->raid_disks; i++)
  4664. seq_printf (seq, "%s",
  4665. conf->disks[i].rdev &&
  4666. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  4667. seq_printf (seq, "]");
  4668. }
  4669. static void print_raid5_conf (struct r5conf *conf)
  4670. {
  4671. int i;
  4672. struct disk_info *tmp;
  4673. printk(KERN_DEBUG "RAID conf printout:\n");
  4674. if (!conf) {
  4675. printk("(conf==NULL)\n");
  4676. return;
  4677. }
  4678. printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
  4679. conf->raid_disks,
  4680. conf->raid_disks - conf->mddev->degraded);
  4681. for (i = 0; i < conf->raid_disks; i++) {
  4682. char b[BDEVNAME_SIZE];
  4683. tmp = conf->disks + i;
  4684. if (tmp->rdev)
  4685. printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
  4686. i, !test_bit(Faulty, &tmp->rdev->flags),
  4687. bdevname(tmp->rdev->bdev, b));
  4688. }
  4689. }
  4690. static int raid5_spare_active(struct mddev *mddev)
  4691. {
  4692. int i;
  4693. struct r5conf *conf = mddev->private;
  4694. struct disk_info *tmp;
  4695. int count = 0;
  4696. unsigned long flags;
  4697. for (i = 0; i < conf->raid_disks; i++) {
  4698. tmp = conf->disks + i;
  4699. if (tmp->replacement
  4700. && tmp->replacement->recovery_offset == MaxSector
  4701. && !test_bit(Faulty, &tmp->replacement->flags)
  4702. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  4703. /* Replacement has just become active. */
  4704. if (!tmp->rdev
  4705. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  4706. count++;
  4707. if (tmp->rdev) {
  4708. /* Replaced device not technically faulty,
  4709. * but we need to be sure it gets removed
  4710. * and never re-added.
  4711. */
  4712. set_bit(Faulty, &tmp->rdev->flags);
  4713. sysfs_notify_dirent_safe(
  4714. tmp->rdev->sysfs_state);
  4715. }
  4716. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  4717. } else if (tmp->rdev
  4718. && tmp->rdev->recovery_offset == MaxSector
  4719. && !test_bit(Faulty, &tmp->rdev->flags)
  4720. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  4721. count++;
  4722. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  4723. }
  4724. }
  4725. spin_lock_irqsave(&conf->device_lock, flags);
  4726. mddev->degraded = calc_degraded(conf);
  4727. spin_unlock_irqrestore(&conf->device_lock, flags);
  4728. print_raid5_conf(conf);
  4729. return count;
  4730. }
  4731. static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  4732. {
  4733. struct r5conf *conf = mddev->private;
  4734. int err = 0;
  4735. int number = rdev->raid_disk;
  4736. struct md_rdev **rdevp;
  4737. struct disk_info *p = conf->disks + number;
  4738. print_raid5_conf(conf);
  4739. if (rdev == p->rdev)
  4740. rdevp = &p->rdev;
  4741. else if (rdev == p->replacement)
  4742. rdevp = &p->replacement;
  4743. else
  4744. return 0;
  4745. if (number >= conf->raid_disks &&
  4746. conf->reshape_progress == MaxSector)
  4747. clear_bit(In_sync, &rdev->flags);
  4748. if (test_bit(In_sync, &rdev->flags) ||
  4749. atomic_read(&rdev->nr_pending)) {
  4750. err = -EBUSY;
  4751. goto abort;
  4752. }
  4753. /* Only remove non-faulty devices if recovery
  4754. * isn't possible.
  4755. */
  4756. if (!test_bit(Faulty, &rdev->flags) &&
  4757. mddev->recovery_disabled != conf->recovery_disabled &&
  4758. !has_failed(conf) &&
  4759. (!p->replacement || p->replacement == rdev) &&
  4760. number < conf->raid_disks) {
  4761. err = -EBUSY;
  4762. goto abort;
  4763. }
  4764. *rdevp = NULL;
  4765. synchronize_rcu();
  4766. if (atomic_read(&rdev->nr_pending)) {
  4767. /* lost the race, try later */
  4768. err = -EBUSY;
  4769. *rdevp = rdev;
  4770. } else if (p->replacement) {
  4771. /* We must have just cleared 'rdev' */
  4772. p->rdev = p->replacement;
  4773. clear_bit(Replacement, &p->replacement->flags);
  4774. smp_mb(); /* Make sure other CPUs may see both as identical
  4775. * but will never see neither - if they are careful
  4776. */
  4777. p->replacement = NULL;
  4778. clear_bit(WantReplacement, &rdev->flags);
  4779. } else
  4780. /* We might have just removed the Replacement as faulty-
  4781. * clear the bit just in case
  4782. */
  4783. clear_bit(WantReplacement, &rdev->flags);
  4784. abort:
  4785. print_raid5_conf(conf);
  4786. return err;
  4787. }
  4788. static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  4789. {
  4790. struct r5conf *conf = mddev->private;
  4791. int err = -EEXIST;
  4792. int disk;
  4793. struct disk_info *p;
  4794. int first = 0;
  4795. int last = conf->raid_disks - 1;
  4796. if (mddev->recovery_disabled == conf->recovery_disabled)
  4797. return -EBUSY;
  4798. if (has_failed(conf))
  4799. /* no point adding a device */
  4800. return -EINVAL;
  4801. if (rdev->raid_disk >= 0)
  4802. first = last = rdev->raid_disk;
  4803. /*
  4804. * find the disk ... but prefer rdev->saved_raid_disk
  4805. * if possible.
  4806. */
  4807. if (rdev->saved_raid_disk >= 0 &&
  4808. rdev->saved_raid_disk >= first &&
  4809. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  4810. disk = rdev->saved_raid_disk;
  4811. else
  4812. disk = first;
  4813. for ( ; disk <= last ; disk++) {
  4814. p = conf->disks + disk;
  4815. if (p->rdev == NULL) {
  4816. clear_bit(In_sync, &rdev->flags);
  4817. rdev->raid_disk = disk;
  4818. err = 0;
  4819. if (rdev->saved_raid_disk != disk)
  4820. conf->fullsync = 1;
  4821. rcu_assign_pointer(p->rdev, rdev);
  4822. break;
  4823. }
  4824. if (test_bit(WantReplacement, &p->rdev->flags) &&
  4825. p->replacement == NULL) {
  4826. clear_bit(In_sync, &rdev->flags);
  4827. set_bit(Replacement, &rdev->flags);
  4828. rdev->raid_disk = disk;
  4829. err = 0;
  4830. conf->fullsync = 1;
  4831. rcu_assign_pointer(p->replacement, rdev);
  4832. break;
  4833. }
  4834. }
  4835. print_raid5_conf(conf);
  4836. return err;
  4837. }
  4838. static int raid5_resize(struct mddev *mddev, sector_t sectors)
  4839. {
  4840. /* no resync is happening, and there is enough space
  4841. * on all devices, so we can resize.
  4842. * We need to make sure resync covers any new space.
  4843. * If the array is shrinking we should possibly wait until
  4844. * any io in the removed space completes, but it hardly seems
  4845. * worth it.
  4846. */
  4847. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4848. md_set_array_sectors(mddev, raid5_size(mddev, sectors,
  4849. mddev->raid_disks));
  4850. if (mddev->array_sectors >
  4851. raid5_size(mddev, sectors, mddev->raid_disks))
  4852. return -EINVAL;
  4853. set_capacity(mddev->gendisk, mddev->array_sectors);
  4854. revalidate_disk(mddev->gendisk);
  4855. if (sectors > mddev->dev_sectors &&
  4856. mddev->recovery_cp > mddev->dev_sectors) {
  4857. mddev->recovery_cp = mddev->dev_sectors;
  4858. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4859. }
  4860. mddev->dev_sectors = sectors;
  4861. mddev->resync_max_sectors = sectors;
  4862. return 0;
  4863. }
  4864. static int check_stripe_cache(struct mddev *mddev)
  4865. {
  4866. /* Can only proceed if there are plenty of stripe_heads.
  4867. * We need a minimum of one full stripe,, and for sensible progress
  4868. * it is best to have about 4 times that.
  4869. * If we require 4 times, then the default 256 4K stripe_heads will
  4870. * allow for chunk sizes up to 256K, which is probably OK.
  4871. * If the chunk size is greater, user-space should request more
  4872. * stripe_heads first.
  4873. */
  4874. struct r5conf *conf = mddev->private;
  4875. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  4876. > conf->max_nr_stripes ||
  4877. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  4878. > conf->max_nr_stripes) {
  4879. printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
  4880. mdname(mddev),
  4881. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  4882. / STRIPE_SIZE)*4);
  4883. return 0;
  4884. }
  4885. return 1;
  4886. }
  4887. static int check_reshape(struct mddev *mddev)
  4888. {
  4889. struct r5conf *conf = mddev->private;
  4890. if (mddev->delta_disks == 0 &&
  4891. mddev->new_layout == mddev->layout &&
  4892. mddev->new_chunk_sectors == mddev->chunk_sectors)
  4893. return 0; /* nothing to do */
  4894. if (mddev->bitmap)
  4895. /* Cannot grow a bitmap yet */
  4896. return -EBUSY;
  4897. if (has_failed(conf))
  4898. return -EINVAL;
  4899. if (mddev->delta_disks < 0) {
  4900. /* We might be able to shrink, but the devices must
  4901. * be made bigger first.
  4902. * For raid6, 4 is the minimum size.
  4903. * Otherwise 2 is the minimum
  4904. */
  4905. int min = 2;
  4906. if (mddev->level == 6)
  4907. min = 4;
  4908. if (mddev->raid_disks + mddev->delta_disks < min)
  4909. return -EINVAL;
  4910. }
  4911. if (!check_stripe_cache(mddev))
  4912. return -ENOSPC;
  4913. return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4914. }
  4915. static int raid5_start_reshape(struct mddev *mddev)
  4916. {
  4917. struct r5conf *conf = mddev->private;
  4918. struct md_rdev *rdev;
  4919. int spares = 0;
  4920. unsigned long flags;
  4921. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4922. return -EBUSY;
  4923. if (!check_stripe_cache(mddev))
  4924. return -ENOSPC;
  4925. list_for_each_entry(rdev, &mddev->disks, same_set)
  4926. if (!test_bit(In_sync, &rdev->flags)
  4927. && !test_bit(Faulty, &rdev->flags))
  4928. spares++;
  4929. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4930. /* Not enough devices even to make a degraded array
  4931. * of that size
  4932. */
  4933. return -EINVAL;
  4934. /* Refuse to reduce size of the array. Any reductions in
  4935. * array size must be through explicit setting of array_size
  4936. * attribute.
  4937. */
  4938. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  4939. < mddev->array_sectors) {
  4940. printk(KERN_ERR "md/raid:%s: array size must be reduced "
  4941. "before number of disks\n", mdname(mddev));
  4942. return -EINVAL;
  4943. }
  4944. atomic_set(&conf->reshape_stripes, 0);
  4945. spin_lock_irq(&conf->device_lock);
  4946. conf->previous_raid_disks = conf->raid_disks;
  4947. conf->raid_disks += mddev->delta_disks;
  4948. conf->prev_chunk_sectors = conf->chunk_sectors;
  4949. conf->chunk_sectors = mddev->new_chunk_sectors;
  4950. conf->prev_algo = conf->algorithm;
  4951. conf->algorithm = mddev->new_layout;
  4952. if (mddev->delta_disks < 0)
  4953. conf->reshape_progress = raid5_size(mddev, 0, 0);
  4954. else
  4955. conf->reshape_progress = 0;
  4956. conf->reshape_safe = conf->reshape_progress;
  4957. conf->generation++;
  4958. spin_unlock_irq(&conf->device_lock);
  4959. /* Add some new drives, as many as will fit.
  4960. * We know there are enough to make the newly sized array work.
  4961. * Don't add devices if we are reducing the number of
  4962. * devices in the array. This is because it is not possible
  4963. * to correctly record the "partially reconstructed" state of
  4964. * such devices during the reshape and confusion could result.
  4965. */
  4966. if (mddev->delta_disks >= 0) {
  4967. int added_devices = 0;
  4968. list_for_each_entry(rdev, &mddev->disks, same_set)
  4969. if (rdev->raid_disk < 0 &&
  4970. !test_bit(Faulty, &rdev->flags)) {
  4971. if (raid5_add_disk(mddev, rdev) == 0) {
  4972. if (rdev->raid_disk
  4973. >= conf->previous_raid_disks) {
  4974. set_bit(In_sync, &rdev->flags);
  4975. added_devices++;
  4976. } else
  4977. rdev->recovery_offset = 0;
  4978. if (sysfs_link_rdev(mddev, rdev))
  4979. /* Failure here is OK */;
  4980. }
  4981. } else if (rdev->raid_disk >= conf->previous_raid_disks
  4982. && !test_bit(Faulty, &rdev->flags)) {
  4983. /* This is a spare that was manually added */
  4984. set_bit(In_sync, &rdev->flags);
  4985. added_devices++;
  4986. }
  4987. /* When a reshape changes the number of devices,
  4988. * ->degraded is measured against the larger of the
  4989. * pre and post number of devices.
  4990. */
  4991. spin_lock_irqsave(&conf->device_lock, flags);
  4992. mddev->degraded = calc_degraded(conf);
  4993. spin_unlock_irqrestore(&conf->device_lock, flags);
  4994. }
  4995. mddev->raid_disks = conf->raid_disks;
  4996. mddev->reshape_position = conf->reshape_progress;
  4997. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4998. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4999. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  5000. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  5001. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  5002. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  5003. "reshape");
  5004. if (!mddev->sync_thread) {
  5005. mddev->recovery = 0;
  5006. spin_lock_irq(&conf->device_lock);
  5007. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  5008. conf->reshape_progress = MaxSector;
  5009. spin_unlock_irq(&conf->device_lock);
  5010. return -EAGAIN;
  5011. }
  5012. conf->reshape_checkpoint = jiffies;
  5013. md_wakeup_thread(mddev->sync_thread);
  5014. md_new_event(mddev);
  5015. return 0;
  5016. }
  5017. /* This is called from the reshape thread and should make any
  5018. * changes needed in 'conf'
  5019. */
  5020. static void end_reshape(struct r5conf *conf)
  5021. {
  5022. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  5023. spin_lock_irq(&conf->device_lock);
  5024. conf->previous_raid_disks = conf->raid_disks;
  5025. conf->reshape_progress = MaxSector;
  5026. spin_unlock_irq(&conf->device_lock);
  5027. wake_up(&conf->wait_for_overlap);
  5028. /* read-ahead size must cover two whole stripes, which is
  5029. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  5030. */
  5031. if (conf->mddev->queue) {
  5032. int data_disks = conf->raid_disks - conf->max_degraded;
  5033. int stripe = data_disks * ((conf->chunk_sectors << 9)
  5034. / PAGE_SIZE);
  5035. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  5036. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  5037. }
  5038. }
  5039. }
  5040. /* This is called from the raid5d thread with mddev_lock held.
  5041. * It makes config changes to the device.
  5042. */
  5043. static void raid5_finish_reshape(struct mddev *mddev)
  5044. {
  5045. struct r5conf *conf = mddev->private;
  5046. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  5047. if (mddev->delta_disks > 0) {
  5048. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  5049. set_capacity(mddev->gendisk, mddev->array_sectors);
  5050. revalidate_disk(mddev->gendisk);
  5051. } else {
  5052. int d;
  5053. spin_lock_irq(&conf->device_lock);
  5054. mddev->degraded = calc_degraded(conf);
  5055. spin_unlock_irq(&conf->device_lock);
  5056. for (d = conf->raid_disks ;
  5057. d < conf->raid_disks - mddev->delta_disks;
  5058. d++) {
  5059. struct md_rdev *rdev = conf->disks[d].rdev;
  5060. if (rdev &&
  5061. raid5_remove_disk(mddev, rdev) == 0) {
  5062. sysfs_unlink_rdev(mddev, rdev);
  5063. rdev->raid_disk = -1;
  5064. }
  5065. }
  5066. }
  5067. mddev->layout = conf->algorithm;
  5068. mddev->chunk_sectors = conf->chunk_sectors;
  5069. mddev->reshape_position = MaxSector;
  5070. mddev->delta_disks = 0;
  5071. }
  5072. }
  5073. static void raid5_quiesce(struct mddev *mddev, int state)
  5074. {
  5075. struct r5conf *conf = mddev->private;
  5076. switch(state) {
  5077. case 2: /* resume for a suspend */
  5078. wake_up(&conf->wait_for_overlap);
  5079. break;
  5080. case 1: /* stop all writes */
  5081. spin_lock_irq(&conf->device_lock);
  5082. /* '2' tells resync/reshape to pause so that all
  5083. * active stripes can drain
  5084. */
  5085. conf->quiesce = 2;
  5086. wait_event_lock_irq(conf->wait_for_stripe,
  5087. atomic_read(&conf->active_stripes) == 0 &&
  5088. atomic_read(&conf->active_aligned_reads) == 0,
  5089. conf->device_lock, /* nothing */);
  5090. conf->quiesce = 1;
  5091. spin_unlock_irq(&conf->device_lock);
  5092. /* allow reshape to continue */
  5093. wake_up(&conf->wait_for_overlap);
  5094. break;
  5095. case 0: /* re-enable writes */
  5096. spin_lock_irq(&conf->device_lock);
  5097. conf->quiesce = 0;
  5098. wake_up(&conf->wait_for_stripe);
  5099. wake_up(&conf->wait_for_overlap);
  5100. spin_unlock_irq(&conf->device_lock);
  5101. break;
  5102. }
  5103. }
  5104. static void *raid45_takeover_raid0(struct mddev *mddev, int level)
  5105. {
  5106. struct r0conf *raid0_conf = mddev->private;
  5107. sector_t sectors;
  5108. /* for raid0 takeover only one zone is supported */
  5109. if (raid0_conf->nr_strip_zones > 1) {
  5110. printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
  5111. mdname(mddev));
  5112. return ERR_PTR(-EINVAL);
  5113. }
  5114. sectors = raid0_conf->strip_zone[0].zone_end;
  5115. sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
  5116. mddev->dev_sectors = sectors;
  5117. mddev->new_level = level;
  5118. mddev->new_layout = ALGORITHM_PARITY_N;
  5119. mddev->new_chunk_sectors = mddev->chunk_sectors;
  5120. mddev->raid_disks += 1;
  5121. mddev->delta_disks = 1;
  5122. /* make sure it will be not marked as dirty */
  5123. mddev->recovery_cp = MaxSector;
  5124. return setup_conf(mddev);
  5125. }
  5126. static void *raid5_takeover_raid1(struct mddev *mddev)
  5127. {
  5128. int chunksect;
  5129. if (mddev->raid_disks != 2 ||
  5130. mddev->degraded > 1)
  5131. return ERR_PTR(-EINVAL);
  5132. /* Should check if there are write-behind devices? */
  5133. chunksect = 64*2; /* 64K by default */
  5134. /* The array must be an exact multiple of chunksize */
  5135. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  5136. chunksect >>= 1;
  5137. if ((chunksect<<9) < STRIPE_SIZE)
  5138. /* array size does not allow a suitable chunk size */
  5139. return ERR_PTR(-EINVAL);
  5140. mddev->new_level = 5;
  5141. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5142. mddev->new_chunk_sectors = chunksect;
  5143. return setup_conf(mddev);
  5144. }
  5145. static void *raid5_takeover_raid6(struct mddev *mddev)
  5146. {
  5147. int new_layout;
  5148. switch (mddev->layout) {
  5149. case ALGORITHM_LEFT_ASYMMETRIC_6:
  5150. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  5151. break;
  5152. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  5153. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  5154. break;
  5155. case ALGORITHM_LEFT_SYMMETRIC_6:
  5156. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5157. break;
  5158. case ALGORITHM_RIGHT_SYMMETRIC_6:
  5159. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  5160. break;
  5161. case ALGORITHM_PARITY_0_6:
  5162. new_layout = ALGORITHM_PARITY_0;
  5163. break;
  5164. case ALGORITHM_PARITY_N:
  5165. new_layout = ALGORITHM_PARITY_N;
  5166. break;
  5167. default:
  5168. return ERR_PTR(-EINVAL);
  5169. }
  5170. mddev->new_level = 5;
  5171. mddev->new_layout = new_layout;
  5172. mddev->delta_disks = -1;
  5173. mddev->raid_disks -= 1;
  5174. return setup_conf(mddev);
  5175. }
  5176. static int raid5_check_reshape(struct mddev *mddev)
  5177. {
  5178. /* For a 2-drive array, the layout and chunk size can be changed
  5179. * immediately as not restriping is needed.
  5180. * For larger arrays we record the new value - after validation
  5181. * to be used by a reshape pass.
  5182. */
  5183. struct r5conf *conf = mddev->private;
  5184. int new_chunk = mddev->new_chunk_sectors;
  5185. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  5186. return -EINVAL;
  5187. if (new_chunk > 0) {
  5188. if (!is_power_of_2(new_chunk))
  5189. return -EINVAL;
  5190. if (new_chunk < (PAGE_SIZE>>9))
  5191. return -EINVAL;
  5192. if (mddev->array_sectors & (new_chunk-1))
  5193. /* not factor of array size */
  5194. return -EINVAL;
  5195. }
  5196. /* They look valid */
  5197. if (mddev->raid_disks == 2) {
  5198. /* can make the change immediately */
  5199. if (mddev->new_layout >= 0) {
  5200. conf->algorithm = mddev->new_layout;
  5201. mddev->layout = mddev->new_layout;
  5202. }
  5203. if (new_chunk > 0) {
  5204. conf->chunk_sectors = new_chunk ;
  5205. mddev->chunk_sectors = new_chunk;
  5206. }
  5207. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5208. md_wakeup_thread(mddev->thread);
  5209. }
  5210. return check_reshape(mddev);
  5211. }
  5212. static int raid6_check_reshape(struct mddev *mddev)
  5213. {
  5214. int new_chunk = mddev->new_chunk_sectors;
  5215. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  5216. return -EINVAL;
  5217. if (new_chunk > 0) {
  5218. if (!is_power_of_2(new_chunk))
  5219. return -EINVAL;
  5220. if (new_chunk < (PAGE_SIZE >> 9))
  5221. return -EINVAL;
  5222. if (mddev->array_sectors & (new_chunk-1))
  5223. /* not factor of array size */
  5224. return -EINVAL;
  5225. }
  5226. /* They look valid */
  5227. return check_reshape(mddev);
  5228. }
  5229. static void *raid5_takeover(struct mddev *mddev)
  5230. {
  5231. /* raid5 can take over:
  5232. * raid0 - if there is only one strip zone - make it a raid4 layout
  5233. * raid1 - if there are two drives. We need to know the chunk size
  5234. * raid4 - trivial - just use a raid4 layout.
  5235. * raid6 - Providing it is a *_6 layout
  5236. */
  5237. if (mddev->level == 0)
  5238. return raid45_takeover_raid0(mddev, 5);
  5239. if (mddev->level == 1)
  5240. return raid5_takeover_raid1(mddev);
  5241. if (mddev->level == 4) {
  5242. mddev->new_layout = ALGORITHM_PARITY_N;
  5243. mddev->new_level = 5;
  5244. return setup_conf(mddev);
  5245. }
  5246. if (mddev->level == 6)
  5247. return raid5_takeover_raid6(mddev);
  5248. return ERR_PTR(-EINVAL);
  5249. }
  5250. static void *raid4_takeover(struct mddev *mddev)
  5251. {
  5252. /* raid4 can take over:
  5253. * raid0 - if there is only one strip zone
  5254. * raid5 - if layout is right
  5255. */
  5256. if (mddev->level == 0)
  5257. return raid45_takeover_raid0(mddev, 4);
  5258. if (mddev->level == 5 &&
  5259. mddev->layout == ALGORITHM_PARITY_N) {
  5260. mddev->new_layout = 0;
  5261. mddev->new_level = 4;
  5262. return setup_conf(mddev);
  5263. }
  5264. return ERR_PTR(-EINVAL);
  5265. }
  5266. static struct md_personality raid5_personality;
  5267. static void *raid6_takeover(struct mddev *mddev)
  5268. {
  5269. /* Currently can only take over a raid5. We map the
  5270. * personality to an equivalent raid6 personality
  5271. * with the Q block at the end.
  5272. */
  5273. int new_layout;
  5274. if (mddev->pers != &raid5_personality)
  5275. return ERR_PTR(-EINVAL);
  5276. if (mddev->degraded > 1)
  5277. return ERR_PTR(-EINVAL);
  5278. if (mddev->raid_disks > 253)
  5279. return ERR_PTR(-EINVAL);
  5280. if (mddev->raid_disks < 3)
  5281. return ERR_PTR(-EINVAL);
  5282. switch (mddev->layout) {
  5283. case ALGORITHM_LEFT_ASYMMETRIC:
  5284. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  5285. break;
  5286. case ALGORITHM_RIGHT_ASYMMETRIC:
  5287. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  5288. break;
  5289. case ALGORITHM_LEFT_SYMMETRIC:
  5290. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  5291. break;
  5292. case ALGORITHM_RIGHT_SYMMETRIC:
  5293. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  5294. break;
  5295. case ALGORITHM_PARITY_0:
  5296. new_layout = ALGORITHM_PARITY_0_6;
  5297. break;
  5298. case ALGORITHM_PARITY_N:
  5299. new_layout = ALGORITHM_PARITY_N;
  5300. break;
  5301. default:
  5302. return ERR_PTR(-EINVAL);
  5303. }
  5304. mddev->new_level = 6;
  5305. mddev->new_layout = new_layout;
  5306. mddev->delta_disks = 1;
  5307. mddev->raid_disks += 1;
  5308. return setup_conf(mddev);
  5309. }
  5310. static struct md_personality raid6_personality =
  5311. {
  5312. .name = "raid6",
  5313. .level = 6,
  5314. .owner = THIS_MODULE,
  5315. .make_request = make_request,
  5316. .run = run,
  5317. .stop = stop,
  5318. .status = status,
  5319. .error_handler = error,
  5320. .hot_add_disk = raid5_add_disk,
  5321. .hot_remove_disk= raid5_remove_disk,
  5322. .spare_active = raid5_spare_active,
  5323. .sync_request = sync_request,
  5324. .resize = raid5_resize,
  5325. .size = raid5_size,
  5326. .check_reshape = raid6_check_reshape,
  5327. .start_reshape = raid5_start_reshape,
  5328. .finish_reshape = raid5_finish_reshape,
  5329. .quiesce = raid5_quiesce,
  5330. .takeover = raid6_takeover,
  5331. };
  5332. static struct md_personality raid5_personality =
  5333. {
  5334. .name = "raid5",
  5335. .level = 5,
  5336. .owner = THIS_MODULE,
  5337. .make_request = make_request,
  5338. .run = run,
  5339. .stop = stop,
  5340. .status = status,
  5341. .error_handler = error,
  5342. .hot_add_disk = raid5_add_disk,
  5343. .hot_remove_disk= raid5_remove_disk,
  5344. .spare_active = raid5_spare_active,
  5345. .sync_request = sync_request,
  5346. .resize = raid5_resize,
  5347. .size = raid5_size,
  5348. .check_reshape = raid5_check_reshape,
  5349. .start_reshape = raid5_start_reshape,
  5350. .finish_reshape = raid5_finish_reshape,
  5351. .quiesce = raid5_quiesce,
  5352. .takeover = raid5_takeover,
  5353. };
  5354. static struct md_personality raid4_personality =
  5355. {
  5356. .name = "raid4",
  5357. .level = 4,
  5358. .owner = THIS_MODULE,
  5359. .make_request = make_request,
  5360. .run = run,
  5361. .stop = stop,
  5362. .status = status,
  5363. .error_handler = error,
  5364. .hot_add_disk = raid5_add_disk,
  5365. .hot_remove_disk= raid5_remove_disk,
  5366. .spare_active = raid5_spare_active,
  5367. .sync_request = sync_request,
  5368. .resize = raid5_resize,
  5369. .size = raid5_size,
  5370. .check_reshape = raid5_check_reshape,
  5371. .start_reshape = raid5_start_reshape,
  5372. .finish_reshape = raid5_finish_reshape,
  5373. .quiesce = raid5_quiesce,
  5374. .takeover = raid4_takeover,
  5375. };
  5376. static int __init raid5_init(void)
  5377. {
  5378. register_md_personality(&raid6_personality);
  5379. register_md_personality(&raid5_personality);
  5380. register_md_personality(&raid4_personality);
  5381. return 0;
  5382. }
  5383. static void raid5_exit(void)
  5384. {
  5385. unregister_md_personality(&raid6_personality);
  5386. unregister_md_personality(&raid5_personality);
  5387. unregister_md_personality(&raid4_personality);
  5388. }
  5389. module_init(raid5_init);
  5390. module_exit(raid5_exit);
  5391. MODULE_LICENSE("GPL");
  5392. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  5393. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  5394. MODULE_ALIAS("md-raid5");
  5395. MODULE_ALIAS("md-raid4");
  5396. MODULE_ALIAS("md-level-5");
  5397. MODULE_ALIAS("md-level-4");
  5398. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  5399. MODULE_ALIAS("md-raid6");
  5400. MODULE_ALIAS("md-level-6");
  5401. /* This used to be two separate modules, they were: */
  5402. MODULE_ALIAS("raid5");
  5403. MODULE_ALIAS("raid6");