raid5.c 179 KB

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