raid5.c 159 KB

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