raid5.c 182 KB

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