raid5.c 182 KB

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