raid5.c 164 KB

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